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.
185 * This code is to be removed when upgrading to kernel hrtimer.
188 * calculated jiffies (max: ULONG_MAX)
190 static unsigned long rounded_tv2jif(const struct timeval *tv)
192 unsigned long sec = tv->tv_sec;
193 unsigned long usec = tv->tv_usec;
196 if (sec > ULONG_MAX / HZ)
199 /* round up to get at least the requested time */
200 usec += 1000000 / HZ - 1;
202 jif = usec / (1000000 / HZ);
204 if (sec * HZ > ULONG_MAX - jif)
207 return jif + sec * HZ;
213 static char *bcm_proc_getifname(int ifindex)
215 struct net_device *dev;
220 dev = __dev_get_by_index(ifindex); /* no usage counting */
227 static int bcm_read_proc(char *page, char **start, off_t off,
228 int count, int *eof, void *data)
231 struct sock *sk = (struct sock *)data;
232 struct bcm_opt *bo = bcm_sk(sk);
235 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
237 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
238 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
239 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
240 bo->dropped_usr_msgs);
241 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
242 bcm_proc_getifname(bo->ifindex));
243 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
245 list_for_each_entry(op, &bo->rx_ops, list) {
247 unsigned long reduction;
249 /* print only active entries & prevent division by zero */
253 len += snprintf(page + len, PAGE_SIZE - len,
255 op->can_id, bcm_proc_getifname(op->ifindex));
256 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
258 (op->flags & RX_CHECK_DLC)?'d':' ');
260 len += snprintf(page + len, PAGE_SIZE - len,
261 "timeo=%ld ", op->j_ival1);
264 len += snprintf(page + len, PAGE_SIZE - len,
265 "thr=%ld ", op->j_ival2);
267 len += snprintf(page + len, PAGE_SIZE - len,
268 "# recv %ld (%ld) => reduction: ",
269 op->frames_filtered, op->frames_abs);
271 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
273 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
274 (reduction == 100)?"near ":"", reduction);
276 if (len > PAGE_SIZE - 200) {
277 /* mark output cut off */
278 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
283 list_for_each_entry(op, &bo->tx_ops, list) {
285 len += snprintf(page + len, PAGE_SIZE - len,
286 "tx_op: %03X %s [%d] ",
287 op->can_id, bcm_proc_getifname(op->ifindex),
290 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
294 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
297 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
300 if (len > PAGE_SIZE - 100) {
301 /* mark output cut off */
302 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
307 len += snprintf(page + len, PAGE_SIZE - len, "\n");
314 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
315 * of the given bcm tx op
317 static void bcm_can_tx(struct bcm_op *op)
320 struct net_device *dev;
321 struct can_frame *cf = &op->frames[op->currframe];
323 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
325 /* no target device? => exit */
329 dev = dev_get_by_index(op->ifindex);
332 /* RFC: should this bcm_op remove itself here? */
336 skb = alloc_skb(CFSIZ,
337 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
342 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
344 /* send with loopback */
349 /* update statistics */
353 /* reached last frame? */
354 if (op->currframe >= op->nframes)
361 * bcm_send_to_user - send a BCM message to the userspace
362 * (consisting of bcm_msg_head + x CAN frames)
364 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
365 struct can_frame *frames, struct timeval *tv)
368 struct can_frame *firstframe;
369 struct sock *sk = op->sk;
370 int datalen = head->nframes * CFSIZ;
371 struct sockaddr_can *addr;
374 skb = alloc_skb(sizeof(*head) + datalen,
375 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
379 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
381 /* can_frames starting here */
382 firstframe = (struct can_frame *) skb->tail;
385 skb_set_timestamp(skb, tv); /* restore timestamp */
387 addr = (struct sockaddr_can *)skb->cb;
388 memset(addr, 0, sizeof(*addr));
389 addr->can_family = AF_CAN;
390 /* restore originator for recvfrom() */
391 addr->can_ifindex = op->rx_ifindex;
394 memcpy(skb_put(skb, datalen), frames, datalen);
397 * the BCM uses the can_dlc-element of the can_frame
398 * structure for internal purposes. This is only
399 * relevant for updates that are generated by the
400 * BCM, where nframes is 1
402 if (head->nframes == 1)
403 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
406 err = sock_queue_rcv_skb(sk, skb);
408 struct bcm_opt *bo = bcm_sk(sk);
410 DBG("sock_queue_rcv_skb failed: %d\n", err);
412 /* don't care about overflows in this statistic */
413 bo->dropped_usr_msgs++;
418 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
420 static void bcm_tx_timeout_handler(unsigned long data)
422 struct bcm_op *op = (struct bcm_op*)data;
424 DBG("Called with bcm_op %p\n", op);
426 if (op->j_ival1 && (op->count > 0)) {
429 if (!op->count && (op->flags & TX_COUNTEVT)) {
430 struct bcm_msg_head msg_head;
432 /* create notification to user */
433 DBG("sending TX_EXPIRED for can_id %03X\n",
436 msg_head.opcode = TX_EXPIRED;
437 msg_head.flags = op->flags;
438 msg_head.count = op->count;
439 msg_head.ival1 = op->ival1;
440 msg_head.ival2 = op->ival2;
441 msg_head.can_id = op->can_id;
442 msg_head.nframes = 0;
444 bcm_send_to_user(op, &msg_head, NULL, NULL);
448 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
449 op->count, op->j_ival1, op->j_ival2);
451 if (op->j_ival1 && (op->count > 0)) {
453 op->timer.expires = jiffies + op->j_ival1;
454 add_timer(&op->timer);
456 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
458 (char*) op->timer.data,
459 (unsigned int) op->timer.expires);
461 /* send (next) frame */
466 op->timer.expires = jiffies + op->j_ival2;
467 add_timer(&op->timer);
469 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
471 (char*) op->timer.data,
472 (unsigned int) op->timer.expires);
474 /* send (next) frame */
478 DBG("no timer restart\n");
485 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
487 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
489 struct bcm_msg_head head;
491 op->j_lastmsg = jiffies;
493 /* update statistics */
494 op->frames_filtered++;
496 /* prevent statistics overflow */
497 if (op->frames_filtered > ULONG_MAX/100)
498 op->frames_filtered = op->frames_abs = 0;
500 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
501 (unsigned int) op->j_lastmsg, op);
502 DBG("sending notification\n");
504 head.opcode = RX_CHANGED;
505 head.flags = op->flags;
506 head.count = op->count;
507 head.ival1 = op->ival1;
508 head.ival2 = op->ival2;
509 head.can_id = op->can_id;
512 bcm_send_to_user(op, &head, data, &op->rx_stamp);
516 * bcm_rx_update_and_send - process a detected relevant receive content change
517 * 1. update the last received data
518 * 2. send a notification to the user (if possible)
520 static void bcm_rx_update_and_send(struct bcm_op *op,
521 struct can_frame *lastdata,
522 struct can_frame *rxdata)
524 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
526 memcpy(lastdata, rxdata, CFSIZ);
529 lastdata->can_dlc |= RX_RECV;
531 /* throttle bcm_rx_changed ? */
532 if ((op->thrtimer.expires) ||
533 ((op->j_ival2) && (nexttx > jiffies))) {
534 /* we are already waiting OR we have to start waiting */
536 /* mark as 'throttled' */
537 lastdata->can_dlc |= RX_THR;
539 if (!(op->thrtimer.expires)) {
540 /* start the timer only the first time */
541 op->thrtimer.expires = nexttx;
542 add_timer(&op->thrtimer);
544 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
545 op->thrtimer.function,
546 (char*) op->thrtimer.data,
547 (unsigned int) op->thrtimer.expires);
551 /* send RX_CHANGED to the user immediately */
552 bcm_rx_changed(op, rxdata);
557 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
558 * received data stored in op->last_frames[]
560 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
561 struct can_frame *rxdata)
564 * no one uses the MSBs of can_dlc for comparation,
565 * so we use it here to detect the first time of reception
568 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
569 /* received data for the first time => send update to user */
570 DBG("first time :)\n");
571 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
575 /* do a real check in can_frame data section */
577 DBG("op->frames[index].data = 0x%016llx\n",
578 GET_U64(&op->frames[index]));
579 DBG("op->last_frames[index].data = 0x%016llx\n",
580 GET_U64(&op->last_frames[index]));
581 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
583 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
584 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
585 DBG("relevant data change :)\n");
586 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
590 if (op->flags & RX_CHECK_DLC) {
591 /* do a real check in can_frame dlc */
592 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
594 DBG("dlc change :)\n");
595 bcm_rx_update_and_send(op, &op->last_frames[index],
600 DBG("no relevant change :(\n");
604 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
606 static void bcm_rx_starttimer(struct bcm_op *op)
608 if (op->flags & RX_NO_AUTOTIMER)
612 op->timer.expires = jiffies + op->j_ival1;
614 DBG("adding rx timeout timer ival1. func=%p data=%p "
617 (char*) op->timer.data,
618 (unsigned int) op->timer.expires);
620 add_timer(&op->timer);
625 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
627 static void bcm_rx_timeout_handler(unsigned long data)
629 struct bcm_op *op = (struct bcm_op*)data;
630 struct bcm_msg_head msg_head;
632 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
634 msg_head.opcode = RX_TIMEOUT;
635 msg_head.flags = op->flags;
636 msg_head.count = op->count;
637 msg_head.ival1 = op->ival1;
638 msg_head.ival2 = op->ival2;
639 msg_head.can_id = op->can_id;
640 msg_head.nframes = 0;
642 bcm_send_to_user(op, &msg_head, NULL, NULL);
644 /* no restart of the timer is done here! */
646 /* if user wants to be informed, when cyclic CAN-Messages come back */
647 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
648 /* clear received can_frames to indicate 'nothing received' */
649 memset(op->last_frames, 0, op->nframes * CFSIZ);
650 DBG("RX_ANNOUNCE_RESTART\n");
655 * bcm_rx_thr_handler - the time for blocked content updates is over now:
656 * Check for throttled data and send it to the userspace
658 static void bcm_rx_thr_handler(unsigned long data)
660 struct bcm_op *op = (struct bcm_op*)data;
663 /* mark disabled / consumed timer */
664 op->thrtimer.expires = 0;
666 if (op->nframes > 1) {
667 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
669 /* for MUX filter we start at index 1 */
670 for (i=1; i<op->nframes; i++) {
671 if ((op->last_frames) &&
672 (op->last_frames[i].can_dlc & RX_THR)) {
673 op->last_frames[i].can_dlc &= ~RX_THR;
674 bcm_rx_changed(op, &op->last_frames[i]);
679 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
681 /* for RX_FILTER_ID and simple filter */
682 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
683 op->last_frames[0].can_dlc &= ~RX_THR;
684 bcm_rx_changed(op, &op->last_frames[0]);
690 * bcm_rx_handler - handle a CAN frame receiption
692 static void bcm_rx_handler(struct sk_buff *skb, void *data)
694 struct bcm_op *op = (struct bcm_op*)data;
695 struct can_frame rxframe;
698 /* disable timeout */
699 del_timer(&op->timer);
701 DBG("Called with bcm_op %p\n", op);
703 if (skb->len == sizeof(rxframe)) {
704 memcpy(&rxframe, skb->data, sizeof(rxframe));
705 /* save rx timestamp */
706 skb_get_timestamp(skb, &op->rx_stamp);
707 /* save originator for recvfrom() */
708 op->rx_ifindex = skb->dev->ifindex;
709 /* update statistics */
712 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
715 DBG("Wrong skb->len = %d\n", skb->len);
720 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
722 if (op->can_id != rxframe.can_id) {
723 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
724 rxframe.can_id, op->can_id);
728 if (op->flags & RX_RTR_FRAME) {
729 /* send reply for RTR-request */
730 DBG("RTR-request\n");
732 /* send op->frames[0] to CAN device */
737 if (op->flags & RX_FILTER_ID) {
738 /* the easiest case */
739 DBG("Easy does it with RX_FILTER_ID\n");
741 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
742 bcm_rx_starttimer(op);
746 if (op->nframes == 1) {
747 /* simple compare with index 0 */
748 DBG("Simple compare\n");
750 bcm_rx_cmp_to_index(op, 0, &rxframe);
751 bcm_rx_starttimer(op);
755 if (op->nframes > 1) {
756 /* multiplex compare */
757 DBG("Multiplex compare\n");
760 * find the first multiplex mask that fits.
761 * Remark: The MUX-mask is stored in index 0
764 for (i=1; i < op->nframes; i++) {
765 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
766 (GET_U64(&op->frames[0]) &
767 GET_U64(&op->frames[i]))) {
768 DBG("found MUX index %d\n", i);
769 bcm_rx_cmp_to_index(op, i, &rxframe);
773 bcm_rx_starttimer(op);
778 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
780 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
785 list_for_each_entry(op, ops, list) {
786 if ((op->can_id == can_id) && (op->ifindex == ifindex))
793 static void bcm_remove_op(struct bcm_op *op)
795 del_timer(&op->timer);
796 del_timer(&op->thrtimer);
798 if ((op->frames) && (op->frames != &op->sframe))
801 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
802 kfree(op->last_frames);
810 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
812 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
814 struct bcm_op *op, *n;
816 list_for_each_entry_safe(op, n, ops, list) {
817 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
818 DBG("removing rx_op %p for can_id %03X\n",
822 * Don't care if we're bound or not (due to netdev
823 * problems) can_rx_unregister() is always a save
827 struct net_device *dev =
828 dev_get_by_index(op->ifindex);
831 can_rx_unregister(dev, op->can_id,
838 can_rx_unregister(NULL, op->can_id,
848 return 0; /* not found */
852 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
854 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
856 struct bcm_op *op, *n;
858 list_for_each_entry_safe(op, n, ops, list) {
859 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
860 DBG("removing rx_op %p for can_id %03X\n",
868 return 0; /* not found */
872 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
874 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
877 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
880 DBG("TRX_READ: did not find op for can_id %03X\n",
885 DBG("TRX_READ: sending status for can_id %03X\n",
887 /* put current values into msg_head */
888 msg_head->flags = op->flags;
889 msg_head->count = op->count;
890 msg_head->ival1 = op->ival1;
891 msg_head->ival2 = op->ival2;
892 msg_head->nframes = op->nframes;
894 bcm_send_to_user(op, msg_head, op->frames, NULL);
900 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
902 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
903 int ifindex, struct sock *sk)
905 struct bcm_opt *bo = bcm_sk(sk);
909 /* we need a real device to send frames */
913 /* we need at least one can_frame */
914 if (msg_head->nframes < 1)
917 /* check the given can_id */
918 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
921 /* update existing BCM operation */
923 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
924 op, msg_head->can_id);
927 * Do we need more space for the can_frames than currently
928 * allocated? -> This is a _really_ unusual use-case and
929 * therefore (complexity / locking) it is not supported.
931 if (msg_head->nframes > op->nframes)
934 /* update can_frames content */
935 for (i = 0; i < msg_head->nframes; i++) {
936 err = memcpy_fromiovec((u8*)&op->frames[i],
937 msg->msg_iov, CFSIZ);
941 if (msg_head->flags & TX_CP_CAN_ID) {
942 /* copy can_id into frame */
943 op->frames[i].can_id = msg_head->can_id;
948 /* insert new BCM operation for the given can_id */
950 op = kzalloc(OPSIZ, GFP_KERNEL);
954 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
955 op, msg_head->can_id);
957 op->can_id = msg_head->can_id;
959 /* create array for can_frames and copy the data */
960 if (msg_head->nframes > 1) {
961 op->frames = kmalloc(msg_head->nframes * CFSIZ,
968 op->frames = &op->sframe;
970 for (i = 0; i < msg_head->nframes; i++) {
971 err = memcpy_fromiovec((u8*)&op->frames[i],
972 msg->msg_iov, CFSIZ);
974 if (op->frames != &op->sframe)
980 if (msg_head->flags & TX_CP_CAN_ID) {
981 /* copy can_id into frame */
982 op->frames[i].can_id = msg_head->can_id;
986 /* tx_ops never compare with previous received messages */
987 op->last_frames = NULL;
989 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
992 op->ifindex = ifindex;
994 /* initialize uninitialized (kmalloc) structure */
995 init_timer(&op->timer);
997 /* currently unused in tx_ops */
998 init_timer(&op->thrtimer);
1000 /* handler for tx_ops */
1001 op->timer.function = bcm_tx_timeout_handler;
1003 /* timer.data points to this op-structure */
1004 op->timer.data = (unsigned long)op;
1006 /* add this bcm_op to the list of the tx_ops */
1007 list_add(&op->list, &bo->tx_ops);
1009 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1011 if (op->nframes != msg_head->nframes) {
1012 op->nframes = msg_head->nframes;
1013 /* start multiple frame transmission with index 0 */
1019 op->flags = msg_head->flags;
1021 if (op->flags & TX_RESET_MULTI_IDX) {
1022 /* start multiple frame transmission with index 0 */
1026 if (op->flags & SETTIMER) {
1027 /* set timer values */
1029 op->count = msg_head->count;
1030 op->ival1 = msg_head->ival1;
1031 op->ival2 = msg_head->ival2;
1032 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1033 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1035 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1036 op->count, op->j_ival1, op->j_ival2);
1038 /* disable an active timer due to zero values? */
1039 if (!op->j_ival1 && !op->j_ival2) {
1040 del_timer(&op->timer);
1041 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1045 if ((op->flags & STARTTIMER) &&
1046 ((op->j_ival1 && op->count) || op->j_ival2)) {
1048 del_timer(&op->timer);
1050 /* spec: send can_frame when starting timer */
1051 op->flags |= TX_ANNOUNCE;
1053 if (op->j_ival1 && (op->count > 0)) {
1054 op->timer.expires = jiffies + op->j_ival1;
1055 /* op->count-- is done in bcm_tx_timeout_handler */
1056 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1059 (char*) op->timer.data,
1060 (unsigned int) op->timer.expires);
1063 op->timer.expires = jiffies + op->j_ival2;
1064 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1067 (char*) op->timer.data,
1068 (unsigned int) op->timer.expires);
1071 add_timer(&op->timer);
1074 if (op->flags & TX_ANNOUNCE)
1077 return msg_head->nframes * CFSIZ + MHSIZ;
1081 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1083 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1084 int ifindex, struct sock *sk)
1086 struct bcm_opt *bo = bcm_sk(sk);
1091 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1092 /* be robust against wrong usage ... */
1093 msg_head->flags |= RX_FILTER_ID;
1094 msg_head->nframes = 0; /* ignore trailing garbage */
1097 if ((msg_head->flags & RX_RTR_FRAME) &&
1098 ((msg_head->nframes != 1) ||
1099 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1101 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1105 /* check the given can_id */
1106 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1108 /* update existing BCM operation */
1110 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1111 op, msg_head->can_id);
1114 * Do we need more space for the can_frames than currently
1115 * allocated? -> This is a _really_ unusual use-case and
1116 * therefore (complexity / locking) it is not supported.
1118 if (msg_head->nframes > op->nframes)
1121 if (msg_head->nframes) {
1122 /* update can_frames content */
1123 err = memcpy_fromiovec((u8*)op->frames,
1125 msg_head->nframes * CFSIZ);
1129 /* clear last_frames to indicate 'nothing received' */
1130 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1133 op->nframes = msg_head->nframes;
1135 /* Only an update -> do not call can_rx_register() */
1139 /* insert new BCM operation for the given can_id */
1141 op = kzalloc(OPSIZ, GFP_KERNEL);
1145 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1146 op, msg_head->can_id);
1148 op->can_id = msg_head->can_id;
1149 op->nframes = msg_head->nframes;
1151 if (msg_head->nframes > 1) {
1152 /* create array for can_frames and copy the data */
1153 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1160 /* create and init array for received can_frames */
1161 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1163 if (!op->last_frames) {
1170 op->frames = &op->sframe;
1171 op->last_frames = &op->last_sframe;
1174 if (msg_head->nframes) {
1175 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1176 msg_head->nframes * CFSIZ);
1178 if (op->frames != &op->sframe)
1180 if (op->last_frames != &op->last_sframe)
1181 kfree(op->last_frames);
1188 op->ifindex = ifindex;
1190 /* initialize uninitialized (kzalloc) structure */
1191 init_timer(&op->timer);
1193 /* init throttle timer for RX_CHANGED */
1194 init_timer(&op->thrtimer);
1196 /* handler for rx timeouts */
1197 op->timer.function = bcm_rx_timeout_handler;
1199 /* timer.data points to this op-structure */
1200 op->timer.data = (unsigned long)op;
1202 /* handler for RX_CHANGED throttle timeouts */
1203 op->thrtimer.function = bcm_rx_thr_handler;
1205 /* timer.data points to this op-structure */
1206 op->thrtimer.data = (unsigned long)op;
1208 /* mark disabled timer */
1209 op->thrtimer.expires = 0;
1211 /* add this bcm_op to the list of the tx_ops */
1212 list_add(&op->list, &bo->rx_ops);
1214 /* call can_rx_register() */
1217 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1220 op->flags = msg_head->flags;
1222 if (op->flags & RX_RTR_FRAME) {
1224 /* no timers in RTR-mode */
1225 del_timer(&op->thrtimer);
1226 del_timer(&op->timer);
1229 * funny feature in RX(!)_SETUP only for RTR-mode:
1230 * copy can_id into frame BUT without RTR-flag to
1231 * prevent a full-load-loopback-test ... ;-]
1233 if ((op->flags & TX_CP_CAN_ID) ||
1234 (op->frames[0].can_id == op->can_id))
1235 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1238 if (op->flags & SETTIMER) {
1240 /* set timer value */
1241 op->ival1 = msg_head->ival1;
1242 op->ival2 = msg_head->ival2;
1243 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1244 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1246 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1247 op->j_ival1, op->j_ival2);
1249 /* disable an active timer due to zero value? */
1251 del_timer(&op->timer);
1252 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1255 /* free currently blocked msgs ? */
1256 if (op->thrtimer.expires) {
1257 DBG("RX_SETUP: unblocking throttled msgs.\n");
1258 del_timer(&op->thrtimer);
1259 /* send blocked msgs hereafter */
1260 op->thrtimer.expires = jiffies + 2;
1261 add_timer(&op->thrtimer);
1264 * if (op->j_ival2) is zero, no (new) throttling
1265 * will happen. For details see functions
1266 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1270 if ((op->flags & STARTTIMER) && op->j_ival1) {
1272 del_timer(&op->timer);
1273 op->timer.expires = jiffies + op->j_ival1;
1275 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1277 (char *) op->timer.function,
1278 (char *) op->timer.data,
1279 (unsigned int) op->timer.expires);
1281 add_timer(&op->timer);
1285 /* now we can register for can_ids, if we added a new bcm_op */
1286 if (do_rx_register) {
1287 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1288 "rx_op is %p\n", op->can_id, op);
1291 struct net_device *dev = dev_get_by_index(ifindex);
1294 can_rx_register(dev, op->can_id,
1295 REGMASK(op->can_id),
1296 bcm_rx_handler, op, IDENT);
1301 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1302 bcm_rx_handler, op, IDENT);
1305 return msg_head->nframes * CFSIZ + MHSIZ;
1309 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1311 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1313 struct sk_buff *skb;
1314 struct net_device *dev;
1317 /* just copy and send one can_frame */
1319 if (!ifindex) /* we need a real device to send frames */
1322 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1327 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1333 DBG_FRAME("BCM: TX_SEND: sending frame",
1334 (struct can_frame *)skb->data);
1336 dev = dev_get_by_index(ifindex);
1344 can_send(skb, 1); /* send with loopback */
1347 return CFSIZ + MHSIZ;
1351 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1353 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1354 struct msghdr *msg, size_t size)
1356 struct sock *sk = sock->sk;
1357 struct bcm_opt *bo = bcm_sk(sk);
1358 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1359 struct bcm_msg_head msg_head;
1360 int ret; /* read bytes or error codes as return value */
1363 DBG("sock %p not bound\n", sk);
1367 /* check for alternative ifindex for this bcm_op */
1369 if (!ifindex && msg->msg_name) {
1370 /* no bound device as default => check msg_name */
1371 struct sockaddr_can *addr =
1372 (struct sockaddr_can *)msg->msg_name;
1374 if (addr->can_family != AF_CAN)
1377 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1379 if (ifindex && !dev_get_by_index(ifindex)) {
1380 DBG("device %d not found\n", ifindex);
1385 /* read message head information */
1387 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1391 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);
1447 * initial settings for all BCM sockets to be set at socket creation time
1449 static int bcm_init(struct sock *sk)
1451 struct bcm_opt *bo = bcm_sk(sk);
1455 bo->dropped_usr_msgs = 0;
1456 bo->bcm_proc_read = NULL;
1458 INIT_LIST_HEAD(&bo->tx_ops);
1459 INIT_LIST_HEAD(&bo->rx_ops);
1465 * notification handler for netdevice status changes
1467 static void bcm_notifier(unsigned long msg, void *data)
1469 struct sock *sk = (struct sock *)data;
1470 struct bcm_opt *bo = bcm_sk(sk);
1472 DBG("called for sock %p\n", sk);
1476 case NETDEV_UNREGISTER:
1481 sk->sk_err = ENETDOWN;
1482 if (!sock_flag(sk, SOCK_DEAD))
1483 sk->sk_error_report(sk);
1488 * standard socket functions
1490 static int bcm_release(struct socket *sock)
1492 struct sock *sk = sock->sk;
1493 struct bcm_opt *bo = bcm_sk(sk);
1494 struct bcm_op *op, *next;
1496 DBG("socket %p, sk %p\n", sock, sk);
1498 /* remove bcm_ops, timer, rx_unregister(), etc. */
1500 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1501 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1505 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1506 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1509 * Don't care if we're bound or not (due to netdev problems)
1510 * can_rx_unregister() is always a save thing to do here.
1513 struct net_device *dev = dev_get_by_index(op->ifindex);
1516 can_rx_unregister(dev, op->can_id,
1517 REGMASK(op->can_id),
1518 bcm_rx_handler, op);
1523 can_rx_unregister(NULL, op->can_id,
1524 REGMASK(op->can_id),
1525 bcm_rx_handler, op);
1530 /* remove procfs entry */
1531 if (proc_dir && bo->bcm_proc_read)
1532 remove_proc_entry(bo->procname, proc_dir);
1534 /* remove device notifier */
1536 struct net_device *dev = dev_get_by_index(bo->ifindex);
1539 can_dev_unregister(dev, bcm_notifier, sk);
1549 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1552 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1553 struct sock *sk = sock->sk;
1554 struct bcm_opt *bo = bcm_sk(sk);
1559 /* bind a device to this socket */
1560 if (addr->can_ifindex) {
1561 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1564 DBG("could not find device index %d\n",
1568 bo->ifindex = dev->ifindex;
1569 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1572 DBG("socket %p bound to device %s (idx %d)\n",
1573 sock, dev->name, dev->ifindex);
1576 /* no notifier for ifindex = 0 ('any' CAN device) */
1583 /* unique socket address as filename */
1584 sprintf(bo->procname, "%p", sock);
1585 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1593 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1594 struct msghdr *msg, size_t size, int flags)
1596 struct sock *sk = sock->sk;
1597 struct sk_buff *skb;
1602 DBG("socket %p, sk %p\n", sock, sk);
1604 noblock = flags & MSG_DONTWAIT;
1605 flags &= ~MSG_DONTWAIT;
1606 skb = skb_recv_datagram(sk, flags, noblock, &error);
1610 DBG("delivering skbuff %p\n", skb);
1613 if (skb->len < size)
1616 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1618 skb_free_datagram(sk, skb);
1622 sock_recv_timestamp(msg, sk, skb);
1624 if (msg->msg_name) {
1625 msg->msg_namelen = sizeof(struct sockaddr_can);
1626 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1629 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1630 skb_free_datagram(sk, skb);
1635 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1638 unsigned int mask = 0;
1640 DBG("socket %p\n", sock);
1642 mask = datagram_poll(file, sock, wait);
1646 static struct proto_ops bcm_ops = {
1648 .release = bcm_release,
1649 .bind = sock_no_bind,
1650 .connect = bcm_connect,
1651 .socketpair = sock_no_socketpair,
1652 .accept = sock_no_accept,
1653 .getname = sock_no_getname,
1655 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1656 .listen = sock_no_listen,
1657 .shutdown = sock_no_shutdown,
1658 .setsockopt = sock_no_setsockopt,
1659 .getsockopt = sock_no_getsockopt,
1660 .sendmsg = bcm_sendmsg,
1661 .recvmsg = bcm_recvmsg,
1662 .mmap = sock_no_mmap,
1663 .sendpage = sock_no_sendpage,
1666 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1667 static struct proto bcm_proto = {
1669 .owner = THIS_MODULE,
1670 .obj_size = sizeof(struct bcm_sock),
1674 static struct can_proto bcm_can_proto = {
1676 .protocol = CAN_BCM,
1677 .capability = BCM_CAP,
1682 static struct can_proto bcm_can_proto = {
1684 .protocol = CAN_BCM,
1685 .capability = BCM_CAP,
1687 .owner = THIS_MODULE,
1688 .obj_size = sizeof(struct bcm_opt),
1693 static int __init bcm_module_init(void)
1697 can_proto_register(&bcm_can_proto);
1699 /* create /proc/net/can/bcm directory */
1700 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1703 proc_dir->owner = THIS_MODULE;
1708 static void __exit bcm_module_exit(void)
1710 can_proto_unregister(&bcm_can_proto);
1713 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1716 module_init(bcm_module_init);
1717 module_exit(bcm_module_exit);