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, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/autoconf.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
58 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
61 /* use of last_frames[index].can_dlc */
62 #define RX_RECV 0x40 /* received data for this element */
63 #define RX_THR 0x80 /* element not been sent due to throttle feature */
64 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
66 /* get best masking value for can_rx_register() for a given single can_id */
67 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
71 static __initdata const char banner[] = KERN_INFO
72 "CAN: broadcast manager (bcm) socket protocol " CAN_VERSION "\n";
74 MODULE_DESCRIPTION("PF_CAN bcm sockets");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
78 #ifdef CONFIG_CAN_DEBUG_CORE
80 module_param(debug, int, S_IRUGO);
81 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
84 #define GET_U64(p) (*(u64*)(p)->data) /* easy access */
87 struct list_head list;
91 unsigned long j_ival1, j_ival2, j_lastmsg;
92 unsigned long frames_abs, frames_filtered;
93 struct timer_list timer, thrtimer;
94 struct timeval ival1, ival2;
95 struct timeval rx_stamp;
100 struct can_frame *frames;
101 struct can_frame *last_frames;
108 struct list_head rx_ops;
109 struct list_head tx_ops;
110 unsigned long dropped_usr_msgs;
111 struct proc_dir_entry *bcm_proc_read;
112 char procname [9]; /* pointer printed in ASCII with \0 */
115 static struct proc_dir_entry *proc_dir = NULL;
117 #ifdef CONFIG_CAN_BCM_USER
120 #define BCM_CAP CAP_NET_RAW
123 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
129 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
132 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
135 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
136 static void *kzalloc(size_t size, unsigned int __nocast flags)
138 void *ret = kmalloc(size, flags);
140 memset(ret, 0, size);
144 static inline void skb_get_timestamp(const struct sk_buff *skb,
145 struct timeval *stamp)
147 stamp->tv_sec = skb->stamp.tv_sec;
148 stamp->tv_usec = skb->stamp.tv_usec;
151 static inline void skb_set_timestamp(struct sk_buff *skb,
152 const struct timeval *stamp)
154 skb->stamp.tv_sec = stamp->tv_sec;
155 skb->stamp.tv_usec = stamp->tv_usec;
159 #define CFSIZ sizeof(struct can_frame)
160 #define OPSIZ sizeof(struct bcm_op)
161 #define MHSIZ sizeof(struct bcm_msg_head)
163 /* procfs functions */
165 static char *bcm_proc_getifname(int ifindex)
167 struct net_device *dev;
172 dev = __dev_get_by_index(ifindex); /* no usage counting */
179 static int bcm_read_proc(char *page, char **start, off_t off,
180 int count, int *eof, void *data)
183 struct sock *sk = (struct sock *)data;
184 struct bcm_opt *bo = bcm_sk(sk);
187 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
189 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
190 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
191 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
192 bo->dropped_usr_msgs);
193 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
194 bcm_proc_getifname(bo->ifindex));
195 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
197 list_for_each_entry(op, &bo->rx_ops, list) {
199 unsigned long reduction;
201 /* print only active entries & prevent division by zero */
205 len += snprintf(page + len, PAGE_SIZE - len,
207 op->can_id, bcm_proc_getifname(op->ifindex));
208 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
210 (op->flags & RX_CHECK_DLC)?'d':' ');
212 len += snprintf(page + len, PAGE_SIZE - len,
213 "timeo=%ld ", op->j_ival1);
216 len += snprintf(page + len, PAGE_SIZE - len,
217 "thr=%ld ", op->j_ival2);
219 len += snprintf(page + len, PAGE_SIZE - len,
220 "# recv %ld (%ld) => reduction: ",
221 op->frames_filtered, op->frames_abs);
223 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
225 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
226 (reduction == 100)?"near ":"", reduction);
228 if (len > PAGE_SIZE - 200) {
229 /* mark output cut off */
230 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
235 list_for_each_entry(op, &bo->tx_ops, list) {
237 len += snprintf(page + len, PAGE_SIZE - len,
238 "tx_op: %03X %s [%d] ",
239 op->can_id, bcm_proc_getifname(op->ifindex),
242 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
246 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
249 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
252 if (len > PAGE_SIZE - 100) {
253 /* mark output cut off */
254 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
259 len += snprintf(page + len, PAGE_SIZE - len, "\n");
265 /* bcm_op handling tx path */
267 static void bcm_can_tx(struct bcm_op *op)
270 struct net_device *dev;
271 struct can_frame *cf = &op->frames[op->currframe];
273 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
275 /* no target device? => exit */
279 dev = dev_get_by_index(op->ifindex);
282 /* RFC: should this bcm_op remove itself here? */
286 skb = alloc_skb(CFSIZ,
287 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
292 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
294 /* send with loopback */
299 /* update statistics */
303 /* reached last frame? */
304 if (op->currframe >= op->nframes)
310 /* bcm_op handling rx path */
311 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
312 struct can_frame *frames, struct timeval *tv)
315 struct can_frame *firstframe;
316 struct sock *sk = op->sk;
317 int datalen = head->nframes * CFSIZ;
318 struct sockaddr_can *addr;
321 skb = alloc_skb(sizeof(*head) + datalen,
322 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
326 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
328 /* can_frames starting here */
329 firstframe = (struct can_frame *) skb->tail;
332 skb_set_timestamp(skb, tv); /* restore timestamp */
334 addr = (struct sockaddr_can *)skb->cb;
335 memset(addr, 0, sizeof(*addr));
336 addr->can_family = AF_CAN;
337 /* restore originator for recvfrom() */
338 addr->can_ifindex = op->rx_ifindex;
341 memcpy(skb_put(skb, datalen), frames, datalen);
344 * the BCM uses the can_dlc-element of the can_frame
345 * structure for internal purposes. This is only
346 * relevant for updates that are generated by the
347 * BCM, where nframes is 1
349 if (head->nframes == 1)
350 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
353 err = sock_queue_rcv_skb(sk, skb);
355 struct bcm_opt *bo = bcm_sk(sk);
357 DBG("sock_queue_rcv_skb failed: %d\n", err);
359 /* don't care about overflows in this statistic */
360 bo->dropped_usr_msgs++;
365 static void bcm_tx_timeout_handler(unsigned long data)
367 struct bcm_op *op = (struct bcm_op*)data;
369 DBG("Called with bcm_op %p\n", op);
371 if (op->j_ival1 && (op->count > 0)) {
375 if (!op->count && (op->flags & TX_COUNTEVT)) {
376 /* create notification to user */
378 struct bcm_msg_head msg_head;
380 DBG("sending TX_EXPIRED for can_id %03X\n",
383 msg_head.opcode = TX_EXPIRED;
384 msg_head.flags = op->flags;
385 msg_head.count = op->count;
386 msg_head.ival1 = op->ival1;
387 msg_head.ival2 = op->ival2;
388 msg_head.can_id = op->can_id;
389 msg_head.nframes = 0;
391 bcm_send_to_user(op, &msg_head, NULL, NULL);
395 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
396 op->count, op->j_ival1, op->j_ival2);
398 if (op->j_ival1 && (op->count > 0)) {
400 op->timer.expires = jiffies + op->j_ival1;
401 add_timer(&op->timer);
403 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
405 (char*) op->timer.data,
406 (unsigned int) op->timer.expires);
408 /* send (next) frame */
412 op->timer.expires = jiffies + op->j_ival2;
413 add_timer(&op->timer);
415 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
417 (char*) op->timer.data,
418 (unsigned int) op->timer.expires);
420 /* send (next) frame */
423 DBG("no timer restart\n");
430 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
432 struct bcm_msg_head head;
434 op->j_lastmsg = jiffies;
436 /* update statistics */
437 op->frames_filtered++;
439 /* prevent statistics overflow */
440 if (op->frames_filtered > ULONG_MAX/100)
441 op->frames_filtered = op->frames_abs = 0;
443 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
444 (unsigned int) op->j_lastmsg, op);
445 DBG("sending notification\n");
447 head.opcode = RX_CHANGED;
448 head.flags = op->flags;
449 head.count = op->count;
450 head.ival1 = op->ival1;
451 head.ival2 = op->ival2;
452 head.can_id = op->can_id;
455 bcm_send_to_user(op, &head, data, &op->rx_stamp);
458 static void bcm_rx_update_and_send(struct bcm_op *op,
459 struct can_frame *lastdata,
460 struct can_frame *rxdata)
462 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
464 memcpy(lastdata, rxdata, CFSIZ);
467 lastdata->can_dlc |= RX_RECV;
469 /* throttle bcm_rx_changed ? */
470 if ((op->thrtimer.expires) ||
471 ((op->j_ival2) && (nexttx > jiffies))) {
472 /* we are already waiting OR we have to start waiting */
474 /* mark as 'throttled' */
475 lastdata->can_dlc |= RX_THR;
477 if (!(op->thrtimer.expires)) {
478 /* start the timer only the first time */
479 op->thrtimer.expires = nexttx;
480 add_timer(&op->thrtimer);
482 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
483 op->thrtimer.function,
484 (char*) op->thrtimer.data,
485 (unsigned int) op->thrtimer.expires);
488 /* send RX_CHANGED to the user */
489 bcm_rx_changed(op, rxdata);
493 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
494 struct can_frame *rxdata)
497 * no one uses the MSBs of can_dlc for comparation,
498 * so we use it here to detect the first time of reception
501 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
502 /* received data for the first time => send update to user */
503 DBG("first time :)\n");
504 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
508 /* do a real check in can_data */
510 DBG("op->frames[index].data = 0x%016llx\n",
511 GET_U64(&op->frames[index]));
512 DBG("op->last_frames[index].data = 0x%016llx\n",
513 GET_U64(&op->last_frames[index]));
514 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
516 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
517 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
518 DBG("relevant data change :)\n");
519 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
524 if (op->flags & RX_CHECK_DLC) {
526 /* do a real check in dlc */
528 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
530 DBG("dlc change :)\n");
531 bcm_rx_update_and_send(op, &op->last_frames[index],
536 DBG("no relevant change :(\n");
539 static void bcm_rx_starttimer(struct bcm_op *op)
541 if (op->flags & RX_NO_AUTOTIMER)
546 op->timer.expires = jiffies + op->j_ival1;
548 DBG("adding rx timeout timer ival1. func=%p data=%p "
551 (char*) op->timer.data,
552 (unsigned int) op->timer.expires);
554 add_timer(&op->timer);
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 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
565 msg_head.opcode = RX_TIMEOUT;
566 msg_head.flags = op->flags;
567 msg_head.count = op->count;
568 msg_head.ival1 = op->ival1;
569 msg_head.ival2 = op->ival2;
570 msg_head.can_id = op->can_id;
571 msg_head.nframes = 0;
573 bcm_send_to_user(op, &msg_head, NULL, NULL);
575 /* no restart of the timer is done here! */
577 /* if user wants to be informed, when cyclic CAN-Messages come back */
578 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
579 /* clear received can_frames to indicate 'nothing received' */
580 memset(op->last_frames, 0, op->nframes * CFSIZ);
581 DBG("RX_ANNOUNCE_RESTART\n");
586 static void bcm_rx_thr_handler(unsigned long data)
588 struct bcm_op *op = (struct bcm_op*)data;
591 /* mark disabled / consumed timer */
592 op->thrtimer.expires = 0;
594 if (op->nframes > 1){
596 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
598 /* for MUX filter we start at index 1 */
599 for (i=1; i<op->nframes; i++){
600 if ((op->last_frames) &&
601 (op->last_frames[i].can_dlc & RX_THR)){
602 op->last_frames[i].can_dlc &= ~RX_THR;
603 bcm_rx_changed(op, &op->last_frames[i]);
608 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
610 /* for RX_FILTER_ID and simple filter */
611 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
612 op->last_frames[0].can_dlc &= ~RX_THR;
613 bcm_rx_changed(op, &op->last_frames[0]);
619 static void bcm_rx_handler(struct sk_buff *skb, void *data)
621 struct bcm_op *op = (struct bcm_op*)data;
622 struct can_frame rxframe;
625 /* disable timeout */
626 del_timer(&op->timer);
628 DBG("Called with bcm_op %p\n", op);
630 if (skb->len == sizeof(rxframe)) {
631 memcpy(&rxframe, skb->data, sizeof(rxframe));
632 /* save rx timestamp */
633 skb_get_timestamp(skb, &op->rx_stamp);
634 /* save originator for recvfrom() */
635 op->rx_ifindex = skb->dev->ifindex;
636 /* update statistics */
639 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
641 DBG("Wrong skb->len = %d\n", skb->len);
646 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
648 if (op->can_id != rxframe.can_id) {
649 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
650 rxframe.can_id, op->can_id);
654 if (op->flags & RX_RTR_FRAME) {
655 /* send reply for RTR-request */
656 DBG("RTR-request\n");
658 /* send op->frames[0] to CAN device */
663 if (op->flags & RX_FILTER_ID) {
664 /* the easiest case */
665 DBG("Easy does it with RX_FILTER_ID\n");
667 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
668 bcm_rx_starttimer(op);
672 if (op->nframes == 1) {
673 /* simple compare with index 0 */
674 DBG("Simple compare\n");
676 bcm_rx_cmp_to_index(op, 0, &rxframe);
677 bcm_rx_starttimer(op);
681 if (op->nframes > 1) {
682 /* multiplex compare */
683 DBG("Multiplex compare\n");
686 * find the first multiplex mask that fits.
687 * Remark: The MUX-mask is stored in index 0
690 for (i=1; i < op->nframes; i++) {
692 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
693 (GET_U64(&op->frames[0]) &
694 GET_U64(&op->frames[i]))) {
695 DBG("found MUX index %d\n", i);
696 bcm_rx_cmp_to_index(op, i, &rxframe);
700 bcm_rx_starttimer(op);
704 /* bcm_op handling: find & delete bcm_op elements */
706 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
711 list_for_each_entry(op, ops, list) {
712 if ((op->can_id == can_id) && (op->ifindex == ifindex))
719 static void bcm_remove_op(struct bcm_op *op)
721 del_timer(&op->timer);
722 del_timer(&op->thrtimer);
726 kfree(op->last_frames);
732 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
734 struct bcm_op *op, *n;
736 list_for_each_entry_safe(op, n, ops, list) {
737 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
738 DBG("removing rx_op %p for can_id %03X\n",
742 * Don't care if we're bound or not (due to netdev
743 * problems) can_rx_unregister() is always a save
747 struct net_device *dev =
748 dev_get_by_index(op->ifindex);
751 can_rx_unregister(dev, op->can_id,
757 can_rx_unregister(NULL, op->can_id,
767 return 0; /* not found */
770 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
772 struct bcm_op *op, *n;
774 list_for_each_entry_safe(op, n, ops, list) {
775 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
776 DBG("removing rx_op %p for can_id %03X\n",
784 return 0; /* not found */
787 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
793 op = bcm_find_op(ops, msg_head->can_id, ifindex);
796 DBG("TRX_READ: sending status for can_id %03X\n",
798 /* put current values into msg_head */
799 msg_head->flags = op->flags;
800 msg_head->count = op->count;
801 msg_head->ival1 = op->ival1;
802 msg_head->ival2 = op->ival2;
803 msg_head->nframes = op->nframes;
805 bcm_send_to_user(op, msg_head, op->frames, NULL);
811 DBG("TRX_READ: did not find op for can_id %03X\n",
819 /* helper functions for bcm_sendmsg() */
821 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
822 int ifindex, struct sock *sk)
824 struct bcm_opt *bo = bcm_sk(sk);
828 /* we need a real device to send frames */
832 /* we need at least one can_frame */
833 if (msg_head->nframes < 1)
836 /* check the given can_id */
838 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
841 /* update existing BCM operation */
843 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
844 op, msg_head->can_id);
847 * Do we need more space for the can_frames than currently
848 * allocated? -> This is a _really_ unusual use-case and
849 * therefore (complexity / locking) it is not supported.
851 if (msg_head->nframes > op->nframes)
854 /* update can_frames content */
855 for (i = 0; i < msg_head->nframes; i++) {
856 err = memcpy_fromiovec((u8*)&op->frames[i],
857 msg->msg_iov, CFSIZ);
861 if (msg_head->flags & TX_CP_CAN_ID) {
862 /* copy can_id into frame */
863 op->frames[i].can_id = msg_head->can_id;
868 /* insert new BCM operation for the given can_id */
870 op = kzalloc(OPSIZ, GFP_KERNEL);
874 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
875 op, msg_head->can_id);
877 op->can_id = msg_head->can_id;
879 /* create array for can_frames and copy the data */
880 op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL);
886 for (i = 0; i < msg_head->nframes; i++) {
887 err = memcpy_fromiovec((u8*)&op->frames[i],
888 msg->msg_iov, CFSIZ);
895 if (msg_head->flags & TX_CP_CAN_ID) {
896 /* copy can_id into frame */
897 op->frames[i].can_id = msg_head->can_id;
901 /* tx_ops never compare with previous received messages */
902 op->last_frames = NULL;
904 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
907 op->ifindex = ifindex;
909 /* initialize uninitialized (kmalloc) structure */
910 init_timer(&op->timer);
912 /* currently unused in tx_ops */
913 init_timer(&op->thrtimer);
915 /* handler for tx_ops */
916 op->timer.function = bcm_tx_timeout_handler;
918 /* timer.data points to this op-structure */
919 op->timer.data = (unsigned long)op;
921 /* add this bcm_op to the list of the tx_ops */
922 list_add(&op->list, &bo->tx_ops);
924 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
926 if (op->nframes != msg_head->nframes) {
927 op->nframes = msg_head->nframes;
928 /* start multiple frame transmission with index 0 */
934 op->flags = msg_head->flags;
936 if (op->flags & TX_RESET_MULTI_IDX) {
937 /* start multiple frame transmission with index 0 */
941 if (op->flags & SETTIMER) {
943 /* set timer values */
945 op->count = msg_head->count;
946 op->ival1 = msg_head->ival1;
947 op->ival2 = msg_head->ival2;
948 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
949 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
951 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
952 op->count, op->j_ival1, op->j_ival2);
954 /* disable an active timer due to zero values? */
955 if (!op->j_ival1 && !op->j_ival2) {
956 del_timer(&op->timer);
957 DBG("TX_SETUP: SETTIMER disabled timer.\n");
961 if ((op->flags & STARTTIMER) &&
962 ((op->j_ival1 && op->count) || op->j_ival2)) {
964 del_timer(&op->timer);
966 /* spec: send can_frame when starting timer */
967 op->flags |= TX_ANNOUNCE;
969 if (op->j_ival1 && (op->count > 0)){
970 op->timer.expires = jiffies + op->j_ival1;
971 /* op->count-- is done in bcm_tx_timeout_handler */
972 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
975 (char*) op->timer.data,
976 (unsigned int) op->timer.expires);
978 op->timer.expires = jiffies + op->j_ival2;
979 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
982 (char*) op->timer.data,
983 (unsigned int) op->timer.expires);
986 add_timer(&op->timer);
989 if (op->flags & TX_ANNOUNCE)
992 return msg_head->nframes * CFSIZ + MHSIZ;
995 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
996 int ifindex, struct sock *sk)
998 struct bcm_opt *bo = bcm_sk(sk);
1003 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1004 /* be robust against wrong usage ... */
1005 msg_head->flags |= RX_FILTER_ID;
1006 msg_head->nframes = 0; /* ignore trailing garbage */
1009 if ((msg_head->flags & RX_RTR_FRAME) &&
1010 ((msg_head->nframes != 1) ||
1011 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1013 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1017 /* check the given can_id */
1018 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1021 /* update existing BCM operation */
1023 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1024 op, msg_head->can_id);
1027 * Do we need more space for the can_frames than currently
1028 * allocated? -> This is a _really_ unusual use-case and
1029 * therefore (complexity / locking) it is not supported.
1031 if (msg_head->nframes > op->nframes)
1034 if (msg_head->nframes) {
1035 /* update can_frames content */
1036 err = memcpy_fromiovec((u8*)op->frames,
1038 msg_head->nframes * CFSIZ);
1042 /* clear last_frames to indicate 'nothing received' */
1043 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1046 op->nframes = msg_head->nframes;
1047 /* Only an update -> do not call can_rx_register() */
1051 /* insert new BCM operation for the given can_id */
1053 op = kzalloc(OPSIZ, GFP_KERNEL);
1057 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1058 op, msg_head->can_id);
1060 op->can_id = msg_head->can_id;
1061 op->nframes = msg_head->nframes;
1063 if (msg_head->nframes) {
1065 /* create array for can_frames and copy the data */
1066 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1073 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1074 msg_head->nframes * CFSIZ);
1081 /* create and init array for received can_frames */
1082 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1084 if (!op->last_frames) {
1090 /* op->frames = NULL due to memset in kzalloc() */
1093 * even when we have the RX_FILTER_ID case, we need
1094 * to store the last frame for the throttle feature
1097 /* create and init array for received can_frames */
1098 op->last_frames = kzalloc(CFSIZ, GFP_KERNEL);
1099 if (!op->last_frames) {
1105 op->sk = sk; /* bcm_delete_rx_op() needs this */
1106 op->ifindex = ifindex;
1108 /* initialize uninitialized (kmalloc) structure */
1109 init_timer(&op->timer);
1111 /* init throttle timer for RX_CHANGED */
1112 init_timer(&op->thrtimer);
1114 /* handler for rx timeouts */
1115 op->timer.function = bcm_rx_timeout_handler;
1117 /* timer.data points to this op-structure */
1118 op->timer.data = (unsigned long)op;
1120 /* handler for RX_CHANGED throttle timeouts */
1121 op->thrtimer.function = bcm_rx_thr_handler;
1123 /* timer.data points to this op-structure */
1124 op->thrtimer.data = (unsigned long)op;
1126 op->thrtimer.expires = 0; /* mark disabled timer */
1128 /* add this bcm_op to the list of the tx_ops */
1129 list_add(&op->list, &bo->rx_ops);
1131 do_rx_register = 1; /* call can_rx_register() */
1133 } /* 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 del_timer(&op->thrtimer);
1144 del_timer(&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 */
1160 op->ival1 = msg_head->ival1;
1161 op->ival2 = msg_head->ival2;
1162 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
1163 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
1165 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1166 op->j_ival1, op->j_ival2);
1168 /* disable an active timer due to zero value? */
1170 del_timer(&op->timer);
1171 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1174 /* free currently blocked msgs ? */
1175 if (op->thrtimer.expires) {
1176 DBG("RX_SETUP: unblocking throttled msgs.\n");
1177 del_timer(&op->thrtimer);
1178 /* send blocked msgs hereafter */
1179 op->thrtimer.expires = jiffies + 2;
1180 add_timer(&op->thrtimer);
1183 * if (op->j_ival2) is zero, no (new) throttling
1184 * will happen. For details see functions
1185 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1189 if ((op->flags & STARTTIMER) && op->j_ival1) {
1191 del_timer(&op->timer);
1193 op->timer.expires = jiffies + op->j_ival1;
1195 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1197 (char *) op->timer.function,
1198 (char *) op->timer.data,
1199 (unsigned int) op->timer.expires);
1201 add_timer(&op->timer);
1205 /* now we can register for can_ids, if we added a new bcm_op */
1206 if (do_rx_register) {
1207 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1208 "rx_op is %p\n", op->can_id, op);
1211 struct net_device *dev = dev_get_by_index(ifindex);
1214 can_rx_register(dev, op->can_id,
1215 REGMASK(op->can_id),
1216 bcm_rx_handler, op, IDENT);
1220 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1221 bcm_rx_handler, op, IDENT);
1224 return msg_head->nframes * CFSIZ + MHSIZ;
1227 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1229 struct sk_buff *skb;
1230 struct net_device *dev;
1233 /* just copy and send one can_frame */
1235 if (!ifindex) /* we need a real device to send frames */
1238 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1243 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1249 DBG_FRAME("BCM: TX_SEND: sending frame",
1250 (struct can_frame *)skb->data);
1252 dev = dev_get_by_index(ifindex);
1260 can_send(skb, 1); /* send with loopback */
1263 return CFSIZ + MHSIZ;
1267 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1268 struct msghdr *msg, size_t size)
1270 struct sock *sk = sock->sk;
1271 struct bcm_opt *bo = bcm_sk(sk);
1272 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1273 struct bcm_msg_head msg_head;
1274 int ret; /* read bytes or error codes as return value */
1277 DBG("sock %p not bound\n", sk);
1281 /* check for alternative ifindex for this bcm_op */
1283 if (!ifindex && msg->msg_name) {
1284 /* no bound device as default => check msg_name */
1285 struct sockaddr_can *addr =
1286 (struct sockaddr_can *)msg->msg_name;
1288 if (addr->can_family != AF_CAN)
1291 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1293 if (ifindex && !dev_get_by_index(ifindex)) {
1294 DBG("device %d not found\n", ifindex);
1299 /* read message head information */
1301 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1305 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1307 switch (msg_head.opcode) {
1311 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1316 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1321 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1329 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1337 /* reuse msg_head for the reply to TX_READ */
1338 msg_head.opcode = TX_STATUS;
1339 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1344 /* reuse msg_head for the reply to RX_READ */
1345 msg_head.opcode = RX_STATUS;
1346 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1351 /* we need at least one can_frame */
1352 if (msg_head.nframes < 1)
1355 ret = bcm_tx_send(msg, ifindex, sk);
1360 DBG("Unknown opcode %d\n", msg_head.opcode);
1368 /* initial settings at socket creation time */
1370 static int bcm_init(struct sock *sk)
1372 struct bcm_opt *bo = bcm_sk(sk);
1376 bo->dropped_usr_msgs = 0;
1377 bo->bcm_proc_read = NULL;
1379 INIT_LIST_HEAD(&bo->tx_ops);
1380 INIT_LIST_HEAD(&bo->rx_ops);
1385 static void bcm_notifier(unsigned long msg, void *data)
1387 struct sock *sk = (struct sock *)data;
1388 struct bcm_opt *bo = bcm_sk(sk);
1390 DBG("called for sock %p\n", sk);
1393 case NETDEV_UNREGISTER:
1398 sk->sk_err = ENETDOWN;
1399 if (!sock_flag(sk, SOCK_DEAD))
1400 sk->sk_error_report(sk);
1404 /* standard socket functions */
1406 static int bcm_release(struct socket *sock)
1408 struct sock *sk = sock->sk;
1409 struct bcm_opt *bo = bcm_sk(sk);
1410 struct bcm_op *op, *next;
1412 DBG("socket %p, sk %p\n", sock, sk);
1414 /* remove bcm_ops, timer, rx_unregister(), etc. */
1416 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1417 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1421 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1422 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1425 * Don't care if we're bound or not (due to netdev problems)
1426 * can_rx_unregister() is always a save thing to do here.
1429 struct net_device *dev = dev_get_by_index(op->ifindex);
1432 can_rx_unregister(dev, op->can_id,
1433 REGMASK(op->can_id),
1434 bcm_rx_handler, op);
1438 can_rx_unregister(NULL, op->can_id,
1439 REGMASK(op->can_id),
1440 bcm_rx_handler, op);
1445 /* remove procfs entry */
1446 if ((proc_dir) && (bo->bcm_proc_read)) {
1447 remove_proc_entry(bo->procname, proc_dir);
1450 /* remove device notifier */
1452 struct net_device *dev = dev_get_by_index(bo->ifindex);
1455 can_dev_unregister(dev, bcm_notifier, sk);
1465 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1468 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1469 struct sock *sk = sock->sk;
1470 struct bcm_opt *bo = bcm_sk(sk);
1475 /* bind a device to this socket */
1476 if (addr->can_ifindex) {
1477 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1480 DBG("could not find device index %d\n",
1484 bo->ifindex = dev->ifindex;
1485 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1488 DBG("socket %p bound to device %s (idx %d)\n",
1489 sock, dev->name, dev->ifindex);
1491 /* no notifier for ifindex = 0 ('any' CAN device) */
1498 /* unique socket address as filename */
1499 sprintf(bo->procname, "%p", sock);
1500 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1508 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1509 struct msghdr *msg, size_t size, int flags)
1511 struct sock *sk = sock->sk;
1512 struct sk_buff *skb;
1517 DBG("socket %p, sk %p\n", sock, sk);
1519 noblock = flags & MSG_DONTWAIT;
1520 flags &= ~MSG_DONTWAIT;
1521 skb = skb_recv_datagram(sk, flags, noblock, &error);
1525 DBG("delivering skbuff %p\n", skb);
1528 if (skb->len < size)
1531 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1533 skb_free_datagram(sk, skb);
1537 sock_recv_timestamp(msg, sk, skb);
1539 if (msg->msg_name) {
1540 msg->msg_namelen = sizeof(struct sockaddr_can);
1541 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1544 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1545 skb_free_datagram(sk, skb);
1550 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1553 unsigned int mask = 0;
1555 DBG("socket %p\n", sock);
1557 mask = datagram_poll(file, sock, wait);
1561 static struct proto_ops bcm_ops = {
1563 .release = bcm_release,
1564 .bind = sock_no_bind,
1565 .connect = bcm_connect,
1566 .socketpair = sock_no_socketpair,
1567 .accept = sock_no_accept,
1568 .getname = sock_no_getname,
1570 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1571 .listen = sock_no_listen,
1572 .shutdown = sock_no_shutdown,
1573 .setsockopt = sock_no_setsockopt,
1574 .getsockopt = sock_no_getsockopt,
1575 .sendmsg = bcm_sendmsg,
1576 .recvmsg = bcm_recvmsg,
1577 .mmap = sock_no_mmap,
1578 .sendpage = sock_no_sendpage,
1581 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1582 static struct proto bcm_proto = {
1584 .owner = THIS_MODULE,
1585 .obj_size = sizeof(struct bcm_sock),
1589 static struct can_proto bcm_can_proto = {
1591 .protocol = CAN_BCM,
1592 .capability = BCM_CAP,
1597 static struct can_proto bcm_can_proto = {
1599 .protocol = CAN_BCM,
1600 .capability = BCM_CAP,
1602 .owner = THIS_MODULE,
1603 .obj_size = sizeof(struct bcm_opt),
1608 static int __init bcm_module_init(void)
1612 can_proto_register(&bcm_can_proto);
1614 /* create /proc/net/can/bcm directory */
1615 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1618 proc_dir->owner = THIS_MODULE;
1623 static void __exit bcm_module_exit(void)
1625 can_proto_unregister(&bcm_can_proto);
1628 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1632 module_init(bcm_module_init);
1633 module_exit(bcm_module_exit);