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/if_arp.h>
53 #include <linux/can.h>
54 #include <linux/can/core.h>
55 #include <linux/can/bcm.h>
58 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
61 /* use of last_frames[index].can_dlc */
62 #define RX_RECV 0x40 /* received data for this element */
63 #define RX_THR 0x80 /* element not been sent due to throttle feature */
64 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
66 /* get best masking value for can_rx_register() for a given single can_id */
67 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
71 #define CAN_BCM_VERSION CAN_VERSION
72 static __initdata const char banner[] = KERN_INFO
73 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
75 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 #ifdef CONFIG_CAN_DEBUG_CORE
81 module_param(debug, int, S_IRUGO);
82 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
85 /* easy access to can_frame payload */
86 static inline u64 GET_U64(const struct can_frame *cp)
88 return *(u64*)cp->data;
92 struct list_head list;
96 unsigned long j_ival1, j_ival2, j_lastmsg;
97 unsigned long frames_abs, frames_filtered;
98 struct timer_list timer, thrtimer;
99 struct timeval ival1, ival2;
100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
103 struct timeval rx_stamp;
109 struct can_frame *frames;
110 struct can_frame *last_frames;
111 struct can_frame sframe;
112 struct can_frame last_sframe;
114 struct net_device *rx_reg_dev;
117 static struct proc_dir_entry *proc_dir = NULL;
119 #ifdef CONFIG_CAN_BCM_USER
122 #define BCM_CAP CAP_NET_RAW
126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
133 struct notifier_block notifier;
134 struct list_head rx_ops;
135 struct list_head tx_ops;
136 unsigned long dropped_usr_msgs;
137 struct proc_dir_entry *bcm_proc_read;
138 char procname [9]; /* pointer printed in ASCII with \0 */
141 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
144 return (struct bcm_sock *)sk;
146 return (struct bcm_sock *)sk->sk_protinfo;
150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
151 static void *kzalloc(size_t size, unsigned int __nocast flags)
153 void *ret = kmalloc(size, flags);
156 memset(ret, 0, size);
161 static inline void skb_get_timestamp(const struct sk_buff *skb,
162 struct timeval *stamp)
164 stamp->tv_sec = skb->stamp.tv_sec;
165 stamp->tv_usec = skb->stamp.tv_usec;
168 static inline void skb_set_timestamp(struct sk_buff *skb,
169 const struct timeval *stamp)
171 skb->stamp.tv_sec = stamp->tv_sec;
172 skb->stamp.tv_usec = stamp->tv_usec;
176 #define CFSIZ sizeof(struct can_frame)
177 #define OPSIZ sizeof(struct bcm_op)
178 #define MHSIZ sizeof(struct bcm_msg_head)
181 * rounded_tv2jif - calculate jiffies from timeval including optional up
182 * @tv: pointer to timeval
185 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
186 * function is intentionally more relaxed on precise timer ticks to get exact
187 * one jiffy for requested 1000us on a 1000HZ machine.
188 * This code is to be removed when upgrading to kernel hrtimer.
191 * calculated jiffies (max: ULONG_MAX)
193 static unsigned long rounded_tv2jif(const struct timeval *tv)
195 unsigned long sec = tv->tv_sec;
196 unsigned long usec = tv->tv_usec;
199 if (sec > ULONG_MAX / HZ)
202 /* round up to get at least the requested time */
203 usec += 1000000 / HZ - 1;
205 jif = usec / (1000000 / HZ);
207 if (sec * HZ > ULONG_MAX - jif)
210 return jif + sec * HZ;
216 static char *bcm_proc_getifname(int ifindex)
218 struct net_device *dev;
223 dev = __dev_get_by_index(ifindex); /* no usage counting */
230 static int bcm_read_proc(char *page, char **start, off_t off,
231 int count, int *eof, void *data)
234 struct sock *sk = (struct sock *)data;
235 struct bcm_sock *bo = bcm_sk(sk);
238 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
240 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
241 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
242 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
243 bo->dropped_usr_msgs);
244 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
245 bcm_proc_getifname(bo->ifindex));
246 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
248 list_for_each_entry(op, &bo->rx_ops, list) {
250 unsigned long reduction;
252 /* print only active entries & prevent division by zero */
256 len += snprintf(page + len, PAGE_SIZE - len,
258 op->can_id, bcm_proc_getifname(op->ifindex));
259 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
261 (op->flags & RX_CHECK_DLC)?'d':' ');
263 len += snprintf(page + len, PAGE_SIZE - len,
264 "timeo=%ld ", op->j_ival1);
267 len += snprintf(page + len, PAGE_SIZE - len,
268 "thr=%ld ", op->j_ival2);
270 len += snprintf(page + len, PAGE_SIZE - len,
271 "# recv %ld (%ld) => reduction: ",
272 op->frames_filtered, op->frames_abs);
274 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
276 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
277 (reduction == 100)?"near ":"", reduction);
279 if (len > PAGE_SIZE - 200) {
280 /* mark output cut off */
281 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
286 list_for_each_entry(op, &bo->tx_ops, list) {
288 len += snprintf(page + len, PAGE_SIZE - len,
289 "tx_op: %03X %s [%d] ",
290 op->can_id, bcm_proc_getifname(op->ifindex),
293 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
297 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
300 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
303 if (len > PAGE_SIZE - 100) {
304 /* mark output cut off */
305 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
310 len += snprintf(page + len, PAGE_SIZE - len, "\n");
317 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
318 * of the given bcm tx op
320 static void bcm_can_tx(struct bcm_op *op)
323 struct net_device *dev;
324 struct can_frame *cf = &op->frames[op->currframe];
326 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
328 /* no target device? => exit */
332 dev = dev_get_by_index(op->ifindex);
335 /* RFC: should this bcm_op remove itself here? */
339 skb = alloc_skb(CFSIZ,
340 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
345 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
347 /* send with loopback */
352 /* update statistics */
356 /* reached last frame? */
357 if (op->currframe >= op->nframes)
364 * bcm_send_to_user - send a BCM message to the userspace
365 * (consisting of bcm_msg_head + x CAN frames)
367 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
368 struct can_frame *frames, int has_timestamp)
371 struct can_frame *firstframe;
372 struct sock *sk = op->sk;
373 int datalen = head->nframes * CFSIZ;
376 skb = alloc_skb(sizeof(*head) + datalen,
377 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
381 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
383 /* can_frames starting here */
384 firstframe = (struct can_frame *) skb->tail;
388 /* restore rx timestamp */
389 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
390 skb->tstamp = op->rx_stamp;
392 skb_set_timestamp(skb, &op->rx_stamp);
396 /* restore originator for recvfrom() */
397 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
398 skb->iif = op->rx_ifindex;
400 skb->input_dev = dev_get_by_index(op->rx_ifindex);
402 dev_put(skb->input_dev);
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_sock *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_sock *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_sock *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_sock *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_sock *bo = container_of(nb, struct bcm_sock, notifier);
1504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1505 struct sock *sk = &bo->sk;
1507 struct sock *sk = bo->sk;
1510 int notify_enodev = 0;
1512 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1513 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1515 if (dev->type != ARPHRD_CAN)
1520 case NETDEV_UNREGISTER:
1523 /* remove device specific receive entries */
1524 list_for_each_entry(op, &bo->rx_ops, list)
1525 if (op->rx_reg_dev == dev)
1526 bcm_rx_unreg(dev, op);
1528 /* remove device reference, if this is our bound device */
1529 if (bo->bound && bo->ifindex == dev->ifindex) {
1537 if (notify_enodev) {
1538 sk->sk_err = ENODEV;
1539 if (!sock_flag(sk, SOCK_DEAD))
1540 sk->sk_error_report(sk);
1545 if (bo->bound && bo->ifindex == dev->ifindex) {
1546 sk->sk_err = ENETDOWN;
1547 if (!sock_flag(sk, SOCK_DEAD))
1548 sk->sk_error_report(sk);
1556 * initial settings for all BCM sockets to be set at socket creation time
1558 static int bcm_init(struct sock *sk)
1560 struct bcm_sock *bo = bcm_sk(sk);
1562 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1567 bo->dropped_usr_msgs = 0;
1568 bo->bcm_proc_read = NULL;
1570 INIT_LIST_HEAD(&bo->tx_ops);
1571 INIT_LIST_HEAD(&bo->rx_ops);
1574 bo->notifier.notifier_call = bcm_notifier;
1576 register_netdevice_notifier(&bo->notifier);
1582 * standard socket functions
1584 static int bcm_release(struct socket *sock)
1586 struct sock *sk = sock->sk;
1587 struct bcm_sock *bo = bcm_sk(sk);
1588 struct bcm_op *op, *next;
1590 DBG("socket %p, sk %p\n", sock, sk);
1592 /* remove bcm_ops, timer, rx_unregister(), etc. */
1594 unregister_netdevice_notifier(&bo->notifier);
1598 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1599 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1603 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1604 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1607 * Don't care if we're bound or not (due to netdev problems)
1608 * can_rx_unregister() is always a save thing to do here.
1612 * Only remove subscriptions that had not
1613 * been removed due to NETDEV_UNREGISTER
1616 if (op->rx_reg_dev) {
1617 struct net_device *dev;
1619 dev = dev_get_by_index(op->ifindex);
1621 bcm_rx_unreg(dev, op);
1626 can_rx_unregister(NULL, op->can_id,
1627 REGMASK(op->can_id),
1628 bcm_rx_handler, op);
1633 /* remove procfs entry */
1634 if (proc_dir && bo->bcm_proc_read)
1635 remove_proc_entry(bo->procname, proc_dir);
1637 /* remove device reference */
1649 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1652 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1653 struct sock *sk = sock->sk;
1654 struct bcm_sock *bo = bcm_sk(sk);
1659 /* bind a device to this socket */
1660 if (addr->can_ifindex) {
1661 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1664 DBG("could not find device index %d\n",
1669 if (dev->type != ARPHRD_CAN) {
1670 DBG("device %d no CAN device\n", addr->can_ifindex);
1675 bo->ifindex = dev->ifindex;
1678 DBG("socket %p bound to device %s (idx %d)\n",
1679 sock, dev->name, dev->ifindex);
1682 /* no interface reference for ifindex = 0 ('any' CAN device) */
1689 /* unique socket address as filename */
1690 sprintf(bo->procname, "%p", sock);
1691 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1699 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1700 struct msghdr *msg, size_t size, int flags)
1702 struct sock *sk = sock->sk;
1703 struct sk_buff *skb;
1708 DBG("socket %p, sk %p\n", sock, sk);
1710 noblock = flags & MSG_DONTWAIT;
1711 flags &= ~MSG_DONTWAIT;
1712 skb = skb_recv_datagram(sk, flags, noblock, &error);
1716 DBG("delivering skbuff %p\n", skb);
1719 if (skb->len < size)
1722 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1724 skb_free_datagram(sk, skb);
1728 sock_recv_timestamp(msg, sk, skb);
1730 if (msg->msg_name) {
1731 struct sockaddr_can *addr = msg->msg_name;
1732 msg->msg_namelen = sizeof(*addr);
1733 memset(addr, 0, sizeof(*addr));
1734 addr->can_family = AF_CAN;
1735 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1736 addr->can_ifindex = skb->iif;
1739 * FIXME: Race condition, skb->input_dev might disappear
1740 * while skb is waiting on the queue.
1743 addr->can_ifindex = skb->input_dev->ifindex;
1745 addr->can_ifindex = 0;
1749 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1750 skb_free_datagram(sk, skb);
1755 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1758 unsigned int mask = 0;
1760 DBG("socket %p\n", sock);
1762 mask = datagram_poll(file, sock, wait);
1766 static struct proto_ops bcm_ops = {
1768 .release = bcm_release,
1769 .bind = sock_no_bind,
1770 .connect = bcm_connect,
1771 .socketpair = sock_no_socketpair,
1772 .accept = sock_no_accept,
1773 .getname = sock_no_getname,
1775 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1776 .listen = sock_no_listen,
1777 .shutdown = sock_no_shutdown,
1778 .setsockopt = sock_no_setsockopt,
1779 .getsockopt = sock_no_getsockopt,
1780 .sendmsg = bcm_sendmsg,
1781 .recvmsg = bcm_recvmsg,
1782 .mmap = sock_no_mmap,
1783 .sendpage = sock_no_sendpage,
1786 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1787 static struct proto bcm_proto = {
1789 .owner = THIS_MODULE,
1790 .obj_size = sizeof(struct bcm_sock),
1794 static struct can_proto bcm_can_proto = {
1796 .protocol = CAN_BCM,
1797 .capability = BCM_CAP,
1802 static struct can_proto bcm_can_proto = {
1804 .protocol = CAN_BCM,
1805 .capability = BCM_CAP,
1807 .owner = THIS_MODULE,
1808 .obj_size = sizeof(struct bcm_sock),
1813 static int __init bcm_module_init(void)
1817 can_proto_register(&bcm_can_proto);
1819 /* create /proc/net/can/bcm directory */
1820 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1823 proc_dir->owner = THIS_MODULE;
1828 static void __exit bcm_module_exit(void)
1830 can_proto_unregister(&bcm_can_proto);
1833 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1836 module_init(bcm_module_init);
1837 module_exit(bcm_module_exit);