2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/uio.h>
50 #include <linux/net.h>
51 #include <linux/netdevice.h>
52 #include <linux/socket.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <linux/can.h>
56 #include <linux/can/core.h>
57 #include <linux/can/bcm.h>
59 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
60 #include <net/net_namespace.h>
62 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
66 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
69 /* use of last_frames[index].can_dlc */
70 #define RX_RECV 0x40 /* received data for this element */
71 #define RX_THR 0x80 /* element not been sent due to throttle feature */
72 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
74 /* get best masking value for can_rx_register() for a given single can_id */
75 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
76 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
78 #define CAN_BCM_VERSION CAN_VERSION
79 static __initdata const char banner[] = KERN_INFO
80 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
86 /* easy access to can_frame payload */
87 static inline u64 GET_U64(const struct can_frame *cp)
89 return *(u64 *)cp->data;
93 struct list_head list;
97 unsigned long j_ival1, j_ival2, j_lastmsg;
98 unsigned long frames_abs, frames_filtered;
99 struct timer_list timer, thrtimer;
100 struct timeval ival1, ival2;
101 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
104 struct timeval rx_stamp;
110 struct can_frame *frames;
111 struct can_frame *last_frames;
112 struct can_frame sframe;
113 struct can_frame last_sframe;
115 struct net_device *rx_reg_dev;
118 static struct proc_dir_entry *proc_dir;
121 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
128 struct notifier_block notifier;
129 struct list_head rx_ops;
130 struct list_head tx_ops;
131 unsigned long dropped_usr_msgs;
132 struct proc_dir_entry *bcm_proc_read;
133 char procname [9]; /* pointer printed in ASCII with \0 */
136 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
138 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
139 return (struct bcm_sock *)sk;
141 return (struct bcm_sock *)sk->sk_protinfo;
145 #define CFSIZ sizeof(struct can_frame)
146 #define OPSIZ sizeof(struct bcm_op)
147 #define MHSIZ sizeof(struct bcm_msg_head)
150 * rounded_tv2jif - calculate jiffies from timeval including optional up
151 * @tv: pointer to timeval
154 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
155 * function is intentionally more relaxed on precise timer ticks to get
156 * exact one jiffy for requested 1000us on a 1000HZ machine.
157 * This code is to be removed when upgrading to kernel hrtimer.
160 * calculated jiffies (max: ULONG_MAX)
162 static unsigned long rounded_tv2jif(const struct timeval *tv)
164 unsigned long sec = tv->tv_sec;
165 unsigned long usec = tv->tv_usec;
168 if (sec > ULONG_MAX / HZ)
171 /* round up to get at least the requested time */
172 usec += 1000000 / HZ - 1;
174 jif = usec / (1000000 / HZ);
176 if (sec * HZ > ULONG_MAX - jif)
179 return jif + sec * HZ;
185 static char *bcm_proc_getifname(int ifindex)
187 struct net_device *dev;
192 /* no usage counting */
193 dev = __dev_get_by_index(&init_net, ifindex);
200 static int bcm_read_proc(char *page, char **start, off_t off,
201 int count, int *eof, void *data)
204 struct sock *sk = (struct sock *)data;
205 struct bcm_sock *bo = bcm_sk(sk);
208 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
210 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
211 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
212 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
213 bo->dropped_usr_msgs);
214 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
215 bcm_proc_getifname(bo->ifindex));
216 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
218 list_for_each_entry(op, &bo->rx_ops, list) {
220 unsigned long reduction;
222 /* print only active entries & prevent division by zero */
226 len += snprintf(page + len, PAGE_SIZE - len,
228 op->can_id, bcm_proc_getifname(op->ifindex));
229 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
231 (op->flags & RX_CHECK_DLC)?'d':' ');
233 len += snprintf(page + len, PAGE_SIZE - len,
234 "timeo=%ld ", op->j_ival1);
237 len += snprintf(page + len, PAGE_SIZE - len,
238 "thr=%ld ", op->j_ival2);
240 len += snprintf(page + len, PAGE_SIZE - len,
241 "# recv %ld (%ld) => reduction: ",
242 op->frames_filtered, op->frames_abs);
244 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
246 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
247 (reduction == 100)?"near ":"", reduction);
249 if (len > PAGE_SIZE - 200) {
250 /* mark output cut off */
251 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
256 list_for_each_entry(op, &bo->tx_ops, list) {
258 len += snprintf(page + len, PAGE_SIZE - len,
259 "tx_op: %03X %s [%d] ",
260 op->can_id, bcm_proc_getifname(op->ifindex),
263 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
267 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
270 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
273 if (len > PAGE_SIZE - 100) {
274 /* mark output cut off */
275 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
280 len += snprintf(page + len, PAGE_SIZE - len, "\n");
287 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
288 * of the given bcm tx op
290 static void bcm_can_tx(struct bcm_op *op)
293 struct net_device *dev;
294 struct can_frame *cf = &op->frames[op->currframe];
296 /* no target device? => exit */
300 dev = dev_get_by_index(&init_net, op->ifindex);
302 /* RFC: should this bcm_op remove itself here? */
306 skb = alloc_skb(CFSIZ, gfp_any());
310 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
312 /* send with loopback */
317 /* update statistics */
321 /* reached last frame? */
322 if (op->currframe >= op->nframes)
329 * bcm_send_to_user - send a BCM message to the userspace
330 * (consisting of bcm_msg_head + x CAN frames)
332 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
333 struct can_frame *frames, int has_timestamp)
336 struct can_frame *firstframe;
337 struct sockaddr_can *addr;
338 struct sock *sk = op->sk;
339 int datalen = head->nframes * CFSIZ;
342 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
346 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
349 /* can_frames starting here */
350 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
351 firstframe = (struct can_frame *) skb_tail_pointer(skb);
353 firstframe = (struct can_frame *) skb->tail;
356 memcpy(skb_put(skb, datalen), frames, datalen);
359 * the BCM uses the can_dlc-element of the can_frame
360 * structure for internal purposes. This is only
361 * relevant for updates that are generated by the
362 * BCM, where nframes is 1
364 if (head->nframes == 1)
365 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
369 /* restore rx timestamp */
370 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
371 skb->tstamp = op->rx_stamp;
373 skb_set_timestamp(skb, &op->rx_stamp);
378 * Put the datagram to the queue so that bcm_recvmsg() can
379 * get it from there. We need to pass the interface index to
380 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
381 * containing the interface index.
384 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
385 addr = (struct sockaddr_can *)skb->cb;
386 memset(addr, 0, sizeof(*addr));
387 addr->can_family = AF_CAN;
388 addr->can_ifindex = op->rx_ifindex;
390 err = sock_queue_rcv_skb(sk, skb);
392 struct bcm_sock *bo = bcm_sk(sk);
395 /* don't care about overflows in this statistic */
396 bo->dropped_usr_msgs++;
401 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
403 static void bcm_tx_timeout_handler(unsigned long data)
405 struct bcm_op *op = (struct bcm_op *)data;
407 if (op->j_ival1 && (op->count > 0)) {
410 if (!op->count && (op->flags & TX_COUNTEVT)) {
411 struct bcm_msg_head msg_head;
413 /* create notification to user */
414 msg_head.opcode = TX_EXPIRED;
415 msg_head.flags = op->flags;
416 msg_head.count = op->count;
417 msg_head.ival1 = op->ival1;
418 msg_head.ival2 = op->ival2;
419 msg_head.can_id = op->can_id;
420 msg_head.nframes = 0;
422 bcm_send_to_user(op, &msg_head, NULL, 0);
426 if (op->j_ival1 && (op->count > 0)) {
428 /* send (next) frame */
430 mod_timer(&op->timer, jiffies + op->j_ival1);
435 /* send (next) frame */
437 mod_timer(&op->timer, jiffies + op->j_ival2);
445 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
447 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
449 struct bcm_msg_head head;
451 op->j_lastmsg = jiffies;
453 /* update statistics */
454 op->frames_filtered++;
456 /* prevent statistics overflow */
457 if (op->frames_filtered > ULONG_MAX/100)
458 op->frames_filtered = op->frames_abs = 0;
460 head.opcode = RX_CHANGED;
461 head.flags = op->flags;
462 head.count = op->count;
463 head.ival1 = op->ival1;
464 head.ival2 = op->ival2;
465 head.can_id = op->can_id;
468 bcm_send_to_user(op, &head, data, 1);
472 * bcm_rx_update_and_send - process a detected relevant receive content change
473 * 1. update the last received data
474 * 2. send a notification to the user (if possible)
476 static void bcm_rx_update_and_send(struct bcm_op *op,
477 struct can_frame *lastdata,
478 struct can_frame *rxdata)
480 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
482 memcpy(lastdata, rxdata, CFSIZ);
485 lastdata->can_dlc |= RX_RECV;
487 /* throttle bcm_rx_changed ? */
488 if ((op->thrtimer.expires) ||
489 ((op->j_ival2) && (nexttx > jiffies))) {
490 /* we are already waiting OR we have to start waiting */
492 /* mark as 'throttled' */
493 lastdata->can_dlc |= RX_THR;
495 if (!(op->thrtimer.expires)) {
496 /* start the timer only the first time */
497 mod_timer(&op->thrtimer, nexttx);
501 /* send RX_CHANGED to the user immediately */
502 bcm_rx_changed(op, rxdata);
507 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
508 * received data stored in op->last_frames[]
510 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
511 struct can_frame *rxdata)
514 * no one uses the MSBs of can_dlc for comparation,
515 * so we use it here to detect the first time of reception
518 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
519 /* received data for the first time => send update to user */
520 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
524 /* do a real check in can_frame data section */
526 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
527 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
528 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
532 if (op->flags & RX_CHECK_DLC) {
533 /* do a real check in can_frame dlc */
534 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
536 bcm_rx_update_and_send(op, &op->last_frames[index],
544 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
546 static void bcm_rx_starttimer(struct bcm_op *op)
548 if (op->flags & RX_NO_AUTOTIMER)
552 mod_timer(&op->timer, jiffies + op->j_ival1);
556 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
558 static void bcm_rx_timeout_handler(unsigned long data)
560 struct bcm_op *op = (struct bcm_op *)data;
561 struct bcm_msg_head msg_head;
563 msg_head.opcode = RX_TIMEOUT;
564 msg_head.flags = op->flags;
565 msg_head.count = op->count;
566 msg_head.ival1 = op->ival1;
567 msg_head.ival2 = op->ival2;
568 msg_head.can_id = op->can_id;
569 msg_head.nframes = 0;
571 bcm_send_to_user(op, &msg_head, NULL, 0);
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);
583 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
585 static int bcm_rx_thr_flush(struct bcm_op *op)
589 if (op->nframes > 1) {
592 /* for MUX filter we start at index 1 */
593 for (i = 1; i < op->nframes; i++) {
594 if ((op->last_frames) &&
595 (op->last_frames[i].can_dlc & RX_THR)) {
596 op->last_frames[i].can_dlc &= ~RX_THR;
597 bcm_rx_changed(op, &op->last_frames[i]);
603 /* for RX_FILTER_ID and simple filter */
604 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
605 op->last_frames[0].can_dlc &= ~RX_THR;
606 bcm_rx_changed(op, &op->last_frames[0]);
615 * bcm_rx_thr_handler - the time for blocked content updates is over now:
616 * Check for throttled data and send it to the userspace
618 static void bcm_rx_thr_handler(unsigned long data)
620 struct bcm_op *op = (struct bcm_op *)data;
622 if (bcm_rx_thr_flush(op))
623 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
625 /* mark disabled / consumed timer */
626 op->thrtimer.expires = 0;
631 * bcm_rx_handler - handle a CAN frame receiption
633 static void bcm_rx_handler(struct sk_buff *skb, void *data)
635 struct bcm_op *op = (struct bcm_op *)data;
636 struct can_frame rxframe;
639 /* disable timeout */
640 del_timer(&op->timer);
642 if (skb->len == sizeof(rxframe)) {
643 memcpy(&rxframe, skb->data, sizeof(rxframe));
644 /* save rx timestamp */
645 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
646 op->rx_stamp = skb->tstamp;
648 skb_get_timestamp(skb, &op->rx_stamp);
650 /* save originator for recvfrom() */
651 op->rx_ifindex = skb->dev->ifindex;
652 /* update statistics */
661 if (op->can_id != rxframe.can_id)
664 if (op->flags & RX_RTR_FRAME) {
665 /* send reply for RTR-request (placed in op->frames[0]) */
670 if (op->flags & RX_FILTER_ID) {
671 /* the easiest case */
672 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
673 bcm_rx_starttimer(op);
677 if (op->nframes == 1) {
678 /* simple compare with index 0 */
679 bcm_rx_cmp_to_index(op, 0, &rxframe);
680 bcm_rx_starttimer(op);
684 if (op->nframes > 1) {
688 * find the first multiplex mask that fits.
689 * Remark: The MUX-mask is stored in index 0
692 for (i = 1; i < op->nframes; i++) {
693 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
694 (GET_U64(&op->frames[0]) &
695 GET_U64(&op->frames[i]))) {
696 bcm_rx_cmp_to_index(op, i, &rxframe);
700 bcm_rx_starttimer(op);
705 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
707 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
712 list_for_each_entry(op, ops, list) {
713 if ((op->can_id == can_id) && (op->ifindex == ifindex))
720 static void bcm_remove_op(struct bcm_op *op)
722 del_timer(&op->timer);
723 del_timer(&op->thrtimer);
725 if ((op->frames) && (op->frames != &op->sframe))
728 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
729 kfree(op->last_frames);
736 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
738 if (op->rx_reg_dev == dev) {
739 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
742 /* mark as removed subscription */
743 op->rx_reg_dev = NULL;
745 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
746 "mismatch %p %p\n", op->rx_reg_dev, dev);
750 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
752 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
754 struct bcm_op *op, *n;
756 list_for_each_entry_safe(op, n, ops, list) {
757 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
760 * Don't care if we're bound or not (due to netdev
761 * problems) can_rx_unregister() is always a save
766 * Only remove subscriptions that had not
767 * been removed due to NETDEV_UNREGISTER
770 if (op->rx_reg_dev) {
771 struct net_device *dev;
773 dev = dev_get_by_index(&init_net,
776 bcm_rx_unreg(dev, op);
781 can_rx_unregister(NULL, op->can_id,
791 return 0; /* not found */
795 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
797 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
799 struct bcm_op *op, *n;
801 list_for_each_entry_safe(op, n, ops, list) {
802 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
809 return 0; /* not found */
813 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
815 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
818 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
823 /* put current values into msg_head */
824 msg_head->flags = op->flags;
825 msg_head->count = op->count;
826 msg_head->ival1 = op->ival1;
827 msg_head->ival2 = op->ival2;
828 msg_head->nframes = op->nframes;
830 bcm_send_to_user(op, msg_head, op->frames, 0);
836 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
838 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
839 int ifindex, struct sock *sk)
841 struct bcm_sock *bo = bcm_sk(sk);
845 /* we need a real device to send frames */
849 /* we need at least one can_frame */
850 if (msg_head->nframes < 1)
853 /* check the given can_id */
854 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
857 /* update existing BCM operation */
860 * Do we need more space for the can_frames than currently
861 * allocated? -> This is a _really_ unusual use-case and
862 * therefore (complexity / locking) it is not supported.
864 if (msg_head->nframes > op->nframes)
867 /* update can_frames content */
868 for (i = 0; i < msg_head->nframes; i++) {
869 err = memcpy_fromiovec((u8 *)&op->frames[i],
870 msg->msg_iov, CFSIZ);
874 if (msg_head->flags & TX_CP_CAN_ID) {
875 /* copy can_id into frame */
876 op->frames[i].can_id = msg_head->can_id;
881 /* insert new BCM operation for the given can_id */
883 op = kzalloc(OPSIZ, GFP_KERNEL);
887 op->can_id = msg_head->can_id;
889 /* create array for can_frames and copy the data */
890 if (msg_head->nframes > 1) {
891 op->frames = kmalloc(msg_head->nframes * CFSIZ,
898 op->frames = &op->sframe;
900 for (i = 0; i < msg_head->nframes; i++) {
901 err = memcpy_fromiovec((u8 *)&op->frames[i],
902 msg->msg_iov, CFSIZ);
904 if (op->frames != &op->sframe)
910 if (msg_head->flags & TX_CP_CAN_ID) {
911 /* copy can_id into frame */
912 op->frames[i].can_id = msg_head->can_id;
916 /* tx_ops never compare with previous received messages */
917 op->last_frames = NULL;
919 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
921 op->ifindex = ifindex;
923 /* initialize uninitialized (kzalloc) structure */
924 setup_timer(&op->timer, bcm_tx_timeout_handler,
927 /* currently unused in tx_ops */
928 init_timer(&op->thrtimer);
930 /* add this bcm_op to the list of the tx_ops */
931 list_add(&op->list, &bo->tx_ops);
933 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
935 if (op->nframes != msg_head->nframes) {
936 op->nframes = msg_head->nframes;
937 /* start multiple frame transmission with index 0 */
943 op->flags = msg_head->flags;
945 if (op->flags & TX_RESET_MULTI_IDX) {
946 /* start multiple frame transmission with index 0 */
950 if (op->flags & SETTIMER) {
951 /* set timer values */
952 op->count = msg_head->count;
953 op->ival1 = msg_head->ival1;
954 op->ival2 = msg_head->ival2;
955 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
956 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
958 /* disable an active timer due to zero values? */
959 if (!op->j_ival1 && !op->j_ival2)
960 del_timer(&op->timer);
963 if ((op->flags & STARTTIMER) &&
964 ((op->j_ival1 && op->count) || op->j_ival2)) {
966 /* spec: send can_frame when starting timer */
967 op->flags |= TX_ANNOUNCE;
969 if (op->j_ival1 && (op->count > 0)) {
970 /* op->count-- is done in bcm_tx_timeout_handler */
971 mod_timer(&op->timer, jiffies + op->j_ival1);
973 mod_timer(&op->timer, jiffies + op->j_ival2);
976 if (op->flags & TX_ANNOUNCE)
979 return msg_head->nframes * CFSIZ + MHSIZ;
983 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
985 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
986 int ifindex, struct sock *sk)
988 struct bcm_sock *bo = bcm_sk(sk);
993 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
994 /* be robust against wrong usage ... */
995 msg_head->flags |= RX_FILTER_ID;
996 /* ignore trailing garbage */
997 msg_head->nframes = 0;
1000 if ((msg_head->flags & RX_RTR_FRAME) &&
1001 ((msg_head->nframes != 1) ||
1002 (!(msg_head->can_id & CAN_RTR_FLAG))))
1005 /* check the given can_id */
1006 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1008 /* update existing BCM operation */
1011 * Do we need more space for the can_frames than currently
1012 * allocated? -> This is a _really_ unusual use-case and
1013 * therefore (complexity / locking) it is not supported.
1015 if (msg_head->nframes > op->nframes)
1018 if (msg_head->nframes) {
1019 /* update can_frames content */
1020 err = memcpy_fromiovec((u8 *)op->frames,
1022 msg_head->nframes * CFSIZ);
1026 /* clear last_frames to indicate 'nothing received' */
1027 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1030 op->nframes = msg_head->nframes;
1032 /* Only an update -> do not call can_rx_register() */
1036 /* insert new BCM operation for the given can_id */
1037 op = kzalloc(OPSIZ, GFP_KERNEL);
1041 op->can_id = msg_head->can_id;
1042 op->nframes = msg_head->nframes;
1044 if (msg_head->nframes > 1) {
1045 /* create array for can_frames and copy the data */
1046 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1053 /* create and init array for received can_frames */
1054 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1056 if (!op->last_frames) {
1063 op->frames = &op->sframe;
1064 op->last_frames = &op->last_sframe;
1067 if (msg_head->nframes) {
1068 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1069 msg_head->nframes * CFSIZ);
1071 if (op->frames != &op->sframe)
1073 if (op->last_frames != &op->last_sframe)
1074 kfree(op->last_frames);
1080 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1082 op->ifindex = ifindex;
1084 /* initialize uninitialized (kzalloc) structure */
1085 setup_timer(&op->timer, bcm_rx_timeout_handler,
1088 /* init throttle timer for RX_CHANGED */
1089 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1092 /* mark disabled timer */
1093 op->thrtimer.expires = 0;
1095 /* add this bcm_op to the list of the rx_ops */
1096 list_add(&op->list, &bo->rx_ops);
1098 /* call can_rx_register() */
1101 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1104 op->flags = msg_head->flags;
1106 if (op->flags & RX_RTR_FRAME) {
1108 /* no timers in RTR-mode */
1109 del_timer(&op->thrtimer);
1110 del_timer(&op->timer);
1113 * funny feature in RX(!)_SETUP only for RTR-mode:
1114 * copy can_id into frame BUT without RTR-flag to
1115 * prevent a full-load-loopback-test ... ;-]
1117 if ((op->flags & TX_CP_CAN_ID) ||
1118 (op->frames[0].can_id == op->can_id))
1119 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1122 if (op->flags & SETTIMER) {
1124 /* set timer value */
1125 op->ival1 = msg_head->ival1;
1126 op->ival2 = msg_head->ival2;
1127 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1128 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1130 /* disable an active timer due to zero value? */
1132 del_timer(&op->timer);
1135 * In any case cancel the throttle timer, flush
1136 * potentially blocked msgs and reset throttle handling
1138 del_timer(&op->thrtimer);
1139 bcm_rx_thr_flush(op);
1140 op->thrtimer.expires = 0;
1143 if ((op->flags & STARTTIMER) && op->j_ival1)
1144 mod_timer(&op->timer, jiffies + op->j_ival1);
1147 /* now we can register for can_ids, if we added a new bcm_op */
1148 if (do_rx_register) {
1150 struct net_device *dev;
1152 dev = dev_get_by_index(&init_net, ifindex);
1154 err = can_rx_register(dev, op->can_id,
1155 REGMASK(op->can_id),
1159 op->rx_reg_dev = dev;
1164 err = can_rx_register(NULL, op->can_id,
1165 REGMASK(op->can_id),
1166 bcm_rx_handler, op, "bcm");
1168 /* this bcm rx op is broken -> remove it */
1169 list_del(&op->list);
1175 return msg_head->nframes * CFSIZ + MHSIZ;
1179 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1181 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1183 struct sk_buff *skb;
1184 struct net_device *dev;
1187 /* we need a real device to send frames */
1191 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1196 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1202 dev = dev_get_by_index(&init_net, ifindex);
1210 can_send(skb, 1); /* send with loopback */
1213 return CFSIZ + MHSIZ;
1217 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1219 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1220 struct msghdr *msg, size_t size)
1222 struct sock *sk = sock->sk;
1223 struct bcm_sock *bo = bcm_sk(sk);
1224 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1225 struct bcm_msg_head msg_head;
1226 int ret; /* read bytes or error codes as return value */
1231 /* check for alternative ifindex for this bcm_op */
1233 if (!ifindex && msg->msg_name) {
1234 /* no bound device as default => check msg_name */
1235 struct sockaddr_can *addr =
1236 (struct sockaddr_can *)msg->msg_name;
1238 if (addr->can_family != AF_CAN)
1241 /* ifindex from sendto() */
1242 ifindex = addr->can_ifindex;
1245 struct net_device *dev;
1247 dev = dev_get_by_index(&init_net, ifindex);
1251 if (dev->type != ARPHRD_CAN) {
1260 /* read message head information */
1262 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1268 switch (msg_head.opcode) {
1271 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1275 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1279 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1286 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1293 /* reuse msg_head for the reply to TX_READ */
1294 msg_head.opcode = TX_STATUS;
1295 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1299 /* reuse msg_head for the reply to RX_READ */
1300 msg_head.opcode = RX_STATUS;
1301 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1305 /* we need at least one can_frame */
1306 if (msg_head.nframes < 1)
1309 ret = bcm_tx_send(msg, ifindex, sk);
1323 * notification handler for netdevice status changes
1325 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1328 struct net_device *dev = (struct net_device *)data;
1329 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1330 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1331 struct sock *sk = &bo->sk;
1333 struct sock *sk = bo->sk;
1336 int notify_enodev = 0;
1338 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1339 if (dev->nd_net != &init_net)
1343 if (dev->type != ARPHRD_CAN)
1348 case NETDEV_UNREGISTER:
1351 /* remove device specific receive entries */
1352 list_for_each_entry(op, &bo->rx_ops, list)
1353 if (op->rx_reg_dev == dev)
1354 bcm_rx_unreg(dev, op);
1356 /* remove device reference, if this is our bound device */
1357 if (bo->bound && bo->ifindex == dev->ifindex) {
1365 if (notify_enodev) {
1366 sk->sk_err = ENODEV;
1367 if (!sock_flag(sk, SOCK_DEAD))
1368 sk->sk_error_report(sk);
1373 if (bo->bound && bo->ifindex == dev->ifindex) {
1374 sk->sk_err = ENETDOWN;
1375 if (!sock_flag(sk, SOCK_DEAD))
1376 sk->sk_error_report(sk);
1384 * initial settings for all BCM sockets to be set at socket creation time
1386 static int bcm_init(struct sock *sk)
1388 struct bcm_sock *bo = bcm_sk(sk);
1390 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1395 bo->dropped_usr_msgs = 0;
1396 bo->bcm_proc_read = NULL;
1398 INIT_LIST_HEAD(&bo->tx_ops);
1399 INIT_LIST_HEAD(&bo->rx_ops);
1402 bo->notifier.notifier_call = bcm_notifier;
1404 register_netdevice_notifier(&bo->notifier);
1410 * standard socket functions
1412 static int bcm_release(struct socket *sock)
1414 struct sock *sk = sock->sk;
1415 struct bcm_sock *bo = bcm_sk(sk);
1416 struct bcm_op *op, *next;
1418 /* remove bcm_ops, timer, rx_unregister(), etc. */
1420 unregister_netdevice_notifier(&bo->notifier);
1424 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1427 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1429 * Don't care if we're bound or not (due to netdev problems)
1430 * can_rx_unregister() is always a save thing to do here.
1434 * Only remove subscriptions that had not
1435 * been removed due to NETDEV_UNREGISTER
1438 if (op->rx_reg_dev) {
1439 struct net_device *dev;
1441 dev = dev_get_by_index(&init_net, op->ifindex);
1443 bcm_rx_unreg(dev, op);
1448 can_rx_unregister(NULL, op->can_id,
1449 REGMASK(op->can_id),
1450 bcm_rx_handler, op);
1455 /* remove procfs entry */
1456 if (proc_dir && bo->bcm_proc_read)
1457 remove_proc_entry(bo->procname, proc_dir);
1459 /* remove device reference */
1471 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1474 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1475 struct sock *sk = sock->sk;
1476 struct bcm_sock *bo = bcm_sk(sk);
1481 /* bind a device to this socket */
1482 if (addr->can_ifindex) {
1483 struct net_device *dev;
1485 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1489 if (dev->type != ARPHRD_CAN) {
1494 bo->ifindex = dev->ifindex;
1498 /* no interface reference for ifindex = 0 ('any' CAN device) */
1505 /* unique socket address as filename */
1506 sprintf(bo->procname, "%p", sock);
1507 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1515 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1516 struct msghdr *msg, size_t size, int flags)
1518 struct sock *sk = sock->sk;
1519 struct sk_buff *skb;
1524 noblock = flags & MSG_DONTWAIT;
1525 flags &= ~MSG_DONTWAIT;
1526 skb = skb_recv_datagram(sk, flags, noblock, &error);
1530 if (skb->len < size)
1533 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1535 skb_free_datagram(sk, skb);
1539 sock_recv_timestamp(msg, sk, skb);
1541 if (msg->msg_name) {
1542 msg->msg_namelen = sizeof(struct sockaddr_can);
1543 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1546 skb_free_datagram(sk, skb);
1551 static struct proto_ops bcm_ops __read_mostly = {
1553 .release = bcm_release,
1554 .bind = sock_no_bind,
1555 .connect = bcm_connect,
1556 .socketpair = sock_no_socketpair,
1557 .accept = sock_no_accept,
1558 .getname = sock_no_getname,
1559 .poll = datagram_poll,
1560 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1561 .listen = sock_no_listen,
1562 .shutdown = sock_no_shutdown,
1563 .setsockopt = sock_no_setsockopt,
1564 .getsockopt = sock_no_getsockopt,
1565 .sendmsg = bcm_sendmsg,
1566 .recvmsg = bcm_recvmsg,
1567 .mmap = sock_no_mmap,
1568 .sendpage = sock_no_sendpage,
1571 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1572 static struct proto bcm_proto __read_mostly = {
1574 .owner = THIS_MODULE,
1575 .obj_size = sizeof(struct bcm_sock),
1579 static struct can_proto bcm_can_proto __read_mostly = {
1581 .protocol = CAN_BCM,
1587 static struct can_proto bcm_can_proto __read_mostly = {
1589 .protocol = CAN_BCM,
1592 .owner = THIS_MODULE,
1593 .obj_size = sizeof(struct bcm_sock),
1598 static int __init bcm_module_init(void)
1604 err = can_proto_register(&bcm_can_proto);
1606 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1610 /* create /proc/net/can-bcm directory */
1611 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1612 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1614 proc_dir = proc_mkdir("can-bcm", proc_net);
1618 proc_dir->owner = THIS_MODULE;
1623 static void __exit bcm_module_exit(void)
1625 can_proto_unregister(&bcm_can_proto);
1628 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1629 proc_net_remove(&init_net, "can-bcm");
1631 proc_net_remove("can-bcm");
1635 module_init(bcm_module_init);
1636 module_exit(bcm_module_exit);