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 and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/hrtimer.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include <linux/uio.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/socket.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <linux/can.h>
57 #include <linux/can/core.h>
58 #include <linux/can/bcm.h>
60 #include <net/net_namespace.h>
62 /* use of last_frames[index].can_dlc */
63 #define RX_RECV 0x40 /* received data for this element */
64 #define RX_THR 0x80 /* element not been sent due to throttle feature */
65 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
67 /* get best masking value for can_rx_register() for a given single can_id */
68 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
69 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
70 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
72 #define CAN_BCM_VERSION CAN_VERSION
73 static __initdata
const char banner
[] = KERN_INFO
74 "can: broadcast manager protocol (rev " CAN_BCM_VERSION
" t)\n";
76 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 MODULE_ALIAS("can-proto-2");
81 /* easy access to can_frame payload */
82 static inline u64
GET_U64(const struct can_frame
*cp
)
84 return *(u64
*)cp
->data
;
88 struct list_head list
;
92 unsigned long frames_abs
, frames_filtered
;
93 struct timeval ival1
, ival2
;
94 struct hrtimer timer
, thrtimer
;
95 struct tasklet_struct tsklet
, thrtsklet
;
96 ktime_t rx_stamp
, kt_ival1
, kt_ival2
, kt_lastmsg
;
101 struct can_frame
*frames
;
102 struct can_frame
*last_frames
;
103 struct can_frame sframe
;
104 struct can_frame last_sframe
;
106 struct net_device
*rx_reg_dev
;
109 static struct proc_dir_entry
*proc_dir
;
115 struct notifier_block notifier
;
116 struct list_head rx_ops
;
117 struct list_head tx_ops
;
118 unsigned long dropped_usr_msgs
;
119 struct proc_dir_entry
*bcm_proc_read
;
120 char procname
[9]; /* pointer printed in ASCII with \0 */
123 static inline struct bcm_sock
*bcm_sk(const struct sock
*sk
)
125 return (struct bcm_sock
*)sk
;
128 #define CFSIZ sizeof(struct can_frame)
129 #define OPSIZ sizeof(struct bcm_op)
130 #define MHSIZ sizeof(struct bcm_msg_head)
135 static char *bcm_proc_getifname(int ifindex
)
137 struct net_device
*dev
;
142 /* no usage counting */
143 dev
= __dev_get_by_index(&init_net
, ifindex
);
150 static int bcm_proc_show(struct seq_file
*m
, void *v
)
152 struct sock
*sk
= (struct sock
*)m
->private;
153 struct bcm_sock
*bo
= bcm_sk(sk
);
156 seq_printf(m
, ">>> socket %p", sk
->sk_socket
);
157 seq_printf(m
, " / sk %p", sk
);
158 seq_printf(m
, " / bo %p", bo
);
159 seq_printf(m
, " / dropped %lu", bo
->dropped_usr_msgs
);
160 seq_printf(m
, " / bound %s", bcm_proc_getifname(bo
->ifindex
));
161 seq_printf(m
, " <<<\n");
163 list_for_each_entry(op
, &bo
->rx_ops
, list
) {
165 unsigned long reduction
;
167 /* print only active entries & prevent division by zero */
171 seq_printf(m
, "rx_op: %03X %-5s ",
172 op
->can_id
, bcm_proc_getifname(op
->ifindex
));
173 seq_printf(m
, "[%d]%c ", op
->nframes
,
174 (op
->flags
& RX_CHECK_DLC
)?'d':' ');
175 if (op
->kt_ival1
.tv64
)
176 seq_printf(m
, "timeo=%lld ",
178 ktime_to_us(op
->kt_ival1
));
180 if (op
->kt_ival2
.tv64
)
181 seq_printf(m
, "thr=%lld ",
183 ktime_to_us(op
->kt_ival2
));
185 seq_printf(m
, "# recv %ld (%ld) => reduction: ",
186 op
->frames_filtered
, op
->frames_abs
);
188 reduction
= 100 - (op
->frames_filtered
* 100) / op
->frames_abs
;
190 seq_printf(m
, "%s%ld%%\n",
191 (reduction
== 100)?"near ":"", reduction
);
194 list_for_each_entry(op
, &bo
->tx_ops
, list
) {
196 seq_printf(m
, "tx_op: %03X %s [%d] ",
197 op
->can_id
, bcm_proc_getifname(op
->ifindex
),
200 if (op
->kt_ival1
.tv64
)
201 seq_printf(m
, "t1=%lld ",
202 (long long) ktime_to_us(op
->kt_ival1
));
204 if (op
->kt_ival2
.tv64
)
205 seq_printf(m
, "t2=%lld ",
206 (long long) ktime_to_us(op
->kt_ival2
));
208 seq_printf(m
, "# sent %ld\n", op
->frames_abs
);
214 static int bcm_proc_open(struct inode
*inode
, struct file
*file
)
216 return single_open(file
, bcm_proc_show
, PDE(inode
)->data
);
219 static const struct file_operations bcm_proc_fops
= {
220 .owner
= THIS_MODULE
,
221 .open
= bcm_proc_open
,
224 .release
= single_release
,
228 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
229 * of the given bcm tx op
231 static void bcm_can_tx(struct bcm_op
*op
)
234 struct net_device
*dev
;
235 struct can_frame
*cf
= &op
->frames
[op
->currframe
];
237 /* no target device? => exit */
241 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
243 /* RFC: should this bcm_op remove itself here? */
247 skb
= alloc_skb(CFSIZ
, gfp_any());
251 memcpy(skb_put(skb
, CFSIZ
), cf
, CFSIZ
);
253 /* send with loopback */
258 /* update statistics */
262 /* reached last frame? */
263 if (op
->currframe
>= op
->nframes
)
270 * bcm_send_to_user - send a BCM message to the userspace
271 * (consisting of bcm_msg_head + x CAN frames)
273 static void bcm_send_to_user(struct bcm_op
*op
, struct bcm_msg_head
*head
,
274 struct can_frame
*frames
, int has_timestamp
)
277 struct can_frame
*firstframe
;
278 struct sockaddr_can
*addr
;
279 struct sock
*sk
= op
->sk
;
280 int datalen
= head
->nframes
* CFSIZ
;
283 skb
= alloc_skb(sizeof(*head
) + datalen
, gfp_any());
287 memcpy(skb_put(skb
, sizeof(*head
)), head
, sizeof(*head
));
290 /* can_frames starting here */
291 firstframe
= (struct can_frame
*)skb_tail_pointer(skb
);
293 memcpy(skb_put(skb
, datalen
), frames
, datalen
);
296 * the BCM uses the can_dlc-element of the can_frame
297 * structure for internal purposes. This is only
298 * relevant for updates that are generated by the
299 * BCM, where nframes is 1
301 if (head
->nframes
== 1)
302 firstframe
->can_dlc
&= BCM_CAN_DLC_MASK
;
306 /* restore rx timestamp */
307 skb
->tstamp
= op
->rx_stamp
;
311 * Put the datagram to the queue so that bcm_recvmsg() can
312 * get it from there. We need to pass the interface index to
313 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
314 * containing the interface index.
317 BUILD_BUG_ON(sizeof(skb
->cb
) < sizeof(struct sockaddr_can
));
318 addr
= (struct sockaddr_can
*)skb
->cb
;
319 memset(addr
, 0, sizeof(*addr
));
320 addr
->can_family
= AF_CAN
;
321 addr
->can_ifindex
= op
->rx_ifindex
;
323 err
= sock_queue_rcv_skb(sk
, skb
);
325 struct bcm_sock
*bo
= bcm_sk(sk
);
328 /* don't care about overflows in this statistic */
329 bo
->dropped_usr_msgs
++;
333 static void bcm_tx_timeout_tsklet(unsigned long data
)
335 struct bcm_op
*op
= (struct bcm_op
*)data
;
336 struct bcm_msg_head msg_head
;
338 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
341 if (!op
->count
&& (op
->flags
& TX_COUNTEVT
)) {
343 /* create notification to user */
344 msg_head
.opcode
= TX_EXPIRED
;
345 msg_head
.flags
= op
->flags
;
346 msg_head
.count
= op
->count
;
347 msg_head
.ival1
= op
->ival1
;
348 msg_head
.ival2
= op
->ival2
;
349 msg_head
.can_id
= op
->can_id
;
350 msg_head
.nframes
= 0;
352 bcm_send_to_user(op
, &msg_head
, NULL
, 0);
356 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
358 /* send (next) frame */
360 hrtimer_start(&op
->timer
,
361 ktime_add(ktime_get(), op
->kt_ival1
),
365 if (op
->kt_ival2
.tv64
) {
367 /* send (next) frame */
369 hrtimer_start(&op
->timer
,
370 ktime_add(ktime_get(), op
->kt_ival2
),
377 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
379 static enum hrtimer_restart
bcm_tx_timeout_handler(struct hrtimer
*hrtimer
)
381 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
383 tasklet_schedule(&op
->tsklet
);
385 return HRTIMER_NORESTART
;
389 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
391 static void bcm_rx_changed(struct bcm_op
*op
, struct can_frame
*data
)
393 struct bcm_msg_head head
;
395 /* update statistics */
396 op
->frames_filtered
++;
398 /* prevent statistics overflow */
399 if (op
->frames_filtered
> ULONG_MAX
/100)
400 op
->frames_filtered
= op
->frames_abs
= 0;
402 /* this element is not throttled anymore */
403 data
->can_dlc
&= (BCM_CAN_DLC_MASK
|RX_RECV
);
405 head
.opcode
= RX_CHANGED
;
406 head
.flags
= op
->flags
;
407 head
.count
= op
->count
;
408 head
.ival1
= op
->ival1
;
409 head
.ival2
= op
->ival2
;
410 head
.can_id
= op
->can_id
;
413 bcm_send_to_user(op
, &head
, data
, 1);
417 * bcm_rx_update_and_send - process a detected relevant receive content change
418 * 1. update the last received data
419 * 2. send a notification to the user (if possible)
421 static void bcm_rx_update_and_send(struct bcm_op
*op
,
422 struct can_frame
*lastdata
,
423 const struct can_frame
*rxdata
)
425 memcpy(lastdata
, rxdata
, CFSIZ
);
427 /* mark as used and throttled by default */
428 lastdata
->can_dlc
|= (RX_RECV
|RX_THR
);
430 /* throtteling mode inactive ? */
431 if (!op
->kt_ival2
.tv64
) {
432 /* send RX_CHANGED to the user immediately */
433 bcm_rx_changed(op
, lastdata
);
437 /* with active throttling timer we are just done here */
438 if (hrtimer_active(&op
->thrtimer
))
441 /* first receiption with enabled throttling mode */
442 if (!op
->kt_lastmsg
.tv64
)
443 goto rx_changed_settime
;
445 /* got a second frame inside a potential throttle period? */
446 if (ktime_us_delta(ktime_get(), op
->kt_lastmsg
) <
447 ktime_to_us(op
->kt_ival2
)) {
448 /* do not send the saved data - only start throttle timer */
449 hrtimer_start(&op
->thrtimer
,
450 ktime_add(op
->kt_lastmsg
, op
->kt_ival2
),
455 /* the gap was that big, that throttling was not needed here */
457 bcm_rx_changed(op
, lastdata
);
458 op
->kt_lastmsg
= ktime_get();
462 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
463 * received data stored in op->last_frames[]
465 static void bcm_rx_cmp_to_index(struct bcm_op
*op
, int index
,
466 const struct can_frame
*rxdata
)
469 * no one uses the MSBs of can_dlc for comparation,
470 * so we use it here to detect the first time of reception
473 if (!(op
->last_frames
[index
].can_dlc
& RX_RECV
)) {
474 /* received data for the first time => send update to user */
475 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
479 /* do a real check in can_frame data section */
481 if ((GET_U64(&op
->frames
[index
]) & GET_U64(rxdata
)) !=
482 (GET_U64(&op
->frames
[index
]) & GET_U64(&op
->last_frames
[index
]))) {
483 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
487 if (op
->flags
& RX_CHECK_DLC
) {
488 /* do a real check in can_frame dlc */
489 if (rxdata
->can_dlc
!= (op
->last_frames
[index
].can_dlc
&
491 bcm_rx_update_and_send(op
, &op
->last_frames
[index
],
499 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
501 static void bcm_rx_starttimer(struct bcm_op
*op
)
503 if (op
->flags
& RX_NO_AUTOTIMER
)
506 if (op
->kt_ival1
.tv64
)
507 hrtimer_start(&op
->timer
, op
->kt_ival1
, HRTIMER_MODE_REL
);
510 static void bcm_rx_timeout_tsklet(unsigned long data
)
512 struct bcm_op
*op
= (struct bcm_op
*)data
;
513 struct bcm_msg_head msg_head
;
515 /* create notification to user */
516 msg_head
.opcode
= RX_TIMEOUT
;
517 msg_head
.flags
= op
->flags
;
518 msg_head
.count
= op
->count
;
519 msg_head
.ival1
= op
->ival1
;
520 msg_head
.ival2
= op
->ival2
;
521 msg_head
.can_id
= op
->can_id
;
522 msg_head
.nframes
= 0;
524 bcm_send_to_user(op
, &msg_head
, NULL
, 0);
528 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
530 static enum hrtimer_restart
bcm_rx_timeout_handler(struct hrtimer
*hrtimer
)
532 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
534 /* schedule before NET_RX_SOFTIRQ */
535 tasklet_hi_schedule(&op
->tsklet
);
537 /* no restart of the timer is done here! */
539 /* if user wants to be informed, when cyclic CAN-Messages come back */
540 if ((op
->flags
& RX_ANNOUNCE_RESUME
) && op
->last_frames
) {
541 /* clear received can_frames to indicate 'nothing received' */
542 memset(op
->last_frames
, 0, op
->nframes
* CFSIZ
);
545 return HRTIMER_NORESTART
;
549 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
551 static inline int bcm_rx_do_flush(struct bcm_op
*op
, int update
, int index
)
553 if ((op
->last_frames
) && (op
->last_frames
[index
].can_dlc
& RX_THR
)) {
555 bcm_rx_changed(op
, &op
->last_frames
[index
]);
562 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
564 * update == 0 : just check if throttled data is available (any irq context)
565 * update == 1 : check and send throttled data to userspace (soft_irq context)
567 static int bcm_rx_thr_flush(struct bcm_op
*op
, int update
)
571 if (op
->nframes
> 1) {
574 /* for MUX filter we start at index 1 */
575 for (i
= 1; i
< op
->nframes
; i
++)
576 updated
+= bcm_rx_do_flush(op
, update
, i
);
579 /* for RX_FILTER_ID and simple filter */
580 updated
+= bcm_rx_do_flush(op
, update
, 0);
586 static void bcm_rx_thr_tsklet(unsigned long data
)
588 struct bcm_op
*op
= (struct bcm_op
*)data
;
590 /* push the changed data to the userspace */
591 bcm_rx_thr_flush(op
, 1);
595 * bcm_rx_thr_handler - the time for blocked content updates is over now:
596 * Check for throttled data and send it to the userspace
598 static enum hrtimer_restart
bcm_rx_thr_handler(struct hrtimer
*hrtimer
)
600 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, thrtimer
);
602 tasklet_schedule(&op
->thrtsklet
);
604 if (bcm_rx_thr_flush(op
, 0)) {
605 hrtimer_forward(hrtimer
, ktime_get(), op
->kt_ival2
);
606 return HRTIMER_RESTART
;
608 /* rearm throttle handling */
609 op
->kt_lastmsg
= ktime_set(0, 0);
610 return HRTIMER_NORESTART
;
615 * bcm_rx_handler - handle a CAN frame receiption
617 static void bcm_rx_handler(struct sk_buff
*skb
, void *data
)
619 struct bcm_op
*op
= (struct bcm_op
*)data
;
620 const struct can_frame
*rxframe
= (struct can_frame
*)skb
->data
;
623 /* disable timeout */
624 hrtimer_cancel(&op
->timer
);
626 if (op
->can_id
!= rxframe
->can_id
)
629 /* save rx timestamp */
630 op
->rx_stamp
= skb
->tstamp
;
631 /* save originator for recvfrom() */
632 op
->rx_ifindex
= skb
->dev
->ifindex
;
633 /* update statistics */
636 if (op
->flags
& RX_RTR_FRAME
) {
637 /* send reply for RTR-request (placed in op->frames[0]) */
642 if (op
->flags
& RX_FILTER_ID
) {
643 /* the easiest case */
644 bcm_rx_update_and_send(op
, &op
->last_frames
[0], rxframe
);
648 if (op
->nframes
== 1) {
649 /* simple compare with index 0 */
650 bcm_rx_cmp_to_index(op
, 0, rxframe
);
654 if (op
->nframes
> 1) {
658 * find the first multiplex mask that fits.
659 * Remark: The MUX-mask is stored in index 0
662 for (i
= 1; i
< op
->nframes
; i
++) {
663 if ((GET_U64(&op
->frames
[0]) & GET_U64(rxframe
)) ==
664 (GET_U64(&op
->frames
[0]) &
665 GET_U64(&op
->frames
[i
]))) {
666 bcm_rx_cmp_to_index(op
, i
, rxframe
);
673 bcm_rx_starttimer(op
);
677 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
679 static struct bcm_op
*bcm_find_op(struct list_head
*ops
, canid_t can_id
,
684 list_for_each_entry(op
, ops
, list
) {
685 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
))
692 static void bcm_remove_op(struct bcm_op
*op
)
694 hrtimer_cancel(&op
->timer
);
695 hrtimer_cancel(&op
->thrtimer
);
698 tasklet_kill(&op
->tsklet
);
700 if (op
->thrtsklet
.func
)
701 tasklet_kill(&op
->thrtsklet
);
703 if ((op
->frames
) && (op
->frames
!= &op
->sframe
))
706 if ((op
->last_frames
) && (op
->last_frames
!= &op
->last_sframe
))
707 kfree(op
->last_frames
);
714 static void bcm_rx_unreg(struct net_device
*dev
, struct bcm_op
*op
)
716 if (op
->rx_reg_dev
== dev
) {
717 can_rx_unregister(dev
, op
->can_id
, REGMASK(op
->can_id
),
720 /* mark as removed subscription */
721 op
->rx_reg_dev
= NULL
;
723 printk(KERN_ERR
"can-bcm: bcm_rx_unreg: registered device "
724 "mismatch %p %p\n", op
->rx_reg_dev
, dev
);
728 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
730 static int bcm_delete_rx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
732 struct bcm_op
*op
, *n
;
734 list_for_each_entry_safe(op
, n
, ops
, list
) {
735 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
738 * Don't care if we're bound or not (due to netdev
739 * problems) can_rx_unregister() is always a save
744 * Only remove subscriptions that had not
745 * been removed due to NETDEV_UNREGISTER
748 if (op
->rx_reg_dev
) {
749 struct net_device
*dev
;
751 dev
= dev_get_by_index(&init_net
,
754 bcm_rx_unreg(dev
, op
);
759 can_rx_unregister(NULL
, op
->can_id
,
769 return 0; /* not found */
773 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
775 static int bcm_delete_tx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
777 struct bcm_op
*op
, *n
;
779 list_for_each_entry_safe(op
, n
, ops
, list
) {
780 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
787 return 0; /* not found */
791 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
793 static int bcm_read_op(struct list_head
*ops
, struct bcm_msg_head
*msg_head
,
796 struct bcm_op
*op
= bcm_find_op(ops
, msg_head
->can_id
, ifindex
);
801 /* put current values into msg_head */
802 msg_head
->flags
= op
->flags
;
803 msg_head
->count
= op
->count
;
804 msg_head
->ival1
= op
->ival1
;
805 msg_head
->ival2
= op
->ival2
;
806 msg_head
->nframes
= op
->nframes
;
808 bcm_send_to_user(op
, msg_head
, op
->frames
, 0);
814 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
816 static int bcm_tx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
817 int ifindex
, struct sock
*sk
)
819 struct bcm_sock
*bo
= bcm_sk(sk
);
823 /* we need a real device to send frames */
827 /* we need at least one can_frame */
828 if (msg_head
->nframes
< 1)
831 /* check the given can_id */
832 op
= bcm_find_op(&bo
->tx_ops
, msg_head
->can_id
, ifindex
);
835 /* update existing BCM operation */
838 * Do we need more space for the can_frames than currently
839 * allocated? -> This is a _really_ unusual use-case and
840 * therefore (complexity / locking) it is not supported.
842 if (msg_head
->nframes
> op
->nframes
)
845 /* update can_frames content */
846 for (i
= 0; i
< msg_head
->nframes
; i
++) {
847 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
848 msg
->msg_iov
, CFSIZ
);
850 if (op
->frames
[i
].can_dlc
> 8)
856 if (msg_head
->flags
& TX_CP_CAN_ID
) {
857 /* copy can_id into frame */
858 op
->frames
[i
].can_id
= msg_head
->can_id
;
863 /* insert new BCM operation for the given can_id */
865 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
869 op
->can_id
= msg_head
->can_id
;
871 /* create array for can_frames and copy the data */
872 if (msg_head
->nframes
> 1) {
873 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
880 op
->frames
= &op
->sframe
;
882 for (i
= 0; i
< msg_head
->nframes
; i
++) {
883 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
884 msg
->msg_iov
, CFSIZ
);
886 if (op
->frames
[i
].can_dlc
> 8)
890 if (op
->frames
!= &op
->sframe
)
896 if (msg_head
->flags
& TX_CP_CAN_ID
) {
897 /* copy can_id into frame */
898 op
->frames
[i
].can_id
= msg_head
->can_id
;
902 /* tx_ops never compare with previous received messages */
903 op
->last_frames
= NULL
;
905 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
907 op
->ifindex
= ifindex
;
909 /* initialize uninitialized (kzalloc) structure */
910 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
911 op
->timer
.function
= bcm_tx_timeout_handler
;
913 /* initialize tasklet for tx countevent notification */
914 tasklet_init(&op
->tsklet
, bcm_tx_timeout_tsklet
,
917 /* currently unused in tx_ops */
918 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
920 /* add this bcm_op to the list of the tx_ops */
921 list_add(&op
->list
, &bo
->tx_ops
);
923 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
925 if (op
->nframes
!= msg_head
->nframes
) {
926 op
->nframes
= msg_head
->nframes
;
927 /* start multiple frame transmission with index 0 */
933 op
->flags
= msg_head
->flags
;
935 if (op
->flags
& TX_RESET_MULTI_IDX
) {
936 /* start multiple frame transmission with index 0 */
940 if (op
->flags
& SETTIMER
) {
941 /* set timer values */
942 op
->count
= msg_head
->count
;
943 op
->ival1
= msg_head
->ival1
;
944 op
->ival2
= msg_head
->ival2
;
945 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
946 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
948 /* disable an active timer due to zero values? */
949 if (!op
->kt_ival1
.tv64
&& !op
->kt_ival2
.tv64
)
950 hrtimer_cancel(&op
->timer
);
953 if ((op
->flags
& STARTTIMER
) &&
954 ((op
->kt_ival1
.tv64
&& op
->count
) || op
->kt_ival2
.tv64
)) {
956 /* spec: send can_frame when starting timer */
957 op
->flags
|= TX_ANNOUNCE
;
959 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
960 /* op->count-- is done in bcm_tx_timeout_handler */
961 hrtimer_start(&op
->timer
, op
->kt_ival1
,
964 hrtimer_start(&op
->timer
, op
->kt_ival2
,
968 if (op
->flags
& TX_ANNOUNCE
)
971 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
975 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
977 static int bcm_rx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
978 int ifindex
, struct sock
*sk
)
980 struct bcm_sock
*bo
= bcm_sk(sk
);
985 if ((msg_head
->flags
& RX_FILTER_ID
) || (!(msg_head
->nframes
))) {
986 /* be robust against wrong usage ... */
987 msg_head
->flags
|= RX_FILTER_ID
;
988 /* ignore trailing garbage */
989 msg_head
->nframes
= 0;
992 if ((msg_head
->flags
& RX_RTR_FRAME
) &&
993 ((msg_head
->nframes
!= 1) ||
994 (!(msg_head
->can_id
& CAN_RTR_FLAG
))))
997 /* check the given can_id */
998 op
= bcm_find_op(&bo
->rx_ops
, msg_head
->can_id
, ifindex
);
1000 /* update existing BCM operation */
1003 * Do we need more space for the can_frames than currently
1004 * allocated? -> This is a _really_ unusual use-case and
1005 * therefore (complexity / locking) it is not supported.
1007 if (msg_head
->nframes
> op
->nframes
)
1010 if (msg_head
->nframes
) {
1011 /* update can_frames content */
1012 err
= memcpy_fromiovec((u8
*)op
->frames
,
1014 msg_head
->nframes
* CFSIZ
);
1018 /* clear last_frames to indicate 'nothing received' */
1019 memset(op
->last_frames
, 0, msg_head
->nframes
* CFSIZ
);
1022 op
->nframes
= msg_head
->nframes
;
1024 /* Only an update -> do not call can_rx_register() */
1028 /* insert new BCM operation for the given can_id */
1029 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
1033 op
->can_id
= msg_head
->can_id
;
1034 op
->nframes
= msg_head
->nframes
;
1036 if (msg_head
->nframes
> 1) {
1037 /* create array for can_frames and copy the data */
1038 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
1045 /* create and init array for received can_frames */
1046 op
->last_frames
= kzalloc(msg_head
->nframes
* CFSIZ
,
1048 if (!op
->last_frames
) {
1055 op
->frames
= &op
->sframe
;
1056 op
->last_frames
= &op
->last_sframe
;
1059 if (msg_head
->nframes
) {
1060 err
= memcpy_fromiovec((u8
*)op
->frames
, msg
->msg_iov
,
1061 msg_head
->nframes
* CFSIZ
);
1063 if (op
->frames
!= &op
->sframe
)
1065 if (op
->last_frames
!= &op
->last_sframe
)
1066 kfree(op
->last_frames
);
1072 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1074 op
->ifindex
= ifindex
;
1076 /* initialize uninitialized (kzalloc) structure */
1077 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1078 op
->timer
.function
= bcm_rx_timeout_handler
;
1080 /* initialize tasklet for rx timeout notification */
1081 tasklet_init(&op
->tsklet
, bcm_rx_timeout_tsklet
,
1082 (unsigned long) op
);
1084 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1085 op
->thrtimer
.function
= bcm_rx_thr_handler
;
1087 /* initialize tasklet for rx throttle handling */
1088 tasklet_init(&op
->thrtsklet
, bcm_rx_thr_tsklet
,
1089 (unsigned long) op
);
1091 /* add this bcm_op to the list of the rx_ops */
1092 list_add(&op
->list
, &bo
->rx_ops
);
1094 /* call can_rx_register() */
1097 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1100 op
->flags
= msg_head
->flags
;
1102 if (op
->flags
& RX_RTR_FRAME
) {
1104 /* no timers in RTR-mode */
1105 hrtimer_cancel(&op
->thrtimer
);
1106 hrtimer_cancel(&op
->timer
);
1109 * funny feature in RX(!)_SETUP only for RTR-mode:
1110 * copy can_id into frame BUT without RTR-flag to
1111 * prevent a full-load-loopback-test ... ;-]
1113 if ((op
->flags
& TX_CP_CAN_ID
) ||
1114 (op
->frames
[0].can_id
== op
->can_id
))
1115 op
->frames
[0].can_id
= op
->can_id
& ~CAN_RTR_FLAG
;
1118 if (op
->flags
& SETTIMER
) {
1120 /* set timer value */
1121 op
->ival1
= msg_head
->ival1
;
1122 op
->ival2
= msg_head
->ival2
;
1123 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
1124 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
1126 /* disable an active timer due to zero value? */
1127 if (!op
->kt_ival1
.tv64
)
1128 hrtimer_cancel(&op
->timer
);
1131 * In any case cancel the throttle timer, flush
1132 * potentially blocked msgs and reset throttle handling
1134 op
->kt_lastmsg
= ktime_set(0, 0);
1135 hrtimer_cancel(&op
->thrtimer
);
1136 bcm_rx_thr_flush(op
, 1);
1139 if ((op
->flags
& STARTTIMER
) && op
->kt_ival1
.tv64
)
1140 hrtimer_start(&op
->timer
, op
->kt_ival1
,
1144 /* now we can register for can_ids, if we added a new bcm_op */
1145 if (do_rx_register
) {
1147 struct net_device
*dev
;
1149 dev
= dev_get_by_index(&init_net
, ifindex
);
1151 err
= can_rx_register(dev
, op
->can_id
,
1152 REGMASK(op
->can_id
),
1156 op
->rx_reg_dev
= dev
;
1161 err
= can_rx_register(NULL
, op
->can_id
,
1162 REGMASK(op
->can_id
),
1163 bcm_rx_handler
, op
, "bcm");
1165 /* this bcm rx op is broken -> remove it */
1166 list_del(&op
->list
);
1172 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
1176 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1178 static int bcm_tx_send(struct msghdr
*msg
, int ifindex
, struct sock
*sk
)
1180 struct sk_buff
*skb
;
1181 struct net_device
*dev
;
1184 /* we need a real device to send frames */
1188 skb
= alloc_skb(CFSIZ
, GFP_KERNEL
);
1193 err
= memcpy_fromiovec(skb_put(skb
, CFSIZ
), msg
->msg_iov
, CFSIZ
);
1199 dev
= dev_get_by_index(&init_net
, ifindex
);
1207 err
= can_send(skb
, 1); /* send with loopback */
1213 return CFSIZ
+ MHSIZ
;
1217 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1219 static int bcm_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1220 struct msghdr
*msg
, size_t size
)
1222 struct sock
*sk
= sock
->sk
;
1223 struct bcm_sock
*bo
= bcm_sk(sk
);
1224 int ifindex
= bo
->ifindex
; /* default ifindex for this bcm_op */
1225 struct bcm_msg_head msg_head
;
1226 int ret
; /* read bytes or error codes as return value */
1231 /* check for valid message length from userspace */
1232 if (size
< MHSIZ
|| (size
- MHSIZ
) % CFSIZ
)
1235 /* check for alternative ifindex for this bcm_op */
1237 if (!ifindex
&& msg
->msg_name
) {
1238 /* no bound device as default => check msg_name */
1239 struct sockaddr_can
*addr
=
1240 (struct sockaddr_can
*)msg
->msg_name
;
1242 if (addr
->can_family
!= AF_CAN
)
1245 /* ifindex from sendto() */
1246 ifindex
= addr
->can_ifindex
;
1249 struct net_device
*dev
;
1251 dev
= dev_get_by_index(&init_net
, ifindex
);
1255 if (dev
->type
!= ARPHRD_CAN
) {
1264 /* read message head information */
1266 ret
= memcpy_fromiovec((u8
*)&msg_head
, msg
->msg_iov
, MHSIZ
);
1272 switch (msg_head
.opcode
) {
1275 ret
= bcm_tx_setup(&msg_head
, msg
, ifindex
, sk
);
1279 ret
= bcm_rx_setup(&msg_head
, msg
, ifindex
, sk
);
1283 if (bcm_delete_tx_op(&bo
->tx_ops
, msg_head
.can_id
, ifindex
))
1290 if (bcm_delete_rx_op(&bo
->rx_ops
, msg_head
.can_id
, ifindex
))
1297 /* reuse msg_head for the reply to TX_READ */
1298 msg_head
.opcode
= TX_STATUS
;
1299 ret
= bcm_read_op(&bo
->tx_ops
, &msg_head
, ifindex
);
1303 /* reuse msg_head for the reply to RX_READ */
1304 msg_head
.opcode
= RX_STATUS
;
1305 ret
= bcm_read_op(&bo
->rx_ops
, &msg_head
, ifindex
);
1309 /* we need exactly one can_frame behind the msg head */
1310 if ((msg_head
.nframes
!= 1) || (size
!= CFSIZ
+ MHSIZ
))
1313 ret
= bcm_tx_send(msg
, ifindex
, sk
);
1327 * notification handler for netdevice status changes
1329 static int bcm_notifier(struct notifier_block
*nb
, unsigned long msg
,
1332 struct net_device
*dev
= (struct net_device
*)data
;
1333 struct bcm_sock
*bo
= container_of(nb
, struct bcm_sock
, notifier
);
1334 struct sock
*sk
= &bo
->sk
;
1336 int notify_enodev
= 0;
1338 if (!net_eq(dev_net(dev
), &init_net
))
1341 if (dev
->type
!= ARPHRD_CAN
)
1346 case NETDEV_UNREGISTER
:
1349 /* remove device specific receive entries */
1350 list_for_each_entry(op
, &bo
->rx_ops
, list
)
1351 if (op
->rx_reg_dev
== dev
)
1352 bcm_rx_unreg(dev
, op
);
1354 /* remove device reference, if this is our bound device */
1355 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1363 if (notify_enodev
) {
1364 sk
->sk_err
= ENODEV
;
1365 if (!sock_flag(sk
, SOCK_DEAD
))
1366 sk
->sk_error_report(sk
);
1371 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1372 sk
->sk_err
= ENETDOWN
;
1373 if (!sock_flag(sk
, SOCK_DEAD
))
1374 sk
->sk_error_report(sk
);
1382 * initial settings for all BCM sockets to be set at socket creation time
1384 static int bcm_init(struct sock
*sk
)
1386 struct bcm_sock
*bo
= bcm_sk(sk
);
1390 bo
->dropped_usr_msgs
= 0;
1391 bo
->bcm_proc_read
= NULL
;
1393 INIT_LIST_HEAD(&bo
->tx_ops
);
1394 INIT_LIST_HEAD(&bo
->rx_ops
);
1397 bo
->notifier
.notifier_call
= bcm_notifier
;
1399 register_netdevice_notifier(&bo
->notifier
);
1405 * standard socket functions
1407 static int bcm_release(struct socket
*sock
)
1409 struct sock
*sk
= sock
->sk
;
1410 struct bcm_sock
*bo
= bcm_sk(sk
);
1411 struct bcm_op
*op
, *next
;
1413 /* remove bcm_ops, timer, rx_unregister(), etc. */
1415 unregister_netdevice_notifier(&bo
->notifier
);
1419 list_for_each_entry_safe(op
, next
, &bo
->tx_ops
, list
)
1422 list_for_each_entry_safe(op
, next
, &bo
->rx_ops
, list
) {
1424 * Don't care if we're bound or not (due to netdev problems)
1425 * can_rx_unregister() is always a save thing to do here.
1429 * Only remove subscriptions that had not
1430 * been removed due to NETDEV_UNREGISTER
1433 if (op
->rx_reg_dev
) {
1434 struct net_device
*dev
;
1436 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
1438 bcm_rx_unreg(dev
, op
);
1443 can_rx_unregister(NULL
, op
->can_id
,
1444 REGMASK(op
->can_id
),
1445 bcm_rx_handler
, op
);
1450 /* remove procfs entry */
1451 if (proc_dir
&& bo
->bcm_proc_read
)
1452 remove_proc_entry(bo
->procname
, proc_dir
);
1454 /* remove device reference */
1469 static int bcm_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int len
,
1472 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
1473 struct sock
*sk
= sock
->sk
;
1474 struct bcm_sock
*bo
= bcm_sk(sk
);
1479 /* bind a device to this socket */
1480 if (addr
->can_ifindex
) {
1481 struct net_device
*dev
;
1483 dev
= dev_get_by_index(&init_net
, addr
->can_ifindex
);
1487 if (dev
->type
!= ARPHRD_CAN
) {
1492 bo
->ifindex
= dev
->ifindex
;
1496 /* no interface reference for ifindex = 0 ('any' CAN device) */
1503 /* unique socket address as filename */
1504 sprintf(bo
->procname
, "%p", sock
);
1505 bo
->bcm_proc_read
= proc_create_data(bo
->procname
, 0644,
1507 &bcm_proc_fops
, sk
);
1513 static int bcm_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1514 struct msghdr
*msg
, size_t size
, int flags
)
1516 struct sock
*sk
= sock
->sk
;
1517 struct sk_buff
*skb
;
1522 noblock
= flags
& MSG_DONTWAIT
;
1523 flags
&= ~MSG_DONTWAIT
;
1524 skb
= skb_recv_datagram(sk
, flags
, noblock
, &error
);
1528 if (skb
->len
< size
)
1531 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
);
1533 skb_free_datagram(sk
, skb
);
1537 sock_recv_ts_and_drops(msg
, sk
, skb
);
1539 if (msg
->msg_name
) {
1540 msg
->msg_namelen
= sizeof(struct sockaddr_can
);
1541 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
1544 skb_free_datagram(sk
, skb
);
1549 static struct proto_ops bcm_ops __read_mostly
= {
1551 .release
= bcm_release
,
1552 .bind
= sock_no_bind
,
1553 .connect
= bcm_connect
,
1554 .socketpair
= sock_no_socketpair
,
1555 .accept
= sock_no_accept
,
1556 .getname
= sock_no_getname
,
1557 .poll
= datagram_poll
,
1558 .ioctl
= NULL
, /* use can_ioctl() from af_can.c */
1559 .listen
= sock_no_listen
,
1560 .shutdown
= sock_no_shutdown
,
1561 .setsockopt
= sock_no_setsockopt
,
1562 .getsockopt
= sock_no_getsockopt
,
1563 .sendmsg
= bcm_sendmsg
,
1564 .recvmsg
= bcm_recvmsg
,
1565 .mmap
= sock_no_mmap
,
1566 .sendpage
= sock_no_sendpage
,
1569 static struct proto bcm_proto __read_mostly
= {
1571 .owner
= THIS_MODULE
,
1572 .obj_size
= sizeof(struct bcm_sock
),
1576 static struct can_proto bcm_can_proto __read_mostly
= {
1578 .protocol
= CAN_BCM
,
1584 static int __init
bcm_module_init(void)
1590 err
= can_proto_register(&bcm_can_proto
);
1592 printk(KERN_ERR
"can: registration of bcm protocol failed\n");
1596 /* create /proc/net/can-bcm directory */
1597 proc_dir
= proc_mkdir("can-bcm", init_net
.proc_net
);
1601 static void __exit
bcm_module_exit(void)
1603 can_proto_unregister(&bcm_can_proto
);
1606 proc_net_remove(&init_net
, "can-bcm");
1609 module_init(bcm_module_init
);
1610 module_exit(bcm_module_exit
);