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>
57 #include <linux/can/version.h>
61 #ifdef CONFIG_CAN_DEBUG_CORE
63 module_param(debug, int, S_IRUGO);
64 #define DBG(args...) (debug & 1 ? \
65 (printk(KERN_DEBUG "BCM %s: ", __func__), \
67 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
68 #define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
71 #define DBG_FRAME(args...)
75 /* use of last_frames[index].can_dlc */
76 #define RX_RECV 0x40 /* received data for this element */
77 #define RX_THR 0x80 /* element not been sent due to throttle feature */
78 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
80 /* get best masking value for can_rx_register() for a given single can_id */
81 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
82 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
85 static __initdata const char banner[] = KERN_INFO
86 "CAN: broadcast manager (bcm) socket protocol " CAN_VERSION "\n";
88 MODULE_DESCRIPTION("PF_CAN bcm sockets");
89 MODULE_LICENSE("Dual BSD/GPL");
90 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
92 #define GET_U64(p) (*(u64*)(p)->data) /* easy access */
95 struct list_head list;
99 unsigned long j_ival1, j_ival2, j_lastmsg;
100 unsigned long frames_abs, frames_filtered;
101 struct timer_list timer, thrtimer;
102 struct timeval ival1, ival2;
103 struct timeval rx_stamp;
108 struct can_frame *frames;
109 struct can_frame *last_frames;
116 struct list_head rx_ops;
117 struct list_head tx_ops;
118 unsigned long dropped_usr_msgs;
119 struct proc_dir_entry *bcm_proc_read;
120 char procname [9]; /* pointer printed in ASCII with \0 */
123 static struct proc_dir_entry *proc_dir = NULL;
125 static int bcm_init(struct sock *sk);
126 static void bcm_notifier(unsigned long msg, void *data);
127 static int bcm_release(struct socket *sock);
128 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
130 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
131 struct msghdr *msg, size_t size);
132 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
133 struct msghdr *msg, size_t size, int flags);
134 static unsigned int bcm_poll(struct file *file, struct socket *sock,
137 static int bcm_read_proc(char *page, char **start, off_t off,
138 int count, int *eof, void *data);
140 static void bcm_tx_timeout_handler(unsigned long data);
141 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
142 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
143 int ifindex, struct sock *sk);
144 static void bcm_can_tx(struct bcm_op *op);
146 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
147 int ifindex, struct sock *sk);
148 static void bcm_rx_handler(struct sk_buff *skb, void *op);
149 static void bcm_rx_timeout_handler(unsigned long data);
150 static void bcm_rx_thr_handler(unsigned long data);
151 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
152 struct can_frame *rxdata);
153 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
154 static void bcm_rx_starttimer(struct bcm_op *op);
155 static void bcm_rx_update_and_send(struct bcm_op *op,
156 struct can_frame *lastdata,
157 struct can_frame *rxdata);
158 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
159 struct can_frame *frames, struct timeval *tv);
161 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id,
163 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id,
165 static void bcm_remove_op(struct bcm_op *op);
166 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
168 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
171 static struct proto_ops bcm_ops = {
173 .release = bcm_release,
174 .bind = sock_no_bind,
175 .connect = bcm_connect,
176 .socketpair = sock_no_socketpair,
177 .accept = sock_no_accept,
178 .getname = sock_no_getname,
180 .ioctl = NULL, /* use can_ioctl() from af_can.c */
181 .listen = sock_no_listen,
182 .shutdown = sock_no_shutdown,
183 .setsockopt = sock_no_setsockopt,
184 .getsockopt = sock_no_getsockopt,
185 .sendmsg = bcm_sendmsg,
186 .recvmsg = bcm_recvmsg,
187 .mmap = sock_no_mmap,
188 .sendpage = sock_no_sendpage,
191 #ifdef CONFIG_CAN_BCM_USER
194 #define BCM_CAP CAP_NET_RAW
197 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
203 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
205 static struct proto bcm_proto = {
207 .owner = THIS_MODULE,
208 .obj_size = sizeof(struct bcm_sock),
212 static struct can_proto bcm_can_proto = {
215 .capability = BCM_CAP,
220 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
222 static struct can_proto bcm_can_proto = {
225 .capability = BCM_CAP,
227 .owner = THIS_MODULE,
228 .obj_size = sizeof(struct bcm_opt),
232 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
233 static void *kzalloc(size_t size, unsigned int __nocast flags)
235 void *ret = kmalloc(size, flags);
237 memset(ret, 0, size);
241 static inline void skb_get_timestamp(const struct sk_buff *skb,
242 struct timeval *stamp)
244 stamp->tv_sec = skb->stamp.tv_sec;
245 stamp->tv_usec = skb->stamp.tv_usec;
248 static inline void skb_set_timestamp(struct sk_buff *skb,
249 const struct timeval *stamp)
251 skb->stamp.tv_sec = stamp->tv_sec;
252 skb->stamp.tv_usec = stamp->tv_usec;
256 #define CFSIZ sizeof(struct can_frame)
257 #define OPSIZ sizeof(struct bcm_op)
258 #define MHSIZ sizeof(struct bcm_msg_head)
260 static int __init bcm_module_init(void)
264 can_proto_register(&bcm_can_proto);
266 /* create /proc/net/can/bcm directory */
267 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
270 proc_dir->owner = THIS_MODULE;
275 static void __exit bcm_module_exit(void)
277 can_proto_unregister(&bcm_can_proto);
280 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
284 /* initial settings at socket creation time */
286 static int bcm_init(struct sock *sk)
288 struct bcm_opt *bo = bcm_sk(sk);
292 bo->dropped_usr_msgs = 0;
293 bo->bcm_proc_read = NULL;
295 INIT_LIST_HEAD(&bo->tx_ops);
296 INIT_LIST_HEAD(&bo->rx_ops);
301 /* handling of netdevice problems */
303 static void bcm_notifier(unsigned long msg, void *data)
305 struct sock *sk = (struct sock *)data;
306 struct bcm_opt *bo = bcm_sk(sk);
308 DBG("called for sock %p\n", sk);
311 case NETDEV_UNREGISTER:
316 sk->sk_err = ENETDOWN;
317 if (!sock_flag(sk, SOCK_DEAD))
318 sk->sk_error_report(sk);
322 /* standard socket functions */
324 static int bcm_release(struct socket *sock)
326 struct sock *sk = sock->sk;
327 struct bcm_opt *bo = bcm_sk(sk);
328 struct bcm_op *op, *next;
330 DBG("socket %p, sk %p\n", sock, sk);
332 /* remove bcm_ops, timer, rx_unregister(), etc. */
334 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
335 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
339 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
340 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
343 * Don't care if we're bound or not (due to netdev problems)
344 * can_rx_unregister() is always a save thing to do here.
347 struct net_device *dev = dev_get_by_index(op->ifindex);
350 can_rx_unregister(dev, op->can_id,
356 can_rx_unregister(NULL, op->can_id,
363 /* remove procfs entry */
364 if ((proc_dir) && (bo->bcm_proc_read)) {
365 remove_proc_entry(bo->procname, proc_dir);
368 /* remove device notifier */
370 struct net_device *dev = dev_get_by_index(bo->ifindex);
373 can_dev_unregister(dev, bcm_notifier, sk);
383 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
386 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
387 struct sock *sk = sock->sk;
388 struct bcm_opt *bo = bcm_sk(sk);
393 /* bind a device to this socket */
394 if (addr->can_ifindex) {
395 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
398 DBG("could not find device index %d\n",
402 bo->ifindex = dev->ifindex;
403 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
406 DBG("socket %p bound to device %s (idx %d)\n",
407 sock, dev->name, dev->ifindex);
409 /* no notifier for ifindex = 0 ('any' CAN device) */
416 /* unique socket address as filename */
417 sprintf(bo->procname, "%p", sock);
418 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
426 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
427 struct msghdr *msg, size_t size)
429 struct sock *sk = sock->sk;
430 struct bcm_opt *bo = bcm_sk(sk);
431 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
432 struct bcm_msg_head msg_head;
433 int ret; /* read bytes or error codes as return value */
436 DBG("sock %p not bound\n", sk);
440 /* check for alternative ifindex for this bcm_op */
442 if (!ifindex && msg->msg_name) {
443 /* no bound device as default => check msg_name */
444 struct sockaddr_can *addr =
445 (struct sockaddr_can *)msg->msg_name;
447 if (addr->can_family != AF_CAN)
450 ifindex = addr->can_ifindex; /* ifindex from sendto() */
452 if (ifindex && !dev_get_by_index(ifindex)) {
453 DBG("device %d not found\n", ifindex);
458 /* read message head information */
460 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
464 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
466 switch (msg_head.opcode) {
470 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
475 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
480 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
488 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
496 /* reuse msg_head for the reply to TX_READ */
497 msg_head.opcode = TX_STATUS;
498 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
503 /* reuse msg_head for the reply to RX_READ */
504 msg_head.opcode = RX_STATUS;
505 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
510 /* we need at least one can_frame */
511 if (msg_head.nframes < 1)
514 ret = bcm_tx_send(msg, ifindex, sk);
519 DBG("Unknown opcode %d\n", msg_head.opcode);
527 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
528 struct msghdr *msg, size_t size, int flags)
530 struct sock *sk = sock->sk;
536 DBG("socket %p, sk %p\n", sock, sk);
538 noblock = flags & MSG_DONTWAIT;
539 flags &= ~MSG_DONTWAIT;
540 skb = skb_recv_datagram(sk, flags, noblock, &error);
544 DBG("delivering skbuff %p\n", skb);
550 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
552 skb_free_datagram(sk, skb);
556 sock_recv_timestamp(msg, sk, skb);
559 msg->msg_namelen = sizeof(struct sockaddr_can);
560 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
563 DBG("freeing sock %p, skbuff %p\n", sk, skb);
564 skb_free_datagram(sk, skb);
569 static unsigned int bcm_poll(struct file *file, struct socket *sock,
572 unsigned int mask = 0;
574 DBG("socket %p\n", sock);
576 mask = datagram_poll(file, sock, wait);
580 /* helper functions for bcm_sendmsg() */
582 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
583 int ifindex, struct sock *sk)
585 struct bcm_opt *bo = bcm_sk(sk);
589 /* we need a real device to send frames */
593 /* we need at least one can_frame */
594 if (msg_head->nframes < 1)
597 /* check the given can_id */
599 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
602 /* update existing BCM operation */
604 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
605 op, msg_head->can_id);
608 * Do we need more space for the can_frames than currently
609 * allocated? -> This is a _really_ unusual use-case and
610 * therefore (complexity / locking) it is not supported.
612 if (msg_head->nframes > op->nframes)
615 /* update can_frames content */
616 for (i = 0; i < msg_head->nframes; i++) {
617 err = memcpy_fromiovec((u8*)&op->frames[i],
618 msg->msg_iov, CFSIZ);
622 if (msg_head->flags & TX_CP_CAN_ID) {
623 /* copy can_id into frame */
624 op->frames[i].can_id = msg_head->can_id;
629 /* insert new BCM operation for the given can_id */
631 op = kzalloc(OPSIZ, GFP_KERNEL);
635 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
636 op, msg_head->can_id);
638 op->can_id = msg_head->can_id;
640 /* create array for can_frames and copy the data */
641 op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL);
647 for (i = 0; i < msg_head->nframes; i++) {
648 err = memcpy_fromiovec((u8*)&op->frames[i],
649 msg->msg_iov, CFSIZ);
656 if (msg_head->flags & TX_CP_CAN_ID) {
657 /* copy can_id into frame */
658 op->frames[i].can_id = msg_head->can_id;
662 /* tx_ops never compare with previous received messages */
663 op->last_frames = NULL;
665 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
668 op->ifindex = ifindex;
670 /* initialize uninitialized (kmalloc) structure */
671 init_timer(&op->timer);
673 /* currently unused in tx_ops */
674 init_timer(&op->thrtimer);
676 /* handler for tx_ops */
677 op->timer.function = bcm_tx_timeout_handler;
679 /* timer.data points to this op-structure */
680 op->timer.data = (unsigned long)op;
682 /* add this bcm_op to the list of the tx_ops */
683 list_add(&op->list, &bo->tx_ops);
685 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
687 if (op->nframes != msg_head->nframes) {
688 op->nframes = msg_head->nframes;
689 /* start multiple frame transmission with index 0 */
695 op->flags = msg_head->flags;
697 if (op->flags & TX_RESET_MULTI_IDX) {
698 /* start multiple frame transmission with index 0 */
702 if (op->flags & SETTIMER) {
704 /* set timer values */
706 op->count = msg_head->count;
707 op->ival1 = msg_head->ival1;
708 op->ival2 = msg_head->ival2;
709 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
710 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
712 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
713 op->count, op->j_ival1, op->j_ival2);
715 /* disable an active timer due to zero values? */
716 if (!op->j_ival1 && !op->j_ival2) {
717 del_timer(&op->timer);
718 DBG("TX_SETUP: SETTIMER disabled timer.\n");
722 if ((op->flags & STARTTIMER) &&
723 ((op->j_ival1 && op->count) || op->j_ival2)) {
725 del_timer(&op->timer);
727 /* spec: send can_frame when starting timer */
728 op->flags |= TX_ANNOUNCE;
730 if (op->j_ival1 && (op->count > 0)){
731 op->timer.expires = jiffies + op->j_ival1;
732 /* op->count-- is done in bcm_tx_timeout_handler */
733 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
736 (char*) op->timer.data,
737 (unsigned int) op->timer.expires);
739 op->timer.expires = jiffies + op->j_ival2;
740 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
743 (char*) op->timer.data,
744 (unsigned int) op->timer.expires);
747 add_timer(&op->timer);
750 if (op->flags & TX_ANNOUNCE)
753 return msg_head->nframes * CFSIZ + MHSIZ;
756 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
757 int ifindex, struct sock *sk)
759 struct bcm_opt *bo = bcm_sk(sk);
764 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
765 /* be robust against wrong usage ... */
766 msg_head->flags |= RX_FILTER_ID;
767 msg_head->nframes = 0; /* ignore trailing garbage */
770 if ((msg_head->flags & RX_RTR_FRAME) &&
771 ((msg_head->nframes != 1) ||
772 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
774 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
778 /* check the given can_id */
779 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
782 /* update existing BCM operation */
784 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
785 op, msg_head->can_id);
788 * Do we need more space for the can_frames than currently
789 * allocated? -> This is a _really_ unusual use-case and
790 * therefore (complexity / locking) it is not supported.
792 if (msg_head->nframes > op->nframes)
795 if (msg_head->nframes) {
796 /* update can_frames content */
797 err = memcpy_fromiovec((u8*)op->frames,
799 msg_head->nframes * CFSIZ);
803 /* clear last_frames to indicate 'nothing received' */
804 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
807 op->nframes = msg_head->nframes;
808 /* Only an update -> do not call can_rx_register() */
812 /* insert new BCM operation for the given can_id */
814 op = kzalloc(OPSIZ, GFP_KERNEL);
818 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
819 op, msg_head->can_id);
821 op->can_id = msg_head->can_id;
822 op->nframes = msg_head->nframes;
824 if (msg_head->nframes) {
826 /* create array for can_frames and copy the data */
827 op->frames = kmalloc(msg_head->nframes * CFSIZ,
834 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
835 msg_head->nframes * CFSIZ);
842 /* create and init array for received can_frames */
843 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
845 if (!op->last_frames) {
851 /* op->frames = NULL due to memset in kzalloc() */
854 * even when we have the RX_FILTER_ID case, we need
855 * to store the last frame for the throttle feature
858 /* create and init array for received can_frames */
859 op->last_frames = kzalloc(CFSIZ, GFP_KERNEL);
860 if (!op->last_frames) {
866 op->sk = sk; /* bcm_delete_rx_op() needs this */
867 op->ifindex = ifindex;
869 /* initialize uninitialized (kmalloc) structure */
870 init_timer(&op->timer);
872 /* init throttle timer for RX_CHANGED */
873 init_timer(&op->thrtimer);
875 /* handler for rx timeouts */
876 op->timer.function = bcm_rx_timeout_handler;
878 /* timer.data points to this op-structure */
879 op->timer.data = (unsigned long)op;
881 /* handler for RX_CHANGED throttle timeouts */
882 op->thrtimer.function = bcm_rx_thr_handler;
884 /* timer.data points to this op-structure */
885 op->thrtimer.data = (unsigned long)op;
887 op->thrtimer.expires = 0; /* mark disabled timer */
889 /* add this bcm_op to the list of the tx_ops */
890 list_add(&op->list, &bo->rx_ops);
892 do_rx_register = 1; /* call can_rx_register() */
894 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
899 op->flags = msg_head->flags;
901 if (op->flags & RX_RTR_FRAME) {
903 /* no timers in RTR-mode */
904 del_timer(&op->thrtimer);
905 del_timer(&op->timer);
908 * funny feature in RX(!)_SETUP only for RTR-mode:
909 * copy can_id into frame BUT without RTR-flag to
910 * prevent a full-load-loopback-test ... ;-]
912 if ((op->flags & TX_CP_CAN_ID) ||
913 (op->frames[0].can_id == op->can_id))
914 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
917 if (op->flags & SETTIMER) {
919 /* set timer value */
921 op->ival1 = msg_head->ival1;
922 op->ival2 = msg_head->ival2;
923 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
924 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
926 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
927 op->j_ival1, op->j_ival2);
929 /* disable an active timer due to zero value? */
931 del_timer(&op->timer);
932 DBG("RX_SETUP: disabled timer rx timeouts.\n");
935 /* free currently blocked msgs ? */
936 if (op->thrtimer.expires) {
937 DBG("RX_SETUP: unblocking throttled msgs.\n");
938 del_timer(&op->thrtimer);
939 /* send blocked msgs hereafter */
940 op->thrtimer.expires = jiffies + 2;
941 add_timer(&op->thrtimer);
944 * if (op->j_ival2) is zero, no (new) throttling
945 * will happen. For details see functions
946 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
950 if ((op->flags & STARTTIMER) && op->j_ival1) {
952 del_timer(&op->timer);
954 op->timer.expires = jiffies + op->j_ival1;
956 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
958 (char *) op->timer.function,
959 (char *) op->timer.data,
960 (unsigned int) op->timer.expires);
962 add_timer(&op->timer);
966 /* now we can register for can_ids, if we added a new bcm_op */
967 if (do_rx_register) {
968 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
969 "rx_op is %p\n", op->can_id, op);
972 struct net_device *dev = dev_get_by_index(ifindex);
975 can_rx_register(dev, op->can_id,
977 bcm_rx_handler, op, IDENT);
981 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
982 bcm_rx_handler, op, IDENT);
985 return msg_head->nframes * CFSIZ + MHSIZ;
988 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
991 struct net_device *dev;
994 /* just copy and send one can_frame */
996 if (!ifindex) /* we need a real device to send frames */
999 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1004 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1010 DBG_FRAME("BCM: TX_SEND: sending frame",
1011 (struct can_frame *)skb->data);
1013 dev = dev_get_by_index(ifindex);
1021 can_send(skb, 1); /* send with loopback */
1024 return CFSIZ + MHSIZ;
1027 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
1033 op = bcm_find_op(ops, msg_head->can_id, ifindex);
1036 DBG("TRX_READ: sending status for can_id %03X\n",
1038 /* put current values into msg_head */
1039 msg_head->flags = op->flags;
1040 msg_head->count = op->count;
1041 msg_head->ival1 = op->ival1;
1042 msg_head->ival2 = op->ival2;
1043 msg_head->nframes = op->nframes;
1045 bcm_send_to_user(op, msg_head, op->frames, NULL);
1051 DBG("TRX_READ: did not find op for can_id %03X\n",
1059 /* procfs functions */
1061 static char *bcm_proc_getifname(int ifindex)
1063 struct net_device *dev;
1068 dev = __dev_get_by_index(ifindex); /* no usage counting */
1075 static int bcm_read_proc(char *page, char **start, off_t off,
1076 int count, int *eof, void *data)
1079 struct sock *sk = (struct sock *)data;
1080 struct bcm_opt *bo = bcm_sk(sk);
1083 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
1085 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
1086 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
1087 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
1088 bo->dropped_usr_msgs);
1089 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
1090 bcm_proc_getifname(bo->ifindex));
1091 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
1093 list_for_each_entry(op, &bo->rx_ops, list) {
1095 unsigned long reduction;
1097 /* print only active entries & prevent division by zero */
1098 if (!op->frames_abs)
1101 len += snprintf(page + len, PAGE_SIZE - len,
1102 "rx_op: %03X %-5s ",
1103 op->can_id, bcm_proc_getifname(op->ifindex));
1104 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1106 (op->flags & RX_CHECK_DLC)?'d':' ');
1108 len += snprintf(page + len, PAGE_SIZE - len,
1109 "timeo=%ld ", op->j_ival1);
1112 len += snprintf(page + len, PAGE_SIZE - len,
1113 "thr=%ld ", op->j_ival2);
1115 len += snprintf(page + len, PAGE_SIZE - len,
1116 "# recv %ld (%ld) => reduction: ",
1117 op->frames_filtered, op->frames_abs);
1119 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1121 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1122 (reduction == 100)?"near ":"", reduction);
1124 if (len > PAGE_SIZE - 200) {
1125 /* mark output cut off */
1126 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1131 list_for_each_entry(op, &bo->tx_ops, list) {
1133 len += snprintf(page + len, PAGE_SIZE - len,
1134 "tx_op: %03X %s [%d] ",
1135 op->can_id, bcm_proc_getifname(op->ifindex),
1138 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
1142 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
1145 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
1148 if (len > PAGE_SIZE - 100) {
1149 /* mark output cut off */
1150 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1155 len += snprintf(page + len, PAGE_SIZE - len, "\n");
1161 /* bcm_op handling tx path */
1163 static void bcm_can_tx(struct bcm_op *op)
1165 struct sk_buff *skb;
1166 struct net_device *dev;
1167 struct can_frame *cf = &op->frames[op->currframe];
1169 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1171 /* no target device? => exit */
1175 dev = dev_get_by_index(op->ifindex);
1178 /* RFC: should this bcm_op remove itself here? */
1182 skb = alloc_skb(CFSIZ,
1183 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1188 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1190 /* send with loopback */
1195 /* update statistics */
1199 /* reached last frame? */
1200 if (op->currframe >= op->nframes)
1206 static void bcm_tx_timeout_handler(unsigned long data)
1208 struct bcm_op *op = (struct bcm_op*)data;
1210 DBG("Called with bcm_op %p\n", op);
1212 if (op->j_ival1 && (op->count > 0)) {
1216 if (!op->count && (op->flags & TX_COUNTEVT)) {
1217 /* create notification to user */
1219 struct bcm_msg_head msg_head;
1221 DBG("sending TX_EXPIRED for can_id %03X\n",
1224 msg_head.opcode = TX_EXPIRED;
1225 msg_head.flags = op->flags;
1226 msg_head.count = op->count;
1227 msg_head.ival1 = op->ival1;
1228 msg_head.ival2 = op->ival2;
1229 msg_head.can_id = op->can_id;
1230 msg_head.nframes = 0;
1232 bcm_send_to_user(op, &msg_head, NULL, NULL);
1236 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1237 op->count, op->j_ival1, op->j_ival2);
1239 if (op->j_ival1 && (op->count > 0)) {
1241 op->timer.expires = jiffies + op->j_ival1;
1242 add_timer(&op->timer);
1244 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
1246 (char*) op->timer.data,
1247 (unsigned int) op->timer.expires);
1249 /* send (next) frame */
1253 op->timer.expires = jiffies + op->j_ival2;
1254 add_timer(&op->timer);
1256 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
1258 (char*) op->timer.data,
1259 (unsigned int) op->timer.expires);
1261 /* send (next) frame */
1264 DBG("no timer restart\n");
1271 /* bcm_op handling rx path */
1273 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1275 struct bcm_op *op = (struct bcm_op*)data;
1276 struct can_frame rxframe;
1279 /* disable timeout */
1280 del_timer(&op->timer);
1282 DBG("Called with bcm_op %p\n", op);
1284 if (skb->len == sizeof(rxframe)) {
1285 memcpy(&rxframe, skb->data, sizeof(rxframe));
1286 /* save rx timestamp */
1287 skb_get_timestamp(skb, &op->rx_stamp);
1288 /* save originator for recvfrom() */
1289 op->rx_ifindex = skb->dev->ifindex;
1290 /* update statistics */
1293 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
1295 DBG("Wrong skb->len = %d\n", skb->len);
1300 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1302 if (op->can_id != rxframe.can_id) {
1303 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
1304 rxframe.can_id, op->can_id);
1308 if (op->flags & RX_RTR_FRAME) {
1309 /* send reply for RTR-request */
1310 DBG("RTR-request\n");
1312 /* send op->frames[0] to CAN device */
1317 if (op->flags & RX_FILTER_ID) {
1318 /* the easiest case */
1319 DBG("Easy does it with RX_FILTER_ID\n");
1321 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1322 bcm_rx_starttimer(op);
1326 if (op->nframes == 1) {
1327 /* simple compare with index 0 */
1328 DBG("Simple compare\n");
1330 bcm_rx_cmp_to_index(op, 0, &rxframe);
1331 bcm_rx_starttimer(op);
1335 if (op->nframes > 1) {
1336 /* multiplex compare */
1337 DBG("Multiplex compare\n");
1340 * find the first multiplex mask that fits.
1341 * Remark: The MUX-mask is stored in index 0
1344 for (i=1; i < op->nframes; i++) {
1346 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1347 (GET_U64(&op->frames[0]) &
1348 GET_U64(&op->frames[i]))) {
1349 DBG("found MUX index %d\n", i);
1350 bcm_rx_cmp_to_index(op, i, &rxframe);
1354 bcm_rx_starttimer(op);
1358 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1359 struct can_frame *rxdata)
1362 * no one uses the MSBs of can_dlc for comparation,
1363 * so we use it here to detect the first time of reception
1366 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
1367 /* received data for the first time => send update to user */
1368 DBG("first time :)\n");
1369 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1373 /* do a real check in can_data */
1375 DBG("op->frames[index].data = 0x%016llx\n",
1376 GET_U64(&op->frames[index]));
1377 DBG("op->last_frames[index].data = 0x%016llx\n",
1378 GET_U64(&op->last_frames[index]));
1379 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1381 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1382 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1383 DBG("relevant data change :)\n");
1384 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1389 if (op->flags & RX_CHECK_DLC) {
1391 /* do a real check in dlc */
1393 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
1394 BCM_CAN_DLC_MASK)) {
1395 DBG("dlc change :)\n");
1396 bcm_rx_update_and_send(op, &op->last_frames[index],
1401 DBG("no relevant change :(\n");
1404 static void bcm_rx_update_and_send(struct bcm_op *op,
1405 struct can_frame *lastdata,
1406 struct can_frame *rxdata)
1408 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1410 memcpy(lastdata, rxdata, CFSIZ);
1413 lastdata->can_dlc |= RX_RECV;
1415 /* throttle bcm_rx_changed ? */
1416 if ((op->thrtimer.expires) ||
1417 ((op->j_ival2) && (nexttx > jiffies))) {
1418 /* we are already waiting OR we have to start waiting */
1420 /* mark as 'throttled' */
1421 lastdata->can_dlc |= RX_THR;
1423 if (!(op->thrtimer.expires)) {
1424 /* start the timer only the first time */
1425 op->thrtimer.expires = nexttx;
1426 add_timer(&op->thrtimer);
1428 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
1429 op->thrtimer.function,
1430 (char*) op->thrtimer.data,
1431 (unsigned int) op->thrtimer.expires);
1434 /* send RX_CHANGED to the user */
1435 bcm_rx_changed(op, rxdata);
1439 static void bcm_rx_starttimer(struct bcm_op *op)
1441 if (op->flags & RX_NO_AUTOTIMER)
1446 op->timer.expires = jiffies + op->j_ival1;
1448 DBG("adding rx timeout timer ival1. func=%p data=%p "
1451 (char*) op->timer.data,
1452 (unsigned int) op->timer.expires);
1454 add_timer(&op->timer);
1459 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1461 struct bcm_msg_head head;
1463 op->j_lastmsg = jiffies;
1465 /* update statistics */
1466 op->frames_filtered++;
1468 /* prevent statistics overflow */
1469 if (op->frames_filtered > ULONG_MAX/100)
1470 op->frames_filtered = op->frames_abs = 0;
1472 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
1473 (unsigned int) op->j_lastmsg, op);
1474 DBG("sending notification\n");
1476 head.opcode = RX_CHANGED;
1477 head.flags = op->flags;
1478 head.count = op->count;
1479 head.ival1 = op->ival1;
1480 head.ival2 = op->ival2;
1481 head.can_id = op->can_id;
1484 bcm_send_to_user(op, &head, data, &op->rx_stamp);
1488 static void bcm_rx_timeout_handler(unsigned long data)
1490 struct bcm_op *op = (struct bcm_op*)data;
1491 struct bcm_msg_head msg_head;
1493 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
1495 msg_head.opcode = RX_TIMEOUT;
1496 msg_head.flags = op->flags;
1497 msg_head.count = op->count;
1498 msg_head.ival1 = op->ival1;
1499 msg_head.ival2 = op->ival2;
1500 msg_head.can_id = op->can_id;
1501 msg_head.nframes = 0;
1503 bcm_send_to_user(op, &msg_head, NULL, NULL);
1505 /* no restart of the timer is done here! */
1507 /* if user wants to be informed, when cyclic CAN-Messages come back */
1508 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1509 /* clear received can_frames to indicate 'nothing received' */
1510 memset(op->last_frames, 0, op->nframes * CFSIZ);
1511 DBG("RX_ANNOUNCE_RESTART\n");
1516 static void bcm_rx_thr_handler(unsigned long data)
1518 struct bcm_op *op = (struct bcm_op*)data;
1521 /* mark disabled / consumed timer */
1522 op->thrtimer.expires = 0;
1524 if (op->nframes > 1){
1526 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
1528 /* for MUX filter we start at index 1 */
1529 for (i=1; i<op->nframes; i++){
1530 if ((op->last_frames) &&
1531 (op->last_frames[i].can_dlc & RX_THR)){
1532 op->last_frames[i].can_dlc &= ~RX_THR;
1533 bcm_rx_changed(op, &op->last_frames[i]);
1538 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
1540 /* for RX_FILTER_ID and simple filter */
1541 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1542 op->last_frames[0].can_dlc &= ~RX_THR;
1543 bcm_rx_changed(op, &op->last_frames[0]);
1548 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1549 struct can_frame *frames, struct timeval *tv)
1551 struct sk_buff *skb;
1552 struct can_frame *firstframe;
1553 struct sock *sk = op->sk;
1554 int datalen = head->nframes * CFSIZ;
1555 struct sockaddr_can *addr;
1558 skb = alloc_skb(sizeof(*head) + datalen,
1559 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1563 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1565 /* can_frames starting here */
1566 firstframe = (struct can_frame *) skb->tail;
1569 skb_set_timestamp(skb, tv); /* restore timestamp */
1571 addr = (struct sockaddr_can *)skb->cb;
1572 memset(addr, 0, sizeof(*addr));
1573 addr->can_family = AF_CAN;
1574 /* restore originator for recvfrom() */
1575 addr->can_ifindex = op->rx_ifindex;
1578 memcpy(skb_put(skb, datalen), frames, datalen);
1581 * the BCM uses the can_dlc-element of the can_frame
1582 * structure for internal purposes. This is only
1583 * relevant for updates that are generated by the
1584 * BCM, where nframes is 1
1586 if (head->nframes == 1)
1587 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1590 err = sock_queue_rcv_skb(sk, skb);
1592 struct bcm_opt *bo = bcm_sk(sk);
1594 DBG("sock_queue_rcv_skb failed: %d\n", err);
1596 /* don't care about overflows in this statistic */
1597 bo->dropped_usr_msgs++;
1601 /* bcm_op handling: find & delete bcm_op elements */
1603 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
1608 list_for_each_entry(op, ops, list) {
1609 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1616 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1618 struct bcm_op *op, *n;
1620 list_for_each_entry_safe(op, n, ops, list) {
1621 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1622 DBG("removing rx_op %p for can_id %03X\n",
1626 * Don't care if we're bound or not (due to netdev
1627 * problems) can_rx_unregister() is always a save
1631 struct net_device *dev =
1632 dev_get_by_index(op->ifindex);
1635 can_rx_unregister(dev, op->can_id,
1636 REGMASK(op->can_id),
1637 bcm_rx_handler, op);
1641 can_rx_unregister(NULL, op->can_id,
1642 REGMASK(op->can_id),
1643 bcm_rx_handler, op);
1645 list_del(&op->list);
1647 return 1; /* done */
1651 return 0; /* not found */
1654 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1656 struct bcm_op *op, *n;
1658 list_for_each_entry_safe(op, n, ops, list) {
1659 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1660 DBG("removing rx_op %p for can_id %03X\n",
1662 list_del(&op->list);
1664 return 1; /* done */
1668 return 0; /* not found */
1671 static void bcm_remove_op(struct bcm_op *op)
1673 del_timer(&op->timer);
1674 del_timer(&op->thrtimer);
1677 if (op->last_frames)
1678 kfree(op->last_frames);
1684 module_init(bcm_module_init);
1685 module_exit(bcm_module_exit);