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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
397 skb->iif = op->rx_ifindex;
399 skb->input_dev = dev_get_by_index(op->rx_ifindex);
401 dev_put(skb->input_dev);
405 memcpy(skb_put(skb, datalen), frames, datalen);
408 * the BCM uses the can_dlc-element of the can_frame
409 * structure for internal purposes. This is only
410 * relevant for updates that are generated by the
411 * BCM, where nframes is 1
413 if (head->nframes == 1)
414 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
417 err = sock_queue_rcv_skb(sk, skb);
419 struct bcm_sock *bo = bcm_sk(sk);
421 DBG("sock_queue_rcv_skb failed: %d\n", err);
423 /* don't care about overflows in this statistic */
424 bo->dropped_usr_msgs++;
429 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
431 static void bcm_tx_timeout_handler(unsigned long data)
433 struct bcm_op *op = (struct bcm_op*)data;
435 DBG("Called with bcm_op %p\n", op);
437 if (op->j_ival1 && (op->count > 0)) {
440 if (!op->count && (op->flags & TX_COUNTEVT)) {
441 struct bcm_msg_head msg_head;
443 /* create notification to user */
444 DBG("sending TX_EXPIRED for can_id %03X\n",
447 msg_head.opcode = TX_EXPIRED;
448 msg_head.flags = op->flags;
449 msg_head.count = op->count;
450 msg_head.ival1 = op->ival1;
451 msg_head.ival2 = op->ival2;
452 msg_head.can_id = op->can_id;
453 msg_head.nframes = 0;
455 bcm_send_to_user(op, &msg_head, NULL, 0);
459 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
460 op->count, op->j_ival1, op->j_ival2);
462 if (op->j_ival1 && (op->count > 0)) {
464 op->timer.expires = jiffies + op->j_ival1;
465 add_timer(&op->timer);
467 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
469 (char*) op->timer.data,
470 (unsigned int) op->timer.expires);
472 /* send (next) frame */
477 op->timer.expires = jiffies + op->j_ival2;
478 add_timer(&op->timer);
480 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
482 (char*) op->timer.data,
483 (unsigned int) op->timer.expires);
485 /* send (next) frame */
489 DBG("no timer restart\n");
496 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
498 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
500 struct bcm_msg_head head;
502 op->j_lastmsg = jiffies;
504 /* update statistics */
505 op->frames_filtered++;
507 /* prevent statistics overflow */
508 if (op->frames_filtered > ULONG_MAX/100)
509 op->frames_filtered = op->frames_abs = 0;
511 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
512 (unsigned int) op->j_lastmsg, op);
513 DBG("sending notification\n");
515 head.opcode = RX_CHANGED;
516 head.flags = op->flags;
517 head.count = op->count;
518 head.ival1 = op->ival1;
519 head.ival2 = op->ival2;
520 head.can_id = op->can_id;
523 bcm_send_to_user(op, &head, data, 1);
527 * bcm_rx_update_and_send - process a detected relevant receive content change
528 * 1. update the last received data
529 * 2. send a notification to the user (if possible)
531 static void bcm_rx_update_and_send(struct bcm_op *op,
532 struct can_frame *lastdata,
533 struct can_frame *rxdata)
535 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
537 memcpy(lastdata, rxdata, CFSIZ);
540 lastdata->can_dlc |= RX_RECV;
542 /* throttle bcm_rx_changed ? */
543 if ((op->thrtimer.expires) ||
544 ((op->j_ival2) && (nexttx > jiffies))) {
545 /* we are already waiting OR we have to start waiting */
547 /* mark as 'throttled' */
548 lastdata->can_dlc |= RX_THR;
550 if (!(op->thrtimer.expires)) {
551 /* start the timer only the first time */
552 op->thrtimer.expires = nexttx;
553 add_timer(&op->thrtimer);
555 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
556 op->thrtimer.function,
557 (char*) op->thrtimer.data,
558 (unsigned int) op->thrtimer.expires);
562 /* send RX_CHANGED to the user immediately */
563 bcm_rx_changed(op, rxdata);
568 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
569 * received data stored in op->last_frames[]
571 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
572 struct can_frame *rxdata)
575 * no one uses the MSBs of can_dlc for comparation,
576 * so we use it here to detect the first time of reception
579 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
580 /* received data for the first time => send update to user */
581 DBG("first time :)\n");
582 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
586 /* do a real check in can_frame data section */
588 DBG("op->frames[index].data = 0x%016llx\n",
589 GET_U64(&op->frames[index]));
590 DBG("op->last_frames[index].data = 0x%016llx\n",
591 GET_U64(&op->last_frames[index]));
592 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
594 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
595 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
596 DBG("relevant data change :)\n");
597 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
601 if (op->flags & RX_CHECK_DLC) {
602 /* do a real check in can_frame dlc */
603 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
605 DBG("dlc change :)\n");
606 bcm_rx_update_and_send(op, &op->last_frames[index],
611 DBG("no relevant change :(\n");
615 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
617 static void bcm_rx_starttimer(struct bcm_op *op)
619 if (op->flags & RX_NO_AUTOTIMER)
623 op->timer.expires = jiffies + op->j_ival1;
625 DBG("adding rx timeout timer ival1. func=%p data=%p "
628 (char*) op->timer.data,
629 (unsigned int) op->timer.expires);
631 add_timer(&op->timer);
636 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
638 static void bcm_rx_timeout_handler(unsigned long data)
640 struct bcm_op *op = (struct bcm_op*)data;
641 struct bcm_msg_head msg_head;
643 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
645 msg_head.opcode = RX_TIMEOUT;
646 msg_head.flags = op->flags;
647 msg_head.count = op->count;
648 msg_head.ival1 = op->ival1;
649 msg_head.ival2 = op->ival2;
650 msg_head.can_id = op->can_id;
651 msg_head.nframes = 0;
653 bcm_send_to_user(op, &msg_head, NULL, 0);
655 /* no restart of the timer is done here! */
657 /* if user wants to be informed, when cyclic CAN-Messages come back */
658 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
659 /* clear received can_frames to indicate 'nothing received' */
660 memset(op->last_frames, 0, op->nframes * CFSIZ);
661 DBG("RX_ANNOUNCE_RESTART\n");
666 * bcm_rx_thr_handler - the time for blocked content updates is over now:
667 * Check for throttled data and send it to the userspace
669 static void bcm_rx_thr_handler(unsigned long data)
671 struct bcm_op *op = (struct bcm_op*)data;
674 /* mark disabled / consumed timer */
675 op->thrtimer.expires = 0;
677 if (op->nframes > 1) {
678 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
680 /* for MUX filter we start at index 1 */
681 for (i=1; i<op->nframes; i++) {
682 if ((op->last_frames) &&
683 (op->last_frames[i].can_dlc & RX_THR)) {
684 op->last_frames[i].can_dlc &= ~RX_THR;
685 bcm_rx_changed(op, &op->last_frames[i]);
690 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
692 /* for RX_FILTER_ID and simple filter */
693 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
694 op->last_frames[0].can_dlc &= ~RX_THR;
695 bcm_rx_changed(op, &op->last_frames[0]);
701 * bcm_rx_handler - handle a CAN frame receiption
703 static void bcm_rx_handler(struct sk_buff *skb, void *data)
705 struct bcm_op *op = (struct bcm_op*)data;
706 struct can_frame rxframe;
709 /* disable timeout */
710 del_timer(&op->timer);
712 DBG("Called with bcm_op %p\n", op);
714 if (skb->len == sizeof(rxframe)) {
715 memcpy(&rxframe, skb->data, sizeof(rxframe));
716 /* save rx timestamp */
717 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
718 op->rx_stamp = skb->tstamp;
720 skb_get_timestamp(skb, &op->rx_stamp);
722 /* save originator for recvfrom() */
723 op->rx_ifindex = skb->dev->ifindex;
724 /* update statistics */
727 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
730 DBG("Wrong skb->len = %d\n", skb->len);
735 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
737 if (op->can_id != rxframe.can_id) {
738 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
739 rxframe.can_id, op->can_id);
743 if (op->flags & RX_RTR_FRAME) {
744 /* send reply for RTR-request */
745 DBG("RTR-request\n");
747 /* send op->frames[0] to CAN device */
752 if (op->flags & RX_FILTER_ID) {
753 /* the easiest case */
754 DBG("Easy does it with RX_FILTER_ID\n");
756 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
757 bcm_rx_starttimer(op);
761 if (op->nframes == 1) {
762 /* simple compare with index 0 */
763 DBG("Simple compare\n");
765 bcm_rx_cmp_to_index(op, 0, &rxframe);
766 bcm_rx_starttimer(op);
770 if (op->nframes > 1) {
771 /* multiplex compare */
772 DBG("Multiplex compare\n");
775 * find the first multiplex mask that fits.
776 * Remark: The MUX-mask is stored in index 0
779 for (i=1; i < op->nframes; i++) {
780 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
781 (GET_U64(&op->frames[0]) &
782 GET_U64(&op->frames[i]))) {
783 DBG("found MUX index %d\n", i);
784 bcm_rx_cmp_to_index(op, i, &rxframe);
788 bcm_rx_starttimer(op);
793 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
795 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
800 list_for_each_entry(op, ops, list) {
801 if ((op->can_id == can_id) && (op->ifindex == ifindex))
808 static void bcm_remove_op(struct bcm_op *op)
810 del_timer(&op->timer);
811 del_timer(&op->thrtimer);
813 if ((op->frames) && (op->frames != &op->sframe))
816 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
817 kfree(op->last_frames);
824 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
826 if (op->rx_reg_dev == dev) {
827 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
830 /* mark as removed subscription */
831 op->rx_reg_dev = NULL;
833 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
834 "mismatch %p %p\n", op->rx_reg_dev, dev);
838 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
840 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
842 struct bcm_op *op, *n;
844 list_for_each_entry_safe(op, n, ops, list) {
845 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
846 DBG("removing rx_op %p for can_id %03X\n",
850 * Don't care if we're bound or not (due to netdev
851 * problems) can_rx_unregister() is always a save
856 * Only remove subscriptions that had not
857 * been removed due to NETDEV_UNREGISTER
860 if (op->rx_reg_dev) {
861 struct net_device *dev;
863 dev = dev_get_by_index(op->ifindex);
865 bcm_rx_unreg(dev, op);
870 can_rx_unregister(NULL, op->can_id,
880 return 0; /* not found */
884 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
886 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
888 struct bcm_op *op, *n;
890 list_for_each_entry_safe(op, n, ops, list) {
891 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
892 DBG("removing rx_op %p for can_id %03X\n",
900 return 0; /* not found */
904 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
906 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
909 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
912 DBG("TRX_READ: did not find op for can_id %03X\n",
917 DBG("TRX_READ: sending status for can_id %03X\n",
919 /* put current values into msg_head */
920 msg_head->flags = op->flags;
921 msg_head->count = op->count;
922 msg_head->ival1 = op->ival1;
923 msg_head->ival2 = op->ival2;
924 msg_head->nframes = op->nframes;
926 bcm_send_to_user(op, msg_head, op->frames, 0);
932 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
934 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
935 int ifindex, struct sock *sk)
937 struct bcm_sock *bo = bcm_sk(sk);
941 /* we need a real device to send frames */
945 /* we need at least one can_frame */
946 if (msg_head->nframes < 1)
949 /* check the given can_id */
950 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
953 /* update existing BCM operation */
955 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
956 op, msg_head->can_id);
959 * Do we need more space for the can_frames than currently
960 * allocated? -> This is a _really_ unusual use-case and
961 * therefore (complexity / locking) it is not supported.
963 if (msg_head->nframes > op->nframes)
966 /* update can_frames content */
967 for (i = 0; i < msg_head->nframes; i++) {
968 err = memcpy_fromiovec((u8*)&op->frames[i],
969 msg->msg_iov, CFSIZ);
973 if (msg_head->flags & TX_CP_CAN_ID) {
974 /* copy can_id into frame */
975 op->frames[i].can_id = msg_head->can_id;
980 /* insert new BCM operation for the given can_id */
982 op = kzalloc(OPSIZ, GFP_KERNEL);
986 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
987 op, msg_head->can_id);
989 op->can_id = msg_head->can_id;
991 /* create array for can_frames and copy the data */
992 if (msg_head->nframes > 1) {
993 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1000 op->frames = &op->sframe;
1002 for (i = 0; i < msg_head->nframes; i++) {
1003 err = memcpy_fromiovec((u8*)&op->frames[i],
1004 msg->msg_iov, CFSIZ);
1006 if (op->frames != &op->sframe)
1012 if (msg_head->flags & TX_CP_CAN_ID) {
1013 /* copy can_id into frame */
1014 op->frames[i].can_id = msg_head->can_id;
1018 /* tx_ops never compare with previous received messages */
1019 op->last_frames = NULL;
1021 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1024 op->ifindex = ifindex;
1026 /* initialize uninitialized (kmalloc) structure */
1027 init_timer(&op->timer);
1029 /* currently unused in tx_ops */
1030 init_timer(&op->thrtimer);
1032 /* handler for tx_ops */
1033 op->timer.function = bcm_tx_timeout_handler;
1035 /* timer.data points to this op-structure */
1036 op->timer.data = (unsigned long)op;
1038 /* add this bcm_op to the list of the tx_ops */
1039 list_add(&op->list, &bo->tx_ops);
1041 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1043 if (op->nframes != msg_head->nframes) {
1044 op->nframes = msg_head->nframes;
1045 /* start multiple frame transmission with index 0 */
1051 op->flags = msg_head->flags;
1053 if (op->flags & TX_RESET_MULTI_IDX) {
1054 /* start multiple frame transmission with index 0 */
1058 if (op->flags & SETTIMER) {
1059 /* set timer values */
1061 op->count = msg_head->count;
1062 op->ival1 = msg_head->ival1;
1063 op->ival2 = msg_head->ival2;
1064 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1065 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1067 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1068 op->count, op->j_ival1, op->j_ival2);
1070 /* disable an active timer due to zero values? */
1071 if (!op->j_ival1 && !op->j_ival2) {
1072 del_timer(&op->timer);
1073 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1077 if ((op->flags & STARTTIMER) &&
1078 ((op->j_ival1 && op->count) || op->j_ival2)) {
1080 del_timer(&op->timer);
1082 /* spec: send can_frame when starting timer */
1083 op->flags |= TX_ANNOUNCE;
1085 if (op->j_ival1 && (op->count > 0)) {
1086 op->timer.expires = jiffies + op->j_ival1;
1087 /* op->count-- is done in bcm_tx_timeout_handler */
1088 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1091 (char*) op->timer.data,
1092 (unsigned int) op->timer.expires);
1095 op->timer.expires = jiffies + op->j_ival2;
1096 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1099 (char*) op->timer.data,
1100 (unsigned int) op->timer.expires);
1103 add_timer(&op->timer);
1106 if (op->flags & TX_ANNOUNCE)
1109 return msg_head->nframes * CFSIZ + MHSIZ;
1113 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1115 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1116 int ifindex, struct sock *sk)
1118 struct bcm_sock *bo = bcm_sk(sk);
1123 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1124 /* be robust against wrong usage ... */
1125 msg_head->flags |= RX_FILTER_ID;
1126 msg_head->nframes = 0; /* ignore trailing garbage */
1129 if ((msg_head->flags & RX_RTR_FRAME) &&
1130 ((msg_head->nframes != 1) ||
1131 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1133 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1137 /* check the given can_id */
1138 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1140 /* update existing BCM operation */
1142 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1143 op, msg_head->can_id);
1146 * Do we need more space for the can_frames than currently
1147 * allocated? -> This is a _really_ unusual use-case and
1148 * therefore (complexity / locking) it is not supported.
1150 if (msg_head->nframes > op->nframes)
1153 if (msg_head->nframes) {
1154 /* update can_frames content */
1155 err = memcpy_fromiovec((u8*)op->frames,
1157 msg_head->nframes * CFSIZ);
1161 /* clear last_frames to indicate 'nothing received' */
1162 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1165 op->nframes = msg_head->nframes;
1167 /* Only an update -> do not call can_rx_register() */
1171 /* insert new BCM operation for the given can_id */
1173 op = kzalloc(OPSIZ, GFP_KERNEL);
1177 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1178 op, msg_head->can_id);
1180 op->can_id = msg_head->can_id;
1181 op->nframes = msg_head->nframes;
1183 if (msg_head->nframes > 1) {
1184 /* create array for can_frames and copy the data */
1185 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1192 /* create and init array for received can_frames */
1193 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1195 if (!op->last_frames) {
1202 op->frames = &op->sframe;
1203 op->last_frames = &op->last_sframe;
1206 if (msg_head->nframes) {
1207 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1208 msg_head->nframes * CFSIZ);
1210 if (op->frames != &op->sframe)
1212 if (op->last_frames != &op->last_sframe)
1213 kfree(op->last_frames);
1220 op->ifindex = ifindex;
1222 /* initialize uninitialized (kzalloc) structure */
1223 init_timer(&op->timer);
1225 /* init throttle timer for RX_CHANGED */
1226 init_timer(&op->thrtimer);
1228 /* handler for rx timeouts */
1229 op->timer.function = bcm_rx_timeout_handler;
1231 /* timer.data points to this op-structure */
1232 op->timer.data = (unsigned long)op;
1234 /* handler for RX_CHANGED throttle timeouts */
1235 op->thrtimer.function = bcm_rx_thr_handler;
1237 /* timer.data points to this op-structure */
1238 op->thrtimer.data = (unsigned long)op;
1240 /* mark disabled timer */
1241 op->thrtimer.expires = 0;
1243 /* add this bcm_op to the list of the tx_ops */
1244 list_add(&op->list, &bo->rx_ops);
1246 /* call can_rx_register() */
1249 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1252 op->flags = msg_head->flags;
1254 if (op->flags & RX_RTR_FRAME) {
1256 /* no timers in RTR-mode */
1257 del_timer(&op->thrtimer);
1258 del_timer(&op->timer);
1261 * funny feature in RX(!)_SETUP only for RTR-mode:
1262 * copy can_id into frame BUT without RTR-flag to
1263 * prevent a full-load-loopback-test ... ;-]
1265 if ((op->flags & TX_CP_CAN_ID) ||
1266 (op->frames[0].can_id == op->can_id))
1267 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1270 if (op->flags & SETTIMER) {
1272 /* set timer value */
1273 op->ival1 = msg_head->ival1;
1274 op->ival2 = msg_head->ival2;
1275 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1276 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1278 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1279 op->j_ival1, op->j_ival2);
1281 /* disable an active timer due to zero value? */
1283 del_timer(&op->timer);
1284 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1287 /* free currently blocked msgs ? */
1288 if (op->thrtimer.expires) {
1289 DBG("RX_SETUP: unblocking throttled msgs.\n");
1290 del_timer(&op->thrtimer);
1291 /* send blocked msgs hereafter */
1292 op->thrtimer.expires = jiffies + 2;
1293 add_timer(&op->thrtimer);
1296 * if (op->j_ival2) is zero, no (new) throttling
1297 * will happen. For details see functions
1298 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1302 if ((op->flags & STARTTIMER) && op->j_ival1) {
1304 del_timer(&op->timer);
1305 op->timer.expires = jiffies + op->j_ival1;
1307 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1309 (char *) op->timer.function,
1310 (char *) op->timer.data,
1311 (unsigned int) op->timer.expires);
1313 add_timer(&op->timer);
1317 /* now we can register for can_ids, if we added a new bcm_op */
1318 if (do_rx_register) {
1319 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1320 "rx_op is %p\n", op->can_id, op);
1323 struct net_device *dev = dev_get_by_index(ifindex);
1326 can_rx_register(dev, op->can_id,
1327 REGMASK(op->can_id),
1328 bcm_rx_handler, op, IDENT);
1329 op->rx_reg_dev = dev;
1334 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1335 bcm_rx_handler, op, IDENT);
1338 return msg_head->nframes * CFSIZ + MHSIZ;
1342 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1344 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1346 struct sk_buff *skb;
1347 struct net_device *dev;
1350 /* just copy and send one can_frame */
1352 if (!ifindex) /* we need a real device to send frames */
1355 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1360 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1366 DBG_FRAME("BCM: TX_SEND: sending frame",
1367 (struct can_frame *)skb->data);
1369 dev = dev_get_by_index(ifindex);
1377 can_send(skb, 1); /* send with loopback */
1380 return CFSIZ + MHSIZ;
1384 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1386 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1387 struct msghdr *msg, size_t size)
1389 struct sock *sk = sock->sk;
1390 struct bcm_sock *bo = bcm_sk(sk);
1391 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1392 struct bcm_msg_head msg_head;
1393 int ret; /* read bytes or error codes as return value */
1396 DBG("sock %p not bound\n", sk);
1400 /* check for alternative ifindex for this bcm_op */
1402 if (!ifindex && msg->msg_name) {
1403 /* no bound device as default => check msg_name */
1404 struct sockaddr_can *addr =
1405 (struct sockaddr_can *)msg->msg_name;
1407 if (addr->can_family != AF_CAN)
1410 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1413 struct net_device *dev = dev_get_by_index(ifindex);
1416 DBG("device %d not found\n", ifindex);
1420 if (dev->type != ARPHRD_CAN) {
1421 DBG("device %d no CAN device\n", ifindex);
1430 /* read message head information */
1432 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1436 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1440 switch (msg_head.opcode) {
1443 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1447 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1451 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1458 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1465 /* reuse msg_head for the reply to TX_READ */
1466 msg_head.opcode = TX_STATUS;
1467 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1471 /* reuse msg_head for the reply to RX_READ */
1472 msg_head.opcode = RX_STATUS;
1473 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1477 /* we need at least one can_frame */
1478 if (msg_head.nframes < 1)
1481 ret = bcm_tx_send(msg, ifindex, sk);
1485 DBG("Unknown opcode %d\n", msg_head.opcode);
1496 * notification handler for netdevice status changes
1498 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1501 struct net_device *dev = (struct net_device *)data;
1502 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1503 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1504 struct sock *sk = &bo->sk;
1506 struct sock *sk = bo->sk;
1509 int notify_enodev = 0;
1511 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1512 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1514 if (dev->type != ARPHRD_CAN)
1519 case NETDEV_UNREGISTER:
1522 /* remove device specific receive entries */
1523 list_for_each_entry(op, &bo->rx_ops, list)
1524 if (op->rx_reg_dev == dev)
1525 bcm_rx_unreg(dev, op);
1527 /* remove device reference, if this is our bound device */
1528 if (bo->bound && bo->ifindex == dev->ifindex) {
1536 if (notify_enodev) {
1537 sk->sk_err = ENODEV;
1538 if (!sock_flag(sk, SOCK_DEAD))
1539 sk->sk_error_report(sk);
1544 if (bo->bound && bo->ifindex == dev->ifindex) {
1545 sk->sk_err = ENETDOWN;
1546 if (!sock_flag(sk, SOCK_DEAD))
1547 sk->sk_error_report(sk);
1555 * initial settings for all BCM sockets to be set at socket creation time
1557 static int bcm_init(struct sock *sk)
1559 struct bcm_sock *bo = bcm_sk(sk);
1561 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1566 bo->dropped_usr_msgs = 0;
1567 bo->bcm_proc_read = NULL;
1569 INIT_LIST_HEAD(&bo->tx_ops);
1570 INIT_LIST_HEAD(&bo->rx_ops);
1573 bo->notifier.notifier_call = bcm_notifier;
1575 register_netdevice_notifier(&bo->notifier);
1581 * standard socket functions
1583 static int bcm_release(struct socket *sock)
1585 struct sock *sk = sock->sk;
1586 struct bcm_sock *bo = bcm_sk(sk);
1587 struct bcm_op *op, *next;
1589 DBG("socket %p, sk %p\n", sock, sk);
1591 /* remove bcm_ops, timer, rx_unregister(), etc. */
1593 unregister_netdevice_notifier(&bo->notifier);
1597 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1598 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1602 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1603 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1606 * Don't care if we're bound or not (due to netdev problems)
1607 * can_rx_unregister() is always a save thing to do here.
1611 * Only remove subscriptions that had not
1612 * been removed due to NETDEV_UNREGISTER
1615 if (op->rx_reg_dev) {
1616 struct net_device *dev;
1618 dev = dev_get_by_index(op->ifindex);
1620 bcm_rx_unreg(dev, op);
1625 can_rx_unregister(NULL, op->can_id,
1626 REGMASK(op->can_id),
1627 bcm_rx_handler, op);
1632 /* remove procfs entry */
1633 if (proc_dir && bo->bcm_proc_read)
1634 remove_proc_entry(bo->procname, proc_dir);
1636 /* remove device reference */
1648 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1651 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1652 struct sock *sk = sock->sk;
1653 struct bcm_sock *bo = bcm_sk(sk);
1658 /* bind a device to this socket */
1659 if (addr->can_ifindex) {
1660 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1663 DBG("could not find device index %d\n",
1668 if (dev->type != ARPHRD_CAN) {
1669 DBG("device %d no CAN device\n", addr->can_ifindex);
1674 bo->ifindex = dev->ifindex;
1677 DBG("socket %p bound to device %s (idx %d)\n",
1678 sock, dev->name, dev->ifindex);
1681 /* no interface reference for ifindex = 0 ('any' CAN device) */
1688 /* unique socket address as filename */
1689 sprintf(bo->procname, "%p", sock);
1690 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1698 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1699 struct msghdr *msg, size_t size, int flags)
1701 struct sock *sk = sock->sk;
1702 struct sk_buff *skb;
1707 DBG("socket %p, sk %p\n", sock, sk);
1709 noblock = flags & MSG_DONTWAIT;
1710 flags &= ~MSG_DONTWAIT;
1711 skb = skb_recv_datagram(sk, flags, noblock, &error);
1715 DBG("delivering skbuff %p\n", skb);
1718 if (skb->len < size)
1721 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1723 skb_free_datagram(sk, skb);
1727 sock_recv_timestamp(msg, sk, skb);
1729 if (msg->msg_name) {
1730 struct sockaddr_can *addr = msg->msg_name;
1731 msg->msg_namelen = sizeof(*addr);
1732 memset(addr, 0, sizeof(*addr));
1733 addr->can_family = AF_CAN;
1734 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1735 addr->can_ifindex = skb->iif;
1737 /* FIXME: Race condition, skb->input_dev might disappear
1738 * while skb is waiting on the queue.
1741 addr->can_ifindex = skb->input_dev->ifindex;
1743 addr->can_ifindex = 0;
1747 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1748 skb_free_datagram(sk, skb);
1753 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1756 unsigned int mask = 0;
1758 DBG("socket %p\n", sock);
1760 mask = datagram_poll(file, sock, wait);
1764 static struct proto_ops bcm_ops = {
1766 .release = bcm_release,
1767 .bind = sock_no_bind,
1768 .connect = bcm_connect,
1769 .socketpair = sock_no_socketpair,
1770 .accept = sock_no_accept,
1771 .getname = sock_no_getname,
1773 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1774 .listen = sock_no_listen,
1775 .shutdown = sock_no_shutdown,
1776 .setsockopt = sock_no_setsockopt,
1777 .getsockopt = sock_no_getsockopt,
1778 .sendmsg = bcm_sendmsg,
1779 .recvmsg = bcm_recvmsg,
1780 .mmap = sock_no_mmap,
1781 .sendpage = sock_no_sendpage,
1784 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1785 static struct proto bcm_proto = {
1787 .owner = THIS_MODULE,
1788 .obj_size = sizeof(struct bcm_sock),
1792 static struct can_proto bcm_can_proto = {
1794 .protocol = CAN_BCM,
1795 .capability = BCM_CAP,
1800 static struct can_proto bcm_can_proto = {
1802 .protocol = CAN_BCM,
1803 .capability = BCM_CAP,
1805 .owner = THIS_MODULE,
1806 .obj_size = sizeof(struct bcm_sock),
1811 static int __init bcm_module_init(void)
1815 can_proto_register(&bcm_can_proto);
1817 /* create /proc/net/can/bcm directory */
1818 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1821 proc_dir->owner = THIS_MODULE;
1826 static void __exit bcm_module_exit(void)
1828 can_proto_unregister(&bcm_can_proto);
1831 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1834 module_init(bcm_module_init);
1835 module_exit(bcm_module_exit);