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 and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/uio.h>
50 #include <linux/net.h>
51 #include <linux/netdevice.h>
52 #include <linux/socket.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <linux/can.h>
56 #include <linux/can/core.h>
57 #include <linux/can/bcm.h>
59 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
60 #include <net/net_namespace.h>
63 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
66 /* use of last_frames[index].can_dlc */
67 #define RX_RECV 0x40 /* received data for this element */
68 #define RX_THR 0x80 /* element not been sent due to throttle feature */
69 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
71 /* get best masking value for can_rx_register() for a given single can_id */
72 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
73 (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 /* easy access to can_frame payload */
84 static inline u64 GET_U64(const struct can_frame *cp)
86 return *(u64 *)cp->data;
90 struct list_head list;
94 unsigned long j_ival1, j_ival2, j_lastmsg;
95 unsigned long frames_abs, frames_filtered;
96 struct timer_list timer, thrtimer;
97 struct timeval ival1, ival2;
98 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
101 struct timeval rx_stamp;
107 struct can_frame *frames;
108 struct can_frame *last_frames;
109 struct can_frame sframe;
110 struct can_frame last_sframe;
112 struct net_device *rx_reg_dev;
115 static struct proc_dir_entry *proc_dir;
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
125 struct notifier_block notifier;
126 struct list_head rx_ops;
127 struct list_head tx_ops;
128 unsigned long dropped_usr_msgs;
129 struct proc_dir_entry *bcm_proc_read;
130 char procname [9]; /* pointer printed in ASCII with \0 */
133 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
136 return (struct bcm_sock *)sk;
138 return (struct bcm_sock *)sk->sk_protinfo;
142 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
143 static void *kzalloc(size_t size, unsigned int __nocast flags)
145 void *ret = kmalloc(size, flags);
148 memset(ret, 0, size);
153 static inline void skb_get_timestamp(const struct sk_buff *skb,
154 struct timeval *stamp)
156 stamp->tv_sec = skb->stamp.tv_sec;
157 stamp->tv_usec = skb->stamp.tv_usec;
160 static inline void skb_set_timestamp(struct sk_buff *skb,
161 const struct timeval *stamp)
163 skb->stamp.tv_sec = stamp->tv_sec;
164 skb->stamp.tv_usec = stamp->tv_usec;
168 #define CFSIZ sizeof(struct can_frame)
169 #define OPSIZ sizeof(struct bcm_op)
170 #define MHSIZ sizeof(struct bcm_msg_head)
173 * rounded_tv2jif - calculate jiffies from timeval including optional up
174 * @tv: pointer to timeval
177 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
178 * function is intentionally more relaxed on precise timer ticks to get
179 * exact one jiffy for requested 1000us on a 1000HZ machine.
180 * This code is to be removed when upgrading to kernel hrtimer.
183 * calculated jiffies (max: ULONG_MAX)
185 static unsigned long rounded_tv2jif(const struct timeval *tv)
187 unsigned long sec = tv->tv_sec;
188 unsigned long usec = tv->tv_usec;
191 if (sec > ULONG_MAX / HZ)
194 /* round up to get at least the requested time */
195 usec += 1000000 / HZ - 1;
197 jif = usec / (1000000 / HZ);
199 if (sec * HZ > ULONG_MAX - jif)
202 return jif + sec * HZ;
208 static char *bcm_proc_getifname(int ifindex)
210 struct net_device *dev;
215 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
216 dev = __dev_get_by_index(&init_net, ifindex); /* no usage counting */
218 dev = __dev_get_by_index(ifindex); /* no usage counting */
226 static int bcm_read_proc(char *page, char **start, off_t off,
227 int count, int *eof, void *data)
230 struct sock *sk = (struct sock *)data;
231 struct bcm_sock *bo = bcm_sk(sk);
234 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
236 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
237 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
238 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
239 bo->dropped_usr_msgs);
240 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
241 bcm_proc_getifname(bo->ifindex));
242 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
244 list_for_each_entry(op, &bo->rx_ops, list) {
246 unsigned long reduction;
248 /* print only active entries & prevent division by zero */
252 len += snprintf(page + len, PAGE_SIZE - len,
254 op->can_id, bcm_proc_getifname(op->ifindex));
255 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
257 (op->flags & RX_CHECK_DLC)?'d':' ');
259 len += snprintf(page + len, PAGE_SIZE - len,
260 "timeo=%ld ", op->j_ival1);
263 len += snprintf(page + len, PAGE_SIZE - len,
264 "thr=%ld ", op->j_ival2);
266 len += snprintf(page + len, PAGE_SIZE - len,
267 "# recv %ld (%ld) => reduction: ",
268 op->frames_filtered, op->frames_abs);
270 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
272 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
273 (reduction == 100)?"near ":"", reduction);
275 if (len > PAGE_SIZE - 200) {
276 /* mark output cut off */
277 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
282 list_for_each_entry(op, &bo->tx_ops, list) {
284 len += snprintf(page + len, PAGE_SIZE - len,
285 "tx_op: %03X %s [%d] ",
286 op->can_id, bcm_proc_getifname(op->ifindex),
289 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
293 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
296 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
299 if (len > PAGE_SIZE - 100) {
300 /* mark output cut off */
301 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
306 len += snprintf(page + len, PAGE_SIZE - len, "\n");
313 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
314 * of the given bcm tx op
316 static void bcm_can_tx(struct bcm_op *op)
319 struct net_device *dev;
320 struct can_frame *cf = &op->frames[op->currframe];
322 /* no target device? => exit */
326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
327 dev = dev_get_by_index(&init_net, op->ifindex);
329 dev = dev_get_by_index(op->ifindex);
332 /* RFC: should this bcm_op remove itself here? */
336 skb = alloc_skb(CFSIZ, gfp_any());
340 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
342 /* send with loopback */
347 /* update statistics */
351 /* reached last frame? */
352 if (op->currframe >= op->nframes)
359 * bcm_send_to_user - send a BCM message to the userspace
360 * (consisting of bcm_msg_head + x CAN frames)
362 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
363 struct can_frame *frames, int has_timestamp)
366 struct can_frame *firstframe;
367 struct sockaddr_can *addr;
368 struct sock *sk = op->sk;
369 int datalen = head->nframes * CFSIZ;
372 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
376 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
379 /* can_frames starting here */
380 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
381 firstframe = (struct can_frame *) skb_tail_pointer(skb);
383 firstframe = (struct can_frame *) skb->tail;
386 memcpy(skb_put(skb, datalen), frames, datalen);
389 * the BCM uses the can_dlc-element of the can_frame
390 * structure for internal purposes. This is only
391 * relevant for updates that are generated by the
392 * BCM, where nframes is 1
394 if (head->nframes == 1)
395 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
399 /* restore rx timestamp */
400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
401 skb->tstamp = op->rx_stamp;
403 skb_set_timestamp(skb, &op->rx_stamp);
408 * Put the datagram to the queue so that bcm_recvmsg() can
409 * get it from there. We need to pass the interface index to
410 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
411 * containing the interface index.
414 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
415 addr = (struct sockaddr_can *)skb->cb;
416 memset(addr, 0, sizeof(*addr));
417 addr->can_family = AF_CAN;
418 addr->can_ifindex = op->rx_ifindex;
420 err = sock_queue_rcv_skb(sk, skb);
422 struct bcm_sock *bo = bcm_sk(sk);
425 /* don't care about overflows in this statistic */
426 bo->dropped_usr_msgs++;
431 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
433 static void bcm_tx_timeout_handler(unsigned long data)
435 struct bcm_op *op = (struct bcm_op *)data;
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 msg_head.opcode = TX_EXPIRED;
445 msg_head.flags = op->flags;
446 msg_head.count = op->count;
447 msg_head.ival1 = op->ival1;
448 msg_head.ival2 = op->ival2;
449 msg_head.can_id = op->can_id;
450 msg_head.nframes = 0;
452 bcm_send_to_user(op, &msg_head, NULL, 0);
456 if (op->j_ival1 && (op->count > 0)) {
458 op->timer.expires = jiffies + op->j_ival1;
459 add_timer(&op->timer);
461 /* send (next) frame */
466 op->timer.expires = jiffies + op->j_ival2;
467 add_timer(&op->timer);
469 /* send (next) frame */
479 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
481 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
483 struct bcm_msg_head head;
485 op->j_lastmsg = jiffies;
487 /* update statistics */
488 op->frames_filtered++;
490 /* prevent statistics overflow */
491 if (op->frames_filtered > ULONG_MAX/100)
492 op->frames_filtered = op->frames_abs = 0;
494 head.opcode = RX_CHANGED;
495 head.flags = op->flags;
496 head.count = op->count;
497 head.ival1 = op->ival1;
498 head.ival2 = op->ival2;
499 head.can_id = op->can_id;
502 bcm_send_to_user(op, &head, data, 1);
506 * bcm_rx_update_and_send - process a detected relevant receive content change
507 * 1. update the last received data
508 * 2. send a notification to the user (if possible)
510 static void bcm_rx_update_and_send(struct bcm_op *op,
511 struct can_frame *lastdata,
512 struct can_frame *rxdata)
514 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
516 memcpy(lastdata, rxdata, CFSIZ);
519 lastdata->can_dlc |= RX_RECV;
521 /* throttle bcm_rx_changed ? */
522 if ((op->thrtimer.expires) ||
523 ((op->j_ival2) && (nexttx > jiffies))) {
524 /* we are already waiting OR we have to start waiting */
526 /* mark as 'throttled' */
527 lastdata->can_dlc |= RX_THR;
529 if (!(op->thrtimer.expires)) {
530 /* start the timer only the first time */
531 op->thrtimer.expires = nexttx;
532 add_timer(&op->thrtimer);
536 /* send RX_CHANGED to the user immediately */
537 bcm_rx_changed(op, rxdata);
542 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
543 * received data stored in op->last_frames[]
545 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
546 struct can_frame *rxdata)
549 * no one uses the MSBs of can_dlc for comparation,
550 * so we use it here to detect the first time of reception
553 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
554 /* received data for the first time => send update to user */
555 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
559 /* do a real check in can_frame data section */
561 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
562 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
563 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
567 if (op->flags & RX_CHECK_DLC) {
568 /* do a real check in can_frame dlc */
569 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
571 bcm_rx_update_and_send(op, &op->last_frames[index],
579 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
581 static void bcm_rx_starttimer(struct bcm_op *op)
583 if (op->flags & RX_NO_AUTOTIMER)
587 op->timer.expires = jiffies + op->j_ival1;
588 add_timer(&op->timer);
593 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
595 static void bcm_rx_timeout_handler(unsigned long data)
597 struct bcm_op *op = (struct bcm_op *)data;
598 struct bcm_msg_head msg_head;
600 msg_head.opcode = RX_TIMEOUT;
601 msg_head.flags = op->flags;
602 msg_head.count = op->count;
603 msg_head.ival1 = op->ival1;
604 msg_head.ival2 = op->ival2;
605 msg_head.can_id = op->can_id;
606 msg_head.nframes = 0;
608 bcm_send_to_user(op, &msg_head, NULL, 0);
610 /* no restart of the timer is done here! */
612 /* if user wants to be informed, when cyclic CAN-Messages come back */
613 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
614 /* clear received can_frames to indicate 'nothing received' */
615 memset(op->last_frames, 0, op->nframes * CFSIZ);
620 * bcm_rx_thr_handler - the time for blocked content updates is over now:
621 * Check for throttled data and send it to the userspace
623 static void bcm_rx_thr_handler(unsigned long data)
625 struct bcm_op *op = (struct bcm_op *)data;
628 /* mark disabled / consumed timer */
629 op->thrtimer.expires = 0;
631 if (op->nframes > 1) {
632 /* for MUX filter we start at index 1 */
633 for (i = 1; i < op->nframes; i++) {
634 if ((op->last_frames) &&
635 (op->last_frames[i].can_dlc & RX_THR)) {
636 op->last_frames[i].can_dlc &= ~RX_THR;
637 bcm_rx_changed(op, &op->last_frames[i]);
642 /* for RX_FILTER_ID and simple filter */
643 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
644 op->last_frames[0].can_dlc &= ~RX_THR;
645 bcm_rx_changed(op, &op->last_frames[0]);
651 * bcm_rx_handler - handle a CAN frame receiption
653 static void bcm_rx_handler(struct sk_buff *skb, void *data)
655 struct bcm_op *op = (struct bcm_op *)data;
656 struct can_frame rxframe;
659 /* disable timeout */
660 del_timer(&op->timer);
662 if (skb->len == sizeof(rxframe)) {
663 memcpy(&rxframe, skb->data, sizeof(rxframe));
664 /* save rx timestamp */
665 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
666 op->rx_stamp = skb->tstamp;
668 skb_get_timestamp(skb, &op->rx_stamp);
670 /* save originator for recvfrom() */
671 op->rx_ifindex = skb->dev->ifindex;
672 /* update statistics */
681 if (op->can_id != rxframe.can_id)
684 if (op->flags & RX_RTR_FRAME) {
685 /* send reply for RTR-request (placed in op->frames[0]) */
690 if (op->flags & RX_FILTER_ID) {
691 /* the easiest case */
693 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
694 bcm_rx_starttimer(op);
698 if (op->nframes == 1) {
699 /* simple compare with index 0 */
701 bcm_rx_cmp_to_index(op, 0, &rxframe);
702 bcm_rx_starttimer(op);
706 if (op->nframes > 1) {
709 * find the first multiplex mask that fits.
710 * Remark: The MUX-mask is stored in index 0
713 for (i = 1; i < op->nframes; i++) {
714 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
715 (GET_U64(&op->frames[0]) &
716 GET_U64(&op->frames[i]))) {
717 bcm_rx_cmp_to_index(op, i, &rxframe);
721 bcm_rx_starttimer(op);
726 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
728 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
733 list_for_each_entry(op, ops, list) {
734 if ((op->can_id == can_id) && (op->ifindex == ifindex))
741 static void bcm_remove_op(struct bcm_op *op)
743 del_timer(&op->timer);
744 del_timer(&op->thrtimer);
746 if ((op->frames) && (op->frames != &op->sframe))
749 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
750 kfree(op->last_frames);
757 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
759 if (op->rx_reg_dev == dev) {
760 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
763 /* mark as removed subscription */
764 op->rx_reg_dev = NULL;
766 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
767 "mismatch %p %p\n", op->rx_reg_dev, dev);
771 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
773 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
775 struct bcm_op *op, *n;
777 list_for_each_entry_safe(op, n, ops, list) {
778 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
781 * Don't care if we're bound or not (due to netdev
782 * problems) can_rx_unregister() is always a save
787 * Only remove subscriptions that had not
788 * been removed due to NETDEV_UNREGISTER
791 if (op->rx_reg_dev) {
792 struct net_device *dev;
794 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
795 dev = dev_get_by_index(&init_net,
798 dev = dev_get_by_index(op->ifindex);
801 bcm_rx_unreg(dev, op);
806 can_rx_unregister(NULL, op->can_id,
816 return 0; /* not found */
820 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
822 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
824 struct bcm_op *op, *n;
826 list_for_each_entry_safe(op, n, ops, list) {
827 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
834 return 0; /* not found */
838 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
840 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
843 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
848 /* put current values into msg_head */
849 msg_head->flags = op->flags;
850 msg_head->count = op->count;
851 msg_head->ival1 = op->ival1;
852 msg_head->ival2 = op->ival2;
853 msg_head->nframes = op->nframes;
855 bcm_send_to_user(op, msg_head, op->frames, 0);
861 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
863 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
864 int ifindex, struct sock *sk)
866 struct bcm_sock *bo = bcm_sk(sk);
870 /* we need a real device to send frames */
874 /* we need at least one can_frame */
875 if (msg_head->nframes < 1)
878 /* check the given can_id */
879 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
882 /* update existing BCM operation
884 * Do we need more space for the can_frames than currently
885 * allocated? -> This is a _really_ unusual use-case and
886 * therefore (complexity / locking) it is not supported.
888 if (msg_head->nframes > op->nframes)
891 /* update can_frames content */
892 for (i = 0; i < msg_head->nframes; i++) {
893 err = memcpy_fromiovec((u8 *)&op->frames[i],
894 msg->msg_iov, CFSIZ);
898 if (msg_head->flags & TX_CP_CAN_ID) {
899 /* copy can_id into frame */
900 op->frames[i].can_id = msg_head->can_id;
905 /* insert new BCM operation for the given can_id */
907 op = kzalloc(OPSIZ, GFP_KERNEL);
911 op->can_id = msg_head->can_id;
913 /* create array for can_frames and copy the data */
914 if (msg_head->nframes > 1) {
915 op->frames = kmalloc(msg_head->nframes * CFSIZ,
922 op->frames = &op->sframe;
924 for (i = 0; i < msg_head->nframes; i++) {
925 err = memcpy_fromiovec((u8 *)&op->frames[i],
926 msg->msg_iov, CFSIZ);
928 if (op->frames != &op->sframe)
934 if (msg_head->flags & TX_CP_CAN_ID) {
935 /* copy can_id into frame */
936 op->frames[i].can_id = msg_head->can_id;
940 /* tx_ops never compare with previous received messages */
941 op->last_frames = NULL;
943 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
946 op->ifindex = ifindex;
948 /* initialize uninitialized (kmalloc) structure */
949 init_timer(&op->timer);
951 /* currently unused in tx_ops */
952 init_timer(&op->thrtimer);
954 /* handler for tx_ops */
955 op->timer.function = bcm_tx_timeout_handler;
957 /* timer.data points to this op-structure */
958 op->timer.data = (unsigned long)op;
960 /* add this bcm_op to the list of the tx_ops */
961 list_add(&op->list, &bo->tx_ops);
963 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
965 if (op->nframes != msg_head->nframes) {
966 op->nframes = msg_head->nframes;
967 /* start multiple frame transmission with index 0 */
973 op->flags = msg_head->flags;
975 if (op->flags & TX_RESET_MULTI_IDX) {
976 /* start multiple frame transmission with index 0 */
980 if (op->flags & SETTIMER) {
981 /* set timer values */
983 op->count = msg_head->count;
984 op->ival1 = msg_head->ival1;
985 op->ival2 = msg_head->ival2;
986 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
987 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
989 /* disable an active timer due to zero values? */
990 if (!op->j_ival1 && !op->j_ival2)
991 del_timer(&op->timer);
994 if ((op->flags & STARTTIMER) &&
995 ((op->j_ival1 && op->count) || op->j_ival2)) {
997 del_timer(&op->timer);
999 /* spec: send can_frame when starting timer */
1000 op->flags |= TX_ANNOUNCE;
1002 if (op->j_ival1 && (op->count > 0)) {
1003 /* op->count-- is done in bcm_tx_timeout_handler */
1004 op->timer.expires = jiffies + op->j_ival1;
1006 op->timer.expires = jiffies + op->j_ival2;
1008 add_timer(&op->timer);
1011 if (op->flags & TX_ANNOUNCE)
1014 return msg_head->nframes * CFSIZ + MHSIZ;
1018 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1020 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1021 int ifindex, struct sock *sk)
1023 struct bcm_sock *bo = bcm_sk(sk);
1028 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1029 /* be robust against wrong usage ... */
1030 msg_head->flags |= RX_FILTER_ID;
1031 msg_head->nframes = 0; /* ignore trailing garbage */
1034 if ((msg_head->flags & RX_RTR_FRAME) &&
1035 ((msg_head->nframes != 1) ||
1036 (!(msg_head->can_id & CAN_RTR_FLAG))))
1039 /* check the given can_id */
1040 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1042 /* update existing BCM operation
1044 * Do we need more space for the can_frames than currently
1045 * allocated? -> This is a _really_ unusual use-case and
1046 * therefore (complexity / locking) it is not supported.
1048 if (msg_head->nframes > op->nframes)
1051 if (msg_head->nframes) {
1052 /* update can_frames content */
1053 err = memcpy_fromiovec((u8 *)op->frames,
1055 msg_head->nframes * CFSIZ);
1059 /* clear last_frames to indicate 'nothing received' */
1060 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1063 op->nframes = msg_head->nframes;
1065 /* Only an update -> do not call can_rx_register() */
1069 /* insert new BCM operation for the given can_id */
1071 op = kzalloc(OPSIZ, GFP_KERNEL);
1075 op->can_id = msg_head->can_id;
1076 op->nframes = msg_head->nframes;
1078 if (msg_head->nframes > 1) {
1079 /* create array for can_frames and copy the data */
1080 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1087 /* create and init array for received can_frames */
1088 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1090 if (!op->last_frames) {
1097 op->frames = &op->sframe;
1098 op->last_frames = &op->last_sframe;
1101 if (msg_head->nframes) {
1102 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1103 msg_head->nframes * CFSIZ);
1105 if (op->frames != &op->sframe)
1107 if (op->last_frames != &op->last_sframe)
1108 kfree(op->last_frames);
1115 op->ifindex = ifindex;
1117 /* initialize uninitialized (kzalloc) structure */
1118 init_timer(&op->timer);
1120 /* init throttle timer for RX_CHANGED */
1121 init_timer(&op->thrtimer);
1123 /* handler for rx timeouts */
1124 op->timer.function = bcm_rx_timeout_handler;
1126 /* timer.data points to this op-structure */
1127 op->timer.data = (unsigned long)op;
1129 /* handler for RX_CHANGED throttle timeouts */
1130 op->thrtimer.function = bcm_rx_thr_handler;
1132 /* timer.data points to this op-structure */
1133 op->thrtimer.data = (unsigned long)op;
1135 /* mark disabled timer */
1136 op->thrtimer.expires = 0;
1138 /* add this bcm_op to the list of the rx_ops */
1139 list_add(&op->list, &bo->rx_ops);
1141 /* call can_rx_register() */
1144 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1147 op->flags = msg_head->flags;
1149 if (op->flags & RX_RTR_FRAME) {
1151 /* no timers in RTR-mode */
1152 del_timer(&op->thrtimer);
1153 del_timer(&op->timer);
1156 * funny feature in RX(!)_SETUP only for RTR-mode:
1157 * copy can_id into frame BUT without RTR-flag to
1158 * prevent a full-load-loopback-test ... ;-]
1160 if ((op->flags & TX_CP_CAN_ID) ||
1161 (op->frames[0].can_id == op->can_id))
1162 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1165 if (op->flags & SETTIMER) {
1167 /* set timer value */
1168 op->ival1 = msg_head->ival1;
1169 op->ival2 = msg_head->ival2;
1170 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1171 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1173 /* disable an active timer due to zero value? */
1175 del_timer(&op->timer);
1177 /* free currently blocked msgs ? */
1178 if (op->thrtimer.expires) {
1179 del_timer(&op->thrtimer);
1180 /* send blocked msgs hereafter */
1181 op->thrtimer.expires = jiffies + 2;
1182 add_timer(&op->thrtimer);
1185 * if (op->j_ival2) is zero, no (new) throttling
1186 * will happen. For details see functions
1187 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1191 if ((op->flags & STARTTIMER) && op->j_ival1) {
1192 del_timer(&op->timer);
1193 op->timer.expires = jiffies + op->j_ival1;
1194 add_timer(&op->timer);
1198 /* now we can register for can_ids, if we added a new bcm_op */
1199 if (do_rx_register) {
1201 struct net_device *dev;
1203 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1204 dev = dev_get_by_index(&init_net, ifindex);
1206 dev = dev_get_by_index(ifindex);
1209 err = can_rx_register(dev, op->can_id,
1210 REGMASK(op->can_id),
1214 op->rx_reg_dev = dev;
1219 err = can_rx_register(NULL, op->can_id,
1220 REGMASK(op->can_id),
1221 bcm_rx_handler, op, "bcm");
1223 /* this bcm rx op is broken -> remove it */
1224 list_del(&op->list);
1230 return msg_head->nframes * CFSIZ + MHSIZ;
1234 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1236 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1238 struct sk_buff *skb;
1239 struct net_device *dev;
1242 /* just copy and send one can_frame */
1244 if (!ifindex) /* we need a real device to send frames */
1247 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1252 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1258 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1259 dev = dev_get_by_index(&init_net, ifindex);
1261 dev = dev_get_by_index(ifindex);
1270 can_send(skb, 1); /* send with loopback */
1273 return CFSIZ + MHSIZ;
1277 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1279 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1280 struct msghdr *msg, size_t size)
1282 struct sock *sk = sock->sk;
1283 struct bcm_sock *bo = bcm_sk(sk);
1284 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1285 struct bcm_msg_head msg_head;
1286 int ret; /* read bytes or error codes as return value */
1291 /* check for alternative ifindex for this bcm_op */
1293 if (!ifindex && msg->msg_name) {
1294 /* no bound device as default => check msg_name */
1295 struct sockaddr_can *addr =
1296 (struct sockaddr_can *)msg->msg_name;
1298 if (addr->can_family != AF_CAN)
1301 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1304 struct net_device *dev;
1306 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1307 dev = dev_get_by_index(&init_net, ifindex);
1309 dev = dev_get_by_index(ifindex);
1314 if (dev->type != ARPHRD_CAN) {
1323 /* read message head information */
1325 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1331 switch (msg_head.opcode) {
1334 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1338 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1342 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1349 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1356 /* reuse msg_head for the reply to TX_READ */
1357 msg_head.opcode = TX_STATUS;
1358 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1362 /* reuse msg_head for the reply to RX_READ */
1363 msg_head.opcode = RX_STATUS;
1364 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1368 /* we need at least one can_frame */
1369 if (msg_head.nframes < 1)
1372 ret = bcm_tx_send(msg, ifindex, sk);
1386 * notification handler for netdevice status changes
1388 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1391 struct net_device *dev = (struct net_device *)data;
1392 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1393 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1394 struct sock *sk = &bo->sk;
1396 struct sock *sk = bo->sk;
1399 int notify_enodev = 0;
1401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1402 if (dev->nd_net != &init_net)
1406 if (dev->type != ARPHRD_CAN)
1411 case NETDEV_UNREGISTER:
1414 /* remove device specific receive entries */
1415 list_for_each_entry(op, &bo->rx_ops, list)
1416 if (op->rx_reg_dev == dev)
1417 bcm_rx_unreg(dev, op);
1419 /* remove device reference, if this is our bound device */
1420 if (bo->bound && bo->ifindex == dev->ifindex) {
1428 if (notify_enodev) {
1429 sk->sk_err = ENODEV;
1430 if (!sock_flag(sk, SOCK_DEAD))
1431 sk->sk_error_report(sk);
1436 if (bo->bound && bo->ifindex == dev->ifindex) {
1437 sk->sk_err = ENETDOWN;
1438 if (!sock_flag(sk, SOCK_DEAD))
1439 sk->sk_error_report(sk);
1447 * initial settings for all BCM sockets to be set at socket creation time
1449 static int bcm_init(struct sock *sk)
1451 struct bcm_sock *bo = bcm_sk(sk);
1453 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1458 bo->dropped_usr_msgs = 0;
1459 bo->bcm_proc_read = NULL;
1461 INIT_LIST_HEAD(&bo->tx_ops);
1462 INIT_LIST_HEAD(&bo->rx_ops);
1465 bo->notifier.notifier_call = bcm_notifier;
1467 register_netdevice_notifier(&bo->notifier);
1473 * standard socket functions
1475 static int bcm_release(struct socket *sock)
1477 struct sock *sk = sock->sk;
1478 struct bcm_sock *bo = bcm_sk(sk);
1479 struct bcm_op *op, *next;
1481 /* remove bcm_ops, timer, rx_unregister(), etc. */
1483 unregister_netdevice_notifier(&bo->notifier);
1487 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1490 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1492 * Don't care if we're bound or not (due to netdev problems)
1493 * can_rx_unregister() is always a save thing to do here.
1497 * Only remove subscriptions that had not
1498 * been removed due to NETDEV_UNREGISTER
1501 if (op->rx_reg_dev) {
1502 struct net_device *dev;
1504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1505 dev = dev_get_by_index(&init_net, op->ifindex);
1507 dev = dev_get_by_index(op->ifindex);
1510 bcm_rx_unreg(dev, op);
1515 can_rx_unregister(NULL, op->can_id,
1516 REGMASK(op->can_id),
1517 bcm_rx_handler, op);
1522 /* remove procfs entry */
1523 if (proc_dir && bo->bcm_proc_read)
1524 remove_proc_entry(bo->procname, proc_dir);
1526 /* remove device reference */
1538 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1541 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1542 struct sock *sk = sock->sk;
1543 struct bcm_sock *bo = bcm_sk(sk);
1548 /* bind a device to this socket */
1549 if (addr->can_ifindex) {
1550 struct net_device *dev;
1552 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1553 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1555 dev = dev_get_by_index(addr->can_ifindex);
1560 if (dev->type != ARPHRD_CAN) {
1565 bo->ifindex = dev->ifindex;
1569 /* no interface reference for ifindex = 0 ('any' CAN device) */
1576 /* unique socket address as filename */
1577 sprintf(bo->procname, "%p", sock);
1578 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1586 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1587 struct msghdr *msg, size_t size, int flags)
1589 struct sock *sk = sock->sk;
1590 struct sk_buff *skb;
1595 noblock = flags & MSG_DONTWAIT;
1596 flags &= ~MSG_DONTWAIT;
1597 skb = skb_recv_datagram(sk, flags, noblock, &error);
1601 if (skb->len < size)
1604 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1606 skb_free_datagram(sk, skb);
1610 sock_recv_timestamp(msg, sk, skb);
1612 if (msg->msg_name) {
1613 msg->msg_namelen = sizeof(struct sockaddr_can);
1614 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1617 skb_free_datagram(sk, skb);
1622 static struct proto_ops bcm_ops __read_mostly = {
1624 .release = bcm_release,
1625 .bind = sock_no_bind,
1626 .connect = bcm_connect,
1627 .socketpair = sock_no_socketpair,
1628 .accept = sock_no_accept,
1629 .getname = sock_no_getname,
1630 .poll = datagram_poll,
1631 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1632 .listen = sock_no_listen,
1633 .shutdown = sock_no_shutdown,
1634 .setsockopt = sock_no_setsockopt,
1635 .getsockopt = sock_no_getsockopt,
1636 .sendmsg = bcm_sendmsg,
1637 .recvmsg = bcm_recvmsg,
1638 .mmap = sock_no_mmap,
1639 .sendpage = sock_no_sendpage,
1642 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1643 static struct proto bcm_proto __read_mostly = {
1645 .owner = THIS_MODULE,
1646 .obj_size = sizeof(struct bcm_sock),
1650 static struct can_proto bcm_can_proto __read_mostly = {
1652 .protocol = CAN_BCM,
1658 static struct can_proto bcm_can_proto __read_mostly = {
1660 .protocol = CAN_BCM,
1663 .owner = THIS_MODULE,
1664 .obj_size = sizeof(struct bcm_sock),
1669 static int __init bcm_module_init(void)
1675 err = can_proto_register(&bcm_can_proto);
1677 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1681 /* create /proc/net/can-bcm directory */
1682 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1683 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1685 proc_dir = proc_mkdir("can-bcm", proc_net);
1689 proc_dir->owner = THIS_MODULE;
1694 static void __exit bcm_module_exit(void)
1696 can_proto_unregister(&bcm_can_proto);
1699 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1700 proc_net_remove(&init_net, "can-bcm");
1702 proc_net_remove("can-bcm");
1706 module_init(bcm_module_init);
1707 module_exit(bcm_module_exit);