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/config.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/af_can.h>
55 #include <linux/can/bcm.h>
56 #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 /* this element has not been sent due to throttle functionality */
78 #define BCM_CAN_DLC_MASK 0x0F /* clean flags by masking with BCM_CAN_DLC_MASK */
79 #define BCM_RX_REGMASK (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)
81 #define NAME "Broadcast Manager (BCM) for LLCF"
83 static __initdata const char banner[] = BANNER(NAME);
85 MODULE_DESCRIPTION(NAME);
86 MODULE_LICENSE("Dual BSD/GPL");
87 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
90 #define GET_U64(p) (*(unsigned long long*)(p)->data)
93 struct list_head list;
96 unsigned long j_ival1, j_ival2, j_lastmsg;
97 unsigned long frames_abs, frames_filtered;
98 struct timeval ival1, ival2, stamp;
99 struct timer_list timer, thrtimer;
103 struct can_frame *frames;
104 struct can_frame *last_frames;
108 struct bcm_user_data {
109 struct list_head rx_ops;
110 struct list_head tx_ops;
111 struct proc_dir_entry *bcm_proc_read;
115 static struct proc_dir_entry *proc_dir = NULL;
116 int bcm_read_proc(char *page, char **start, off_t off,
117 int count, int *eof, void *data);
119 static void bcm_notifier(unsigned long msg, void *data);
120 static int bcm_release(struct socket *sock);
121 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
123 static unsigned int bcm_poll(struct file *file, struct socket *sock,
125 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
126 struct msghdr *msg, size_t size);
127 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
128 struct msghdr *msg, size_t size, int flags);
130 static void bcm_tx_timeout_handler(unsigned long data);
131 static void bcm_rx_handler(struct sk_buff *skb, void *op);
132 static void bcm_rx_timeout_handler(unsigned long data);
133 static void bcm_rx_thr_handler(unsigned long data);
134 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id);
135 static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id);
136 static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id);
137 static void bcm_remove_op(struct bcm_op *op);
138 static void bcm_can_tx(struct bcm_op *op);
139 static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
140 struct can_frame *frames, struct timeval *tv);
141 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
142 static void bcm_rx_starttimer(struct bcm_op *op);
143 static void bcm_rx_update_and_send(struct bcm_op *op,
144 struct can_frame *lastdata,
145 struct can_frame *rxdata);
146 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
147 struct can_frame *rxdata);
149 static struct proto_ops bcm_ops = {
151 .release = bcm_release,
152 .bind = sock_no_bind,
153 .connect = bcm_connect,
154 .socketpair = sock_no_socketpair,
155 .accept = sock_no_accept,
156 .getname = sock_no_getname,
158 .ioctl = NULL, /* use can_ioctl() from af_can.c */
159 .listen = sock_no_listen,
160 .shutdown = sock_no_shutdown,
161 .setsockopt = sock_no_setsockopt,
162 .getsockopt = sock_no_getsockopt,
163 .sendmsg = bcm_sendmsg,
164 .recvmsg = bcm_recvmsg,
165 .mmap = sock_no_mmap,
166 .sendpage = sock_no_sendpage,
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
173 struct bcm_user_data opt;
176 #define bcm_sk(sk) ((struct bcm_user_data *)(sk)->sk_protinfo)
178 static struct proto bcm_proto = {
180 .owner = THIS_MODULE,
181 .obj_size = sizeof(struct bcm_sock),
184 static struct can_proto bcm_can_proto = {
191 #define bcm_sk(sk) ((struct bcm_user_data *)(sk)->sk_protinfo)
193 static struct can_proto bcm_can_proto = {
195 .owner = THIS_MODULE,
202 static int __init bcm_init(void)
206 can_proto_register(CAN_BCM, &bcm_can_proto);
208 /* create /proc/net/can/bcm directory */
209 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
212 proc_dir->owner = THIS_MODULE;
217 static void __exit bcm_exit(void)
219 can_proto_unregister(CAN_BCM);
222 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
226 static void bcm_notifier(unsigned long msg, void *data)
228 struct sock *sk = (struct sock *)data;
230 DBG("called for sock %p\n", sk);
233 case NETDEV_UNREGISTER:
234 sk->sk_bound_dev_if = 0;
236 sk->sk_err = ENETDOWN;
237 sk->sk_error_report(sk);
241 static int bcm_release(struct socket *sock)
243 struct sock *sk = sock->sk;
244 struct bcm_user_data *ud = bcm_sk(sk);
245 struct bcm_op *op, *n;
247 /* many things to do here:
248 free all rx_ops and tx_ops, bcm_user_data structure,
249 can_rx_unregister(dev, canid, raw_rcv) and can-data in ?x_ops */
251 DBG("socket %p, sk %p\n", sock, sk);
253 /* remove userdata, bcm_ops, timer, etc. */
256 list_for_each_entry_safe(op, n, &ud->tx_ops, list) {
257 DBG("removing tx_op (%p) for can_id <%03X>\n", op, op->can_id);
261 list_for_each_entry_safe(op, n, &ud->rx_ops, list) {
262 DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
264 if (sk->sk_bound_dev_if) {
265 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
267 can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
271 DBG("sock %p not bound for can_rx_unregister()\n", sk);
276 if ((proc_dir) && (ud->bcm_proc_read)) {
277 remove_proc_entry(ud->procname, proc_dir);
281 sk->sk_protinfo = NULL;
284 if (sk->sk_bound_dev_if) {
285 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
287 can_dev_unregister(dev, bcm_notifier, sk);
291 DBG("sock %p not bound for can_dev_unregister()\n", sk);
298 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
301 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
302 struct sock *sk = sock->sk;
303 struct net_device *dev;
304 struct bcm_user_data *ud;
306 /* bind a device to this socket */
308 dev = dev_get_by_index(addr->can_ifindex);
310 DBG("could not find device %d\n", addr->can_ifindex);
313 sk->sk_bound_dev_if = dev->ifindex;
314 can_dev_register(dev, bcm_notifier, sk);
317 DBG("socket %p to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
319 /* create struct for BCM-specific data for this socket */
321 if (!(ud = kmalloc(sizeof(struct bcm_user_data), GFP_KERNEL)))
324 /* intitial BCM operations */
325 INIT_LIST_HEAD(&ud->tx_ops);
326 INIT_LIST_HEAD(&ud->rx_ops);
327 ud->bcm_proc_read = NULL;
329 sk->sk_protinfo = ud;
332 sprintf(ud->procname, "%p", ud);
333 ud->bcm_proc_read = create_proc_read_entry(ud->procname, 0644,
334 proc_dir, bcm_read_proc, ud);
340 int bcm_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
343 struct bcm_user_data *ud = (struct bcm_user_data *) data;
345 struct net_device *dev = NULL;
347 len += snprintf(page + len, PAGE_SIZE - len,">>> ud %p", ud);
349 if (!list_empty(&ud->rx_ops)) {
351 op = list_entry(ud->rx_ops.next, struct bcm_op, list);
353 if (sk->sk_bound_dev_if)
354 dev = dev_get_by_index(sk->sk_bound_dev_if);
355 len += snprintf(page + len, PAGE_SIZE - len,
356 " / sk %p / socket %p", sk, sk->sk_socket);
357 } else if (!list_empty(&ud->tx_ops)) {
359 op = list_entry(ud->tx_ops.next, struct bcm_op, list);
361 if (sk->sk_bound_dev_if)
362 dev = dev_get_by_index(sk->sk_bound_dev_if);
363 len += snprintf(page + len, PAGE_SIZE - len,
364 " / sk %p / socket %p", sk, sk->sk_socket);
368 len += snprintf(page + len, PAGE_SIZE - len, " / %s", dev->name);
372 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
374 list_for_each_entry(op, &ud->rx_ops, list) {
376 unsigned long reduction;
378 /* print only active entries & prevent division by zero */
382 len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X [%d]%c ",
383 op->can_id, op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
385 len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
388 len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
390 len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
391 op->frames_filtered, op->frames_abs);
393 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
395 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
396 (reduction == 100)?"near ":"", reduction);
398 if (len >= PAGE_SIZE - 100) {
399 /* mark output cut off */
400 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
405 list_for_each_entry(op, &ud->tx_ops, list) {
407 len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X [%d] ",
408 op->can_id, op->nframes);
410 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
413 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
415 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
417 if (len >= PAGE_SIZE - 100) {
418 /* mark output cut off */
419 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
424 len += snprintf(page + len, PAGE_SIZE - len, "\n");
430 static unsigned int bcm_poll(struct file *file, struct socket *sock,
433 unsigned int mask = 0;
435 DBG("socket %p\n", sock);
437 mask = datagram_poll(file, sock, wait);
441 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
442 struct msghdr *msg, size_t size)
444 struct bcm_msg_head msg_head;
448 struct sock *sk = sock->sk;
449 struct bcm_user_data *ud = bcm_sk(sk);
451 int rbytes = 0; /* read bytes as return value */
453 /* read message head information */
455 if ((err = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
456 sizeof(msg_head))) < 0)
459 DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
461 if (!sk->sk_bound_dev_if) {
462 DBG("sock %p not bound\n", sk); /* and therefore ud not initialized */
466 switch (msg_head.opcode) {
470 if (msg_head.nframes < 1) /* we need at least one can_frame */
473 /* check the given can_id */
475 if (!(op = bcm_find_op(&ud->tx_ops, msg_head.can_id))) {
477 /* insert new BCM operation for the given can_id */
479 if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
482 memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
484 DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
485 op, msg_head.can_id);
487 op->can_id = msg_head.can_id;
489 /* create array for can_frames and copy the data */
490 if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
495 for (i = 0; i < msg_head.nframes; i++) {
496 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
497 if (msg_head.flags & TX_CP_CAN_ID)
498 op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
501 op->last_frames = NULL; /* tx_ops never compare with previous received messages */
503 op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
505 init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
506 init_timer(&op->thrtimer); /* currently unused in tx_ops */
508 op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
509 op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
511 /* add this bcm_op to the list of the tx_ops */
512 list_add(&op->list, &ud->tx_ops);
515 /* update existing BCM operation */
517 DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
518 op, msg_head.can_id);
520 /* do we need more space for the can_frames? */
521 if (msg_head.nframes > op->nframes) {
523 /* yes => create new array */
526 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
533 /* update can_frames content */
534 for (i = 0; i < msg_head.nframes; i++) {
535 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
536 if (msg_head.flags & TX_CP_CAN_ID)
537 op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
542 if (op->nframes != msg_head.nframes) {
543 op->nframes = msg_head.nframes;
544 op->currframe = 0; /* start multiple frame transmission with index 0 */
549 op->flags = msg_head.flags;
551 if (op->flags & TX_RESET_MULTI_IDX)
552 op->currframe = 0; /* start multiple frame transmission with index 0 */
554 if (op->flags & SETTIMER) {
556 /* set timer values */
558 op->count = msg_head.count;
559 op->ival1 = msg_head.ival1;
560 op->ival2 = msg_head.ival2;
561 op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
562 op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
564 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
565 op->count, op->j_ival1, op->j_ival2);
567 /* disable an active timer due to zero values? */
568 if (!op->j_ival1 && !op->j_ival2) {
569 del_timer(&op->timer);
570 DBG("TX_SETUP: SETTIMER disabled timer.\n");
575 if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
577 del_timer(&op->timer);
579 op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
580 if (op->j_ival1 && (op->count > 0)){
581 op->timer.expires = jiffies + op->j_ival1;
582 /* op->count-- is done in bcm_tx_timeout_handler */
583 DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
585 (char*) op->timer.data,
586 (unsigned int) op->timer.expires);
588 op->timer.expires = jiffies + op->j_ival2;
589 DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
591 (char*) op->timer.data,
592 (unsigned int) op->timer.expires);
595 add_timer(&op->timer);
598 if (op->flags & TX_ANNOUNCE)
601 rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
603 break; /* TX_SETUP */
607 bcm_delete_tx_op(&ud->tx_ops, msg_head.can_id);
609 rbytes = sizeof(struct bcm_msg_head);
611 break; /* TX_DELETE */
615 /* reuse msg_head for the reply */
616 msg_head.opcode = TX_STATUS; /* reply to TX_READ */
617 op = bcm_find_op(&ud->tx_ops, msg_head.can_id);
618 c = 'T'; /* for nice debug output ... */
624 /* reuse msg_head for the reply */
625 msg_head.opcode = RX_STATUS; /* reply to RX_READ */
626 op = bcm_find_op(&ud->rx_ops, msg_head.can_id);
627 c = 'R'; /* for nice debug output ... */
631 /* check the given can_id */
634 DBG("%cX_READ: did not find op for can_id <%03X>\n",
637 msg_head.flags |= CMD_ERROR;
638 msg_head.nframes = 0;
639 bcm_send_to_user(sk, &msg_head, NULL, NULL);
642 DBG("%cX_READ: sending status for can_id <%03X>\n",
645 /* put current values into msg_head */
646 msg_head.flags = op->flags;
647 msg_head.count = op->count;
648 msg_head.ival1 = op->ival1;
649 msg_head.ival2 = op->ival2;
650 msg_head.nframes = op->nframes;
652 bcm_send_to_user(sk, &msg_head, op->frames, NULL);
655 rbytes = sizeof(struct bcm_msg_head);
657 break; /* [T|R]X_READ */
662 struct net_device *dev;
664 /* just copy and send one can_frame */
666 if (msg_head.nframes < 1) /* we need at least one can_frame */
669 skb = alloc_skb(sizeof(struct can_frame), GFP_KERNEL);
674 memcpy_fromiovec(skb_put(skb, sizeof(struct can_frame)), msg->msg_iov, sizeof(struct can_frame));
676 DBG_FRAME("BCM: TX_SEND: sending frame",
677 (struct can_frame *)skb->data);
678 dev = dev_get_by_index(sk->sk_bound_dev_if);
683 can_send(skb, 1); /* send with loopback */
687 rbytes = sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
693 if ((msg_head.flags & RX_FILTER_ID) || (!(msg_head.nframes))) {
694 /* be robust against wrong usage ... */
695 msg_head.flags |= RX_FILTER_ID;
696 msg_head.nframes = 0; /* ignore trailing garbage */
699 if ((msg_head.flags & RX_RTR_FRAME) &&
700 ((msg_head.nframes != 1) || (!(msg_head.can_id & CAN_RTR_FLAG)))) {
702 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
704 msg_head.flags |= CMD_ERROR; /* return msg_head back to sender */
705 msg_head.nframes = 0;
706 bcm_send_to_user(sk, &msg_head, NULL, NULL);
708 rbytes = sizeof(struct bcm_msg_head);
713 /* check the given can_id */
715 if (!(op = bcm_find_op(&ud->rx_ops, msg_head.can_id))) {
717 /* insert new BCM operation for the given can_id */
719 if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
722 memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
724 DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
725 op, msg_head.can_id);
727 op->can_id = msg_head.can_id;
728 op->nframes = msg_head.nframes;
732 /* create array for can_frames and copy the data */
733 if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
738 for (i = 0; i < msg_head.nframes; i++)
739 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
741 /* create array for received can_frames */
742 if (!(op->last_frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
748 /* clear received can_frames to indicate 'nothing received' */
749 memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
753 /* even when we have the RX_FILTER_ID case, we need to store the last frame */
754 /* for the throttle functionality */
756 /* create array for received can_frames */
757 if (!(op->last_frames = kmalloc(sizeof(struct can_frame), GFP_KERNEL))) {
762 /* clear received can_frames to indicate 'nothing received' */
763 memset(op->last_frames, 0, sizeof(struct can_frame));
766 op->sk = sk; /* bcm_delete_rx_op() needs this */
768 init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
769 init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
771 op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
772 op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
774 op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
775 op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
776 op->thrtimer.expires = 0; /* mark disabled timer */
778 /* add this bcm_op to the list of the tx_ops */
779 list_add(&op->list, &ud->rx_ops);
781 c=1; /* call can_rx_register() at end of RX_SETUP */
784 /* update existing BCM operation */
786 DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
787 op, msg_head.can_id);
789 /* do we need more space for the can_frames? */
790 if (msg_head.nframes > op->nframes) {
792 /* yes => create new arrays */
796 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
803 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
806 kfree (op->last_frames);
810 if (msg_head.nframes) {
811 /* update can_frames content */
812 for (i = 0; i < msg_head.nframes; i++)
813 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
815 /* clear received can_frames to indicate 'nothing received' */
816 memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
819 op->nframes = msg_head.nframes;
820 c=0; /* do not call can_rx_register() at end of RX_SETUP */
822 } /* if (!bcm_find_op(&ud->tx_ops, msg_head.can_id)) */
827 op->flags = msg_head.flags;
829 if (op->flags & RX_RTR_FRAME) {
831 /* no timers in RTR-mode */
832 del_timer(&op->thrtimer);
833 del_timer(&op->timer);
835 /* funny feature in RX(!)_SETUP only for RTR-mode: */
836 /* copy can_id into frame BUT without RTR-flag to */
837 /* prevent a full-load-loopback-test ... ;-] */
838 if ((op->flags & TX_CP_CAN_ID) ||
839 (op->frames[0].can_id == op->can_id))
840 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
843 if (op->flags & SETTIMER) {
845 /* set timer value */
847 op->ival1 = msg_head.ival1;
848 op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
849 op->ival2 = msg_head.ival2;
850 op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
852 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
853 op->j_ival1, op->j_ival2);
855 /* disable an active timer due to zero value? */
857 del_timer(&op->timer);
858 DBG("RX_SETUP: disabled timer for rx timeouts.\n");
861 /* free currently blocked msgs ? */
862 if (op->thrtimer.expires) { /* running throttle timer? */
863 DBG("RX_SETUP: unblocking throttled msgs.\n");
864 del_timer(&op->thrtimer);
865 op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
866 add_timer(&op->thrtimer);
868 /* if (op->j_ival2) is zero, no (new) throttling will happen */
869 /* see bcm_rx_update_and_send() and bcm_rx_thr_handler() */
872 if ((op->flags & STARTTIMER) && op->j_ival1) {
874 del_timer(&op->timer);
876 op->timer.expires = jiffies + op->j_ival1;
878 DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
879 (char *) op->timer.function,
880 (char *) op->timer.data,
881 (unsigned int) op->timer.expires);
883 add_timer(&op->timer);
887 /* now we can register for can_ids, if we added a new bcm_op */
889 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
891 DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
894 can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
899 rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
901 break; /* RX_SETUP */
905 bcm_delete_rx_op(&ud->rx_ops, msg_head.can_id);
907 rbytes = sizeof(struct bcm_msg_head);
909 break; /* RX_DELETE */
913 DBG("Unknown opcode %d\n", msg_head.opcode);
915 msg_head.flags |= CMD_ERROR; /* return msg_head back to sender */
916 msg_head.nframes = 0;
917 bcm_send_to_user(sk, &msg_head, NULL, NULL);
919 rbytes = sizeof(struct bcm_msg_head);
927 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
928 struct msghdr *msg, size_t size, int flags)
930 struct sock *sk = sock->sk;
936 DBG("socket %p, sk %p\n", sock, sk);
938 noblock = flags & MSG_DONTWAIT;
939 flags &= ~MSG_DONTWAIT;
940 if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
944 DBG("delivering skbuff %p\n", skb);
949 if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
950 skb_free_datagram(sk, skb);
954 sock_recv_timestamp(msg, sk, skb);
956 DBG("freeing sock %p, skbuff %p\n", sk, skb);
957 skb_free_datagram(sk, skb);
962 static void bcm_tx_timeout_handler(unsigned long data)
964 struct bcm_op *op = (struct bcm_op*)data;
966 DBG("Called with bcm_op (%p)\n", op);
968 if (op->j_ival1 && (op->count > 0)) {
972 if (!op->count && (op->flags & TX_COUNTEVT)) { /* create notification to user? */
974 struct bcm_msg_head msg_head;
976 DBG("sending TX_EXPIRED for can_id <%03X>\n", op->can_id);
978 msg_head.opcode = TX_EXPIRED;
979 msg_head.flags = op->flags;
980 msg_head.count = op->count;
981 msg_head.ival1 = op->ival1;
982 msg_head.ival2 = op->ival2;
983 msg_head.can_id = op->can_id;
984 msg_head.nframes = 0;
986 bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
990 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
991 op->count, op->j_ival1, op->j_ival2);
993 if (op->j_ival1 && (op->count > 0)) {
995 op->timer.expires = jiffies + op->j_ival1;
996 add_timer(&op->timer);
998 DBG("adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
1000 (char*) op->timer.data,
1001 (unsigned int) op->timer.expires);
1003 bcm_can_tx(op); /* send (next) frame */
1006 op->timer.expires = jiffies + op->j_ival2;
1007 add_timer(&op->timer);
1009 DBG("adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
1011 (char*) op->timer.data,
1012 (unsigned int) op->timer.expires);
1014 bcm_can_tx(op); /* send (next) frame */
1016 DBG("no timer restart\n");
1023 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1025 struct bcm_op *op = (struct bcm_op*)data;
1026 struct can_frame rxframe;
1029 del_timer(&op->timer); /* disable timeout */
1031 DBG("Called with bcm_op (%p)\n", op);
1033 if (skb->len == sizeof(rxframe)) {
1034 memcpy(&rxframe, skb->data, sizeof(rxframe));
1035 skb_get_timestamp(skb, &op->stamp); /* save rx timestamp */
1036 op->frames_abs++; /* statistics */
1038 DBG("got can_frame with can_id <%03X>\n", rxframe.can_id);
1040 DBG("Wrong skb->len = %d\n", skb->len);
1045 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1047 if (op->can_id != rxframe.can_id) {
1048 DBG("ERROR! Got wrong can_id <%03X>! Expected <%03X>.\n",
1049 rxframe.can_id, op->can_id);
1053 if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
1054 DBG("RTR-request\n");
1055 bcm_can_tx(op); /* send op->frames[0] to CAN device */
1059 if (op->flags & RX_FILTER_ID) { /* the easiest case */
1060 DBG("Easy does it with RX_FILTER_ID\n");
1061 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1062 bcm_rx_starttimer(op);
1066 if (op->nframes == 1) { /* simple compare with index 0 */
1067 DBG("Simple compare\n");
1068 bcm_rx_cmp_to_index(op, 0, &rxframe);
1069 bcm_rx_starttimer(op);
1073 if (op->nframes > 1) { /* multiplex compare */
1075 DBG("Multiplex compare\n");
1076 /* find the first multiplex mask that fits */
1077 /* MUX-mask is in index 0 */
1079 for (i=1; i < op->nframes; i++) {
1081 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1082 (GET_U64(&op->frames[0]) & GET_U64(&op->frames[i]))) {
1083 DBG("found MUX index %d\n", i);
1084 bcm_rx_cmp_to_index(op, i, &rxframe);
1088 bcm_rx_starttimer(op);
1092 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1093 struct can_frame *rxdata)
1095 /* no one uses the MSBs of can_dlc for comparation, */
1096 /* so we use it here to detect the first time of reception */
1098 if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
1099 DBG("first time :)\n");
1100 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1104 /* do a real check in can_data */
1106 DBG("op->frames[index].data = 0x%016llx\n", GET_U64(&op->frames[index]));
1107 DBG("op->last_frames[index].data = 0x%016llx\n",
1108 GET_U64(&op->last_frames[index]));
1109 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1111 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1112 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1113 DBG("relevant data change :)\n");
1114 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1119 if (op->flags & RX_CHECK_DLC) {
1121 /* do a real check in dlc */
1123 if (rxdata->can_dlc != (op->last_frames[index].can_dlc & BCM_CAN_DLC_MASK)) {
1124 DBG("dlc change :)\n");
1125 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1129 DBG("no relevant change :(\n");
1132 static void bcm_rx_update_and_send(struct bcm_op *op,
1133 struct can_frame *lastdata,
1134 struct can_frame *rxdata)
1136 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1138 memcpy(lastdata, rxdata, sizeof(struct can_frame));
1139 lastdata->can_dlc |= RX_RECV; /* mark as used */
1141 /* throttle bcm_rx_changed ? */
1142 if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
1143 ((op->j_ival2) && (nexttx > jiffies))) { /* we have to wait */
1145 lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
1147 if (!(op->thrtimer.expires)) { /* start only the first time */
1148 op->thrtimer.expires = nexttx;
1149 add_timer(&op->thrtimer);
1151 DBG("adding thrtimer. func=%p data=(%p) exp=0x%08X\n",
1152 op->thrtimer.function,
1153 (char*) op->thrtimer.data,
1154 (unsigned int) op->thrtimer.expires);
1157 bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
1160 static void bcm_rx_starttimer(struct bcm_op *op)
1162 if (op->flags & RX_NO_AUTOTIMER)
1167 op->timer.expires = jiffies + op->j_ival1;
1169 DBG("adding rx timeout timer ival1. func=%p data=(%p) exp=0x%08X\n",
1171 (char*) op->timer.data,
1172 (unsigned int) op->timer.expires);
1174 add_timer(&op->timer);
1179 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1181 struct bcm_msg_head head;
1183 op->j_lastmsg = jiffies;
1184 op->frames_filtered++; /* statistics */
1186 if (op->frames_filtered > ULONG_MAX/100)
1187 op->frames_filtered = op->frames_abs = 0; /* restart - spinlock ? */
1189 DBG("setting j_lastmsg to 0x%08X for rx_op(%p)\n",
1190 (unsigned int) op->j_lastmsg, op);
1191 DBG("sending notification\n");
1193 head.opcode = RX_CHANGED;
1194 head.flags = op->flags;
1195 head.count = op->count;
1196 head.ival1 = op->ival1;
1197 head.ival2 = op->ival2;
1198 head.can_id = op->can_id;
1201 bcm_send_to_user(op->sk, &head, data, &op->stamp);
1205 static void bcm_rx_timeout_handler(unsigned long data)
1207 struct bcm_op *op = (struct bcm_op*)data;
1208 struct bcm_msg_head msg_head;
1210 DBG("sending RX_TIMEOUT for can_id <%03X>. op is (%p)\n", op->can_id, op);
1212 msg_head.opcode = RX_TIMEOUT;
1213 msg_head.flags = op->flags;
1214 msg_head.count = op->count;
1215 msg_head.ival1 = op->ival1;
1216 msg_head.ival2 = op->ival2;
1217 msg_head.can_id = op->can_id;
1218 msg_head.nframes = 0;
1220 bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
1222 /* no restart of the timer is done here! */
1224 /* if the user wants to be informed, when cyclic CAN-Messages come back ... */
1225 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1226 /* clear received can_frames to indicate 'nothing received' */
1227 memset(op->last_frames, 0, op->nframes * sizeof(struct can_frame));
1228 DBG("RX_ANNOUNCE_RESTART\n");
1233 static void bcm_rx_thr_handler(unsigned long data)
1235 struct bcm_op *op = (struct bcm_op*)data;
1238 op->thrtimer.expires = 0; /* mark disabled / consumed timer */
1240 if (op->nframes > 1){
1242 DBG("sending MUX RX_CHANGED for can_id <%03X>. op is (%p)\n",
1244 /* for MUX filter we start at index 1 */
1245 for (i=1; i<op->nframes; i++){
1246 if ((op->last_frames) && (op->last_frames[i].can_dlc & RX_THR)){
1247 op->last_frames[i].can_dlc &= ~RX_THR;
1248 bcm_rx_changed(op, &op->last_frames[i]);
1253 DBG("sending simple RX_CHANGED for can_id <%03X>. op is (%p)\n",
1255 /* for RX_FILTER_ID and simple filter */
1256 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1257 op->last_frames[0].can_dlc &= ~RX_THR;
1258 bcm_rx_changed(op, &op->last_frames[0]);
1263 static void bcm_can_tx(struct bcm_op *op)
1265 struct sk_buff *skb;
1266 struct net_device *dev;
1267 struct can_frame *cf = &op->frames[op->currframe];
1269 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1271 skb = alloc_skb(sizeof(struct can_frame),
1272 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1277 memcpy(skb_put(skb, sizeof(struct can_frame)), cf, sizeof(struct can_frame));
1279 if (op->sk->sk_bound_dev_if) {
1280 dev = dev_get_by_index(op->sk->sk_bound_dev_if);
1285 can_send(skb, 1); /* send with loopback */
1291 op->frames_abs++; /* statistics */
1293 /* reached last frame? */
1294 if (op->currframe >= op->nframes)
1299 static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
1300 struct can_frame *frames, struct timeval *tv)
1302 struct sk_buff *skb;
1303 struct can_frame *firstframe;
1304 int datalen = head->nframes * sizeof(struct can_frame);
1308 DBG("no sk available\n");
1312 skb = alloc_skb(sizeof(*head) + datalen,
1313 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1314 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1315 firstframe = (struct can_frame *) skb->tail; /* can_frames starting here */
1318 skb_set_timestamp(skb, tv);
1321 memcpy(skb_put(skb, datalen), frames, datalen);
1323 /* the BCM uses the can_dlc-element of the can_frame structure
1324 for internal purposes. This is only relevant for updates
1325 that are generated by the BCM, where nframes is 1
1327 if (head->nframes == 1)
1328 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1330 if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
1331 DBG("sock_queue_rcv_skb failed: %d\n", err);
1336 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id)
1340 list_for_each_entry(p, ops, list)
1341 if (p->can_id == can_id)
1347 static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id)
1349 struct bcm_op *p, *n;
1351 list_for_each_entry_safe(p, n, ops, list) {
1352 if (p->can_id == can_id) {
1353 DBG("removing rx_op (%p) for can_id <%03X>\n", p, p->can_id);
1355 if (p->sk->sk_bound_dev_if) {
1356 struct net_device *dev = dev_get_by_index(p->sk->sk_bound_dev_if);
1358 can_rx_unregister(dev, p->can_id, BCM_RX_REGMASK, bcm_rx_handler, p);
1362 DBG("sock %p not bound for can_rx_unregister()\n", p->sk);
1371 static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id)
1373 struct bcm_op *p, *n;
1375 list_for_each_entry_safe(p, n, ops, list) {
1376 if (p->can_id == can_id) {
1377 DBG("removing rx_op (%p) for can_id <%03X>\n",
1386 static void bcm_remove_op(struct bcm_op *op)
1388 del_timer(&op->timer);
1389 del_timer(&op->thrtimer);
1392 if (op->last_frames)
1393 kfree(op->last_frames);
1399 module_init(bcm_init);
1400 module_exit(bcm_exit);