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;
116 static struct proc_dir_entry *proc_dir = NULL;
118 #ifdef CONFIG_CAN_BCM_USER
121 #define BCM_CAP CAP_NET_RAW
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
132 struct notifier_block notifier;
133 struct list_head rx_ops;
134 struct list_head tx_ops;
135 unsigned long dropped_usr_msgs;
136 struct proc_dir_entry *bcm_proc_read;
137 char procname [9]; /* pointer printed in ASCII with \0 */
140 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
142 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
143 return (struct bcm_sock *)sk;
145 return (struct bcm_sock *)sk->sk_protinfo;
149 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
150 static void *kzalloc(size_t size, unsigned int __nocast flags)
152 void *ret = kmalloc(size, flags);
155 memset(ret, 0, size);
160 static inline void skb_get_timestamp(const struct sk_buff *skb,
161 struct timeval *stamp)
163 stamp->tv_sec = skb->stamp.tv_sec;
164 stamp->tv_usec = skb->stamp.tv_usec;
167 static inline void skb_set_timestamp(struct sk_buff *skb,
168 const struct timeval *stamp)
170 skb->stamp.tv_sec = stamp->tv_sec;
171 skb->stamp.tv_usec = stamp->tv_usec;
175 #define CFSIZ sizeof(struct can_frame)
176 #define OPSIZ sizeof(struct bcm_op)
177 #define MHSIZ sizeof(struct bcm_msg_head)
180 * rounded_tv2jif - calculate jiffies from timeval including optional up
181 * @tv: pointer to timeval
184 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
185 * function is intentionally more relaxed on precise timer ticks to get exact
186 * one jiffy for requested 1000us on a 1000HZ machine.
187 * This code is to be removed when upgrading to kernel hrtimer.
190 * calculated jiffies (max: ULONG_MAX)
192 static unsigned long rounded_tv2jif(const struct timeval *tv)
194 unsigned long sec = tv->tv_sec;
195 unsigned long usec = tv->tv_usec;
198 if (sec > ULONG_MAX / HZ)
201 /* round up to get at least the requested time */
202 usec += 1000000 / HZ - 1;
204 jif = usec / (1000000 / HZ);
206 if (sec * HZ > ULONG_MAX - jif)
209 return jif + sec * HZ;
215 static char *bcm_proc_getifname(int ifindex)
217 struct net_device *dev;
222 dev = __dev_get_by_index(ifindex); /* no usage counting */
229 static int bcm_read_proc(char *page, char **start, off_t off,
230 int count, int *eof, void *data)
233 struct sock *sk = (struct sock *)data;
234 struct bcm_sock *bo = bcm_sk(sk);
237 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
239 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
240 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
241 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
242 bo->dropped_usr_msgs);
243 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
244 bcm_proc_getifname(bo->ifindex));
245 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
247 list_for_each_entry(op, &bo->rx_ops, list) {
249 unsigned long reduction;
251 /* print only active entries & prevent division by zero */
255 len += snprintf(page + len, PAGE_SIZE - len,
257 op->can_id, bcm_proc_getifname(op->ifindex));
258 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
260 (op->flags & RX_CHECK_DLC)?'d':' ');
262 len += snprintf(page + len, PAGE_SIZE - len,
263 "timeo=%ld ", op->j_ival1);
266 len += snprintf(page + len, PAGE_SIZE - len,
267 "thr=%ld ", op->j_ival2);
269 len += snprintf(page + len, PAGE_SIZE - len,
270 "# recv %ld (%ld) => reduction: ",
271 op->frames_filtered, op->frames_abs);
273 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
275 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
276 (reduction == 100)?"near ":"", reduction);
278 if (len > PAGE_SIZE - 200) {
279 /* mark output cut off */
280 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
285 list_for_each_entry(op, &bo->tx_ops, list) {
287 len += snprintf(page + len, PAGE_SIZE - len,
288 "tx_op: %03X %s [%d] ",
289 op->can_id, bcm_proc_getifname(op->ifindex),
292 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
296 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
299 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
302 if (len > PAGE_SIZE - 100) {
303 /* mark output cut off */
304 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
309 len += snprintf(page + len, PAGE_SIZE - len, "\n");
316 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
317 * of the given bcm tx op
319 static void bcm_can_tx(struct bcm_op *op)
322 struct net_device *dev;
323 struct can_frame *cf = &op->frames[op->currframe];
325 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
327 /* no target device? => exit */
331 dev = dev_get_by_index(op->ifindex);
334 /* RFC: should this bcm_op remove itself here? */
338 skb = alloc_skb(CFSIZ,
339 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
344 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
346 /* send with loopback */
351 /* update statistics */
355 /* reached last frame? */
356 if (op->currframe >= op->nframes)
363 * bcm_send_to_user - send a BCM message to the userspace
364 * (consisting of bcm_msg_head + x CAN frames)
366 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
367 struct can_frame *frames, int has_timestamp)
370 struct can_frame *firstframe;
371 struct sock *sk = op->sk;
372 int datalen = head->nframes * CFSIZ;
375 skb = alloc_skb(sizeof(*head) + datalen,
376 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
380 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
382 /* can_frames starting here */
383 firstframe = (struct can_frame *) skb->tail;
387 /* restore rx timestamp */
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
389 skb->tstamp = op->rx_stamp;
391 skb_set_timestamp(skb, &op->rx_stamp);
395 /* restore originator for recvfrom() */
396 skb->iif = op->rx_ifindex;
399 memcpy(skb_put(skb, datalen), frames, datalen);
402 * the BCM uses the can_dlc-element of the can_frame
403 * structure for internal purposes. This is only
404 * relevant for updates that are generated by the
405 * BCM, where nframes is 1
407 if (head->nframes == 1)
408 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
411 err = sock_queue_rcv_skb(sk, skb);
413 struct bcm_sock *bo = bcm_sk(sk);
415 DBG("sock_queue_rcv_skb failed: %d\n", err);
417 /* don't care about overflows in this statistic */
418 bo->dropped_usr_msgs++;
423 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
425 static void bcm_tx_timeout_handler(unsigned long data)
427 struct bcm_op *op = (struct bcm_op*)data;
429 DBG("Called with bcm_op %p\n", op);
431 if (op->j_ival1 && (op->count > 0)) {
434 if (!op->count && (op->flags & TX_COUNTEVT)) {
435 struct bcm_msg_head msg_head;
437 /* create notification to user */
438 DBG("sending TX_EXPIRED for can_id %03X\n",
441 msg_head.opcode = TX_EXPIRED;
442 msg_head.flags = op->flags;
443 msg_head.count = op->count;
444 msg_head.ival1 = op->ival1;
445 msg_head.ival2 = op->ival2;
446 msg_head.can_id = op->can_id;
447 msg_head.nframes = 0;
449 bcm_send_to_user(op, &msg_head, NULL, 0);
453 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
454 op->count, op->j_ival1, op->j_ival2);
456 if (op->j_ival1 && (op->count > 0)) {
458 op->timer.expires = jiffies + op->j_ival1;
459 add_timer(&op->timer);
461 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
463 (char*) op->timer.data,
464 (unsigned int) op->timer.expires);
466 /* send (next) frame */
471 op->timer.expires = jiffies + op->j_ival2;
472 add_timer(&op->timer);
474 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
476 (char*) op->timer.data,
477 (unsigned int) op->timer.expires);
479 /* send (next) frame */
483 DBG("no timer restart\n");
490 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
492 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
494 struct bcm_msg_head head;
496 op->j_lastmsg = jiffies;
498 /* update statistics */
499 op->frames_filtered++;
501 /* prevent statistics overflow */
502 if (op->frames_filtered > ULONG_MAX/100)
503 op->frames_filtered = op->frames_abs = 0;
505 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
506 (unsigned int) op->j_lastmsg, op);
507 DBG("sending notification\n");
509 head.opcode = RX_CHANGED;
510 head.flags = op->flags;
511 head.count = op->count;
512 head.ival1 = op->ival1;
513 head.ival2 = op->ival2;
514 head.can_id = op->can_id;
517 bcm_send_to_user(op, &head, data, 1);
521 * bcm_rx_update_and_send - process a detected relevant receive content change
522 * 1. update the last received data
523 * 2. send a notification to the user (if possible)
525 static void bcm_rx_update_and_send(struct bcm_op *op,
526 struct can_frame *lastdata,
527 struct can_frame *rxdata)
529 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
531 memcpy(lastdata, rxdata, CFSIZ);
534 lastdata->can_dlc |= RX_RECV;
536 /* throttle bcm_rx_changed ? */
537 if ((op->thrtimer.expires) ||
538 ((op->j_ival2) && (nexttx > jiffies))) {
539 /* we are already waiting OR we have to start waiting */
541 /* mark as 'throttled' */
542 lastdata->can_dlc |= RX_THR;
544 if (!(op->thrtimer.expires)) {
545 /* start the timer only the first time */
546 op->thrtimer.expires = nexttx;
547 add_timer(&op->thrtimer);
549 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
550 op->thrtimer.function,
551 (char*) op->thrtimer.data,
552 (unsigned int) op->thrtimer.expires);
556 /* send RX_CHANGED to the user immediately */
557 bcm_rx_changed(op, rxdata);
562 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
563 * received data stored in op->last_frames[]
565 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
566 struct can_frame *rxdata)
569 * no one uses the MSBs of can_dlc for comparation,
570 * so we use it here to detect the first time of reception
573 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
574 /* received data for the first time => send update to user */
575 DBG("first time :)\n");
576 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
580 /* do a real check in can_frame data section */
582 DBG("op->frames[index].data = 0x%016llx\n",
583 GET_U64(&op->frames[index]));
584 DBG("op->last_frames[index].data = 0x%016llx\n",
585 GET_U64(&op->last_frames[index]));
586 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
588 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
589 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
590 DBG("relevant data change :)\n");
591 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
595 if (op->flags & RX_CHECK_DLC) {
596 /* do a real check in can_frame dlc */
597 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
599 DBG("dlc change :)\n");
600 bcm_rx_update_and_send(op, &op->last_frames[index],
605 DBG("no relevant change :(\n");
609 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
611 static void bcm_rx_starttimer(struct bcm_op *op)
613 if (op->flags & RX_NO_AUTOTIMER)
617 op->timer.expires = jiffies + op->j_ival1;
619 DBG("adding rx timeout timer ival1. func=%p data=%p "
622 (char*) op->timer.data,
623 (unsigned int) op->timer.expires);
625 add_timer(&op->timer);
630 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
632 static void bcm_rx_timeout_handler(unsigned long data)
634 struct bcm_op *op = (struct bcm_op*)data;
635 struct bcm_msg_head msg_head;
637 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
639 msg_head.opcode = RX_TIMEOUT;
640 msg_head.flags = op->flags;
641 msg_head.count = op->count;
642 msg_head.ival1 = op->ival1;
643 msg_head.ival2 = op->ival2;
644 msg_head.can_id = op->can_id;
645 msg_head.nframes = 0;
647 bcm_send_to_user(op, &msg_head, NULL, 0);
649 /* no restart of the timer is done here! */
651 /* if user wants to be informed, when cyclic CAN-Messages come back */
652 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
653 /* clear received can_frames to indicate 'nothing received' */
654 memset(op->last_frames, 0, op->nframes * CFSIZ);
655 DBG("RX_ANNOUNCE_RESTART\n");
660 * bcm_rx_thr_handler - the time for blocked content updates is over now:
661 * Check for throttled data and send it to the userspace
663 static void bcm_rx_thr_handler(unsigned long data)
665 struct bcm_op *op = (struct bcm_op*)data;
668 /* mark disabled / consumed timer */
669 op->thrtimer.expires = 0;
671 if (op->nframes > 1) {
672 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
674 /* for MUX filter we start at index 1 */
675 for (i=1; i<op->nframes; i++) {
676 if ((op->last_frames) &&
677 (op->last_frames[i].can_dlc & RX_THR)) {
678 op->last_frames[i].can_dlc &= ~RX_THR;
679 bcm_rx_changed(op, &op->last_frames[i]);
684 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
686 /* for RX_FILTER_ID and simple filter */
687 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
688 op->last_frames[0].can_dlc &= ~RX_THR;
689 bcm_rx_changed(op, &op->last_frames[0]);
695 * bcm_rx_handler - handle a CAN frame receiption
697 static void bcm_rx_handler(struct sk_buff *skb, void *data)
699 struct bcm_op *op = (struct bcm_op*)data;
700 struct can_frame rxframe;
703 /* disable timeout */
704 del_timer(&op->timer);
706 DBG("Called with bcm_op %p\n", op);
708 if (skb->len == sizeof(rxframe)) {
709 memcpy(&rxframe, skb->data, sizeof(rxframe));
710 /* save rx timestamp */
711 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
712 op->rx_stamp = skb->tstamp;
714 skb_get_timestamp(skb, &op->rx_stamp);
716 /* save originator for recvfrom() */
717 op->rx_ifindex = skb->dev->ifindex;
718 /* update statistics */
721 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
724 DBG("Wrong skb->len = %d\n", skb->len);
729 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
731 if (op->can_id != rxframe.can_id) {
732 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
733 rxframe.can_id, op->can_id);
737 if (op->flags & RX_RTR_FRAME) {
738 /* send reply for RTR-request */
739 DBG("RTR-request\n");
741 /* send op->frames[0] to CAN device */
746 if (op->flags & RX_FILTER_ID) {
747 /* the easiest case */
748 DBG("Easy does it with RX_FILTER_ID\n");
750 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
751 bcm_rx_starttimer(op);
755 if (op->nframes == 1) {
756 /* simple compare with index 0 */
757 DBG("Simple compare\n");
759 bcm_rx_cmp_to_index(op, 0, &rxframe);
760 bcm_rx_starttimer(op);
764 if (op->nframes > 1) {
765 /* multiplex compare */
766 DBG("Multiplex compare\n");
769 * find the first multiplex mask that fits.
770 * Remark: The MUX-mask is stored in index 0
773 for (i=1; i < op->nframes; i++) {
774 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
775 (GET_U64(&op->frames[0]) &
776 GET_U64(&op->frames[i]))) {
777 DBG("found MUX index %d\n", i);
778 bcm_rx_cmp_to_index(op, i, &rxframe);
782 bcm_rx_starttimer(op);
787 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
789 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
794 list_for_each_entry(op, ops, list) {
795 if ((op->can_id == can_id) && (op->ifindex == ifindex))
802 static void bcm_remove_op(struct bcm_op *op)
804 del_timer(&op->timer);
805 del_timer(&op->thrtimer);
807 if ((op->frames) && (op->frames != &op->sframe))
810 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
811 kfree(op->last_frames);
818 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
820 if (op->rx_reg_dev == dev) {
821 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
824 /* mark as removed subscription */
825 op->rx_reg_dev = NULL;
827 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
828 "mismatch %p %p\n", op->rx_reg_dev, dev);
832 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
834 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
836 struct bcm_op *op, *n;
838 list_for_each_entry_safe(op, n, ops, list) {
839 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
840 DBG("removing rx_op %p for can_id %03X\n",
844 * Don't care if we're bound or not (due to netdev
845 * problems) can_rx_unregister() is always a save
850 * Only remove subscriptions that had not
851 * been removed due to NETDEV_UNREGISTER
854 if (op->rx_reg_dev) {
855 struct net_device *dev;
857 dev = dev_get_by_index(op->ifindex);
859 bcm_rx_unreg(dev, op);
864 can_rx_unregister(NULL, op->can_id,
874 return 0; /* not found */
878 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
880 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
882 struct bcm_op *op, *n;
884 list_for_each_entry_safe(op, n, ops, list) {
885 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
886 DBG("removing rx_op %p for can_id %03X\n",
894 return 0; /* not found */
898 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
900 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
903 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
906 DBG("TRX_READ: did not find op for can_id %03X\n",
911 DBG("TRX_READ: sending status for can_id %03X\n",
913 /* put current values into msg_head */
914 msg_head->flags = op->flags;
915 msg_head->count = op->count;
916 msg_head->ival1 = op->ival1;
917 msg_head->ival2 = op->ival2;
918 msg_head->nframes = op->nframes;
920 bcm_send_to_user(op, msg_head, op->frames, 0);
926 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
928 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
929 int ifindex, struct sock *sk)
931 struct bcm_sock *bo = bcm_sk(sk);
935 /* we need a real device to send frames */
939 /* we need at least one can_frame */
940 if (msg_head->nframes < 1)
943 /* check the given can_id */
944 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
947 /* update existing BCM operation */
949 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
950 op, msg_head->can_id);
953 * Do we need more space for the can_frames than currently
954 * allocated? -> This is a _really_ unusual use-case and
955 * therefore (complexity / locking) it is not supported.
957 if (msg_head->nframes > op->nframes)
960 /* update can_frames content */
961 for (i = 0; i < msg_head->nframes; i++) {
962 err = memcpy_fromiovec((u8*)&op->frames[i],
963 msg->msg_iov, CFSIZ);
967 if (msg_head->flags & TX_CP_CAN_ID) {
968 /* copy can_id into frame */
969 op->frames[i].can_id = msg_head->can_id;
974 /* insert new BCM operation for the given can_id */
976 op = kzalloc(OPSIZ, GFP_KERNEL);
980 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
981 op, msg_head->can_id);
983 op->can_id = msg_head->can_id;
985 /* create array for can_frames and copy the data */
986 if (msg_head->nframes > 1) {
987 op->frames = kmalloc(msg_head->nframes * CFSIZ,
994 op->frames = &op->sframe;
996 for (i = 0; i < msg_head->nframes; i++) {
997 err = memcpy_fromiovec((u8*)&op->frames[i],
998 msg->msg_iov, CFSIZ);
1000 if (op->frames != &op->sframe)
1006 if (msg_head->flags & TX_CP_CAN_ID) {
1007 /* copy can_id into frame */
1008 op->frames[i].can_id = msg_head->can_id;
1012 /* tx_ops never compare with previous received messages */
1013 op->last_frames = NULL;
1015 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1018 op->ifindex = ifindex;
1020 /* initialize uninitialized (kmalloc) structure */
1021 init_timer(&op->timer);
1023 /* currently unused in tx_ops */
1024 init_timer(&op->thrtimer);
1026 /* handler for tx_ops */
1027 op->timer.function = bcm_tx_timeout_handler;
1029 /* timer.data points to this op-structure */
1030 op->timer.data = (unsigned long)op;
1032 /* add this bcm_op to the list of the tx_ops */
1033 list_add(&op->list, &bo->tx_ops);
1035 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1037 if (op->nframes != msg_head->nframes) {
1038 op->nframes = msg_head->nframes;
1039 /* start multiple frame transmission with index 0 */
1045 op->flags = msg_head->flags;
1047 if (op->flags & TX_RESET_MULTI_IDX) {
1048 /* start multiple frame transmission with index 0 */
1052 if (op->flags & SETTIMER) {
1053 /* set timer values */
1055 op->count = msg_head->count;
1056 op->ival1 = msg_head->ival1;
1057 op->ival2 = msg_head->ival2;
1058 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1059 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1061 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1062 op->count, op->j_ival1, op->j_ival2);
1064 /* disable an active timer due to zero values? */
1065 if (!op->j_ival1 && !op->j_ival2) {
1066 del_timer(&op->timer);
1067 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1071 if ((op->flags & STARTTIMER) &&
1072 ((op->j_ival1 && op->count) || op->j_ival2)) {
1074 del_timer(&op->timer);
1076 /* spec: send can_frame when starting timer */
1077 op->flags |= TX_ANNOUNCE;
1079 if (op->j_ival1 && (op->count > 0)) {
1080 op->timer.expires = jiffies + op->j_ival1;
1081 /* op->count-- is done in bcm_tx_timeout_handler */
1082 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1085 (char*) op->timer.data,
1086 (unsigned int) op->timer.expires);
1089 op->timer.expires = jiffies + op->j_ival2;
1090 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1093 (char*) op->timer.data,
1094 (unsigned int) op->timer.expires);
1097 add_timer(&op->timer);
1100 if (op->flags & TX_ANNOUNCE)
1103 return msg_head->nframes * CFSIZ + MHSIZ;
1107 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1109 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1110 int ifindex, struct sock *sk)
1112 struct bcm_sock *bo = bcm_sk(sk);
1117 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1118 /* be robust against wrong usage ... */
1119 msg_head->flags |= RX_FILTER_ID;
1120 msg_head->nframes = 0; /* ignore trailing garbage */
1123 if ((msg_head->flags & RX_RTR_FRAME) &&
1124 ((msg_head->nframes != 1) ||
1125 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1127 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1131 /* check the given can_id */
1132 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1134 /* update existing BCM operation */
1136 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1137 op, msg_head->can_id);
1140 * Do we need more space for the can_frames than currently
1141 * allocated? -> This is a _really_ unusual use-case and
1142 * therefore (complexity / locking) it is not supported.
1144 if (msg_head->nframes > op->nframes)
1147 if (msg_head->nframes) {
1148 /* update can_frames content */
1149 err = memcpy_fromiovec((u8*)op->frames,
1151 msg_head->nframes * CFSIZ);
1155 /* clear last_frames to indicate 'nothing received' */
1156 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1159 op->nframes = msg_head->nframes;
1161 /* Only an update -> do not call can_rx_register() */
1165 /* insert new BCM operation for the given can_id */
1167 op = kzalloc(OPSIZ, GFP_KERNEL);
1171 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1172 op, msg_head->can_id);
1174 op->can_id = msg_head->can_id;
1175 op->nframes = msg_head->nframes;
1177 if (msg_head->nframes > 1) {
1178 /* create array for can_frames and copy the data */
1179 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1186 /* create and init array for received can_frames */
1187 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1189 if (!op->last_frames) {
1196 op->frames = &op->sframe;
1197 op->last_frames = &op->last_sframe;
1200 if (msg_head->nframes) {
1201 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1202 msg_head->nframes * CFSIZ);
1204 if (op->frames != &op->sframe)
1206 if (op->last_frames != &op->last_sframe)
1207 kfree(op->last_frames);
1214 op->ifindex = ifindex;
1216 /* initialize uninitialized (kzalloc) structure */
1217 init_timer(&op->timer);
1219 /* init throttle timer for RX_CHANGED */
1220 init_timer(&op->thrtimer);
1222 /* handler for rx timeouts */
1223 op->timer.function = bcm_rx_timeout_handler;
1225 /* timer.data points to this op-structure */
1226 op->timer.data = (unsigned long)op;
1228 /* handler for RX_CHANGED throttle timeouts */
1229 op->thrtimer.function = bcm_rx_thr_handler;
1231 /* timer.data points to this op-structure */
1232 op->thrtimer.data = (unsigned long)op;
1234 /* mark disabled timer */
1235 op->thrtimer.expires = 0;
1237 /* add this bcm_op to the list of the tx_ops */
1238 list_add(&op->list, &bo->rx_ops);
1240 /* call can_rx_register() */
1243 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1246 op->flags = msg_head->flags;
1248 if (op->flags & RX_RTR_FRAME) {
1250 /* no timers in RTR-mode */
1251 del_timer(&op->thrtimer);
1252 del_timer(&op->timer);
1255 * funny feature in RX(!)_SETUP only for RTR-mode:
1256 * copy can_id into frame BUT without RTR-flag to
1257 * prevent a full-load-loopback-test ... ;-]
1259 if ((op->flags & TX_CP_CAN_ID) ||
1260 (op->frames[0].can_id == op->can_id))
1261 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1264 if (op->flags & SETTIMER) {
1266 /* set timer value */
1267 op->ival1 = msg_head->ival1;
1268 op->ival2 = msg_head->ival2;
1269 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1270 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1272 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1273 op->j_ival1, op->j_ival2);
1275 /* disable an active timer due to zero value? */
1277 del_timer(&op->timer);
1278 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1281 /* free currently blocked msgs ? */
1282 if (op->thrtimer.expires) {
1283 DBG("RX_SETUP: unblocking throttled msgs.\n");
1284 del_timer(&op->thrtimer);
1285 /* send blocked msgs hereafter */
1286 op->thrtimer.expires = jiffies + 2;
1287 add_timer(&op->thrtimer);
1290 * if (op->j_ival2) is zero, no (new) throttling
1291 * will happen. For details see functions
1292 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1296 if ((op->flags & STARTTIMER) && op->j_ival1) {
1298 del_timer(&op->timer);
1299 op->timer.expires = jiffies + op->j_ival1;
1301 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1303 (char *) op->timer.function,
1304 (char *) op->timer.data,
1305 (unsigned int) op->timer.expires);
1307 add_timer(&op->timer);
1311 /* now we can register for can_ids, if we added a new bcm_op */
1312 if (do_rx_register) {
1313 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1314 "rx_op is %p\n", op->can_id, op);
1317 struct net_device *dev = dev_get_by_index(ifindex);
1320 can_rx_register(dev, op->can_id,
1321 REGMASK(op->can_id),
1322 bcm_rx_handler, op, IDENT);
1323 op->rx_reg_dev = dev;
1328 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1329 bcm_rx_handler, op, IDENT);
1332 return msg_head->nframes * CFSIZ + MHSIZ;
1336 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1338 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1340 struct sk_buff *skb;
1341 struct net_device *dev;
1344 /* just copy and send one can_frame */
1346 if (!ifindex) /* we need a real device to send frames */
1349 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1354 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1360 DBG_FRAME("BCM: TX_SEND: sending frame",
1361 (struct can_frame *)skb->data);
1363 dev = dev_get_by_index(ifindex);
1371 can_send(skb, 1); /* send with loopback */
1374 return CFSIZ + MHSIZ;
1378 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1380 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1381 struct msghdr *msg, size_t size)
1383 struct sock *sk = sock->sk;
1384 struct bcm_sock *bo = bcm_sk(sk);
1385 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1386 struct bcm_msg_head msg_head;
1387 int ret; /* read bytes or error codes as return value */
1390 DBG("sock %p not bound\n", sk);
1394 /* check for alternative ifindex for this bcm_op */
1396 if (!ifindex && msg->msg_name) {
1397 /* no bound device as default => check msg_name */
1398 struct sockaddr_can *addr =
1399 (struct sockaddr_can *)msg->msg_name;
1401 if (addr->can_family != AF_CAN)
1404 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1407 struct net_device *dev = dev_get_by_index(ifindex);
1410 DBG("device %d not found\n", ifindex);
1414 if (dev->type != ARPHRD_CAN) {
1415 DBG("device %d no CAN device\n", ifindex);
1424 /* read message head information */
1426 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1430 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1434 switch (msg_head.opcode) {
1437 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1441 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1445 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1452 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1459 /* reuse msg_head for the reply to TX_READ */
1460 msg_head.opcode = TX_STATUS;
1461 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1465 /* reuse msg_head for the reply to RX_READ */
1466 msg_head.opcode = RX_STATUS;
1467 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1471 /* we need at least one can_frame */
1472 if (msg_head.nframes < 1)
1475 ret = bcm_tx_send(msg, ifindex, sk);
1479 DBG("Unknown opcode %d\n", msg_head.opcode);
1490 * notification handler for netdevice status changes
1492 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1495 struct net_device *dev = (struct net_device *)data;
1496 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1497 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1498 struct sock *sk = &bo->sk;
1500 struct sock *sk = bo->sk;
1503 int notify_enodev = 0;
1505 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1506 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1508 if (dev->type != ARPHRD_CAN)
1513 case NETDEV_UNREGISTER:
1516 /* remove device specific receive entries */
1517 list_for_each_entry(op, &bo->rx_ops, list)
1518 if (op->rx_reg_dev == dev)
1519 bcm_rx_unreg(dev, op);
1521 /* remove device reference, if this is our bound device */
1522 if (bo->bound && bo->ifindex == dev->ifindex) {
1530 if (notify_enodev) {
1531 sk->sk_err = ENODEV;
1532 if (!sock_flag(sk, SOCK_DEAD))
1533 sk->sk_error_report(sk);
1538 if (bo->bound && bo->ifindex == dev->ifindex) {
1539 sk->sk_err = ENETDOWN;
1540 if (!sock_flag(sk, SOCK_DEAD))
1541 sk->sk_error_report(sk);
1549 * initial settings for all BCM sockets to be set at socket creation time
1551 static int bcm_init(struct sock *sk)
1553 struct bcm_sock *bo = bcm_sk(sk);
1555 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1560 bo->dropped_usr_msgs = 0;
1561 bo->bcm_proc_read = NULL;
1563 INIT_LIST_HEAD(&bo->tx_ops);
1564 INIT_LIST_HEAD(&bo->rx_ops);
1567 bo->notifier.notifier_call = bcm_notifier;
1569 register_netdevice_notifier(&bo->notifier);
1575 * standard socket functions
1577 static int bcm_release(struct socket *sock)
1579 struct sock *sk = sock->sk;
1580 struct bcm_sock *bo = bcm_sk(sk);
1581 struct bcm_op *op, *next;
1583 DBG("socket %p, sk %p\n", sock, sk);
1585 /* remove bcm_ops, timer, rx_unregister(), etc. */
1587 unregister_netdevice_notifier(&bo->notifier);
1591 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1592 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1596 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1597 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1600 * Don't care if we're bound or not (due to netdev problems)
1601 * can_rx_unregister() is always a save thing to do here.
1605 * Only remove subscriptions that had not
1606 * been removed due to NETDEV_UNREGISTER
1609 if (op->rx_reg_dev) {
1610 struct net_device *dev;
1612 dev = dev_get_by_index(op->ifindex);
1614 bcm_rx_unreg(dev, op);
1619 can_rx_unregister(NULL, op->can_id,
1620 REGMASK(op->can_id),
1621 bcm_rx_handler, op);
1626 /* remove procfs entry */
1627 if (proc_dir && bo->bcm_proc_read)
1628 remove_proc_entry(bo->procname, proc_dir);
1630 /* remove device reference */
1642 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1645 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1646 struct sock *sk = sock->sk;
1647 struct bcm_sock *bo = bcm_sk(sk);
1652 /* bind a device to this socket */
1653 if (addr->can_ifindex) {
1654 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1657 DBG("could not find device index %d\n",
1662 if (dev->type != ARPHRD_CAN) {
1663 DBG("device %d no CAN device\n", addr->can_ifindex);
1668 bo->ifindex = dev->ifindex;
1671 DBG("socket %p bound to device %s (idx %d)\n",
1672 sock, dev->name, dev->ifindex);
1675 /* no interface reference for ifindex = 0 ('any' CAN device) */
1682 /* unique socket address as filename */
1683 sprintf(bo->procname, "%p", sock);
1684 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1692 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1693 struct msghdr *msg, size_t size, int flags)
1695 struct sock *sk = sock->sk;
1696 struct sk_buff *skb;
1701 DBG("socket %p, sk %p\n", sock, sk);
1703 noblock = flags & MSG_DONTWAIT;
1704 flags &= ~MSG_DONTWAIT;
1705 skb = skb_recv_datagram(sk, flags, noblock, &error);
1709 DBG("delivering skbuff %p\n", skb);
1712 if (skb->len < size)
1715 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1717 skb_free_datagram(sk, skb);
1721 sock_recv_timestamp(msg, sk, skb);
1723 if (msg->msg_name) {
1724 struct sockaddr_can *addr = msg->msg_name;
1725 msg->msg_namelen = sizeof(*addr);
1726 memset(addr, 0, sizeof(*addr));
1727 addr->can_family = AF_CAN;
1728 addr->can_ifindex = skb->iif;
1731 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1732 skb_free_datagram(sk, skb);
1737 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1740 unsigned int mask = 0;
1742 DBG("socket %p\n", sock);
1744 mask = datagram_poll(file, sock, wait);
1748 static struct proto_ops bcm_ops = {
1750 .release = bcm_release,
1751 .bind = sock_no_bind,
1752 .connect = bcm_connect,
1753 .socketpair = sock_no_socketpair,
1754 .accept = sock_no_accept,
1755 .getname = sock_no_getname,
1757 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1758 .listen = sock_no_listen,
1759 .shutdown = sock_no_shutdown,
1760 .setsockopt = sock_no_setsockopt,
1761 .getsockopt = sock_no_getsockopt,
1762 .sendmsg = bcm_sendmsg,
1763 .recvmsg = bcm_recvmsg,
1764 .mmap = sock_no_mmap,
1765 .sendpage = sock_no_sendpage,
1768 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1769 static struct proto bcm_proto = {
1771 .owner = THIS_MODULE,
1772 .obj_size = sizeof(struct bcm_sock),
1776 static struct can_proto bcm_can_proto = {
1778 .protocol = CAN_BCM,
1779 .capability = BCM_CAP,
1784 static struct can_proto bcm_can_proto = {
1786 .protocol = CAN_BCM,
1787 .capability = BCM_CAP,
1789 .owner = THIS_MODULE,
1790 .obj_size = sizeof(struct bcm_sock),
1795 static int __init bcm_module_init(void)
1799 can_proto_register(&bcm_can_proto);
1801 /* create /proc/net/can/bcm directory */
1802 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1805 proc_dir->owner = THIS_MODULE;
1810 static void __exit bcm_module_exit(void)
1812 can_proto_unregister(&bcm_can_proto);
1815 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1818 module_init(bcm_module_init);
1819 module_exit(bcm_module_exit);