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 #define CAN_BCM_VERSION CAN_VERSION
72 static __initdata const char banner[] = KERN_INFO
73 "can: broadcast manager protocol # rev " CAN_BCM_VERSION "\n";
75 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 #ifdef CONFIG_CAN_DEBUG_CORE
81 module_param(debug, int, S_IRUGO);
82 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
85 /* easy access to can_frame payload */
86 static inline u64 GET_U64(const struct can_frame *cp)
88 return *(u64*)cp->data;
92 struct list_head list;
96 unsigned long j_ival1, j_ival2, j_lastmsg;
97 unsigned long frames_abs, frames_filtered;
98 struct timer_list timer, thrtimer;
99 struct timeval ival1, ival2;
100 struct timeval rx_stamp;
105 struct can_frame *frames;
106 struct can_frame *last_frames;
107 struct can_frame sframe;
108 struct can_frame last_sframe;
115 struct list_head rx_ops;
116 struct list_head tx_ops;
117 unsigned long dropped_usr_msgs;
118 struct proc_dir_entry *bcm_proc_read;
119 char procname [9]; /* pointer printed in ASCII with \0 */
122 static struct proc_dir_entry *proc_dir = NULL;
124 #ifdef CONFIG_CAN_BCM_USER
127 #define BCM_CAP CAP_NET_RAW
130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
136 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
138 return &((struct bcm_sock *)sk)->opt;
141 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
143 return (struct bcm_opt *)sk->sk_protinfo;
147 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
148 static void *kzalloc(size_t size, unsigned int __nocast flags)
150 void *ret = kmalloc(size, flags);
153 memset(ret, 0, size);
158 static inline void skb_get_timestamp(const struct sk_buff *skb,
159 struct timeval *stamp)
161 stamp->tv_sec = skb->stamp.tv_sec;
162 stamp->tv_usec = skb->stamp.tv_usec;
165 static inline void skb_set_timestamp(struct sk_buff *skb,
166 const struct timeval *stamp)
168 skb->stamp.tv_sec = stamp->tv_sec;
169 skb->stamp.tv_usec = stamp->tv_usec;
173 #define CFSIZ sizeof(struct can_frame)
174 #define OPSIZ sizeof(struct bcm_op)
175 #define MHSIZ sizeof(struct bcm_msg_head)
178 * rounded_tv2jif - calculate jiffies from timeval including optional up
179 * @tv: pointer to timeval
182 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
183 * function is intentionally more relaxed on precise timer ticks to get exact
184 * one jiffy for requested 1000us on a 1000HZ machine.
187 * calculated jiffies (max: ULONG_MAX)
189 static unsigned long rounded_tv2jif(const struct timeval *tv)
191 unsigned long sec = tv->tv_sec;
192 unsigned long usec = tv->tv_usec;
195 if (sec > ULONG_MAX / HZ)
198 /* round up to get at least the requested time */
199 usec += 1000000 / HZ - 1;
201 jif = usec / (1000000 / HZ);
203 if (sec * HZ > ULONG_MAX - jif)
206 return jif + sec * HZ;
212 static char *bcm_proc_getifname(int ifindex)
214 struct net_device *dev;
219 dev = __dev_get_by_index(ifindex); /* no usage counting */
226 static int bcm_read_proc(char *page, char **start, off_t off,
227 int count, int *eof, void *data)
230 struct sock *sk = (struct sock *)data;
231 struct bcm_opt *bo = bcm_sk(sk);
234 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
236 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
237 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
238 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
239 bo->dropped_usr_msgs);
240 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
241 bcm_proc_getifname(bo->ifindex));
242 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
244 list_for_each_entry(op, &bo->rx_ops, list) {
246 unsigned long reduction;
248 /* print only active entries & prevent division by zero */
252 len += snprintf(page + len, PAGE_SIZE - len,
254 op->can_id, bcm_proc_getifname(op->ifindex));
255 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
257 (op->flags & RX_CHECK_DLC)?'d':' ');
259 len += snprintf(page + len, PAGE_SIZE - len,
260 "timeo=%ld ", op->j_ival1);
263 len += snprintf(page + len, PAGE_SIZE - len,
264 "thr=%ld ", op->j_ival2);
266 len += snprintf(page + len, PAGE_SIZE - len,
267 "# recv %ld (%ld) => reduction: ",
268 op->frames_filtered, op->frames_abs);
270 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
272 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
273 (reduction == 100)?"near ":"", reduction);
275 if (len > PAGE_SIZE - 200) {
276 /* mark output cut off */
277 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
282 list_for_each_entry(op, &bo->tx_ops, list) {
284 len += snprintf(page + len, PAGE_SIZE - len,
285 "tx_op: %03X %s [%d] ",
286 op->can_id, bcm_proc_getifname(op->ifindex),
289 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
293 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
296 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
299 if (len > PAGE_SIZE - 100) {
300 /* mark output cut off */
301 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
306 len += snprintf(page + len, PAGE_SIZE - len, "\n");
313 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
314 * of the given bcm tx op
316 static void bcm_can_tx(struct bcm_op *op)
319 struct net_device *dev;
320 struct can_frame *cf = &op->frames[op->currframe];
322 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
324 /* no target device? => exit */
328 dev = dev_get_by_index(op->ifindex);
331 /* RFC: should this bcm_op remove itself here? */
335 skb = alloc_skb(CFSIZ,
336 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
341 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
343 /* send with loopback */
348 /* update statistics */
352 /* reached last frame? */
353 if (op->currframe >= op->nframes)
360 * bcm_send_to_user - send a BCM message to the userspace
361 * (consisting of bcm_msg_head + x CAN frames)
363 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
364 struct can_frame *frames, struct timeval *tv)
367 struct can_frame *firstframe;
368 struct sock *sk = op->sk;
369 int datalen = head->nframes * CFSIZ;
370 struct sockaddr_can *addr;
373 skb = alloc_skb(sizeof(*head) + datalen,
374 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
378 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
380 /* can_frames starting here */
381 firstframe = (struct can_frame *) skb->tail;
384 skb_set_timestamp(skb, tv); /* restore timestamp */
386 addr = (struct sockaddr_can *)skb->cb;
387 memset(addr, 0, sizeof(*addr));
388 addr->can_family = AF_CAN;
389 /* restore originator for recvfrom() */
390 addr->can_ifindex = op->rx_ifindex;
393 memcpy(skb_put(skb, datalen), frames, datalen);
396 * the BCM uses the can_dlc-element of the can_frame
397 * structure for internal purposes. This is only
398 * relevant for updates that are generated by the
399 * BCM, where nframes is 1
401 if (head->nframes == 1)
402 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
405 err = sock_queue_rcv_skb(sk, skb);
407 struct bcm_opt *bo = bcm_sk(sk);
409 DBG("sock_queue_rcv_skb failed: %d\n", err);
411 /* don't care about overflows in this statistic */
412 bo->dropped_usr_msgs++;
417 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
419 static void bcm_tx_timeout_handler(unsigned long data)
421 struct bcm_op *op = (struct bcm_op*)data;
423 DBG("Called with bcm_op %p\n", op);
425 if (op->j_ival1 && (op->count > 0)) {
428 if (!op->count && (op->flags & TX_COUNTEVT)) {
429 struct bcm_msg_head msg_head;
431 /* create notification to user */
432 DBG("sending TX_EXPIRED for can_id %03X\n",
435 msg_head.opcode = TX_EXPIRED;
436 msg_head.flags = op->flags;
437 msg_head.count = op->count;
438 msg_head.ival1 = op->ival1;
439 msg_head.ival2 = op->ival2;
440 msg_head.can_id = op->can_id;
441 msg_head.nframes = 0;
443 bcm_send_to_user(op, &msg_head, NULL, NULL);
447 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
448 op->count, op->j_ival1, op->j_ival2);
450 if (op->j_ival1 && (op->count > 0)) {
452 op->timer.expires = jiffies + op->j_ival1;
453 add_timer(&op->timer);
455 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
457 (char*) op->timer.data,
458 (unsigned int) op->timer.expires);
460 /* send (next) frame */
465 op->timer.expires = jiffies + op->j_ival2;
466 add_timer(&op->timer);
468 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
470 (char*) op->timer.data,
471 (unsigned int) op->timer.expires);
473 /* send (next) frame */
477 DBG("no timer restart\n");
484 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
486 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
488 struct bcm_msg_head head;
490 op->j_lastmsg = jiffies;
492 /* update statistics */
493 op->frames_filtered++;
495 /* prevent statistics overflow */
496 if (op->frames_filtered > ULONG_MAX/100)
497 op->frames_filtered = op->frames_abs = 0;
499 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
500 (unsigned int) op->j_lastmsg, op);
501 DBG("sending notification\n");
503 head.opcode = RX_CHANGED;
504 head.flags = op->flags;
505 head.count = op->count;
506 head.ival1 = op->ival1;
507 head.ival2 = op->ival2;
508 head.can_id = op->can_id;
511 bcm_send_to_user(op, &head, data, &op->rx_stamp);
515 * bcm_rx_update_and_send - process a detected relevant receive content change
516 * 1. update the last received data
517 * 2. send a notification to the user (if possible)
519 static void bcm_rx_update_and_send(struct bcm_op *op,
520 struct can_frame *lastdata,
521 struct can_frame *rxdata)
523 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
525 memcpy(lastdata, rxdata, CFSIZ);
528 lastdata->can_dlc |= RX_RECV;
530 /* throttle bcm_rx_changed ? */
531 if ((op->thrtimer.expires) ||
532 ((op->j_ival2) && (nexttx > jiffies))) {
533 /* we are already waiting OR we have to start waiting */
535 /* mark as 'throttled' */
536 lastdata->can_dlc |= RX_THR;
538 if (!(op->thrtimer.expires)) {
539 /* start the timer only the first time */
540 op->thrtimer.expires = nexttx;
541 add_timer(&op->thrtimer);
543 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
544 op->thrtimer.function,
545 (char*) op->thrtimer.data,
546 (unsigned int) op->thrtimer.expires);
550 /* send RX_CHANGED to the user immediately */
551 bcm_rx_changed(op, rxdata);
556 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
557 * received data stored in op->last_frames[]
559 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
560 struct can_frame *rxdata)
563 * no one uses the MSBs of can_dlc for comparation,
564 * so we use it here to detect the first time of reception
567 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
568 /* received data for the first time => send update to user */
569 DBG("first time :)\n");
570 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
574 /* do a real check in can_frame data section */
576 DBG("op->frames[index].data = 0x%016llx\n",
577 GET_U64(&op->frames[index]));
578 DBG("op->last_frames[index].data = 0x%016llx\n",
579 GET_U64(&op->last_frames[index]));
580 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
582 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
583 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
584 DBG("relevant data change :)\n");
585 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
589 if (op->flags & RX_CHECK_DLC) {
590 /* do a real check in can_frame dlc */
591 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
593 DBG("dlc change :)\n");
594 bcm_rx_update_and_send(op, &op->last_frames[index],
599 DBG("no relevant change :(\n");
603 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
605 static void bcm_rx_starttimer(struct bcm_op *op)
607 if (op->flags & RX_NO_AUTOTIMER)
611 op->timer.expires = jiffies + op->j_ival1;
613 DBG("adding rx timeout timer ival1. func=%p data=%p "
616 (char*) op->timer.data,
617 (unsigned int) op->timer.expires);
619 add_timer(&op->timer);
624 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
626 static void bcm_rx_timeout_handler(unsigned long data)
628 struct bcm_op *op = (struct bcm_op*)data;
629 struct bcm_msg_head msg_head;
631 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
633 msg_head.opcode = RX_TIMEOUT;
634 msg_head.flags = op->flags;
635 msg_head.count = op->count;
636 msg_head.ival1 = op->ival1;
637 msg_head.ival2 = op->ival2;
638 msg_head.can_id = op->can_id;
639 msg_head.nframes = 0;
641 bcm_send_to_user(op, &msg_head, NULL, NULL);
643 /* no restart of the timer is done here! */
645 /* if user wants to be informed, when cyclic CAN-Messages come back */
646 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
647 /* clear received can_frames to indicate 'nothing received' */
648 memset(op->last_frames, 0, op->nframes * CFSIZ);
649 DBG("RX_ANNOUNCE_RESTART\n");
654 * bcm_rx_thr_handler - the time for blocked content updates is over now:
655 * Check for throttled data and send it to the userspace
657 static void bcm_rx_thr_handler(unsigned long data)
659 struct bcm_op *op = (struct bcm_op*)data;
662 /* mark disabled / consumed timer */
663 op->thrtimer.expires = 0;
665 if (op->nframes > 1) {
666 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
668 /* for MUX filter we start at index 1 */
669 for (i=1; i<op->nframes; i++) {
670 if ((op->last_frames) &&
671 (op->last_frames[i].can_dlc & RX_THR)) {
672 op->last_frames[i].can_dlc &= ~RX_THR;
673 bcm_rx_changed(op, &op->last_frames[i]);
678 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
680 /* for RX_FILTER_ID and simple filter */
681 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
682 op->last_frames[0].can_dlc &= ~RX_THR;
683 bcm_rx_changed(op, &op->last_frames[0]);
689 * bcm_rx_handler - handle a CAN frame receiption
691 static void bcm_rx_handler(struct sk_buff *skb, void *data)
693 struct bcm_op *op = (struct bcm_op*)data;
694 struct can_frame rxframe;
697 /* disable timeout */
698 del_timer(&op->timer);
700 DBG("Called with bcm_op %p\n", op);
702 if (skb->len == sizeof(rxframe)) {
703 memcpy(&rxframe, skb->data, sizeof(rxframe));
704 /* save rx timestamp */
705 skb_get_timestamp(skb, &op->rx_stamp);
706 /* save originator for recvfrom() */
707 op->rx_ifindex = skb->dev->ifindex;
708 /* update statistics */
711 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
714 DBG("Wrong skb->len = %d\n", skb->len);
719 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
721 if (op->can_id != rxframe.can_id) {
722 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
723 rxframe.can_id, op->can_id);
727 if (op->flags & RX_RTR_FRAME) {
728 /* send reply for RTR-request */
729 DBG("RTR-request\n");
731 /* send op->frames[0] to CAN device */
736 if (op->flags & RX_FILTER_ID) {
737 /* the easiest case */
738 DBG("Easy does it with RX_FILTER_ID\n");
740 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
741 bcm_rx_starttimer(op);
745 if (op->nframes == 1) {
746 /* simple compare with index 0 */
747 DBG("Simple compare\n");
749 bcm_rx_cmp_to_index(op, 0, &rxframe);
750 bcm_rx_starttimer(op);
754 if (op->nframes > 1) {
755 /* multiplex compare */
756 DBG("Multiplex compare\n");
759 * find the first multiplex mask that fits.
760 * Remark: The MUX-mask is stored in index 0
763 for (i=1; i < op->nframes; i++) {
764 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
765 (GET_U64(&op->frames[0]) &
766 GET_U64(&op->frames[i]))) {
767 DBG("found MUX index %d\n", i);
768 bcm_rx_cmp_to_index(op, i, &rxframe);
772 bcm_rx_starttimer(op);
777 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
779 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
784 list_for_each_entry(op, ops, list) {
785 if ((op->can_id == can_id) && (op->ifindex == ifindex))
792 static void bcm_remove_op(struct bcm_op *op)
794 del_timer(&op->timer);
795 del_timer(&op->thrtimer);
797 if ((op->frames) && (op->frames != &op->sframe))
800 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
801 kfree(op->last_frames);
809 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
811 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
813 struct bcm_op *op, *n;
815 list_for_each_entry_safe(op, n, ops, list) {
816 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
817 DBG("removing rx_op %p for can_id %03X\n",
821 * Don't care if we're bound or not (due to netdev
822 * problems) can_rx_unregister() is always a save
826 struct net_device *dev =
827 dev_get_by_index(op->ifindex);
830 can_rx_unregister(dev, op->can_id,
837 can_rx_unregister(NULL, op->can_id,
847 return 0; /* not found */
851 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
853 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
855 struct bcm_op *op, *n;
857 list_for_each_entry_safe(op, n, ops, list) {
858 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
859 DBG("removing rx_op %p for can_id %03X\n",
867 return 0; /* not found */
871 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
873 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
876 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
879 DBG("TRX_READ: did not find op for can_id %03X\n",
884 DBG("TRX_READ: sending status for can_id %03X\n",
886 /* put current values into msg_head */
887 msg_head->flags = op->flags;
888 msg_head->count = op->count;
889 msg_head->ival1 = op->ival1;
890 msg_head->ival2 = op->ival2;
891 msg_head->nframes = op->nframes;
893 bcm_send_to_user(op, msg_head, op->frames, NULL);
899 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
901 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
902 int ifindex, struct sock *sk)
904 struct bcm_opt *bo = bcm_sk(sk);
908 /* we need a real device to send frames */
912 /* we need at least one can_frame */
913 if (msg_head->nframes < 1)
916 /* check the given can_id */
917 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
920 /* update existing BCM operation */
922 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
923 op, msg_head->can_id);
926 * Do we need more space for the can_frames than currently
927 * allocated? -> This is a _really_ unusual use-case and
928 * therefore (complexity / locking) it is not supported.
930 if (msg_head->nframes > op->nframes)
933 /* update can_frames content */
934 for (i = 0; i < msg_head->nframes; i++) {
935 err = memcpy_fromiovec((u8*)&op->frames[i],
936 msg->msg_iov, CFSIZ);
940 if (msg_head->flags & TX_CP_CAN_ID) {
941 /* copy can_id into frame */
942 op->frames[i].can_id = msg_head->can_id;
947 /* insert new BCM operation for the given can_id */
949 op = kzalloc(OPSIZ, GFP_KERNEL);
953 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
954 op, msg_head->can_id);
956 op->can_id = msg_head->can_id;
958 /* create array for can_frames and copy the data */
959 if (msg_head->nframes > 1) {
960 op->frames = kmalloc(msg_head->nframes * CFSIZ,
967 op->frames = &op->sframe;
969 for (i = 0; i < msg_head->nframes; i++) {
970 err = memcpy_fromiovec((u8*)&op->frames[i],
971 msg->msg_iov, CFSIZ);
973 if (op->frames != &op->sframe)
979 if (msg_head->flags & TX_CP_CAN_ID) {
980 /* copy can_id into frame */
981 op->frames[i].can_id = msg_head->can_id;
985 /* tx_ops never compare with previous received messages */
986 op->last_frames = NULL;
988 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
991 op->ifindex = ifindex;
993 /* initialize uninitialized (kmalloc) structure */
994 init_timer(&op->timer);
996 /* currently unused in tx_ops */
997 init_timer(&op->thrtimer);
999 /* handler for tx_ops */
1000 op->timer.function = bcm_tx_timeout_handler;
1002 /* timer.data points to this op-structure */
1003 op->timer.data = (unsigned long)op;
1005 /* add this bcm_op to the list of the tx_ops */
1006 list_add(&op->list, &bo->tx_ops);
1008 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1010 if (op->nframes != msg_head->nframes) {
1011 op->nframes = msg_head->nframes;
1012 /* start multiple frame transmission with index 0 */
1018 op->flags = msg_head->flags;
1020 if (op->flags & TX_RESET_MULTI_IDX) {
1021 /* start multiple frame transmission with index 0 */
1025 if (op->flags & SETTIMER) {
1026 /* set timer values */
1028 op->count = msg_head->count;
1029 op->ival1 = msg_head->ival1;
1030 op->ival2 = msg_head->ival2;
1031 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1032 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1034 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1035 op->count, op->j_ival1, op->j_ival2);
1037 /* disable an active timer due to zero values? */
1038 if (!op->j_ival1 && !op->j_ival2) {
1039 del_timer(&op->timer);
1040 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1044 if ((op->flags & STARTTIMER) &&
1045 ((op->j_ival1 && op->count) || op->j_ival2)) {
1047 del_timer(&op->timer);
1049 /* spec: send can_frame when starting timer */
1050 op->flags |= TX_ANNOUNCE;
1052 if (op->j_ival1 && (op->count > 0)) {
1053 op->timer.expires = jiffies + op->j_ival1;
1054 /* op->count-- is done in bcm_tx_timeout_handler */
1055 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1058 (char*) op->timer.data,
1059 (unsigned int) op->timer.expires);
1062 op->timer.expires = jiffies + op->j_ival2;
1063 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1066 (char*) op->timer.data,
1067 (unsigned int) op->timer.expires);
1070 add_timer(&op->timer);
1073 if (op->flags & TX_ANNOUNCE)
1076 return msg_head->nframes * CFSIZ + MHSIZ;
1080 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1082 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1083 int ifindex, struct sock *sk)
1085 struct bcm_opt *bo = bcm_sk(sk);
1090 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1091 /* be robust against wrong usage ... */
1092 msg_head->flags |= RX_FILTER_ID;
1093 msg_head->nframes = 0; /* ignore trailing garbage */
1096 if ((msg_head->flags & RX_RTR_FRAME) &&
1097 ((msg_head->nframes != 1) ||
1098 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1100 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1104 /* check the given can_id */
1105 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1107 /* update existing BCM operation */
1109 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1110 op, msg_head->can_id);
1113 * Do we need more space for the can_frames than currently
1114 * allocated? -> This is a _really_ unusual use-case and
1115 * therefore (complexity / locking) it is not supported.
1117 if (msg_head->nframes > op->nframes)
1120 if (msg_head->nframes) {
1121 /* update can_frames content */
1122 err = memcpy_fromiovec((u8*)op->frames,
1124 msg_head->nframes * CFSIZ);
1128 /* clear last_frames to indicate 'nothing received' */
1129 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1132 op->nframes = msg_head->nframes;
1134 /* Only an update -> do not call can_rx_register() */
1138 /* insert new BCM operation for the given can_id */
1140 op = kzalloc(OPSIZ, GFP_KERNEL);
1144 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1145 op, msg_head->can_id);
1147 op->can_id = msg_head->can_id;
1148 op->nframes = msg_head->nframes;
1150 if (msg_head->nframes > 1) {
1151 /* create array for can_frames and copy the data */
1152 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1159 /* create and init array for received can_frames */
1160 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1162 if (!op->last_frames) {
1169 op->frames = &op->sframe;
1170 op->last_frames = &op->last_sframe;
1173 if (msg_head->nframes) {
1174 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1175 msg_head->nframes * CFSIZ);
1177 if (op->frames != &op->sframe)
1179 if (op->last_frames != &op->last_sframe)
1180 kfree(op->last_frames);
1187 op->ifindex = ifindex;
1189 /* initialize uninitialized (kzalloc) structure */
1190 init_timer(&op->timer);
1192 /* init throttle timer for RX_CHANGED */
1193 init_timer(&op->thrtimer);
1195 /* handler for rx timeouts */
1196 op->timer.function = bcm_rx_timeout_handler;
1198 /* timer.data points to this op-structure */
1199 op->timer.data = (unsigned long)op;
1201 /* handler for RX_CHANGED throttle timeouts */
1202 op->thrtimer.function = bcm_rx_thr_handler;
1204 /* timer.data points to this op-structure */
1205 op->thrtimer.data = (unsigned long)op;
1207 /* mark disabled timer */
1208 op->thrtimer.expires = 0;
1210 /* add this bcm_op to the list of the tx_ops */
1211 list_add(&op->list, &bo->rx_ops);
1213 /* call can_rx_register() */
1216 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1219 op->flags = msg_head->flags;
1221 if (op->flags & RX_RTR_FRAME) {
1223 /* no timers in RTR-mode */
1224 del_timer(&op->thrtimer);
1225 del_timer(&op->timer);
1228 * funny feature in RX(!)_SETUP only for RTR-mode:
1229 * copy can_id into frame BUT without RTR-flag to
1230 * prevent a full-load-loopback-test ... ;-]
1232 if ((op->flags & TX_CP_CAN_ID) ||
1233 (op->frames[0].can_id == op->can_id))
1234 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1237 if (op->flags & SETTIMER) {
1239 /* set timer value */
1240 op->ival1 = msg_head->ival1;
1241 op->ival2 = msg_head->ival2;
1242 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1243 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1245 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1246 op->j_ival1, op->j_ival2);
1248 /* disable an active timer due to zero value? */
1250 del_timer(&op->timer);
1251 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1254 /* free currently blocked msgs ? */
1255 if (op->thrtimer.expires) {
1256 DBG("RX_SETUP: unblocking throttled msgs.\n");
1257 del_timer(&op->thrtimer);
1258 /* send blocked msgs hereafter */
1259 op->thrtimer.expires = jiffies + 2;
1260 add_timer(&op->thrtimer);
1263 * if (op->j_ival2) is zero, no (new) throttling
1264 * will happen. For details see functions
1265 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1269 if ((op->flags & STARTTIMER) && op->j_ival1) {
1271 del_timer(&op->timer);
1272 op->timer.expires = jiffies + op->j_ival1;
1274 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1276 (char *) op->timer.function,
1277 (char *) op->timer.data,
1278 (unsigned int) op->timer.expires);
1280 add_timer(&op->timer);
1284 /* now we can register for can_ids, if we added a new bcm_op */
1285 if (do_rx_register) {
1286 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1287 "rx_op is %p\n", op->can_id, op);
1290 struct net_device *dev = dev_get_by_index(ifindex);
1293 can_rx_register(dev, op->can_id,
1294 REGMASK(op->can_id),
1295 bcm_rx_handler, op, IDENT);
1300 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1301 bcm_rx_handler, op, IDENT);
1304 return msg_head->nframes * CFSIZ + MHSIZ;
1308 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1310 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1312 struct sk_buff *skb;
1313 struct net_device *dev;
1316 /* just copy and send one can_frame */
1318 if (!ifindex) /* we need a real device to send frames */
1321 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1326 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1332 DBG_FRAME("BCM: TX_SEND: sending frame",
1333 (struct can_frame *)skb->data);
1335 dev = dev_get_by_index(ifindex);
1343 can_send(skb, 1); /* send with loopback */
1346 return CFSIZ + MHSIZ;
1350 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1352 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1353 struct msghdr *msg, size_t size)
1355 struct sock *sk = sock->sk;
1356 struct bcm_opt *bo = bcm_sk(sk);
1357 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1358 struct bcm_msg_head msg_head;
1359 int ret; /* read bytes or error codes as return value */
1362 DBG("sock %p not bound\n", sk);
1366 /* check for alternative ifindex for this bcm_op */
1368 if (!ifindex && msg->msg_name) {
1369 /* no bound device as default => check msg_name */
1370 struct sockaddr_can *addr =
1371 (struct sockaddr_can *)msg->msg_name;
1373 if (addr->can_family != AF_CAN)
1376 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1378 if (ifindex && !dev_get_by_index(ifindex)) {
1379 DBG("device %d not found\n", ifindex);
1384 /* read message head information */
1386 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1390 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1392 switch (msg_head.opcode) {
1395 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1399 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1403 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1410 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1417 /* reuse msg_head for the reply to TX_READ */
1418 msg_head.opcode = TX_STATUS;
1419 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1423 /* reuse msg_head for the reply to RX_READ */
1424 msg_head.opcode = RX_STATUS;
1425 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1429 /* we need at least one can_frame */
1430 if (msg_head.nframes < 1)
1433 ret = bcm_tx_send(msg, ifindex, sk);
1437 DBG("Unknown opcode %d\n", msg_head.opcode);
1446 * initial settings for all BCM sockets to be set at socket creation time
1448 static int bcm_init(struct sock *sk)
1450 struct bcm_opt *bo = bcm_sk(sk);
1454 bo->dropped_usr_msgs = 0;
1455 bo->bcm_proc_read = NULL;
1457 INIT_LIST_HEAD(&bo->tx_ops);
1458 INIT_LIST_HEAD(&bo->rx_ops);
1464 * notification handler for netdevice status changes
1466 static void bcm_notifier(unsigned long msg, void *data)
1468 struct sock *sk = (struct sock *)data;
1469 struct bcm_opt *bo = bcm_sk(sk);
1471 DBG("called for sock %p\n", sk);
1475 case NETDEV_UNREGISTER:
1480 sk->sk_err = ENETDOWN;
1481 if (!sock_flag(sk, SOCK_DEAD))
1482 sk->sk_error_report(sk);
1487 * standard socket functions
1489 static int bcm_release(struct socket *sock)
1491 struct sock *sk = sock->sk;
1492 struct bcm_opt *bo = bcm_sk(sk);
1493 struct bcm_op *op, *next;
1495 DBG("socket %p, sk %p\n", sock, sk);
1497 /* remove bcm_ops, timer, rx_unregister(), etc. */
1499 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1500 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1504 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1505 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1508 * Don't care if we're bound or not (due to netdev problems)
1509 * can_rx_unregister() is always a save thing to do here.
1512 struct net_device *dev = dev_get_by_index(op->ifindex);
1515 can_rx_unregister(dev, op->can_id,
1516 REGMASK(op->can_id),
1517 bcm_rx_handler, op);
1522 can_rx_unregister(NULL, op->can_id,
1523 REGMASK(op->can_id),
1524 bcm_rx_handler, op);
1529 /* remove procfs entry */
1530 if (proc_dir && bo->bcm_proc_read)
1531 remove_proc_entry(bo->procname, proc_dir);
1533 /* remove device notifier */
1535 struct net_device *dev = dev_get_by_index(bo->ifindex);
1538 can_dev_unregister(dev, bcm_notifier, sk);
1548 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1551 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1552 struct sock *sk = sock->sk;
1553 struct bcm_opt *bo = bcm_sk(sk);
1558 /* bind a device to this socket */
1559 if (addr->can_ifindex) {
1560 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1563 DBG("could not find device index %d\n",
1567 bo->ifindex = dev->ifindex;
1568 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1571 DBG("socket %p bound to device %s (idx %d)\n",
1572 sock, dev->name, dev->ifindex);
1575 /* no notifier for ifindex = 0 ('any' CAN device) */
1582 /* unique socket address as filename */
1583 sprintf(bo->procname, "%p", sock);
1584 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1592 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1593 struct msghdr *msg, size_t size, int flags)
1595 struct sock *sk = sock->sk;
1596 struct sk_buff *skb;
1601 DBG("socket %p, sk %p\n", sock, sk);
1603 noblock = flags & MSG_DONTWAIT;
1604 flags &= ~MSG_DONTWAIT;
1605 skb = skb_recv_datagram(sk, flags, noblock, &error);
1609 DBG("delivering skbuff %p\n", skb);
1612 if (skb->len < size)
1615 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1617 skb_free_datagram(sk, skb);
1621 sock_recv_timestamp(msg, sk, skb);
1623 if (msg->msg_name) {
1624 msg->msg_namelen = sizeof(struct sockaddr_can);
1625 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1628 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1629 skb_free_datagram(sk, skb);
1634 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1637 unsigned int mask = 0;
1639 DBG("socket %p\n", sock);
1641 mask = datagram_poll(file, sock, wait);
1645 static struct proto_ops bcm_ops = {
1647 .release = bcm_release,
1648 .bind = sock_no_bind,
1649 .connect = bcm_connect,
1650 .socketpair = sock_no_socketpair,
1651 .accept = sock_no_accept,
1652 .getname = sock_no_getname,
1654 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1655 .listen = sock_no_listen,
1656 .shutdown = sock_no_shutdown,
1657 .setsockopt = sock_no_setsockopt,
1658 .getsockopt = sock_no_getsockopt,
1659 .sendmsg = bcm_sendmsg,
1660 .recvmsg = bcm_recvmsg,
1661 .mmap = sock_no_mmap,
1662 .sendpage = sock_no_sendpage,
1665 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1666 static struct proto bcm_proto = {
1668 .owner = THIS_MODULE,
1669 .obj_size = sizeof(struct bcm_sock),
1673 static struct can_proto bcm_can_proto = {
1675 .protocol = CAN_BCM,
1676 .capability = BCM_CAP,
1681 static struct can_proto bcm_can_proto = {
1683 .protocol = CAN_BCM,
1684 .capability = BCM_CAP,
1686 .owner = THIS_MODULE,
1687 .obj_size = sizeof(struct bcm_opt),
1692 static int __init bcm_module_init(void)
1696 can_proto_register(&bcm_can_proto);
1698 /* create /proc/net/can/bcm directory */
1699 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1702 proc_dir->owner = THIS_MODULE;
1707 static void __exit bcm_module_exit(void)
1709 can_proto_unregister(&bcm_can_proto);
1712 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1715 module_init(bcm_module_init);
1716 module_exit(bcm_module_exit);