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;
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));
389 /* can_frames starting here */
390 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
391 firstframe = (struct can_frame *) skb_tail_pointer(skb);
393 firstframe = (struct can_frame *) skb->tail;
396 memcpy(skb_put(skb, datalen), frames, datalen);
399 * the BCM uses the can_dlc-element of the can_frame
400 * structure for internal purposes. This is only
401 * relevant for updates that are generated by the
402 * BCM, where nframes is 1
404 if (head->nframes == 1)
405 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
409 /* restore rx timestamp */
410 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
411 skb->tstamp = op->rx_stamp;
413 skb_set_timestamp(skb, &op->rx_stamp);
417 /* restore originator for recvfrom() */
418 addr = (struct sockaddr_can *)skb->cb;
419 memset(addr, 0, sizeof(*addr));
420 addr->can_family = AF_CAN;
421 addr->can_ifindex = op->rx_ifindex;
423 err = sock_queue_rcv_skb(sk, skb);
425 struct bcm_sock *bo = bcm_sk(sk);
427 DBG("sock_queue_rcv_skb failed: %d\n", err);
429 /* don't care about overflows in this statistic */
430 bo->dropped_usr_msgs++;
435 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
437 static void bcm_tx_timeout_handler(unsigned long data)
439 struct bcm_op *op = (struct bcm_op *)data;
441 DBG("Called with bcm_op %p\n", op);
443 if (op->j_ival1 && (op->count > 0)) {
446 if (!op->count && (op->flags & TX_COUNTEVT)) {
447 struct bcm_msg_head msg_head;
449 /* create notification to user */
450 DBG("sending TX_EXPIRED for can_id %03X\n",
453 msg_head.opcode = TX_EXPIRED;
454 msg_head.flags = op->flags;
455 msg_head.count = op->count;
456 msg_head.ival1 = op->ival1;
457 msg_head.ival2 = op->ival2;
458 msg_head.can_id = op->can_id;
459 msg_head.nframes = 0;
461 bcm_send_to_user(op, &msg_head, NULL, 0);
465 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
466 op->count, op->j_ival1, op->j_ival2);
468 if (op->j_ival1 && (op->count > 0)) {
470 op->timer.expires = jiffies + op->j_ival1;
471 add_timer(&op->timer);
473 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
475 (char *) op->timer.data,
476 (unsigned int) op->timer.expires);
478 /* send (next) frame */
483 op->timer.expires = jiffies + op->j_ival2;
484 add_timer(&op->timer);
486 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
488 (char *) op->timer.data,
489 (unsigned int) op->timer.expires);
491 /* send (next) frame */
495 DBG("no timer restart\n");
502 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
504 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
506 struct bcm_msg_head head;
508 op->j_lastmsg = jiffies;
510 /* update statistics */
511 op->frames_filtered++;
513 /* prevent statistics overflow */
514 if (op->frames_filtered > ULONG_MAX/100)
515 op->frames_filtered = op->frames_abs = 0;
517 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
518 (unsigned int) op->j_lastmsg, op);
519 DBG("sending notification\n");
521 head.opcode = RX_CHANGED;
522 head.flags = op->flags;
523 head.count = op->count;
524 head.ival1 = op->ival1;
525 head.ival2 = op->ival2;
526 head.can_id = op->can_id;
529 bcm_send_to_user(op, &head, data, 1);
533 * bcm_rx_update_and_send - process a detected relevant receive content change
534 * 1. update the last received data
535 * 2. send a notification to the user (if possible)
537 static void bcm_rx_update_and_send(struct bcm_op *op,
538 struct can_frame *lastdata,
539 struct can_frame *rxdata)
541 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
543 memcpy(lastdata, rxdata, CFSIZ);
546 lastdata->can_dlc |= RX_RECV;
548 /* throttle bcm_rx_changed ? */
549 if ((op->thrtimer.expires) ||
550 ((op->j_ival2) && (nexttx > jiffies))) {
551 /* we are already waiting OR we have to start waiting */
553 /* mark as 'throttled' */
554 lastdata->can_dlc |= RX_THR;
556 if (!(op->thrtimer.expires)) {
557 /* start the timer only the first time */
558 op->thrtimer.expires = nexttx;
559 add_timer(&op->thrtimer);
561 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
562 op->thrtimer.function,
563 (char *) op->thrtimer.data,
564 (unsigned int) op->thrtimer.expires);
568 /* send RX_CHANGED to the user immediately */
569 bcm_rx_changed(op, rxdata);
574 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
575 * received data stored in op->last_frames[]
577 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
578 struct can_frame *rxdata)
581 * no one uses the MSBs of can_dlc for comparation,
582 * so we use it here to detect the first time of reception
585 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
586 /* received data for the first time => send update to user */
587 DBG("first time :)\n");
588 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
592 /* do a real check in can_frame data section */
594 DBG("op->frames[index].data = 0x%016llx\n",
595 GET_U64(&op->frames[index]));
596 DBG("op->last_frames[index].data = 0x%016llx\n",
597 GET_U64(&op->last_frames[index]));
598 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
600 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
601 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
602 DBG("relevant data change :)\n");
603 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
607 if (op->flags & RX_CHECK_DLC) {
608 /* do a real check in can_frame dlc */
609 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
611 DBG("dlc change :)\n");
612 bcm_rx_update_and_send(op, &op->last_frames[index],
617 DBG("no relevant change :(\n");
621 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
623 static void bcm_rx_starttimer(struct bcm_op *op)
625 if (op->flags & RX_NO_AUTOTIMER)
629 op->timer.expires = jiffies + op->j_ival1;
631 DBG("adding rx timeout timer ival1. func=%p data=%p "
634 (char *) op->timer.data,
635 (unsigned int) op->timer.expires);
637 add_timer(&op->timer);
642 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
644 static void bcm_rx_timeout_handler(unsigned long data)
646 struct bcm_op *op = (struct bcm_op *)data;
647 struct bcm_msg_head msg_head;
649 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
651 msg_head.opcode = RX_TIMEOUT;
652 msg_head.flags = op->flags;
653 msg_head.count = op->count;
654 msg_head.ival1 = op->ival1;
655 msg_head.ival2 = op->ival2;
656 msg_head.can_id = op->can_id;
657 msg_head.nframes = 0;
659 bcm_send_to_user(op, &msg_head, NULL, 0);
661 /* no restart of the timer is done here! */
663 /* if user wants to be informed, when cyclic CAN-Messages come back */
664 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
665 /* clear received can_frames to indicate 'nothing received' */
666 memset(op->last_frames, 0, op->nframes * CFSIZ);
667 DBG("RX_ANNOUNCE_RESTART\n");
672 * bcm_rx_thr_handler - the time for blocked content updates is over now:
673 * Check for throttled data and send it to the userspace
675 static void bcm_rx_thr_handler(unsigned long data)
677 struct bcm_op *op = (struct bcm_op *)data;
680 /* mark disabled / consumed timer */
681 op->thrtimer.expires = 0;
683 if (op->nframes > 1) {
684 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
686 /* for MUX filter we start at index 1 */
687 for (i = 1; i < op->nframes; i++) {
688 if ((op->last_frames) &&
689 (op->last_frames[i].can_dlc & RX_THR)) {
690 op->last_frames[i].can_dlc &= ~RX_THR;
691 bcm_rx_changed(op, &op->last_frames[i]);
696 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
698 /* for RX_FILTER_ID and simple filter */
699 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
700 op->last_frames[0].can_dlc &= ~RX_THR;
701 bcm_rx_changed(op, &op->last_frames[0]);
707 * bcm_rx_handler - handle a CAN frame receiption
709 static void bcm_rx_handler(struct sk_buff *skb, void *data)
711 struct bcm_op *op = (struct bcm_op *)data;
712 struct can_frame rxframe;
715 /* disable timeout */
716 del_timer(&op->timer);
718 DBG("Called with bcm_op %p\n", op);
720 if (skb->len == sizeof(rxframe)) {
721 memcpy(&rxframe, skb->data, sizeof(rxframe));
722 /* save rx timestamp */
723 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
724 op->rx_stamp = skb->tstamp;
726 skb_get_timestamp(skb, &op->rx_stamp);
728 /* save originator for recvfrom() */
729 op->rx_ifindex = skb->dev->ifindex;
730 /* update statistics */
733 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
736 DBG("Wrong skb->len = %d\n", skb->len);
741 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
743 if (op->can_id != rxframe.can_id) {
744 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
745 rxframe.can_id, op->can_id);
749 if (op->flags & RX_RTR_FRAME) {
750 /* send reply for RTR-request */
751 DBG("RTR-request\n");
753 /* send op->frames[0] to CAN device */
758 if (op->flags & RX_FILTER_ID) {
759 /* the easiest case */
760 DBG("Easy does it with RX_FILTER_ID\n");
762 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
763 bcm_rx_starttimer(op);
767 if (op->nframes == 1) {
768 /* simple compare with index 0 */
769 DBG("Simple compare\n");
771 bcm_rx_cmp_to_index(op, 0, &rxframe);
772 bcm_rx_starttimer(op);
776 if (op->nframes > 1) {
777 /* multiplex compare */
778 DBG("Multiplex compare\n");
781 * find the first multiplex mask that fits.
782 * Remark: The MUX-mask is stored in index 0
785 for (i = 1; i < op->nframes; i++) {
786 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
787 (GET_U64(&op->frames[0]) &
788 GET_U64(&op->frames[i]))) {
789 DBG("found MUX index %d\n", i);
790 bcm_rx_cmp_to_index(op, i, &rxframe);
794 bcm_rx_starttimer(op);
799 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
801 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
806 list_for_each_entry(op, ops, list) {
807 if ((op->can_id == can_id) && (op->ifindex == ifindex))
814 static void bcm_remove_op(struct bcm_op *op)
816 del_timer(&op->timer);
817 del_timer(&op->thrtimer);
819 if ((op->frames) && (op->frames != &op->sframe))
822 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
823 kfree(op->last_frames);
830 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
832 if (op->rx_reg_dev == dev) {
833 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
836 /* mark as removed subscription */
837 op->rx_reg_dev = NULL;
839 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
840 "mismatch %p %p\n", op->rx_reg_dev, dev);
844 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
846 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
848 struct bcm_op *op, *n;
850 list_for_each_entry_safe(op, n, ops, list) {
851 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
852 DBG("removing rx_op %p for can_id %03X\n",
856 * Don't care if we're bound or not (due to netdev
857 * problems) can_rx_unregister() is always a save
862 * Only remove subscriptions that had not
863 * been removed due to NETDEV_UNREGISTER
866 if (op->rx_reg_dev) {
867 struct net_device *dev;
869 dev = dev_get_by_index(op->ifindex);
871 bcm_rx_unreg(dev, op);
876 can_rx_unregister(NULL, op->can_id,
886 return 0; /* not found */
890 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
892 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
894 struct bcm_op *op, *n;
896 list_for_each_entry_safe(op, n, ops, list) {
897 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
898 DBG("removing rx_op %p for can_id %03X\n",
906 return 0; /* not found */
910 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
912 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
915 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
918 DBG("TRX_READ: did not find op for can_id %03X\n",
923 DBG("TRX_READ: sending status for can_id %03X\n",
925 /* put current values into msg_head */
926 msg_head->flags = op->flags;
927 msg_head->count = op->count;
928 msg_head->ival1 = op->ival1;
929 msg_head->ival2 = op->ival2;
930 msg_head->nframes = op->nframes;
932 bcm_send_to_user(op, msg_head, op->frames, 0);
938 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
940 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
941 int ifindex, struct sock *sk)
943 struct bcm_sock *bo = bcm_sk(sk);
947 /* we need a real device to send frames */
951 /* we need at least one can_frame */
952 if (msg_head->nframes < 1)
955 /* check the given can_id */
956 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
959 /* update existing BCM operation */
961 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
962 op, msg_head->can_id);
965 * Do we need more space for the can_frames than currently
966 * allocated? -> This is a _really_ unusual use-case and
967 * therefore (complexity / locking) it is not supported.
969 if (msg_head->nframes > op->nframes)
972 /* update can_frames content */
973 for (i = 0; i < msg_head->nframes; i++) {
974 err = memcpy_fromiovec((u8 *)&op->frames[i],
975 msg->msg_iov, CFSIZ);
979 if (msg_head->flags & TX_CP_CAN_ID) {
980 /* copy can_id into frame */
981 op->frames[i].can_id = msg_head->can_id;
986 /* insert new BCM operation for the given can_id */
988 op = kzalloc(OPSIZ, GFP_KERNEL);
992 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
993 op, msg_head->can_id);
995 op->can_id = msg_head->can_id;
997 /* create array for can_frames and copy the data */
998 if (msg_head->nframes > 1) {
999 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1006 op->frames = &op->sframe;
1008 for (i = 0; i < msg_head->nframes; i++) {
1009 err = memcpy_fromiovec((u8 *)&op->frames[i],
1010 msg->msg_iov, CFSIZ);
1012 if (op->frames != &op->sframe)
1018 if (msg_head->flags & TX_CP_CAN_ID) {
1019 /* copy can_id into frame */
1020 op->frames[i].can_id = msg_head->can_id;
1024 /* tx_ops never compare with previous received messages */
1025 op->last_frames = NULL;
1027 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1030 op->ifindex = ifindex;
1032 /* initialize uninitialized (kmalloc) structure */
1033 init_timer(&op->timer);
1035 /* currently unused in tx_ops */
1036 init_timer(&op->thrtimer);
1038 /* handler for tx_ops */
1039 op->timer.function = bcm_tx_timeout_handler;
1041 /* timer.data points to this op-structure */
1042 op->timer.data = (unsigned long)op;
1044 /* add this bcm_op to the list of the tx_ops */
1045 list_add(&op->list, &bo->tx_ops);
1047 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1049 if (op->nframes != msg_head->nframes) {
1050 op->nframes = msg_head->nframes;
1051 /* start multiple frame transmission with index 0 */
1057 op->flags = msg_head->flags;
1059 if (op->flags & TX_RESET_MULTI_IDX) {
1060 /* start multiple frame transmission with index 0 */
1064 if (op->flags & SETTIMER) {
1065 /* set timer values */
1067 op->count = msg_head->count;
1068 op->ival1 = msg_head->ival1;
1069 op->ival2 = msg_head->ival2;
1070 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1071 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1073 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1074 op->count, op->j_ival1, op->j_ival2);
1076 /* disable an active timer due to zero values? */
1077 if (!op->j_ival1 && !op->j_ival2) {
1078 del_timer(&op->timer);
1079 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1083 if ((op->flags & STARTTIMER) &&
1084 ((op->j_ival1 && op->count) || op->j_ival2)) {
1086 del_timer(&op->timer);
1088 /* spec: send can_frame when starting timer */
1089 op->flags |= TX_ANNOUNCE;
1091 if (op->j_ival1 && (op->count > 0)) {
1092 op->timer.expires = jiffies + op->j_ival1;
1093 /* op->count-- is done in bcm_tx_timeout_handler */
1094 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1097 (char *) op->timer.data,
1098 (unsigned int) op->timer.expires);
1101 op->timer.expires = jiffies + op->j_ival2;
1102 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1105 (char *) op->timer.data,
1106 (unsigned int) op->timer.expires);
1109 add_timer(&op->timer);
1112 if (op->flags & TX_ANNOUNCE)
1115 return msg_head->nframes * CFSIZ + MHSIZ;
1119 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1121 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1122 int ifindex, struct sock *sk)
1124 struct bcm_sock *bo = bcm_sk(sk);
1129 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1130 /* be robust against wrong usage ... */
1131 msg_head->flags |= RX_FILTER_ID;
1132 msg_head->nframes = 0; /* ignore trailing garbage */
1135 if ((msg_head->flags & RX_RTR_FRAME) &&
1136 ((msg_head->nframes != 1) ||
1137 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1139 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1143 /* check the given can_id */
1144 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1146 /* update existing BCM operation */
1148 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1149 op, msg_head->can_id);
1152 * Do we need more space for the can_frames than currently
1153 * allocated? -> This is a _really_ unusual use-case and
1154 * therefore (complexity / locking) it is not supported.
1156 if (msg_head->nframes > op->nframes)
1159 if (msg_head->nframes) {
1160 /* update can_frames content */
1161 err = memcpy_fromiovec((u8 *)op->frames,
1163 msg_head->nframes * CFSIZ);
1167 /* clear last_frames to indicate 'nothing received' */
1168 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1171 op->nframes = msg_head->nframes;
1173 /* Only an update -> do not call can_rx_register() */
1177 /* insert new BCM operation for the given can_id */
1179 op = kzalloc(OPSIZ, GFP_KERNEL);
1183 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1184 op, msg_head->can_id);
1186 op->can_id = msg_head->can_id;
1187 op->nframes = msg_head->nframes;
1189 if (msg_head->nframes > 1) {
1190 /* create array for can_frames and copy the data */
1191 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1198 /* create and init array for received can_frames */
1199 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1201 if (!op->last_frames) {
1208 op->frames = &op->sframe;
1209 op->last_frames = &op->last_sframe;
1212 if (msg_head->nframes) {
1213 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1214 msg_head->nframes * CFSIZ);
1216 if (op->frames != &op->sframe)
1218 if (op->last_frames != &op->last_sframe)
1219 kfree(op->last_frames);
1226 op->ifindex = ifindex;
1228 /* initialize uninitialized (kzalloc) structure */
1229 init_timer(&op->timer);
1231 /* init throttle timer for RX_CHANGED */
1232 init_timer(&op->thrtimer);
1234 /* handler for rx timeouts */
1235 op->timer.function = bcm_rx_timeout_handler;
1237 /* timer.data points to this op-structure */
1238 op->timer.data = (unsigned long)op;
1240 /* handler for RX_CHANGED throttle timeouts */
1241 op->thrtimer.function = bcm_rx_thr_handler;
1243 /* timer.data points to this op-structure */
1244 op->thrtimer.data = (unsigned long)op;
1246 /* mark disabled timer */
1247 op->thrtimer.expires = 0;
1249 /* add this bcm_op to the list of the tx_ops */
1250 list_add(&op->list, &bo->rx_ops);
1252 /* call can_rx_register() */
1255 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1258 op->flags = msg_head->flags;
1260 if (op->flags & RX_RTR_FRAME) {
1262 /* no timers in RTR-mode */
1263 del_timer(&op->thrtimer);
1264 del_timer(&op->timer);
1267 * funny feature in RX(!)_SETUP only for RTR-mode:
1268 * copy can_id into frame BUT without RTR-flag to
1269 * prevent a full-load-loopback-test ... ;-]
1271 if ((op->flags & TX_CP_CAN_ID) ||
1272 (op->frames[0].can_id == op->can_id))
1273 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1276 if (op->flags & SETTIMER) {
1278 /* set timer value */
1279 op->ival1 = msg_head->ival1;
1280 op->ival2 = msg_head->ival2;
1281 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1282 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1284 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1285 op->j_ival1, op->j_ival2);
1287 /* disable an active timer due to zero value? */
1289 del_timer(&op->timer);
1290 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1293 /* free currently blocked msgs ? */
1294 if (op->thrtimer.expires) {
1295 DBG("RX_SETUP: unblocking throttled msgs.\n");
1296 del_timer(&op->thrtimer);
1297 /* send blocked msgs hereafter */
1298 op->thrtimer.expires = jiffies + 2;
1299 add_timer(&op->thrtimer);
1302 * if (op->j_ival2) is zero, no (new) throttling
1303 * will happen. For details see functions
1304 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1308 if ((op->flags & STARTTIMER) && op->j_ival1) {
1310 del_timer(&op->timer);
1311 op->timer.expires = jiffies + op->j_ival1;
1313 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1315 (char *) op->timer.function,
1316 (char *) op->timer.data,
1317 (unsigned int) op->timer.expires);
1319 add_timer(&op->timer);
1323 /* now we can register for can_ids, if we added a new bcm_op */
1324 if (do_rx_register) {
1325 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1326 "rx_op is %p\n", op->can_id, op);
1329 struct net_device *dev = dev_get_by_index(ifindex);
1332 can_rx_register(dev, op->can_id,
1333 REGMASK(op->can_id),
1334 bcm_rx_handler, op, IDENT);
1335 op->rx_reg_dev = dev;
1340 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1341 bcm_rx_handler, op, IDENT);
1344 return msg_head->nframes * CFSIZ + MHSIZ;
1348 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1350 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1352 struct sk_buff *skb;
1353 struct net_device *dev;
1356 /* just copy and send one can_frame */
1358 if (!ifindex) /* we need a real device to send frames */
1361 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1366 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1372 DBG_FRAME("BCM: TX_SEND: sending frame",
1373 (struct can_frame *)skb->data);
1375 dev = dev_get_by_index(ifindex);
1383 can_send(skb, 1); /* send with loopback */
1386 return CFSIZ + MHSIZ;
1390 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1392 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1393 struct msghdr *msg, size_t size)
1395 struct sock *sk = sock->sk;
1396 struct bcm_sock *bo = bcm_sk(sk);
1397 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1398 struct bcm_msg_head msg_head;
1399 int ret; /* read bytes or error codes as return value */
1402 DBG("sock %p not bound\n", sk);
1406 /* check for alternative ifindex for this bcm_op */
1408 if (!ifindex && msg->msg_name) {
1409 /* no bound device as default => check msg_name */
1410 struct sockaddr_can *addr =
1411 (struct sockaddr_can *)msg->msg_name;
1413 if (addr->can_family != AF_CAN)
1416 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1419 struct net_device *dev = dev_get_by_index(ifindex);
1422 DBG("device %d not found\n", ifindex);
1426 if (dev->type != ARPHRD_CAN) {
1427 DBG("device %d no CAN device\n", ifindex);
1436 /* read message head information */
1438 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1442 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1446 switch (msg_head.opcode) {
1449 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1453 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1457 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1464 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1471 /* reuse msg_head for the reply to TX_READ */
1472 msg_head.opcode = TX_STATUS;
1473 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1477 /* reuse msg_head for the reply to RX_READ */
1478 msg_head.opcode = RX_STATUS;
1479 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1483 /* we need at least one can_frame */
1484 if (msg_head.nframes < 1)
1487 ret = bcm_tx_send(msg, ifindex, sk);
1491 DBG("Unknown opcode %d\n", msg_head.opcode);
1502 * notification handler for netdevice status changes
1504 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1507 struct net_device *dev = (struct net_device *)data;
1508 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1509 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1510 struct sock *sk = &bo->sk;
1512 struct sock *sk = bo->sk;
1515 int notify_enodev = 0;
1517 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1518 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1520 if (dev->type != ARPHRD_CAN)
1525 case NETDEV_UNREGISTER:
1528 /* remove device specific receive entries */
1529 list_for_each_entry(op, &bo->rx_ops, list)
1530 if (op->rx_reg_dev == dev)
1531 bcm_rx_unreg(dev, op);
1533 /* remove device reference, if this is our bound device */
1534 if (bo->bound && bo->ifindex == dev->ifindex) {
1542 if (notify_enodev) {
1543 sk->sk_err = ENODEV;
1544 if (!sock_flag(sk, SOCK_DEAD))
1545 sk->sk_error_report(sk);
1550 if (bo->bound && bo->ifindex == dev->ifindex) {
1551 sk->sk_err = ENETDOWN;
1552 if (!sock_flag(sk, SOCK_DEAD))
1553 sk->sk_error_report(sk);
1561 * initial settings for all BCM sockets to be set at socket creation time
1563 static int bcm_init(struct sock *sk)
1565 struct bcm_sock *bo = bcm_sk(sk);
1567 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1572 bo->dropped_usr_msgs = 0;
1573 bo->bcm_proc_read = NULL;
1575 INIT_LIST_HEAD(&bo->tx_ops);
1576 INIT_LIST_HEAD(&bo->rx_ops);
1579 bo->notifier.notifier_call = bcm_notifier;
1581 register_netdevice_notifier(&bo->notifier);
1587 * standard socket functions
1589 static int bcm_release(struct socket *sock)
1591 struct sock *sk = sock->sk;
1592 struct bcm_sock *bo = bcm_sk(sk);
1593 struct bcm_op *op, *next;
1595 DBG("socket %p, sk %p\n", sock, sk);
1597 /* remove bcm_ops, timer, rx_unregister(), etc. */
1599 unregister_netdevice_notifier(&bo->notifier);
1603 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1604 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1608 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1609 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1612 * Don't care if we're bound or not (due to netdev problems)
1613 * can_rx_unregister() is always a save thing to do here.
1617 * Only remove subscriptions that had not
1618 * been removed due to NETDEV_UNREGISTER
1621 if (op->rx_reg_dev) {
1622 struct net_device *dev;
1624 dev = dev_get_by_index(op->ifindex);
1626 bcm_rx_unreg(dev, op);
1631 can_rx_unregister(NULL, op->can_id,
1632 REGMASK(op->can_id),
1633 bcm_rx_handler, op);
1638 /* remove procfs entry */
1639 if (proc_dir && bo->bcm_proc_read)
1640 remove_proc_entry(bo->procname, proc_dir);
1642 /* remove device reference */
1654 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1657 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1658 struct sock *sk = sock->sk;
1659 struct bcm_sock *bo = bcm_sk(sk);
1664 /* bind a device to this socket */
1665 if (addr->can_ifindex) {
1666 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1669 DBG("could not find device index %d\n",
1674 if (dev->type != ARPHRD_CAN) {
1675 DBG("device %d no CAN device\n", addr->can_ifindex);
1680 bo->ifindex = dev->ifindex;
1683 DBG("socket %p bound to device %s (idx %d)\n",
1684 sock, dev->name, dev->ifindex);
1687 /* no interface reference for ifindex = 0 ('any' CAN device) */
1694 /* unique socket address as filename */
1695 sprintf(bo->procname, "%p", sock);
1696 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1704 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1705 struct msghdr *msg, size_t size, int flags)
1707 struct sock *sk = sock->sk;
1708 struct sk_buff *skb;
1713 DBG("socket %p, sk %p\n", sock, sk);
1715 noblock = flags & MSG_DONTWAIT;
1716 flags &= ~MSG_DONTWAIT;
1717 skb = skb_recv_datagram(sk, flags, noblock, &error);
1721 DBG("delivering skbuff %p\n", skb);
1724 if (skb->len < size)
1727 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1729 skb_free_datagram(sk, skb);
1733 sock_recv_timestamp(msg, sk, skb);
1735 if (msg->msg_name) {
1736 msg->msg_namelen = sizeof(struct sockaddr_can);
1737 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1740 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1741 skb_free_datagram(sk, skb);
1746 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1749 unsigned int mask = 0;
1751 DBG("socket %p\n", sock);
1753 mask = datagram_poll(file, sock, wait);
1757 static struct proto_ops bcm_ops = {
1759 .release = bcm_release,
1760 .bind = sock_no_bind,
1761 .connect = bcm_connect,
1762 .socketpair = sock_no_socketpair,
1763 .accept = sock_no_accept,
1764 .getname = sock_no_getname,
1766 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1767 .listen = sock_no_listen,
1768 .shutdown = sock_no_shutdown,
1769 .setsockopt = sock_no_setsockopt,
1770 .getsockopt = sock_no_getsockopt,
1771 .sendmsg = bcm_sendmsg,
1772 .recvmsg = bcm_recvmsg,
1773 .mmap = sock_no_mmap,
1774 .sendpage = sock_no_sendpage,
1777 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1778 static struct proto bcm_proto = {
1780 .owner = THIS_MODULE,
1781 .obj_size = sizeof(struct bcm_sock),
1785 static struct can_proto bcm_can_proto = {
1787 .protocol = CAN_BCM,
1788 .capability = BCM_CAP,
1793 static struct can_proto bcm_can_proto = {
1795 .protocol = CAN_BCM,
1796 .capability = BCM_CAP,
1798 .owner = THIS_MODULE,
1799 .obj_size = sizeof(struct bcm_sock),
1804 static int __init bcm_module_init(void)
1808 can_proto_register(&bcm_can_proto);
1810 /* create /proc/net/can/bcm directory */
1811 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1814 proc_dir->owner = THIS_MODULE;
1819 static void __exit bcm_module_exit(void)
1821 can_proto_unregister(&bcm_can_proto);
1824 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1827 module_init(bcm_module_init);
1828 module_exit(bcm_module_exit);