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 */
74 * To send multiple CAN frame content within TX_SETUP or to filter
75 * CAN messages with multiplex index within RX_SETUP, the number of
76 * different filters is limited to 256 due to the one byte index value.
78 #define MAX_NFRAMES 256
80 /* use of last_frames[index].can_dlc */
81 #define RX_RECV 0x40 /* received data for this element */
82 #define RX_THR 0x80 /* element not been sent due to throttle feature */
83 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
85 /* get best masking value for can_rx_register() for a given single can_id */
86 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
87 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
88 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
90 #define CAN_BCM_VERSION CAN_VERSION
91 static __initdata const char banner[] = KERN_INFO
92 "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
94 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
95 MODULE_LICENSE("Dual BSD/GPL");
96 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
97 MODULE_ALIAS("can-proto-2");
99 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
100 #error This code only supports Kernel versions 2.6.22+
101 #error For older 2.6 Kernels please use bcm-prior-2-6-22.c instead of bcm.c
104 /* easy access to can_frame payload */
105 static inline u64 GET_U64(const struct can_frame *cp)
107 return *(u64 *)cp->data;
111 struct list_head list;
115 unsigned long frames_abs, frames_filtered;
116 struct timeval ival1, ival2;
117 struct hrtimer timer, thrtimer;
118 struct tasklet_struct tsklet, thrtsklet;
119 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
124 struct can_frame *frames;
125 struct can_frame *last_frames;
126 struct can_frame sframe;
127 struct can_frame last_sframe;
129 struct net_device *rx_reg_dev;
132 static struct proc_dir_entry *proc_dir;
138 struct notifier_block notifier;
139 struct list_head rx_ops;
140 struct list_head tx_ops;
141 unsigned long dropped_usr_msgs;
142 struct proc_dir_entry *bcm_proc_read;
143 char procname [32]; /* inode number in decimal with \0 */
146 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
148 return (struct bcm_sock *)sk;
151 #define CFSIZ sizeof(struct can_frame)
152 #define OPSIZ sizeof(struct bcm_op)
153 #define MHSIZ sizeof(struct bcm_msg_head)
158 static char *bcm_proc_getifname(char *result, int ifindex)
160 struct net_device *dev;
165 read_lock(&dev_base_lock);
166 dev = __dev_get_by_index(&init_net, ifindex);
168 strcpy(result, dev->name);
170 strcpy(result, "???");
171 read_unlock(&dev_base_lock);
176 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
177 static int bcm_proc_show(struct seq_file *m, void *v)
179 char ifname[IFNAMSIZ];
180 struct sock *sk = (struct sock *)m->private;
181 struct bcm_sock *bo = bcm_sk(sk);
184 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
185 seq_printf(m, ">>> socket %p", sk->sk_socket);
186 seq_printf(m, " / sk %p", sk);
187 seq_printf(m, " / bo %p", bo);
189 seq_printf(m, ">>> socket %pK", sk->sk_socket);
190 seq_printf(m, " / sk %pK", sk);
191 seq_printf(m, " / bo %pK", bo);
193 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
194 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
195 seq_printf(m, " <<<\n");
197 list_for_each_entry(op, &bo->rx_ops, list) {
199 unsigned long reduction;
201 /* print only active entries & prevent division by zero */
205 seq_printf(m, "rx_op: %03X %-5s ",
206 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
207 seq_printf(m, "[%u]%c ", op->nframes,
208 (op->flags & RX_CHECK_DLC)?'d':' ');
209 if (op->kt_ival1.tv64)
210 seq_printf(m, "timeo=%lld ",
212 ktime_to_us(op->kt_ival1));
214 if (op->kt_ival2.tv64)
215 seq_printf(m, "thr=%lld ",
217 ktime_to_us(op->kt_ival2));
219 seq_printf(m, "# recv %ld (%ld) => reduction: ",
220 op->frames_filtered, op->frames_abs);
222 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
224 seq_printf(m, "%s%ld%%\n",
225 (reduction == 100)?"near ":"", reduction);
228 list_for_each_entry(op, &bo->tx_ops, list) {
230 seq_printf(m, "tx_op: %03X %s [%u] ",
232 bcm_proc_getifname(ifname, op->ifindex),
235 if (op->kt_ival1.tv64)
236 seq_printf(m, "t1=%lld ",
237 (long long) ktime_to_us(op->kt_ival1));
239 if (op->kt_ival2.tv64)
240 seq_printf(m, "t2=%lld ",
241 (long long) ktime_to_us(op->kt_ival2));
243 seq_printf(m, "# sent %ld\n", op->frames_abs);
249 static int bcm_proc_open(struct inode *inode, struct file *file)
251 return single_open(file, bcm_proc_show, PDE(inode)->data);
254 static const struct file_operations bcm_proc_fops = {
255 .owner = THIS_MODULE,
256 .open = bcm_proc_open,
259 .release = single_release,
262 static int bcm_read_proc(char *page, char **start, off_t off,
263 int count, int *eof, void *data)
265 char ifname[IFNAMSIZ];
267 struct sock *sk = (struct sock *)data;
268 struct bcm_sock *bo = bcm_sk(sk);
271 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
273 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
274 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
275 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
276 bo->dropped_usr_msgs);
277 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
278 bcm_proc_getifname(ifname, bo->ifindex));
279 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
281 list_for_each_entry(op, &bo->rx_ops, list) {
283 unsigned long reduction;
285 /* print only active entries & prevent division by zero */
289 len += snprintf(page + len, PAGE_SIZE - len,
291 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
292 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
294 (op->flags & RX_CHECK_DLC)?'d':' ');
295 if (op->kt_ival1.tv64)
296 len += snprintf(page + len, PAGE_SIZE - len,
299 ktime_to_us(op->kt_ival1));
301 if (op->kt_ival2.tv64)
302 len += snprintf(page + len, PAGE_SIZE - len,
305 ktime_to_us(op->kt_ival2));
307 len += snprintf(page + len, PAGE_SIZE - len,
308 "# recv %ld (%ld) => reduction: ",
309 op->frames_filtered, op->frames_abs);
311 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
313 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
314 (reduction == 100)?"near ":"", reduction);
316 if (len > PAGE_SIZE - 200) {
317 /* mark output cut off */
318 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
323 list_for_each_entry(op, &bo->tx_ops, list) {
325 len += snprintf(page + len, PAGE_SIZE - len,
326 "tx_op: %03X %s [%d] ",
328 bcm_proc_getifname(ifname, op->ifindex),
331 if (op->kt_ival1.tv64)
332 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
333 (long long) ktime_to_us(op->kt_ival1));
335 if (op->kt_ival2.tv64)
336 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
337 (long long) ktime_to_us(op->kt_ival2));
339 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
342 if (len > PAGE_SIZE - 100) {
343 /* mark output cut off */
344 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
349 len += snprintf(page + len, PAGE_SIZE - len, "\n");
357 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
358 * of the given bcm tx op
360 static void bcm_can_tx(struct bcm_op *op)
363 struct net_device *dev;
364 struct can_frame *cf = &op->frames[op->currframe];
366 /* no target device? => exit */
370 dev = dev_get_by_index(&init_net, op->ifindex);
372 /* RFC: should this bcm_op remove itself here? */
376 skb = alloc_skb(CFSIZ, gfp_any());
380 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
382 /* send with loopback */
387 /* update statistics */
391 /* reached last frame? */
392 if (op->currframe >= op->nframes)
399 * bcm_send_to_user - send a BCM message to the userspace
400 * (consisting of bcm_msg_head + x CAN frames)
402 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
403 struct can_frame *frames, int has_timestamp)
406 struct can_frame *firstframe;
407 struct sockaddr_can *addr;
408 struct sock *sk = op->sk;
409 unsigned int datalen = head->nframes * CFSIZ;
412 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
416 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
419 /* can_frames starting here */
420 firstframe = (struct can_frame *)skb_tail_pointer(skb);
422 memcpy(skb_put(skb, datalen), frames, datalen);
425 * the BCM uses the can_dlc-element of the can_frame
426 * structure for internal purposes. This is only
427 * relevant for updates that are generated by the
428 * BCM, where nframes is 1
430 if (head->nframes == 1)
431 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
435 /* restore rx timestamp */
436 skb->tstamp = op->rx_stamp;
440 * Put the datagram to the queue so that bcm_recvmsg() can
441 * get it from there. We need to pass the interface index to
442 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
443 * containing the interface index.
446 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
447 addr = (struct sockaddr_can *)skb->cb;
448 memset(addr, 0, sizeof(*addr));
449 addr->can_family = AF_CAN;
450 addr->can_ifindex = op->rx_ifindex;
452 err = sock_queue_rcv_skb(sk, skb);
454 struct bcm_sock *bo = bcm_sk(sk);
457 /* don't care about overflows in this statistic */
458 bo->dropped_usr_msgs++;
462 static void bcm_tx_timeout_tsklet(unsigned long data)
464 struct bcm_op *op = (struct bcm_op *)data;
465 struct bcm_msg_head msg_head;
467 if (op->kt_ival1.tv64 && (op->count > 0)) {
470 if (!op->count && (op->flags & TX_COUNTEVT)) {
472 /* create notification to user */
473 msg_head.opcode = TX_EXPIRED;
474 msg_head.flags = op->flags;
475 msg_head.count = op->count;
476 msg_head.ival1 = op->ival1;
477 msg_head.ival2 = op->ival2;
478 msg_head.can_id = op->can_id;
479 msg_head.nframes = 0;
481 bcm_send_to_user(op, &msg_head, NULL, 0);
485 if (op->kt_ival1.tv64 && (op->count > 0)) {
487 /* send (next) frame */
489 hrtimer_start(&op->timer,
490 ktime_add(ktime_get(), op->kt_ival1),
494 if (op->kt_ival2.tv64) {
496 /* send (next) frame */
498 hrtimer_start(&op->timer,
499 ktime_add(ktime_get(), op->kt_ival2),
506 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
508 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
510 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
512 tasklet_schedule(&op->tsklet);
514 return HRTIMER_NORESTART;
518 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
520 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
522 struct bcm_msg_head head;
524 /* update statistics */
525 op->frames_filtered++;
527 /* prevent statistics overflow */
528 if (op->frames_filtered > ULONG_MAX/100)
529 op->frames_filtered = op->frames_abs = 0;
531 /* this element is not throttled anymore */
532 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
534 head.opcode = RX_CHANGED;
535 head.flags = op->flags;
536 head.count = op->count;
537 head.ival1 = op->ival1;
538 head.ival2 = op->ival2;
539 head.can_id = op->can_id;
542 bcm_send_to_user(op, &head, data, 1);
545 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
546 /* is part of linux/hrtimer.h since 2.6.26 */
547 static inline int hrtimer_callback_running(struct hrtimer *timer)
549 return timer->state & HRTIMER_STATE_CALLBACK;
552 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
553 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
555 return ktime_to_us(ktime_sub(later, earlier));
559 * bcm_rx_update_and_send - process a detected relevant receive content change
560 * 1. update the last received data
561 * 2. send a notification to the user (if possible)
563 static void bcm_rx_update_and_send(struct bcm_op *op,
564 struct can_frame *lastdata,
565 const struct can_frame *rxdata)
567 memcpy(lastdata, rxdata, CFSIZ);
569 /* mark as used and throttled by default */
570 lastdata->can_dlc |= (RX_RECV|RX_THR);
572 /* throtteling mode inactive ? */
573 if (!op->kt_ival2.tv64) {
574 /* send RX_CHANGED to the user immediately */
575 bcm_rx_changed(op, lastdata);
579 /* with active throttling timer we are just done here */
580 if (hrtimer_active(&op->thrtimer))
583 /* first receiption with enabled throttling mode */
584 if (!op->kt_lastmsg.tv64)
585 goto rx_changed_settime;
587 /* got a second frame inside a potential throttle period? */
588 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
589 ktime_to_us(op->kt_ival2)) {
590 /* do not send the saved data - only start throttle timer */
591 hrtimer_start(&op->thrtimer,
592 ktime_add(op->kt_lastmsg, op->kt_ival2),
597 /* the gap was that big, that throttling was not needed here */
599 bcm_rx_changed(op, lastdata);
600 op->kt_lastmsg = ktime_get();
604 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
605 * received data stored in op->last_frames[]
607 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
608 const struct can_frame *rxdata)
611 * no one uses the MSBs of can_dlc for comparation,
612 * so we use it here to detect the first time of reception
615 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
616 /* received data for the first time => send update to user */
617 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
621 /* do a real check in can_frame data section */
623 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
624 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
625 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
629 if (op->flags & RX_CHECK_DLC) {
630 /* do a real check in can_frame dlc */
631 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
633 bcm_rx_update_and_send(op, &op->last_frames[index],
641 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
643 static void bcm_rx_starttimer(struct bcm_op *op)
645 if (op->flags & RX_NO_AUTOTIMER)
648 if (op->kt_ival1.tv64)
649 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
652 static void bcm_rx_timeout_tsklet(unsigned long data)
654 struct bcm_op *op = (struct bcm_op *)data;
655 struct bcm_msg_head msg_head;
657 /* create notification to user */
658 msg_head.opcode = RX_TIMEOUT;
659 msg_head.flags = op->flags;
660 msg_head.count = op->count;
661 msg_head.ival1 = op->ival1;
662 msg_head.ival2 = op->ival2;
663 msg_head.can_id = op->can_id;
664 msg_head.nframes = 0;
666 bcm_send_to_user(op, &msg_head, NULL, 0);
670 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
672 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
674 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
676 /* schedule before NET_RX_SOFTIRQ */
677 tasklet_hi_schedule(&op->tsklet);
679 /* no restart of the timer is done here! */
681 /* if user wants to be informed, when cyclic CAN-Messages come back */
682 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
683 /* clear received can_frames to indicate 'nothing received' */
684 memset(op->last_frames, 0, op->nframes * CFSIZ);
687 return HRTIMER_NORESTART;
691 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
693 static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
696 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
698 bcm_rx_changed(op, &op->last_frames[index]);
705 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
707 * update == 0 : just check if throttled data is available (any irq context)
708 * update == 1 : check and send throttled data to userspace (soft_irq context)
710 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
714 if (op->nframes > 1) {
717 /* for MUX filter we start at index 1 */
718 for (i = 1; i < op->nframes; i++)
719 updated += bcm_rx_do_flush(op, update, i);
722 /* for RX_FILTER_ID and simple filter */
723 updated += bcm_rx_do_flush(op, update, 0);
729 static void bcm_rx_thr_tsklet(unsigned long data)
731 struct bcm_op *op = (struct bcm_op *)data;
733 /* push the changed data to the userspace */
734 bcm_rx_thr_flush(op, 1);
738 * bcm_rx_thr_handler - the time for blocked content updates is over now:
739 * Check for throttled data and send it to the userspace
741 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
743 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
745 tasklet_schedule(&op->thrtsklet);
747 if (bcm_rx_thr_flush(op, 0)) {
748 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
749 return HRTIMER_RESTART;
751 /* rearm throttle handling */
752 op->kt_lastmsg = ktime_set(0, 0);
753 return HRTIMER_NORESTART;
758 * bcm_rx_handler - handle a CAN frame receiption
760 static void bcm_rx_handler(struct sk_buff *skb, void *data)
762 struct bcm_op *op = (struct bcm_op *)data;
763 const struct can_frame *rxframe = (struct can_frame *)skb->data;
766 /* disable timeout */
767 hrtimer_cancel(&op->timer);
769 if (op->can_id != rxframe->can_id)
772 /* save rx timestamp */
773 op->rx_stamp = skb->tstamp;
774 /* save originator for recvfrom() */
775 op->rx_ifindex = skb->dev->ifindex;
776 /* update statistics */
779 if (op->flags & RX_RTR_FRAME) {
780 /* send reply for RTR-request (placed in op->frames[0]) */
785 if (op->flags & RX_FILTER_ID) {
786 /* the easiest case */
787 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
791 if (op->nframes == 1) {
792 /* simple compare with index 0 */
793 bcm_rx_cmp_to_index(op, 0, rxframe);
797 if (op->nframes > 1) {
801 * find the first multiplex mask that fits.
802 * Remark: The MUX-mask is stored in index 0
805 for (i = 1; i < op->nframes; i++) {
806 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
807 (GET_U64(&op->frames[0]) &
808 GET_U64(&op->frames[i]))) {
809 bcm_rx_cmp_to_index(op, i, rxframe);
816 bcm_rx_starttimer(op);
820 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
822 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
827 list_for_each_entry(op, ops, list) {
828 if ((op->can_id == can_id) && (op->ifindex == ifindex))
835 static void bcm_remove_op(struct bcm_op *op)
837 hrtimer_cancel(&op->timer);
838 hrtimer_cancel(&op->thrtimer);
841 tasklet_kill(&op->tsklet);
843 if (op->thrtsklet.func)
844 tasklet_kill(&op->thrtsklet);
846 if ((op->frames) && (op->frames != &op->sframe))
849 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
850 kfree(op->last_frames);
857 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
859 if (op->rx_reg_dev == dev) {
860 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
863 /* mark as removed subscription */
864 op->rx_reg_dev = NULL;
866 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
867 "mismatch %p %p\n", op->rx_reg_dev, dev);
871 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
873 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
875 struct bcm_op *op, *n;
877 list_for_each_entry_safe(op, n, ops, list) {
878 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
881 * Don't care if we're bound or not (due to netdev
882 * problems) can_rx_unregister() is always a save
887 * Only remove subscriptions that had not
888 * been removed due to NETDEV_UNREGISTER
891 if (op->rx_reg_dev) {
892 struct net_device *dev;
894 dev = dev_get_by_index(&init_net,
897 bcm_rx_unreg(dev, op);
902 can_rx_unregister(NULL, op->can_id,
912 return 0; /* not found */
916 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
918 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
920 struct bcm_op *op, *n;
922 list_for_each_entry_safe(op, n, ops, list) {
923 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
930 return 0; /* not found */
934 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
936 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
939 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
944 /* put current values into msg_head */
945 msg_head->flags = op->flags;
946 msg_head->count = op->count;
947 msg_head->ival1 = op->ival1;
948 msg_head->ival2 = op->ival2;
949 msg_head->nframes = op->nframes;
951 bcm_send_to_user(op, msg_head, op->frames, 0);
957 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
959 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
960 int ifindex, struct sock *sk)
962 struct bcm_sock *bo = bcm_sk(sk);
967 /* we need a real device to send frames */
971 /* check nframes boundaries - we need at least one can_frame */
972 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
975 /* check the given can_id */
976 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
979 /* update existing BCM operation */
982 * Do we need more space for the can_frames than currently
983 * allocated? -> This is a _really_ unusual use-case and
984 * therefore (complexity / locking) it is not supported.
986 if (msg_head->nframes > op->nframes)
989 /* update can_frames content */
990 for (i = 0; i < msg_head->nframes; i++) {
991 err = memcpy_fromiovec((u8 *)&op->frames[i],
992 msg->msg_iov, CFSIZ);
994 if (op->frames[i].can_dlc > 8)
1000 if (msg_head->flags & TX_CP_CAN_ID) {
1001 /* copy can_id into frame */
1002 op->frames[i].can_id = msg_head->can_id;
1007 /* insert new BCM operation for the given can_id */
1009 op = kzalloc(OPSIZ, GFP_KERNEL);
1013 op->can_id = msg_head->can_id;
1015 /* create array for can_frames and copy the data */
1016 if (msg_head->nframes > 1) {
1017 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1024 op->frames = &op->sframe;
1026 for (i = 0; i < msg_head->nframes; i++) {
1027 err = memcpy_fromiovec((u8 *)&op->frames[i],
1028 msg->msg_iov, CFSIZ);
1030 if (op->frames[i].can_dlc > 8)
1034 if (op->frames != &op->sframe)
1040 if (msg_head->flags & TX_CP_CAN_ID) {
1041 /* copy can_id into frame */
1042 op->frames[i].can_id = msg_head->can_id;
1046 /* tx_ops never compare with previous received messages */
1047 op->last_frames = NULL;
1049 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1051 op->ifindex = ifindex;
1053 /* initialize uninitialized (kzalloc) structure */
1054 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1055 op->timer.function = bcm_tx_timeout_handler;
1057 /* initialize tasklet for tx countevent notification */
1058 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1059 (unsigned long) op);
1061 /* currently unused in tx_ops */
1062 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1064 /* add this bcm_op to the list of the tx_ops */
1065 list_add(&op->list, &bo->tx_ops);
1067 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1069 if (op->nframes != msg_head->nframes) {
1070 op->nframes = msg_head->nframes;
1071 /* start multiple frame transmission with index 0 */
1077 op->flags = msg_head->flags;
1079 if (op->flags & TX_RESET_MULTI_IDX) {
1080 /* start multiple frame transmission with index 0 */
1084 if (op->flags & SETTIMER) {
1085 /* set timer values */
1086 op->count = msg_head->count;
1087 op->ival1 = msg_head->ival1;
1088 op->ival2 = msg_head->ival2;
1089 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1090 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1092 /* disable an active timer due to zero values? */
1093 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1094 hrtimer_cancel(&op->timer);
1097 if ((op->flags & STARTTIMER) &&
1098 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1100 /* spec: send can_frame when starting timer */
1101 op->flags |= TX_ANNOUNCE;
1103 if (op->kt_ival1.tv64 && (op->count > 0)) {
1104 /* op->count-- is done in bcm_tx_timeout_handler */
1105 hrtimer_start(&op->timer, op->kt_ival1,
1108 hrtimer_start(&op->timer, op->kt_ival2,
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 /* ignore trailing garbage */
1133 msg_head->nframes = 0;
1136 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1137 if (msg_head->nframes > MAX_NFRAMES + 1)
1140 if ((msg_head->flags & RX_RTR_FRAME) &&
1141 ((msg_head->nframes != 1) ||
1142 (!(msg_head->can_id & CAN_RTR_FLAG))))
1145 /* check the given can_id */
1146 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1148 /* update existing BCM operation */
1151 * Do we need more space for the can_frames than currently
1152 * allocated? -> This is a _really_ unusual use-case and
1153 * therefore (complexity / locking) it is not supported.
1155 if (msg_head->nframes > op->nframes)
1158 if (msg_head->nframes) {
1159 /* update can_frames content */
1160 err = memcpy_fromiovec((u8 *)op->frames,
1162 msg_head->nframes * CFSIZ);
1166 /* clear last_frames to indicate 'nothing received' */
1167 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1170 op->nframes = msg_head->nframes;
1172 /* Only an update -> do not call can_rx_register() */
1176 /* insert new BCM operation for the given can_id */
1177 op = kzalloc(OPSIZ, GFP_KERNEL);
1181 op->can_id = msg_head->can_id;
1182 op->nframes = msg_head->nframes;
1184 if (msg_head->nframes > 1) {
1185 /* create array for can_frames and copy the data */
1186 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1193 /* create and init array for received can_frames */
1194 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1196 if (!op->last_frames) {
1203 op->frames = &op->sframe;
1204 op->last_frames = &op->last_sframe;
1207 if (msg_head->nframes) {
1208 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1209 msg_head->nframes * CFSIZ);
1211 if (op->frames != &op->sframe)
1213 if (op->last_frames != &op->last_sframe)
1214 kfree(op->last_frames);
1220 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1222 op->ifindex = ifindex;
1224 /* initialize uninitialized (kzalloc) structure */
1225 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1226 op->timer.function = bcm_rx_timeout_handler;
1228 /* initialize tasklet for rx timeout notification */
1229 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1230 (unsigned long) op);
1232 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1233 op->thrtimer.function = bcm_rx_thr_handler;
1235 /* initialize tasklet for rx throttle handling */
1236 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1237 (unsigned long) op);
1239 /* add this bcm_op to the list of the rx_ops */
1240 list_add(&op->list, &bo->rx_ops);
1242 /* call can_rx_register() */
1245 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1248 op->flags = msg_head->flags;
1250 if (op->flags & RX_RTR_FRAME) {
1252 /* no timers in RTR-mode */
1253 hrtimer_cancel(&op->thrtimer);
1254 hrtimer_cancel(&op->timer);
1257 * funny feature in RX(!)_SETUP only for RTR-mode:
1258 * copy can_id into frame BUT without RTR-flag to
1259 * prevent a full-load-loopback-test ... ;-]
1261 if ((op->flags & TX_CP_CAN_ID) ||
1262 (op->frames[0].can_id == op->can_id))
1263 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1266 if (op->flags & SETTIMER) {
1268 /* set timer value */
1269 op->ival1 = msg_head->ival1;
1270 op->ival2 = msg_head->ival2;
1271 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1272 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1274 /* disable an active timer due to zero value? */
1275 if (!op->kt_ival1.tv64)
1276 hrtimer_cancel(&op->timer);
1279 * In any case cancel the throttle timer, flush
1280 * potentially blocked msgs and reset throttle handling
1282 op->kt_lastmsg = ktime_set(0, 0);
1283 hrtimer_cancel(&op->thrtimer);
1284 bcm_rx_thr_flush(op, 1);
1287 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1288 hrtimer_start(&op->timer, op->kt_ival1,
1292 /* now we can register for can_ids, if we added a new bcm_op */
1293 if (do_rx_register) {
1295 struct net_device *dev;
1297 dev = dev_get_by_index(&init_net, ifindex);
1299 err = can_rx_register(dev, op->can_id,
1300 REGMASK(op->can_id),
1304 op->rx_reg_dev = dev;
1309 err = can_rx_register(NULL, op->can_id,
1310 REGMASK(op->can_id),
1311 bcm_rx_handler, op, "bcm");
1313 /* this bcm rx op is broken -> remove it */
1314 list_del(&op->list);
1320 return msg_head->nframes * CFSIZ + MHSIZ;
1324 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1326 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1328 struct sk_buff *skb;
1329 struct net_device *dev;
1332 /* we need a real device to send frames */
1336 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1341 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1347 dev = dev_get_by_index(&init_net, ifindex);
1355 err = can_send(skb, 1); /* send with loopback */
1361 return CFSIZ + MHSIZ;
1365 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1367 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1368 struct msghdr *msg, size_t size)
1370 struct sock *sk = sock->sk;
1371 struct bcm_sock *bo = bcm_sk(sk);
1372 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1373 struct bcm_msg_head msg_head;
1374 int ret; /* read bytes or error codes as return value */
1379 /* check for valid message length from userspace */
1380 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1383 /* check for alternative ifindex for this bcm_op */
1385 if (!ifindex && msg->msg_name) {
1386 /* no bound device as default => check msg_name */
1387 struct sockaddr_can *addr =
1388 (struct sockaddr_can *)msg->msg_name;
1390 if (msg->msg_namelen < sizeof(*addr))
1393 if (addr->can_family != AF_CAN)
1396 /* ifindex from sendto() */
1397 ifindex = addr->can_ifindex;
1400 struct net_device *dev;
1402 dev = dev_get_by_index(&init_net, ifindex);
1406 if (dev->type != ARPHRD_CAN) {
1415 /* read message head information */
1417 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1423 switch (msg_head.opcode) {
1426 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1430 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1434 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1441 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1448 /* reuse msg_head for the reply to TX_READ */
1449 msg_head.opcode = TX_STATUS;
1450 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1454 /* reuse msg_head for the reply to RX_READ */
1455 msg_head.opcode = RX_STATUS;
1456 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1460 /* we need exactly one can_frame behind the msg head */
1461 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1464 ret = bcm_tx_send(msg, ifindex, sk);
1478 * notification handler for netdevice status changes
1480 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1483 struct net_device *dev = (struct net_device *)data;
1484 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1485 struct sock *sk = &bo->sk;
1487 int notify_enodev = 0;
1489 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1490 if (!net_eq(dev_net(dev), &init_net))
1492 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1493 if (dev->nd_net != &init_net)
1497 if (dev->type != ARPHRD_CAN)
1502 case NETDEV_UNREGISTER:
1505 /* remove device specific receive entries */
1506 list_for_each_entry(op, &bo->rx_ops, list)
1507 if (op->rx_reg_dev == dev)
1508 bcm_rx_unreg(dev, op);
1510 /* remove device reference, if this is our bound device */
1511 if (bo->bound && bo->ifindex == dev->ifindex) {
1519 if (notify_enodev) {
1520 sk->sk_err = ENODEV;
1521 if (!sock_flag(sk, SOCK_DEAD))
1522 sk->sk_error_report(sk);
1527 if (bo->bound && bo->ifindex == dev->ifindex) {
1528 sk->sk_err = ENETDOWN;
1529 if (!sock_flag(sk, SOCK_DEAD))
1530 sk->sk_error_report(sk);
1538 * initial settings for all BCM sockets to be set at socket creation time
1540 static int bcm_init(struct sock *sk)
1542 struct bcm_sock *bo = bcm_sk(sk);
1546 bo->dropped_usr_msgs = 0;
1547 bo->bcm_proc_read = NULL;
1549 INIT_LIST_HEAD(&bo->tx_ops);
1550 INIT_LIST_HEAD(&bo->rx_ops);
1553 bo->notifier.notifier_call = bcm_notifier;
1555 register_netdevice_notifier(&bo->notifier);
1561 * standard socket functions
1563 static int bcm_release(struct socket *sock)
1565 struct sock *sk = sock->sk;
1566 struct bcm_sock *bo;
1567 struct bcm_op *op, *next;
1574 /* remove bcm_ops, timer, rx_unregister(), etc. */
1576 unregister_netdevice_notifier(&bo->notifier);
1580 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1583 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1585 * Don't care if we're bound or not (due to netdev problems)
1586 * can_rx_unregister() is always a save thing to do here.
1590 * Only remove subscriptions that had not
1591 * been removed due to NETDEV_UNREGISTER
1594 if (op->rx_reg_dev) {
1595 struct net_device *dev;
1597 dev = dev_get_by_index(&init_net, op->ifindex);
1599 bcm_rx_unreg(dev, op);
1604 can_rx_unregister(NULL, op->can_id,
1605 REGMASK(op->can_id),
1606 bcm_rx_handler, op);
1611 /* remove procfs entry */
1612 if (proc_dir && bo->bcm_proc_read)
1613 remove_proc_entry(bo->procname, proc_dir);
1615 /* remove device reference */
1630 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1633 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1634 struct sock *sk = sock->sk;
1635 struct bcm_sock *bo = bcm_sk(sk);
1637 if (len < sizeof(*addr))
1643 /* bind a device to this socket */
1644 if (addr->can_ifindex) {
1645 struct net_device *dev;
1647 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1651 if (dev->type != ARPHRD_CAN) {
1656 bo->ifindex = dev->ifindex;
1660 /* no interface reference for ifindex = 0 ('any' CAN device) */
1667 /* unique socket address as filename */
1668 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1669 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1670 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1672 &bcm_proc_fops, sk);
1674 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1683 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1684 struct msghdr *msg, size_t size, int flags)
1686 struct sock *sk = sock->sk;
1687 struct sk_buff *skb;
1692 noblock = flags & MSG_DONTWAIT;
1693 flags &= ~MSG_DONTWAIT;
1694 skb = skb_recv_datagram(sk, flags, noblock, &error);
1698 if (skb->len < size)
1701 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1703 skb_free_datagram(sk, skb);
1707 sock_recv_timestamp(msg, sk, skb);
1709 if (msg->msg_name) {
1710 msg->msg_namelen = sizeof(struct sockaddr_can);
1711 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1714 skb_free_datagram(sk, skb);
1719 static const struct proto_ops bcm_ops = {
1721 .release = bcm_release,
1722 .bind = sock_no_bind,
1723 .connect = bcm_connect,
1724 .socketpair = sock_no_socketpair,
1725 .accept = sock_no_accept,
1726 .getname = sock_no_getname,
1727 .poll = datagram_poll,
1728 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1729 .listen = sock_no_listen,
1730 .shutdown = sock_no_shutdown,
1731 .setsockopt = sock_no_setsockopt,
1732 .getsockopt = sock_no_getsockopt,
1733 .sendmsg = bcm_sendmsg,
1734 .recvmsg = bcm_recvmsg,
1735 .mmap = sock_no_mmap,
1736 .sendpage = sock_no_sendpage,
1739 static struct proto bcm_proto __read_mostly = {
1741 .owner = THIS_MODULE,
1742 .obj_size = sizeof(struct bcm_sock),
1746 static const struct can_proto bcm_can_proto = {
1748 .protocol = CAN_BCM,
1749 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
1756 static int __init bcm_module_init(void)
1762 err = can_proto_register(&bcm_can_proto);
1764 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1768 /* create /proc/net/can-bcm directory */
1769 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1770 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1772 proc_dir = proc_mkdir("can-bcm", proc_net);
1775 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
1777 proc_dir->owner = THIS_MODULE;
1783 static void __exit bcm_module_exit(void)
1785 can_proto_unregister(&bcm_can_proto);
1788 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1789 proc_net_remove(&init_net, "can-bcm");
1791 proc_net_remove("can-bcm");
1795 module_init(bcm_module_init);
1796 module_exit(bcm_module_exit);