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/module.h>
46 #include <linux/init.h>
47 #include <linux/net.h>
48 #include <linux/netdevice.h>
49 #include <linux/proc_fs.h>
50 #include <linux/poll.h>
51 #include <linux/can.h>
52 #include <linux/can/core.h>
53 #include <linux/can/bcm.h>
56 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
59 /* use of last_frames[index].can_dlc */
60 #define RX_RECV 0x40 /* received data for this element */
61 #define RX_THR 0x80 /* element not been sent due to throttle feature */
62 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
64 /* get best masking value for can_rx_register() for a given single can_id */
65 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
66 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
69 #define CAN_BCM_VERSION CAN_VERSION
70 static __initdata const char banner[] = KERN_INFO
71 "can: broadcast manager protocol # rev " CAN_BCM_VERSION "\n";
73 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
74 MODULE_LICENSE("Dual BSD/GPL");
75 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
77 #ifdef CONFIG_CAN_DEBUG_CORE
79 module_param(debug, int, S_IRUGO);
80 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
83 /* easy access to can_frame payload */
84 static inline u64 GET_U64(const struct can_frame *cp)
86 return *(u64*)cp->data;
90 struct list_head list;
94 unsigned long j_ival1, j_ival2, j_lastmsg;
95 unsigned long frames_abs, frames_filtered;
96 struct timer_list timer, thrtimer;
97 struct timeval ival1, ival2;
98 struct timeval rx_stamp;
103 struct can_frame *frames;
104 struct can_frame *last_frames;
105 struct can_frame sframe;
106 struct can_frame last_sframe;
113 struct list_head rx_ops;
114 struct list_head tx_ops;
115 unsigned long dropped_usr_msgs;
116 struct proc_dir_entry *bcm_proc_read;
117 char procname [9]; /* pointer printed in ASCII with \0 */
120 static struct proc_dir_entry *proc_dir = NULL;
122 #ifdef CONFIG_CAN_BCM_USER
125 #define BCM_CAP CAP_NET_RAW
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
134 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
136 return &((struct bcm_sock *)sk)->opt;
139 static inline struct bcm_opt *bcm_sk(const struct sock *sk)
141 return (struct bcm_opt *)sk->sk_protinfo;
145 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
146 static void *kzalloc(size_t size, unsigned int __nocast flags)
148 void *ret = kmalloc(size, flags);
151 memset(ret, 0, size);
156 static inline void skb_get_timestamp(const struct sk_buff *skb,
157 struct timeval *stamp)
159 stamp->tv_sec = skb->stamp.tv_sec;
160 stamp->tv_usec = skb->stamp.tv_usec;
163 static inline void skb_set_timestamp(struct sk_buff *skb,
164 const struct timeval *stamp)
166 skb->stamp.tv_sec = stamp->tv_sec;
167 skb->stamp.tv_usec = stamp->tv_usec;
171 #define CFSIZ sizeof(struct can_frame)
172 #define OPSIZ sizeof(struct bcm_op)
173 #define MHSIZ sizeof(struct bcm_msg_head)
176 * rounded_tv2jif - calculate jiffies from timeval including optional up
177 * @tv: pointer to timeval
180 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
181 * function is intentionally more relaxed on precise timer ticks to get exact
182 * one jiffy for requested 1000us on a 1000HZ machine.
183 * This code is to be removed when upgrading to kernel hrtimer.
186 * calculated jiffies (max: ULONG_MAX)
188 static unsigned long rounded_tv2jif(const struct timeval *tv)
190 unsigned long sec = tv->tv_sec;
191 unsigned long usec = tv->tv_usec;
194 if (sec > ULONG_MAX / HZ)
197 /* round up to get at least the requested time */
198 usec += 1000000 / HZ - 1;
200 jif = usec / (1000000 / HZ);
202 if (sec * HZ > ULONG_MAX - jif)
205 return jif + sec * HZ;
211 static char *bcm_proc_getifname(int ifindex)
213 struct net_device *dev;
218 dev = __dev_get_by_index(ifindex); /* no usage counting */
225 static int bcm_read_proc(char *page, char **start, off_t off,
226 int count, int *eof, void *data)
229 struct sock *sk = (struct sock *)data;
230 struct bcm_opt *bo = bcm_sk(sk);
233 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
235 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
236 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
237 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
238 bo->dropped_usr_msgs);
239 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
240 bcm_proc_getifname(bo->ifindex));
241 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
243 list_for_each_entry(op, &bo->rx_ops, list) {
245 unsigned long reduction;
247 /* print only active entries & prevent division by zero */
251 len += snprintf(page + len, PAGE_SIZE - len,
253 op->can_id, bcm_proc_getifname(op->ifindex));
254 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
256 (op->flags & RX_CHECK_DLC)?'d':' ');
258 len += snprintf(page + len, PAGE_SIZE - len,
259 "timeo=%ld ", op->j_ival1);
262 len += snprintf(page + len, PAGE_SIZE - len,
263 "thr=%ld ", op->j_ival2);
265 len += snprintf(page + len, PAGE_SIZE - len,
266 "# recv %ld (%ld) => reduction: ",
267 op->frames_filtered, op->frames_abs);
269 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
271 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
272 (reduction == 100)?"near ":"", reduction);
274 if (len > PAGE_SIZE - 200) {
275 /* mark output cut off */
276 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
281 list_for_each_entry(op, &bo->tx_ops, list) {
283 len += snprintf(page + len, PAGE_SIZE - len,
284 "tx_op: %03X %s [%d] ",
285 op->can_id, bcm_proc_getifname(op->ifindex),
288 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
292 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
295 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
298 if (len > PAGE_SIZE - 100) {
299 /* mark output cut off */
300 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
305 len += snprintf(page + len, PAGE_SIZE - len, "\n");
312 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
313 * of the given bcm tx op
315 static void bcm_can_tx(struct bcm_op *op)
318 struct net_device *dev;
319 struct can_frame *cf = &op->frames[op->currframe];
321 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
323 /* no target device? => exit */
327 dev = dev_get_by_index(op->ifindex);
330 /* RFC: should this bcm_op remove itself here? */
334 skb = alloc_skb(CFSIZ,
335 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
340 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
342 /* send with loopback */
347 /* update statistics */
351 /* reached last frame? */
352 if (op->currframe >= op->nframes)
359 * bcm_send_to_user - send a BCM message to the userspace
360 * (consisting of bcm_msg_head + x CAN frames)
362 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
363 struct can_frame *frames, struct timeval *tv)
366 struct can_frame *firstframe;
367 struct sock *sk = op->sk;
368 int datalen = head->nframes * CFSIZ;
369 struct sockaddr_can *addr;
372 skb = alloc_skb(sizeof(*head) + datalen,
373 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
377 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
379 /* can_frames starting here */
380 firstframe = (struct can_frame *) skb->tail;
383 skb_set_timestamp(skb, tv); /* restore timestamp */
385 addr = (struct sockaddr_can *)skb->cb;
386 memset(addr, 0, sizeof(*addr));
387 addr->can_family = AF_CAN;
388 /* restore originator for recvfrom() */
389 addr->can_ifindex = op->rx_ifindex;
392 memcpy(skb_put(skb, datalen), frames, datalen);
395 * the BCM uses the can_dlc-element of the can_frame
396 * structure for internal purposes. This is only
397 * relevant for updates that are generated by the
398 * BCM, where nframes is 1
400 if (head->nframes == 1)
401 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
404 err = sock_queue_rcv_skb(sk, skb);
406 struct bcm_opt *bo = bcm_sk(sk);
408 DBG("sock_queue_rcv_skb failed: %d\n", err);
410 /* don't care about overflows in this statistic */
411 bo->dropped_usr_msgs++;
416 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
418 static void bcm_tx_timeout_handler(unsigned long data)
420 struct bcm_op *op = (struct bcm_op*)data;
422 DBG("Called with bcm_op %p\n", op);
424 if (op->j_ival1 && (op->count > 0)) {
427 if (!op->count && (op->flags & TX_COUNTEVT)) {
428 struct bcm_msg_head msg_head;
430 /* create notification to user */
431 DBG("sending TX_EXPIRED for can_id %03X\n",
434 msg_head.opcode = TX_EXPIRED;
435 msg_head.flags = op->flags;
436 msg_head.count = op->count;
437 msg_head.ival1 = op->ival1;
438 msg_head.ival2 = op->ival2;
439 msg_head.can_id = op->can_id;
440 msg_head.nframes = 0;
442 bcm_send_to_user(op, &msg_head, NULL, NULL);
446 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
447 op->count, op->j_ival1, op->j_ival2);
449 if (op->j_ival1 && (op->count > 0)) {
451 op->timer.expires = jiffies + op->j_ival1;
452 add_timer(&op->timer);
454 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
456 (char*) op->timer.data,
457 (unsigned int) op->timer.expires);
459 /* send (next) frame */
464 op->timer.expires = jiffies + op->j_ival2;
465 add_timer(&op->timer);
467 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
469 (char*) op->timer.data,
470 (unsigned int) op->timer.expires);
472 /* send (next) frame */
476 DBG("no timer restart\n");
483 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
485 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
487 struct bcm_msg_head head;
489 op->j_lastmsg = jiffies;
491 /* update statistics */
492 op->frames_filtered++;
494 /* prevent statistics overflow */
495 if (op->frames_filtered > ULONG_MAX/100)
496 op->frames_filtered = op->frames_abs = 0;
498 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
499 (unsigned int) op->j_lastmsg, op);
500 DBG("sending notification\n");
502 head.opcode = RX_CHANGED;
503 head.flags = op->flags;
504 head.count = op->count;
505 head.ival1 = op->ival1;
506 head.ival2 = op->ival2;
507 head.can_id = op->can_id;
510 bcm_send_to_user(op, &head, data, &op->rx_stamp);
514 * bcm_rx_update_and_send - process a detected relevant receive content change
515 * 1. update the last received data
516 * 2. send a notification to the user (if possible)
518 static void bcm_rx_update_and_send(struct bcm_op *op,
519 struct can_frame *lastdata,
520 struct can_frame *rxdata)
522 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
524 memcpy(lastdata, rxdata, CFSIZ);
527 lastdata->can_dlc |= RX_RECV;
529 /* throttle bcm_rx_changed ? */
530 if ((op->thrtimer.expires) ||
531 ((op->j_ival2) && (nexttx > jiffies))) {
532 /* we are already waiting OR we have to start waiting */
534 /* mark as 'throttled' */
535 lastdata->can_dlc |= RX_THR;
537 if (!(op->thrtimer.expires)) {
538 /* start the timer only the first time */
539 op->thrtimer.expires = nexttx;
540 add_timer(&op->thrtimer);
542 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
543 op->thrtimer.function,
544 (char*) op->thrtimer.data,
545 (unsigned int) op->thrtimer.expires);
549 /* send RX_CHANGED to the user immediately */
550 bcm_rx_changed(op, rxdata);
555 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
556 * received data stored in op->last_frames[]
558 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
559 struct can_frame *rxdata)
562 * no one uses the MSBs of can_dlc for comparation,
563 * so we use it here to detect the first time of reception
566 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
567 /* received data for the first time => send update to user */
568 DBG("first time :)\n");
569 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
573 /* do a real check in can_frame data section */
575 DBG("op->frames[index].data = 0x%016llx\n",
576 GET_U64(&op->frames[index]));
577 DBG("op->last_frames[index].data = 0x%016llx\n",
578 GET_U64(&op->last_frames[index]));
579 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
581 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
582 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
583 DBG("relevant data change :)\n");
584 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
588 if (op->flags & RX_CHECK_DLC) {
589 /* do a real check in can_frame dlc */
590 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
592 DBG("dlc change :)\n");
593 bcm_rx_update_and_send(op, &op->last_frames[index],
598 DBG("no relevant change :(\n");
602 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
604 static void bcm_rx_starttimer(struct bcm_op *op)
606 if (op->flags & RX_NO_AUTOTIMER)
610 op->timer.expires = jiffies + op->j_ival1;
612 DBG("adding rx timeout timer ival1. func=%p data=%p "
615 (char*) op->timer.data,
616 (unsigned int) op->timer.expires);
618 add_timer(&op->timer);
623 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
625 static void bcm_rx_timeout_handler(unsigned long data)
627 struct bcm_op *op = (struct bcm_op*)data;
628 struct bcm_msg_head msg_head;
630 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
632 msg_head.opcode = RX_TIMEOUT;
633 msg_head.flags = op->flags;
634 msg_head.count = op->count;
635 msg_head.ival1 = op->ival1;
636 msg_head.ival2 = op->ival2;
637 msg_head.can_id = op->can_id;
638 msg_head.nframes = 0;
640 bcm_send_to_user(op, &msg_head, NULL, NULL);
642 /* no restart of the timer is done here! */
644 /* if user wants to be informed, when cyclic CAN-Messages come back */
645 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
646 /* clear received can_frames to indicate 'nothing received' */
647 memset(op->last_frames, 0, op->nframes * CFSIZ);
648 DBG("RX_ANNOUNCE_RESTART\n");
653 * bcm_rx_thr_handler - the time for blocked content updates is over now:
654 * Check for throttled data and send it to the userspace
656 static void bcm_rx_thr_handler(unsigned long data)
658 struct bcm_op *op = (struct bcm_op*)data;
661 /* mark disabled / consumed timer */
662 op->thrtimer.expires = 0;
664 if (op->nframes > 1) {
665 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
667 /* for MUX filter we start at index 1 */
668 for (i=1; i<op->nframes; i++) {
669 if ((op->last_frames) &&
670 (op->last_frames[i].can_dlc & RX_THR)) {
671 op->last_frames[i].can_dlc &= ~RX_THR;
672 bcm_rx_changed(op, &op->last_frames[i]);
677 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
679 /* for RX_FILTER_ID and simple filter */
680 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
681 op->last_frames[0].can_dlc &= ~RX_THR;
682 bcm_rx_changed(op, &op->last_frames[0]);
688 * bcm_rx_handler - handle a CAN frame receiption
690 static void bcm_rx_handler(struct sk_buff *skb, void *data)
692 struct bcm_op *op = (struct bcm_op*)data;
693 struct can_frame rxframe;
696 /* disable timeout */
697 del_timer(&op->timer);
699 DBG("Called with bcm_op %p\n", op);
701 if (skb->len == sizeof(rxframe)) {
702 memcpy(&rxframe, skb->data, sizeof(rxframe));
703 /* save rx timestamp */
704 skb_get_timestamp(skb, &op->rx_stamp);
705 /* save originator for recvfrom() */
706 op->rx_ifindex = skb->dev->ifindex;
707 /* update statistics */
710 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
713 DBG("Wrong skb->len = %d\n", skb->len);
718 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
720 if (op->can_id != rxframe.can_id) {
721 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
722 rxframe.can_id, op->can_id);
726 if (op->flags & RX_RTR_FRAME) {
727 /* send reply for RTR-request */
728 DBG("RTR-request\n");
730 /* send op->frames[0] to CAN device */
735 if (op->flags & RX_FILTER_ID) {
736 /* the easiest case */
737 DBG("Easy does it with RX_FILTER_ID\n");
739 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
740 bcm_rx_starttimer(op);
744 if (op->nframes == 1) {
745 /* simple compare with index 0 */
746 DBG("Simple compare\n");
748 bcm_rx_cmp_to_index(op, 0, &rxframe);
749 bcm_rx_starttimer(op);
753 if (op->nframes > 1) {
754 /* multiplex compare */
755 DBG("Multiplex compare\n");
758 * find the first multiplex mask that fits.
759 * Remark: The MUX-mask is stored in index 0
762 for (i=1; i < op->nframes; i++) {
763 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
764 (GET_U64(&op->frames[0]) &
765 GET_U64(&op->frames[i]))) {
766 DBG("found MUX index %d\n", i);
767 bcm_rx_cmp_to_index(op, i, &rxframe);
771 bcm_rx_starttimer(op);
776 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
778 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
783 list_for_each_entry(op, ops, list) {
784 if ((op->can_id == can_id) && (op->ifindex == ifindex))
791 static void bcm_remove_op(struct bcm_op *op)
793 del_timer(&op->timer);
794 del_timer(&op->thrtimer);
796 if ((op->frames) && (op->frames != &op->sframe))
799 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
800 kfree(op->last_frames);
808 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
810 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
812 struct bcm_op *op, *n;
814 list_for_each_entry_safe(op, n, ops, list) {
815 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
816 DBG("removing rx_op %p for can_id %03X\n",
820 * Don't care if we're bound or not (due to netdev
821 * problems) can_rx_unregister() is always a save
825 struct net_device *dev =
826 dev_get_by_index(op->ifindex);
829 can_rx_unregister(dev, op->can_id,
836 can_rx_unregister(NULL, op->can_id,
846 return 0; /* not found */
850 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
852 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
854 struct bcm_op *op, *n;
856 list_for_each_entry_safe(op, n, ops, list) {
857 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
858 DBG("removing rx_op %p for can_id %03X\n",
866 return 0; /* not found */
870 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
872 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
875 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
878 DBG("TRX_READ: did not find op for can_id %03X\n",
883 DBG("TRX_READ: sending status for can_id %03X\n",
885 /* put current values into msg_head */
886 msg_head->flags = op->flags;
887 msg_head->count = op->count;
888 msg_head->ival1 = op->ival1;
889 msg_head->ival2 = op->ival2;
890 msg_head->nframes = op->nframes;
892 bcm_send_to_user(op, msg_head, op->frames, NULL);
898 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
900 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
901 int ifindex, struct sock *sk)
903 struct bcm_opt *bo = bcm_sk(sk);
907 /* we need a real device to send frames */
911 /* we need at least one can_frame */
912 if (msg_head->nframes < 1)
915 /* check the given can_id */
916 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
919 /* update existing BCM operation */
921 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
922 op, msg_head->can_id);
925 * Do we need more space for the can_frames than currently
926 * allocated? -> This is a _really_ unusual use-case and
927 * therefore (complexity / locking) it is not supported.
929 if (msg_head->nframes > op->nframes)
932 /* update can_frames content */
933 for (i = 0; i < msg_head->nframes; i++) {
934 err = memcpy_fromiovec((u8*)&op->frames[i],
935 msg->msg_iov, CFSIZ);
939 if (msg_head->flags & TX_CP_CAN_ID) {
940 /* copy can_id into frame */
941 op->frames[i].can_id = msg_head->can_id;
946 /* insert new BCM operation for the given can_id */
948 op = kzalloc(OPSIZ, GFP_KERNEL);
952 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
953 op, msg_head->can_id);
955 op->can_id = msg_head->can_id;
957 /* create array for can_frames and copy the data */
958 if (msg_head->nframes > 1) {
959 op->frames = kmalloc(msg_head->nframes * CFSIZ,
966 op->frames = &op->sframe;
968 for (i = 0; i < msg_head->nframes; i++) {
969 err = memcpy_fromiovec((u8*)&op->frames[i],
970 msg->msg_iov, CFSIZ);
972 if (op->frames != &op->sframe)
978 if (msg_head->flags & TX_CP_CAN_ID) {
979 /* copy can_id into frame */
980 op->frames[i].can_id = msg_head->can_id;
984 /* tx_ops never compare with previous received messages */
985 op->last_frames = NULL;
987 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
990 op->ifindex = ifindex;
992 /* initialize uninitialized (kmalloc) structure */
993 init_timer(&op->timer);
995 /* currently unused in tx_ops */
996 init_timer(&op->thrtimer);
998 /* handler for tx_ops */
999 op->timer.function = bcm_tx_timeout_handler;
1001 /* timer.data points to this op-structure */
1002 op->timer.data = (unsigned long)op;
1004 /* add this bcm_op to the list of the tx_ops */
1005 list_add(&op->list, &bo->tx_ops);
1007 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1009 if (op->nframes != msg_head->nframes) {
1010 op->nframes = msg_head->nframes;
1011 /* start multiple frame transmission with index 0 */
1017 op->flags = msg_head->flags;
1019 if (op->flags & TX_RESET_MULTI_IDX) {
1020 /* start multiple frame transmission with index 0 */
1024 if (op->flags & SETTIMER) {
1025 /* set timer values */
1027 op->count = msg_head->count;
1028 op->ival1 = msg_head->ival1;
1029 op->ival2 = msg_head->ival2;
1030 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1031 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1033 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1034 op->count, op->j_ival1, op->j_ival2);
1036 /* disable an active timer due to zero values? */
1037 if (!op->j_ival1 && !op->j_ival2) {
1038 del_timer(&op->timer);
1039 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1043 if ((op->flags & STARTTIMER) &&
1044 ((op->j_ival1 && op->count) || op->j_ival2)) {
1046 del_timer(&op->timer);
1048 /* spec: send can_frame when starting timer */
1049 op->flags |= TX_ANNOUNCE;
1051 if (op->j_ival1 && (op->count > 0)) {
1052 op->timer.expires = jiffies + op->j_ival1;
1053 /* op->count-- is done in bcm_tx_timeout_handler */
1054 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1057 (char*) op->timer.data,
1058 (unsigned int) op->timer.expires);
1061 op->timer.expires = jiffies + op->j_ival2;
1062 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1065 (char*) op->timer.data,
1066 (unsigned int) op->timer.expires);
1069 add_timer(&op->timer);
1072 if (op->flags & TX_ANNOUNCE)
1075 return msg_head->nframes * CFSIZ + MHSIZ;
1079 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1081 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1082 int ifindex, struct sock *sk)
1084 struct bcm_opt *bo = bcm_sk(sk);
1089 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1090 /* be robust against wrong usage ... */
1091 msg_head->flags |= RX_FILTER_ID;
1092 msg_head->nframes = 0; /* ignore trailing garbage */
1095 if ((msg_head->flags & RX_RTR_FRAME) &&
1096 ((msg_head->nframes != 1) ||
1097 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1099 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1103 /* check the given can_id */
1104 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1106 /* update existing BCM operation */
1108 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1109 op, msg_head->can_id);
1112 * Do we need more space for the can_frames than currently
1113 * allocated? -> This is a _really_ unusual use-case and
1114 * therefore (complexity / locking) it is not supported.
1116 if (msg_head->nframes > op->nframes)
1119 if (msg_head->nframes) {
1120 /* update can_frames content */
1121 err = memcpy_fromiovec((u8*)op->frames,
1123 msg_head->nframes * CFSIZ);
1127 /* clear last_frames to indicate 'nothing received' */
1128 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1131 op->nframes = msg_head->nframes;
1133 /* Only an update -> do not call can_rx_register() */
1137 /* insert new BCM operation for the given can_id */
1139 op = kzalloc(OPSIZ, GFP_KERNEL);
1143 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1144 op, msg_head->can_id);
1146 op->can_id = msg_head->can_id;
1147 op->nframes = msg_head->nframes;
1149 if (msg_head->nframes > 1) {
1150 /* create array for can_frames and copy the data */
1151 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1158 /* create and init array for received can_frames */
1159 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1161 if (!op->last_frames) {
1168 op->frames = &op->sframe;
1169 op->last_frames = &op->last_sframe;
1172 if (msg_head->nframes) {
1173 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1174 msg_head->nframes * CFSIZ);
1176 if (op->frames != &op->sframe)
1178 if (op->last_frames != &op->last_sframe)
1179 kfree(op->last_frames);
1186 op->ifindex = ifindex;
1188 /* initialize uninitialized (kzalloc) structure */
1189 init_timer(&op->timer);
1191 /* init throttle timer for RX_CHANGED */
1192 init_timer(&op->thrtimer);
1194 /* handler for rx timeouts */
1195 op->timer.function = bcm_rx_timeout_handler;
1197 /* timer.data points to this op-structure */
1198 op->timer.data = (unsigned long)op;
1200 /* handler for RX_CHANGED throttle timeouts */
1201 op->thrtimer.function = bcm_rx_thr_handler;
1203 /* timer.data points to this op-structure */
1204 op->thrtimer.data = (unsigned long)op;
1206 /* mark disabled timer */
1207 op->thrtimer.expires = 0;
1209 /* add this bcm_op to the list of the tx_ops */
1210 list_add(&op->list, &bo->rx_ops);
1212 /* call can_rx_register() */
1215 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1218 op->flags = msg_head->flags;
1220 if (op->flags & RX_RTR_FRAME) {
1222 /* no timers in RTR-mode */
1223 del_timer(&op->thrtimer);
1224 del_timer(&op->timer);
1227 * funny feature in RX(!)_SETUP only for RTR-mode:
1228 * copy can_id into frame BUT without RTR-flag to
1229 * prevent a full-load-loopback-test ... ;-]
1231 if ((op->flags & TX_CP_CAN_ID) ||
1232 (op->frames[0].can_id == op->can_id))
1233 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1236 if (op->flags & SETTIMER) {
1238 /* set timer value */
1239 op->ival1 = msg_head->ival1;
1240 op->ival2 = msg_head->ival2;
1241 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1242 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1244 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1245 op->j_ival1, op->j_ival2);
1247 /* disable an active timer due to zero value? */
1249 del_timer(&op->timer);
1250 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1253 /* free currently blocked msgs ? */
1254 if (op->thrtimer.expires) {
1255 DBG("RX_SETUP: unblocking throttled msgs.\n");
1256 del_timer(&op->thrtimer);
1257 /* send blocked msgs hereafter */
1258 op->thrtimer.expires = jiffies + 2;
1259 add_timer(&op->thrtimer);
1262 * if (op->j_ival2) is zero, no (new) throttling
1263 * will happen. For details see functions
1264 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1268 if ((op->flags & STARTTIMER) && op->j_ival1) {
1270 del_timer(&op->timer);
1271 op->timer.expires = jiffies + op->j_ival1;
1273 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1275 (char *) op->timer.function,
1276 (char *) op->timer.data,
1277 (unsigned int) op->timer.expires);
1279 add_timer(&op->timer);
1283 /* now we can register for can_ids, if we added a new bcm_op */
1284 if (do_rx_register) {
1285 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1286 "rx_op is %p\n", op->can_id, op);
1289 struct net_device *dev = dev_get_by_index(ifindex);
1292 can_rx_register(dev, op->can_id,
1293 REGMASK(op->can_id),
1294 bcm_rx_handler, op, IDENT);
1299 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1300 bcm_rx_handler, op, IDENT);
1303 return msg_head->nframes * CFSIZ + MHSIZ;
1307 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1309 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1311 struct sk_buff *skb;
1312 struct net_device *dev;
1315 /* just copy and send one can_frame */
1317 if (!ifindex) /* we need a real device to send frames */
1320 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1325 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1331 DBG_FRAME("BCM: TX_SEND: sending frame",
1332 (struct can_frame *)skb->data);
1334 dev = dev_get_by_index(ifindex);
1342 can_send(skb, 1); /* send with loopback */
1345 return CFSIZ + MHSIZ;
1349 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1351 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1352 struct msghdr *msg, size_t size)
1354 struct sock *sk = sock->sk;
1355 struct bcm_opt *bo = bcm_sk(sk);
1356 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1357 struct bcm_msg_head msg_head;
1358 int ret; /* read bytes or error codes as return value */
1361 DBG("sock %p not bound\n", sk);
1365 /* check for alternative ifindex for this bcm_op */
1367 if (!ifindex && msg->msg_name) {
1368 /* no bound device as default => check msg_name */
1369 struct sockaddr_can *addr =
1370 (struct sockaddr_can *)msg->msg_name;
1372 if (addr->can_family != AF_CAN)
1375 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1377 if (ifindex && !dev_get_by_index(ifindex)) {
1378 DBG("device %d not found\n", ifindex);
1383 /* read message head information */
1385 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1389 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1393 switch (msg_head.opcode) {
1396 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1400 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1404 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1411 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1418 /* reuse msg_head for the reply to TX_READ */
1419 msg_head.opcode = TX_STATUS;
1420 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1424 /* reuse msg_head for the reply to RX_READ */
1425 msg_head.opcode = RX_STATUS;
1426 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1430 /* we need at least one can_frame */
1431 if (msg_head.nframes < 1)
1434 ret = bcm_tx_send(msg, ifindex, sk);
1438 DBG("Unknown opcode %d\n", msg_head.opcode);
1449 * initial settings for all BCM sockets to be set at socket creation time
1451 static int bcm_init(struct sock *sk)
1453 struct bcm_opt *bo = bcm_sk(sk);
1457 bo->dropped_usr_msgs = 0;
1458 bo->bcm_proc_read = NULL;
1460 INIT_LIST_HEAD(&bo->tx_ops);
1461 INIT_LIST_HEAD(&bo->rx_ops);
1467 * notification handler for netdevice status changes
1469 static void bcm_notifier(unsigned long msg, void *data)
1471 struct sock *sk = (struct sock *)data;
1472 struct bcm_opt *bo = bcm_sk(sk);
1474 DBG("called for sock %p\n", sk);
1478 case NETDEV_UNREGISTER:
1483 sk->sk_err = ENETDOWN;
1484 if (!sock_flag(sk, SOCK_DEAD))
1485 sk->sk_error_report(sk);
1490 * standard socket functions
1492 static int bcm_release(struct socket *sock)
1494 struct sock *sk = sock->sk;
1495 struct bcm_opt *bo = bcm_sk(sk);
1496 struct bcm_op *op, *next;
1498 DBG("socket %p, sk %p\n", sock, sk);
1500 /* remove bcm_ops, timer, rx_unregister(), etc. */
1502 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1503 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1507 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1508 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1511 * Don't care if we're bound or not (due to netdev problems)
1512 * can_rx_unregister() is always a save thing to do here.
1515 struct net_device *dev = dev_get_by_index(op->ifindex);
1518 can_rx_unregister(dev, op->can_id,
1519 REGMASK(op->can_id),
1520 bcm_rx_handler, op);
1525 can_rx_unregister(NULL, op->can_id,
1526 REGMASK(op->can_id),
1527 bcm_rx_handler, op);
1532 /* remove procfs entry */
1533 if (proc_dir && bo->bcm_proc_read)
1534 remove_proc_entry(bo->procname, proc_dir);
1536 /* remove device notifier */
1538 struct net_device *dev = dev_get_by_index(bo->ifindex);
1541 can_dev_unregister(dev, bcm_notifier, sk);
1551 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1554 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1555 struct sock *sk = sock->sk;
1556 struct bcm_opt *bo = bcm_sk(sk);
1561 /* bind a device to this socket */
1562 if (addr->can_ifindex) {
1563 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1566 DBG("could not find device index %d\n",
1570 bo->ifindex = dev->ifindex;
1571 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1574 DBG("socket %p bound to device %s (idx %d)\n",
1575 sock, dev->name, dev->ifindex);
1578 /* no notifier for ifindex = 0 ('any' CAN device) */
1585 /* unique socket address as filename */
1586 sprintf(bo->procname, "%p", sock);
1587 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1595 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1596 struct msghdr *msg, size_t size, int flags)
1598 struct sock *sk = sock->sk;
1599 struct sk_buff *skb;
1604 DBG("socket %p, sk %p\n", sock, sk);
1606 noblock = flags & MSG_DONTWAIT;
1607 flags &= ~MSG_DONTWAIT;
1608 skb = skb_recv_datagram(sk, flags, noblock, &error);
1612 DBG("delivering skbuff %p\n", skb);
1615 if (skb->len < size)
1618 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1620 skb_free_datagram(sk, skb);
1624 sock_recv_timestamp(msg, sk, skb);
1626 if (msg->msg_name) {
1627 msg->msg_namelen = sizeof(struct sockaddr_can);
1628 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1631 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1632 skb_free_datagram(sk, skb);
1637 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1640 unsigned int mask = 0;
1642 DBG("socket %p\n", sock);
1644 mask = datagram_poll(file, sock, wait);
1648 static struct proto_ops bcm_ops = {
1650 .release = bcm_release,
1651 .bind = sock_no_bind,
1652 .connect = bcm_connect,
1653 .socketpair = sock_no_socketpair,
1654 .accept = sock_no_accept,
1655 .getname = sock_no_getname,
1657 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1658 .listen = sock_no_listen,
1659 .shutdown = sock_no_shutdown,
1660 .setsockopt = sock_no_setsockopt,
1661 .getsockopt = sock_no_getsockopt,
1662 .sendmsg = bcm_sendmsg,
1663 .recvmsg = bcm_recvmsg,
1664 .mmap = sock_no_mmap,
1665 .sendpage = sock_no_sendpage,
1668 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1669 static struct proto bcm_proto = {
1671 .owner = THIS_MODULE,
1672 .obj_size = sizeof(struct bcm_sock),
1676 static struct can_proto bcm_can_proto = {
1678 .protocol = CAN_BCM,
1679 .capability = BCM_CAP,
1684 static struct can_proto bcm_can_proto = {
1686 .protocol = CAN_BCM,
1687 .capability = BCM_CAP,
1689 .owner = THIS_MODULE,
1690 .obj_size = sizeof(struct bcm_opt),
1695 static int __init bcm_module_init(void)
1699 can_proto_register(&bcm_can_proto);
1701 /* create /proc/net/can/bcm directory */
1702 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1705 proc_dir->owner = THIS_MODULE;
1710 static void __exit bcm_module_exit(void)
1712 can_proto_unregister(&bcm_can_proto);
1715 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1718 module_init(bcm_module_init);
1719 module_exit(bcm_module_exit);