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 #include <linux/uio.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/socket.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <linux/can.h>
57 #include <linux/can/core.h>
58 #include <linux/can/bcm.h>
60 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
61 #include <net/net_namespace.h>
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
67 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
70 /* use of last_frames[index].can_dlc */
71 #define RX_RECV 0x40 /* received data for this element */
72 #define RX_THR 0x80 /* element not been sent due to throttle feature */
73 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
75 /* get best masking value for can_rx_register() for a given single can_id */
76 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
77 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
78 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
80 #define CAN_BCM_VERSION CAN_VERSION
81 static __initdata const char banner[] = KERN_INFO
82 "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
84 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
85 MODULE_LICENSE("Dual BSD/GPL");
86 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
88 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
89 #error This code only supports Kernel versions 2.6.22+
90 #error For older 2.6 Kernels please use bcm-prior-2-6-22.c instead of bcm.c
93 /* easy access to can_frame payload */
94 static inline u64 GET_U64(const struct can_frame *cp)
96 return *(u64 *)cp->data;
100 struct list_head list;
104 unsigned long frames_abs, frames_filtered;
105 struct timeval ival1, ival2;
106 struct hrtimer timer, thrtimer;
107 struct tasklet_struct tsklet, thrtsklet;
108 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
113 struct can_frame *frames;
114 struct can_frame *last_frames;
115 struct can_frame sframe;
116 struct can_frame last_sframe;
118 struct net_device *rx_reg_dev;
121 static struct proc_dir_entry *proc_dir;
127 struct notifier_block notifier;
128 struct list_head rx_ops;
129 struct list_head tx_ops;
130 unsigned long dropped_usr_msgs;
131 struct proc_dir_entry *bcm_proc_read;
132 char procname [9]; /* pointer printed in ASCII with \0 */
135 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
137 return (struct bcm_sock *)sk;
140 #define CFSIZ sizeof(struct can_frame)
141 #define OPSIZ sizeof(struct bcm_op)
142 #define MHSIZ sizeof(struct bcm_msg_head)
147 static char *bcm_proc_getifname(int ifindex)
149 struct net_device *dev;
154 /* no usage counting */
155 dev = __dev_get_by_index(&init_net, ifindex);
162 static int bcm_read_proc(char *page, char **start, off_t off,
163 int count, int *eof, void *data)
166 struct sock *sk = (struct sock *)data;
167 struct bcm_sock *bo = bcm_sk(sk);
170 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
172 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
173 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
174 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
175 bo->dropped_usr_msgs);
176 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
177 bcm_proc_getifname(bo->ifindex));
178 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
180 list_for_each_entry(op, &bo->rx_ops, list) {
182 unsigned long reduction;
184 /* print only active entries & prevent division by zero */
188 len += snprintf(page + len, PAGE_SIZE - len,
190 op->can_id, bcm_proc_getifname(op->ifindex));
191 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
193 (op->flags & RX_CHECK_DLC)?'d':' ');
194 if (op->kt_ival1.tv64)
195 len += snprintf(page + len, PAGE_SIZE - len,
198 ktime_to_us(op->kt_ival1));
200 if (op->kt_ival2.tv64)
201 len += snprintf(page + len, PAGE_SIZE - len,
204 ktime_to_us(op->kt_ival2));
206 len += snprintf(page + len, PAGE_SIZE - len,
207 "# recv %ld (%ld) => reduction: ",
208 op->frames_filtered, op->frames_abs);
210 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
212 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
213 (reduction == 100)?"near ":"", reduction);
215 if (len > PAGE_SIZE - 200) {
216 /* mark output cut off */
217 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
222 list_for_each_entry(op, &bo->tx_ops, list) {
224 len += snprintf(page + len, PAGE_SIZE - len,
225 "tx_op: %03X %s [%d] ",
226 op->can_id, bcm_proc_getifname(op->ifindex),
229 if (op->kt_ival1.tv64)
230 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
231 (long long) ktime_to_us(op->kt_ival1));
233 if (op->kt_ival2.tv64)
234 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
235 (long long) ktime_to_us(op->kt_ival2));
237 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
240 if (len > PAGE_SIZE - 100) {
241 /* mark output cut off */
242 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
247 len += snprintf(page + len, PAGE_SIZE - len, "\n");
254 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
255 * of the given bcm tx op
257 static void bcm_can_tx(struct bcm_op *op)
260 struct net_device *dev;
261 struct can_frame *cf = &op->frames[op->currframe];
263 /* no target device? => exit */
267 dev = dev_get_by_index(&init_net, op->ifindex);
269 /* RFC: should this bcm_op remove itself here? */
273 skb = alloc_skb(CFSIZ, gfp_any());
277 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
279 /* send with loopback */
284 /* update statistics */
288 /* reached last frame? */
289 if (op->currframe >= op->nframes)
296 * bcm_send_to_user - send a BCM message to the userspace
297 * (consisting of bcm_msg_head + x CAN frames)
299 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
300 struct can_frame *frames, int has_timestamp)
303 struct can_frame *firstframe;
304 struct sockaddr_can *addr;
305 struct sock *sk = op->sk;
306 int datalen = head->nframes * CFSIZ;
309 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
313 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
316 /* can_frames starting here */
317 firstframe = (struct can_frame *)skb_tail_pointer(skb);
319 memcpy(skb_put(skb, datalen), frames, datalen);
322 * the BCM uses the can_dlc-element of the can_frame
323 * structure for internal purposes. This is only
324 * relevant for updates that are generated by the
325 * BCM, where nframes is 1
327 if (head->nframes == 1)
328 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
332 /* restore rx timestamp */
333 skb->tstamp = op->rx_stamp;
337 * Put the datagram to the queue so that bcm_recvmsg() can
338 * get it from there. We need to pass the interface index to
339 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
340 * containing the interface index.
343 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
344 addr = (struct sockaddr_can *)skb->cb;
345 memset(addr, 0, sizeof(*addr));
346 addr->can_family = AF_CAN;
347 addr->can_ifindex = op->rx_ifindex;
349 err = sock_queue_rcv_skb(sk, skb);
351 struct bcm_sock *bo = bcm_sk(sk);
354 /* don't care about overflows in this statistic */
355 bo->dropped_usr_msgs++;
359 static void bcm_tx_timeout_tsklet(unsigned long data)
361 struct bcm_op *op = (struct bcm_op *)data;
362 struct bcm_msg_head msg_head;
364 /* create notification to user */
365 msg_head.opcode = TX_EXPIRED;
366 msg_head.flags = op->flags;
367 msg_head.count = op->count;
368 msg_head.ival1 = op->ival1;
369 msg_head.ival2 = op->ival2;
370 msg_head.can_id = op->can_id;
371 msg_head.nframes = 0;
373 bcm_send_to_user(op, &msg_head, NULL, 0);
377 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
379 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
381 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
382 enum hrtimer_restart ret = HRTIMER_NORESTART;
384 if (op->kt_ival1.tv64 && (op->count > 0)) {
387 if (!op->count && (op->flags & TX_COUNTEVT))
388 tasklet_schedule(&op->tsklet);
391 if (op->kt_ival1.tv64 && (op->count > 0)) {
393 /* send (next) frame */
395 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
396 ret = HRTIMER_RESTART;
399 if (op->kt_ival2.tv64) {
401 /* send (next) frame */
403 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
404 ret = HRTIMER_RESTART;
412 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
414 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
416 struct bcm_msg_head head;
418 /* update statistics */
419 op->frames_filtered++;
421 /* prevent statistics overflow */
422 if (op->frames_filtered > ULONG_MAX/100)
423 op->frames_filtered = op->frames_abs = 0;
425 /* this element is not throttled anymore */
426 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
428 head.opcode = RX_CHANGED;
429 head.flags = op->flags;
430 head.count = op->count;
431 head.ival1 = op->ival1;
432 head.ival2 = op->ival2;
433 head.can_id = op->can_id;
436 bcm_send_to_user(op, &head, data, 1);
439 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
440 /* is part of linux/hrtimer.h since 2.6.26 */
441 static inline int hrtimer_callback_running(struct hrtimer *timer)
443 return timer->state & HRTIMER_STATE_CALLBACK;
446 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
447 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
449 return ktime_to_us(ktime_sub(later, earlier));
453 * bcm_rx_update_and_send - process a detected relevant receive content change
454 * 1. update the last received data
455 * 2. send a notification to the user (if possible)
457 static void bcm_rx_update_and_send(struct bcm_op *op,
458 struct can_frame *lastdata,
459 const struct can_frame *rxdata)
461 memcpy(lastdata, rxdata, CFSIZ);
463 /* mark as used and throttled by default */
464 lastdata->can_dlc |= (RX_RECV|RX_THR);
466 /* throtteling mode inactive ? */
467 if (!op->kt_ival2.tv64) {
468 /* send RX_CHANGED to the user immediately */
469 bcm_rx_changed(op, lastdata);
473 /* with active throttling timer we are just done here */
474 if (hrtimer_active(&op->thrtimer))
477 /* first receiption with enabled throttling mode */
478 if (!op->kt_lastmsg.tv64)
479 goto rx_changed_settime;
481 /* got a second frame inside a potential throttle period? */
482 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
483 ktime_to_us(op->kt_ival2)) {
484 /* do not send the saved data - only start throttle timer */
485 hrtimer_start(&op->thrtimer,
486 ktime_add(op->kt_lastmsg, op->kt_ival2),
491 /* the gap was that big, that throttling was not needed here */
493 bcm_rx_changed(op, lastdata);
494 op->kt_lastmsg = ktime_get();
498 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
499 * received data stored in op->last_frames[]
501 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
502 const struct can_frame *rxdata)
505 * no one uses the MSBs of can_dlc for comparation,
506 * so we use it here to detect the first time of reception
509 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
510 /* received data for the first time => send update to user */
511 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
515 /* do a real check in can_frame data section */
517 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
518 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
519 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
523 if (op->flags & RX_CHECK_DLC) {
524 /* do a real check in can_frame dlc */
525 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
527 bcm_rx_update_and_send(op, &op->last_frames[index],
535 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
537 static void bcm_rx_starttimer(struct bcm_op *op)
539 if (op->flags & RX_NO_AUTOTIMER)
542 if (op->kt_ival1.tv64)
543 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
546 static void bcm_rx_timeout_tsklet(unsigned long data)
548 struct bcm_op *op = (struct bcm_op *)data;
549 struct bcm_msg_head msg_head;
551 /* create notification to user */
552 msg_head.opcode = RX_TIMEOUT;
553 msg_head.flags = op->flags;
554 msg_head.count = op->count;
555 msg_head.ival1 = op->ival1;
556 msg_head.ival2 = op->ival2;
557 msg_head.can_id = op->can_id;
558 msg_head.nframes = 0;
560 bcm_send_to_user(op, &msg_head, NULL, 0);
564 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
566 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
568 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
570 /* schedule before NET_RX_SOFTIRQ */
571 tasklet_hi_schedule(&op->tsklet);
573 /* no restart of the timer is done here! */
575 /* if user wants to be informed, when cyclic CAN-Messages come back */
576 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
577 /* clear received can_frames to indicate 'nothing received' */
578 memset(op->last_frames, 0, op->nframes * CFSIZ);
581 return HRTIMER_NORESTART;
585 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
587 static inline int bcm_rx_do_flush(struct bcm_op *op, int update, int index)
589 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
591 bcm_rx_changed(op, &op->last_frames[index]);
598 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
600 * update == 0 : just check if throttled data is available (any irq context)
601 * update == 1 : check and send throttled data to userspace (soft_irq context)
603 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
607 if (op->nframes > 1) {
610 /* for MUX filter we start at index 1 */
611 for (i = 1; i < op->nframes; i++)
612 updated += bcm_rx_do_flush(op, update, i);
615 /* for RX_FILTER_ID and simple filter */
616 updated += bcm_rx_do_flush(op, update, 0);
622 static void bcm_rx_thr_tsklet(unsigned long data)
624 struct bcm_op *op = (struct bcm_op *)data;
626 /* push the changed data to the userspace */
627 bcm_rx_thr_flush(op, 1);
631 * bcm_rx_thr_handler - the time for blocked content updates is over now:
632 * Check for throttled data and send it to the userspace
634 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
636 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
638 tasklet_schedule(&op->thrtsklet);
640 if (bcm_rx_thr_flush(op, 0)) {
641 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
642 return HRTIMER_RESTART;
644 /* rearm throttle handling */
645 op->kt_lastmsg = ktime_set(0, 0);
646 return HRTIMER_NORESTART;
651 * bcm_rx_handler - handle a CAN frame receiption
653 static void bcm_rx_handler(struct sk_buff *skb, void *data)
655 struct bcm_op *op = (struct bcm_op *)data;
656 const struct can_frame *rxframe = (struct can_frame *)skb->data;
659 /* disable timeout */
660 hrtimer_cancel(&op->timer);
662 if (op->can_id != rxframe->can_id)
665 /* save rx timestamp */
666 op->rx_stamp = skb->tstamp;
667 /* save originator for recvfrom() */
668 op->rx_ifindex = skb->dev->ifindex;
669 /* update statistics */
672 if (op->flags & RX_RTR_FRAME) {
673 /* send reply for RTR-request (placed in op->frames[0]) */
678 if (op->flags & RX_FILTER_ID) {
679 /* the easiest case */
680 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
681 goto rx_freeskb_starttimer;
684 if (op->nframes == 1) {
685 /* simple compare with index 0 */
686 bcm_rx_cmp_to_index(op, 0, rxframe);
687 goto rx_freeskb_starttimer;
690 if (op->nframes > 1) {
694 * find the first multiplex mask that fits.
695 * Remark: The MUX-mask is stored in index 0
698 for (i = 1; i < op->nframes; i++) {
699 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
700 (GET_U64(&op->frames[0]) &
701 GET_U64(&op->frames[i]))) {
702 bcm_rx_cmp_to_index(op, i, rxframe);
708 rx_freeskb_starttimer:
709 bcm_rx_starttimer(op);
715 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
717 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
722 list_for_each_entry(op, ops, list) {
723 if ((op->can_id == can_id) && (op->ifindex == ifindex))
730 static void bcm_remove_op(struct bcm_op *op)
732 hrtimer_cancel(&op->timer);
733 hrtimer_cancel(&op->thrtimer);
736 tasklet_kill(&op->tsklet);
738 if (op->thrtsklet.func)
739 tasklet_kill(&op->thrtsklet);
741 if ((op->frames) && (op->frames != &op->sframe))
744 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
745 kfree(op->last_frames);
752 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
754 if (op->rx_reg_dev == dev) {
755 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
758 /* mark as removed subscription */
759 op->rx_reg_dev = NULL;
761 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
762 "mismatch %p %p\n", op->rx_reg_dev, dev);
766 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
768 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
770 struct bcm_op *op, *n;
772 list_for_each_entry_safe(op, n, ops, list) {
773 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
776 * Don't care if we're bound or not (due to netdev
777 * problems) can_rx_unregister() is always a save
782 * Only remove subscriptions that had not
783 * been removed due to NETDEV_UNREGISTER
786 if (op->rx_reg_dev) {
787 struct net_device *dev;
789 dev = dev_get_by_index(&init_net,
792 bcm_rx_unreg(dev, op);
797 can_rx_unregister(NULL, op->can_id,
807 return 0; /* not found */
811 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
813 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
815 struct bcm_op *op, *n;
817 list_for_each_entry_safe(op, n, ops, list) {
818 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
825 return 0; /* not found */
829 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
831 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
834 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
839 /* put current values into msg_head */
840 msg_head->flags = op->flags;
841 msg_head->count = op->count;
842 msg_head->ival1 = op->ival1;
843 msg_head->ival2 = op->ival2;
844 msg_head->nframes = op->nframes;
846 bcm_send_to_user(op, msg_head, op->frames, 0);
852 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
854 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
855 int ifindex, struct sock *sk)
857 struct bcm_sock *bo = bcm_sk(sk);
861 /* we need a real device to send frames */
865 /* we need at least one can_frame */
866 if (msg_head->nframes < 1)
869 /* check the given can_id */
870 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
873 /* update existing BCM operation */
876 * Do we need more space for the can_frames than currently
877 * allocated? -> This is a _really_ unusual use-case and
878 * therefore (complexity / locking) it is not supported.
880 if (msg_head->nframes > op->nframes)
883 /* update can_frames content */
884 for (i = 0; i < msg_head->nframes; i++) {
885 err = memcpy_fromiovec((u8 *)&op->frames[i],
886 msg->msg_iov, CFSIZ);
888 if (op->frames[i].can_dlc > 8)
894 if (msg_head->flags & TX_CP_CAN_ID) {
895 /* copy can_id into frame */
896 op->frames[i].can_id = msg_head->can_id;
901 /* insert new BCM operation for the given can_id */
903 op = kzalloc(OPSIZ, GFP_KERNEL);
907 op->can_id = msg_head->can_id;
909 /* create array for can_frames and copy the data */
910 if (msg_head->nframes > 1) {
911 op->frames = kmalloc(msg_head->nframes * CFSIZ,
918 op->frames = &op->sframe;
920 for (i = 0; i < msg_head->nframes; i++) {
921 err = memcpy_fromiovec((u8 *)&op->frames[i],
922 msg->msg_iov, CFSIZ);
924 if (op->frames[i].can_dlc > 8)
928 if (op->frames != &op->sframe)
934 if (msg_head->flags & TX_CP_CAN_ID) {
935 /* copy can_id into frame */
936 op->frames[i].can_id = msg_head->can_id;
940 /* tx_ops never compare with previous received messages */
941 op->last_frames = NULL;
943 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
945 op->ifindex = ifindex;
947 /* initialize uninitialized (kzalloc) structure */
948 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
949 op->timer.function = bcm_tx_timeout_handler;
951 /* initialize tasklet for tx countevent notification */
952 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
955 /* currently unused in tx_ops */
956 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
958 /* add this bcm_op to the list of the tx_ops */
959 list_add(&op->list, &bo->tx_ops);
961 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
963 if (op->nframes != msg_head->nframes) {
964 op->nframes = msg_head->nframes;
965 /* start multiple frame transmission with index 0 */
971 op->flags = msg_head->flags;
973 if (op->flags & TX_RESET_MULTI_IDX) {
974 /* start multiple frame transmission with index 0 */
978 if (op->flags & SETTIMER) {
979 /* set timer values */
980 op->count = msg_head->count;
981 op->ival1 = msg_head->ival1;
982 op->ival2 = msg_head->ival2;
983 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
984 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
986 /* disable an active timer due to zero values? */
987 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
988 hrtimer_cancel(&op->timer);
991 if ((op->flags & STARTTIMER) &&
992 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
994 /* spec: send can_frame when starting timer */
995 op->flags |= TX_ANNOUNCE;
997 if (op->kt_ival1.tv64 && (op->count > 0)) {
998 /* op->count-- is done in bcm_tx_timeout_handler */
999 hrtimer_start(&op->timer, op->kt_ival1,
1002 hrtimer_start(&op->timer, op->kt_ival2,
1006 if (op->flags & TX_ANNOUNCE)
1009 return msg_head->nframes * CFSIZ + MHSIZ;
1013 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1015 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1016 int ifindex, struct sock *sk)
1018 struct bcm_sock *bo = bcm_sk(sk);
1023 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1024 /* be robust against wrong usage ... */
1025 msg_head->flags |= RX_FILTER_ID;
1026 /* ignore trailing garbage */
1027 msg_head->nframes = 0;
1030 if ((msg_head->flags & RX_RTR_FRAME) &&
1031 ((msg_head->nframes != 1) ||
1032 (!(msg_head->can_id & CAN_RTR_FLAG))))
1035 /* check the given can_id */
1036 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1038 /* update existing BCM operation */
1041 * Do we need more space for the can_frames than currently
1042 * allocated? -> This is a _really_ unusual use-case and
1043 * therefore (complexity / locking) it is not supported.
1045 if (msg_head->nframes > op->nframes)
1048 if (msg_head->nframes) {
1049 /* update can_frames content */
1050 err = memcpy_fromiovec((u8 *)op->frames,
1052 msg_head->nframes * CFSIZ);
1056 /* clear last_frames to indicate 'nothing received' */
1057 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1060 op->nframes = msg_head->nframes;
1062 /* Only an update -> do not call can_rx_register() */
1066 /* insert new BCM operation for the given can_id */
1067 op = kzalloc(OPSIZ, GFP_KERNEL);
1071 op->can_id = msg_head->can_id;
1072 op->nframes = msg_head->nframes;
1074 if (msg_head->nframes > 1) {
1075 /* create array for can_frames and copy the data */
1076 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1083 /* create and init array for received can_frames */
1084 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1086 if (!op->last_frames) {
1093 op->frames = &op->sframe;
1094 op->last_frames = &op->last_sframe;
1097 if (msg_head->nframes) {
1098 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1099 msg_head->nframes * CFSIZ);
1101 if (op->frames != &op->sframe)
1103 if (op->last_frames != &op->last_sframe)
1104 kfree(op->last_frames);
1110 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1112 op->ifindex = ifindex;
1114 /* initialize uninitialized (kzalloc) structure */
1115 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1116 op->timer.function = bcm_rx_timeout_handler;
1118 /* initialize tasklet for rx timeout notification */
1119 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1120 (unsigned long) op);
1122 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1123 op->thrtimer.function = bcm_rx_thr_handler;
1125 /* initialize tasklet for rx throttle handling */
1126 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1127 (unsigned long) op);
1129 /* add this bcm_op to the list of the rx_ops */
1130 list_add(&op->list, &bo->rx_ops);
1132 /* call can_rx_register() */
1135 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1138 op->flags = msg_head->flags;
1140 if (op->flags & RX_RTR_FRAME) {
1142 /* no timers in RTR-mode */
1143 hrtimer_cancel(&op->thrtimer);
1144 hrtimer_cancel(&op->timer);
1147 * funny feature in RX(!)_SETUP only for RTR-mode:
1148 * copy can_id into frame BUT without RTR-flag to
1149 * prevent a full-load-loopback-test ... ;-]
1151 if ((op->flags & TX_CP_CAN_ID) ||
1152 (op->frames[0].can_id == op->can_id))
1153 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1156 if (op->flags & SETTIMER) {
1158 /* set timer value */
1159 op->ival1 = msg_head->ival1;
1160 op->ival2 = msg_head->ival2;
1161 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1162 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1164 /* disable an active timer due to zero value? */
1165 if (!op->kt_ival1.tv64)
1166 hrtimer_cancel(&op->timer);
1169 * In any case cancel the throttle timer, flush
1170 * potentially blocked msgs and reset throttle handling
1172 op->kt_lastmsg = ktime_set(0, 0);
1173 hrtimer_cancel(&op->thrtimer);
1174 bcm_rx_thr_flush(op, 1);
1177 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1178 hrtimer_start(&op->timer, op->kt_ival1,
1182 /* now we can register for can_ids, if we added a new bcm_op */
1183 if (do_rx_register) {
1185 struct net_device *dev;
1187 dev = dev_get_by_index(&init_net, ifindex);
1189 err = can_rx_register(dev, op->can_id,
1190 REGMASK(op->can_id),
1194 op->rx_reg_dev = dev;
1199 err = can_rx_register(NULL, op->can_id,
1200 REGMASK(op->can_id),
1201 bcm_rx_handler, op, "bcm");
1203 /* this bcm rx op is broken -> remove it */
1204 list_del(&op->list);
1210 return msg_head->nframes * CFSIZ + MHSIZ;
1214 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1216 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1218 struct sk_buff *skb;
1219 struct net_device *dev;
1222 /* we need a real device to send frames */
1226 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1231 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1237 dev = dev_get_by_index(&init_net, ifindex);
1245 err = can_send(skb, 1); /* send with loopback */
1251 return CFSIZ + MHSIZ;
1255 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1257 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1258 struct msghdr *msg, size_t size)
1260 struct sock *sk = sock->sk;
1261 struct bcm_sock *bo = bcm_sk(sk);
1262 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1263 struct bcm_msg_head msg_head;
1264 int ret; /* read bytes or error codes as return value */
1269 /* check for valid message length from userspace */
1270 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1273 /* check for alternative ifindex for this bcm_op */
1275 if (!ifindex && msg->msg_name) {
1276 /* no bound device as default => check msg_name */
1277 struct sockaddr_can *addr =
1278 (struct sockaddr_can *)msg->msg_name;
1280 if (addr->can_family != AF_CAN)
1283 /* ifindex from sendto() */
1284 ifindex = addr->can_ifindex;
1287 struct net_device *dev;
1289 dev = dev_get_by_index(&init_net, ifindex);
1293 if (dev->type != ARPHRD_CAN) {
1302 /* read message head information */
1304 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1310 switch (msg_head.opcode) {
1313 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1317 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1321 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1328 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1335 /* reuse msg_head for the reply to TX_READ */
1336 msg_head.opcode = TX_STATUS;
1337 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1341 /* reuse msg_head for the reply to RX_READ */
1342 msg_head.opcode = RX_STATUS;
1343 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1347 /* we need exactly one can_frame behind the msg head */
1348 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1351 ret = bcm_tx_send(msg, ifindex, sk);
1365 * notification handler for netdevice status changes
1367 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1370 struct net_device *dev = (struct net_device *)data;
1371 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1372 struct sock *sk = &bo->sk;
1374 int notify_enodev = 0;
1376 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1377 if (!net_eq(dev_net(dev), &init_net))
1379 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1380 if (dev->nd_net != &init_net)
1384 if (dev->type != ARPHRD_CAN)
1389 case NETDEV_UNREGISTER:
1392 /* remove device specific receive entries */
1393 list_for_each_entry(op, &bo->rx_ops, list)
1394 if (op->rx_reg_dev == dev)
1395 bcm_rx_unreg(dev, op);
1397 /* remove device reference, if this is our bound device */
1398 if (bo->bound && bo->ifindex == dev->ifindex) {
1406 if (notify_enodev) {
1407 sk->sk_err = ENODEV;
1408 if (!sock_flag(sk, SOCK_DEAD))
1409 sk->sk_error_report(sk);
1414 if (bo->bound && bo->ifindex == dev->ifindex) {
1415 sk->sk_err = ENETDOWN;
1416 if (!sock_flag(sk, SOCK_DEAD))
1417 sk->sk_error_report(sk);
1425 * initial settings for all BCM sockets to be set at socket creation time
1427 static int bcm_init(struct sock *sk)
1429 struct bcm_sock *bo = bcm_sk(sk);
1433 bo->dropped_usr_msgs = 0;
1434 bo->bcm_proc_read = NULL;
1436 INIT_LIST_HEAD(&bo->tx_ops);
1437 INIT_LIST_HEAD(&bo->rx_ops);
1440 bo->notifier.notifier_call = bcm_notifier;
1442 register_netdevice_notifier(&bo->notifier);
1448 * standard socket functions
1450 static int bcm_release(struct socket *sock)
1452 struct sock *sk = sock->sk;
1453 struct bcm_sock *bo = bcm_sk(sk);
1454 struct bcm_op *op, *next;
1456 /* remove bcm_ops, timer, rx_unregister(), etc. */
1458 unregister_netdevice_notifier(&bo->notifier);
1462 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1465 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1467 * Don't care if we're bound or not (due to netdev problems)
1468 * can_rx_unregister() is always a save thing to do here.
1472 * Only remove subscriptions that had not
1473 * been removed due to NETDEV_UNREGISTER
1476 if (op->rx_reg_dev) {
1477 struct net_device *dev;
1479 dev = dev_get_by_index(&init_net, op->ifindex);
1481 bcm_rx_unreg(dev, op);
1486 can_rx_unregister(NULL, op->can_id,
1487 REGMASK(op->can_id),
1488 bcm_rx_handler, op);
1493 /* remove procfs entry */
1494 if (proc_dir && bo->bcm_proc_read)
1495 remove_proc_entry(bo->procname, proc_dir);
1497 /* remove device reference */
1509 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1512 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1513 struct sock *sk = sock->sk;
1514 struct bcm_sock *bo = bcm_sk(sk);
1519 /* bind a device to this socket */
1520 if (addr->can_ifindex) {
1521 struct net_device *dev;
1523 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1527 if (dev->type != ARPHRD_CAN) {
1532 bo->ifindex = dev->ifindex;
1536 /* no interface reference for ifindex = 0 ('any' CAN device) */
1543 /* unique socket address as filename */
1544 sprintf(bo->procname, "%p", sock);
1545 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1553 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1554 struct msghdr *msg, size_t size, int flags)
1556 struct sock *sk = sock->sk;
1557 struct sk_buff *skb;
1562 noblock = flags & MSG_DONTWAIT;
1563 flags &= ~MSG_DONTWAIT;
1564 skb = skb_recv_datagram(sk, flags, noblock, &error);
1568 if (skb->len < size)
1571 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1573 skb_free_datagram(sk, skb);
1577 sock_recv_timestamp(msg, sk, skb);
1579 if (msg->msg_name) {
1580 msg->msg_namelen = sizeof(struct sockaddr_can);
1581 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1584 skb_free_datagram(sk, skb);
1589 static struct proto_ops bcm_ops __read_mostly = {
1591 .release = bcm_release,
1592 .bind = sock_no_bind,
1593 .connect = bcm_connect,
1594 .socketpair = sock_no_socketpair,
1595 .accept = sock_no_accept,
1596 .getname = sock_no_getname,
1597 .poll = datagram_poll,
1598 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1599 .listen = sock_no_listen,
1600 .shutdown = sock_no_shutdown,
1601 .setsockopt = sock_no_setsockopt,
1602 .getsockopt = sock_no_getsockopt,
1603 .sendmsg = bcm_sendmsg,
1604 .recvmsg = bcm_recvmsg,
1605 .mmap = sock_no_mmap,
1606 .sendpage = sock_no_sendpage,
1609 static struct proto bcm_proto __read_mostly = {
1611 .owner = THIS_MODULE,
1612 .obj_size = sizeof(struct bcm_sock),
1616 static struct can_proto bcm_can_proto __read_mostly = {
1618 .protocol = CAN_BCM,
1624 static int __init bcm_module_init(void)
1630 err = can_proto_register(&bcm_can_proto);
1632 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1636 /* create /proc/net/can-bcm directory */
1637 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1638 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1640 proc_dir = proc_mkdir("can-bcm", proc_net);
1644 proc_dir->owner = THIS_MODULE;
1649 static void __exit bcm_module_exit(void)
1651 can_proto_unregister(&bcm_can_proto);
1654 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1655 proc_net_remove(&init_net, "can-bcm");
1657 proc_net_remove("can-bcm");
1661 module_init(bcm_module_init);
1662 module_exit(bcm_module_exit);