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 " 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),
233 #define CFSIZ sizeof(struct can_frame)
234 #define OPSIZ sizeof(struct bcm_op)
235 #define MHSIZ sizeof(struct bcm_msg_head)
237 static int __init bcm_module_init(void)
241 can_proto_register(&bcm_can_proto);
243 /* create /proc/net/can/bcm directory */
244 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
247 proc_dir->owner = THIS_MODULE;
252 static void __exit bcm_module_exit(void)
254 can_proto_unregister(&bcm_can_proto);
257 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
261 /**************************************************/
262 /* initial settings at socket creation time */
263 /**************************************************/
265 static int bcm_init(struct sock *sk)
267 struct bcm_opt *bo = bcm_sk(sk);
271 bo->dropped_usr_msgs = 0;
272 bo->bcm_proc_read = NULL;
274 INIT_LIST_HEAD(&bo->tx_ops);
275 INIT_LIST_HEAD(&bo->rx_ops);
280 /**************************************************/
281 /* handling of netdevice problems */
282 /**************************************************/
284 static void bcm_notifier(unsigned long msg, void *data)
286 struct sock *sk = (struct sock *)data;
287 struct bcm_opt *bo = bcm_sk(sk);
289 DBG("called for sock %p\n", sk);
292 case NETDEV_UNREGISTER:
297 sk->sk_err = ENETDOWN;
298 if (!sock_flag(sk, SOCK_DEAD))
299 sk->sk_error_report(sk);
303 /**************************************************/
304 /* standard socket functions */
305 /**************************************************/
307 static int bcm_release(struct socket *sock)
309 struct sock *sk = sock->sk;
310 struct bcm_opt *bo = bcm_sk(sk);
311 struct bcm_op *op, *next;
313 DBG("socket %p, sk %p\n", sock, sk);
315 /* remove bcm_ops, timer, rx_unregister(), etc. */
317 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
318 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
322 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
323 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
325 /* Don't care if we're bound or not (due to netdev problems) */
326 /* can_rx_unregister() is always a save thing to do here */
328 struct net_device *dev = dev_get_by_index(op->ifindex);
330 can_rx_unregister(dev, op->can_id,
336 can_rx_unregister(NULL, op->can_id,
343 /* remove procfs entry */
344 if ((proc_dir) && (bo->bcm_proc_read)) {
345 remove_proc_entry(bo->procname, proc_dir);
348 /* remove device notifier */
350 struct net_device *dev = dev_get_by_index(bo->ifindex);
352 can_dev_unregister(dev, bcm_notifier, sk);
362 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
365 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
366 struct sock *sk = sock->sk;
367 struct bcm_opt *bo = bcm_sk(sk);
372 /* bind a device to this socket */
373 if (addr->can_ifindex) {
374 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
376 DBG("could not find device index %d\n",
380 bo->ifindex = dev->ifindex;
381 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
384 DBG("socket %p bound to device %s (idx %d)\n",
385 sock, dev->name, dev->ifindex);
387 /* no notifier for ifindex = 0 ('any' CAN device) */
394 /* unique socket address as filename */
395 sprintf(bo->procname, "%p", sock);
396 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
404 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
405 struct msghdr *msg, size_t size)
407 struct sock *sk = sock->sk;
408 struct bcm_opt *bo = bcm_sk(sk);
409 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
410 struct bcm_msg_head msg_head;
411 int ret; /* read bytes or error codes as return value */
414 DBG("sock %p not bound\n", sk);
418 /* check for alternative ifindex for this bcm_op */
420 if (!ifindex && msg->msg_name) { /* no bound device as default */
421 struct sockaddr_can *addr =
422 (struct sockaddr_can *)msg->msg_name;
423 if (addr->can_family != AF_CAN)
425 ifindex = addr->can_ifindex; /* ifindex from sendto() */
427 if (ifindex && !dev_get_by_index(ifindex)) {
428 DBG("device %d not found\n", ifindex);
433 /* read message head information */
435 if ((ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov,
439 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
441 switch (msg_head.opcode) {
445 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
450 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
455 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
463 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
471 /* reuse msg_head for the reply */
472 msg_head.opcode = TX_STATUS; /* reply to TX_READ */
473 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
478 /* reuse msg_head for the reply */
479 msg_head.opcode = RX_STATUS; /* reply to RX_READ */
480 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
485 if (msg_head.nframes < 1) /* we need at least one can_frame */
488 ret = bcm_tx_send(msg, ifindex, sk);
493 DBG("Unknown opcode %d\n", msg_head.opcode);
501 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
502 struct msghdr *msg, size_t size, int flags)
504 struct sock *sk = sock->sk;
510 DBG("socket %p, sk %p\n", sock, sk);
512 noblock = flags & MSG_DONTWAIT;
513 flags &= ~MSG_DONTWAIT;
514 if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
518 DBG("delivering skbuff %p\n", skb);
523 if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
524 skb_free_datagram(sk, skb);
528 sock_recv_timestamp(msg, sk, skb);
531 msg->msg_namelen = sizeof(struct sockaddr_can);
532 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
535 DBG("freeing sock %p, skbuff %p\n", sk, skb);
536 skb_free_datagram(sk, skb);
541 static unsigned int bcm_poll(struct file *file, struct socket *sock,
544 unsigned int mask = 0;
546 DBG("socket %p\n", sock);
548 mask = datagram_poll(file, sock, wait);
552 /**************************************************/
553 /* helper functions for bcm_sendmsg() */
554 /**************************************************/
556 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
557 int ifindex, struct sock *sk)
559 struct bcm_opt *bo = bcm_sk(sk);
563 if (!ifindex) /* we need a real device to send frames */
566 if (msg_head->nframes < 1) /* we need at least one can_frame */
569 /* check the given can_id */
571 if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
573 /* update existing BCM operation */
575 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
576 op, msg_head->can_id);
578 /* Do we need more space for the can_frames than currently */
579 /* allocated? -> This is a _really_ unusual use-case and */
580 /* therefore (complexity / locking) it is not supported. */
581 if (msg_head->nframes > op->nframes)
584 /* update can_frames content */
585 for (i = 0; i < msg_head->nframes; i++) {
586 if ((err = memcpy_fromiovec((u8*)&op->frames[i],
587 msg->msg_iov, CFSIZ)) < 0)
590 if (msg_head->flags & TX_CP_CAN_ID) {
591 /* copy can_id into frame */
592 op->frames[i].can_id = msg_head->can_id;
597 /* insert new BCM operation for the given can_id */
599 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
602 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
604 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
605 op, msg_head->can_id);
607 op->can_id = msg_head->can_id;
609 /* create array for can_frames and copy the data */
610 if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
616 for (i = 0; i < msg_head->nframes; i++) {
617 if ((err = memcpy_fromiovec((u8*)&op->frames[i],
625 if (msg_head->flags & TX_CP_CAN_ID) {
626 /* copy can_id into frame */
627 op->frames[i].can_id = msg_head->can_id;
631 /* tx_ops never compare with previous received messages */
632 op->last_frames = NULL;
634 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
637 op->ifindex = ifindex;
639 /* initialize uninitialized (kmalloc) structure */
640 init_timer(&op->timer);
642 /* currently unused in tx_ops */
643 init_timer(&op->thrtimer);
645 /* handler for tx_ops */
646 op->timer.function = bcm_tx_timeout_handler;
648 /* timer.data points to this op-structure */
649 op->timer.data = (unsigned long)op;
651 /* add this bcm_op to the list of the tx_ops */
652 list_add(&op->list, &bo->tx_ops);
654 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
656 if (op->nframes != msg_head->nframes) {
657 op->nframes = msg_head->nframes;
658 /* start multiple frame transmission with index 0 */
664 op->flags = msg_head->flags;
666 if (op->flags & TX_RESET_MULTI_IDX) {
667 /* start multiple frame transmission with index 0 */
671 if (op->flags & SETTIMER) {
673 /* set timer values */
675 op->count = msg_head->count;
676 op->ival1 = msg_head->ival1;
677 op->ival2 = msg_head->ival2;
678 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
679 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
681 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
682 op->count, op->j_ival1, op->j_ival2);
684 /* disable an active timer due to zero values? */
685 if (!op->j_ival1 && !op->j_ival2) {
686 del_timer(&op->timer);
687 DBG("TX_SETUP: SETTIMER disabled timer.\n");
691 if ((op->flags & STARTTIMER) &&
692 ((op->j_ival1 && op->count) || op->j_ival2)) {
694 del_timer(&op->timer);
696 /* spec: send can_frame when starting timer */
697 op->flags |= TX_ANNOUNCE;
699 if (op->j_ival1 && (op->count > 0)){
700 op->timer.expires = jiffies + op->j_ival1;
701 /* op->count-- is done in bcm_tx_timeout_handler */
702 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
705 (char*) op->timer.data,
706 (unsigned int) op->timer.expires);
708 op->timer.expires = jiffies + op->j_ival2;
709 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
712 (char*) op->timer.data,
713 (unsigned int) op->timer.expires);
716 add_timer(&op->timer);
719 if (op->flags & TX_ANNOUNCE)
722 return msg_head->nframes * CFSIZ + MHSIZ;
725 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
726 int ifindex, struct sock *sk)
728 struct bcm_opt *bo = bcm_sk(sk);
733 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
734 /* be robust against wrong usage ... */
735 msg_head->flags |= RX_FILTER_ID;
736 msg_head->nframes = 0; /* ignore trailing garbage */
739 if ((msg_head->flags & RX_RTR_FRAME) &&
740 ((msg_head->nframes != 1) ||
741 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
743 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
747 /* check the given can_id */
749 if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
751 /* update existing BCM operation */
753 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
754 op, msg_head->can_id);
756 /* Do we need more space for the can_frames than currently */
757 /* allocated? -> This is a _really_ unusual use-case and */
758 /* therefore (complexity / locking) it is not supported. */
759 if (msg_head->nframes > op->nframes)
762 if (msg_head->nframes) {
763 /* update can_frames content */
764 if ((err = memcpy_fromiovec((u8*)op->frames,
770 /* clear last_frames to indicate 'nothing received' */
771 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
774 op->nframes = msg_head->nframes;
775 /* Only an update -> do not call can_rx_register() */
779 /* insert new BCM operation for the given can_id */
781 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
784 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
786 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
787 op, msg_head->can_id);
789 op->can_id = msg_head->can_id;
790 op->nframes = msg_head->nframes;
792 if (msg_head->nframes) {
794 /* create array for can_frames and copy the data */
795 if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
801 if ((err = memcpy_fromiovec((u8*)op->frames,
810 /* create array for received can_frames */
811 if (!(op->last_frames = kmalloc(msg_head->nframes
819 /* clear last_frames to indicate 'nothing received' */
820 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
822 /* op->frames = NULL due to memset */
824 /* even when we have the RX_FILTER_ID case, we need */
825 /* to store the last frame for the throttle feature */
827 /* create array for received can_frames */
828 if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
833 /* clear last_frames to indicate 'nothing received' */
834 memset(op->last_frames, 0, CFSIZ);
837 op->sk = sk; /* bcm_delete_rx_op() needs this */
838 op->ifindex = ifindex;
840 /* initialize uninitialized (kmalloc) structure */
841 init_timer(&op->timer);
843 /* init throttle timer for RX_CHANGED */
844 init_timer(&op->thrtimer);
846 /* handler for rx timeouts */
847 op->timer.function = bcm_rx_timeout_handler;
849 /* timer.data points to this op-structure */
850 op->timer.data = (unsigned long)op;
852 /* handler for RX_CHANGED throttle timeouts */
853 op->thrtimer.function = bcm_rx_thr_handler;
855 /* timer.data points to this op-structure */
856 op->thrtimer.data = (unsigned long)op;
858 op->thrtimer.expires = 0; /* mark disabled timer */
860 /* add this bcm_op to the list of the tx_ops */
861 list_add(&op->list, &bo->rx_ops);
863 do_rx_register = 1; /* call can_rx_register() */
865 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
870 op->flags = msg_head->flags;
872 if (op->flags & RX_RTR_FRAME) {
874 /* no timers in RTR-mode */
875 del_timer(&op->thrtimer);
876 del_timer(&op->timer);
878 /* funny feature in RX(!)_SETUP only for RTR-mode: */
879 /* copy can_id into frame BUT without RTR-flag to */
880 /* prevent a full-load-loopback-test ... ;-] */
881 if ((op->flags & TX_CP_CAN_ID) ||
882 (op->frames[0].can_id == op->can_id))
883 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
886 if (op->flags & SETTIMER) {
888 /* set timer value */
890 op->ival1 = msg_head->ival1;
891 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
892 op->ival2 = msg_head->ival2;
893 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
895 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
896 op->j_ival1, op->j_ival2);
898 /* disable an active timer due to zero value? */
900 del_timer(&op->timer);
901 DBG("RX_SETUP: disabled timer rx timeouts.\n");
904 /* free currently blocked msgs ? */
905 if (op->thrtimer.expires) { /* blocked by timer? */
906 DBG("RX_SETUP: unblocking throttled msgs.\n");
907 del_timer(&op->thrtimer);
908 /* send blocked msgs hereafter */
909 op->thrtimer.expires = jiffies + 2;
910 add_timer(&op->thrtimer);
912 /* if (op->j_ival2) is zero, no (new) throttling */
913 /* will happen. For details see functions */
914 /* bcm_rx_update_and_send() and bcm_rx_thr_handler() */
917 if ((op->flags & STARTTIMER) && op->j_ival1) {
919 del_timer(&op->timer);
921 op->timer.expires = jiffies + op->j_ival1;
923 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
925 (char *) op->timer.function,
926 (char *) op->timer.data,
927 (unsigned int) op->timer.expires);
929 add_timer(&op->timer);
933 /* now we can register for can_ids, if we added a new bcm_op */
934 if (do_rx_register) {
935 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
936 "rx_op is %p\n", op->can_id, op);
939 struct net_device *dev = dev_get_by_index(ifindex);
942 can_rx_register(dev, op->can_id,
944 bcm_rx_handler, op, IDENT);
948 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
949 bcm_rx_handler, op, IDENT);
952 return msg_head->nframes * CFSIZ + MHSIZ;
955 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
958 struct net_device *dev;
961 /* just copy and send one can_frame */
963 if (!ifindex) /* we need a real device to send frames */
966 skb = alloc_skb(CFSIZ, GFP_KERNEL);
971 if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov,
977 DBG_FRAME("BCM: TX_SEND: sending frame",
978 (struct can_frame *)skb->data);
979 dev = dev_get_by_index(ifindex);
988 can_send(skb, 1); /* send with loopback */
991 return CFSIZ + MHSIZ;
994 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
1000 if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
1002 DBG("TRX_READ: sending status for can_id %03X\n",
1004 /* put current values into msg_head */
1005 msg_head->flags = op->flags;
1006 msg_head->count = op->count;
1007 msg_head->ival1 = op->ival1;
1008 msg_head->ival2 = op->ival2;
1009 msg_head->nframes = op->nframes;
1011 bcm_send_to_user(op, msg_head, op->frames, NULL);
1017 DBG("TRX_READ: did not find op for can_id %03X\n",
1025 /**************************************************/
1026 /* procfs functions */
1027 /**************************************************/
1029 static char *bcm_proc_getifname(int ifindex)
1031 struct net_device *dev;
1036 dev = __dev_get_by_index(ifindex); /* no usage counting */
1043 static int bcm_read_proc(char *page, char **start, off_t off,
1044 int count, int *eof, void *data)
1047 struct sock *sk = (struct sock *)data;
1048 struct bcm_opt *bo = bcm_sk(sk);
1051 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
1053 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
1054 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
1055 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
1056 bo->dropped_usr_msgs);
1057 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
1058 bcm_proc_getifname(bo->ifindex));
1059 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
1061 list_for_each_entry(op, &bo->rx_ops, list) {
1063 unsigned long reduction;
1065 /* print only active entries & prevent division by zero */
1066 if (!op->frames_abs)
1069 len += snprintf(page + len, PAGE_SIZE - len,
1070 "rx_op: %03X %-5s ",
1071 op->can_id, bcm_proc_getifname(op->ifindex));
1072 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1074 (op->flags & RX_CHECK_DLC)?'d':' ');
1076 len += snprintf(page + len, PAGE_SIZE - len,
1077 "timeo=%ld ", op->j_ival1);
1080 len += snprintf(page + len, PAGE_SIZE - len,
1081 "thr=%ld ", op->j_ival2);
1083 len += snprintf(page + len, PAGE_SIZE - len,
1084 "# recv %ld (%ld) => reduction: ",
1085 op->frames_filtered, op->frames_abs);
1087 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1089 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1090 (reduction == 100)?"near ":"", reduction);
1092 if (len > PAGE_SIZE - 200) {
1093 /* mark output cut off */
1094 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1099 list_for_each_entry(op, &bo->tx_ops, list) {
1101 len += snprintf(page + len, PAGE_SIZE - len,
1102 "tx_op: %03X %s [%d] ",
1103 op->can_id, bcm_proc_getifname(op->ifindex),
1106 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
1110 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
1113 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
1116 if (len > PAGE_SIZE - 100) {
1117 /* mark output cut off */
1118 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1123 len += snprintf(page + len, PAGE_SIZE - len, "\n");
1129 /**************************************************/
1130 /* bcm_op handling tx path */
1131 /**************************************************/
1133 static void bcm_can_tx(struct bcm_op *op)
1135 struct sk_buff *skb;
1136 struct net_device *dev;
1137 struct can_frame *cf = &op->frames[op->currframe];
1139 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1142 return; /* no target device -> exit */
1144 dev = dev_get_by_index(op->ifindex);
1147 return; /* should this bcm_op remove itself here? */
1149 skb = alloc_skb(CFSIZ,
1150 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1153 goto out; /* no memory */
1155 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1159 can_send(skb, 1); /* send with loopback */
1162 op->frames_abs++; /* statistics */
1164 /* reached last frame? */
1165 if (op->currframe >= op->nframes)
1171 static void bcm_tx_timeout_handler(unsigned long data)
1173 struct bcm_op *op = (struct bcm_op*)data;
1175 DBG("Called with bcm_op %p\n", op);
1177 if (op->j_ival1 && (op->count > 0)) {
1181 if (!op->count && (op->flags & TX_COUNTEVT)) {
1182 /* create notification to user */
1184 struct bcm_msg_head msg_head;
1186 DBG("sending TX_EXPIRED for can_id %03X\n",
1189 msg_head.opcode = TX_EXPIRED;
1190 msg_head.flags = op->flags;
1191 msg_head.count = op->count;
1192 msg_head.ival1 = op->ival1;
1193 msg_head.ival2 = op->ival2;
1194 msg_head.can_id = op->can_id;
1195 msg_head.nframes = 0;
1197 bcm_send_to_user(op, &msg_head, NULL, NULL);
1201 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1202 op->count, op->j_ival1, op->j_ival2);
1204 if (op->j_ival1 && (op->count > 0)) {
1206 op->timer.expires = jiffies + op->j_ival1;
1207 add_timer(&op->timer);
1209 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
1211 (char*) op->timer.data,
1212 (unsigned int) op->timer.expires);
1214 bcm_can_tx(op); /* send (next) frame */
1217 op->timer.expires = jiffies + op->j_ival2;
1218 add_timer(&op->timer);
1220 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
1222 (char*) op->timer.data,
1223 (unsigned int) op->timer.expires);
1225 bcm_can_tx(op); /* send (next) frame */
1227 DBG("no timer restart\n");
1234 /**************************************************/
1235 /* bcm_op handling rx path */
1236 /**************************************************/
1238 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1240 struct bcm_op *op = (struct bcm_op*)data;
1241 struct can_frame rxframe;
1244 del_timer(&op->timer); /* disable timeout */
1246 DBG("Called with bcm_op %p\n", op);
1248 if (skb->len == sizeof(rxframe)) {
1249 memcpy(&rxframe, skb->data, sizeof(rxframe));
1250 skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
1251 /* save originator for recvfrom() */
1252 op->rx_ifindex = skb->dev->ifindex;
1253 op->frames_abs++; /* statistics */
1255 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
1257 DBG("Wrong skb->len = %d\n", skb->len);
1262 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1264 if (op->can_id != rxframe.can_id) {
1265 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
1266 rxframe.can_id, op->can_id);
1270 if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
1271 DBG("RTR-request\n");
1272 bcm_can_tx(op); /* send op->frames[0] to CAN device */
1276 if (op->flags & RX_FILTER_ID) { /* the easiest case */
1277 DBG("Easy does it with RX_FILTER_ID\n");
1278 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1279 bcm_rx_starttimer(op);
1283 if (op->nframes == 1) { /* simple compare with index 0 */
1284 DBG("Simple compare\n");
1285 bcm_rx_cmp_to_index(op, 0, &rxframe);
1286 bcm_rx_starttimer(op);
1290 if (op->nframes > 1) { /* multiplex compare */
1292 DBG("Multiplex compare\n");
1293 /* find the first multiplex mask that fits */
1294 /* MUX-mask is in index 0 */
1296 for (i=1; i < op->nframes; i++) {
1298 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1299 (GET_U64(&op->frames[0]) &
1300 GET_U64(&op->frames[i]))) {
1301 DBG("found MUX index %d\n", i);
1302 bcm_rx_cmp_to_index(op, i, &rxframe);
1306 bcm_rx_starttimer(op);
1310 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1311 struct can_frame *rxdata)
1313 /* no one uses the MSBs of can_dlc for comparation, */
1314 /* so we use it here to detect the first time of reception */
1316 if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
1317 DBG("first time :)\n");
1318 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1322 /* do a real check in can_data */
1324 DBG("op->frames[index].data = 0x%016llx\n",
1325 GET_U64(&op->frames[index]));
1326 DBG("op->last_frames[index].data = 0x%016llx\n",
1327 GET_U64(&op->last_frames[index]));
1328 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1330 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1331 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1332 DBG("relevant data change :)\n");
1333 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1338 if (op->flags & RX_CHECK_DLC) {
1340 /* do a real check in dlc */
1342 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
1343 BCM_CAN_DLC_MASK)) {
1344 DBG("dlc change :)\n");
1345 bcm_rx_update_and_send(op, &op->last_frames[index],
1350 DBG("no relevant change :(\n");
1353 static void bcm_rx_update_and_send(struct bcm_op *op,
1354 struct can_frame *lastdata,
1355 struct can_frame *rxdata)
1357 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1359 memcpy(lastdata, rxdata, CFSIZ);
1360 lastdata->can_dlc |= RX_RECV; /* mark as used */
1362 /* throttle bcm_rx_changed ? */
1363 if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
1364 ((op->j_ival2) && (nexttx > jiffies))) { /* we have to wait */
1366 lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
1368 if (!(op->thrtimer.expires)) { /* start only the first time */
1369 op->thrtimer.expires = nexttx;
1370 add_timer(&op->thrtimer);
1372 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
1373 op->thrtimer.function,
1374 (char*) op->thrtimer.data,
1375 (unsigned int) op->thrtimer.expires);
1378 bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
1381 static void bcm_rx_starttimer(struct bcm_op *op)
1383 if (op->flags & RX_NO_AUTOTIMER)
1388 op->timer.expires = jiffies + op->j_ival1;
1390 DBG("adding rx timeout timer ival1. func=%p data=%p "
1393 (char*) op->timer.data,
1394 (unsigned int) op->timer.expires);
1396 add_timer(&op->timer);
1401 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1403 struct bcm_msg_head head;
1405 op->j_lastmsg = jiffies;
1406 op->frames_filtered++; /* statistics */
1408 if (op->frames_filtered > ULONG_MAX/100)
1409 op->frames_filtered = op->frames_abs = 0; /* restart */
1411 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
1412 (unsigned int) op->j_lastmsg, op);
1413 DBG("sending notification\n");
1415 head.opcode = RX_CHANGED;
1416 head.flags = op->flags;
1417 head.count = op->count;
1418 head.ival1 = op->ival1;
1419 head.ival2 = op->ival2;
1420 head.can_id = op->can_id;
1423 bcm_send_to_user(op, &head, data, &op->rx_stamp);
1427 static void bcm_rx_timeout_handler(unsigned long data)
1429 struct bcm_op *op = (struct bcm_op*)data;
1430 struct bcm_msg_head msg_head;
1432 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
1434 msg_head.opcode = RX_TIMEOUT;
1435 msg_head.flags = op->flags;
1436 msg_head.count = op->count;
1437 msg_head.ival1 = op->ival1;
1438 msg_head.ival2 = op->ival2;
1439 msg_head.can_id = op->can_id;
1440 msg_head.nframes = 0;
1442 bcm_send_to_user(op, &msg_head, NULL, NULL);
1444 /* no restart of the timer is done here! */
1446 /* if user wants to be informed, when cyclic CAN-Messages come back */
1447 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1448 /* clear received can_frames to indicate 'nothing received' */
1449 memset(op->last_frames, 0, op->nframes * CFSIZ);
1450 DBG("RX_ANNOUNCE_RESTART\n");
1455 static void bcm_rx_thr_handler(unsigned long data)
1457 struct bcm_op *op = (struct bcm_op*)data;
1460 op->thrtimer.expires = 0; /* mark disabled / consumed timer */
1462 if (op->nframes > 1){
1464 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
1466 /* for MUX filter we start at index 1 */
1467 for (i=1; i<op->nframes; i++){
1468 if ((op->last_frames) &&
1469 (op->last_frames[i].can_dlc & RX_THR)){
1470 op->last_frames[i].can_dlc &= ~RX_THR;
1471 bcm_rx_changed(op, &op->last_frames[i]);
1476 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
1478 /* for RX_FILTER_ID and simple filter */
1479 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1480 op->last_frames[0].can_dlc &= ~RX_THR;
1481 bcm_rx_changed(op, &op->last_frames[0]);
1486 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1487 struct can_frame *frames, struct timeval *tv)
1489 struct sk_buff *skb;
1490 struct can_frame *firstframe;
1491 struct sock *sk = op->sk;
1492 int datalen = head->nframes * CFSIZ;
1493 struct sockaddr_can *addr;
1496 skb = alloc_skb(sizeof(*head) + datalen,
1497 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1501 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1502 /* can_frames starting here */
1503 firstframe = (struct can_frame *) skb->tail;
1506 skb_set_timestamp(skb, tv); /* restore timestamp */
1508 addr = (struct sockaddr_can *)skb->cb;
1509 memset(addr, 0, sizeof(*addr));
1510 addr->can_family = AF_CAN;
1511 /* restore originator for recvfrom() */
1512 addr->can_ifindex = op->rx_ifindex;
1515 memcpy(skb_put(skb, datalen), frames, datalen);
1517 /* the BCM uses the can_dlc-element of the can_frame */
1518 /* structure for internal purposes. This is only */
1519 /* relevant for updates that are generated by the */
1520 /* BCM, where nframes is 1 */
1521 if (head->nframes == 1)
1522 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1524 if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
1525 struct bcm_opt *bo = bcm_sk(sk);
1526 DBG("sock_queue_rcv_skb failed: %d\n", err);
1528 bo->dropped_usr_msgs++; /* don't care about overflows */
1532 /**************************************************/
1533 /* bcm_op handling: find & delete bcm_op elements */
1534 /**************************************************/
1536 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
1541 list_for_each_entry(op, ops, list)
1542 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1548 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1550 struct bcm_op *op, *n;
1552 list_for_each_entry_safe(op, n, ops, list) {
1553 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1554 DBG("removing rx_op %p for can_id %03X\n",
1557 /* Don't care if we're bound or not (due to netdev */
1558 /* problems) can_rx_unregister() is always a save */
1559 /* thing to do here. */
1561 struct net_device *dev =
1562 dev_get_by_index(op->ifindex);
1564 can_rx_unregister(dev, op->can_id,
1565 REGMASK(op->can_id),
1566 bcm_rx_handler, op);
1570 can_rx_unregister(NULL, op->can_id,
1571 REGMASK(op->can_id),
1572 bcm_rx_handler, op);
1574 list_del(&op->list);
1576 return 1; /* done */
1580 return 0; /* not found */
1583 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1585 struct bcm_op *op, *n;
1587 list_for_each_entry_safe(op, n, ops, list) {
1588 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1589 DBG("removing rx_op %p for can_id %03X\n",
1591 list_del(&op->list);
1593 return 1; /* done */
1597 return 0; /* not found */
1600 static void bcm_remove_op(struct bcm_op *op)
1602 del_timer(&op->timer);
1603 del_timer(&op->thrtimer);
1606 if (op->last_frames)
1607 kfree(op->last_frames);
1613 module_init(bcm_module_init);
1614 module_exit(bcm_module_exit);