2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/autoconf.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
58 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
61 /* use of last_frames[index].can_dlc */
62 #define RX_RECV 0x40 /* received data for this element */
63 #define RX_THR 0x80 /* element not been sent due to throttle feature */
64 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
66 /* get best masking value for can_rx_register() for a given single can_id */
67 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
71 static __initdata const char banner[] = KERN_INFO
72 "CAN: broadcast manager (bcm) socket protocol " CAN_VERSION "\n";
74 MODULE_DESCRIPTION("PF_CAN bcm sockets");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
78 #ifdef CONFIG_CAN_DEBUG_CORE
80 module_param(debug, int, S_IRUGO);
81 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
84 #define GET_U64(p) (*(u64*)(p)->data) /* easy access */
87 struct list_head list;
91 unsigned long j_ival1, j_ival2, j_lastmsg;
92 unsigned long frames_abs, frames_filtered;
93 struct timer_list timer, thrtimer;
94 struct timeval ival1, ival2;
95 struct timeval rx_stamp;
100 struct can_frame *frames;
101 struct can_frame *last_frames;
108 struct list_head rx_ops;
109 struct list_head tx_ops;
110 unsigned long dropped_usr_msgs;
111 struct proc_dir_entry *bcm_proc_read;
112 char procname [9]; /* pointer printed in ASCII with \0 */
115 static struct proc_dir_entry *proc_dir = NULL;
117 static int bcm_init(struct sock *sk);
118 static void bcm_notifier(unsigned long msg, void *data);
119 static int bcm_release(struct socket *sock);
120 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
122 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
123 struct msghdr *msg, size_t size);
124 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
125 struct msghdr *msg, size_t size, int flags);
126 static unsigned int bcm_poll(struct file *file, struct socket *sock,
129 static int bcm_read_proc(char *page, char **start, off_t off,
130 int count, int *eof, void *data);
132 static void bcm_tx_timeout_handler(unsigned long data);
133 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
134 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
135 int ifindex, struct sock *sk);
136 static void bcm_can_tx(struct bcm_op *op);
138 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
139 int ifindex, struct sock *sk);
140 static void bcm_rx_handler(struct sk_buff *skb, void *op);
141 static void bcm_rx_timeout_handler(unsigned long data);
142 static void bcm_rx_thr_handler(unsigned long data);
143 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
144 struct can_frame *rxdata);
145 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
146 static void bcm_rx_starttimer(struct bcm_op *op);
147 static void bcm_rx_update_and_send(struct bcm_op *op,
148 struct can_frame *lastdata,
149 struct can_frame *rxdata);
150 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
151 struct can_frame *frames, struct timeval *tv);
153 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id,
155 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id,
157 static void bcm_remove_op(struct bcm_op *op);
158 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
160 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
163 static struct proto_ops bcm_ops = {
165 .release = bcm_release,
166 .bind = sock_no_bind,
167 .connect = bcm_connect,
168 .socketpair = sock_no_socketpair,
169 .accept = sock_no_accept,
170 .getname = sock_no_getname,
172 .ioctl = NULL, /* use can_ioctl() from af_can.c */
173 .listen = sock_no_listen,
174 .shutdown = sock_no_shutdown,
175 .setsockopt = sock_no_setsockopt,
176 .getsockopt = sock_no_getsockopt,
177 .sendmsg = bcm_sendmsg,
178 .recvmsg = bcm_recvmsg,
179 .mmap = sock_no_mmap,
180 .sendpage = sock_no_sendpage,
183 #ifdef CONFIG_CAN_BCM_USER
186 #define BCM_CAP CAP_NET_RAW
189 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
195 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
197 static struct proto bcm_proto = {
199 .owner = THIS_MODULE,
200 .obj_size = sizeof(struct bcm_sock),
204 static struct can_proto bcm_can_proto = {
207 .capability = BCM_CAP,
212 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
214 static struct can_proto bcm_can_proto = {
217 .capability = BCM_CAP,
219 .owner = THIS_MODULE,
220 .obj_size = sizeof(struct bcm_opt),
225 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
226 static void *kzalloc(size_t size, unsigned int __nocast flags)
228 void *ret = kmalloc(size, flags);
230 memset(ret, 0, size);
234 static inline void skb_get_timestamp(const struct sk_buff *skb,
235 struct timeval *stamp)
237 stamp->tv_sec = skb->stamp.tv_sec;
238 stamp->tv_usec = skb->stamp.tv_usec;
241 static inline void skb_set_timestamp(struct sk_buff *skb,
242 const struct timeval *stamp)
244 skb->stamp.tv_sec = stamp->tv_sec;
245 skb->stamp.tv_usec = stamp->tv_usec;
249 #define CFSIZ sizeof(struct can_frame)
250 #define OPSIZ sizeof(struct bcm_op)
251 #define MHSIZ sizeof(struct bcm_msg_head)
253 static int __init bcm_module_init(void)
257 can_proto_register(&bcm_can_proto);
259 /* create /proc/net/can/bcm directory */
260 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
263 proc_dir->owner = THIS_MODULE;
268 static void __exit bcm_module_exit(void)
270 can_proto_unregister(&bcm_can_proto);
273 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
277 /* initial settings at socket creation time */
279 static int bcm_init(struct sock *sk)
281 struct bcm_opt *bo = bcm_sk(sk);
285 bo->dropped_usr_msgs = 0;
286 bo->bcm_proc_read = NULL;
288 INIT_LIST_HEAD(&bo->tx_ops);
289 INIT_LIST_HEAD(&bo->rx_ops);
294 /* handling of netdevice problems */
296 static void bcm_notifier(unsigned long msg, void *data)
298 struct sock *sk = (struct sock *)data;
299 struct bcm_opt *bo = bcm_sk(sk);
301 DBG("called for sock %p\n", sk);
304 case NETDEV_UNREGISTER:
309 sk->sk_err = ENETDOWN;
310 if (!sock_flag(sk, SOCK_DEAD))
311 sk->sk_error_report(sk);
315 /* standard socket functions */
317 static int bcm_release(struct socket *sock)
319 struct sock *sk = sock->sk;
320 struct bcm_opt *bo = bcm_sk(sk);
321 struct bcm_op *op, *next;
323 DBG("socket %p, sk %p\n", sock, sk);
325 /* remove bcm_ops, timer, rx_unregister(), etc. */
327 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
328 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
332 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
333 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
336 * Don't care if we're bound or not (due to netdev problems)
337 * can_rx_unregister() is always a save thing to do here.
340 struct net_device *dev = dev_get_by_index(op->ifindex);
343 can_rx_unregister(dev, op->can_id,
349 can_rx_unregister(NULL, op->can_id,
356 /* remove procfs entry */
357 if ((proc_dir) && (bo->bcm_proc_read)) {
358 remove_proc_entry(bo->procname, proc_dir);
361 /* remove device notifier */
363 struct net_device *dev = dev_get_by_index(bo->ifindex);
366 can_dev_unregister(dev, bcm_notifier, sk);
376 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
379 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
380 struct sock *sk = sock->sk;
381 struct bcm_opt *bo = bcm_sk(sk);
386 /* bind a device to this socket */
387 if (addr->can_ifindex) {
388 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
391 DBG("could not find device index %d\n",
395 bo->ifindex = dev->ifindex;
396 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
399 DBG("socket %p bound to device %s (idx %d)\n",
400 sock, dev->name, dev->ifindex);
402 /* no notifier for ifindex = 0 ('any' CAN device) */
409 /* unique socket address as filename */
410 sprintf(bo->procname, "%p", sock);
411 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
419 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
420 struct msghdr *msg, size_t size)
422 struct sock *sk = sock->sk;
423 struct bcm_opt *bo = bcm_sk(sk);
424 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
425 struct bcm_msg_head msg_head;
426 int ret; /* read bytes or error codes as return value */
429 DBG("sock %p not bound\n", sk);
433 /* check for alternative ifindex for this bcm_op */
435 if (!ifindex && msg->msg_name) {
436 /* no bound device as default => check msg_name */
437 struct sockaddr_can *addr =
438 (struct sockaddr_can *)msg->msg_name;
440 if (addr->can_family != AF_CAN)
443 ifindex = addr->can_ifindex; /* ifindex from sendto() */
445 if (ifindex && !dev_get_by_index(ifindex)) {
446 DBG("device %d not found\n", ifindex);
451 /* read message head information */
453 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
457 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
459 switch (msg_head.opcode) {
463 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
468 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
473 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
481 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
489 /* reuse msg_head for the reply to TX_READ */
490 msg_head.opcode = TX_STATUS;
491 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
496 /* reuse msg_head for the reply to RX_READ */
497 msg_head.opcode = RX_STATUS;
498 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
503 /* we need at least one can_frame */
504 if (msg_head.nframes < 1)
507 ret = bcm_tx_send(msg, ifindex, sk);
512 DBG("Unknown opcode %d\n", msg_head.opcode);
520 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
521 struct msghdr *msg, size_t size, int flags)
523 struct sock *sk = sock->sk;
529 DBG("socket %p, sk %p\n", sock, sk);
531 noblock = flags & MSG_DONTWAIT;
532 flags &= ~MSG_DONTWAIT;
533 skb = skb_recv_datagram(sk, flags, noblock, &error);
537 DBG("delivering skbuff %p\n", skb);
543 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
545 skb_free_datagram(sk, skb);
549 sock_recv_timestamp(msg, sk, skb);
552 msg->msg_namelen = sizeof(struct sockaddr_can);
553 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
556 DBG("freeing sock %p, skbuff %p\n", sk, skb);
557 skb_free_datagram(sk, skb);
562 static unsigned int bcm_poll(struct file *file, struct socket *sock,
565 unsigned int mask = 0;
567 DBG("socket %p\n", sock);
569 mask = datagram_poll(file, sock, wait);
573 /* helper functions for bcm_sendmsg() */
575 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
576 int ifindex, struct sock *sk)
578 struct bcm_opt *bo = bcm_sk(sk);
582 /* we need a real device to send frames */
586 /* we need at least one can_frame */
587 if (msg_head->nframes < 1)
590 /* check the given can_id */
592 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
595 /* update existing BCM operation */
597 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
598 op, msg_head->can_id);
601 * Do we need more space for the can_frames than currently
602 * allocated? -> This is a _really_ unusual use-case and
603 * therefore (complexity / locking) it is not supported.
605 if (msg_head->nframes > op->nframes)
608 /* update can_frames content */
609 for (i = 0; i < msg_head->nframes; i++) {
610 err = memcpy_fromiovec((u8*)&op->frames[i],
611 msg->msg_iov, CFSIZ);
615 if (msg_head->flags & TX_CP_CAN_ID) {
616 /* copy can_id into frame */
617 op->frames[i].can_id = msg_head->can_id;
622 /* insert new BCM operation for the given can_id */
624 op = kzalloc(OPSIZ, GFP_KERNEL);
628 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
629 op, msg_head->can_id);
631 op->can_id = msg_head->can_id;
633 /* create array for can_frames and copy the data */
634 op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL);
640 for (i = 0; i < msg_head->nframes; i++) {
641 err = memcpy_fromiovec((u8*)&op->frames[i],
642 msg->msg_iov, CFSIZ);
649 if (msg_head->flags & TX_CP_CAN_ID) {
650 /* copy can_id into frame */
651 op->frames[i].can_id = msg_head->can_id;
655 /* tx_ops never compare with previous received messages */
656 op->last_frames = NULL;
658 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
661 op->ifindex = ifindex;
663 /* initialize uninitialized (kmalloc) structure */
664 init_timer(&op->timer);
666 /* currently unused in tx_ops */
667 init_timer(&op->thrtimer);
669 /* handler for tx_ops */
670 op->timer.function = bcm_tx_timeout_handler;
672 /* timer.data points to this op-structure */
673 op->timer.data = (unsigned long)op;
675 /* add this bcm_op to the list of the tx_ops */
676 list_add(&op->list, &bo->tx_ops);
678 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
680 if (op->nframes != msg_head->nframes) {
681 op->nframes = msg_head->nframes;
682 /* start multiple frame transmission with index 0 */
688 op->flags = msg_head->flags;
690 if (op->flags & TX_RESET_MULTI_IDX) {
691 /* start multiple frame transmission with index 0 */
695 if (op->flags & SETTIMER) {
697 /* set timer values */
699 op->count = msg_head->count;
700 op->ival1 = msg_head->ival1;
701 op->ival2 = msg_head->ival2;
702 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
703 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
705 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
706 op->count, op->j_ival1, op->j_ival2);
708 /* disable an active timer due to zero values? */
709 if (!op->j_ival1 && !op->j_ival2) {
710 del_timer(&op->timer);
711 DBG("TX_SETUP: SETTIMER disabled timer.\n");
715 if ((op->flags & STARTTIMER) &&
716 ((op->j_ival1 && op->count) || op->j_ival2)) {
718 del_timer(&op->timer);
720 /* spec: send can_frame when starting timer */
721 op->flags |= TX_ANNOUNCE;
723 if (op->j_ival1 && (op->count > 0)){
724 op->timer.expires = jiffies + op->j_ival1;
725 /* op->count-- is done in bcm_tx_timeout_handler */
726 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
729 (char*) op->timer.data,
730 (unsigned int) op->timer.expires);
732 op->timer.expires = jiffies + op->j_ival2;
733 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
736 (char*) op->timer.data,
737 (unsigned int) op->timer.expires);
740 add_timer(&op->timer);
743 if (op->flags & TX_ANNOUNCE)
746 return msg_head->nframes * CFSIZ + MHSIZ;
749 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
750 int ifindex, struct sock *sk)
752 struct bcm_opt *bo = bcm_sk(sk);
757 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
758 /* be robust against wrong usage ... */
759 msg_head->flags |= RX_FILTER_ID;
760 msg_head->nframes = 0; /* ignore trailing garbage */
763 if ((msg_head->flags & RX_RTR_FRAME) &&
764 ((msg_head->nframes != 1) ||
765 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
767 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
771 /* check the given can_id */
772 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
775 /* update existing BCM operation */
777 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
778 op, msg_head->can_id);
781 * Do we need more space for the can_frames than currently
782 * allocated? -> This is a _really_ unusual use-case and
783 * therefore (complexity / locking) it is not supported.
785 if (msg_head->nframes > op->nframes)
788 if (msg_head->nframes) {
789 /* update can_frames content */
790 err = memcpy_fromiovec((u8*)op->frames,
792 msg_head->nframes * CFSIZ);
796 /* clear last_frames to indicate 'nothing received' */
797 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
800 op->nframes = msg_head->nframes;
801 /* Only an update -> do not call can_rx_register() */
805 /* insert new BCM operation for the given can_id */
807 op = kzalloc(OPSIZ, GFP_KERNEL);
811 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
812 op, msg_head->can_id);
814 op->can_id = msg_head->can_id;
815 op->nframes = msg_head->nframes;
817 if (msg_head->nframes) {
819 /* create array for can_frames and copy the data */
820 op->frames = kmalloc(msg_head->nframes * CFSIZ,
827 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
828 msg_head->nframes * CFSIZ);
835 /* create and init array for received can_frames */
836 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
838 if (!op->last_frames) {
844 /* op->frames = NULL due to memset in kzalloc() */
847 * even when we have the RX_FILTER_ID case, we need
848 * to store the last frame for the throttle feature
851 /* create and init array for received can_frames */
852 op->last_frames = kzalloc(CFSIZ, GFP_KERNEL);
853 if (!op->last_frames) {
859 op->sk = sk; /* bcm_delete_rx_op() needs this */
860 op->ifindex = ifindex;
862 /* initialize uninitialized (kmalloc) structure */
863 init_timer(&op->timer);
865 /* init throttle timer for RX_CHANGED */
866 init_timer(&op->thrtimer);
868 /* handler for rx timeouts */
869 op->timer.function = bcm_rx_timeout_handler;
871 /* timer.data points to this op-structure */
872 op->timer.data = (unsigned long)op;
874 /* handler for RX_CHANGED throttle timeouts */
875 op->thrtimer.function = bcm_rx_thr_handler;
877 /* timer.data points to this op-structure */
878 op->thrtimer.data = (unsigned long)op;
880 op->thrtimer.expires = 0; /* mark disabled timer */
882 /* add this bcm_op to the list of the tx_ops */
883 list_add(&op->list, &bo->rx_ops);
885 do_rx_register = 1; /* call can_rx_register() */
887 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
892 op->flags = msg_head->flags;
894 if (op->flags & RX_RTR_FRAME) {
896 /* no timers in RTR-mode */
897 del_timer(&op->thrtimer);
898 del_timer(&op->timer);
901 * funny feature in RX(!)_SETUP only for RTR-mode:
902 * copy can_id into frame BUT without RTR-flag to
903 * prevent a full-load-loopback-test ... ;-]
905 if ((op->flags & TX_CP_CAN_ID) ||
906 (op->frames[0].can_id == op->can_id))
907 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
910 if (op->flags & SETTIMER) {
912 /* set timer value */
914 op->ival1 = msg_head->ival1;
915 op->ival2 = msg_head->ival2;
916 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
917 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
919 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
920 op->j_ival1, op->j_ival2);
922 /* disable an active timer due to zero value? */
924 del_timer(&op->timer);
925 DBG("RX_SETUP: disabled timer rx timeouts.\n");
928 /* free currently blocked msgs ? */
929 if (op->thrtimer.expires) {
930 DBG("RX_SETUP: unblocking throttled msgs.\n");
931 del_timer(&op->thrtimer);
932 /* send blocked msgs hereafter */
933 op->thrtimer.expires = jiffies + 2;
934 add_timer(&op->thrtimer);
937 * if (op->j_ival2) is zero, no (new) throttling
938 * will happen. For details see functions
939 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
943 if ((op->flags & STARTTIMER) && op->j_ival1) {
945 del_timer(&op->timer);
947 op->timer.expires = jiffies + op->j_ival1;
949 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
951 (char *) op->timer.function,
952 (char *) op->timer.data,
953 (unsigned int) op->timer.expires);
955 add_timer(&op->timer);
959 /* now we can register for can_ids, if we added a new bcm_op */
960 if (do_rx_register) {
961 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
962 "rx_op is %p\n", op->can_id, op);
965 struct net_device *dev = dev_get_by_index(ifindex);
968 can_rx_register(dev, op->can_id,
970 bcm_rx_handler, op, IDENT);
974 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
975 bcm_rx_handler, op, IDENT);
978 return msg_head->nframes * CFSIZ + MHSIZ;
981 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
984 struct net_device *dev;
987 /* just copy and send one can_frame */
989 if (!ifindex) /* we need a real device to send frames */
992 skb = alloc_skb(CFSIZ, GFP_KERNEL);
997 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1003 DBG_FRAME("BCM: TX_SEND: sending frame",
1004 (struct can_frame *)skb->data);
1006 dev = dev_get_by_index(ifindex);
1014 can_send(skb, 1); /* send with loopback */
1017 return CFSIZ + MHSIZ;
1020 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
1026 op = bcm_find_op(ops, msg_head->can_id, ifindex);
1029 DBG("TRX_READ: sending status for can_id %03X\n",
1031 /* put current values into msg_head */
1032 msg_head->flags = op->flags;
1033 msg_head->count = op->count;
1034 msg_head->ival1 = op->ival1;
1035 msg_head->ival2 = op->ival2;
1036 msg_head->nframes = op->nframes;
1038 bcm_send_to_user(op, msg_head, op->frames, NULL);
1044 DBG("TRX_READ: did not find op for can_id %03X\n",
1052 /* procfs functions */
1054 static char *bcm_proc_getifname(int ifindex)
1056 struct net_device *dev;
1061 dev = __dev_get_by_index(ifindex); /* no usage counting */
1068 static int bcm_read_proc(char *page, char **start, off_t off,
1069 int count, int *eof, void *data)
1072 struct sock *sk = (struct sock *)data;
1073 struct bcm_opt *bo = bcm_sk(sk);
1076 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
1078 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
1079 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
1080 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
1081 bo->dropped_usr_msgs);
1082 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
1083 bcm_proc_getifname(bo->ifindex));
1084 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
1086 list_for_each_entry(op, &bo->rx_ops, list) {
1088 unsigned long reduction;
1090 /* print only active entries & prevent division by zero */
1091 if (!op->frames_abs)
1094 len += snprintf(page + len, PAGE_SIZE - len,
1095 "rx_op: %03X %-5s ",
1096 op->can_id, bcm_proc_getifname(op->ifindex));
1097 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1099 (op->flags & RX_CHECK_DLC)?'d':' ');
1101 len += snprintf(page + len, PAGE_SIZE - len,
1102 "timeo=%ld ", op->j_ival1);
1105 len += snprintf(page + len, PAGE_SIZE - len,
1106 "thr=%ld ", op->j_ival2);
1108 len += snprintf(page + len, PAGE_SIZE - len,
1109 "# recv %ld (%ld) => reduction: ",
1110 op->frames_filtered, op->frames_abs);
1112 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1114 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1115 (reduction == 100)?"near ":"", reduction);
1117 if (len > PAGE_SIZE - 200) {
1118 /* mark output cut off */
1119 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1124 list_for_each_entry(op, &bo->tx_ops, list) {
1126 len += snprintf(page + len, PAGE_SIZE - len,
1127 "tx_op: %03X %s [%d] ",
1128 op->can_id, bcm_proc_getifname(op->ifindex),
1131 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
1135 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
1138 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
1141 if (len > PAGE_SIZE - 100) {
1142 /* mark output cut off */
1143 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1148 len += snprintf(page + len, PAGE_SIZE - len, "\n");
1154 /* bcm_op handling tx path */
1156 static void bcm_can_tx(struct bcm_op *op)
1158 struct sk_buff *skb;
1159 struct net_device *dev;
1160 struct can_frame *cf = &op->frames[op->currframe];
1162 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1164 /* no target device? => exit */
1168 dev = dev_get_by_index(op->ifindex);
1171 /* RFC: should this bcm_op remove itself here? */
1175 skb = alloc_skb(CFSIZ,
1176 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1181 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1183 /* send with loopback */
1188 /* update statistics */
1192 /* reached last frame? */
1193 if (op->currframe >= op->nframes)
1199 static void bcm_tx_timeout_handler(unsigned long data)
1201 struct bcm_op *op = (struct bcm_op*)data;
1203 DBG("Called with bcm_op %p\n", op);
1205 if (op->j_ival1 && (op->count > 0)) {
1209 if (!op->count && (op->flags & TX_COUNTEVT)) {
1210 /* create notification to user */
1212 struct bcm_msg_head msg_head;
1214 DBG("sending TX_EXPIRED for can_id %03X\n",
1217 msg_head.opcode = TX_EXPIRED;
1218 msg_head.flags = op->flags;
1219 msg_head.count = op->count;
1220 msg_head.ival1 = op->ival1;
1221 msg_head.ival2 = op->ival2;
1222 msg_head.can_id = op->can_id;
1223 msg_head.nframes = 0;
1225 bcm_send_to_user(op, &msg_head, NULL, NULL);
1229 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1230 op->count, op->j_ival1, op->j_ival2);
1232 if (op->j_ival1 && (op->count > 0)) {
1234 op->timer.expires = jiffies + op->j_ival1;
1235 add_timer(&op->timer);
1237 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
1239 (char*) op->timer.data,
1240 (unsigned int) op->timer.expires);
1242 /* send (next) frame */
1246 op->timer.expires = jiffies + op->j_ival2;
1247 add_timer(&op->timer);
1249 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
1251 (char*) op->timer.data,
1252 (unsigned int) op->timer.expires);
1254 /* send (next) frame */
1257 DBG("no timer restart\n");
1264 /* bcm_op handling rx path */
1266 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1268 struct bcm_op *op = (struct bcm_op*)data;
1269 struct can_frame rxframe;
1272 /* disable timeout */
1273 del_timer(&op->timer);
1275 DBG("Called with bcm_op %p\n", op);
1277 if (skb->len == sizeof(rxframe)) {
1278 memcpy(&rxframe, skb->data, sizeof(rxframe));
1279 /* save rx timestamp */
1280 skb_get_timestamp(skb, &op->rx_stamp);
1281 /* save originator for recvfrom() */
1282 op->rx_ifindex = skb->dev->ifindex;
1283 /* update statistics */
1286 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
1288 DBG("Wrong skb->len = %d\n", skb->len);
1293 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1295 if (op->can_id != rxframe.can_id) {
1296 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
1297 rxframe.can_id, op->can_id);
1301 if (op->flags & RX_RTR_FRAME) {
1302 /* send reply for RTR-request */
1303 DBG("RTR-request\n");
1305 /* send op->frames[0] to CAN device */
1310 if (op->flags & RX_FILTER_ID) {
1311 /* the easiest case */
1312 DBG("Easy does it with RX_FILTER_ID\n");
1314 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1315 bcm_rx_starttimer(op);
1319 if (op->nframes == 1) {
1320 /* simple compare with index 0 */
1321 DBG("Simple compare\n");
1323 bcm_rx_cmp_to_index(op, 0, &rxframe);
1324 bcm_rx_starttimer(op);
1328 if (op->nframes > 1) {
1329 /* multiplex compare */
1330 DBG("Multiplex compare\n");
1333 * find the first multiplex mask that fits.
1334 * Remark: The MUX-mask is stored in index 0
1337 for (i=1; i < op->nframes; i++) {
1339 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1340 (GET_U64(&op->frames[0]) &
1341 GET_U64(&op->frames[i]))) {
1342 DBG("found MUX index %d\n", i);
1343 bcm_rx_cmp_to_index(op, i, &rxframe);
1347 bcm_rx_starttimer(op);
1351 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1352 struct can_frame *rxdata)
1355 * no one uses the MSBs of can_dlc for comparation,
1356 * so we use it here to detect the first time of reception
1359 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
1360 /* received data for the first time => send update to user */
1361 DBG("first time :)\n");
1362 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1366 /* do a real check in can_data */
1368 DBG("op->frames[index].data = 0x%016llx\n",
1369 GET_U64(&op->frames[index]));
1370 DBG("op->last_frames[index].data = 0x%016llx\n",
1371 GET_U64(&op->last_frames[index]));
1372 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1374 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1375 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1376 DBG("relevant data change :)\n");
1377 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1382 if (op->flags & RX_CHECK_DLC) {
1384 /* do a real check in dlc */
1386 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
1387 BCM_CAN_DLC_MASK)) {
1388 DBG("dlc change :)\n");
1389 bcm_rx_update_and_send(op, &op->last_frames[index],
1394 DBG("no relevant change :(\n");
1397 static void bcm_rx_update_and_send(struct bcm_op *op,
1398 struct can_frame *lastdata,
1399 struct can_frame *rxdata)
1401 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1403 memcpy(lastdata, rxdata, CFSIZ);
1406 lastdata->can_dlc |= RX_RECV;
1408 /* throttle bcm_rx_changed ? */
1409 if ((op->thrtimer.expires) ||
1410 ((op->j_ival2) && (nexttx > jiffies))) {
1411 /* we are already waiting OR we have to start waiting */
1413 /* mark as 'throttled' */
1414 lastdata->can_dlc |= RX_THR;
1416 if (!(op->thrtimer.expires)) {
1417 /* start the timer only the first time */
1418 op->thrtimer.expires = nexttx;
1419 add_timer(&op->thrtimer);
1421 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
1422 op->thrtimer.function,
1423 (char*) op->thrtimer.data,
1424 (unsigned int) op->thrtimer.expires);
1427 /* send RX_CHANGED to the user */
1428 bcm_rx_changed(op, rxdata);
1432 static void bcm_rx_starttimer(struct bcm_op *op)
1434 if (op->flags & RX_NO_AUTOTIMER)
1439 op->timer.expires = jiffies + op->j_ival1;
1441 DBG("adding rx timeout timer ival1. func=%p data=%p "
1444 (char*) op->timer.data,
1445 (unsigned int) op->timer.expires);
1447 add_timer(&op->timer);
1452 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1454 struct bcm_msg_head head;
1456 op->j_lastmsg = jiffies;
1458 /* update statistics */
1459 op->frames_filtered++;
1461 /* prevent statistics overflow */
1462 if (op->frames_filtered > ULONG_MAX/100)
1463 op->frames_filtered = op->frames_abs = 0;
1465 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
1466 (unsigned int) op->j_lastmsg, op);
1467 DBG("sending notification\n");
1469 head.opcode = RX_CHANGED;
1470 head.flags = op->flags;
1471 head.count = op->count;
1472 head.ival1 = op->ival1;
1473 head.ival2 = op->ival2;
1474 head.can_id = op->can_id;
1477 bcm_send_to_user(op, &head, data, &op->rx_stamp);
1481 static void bcm_rx_timeout_handler(unsigned long data)
1483 struct bcm_op *op = (struct bcm_op*)data;
1484 struct bcm_msg_head msg_head;
1486 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
1488 msg_head.opcode = RX_TIMEOUT;
1489 msg_head.flags = op->flags;
1490 msg_head.count = op->count;
1491 msg_head.ival1 = op->ival1;
1492 msg_head.ival2 = op->ival2;
1493 msg_head.can_id = op->can_id;
1494 msg_head.nframes = 0;
1496 bcm_send_to_user(op, &msg_head, NULL, NULL);
1498 /* no restart of the timer is done here! */
1500 /* if user wants to be informed, when cyclic CAN-Messages come back */
1501 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1502 /* clear received can_frames to indicate 'nothing received' */
1503 memset(op->last_frames, 0, op->nframes * CFSIZ);
1504 DBG("RX_ANNOUNCE_RESTART\n");
1509 static void bcm_rx_thr_handler(unsigned long data)
1511 struct bcm_op *op = (struct bcm_op*)data;
1514 /* mark disabled / consumed timer */
1515 op->thrtimer.expires = 0;
1517 if (op->nframes > 1){
1519 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
1521 /* for MUX filter we start at index 1 */
1522 for (i=1; i<op->nframes; i++){
1523 if ((op->last_frames) &&
1524 (op->last_frames[i].can_dlc & RX_THR)){
1525 op->last_frames[i].can_dlc &= ~RX_THR;
1526 bcm_rx_changed(op, &op->last_frames[i]);
1531 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
1533 /* for RX_FILTER_ID and simple filter */
1534 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1535 op->last_frames[0].can_dlc &= ~RX_THR;
1536 bcm_rx_changed(op, &op->last_frames[0]);
1541 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1542 struct can_frame *frames, struct timeval *tv)
1544 struct sk_buff *skb;
1545 struct can_frame *firstframe;
1546 struct sock *sk = op->sk;
1547 int datalen = head->nframes * CFSIZ;
1548 struct sockaddr_can *addr;
1551 skb = alloc_skb(sizeof(*head) + datalen,
1552 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1556 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1558 /* can_frames starting here */
1559 firstframe = (struct can_frame *) skb->tail;
1562 skb_set_timestamp(skb, tv); /* restore timestamp */
1564 addr = (struct sockaddr_can *)skb->cb;
1565 memset(addr, 0, sizeof(*addr));
1566 addr->can_family = AF_CAN;
1567 /* restore originator for recvfrom() */
1568 addr->can_ifindex = op->rx_ifindex;
1571 memcpy(skb_put(skb, datalen), frames, datalen);
1574 * the BCM uses the can_dlc-element of the can_frame
1575 * structure for internal purposes. This is only
1576 * relevant for updates that are generated by the
1577 * BCM, where nframes is 1
1579 if (head->nframes == 1)
1580 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1583 err = sock_queue_rcv_skb(sk, skb);
1585 struct bcm_opt *bo = bcm_sk(sk);
1587 DBG("sock_queue_rcv_skb failed: %d\n", err);
1589 /* don't care about overflows in this statistic */
1590 bo->dropped_usr_msgs++;
1594 /* bcm_op handling: find & delete bcm_op elements */
1596 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
1601 list_for_each_entry(op, ops, list) {
1602 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1609 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1611 struct bcm_op *op, *n;
1613 list_for_each_entry_safe(op, n, ops, list) {
1614 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1615 DBG("removing rx_op %p for can_id %03X\n",
1619 * Don't care if we're bound or not (due to netdev
1620 * problems) can_rx_unregister() is always a save
1624 struct net_device *dev =
1625 dev_get_by_index(op->ifindex);
1628 can_rx_unregister(dev, op->can_id,
1629 REGMASK(op->can_id),
1630 bcm_rx_handler, op);
1634 can_rx_unregister(NULL, op->can_id,
1635 REGMASK(op->can_id),
1636 bcm_rx_handler, op);
1638 list_del(&op->list);
1640 return 1; /* done */
1644 return 0; /* not found */
1647 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1649 struct bcm_op *op, *n;
1651 list_for_each_entry_safe(op, n, ops, list) {
1652 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1653 DBG("removing rx_op %p for can_id %03X\n",
1655 list_del(&op->list);
1657 return 1; /* done */
1661 return 0; /* not found */
1664 static void bcm_remove_op(struct bcm_op *op)
1666 del_timer(&op->timer);
1667 del_timer(&op->thrtimer);
1670 if (op->last_frames)
1671 kfree(op->last_frames);
1677 module_init(bcm_module_init);
1678 module_exit(bcm_module_exit);