42 #include <linux/module.h>
45 #include <linux/hrtimer.h>
46 #include <linux/list.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>
58 #include <linux/slab.h>
67 #define MAX_NFRAMES 256
72 #define BCM_CAN_DLC_MASK 0x0F
75 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
76 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
77 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
79 #define CAN_BCM_VERSION CAN_VERSION
91 return *(
u64 *)cp->data;
135 #define CFSIZ sizeof(struct can_frame)
136 #define OPSIZ sizeof(struct bcm_op)
137 #define MHSIZ sizeof(struct bcm_msg_head)
160 static int bcm_proc_show(
struct seq_file *
m,
void *
v)
170 seq_printf(m,
" / dropped %lu", bo->dropped_usr_msgs);
171 seq_printf(m,
" / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
176 unsigned long reduction;
196 seq_printf(m,
"# recv %ld (%ld) => reduction: ",
202 (reduction == 100)?
"near ":
"", reduction);
209 bcm_proc_getifname(ifname, op->
ifindex),
214 (
long long) ktime_to_us(op->
kt_ival1));
218 (
long long) ktime_to_us(op->
kt_ival2));
233 .open = bcm_proc_open,
243 static void bcm_can_tx(
struct bcm_op *op)
259 skb = alloc_skb(
CFSIZ, gfp_any());
286 struct can_frame *frames,
int has_timestamp)
295 skb = alloc_skb(
sizeof(*head) + datalen, gfp_any());
303 firstframe = (
struct can_frame *)skb_tail_pointer(skb);
331 memset(addr, 0,
sizeof(*addr));
345 static void bcm_tx_start_timer(
struct bcm_op *op)
357 static void bcm_tx_timeout_tsklet(
unsigned long data)
369 msg_head.flags = op->
flags;
370 msg_head.count = op->
count;
371 msg_head.ival1 = op->ival1;
373 msg_head.can_id = op->
can_id;
374 msg_head.nframes = 0;
376 bcm_send_to_user(op, &msg_head,
NULL, 0);
383 bcm_tx_start_timer(op);
393 tasklet_schedule(&op->tsklet);
418 head.ival1 = op->ival1;
423 bcm_send_to_user(op, &head, data, 1);
431 static void bcm_rx_update_and_send(
struct bcm_op *op,
443 bcm_rx_changed(op, lastdata);
453 goto rx_changed_settime;
467 bcm_rx_changed(op, lastdata);
475 static void bcm_rx_cmp_to_index(
struct bcm_op *op,
unsigned int index,
485 bcm_rx_update_and_send(op, &op->
last_frames[index], rxdata);
491 if ((GET_U64(&op->
frames[index]) & GET_U64(rxdata)) !=
493 bcm_rx_update_and_send(op, &op->
last_frames[index], rxdata);
501 bcm_rx_update_and_send(op, &op->
last_frames[index],
511 static void bcm_rx_starttimer(
struct bcm_op *op)
520 static void bcm_rx_timeout_tsklet(
unsigned long data)
527 msg_head.flags = op->
flags;
528 msg_head.count = op->
count;
529 msg_head.ival1 = op->ival1;
531 msg_head.can_id = op->
can_id;
532 msg_head.nframes = 0;
534 bcm_send_to_user(op, &msg_head,
NULL, 0);
545 tasklet_hi_schedule(&op->tsklet);
561 static inline int bcm_rx_do_flush(
struct bcm_op *op,
int update,
578 static int bcm_rx_thr_flush(
struct bcm_op *op,
int update)
586 for (i = 1; i < op->
nframes; i++)
587 updated += bcm_rx_do_flush(op, update, i);
591 updated += bcm_rx_do_flush(op, update, 0);
597 static void bcm_rx_thr_tsklet(
unsigned long data)
602 bcm_rx_thr_flush(op, 1);
609 static enum hrtimer_restart bcm_rx_thr_handler(
struct hrtimer *hrtimer)
615 if (bcm_rx_thr_flush(op, 0)) {
628 static void bcm_rx_handler(
struct sk_buff *skb,
void *data)
655 bcm_rx_update_and_send(op, &op->
last_frames[0], rxframe);
661 bcm_rx_cmp_to_index(op, 0, rxframe);
673 for (i = 1; i < op->
nframes; i++) {
674 if ((GET_U64(&op->
frames[0]) & GET_U64(rxframe)) ==
675 (GET_U64(&op->
frames[0]) &
676 GET_U64(&op->
frames[i]))) {
677 bcm_rx_cmp_to_index(op, i, rxframe);
684 bcm_rx_starttimer(op);
703 static void bcm_remove_op(
struct bcm_op *op)
739 static int bcm_delete_rx_op(
struct list_head *ops,
canid_t can_id,
int ifindex)
763 bcm_rx_unreg(dev, op);
784 static int bcm_delete_tx_op(
struct list_head *ops,
canid_t can_id,
int ifindex)
805 struct bcm_op *op = bcm_find_op(ops, msg_head->
can_id, ifindex);
813 msg_head->ival1 = op->ival1;
817 bcm_send_to_user(op, msg_head, op->
frames, 0);
826 int ifindex,
struct sock *sk)
842 op = bcm_find_op(&bo->
tx_ops, msg_head->
can_id, ifindex);
856 for (i = 0; i < msg_head->
nframes; i++) {
860 if (op->
frames[i].can_dlc > 8)
892 for (i = 0; i < msg_head->
nframes; i++) {
896 if (op->
frames[i].can_dlc > 8)
921 op->timer.function = bcm_tx_timeout_handler;
953 op->ival1 = msg_head->ival1;
955 op->
kt_ival1 = timeval_to_ktime(msg_head->ival1);
976 bcm_tx_start_timer(op);
985 int ifindex,
struct sock *sk)
1009 op = bcm_find_op(&bo->
rx_ops, msg_head->
can_id, ifindex);
1092 op->timer.function = bcm_rx_timeout_handler;
1096 (
unsigned long) op);
1099 op->
thrtimer.function = bcm_rx_thr_handler;
1103 (
unsigned long) op);
1135 op->ival1 = msg_head->ival1;
1137 op->
kt_ival1 = timeval_to_ktime(msg_head->ival1);
1150 bcm_rx_thr_flush(op, 1);
1159 if (do_rx_register) {
1177 bcm_rx_handler, op,
"bcm");
1192 static int bcm_tx_send(
struct msghdr *msg,
int ifindex,
struct sock *sk)
1236 struct sock *sk = sock->
sk;
1289 switch (msg_head.
opcode) {
1292 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1296 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1300 if (bcm_delete_tx_op(&bo->
tx_ops, msg_head.
can_id, ifindex))
1307 if (bcm_delete_rx_op(&bo->
rx_ops, msg_head.
can_id, ifindex))
1316 ret = bcm_read_op(&bo->
tx_ops, &msg_head, ifindex);
1322 ret = bcm_read_op(&bo->
rx_ops, &msg_head, ifindex);
1327 if ((msg_head.
nframes != 1) || (size != CFSIZ +
MHSIZ))
1330 ret = bcm_tx_send(msg, ifindex, sk);
1346 static int bcm_notifier(
struct notifier_block *nb,
unsigned long msg,
1351 struct sock *sk = &bo->
sk;
1353 int notify_enodev = 0;
1355 if (!net_eq(dev_net(dev), &
init_net))
1369 bcm_rx_unreg(dev, op);
1372 if (bo->bound && bo->ifindex == dev->ifindex) {
1380 if (notify_enodev) {
1401 static int bcm_init(
struct sock *sk)
1410 INIT_LIST_HEAD(&bo->
tx_ops);
1411 INIT_LIST_HEAD(&bo->
rx_ops);
1414 bo->
notifier.notifier_call = bcm_notifier;
1424 static int bcm_release(
struct socket *sock)
1426 struct sock *sk = sock->
sk;
1455 if (op->rx_reg_dev) {
1460 bcm_rx_unreg(dev, op);
1467 bcm_rx_handler, op);
1495 struct sock *sk = sock->
sk;
1498 if (len <
sizeof(*addr))
1532 &bcm_proc_fops, sk);
1538 static int bcm_recvmsg(
struct kiocb *iocb,
struct socket *sock,
1539 struct msghdr *msg,
size_t size,
int flags)
1541 struct sock *sk = sock->
sk;
1548 flags &= ~MSG_DONTWAIT;
1553 if (skb->
len < size)
1562 sock_recv_ts_and_drops(msg, sk, skb);
1574 static const struct proto_ops bcm_ops = {
1576 .release = bcm_release,
1578 .connect = bcm_connect,
1588 .sendmsg = bcm_sendmsg,
1589 .recvmsg = bcm_recvmsg,
1597 .obj_size =
sizeof(
struct bcm_sock),
1601 static const struct can_proto bcm_can_proto = {
1608 static int __init bcm_module_init(
void)
1625 static void __exit bcm_module_exit(
void)