2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2005 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/bcm.h>
56 #include <linux/can/version.h>
62 #ifdef CONFIG_CAN_DEBUG_CORE
64 module_param(debug, int, S_IRUGO);
65 #define DBG(args...) (debug & 1 ? \
66 (printk(KERN_DEBUG "BCM %s: ", __func__), \
68 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
69 #define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
72 #define DBG_FRAME(args...)
76 /* use of last_frames[index].can_dlc */
77 #define RX_RECV 0x40 /* received data for this element */
78 #define RX_THR 0x80 /* this element has not been sent due to throttle functionality */
79 #define BCM_CAN_DLC_MASK 0x0F /* clean flags by masking with BCM_CAN_DLC_MASK */
80 #define BCM_RX_REGMASK (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)
82 #define NAME "Broadcast Manager (BCM) for LLCF"
84 static __initdata const char banner[] = BANNER(NAME);
86 MODULE_DESCRIPTION(NAME);
87 MODULE_LICENSE("Dual BSD/GPL");
88 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
90 #define GET_U64(p) (*(unsigned long long*)(p)->data) /* easy access */
93 struct list_head list;
97 unsigned long j_ival1, j_ival2, j_lastmsg;
98 unsigned long frames_abs, frames_filtered;
99 struct timer_list timer, thrtimer;
100 struct timeval ival1, ival2;
101 struct timeval rx_stamp;
106 struct can_frame *frames;
107 struct can_frame *last_frames;
114 struct list_head rx_ops;
115 struct list_head tx_ops;
116 unsigned long dropped_usr_msgs;
117 struct proc_dir_entry *bcm_proc_read;
118 char procname [9]; /* pointer printed in ASCII with \0 */
121 static struct proc_dir_entry *proc_dir = NULL;
123 static int bcm_init(struct sock *sk);
124 static void bcm_notifier(unsigned long msg, void *data);
125 static int bcm_release(struct socket *sock);
126 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
128 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
129 struct msghdr *msg, size_t size);
130 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
131 struct msghdr *msg, size_t size, int flags);
132 static unsigned int bcm_poll(struct file *file, struct socket *sock,
135 static int bcm_read_proc(char *page, char **start, off_t off,
136 int count, int *eof, void *data);
138 static void bcm_tx_timeout_handler(unsigned long data);
139 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
140 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
141 int ifindex, struct sock *sk);
142 static void bcm_can_tx(struct bcm_op *op);
144 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
145 int ifindex, struct sock *sk);
146 static void bcm_rx_handler(struct sk_buff *skb, void *op);
147 static void bcm_rx_timeout_handler(unsigned long data);
148 static void bcm_rx_thr_handler(unsigned long data);
149 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
150 struct can_frame *rxdata);
151 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
152 static void bcm_rx_starttimer(struct bcm_op *op);
153 static void bcm_rx_update_and_send(struct bcm_op *op,
154 struct can_frame *lastdata,
155 struct can_frame *rxdata);
156 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
157 struct can_frame *frames, struct timeval *tv);
159 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex);
160 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex);
161 static void bcm_remove_op(struct bcm_op *op);
162 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex);
163 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex);
165 static struct proto_ops bcm_ops = {
167 .release = bcm_release,
168 .bind = sock_no_bind,
169 .connect = bcm_connect,
170 .socketpair = sock_no_socketpair,
171 .accept = sock_no_accept,
172 .getname = sock_no_getname,
174 .ioctl = NULL, /* use can_ioctl() from af_can.c */
175 .listen = sock_no_listen,
176 .shutdown = sock_no_shutdown,
177 .setsockopt = sock_no_setsockopt,
178 .getsockopt = sock_no_getsockopt,
179 .sendmsg = bcm_sendmsg,
180 .recvmsg = bcm_recvmsg,
181 .mmap = sock_no_mmap,
182 .sendpage = sock_no_sendpage,
185 #ifdef CONFIG_CAN_BCM_USER
188 #define BCM_CAP CAP_NET_RAW
191 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
198 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
200 static struct proto bcm_proto = {
202 .owner = THIS_MODULE,
203 .obj_size = sizeof(struct bcm_sock),
207 static struct can_proto bcm_can_proto = {
210 .capability = BCM_CAP,
217 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
219 static struct can_proto bcm_can_proto = {
222 .capability = BCM_CAP,
224 .owner = THIS_MODULE,
225 .obj_size = sizeof(struct bcm_opt),
231 #define CFSIZ sizeof(struct can_frame)
232 #define OPSIZ sizeof(struct bcm_op)
233 #define MHSIZ sizeof(struct bcm_msg_head)
235 static int __init bcm_module_init(void)
239 can_proto_register(&bcm_can_proto);
241 /* create /proc/net/can/bcm directory */
242 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
245 proc_dir->owner = THIS_MODULE;
250 static void __exit bcm_module_exit(void)
252 can_proto_unregister(&bcm_can_proto);
255 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
259 /**************************************************/
260 /* initial settings at socket creation time */
261 /**************************************************/
263 static int bcm_init(struct sock *sk)
265 struct bcm_opt *bo = bcm_sk(sk);
269 bo->dropped_usr_msgs = 0;
270 bo->bcm_proc_read = NULL;
272 INIT_LIST_HEAD(&bo->tx_ops);
273 INIT_LIST_HEAD(&bo->rx_ops);
278 /**************************************************/
279 /* handling of netdevice problems */
280 /**************************************************/
282 static void bcm_notifier(unsigned long msg, void *data)
284 struct sock *sk = (struct sock *)data;
285 struct bcm_opt *bo = bcm_sk(sk);
287 DBG("called for sock %p\n", sk);
290 case NETDEV_UNREGISTER:
295 sk->sk_err = ENETDOWN;
296 if (!sock_flag(sk, SOCK_DEAD))
297 sk->sk_error_report(sk);
301 /**************************************************/
302 /* standard socket functions */
303 /**************************************************/
305 static int bcm_release(struct socket *sock)
307 struct sock *sk = sock->sk;
308 struct bcm_opt *bo = bcm_sk(sk);
309 struct bcm_op *op, *n;
311 DBG("socket %p, sk %p\n", sock, sk);
313 /* remove bcm_ops, timer, rx_unregister(), etc. */
315 list_for_each_entry_safe(op, n, &bo->tx_ops, list) {
316 DBG("removing tx_op (%p) for can_id <%03X>\n", op, op->can_id);
320 list_for_each_entry_safe(op, n, &bo->rx_ops, list) {
321 DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
323 /* Don't care if we're bound or not (due to netdev problems) */
324 /* can_rx_unregister() is always a save thing to do here */
326 struct net_device *dev = dev_get_by_index(op->ifindex);
328 can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
332 can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
338 /* remove procfs entry */
339 if ((proc_dir) && (bo->bcm_proc_read)) {
340 remove_proc_entry(bo->procname, proc_dir);
343 /* remove device notifier */
345 struct net_device *dev = dev_get_by_index(bo->ifindex);
347 can_dev_unregister(dev, bcm_notifier, sk);
357 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
360 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
361 struct sock *sk = sock->sk;
362 struct bcm_opt *bo = bcm_sk(sk);
367 /* bind a device to this socket */
368 if (addr->can_ifindex) {
369 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
371 DBG("could not find device index %d\n", addr->can_ifindex);
374 bo->ifindex = dev->ifindex;
375 can_dev_register(dev, bcm_notifier, sk); /* register notifier */
378 DBG("socket %p bound to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
380 bo->ifindex = 0; /* no notifier for ifindex = 0 ('any' CAN device) */
385 sprintf(bo->procname, "%p", sock); /* unique socket address as filename */
386 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
387 proc_dir, bcm_read_proc, sk);
393 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
394 struct msghdr *msg, size_t size)
396 struct sock *sk = sock->sk;
397 struct bcm_opt *bo = bcm_sk(sk);
398 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
399 struct bcm_msg_head msg_head;
400 int ret; /* read bytes or error codes as return value */
403 DBG("sock %p not bound\n", sk);
407 /* check for alternative ifindex for this bcm_op */
409 if (!ifindex && msg->msg_name) { /* no bound device as default */
410 struct sockaddr_can *addr = (struct sockaddr_can *)msg->msg_name;
411 if (addr->can_family != AF_CAN)
413 ifindex = addr->can_ifindex; /* ifindex from sendto() */
415 if (ifindex && !dev_get_by_index(ifindex)) {
416 DBG("device %d not found\n", ifindex);
421 /* read message head information */
423 if ((ret = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
427 DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
429 switch (msg_head.opcode) {
433 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
438 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
443 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
451 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
459 /* reuse msg_head for the reply */
460 msg_head.opcode = TX_STATUS; /* reply to TX_READ */
461 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
466 /* reuse msg_head for the reply */
467 msg_head.opcode = RX_STATUS; /* reply to RX_READ */
468 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
473 if (msg_head.nframes < 1) /* we need at least one can_frame */
476 ret = bcm_tx_send(msg, ifindex, sk);
481 DBG("Unknown opcode %d\n", msg_head.opcode);
489 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
490 struct msghdr *msg, size_t size, int flags)
492 struct sock *sk = sock->sk;
498 DBG("socket %p, sk %p\n", sock, sk);
500 noblock = flags & MSG_DONTWAIT;
501 flags &= ~MSG_DONTWAIT;
502 if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
506 DBG("delivering skbuff %p\n", skb);
511 if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
512 skb_free_datagram(sk, skb);
516 sock_recv_timestamp(msg, sk, skb);
519 msg->msg_namelen = sizeof(struct sockaddr_can);
520 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
523 DBG("freeing sock %p, skbuff %p\n", sk, skb);
524 skb_free_datagram(sk, skb);
529 static unsigned int bcm_poll(struct file *file, struct socket *sock,
532 unsigned int mask = 0;
534 DBG("socket %p\n", sock);
536 mask = datagram_poll(file, sock, wait);
540 /**************************************************/
541 /* helper functions for bcm_sendmsg() */
542 /**************************************************/
544 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
545 int ifindex, struct sock *sk)
547 struct bcm_opt *bo = bcm_sk(sk);
551 if (!ifindex) /* we need a real device to send frames */
554 if (msg_head->nframes < 1) /* we need at least one can_frame */
557 /* check the given can_id */
559 if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
561 /* update existing BCM operation */
563 DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
564 op, msg_head->can_id);
566 /* Do we need more space for the can_frames than currently */
567 /* allocated? -> This is a _really_ unusual use-case and */
568 /* therefore (complexity / locking) it is not supported. */
569 if (msg_head->nframes > op->nframes)
572 /* update can_frames content */
573 for (i = 0; i < msg_head->nframes; i++) {
574 if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0)
577 if (msg_head->flags & TX_CP_CAN_ID)
578 op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
582 /* insert new BCM operation for the given can_id */
584 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
587 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
589 DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
590 op, msg_head->can_id);
592 op->can_id = msg_head->can_id;
594 /* create array for can_frames and copy the data */
595 if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
600 for (i = 0; i < msg_head->nframes; i++) {
601 if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0) {
607 if (msg_head->flags & TX_CP_CAN_ID)
608 op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
611 op->last_frames = NULL; /* tx_ops never compare with previous received messages */
613 op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
614 op->ifindex = ifindex;
616 init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
617 init_timer(&op->thrtimer); /* currently unused in tx_ops */
619 op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
620 op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
622 /* add this bcm_op to the list of the tx_ops */
623 list_add(&op->list, &bo->tx_ops);
625 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
627 if (op->nframes != msg_head->nframes) {
628 op->nframes = msg_head->nframes;
629 op->currframe = 0; /* start multiple frame transmission with index 0 */
634 op->flags = msg_head->flags;
636 if (op->flags & TX_RESET_MULTI_IDX)
637 op->currframe = 0; /* start multiple frame transmission with index 0 */
639 if (op->flags & SETTIMER) {
641 /* set timer values */
643 op->count = msg_head->count;
644 op->ival1 = msg_head->ival1;
645 op->ival2 = msg_head->ival2;
646 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
647 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
649 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
650 op->count, op->j_ival1, op->j_ival2);
652 /* disable an active timer due to zero values? */
653 if (!op->j_ival1 && !op->j_ival2) {
654 del_timer(&op->timer);
655 DBG("TX_SETUP: SETTIMER disabled timer.\n");
659 if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
661 del_timer(&op->timer);
663 op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
664 if (op->j_ival1 && (op->count > 0)){
665 op->timer.expires = jiffies + op->j_ival1;
666 /* op->count-- is done in bcm_tx_timeout_handler */
667 DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
669 (char*) op->timer.data,
670 (unsigned int) op->timer.expires);
672 op->timer.expires = jiffies + op->j_ival2;
673 DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
675 (char*) op->timer.data,
676 (unsigned int) op->timer.expires);
679 add_timer(&op->timer);
682 if (op->flags & TX_ANNOUNCE)
685 return msg_head->nframes * CFSIZ + MHSIZ;
688 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
689 int ifindex, struct sock *sk)
691 struct bcm_opt *bo = bcm_sk(sk);
696 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
697 /* be robust against wrong usage ... */
698 msg_head->flags |= RX_FILTER_ID;
699 msg_head->nframes = 0; /* ignore trailing garbage */
702 if ((msg_head->flags & RX_RTR_FRAME) &&
703 ((msg_head->nframes != 1) || (!(msg_head->can_id & CAN_RTR_FLAG)))) {
705 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
709 /* check the given can_id */
711 if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
713 /* update existing BCM operation */
715 DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
716 op, msg_head->can_id);
718 /* Do we need more space for the can_frames than currently */
719 /* allocated? -> This is a _really_ unusual use-case and */
720 /* therefore (complexity / locking) it is not supported. */
721 if (msg_head->nframes > op->nframes)
724 if (msg_head->nframes) {
725 /* update can_frames content */
726 if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ) < 0))
729 /* clear received can_frames to indicate 'nothing received' */
730 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
733 op->nframes = msg_head->nframes;
734 /* Only an update -> do not call can_rx_register() at end of RX_SETUP */
738 /* insert new BCM operation for the given can_id */
740 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
743 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
745 DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
746 op, msg_head->can_id);
748 op->can_id = msg_head->can_id;
749 op->nframes = msg_head->nframes;
751 if (msg_head->nframes) {
753 /* create array for can_frames and copy the data */
754 if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
759 if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ)) < 0) {
765 /* create array for received can_frames */
766 if (!(op->last_frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
772 /* clear received can_frames to indicate 'nothing received' */
773 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
775 /* op->frames = NULL due to memset */
777 /* even when we have the RX_FILTER_ID case, we need to store the last frame */
778 /* for the throttle functionality */
780 /* create array for received can_frames */
781 if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
786 /* clear received can_frames to indicate 'nothing received' */
787 memset(op->last_frames, 0, CFSIZ);
790 op->sk = sk; /* bcm_delete_rx_op() needs this */
791 op->ifindex = ifindex;
793 init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
794 init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
796 op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
797 op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
799 op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
800 op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
801 op->thrtimer.expires = 0; /* mark disabled timer */
803 /* add this bcm_op to the list of the tx_ops */
804 list_add(&op->list, &bo->rx_ops);
806 do_rx_register = 1; /* call can_rx_register() at end of RX_SETUP */
808 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
813 op->flags = msg_head->flags;
815 if (op->flags & RX_RTR_FRAME) {
817 /* no timers in RTR-mode */
818 del_timer(&op->thrtimer);
819 del_timer(&op->timer);
821 /* funny feature in RX(!)_SETUP only for RTR-mode: */
822 /* copy can_id into frame BUT without RTR-flag to */
823 /* prevent a full-load-loopback-test ... ;-] */
824 if ((op->flags & TX_CP_CAN_ID) ||
825 (op->frames[0].can_id == op->can_id))
826 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
829 if (op->flags & SETTIMER) {
831 /* set timer value */
833 op->ival1 = msg_head->ival1;
834 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
835 op->ival2 = msg_head->ival2;
836 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
838 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
839 op->j_ival1, op->j_ival2);
841 /* disable an active timer due to zero value? */
843 del_timer(&op->timer);
844 DBG("RX_SETUP: disabled timer for rx timeouts.\n");
847 /* free currently blocked msgs ? */
848 if (op->thrtimer.expires) { /* running throttle timer? */
849 DBG("RX_SETUP: unblocking throttled msgs.\n");
850 del_timer(&op->thrtimer);
851 op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
852 add_timer(&op->thrtimer);
854 /* if (op->j_ival2) is zero, no (new) throttling will happen */
855 /* see bcm_rx_update_and_send() and bcm_rx_thr_handler() */
858 if ((op->flags & STARTTIMER) && op->j_ival1) {
860 del_timer(&op->timer);
862 op->timer.expires = jiffies + op->j_ival1;
864 DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
865 (char *) op->timer.function,
866 (char *) op->timer.data,
867 (unsigned int) op->timer.expires);
869 add_timer(&op->timer);
873 /* now we can register for can_ids, if we added a new bcm_op */
874 if (do_rx_register) {
875 DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
878 struct net_device *dev = dev_get_by_index(ifindex);
881 can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
885 can_rx_register(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
888 return msg_head->nframes * CFSIZ + MHSIZ;
891 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
894 struct net_device *dev;
897 /* just copy and send one can_frame */
899 if (!ifindex) /* we need a real device to send frames */
902 skb = alloc_skb(CFSIZ, GFP_KERNEL);
907 if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ)) < 0) {
912 DBG_FRAME("BCM: TX_SEND: sending frame",
913 (struct can_frame *)skb->data);
914 dev = dev_get_by_index(ifindex);
923 can_send(skb, 1); /* send with loopback */
926 return CFSIZ + MHSIZ;
929 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex)
934 if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
936 DBG("TRX_READ: sending status for can_id <%03X>\n", msg_head->can_id);
937 /* put current values into msg_head */
938 msg_head->flags = op->flags;
939 msg_head->count = op->count;
940 msg_head->ival1 = op->ival1;
941 msg_head->ival2 = op->ival2;
942 msg_head->nframes = op->nframes;
944 bcm_send_to_user(op, msg_head, op->frames, NULL);
950 DBG("TRX_READ: did not find op for can_id <%03X>\n",
958 /**************************************************/
959 /* procfs functions */
960 /**************************************************/
962 static char *bcm_proc_getifname(int ifindex)
964 struct net_device *dev;
969 dev = __dev_get_by_index(ifindex); /* no usage counting */
976 static int bcm_read_proc(char *page, char **start, off_t off,
977 int count, int *eof, void *data)
980 struct sock *sk = (struct sock *)data;
981 struct bcm_opt *bo = bcm_sk(sk);
984 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p", sk->sk_socket);
985 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
986 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
987 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu", bo->dropped_usr_msgs);
988 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s", bcm_proc_getifname(bo->ifindex));
989 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
991 list_for_each_entry(op, &bo->rx_ops, list) {
993 unsigned long reduction;
995 /* print only active entries & prevent division by zero */
999 len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X %-5s ",
1000 op->can_id, bcm_proc_getifname(op->ifindex));
1001 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1002 op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
1004 len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
1007 len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
1009 len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
1010 op->frames_filtered, op->frames_abs);
1012 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1014 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1015 (reduction == 100)?"near ":"", reduction);
1017 if (len > PAGE_SIZE - 200) {
1018 /* mark output cut off */
1019 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1024 list_for_each_entry(op, &bo->tx_ops, list) {
1026 len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X %s [%d] ",
1027 op->can_id, bcm_proc_getifname(op->ifindex), op->nframes);
1029 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
1032 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
1034 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
1036 if (len > PAGE_SIZE - 100) {
1037 /* mark output cut off */
1038 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1043 len += snprintf(page + len, PAGE_SIZE - len, "\n");
1049 /**************************************************/
1050 /* bcm_op handling tx path */
1051 /**************************************************/
1053 static void bcm_can_tx(struct bcm_op *op)
1055 struct sk_buff *skb;
1056 struct net_device *dev;
1057 struct can_frame *cf = &op->frames[op->currframe];
1059 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1062 return; /* no target device -> exit */
1064 dev = dev_get_by_index(op->ifindex);
1067 return; /* should this bcm_op remove itself here? */
1069 skb = alloc_skb(CFSIZ,
1070 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1073 goto out; /* no memory */
1075 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1079 can_send(skb, 1); /* send with loopback */
1082 op->frames_abs++; /* statistics */
1084 /* reached last frame? */
1085 if (op->currframe >= op->nframes)
1091 static void bcm_tx_timeout_handler(unsigned long data)
1093 struct bcm_op *op = (struct bcm_op*)data;
1095 DBG("Called with bcm_op (%p)\n", op);
1097 if (op->j_ival1 && (op->count > 0)) {
1101 if (!op->count && (op->flags & TX_COUNTEVT)) { /* create notification to user? */
1103 struct bcm_msg_head msg_head;
1105 DBG("sending TX_EXPIRED for can_id <%03X>\n", op->can_id);
1107 msg_head.opcode = TX_EXPIRED;
1108 msg_head.flags = op->flags;
1109 msg_head.count = op->count;
1110 msg_head.ival1 = op->ival1;
1111 msg_head.ival2 = op->ival2;
1112 msg_head.can_id = op->can_id;
1113 msg_head.nframes = 0;
1115 bcm_send_to_user(op, &msg_head, NULL, NULL);
1119 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1120 op->count, op->j_ival1, op->j_ival2);
1122 if (op->j_ival1 && (op->count > 0)) {
1124 op->timer.expires = jiffies + op->j_ival1;
1125 add_timer(&op->timer);
1127 DBG("adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
1129 (char*) op->timer.data,
1130 (unsigned int) op->timer.expires);
1132 bcm_can_tx(op); /* send (next) frame */
1135 op->timer.expires = jiffies + op->j_ival2;
1136 add_timer(&op->timer);
1138 DBG("adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
1140 (char*) op->timer.data,
1141 (unsigned int) op->timer.expires);
1143 bcm_can_tx(op); /* send (next) frame */
1145 DBG("no timer restart\n");
1152 /**************************************************/
1153 /* bcm_op handling rx path */
1154 /**************************************************/
1156 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1158 struct bcm_op *op = (struct bcm_op*)data;
1159 struct can_frame rxframe;
1162 del_timer(&op->timer); /* disable timeout */
1164 DBG("Called with bcm_op (%p)\n", op);
1166 if (skb->len == sizeof(rxframe)) {
1167 memcpy(&rxframe, skb->data, sizeof(rxframe));
1168 skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
1169 op->rx_ifindex = skb->dev->ifindex; /* save originator for recvfrom() */
1170 op->frames_abs++; /* statistics */
1172 DBG("got can_frame with can_id <%03X>\n", rxframe.can_id);
1174 DBG("Wrong skb->len = %d\n", skb->len);
1179 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1181 if (op->can_id != rxframe.can_id) {
1182 DBG("ERROR! Got wrong can_id <%03X>! Expected <%03X>.\n",
1183 rxframe.can_id, op->can_id);
1187 if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
1188 DBG("RTR-request\n");
1189 bcm_can_tx(op); /* send op->frames[0] to CAN device */
1193 if (op->flags & RX_FILTER_ID) { /* the easiest case */
1194 DBG("Easy does it with RX_FILTER_ID\n");
1195 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1196 bcm_rx_starttimer(op);
1200 if (op->nframes == 1) { /* simple compare with index 0 */
1201 DBG("Simple compare\n");
1202 bcm_rx_cmp_to_index(op, 0, &rxframe);
1203 bcm_rx_starttimer(op);
1207 if (op->nframes > 1) { /* multiplex compare */
1209 DBG("Multiplex compare\n");
1210 /* find the first multiplex mask that fits */
1211 /* MUX-mask is in index 0 */
1213 for (i=1; i < op->nframes; i++) {
1215 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1216 (GET_U64(&op->frames[0]) & GET_U64(&op->frames[i]))) {
1217 DBG("found MUX index %d\n", i);
1218 bcm_rx_cmp_to_index(op, i, &rxframe);
1222 bcm_rx_starttimer(op);
1226 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1227 struct can_frame *rxdata)
1229 /* no one uses the MSBs of can_dlc for comparation, */
1230 /* so we use it here to detect the first time of reception */
1232 if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
1233 DBG("first time :)\n");
1234 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1238 /* do a real check in can_data */
1240 DBG("op->frames[index].data = 0x%016llx\n", GET_U64(&op->frames[index]));
1241 DBG("op->last_frames[index].data = 0x%016llx\n",
1242 GET_U64(&op->last_frames[index]));
1243 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1245 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1246 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1247 DBG("relevant data change :)\n");
1248 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1253 if (op->flags & RX_CHECK_DLC) {
1255 /* do a real check in dlc */
1257 if (rxdata->can_dlc != (op->last_frames[index].can_dlc & BCM_CAN_DLC_MASK)) {
1258 DBG("dlc change :)\n");
1259 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1263 DBG("no relevant change :(\n");
1266 static void bcm_rx_update_and_send(struct bcm_op *op,
1267 struct can_frame *lastdata,
1268 struct can_frame *rxdata)
1270 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1272 memcpy(lastdata, rxdata, CFSIZ);
1273 lastdata->can_dlc |= RX_RECV; /* mark as used */
1275 /* throttle bcm_rx_changed ? */
1276 if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
1277 ((op->j_ival2) && (nexttx > jiffies))) { /* we have to wait */
1279 lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
1281 if (!(op->thrtimer.expires)) { /* start only the first time */
1282 op->thrtimer.expires = nexttx;
1283 add_timer(&op->thrtimer);
1285 DBG("adding thrtimer. func=%p data=(%p) exp=0x%08X\n",
1286 op->thrtimer.function,
1287 (char*) op->thrtimer.data,
1288 (unsigned int) op->thrtimer.expires);
1291 bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
1294 static void bcm_rx_starttimer(struct bcm_op *op)
1296 if (op->flags & RX_NO_AUTOTIMER)
1301 op->timer.expires = jiffies + op->j_ival1;
1303 DBG("adding rx timeout timer ival1. func=%p data=(%p) exp=0x%08X\n",
1305 (char*) op->timer.data,
1306 (unsigned int) op->timer.expires);
1308 add_timer(&op->timer);
1313 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1315 struct bcm_msg_head head;
1317 op->j_lastmsg = jiffies;
1318 op->frames_filtered++; /* statistics */
1320 if (op->frames_filtered > ULONG_MAX/100)
1321 op->frames_filtered = op->frames_abs = 0; /* restart - spinlock ? */
1323 DBG("setting j_lastmsg to 0x%08X for rx_op(%p)\n",
1324 (unsigned int) op->j_lastmsg, op);
1325 DBG("sending notification\n");
1327 head.opcode = RX_CHANGED;
1328 head.flags = op->flags;
1329 head.count = op->count;
1330 head.ival1 = op->ival1;
1331 head.ival2 = op->ival2;
1332 head.can_id = op->can_id;
1335 bcm_send_to_user(op, &head, data, &op->rx_stamp);
1339 static void bcm_rx_timeout_handler(unsigned long data)
1341 struct bcm_op *op = (struct bcm_op*)data;
1342 struct bcm_msg_head msg_head;
1344 DBG("sending RX_TIMEOUT for can_id <%03X>. op is (%p)\n", op->can_id, op);
1346 msg_head.opcode = RX_TIMEOUT;
1347 msg_head.flags = op->flags;
1348 msg_head.count = op->count;
1349 msg_head.ival1 = op->ival1;
1350 msg_head.ival2 = op->ival2;
1351 msg_head.can_id = op->can_id;
1352 msg_head.nframes = 0;
1354 bcm_send_to_user(op, &msg_head, NULL, NULL);
1356 /* no restart of the timer is done here! */
1358 /* if the user wants to be informed, when cyclic CAN-Messages come back ... */
1359 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1360 /* clear received can_frames to indicate 'nothing received' */
1361 memset(op->last_frames, 0, op->nframes * CFSIZ);
1362 DBG("RX_ANNOUNCE_RESTART\n");
1367 static void bcm_rx_thr_handler(unsigned long data)
1369 struct bcm_op *op = (struct bcm_op*)data;
1372 op->thrtimer.expires = 0; /* mark disabled / consumed timer */
1374 if (op->nframes > 1){
1376 DBG("sending MUX RX_CHANGED for can_id <%03X>. op is (%p)\n",
1378 /* for MUX filter we start at index 1 */
1379 for (i=1; i<op->nframes; i++){
1380 if ((op->last_frames) && (op->last_frames[i].can_dlc & RX_THR)){
1381 op->last_frames[i].can_dlc &= ~RX_THR;
1382 bcm_rx_changed(op, &op->last_frames[i]);
1387 DBG("sending simple RX_CHANGED for can_id <%03X>. op is (%p)\n",
1389 /* for RX_FILTER_ID and simple filter */
1390 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1391 op->last_frames[0].can_dlc &= ~RX_THR;
1392 bcm_rx_changed(op, &op->last_frames[0]);
1397 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1398 struct can_frame *frames, struct timeval *tv)
1400 struct sk_buff *skb;
1401 struct can_frame *firstframe;
1402 struct sock *sk = op->sk;
1403 int datalen = head->nframes * CFSIZ;
1404 struct sockaddr_can *addr;
1407 skb = alloc_skb(sizeof(*head) + datalen,
1408 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1412 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1413 firstframe = (struct can_frame *) skb->tail; /* can_frames starting here */
1416 skb_set_timestamp(skb, tv); /* restore timestamp */
1418 addr = (struct sockaddr_can *)skb->cb;
1419 memset(addr, 0, sizeof(*addr));
1420 addr->can_family = AF_CAN;
1421 addr->can_ifindex = op->rx_ifindex; /* restore originator for recvfrom() */
1424 memcpy(skb_put(skb, datalen), frames, datalen);
1426 /* the BCM uses the can_dlc-element of the can_frame structure
1427 for internal purposes. This is only relevant for updates
1428 that are generated by the BCM, where nframes is 1
1430 if (head->nframes == 1)
1431 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1433 if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
1434 struct bcm_opt *bo = bcm_sk(sk);
1435 DBG("sock_queue_rcv_skb failed: %d\n", err);
1437 bo->dropped_usr_msgs++; /* don't care about overflows */
1441 /**************************************************/
1442 /* bcm_op handling: find & delete bcm_op elements */
1443 /**************************************************/
1445 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex)
1449 list_for_each_entry(op, ops, list)
1450 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1456 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1458 struct bcm_op *op, *n;
1460 list_for_each_entry_safe(op, n, ops, list) {
1461 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1462 DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
1464 /* Don't care if we're bound or not (due to netdev problems) */
1465 /* can_rx_unregister() is always a save thing to do here */
1467 struct net_device *dev = dev_get_by_index(op->ifindex);
1469 can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
1473 can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
1475 list_del(&op->list);
1477 return 1; /* done */
1481 return 0; /* not found */
1484 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1486 struct bcm_op *op, *n;
1488 list_for_each_entry_safe(op, n, ops, list) {
1489 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1490 DBG("removing rx_op (%p) for can_id <%03X>\n",
1492 list_del(&op->list);
1494 return 1; /* done */
1498 return 0; /* not found */
1501 static void bcm_remove_op(struct bcm_op *op)
1503 del_timer(&op->timer);
1504 del_timer(&op->thrtimer);
1507 if (op->last_frames)
1508 kfree(op->last_frames);
1514 module_init(bcm_module_init);
1515 module_exit(bcm_module_exit);