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>
59 #include <linux/slab.h>
61 #include <net/net_namespace.h>
63 /* use of last_frames[index].can_dlc */
64 #define RX_RECV 0x40 /* received data for this element */
65 #define RX_THR 0x80 /* element not been sent due to throttle feature */
66 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
68 /* get best masking value for can_rx_register() for a given single can_id */
69 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
70 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
71 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
73 #define CAN_BCM_VERSION CAN_VERSION
74 static __initdata
const char banner
[] = KERN_INFO
75 "can: broadcast manager protocol (rev " CAN_BCM_VERSION
" t)\n";
77 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
78 MODULE_LICENSE("Dual BSD/GPL");
79 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
80 MODULE_ALIAS("can-proto-2");
82 /* easy access to can_frame payload */
83 static inline u64
GET_U64(const struct can_frame
*cp
)
85 return *(u64
*)cp
->data
;
89 struct list_head list
;
93 unsigned long frames_abs
, frames_filtered
;
94 struct timeval ival1
, ival2
;
95 struct hrtimer timer
, thrtimer
;
96 struct tasklet_struct tsklet
, thrtsklet
;
97 ktime_t rx_stamp
, kt_ival1
, kt_ival2
, kt_lastmsg
;
102 struct can_frame
*frames
;
103 struct can_frame
*last_frames
;
104 struct can_frame sframe
;
105 struct can_frame last_sframe
;
107 struct net_device
*rx_reg_dev
;
110 static struct proc_dir_entry
*proc_dir
;
116 struct notifier_block notifier
;
117 struct list_head rx_ops
;
118 struct list_head tx_ops
;
119 unsigned long dropped_usr_msgs
;
120 struct proc_dir_entry
*bcm_proc_read
;
121 char procname
[9]; /* pointer printed in ASCII with \0 */
124 static inline struct bcm_sock
*bcm_sk(const struct sock
*sk
)
126 return (struct bcm_sock
*)sk
;
129 #define CFSIZ sizeof(struct can_frame)
130 #define OPSIZ sizeof(struct bcm_op)
131 #define MHSIZ sizeof(struct bcm_msg_head)
136 static char *bcm_proc_getifname(char *result
, int ifindex
)
138 struct net_device
*dev
;
144 dev
= dev_get_by_index_rcu(&init_net
, ifindex
);
146 strcpy(result
, dev
->name
);
148 strcpy(result
, "???");
154 static int bcm_proc_show(struct seq_file
*m
, void *v
)
156 char ifname
[IFNAMSIZ
];
157 struct sock
*sk
= (struct sock
*)m
->private;
158 struct bcm_sock
*bo
= bcm_sk(sk
);
161 seq_printf(m
, ">>> socket %p", sk
->sk_socket
);
162 seq_printf(m
, " / sk %p", sk
);
163 seq_printf(m
, " / bo %p", bo
);
164 seq_printf(m
, " / dropped %lu", bo
->dropped_usr_msgs
);
165 seq_printf(m
, " / bound %s", bcm_proc_getifname(ifname
, bo
->ifindex
));
166 seq_printf(m
, " <<<\n");
168 list_for_each_entry(op
, &bo
->rx_ops
, list
) {
170 unsigned long reduction
;
172 /* print only active entries & prevent division by zero */
176 seq_printf(m
, "rx_op: %03X %-5s ",
177 op
->can_id
, bcm_proc_getifname(ifname
, op
->ifindex
));
178 seq_printf(m
, "[%d]%c ", op
->nframes
,
179 (op
->flags
& RX_CHECK_DLC
)?'d':' ');
180 if (op
->kt_ival1
.tv64
)
181 seq_printf(m
, "timeo=%lld ",
183 ktime_to_us(op
->kt_ival1
));
185 if (op
->kt_ival2
.tv64
)
186 seq_printf(m
, "thr=%lld ",
188 ktime_to_us(op
->kt_ival2
));
190 seq_printf(m
, "# recv %ld (%ld) => reduction: ",
191 op
->frames_filtered
, op
->frames_abs
);
193 reduction
= 100 - (op
->frames_filtered
* 100) / op
->frames_abs
;
195 seq_printf(m
, "%s%ld%%\n",
196 (reduction
== 100)?"near ":"", reduction
);
199 list_for_each_entry(op
, &bo
->tx_ops
, list
) {
201 seq_printf(m
, "tx_op: %03X %s [%d] ",
203 bcm_proc_getifname(ifname
, op
->ifindex
),
206 if (op
->kt_ival1
.tv64
)
207 seq_printf(m
, "t1=%lld ",
208 (long long) ktime_to_us(op
->kt_ival1
));
210 if (op
->kt_ival2
.tv64
)
211 seq_printf(m
, "t2=%lld ",
212 (long long) ktime_to_us(op
->kt_ival2
));
214 seq_printf(m
, "# sent %ld\n", op
->frames_abs
);
220 static int bcm_proc_open(struct inode
*inode
, struct file
*file
)
222 return single_open(file
, bcm_proc_show
, PDE(inode
)->data
);
225 static const struct file_operations bcm_proc_fops
= {
226 .owner
= THIS_MODULE
,
227 .open
= bcm_proc_open
,
230 .release
= single_release
,
234 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
235 * of the given bcm tx op
237 static void bcm_can_tx(struct bcm_op
*op
)
240 struct net_device
*dev
;
241 struct can_frame
*cf
= &op
->frames
[op
->currframe
];
243 /* no target device? => exit */
247 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
249 /* RFC: should this bcm_op remove itself here? */
253 skb
= alloc_skb(CFSIZ
, gfp_any());
257 memcpy(skb_put(skb
, CFSIZ
), cf
, CFSIZ
);
259 /* send with loopback */
264 /* update statistics */
268 /* reached last frame? */
269 if (op
->currframe
>= op
->nframes
)
276 * bcm_send_to_user - send a BCM message to the userspace
277 * (consisting of bcm_msg_head + x CAN frames)
279 static void bcm_send_to_user(struct bcm_op
*op
, struct bcm_msg_head
*head
,
280 struct can_frame
*frames
, int has_timestamp
)
283 struct can_frame
*firstframe
;
284 struct sockaddr_can
*addr
;
285 struct sock
*sk
= op
->sk
;
286 int datalen
= head
->nframes
* CFSIZ
;
289 skb
= alloc_skb(sizeof(*head
) + datalen
, gfp_any());
293 memcpy(skb_put(skb
, sizeof(*head
)), head
, sizeof(*head
));
296 /* can_frames starting here */
297 firstframe
= (struct can_frame
*)skb_tail_pointer(skb
);
299 memcpy(skb_put(skb
, datalen
), frames
, datalen
);
302 * the BCM uses the can_dlc-element of the can_frame
303 * structure for internal purposes. This is only
304 * relevant for updates that are generated by the
305 * BCM, where nframes is 1
307 if (head
->nframes
== 1)
308 firstframe
->can_dlc
&= BCM_CAN_DLC_MASK
;
312 /* restore rx timestamp */
313 skb
->tstamp
= op
->rx_stamp
;
317 * Put the datagram to the queue so that bcm_recvmsg() can
318 * get it from there. We need to pass the interface index to
319 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
320 * containing the interface index.
323 BUILD_BUG_ON(sizeof(skb
->cb
) < sizeof(struct sockaddr_can
));
324 addr
= (struct sockaddr_can
*)skb
->cb
;
325 memset(addr
, 0, sizeof(*addr
));
326 addr
->can_family
= AF_CAN
;
327 addr
->can_ifindex
= op
->rx_ifindex
;
329 err
= sock_queue_rcv_skb(sk
, skb
);
331 struct bcm_sock
*bo
= bcm_sk(sk
);
334 /* don't care about overflows in this statistic */
335 bo
->dropped_usr_msgs
++;
339 static void bcm_tx_timeout_tsklet(unsigned long data
)
341 struct bcm_op
*op
= (struct bcm_op
*)data
;
342 struct bcm_msg_head msg_head
;
344 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
347 if (!op
->count
&& (op
->flags
& TX_COUNTEVT
)) {
349 /* create notification to user */
350 msg_head
.opcode
= TX_EXPIRED
;
351 msg_head
.flags
= op
->flags
;
352 msg_head
.count
= op
->count
;
353 msg_head
.ival1
= op
->ival1
;
354 msg_head
.ival2
= op
->ival2
;
355 msg_head
.can_id
= op
->can_id
;
356 msg_head
.nframes
= 0;
358 bcm_send_to_user(op
, &msg_head
, NULL
, 0);
362 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
364 /* send (next) frame */
366 hrtimer_start(&op
->timer
,
367 ktime_add(ktime_get(), op
->kt_ival1
),
371 if (op
->kt_ival2
.tv64
) {
373 /* send (next) frame */
375 hrtimer_start(&op
->timer
,
376 ktime_add(ktime_get(), op
->kt_ival2
),
383 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
385 static enum hrtimer_restart
bcm_tx_timeout_handler(struct hrtimer
*hrtimer
)
387 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
389 tasklet_schedule(&op
->tsklet
);
391 return HRTIMER_NORESTART
;
395 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
397 static void bcm_rx_changed(struct bcm_op
*op
, struct can_frame
*data
)
399 struct bcm_msg_head head
;
401 /* update statistics */
402 op
->frames_filtered
++;
404 /* prevent statistics overflow */
405 if (op
->frames_filtered
> ULONG_MAX
/100)
406 op
->frames_filtered
= op
->frames_abs
= 0;
408 /* this element is not throttled anymore */
409 data
->can_dlc
&= (BCM_CAN_DLC_MASK
|RX_RECV
);
411 head
.opcode
= RX_CHANGED
;
412 head
.flags
= op
->flags
;
413 head
.count
= op
->count
;
414 head
.ival1
= op
->ival1
;
415 head
.ival2
= op
->ival2
;
416 head
.can_id
= op
->can_id
;
419 bcm_send_to_user(op
, &head
, data
, 1);
423 * bcm_rx_update_and_send - process a detected relevant receive content change
424 * 1. update the last received data
425 * 2. send a notification to the user (if possible)
427 static void bcm_rx_update_and_send(struct bcm_op
*op
,
428 struct can_frame
*lastdata
,
429 const struct can_frame
*rxdata
)
431 memcpy(lastdata
, rxdata
, CFSIZ
);
433 /* mark as used and throttled by default */
434 lastdata
->can_dlc
|= (RX_RECV
|RX_THR
);
436 /* throtteling mode inactive ? */
437 if (!op
->kt_ival2
.tv64
) {
438 /* send RX_CHANGED to the user immediately */
439 bcm_rx_changed(op
, lastdata
);
443 /* with active throttling timer we are just done here */
444 if (hrtimer_active(&op
->thrtimer
))
447 /* first receiption with enabled throttling mode */
448 if (!op
->kt_lastmsg
.tv64
)
449 goto rx_changed_settime
;
451 /* got a second frame inside a potential throttle period? */
452 if (ktime_us_delta(ktime_get(), op
->kt_lastmsg
) <
453 ktime_to_us(op
->kt_ival2
)) {
454 /* do not send the saved data - only start throttle timer */
455 hrtimer_start(&op
->thrtimer
,
456 ktime_add(op
->kt_lastmsg
, op
->kt_ival2
),
461 /* the gap was that big, that throttling was not needed here */
463 bcm_rx_changed(op
, lastdata
);
464 op
->kt_lastmsg
= ktime_get();
468 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
469 * received data stored in op->last_frames[]
471 static void bcm_rx_cmp_to_index(struct bcm_op
*op
, int index
,
472 const struct can_frame
*rxdata
)
475 * no one uses the MSBs of can_dlc for comparation,
476 * so we use it here to detect the first time of reception
479 if (!(op
->last_frames
[index
].can_dlc
& RX_RECV
)) {
480 /* received data for the first time => send update to user */
481 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
485 /* do a real check in can_frame data section */
487 if ((GET_U64(&op
->frames
[index
]) & GET_U64(rxdata
)) !=
488 (GET_U64(&op
->frames
[index
]) & GET_U64(&op
->last_frames
[index
]))) {
489 bcm_rx_update_and_send(op
, &op
->last_frames
[index
], rxdata
);
493 if (op
->flags
& RX_CHECK_DLC
) {
494 /* do a real check in can_frame dlc */
495 if (rxdata
->can_dlc
!= (op
->last_frames
[index
].can_dlc
&
497 bcm_rx_update_and_send(op
, &op
->last_frames
[index
],
505 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
507 static void bcm_rx_starttimer(struct bcm_op
*op
)
509 if (op
->flags
& RX_NO_AUTOTIMER
)
512 if (op
->kt_ival1
.tv64
)
513 hrtimer_start(&op
->timer
, op
->kt_ival1
, HRTIMER_MODE_REL
);
516 static void bcm_rx_timeout_tsklet(unsigned long data
)
518 struct bcm_op
*op
= (struct bcm_op
*)data
;
519 struct bcm_msg_head msg_head
;
521 /* create notification to user */
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);
534 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
536 static enum hrtimer_restart
bcm_rx_timeout_handler(struct hrtimer
*hrtimer
)
538 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, timer
);
540 /* schedule before NET_RX_SOFTIRQ */
541 tasklet_hi_schedule(&op
->tsklet
);
543 /* no restart of the timer is done here! */
545 /* if user wants to be informed, when cyclic CAN-Messages come back */
546 if ((op
->flags
& RX_ANNOUNCE_RESUME
) && op
->last_frames
) {
547 /* clear received can_frames to indicate 'nothing received' */
548 memset(op
->last_frames
, 0, op
->nframes
* CFSIZ
);
551 return HRTIMER_NORESTART
;
555 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
557 static inline int bcm_rx_do_flush(struct bcm_op
*op
, int update
, int index
)
559 if ((op
->last_frames
) && (op
->last_frames
[index
].can_dlc
& RX_THR
)) {
561 bcm_rx_changed(op
, &op
->last_frames
[index
]);
568 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
570 * update == 0 : just check if throttled data is available (any irq context)
571 * update == 1 : check and send throttled data to userspace (soft_irq context)
573 static int bcm_rx_thr_flush(struct bcm_op
*op
, int update
)
577 if (op
->nframes
> 1) {
580 /* for MUX filter we start at index 1 */
581 for (i
= 1; i
< op
->nframes
; i
++)
582 updated
+= bcm_rx_do_flush(op
, update
, i
);
585 /* for RX_FILTER_ID and simple filter */
586 updated
+= bcm_rx_do_flush(op
, update
, 0);
592 static void bcm_rx_thr_tsklet(unsigned long data
)
594 struct bcm_op
*op
= (struct bcm_op
*)data
;
596 /* push the changed data to the userspace */
597 bcm_rx_thr_flush(op
, 1);
601 * bcm_rx_thr_handler - the time for blocked content updates is over now:
602 * Check for throttled data and send it to the userspace
604 static enum hrtimer_restart
bcm_rx_thr_handler(struct hrtimer
*hrtimer
)
606 struct bcm_op
*op
= container_of(hrtimer
, struct bcm_op
, thrtimer
);
608 tasklet_schedule(&op
->thrtsklet
);
610 if (bcm_rx_thr_flush(op
, 0)) {
611 hrtimer_forward(hrtimer
, ktime_get(), op
->kt_ival2
);
612 return HRTIMER_RESTART
;
614 /* rearm throttle handling */
615 op
->kt_lastmsg
= ktime_set(0, 0);
616 return HRTIMER_NORESTART
;
621 * bcm_rx_handler - handle a CAN frame receiption
623 static void bcm_rx_handler(struct sk_buff
*skb
, void *data
)
625 struct bcm_op
*op
= (struct bcm_op
*)data
;
626 const struct can_frame
*rxframe
= (struct can_frame
*)skb
->data
;
629 /* disable timeout */
630 hrtimer_cancel(&op
->timer
);
632 if (op
->can_id
!= rxframe
->can_id
)
635 /* save rx timestamp */
636 op
->rx_stamp
= skb
->tstamp
;
637 /* save originator for recvfrom() */
638 op
->rx_ifindex
= skb
->dev
->ifindex
;
639 /* update statistics */
642 if (op
->flags
& RX_RTR_FRAME
) {
643 /* send reply for RTR-request (placed in op->frames[0]) */
648 if (op
->flags
& RX_FILTER_ID
) {
649 /* the easiest case */
650 bcm_rx_update_and_send(op
, &op
->last_frames
[0], rxframe
);
654 if (op
->nframes
== 1) {
655 /* simple compare with index 0 */
656 bcm_rx_cmp_to_index(op
, 0, rxframe
);
660 if (op
->nframes
> 1) {
664 * find the first multiplex mask that fits.
665 * Remark: The MUX-mask is stored in index 0
668 for (i
= 1; i
< op
->nframes
; i
++) {
669 if ((GET_U64(&op
->frames
[0]) & GET_U64(rxframe
)) ==
670 (GET_U64(&op
->frames
[0]) &
671 GET_U64(&op
->frames
[i
]))) {
672 bcm_rx_cmp_to_index(op
, i
, rxframe
);
679 bcm_rx_starttimer(op
);
683 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
685 static struct bcm_op
*bcm_find_op(struct list_head
*ops
, canid_t can_id
,
690 list_for_each_entry(op
, ops
, list
) {
691 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
))
698 static void bcm_remove_op(struct bcm_op
*op
)
700 hrtimer_cancel(&op
->timer
);
701 hrtimer_cancel(&op
->thrtimer
);
704 tasklet_kill(&op
->tsklet
);
706 if (op
->thrtsklet
.func
)
707 tasklet_kill(&op
->thrtsklet
);
709 if ((op
->frames
) && (op
->frames
!= &op
->sframe
))
712 if ((op
->last_frames
) && (op
->last_frames
!= &op
->last_sframe
))
713 kfree(op
->last_frames
);
718 static void bcm_rx_unreg(struct net_device
*dev
, struct bcm_op
*op
)
720 if (op
->rx_reg_dev
== dev
) {
721 can_rx_unregister(dev
, op
->can_id
, REGMASK(op
->can_id
),
724 /* mark as removed subscription */
725 op
->rx_reg_dev
= NULL
;
727 printk(KERN_ERR
"can-bcm: bcm_rx_unreg: registered device "
728 "mismatch %p %p\n", op
->rx_reg_dev
, dev
);
732 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
734 static int bcm_delete_rx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
736 struct bcm_op
*op
, *n
;
738 list_for_each_entry_safe(op
, n
, ops
, list
) {
739 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
742 * Don't care if we're bound or not (due to netdev
743 * problems) can_rx_unregister() is always a save
748 * Only remove subscriptions that had not
749 * been removed due to NETDEV_UNREGISTER
752 if (op
->rx_reg_dev
) {
753 struct net_device
*dev
;
755 dev
= dev_get_by_index(&init_net
,
758 bcm_rx_unreg(dev
, op
);
763 can_rx_unregister(NULL
, op
->can_id
,
773 return 0; /* not found */
777 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
779 static int bcm_delete_tx_op(struct list_head
*ops
, canid_t can_id
, int ifindex
)
781 struct bcm_op
*op
, *n
;
783 list_for_each_entry_safe(op
, n
, ops
, list
) {
784 if ((op
->can_id
== can_id
) && (op
->ifindex
== ifindex
)) {
791 return 0; /* not found */
795 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
797 static int bcm_read_op(struct list_head
*ops
, struct bcm_msg_head
*msg_head
,
800 struct bcm_op
*op
= bcm_find_op(ops
, msg_head
->can_id
, ifindex
);
805 /* put current values into msg_head */
806 msg_head
->flags
= op
->flags
;
807 msg_head
->count
= op
->count
;
808 msg_head
->ival1
= op
->ival1
;
809 msg_head
->ival2
= op
->ival2
;
810 msg_head
->nframes
= op
->nframes
;
812 bcm_send_to_user(op
, msg_head
, op
->frames
, 0);
818 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
820 static int bcm_tx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
821 int ifindex
, struct sock
*sk
)
823 struct bcm_sock
*bo
= bcm_sk(sk
);
827 /* we need a real device to send frames */
831 /* we need at least one can_frame */
832 if (msg_head
->nframes
< 1)
835 /* check the given can_id */
836 op
= bcm_find_op(&bo
->tx_ops
, msg_head
->can_id
, ifindex
);
839 /* update existing BCM operation */
842 * Do we need more space for the can_frames than currently
843 * allocated? -> This is a _really_ unusual use-case and
844 * therefore (complexity / locking) it is not supported.
846 if (msg_head
->nframes
> op
->nframes
)
849 /* update can_frames content */
850 for (i
= 0; i
< msg_head
->nframes
; i
++) {
851 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
852 msg
->msg_iov
, CFSIZ
);
854 if (op
->frames
[i
].can_dlc
> 8)
860 if (msg_head
->flags
& TX_CP_CAN_ID
) {
861 /* copy can_id into frame */
862 op
->frames
[i
].can_id
= msg_head
->can_id
;
867 /* insert new BCM operation for the given can_id */
869 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
873 op
->can_id
= msg_head
->can_id
;
875 /* create array for can_frames and copy the data */
876 if (msg_head
->nframes
> 1) {
877 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
884 op
->frames
= &op
->sframe
;
886 for (i
= 0; i
< msg_head
->nframes
; i
++) {
887 err
= memcpy_fromiovec((u8
*)&op
->frames
[i
],
888 msg
->msg_iov
, CFSIZ
);
890 if (op
->frames
[i
].can_dlc
> 8)
894 if (op
->frames
!= &op
->sframe
)
900 if (msg_head
->flags
& TX_CP_CAN_ID
) {
901 /* copy can_id into frame */
902 op
->frames
[i
].can_id
= msg_head
->can_id
;
906 /* tx_ops never compare with previous received messages */
907 op
->last_frames
= NULL
;
909 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
911 op
->ifindex
= ifindex
;
913 /* initialize uninitialized (kzalloc) structure */
914 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
915 op
->timer
.function
= bcm_tx_timeout_handler
;
917 /* initialize tasklet for tx countevent notification */
918 tasklet_init(&op
->tsklet
, bcm_tx_timeout_tsklet
,
921 /* currently unused in tx_ops */
922 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
924 /* add this bcm_op to the list of the tx_ops */
925 list_add(&op
->list
, &bo
->tx_ops
);
927 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
929 if (op
->nframes
!= msg_head
->nframes
) {
930 op
->nframes
= msg_head
->nframes
;
931 /* start multiple frame transmission with index 0 */
937 op
->flags
= msg_head
->flags
;
939 if (op
->flags
& TX_RESET_MULTI_IDX
) {
940 /* start multiple frame transmission with index 0 */
944 if (op
->flags
& SETTIMER
) {
945 /* set timer values */
946 op
->count
= msg_head
->count
;
947 op
->ival1
= msg_head
->ival1
;
948 op
->ival2
= msg_head
->ival2
;
949 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
950 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
952 /* disable an active timer due to zero values? */
953 if (!op
->kt_ival1
.tv64
&& !op
->kt_ival2
.tv64
)
954 hrtimer_cancel(&op
->timer
);
957 if ((op
->flags
& STARTTIMER
) &&
958 ((op
->kt_ival1
.tv64
&& op
->count
) || op
->kt_ival2
.tv64
)) {
960 /* spec: send can_frame when starting timer */
961 op
->flags
|= TX_ANNOUNCE
;
963 if (op
->kt_ival1
.tv64
&& (op
->count
> 0)) {
964 /* op->count-- is done in bcm_tx_timeout_handler */
965 hrtimer_start(&op
->timer
, op
->kt_ival1
,
968 hrtimer_start(&op
->timer
, op
->kt_ival2
,
972 if (op
->flags
& TX_ANNOUNCE
)
975 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
979 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
981 static int bcm_rx_setup(struct bcm_msg_head
*msg_head
, struct msghdr
*msg
,
982 int ifindex
, struct sock
*sk
)
984 struct bcm_sock
*bo
= bcm_sk(sk
);
989 if ((msg_head
->flags
& RX_FILTER_ID
) || (!(msg_head
->nframes
))) {
990 /* be robust against wrong usage ... */
991 msg_head
->flags
|= RX_FILTER_ID
;
992 /* ignore trailing garbage */
993 msg_head
->nframes
= 0;
996 if ((msg_head
->flags
& RX_RTR_FRAME
) &&
997 ((msg_head
->nframes
!= 1) ||
998 (!(msg_head
->can_id
& CAN_RTR_FLAG
))))
1001 /* check the given can_id */
1002 op
= bcm_find_op(&bo
->rx_ops
, msg_head
->can_id
, ifindex
);
1004 /* update existing BCM operation */
1007 * Do we need more space for the can_frames than currently
1008 * allocated? -> This is a _really_ unusual use-case and
1009 * therefore (complexity / locking) it is not supported.
1011 if (msg_head
->nframes
> op
->nframes
)
1014 if (msg_head
->nframes
) {
1015 /* update can_frames content */
1016 err
= memcpy_fromiovec((u8
*)op
->frames
,
1018 msg_head
->nframes
* CFSIZ
);
1022 /* clear last_frames to indicate 'nothing received' */
1023 memset(op
->last_frames
, 0, msg_head
->nframes
* CFSIZ
);
1026 op
->nframes
= msg_head
->nframes
;
1028 /* Only an update -> do not call can_rx_register() */
1032 /* insert new BCM operation for the given can_id */
1033 op
= kzalloc(OPSIZ
, GFP_KERNEL
);
1037 op
->can_id
= msg_head
->can_id
;
1038 op
->nframes
= msg_head
->nframes
;
1040 if (msg_head
->nframes
> 1) {
1041 /* create array for can_frames and copy the data */
1042 op
->frames
= kmalloc(msg_head
->nframes
* CFSIZ
,
1049 /* create and init array for received can_frames */
1050 op
->last_frames
= kzalloc(msg_head
->nframes
* CFSIZ
,
1052 if (!op
->last_frames
) {
1059 op
->frames
= &op
->sframe
;
1060 op
->last_frames
= &op
->last_sframe
;
1063 if (msg_head
->nframes
) {
1064 err
= memcpy_fromiovec((u8
*)op
->frames
, msg
->msg_iov
,
1065 msg_head
->nframes
* CFSIZ
);
1067 if (op
->frames
!= &op
->sframe
)
1069 if (op
->last_frames
!= &op
->last_sframe
)
1070 kfree(op
->last_frames
);
1076 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1078 op
->ifindex
= ifindex
;
1080 /* initialize uninitialized (kzalloc) structure */
1081 hrtimer_init(&op
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1082 op
->timer
.function
= bcm_rx_timeout_handler
;
1084 /* initialize tasklet for rx timeout notification */
1085 tasklet_init(&op
->tsklet
, bcm_rx_timeout_tsklet
,
1086 (unsigned long) op
);
1088 hrtimer_init(&op
->thrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1089 op
->thrtimer
.function
= bcm_rx_thr_handler
;
1091 /* initialize tasklet for rx throttle handling */
1092 tasklet_init(&op
->thrtsklet
, bcm_rx_thr_tsklet
,
1093 (unsigned long) op
);
1095 /* add this bcm_op to the list of the rx_ops */
1096 list_add(&op
->list
, &bo
->rx_ops
);
1098 /* call can_rx_register() */
1101 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1104 op
->flags
= msg_head
->flags
;
1106 if (op
->flags
& RX_RTR_FRAME
) {
1108 /* no timers in RTR-mode */
1109 hrtimer_cancel(&op
->thrtimer
);
1110 hrtimer_cancel(&op
->timer
);
1113 * funny feature in RX(!)_SETUP only for RTR-mode:
1114 * copy can_id into frame BUT without RTR-flag to
1115 * prevent a full-load-loopback-test ... ;-]
1117 if ((op
->flags
& TX_CP_CAN_ID
) ||
1118 (op
->frames
[0].can_id
== op
->can_id
))
1119 op
->frames
[0].can_id
= op
->can_id
& ~CAN_RTR_FLAG
;
1122 if (op
->flags
& SETTIMER
) {
1124 /* set timer value */
1125 op
->ival1
= msg_head
->ival1
;
1126 op
->ival2
= msg_head
->ival2
;
1127 op
->kt_ival1
= timeval_to_ktime(msg_head
->ival1
);
1128 op
->kt_ival2
= timeval_to_ktime(msg_head
->ival2
);
1130 /* disable an active timer due to zero value? */
1131 if (!op
->kt_ival1
.tv64
)
1132 hrtimer_cancel(&op
->timer
);
1135 * In any case cancel the throttle timer, flush
1136 * potentially blocked msgs and reset throttle handling
1138 op
->kt_lastmsg
= ktime_set(0, 0);
1139 hrtimer_cancel(&op
->thrtimer
);
1140 bcm_rx_thr_flush(op
, 1);
1143 if ((op
->flags
& STARTTIMER
) && op
->kt_ival1
.tv64
)
1144 hrtimer_start(&op
->timer
, op
->kt_ival1
,
1148 /* now we can register for can_ids, if we added a new bcm_op */
1149 if (do_rx_register
) {
1151 struct net_device
*dev
;
1153 dev
= dev_get_by_index(&init_net
, ifindex
);
1155 err
= can_rx_register(dev
, op
->can_id
,
1156 REGMASK(op
->can_id
),
1160 op
->rx_reg_dev
= dev
;
1165 err
= can_rx_register(NULL
, op
->can_id
,
1166 REGMASK(op
->can_id
),
1167 bcm_rx_handler
, op
, "bcm");
1169 /* this bcm rx op is broken -> remove it */
1170 list_del(&op
->list
);
1176 return msg_head
->nframes
* CFSIZ
+ MHSIZ
;
1180 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1182 static int bcm_tx_send(struct msghdr
*msg
, int ifindex
, struct sock
*sk
)
1184 struct sk_buff
*skb
;
1185 struct net_device
*dev
;
1188 /* we need a real device to send frames */
1192 skb
= alloc_skb(CFSIZ
, GFP_KERNEL
);
1197 err
= memcpy_fromiovec(skb_put(skb
, CFSIZ
), msg
->msg_iov
, CFSIZ
);
1203 dev
= dev_get_by_index(&init_net
, ifindex
);
1211 err
= can_send(skb
, 1); /* send with loopback */
1217 return CFSIZ
+ MHSIZ
;
1221 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1223 static int bcm_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1224 struct msghdr
*msg
, size_t size
)
1226 struct sock
*sk
= sock
->sk
;
1227 struct bcm_sock
*bo
= bcm_sk(sk
);
1228 int ifindex
= bo
->ifindex
; /* default ifindex for this bcm_op */
1229 struct bcm_msg_head msg_head
;
1230 int ret
; /* read bytes or error codes as return value */
1235 /* check for valid message length from userspace */
1236 if (size
< MHSIZ
|| (size
- MHSIZ
) % CFSIZ
)
1239 /* check for alternative ifindex for this bcm_op */
1241 if (!ifindex
&& msg
->msg_name
) {
1242 /* no bound device as default => check msg_name */
1243 struct sockaddr_can
*addr
=
1244 (struct sockaddr_can
*)msg
->msg_name
;
1246 if (addr
->can_family
!= AF_CAN
)
1249 /* ifindex from sendto() */
1250 ifindex
= addr
->can_ifindex
;
1253 struct net_device
*dev
;
1255 dev
= dev_get_by_index(&init_net
, ifindex
);
1259 if (dev
->type
!= ARPHRD_CAN
) {
1268 /* read message head information */
1270 ret
= memcpy_fromiovec((u8
*)&msg_head
, msg
->msg_iov
, MHSIZ
);
1276 switch (msg_head
.opcode
) {
1279 ret
= bcm_tx_setup(&msg_head
, msg
, ifindex
, sk
);
1283 ret
= bcm_rx_setup(&msg_head
, msg
, ifindex
, sk
);
1287 if (bcm_delete_tx_op(&bo
->tx_ops
, msg_head
.can_id
, ifindex
))
1294 if (bcm_delete_rx_op(&bo
->rx_ops
, msg_head
.can_id
, ifindex
))
1301 /* reuse msg_head for the reply to TX_READ */
1302 msg_head
.opcode
= TX_STATUS
;
1303 ret
= bcm_read_op(&bo
->tx_ops
, &msg_head
, ifindex
);
1307 /* reuse msg_head for the reply to RX_READ */
1308 msg_head
.opcode
= RX_STATUS
;
1309 ret
= bcm_read_op(&bo
->rx_ops
, &msg_head
, ifindex
);
1313 /* we need exactly one can_frame behind the msg head */
1314 if ((msg_head
.nframes
!= 1) || (size
!= CFSIZ
+ MHSIZ
))
1317 ret
= bcm_tx_send(msg
, ifindex
, sk
);
1331 * notification handler for netdevice status changes
1333 static int bcm_notifier(struct notifier_block
*nb
, unsigned long msg
,
1336 struct net_device
*dev
= (struct net_device
*)data
;
1337 struct bcm_sock
*bo
= container_of(nb
, struct bcm_sock
, notifier
);
1338 struct sock
*sk
= &bo
->sk
;
1340 int notify_enodev
= 0;
1342 if (!net_eq(dev_net(dev
), &init_net
))
1345 if (dev
->type
!= ARPHRD_CAN
)
1350 case NETDEV_UNREGISTER
:
1353 /* remove device specific receive entries */
1354 list_for_each_entry(op
, &bo
->rx_ops
, list
)
1355 if (op
->rx_reg_dev
== dev
)
1356 bcm_rx_unreg(dev
, op
);
1358 /* remove device reference, if this is our bound device */
1359 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1367 if (notify_enodev
) {
1368 sk
->sk_err
= ENODEV
;
1369 if (!sock_flag(sk
, SOCK_DEAD
))
1370 sk
->sk_error_report(sk
);
1375 if (bo
->bound
&& bo
->ifindex
== dev
->ifindex
) {
1376 sk
->sk_err
= ENETDOWN
;
1377 if (!sock_flag(sk
, SOCK_DEAD
))
1378 sk
->sk_error_report(sk
);
1386 * initial settings for all BCM sockets to be set at socket creation time
1388 static int bcm_init(struct sock
*sk
)
1390 struct bcm_sock
*bo
= bcm_sk(sk
);
1394 bo
->dropped_usr_msgs
= 0;
1395 bo
->bcm_proc_read
= NULL
;
1397 INIT_LIST_HEAD(&bo
->tx_ops
);
1398 INIT_LIST_HEAD(&bo
->rx_ops
);
1401 bo
->notifier
.notifier_call
= bcm_notifier
;
1403 register_netdevice_notifier(&bo
->notifier
);
1409 * standard socket functions
1411 static int bcm_release(struct socket
*sock
)
1413 struct sock
*sk
= sock
->sk
;
1414 struct bcm_sock
*bo
= bcm_sk(sk
);
1415 struct bcm_op
*op
, *next
;
1417 /* remove bcm_ops, timer, rx_unregister(), etc. */
1419 unregister_netdevice_notifier(&bo
->notifier
);
1423 list_for_each_entry_safe(op
, next
, &bo
->tx_ops
, list
)
1426 list_for_each_entry_safe(op
, next
, &bo
->rx_ops
, list
) {
1428 * Don't care if we're bound or not (due to netdev problems)
1429 * can_rx_unregister() is always a save thing to do here.
1433 * Only remove subscriptions that had not
1434 * been removed due to NETDEV_UNREGISTER
1437 if (op
->rx_reg_dev
) {
1438 struct net_device
*dev
;
1440 dev
= dev_get_by_index(&init_net
, op
->ifindex
);
1442 bcm_rx_unreg(dev
, op
);
1447 can_rx_unregister(NULL
, op
->can_id
,
1448 REGMASK(op
->can_id
),
1449 bcm_rx_handler
, op
);
1454 /* remove procfs entry */
1455 if (proc_dir
&& bo
->bcm_proc_read
)
1456 remove_proc_entry(bo
->procname
, proc_dir
);
1458 /* remove device reference */
1473 static int bcm_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int len
,
1476 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
1477 struct sock
*sk
= sock
->sk
;
1478 struct bcm_sock
*bo
= bcm_sk(sk
);
1480 if (len
< sizeof(*addr
))
1486 /* bind a device to this socket */
1487 if (addr
->can_ifindex
) {
1488 struct net_device
*dev
;
1490 dev
= dev_get_by_index(&init_net
, addr
->can_ifindex
);
1494 if (dev
->type
!= ARPHRD_CAN
) {
1499 bo
->ifindex
= dev
->ifindex
;
1503 /* no interface reference for ifindex = 0 ('any' CAN device) */
1510 /* unique socket address as filename */
1511 sprintf(bo
->procname
, "%p", sock
);
1512 bo
->bcm_proc_read
= proc_create_data(bo
->procname
, 0644,
1514 &bcm_proc_fops
, sk
);
1520 static int bcm_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1521 struct msghdr
*msg
, size_t size
, int flags
)
1523 struct sock
*sk
= sock
->sk
;
1524 struct sk_buff
*skb
;
1529 noblock
= flags
& MSG_DONTWAIT
;
1530 flags
&= ~MSG_DONTWAIT
;
1531 skb
= skb_recv_datagram(sk
, flags
, noblock
, &error
);
1535 if (skb
->len
< size
)
1538 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
);
1540 skb_free_datagram(sk
, skb
);
1544 sock_recv_ts_and_drops(msg
, sk
, skb
);
1546 if (msg
->msg_name
) {
1547 msg
->msg_namelen
= sizeof(struct sockaddr_can
);
1548 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
1551 skb_free_datagram(sk
, skb
);
1556 static struct proto_ops bcm_ops __read_mostly
= {
1558 .release
= bcm_release
,
1559 .bind
= sock_no_bind
,
1560 .connect
= bcm_connect
,
1561 .socketpair
= sock_no_socketpair
,
1562 .accept
= sock_no_accept
,
1563 .getname
= sock_no_getname
,
1564 .poll
= datagram_poll
,
1565 .ioctl
= NULL
, /* use can_ioctl() from af_can.c */
1566 .listen
= sock_no_listen
,
1567 .shutdown
= sock_no_shutdown
,
1568 .setsockopt
= sock_no_setsockopt
,
1569 .getsockopt
= sock_no_getsockopt
,
1570 .sendmsg
= bcm_sendmsg
,
1571 .recvmsg
= bcm_recvmsg
,
1572 .mmap
= sock_no_mmap
,
1573 .sendpage
= sock_no_sendpage
,
1576 static struct proto bcm_proto __read_mostly
= {
1578 .owner
= THIS_MODULE
,
1579 .obj_size
= sizeof(struct bcm_sock
),
1583 static struct can_proto bcm_can_proto __read_mostly
= {
1585 .protocol
= CAN_BCM
,
1590 static int __init
bcm_module_init(void)
1596 err
= can_proto_register(&bcm_can_proto
);
1598 printk(KERN_ERR
"can: registration of bcm protocol failed\n");
1602 /* create /proc/net/can-bcm directory */
1603 proc_dir
= proc_mkdir("can-bcm", init_net
.proc_net
);
1607 static void __exit
bcm_module_exit(void)
1609 can_proto_unregister(&bcm_can_proto
);
1612 proc_net_remove(&init_net
, "can-bcm");
1615 module_init(bcm_module_init
);
1616 module_exit(bcm_module_exit
);