2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/autoconf.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
58 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
61 #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),
233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
234 static void *kzalloc(size_t size, unsigned int __nocast flags)
236 void *ret = kmalloc(size, flags);
238 memset(ret, 0, size);
242 static inline void skb_get_timestamp(const struct sk_buff *skb,
243 struct timeval *stamp)
245 stamp->tv_sec = skb->stamp.tv_sec;
246 stamp->tv_usec = skb->stamp.tv_usec;
249 static inline void skb_set_timestamp(struct sk_buff *skb,
250 const struct timeval *stamp)
252 skb->stamp.tv_sec = stamp->tv_sec;
253 skb->stamp.tv_usec = stamp->tv_usec;
257 #define CFSIZ sizeof(struct can_frame)
258 #define OPSIZ sizeof(struct bcm_op)
259 #define MHSIZ sizeof(struct bcm_msg_head)
261 static int __init bcm_module_init(void)
265 can_proto_register(&bcm_can_proto);
267 /* create /proc/net/can/bcm directory */
268 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
271 proc_dir->owner = THIS_MODULE;
276 static void __exit bcm_module_exit(void)
278 can_proto_unregister(&bcm_can_proto);
281 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
285 /* initial settings at socket creation time */
287 static int bcm_init(struct sock *sk)
289 struct bcm_opt *bo = bcm_sk(sk);
293 bo->dropped_usr_msgs = 0;
294 bo->bcm_proc_read = NULL;
296 INIT_LIST_HEAD(&bo->tx_ops);
297 INIT_LIST_HEAD(&bo->rx_ops);
302 /* handling of netdevice problems */
304 static void bcm_notifier(unsigned long msg, void *data)
306 struct sock *sk = (struct sock *)data;
307 struct bcm_opt *bo = bcm_sk(sk);
309 DBG("called for sock %p\n", sk);
312 case NETDEV_UNREGISTER:
317 sk->sk_err = ENETDOWN;
318 if (!sock_flag(sk, SOCK_DEAD))
319 sk->sk_error_report(sk);
323 /* standard socket functions */
325 static int bcm_release(struct socket *sock)
327 struct sock *sk = sock->sk;
328 struct bcm_opt *bo = bcm_sk(sk);
329 struct bcm_op *op, *next;
331 DBG("socket %p, sk %p\n", sock, sk);
333 /* remove bcm_ops, timer, rx_unregister(), etc. */
335 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
336 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
340 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
341 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
344 * Don't care if we're bound or not (due to netdev problems)
345 * can_rx_unregister() is always a save thing to do here.
348 struct net_device *dev = dev_get_by_index(op->ifindex);
351 can_rx_unregister(dev, op->can_id,
357 can_rx_unregister(NULL, op->can_id,
364 /* remove procfs entry */
365 if ((proc_dir) && (bo->bcm_proc_read)) {
366 remove_proc_entry(bo->procname, proc_dir);
369 /* remove device notifier */
371 struct net_device *dev = dev_get_by_index(bo->ifindex);
374 can_dev_unregister(dev, bcm_notifier, sk);
384 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
387 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
388 struct sock *sk = sock->sk;
389 struct bcm_opt *bo = bcm_sk(sk);
394 /* bind a device to this socket */
395 if (addr->can_ifindex) {
396 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
399 DBG("could not find device index %d\n",
403 bo->ifindex = dev->ifindex;
404 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
407 DBG("socket %p bound to device %s (idx %d)\n",
408 sock, dev->name, dev->ifindex);
410 /* no notifier for ifindex = 0 ('any' CAN device) */
417 /* unique socket address as filename */
418 sprintf(bo->procname, "%p", sock);
419 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
427 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
428 struct msghdr *msg, size_t size)
430 struct sock *sk = sock->sk;
431 struct bcm_opt *bo = bcm_sk(sk);
432 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
433 struct bcm_msg_head msg_head;
434 int ret; /* read bytes or error codes as return value */
437 DBG("sock %p not bound\n", sk);
441 /* check for alternative ifindex for this bcm_op */
443 if (!ifindex && msg->msg_name) {
444 /* no bound device as default => check msg_name */
445 struct sockaddr_can *addr =
446 (struct sockaddr_can *)msg->msg_name;
448 if (addr->can_family != AF_CAN)
451 ifindex = addr->can_ifindex; /* ifindex from sendto() */
453 if (ifindex && !dev_get_by_index(ifindex)) {
454 DBG("device %d not found\n", ifindex);
459 /* read message head information */
461 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
465 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
467 switch (msg_head.opcode) {
471 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
476 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
481 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
489 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
497 /* reuse msg_head for the reply to TX_READ */
498 msg_head.opcode = TX_STATUS;
499 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
504 /* reuse msg_head for the reply to RX_READ */
505 msg_head.opcode = RX_STATUS;
506 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
511 /* we need at least one can_frame */
512 if (msg_head.nframes < 1)
515 ret = bcm_tx_send(msg, ifindex, sk);
520 DBG("Unknown opcode %d\n", msg_head.opcode);
528 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
529 struct msghdr *msg, size_t size, int flags)
531 struct sock *sk = sock->sk;
537 DBG("socket %p, sk %p\n", sock, sk);
539 noblock = flags & MSG_DONTWAIT;
540 flags &= ~MSG_DONTWAIT;
541 skb = skb_recv_datagram(sk, flags, noblock, &error);
545 DBG("delivering skbuff %p\n", skb);
551 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
553 skb_free_datagram(sk, skb);
557 sock_recv_timestamp(msg, sk, skb);
560 msg->msg_namelen = sizeof(struct sockaddr_can);
561 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
564 DBG("freeing sock %p, skbuff %p\n", sk, skb);
565 skb_free_datagram(sk, skb);
570 static unsigned int bcm_poll(struct file *file, struct socket *sock,
573 unsigned int mask = 0;
575 DBG("socket %p\n", sock);
577 mask = datagram_poll(file, sock, wait);
581 /* helper functions for bcm_sendmsg() */
583 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
584 int ifindex, struct sock *sk)
586 struct bcm_opt *bo = bcm_sk(sk);
590 /* we need a real device to send frames */
594 /* we need at least one can_frame */
595 if (msg_head->nframes < 1)
598 /* check the given can_id */
600 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
603 /* update existing BCM operation */
605 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
606 op, msg_head->can_id);
609 * Do we need more space for the can_frames than currently
610 * allocated? -> This is a _really_ unusual use-case and
611 * therefore (complexity / locking) it is not supported.
613 if (msg_head->nframes > op->nframes)
616 /* update can_frames content */
617 for (i = 0; i < msg_head->nframes; i++) {
618 err = memcpy_fromiovec((u8*)&op->frames[i],
619 msg->msg_iov, CFSIZ);
623 if (msg_head->flags & TX_CP_CAN_ID) {
624 /* copy can_id into frame */
625 op->frames[i].can_id = msg_head->can_id;
630 /* insert new BCM operation for the given can_id */
632 op = kzalloc(OPSIZ, GFP_KERNEL);
636 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
637 op, msg_head->can_id);
639 op->can_id = msg_head->can_id;
641 /* create array for can_frames and copy the data */
642 op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL);
648 for (i = 0; i < msg_head->nframes; i++) {
649 err = memcpy_fromiovec((u8*)&op->frames[i],
650 msg->msg_iov, CFSIZ);
657 if (msg_head->flags & TX_CP_CAN_ID) {
658 /* copy can_id into frame */
659 op->frames[i].can_id = msg_head->can_id;
663 /* tx_ops never compare with previous received messages */
664 op->last_frames = NULL;
666 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
669 op->ifindex = ifindex;
671 /* initialize uninitialized (kmalloc) structure */
672 init_timer(&op->timer);
674 /* currently unused in tx_ops */
675 init_timer(&op->thrtimer);
677 /* handler for tx_ops */
678 op->timer.function = bcm_tx_timeout_handler;
680 /* timer.data points to this op-structure */
681 op->timer.data = (unsigned long)op;
683 /* add this bcm_op to the list of the tx_ops */
684 list_add(&op->list, &bo->tx_ops);
686 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
688 if (op->nframes != msg_head->nframes) {
689 op->nframes = msg_head->nframes;
690 /* start multiple frame transmission with index 0 */
696 op->flags = msg_head->flags;
698 if (op->flags & TX_RESET_MULTI_IDX) {
699 /* start multiple frame transmission with index 0 */
703 if (op->flags & SETTIMER) {
705 /* set timer values */
707 op->count = msg_head->count;
708 op->ival1 = msg_head->ival1;
709 op->ival2 = msg_head->ival2;
710 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
711 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
713 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
714 op->count, op->j_ival1, op->j_ival2);
716 /* disable an active timer due to zero values? */
717 if (!op->j_ival1 && !op->j_ival2) {
718 del_timer(&op->timer);
719 DBG("TX_SETUP: SETTIMER disabled timer.\n");
723 if ((op->flags & STARTTIMER) &&
724 ((op->j_ival1 && op->count) || op->j_ival2)) {
726 del_timer(&op->timer);
728 /* spec: send can_frame when starting timer */
729 op->flags |= TX_ANNOUNCE;
731 if (op->j_ival1 && (op->count > 0)){
732 op->timer.expires = jiffies + op->j_ival1;
733 /* op->count-- is done in bcm_tx_timeout_handler */
734 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
737 (char*) op->timer.data,
738 (unsigned int) op->timer.expires);
740 op->timer.expires = jiffies + op->j_ival2;
741 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
744 (char*) op->timer.data,
745 (unsigned int) op->timer.expires);
748 add_timer(&op->timer);
751 if (op->flags & TX_ANNOUNCE)
754 return msg_head->nframes * CFSIZ + MHSIZ;
757 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
758 int ifindex, struct sock *sk)
760 struct bcm_opt *bo = bcm_sk(sk);
765 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
766 /* be robust against wrong usage ... */
767 msg_head->flags |= RX_FILTER_ID;
768 msg_head->nframes = 0; /* ignore trailing garbage */
771 if ((msg_head->flags & RX_RTR_FRAME) &&
772 ((msg_head->nframes != 1) ||
773 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
775 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
779 /* check the given can_id */
780 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
783 /* update existing BCM operation */
785 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
786 op, msg_head->can_id);
789 * Do we need more space for the can_frames than currently
790 * allocated? -> This is a _really_ unusual use-case and
791 * therefore (complexity / locking) it is not supported.
793 if (msg_head->nframes > op->nframes)
796 if (msg_head->nframes) {
797 /* update can_frames content */
798 err = memcpy_fromiovec((u8*)op->frames,
800 msg_head->nframes * CFSIZ);
804 /* clear last_frames to indicate 'nothing received' */
805 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
808 op->nframes = msg_head->nframes;
809 /* Only an update -> do not call can_rx_register() */
813 /* insert new BCM operation for the given can_id */
815 op = kzalloc(OPSIZ, GFP_KERNEL);
819 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
820 op, msg_head->can_id);
822 op->can_id = msg_head->can_id;
823 op->nframes = msg_head->nframes;
825 if (msg_head->nframes) {
827 /* create array for can_frames and copy the data */
828 op->frames = kmalloc(msg_head->nframes * CFSIZ,
835 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
836 msg_head->nframes * CFSIZ);
843 /* create and init array for received can_frames */
844 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
846 if (!op->last_frames) {
852 /* op->frames = NULL due to memset in kzalloc() */
855 * even when we have the RX_FILTER_ID case, we need
856 * to store the last frame for the throttle feature
859 /* create and init array for received can_frames */
860 op->last_frames = kzalloc(CFSIZ, GFP_KERNEL);
861 if (!op->last_frames) {
867 op->sk = sk; /* bcm_delete_rx_op() needs this */
868 op->ifindex = ifindex;
870 /* initialize uninitialized (kmalloc) structure */
871 init_timer(&op->timer);
873 /* init throttle timer for RX_CHANGED */
874 init_timer(&op->thrtimer);
876 /* handler for rx timeouts */
877 op->timer.function = bcm_rx_timeout_handler;
879 /* timer.data points to this op-structure */
880 op->timer.data = (unsigned long)op;
882 /* handler for RX_CHANGED throttle timeouts */
883 op->thrtimer.function = bcm_rx_thr_handler;
885 /* timer.data points to this op-structure */
886 op->thrtimer.data = (unsigned long)op;
888 op->thrtimer.expires = 0; /* mark disabled timer */
890 /* add this bcm_op to the list of the tx_ops */
891 list_add(&op->list, &bo->rx_ops);
893 do_rx_register = 1; /* call can_rx_register() */
895 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
900 op->flags = msg_head->flags;
902 if (op->flags & RX_RTR_FRAME) {
904 /* no timers in RTR-mode */
905 del_timer(&op->thrtimer);
906 del_timer(&op->timer);
909 * funny feature in RX(!)_SETUP only for RTR-mode:
910 * copy can_id into frame BUT without RTR-flag to
911 * prevent a full-load-loopback-test ... ;-]
913 if ((op->flags & TX_CP_CAN_ID) ||
914 (op->frames[0].can_id == op->can_id))
915 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
918 if (op->flags & SETTIMER) {
920 /* set timer value */
922 op->ival1 = msg_head->ival1;
923 op->ival2 = msg_head->ival2;
924 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
925 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
927 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
928 op->j_ival1, op->j_ival2);
930 /* disable an active timer due to zero value? */
932 del_timer(&op->timer);
933 DBG("RX_SETUP: disabled timer rx timeouts.\n");
936 /* free currently blocked msgs ? */
937 if (op->thrtimer.expires) {
938 DBG("RX_SETUP: unblocking throttled msgs.\n");
939 del_timer(&op->thrtimer);
940 /* send blocked msgs hereafter */
941 op->thrtimer.expires = jiffies + 2;
942 add_timer(&op->thrtimer);
945 * if (op->j_ival2) is zero, no (new) throttling
946 * will happen. For details see functions
947 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
951 if ((op->flags & STARTTIMER) && op->j_ival1) {
953 del_timer(&op->timer);
955 op->timer.expires = jiffies + op->j_ival1;
957 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
959 (char *) op->timer.function,
960 (char *) op->timer.data,
961 (unsigned int) op->timer.expires);
963 add_timer(&op->timer);
967 /* now we can register for can_ids, if we added a new bcm_op */
968 if (do_rx_register) {
969 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
970 "rx_op is %p\n", op->can_id, op);
973 struct net_device *dev = dev_get_by_index(ifindex);
976 can_rx_register(dev, op->can_id,
978 bcm_rx_handler, op, IDENT);
982 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
983 bcm_rx_handler, op, IDENT);
986 return msg_head->nframes * CFSIZ + MHSIZ;
989 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
992 struct net_device *dev;
995 /* just copy and send one can_frame */
997 if (!ifindex) /* we need a real device to send frames */
1000 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1005 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1011 DBG_FRAME("BCM: TX_SEND: sending frame",
1012 (struct can_frame *)skb->data);
1014 dev = dev_get_by_index(ifindex);
1022 can_send(skb, 1); /* send with loopback */
1025 return CFSIZ + MHSIZ;
1028 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
1034 op = bcm_find_op(ops, msg_head->can_id, ifindex);
1037 DBG("TRX_READ: sending status for can_id %03X\n",
1039 /* put current values into msg_head */
1040 msg_head->flags = op->flags;
1041 msg_head->count = op->count;
1042 msg_head->ival1 = op->ival1;
1043 msg_head->ival2 = op->ival2;
1044 msg_head->nframes = op->nframes;
1046 bcm_send_to_user(op, msg_head, op->frames, NULL);
1052 DBG("TRX_READ: did not find op for can_id %03X\n",
1060 /* procfs functions */
1062 static char *bcm_proc_getifname(int ifindex)
1064 struct net_device *dev;
1069 dev = __dev_get_by_index(ifindex); /* no usage counting */
1076 static int bcm_read_proc(char *page, char **start, off_t off,
1077 int count, int *eof, void *data)
1080 struct sock *sk = (struct sock *)data;
1081 struct bcm_opt *bo = bcm_sk(sk);
1084 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
1086 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
1087 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
1088 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
1089 bo->dropped_usr_msgs);
1090 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
1091 bcm_proc_getifname(bo->ifindex));
1092 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
1094 list_for_each_entry(op, &bo->rx_ops, list) {
1096 unsigned long reduction;
1098 /* print only active entries & prevent division by zero */
1099 if (!op->frames_abs)
1102 len += snprintf(page + len, PAGE_SIZE - len,
1103 "rx_op: %03X %-5s ",
1104 op->can_id, bcm_proc_getifname(op->ifindex));
1105 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1107 (op->flags & RX_CHECK_DLC)?'d':' ');
1109 len += snprintf(page + len, PAGE_SIZE - len,
1110 "timeo=%ld ", op->j_ival1);
1113 len += snprintf(page + len, PAGE_SIZE - len,
1114 "thr=%ld ", op->j_ival2);
1116 len += snprintf(page + len, PAGE_SIZE - len,
1117 "# recv %ld (%ld) => reduction: ",
1118 op->frames_filtered, op->frames_abs);
1120 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1122 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1123 (reduction == 100)?"near ":"", reduction);
1125 if (len > PAGE_SIZE - 200) {
1126 /* mark output cut off */
1127 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1132 list_for_each_entry(op, &bo->tx_ops, list) {
1134 len += snprintf(page + len, PAGE_SIZE - len,
1135 "tx_op: %03X %s [%d] ",
1136 op->can_id, bcm_proc_getifname(op->ifindex),
1139 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
1143 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
1146 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
1149 if (len > PAGE_SIZE - 100) {
1150 /* mark output cut off */
1151 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1156 len += snprintf(page + len, PAGE_SIZE - len, "\n");
1162 /* bcm_op handling tx path */
1164 static void bcm_can_tx(struct bcm_op *op)
1166 struct sk_buff *skb;
1167 struct net_device *dev;
1168 struct can_frame *cf = &op->frames[op->currframe];
1170 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1172 /* no target device? => exit */
1176 dev = dev_get_by_index(op->ifindex);
1179 /* RFC: should this bcm_op remove itself here? */
1183 skb = alloc_skb(CFSIZ,
1184 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1189 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1191 /* send with loopback */
1196 /* update statistics */
1200 /* reached last frame? */
1201 if (op->currframe >= op->nframes)
1207 static void bcm_tx_timeout_handler(unsigned long data)
1209 struct bcm_op *op = (struct bcm_op*)data;
1211 DBG("Called with bcm_op %p\n", op);
1213 if (op->j_ival1 && (op->count > 0)) {
1217 if (!op->count && (op->flags & TX_COUNTEVT)) {
1218 /* create notification to user */
1220 struct bcm_msg_head msg_head;
1222 DBG("sending TX_EXPIRED for can_id %03X\n",
1225 msg_head.opcode = TX_EXPIRED;
1226 msg_head.flags = op->flags;
1227 msg_head.count = op->count;
1228 msg_head.ival1 = op->ival1;
1229 msg_head.ival2 = op->ival2;
1230 msg_head.can_id = op->can_id;
1231 msg_head.nframes = 0;
1233 bcm_send_to_user(op, &msg_head, NULL, NULL);
1237 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1238 op->count, op->j_ival1, op->j_ival2);
1240 if (op->j_ival1 && (op->count > 0)) {
1242 op->timer.expires = jiffies + op->j_ival1;
1243 add_timer(&op->timer);
1245 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
1247 (char*) op->timer.data,
1248 (unsigned int) op->timer.expires);
1250 /* send (next) frame */
1254 op->timer.expires = jiffies + op->j_ival2;
1255 add_timer(&op->timer);
1257 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
1259 (char*) op->timer.data,
1260 (unsigned int) op->timer.expires);
1262 /* send (next) frame */
1265 DBG("no timer restart\n");
1272 /* bcm_op handling rx path */
1274 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1276 struct bcm_op *op = (struct bcm_op*)data;
1277 struct can_frame rxframe;
1280 /* disable timeout */
1281 del_timer(&op->timer);
1283 DBG("Called with bcm_op %p\n", op);
1285 if (skb->len == sizeof(rxframe)) {
1286 memcpy(&rxframe, skb->data, sizeof(rxframe));
1287 /* save rx timestamp */
1288 skb_get_timestamp(skb, &op->rx_stamp);
1289 /* save originator for recvfrom() */
1290 op->rx_ifindex = skb->dev->ifindex;
1291 /* update statistics */
1294 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
1296 DBG("Wrong skb->len = %d\n", skb->len);
1301 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1303 if (op->can_id != rxframe.can_id) {
1304 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
1305 rxframe.can_id, op->can_id);
1309 if (op->flags & RX_RTR_FRAME) {
1310 /* send reply for RTR-request */
1311 DBG("RTR-request\n");
1313 /* send op->frames[0] to CAN device */
1318 if (op->flags & RX_FILTER_ID) {
1319 /* the easiest case */
1320 DBG("Easy does it with RX_FILTER_ID\n");
1322 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1323 bcm_rx_starttimer(op);
1327 if (op->nframes == 1) {
1328 /* simple compare with index 0 */
1329 DBG("Simple compare\n");
1331 bcm_rx_cmp_to_index(op, 0, &rxframe);
1332 bcm_rx_starttimer(op);
1336 if (op->nframes > 1) {
1337 /* multiplex compare */
1338 DBG("Multiplex compare\n");
1341 * find the first multiplex mask that fits.
1342 * Remark: The MUX-mask is stored in index 0
1345 for (i=1; i < op->nframes; i++) {
1347 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1348 (GET_U64(&op->frames[0]) &
1349 GET_U64(&op->frames[i]))) {
1350 DBG("found MUX index %d\n", i);
1351 bcm_rx_cmp_to_index(op, i, &rxframe);
1355 bcm_rx_starttimer(op);
1359 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1360 struct can_frame *rxdata)
1363 * no one uses the MSBs of can_dlc for comparation,
1364 * so we use it here to detect the first time of reception
1367 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
1368 /* received data for the first time => send update to user */
1369 DBG("first time :)\n");
1370 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1374 /* do a real check in can_data */
1376 DBG("op->frames[index].data = 0x%016llx\n",
1377 GET_U64(&op->frames[index]));
1378 DBG("op->last_frames[index].data = 0x%016llx\n",
1379 GET_U64(&op->last_frames[index]));
1380 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1382 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1383 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1384 DBG("relevant data change :)\n");
1385 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1390 if (op->flags & RX_CHECK_DLC) {
1392 /* do a real check in dlc */
1394 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
1395 BCM_CAN_DLC_MASK)) {
1396 DBG("dlc change :)\n");
1397 bcm_rx_update_and_send(op, &op->last_frames[index],
1402 DBG("no relevant change :(\n");
1405 static void bcm_rx_update_and_send(struct bcm_op *op,
1406 struct can_frame *lastdata,
1407 struct can_frame *rxdata)
1409 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1411 memcpy(lastdata, rxdata, CFSIZ);
1414 lastdata->can_dlc |= RX_RECV;
1416 /* throttle bcm_rx_changed ? */
1417 if ((op->thrtimer.expires) ||
1418 ((op->j_ival2) && (nexttx > jiffies))) {
1419 /* we are already waiting OR we have to start waiting */
1421 /* mark as 'throttled' */
1422 lastdata->can_dlc |= RX_THR;
1424 if (!(op->thrtimer.expires)) {
1425 /* start the timer only the first time */
1426 op->thrtimer.expires = nexttx;
1427 add_timer(&op->thrtimer);
1429 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
1430 op->thrtimer.function,
1431 (char*) op->thrtimer.data,
1432 (unsigned int) op->thrtimer.expires);
1435 /* send RX_CHANGED to the user */
1436 bcm_rx_changed(op, rxdata);
1440 static void bcm_rx_starttimer(struct bcm_op *op)
1442 if (op->flags & RX_NO_AUTOTIMER)
1447 op->timer.expires = jiffies + op->j_ival1;
1449 DBG("adding rx timeout timer ival1. func=%p data=%p "
1452 (char*) op->timer.data,
1453 (unsigned int) op->timer.expires);
1455 add_timer(&op->timer);
1460 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1462 struct bcm_msg_head head;
1464 op->j_lastmsg = jiffies;
1466 /* update statistics */
1467 op->frames_filtered++;
1469 /* prevent statistics overflow */
1470 if (op->frames_filtered > ULONG_MAX/100)
1471 op->frames_filtered = op->frames_abs = 0;
1473 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
1474 (unsigned int) op->j_lastmsg, op);
1475 DBG("sending notification\n");
1477 head.opcode = RX_CHANGED;
1478 head.flags = op->flags;
1479 head.count = op->count;
1480 head.ival1 = op->ival1;
1481 head.ival2 = op->ival2;
1482 head.can_id = op->can_id;
1485 bcm_send_to_user(op, &head, data, &op->rx_stamp);
1489 static void bcm_rx_timeout_handler(unsigned long data)
1491 struct bcm_op *op = (struct bcm_op*)data;
1492 struct bcm_msg_head msg_head;
1494 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
1496 msg_head.opcode = RX_TIMEOUT;
1497 msg_head.flags = op->flags;
1498 msg_head.count = op->count;
1499 msg_head.ival1 = op->ival1;
1500 msg_head.ival2 = op->ival2;
1501 msg_head.can_id = op->can_id;
1502 msg_head.nframes = 0;
1504 bcm_send_to_user(op, &msg_head, NULL, NULL);
1506 /* no restart of the timer is done here! */
1508 /* if user wants to be informed, when cyclic CAN-Messages come back */
1509 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1510 /* clear received can_frames to indicate 'nothing received' */
1511 memset(op->last_frames, 0, op->nframes * CFSIZ);
1512 DBG("RX_ANNOUNCE_RESTART\n");
1517 static void bcm_rx_thr_handler(unsigned long data)
1519 struct bcm_op *op = (struct bcm_op*)data;
1522 /* mark disabled / consumed timer */
1523 op->thrtimer.expires = 0;
1525 if (op->nframes > 1){
1527 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
1529 /* for MUX filter we start at index 1 */
1530 for (i=1; i<op->nframes; i++){
1531 if ((op->last_frames) &&
1532 (op->last_frames[i].can_dlc & RX_THR)){
1533 op->last_frames[i].can_dlc &= ~RX_THR;
1534 bcm_rx_changed(op, &op->last_frames[i]);
1539 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
1541 /* for RX_FILTER_ID and simple filter */
1542 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1543 op->last_frames[0].can_dlc &= ~RX_THR;
1544 bcm_rx_changed(op, &op->last_frames[0]);
1549 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1550 struct can_frame *frames, struct timeval *tv)
1552 struct sk_buff *skb;
1553 struct can_frame *firstframe;
1554 struct sock *sk = op->sk;
1555 int datalen = head->nframes * CFSIZ;
1556 struct sockaddr_can *addr;
1559 skb = alloc_skb(sizeof(*head) + datalen,
1560 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1564 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1566 /* can_frames starting here */
1567 firstframe = (struct can_frame *) skb->tail;
1570 skb_set_timestamp(skb, tv); /* restore timestamp */
1572 addr = (struct sockaddr_can *)skb->cb;
1573 memset(addr, 0, sizeof(*addr));
1574 addr->can_family = AF_CAN;
1575 /* restore originator for recvfrom() */
1576 addr->can_ifindex = op->rx_ifindex;
1579 memcpy(skb_put(skb, datalen), frames, datalen);
1582 * the BCM uses the can_dlc-element of the can_frame
1583 * structure for internal purposes. This is only
1584 * relevant for updates that are generated by the
1585 * BCM, where nframes is 1
1587 if (head->nframes == 1)
1588 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1591 err = sock_queue_rcv_skb(sk, skb);
1593 struct bcm_opt *bo = bcm_sk(sk);
1595 DBG("sock_queue_rcv_skb failed: %d\n", err);
1597 /* don't care about overflows in this statistic */
1598 bo->dropped_usr_msgs++;
1602 /* bcm_op handling: find & delete bcm_op elements */
1604 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
1609 list_for_each_entry(op, ops, list) {
1610 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1617 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1619 struct bcm_op *op, *n;
1621 list_for_each_entry_safe(op, n, ops, list) {
1622 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1623 DBG("removing rx_op %p for can_id %03X\n",
1627 * Don't care if we're bound or not (due to netdev
1628 * problems) can_rx_unregister() is always a save
1632 struct net_device *dev =
1633 dev_get_by_index(op->ifindex);
1636 can_rx_unregister(dev, op->can_id,
1637 REGMASK(op->can_id),
1638 bcm_rx_handler, op);
1642 can_rx_unregister(NULL, op->can_id,
1643 REGMASK(op->can_id),
1644 bcm_rx_handler, op);
1646 list_del(&op->list);
1648 return 1; /* done */
1652 return 0; /* not found */
1655 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1657 struct bcm_op *op, *n;
1659 list_for_each_entry_safe(op, n, ops, list) {
1660 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1661 DBG("removing rx_op %p for can_id %03X\n",
1663 list_del(&op->list);
1665 return 1; /* done */
1669 return 0; /* not found */
1672 static void bcm_remove_op(struct bcm_op *op)
1674 del_timer(&op->timer);
1675 del_timer(&op->thrtimer);
1678 if (op->last_frames)
1679 kfree(op->last_frames);
1685 module_init(bcm_module_init);
1686 module_exit(bcm_module_exit);