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 seq_printf(m, ">>> socket %p", sk->sk_socket);
185 seq_printf(m, " / sk %p", sk);
186 seq_printf(m, " / bo %p", bo);
187 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
188 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
189 seq_printf(m, " <<<\n");
191 list_for_each_entry(op, &bo->rx_ops, list) {
193 unsigned long reduction;
195 /* print only active entries & prevent division by zero */
199 seq_printf(m, "rx_op: %03X %-5s ",
200 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
201 seq_printf(m, "[%u]%c ", op->nframes,
202 (op->flags & RX_CHECK_DLC)?'d':' ');
203 if (op->kt_ival1.tv64)
204 seq_printf(m, "timeo=%lld ",
206 ktime_to_us(op->kt_ival1));
208 if (op->kt_ival2.tv64)
209 seq_printf(m, "thr=%lld ",
211 ktime_to_us(op->kt_ival2));
213 seq_printf(m, "# recv %ld (%ld) => reduction: ",
214 op->frames_filtered, op->frames_abs);
216 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
218 seq_printf(m, "%s%ld%%\n",
219 (reduction == 100)?"near ":"", reduction);
222 list_for_each_entry(op, &bo->tx_ops, list) {
224 seq_printf(m, "tx_op: %03X %s [%u] ",
226 bcm_proc_getifname(ifname, op->ifindex),
229 if (op->kt_ival1.tv64)
230 seq_printf(m, "t1=%lld ",
231 (long long) ktime_to_us(op->kt_ival1));
233 if (op->kt_ival2.tv64)
234 seq_printf(m, "t2=%lld ",
235 (long long) ktime_to_us(op->kt_ival2));
237 seq_printf(m, "# sent %ld\n", op->frames_abs);
243 static int bcm_proc_open(struct inode *inode, struct file *file)
245 return single_open(file, bcm_proc_show, PDE(inode)->data);
248 static const struct file_operations bcm_proc_fops = {
249 .owner = THIS_MODULE,
250 .open = bcm_proc_open,
253 .release = single_release,
256 static int bcm_read_proc(char *page, char **start, off_t off,
257 int count, int *eof, void *data)
259 char ifname[IFNAMSIZ];
261 struct sock *sk = (struct sock *)data;
262 struct bcm_sock *bo = bcm_sk(sk);
265 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
267 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
268 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
269 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
270 bo->dropped_usr_msgs);
271 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
272 bcm_proc_getifname(ifname, bo->ifindex));
273 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
275 list_for_each_entry(op, &bo->rx_ops, list) {
277 unsigned long reduction;
279 /* print only active entries & prevent division by zero */
283 len += snprintf(page + len, PAGE_SIZE - len,
285 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
286 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
288 (op->flags & RX_CHECK_DLC)?'d':' ');
289 if (op->kt_ival1.tv64)
290 len += snprintf(page + len, PAGE_SIZE - len,
293 ktime_to_us(op->kt_ival1));
295 if (op->kt_ival2.tv64)
296 len += snprintf(page + len, PAGE_SIZE - len,
299 ktime_to_us(op->kt_ival2));
301 len += snprintf(page + len, PAGE_SIZE - len,
302 "# recv %ld (%ld) => reduction: ",
303 op->frames_filtered, op->frames_abs);
305 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
307 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
308 (reduction == 100)?"near ":"", reduction);
310 if (len > PAGE_SIZE - 200) {
311 /* mark output cut off */
312 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
317 list_for_each_entry(op, &bo->tx_ops, list) {
319 len += snprintf(page + len, PAGE_SIZE - len,
320 "tx_op: %03X %s [%d] ",
322 bcm_proc_getifname(ifname, op->ifindex),
325 if (op->kt_ival1.tv64)
326 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
327 (long long) ktime_to_us(op->kt_ival1));
329 if (op->kt_ival2.tv64)
330 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
331 (long long) ktime_to_us(op->kt_ival2));
333 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
336 if (len > PAGE_SIZE - 100) {
337 /* mark output cut off */
338 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
343 len += snprintf(page + len, PAGE_SIZE - len, "\n");
351 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
352 * of the given bcm tx op
354 static void bcm_can_tx(struct bcm_op *op)
357 struct net_device *dev;
358 struct can_frame *cf = &op->frames[op->currframe];
360 /* no target device? => exit */
364 dev = dev_get_by_index(&init_net, op->ifindex);
366 /* RFC: should this bcm_op remove itself here? */
370 skb = alloc_skb(CFSIZ, gfp_any());
374 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
376 /* send with loopback */
381 /* update statistics */
385 /* reached last frame? */
386 if (op->currframe >= op->nframes)
393 * bcm_send_to_user - send a BCM message to the userspace
394 * (consisting of bcm_msg_head + x CAN frames)
396 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
397 struct can_frame *frames, int has_timestamp)
400 struct can_frame *firstframe;
401 struct sockaddr_can *addr;
402 struct sock *sk = op->sk;
403 unsigned int datalen = head->nframes * CFSIZ;
406 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
410 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
413 /* can_frames starting here */
414 firstframe = (struct can_frame *)skb_tail_pointer(skb);
416 memcpy(skb_put(skb, datalen), frames, datalen);
419 * the BCM uses the can_dlc-element of the can_frame
420 * structure for internal purposes. This is only
421 * relevant for updates that are generated by the
422 * BCM, where nframes is 1
424 if (head->nframes == 1)
425 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
429 /* restore rx timestamp */
430 skb->tstamp = op->rx_stamp;
434 * Put the datagram to the queue so that bcm_recvmsg() can
435 * get it from there. We need to pass the interface index to
436 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
437 * containing the interface index.
440 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
441 addr = (struct sockaddr_can *)skb->cb;
442 memset(addr, 0, sizeof(*addr));
443 addr->can_family = AF_CAN;
444 addr->can_ifindex = op->rx_ifindex;
446 err = sock_queue_rcv_skb(sk, skb);
448 struct bcm_sock *bo = bcm_sk(sk);
451 /* don't care about overflows in this statistic */
452 bo->dropped_usr_msgs++;
456 static void bcm_tx_timeout_tsklet(unsigned long data)
458 struct bcm_op *op = (struct bcm_op *)data;
459 struct bcm_msg_head msg_head;
461 if (op->kt_ival1.tv64 && (op->count > 0)) {
464 if (!op->count && (op->flags & TX_COUNTEVT)) {
466 /* create notification to user */
467 msg_head.opcode = TX_EXPIRED;
468 msg_head.flags = op->flags;
469 msg_head.count = op->count;
470 msg_head.ival1 = op->ival1;
471 msg_head.ival2 = op->ival2;
472 msg_head.can_id = op->can_id;
473 msg_head.nframes = 0;
475 bcm_send_to_user(op, &msg_head, NULL, 0);
479 if (op->kt_ival1.tv64 && (op->count > 0)) {
481 /* send (next) frame */
483 hrtimer_start(&op->timer,
484 ktime_add(ktime_get(), op->kt_ival1),
488 if (op->kt_ival2.tv64) {
490 /* send (next) frame */
492 hrtimer_start(&op->timer,
493 ktime_add(ktime_get(), op->kt_ival2),
500 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
502 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
504 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
506 tasklet_schedule(&op->tsklet);
508 return HRTIMER_NORESTART;
512 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
514 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
516 struct bcm_msg_head head;
518 /* update statistics */
519 op->frames_filtered++;
521 /* prevent statistics overflow */
522 if (op->frames_filtered > ULONG_MAX/100)
523 op->frames_filtered = op->frames_abs = 0;
525 /* this element is not throttled anymore */
526 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
528 head.opcode = RX_CHANGED;
529 head.flags = op->flags;
530 head.count = op->count;
531 head.ival1 = op->ival1;
532 head.ival2 = op->ival2;
533 head.can_id = op->can_id;
536 bcm_send_to_user(op, &head, data, 1);
539 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
540 /* is part of linux/hrtimer.h since 2.6.26 */
541 static inline int hrtimer_callback_running(struct hrtimer *timer)
543 return timer->state & HRTIMER_STATE_CALLBACK;
546 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
547 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
549 return ktime_to_us(ktime_sub(later, earlier));
553 * bcm_rx_update_and_send - process a detected relevant receive content change
554 * 1. update the last received data
555 * 2. send a notification to the user (if possible)
557 static void bcm_rx_update_and_send(struct bcm_op *op,
558 struct can_frame *lastdata,
559 const struct can_frame *rxdata)
561 memcpy(lastdata, rxdata, CFSIZ);
563 /* mark as used and throttled by default */
564 lastdata->can_dlc |= (RX_RECV|RX_THR);
566 /* throtteling mode inactive ? */
567 if (!op->kt_ival2.tv64) {
568 /* send RX_CHANGED to the user immediately */
569 bcm_rx_changed(op, lastdata);
573 /* with active throttling timer we are just done here */
574 if (hrtimer_active(&op->thrtimer))
577 /* first receiption with enabled throttling mode */
578 if (!op->kt_lastmsg.tv64)
579 goto rx_changed_settime;
581 /* got a second frame inside a potential throttle period? */
582 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
583 ktime_to_us(op->kt_ival2)) {
584 /* do not send the saved data - only start throttle timer */
585 hrtimer_start(&op->thrtimer,
586 ktime_add(op->kt_lastmsg, op->kt_ival2),
591 /* the gap was that big, that throttling was not needed here */
593 bcm_rx_changed(op, lastdata);
594 op->kt_lastmsg = ktime_get();
598 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
599 * received data stored in op->last_frames[]
601 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
602 const struct can_frame *rxdata)
605 * no one uses the MSBs of can_dlc for comparation,
606 * so we use it here to detect the first time of reception
609 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
610 /* received data for the first time => send update to user */
611 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
615 /* do a real check in can_frame data section */
617 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
618 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
619 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
623 if (op->flags & RX_CHECK_DLC) {
624 /* do a real check in can_frame dlc */
625 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
627 bcm_rx_update_and_send(op, &op->last_frames[index],
635 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
637 static void bcm_rx_starttimer(struct bcm_op *op)
639 if (op->flags & RX_NO_AUTOTIMER)
642 if (op->kt_ival1.tv64)
643 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
646 static void bcm_rx_timeout_tsklet(unsigned long data)
648 struct bcm_op *op = (struct bcm_op *)data;
649 struct bcm_msg_head msg_head;
651 /* create notification to user */
652 msg_head.opcode = RX_TIMEOUT;
653 msg_head.flags = op->flags;
654 msg_head.count = op->count;
655 msg_head.ival1 = op->ival1;
656 msg_head.ival2 = op->ival2;
657 msg_head.can_id = op->can_id;
658 msg_head.nframes = 0;
660 bcm_send_to_user(op, &msg_head, NULL, 0);
664 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
666 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
668 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
670 /* schedule before NET_RX_SOFTIRQ */
671 tasklet_hi_schedule(&op->tsklet);
673 /* no restart of the timer is done here! */
675 /* if user wants to be informed, when cyclic CAN-Messages come back */
676 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
677 /* clear received can_frames to indicate 'nothing received' */
678 memset(op->last_frames, 0, op->nframes * CFSIZ);
681 return HRTIMER_NORESTART;
685 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
687 static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
690 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
692 bcm_rx_changed(op, &op->last_frames[index]);
699 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
701 * update == 0 : just check if throttled data is available (any irq context)
702 * update == 1 : check and send throttled data to userspace (soft_irq context)
704 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
708 if (op->nframes > 1) {
711 /* for MUX filter we start at index 1 */
712 for (i = 1; i < op->nframes; i++)
713 updated += bcm_rx_do_flush(op, update, i);
716 /* for RX_FILTER_ID and simple filter */
717 updated += bcm_rx_do_flush(op, update, 0);
723 static void bcm_rx_thr_tsklet(unsigned long data)
725 struct bcm_op *op = (struct bcm_op *)data;
727 /* push the changed data to the userspace */
728 bcm_rx_thr_flush(op, 1);
732 * bcm_rx_thr_handler - the time for blocked content updates is over now:
733 * Check for throttled data and send it to the userspace
735 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
737 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
739 tasklet_schedule(&op->thrtsklet);
741 if (bcm_rx_thr_flush(op, 0)) {
742 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
743 return HRTIMER_RESTART;
745 /* rearm throttle handling */
746 op->kt_lastmsg = ktime_set(0, 0);
747 return HRTIMER_NORESTART;
752 * bcm_rx_handler - handle a CAN frame receiption
754 static void bcm_rx_handler(struct sk_buff *skb, void *data)
756 struct bcm_op *op = (struct bcm_op *)data;
757 const struct can_frame *rxframe = (struct can_frame *)skb->data;
760 /* disable timeout */
761 hrtimer_cancel(&op->timer);
763 if (op->can_id != rxframe->can_id)
766 /* save rx timestamp */
767 op->rx_stamp = skb->tstamp;
768 /* save originator for recvfrom() */
769 op->rx_ifindex = skb->dev->ifindex;
770 /* update statistics */
773 if (op->flags & RX_RTR_FRAME) {
774 /* send reply for RTR-request (placed in op->frames[0]) */
779 if (op->flags & RX_FILTER_ID) {
780 /* the easiest case */
781 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
785 if (op->nframes == 1) {
786 /* simple compare with index 0 */
787 bcm_rx_cmp_to_index(op, 0, rxframe);
791 if (op->nframes > 1) {
795 * find the first multiplex mask that fits.
796 * Remark: The MUX-mask is stored in index 0
799 for (i = 1; i < op->nframes; i++) {
800 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
801 (GET_U64(&op->frames[0]) &
802 GET_U64(&op->frames[i]))) {
803 bcm_rx_cmp_to_index(op, i, rxframe);
810 bcm_rx_starttimer(op);
814 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
816 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
821 list_for_each_entry(op, ops, list) {
822 if ((op->can_id == can_id) && (op->ifindex == ifindex))
829 static void bcm_remove_op(struct bcm_op *op)
831 hrtimer_cancel(&op->timer);
832 hrtimer_cancel(&op->thrtimer);
835 tasklet_kill(&op->tsklet);
837 if (op->thrtsklet.func)
838 tasklet_kill(&op->thrtsklet);
840 if ((op->frames) && (op->frames != &op->sframe))
843 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
844 kfree(op->last_frames);
851 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
853 if (op->rx_reg_dev == dev) {
854 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
857 /* mark as removed subscription */
858 op->rx_reg_dev = NULL;
860 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
861 "mismatch %p %p\n", op->rx_reg_dev, dev);
865 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
867 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
869 struct bcm_op *op, *n;
871 list_for_each_entry_safe(op, n, ops, list) {
872 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
875 * Don't care if we're bound or not (due to netdev
876 * problems) can_rx_unregister() is always a save
881 * Only remove subscriptions that had not
882 * been removed due to NETDEV_UNREGISTER
885 if (op->rx_reg_dev) {
886 struct net_device *dev;
888 dev = dev_get_by_index(&init_net,
891 bcm_rx_unreg(dev, op);
896 can_rx_unregister(NULL, op->can_id,
906 return 0; /* not found */
910 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
912 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
914 struct bcm_op *op, *n;
916 list_for_each_entry_safe(op, n, ops, list) {
917 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
924 return 0; /* not found */
928 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
930 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
933 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
938 /* put current values into msg_head */
939 msg_head->flags = op->flags;
940 msg_head->count = op->count;
941 msg_head->ival1 = op->ival1;
942 msg_head->ival2 = op->ival2;
943 msg_head->nframes = op->nframes;
945 bcm_send_to_user(op, msg_head, op->frames, 0);
951 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
953 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
954 int ifindex, struct sock *sk)
956 struct bcm_sock *bo = bcm_sk(sk);
961 /* we need a real device to send frames */
965 /* check nframes boundaries - we need at least one can_frame */
966 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
969 /* check the given can_id */
970 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
973 /* update existing BCM operation */
976 * Do we need more space for the can_frames than currently
977 * allocated? -> This is a _really_ unusual use-case and
978 * therefore (complexity / locking) it is not supported.
980 if (msg_head->nframes > op->nframes)
983 /* update can_frames content */
984 for (i = 0; i < msg_head->nframes; i++) {
985 err = memcpy_fromiovec((u8 *)&op->frames[i],
986 msg->msg_iov, CFSIZ);
988 if (op->frames[i].can_dlc > 8)
994 if (msg_head->flags & TX_CP_CAN_ID) {
995 /* copy can_id into frame */
996 op->frames[i].can_id = msg_head->can_id;
1001 /* insert new BCM operation for the given can_id */
1003 op = kzalloc(OPSIZ, GFP_KERNEL);
1007 op->can_id = msg_head->can_id;
1009 /* create array for can_frames and copy the data */
1010 if (msg_head->nframes > 1) {
1011 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1018 op->frames = &op->sframe;
1020 for (i = 0; i < msg_head->nframes; i++) {
1021 err = memcpy_fromiovec((u8 *)&op->frames[i],
1022 msg->msg_iov, CFSIZ);
1024 if (op->frames[i].can_dlc > 8)
1028 if (op->frames != &op->sframe)
1034 if (msg_head->flags & TX_CP_CAN_ID) {
1035 /* copy can_id into frame */
1036 op->frames[i].can_id = msg_head->can_id;
1040 /* tx_ops never compare with previous received messages */
1041 op->last_frames = NULL;
1043 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1045 op->ifindex = ifindex;
1047 /* initialize uninitialized (kzalloc) structure */
1048 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1049 op->timer.function = bcm_tx_timeout_handler;
1051 /* initialize tasklet for tx countevent notification */
1052 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1053 (unsigned long) op);
1055 /* currently unused in tx_ops */
1056 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1058 /* add this bcm_op to the list of the tx_ops */
1059 list_add(&op->list, &bo->tx_ops);
1061 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1063 if (op->nframes != msg_head->nframes) {
1064 op->nframes = msg_head->nframes;
1065 /* start multiple frame transmission with index 0 */
1071 op->flags = msg_head->flags;
1073 if (op->flags & TX_RESET_MULTI_IDX) {
1074 /* start multiple frame transmission with index 0 */
1078 if (op->flags & SETTIMER) {
1079 /* set timer values */
1080 op->count = msg_head->count;
1081 op->ival1 = msg_head->ival1;
1082 op->ival2 = msg_head->ival2;
1083 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1084 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1086 /* disable an active timer due to zero values? */
1087 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1088 hrtimer_cancel(&op->timer);
1091 if ((op->flags & STARTTIMER) &&
1092 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1094 /* spec: send can_frame when starting timer */
1095 op->flags |= TX_ANNOUNCE;
1097 if (op->kt_ival1.tv64 && (op->count > 0)) {
1098 /* op->count-- is done in bcm_tx_timeout_handler */
1099 hrtimer_start(&op->timer, op->kt_ival1,
1102 hrtimer_start(&op->timer, op->kt_ival2,
1106 if (op->flags & TX_ANNOUNCE)
1109 return msg_head->nframes * CFSIZ + MHSIZ;
1113 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1115 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1116 int ifindex, struct sock *sk)
1118 struct bcm_sock *bo = bcm_sk(sk);
1123 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1124 /* be robust against wrong usage ... */
1125 msg_head->flags |= RX_FILTER_ID;
1126 /* ignore trailing garbage */
1127 msg_head->nframes = 0;
1130 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1131 if (msg_head->nframes > MAX_NFRAMES + 1)
1134 if ((msg_head->flags & RX_RTR_FRAME) &&
1135 ((msg_head->nframes != 1) ||
1136 (!(msg_head->can_id & CAN_RTR_FLAG))))
1139 /* check the given can_id */
1140 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1142 /* update existing BCM operation */
1145 * Do we need more space for the can_frames than currently
1146 * allocated? -> This is a _really_ unusual use-case and
1147 * therefore (complexity / locking) it is not supported.
1149 if (msg_head->nframes > op->nframes)
1152 if (msg_head->nframes) {
1153 /* update can_frames content */
1154 err = memcpy_fromiovec((u8 *)op->frames,
1156 msg_head->nframes * CFSIZ);
1160 /* clear last_frames to indicate 'nothing received' */
1161 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1164 op->nframes = msg_head->nframes;
1166 /* Only an update -> do not call can_rx_register() */
1170 /* insert new BCM operation for the given can_id */
1171 op = kzalloc(OPSIZ, GFP_KERNEL);
1175 op->can_id = msg_head->can_id;
1176 op->nframes = msg_head->nframes;
1178 if (msg_head->nframes > 1) {
1179 /* create array for can_frames and copy the data */
1180 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1187 /* create and init array for received can_frames */
1188 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1190 if (!op->last_frames) {
1197 op->frames = &op->sframe;
1198 op->last_frames = &op->last_sframe;
1201 if (msg_head->nframes) {
1202 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1203 msg_head->nframes * CFSIZ);
1205 if (op->frames != &op->sframe)
1207 if (op->last_frames != &op->last_sframe)
1208 kfree(op->last_frames);
1214 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1216 op->ifindex = ifindex;
1218 /* initialize uninitialized (kzalloc) structure */
1219 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1220 op->timer.function = bcm_rx_timeout_handler;
1222 /* initialize tasklet for rx timeout notification */
1223 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1224 (unsigned long) op);
1226 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1227 op->thrtimer.function = bcm_rx_thr_handler;
1229 /* initialize tasklet for rx throttle handling */
1230 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1231 (unsigned long) op);
1233 /* add this bcm_op to the list of the rx_ops */
1234 list_add(&op->list, &bo->rx_ops);
1236 /* call can_rx_register() */
1239 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1242 op->flags = msg_head->flags;
1244 if (op->flags & RX_RTR_FRAME) {
1246 /* no timers in RTR-mode */
1247 hrtimer_cancel(&op->thrtimer);
1248 hrtimer_cancel(&op->timer);
1251 * funny feature in RX(!)_SETUP only for RTR-mode:
1252 * copy can_id into frame BUT without RTR-flag to
1253 * prevent a full-load-loopback-test ... ;-]
1255 if ((op->flags & TX_CP_CAN_ID) ||
1256 (op->frames[0].can_id == op->can_id))
1257 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1260 if (op->flags & SETTIMER) {
1262 /* set timer value */
1263 op->ival1 = msg_head->ival1;
1264 op->ival2 = msg_head->ival2;
1265 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1266 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1268 /* disable an active timer due to zero value? */
1269 if (!op->kt_ival1.tv64)
1270 hrtimer_cancel(&op->timer);
1273 * In any case cancel the throttle timer, flush
1274 * potentially blocked msgs and reset throttle handling
1276 op->kt_lastmsg = ktime_set(0, 0);
1277 hrtimer_cancel(&op->thrtimer);
1278 bcm_rx_thr_flush(op, 1);
1281 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1282 hrtimer_start(&op->timer, op->kt_ival1,
1286 /* now we can register for can_ids, if we added a new bcm_op */
1287 if (do_rx_register) {
1289 struct net_device *dev;
1291 dev = dev_get_by_index(&init_net, ifindex);
1293 err = can_rx_register(dev, op->can_id,
1294 REGMASK(op->can_id),
1298 op->rx_reg_dev = dev;
1303 err = can_rx_register(NULL, op->can_id,
1304 REGMASK(op->can_id),
1305 bcm_rx_handler, op, "bcm");
1307 /* this bcm rx op is broken -> remove it */
1308 list_del(&op->list);
1314 return msg_head->nframes * CFSIZ + MHSIZ;
1318 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1320 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1322 struct sk_buff *skb;
1323 struct net_device *dev;
1326 /* we need a real device to send frames */
1330 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1335 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1341 dev = dev_get_by_index(&init_net, ifindex);
1349 err = can_send(skb, 1); /* send with loopback */
1355 return CFSIZ + MHSIZ;
1359 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1361 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1362 struct msghdr *msg, size_t size)
1364 struct sock *sk = sock->sk;
1365 struct bcm_sock *bo = bcm_sk(sk);
1366 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1367 struct bcm_msg_head msg_head;
1368 int ret; /* read bytes or error codes as return value */
1373 /* check for valid message length from userspace */
1374 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1377 /* check for alternative ifindex for this bcm_op */
1379 if (!ifindex && msg->msg_name) {
1380 /* no bound device as default => check msg_name */
1381 struct sockaddr_can *addr =
1382 (struct sockaddr_can *)msg->msg_name;
1384 if (msg->msg_namelen < sizeof(*addr))
1387 if (addr->can_family != AF_CAN)
1390 /* ifindex from sendto() */
1391 ifindex = addr->can_ifindex;
1394 struct net_device *dev;
1396 dev = dev_get_by_index(&init_net, ifindex);
1400 if (dev->type != ARPHRD_CAN) {
1409 /* read message head information */
1411 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1417 switch (msg_head.opcode) {
1420 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1424 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1428 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1435 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1442 /* reuse msg_head for the reply to TX_READ */
1443 msg_head.opcode = TX_STATUS;
1444 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1448 /* reuse msg_head for the reply to RX_READ */
1449 msg_head.opcode = RX_STATUS;
1450 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1454 /* we need exactly one can_frame behind the msg head */
1455 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1458 ret = bcm_tx_send(msg, ifindex, sk);
1472 * notification handler for netdevice status changes
1474 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1477 struct net_device *dev = (struct net_device *)data;
1478 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1479 struct sock *sk = &bo->sk;
1481 int notify_enodev = 0;
1483 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1484 if (!net_eq(dev_net(dev), &init_net))
1486 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1487 if (dev->nd_net != &init_net)
1491 if (dev->type != ARPHRD_CAN)
1496 case NETDEV_UNREGISTER:
1499 /* remove device specific receive entries */
1500 list_for_each_entry(op, &bo->rx_ops, list)
1501 if (op->rx_reg_dev == dev)
1502 bcm_rx_unreg(dev, op);
1504 /* remove device reference, if this is our bound device */
1505 if (bo->bound && bo->ifindex == dev->ifindex) {
1513 if (notify_enodev) {
1514 sk->sk_err = ENODEV;
1515 if (!sock_flag(sk, SOCK_DEAD))
1516 sk->sk_error_report(sk);
1521 if (bo->bound && bo->ifindex == dev->ifindex) {
1522 sk->sk_err = ENETDOWN;
1523 if (!sock_flag(sk, SOCK_DEAD))
1524 sk->sk_error_report(sk);
1532 * initial settings for all BCM sockets to be set at socket creation time
1534 static int bcm_init(struct sock *sk)
1536 struct bcm_sock *bo = bcm_sk(sk);
1540 bo->dropped_usr_msgs = 0;
1541 bo->bcm_proc_read = NULL;
1543 INIT_LIST_HEAD(&bo->tx_ops);
1544 INIT_LIST_HEAD(&bo->rx_ops);
1547 bo->notifier.notifier_call = bcm_notifier;
1549 register_netdevice_notifier(&bo->notifier);
1555 * standard socket functions
1557 static int bcm_release(struct socket *sock)
1559 struct sock *sk = sock->sk;
1560 struct bcm_sock *bo;
1561 struct bcm_op *op, *next;
1568 /* remove bcm_ops, timer, rx_unregister(), etc. */
1570 unregister_netdevice_notifier(&bo->notifier);
1574 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1577 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1579 * Don't care if we're bound or not (due to netdev problems)
1580 * can_rx_unregister() is always a save thing to do here.
1584 * Only remove subscriptions that had not
1585 * been removed due to NETDEV_UNREGISTER
1588 if (op->rx_reg_dev) {
1589 struct net_device *dev;
1591 dev = dev_get_by_index(&init_net, op->ifindex);
1593 bcm_rx_unreg(dev, op);
1598 can_rx_unregister(NULL, op->can_id,
1599 REGMASK(op->can_id),
1600 bcm_rx_handler, op);
1605 /* remove procfs entry */
1606 if (proc_dir && bo->bcm_proc_read)
1607 remove_proc_entry(bo->procname, proc_dir);
1609 /* remove device reference */
1624 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1627 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1628 struct sock *sk = sock->sk;
1629 struct bcm_sock *bo = bcm_sk(sk);
1631 if (len < sizeof(*addr))
1637 /* bind a device to this socket */
1638 if (addr->can_ifindex) {
1639 struct net_device *dev;
1641 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1645 if (dev->type != ARPHRD_CAN) {
1650 bo->ifindex = dev->ifindex;
1654 /* no interface reference for ifindex = 0 ('any' CAN device) */
1661 /* unique socket address as filename */
1662 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1663 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1664 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1666 &bcm_proc_fops, sk);
1668 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1677 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1678 struct msghdr *msg, size_t size, int flags)
1680 struct sock *sk = sock->sk;
1681 struct sk_buff *skb;
1686 noblock = flags & MSG_DONTWAIT;
1687 flags &= ~MSG_DONTWAIT;
1688 skb = skb_recv_datagram(sk, flags, noblock, &error);
1692 if (skb->len < size)
1695 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1697 skb_free_datagram(sk, skb);
1701 sock_recv_timestamp(msg, sk, skb);
1703 if (msg->msg_name) {
1704 msg->msg_namelen = sizeof(struct sockaddr_can);
1705 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1708 skb_free_datagram(sk, skb);
1713 static const struct proto_ops bcm_ops = {
1715 .release = bcm_release,
1716 .bind = sock_no_bind,
1717 .connect = bcm_connect,
1718 .socketpair = sock_no_socketpair,
1719 .accept = sock_no_accept,
1720 .getname = sock_no_getname,
1721 .poll = datagram_poll,
1722 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1723 .listen = sock_no_listen,
1724 .shutdown = sock_no_shutdown,
1725 .setsockopt = sock_no_setsockopt,
1726 .getsockopt = sock_no_getsockopt,
1727 .sendmsg = bcm_sendmsg,
1728 .recvmsg = bcm_recvmsg,
1729 .mmap = sock_no_mmap,
1730 .sendpage = sock_no_sendpage,
1733 static struct proto bcm_proto __read_mostly = {
1735 .owner = THIS_MODULE,
1736 .obj_size = sizeof(struct bcm_sock),
1740 static const struct can_proto bcm_can_proto = {
1742 .protocol = CAN_BCM,
1743 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
1750 static int __init bcm_module_init(void)
1756 err = can_proto_register(&bcm_can_proto);
1758 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1762 /* create /proc/net/can-bcm directory */
1763 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1764 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1766 proc_dir = proc_mkdir("can-bcm", proc_net);
1769 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
1771 proc_dir->owner = THIS_MODULE;
1777 static void __exit bcm_module_exit(void)
1779 can_proto_unregister(&bcm_can_proto);
1782 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1783 proc_net_remove(&init_net, "can-bcm");
1785 proc_net_remove("can-bcm");
1789 module_init(bcm_module_init);
1790 module_exit(bcm_module_exit);