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/version.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/proc_fs.h>
50 #include <linux/uio.h>
51 #include <linux/poll.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/socket.h>
55 #include <linux/if_arp.h>
56 #include <linux/skbuff.h>
57 #include <linux/can.h>
58 #include <linux/can/core.h>
59 #include <linux/can/bcm.h>
62 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
65 /* use of last_frames[index].can_dlc */
66 #define RX_RECV 0x40 /* received data for this element */
67 #define RX_THR 0x80 /* element not been sent due to throttle feature */
68 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
70 /* get best masking value for can_rx_register() for a given single can_id */
71 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
72 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
75 #define CAN_BCM_VERSION CAN_VERSION
76 static __initdata const char banner[] = KERN_INFO
77 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
79 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
80 MODULE_LICENSE("Dual BSD/GPL");
81 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
83 #ifdef CONFIG_CAN_DEBUG_CORE
85 module_param(debug, int, S_IRUGO);
86 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
89 /* easy access to can_frame payload */
90 static inline u64 GET_U64(const struct can_frame *cp)
92 return *(u64*)cp->data;
96 struct list_head list;
100 unsigned long j_ival1, j_ival2, j_lastmsg;
101 unsigned long frames_abs, frames_filtered;
102 struct timer_list timer, thrtimer;
103 struct timeval ival1, ival2;
104 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
107 struct timeval rx_stamp;
113 struct can_frame *frames;
114 struct can_frame *last_frames;
115 struct can_frame sframe;
116 struct can_frame last_sframe;
118 struct net_device *rx_reg_dev;
121 static struct proc_dir_entry *proc_dir = NULL;
123 #ifdef CONFIG_CAN_BCM_USER
126 #define BCM_CAP CAP_NET_RAW
130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
137 struct notifier_block notifier;
138 struct list_head rx_ops;
139 struct list_head tx_ops;
140 unsigned long dropped_usr_msgs;
141 struct proc_dir_entry *bcm_proc_read;
142 char procname [9]; /* pointer printed in ASCII with \0 */
145 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
147 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
148 return (struct bcm_sock *)sk;
150 return (struct bcm_sock *)sk->sk_protinfo;
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
155 static void *kzalloc(size_t size, unsigned int __nocast flags)
157 void *ret = kmalloc(size, flags);
160 memset(ret, 0, size);
165 static inline void skb_get_timestamp(const struct sk_buff *skb,
166 struct timeval *stamp)
168 stamp->tv_sec = skb->stamp.tv_sec;
169 stamp->tv_usec = skb->stamp.tv_usec;
172 static inline void skb_set_timestamp(struct sk_buff *skb,
173 const struct timeval *stamp)
175 skb->stamp.tv_sec = stamp->tv_sec;
176 skb->stamp.tv_usec = stamp->tv_usec;
180 #define CFSIZ sizeof(struct can_frame)
181 #define OPSIZ sizeof(struct bcm_op)
182 #define MHSIZ sizeof(struct bcm_msg_head)
185 * rounded_tv2jif - calculate jiffies from timeval including optional up
186 * @tv: pointer to timeval
189 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
190 * function is intentionally more relaxed on precise timer ticks to get exact
191 * one jiffy for requested 1000us on a 1000HZ machine.
192 * This code is to be removed when upgrading to kernel hrtimer.
195 * calculated jiffies (max: ULONG_MAX)
197 static unsigned long rounded_tv2jif(const struct timeval *tv)
199 unsigned long sec = tv->tv_sec;
200 unsigned long usec = tv->tv_usec;
203 if (sec > ULONG_MAX / HZ)
206 /* round up to get at least the requested time */
207 usec += 1000000 / HZ - 1;
209 jif = usec / (1000000 / HZ);
211 if (sec * HZ > ULONG_MAX - jif)
214 return jif + sec * HZ;
220 static char *bcm_proc_getifname(int ifindex)
222 struct net_device *dev;
227 dev = __dev_get_by_index(ifindex); /* no usage counting */
234 static int bcm_read_proc(char *page, char **start, off_t off,
235 int count, int *eof, void *data)
238 struct sock *sk = (struct sock *)data;
239 struct bcm_sock *bo = bcm_sk(sk);
242 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
244 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
245 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
246 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
247 bo->dropped_usr_msgs);
248 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
249 bcm_proc_getifname(bo->ifindex));
250 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
252 list_for_each_entry(op, &bo->rx_ops, list) {
254 unsigned long reduction;
256 /* print only active entries & prevent division by zero */
260 len += snprintf(page + len, PAGE_SIZE - len,
262 op->can_id, bcm_proc_getifname(op->ifindex));
263 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
265 (op->flags & RX_CHECK_DLC)?'d':' ');
267 len += snprintf(page + len, PAGE_SIZE - len,
268 "timeo=%ld ", op->j_ival1);
271 len += snprintf(page + len, PAGE_SIZE - len,
272 "thr=%ld ", op->j_ival2);
274 len += snprintf(page + len, PAGE_SIZE - len,
275 "# recv %ld (%ld) => reduction: ",
276 op->frames_filtered, op->frames_abs);
278 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
280 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
281 (reduction == 100)?"near ":"", reduction);
283 if (len > PAGE_SIZE - 200) {
284 /* mark output cut off */
285 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
290 list_for_each_entry(op, &bo->tx_ops, list) {
292 len += snprintf(page + len, PAGE_SIZE - len,
293 "tx_op: %03X %s [%d] ",
294 op->can_id, bcm_proc_getifname(op->ifindex),
297 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
301 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
304 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
307 if (len > PAGE_SIZE - 100) {
308 /* mark output cut off */
309 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
314 len += snprintf(page + len, PAGE_SIZE - len, "\n");
321 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
322 * of the given bcm tx op
324 static void bcm_can_tx(struct bcm_op *op)
327 struct net_device *dev;
328 struct can_frame *cf = &op->frames[op->currframe];
330 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
332 /* no target device? => exit */
336 dev = dev_get_by_index(op->ifindex);
339 /* RFC: should this bcm_op remove itself here? */
343 skb = alloc_skb(CFSIZ,
344 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
349 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
351 /* send with loopback */
356 /* update statistics */
360 /* reached last frame? */
361 if (op->currframe >= op->nframes)
368 * bcm_send_to_user - send a BCM message to the userspace
369 * (consisting of bcm_msg_head + x CAN frames)
371 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
372 struct can_frame *frames, int has_timestamp)
375 struct can_frame *firstframe;
376 struct sockaddr_can *addr;
377 struct sock *sk = op->sk;
378 int datalen = head->nframes * CFSIZ;
381 skb = alloc_skb(sizeof(*head) + datalen,
382 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
386 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
388 /* can_frames starting here */
389 firstframe = (struct can_frame *) skb->tail;
393 /* restore rx timestamp */
394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
395 skb->tstamp = op->rx_stamp;
397 skb_set_timestamp(skb, &op->rx_stamp);
401 /* restore originator for recvfrom() */
402 addr = (struct sockaddr_can *)skb->cb;
403 memset(addr, 0, sizeof(*addr));
404 addr->can_family = AF_CAN;
405 addr->can_ifindex = op->rx_ifindex;
408 memcpy(skb_put(skb, datalen), frames, datalen);
411 * the BCM uses the can_dlc-element of the can_frame
412 * structure for internal purposes. This is only
413 * relevant for updates that are generated by the
414 * BCM, where nframes is 1
416 if (head->nframes == 1)
417 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
420 err = sock_queue_rcv_skb(sk, skb);
422 struct bcm_sock *bo = bcm_sk(sk);
424 DBG("sock_queue_rcv_skb failed: %d\n", err);
426 /* don't care about overflows in this statistic */
427 bo->dropped_usr_msgs++;
432 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
434 static void bcm_tx_timeout_handler(unsigned long data)
436 struct bcm_op *op = (struct bcm_op*)data;
438 DBG("Called with bcm_op %p\n", op);
440 if (op->j_ival1 && (op->count > 0)) {
443 if (!op->count && (op->flags & TX_COUNTEVT)) {
444 struct bcm_msg_head msg_head;
446 /* create notification to user */
447 DBG("sending TX_EXPIRED for can_id %03X\n",
450 msg_head.opcode = TX_EXPIRED;
451 msg_head.flags = op->flags;
452 msg_head.count = op->count;
453 msg_head.ival1 = op->ival1;
454 msg_head.ival2 = op->ival2;
455 msg_head.can_id = op->can_id;
456 msg_head.nframes = 0;
458 bcm_send_to_user(op, &msg_head, NULL, 0);
462 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
463 op->count, op->j_ival1, op->j_ival2);
465 if (op->j_ival1 && (op->count > 0)) {
467 op->timer.expires = jiffies + op->j_ival1;
468 add_timer(&op->timer);
470 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
472 (char*) op->timer.data,
473 (unsigned int) op->timer.expires);
475 /* send (next) frame */
480 op->timer.expires = jiffies + op->j_ival2;
481 add_timer(&op->timer);
483 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
485 (char*) op->timer.data,
486 (unsigned int) op->timer.expires);
488 /* send (next) frame */
492 DBG("no timer restart\n");
499 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
501 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
503 struct bcm_msg_head head;
505 op->j_lastmsg = jiffies;
507 /* update statistics */
508 op->frames_filtered++;
510 /* prevent statistics overflow */
511 if (op->frames_filtered > ULONG_MAX/100)
512 op->frames_filtered = op->frames_abs = 0;
514 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
515 (unsigned int) op->j_lastmsg, op);
516 DBG("sending notification\n");
518 head.opcode = RX_CHANGED;
519 head.flags = op->flags;
520 head.count = op->count;
521 head.ival1 = op->ival1;
522 head.ival2 = op->ival2;
523 head.can_id = op->can_id;
526 bcm_send_to_user(op, &head, data, 1);
530 * bcm_rx_update_and_send - process a detected relevant receive content change
531 * 1. update the last received data
532 * 2. send a notification to the user (if possible)
534 static void bcm_rx_update_and_send(struct bcm_op *op,
535 struct can_frame *lastdata,
536 struct can_frame *rxdata)
538 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
540 memcpy(lastdata, rxdata, CFSIZ);
543 lastdata->can_dlc |= RX_RECV;
545 /* throttle bcm_rx_changed ? */
546 if ((op->thrtimer.expires) ||
547 ((op->j_ival2) && (nexttx > jiffies))) {
548 /* we are already waiting OR we have to start waiting */
550 /* mark as 'throttled' */
551 lastdata->can_dlc |= RX_THR;
553 if (!(op->thrtimer.expires)) {
554 /* start the timer only the first time */
555 op->thrtimer.expires = nexttx;
556 add_timer(&op->thrtimer);
558 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
559 op->thrtimer.function,
560 (char*) op->thrtimer.data,
561 (unsigned int) op->thrtimer.expires);
565 /* send RX_CHANGED to the user immediately */
566 bcm_rx_changed(op, rxdata);
571 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
572 * received data stored in op->last_frames[]
574 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
575 struct can_frame *rxdata)
578 * no one uses the MSBs of can_dlc for comparation,
579 * so we use it here to detect the first time of reception
582 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
583 /* received data for the first time => send update to user */
584 DBG("first time :)\n");
585 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
589 /* do a real check in can_frame data section */
591 DBG("op->frames[index].data = 0x%016llx\n",
592 GET_U64(&op->frames[index]));
593 DBG("op->last_frames[index].data = 0x%016llx\n",
594 GET_U64(&op->last_frames[index]));
595 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
597 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
598 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
599 DBG("relevant data change :)\n");
600 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
604 if (op->flags & RX_CHECK_DLC) {
605 /* do a real check in can_frame dlc */
606 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
608 DBG("dlc change :)\n");
609 bcm_rx_update_and_send(op, &op->last_frames[index],
614 DBG("no relevant change :(\n");
618 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
620 static void bcm_rx_starttimer(struct bcm_op *op)
622 if (op->flags & RX_NO_AUTOTIMER)
626 op->timer.expires = jiffies + op->j_ival1;
628 DBG("adding rx timeout timer ival1. func=%p data=%p "
631 (char*) op->timer.data,
632 (unsigned int) op->timer.expires);
634 add_timer(&op->timer);
639 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
641 static void bcm_rx_timeout_handler(unsigned long data)
643 struct bcm_op *op = (struct bcm_op*)data;
644 struct bcm_msg_head msg_head;
646 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
648 msg_head.opcode = RX_TIMEOUT;
649 msg_head.flags = op->flags;
650 msg_head.count = op->count;
651 msg_head.ival1 = op->ival1;
652 msg_head.ival2 = op->ival2;
653 msg_head.can_id = op->can_id;
654 msg_head.nframes = 0;
656 bcm_send_to_user(op, &msg_head, NULL, 0);
658 /* no restart of the timer is done here! */
660 /* if user wants to be informed, when cyclic CAN-Messages come back */
661 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
662 /* clear received can_frames to indicate 'nothing received' */
663 memset(op->last_frames, 0, op->nframes * CFSIZ);
664 DBG("RX_ANNOUNCE_RESTART\n");
669 * bcm_rx_thr_handler - the time for blocked content updates is over now:
670 * Check for throttled data and send it to the userspace
672 static void bcm_rx_thr_handler(unsigned long data)
674 struct bcm_op *op = (struct bcm_op*)data;
677 /* mark disabled / consumed timer */
678 op->thrtimer.expires = 0;
680 if (op->nframes > 1) {
681 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
683 /* for MUX filter we start at index 1 */
684 for (i=1; i<op->nframes; i++) {
685 if ((op->last_frames) &&
686 (op->last_frames[i].can_dlc & RX_THR)) {
687 op->last_frames[i].can_dlc &= ~RX_THR;
688 bcm_rx_changed(op, &op->last_frames[i]);
693 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
695 /* for RX_FILTER_ID and simple filter */
696 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
697 op->last_frames[0].can_dlc &= ~RX_THR;
698 bcm_rx_changed(op, &op->last_frames[0]);
704 * bcm_rx_handler - handle a CAN frame receiption
706 static void bcm_rx_handler(struct sk_buff *skb, void *data)
708 struct bcm_op *op = (struct bcm_op*)data;
709 struct can_frame rxframe;
712 /* disable timeout */
713 del_timer(&op->timer);
715 DBG("Called with bcm_op %p\n", op);
717 if (skb->len == sizeof(rxframe)) {
718 memcpy(&rxframe, skb->data, sizeof(rxframe));
719 /* save rx timestamp */
720 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
721 op->rx_stamp = skb->tstamp;
723 skb_get_timestamp(skb, &op->rx_stamp);
725 /* save originator for recvfrom() */
726 op->rx_ifindex = skb->dev->ifindex;
727 /* update statistics */
730 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
733 DBG("Wrong skb->len = %d\n", skb->len);
738 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
740 if (op->can_id != rxframe.can_id) {
741 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
742 rxframe.can_id, op->can_id);
746 if (op->flags & RX_RTR_FRAME) {
747 /* send reply for RTR-request */
748 DBG("RTR-request\n");
750 /* send op->frames[0] to CAN device */
755 if (op->flags & RX_FILTER_ID) {
756 /* the easiest case */
757 DBG("Easy does it with RX_FILTER_ID\n");
759 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
760 bcm_rx_starttimer(op);
764 if (op->nframes == 1) {
765 /* simple compare with index 0 */
766 DBG("Simple compare\n");
768 bcm_rx_cmp_to_index(op, 0, &rxframe);
769 bcm_rx_starttimer(op);
773 if (op->nframes > 1) {
774 /* multiplex compare */
775 DBG("Multiplex compare\n");
778 * find the first multiplex mask that fits.
779 * Remark: The MUX-mask is stored in index 0
782 for (i=1; i < op->nframes; i++) {
783 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
784 (GET_U64(&op->frames[0]) &
785 GET_U64(&op->frames[i]))) {
786 DBG("found MUX index %d\n", i);
787 bcm_rx_cmp_to_index(op, i, &rxframe);
791 bcm_rx_starttimer(op);
796 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
798 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
803 list_for_each_entry(op, ops, list) {
804 if ((op->can_id == can_id) && (op->ifindex == ifindex))
811 static void bcm_remove_op(struct bcm_op *op)
813 del_timer(&op->timer);
814 del_timer(&op->thrtimer);
816 if ((op->frames) && (op->frames != &op->sframe))
819 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
820 kfree(op->last_frames);
827 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
829 if (op->rx_reg_dev == dev) {
830 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
833 /* mark as removed subscription */
834 op->rx_reg_dev = NULL;
836 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
837 "mismatch %p %p\n", op->rx_reg_dev, dev);
841 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
843 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
845 struct bcm_op *op, *n;
847 list_for_each_entry_safe(op, n, ops, list) {
848 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
849 DBG("removing rx_op %p for can_id %03X\n",
853 * Don't care if we're bound or not (due to netdev
854 * problems) can_rx_unregister() is always a save
859 * Only remove subscriptions that had not
860 * been removed due to NETDEV_UNREGISTER
863 if (op->rx_reg_dev) {
864 struct net_device *dev;
866 dev = dev_get_by_index(op->ifindex);
868 bcm_rx_unreg(dev, op);
873 can_rx_unregister(NULL, op->can_id,
883 return 0; /* not found */
887 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
889 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
891 struct bcm_op *op, *n;
893 list_for_each_entry_safe(op, n, ops, list) {
894 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
895 DBG("removing rx_op %p for can_id %03X\n",
903 return 0; /* not found */
907 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
909 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
912 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
915 DBG("TRX_READ: did not find op for can_id %03X\n",
920 DBG("TRX_READ: sending status for can_id %03X\n",
922 /* put current values into msg_head */
923 msg_head->flags = op->flags;
924 msg_head->count = op->count;
925 msg_head->ival1 = op->ival1;
926 msg_head->ival2 = op->ival2;
927 msg_head->nframes = op->nframes;
929 bcm_send_to_user(op, msg_head, op->frames, 0);
935 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
937 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
938 int ifindex, struct sock *sk)
940 struct bcm_sock *bo = bcm_sk(sk);
944 /* we need a real device to send frames */
948 /* we need at least one can_frame */
949 if (msg_head->nframes < 1)
952 /* check the given can_id */
953 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
956 /* update existing BCM operation */
958 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
959 op, msg_head->can_id);
962 * Do we need more space for the can_frames than currently
963 * allocated? -> This is a _really_ unusual use-case and
964 * therefore (complexity / locking) it is not supported.
966 if (msg_head->nframes > op->nframes)
969 /* update can_frames content */
970 for (i = 0; i < msg_head->nframes; i++) {
971 err = memcpy_fromiovec((u8*)&op->frames[i],
972 msg->msg_iov, CFSIZ);
976 if (msg_head->flags & TX_CP_CAN_ID) {
977 /* copy can_id into frame */
978 op->frames[i].can_id = msg_head->can_id;
983 /* insert new BCM operation for the given can_id */
985 op = kzalloc(OPSIZ, GFP_KERNEL);
989 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
990 op, msg_head->can_id);
992 op->can_id = msg_head->can_id;
994 /* create array for can_frames and copy the data */
995 if (msg_head->nframes > 1) {
996 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1003 op->frames = &op->sframe;
1005 for (i = 0; i < msg_head->nframes; i++) {
1006 err = memcpy_fromiovec((u8*)&op->frames[i],
1007 msg->msg_iov, CFSIZ);
1009 if (op->frames != &op->sframe)
1015 if (msg_head->flags & TX_CP_CAN_ID) {
1016 /* copy can_id into frame */
1017 op->frames[i].can_id = msg_head->can_id;
1021 /* tx_ops never compare with previous received messages */
1022 op->last_frames = NULL;
1024 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1027 op->ifindex = ifindex;
1029 /* initialize uninitialized (kmalloc) structure */
1030 init_timer(&op->timer);
1032 /* currently unused in tx_ops */
1033 init_timer(&op->thrtimer);
1035 /* handler for tx_ops */
1036 op->timer.function = bcm_tx_timeout_handler;
1038 /* timer.data points to this op-structure */
1039 op->timer.data = (unsigned long)op;
1041 /* add this bcm_op to the list of the tx_ops */
1042 list_add(&op->list, &bo->tx_ops);
1044 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1046 if (op->nframes != msg_head->nframes) {
1047 op->nframes = msg_head->nframes;
1048 /* start multiple frame transmission with index 0 */
1054 op->flags = msg_head->flags;
1056 if (op->flags & TX_RESET_MULTI_IDX) {
1057 /* start multiple frame transmission with index 0 */
1061 if (op->flags & SETTIMER) {
1062 /* set timer values */
1064 op->count = msg_head->count;
1065 op->ival1 = msg_head->ival1;
1066 op->ival2 = msg_head->ival2;
1067 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1068 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1070 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1071 op->count, op->j_ival1, op->j_ival2);
1073 /* disable an active timer due to zero values? */
1074 if (!op->j_ival1 && !op->j_ival2) {
1075 del_timer(&op->timer);
1076 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1080 if ((op->flags & STARTTIMER) &&
1081 ((op->j_ival1 && op->count) || op->j_ival2)) {
1083 del_timer(&op->timer);
1085 /* spec: send can_frame when starting timer */
1086 op->flags |= TX_ANNOUNCE;
1088 if (op->j_ival1 && (op->count > 0)) {
1089 op->timer.expires = jiffies + op->j_ival1;
1090 /* op->count-- is done in bcm_tx_timeout_handler */
1091 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1094 (char*) op->timer.data,
1095 (unsigned int) op->timer.expires);
1098 op->timer.expires = jiffies + op->j_ival2;
1099 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1102 (char*) op->timer.data,
1103 (unsigned int) op->timer.expires);
1106 add_timer(&op->timer);
1109 if (op->flags & TX_ANNOUNCE)
1112 return msg_head->nframes * CFSIZ + MHSIZ;
1116 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1118 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1119 int ifindex, struct sock *sk)
1121 struct bcm_sock *bo = bcm_sk(sk);
1126 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1127 /* be robust against wrong usage ... */
1128 msg_head->flags |= RX_FILTER_ID;
1129 msg_head->nframes = 0; /* ignore trailing garbage */
1132 if ((msg_head->flags & RX_RTR_FRAME) &&
1133 ((msg_head->nframes != 1) ||
1134 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1136 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1140 /* check the given can_id */
1141 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1143 /* update existing BCM operation */
1145 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1146 op, msg_head->can_id);
1149 * Do we need more space for the can_frames than currently
1150 * allocated? -> This is a _really_ unusual use-case and
1151 * therefore (complexity / locking) it is not supported.
1153 if (msg_head->nframes > op->nframes)
1156 if (msg_head->nframes) {
1157 /* update can_frames content */
1158 err = memcpy_fromiovec((u8*)op->frames,
1160 msg_head->nframes * CFSIZ);
1164 /* clear last_frames to indicate 'nothing received' */
1165 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1168 op->nframes = msg_head->nframes;
1170 /* Only an update -> do not call can_rx_register() */
1174 /* insert new BCM operation for the given can_id */
1176 op = kzalloc(OPSIZ, GFP_KERNEL);
1180 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1181 op, msg_head->can_id);
1183 op->can_id = msg_head->can_id;
1184 op->nframes = msg_head->nframes;
1186 if (msg_head->nframes > 1) {
1187 /* create array for can_frames and copy the data */
1188 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1195 /* create and init array for received can_frames */
1196 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1198 if (!op->last_frames) {
1205 op->frames = &op->sframe;
1206 op->last_frames = &op->last_sframe;
1209 if (msg_head->nframes) {
1210 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1211 msg_head->nframes * CFSIZ);
1213 if (op->frames != &op->sframe)
1215 if (op->last_frames != &op->last_sframe)
1216 kfree(op->last_frames);
1223 op->ifindex = ifindex;
1225 /* initialize uninitialized (kzalloc) structure */
1226 init_timer(&op->timer);
1228 /* init throttle timer for RX_CHANGED */
1229 init_timer(&op->thrtimer);
1231 /* handler for rx timeouts */
1232 op->timer.function = bcm_rx_timeout_handler;
1234 /* timer.data points to this op-structure */
1235 op->timer.data = (unsigned long)op;
1237 /* handler for RX_CHANGED throttle timeouts */
1238 op->thrtimer.function = bcm_rx_thr_handler;
1240 /* timer.data points to this op-structure */
1241 op->thrtimer.data = (unsigned long)op;
1243 /* mark disabled timer */
1244 op->thrtimer.expires = 0;
1246 /* add this bcm_op to the list of the tx_ops */
1247 list_add(&op->list, &bo->rx_ops);
1249 /* call can_rx_register() */
1252 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1255 op->flags = msg_head->flags;
1257 if (op->flags & RX_RTR_FRAME) {
1259 /* no timers in RTR-mode */
1260 del_timer(&op->thrtimer);
1261 del_timer(&op->timer);
1264 * funny feature in RX(!)_SETUP only for RTR-mode:
1265 * copy can_id into frame BUT without RTR-flag to
1266 * prevent a full-load-loopback-test ... ;-]
1268 if ((op->flags & TX_CP_CAN_ID) ||
1269 (op->frames[0].can_id == op->can_id))
1270 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1273 if (op->flags & SETTIMER) {
1275 /* set timer value */
1276 op->ival1 = msg_head->ival1;
1277 op->ival2 = msg_head->ival2;
1278 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1279 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1281 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1282 op->j_ival1, op->j_ival2);
1284 /* disable an active timer due to zero value? */
1286 del_timer(&op->timer);
1287 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1290 /* free currently blocked msgs ? */
1291 if (op->thrtimer.expires) {
1292 DBG("RX_SETUP: unblocking throttled msgs.\n");
1293 del_timer(&op->thrtimer);
1294 /* send blocked msgs hereafter */
1295 op->thrtimer.expires = jiffies + 2;
1296 add_timer(&op->thrtimer);
1299 * if (op->j_ival2) is zero, no (new) throttling
1300 * will happen. For details see functions
1301 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1305 if ((op->flags & STARTTIMER) && op->j_ival1) {
1307 del_timer(&op->timer);
1308 op->timer.expires = jiffies + op->j_ival1;
1310 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1312 (char *) op->timer.function,
1313 (char *) op->timer.data,
1314 (unsigned int) op->timer.expires);
1316 add_timer(&op->timer);
1320 /* now we can register for can_ids, if we added a new bcm_op */
1321 if (do_rx_register) {
1322 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1323 "rx_op is %p\n", op->can_id, op);
1326 struct net_device *dev = dev_get_by_index(ifindex);
1329 can_rx_register(dev, op->can_id,
1330 REGMASK(op->can_id),
1331 bcm_rx_handler, op, IDENT);
1332 op->rx_reg_dev = dev;
1337 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1338 bcm_rx_handler, op, IDENT);
1341 return msg_head->nframes * CFSIZ + MHSIZ;
1345 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1347 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1349 struct sk_buff *skb;
1350 struct net_device *dev;
1353 /* just copy and send one can_frame */
1355 if (!ifindex) /* we need a real device to send frames */
1358 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1363 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1369 DBG_FRAME("BCM: TX_SEND: sending frame",
1370 (struct can_frame *)skb->data);
1372 dev = dev_get_by_index(ifindex);
1380 can_send(skb, 1); /* send with loopback */
1383 return CFSIZ + MHSIZ;
1387 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1389 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1390 struct msghdr *msg, size_t size)
1392 struct sock *sk = sock->sk;
1393 struct bcm_sock *bo = bcm_sk(sk);
1394 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1395 struct bcm_msg_head msg_head;
1396 int ret; /* read bytes or error codes as return value */
1399 DBG("sock %p not bound\n", sk);
1403 /* check for alternative ifindex for this bcm_op */
1405 if (!ifindex && msg->msg_name) {
1406 /* no bound device as default => check msg_name */
1407 struct sockaddr_can *addr =
1408 (struct sockaddr_can *)msg->msg_name;
1410 if (addr->can_family != AF_CAN)
1413 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1416 struct net_device *dev = dev_get_by_index(ifindex);
1419 DBG("device %d not found\n", ifindex);
1423 if (dev->type != ARPHRD_CAN) {
1424 DBG("device %d no CAN device\n", ifindex);
1433 /* read message head information */
1435 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1439 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1443 switch (msg_head.opcode) {
1446 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1450 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1454 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1461 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1468 /* reuse msg_head for the reply to TX_READ */
1469 msg_head.opcode = TX_STATUS;
1470 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1474 /* reuse msg_head for the reply to RX_READ */
1475 msg_head.opcode = RX_STATUS;
1476 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1480 /* we need at least one can_frame */
1481 if (msg_head.nframes < 1)
1484 ret = bcm_tx_send(msg, ifindex, sk);
1488 DBG("Unknown opcode %d\n", msg_head.opcode);
1499 * notification handler for netdevice status changes
1501 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1504 struct net_device *dev = (struct net_device *)data;
1505 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1506 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1507 struct sock *sk = &bo->sk;
1509 struct sock *sk = bo->sk;
1512 int notify_enodev = 0;
1514 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1515 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1517 if (dev->type != ARPHRD_CAN)
1522 case NETDEV_UNREGISTER:
1525 /* remove device specific receive entries */
1526 list_for_each_entry(op, &bo->rx_ops, list)
1527 if (op->rx_reg_dev == dev)
1528 bcm_rx_unreg(dev, op);
1530 /* remove device reference, if this is our bound device */
1531 if (bo->bound && bo->ifindex == dev->ifindex) {
1539 if (notify_enodev) {
1540 sk->sk_err = ENODEV;
1541 if (!sock_flag(sk, SOCK_DEAD))
1542 sk->sk_error_report(sk);
1547 if (bo->bound && bo->ifindex == dev->ifindex) {
1548 sk->sk_err = ENETDOWN;
1549 if (!sock_flag(sk, SOCK_DEAD))
1550 sk->sk_error_report(sk);
1558 * initial settings for all BCM sockets to be set at socket creation time
1560 static int bcm_init(struct sock *sk)
1562 struct bcm_sock *bo = bcm_sk(sk);
1564 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1569 bo->dropped_usr_msgs = 0;
1570 bo->bcm_proc_read = NULL;
1572 INIT_LIST_HEAD(&bo->tx_ops);
1573 INIT_LIST_HEAD(&bo->rx_ops);
1576 bo->notifier.notifier_call = bcm_notifier;
1578 register_netdevice_notifier(&bo->notifier);
1584 * standard socket functions
1586 static int bcm_release(struct socket *sock)
1588 struct sock *sk = sock->sk;
1589 struct bcm_sock *bo = bcm_sk(sk);
1590 struct bcm_op *op, *next;
1592 DBG("socket %p, sk %p\n", sock, sk);
1594 /* remove bcm_ops, timer, rx_unregister(), etc. */
1596 unregister_netdevice_notifier(&bo->notifier);
1600 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1601 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1605 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1606 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1609 * Don't care if we're bound or not (due to netdev problems)
1610 * can_rx_unregister() is always a save thing to do here.
1614 * Only remove subscriptions that had not
1615 * been removed due to NETDEV_UNREGISTER
1618 if (op->rx_reg_dev) {
1619 struct net_device *dev;
1621 dev = dev_get_by_index(op->ifindex);
1623 bcm_rx_unreg(dev, op);
1628 can_rx_unregister(NULL, op->can_id,
1629 REGMASK(op->can_id),
1630 bcm_rx_handler, op);
1635 /* remove procfs entry */
1636 if (proc_dir && bo->bcm_proc_read)
1637 remove_proc_entry(bo->procname, proc_dir);
1639 /* remove device reference */
1651 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1654 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1655 struct sock *sk = sock->sk;
1656 struct bcm_sock *bo = bcm_sk(sk);
1661 /* bind a device to this socket */
1662 if (addr->can_ifindex) {
1663 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1666 DBG("could not find device index %d\n",
1671 if (dev->type != ARPHRD_CAN) {
1672 DBG("device %d no CAN device\n", addr->can_ifindex);
1677 bo->ifindex = dev->ifindex;
1680 DBG("socket %p bound to device %s (idx %d)\n",
1681 sock, dev->name, dev->ifindex);
1684 /* no interface reference for ifindex = 0 ('any' CAN device) */
1691 /* unique socket address as filename */
1692 sprintf(bo->procname, "%p", sock);
1693 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1701 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1702 struct msghdr *msg, size_t size, int flags)
1704 struct sock *sk = sock->sk;
1705 struct sk_buff *skb;
1710 DBG("socket %p, sk %p\n", sock, sk);
1712 noblock = flags & MSG_DONTWAIT;
1713 flags &= ~MSG_DONTWAIT;
1714 skb = skb_recv_datagram(sk, flags, noblock, &error);
1718 DBG("delivering skbuff %p\n", skb);
1721 if (skb->len < size)
1724 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1726 skb_free_datagram(sk, skb);
1730 sock_recv_timestamp(msg, sk, skb);
1732 if (msg->msg_name) {
1733 msg->msg_namelen = sizeof(struct sockaddr_can);
1734 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1737 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1738 skb_free_datagram(sk, skb);
1743 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1746 unsigned int mask = 0;
1748 DBG("socket %p\n", sock);
1750 mask = datagram_poll(file, sock, wait);
1754 static struct proto_ops bcm_ops = {
1756 .release = bcm_release,
1757 .bind = sock_no_bind,
1758 .connect = bcm_connect,
1759 .socketpair = sock_no_socketpair,
1760 .accept = sock_no_accept,
1761 .getname = sock_no_getname,
1763 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1764 .listen = sock_no_listen,
1765 .shutdown = sock_no_shutdown,
1766 .setsockopt = sock_no_setsockopt,
1767 .getsockopt = sock_no_getsockopt,
1768 .sendmsg = bcm_sendmsg,
1769 .recvmsg = bcm_recvmsg,
1770 .mmap = sock_no_mmap,
1771 .sendpage = sock_no_sendpage,
1774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1775 static struct proto bcm_proto = {
1777 .owner = THIS_MODULE,
1778 .obj_size = sizeof(struct bcm_sock),
1782 static struct can_proto bcm_can_proto = {
1784 .protocol = CAN_BCM,
1785 .capability = BCM_CAP,
1790 static struct can_proto bcm_can_proto = {
1792 .protocol = CAN_BCM,
1793 .capability = BCM_CAP,
1795 .owner = THIS_MODULE,
1796 .obj_size = sizeof(struct bcm_sock),
1801 static int __init bcm_module_init(void)
1805 can_proto_register(&bcm_can_proto);
1807 /* create /proc/net/can/bcm directory */
1808 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1811 proc_dir->owner = THIS_MODULE;
1816 static void __exit bcm_module_exit(void)
1818 can_proto_unregister(&bcm_can_proto);
1821 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1824 module_init(bcm_module_init);
1825 module_exit(bcm_module_exit);