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/module.h>
46 #include <linux/init.h>
47 #include <linux/version.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
52 #include <linux/can.h>
53 #include <linux/can/core.h>
54 #include <linux/can/bcm.h>
57 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
60 /* use of last_frames[index].can_dlc */
61 #define RX_RECV 0x40 /* received data for this element */
62 #define RX_THR 0x80 /* element not been sent due to throttle feature */
63 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
65 /* get best masking value for can_rx_register() for a given single can_id */
66 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
67 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
70 #define CAN_BCM_VERSION CAN_VERSION
71 static __initdata const char banner[] = KERN_INFO
72 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
74 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
78 #ifdef CONFIG_CAN_DEBUG_CORE
80 module_param(debug, int, S_IRUGO);
81 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
84 /* easy access to can_frame payload */
85 static inline u64 GET_U64(const struct can_frame *cp)
87 return *(u64*)cp->data;
91 struct list_head list;
95 unsigned long j_ival1, j_ival2, j_lastmsg;
96 unsigned long frames_abs, frames_filtered;
97 struct timer_list timer, thrtimer;
98 struct timeval ival1, ival2;
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
102 struct timeval rx_stamp;
108 struct can_frame *frames;
109 struct can_frame *last_frames;
110 struct can_frame sframe;
111 struct can_frame last_sframe;
113 struct net_device *rx_reg_dev;
119 struct notifier_block notifier;
120 struct list_head rx_ops;
121 struct list_head tx_ops;
122 unsigned long dropped_usr_msgs;
123 struct proc_dir_entry *bcm_proc_read;
124 char procname [9]; /* pointer printed in ASCII with \0 */
127 static struct proc_dir_entry *proc_dir = NULL;
129 #ifdef CONFIG_CAN_BCM_USER
132 #define BCM_CAP CAP_NET_RAW
135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
141 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
143 return &((struct bcm_sock *)sk)->opt;
146 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
148 return (struct bcm_opt *)sk->sk_protinfo;
152 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
153 static void *kzalloc(size_t size, unsigned int __nocast flags)
155 void *ret = kmalloc(size, flags);
158 memset(ret, 0, size);
163 static inline void skb_get_timestamp(const struct sk_buff *skb,
164 struct timeval *stamp)
166 stamp->tv_sec = skb->stamp.tv_sec;
167 stamp->tv_usec = skb->stamp.tv_usec;
170 static inline void skb_set_timestamp(struct sk_buff *skb,
171 const struct timeval *stamp)
173 skb->stamp.tv_sec = stamp->tv_sec;
174 skb->stamp.tv_usec = stamp->tv_usec;
178 #define CFSIZ sizeof(struct can_frame)
179 #define OPSIZ sizeof(struct bcm_op)
180 #define MHSIZ sizeof(struct bcm_msg_head)
183 * rounded_tv2jif - calculate jiffies from timeval including optional up
184 * @tv: pointer to timeval
187 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
188 * function is intentionally more relaxed on precise timer ticks to get exact
189 * one jiffy for requested 1000us on a 1000HZ machine.
190 * This code is to be removed when upgrading to kernel hrtimer.
193 * calculated jiffies (max: ULONG_MAX)
195 static unsigned long rounded_tv2jif(const struct timeval *tv)
197 unsigned long sec = tv->tv_sec;
198 unsigned long usec = tv->tv_usec;
201 if (sec > ULONG_MAX / HZ)
204 /* round up to get at least the requested time */
205 usec += 1000000 / HZ - 1;
207 jif = usec / (1000000 / HZ);
209 if (sec * HZ > ULONG_MAX - jif)
212 return jif + sec * HZ;
218 static char *bcm_proc_getifname(int ifindex)
220 struct net_device *dev;
225 dev = __dev_get_by_index(ifindex); /* no usage counting */
232 static int bcm_read_proc(char *page, char **start, off_t off,
233 int count, int *eof, void *data)
236 struct sock *sk = (struct sock *)data;
237 struct bcm_opt *bo = bcm_sk(sk);
240 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
242 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
243 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
244 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
245 bo->dropped_usr_msgs);
246 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
247 bcm_proc_getifname(bo->ifindex));
248 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
250 list_for_each_entry(op, &bo->rx_ops, list) {
252 unsigned long reduction;
254 /* print only active entries & prevent division by zero */
258 len += snprintf(page + len, PAGE_SIZE - len,
260 op->can_id, bcm_proc_getifname(op->ifindex));
261 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
263 (op->flags & RX_CHECK_DLC)?'d':' ');
265 len += snprintf(page + len, PAGE_SIZE - len,
266 "timeo=%ld ", op->j_ival1);
269 len += snprintf(page + len, PAGE_SIZE - len,
270 "thr=%ld ", op->j_ival2);
272 len += snprintf(page + len, PAGE_SIZE - len,
273 "# recv %ld (%ld) => reduction: ",
274 op->frames_filtered, op->frames_abs);
276 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
278 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
279 (reduction == 100)?"near ":"", reduction);
281 if (len > PAGE_SIZE - 200) {
282 /* mark output cut off */
283 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
288 list_for_each_entry(op, &bo->tx_ops, list) {
290 len += snprintf(page + len, PAGE_SIZE - len,
291 "tx_op: %03X %s [%d] ",
292 op->can_id, bcm_proc_getifname(op->ifindex),
295 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
299 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
302 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
305 if (len > PAGE_SIZE - 100) {
306 /* mark output cut off */
307 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
312 len += snprintf(page + len, PAGE_SIZE - len, "\n");
319 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
320 * of the given bcm tx op
322 static void bcm_can_tx(struct bcm_op *op)
325 struct net_device *dev;
326 struct can_frame *cf = &op->frames[op->currframe];
328 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
330 /* no target device? => exit */
334 dev = dev_get_by_index(op->ifindex);
337 /* RFC: should this bcm_op remove itself here? */
341 skb = alloc_skb(CFSIZ,
342 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
347 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
349 /* send with loopback */
354 /* update statistics */
358 /* reached last frame? */
359 if (op->currframe >= op->nframes)
366 * bcm_send_to_user - send a BCM message to the userspace
367 * (consisting of bcm_msg_head + x CAN frames)
369 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
370 struct can_frame *frames, int has_timestamp)
373 struct can_frame *firstframe;
374 struct sock *sk = op->sk;
375 int datalen = head->nframes * CFSIZ;
376 struct sockaddr_can *addr;
379 skb = alloc_skb(sizeof(*head) + datalen,
380 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
384 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
386 /* can_frames starting here */
387 firstframe = (struct can_frame *) skb->tail;
391 /* restore rx timestamp */
392 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
393 skb->tstamp = op->rx_stamp;
395 skb_set_timestamp(skb, &op->rx_stamp);
399 addr = (struct sockaddr_can *)skb->cb;
400 memset(addr, 0, sizeof(*addr));
401 addr->can_family = AF_CAN;
402 /* restore originator for recvfrom() */
403 addr->can_ifindex = op->rx_ifindex;
406 memcpy(skb_put(skb, datalen), frames, datalen);
409 * the BCM uses the can_dlc-element of the can_frame
410 * structure for internal purposes. This is only
411 * relevant for updates that are generated by the
412 * BCM, where nframes is 1
414 if (head->nframes == 1)
415 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
418 err = sock_queue_rcv_skb(sk, skb);
420 struct bcm_opt *bo = bcm_sk(sk);
422 DBG("sock_queue_rcv_skb failed: %d\n", err);
424 /* don't care about overflows in this statistic */
425 bo->dropped_usr_msgs++;
430 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
432 static void bcm_tx_timeout_handler(unsigned long data)
434 struct bcm_op *op = (struct bcm_op*)data;
436 DBG("Called with bcm_op %p\n", op);
438 if (op->j_ival1 && (op->count > 0)) {
441 if (!op->count && (op->flags & TX_COUNTEVT)) {
442 struct bcm_msg_head msg_head;
444 /* create notification to user */
445 DBG("sending TX_EXPIRED for can_id %03X\n",
448 msg_head.opcode = TX_EXPIRED;
449 msg_head.flags = op->flags;
450 msg_head.count = op->count;
451 msg_head.ival1 = op->ival1;
452 msg_head.ival2 = op->ival2;
453 msg_head.can_id = op->can_id;
454 msg_head.nframes = 0;
456 bcm_send_to_user(op, &msg_head, NULL, 0);
460 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
461 op->count, op->j_ival1, op->j_ival2);
463 if (op->j_ival1 && (op->count > 0)) {
465 op->timer.expires = jiffies + op->j_ival1;
466 add_timer(&op->timer);
468 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
470 (char*) op->timer.data,
471 (unsigned int) op->timer.expires);
473 /* send (next) frame */
478 op->timer.expires = jiffies + op->j_ival2;
479 add_timer(&op->timer);
481 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
483 (char*) op->timer.data,
484 (unsigned int) op->timer.expires);
486 /* send (next) frame */
490 DBG("no timer restart\n");
497 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
499 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
501 struct bcm_msg_head head;
503 op->j_lastmsg = jiffies;
505 /* update statistics */
506 op->frames_filtered++;
508 /* prevent statistics overflow */
509 if (op->frames_filtered > ULONG_MAX/100)
510 op->frames_filtered = op->frames_abs = 0;
512 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
513 (unsigned int) op->j_lastmsg, op);
514 DBG("sending notification\n");
516 head.opcode = RX_CHANGED;
517 head.flags = op->flags;
518 head.count = op->count;
519 head.ival1 = op->ival1;
520 head.ival2 = op->ival2;
521 head.can_id = op->can_id;
524 bcm_send_to_user(op, &head, data, 1);
528 * bcm_rx_update_and_send - process a detected relevant receive content change
529 * 1. update the last received data
530 * 2. send a notification to the user (if possible)
532 static void bcm_rx_update_and_send(struct bcm_op *op,
533 struct can_frame *lastdata,
534 struct can_frame *rxdata)
536 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
538 memcpy(lastdata, rxdata, CFSIZ);
541 lastdata->can_dlc |= RX_RECV;
543 /* throttle bcm_rx_changed ? */
544 if ((op->thrtimer.expires) ||
545 ((op->j_ival2) && (nexttx > jiffies))) {
546 /* we are already waiting OR we have to start waiting */
548 /* mark as 'throttled' */
549 lastdata->can_dlc |= RX_THR;
551 if (!(op->thrtimer.expires)) {
552 /* start the timer only the first time */
553 op->thrtimer.expires = nexttx;
554 add_timer(&op->thrtimer);
556 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
557 op->thrtimer.function,
558 (char*) op->thrtimer.data,
559 (unsigned int) op->thrtimer.expires);
563 /* send RX_CHANGED to the user immediately */
564 bcm_rx_changed(op, rxdata);
569 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
570 * received data stored in op->last_frames[]
572 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
573 struct can_frame *rxdata)
576 * no one uses the MSBs of can_dlc for comparation,
577 * so we use it here to detect the first time of reception
580 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
581 /* received data for the first time => send update to user */
582 DBG("first time :)\n");
583 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
587 /* do a real check in can_frame data section */
589 DBG("op->frames[index].data = 0x%016llx\n",
590 GET_U64(&op->frames[index]));
591 DBG("op->last_frames[index].data = 0x%016llx\n",
592 GET_U64(&op->last_frames[index]));
593 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
595 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
596 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
597 DBG("relevant data change :)\n");
598 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
602 if (op->flags & RX_CHECK_DLC) {
603 /* do a real check in can_frame dlc */
604 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
606 DBG("dlc change :)\n");
607 bcm_rx_update_and_send(op, &op->last_frames[index],
612 DBG("no relevant change :(\n");
616 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
618 static void bcm_rx_starttimer(struct bcm_op *op)
620 if (op->flags & RX_NO_AUTOTIMER)
624 op->timer.expires = jiffies + op->j_ival1;
626 DBG("adding rx timeout timer ival1. func=%p data=%p "
629 (char*) op->timer.data,
630 (unsigned int) op->timer.expires);
632 add_timer(&op->timer);
637 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
639 static void bcm_rx_timeout_handler(unsigned long data)
641 struct bcm_op *op = (struct bcm_op*)data;
642 struct bcm_msg_head msg_head;
644 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
646 msg_head.opcode = RX_TIMEOUT;
647 msg_head.flags = op->flags;
648 msg_head.count = op->count;
649 msg_head.ival1 = op->ival1;
650 msg_head.ival2 = op->ival2;
651 msg_head.can_id = op->can_id;
652 msg_head.nframes = 0;
654 bcm_send_to_user(op, &msg_head, NULL, 0);
656 /* no restart of the timer is done here! */
658 /* if user wants to be informed, when cyclic CAN-Messages come back */
659 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
660 /* clear received can_frames to indicate 'nothing received' */
661 memset(op->last_frames, 0, op->nframes * CFSIZ);
662 DBG("RX_ANNOUNCE_RESTART\n");
667 * bcm_rx_thr_handler - the time for blocked content updates is over now:
668 * Check for throttled data and send it to the userspace
670 static void bcm_rx_thr_handler(unsigned long data)
672 struct bcm_op *op = (struct bcm_op*)data;
675 /* mark disabled / consumed timer */
676 op->thrtimer.expires = 0;
678 if (op->nframes > 1) {
679 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
681 /* for MUX filter we start at index 1 */
682 for (i=1; i<op->nframes; i++) {
683 if ((op->last_frames) &&
684 (op->last_frames[i].can_dlc & RX_THR)) {
685 op->last_frames[i].can_dlc &= ~RX_THR;
686 bcm_rx_changed(op, &op->last_frames[i]);
691 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
693 /* for RX_FILTER_ID and simple filter */
694 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
695 op->last_frames[0].can_dlc &= ~RX_THR;
696 bcm_rx_changed(op, &op->last_frames[0]);
702 * bcm_rx_handler - handle a CAN frame receiption
704 static void bcm_rx_handler(struct sk_buff *skb, void *data)
706 struct bcm_op *op = (struct bcm_op*)data;
707 struct can_frame rxframe;
710 /* disable timeout */
711 del_timer(&op->timer);
713 DBG("Called with bcm_op %p\n", op);
715 if (skb->len == sizeof(rxframe)) {
716 memcpy(&rxframe, skb->data, sizeof(rxframe));
717 /* save rx timestamp */
718 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
719 op->rx_stamp = skb->tstamp;
721 skb_get_timestamp(skb, &op->rx_stamp);
723 /* save originator for recvfrom() */
724 op->rx_ifindex = skb->dev->ifindex;
725 /* update statistics */
728 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
731 DBG("Wrong skb->len = %d\n", skb->len);
736 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
738 if (op->can_id != rxframe.can_id) {
739 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
740 rxframe.can_id, op->can_id);
744 if (op->flags & RX_RTR_FRAME) {
745 /* send reply for RTR-request */
746 DBG("RTR-request\n");
748 /* send op->frames[0] to CAN device */
753 if (op->flags & RX_FILTER_ID) {
754 /* the easiest case */
755 DBG("Easy does it with RX_FILTER_ID\n");
757 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
758 bcm_rx_starttimer(op);
762 if (op->nframes == 1) {
763 /* simple compare with index 0 */
764 DBG("Simple compare\n");
766 bcm_rx_cmp_to_index(op, 0, &rxframe);
767 bcm_rx_starttimer(op);
771 if (op->nframes > 1) {
772 /* multiplex compare */
773 DBG("Multiplex compare\n");
776 * find the first multiplex mask that fits.
777 * Remark: The MUX-mask is stored in index 0
780 for (i=1; i < op->nframes; i++) {
781 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
782 (GET_U64(&op->frames[0]) &
783 GET_U64(&op->frames[i]))) {
784 DBG("found MUX index %d\n", i);
785 bcm_rx_cmp_to_index(op, i, &rxframe);
789 bcm_rx_starttimer(op);
794 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
796 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
801 list_for_each_entry(op, ops, list) {
802 if ((op->can_id == can_id) && (op->ifindex == ifindex))
809 static void bcm_remove_op(struct bcm_op *op)
811 del_timer(&op->timer);
812 del_timer(&op->thrtimer);
814 if ((op->frames) && (op->frames != &op->sframe))
817 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
818 kfree(op->last_frames);
825 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
827 if (op->rx_reg_dev == dev) {
828 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
831 /* mark as removed subscription */
832 op->rx_reg_dev = NULL;
834 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
835 "mismatch %p %p\n", op->rx_reg_dev, dev);
839 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
841 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
843 struct bcm_op *op, *n;
845 list_for_each_entry_safe(op, n, ops, list) {
846 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
847 DBG("removing rx_op %p for can_id %03X\n",
851 * Don't care if we're bound or not (due to netdev
852 * problems) can_rx_unregister() is always a save
857 * Only remove subscriptions that had not
858 * been removed due to NETDEV_UNREGISTER
861 if (op->rx_reg_dev) {
862 struct net_device *dev;
864 dev = dev_get_by_index(op->ifindex);
866 bcm_rx_unreg(dev, op);
871 can_rx_unregister(NULL, op->can_id,
881 return 0; /* not found */
885 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
887 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
889 struct bcm_op *op, *n;
891 list_for_each_entry_safe(op, n, ops, list) {
892 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
893 DBG("removing rx_op %p for can_id %03X\n",
901 return 0; /* not found */
905 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
907 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
910 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
913 DBG("TRX_READ: did not find op for can_id %03X\n",
918 DBG("TRX_READ: sending status for can_id %03X\n",
920 /* put current values into msg_head */
921 msg_head->flags = op->flags;
922 msg_head->count = op->count;
923 msg_head->ival1 = op->ival1;
924 msg_head->ival2 = op->ival2;
925 msg_head->nframes = op->nframes;
927 bcm_send_to_user(op, msg_head, op->frames, 0);
933 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
935 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
936 int ifindex, struct sock *sk)
938 struct bcm_opt *bo = bcm_sk(sk);
942 /* we need a real device to send frames */
946 /* we need at least one can_frame */
947 if (msg_head->nframes < 1)
950 /* check the given can_id */
951 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
954 /* update existing BCM operation */
956 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
957 op, msg_head->can_id);
960 * Do we need more space for the can_frames than currently
961 * allocated? -> This is a _really_ unusual use-case and
962 * therefore (complexity / locking) it is not supported.
964 if (msg_head->nframes > op->nframes)
967 /* update can_frames content */
968 for (i = 0; i < msg_head->nframes; i++) {
969 err = memcpy_fromiovec((u8*)&op->frames[i],
970 msg->msg_iov, CFSIZ);
974 if (msg_head->flags & TX_CP_CAN_ID) {
975 /* copy can_id into frame */
976 op->frames[i].can_id = msg_head->can_id;
981 /* insert new BCM operation for the given can_id */
983 op = kzalloc(OPSIZ, GFP_KERNEL);
987 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
988 op, msg_head->can_id);
990 op->can_id = msg_head->can_id;
992 /* create array for can_frames and copy the data */
993 if (msg_head->nframes > 1) {
994 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1001 op->frames = &op->sframe;
1003 for (i = 0; i < msg_head->nframes; i++) {
1004 err = memcpy_fromiovec((u8*)&op->frames[i],
1005 msg->msg_iov, CFSIZ);
1007 if (op->frames != &op->sframe)
1013 if (msg_head->flags & TX_CP_CAN_ID) {
1014 /* copy can_id into frame */
1015 op->frames[i].can_id = msg_head->can_id;
1019 /* tx_ops never compare with previous received messages */
1020 op->last_frames = NULL;
1022 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1025 op->ifindex = ifindex;
1027 /* initialize uninitialized (kmalloc) structure */
1028 init_timer(&op->timer);
1030 /* currently unused in tx_ops */
1031 init_timer(&op->thrtimer);
1033 /* handler for tx_ops */
1034 op->timer.function = bcm_tx_timeout_handler;
1036 /* timer.data points to this op-structure */
1037 op->timer.data = (unsigned long)op;
1039 /* add this bcm_op to the list of the tx_ops */
1040 list_add(&op->list, &bo->tx_ops);
1042 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1044 if (op->nframes != msg_head->nframes) {
1045 op->nframes = msg_head->nframes;
1046 /* start multiple frame transmission with index 0 */
1052 op->flags = msg_head->flags;
1054 if (op->flags & TX_RESET_MULTI_IDX) {
1055 /* start multiple frame transmission with index 0 */
1059 if (op->flags & SETTIMER) {
1060 /* set timer values */
1062 op->count = msg_head->count;
1063 op->ival1 = msg_head->ival1;
1064 op->ival2 = msg_head->ival2;
1065 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1066 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1068 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1069 op->count, op->j_ival1, op->j_ival2);
1071 /* disable an active timer due to zero values? */
1072 if (!op->j_ival1 && !op->j_ival2) {
1073 del_timer(&op->timer);
1074 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1078 if ((op->flags & STARTTIMER) &&
1079 ((op->j_ival1 && op->count) || op->j_ival2)) {
1081 del_timer(&op->timer);
1083 /* spec: send can_frame when starting timer */
1084 op->flags |= TX_ANNOUNCE;
1086 if (op->j_ival1 && (op->count > 0)) {
1087 op->timer.expires = jiffies + op->j_ival1;
1088 /* op->count-- is done in bcm_tx_timeout_handler */
1089 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1092 (char*) op->timer.data,
1093 (unsigned int) op->timer.expires);
1096 op->timer.expires = jiffies + op->j_ival2;
1097 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1100 (char*) op->timer.data,
1101 (unsigned int) op->timer.expires);
1104 add_timer(&op->timer);
1107 if (op->flags & TX_ANNOUNCE)
1110 return msg_head->nframes * CFSIZ + MHSIZ;
1114 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1116 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1117 int ifindex, struct sock *sk)
1119 struct bcm_opt *bo = bcm_sk(sk);
1124 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1125 /* be robust against wrong usage ... */
1126 msg_head->flags |= RX_FILTER_ID;
1127 msg_head->nframes = 0; /* ignore trailing garbage */
1130 if ((msg_head->flags & RX_RTR_FRAME) &&
1131 ((msg_head->nframes != 1) ||
1132 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1134 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1138 /* check the given can_id */
1139 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1141 /* update existing BCM operation */
1143 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1144 op, msg_head->can_id);
1147 * Do we need more space for the can_frames than currently
1148 * allocated? -> This is a _really_ unusual use-case and
1149 * therefore (complexity / locking) it is not supported.
1151 if (msg_head->nframes > op->nframes)
1154 if (msg_head->nframes) {
1155 /* update can_frames content */
1156 err = memcpy_fromiovec((u8*)op->frames,
1158 msg_head->nframes * CFSIZ);
1162 /* clear last_frames to indicate 'nothing received' */
1163 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1166 op->nframes = msg_head->nframes;
1168 /* Only an update -> do not call can_rx_register() */
1172 /* insert new BCM operation for the given can_id */
1174 op = kzalloc(OPSIZ, GFP_KERNEL);
1178 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1179 op, msg_head->can_id);
1181 op->can_id = msg_head->can_id;
1182 op->nframes = msg_head->nframes;
1184 if (msg_head->nframes > 1) {
1185 /* create array for can_frames and copy the data */
1186 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1193 /* create and init array for received can_frames */
1194 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1196 if (!op->last_frames) {
1203 op->frames = &op->sframe;
1204 op->last_frames = &op->last_sframe;
1207 if (msg_head->nframes) {
1208 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1209 msg_head->nframes * CFSIZ);
1211 if (op->frames != &op->sframe)
1213 if (op->last_frames != &op->last_sframe)
1214 kfree(op->last_frames);
1221 op->ifindex = ifindex;
1223 /* initialize uninitialized (kzalloc) structure */
1224 init_timer(&op->timer);
1226 /* init throttle timer for RX_CHANGED */
1227 init_timer(&op->thrtimer);
1229 /* handler for rx timeouts */
1230 op->timer.function = bcm_rx_timeout_handler;
1232 /* timer.data points to this op-structure */
1233 op->timer.data = (unsigned long)op;
1235 /* handler for RX_CHANGED throttle timeouts */
1236 op->thrtimer.function = bcm_rx_thr_handler;
1238 /* timer.data points to this op-structure */
1239 op->thrtimer.data = (unsigned long)op;
1241 /* mark disabled timer */
1242 op->thrtimer.expires = 0;
1244 /* add this bcm_op to the list of the tx_ops */
1245 list_add(&op->list, &bo->rx_ops);
1247 /* call can_rx_register() */
1250 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1253 op->flags = msg_head->flags;
1255 if (op->flags & RX_RTR_FRAME) {
1257 /* no timers in RTR-mode */
1258 del_timer(&op->thrtimer);
1259 del_timer(&op->timer);
1262 * funny feature in RX(!)_SETUP only for RTR-mode:
1263 * copy can_id into frame BUT without RTR-flag to
1264 * prevent a full-load-loopback-test ... ;-]
1266 if ((op->flags & TX_CP_CAN_ID) ||
1267 (op->frames[0].can_id == op->can_id))
1268 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1271 if (op->flags & SETTIMER) {
1273 /* set timer value */
1274 op->ival1 = msg_head->ival1;
1275 op->ival2 = msg_head->ival2;
1276 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1277 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1279 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1280 op->j_ival1, op->j_ival2);
1282 /* disable an active timer due to zero value? */
1284 del_timer(&op->timer);
1285 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1288 /* free currently blocked msgs ? */
1289 if (op->thrtimer.expires) {
1290 DBG("RX_SETUP: unblocking throttled msgs.\n");
1291 del_timer(&op->thrtimer);
1292 /* send blocked msgs hereafter */
1293 op->thrtimer.expires = jiffies + 2;
1294 add_timer(&op->thrtimer);
1297 * if (op->j_ival2) is zero, no (new) throttling
1298 * will happen. For details see functions
1299 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1303 if ((op->flags & STARTTIMER) && op->j_ival1) {
1305 del_timer(&op->timer);
1306 op->timer.expires = jiffies + op->j_ival1;
1308 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1310 (char *) op->timer.function,
1311 (char *) op->timer.data,
1312 (unsigned int) op->timer.expires);
1314 add_timer(&op->timer);
1318 /* now we can register for can_ids, if we added a new bcm_op */
1319 if (do_rx_register) {
1320 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1321 "rx_op is %p\n", op->can_id, op);
1324 struct net_device *dev = dev_get_by_index(ifindex);
1327 can_rx_register(dev, op->can_id,
1328 REGMASK(op->can_id),
1329 bcm_rx_handler, op, IDENT);
1330 op->rx_reg_dev = dev;
1335 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1336 bcm_rx_handler, op, IDENT);
1339 return msg_head->nframes * CFSIZ + MHSIZ;
1343 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1345 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1347 struct sk_buff *skb;
1348 struct net_device *dev;
1351 /* just copy and send one can_frame */
1353 if (!ifindex) /* we need a real device to send frames */
1356 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1361 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1367 DBG_FRAME("BCM: TX_SEND: sending frame",
1368 (struct can_frame *)skb->data);
1370 dev = dev_get_by_index(ifindex);
1378 can_send(skb, 1); /* send with loopback */
1381 return CFSIZ + MHSIZ;
1385 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1387 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1388 struct msghdr *msg, size_t size)
1390 struct sock *sk = sock->sk;
1391 struct bcm_opt *bo = bcm_sk(sk);
1392 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1393 struct bcm_msg_head msg_head;
1394 int ret; /* read bytes or error codes as return value */
1397 DBG("sock %p not bound\n", sk);
1401 /* check for alternative ifindex for this bcm_op */
1403 if (!ifindex && msg->msg_name) {
1404 /* no bound device as default => check msg_name */
1405 struct sockaddr_can *addr =
1406 (struct sockaddr_can *)msg->msg_name;
1408 if (addr->can_family != AF_CAN)
1411 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1414 struct net_device *dev = dev_get_by_index(ifindex);
1417 DBG("device %d not found\n", ifindex);
1421 if (dev->type != ARPHRD_CAN) {
1422 DBG("device %d no CAN device\n", ifindex);
1431 /* read message head information */
1433 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1437 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1441 switch (msg_head.opcode) {
1444 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1448 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1452 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1459 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1466 /* reuse msg_head for the reply to TX_READ */
1467 msg_head.opcode = TX_STATUS;
1468 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1472 /* reuse msg_head for the reply to RX_READ */
1473 msg_head.opcode = RX_STATUS;
1474 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1478 /* we need at least one can_frame */
1479 if (msg_head.nframes < 1)
1482 ret = bcm_tx_send(msg, ifindex, sk);
1486 DBG("Unknown opcode %d\n", msg_head.opcode);
1497 * notification handler for netdevice status changes
1499 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1502 struct net_device *dev = (struct net_device *)data;
1503 struct bcm_opt *bo = container_of(nb, struct bcm_opt, notifier);
1504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1505 struct bcm_sock *bs = container_of(bo, struct bcm_sock, opt);
1506 struct sock *sk = &bs->sk;
1508 #error TODO (if needed): Notifier support for Kernel Versions < 2.6.12
1511 int notify_enodev = 0;
1513 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1514 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1516 if (dev->type != ARPHRD_CAN)
1521 case NETDEV_UNREGISTER:
1524 /* remove device specific receive entries */
1525 list_for_each_entry(op, &bo->rx_ops, list)
1526 if (op->rx_reg_dev == dev)
1527 bcm_rx_unreg(dev, op);
1529 /* remove device reference, if this is our bound device */
1530 if (bo->bound && bo->ifindex == dev->ifindex) {
1538 if (notify_enodev) {
1539 sk->sk_err = ENODEV;
1540 if (!sock_flag(sk, SOCK_DEAD))
1541 sk->sk_error_report(sk);
1546 if (bo->bound && bo->ifindex == dev->ifindex) {
1547 sk->sk_err = ENETDOWN;
1548 if (!sock_flag(sk, SOCK_DEAD))
1549 sk->sk_error_report(sk);
1557 * initial settings for all BCM sockets to be set at socket creation time
1559 static int bcm_init(struct sock *sk)
1561 struct bcm_opt *bo = bcm_sk(sk);
1565 bo->dropped_usr_msgs = 0;
1566 bo->bcm_proc_read = NULL;
1568 INIT_LIST_HEAD(&bo->tx_ops);
1569 INIT_LIST_HEAD(&bo->rx_ops);
1572 bo->notifier.notifier_call = bcm_notifier;
1574 register_netdevice_notifier(&bo->notifier);
1580 * standard socket functions
1582 static int bcm_release(struct socket *sock)
1584 struct sock *sk = sock->sk;
1585 struct bcm_opt *bo = bcm_sk(sk);
1586 struct bcm_op *op, *next;
1588 DBG("socket %p, sk %p\n", sock, sk);
1590 /* remove bcm_ops, timer, rx_unregister(), etc. */
1592 unregister_netdevice_notifier(&bo->notifier);
1596 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1597 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1601 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1602 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1605 * Don't care if we're bound or not (due to netdev problems)
1606 * can_rx_unregister() is always a save thing to do here.
1610 * Only remove subscriptions that had not
1611 * been removed due to NETDEV_UNREGISTER
1614 if (op->rx_reg_dev) {
1615 struct net_device *dev;
1617 dev = dev_get_by_index(op->ifindex);
1619 bcm_rx_unreg(dev, op);
1624 can_rx_unregister(NULL, op->can_id,
1625 REGMASK(op->can_id),
1626 bcm_rx_handler, op);
1631 /* remove procfs entry */
1632 if (proc_dir && bo->bcm_proc_read)
1633 remove_proc_entry(bo->procname, proc_dir);
1635 /* remove device reference */
1647 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1650 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1651 struct sock *sk = sock->sk;
1652 struct bcm_opt *bo = bcm_sk(sk);
1657 /* bind a device to this socket */
1658 if (addr->can_ifindex) {
1659 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1662 DBG("could not find device index %d\n",
1667 if (dev->type != ARPHRD_CAN) {
1668 DBG("device %d no CAN device\n", addr->can_ifindex);
1673 bo->ifindex = dev->ifindex;
1676 DBG("socket %p bound to device %s (idx %d)\n",
1677 sock, dev->name, dev->ifindex);
1680 /* no interface reference for ifindex = 0 ('any' CAN device) */
1687 /* unique socket address as filename */
1688 sprintf(bo->procname, "%p", sock);
1689 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1697 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1698 struct msghdr *msg, size_t size, int flags)
1700 struct sock *sk = sock->sk;
1701 struct sk_buff *skb;
1706 DBG("socket %p, sk %p\n", sock, sk);
1708 noblock = flags & MSG_DONTWAIT;
1709 flags &= ~MSG_DONTWAIT;
1710 skb = skb_recv_datagram(sk, flags, noblock, &error);
1714 DBG("delivering skbuff %p\n", skb);
1717 if (skb->len < size)
1720 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1722 skb_free_datagram(sk, skb);
1726 sock_recv_timestamp(msg, sk, skb);
1728 if (msg->msg_name) {
1729 msg->msg_namelen = sizeof(struct sockaddr_can);
1730 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1733 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1734 skb_free_datagram(sk, skb);
1739 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1742 unsigned int mask = 0;
1744 DBG("socket %p\n", sock);
1746 mask = datagram_poll(file, sock, wait);
1750 static struct proto_ops bcm_ops = {
1752 .release = bcm_release,
1753 .bind = sock_no_bind,
1754 .connect = bcm_connect,
1755 .socketpair = sock_no_socketpair,
1756 .accept = sock_no_accept,
1757 .getname = sock_no_getname,
1759 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1760 .listen = sock_no_listen,
1761 .shutdown = sock_no_shutdown,
1762 .setsockopt = sock_no_setsockopt,
1763 .getsockopt = sock_no_getsockopt,
1764 .sendmsg = bcm_sendmsg,
1765 .recvmsg = bcm_recvmsg,
1766 .mmap = sock_no_mmap,
1767 .sendpage = sock_no_sendpage,
1770 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1771 static struct proto bcm_proto = {
1773 .owner = THIS_MODULE,
1774 .obj_size = sizeof(struct bcm_sock),
1778 static struct can_proto bcm_can_proto = {
1780 .protocol = CAN_BCM,
1781 .capability = BCM_CAP,
1786 static struct can_proto bcm_can_proto = {
1788 .protocol = CAN_BCM,
1789 .capability = BCM_CAP,
1791 .owner = THIS_MODULE,
1792 .obj_size = sizeof(struct bcm_opt),
1797 static int __init bcm_module_init(void)
1801 can_proto_register(&bcm_can_proto);
1803 /* create /proc/net/can/bcm directory */
1804 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1807 proc_dir->owner = THIS_MODULE;
1812 static void __exit bcm_module_exit(void)
1814 can_proto_unregister(&bcm_can_proto);
1817 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1820 module_init(bcm_module_init);
1821 module_exit(bcm_module_exit);