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/uio.h>
50 #include <linux/net.h>
51 #include <linux/netdevice.h>
52 #include <linux/socket.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <linux/can.h>
56 #include <linux/can/core.h>
57 #include <linux/can/bcm.h>
59 #include <net/net_namespace.h>
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_EFF_FLAG) ? \
68 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
69 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
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 /* easy access to can_frame payload */
80 static inline u64
GET_U64(const struct can_frame
*cp
)
82 return *(u64
*)cp
->data
;
86 struct list_head list
;
90 unsigned long frames_abs
, frames_filtered
;
91 struct timeval ival1
, ival2
;
92 struct hrtimer timer
, thrtimer
;
93 ktime_t rx_stamp
, kt_ival1
, kt_ival2
, kt_lastmsg
;
98 struct can_frame
*frames
;
99 struct can_frame
*last_frames
;
100 struct can_frame sframe
;
101 struct can_frame last_sframe
;
103 struct net_device
*rx_reg_dev
;
106 static struct proc_dir_entry
*proc_dir
;
112 struct notifier_block notifier
;
113 struct list_head rx_ops
;
114 struct list_head tx_ops
;
115 unsigned long dropped_usr_msgs
;
116 struct proc_dir_entry
*bcm_proc_read
;
117 char procname
[9]; /* pointer printed in ASCII with \0 */
120 static inline struct bcm_sock
*bcm_sk(const struct sock
*sk
)
122 return (struct bcm_sock
*)sk
;
125 #define CFSIZ sizeof(struct can_frame)
126 #define OPSIZ sizeof(struct bcm_op)
127 #define MHSIZ sizeof(struct bcm_msg_head)
132 static char *bcm_proc_getifname(int ifindex
)
134 struct net_device
*dev
;
139 /* no usage counting */
140 dev
= __dev_get_by_index(&init_net
, ifindex
);
147 static int bcm_read_proc(char *page
, char **start
, off_t off
,
148 int count
, int *eof
, void *data
)
151 struct sock
*sk
= (struct sock
*)data
;
152 struct bcm_sock
*bo
= bcm_sk(sk
);
155 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, ">>> socket %p",
157 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, " / sk %p", sk
);
158 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, " / bo %p", bo
);
159 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, " / dropped %lu",
160 bo
->dropped_usr_msgs
);
161 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, " / bound %s",
162 bcm_proc_getifname(bo
->ifindex
));
163 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, " <<<\n");
165 list_for_each_entry(op
, &bo
->rx_ops
, list
) {
167 unsigned long reduction
;
169 /* print only active entries & prevent division by zero */
173 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
,
175 op
->can_id
, bcm_proc_getifname(op
->ifindex
));
176 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "[%d]%c ",
178 (op
->flags
& RX_CHECK_DLC
)?'d':' ');
179 if (op
->kt_ival1
.tv64
)
180 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
,
183 ktime_to_us(op
->kt_ival1
));
185 if (op
->kt_ival2
.tv64
)
186 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
,
189 ktime_to_us(op
->kt_ival2
));
191 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
,
192 "# recv %ld (%ld) => reduction: ",
193 op
->frames_filtered
, op
->frames_abs
);
195 reduction
= 100 - (op
->frames_filtered
* 100) / op
->frames_abs
;
197 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "%s%ld%%\n",
198 (reduction
== 100)?"near ":"", reduction
);
200 if (len
> PAGE_SIZE
- 200) {
201 /* mark output cut off */
202 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "(..)\n");
207 list_for_each_entry(op
, &bo
->tx_ops
, list
) {
209 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
,
210 "tx_op: %03X %s [%d] ",
211 op
->can_id
, bcm_proc_getifname(op
->ifindex
),
214 if (op
->kt_ival1
.tv64
)
215 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "t1=%lld ",
216 (long long) ktime_to_us(op
->kt_ival1
));
218 if (op
->kt_ival2
.tv64
)
219 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "t2=%lld ",
220 (long long) ktime_to_us(op
->kt_ival2
));
222 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "# sent %ld\n",
225 if (len
> PAGE_SIZE
- 100) {
226 /* mark output cut off */
227 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "(..)\n");
232 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "\n");
239 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
240 * of the given bcm tx op
242 static void bcm_can_tx(struct bcm_op
*op
)
245 struct net_device
*dev
;
246 struct can_frame
*cf
= &op
->frames
[op
->currframe
];
248 /* no target device? => exit */
252 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
254 /* RFC: should this bcm_op remove itself here? */
258 skb
= alloc_skb(CFSIZ
, gfp_any());
262 memcpy(skb_put(skb
, CFSIZ
), cf
, CFSIZ
);
264 /* send with loopback */
269 /* update statistics */
273 /* reached last frame? */
274 if (op
->currframe
>= op
->nframes
)
281 * bcm_send_to_user - send a BCM message to the userspace
282 * (consisting of bcm_msg_head + x CAN frames)
284 static void bcm_send_to_user(struct bcm_op
*op
, struct bcm_msg_head
*head
,
285 struct can_frame
*frames
, int has_timestamp
)
288 struct can_frame
*firstframe
;
289 struct sockaddr_can
*addr
;
290 struct sock
*sk
= op
->sk
;
291 int datalen
= head
->nframes
* CFSIZ
;
294 skb
= alloc_skb(sizeof(*head
) + datalen
, gfp_any());
298 memcpy(skb_put(skb
, sizeof(*head
)), head
, sizeof(*head
));
301 /* can_frames starting here */
302 firstframe
= (struct can_frame
*)skb_tail_pointer(skb
);
304 memcpy(skb_put(skb
, datalen
), frames
, datalen
);
307 * the BCM uses the can_dlc-element of the can_frame
308 * structure for internal purposes. This is only
309 * relevant for updates that are generated by the
310 * BCM, where nframes is 1
312 if (head
->nframes
== 1)
313 firstframe
->can_dlc
&= BCM_CAN_DLC_MASK
;
317 /* restore rx timestamp */
318 skb
->tstamp
= op
->rx_stamp
;
322 * Put the datagram to the queue so that bcm_recvmsg() can
323 * get it from there. We need to pass the interface index to
324 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
325 * containing the interface index.
328 BUILD_BUG_ON(sizeof(skb
->cb
) < sizeof(struct sockaddr_can
));
329 addr
= (struct sockaddr_can
*)skb
->cb
;
330 memset(addr
, 0, sizeof(*addr
));
331 addr
->can_family
= AF_CAN
;
332 addr
->can_ifindex
= op
->rx_ifindex
;
334 err
= sock_queue_rcv_skb(sk
, skb
);
336 struct bcm_sock
*bo
= bcm_sk(sk
);
339 /* don't care about overflows in this statistic */
340 bo
->dropped_usr_msgs
++;
345 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
347 static enum hrtimer_restart
bcm_tx_timeout_handler(struct hrtimer
*hrtimer
)
349 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
350 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
352 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
355 if (!op
->count
&& (op
->flags
& TX_COUNTEVT
)) {
356 struct bcm_msg_head msg_head
;
358 /* create notification to user */
359 msg_head
.opcode
= TX_EXPIRED
;
360 msg_head
.flags
= op
->flags
;
361 msg_head
.count
= op
->count
;
362 msg_head
.ival1
= op
->ival1
;
363 msg_head
.ival2
= op
->ival2
;
364 msg_head
.can_id
= op
->can_id
;
365 msg_head
.nframes
= 0;
367 bcm_send_to_user(op
, &msg_head
, NULL
, 0);
371 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
373 /* send (next) frame */
375 hrtimer_forward(hrtimer
, ktime_get(), op
->kt_ival1
);
376 ret
= HRTIMER_RESTART
;
379 if (op
->kt_ival2
.tv64
) {
381 /* send (next) frame */
383 hrtimer_forward(hrtimer
, ktime_get(), op
->kt_ival2
);
384 ret
= HRTIMER_RESTART
;
392 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
394 static void bcm_rx_changed(struct bcm_op
*op
, struct can_frame
*data
)
396 struct bcm_msg_head head
;
398 /* update statistics */
399 op
->frames_filtered
++;
401 /* prevent statistics overflow */
402 if (op
->frames_filtered
> ULONG_MAX
/100)
403 op
->frames_filtered
= op
->frames_abs
= 0;
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 struct can_frame
*rxdata
)
425 memcpy(lastdata
, rxdata
, CFSIZ
);
428 lastdata
->can_dlc
|= RX_RECV
;
430 /* throtteling mode inactive OR data update already on the run ? */
431 if (!op
->kt_ival2
.tv64
|| hrtimer_callback_running(&op
->thrtimer
)) {
432 /* send RX_CHANGED to the user immediately */
433 bcm_rx_changed(op
, rxdata
);
437 if (hrtimer_active(&op
->thrtimer
)) {
438 /* mark as 'throttled' */
439 lastdata
->can_dlc
|= RX_THR
;
443 if (!op
->kt_lastmsg
.tv64
) {
444 /* send first RX_CHANGED to the user immediately */
445 bcm_rx_changed(op
, rxdata
);
446 op
->kt_lastmsg
= ktime_get();
450 if (ktime_us_delta(ktime_get(), op
->kt_lastmsg
) <
451 ktime_to_us(op
->kt_ival2
)) {
452 /* mark as 'throttled' and start timer */
453 lastdata
->can_dlc
|= RX_THR
;
454 hrtimer_start(&op
->thrtimer
,
455 ktime_add(op
->kt_lastmsg
, op
->kt_ival2
),
460 /* the gap was that big, that throttling was not needed here */
461 bcm_rx_changed(op
, rxdata
);
462 op
->kt_lastmsg
= ktime_get();
466 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
467 * received data stored in op->last_frames[]
469 static void bcm_rx_cmp_to_index(struct bcm_op
*op
, int index
,
470 struct can_frame
*rxdata
)
473 * no one uses the MSBs of can_dlc for comparation,
474 * so we use it here to detect the first time of reception
477 if (!(op
->last_frames
[index
].can_dlc
& RX_RECV
)) {
478 /* received data for the first time => send update to user */
479 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
483 /* do a real check in can_frame data section */
485 if ((GET_U64(&op
->frames
[index
]) & GET_U64(rxdata
)) !=
486 (GET_U64(&op
->frames
[index
]) & GET_U64(&op
->last_frames
[index
]))) {
487 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
491 if (op
->flags
& RX_CHECK_DLC
) {
492 /* do a real check in can_frame dlc */
493 if (rxdata
->can_dlc
!= (op
->last_frames
[index
].can_dlc
&
495 bcm_rx_update_and_send(op
, &op
->last_frames
[index
],
503 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
505 static void bcm_rx_starttimer(struct bcm_op
*op
)
507 if (op
->flags
& RX_NO_AUTOTIMER
)
510 if (op
->kt_ival1
.tv64
)
511 hrtimer_start(&op
->timer
, op
->kt_ival1
, HRTIMER_MODE_REL
);
515 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
517 static enum hrtimer_restart
bcm_rx_timeout_handler(struct hrtimer
*hrtimer
)
519 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
520 struct bcm_msg_head msg_head
;
522 msg_head
.opcode
= RX_TIMEOUT
;
523 msg_head
.flags
= op
->flags
;
524 msg_head
.count
= op
->count
;
525 msg_head
.ival1
= op
->ival1
;
526 msg_head
.ival2
= op
->ival2
;
527 msg_head
.can_id
= op
->can_id
;
528 msg_head
.nframes
= 0;
530 bcm_send_to_user(op
, &msg_head
, NULL
, 0);
532 /* no restart of the timer is done here! */
534 /* if user wants to be informed, when cyclic CAN-Messages come back */
535 if ((op
->flags
& RX_ANNOUNCE_RESUME
) && op
->last_frames
) {
536 /* clear received can_frames to indicate 'nothing received' */
537 memset(op
->last_frames
, 0, op
->nframes
* CFSIZ
);
540 return HRTIMER_NORESTART
;
544 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
546 static int bcm_rx_thr_flush(struct bcm_op
*op
)
550 if (op
->nframes
> 1) {
553 /* for MUX filter we start at index 1 */
554 for (i
= 1; i
< op
->nframes
; i
++) {
555 if ((op
->last_frames
) &&
556 (op
->last_frames
[i
].can_dlc
& RX_THR
)) {
557 op
->last_frames
[i
].can_dlc
&= ~RX_THR
;
558 bcm_rx_changed(op
, &op
->last_frames
[i
]);
564 /* for RX_FILTER_ID and simple filter */
565 if (op
->last_frames
&& (op
->last_frames
[0].can_dlc
& RX_THR
)) {
566 op
->last_frames
[0].can_dlc
&= ~RX_THR
;
567 bcm_rx_changed(op
, &op
->last_frames
[0]);
576 * bcm_rx_thr_handler - the time for blocked content updates is over now:
577 * Check for throttled data and send it to the userspace
579 static enum hrtimer_restart
bcm_rx_thr_handler(struct hrtimer
*hrtimer
)
581 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, thrtimer
);
583 if (bcm_rx_thr_flush(op
)) {
584 hrtimer_forward(hrtimer
, ktime_get(), op
->kt_ival2
);
585 return HRTIMER_RESTART
;
587 /* rearm throttle handling */
588 op
->kt_lastmsg
= ktime_set(0, 0);
589 return HRTIMER_NORESTART
;
594 * bcm_rx_handler - handle a CAN frame receiption
596 static void bcm_rx_handler(struct sk_buff
*skb
, void *data
)
598 struct bcm_op
*op
= (struct bcm_op
*)data
;
599 struct can_frame rxframe
;
602 /* disable timeout */
603 hrtimer_cancel(&op
->timer
);
605 if (skb
->len
== sizeof(rxframe
)) {
606 memcpy(&rxframe
, skb
->data
, sizeof(rxframe
));
607 /* save rx timestamp */
608 op
->rx_stamp
= skb
->tstamp
;
609 /* save originator for recvfrom() */
610 op
->rx_ifindex
= skb
->dev
->ifindex
;
611 /* update statistics */
620 if (op
->can_id
!= rxframe
.can_id
)
623 if (op
->flags
& RX_RTR_FRAME
) {
624 /* send reply for RTR-request (placed in op->frames[0]) */
629 if (op
->flags
& RX_FILTER_ID
) {
630 /* the easiest case */
631 bcm_rx_update_and_send(op
, &op
->last_frames
[0], &rxframe
);
632 bcm_rx_starttimer(op
);
636 if (op
->nframes
== 1) {
637 /* simple compare with index 0 */
638 bcm_rx_cmp_to_index(op
, 0, &rxframe
);
639 bcm_rx_starttimer(op
);
643 if (op
->nframes
> 1) {
647 * find the first multiplex mask that fits.
648 * Remark: The MUX-mask is stored in index 0
651 for (i
= 1; i
< op
->nframes
; i
++) {
652 if ((GET_U64(&op
->frames
[0]) & GET_U64(&rxframe
)) ==
653 (GET_U64(&op
->frames
[0]) &
654 GET_U64(&op
->frames
[i
]))) {
655 bcm_rx_cmp_to_index(op
, i
, &rxframe
);
659 bcm_rx_starttimer(op
);
664 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
666 static struct bcm_op
*bcm_find_op(struct list_head
*ops
, canid_t can_id
,
671 list_for_each_entry(op
, ops
, list
) {
672 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
))
679 static void bcm_remove_op(struct bcm_op
*op
)
681 hrtimer_cancel(&op
->timer
);
682 hrtimer_cancel(&op
->thrtimer
);
684 if ((op
->frames
) && (op
->frames
!= &op
->sframe
))
687 if ((op
->last_frames
) && (op
->last_frames
!= &op
->last_sframe
))
688 kfree(op
->last_frames
);
695 static void bcm_rx_unreg(struct net_device
*dev
, struct bcm_op
*op
)
697 if (op
->rx_reg_dev
== dev
) {
698 can_rx_unregister(dev
, op
->can_id
, REGMASK(op
->can_id
),
701 /* mark as removed subscription */
702 op
->rx_reg_dev
= NULL
;
704 printk(KERN_ERR
"can-bcm: bcm_rx_unreg: registered device "
705 "mismatch %p %p\n", op
->rx_reg_dev
, dev
);
709 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
711 static int bcm_delete_rx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
713 struct bcm_op
*op
, *n
;
715 list_for_each_entry_safe(op
, n
, ops
, list
) {
716 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
719 * Don't care if we're bound or not (due to netdev
720 * problems) can_rx_unregister() is always a save
725 * Only remove subscriptions that had not
726 * been removed due to NETDEV_UNREGISTER
729 if (op
->rx_reg_dev
) {
730 struct net_device
*dev
;
732 dev
= dev_get_by_index(&init_net
,
735 bcm_rx_unreg(dev
, op
);
740 can_rx_unregister(NULL
, op
->can_id
,
750 return 0; /* not found */
754 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
756 static int bcm_delete_tx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
758 struct bcm_op
*op
, *n
;
760 list_for_each_entry_safe(op
, n
, ops
, list
) {
761 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
768 return 0; /* not found */
772 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
774 static int bcm_read_op(struct list_head
*ops
, struct bcm_msg_head
*msg_head
,
777 struct bcm_op
*op
= bcm_find_op(ops
, msg_head
->can_id
, ifindex
);
782 /* put current values into msg_head */
783 msg_head
->flags
= op
->flags
;
784 msg_head
->count
= op
->count
;
785 msg_head
->ival1
= op
->ival1
;
786 msg_head
->ival2
= op
->ival2
;
787 msg_head
->nframes
= op
->nframes
;
789 bcm_send_to_user(op
, msg_head
, op
->frames
, 0);
795 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
797 static int bcm_tx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
798 int ifindex
, struct sock
*sk
)
800 struct bcm_sock
*bo
= bcm_sk(sk
);
804 /* we need a real device to send frames */
808 /* we need at least one can_frame */
809 if (msg_head
->nframes
< 1)
812 /* check the given can_id */
813 op
= bcm_find_op(&bo
->tx_ops
, msg_head
->can_id
, ifindex
);
816 /* update existing BCM operation */
819 * Do we need more space for the can_frames than currently
820 * allocated? -> This is a _really_ unusual use-case and
821 * therefore (complexity / locking) it is not supported.
823 if (msg_head
->nframes
> op
->nframes
)
826 /* update can_frames content */
827 for (i
= 0; i
< msg_head
->nframes
; i
++) {
828 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
829 msg
->msg_iov
, CFSIZ
);
831 if (op
->frames
[i
].can_dlc
> 8)
837 if (msg_head
->flags
& TX_CP_CAN_ID
) {
838 /* copy can_id into frame */
839 op
->frames
[i
].can_id
= msg_head
->can_id
;
844 /* insert new BCM operation for the given can_id */
846 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
850 op
->can_id
= msg_head
->can_id
;
852 /* create array for can_frames and copy the data */
853 if (msg_head
->nframes
> 1) {
854 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
861 op
->frames
= &op
->sframe
;
863 for (i
= 0; i
< msg_head
->nframes
; i
++) {
864 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
865 msg
->msg_iov
, CFSIZ
);
867 if (op
->frames
[i
].can_dlc
> 8)
871 if (op
->frames
!= &op
->sframe
)
877 if (msg_head
->flags
& TX_CP_CAN_ID
) {
878 /* copy can_id into frame */
879 op
->frames
[i
].can_id
= msg_head
->can_id
;
883 /* tx_ops never compare with previous received messages */
884 op
->last_frames
= NULL
;
886 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
888 op
->ifindex
= ifindex
;
890 /* initialize uninitialized (kzalloc) structure */
891 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
892 op
->timer
.function
= bcm_tx_timeout_handler
;
894 /* currently unused in tx_ops */
895 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
897 /* add this bcm_op to the list of the tx_ops */
898 list_add(&op
->list
, &bo
->tx_ops
);
900 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
902 if (op
->nframes
!= msg_head
->nframes
) {
903 op
->nframes
= msg_head
->nframes
;
904 /* start multiple frame transmission with index 0 */
910 op
->flags
= msg_head
->flags
;
912 if (op
->flags
& TX_RESET_MULTI_IDX
) {
913 /* start multiple frame transmission with index 0 */
917 if (op
->flags
& SETTIMER
) {
918 /* set timer values */
919 op
->count
= msg_head
->count
;
920 op
->ival1
= msg_head
->ival1
;
921 op
->ival2
= msg_head
->ival2
;
922 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
923 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
925 /* disable an active timer due to zero values? */
926 if (!op
->kt_ival1
.tv64
&& !op
->kt_ival2
.tv64
)
927 hrtimer_cancel(&op
->timer
);
930 if ((op
->flags
& STARTTIMER
) &&
931 ((op
->kt_ival1
.tv64
&& op
->count
) || op
->kt_ival2
.tv64
)) {
933 /* spec: send can_frame when starting timer */
934 op
->flags
|= TX_ANNOUNCE
;
936 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
937 /* op->count-- is done in bcm_tx_timeout_handler */
938 hrtimer_start(&op
->timer
, op
->kt_ival1
,
941 hrtimer_start(&op
->timer
, op
->kt_ival2
,
945 if (op
->flags
& TX_ANNOUNCE
)
948 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
952 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
954 static int bcm_rx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
955 int ifindex
, struct sock
*sk
)
957 struct bcm_sock
*bo
= bcm_sk(sk
);
962 if ((msg_head
->flags
& RX_FILTER_ID
) || (!(msg_head
->nframes
))) {
963 /* be robust against wrong usage ... */
964 msg_head
->flags
|= RX_FILTER_ID
;
965 /* ignore trailing garbage */
966 msg_head
->nframes
= 0;
969 if ((msg_head
->flags
& RX_RTR_FRAME
) &&
970 ((msg_head
->nframes
!= 1) ||
971 (!(msg_head
->can_id
& CAN_RTR_FLAG
))))
974 /* check the given can_id */
975 op
= bcm_find_op(&bo
->rx_ops
, msg_head
->can_id
, ifindex
);
977 /* update existing BCM operation */
980 * Do we need more space for the can_frames than currently
981 * allocated? -> This is a _really_ unusual use-case and
982 * therefore (complexity / locking) it is not supported.
984 if (msg_head
->nframes
> op
->nframes
)
987 if (msg_head
->nframes
) {
988 /* update can_frames content */
989 err
= memcpy_fromiovec((u8
*)op
->frames
,
991 msg_head
->nframes
* CFSIZ
);
995 /* clear last_frames to indicate 'nothing received' */
996 memset(op
->last_frames
, 0, msg_head
->nframes
* CFSIZ
);
999 op
->nframes
= msg_head
->nframes
;
1001 /* Only an update -> do not call can_rx_register() */
1005 /* insert new BCM operation for the given can_id */
1006 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
1010 op
->can_id
= msg_head
->can_id
;
1011 op
->nframes
= msg_head
->nframes
;
1013 if (msg_head
->nframes
> 1) {
1014 /* create array for can_frames and copy the data */
1015 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
1022 /* create and init array for received can_frames */
1023 op
->last_frames
= kzalloc(msg_head
->nframes
* CFSIZ
,
1025 if (!op
->last_frames
) {
1032 op
->frames
= &op
->sframe
;
1033 op
->last_frames
= &op
->last_sframe
;
1036 if (msg_head
->nframes
) {
1037 err
= memcpy_fromiovec((u8
*)op
->frames
, msg
->msg_iov
,
1038 msg_head
->nframes
* CFSIZ
);
1040 if (op
->frames
!= &op
->sframe
)
1042 if (op
->last_frames
!= &op
->last_sframe
)
1043 kfree(op
->last_frames
);
1049 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1051 op
->ifindex
= ifindex
;
1053 /* initialize uninitialized (kzalloc) structure */
1054 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1055 op
->timer
.function
= bcm_rx_timeout_handler
;
1057 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1058 op
->thrtimer
.function
= bcm_rx_thr_handler
;
1060 /* add this bcm_op to the list of the rx_ops */
1061 list_add(&op
->list
, &bo
->rx_ops
);
1063 /* call can_rx_register() */
1066 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1069 op
->flags
= msg_head
->flags
;
1071 if (op
->flags
& RX_RTR_FRAME
) {
1073 /* no timers in RTR-mode */
1074 hrtimer_cancel(&op
->thrtimer
);
1075 hrtimer_cancel(&op
->timer
);
1078 * funny feature in RX(!)_SETUP only for RTR-mode:
1079 * copy can_id into frame BUT without RTR-flag to
1080 * prevent a full-load-loopback-test ... ;-]
1082 if ((op
->flags
& TX_CP_CAN_ID
) ||
1083 (op
->frames
[0].can_id
== op
->can_id
))
1084 op
->frames
[0].can_id
= op
->can_id
& ~CAN_RTR_FLAG
;
1087 if (op
->flags
& SETTIMER
) {
1089 /* set timer value */
1090 op
->ival1
= msg_head
->ival1
;
1091 op
->ival2
= msg_head
->ival2
;
1092 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
1093 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
1095 /* disable an active timer due to zero value? */
1096 if (!op
->kt_ival1
.tv64
)
1097 hrtimer_cancel(&op
->timer
);
1100 * In any case cancel the throttle timer, flush
1101 * potentially blocked msgs and reset throttle handling
1103 op
->kt_lastmsg
= ktime_set(0, 0);
1104 hrtimer_cancel(&op
->thrtimer
);
1105 bcm_rx_thr_flush(op
);
1108 if ((op
->flags
& STARTTIMER
) && op
->kt_ival1
.tv64
)
1109 hrtimer_start(&op
->timer
, op
->kt_ival1
,
1113 /* now we can register for can_ids, if we added a new bcm_op */
1114 if (do_rx_register
) {
1116 struct net_device
*dev
;
1118 dev
= dev_get_by_index(&init_net
, ifindex
);
1120 err
= can_rx_register(dev
, op
->can_id
,
1121 REGMASK(op
->can_id
),
1125 op
->rx_reg_dev
= dev
;
1130 err
= can_rx_register(NULL
, op
->can_id
,
1131 REGMASK(op
->can_id
),
1132 bcm_rx_handler
, op
, "bcm");
1134 /* this bcm rx op is broken -> remove it */
1135 list_del(&op
->list
);
1141 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
1145 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1147 static int bcm_tx_send(struct msghdr
*msg
, int ifindex
, struct sock
*sk
)
1149 struct sk_buff
*skb
;
1150 struct net_device
*dev
;
1153 /* we need a real device to send frames */
1157 skb
= alloc_skb(CFSIZ
, GFP_KERNEL
);
1162 err
= memcpy_fromiovec(skb_put(skb
, CFSIZ
), msg
->msg_iov
, CFSIZ
);
1168 dev
= dev_get_by_index(&init_net
, ifindex
);
1176 err
= can_send(skb
, 1); /* send with loopback */
1182 return CFSIZ
+ MHSIZ
;
1186 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1188 static int bcm_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1189 struct msghdr
*msg
, size_t size
)
1191 struct sock
*sk
= sock
->sk
;
1192 struct bcm_sock
*bo
= bcm_sk(sk
);
1193 int ifindex
= bo
->ifindex
; /* default ifindex for this bcm_op */
1194 struct bcm_msg_head msg_head
;
1195 int ret
; /* read bytes or error codes as return value */
1200 /* check for valid message length from userspace */
1201 if (size
< MHSIZ
|| (size
- MHSIZ
) % CFSIZ
)
1204 /* check for alternative ifindex for this bcm_op */
1206 if (!ifindex
&& msg
->msg_name
) {
1207 /* no bound device as default => check msg_name */
1208 struct sockaddr_can
*addr
=
1209 (struct sockaddr_can
*)msg
->msg_name
;
1211 if (addr
->can_family
!= AF_CAN
)
1214 /* ifindex from sendto() */
1215 ifindex
= addr
->can_ifindex
;
1218 struct net_device
*dev
;
1220 dev
= dev_get_by_index(&init_net
, ifindex
);
1224 if (dev
->type
!= ARPHRD_CAN
) {
1233 /* read message head information */
1235 ret
= memcpy_fromiovec((u8
*)&msg_head
, msg
->msg_iov
, MHSIZ
);
1241 switch (msg_head
.opcode
) {
1244 ret
= bcm_tx_setup(&msg_head
, msg
, ifindex
, sk
);
1248 ret
= bcm_rx_setup(&msg_head
, msg
, ifindex
, sk
);
1252 if (bcm_delete_tx_op(&bo
->tx_ops
, msg_head
.can_id
, ifindex
))
1259 if (bcm_delete_rx_op(&bo
->rx_ops
, msg_head
.can_id
, ifindex
))
1266 /* reuse msg_head for the reply to TX_READ */
1267 msg_head
.opcode
= TX_STATUS
;
1268 ret
= bcm_read_op(&bo
->tx_ops
, &msg_head
, ifindex
);
1272 /* reuse msg_head for the reply to RX_READ */
1273 msg_head
.opcode
= RX_STATUS
;
1274 ret
= bcm_read_op(&bo
->rx_ops
, &msg_head
, ifindex
);
1278 /* we need exactly one can_frame behind the msg head */
1279 if ((msg_head
.nframes
!= 1) || (size
!= CFSIZ
+ MHSIZ
))
1282 ret
= bcm_tx_send(msg
, ifindex
, sk
);
1296 * notification handler for netdevice status changes
1298 static int bcm_notifier(struct notifier_block
*nb
, unsigned long msg
,
1301 struct net_device
*dev
= (struct net_device
*)data
;
1302 struct bcm_sock
*bo
= container_of(nb
, struct bcm_sock
, notifier
);
1303 struct sock
*sk
= &bo
->sk
;
1305 int notify_enodev
= 0;
1307 if (!net_eq(dev_net(dev
), &init_net
))
1310 if (dev
->type
!= ARPHRD_CAN
)
1315 case NETDEV_UNREGISTER
:
1318 /* remove device specific receive entries */
1319 list_for_each_entry(op
, &bo
->rx_ops
, list
)
1320 if (op
->rx_reg_dev
== dev
)
1321 bcm_rx_unreg(dev
, op
);
1323 /* remove device reference, if this is our bound device */
1324 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1332 if (notify_enodev
) {
1333 sk
->sk_err
= ENODEV
;
1334 if (!sock_flag(sk
, SOCK_DEAD
))
1335 sk
->sk_error_report(sk
);
1340 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1341 sk
->sk_err
= ENETDOWN
;
1342 if (!sock_flag(sk
, SOCK_DEAD
))
1343 sk
->sk_error_report(sk
);
1351 * initial settings for all BCM sockets to be set at socket creation time
1353 static int bcm_init(struct sock
*sk
)
1355 struct bcm_sock
*bo
= bcm_sk(sk
);
1359 bo
->dropped_usr_msgs
= 0;
1360 bo
->bcm_proc_read
= NULL
;
1362 INIT_LIST_HEAD(&bo
->tx_ops
);
1363 INIT_LIST_HEAD(&bo
->rx_ops
);
1366 bo
->notifier
.notifier_call
= bcm_notifier
;
1368 register_netdevice_notifier(&bo
->notifier
);
1374 * standard socket functions
1376 static int bcm_release(struct socket
*sock
)
1378 struct sock
*sk
= sock
->sk
;
1379 struct bcm_sock
*bo
= bcm_sk(sk
);
1380 struct bcm_op
*op
, *next
;
1382 /* remove bcm_ops, timer, rx_unregister(), etc. */
1384 unregister_netdevice_notifier(&bo
->notifier
);
1388 list_for_each_entry_safe(op
, next
, &bo
->tx_ops
, list
)
1391 list_for_each_entry_safe(op
, next
, &bo
->rx_ops
, list
) {
1393 * Don't care if we're bound or not (due to netdev problems)
1394 * can_rx_unregister() is always a save thing to do here.
1398 * Only remove subscriptions that had not
1399 * been removed due to NETDEV_UNREGISTER
1402 if (op
->rx_reg_dev
) {
1403 struct net_device
*dev
;
1405 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
1407 bcm_rx_unreg(dev
, op
);
1412 can_rx_unregister(NULL
, op
->can_id
,
1413 REGMASK(op
->can_id
),
1414 bcm_rx_handler
, op
);
1419 /* remove procfs entry */
1420 if (proc_dir
&& bo
->bcm_proc_read
)
1421 remove_proc_entry(bo
->procname
, proc_dir
);
1423 /* remove device reference */
1435 static int bcm_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int len
,
1438 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
1439 struct sock
*sk
= sock
->sk
;
1440 struct bcm_sock
*bo
= bcm_sk(sk
);
1445 /* bind a device to this socket */
1446 if (addr
->can_ifindex
) {
1447 struct net_device
*dev
;
1449 dev
= dev_get_by_index(&init_net
, addr
->can_ifindex
);
1453 if (dev
->type
!= ARPHRD_CAN
) {
1458 bo
->ifindex
= dev
->ifindex
;
1462 /* no interface reference for ifindex = 0 ('any' CAN device) */
1469 /* unique socket address as filename */
1470 sprintf(bo
->procname
, "%p", sock
);
1471 bo
->bcm_proc_read
= create_proc_read_entry(bo
->procname
, 0644,
1479 static int bcm_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1480 struct msghdr
*msg
, size_t size
, int flags
)
1482 struct sock
*sk
= sock
->sk
;
1483 struct sk_buff
*skb
;
1488 noblock
= flags
& MSG_DONTWAIT
;
1489 flags
&= ~MSG_DONTWAIT
;
1490 skb
= skb_recv_datagram(sk
, flags
, noblock
, &error
);
1494 if (skb
->len
< size
)
1497 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
);
1499 skb_free_datagram(sk
, skb
);
1503 sock_recv_timestamp(msg
, sk
, skb
);
1505 if (msg
->msg_name
) {
1506 msg
->msg_namelen
= sizeof(struct sockaddr_can
);
1507 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
1510 skb_free_datagram(sk
, skb
);
1515 static struct proto_ops bcm_ops __read_mostly
= {
1517 .release
= bcm_release
,
1518 .bind
= sock_no_bind
,
1519 .connect
= bcm_connect
,
1520 .socketpair
= sock_no_socketpair
,
1521 .accept
= sock_no_accept
,
1522 .getname
= sock_no_getname
,
1523 .poll
= datagram_poll
,
1524 .ioctl
= NULL
, /* use can_ioctl() from af_can.c */
1525 .listen
= sock_no_listen
,
1526 .shutdown
= sock_no_shutdown
,
1527 .setsockopt
= sock_no_setsockopt
,
1528 .getsockopt
= sock_no_getsockopt
,
1529 .sendmsg
= bcm_sendmsg
,
1530 .recvmsg
= bcm_recvmsg
,
1531 .mmap
= sock_no_mmap
,
1532 .sendpage
= sock_no_sendpage
,
1535 static struct proto bcm_proto __read_mostly
= {
1537 .owner
= THIS_MODULE
,
1538 .obj_size
= sizeof(struct bcm_sock
),
1542 static struct can_proto bcm_can_proto __read_mostly
= {
1544 .protocol
= CAN_BCM
,
1550 static int __init
bcm_module_init(void)
1556 err
= can_proto_register(&bcm_can_proto
);
1558 printk(KERN_ERR
"can: registration of bcm protocol failed\n");
1562 /* create /proc/net/can-bcm directory */
1563 proc_dir
= proc_mkdir("can-bcm", init_net
.proc_net
);
1566 proc_dir
->owner
= THIS_MODULE
;
1571 static void __exit
bcm_module_exit(void)
1573 can_proto_unregister(&bcm_can_proto
);
1576 proc_net_remove(&init_net
, "can-bcm");
1579 module_init(bcm_module_init
);
1580 module_exit(bcm_module_exit
);