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/hrtimer.h>
48 #include <linux/list.h>
49 #include <linux/proc_fs.h>
50 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
51 #include <linux/seq_file.h>
53 #include <linux/uio.h>
54 #include <linux/net.h>
55 #include <linux/netdevice.h>
56 #include <linux/socket.h>
57 #include <linux/if_arp.h>
58 #include <linux/skbuff.h>
59 #include <socketcan/can.h>
60 #include <socketcan/can/core.h>
61 #include <socketcan/can/bcm.h>
63 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
64 #include <net/net_namespace.h>
66 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
70 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
73 /* use of last_frames[index].can_dlc */
74 #define RX_RECV 0x40 /* received data for this element */
75 #define RX_THR 0x80 /* element not been sent due to throttle feature */
76 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
78 /* get best masking value for can_rx_register() for a given single can_id */
79 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
80 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
81 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
83 #define CAN_BCM_VERSION CAN_VERSION
84 static __initdata const char banner[] = KERN_INFO
85 "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
87 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
88 MODULE_LICENSE("Dual BSD/GPL");
89 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
90 MODULE_ALIAS("can-proto-2");
92 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
93 #error This code only supports Kernel versions 2.6.22+
94 #error For older 2.6 Kernels please use bcm-prior-2-6-22.c instead of bcm.c
97 /* easy access to can_frame payload */
98 static inline u64 GET_U64(const struct can_frame *cp)
100 return *(u64 *)cp->data;
104 struct list_head list;
108 unsigned long frames_abs, frames_filtered;
109 struct timeval ival1, ival2;
110 struct hrtimer timer, thrtimer;
111 struct tasklet_struct tsklet, thrtsklet;
112 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
117 struct can_frame *frames;
118 struct can_frame *last_frames;
119 struct can_frame sframe;
120 struct can_frame last_sframe;
122 struct net_device *rx_reg_dev;
125 static struct proc_dir_entry *proc_dir;
131 struct notifier_block notifier;
132 struct list_head rx_ops;
133 struct list_head tx_ops;
134 unsigned long dropped_usr_msgs;
135 struct proc_dir_entry *bcm_proc_read;
136 char procname [9]; /* pointer printed in ASCII with \0 */
139 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
141 return (struct bcm_sock *)sk;
144 #define CFSIZ sizeof(struct can_frame)
145 #define OPSIZ sizeof(struct bcm_op)
146 #define MHSIZ sizeof(struct bcm_msg_head)
151 static char *bcm_proc_getifname(char *result, int ifindex)
153 struct net_device *dev;
158 read_lock(&dev_base_lock);
159 dev = __dev_get_by_index(&init_net, ifindex);
161 strcpy(result, dev->name);
163 strcpy(result, "???");
164 read_unlock(&dev_base_lock);
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
170 static int bcm_proc_show(struct seq_file *m, void *v)
172 char ifname[IFNAMSIZ];
173 struct sock *sk = (struct sock *)m->private;
174 struct bcm_sock *bo = bcm_sk(sk);
177 seq_printf(m, ">>> socket %p", sk->sk_socket);
178 seq_printf(m, " / sk %p", sk);
179 seq_printf(m, " / bo %p", bo);
180 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
181 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
182 seq_printf(m, " <<<\n");
184 list_for_each_entry(op, &bo->rx_ops, list) {
186 unsigned long reduction;
188 /* print only active entries & prevent division by zero */
192 seq_printf(m, "rx_op: %03X %-5s ",
193 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
194 seq_printf(m, "[%d]%c ", op->nframes,
195 (op->flags & RX_CHECK_DLC)?'d':' ');
196 if (op->kt_ival1.tv64)
197 seq_printf(m, "timeo=%lld ",
199 ktime_to_us(op->kt_ival1));
201 if (op->kt_ival2.tv64)
202 seq_printf(m, "thr=%lld ",
204 ktime_to_us(op->kt_ival2));
206 seq_printf(m, "# recv %ld (%ld) => reduction: ",
207 op->frames_filtered, op->frames_abs);
209 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
211 seq_printf(m, "%s%ld%%\n",
212 (reduction == 100)?"near ":"", reduction);
215 list_for_each_entry(op, &bo->tx_ops, list) {
217 seq_printf(m, "tx_op: %03X %s [%d] ",
219 bcm_proc_getifname(ifname, op->ifindex),
222 if (op->kt_ival1.tv64)
223 seq_printf(m, "t1=%lld ",
224 (long long) ktime_to_us(op->kt_ival1));
226 if (op->kt_ival2.tv64)
227 seq_printf(m, "t2=%lld ",
228 (long long) ktime_to_us(op->kt_ival2));
230 seq_printf(m, "# sent %ld\n", op->frames_abs);
236 static int bcm_proc_open(struct inode *inode, struct file *file)
238 return single_open(file, bcm_proc_show, PDE(inode)->data);
241 static const struct file_operations bcm_proc_fops = {
242 .owner = THIS_MODULE,
243 .open = bcm_proc_open,
246 .release = single_release,
249 static int bcm_read_proc(char *page, char **start, off_t off,
250 int count, int *eof, void *data)
252 char ifname[IFNAMSIZ];
254 struct sock *sk = (struct sock *)data;
255 struct bcm_sock *bo = bcm_sk(sk);
258 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
260 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
261 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
262 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
263 bo->dropped_usr_msgs);
264 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
265 bcm_proc_getifname(ifname, bo->ifindex));
266 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
268 list_for_each_entry(op, &bo->rx_ops, list) {
270 unsigned long reduction;
272 /* print only active entries & prevent division by zero */
276 len += snprintf(page + len, PAGE_SIZE - len,
278 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
279 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
281 (op->flags & RX_CHECK_DLC)?'d':' ');
282 if (op->kt_ival1.tv64)
283 len += snprintf(page + len, PAGE_SIZE - len,
286 ktime_to_us(op->kt_ival1));
288 if (op->kt_ival2.tv64)
289 len += snprintf(page + len, PAGE_SIZE - len,
292 ktime_to_us(op->kt_ival2));
294 len += snprintf(page + len, PAGE_SIZE - len,
295 "# recv %ld (%ld) => reduction: ",
296 op->frames_filtered, op->frames_abs);
298 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
300 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
301 (reduction == 100)?"near ":"", reduction);
303 if (len > PAGE_SIZE - 200) {
304 /* mark output cut off */
305 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
310 list_for_each_entry(op, &bo->tx_ops, list) {
312 len += snprintf(page + len, PAGE_SIZE - len,
313 "tx_op: %03X %s [%d] ",
315 bcm_proc_getifname(ifname, op->ifindex),
318 if (op->kt_ival1.tv64)
319 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
320 (long long) ktime_to_us(op->kt_ival1));
322 if (op->kt_ival2.tv64)
323 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
324 (long long) ktime_to_us(op->kt_ival2));
326 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
329 if (len > PAGE_SIZE - 100) {
330 /* mark output cut off */
331 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
336 len += snprintf(page + len, PAGE_SIZE - len, "\n");
344 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
345 * of the given bcm tx op
347 static void bcm_can_tx(struct bcm_op *op)
350 struct net_device *dev;
351 struct can_frame *cf = &op->frames[op->currframe];
353 /* no target device? => exit */
357 dev = dev_get_by_index(&init_net, op->ifindex);
359 /* RFC: should this bcm_op remove itself here? */
363 skb = alloc_skb(CFSIZ, gfp_any());
367 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
369 /* send with loopback */
374 /* update statistics */
378 /* reached last frame? */
379 if (op->currframe >= op->nframes)
386 * bcm_send_to_user - send a BCM message to the userspace
387 * (consisting of bcm_msg_head + x CAN frames)
389 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
390 struct can_frame *frames, int has_timestamp)
393 struct can_frame *firstframe;
394 struct sockaddr_can *addr;
395 struct sock *sk = op->sk;
396 int datalen = head->nframes * CFSIZ;
399 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
403 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
406 /* can_frames starting here */
407 firstframe = (struct can_frame *)skb_tail_pointer(skb);
409 memcpy(skb_put(skb, datalen), frames, datalen);
412 * the BCM uses the can_dlc-element of the can_frame
413 * structure for internal purposes. This is only
414 * relevant for updates that are generated by the
415 * BCM, where nframes is 1
417 if (head->nframes == 1)
418 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
422 /* restore rx timestamp */
423 skb->tstamp = op->rx_stamp;
427 * Put the datagram to the queue so that bcm_recvmsg() can
428 * get it from there. We need to pass the interface index to
429 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
430 * containing the interface index.
433 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
434 addr = (struct sockaddr_can *)skb->cb;
435 memset(addr, 0, sizeof(*addr));
436 addr->can_family = AF_CAN;
437 addr->can_ifindex = op->rx_ifindex;
439 err = sock_queue_rcv_skb(sk, skb);
441 struct bcm_sock *bo = bcm_sk(sk);
444 /* don't care about overflows in this statistic */
445 bo->dropped_usr_msgs++;
449 static void bcm_tx_timeout_tsklet(unsigned long data)
451 struct bcm_op *op = (struct bcm_op *)data;
452 struct bcm_msg_head msg_head;
454 if (op->kt_ival1.tv64 && (op->count > 0)) {
457 if (!op->count && (op->flags & TX_COUNTEVT)) {
459 /* create notification to user */
460 msg_head.opcode = TX_EXPIRED;
461 msg_head.flags = op->flags;
462 msg_head.count = op->count;
463 msg_head.ival1 = op->ival1;
464 msg_head.ival2 = op->ival2;
465 msg_head.can_id = op->can_id;
466 msg_head.nframes = 0;
468 bcm_send_to_user(op, &msg_head, NULL, 0);
472 if (op->kt_ival1.tv64 && (op->count > 0)) {
474 /* send (next) frame */
476 hrtimer_start(&op->timer,
477 ktime_add(ktime_get(), op->kt_ival1),
481 if (op->kt_ival2.tv64) {
483 /* send (next) frame */
485 hrtimer_start(&op->timer,
486 ktime_add(ktime_get(), op->kt_ival2),
493 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
495 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
497 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
499 tasklet_schedule(&op->tsklet);
501 return HRTIMER_NORESTART;
505 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
507 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
509 struct bcm_msg_head head;
511 /* update statistics */
512 op->frames_filtered++;
514 /* prevent statistics overflow */
515 if (op->frames_filtered > ULONG_MAX/100)
516 op->frames_filtered = op->frames_abs = 0;
518 /* this element is not throttled anymore */
519 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
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);
532 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
533 /* is part of linux/hrtimer.h since 2.6.26 */
534 static inline int hrtimer_callback_running(struct hrtimer *timer)
536 return timer->state & HRTIMER_STATE_CALLBACK;
539 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
540 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
542 return ktime_to_us(ktime_sub(later, earlier));
546 * bcm_rx_update_and_send - process a detected relevant receive content change
547 * 1. update the last received data
548 * 2. send a notification to the user (if possible)
550 static void bcm_rx_update_and_send(struct bcm_op *op,
551 struct can_frame *lastdata,
552 const struct can_frame *rxdata)
554 memcpy(lastdata, rxdata, CFSIZ);
556 /* mark as used and throttled by default */
557 lastdata->can_dlc |= (RX_RECV|RX_THR);
559 /* throtteling mode inactive ? */
560 if (!op->kt_ival2.tv64) {
561 /* send RX_CHANGED to the user immediately */
562 bcm_rx_changed(op, lastdata);
566 /* with active throttling timer we are just done here */
567 if (hrtimer_active(&op->thrtimer))
570 /* first receiption with enabled throttling mode */
571 if (!op->kt_lastmsg.tv64)
572 goto rx_changed_settime;
574 /* got a second frame inside a potential throttle period? */
575 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
576 ktime_to_us(op->kt_ival2)) {
577 /* do not send the saved data - only start throttle timer */
578 hrtimer_start(&op->thrtimer,
579 ktime_add(op->kt_lastmsg, op->kt_ival2),
584 /* the gap was that big, that throttling was not needed here */
586 bcm_rx_changed(op, lastdata);
587 op->kt_lastmsg = ktime_get();
591 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
592 * received data stored in op->last_frames[]
594 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
595 const struct can_frame *rxdata)
598 * no one uses the MSBs of can_dlc for comparation,
599 * so we use it here to detect the first time of reception
602 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
603 /* received data for the first time => send update to user */
604 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
608 /* do a real check in can_frame data section */
610 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
611 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
612 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
616 if (op->flags & RX_CHECK_DLC) {
617 /* do a real check in can_frame dlc */
618 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
620 bcm_rx_update_and_send(op, &op->last_frames[index],
628 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
630 static void bcm_rx_starttimer(struct bcm_op *op)
632 if (op->flags & RX_NO_AUTOTIMER)
635 if (op->kt_ival1.tv64)
636 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
639 static void bcm_rx_timeout_tsklet(unsigned long data)
641 struct bcm_op *op = (struct bcm_op *)data;
642 struct bcm_msg_head msg_head;
644 /* create notification to user */
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);
657 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
659 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
661 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
663 /* schedule before NET_RX_SOFTIRQ */
664 tasklet_hi_schedule(&op->tsklet);
666 /* no restart of the timer is done here! */
668 /* if user wants to be informed, when cyclic CAN-Messages come back */
669 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
670 /* clear received can_frames to indicate 'nothing received' */
671 memset(op->last_frames, 0, op->nframes * CFSIZ);
674 return HRTIMER_NORESTART;
678 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
680 static inline int bcm_rx_do_flush(struct bcm_op *op, int update, int index)
682 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
684 bcm_rx_changed(op, &op->last_frames[index]);
691 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
693 * update == 0 : just check if throttled data is available (any irq context)
694 * update == 1 : check and send throttled data to userspace (soft_irq context)
696 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
700 if (op->nframes > 1) {
703 /* for MUX filter we start at index 1 */
704 for (i = 1; i < op->nframes; i++)
705 updated += bcm_rx_do_flush(op, update, i);
708 /* for RX_FILTER_ID and simple filter */
709 updated += bcm_rx_do_flush(op, update, 0);
715 static void bcm_rx_thr_tsklet(unsigned long data)
717 struct bcm_op *op = (struct bcm_op *)data;
719 /* push the changed data to the userspace */
720 bcm_rx_thr_flush(op, 1);
724 * bcm_rx_thr_handler - the time for blocked content updates is over now:
725 * Check for throttled data and send it to the userspace
727 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
729 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
731 tasklet_schedule(&op->thrtsklet);
733 if (bcm_rx_thr_flush(op, 0)) {
734 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
735 return HRTIMER_RESTART;
737 /* rearm throttle handling */
738 op->kt_lastmsg = ktime_set(0, 0);
739 return HRTIMER_NORESTART;
744 * bcm_rx_handler - handle a CAN frame receiption
746 static void bcm_rx_handler(struct sk_buff *skb, void *data)
748 struct bcm_op *op = (struct bcm_op *)data;
749 const struct can_frame *rxframe = (struct can_frame *)skb->data;
752 /* disable timeout */
753 hrtimer_cancel(&op->timer);
755 if (op->can_id != rxframe->can_id)
758 /* save rx timestamp */
759 op->rx_stamp = skb->tstamp;
760 /* save originator for recvfrom() */
761 op->rx_ifindex = skb->dev->ifindex;
762 /* update statistics */
765 if (op->flags & RX_RTR_FRAME) {
766 /* send reply for RTR-request (placed in op->frames[0]) */
771 if (op->flags & RX_FILTER_ID) {
772 /* the easiest case */
773 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
777 if (op->nframes == 1) {
778 /* simple compare with index 0 */
779 bcm_rx_cmp_to_index(op, 0, rxframe);
783 if (op->nframes > 1) {
787 * find the first multiplex mask that fits.
788 * Remark: The MUX-mask is stored in index 0
791 for (i = 1; i < op->nframes; i++) {
792 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
793 (GET_U64(&op->frames[0]) &
794 GET_U64(&op->frames[i]))) {
795 bcm_rx_cmp_to_index(op, i, rxframe);
802 bcm_rx_starttimer(op);
806 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
808 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
813 list_for_each_entry(op, ops, list) {
814 if ((op->can_id == can_id) && (op->ifindex == ifindex))
821 static void bcm_remove_op(struct bcm_op *op)
823 hrtimer_cancel(&op->timer);
824 hrtimer_cancel(&op->thrtimer);
827 tasklet_kill(&op->tsklet);
829 if (op->thrtsklet.func)
830 tasklet_kill(&op->thrtsklet);
832 if ((op->frames) && (op->frames != &op->sframe))
835 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
836 kfree(op->last_frames);
843 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
845 if (op->rx_reg_dev == dev) {
846 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
849 /* mark as removed subscription */
850 op->rx_reg_dev = NULL;
852 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
853 "mismatch %p %p\n", op->rx_reg_dev, dev);
857 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
859 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
861 struct bcm_op *op, *n;
863 list_for_each_entry_safe(op, n, ops, list) {
864 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
867 * Don't care if we're bound or not (due to netdev
868 * problems) can_rx_unregister() is always a save
873 * Only remove subscriptions that had not
874 * been removed due to NETDEV_UNREGISTER
877 if (op->rx_reg_dev) {
878 struct net_device *dev;
880 dev = dev_get_by_index(&init_net,
883 bcm_rx_unreg(dev, op);
888 can_rx_unregister(NULL, op->can_id,
898 return 0; /* not found */
902 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
904 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
906 struct bcm_op *op, *n;
908 list_for_each_entry_safe(op, n, ops, list) {
909 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
916 return 0; /* not found */
920 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
922 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
925 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
930 /* put current values into msg_head */
931 msg_head->flags = op->flags;
932 msg_head->count = op->count;
933 msg_head->ival1 = op->ival1;
934 msg_head->ival2 = op->ival2;
935 msg_head->nframes = op->nframes;
937 bcm_send_to_user(op, msg_head, op->frames, 0);
943 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
945 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
946 int ifindex, struct sock *sk)
948 struct bcm_sock *bo = bcm_sk(sk);
952 /* we need a real device to send frames */
956 /* we need at least one can_frame */
957 if (msg_head->nframes < 1)
960 /* check the given can_id */
961 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
964 /* update existing BCM operation */
967 * Do we need more space for the can_frames than currently
968 * allocated? -> This is a _really_ unusual use-case and
969 * therefore (complexity / locking) it is not supported.
971 if (msg_head->nframes > op->nframes)
974 /* update can_frames content */
975 for (i = 0; i < msg_head->nframes; i++) {
976 err = memcpy_fromiovec((u8 *)&op->frames[i],
977 msg->msg_iov, CFSIZ);
979 if (op->frames[i].can_dlc > 8)
985 if (msg_head->flags & TX_CP_CAN_ID) {
986 /* copy can_id into frame */
987 op->frames[i].can_id = msg_head->can_id;
992 /* insert new BCM operation for the given can_id */
994 op = kzalloc(OPSIZ, GFP_KERNEL);
998 op->can_id = msg_head->can_id;
1000 /* create array for can_frames and copy the data */
1001 if (msg_head->nframes > 1) {
1002 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1009 op->frames = &op->sframe;
1011 for (i = 0; i < msg_head->nframes; i++) {
1012 err = memcpy_fromiovec((u8 *)&op->frames[i],
1013 msg->msg_iov, CFSIZ);
1015 if (op->frames[i].can_dlc > 8)
1019 if (op->frames != &op->sframe)
1025 if (msg_head->flags & TX_CP_CAN_ID) {
1026 /* copy can_id into frame */
1027 op->frames[i].can_id = msg_head->can_id;
1031 /* tx_ops never compare with previous received messages */
1032 op->last_frames = NULL;
1034 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1036 op->ifindex = ifindex;
1038 /* initialize uninitialized (kzalloc) structure */
1039 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1040 op->timer.function = bcm_tx_timeout_handler;
1042 /* initialize tasklet for tx countevent notification */
1043 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1044 (unsigned long) op);
1046 /* currently unused in tx_ops */
1047 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1049 /* add this bcm_op to the list of the tx_ops */
1050 list_add(&op->list, &bo->tx_ops);
1052 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1054 if (op->nframes != msg_head->nframes) {
1055 op->nframes = msg_head->nframes;
1056 /* start multiple frame transmission with index 0 */
1062 op->flags = msg_head->flags;
1064 if (op->flags & TX_RESET_MULTI_IDX) {
1065 /* start multiple frame transmission with index 0 */
1069 if (op->flags & SETTIMER) {
1070 /* set timer values */
1071 op->count = msg_head->count;
1072 op->ival1 = msg_head->ival1;
1073 op->ival2 = msg_head->ival2;
1074 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1075 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1077 /* disable an active timer due to zero values? */
1078 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1079 hrtimer_cancel(&op->timer);
1082 if ((op->flags & STARTTIMER) &&
1083 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1085 /* spec: send can_frame when starting timer */
1086 op->flags |= TX_ANNOUNCE;
1088 if (op->kt_ival1.tv64 && (op->count > 0)) {
1089 /* op->count-- is done in bcm_tx_timeout_handler */
1090 hrtimer_start(&op->timer, op->kt_ival1,
1093 hrtimer_start(&op->timer, op->kt_ival2,
1097 if (op->flags & TX_ANNOUNCE)
1100 return msg_head->nframes * CFSIZ + MHSIZ;
1104 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1106 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1107 int ifindex, struct sock *sk)
1109 struct bcm_sock *bo = bcm_sk(sk);
1114 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1115 /* be robust against wrong usage ... */
1116 msg_head->flags |= RX_FILTER_ID;
1117 /* ignore trailing garbage */
1118 msg_head->nframes = 0;
1121 if ((msg_head->flags & RX_RTR_FRAME) &&
1122 ((msg_head->nframes != 1) ||
1123 (!(msg_head->can_id & CAN_RTR_FLAG))))
1126 /* check the given can_id */
1127 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1129 /* update existing BCM operation */
1132 * Do we need more space for the can_frames than currently
1133 * allocated? -> This is a _really_ unusual use-case and
1134 * therefore (complexity / locking) it is not supported.
1136 if (msg_head->nframes > op->nframes)
1139 if (msg_head->nframes) {
1140 /* update can_frames content */
1141 err = memcpy_fromiovec((u8 *)op->frames,
1143 msg_head->nframes * CFSIZ);
1147 /* clear last_frames to indicate 'nothing received' */
1148 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1151 op->nframes = msg_head->nframes;
1153 /* Only an update -> do not call can_rx_register() */
1157 /* insert new BCM operation for the given can_id */
1158 op = kzalloc(OPSIZ, GFP_KERNEL);
1162 op->can_id = msg_head->can_id;
1163 op->nframes = msg_head->nframes;
1165 if (msg_head->nframes > 1) {
1166 /* create array for can_frames and copy the data */
1167 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1174 /* create and init array for received can_frames */
1175 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1177 if (!op->last_frames) {
1184 op->frames = &op->sframe;
1185 op->last_frames = &op->last_sframe;
1188 if (msg_head->nframes) {
1189 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1190 msg_head->nframes * CFSIZ);
1192 if (op->frames != &op->sframe)
1194 if (op->last_frames != &op->last_sframe)
1195 kfree(op->last_frames);
1201 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1203 op->ifindex = ifindex;
1205 /* initialize uninitialized (kzalloc) structure */
1206 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1207 op->timer.function = bcm_rx_timeout_handler;
1209 /* initialize tasklet for rx timeout notification */
1210 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1211 (unsigned long) op);
1213 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1214 op->thrtimer.function = bcm_rx_thr_handler;
1216 /* initialize tasklet for rx throttle handling */
1217 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1218 (unsigned long) op);
1220 /* add this bcm_op to the list of the rx_ops */
1221 list_add(&op->list, &bo->rx_ops);
1223 /* call can_rx_register() */
1226 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1229 op->flags = msg_head->flags;
1231 if (op->flags & RX_RTR_FRAME) {
1233 /* no timers in RTR-mode */
1234 hrtimer_cancel(&op->thrtimer);
1235 hrtimer_cancel(&op->timer);
1238 * funny feature in RX(!)_SETUP only for RTR-mode:
1239 * copy can_id into frame BUT without RTR-flag to
1240 * prevent a full-load-loopback-test ... ;-]
1242 if ((op->flags & TX_CP_CAN_ID) ||
1243 (op->frames[0].can_id == op->can_id))
1244 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1247 if (op->flags & SETTIMER) {
1249 /* set timer value */
1250 op->ival1 = msg_head->ival1;
1251 op->ival2 = msg_head->ival2;
1252 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1253 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1255 /* disable an active timer due to zero value? */
1256 if (!op->kt_ival1.tv64)
1257 hrtimer_cancel(&op->timer);
1260 * In any case cancel the throttle timer, flush
1261 * potentially blocked msgs and reset throttle handling
1263 op->kt_lastmsg = ktime_set(0, 0);
1264 hrtimer_cancel(&op->thrtimer);
1265 bcm_rx_thr_flush(op, 1);
1268 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1269 hrtimer_start(&op->timer, op->kt_ival1,
1273 /* now we can register for can_ids, if we added a new bcm_op */
1274 if (do_rx_register) {
1276 struct net_device *dev;
1278 dev = dev_get_by_index(&init_net, ifindex);
1280 err = can_rx_register(dev, op->can_id,
1281 REGMASK(op->can_id),
1285 op->rx_reg_dev = dev;
1290 err = can_rx_register(NULL, op->can_id,
1291 REGMASK(op->can_id),
1292 bcm_rx_handler, op, "bcm");
1294 /* this bcm rx op is broken -> remove it */
1295 list_del(&op->list);
1301 return msg_head->nframes * CFSIZ + MHSIZ;
1305 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1307 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1309 struct sk_buff *skb;
1310 struct net_device *dev;
1313 /* we need a real device to send frames */
1317 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1322 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1328 dev = dev_get_by_index(&init_net, ifindex);
1336 err = can_send(skb, 1); /* send with loopback */
1342 return CFSIZ + MHSIZ;
1346 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1348 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1349 struct msghdr *msg, size_t size)
1351 struct sock *sk = sock->sk;
1352 struct bcm_sock *bo = bcm_sk(sk);
1353 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1354 struct bcm_msg_head msg_head;
1355 int ret; /* read bytes or error codes as return value */
1360 /* check for valid message length from userspace */
1361 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1364 /* check for alternative ifindex for this bcm_op */
1366 if (!ifindex && msg->msg_name) {
1367 /* no bound device as default => check msg_name */
1368 struct sockaddr_can *addr =
1369 (struct sockaddr_can *)msg->msg_name;
1371 if (addr->can_family != AF_CAN)
1374 /* ifindex from sendto() */
1375 ifindex = addr->can_ifindex;
1378 struct net_device *dev;
1380 dev = dev_get_by_index(&init_net, ifindex);
1384 if (dev->type != ARPHRD_CAN) {
1393 /* read message head information */
1395 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1401 switch (msg_head.opcode) {
1404 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1408 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1412 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1419 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1426 /* reuse msg_head for the reply to TX_READ */
1427 msg_head.opcode = TX_STATUS;
1428 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1432 /* reuse msg_head for the reply to RX_READ */
1433 msg_head.opcode = RX_STATUS;
1434 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1438 /* we need exactly one can_frame behind the msg head */
1439 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1442 ret = bcm_tx_send(msg, ifindex, sk);
1456 * notification handler for netdevice status changes
1458 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1461 struct net_device *dev = (struct net_device *)data;
1462 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1463 struct sock *sk = &bo->sk;
1465 int notify_enodev = 0;
1467 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1468 if (!net_eq(dev_net(dev), &init_net))
1470 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1471 if (dev->nd_net != &init_net)
1475 if (dev->type != ARPHRD_CAN)
1480 case NETDEV_UNREGISTER:
1483 /* remove device specific receive entries */
1484 list_for_each_entry(op, &bo->rx_ops, list)
1485 if (op->rx_reg_dev == dev)
1486 bcm_rx_unreg(dev, op);
1488 /* remove device reference, if this is our bound device */
1489 if (bo->bound && bo->ifindex == dev->ifindex) {
1497 if (notify_enodev) {
1498 sk->sk_err = ENODEV;
1499 if (!sock_flag(sk, SOCK_DEAD))
1500 sk->sk_error_report(sk);
1505 if (bo->bound && bo->ifindex == dev->ifindex) {
1506 sk->sk_err = ENETDOWN;
1507 if (!sock_flag(sk, SOCK_DEAD))
1508 sk->sk_error_report(sk);
1516 * initial settings for all BCM sockets to be set at socket creation time
1518 static int bcm_init(struct sock *sk)
1520 struct bcm_sock *bo = bcm_sk(sk);
1524 bo->dropped_usr_msgs = 0;
1525 bo->bcm_proc_read = NULL;
1527 INIT_LIST_HEAD(&bo->tx_ops);
1528 INIT_LIST_HEAD(&bo->rx_ops);
1531 bo->notifier.notifier_call = bcm_notifier;
1533 register_netdevice_notifier(&bo->notifier);
1539 * standard socket functions
1541 static int bcm_release(struct socket *sock)
1543 struct sock *sk = sock->sk;
1544 struct bcm_sock *bo = bcm_sk(sk);
1545 struct bcm_op *op, *next;
1547 /* remove bcm_ops, timer, rx_unregister(), etc. */
1549 unregister_netdevice_notifier(&bo->notifier);
1553 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1556 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1558 * Don't care if we're bound or not (due to netdev problems)
1559 * can_rx_unregister() is always a save thing to do here.
1563 * Only remove subscriptions that had not
1564 * been removed due to NETDEV_UNREGISTER
1567 if (op->rx_reg_dev) {
1568 struct net_device *dev;
1570 dev = dev_get_by_index(&init_net, op->ifindex);
1572 bcm_rx_unreg(dev, op);
1577 can_rx_unregister(NULL, op->can_id,
1578 REGMASK(op->can_id),
1579 bcm_rx_handler, op);
1584 /* remove procfs entry */
1585 if (proc_dir && bo->bcm_proc_read)
1586 remove_proc_entry(bo->procname, proc_dir);
1588 /* remove device reference */
1603 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1606 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1607 struct sock *sk = sock->sk;
1608 struct bcm_sock *bo = bcm_sk(sk);
1613 /* bind a device to this socket */
1614 if (addr->can_ifindex) {
1615 struct net_device *dev;
1617 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1621 if (dev->type != ARPHRD_CAN) {
1626 bo->ifindex = dev->ifindex;
1630 /* no interface reference for ifindex = 0 ('any' CAN device) */
1637 /* unique socket address as filename */
1638 sprintf(bo->procname, "%p", sock);
1639 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1640 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1642 &bcm_proc_fops, sk);
1644 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1653 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1654 struct msghdr *msg, size_t size, int flags)
1656 struct sock *sk = sock->sk;
1657 struct sk_buff *skb;
1662 noblock = flags & MSG_DONTWAIT;
1663 flags &= ~MSG_DONTWAIT;
1664 skb = skb_recv_datagram(sk, flags, noblock, &error);
1668 if (skb->len < size)
1671 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1673 skb_free_datagram(sk, skb);
1677 sock_recv_timestamp(msg, sk, skb);
1679 if (msg->msg_name) {
1680 msg->msg_namelen = sizeof(struct sockaddr_can);
1681 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1684 skb_free_datagram(sk, skb);
1689 static struct proto_ops bcm_ops __read_mostly = {
1691 .release = bcm_release,
1692 .bind = sock_no_bind,
1693 .connect = bcm_connect,
1694 .socketpair = sock_no_socketpair,
1695 .accept = sock_no_accept,
1696 .getname = sock_no_getname,
1697 .poll = datagram_poll,
1698 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1699 .listen = sock_no_listen,
1700 .shutdown = sock_no_shutdown,
1701 .setsockopt = sock_no_setsockopt,
1702 .getsockopt = sock_no_getsockopt,
1703 .sendmsg = bcm_sendmsg,
1704 .recvmsg = bcm_recvmsg,
1705 .mmap = sock_no_mmap,
1706 .sendpage = sock_no_sendpage,
1709 static struct proto bcm_proto __read_mostly = {
1711 .owner = THIS_MODULE,
1712 .obj_size = sizeof(struct bcm_sock),
1716 static struct can_proto bcm_can_proto __read_mostly = {
1718 .protocol = CAN_BCM,
1724 static int __init bcm_module_init(void)
1730 err = can_proto_register(&bcm_can_proto);
1732 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1736 /* create /proc/net/can-bcm directory */
1737 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1738 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1740 proc_dir = proc_mkdir("can-bcm", proc_net);
1743 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
1745 proc_dir->owner = THIS_MODULE;
1751 static void __exit bcm_module_exit(void)
1753 can_proto_unregister(&bcm_can_proto);
1756 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1757 proc_net_remove(&init_net, "can-bcm");
1759 proc_net_remove("can-bcm");
1763 module_init(bcm_module_init);
1764 module_exit(bcm_module_exit);