2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/autoconf.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
58 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
61 /* use of last_frames[index].can_dlc */
62 #define RX_RECV 0x40 /* received data for this element */
63 #define RX_THR 0x80 /* element not been sent due to throttle feature */
64 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
66 /* get best masking value for can_rx_register() for a given single can_id */
67 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
71 static __initdata const char banner[] = KERN_INFO
72 "CAN: broadcast manager (bcm) socket protocol " CAN_VERSION "\n";
74 MODULE_DESCRIPTION("PF_CAN bcm sockets");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
78 #ifdef CONFIG_CAN_DEBUG_CORE
80 module_param(debug, int, S_IRUGO);
81 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
84 #define GET_U64(p) (*(u64*)(p)->data) /* easy access to can_frame payload */
87 struct list_head list;
91 unsigned long j_ival1, j_ival2, j_lastmsg;
92 unsigned long frames_abs, frames_filtered;
93 struct timer_list timer, thrtimer;
94 struct timeval ival1, ival2;
95 struct timeval rx_stamp;
100 struct can_frame *frames;
101 struct can_frame *last_frames;
102 struct can_frame sframe;
103 struct can_frame last_sframe;
110 struct list_head rx_ops;
111 struct list_head tx_ops;
112 unsigned long dropped_usr_msgs;
113 struct proc_dir_entry *bcm_proc_read;
114 char procname [9]; /* pointer printed in ASCII with \0 */
117 static struct proc_dir_entry *proc_dir = NULL;
119 #ifdef CONFIG_CAN_BCM_USER
122 #define BCM_CAP CAP_NET_RAW
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
131 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
133 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
136 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
137 static void *kzalloc(size_t size, unsigned int __nocast flags)
139 void *ret = kmalloc(size, flags);
142 memset(ret, 0, size);
147 static inline void skb_get_timestamp(const struct sk_buff *skb,
148 struct timeval *stamp)
150 stamp->tv_sec = skb->stamp.tv_sec;
151 stamp->tv_usec = skb->stamp.tv_usec;
154 static inline void skb_set_timestamp(struct sk_buff *skb,
155 const struct timeval *stamp)
157 skb->stamp.tv_sec = stamp->tv_sec;
158 skb->stamp.tv_usec = stamp->tv_usec;
162 #define CFSIZ sizeof(struct can_frame)
163 #define OPSIZ sizeof(struct bcm_op)
164 #define MHSIZ sizeof(struct bcm_msg_head)
169 static char *bcm_proc_getifname(int ifindex)
171 struct net_device *dev;
176 dev = __dev_get_by_index(ifindex); /* no usage counting */
183 static int bcm_read_proc(char *page, char **start, off_t off,
184 int count, int *eof, void *data)
187 struct sock *sk = (struct sock *)data;
188 struct bcm_opt *bo = bcm_sk(sk);
191 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
193 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
194 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
195 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
196 bo->dropped_usr_msgs);
197 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
198 bcm_proc_getifname(bo->ifindex));
199 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
201 list_for_each_entry(op, &bo->rx_ops, list) {
203 unsigned long reduction;
205 /* print only active entries & prevent division by zero */
209 len += snprintf(page + len, PAGE_SIZE - len,
211 op->can_id, bcm_proc_getifname(op->ifindex));
212 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
214 (op->flags & RX_CHECK_DLC)?'d':' ');
216 len += snprintf(page + len, PAGE_SIZE - len,
217 "timeo=%ld ", op->j_ival1);
220 len += snprintf(page + len, PAGE_SIZE - len,
221 "thr=%ld ", op->j_ival2);
223 len += snprintf(page + len, PAGE_SIZE - len,
224 "# recv %ld (%ld) => reduction: ",
225 op->frames_filtered, op->frames_abs);
227 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
229 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
230 (reduction == 100)?"near ":"", reduction);
232 if (len > PAGE_SIZE - 200) {
233 /* mark output cut off */
234 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
239 list_for_each_entry(op, &bo->tx_ops, list) {
241 len += snprintf(page + len, PAGE_SIZE - len,
242 "tx_op: %03X %s [%d] ",
243 op->can_id, bcm_proc_getifname(op->ifindex),
246 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
250 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
253 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
256 if (len > PAGE_SIZE - 100) {
257 /* mark output cut off */
258 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
263 len += snprintf(page + len, PAGE_SIZE - len, "\n");
270 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
271 * of the given bcm tx op
273 static void bcm_can_tx(struct bcm_op *op)
276 struct net_device *dev;
277 struct can_frame *cf = &op->frames[op->currframe];
279 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
281 /* no target device? => exit */
285 dev = dev_get_by_index(op->ifindex);
288 /* RFC: should this bcm_op remove itself here? */
292 skb = alloc_skb(CFSIZ,
293 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
298 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
300 /* send with loopback */
305 /* update statistics */
309 /* reached last frame? */
310 if (op->currframe >= op->nframes)
317 * bcm_send_to_user - send a BCM message to the userspace
318 * (consisting of bcm_msg_head + x CAN frames)
320 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
321 struct can_frame *frames, struct timeval *tv)
324 struct can_frame *firstframe;
325 struct sock *sk = op->sk;
326 int datalen = head->nframes * CFSIZ;
327 struct sockaddr_can *addr;
330 skb = alloc_skb(sizeof(*head) + datalen,
331 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
335 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
337 /* can_frames starting here */
338 firstframe = (struct can_frame *) skb->tail;
341 skb_set_timestamp(skb, tv); /* restore timestamp */
343 addr = (struct sockaddr_can *)skb->cb;
344 memset(addr, 0, sizeof(*addr));
345 addr->can_family = AF_CAN;
346 /* restore originator for recvfrom() */
347 addr->can_ifindex = op->rx_ifindex;
350 memcpy(skb_put(skb, datalen), frames, datalen);
353 * the BCM uses the can_dlc-element of the can_frame
354 * structure for internal purposes. This is only
355 * relevant for updates that are generated by the
356 * BCM, where nframes is 1
358 if (head->nframes == 1)
359 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
362 err = sock_queue_rcv_skb(sk, skb);
364 struct bcm_opt *bo = bcm_sk(sk);
366 DBG("sock_queue_rcv_skb failed: %d\n", err);
368 /* don't care about overflows in this statistic */
369 bo->dropped_usr_msgs++;
374 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
376 static void bcm_tx_timeout_handler(unsigned long data)
378 struct bcm_op *op = (struct bcm_op*)data;
380 DBG("Called with bcm_op %p\n", op);
382 if (op->j_ival1 && (op->count > 0)) {
385 if (!op->count && (op->flags & TX_COUNTEVT)) {
386 struct bcm_msg_head msg_head;
388 /* create notification to user */
389 DBG("sending TX_EXPIRED for can_id %03X\n",
392 msg_head.opcode = TX_EXPIRED;
393 msg_head.flags = op->flags;
394 msg_head.count = op->count;
395 msg_head.ival1 = op->ival1;
396 msg_head.ival2 = op->ival2;
397 msg_head.can_id = op->can_id;
398 msg_head.nframes = 0;
400 bcm_send_to_user(op, &msg_head, NULL, NULL);
404 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
405 op->count, op->j_ival1, op->j_ival2);
407 if (op->j_ival1 && (op->count > 0)) {
409 op->timer.expires = jiffies + op->j_ival1;
410 add_timer(&op->timer);
412 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
414 (char*) op->timer.data,
415 (unsigned int) op->timer.expires);
417 /* send (next) frame */
422 op->timer.expires = jiffies + op->j_ival2;
423 add_timer(&op->timer);
425 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
427 (char*) op->timer.data,
428 (unsigned int) op->timer.expires);
430 /* send (next) frame */
434 DBG("no timer restart\n");
441 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
443 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
445 struct bcm_msg_head head;
447 op->j_lastmsg = jiffies;
449 /* update statistics */
450 op->frames_filtered++;
452 /* prevent statistics overflow */
453 if (op->frames_filtered > ULONG_MAX/100)
454 op->frames_filtered = op->frames_abs = 0;
456 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
457 (unsigned int) op->j_lastmsg, op);
458 DBG("sending notification\n");
460 head.opcode = RX_CHANGED;
461 head.flags = op->flags;
462 head.count = op->count;
463 head.ival1 = op->ival1;
464 head.ival2 = op->ival2;
465 head.can_id = op->can_id;
468 bcm_send_to_user(op, &head, data, &op->rx_stamp);
472 * bcm_rx_update_and_send - process a detected relevant receive content change
473 * 1. update the last received data
474 * 2. send a notification to the user (if possible)
476 static void bcm_rx_update_and_send(struct bcm_op *op,
477 struct can_frame *lastdata,
478 struct can_frame *rxdata)
480 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
482 memcpy(lastdata, rxdata, CFSIZ);
485 lastdata->can_dlc |= RX_RECV;
487 /* throttle bcm_rx_changed ? */
488 if ((op->thrtimer.expires) ||
489 ((op->j_ival2) && (nexttx > jiffies))) {
490 /* we are already waiting OR we have to start waiting */
492 /* mark as 'throttled' */
493 lastdata->can_dlc |= RX_THR;
495 if (!(op->thrtimer.expires)) {
496 /* start the timer only the first time */
497 op->thrtimer.expires = nexttx;
498 add_timer(&op->thrtimer);
500 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
501 op->thrtimer.function,
502 (char*) op->thrtimer.data,
503 (unsigned int) op->thrtimer.expires);
507 /* send RX_CHANGED to the user immediately */
508 bcm_rx_changed(op, rxdata);
513 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
514 * received data stored in op->last_frames[]
516 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
517 struct can_frame *rxdata)
520 * no one uses the MSBs of can_dlc for comparation,
521 * so we use it here to detect the first time of reception
524 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
525 /* received data for the first time => send update to user */
526 DBG("first time :)\n");
527 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
531 /* do a real check in can_frame data section */
533 DBG("op->frames[index].data = 0x%016llx\n",
534 GET_U64(&op->frames[index]));
535 DBG("op->last_frames[index].data = 0x%016llx\n",
536 GET_U64(&op->last_frames[index]));
537 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
539 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
540 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
541 DBG("relevant data change :)\n");
542 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
546 if (op->flags & RX_CHECK_DLC) {
547 /* do a real check in can_frame dlc */
548 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
550 DBG("dlc change :)\n");
551 bcm_rx_update_and_send(op, &op->last_frames[index],
556 DBG("no relevant change :(\n");
560 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
562 static void bcm_rx_starttimer(struct bcm_op *op)
564 if (op->flags & RX_NO_AUTOTIMER)
568 op->timer.expires = jiffies + op->j_ival1;
570 DBG("adding rx timeout timer ival1. func=%p data=%p "
573 (char*) op->timer.data,
574 (unsigned int) op->timer.expires);
576 add_timer(&op->timer);
581 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
583 static void bcm_rx_timeout_handler(unsigned long data)
585 struct bcm_op *op = (struct bcm_op*)data;
586 struct bcm_msg_head msg_head;
588 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
590 msg_head.opcode = RX_TIMEOUT;
591 msg_head.flags = op->flags;
592 msg_head.count = op->count;
593 msg_head.ival1 = op->ival1;
594 msg_head.ival2 = op->ival2;
595 msg_head.can_id = op->can_id;
596 msg_head.nframes = 0;
598 bcm_send_to_user(op, &msg_head, NULL, NULL);
600 /* no restart of the timer is done here! */
602 /* if user wants to be informed, when cyclic CAN-Messages come back */
603 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
604 /* clear received can_frames to indicate 'nothing received' */
605 memset(op->last_frames, 0, op->nframes * CFSIZ);
606 DBG("RX_ANNOUNCE_RESTART\n");
611 * bcm_rx_thr_handler - the time for blocked content updates is over now:
612 * Check for throttled data and send it to the userspace
614 static void bcm_rx_thr_handler(unsigned long data)
616 struct bcm_op *op = (struct bcm_op*)data;
619 /* mark disabled / consumed timer */
620 op->thrtimer.expires = 0;
622 if (op->nframes > 1) {
623 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
625 /* for MUX filter we start at index 1 */
626 for (i=1; i<op->nframes; i++) {
627 if ((op->last_frames) &&
628 (op->last_frames[i].can_dlc & RX_THR)) {
629 op->last_frames[i].can_dlc &= ~RX_THR;
630 bcm_rx_changed(op, &op->last_frames[i]);
635 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
637 /* for RX_FILTER_ID and simple filter */
638 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
639 op->last_frames[0].can_dlc &= ~RX_THR;
640 bcm_rx_changed(op, &op->last_frames[0]);
646 * bcm_rx_handler - handle a CAN frame receiption
648 static void bcm_rx_handler(struct sk_buff *skb, void *data)
650 struct bcm_op *op = (struct bcm_op*)data;
651 struct can_frame rxframe;
654 /* disable timeout */
655 del_timer(&op->timer);
657 DBG("Called with bcm_op %p\n", op);
659 if (skb->len == sizeof(rxframe)) {
660 memcpy(&rxframe, skb->data, sizeof(rxframe));
661 /* save rx timestamp */
662 skb_get_timestamp(skb, &op->rx_stamp);
663 /* save originator for recvfrom() */
664 op->rx_ifindex = skb->dev->ifindex;
665 /* update statistics */
668 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
671 DBG("Wrong skb->len = %d\n", skb->len);
676 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
678 if (op->can_id != rxframe.can_id) {
679 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
680 rxframe.can_id, op->can_id);
684 if (op->flags & RX_RTR_FRAME) {
685 /* send reply for RTR-request */
686 DBG("RTR-request\n");
688 /* send op->frames[0] to CAN device */
693 if (op->flags & RX_FILTER_ID) {
694 /* the easiest case */
695 DBG("Easy does it with RX_FILTER_ID\n");
697 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
698 bcm_rx_starttimer(op);
702 if (op->nframes == 1) {
703 /* simple compare with index 0 */
704 DBG("Simple compare\n");
706 bcm_rx_cmp_to_index(op, 0, &rxframe);
707 bcm_rx_starttimer(op);
711 if (op->nframes > 1) {
712 /* multiplex compare */
713 DBG("Multiplex compare\n");
716 * find the first multiplex mask that fits.
717 * Remark: The MUX-mask is stored in index 0
720 for (i=1; i < op->nframes; i++) {
721 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
722 (GET_U64(&op->frames[0]) &
723 GET_U64(&op->frames[i]))) {
724 DBG("found MUX index %d\n", i);
725 bcm_rx_cmp_to_index(op, i, &rxframe);
729 bcm_rx_starttimer(op);
734 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
736 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
741 list_for_each_entry(op, ops, list) {
742 if ((op->can_id == can_id) && (op->ifindex == ifindex))
749 static void bcm_remove_op(struct bcm_op *op)
751 del_timer(&op->timer);
752 del_timer(&op->thrtimer);
754 if ((op->frames) && (op->frames != &op->sframe))
757 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
758 kfree(op->last_frames);
766 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
768 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
770 struct bcm_op *op, *n;
772 list_for_each_entry_safe(op, n, ops, list) {
773 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
774 DBG("removing rx_op %p for can_id %03X\n",
778 * Don't care if we're bound or not (due to netdev
779 * problems) can_rx_unregister() is always a save
783 struct net_device *dev =
784 dev_get_by_index(op->ifindex);
787 can_rx_unregister(dev, op->can_id,
794 can_rx_unregister(NULL, op->can_id,
804 return 0; /* not found */
808 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
810 static int bcm_delete_tx_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",
824 return 0; /* not found */
828 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
830 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
833 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
836 DBG("TRX_READ: did not find op for can_id %03X\n",
841 DBG("TRX_READ: sending status for can_id %03X\n",
843 /* put current values into msg_head */
844 msg_head->flags = op->flags;
845 msg_head->count = op->count;
846 msg_head->ival1 = op->ival1;
847 msg_head->ival2 = op->ival2;
848 msg_head->nframes = op->nframes;
850 bcm_send_to_user(op, msg_head, op->frames, NULL);
856 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
858 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
859 int ifindex, struct sock *sk)
861 struct bcm_opt *bo = bcm_sk(sk);
865 /* we need a real device to send frames */
869 /* we need at least one can_frame */
870 if (msg_head->nframes < 1)
873 /* check the given can_id */
874 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
877 /* update existing BCM operation */
879 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
880 op, msg_head->can_id);
883 * Do we need more space for the can_frames than currently
884 * allocated? -> This is a _really_ unusual use-case and
885 * therefore (complexity / locking) it is not supported.
887 if (msg_head->nframes > op->nframes)
890 /* update can_frames content */
891 for (i = 0; i < msg_head->nframes; i++) {
892 err = memcpy_fromiovec((u8*)&op->frames[i],
893 msg->msg_iov, CFSIZ);
897 if (msg_head->flags & TX_CP_CAN_ID) {
898 /* copy can_id into frame */
899 op->frames[i].can_id = msg_head->can_id;
904 /* insert new BCM operation for the given can_id */
906 op = kzalloc(OPSIZ, GFP_KERNEL);
910 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
911 op, msg_head->can_id);
913 op->can_id = msg_head->can_id;
915 /* create array for can_frames and copy the data */
916 if (msg_head->nframes > 1) {
917 op->frames = kmalloc(msg_head->nframes * CFSIZ,
924 op->frames = &op->sframe;
926 for (i = 0; i < msg_head->nframes; i++) {
927 err = memcpy_fromiovec((u8*)&op->frames[i],
928 msg->msg_iov, CFSIZ);
935 if (msg_head->flags & TX_CP_CAN_ID) {
936 /* copy can_id into frame */
937 op->frames[i].can_id = msg_head->can_id;
941 /* tx_ops never compare with previous received messages */
942 op->last_frames = NULL;
944 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
947 op->ifindex = ifindex;
949 /* initialize uninitialized (kmalloc) structure */
950 init_timer(&op->timer);
952 /* currently unused in tx_ops */
953 init_timer(&op->thrtimer);
955 /* handler for tx_ops */
956 op->timer.function = bcm_tx_timeout_handler;
958 /* timer.data points to this op-structure */
959 op->timer.data = (unsigned long)op;
961 /* add this bcm_op to the list of the tx_ops */
962 list_add(&op->list, &bo->tx_ops);
964 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
966 if (op->nframes != msg_head->nframes) {
967 op->nframes = msg_head->nframes;
968 /* start multiple frame transmission with index 0 */
974 op->flags = msg_head->flags;
976 if (op->flags & TX_RESET_MULTI_IDX) {
977 /* start multiple frame transmission with index 0 */
981 if (op->flags & SETTIMER) {
982 /* set timer values */
984 op->count = msg_head->count;
985 op->ival1 = msg_head->ival1;
986 op->ival2 = msg_head->ival2;
987 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
988 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
990 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
991 op->count, op->j_ival1, op->j_ival2);
993 /* disable an active timer due to zero values? */
994 if (!op->j_ival1 && !op->j_ival2) {
995 del_timer(&op->timer);
996 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1000 if ((op->flags & STARTTIMER) &&
1001 ((op->j_ival1 && op->count) || op->j_ival2)) {
1003 del_timer(&op->timer);
1005 /* spec: send can_frame when starting timer */
1006 op->flags |= TX_ANNOUNCE;
1008 if (op->j_ival1 && (op->count > 0)) {
1009 op->timer.expires = jiffies + op->j_ival1;
1010 /* op->count-- is done in bcm_tx_timeout_handler */
1011 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1014 (char*) op->timer.data,
1015 (unsigned int) op->timer.expires);
1018 op->timer.expires = jiffies + op->j_ival2;
1019 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1022 (char*) op->timer.data,
1023 (unsigned int) op->timer.expires);
1026 add_timer(&op->timer);
1029 if (op->flags & TX_ANNOUNCE)
1032 return msg_head->nframes * CFSIZ + MHSIZ;
1036 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1038 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1039 int ifindex, struct sock *sk)
1041 struct bcm_opt *bo = bcm_sk(sk);
1046 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1047 /* be robust against wrong usage ... */
1048 msg_head->flags |= RX_FILTER_ID;
1049 msg_head->nframes = 0; /* ignore trailing garbage */
1052 if ((msg_head->flags & RX_RTR_FRAME) &&
1053 ((msg_head->nframes != 1) ||
1054 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1056 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1060 /* check the given can_id */
1061 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1064 /* update existing BCM operation */
1066 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1067 op, msg_head->can_id);
1070 * Do we need more space for the can_frames than currently
1071 * allocated? -> This is a _really_ unusual use-case and
1072 * therefore (complexity / locking) it is not supported.
1074 if (msg_head->nframes > op->nframes)
1077 if (msg_head->nframes) {
1078 /* update can_frames content */
1079 err = memcpy_fromiovec((u8*)op->frames,
1081 msg_head->nframes * CFSIZ);
1085 /* clear last_frames to indicate 'nothing received' */
1086 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1089 op->nframes = msg_head->nframes;
1091 /* Only an update -> do not call can_rx_register() */
1095 /* insert new BCM operation for the given can_id */
1097 op = kzalloc(OPSIZ, GFP_KERNEL);
1101 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1102 op, msg_head->can_id);
1104 op->can_id = msg_head->can_id;
1105 op->nframes = msg_head->nframes;
1107 if (msg_head->nframes) {
1109 /* create array for can_frames and copy the data */
1110 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1117 err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1118 msg_head->nframes * CFSIZ);
1125 /* create and init array for received can_frames */
1126 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1128 if (!op->last_frames) {
1135 /* op->frames = NULL due to memset in kzalloc() */
1138 * even when we have the RX_FILTER_ID case, we need
1139 * to store the last frame for the throttle feature
1142 /* create and init array for received can_frames */
1143 op->last_frames = kzalloc(CFSIZ, GFP_KERNEL);
1144 if (!op->last_frames) {
1151 op->ifindex = ifindex;
1153 /* initialize uninitialized (kzalloc) structure */
1154 init_timer(&op->timer);
1156 /* init throttle timer for RX_CHANGED */
1157 init_timer(&op->thrtimer);
1159 /* handler for rx timeouts */
1160 op->timer.function = bcm_rx_timeout_handler;
1162 /* timer.data points to this op-structure */
1163 op->timer.data = (unsigned long)op;
1165 /* handler for RX_CHANGED throttle timeouts */
1166 op->thrtimer.function = bcm_rx_thr_handler;
1168 /* timer.data points to this op-structure */
1169 op->thrtimer.data = (unsigned long)op;
1171 /* mark disabled timer */
1172 op->thrtimer.expires = 0;
1174 /* add this bcm_op to the list of the tx_ops */
1175 list_add(&op->list, &bo->rx_ops);
1177 /* call can_rx_register() */
1180 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1183 op->flags = msg_head->flags;
1185 if (op->flags & RX_RTR_FRAME) {
1187 /* no timers in RTR-mode */
1188 del_timer(&op->thrtimer);
1189 del_timer(&op->timer);
1192 * funny feature in RX(!)_SETUP only for RTR-mode:
1193 * copy can_id into frame BUT without RTR-flag to
1194 * prevent a full-load-loopback-test ... ;-]
1196 if ((op->flags & TX_CP_CAN_ID) ||
1197 (op->frames[0].can_id == op->can_id))
1198 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1201 if (op->flags & SETTIMER) {
1203 /* set timer value */
1204 op->ival1 = msg_head->ival1;
1205 op->ival2 = msg_head->ival2;
1206 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
1207 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
1209 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1210 op->j_ival1, op->j_ival2);
1212 /* disable an active timer due to zero value? */
1214 del_timer(&op->timer);
1215 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1218 /* free currently blocked msgs ? */
1219 if (op->thrtimer.expires) {
1220 DBG("RX_SETUP: unblocking throttled msgs.\n");
1221 del_timer(&op->thrtimer);
1222 /* send blocked msgs hereafter */
1223 op->thrtimer.expires = jiffies + 2;
1224 add_timer(&op->thrtimer);
1227 * if (op->j_ival2) is zero, no (new) throttling
1228 * will happen. For details see functions
1229 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1233 if ((op->flags & STARTTIMER) && op->j_ival1) {
1235 del_timer(&op->timer);
1236 op->timer.expires = jiffies + op->j_ival1;
1238 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1240 (char *) op->timer.function,
1241 (char *) op->timer.data,
1242 (unsigned int) op->timer.expires);
1244 add_timer(&op->timer);
1248 /* now we can register for can_ids, if we added a new bcm_op */
1249 if (do_rx_register) {
1250 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1251 "rx_op is %p\n", op->can_id, op);
1254 struct net_device *dev = dev_get_by_index(ifindex);
1257 can_rx_register(dev, op->can_id,
1258 REGMASK(op->can_id),
1259 bcm_rx_handler, op, IDENT);
1264 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1265 bcm_rx_handler, op, IDENT);
1268 return msg_head->nframes * CFSIZ + MHSIZ;
1272 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1274 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1276 struct sk_buff *skb;
1277 struct net_device *dev;
1280 /* just copy and send one can_frame */
1282 if (!ifindex) /* we need a real device to send frames */
1285 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1290 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1296 DBG_FRAME("BCM: TX_SEND: sending frame",
1297 (struct can_frame *)skb->data);
1299 dev = dev_get_by_index(ifindex);
1307 can_send(skb, 1); /* send with loopback */
1310 return CFSIZ + MHSIZ;
1314 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1316 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1317 struct msghdr *msg, size_t size)
1319 struct sock *sk = sock->sk;
1320 struct bcm_opt *bo = bcm_sk(sk);
1321 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1322 struct bcm_msg_head msg_head;
1323 int ret; /* read bytes or error codes as return value */
1326 DBG("sock %p not bound\n", sk);
1330 /* check for alternative ifindex for this bcm_op */
1332 if (!ifindex && msg->msg_name) {
1333 /* no bound device as default => check msg_name */
1334 struct sockaddr_can *addr =
1335 (struct sockaddr_can *)msg->msg_name;
1337 if (addr->can_family != AF_CAN)
1340 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1342 if (ifindex && !dev_get_by_index(ifindex)) {
1343 DBG("device %d not found\n", ifindex);
1348 /* read message head information */
1350 ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1354 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1356 switch (msg_head.opcode) {
1359 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1363 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1367 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1374 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1381 /* reuse msg_head for the reply to TX_READ */
1382 msg_head.opcode = TX_STATUS;
1383 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1387 /* reuse msg_head for the reply to RX_READ */
1388 msg_head.opcode = RX_STATUS;
1389 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1393 /* we need at least one can_frame */
1394 if (msg_head.nframes < 1)
1397 ret = bcm_tx_send(msg, ifindex, sk);
1401 DBG("Unknown opcode %d\n", msg_head.opcode);
1410 * initial settings for all BCM sockets to be set at socket creation time
1412 static int bcm_init(struct sock *sk)
1414 struct bcm_opt *bo = bcm_sk(sk);
1418 bo->dropped_usr_msgs = 0;
1419 bo->bcm_proc_read = NULL;
1421 INIT_LIST_HEAD(&bo->tx_ops);
1422 INIT_LIST_HEAD(&bo->rx_ops);
1428 * notification handler for netdevice status changes
1430 static void bcm_notifier(unsigned long msg, void *data)
1432 struct sock *sk = (struct sock *)data;
1433 struct bcm_opt *bo = bcm_sk(sk);
1435 DBG("called for sock %p\n", sk);
1439 case NETDEV_UNREGISTER:
1444 sk->sk_err = ENETDOWN;
1445 if (!sock_flag(sk, SOCK_DEAD))
1446 sk->sk_error_report(sk);
1451 * standard socket functions
1453 static int bcm_release(struct socket *sock)
1455 struct sock *sk = sock->sk;
1456 struct bcm_opt *bo = bcm_sk(sk);
1457 struct bcm_op *op, *next;
1459 DBG("socket %p, sk %p\n", sock, sk);
1461 /* remove bcm_ops, timer, rx_unregister(), etc. */
1463 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1464 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1468 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1469 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1472 * Don't care if we're bound or not (due to netdev problems)
1473 * can_rx_unregister() is always a save thing to do here.
1476 struct net_device *dev = dev_get_by_index(op->ifindex);
1479 can_rx_unregister(dev, op->can_id,
1480 REGMASK(op->can_id),
1481 bcm_rx_handler, op);
1486 can_rx_unregister(NULL, op->can_id,
1487 REGMASK(op->can_id),
1488 bcm_rx_handler, op);
1493 /* remove procfs entry */
1494 if (proc_dir && bo->bcm_proc_read)
1495 remove_proc_entry(bo->procname, proc_dir);
1497 /* remove device notifier */
1499 struct net_device *dev = dev_get_by_index(bo->ifindex);
1502 can_dev_unregister(dev, bcm_notifier, sk);
1512 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1515 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1516 struct sock *sk = sock->sk;
1517 struct bcm_opt *bo = bcm_sk(sk);
1522 /* bind a device to this socket */
1523 if (addr->can_ifindex) {
1524 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1527 DBG("could not find device index %d\n",
1531 bo->ifindex = dev->ifindex;
1532 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1535 DBG("socket %p bound to device %s (idx %d)\n",
1536 sock, dev->name, dev->ifindex);
1539 /* no notifier for ifindex = 0 ('any' CAN device) */
1546 /* unique socket address as filename */
1547 sprintf(bo->procname, "%p", sock);
1548 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1556 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1557 struct msghdr *msg, size_t size, int flags)
1559 struct sock *sk = sock->sk;
1560 struct sk_buff *skb;
1565 DBG("socket %p, sk %p\n", sock, sk);
1567 noblock = flags & MSG_DONTWAIT;
1568 flags &= ~MSG_DONTWAIT;
1569 skb = skb_recv_datagram(sk, flags, noblock, &error);
1573 DBG("delivering skbuff %p\n", skb);
1576 if (skb->len < size)
1579 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1581 skb_free_datagram(sk, skb);
1585 sock_recv_timestamp(msg, sk, skb);
1587 if (msg->msg_name) {
1588 msg->msg_namelen = sizeof(struct sockaddr_can);
1589 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1592 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1593 skb_free_datagram(sk, skb);
1598 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1601 unsigned int mask = 0;
1603 DBG("socket %p\n", sock);
1605 mask = datagram_poll(file, sock, wait);
1609 static struct proto_ops bcm_ops = {
1611 .release = bcm_release,
1612 .bind = sock_no_bind,
1613 .connect = bcm_connect,
1614 .socketpair = sock_no_socketpair,
1615 .accept = sock_no_accept,
1616 .getname = sock_no_getname,
1618 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1619 .listen = sock_no_listen,
1620 .shutdown = sock_no_shutdown,
1621 .setsockopt = sock_no_setsockopt,
1622 .getsockopt = sock_no_getsockopt,
1623 .sendmsg = bcm_sendmsg,
1624 .recvmsg = bcm_recvmsg,
1625 .mmap = sock_no_mmap,
1626 .sendpage = sock_no_sendpage,
1629 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1630 static struct proto bcm_proto = {
1632 .owner = THIS_MODULE,
1633 .obj_size = sizeof(struct bcm_sock),
1637 static struct can_proto bcm_can_proto = {
1639 .protocol = CAN_BCM,
1640 .capability = BCM_CAP,
1645 static struct can_proto bcm_can_proto = {
1647 .protocol = CAN_BCM,
1648 .capability = BCM_CAP,
1650 .owner = THIS_MODULE,
1651 .obj_size = sizeof(struct bcm_opt),
1656 static int __init bcm_module_init(void)
1660 can_proto_register(&bcm_can_proto);
1662 /* create /proc/net/can/bcm directory */
1663 proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1666 proc_dir->owner = THIS_MODULE;
1671 static void __exit bcm_module_exit(void)
1673 can_proto_unregister(&bcm_can_proto);
1676 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1679 module_init(bcm_module_init);
1680 module_exit(bcm_module_exit);