/*
- * bcm.c
+ * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
*
* Copyright (c) 2002-2005 Volkswagen Group Electronic Research
* All rights reserved.
#include "af_can.h"
-
RCSID("$Id$");
#ifdef CONFIG_CAN_DEBUG_CORE
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
-
-#define GET_U64(p) (*(unsigned long long*)(p)->data)
+#define GET_U64(p) (*(unsigned long long*)(p)->data) /* easy access */
struct bcm_op {
struct list_head list;
+ int ifindex;
canid_t can_id;
int flags;
unsigned long j_ival1, j_ival2, j_lastmsg;
unsigned long frames_abs, frames_filtered;
- struct timeval ival1, ival2, stamp;
struct timer_list timer, thrtimer;
+ struct timeval ival1, ival2;
+ struct timeval rx_stamp;
+ int rx_ifindex;
int count;
int nframes;
int currframe;
};
static struct proc_dir_entry *proc_dir = NULL;
-static int bcm_init(struct sock *sk);
-static int bcm_read_proc(char *page, char **start, off_t off,
- int count, int *eof, void *data);
+static int bcm_init(struct sock *sk);
static void bcm_notifier(unsigned long msg, void *data);
-static int bcm_release(struct socket *sock);
-static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
- int flags);
+static int bcm_release(struct socket *sock);
+static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
+ int flags);
+static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
+ struct msghdr *msg, size_t size);
+static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
+ struct msghdr *msg, size_t size, int flags);
static unsigned int bcm_poll(struct file *file, struct socket *sock,
poll_table *wait);
-static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size);
-static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size, int flags);
+
+static int bcm_read_proc(char *page, char **start, off_t off,
+ int count, int *eof, void *data);
static void bcm_tx_timeout_handler(unsigned long data);
+static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
+static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+ int ifindex, struct sock *sk);
+static void bcm_can_tx(struct bcm_op *op);
+
+static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+ int ifindex, struct sock *sk);
static void bcm_rx_handler(struct sk_buff *skb, void *op);
static void bcm_rx_timeout_handler(unsigned long data);
static void bcm_rx_thr_handler(unsigned long data);
-static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id);
-static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id);
-static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id);
-static void bcm_remove_op(struct bcm_op *op);
-static void bcm_can_tx(struct bcm_op *op);
-static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
- struct can_frame *frames, struct timeval *tv);
+static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
+ struct can_frame *rxdata);
static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
static void bcm_rx_starttimer(struct bcm_op *op);
static void bcm_rx_update_and_send(struct bcm_op *op,
struct can_frame *lastdata,
struct can_frame *rxdata);
-static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
- struct can_frame *rxdata);
+static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
+ struct can_frame *frames, struct timeval *tv);
+
+static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex);
+static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex);
+static void bcm_remove_op(struct bcm_op *op);
+static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex);
+static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex);
static struct proto_ops bcm_ops = {
.family = PF_CAN,
#endif
+#define CFSIZ sizeof(struct can_frame)
+#define OPSIZ sizeof(struct bcm_op)
+#define MHSIZ sizeof(struct bcm_msg_head)
static int __init bcm_module_init(void)
{
}
+/**************************************************/
+/* initial settings at socket creation time */
+/**************************************************/
+
static int bcm_init(struct sock *sk)
{
struct bcm_opt *bo = bcm_sk(sk);
return 0;
}
+/**************************************************/
+/* handling of netdevice problems */
+/**************************************************/
+
static void bcm_notifier(unsigned long msg, void *data)
{
struct sock *sk = (struct sock *)data;
+ struct bcm_opt *bo = bcm_sk(sk);
DBG("called for sock %p\n", sk);
switch (msg) {
case NETDEV_UNREGISTER:
- sk->sk_bound_dev_if = 0;
+ bo->bound = 0;
+ bo->ifindex = 0;
/* fallthrough */
case NETDEV_DOWN:
sk->sk_err = ENETDOWN;
}
}
+/**************************************************/
+/* standard socket functions */
+/**************************************************/
+
static int bcm_release(struct socket *sock)
{
struct sock *sk = sock->sk;
list_for_each_entry_safe(op, n, &bo->rx_ops, list) {
DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
- if (sk->sk_bound_dev_if) {
- struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+ /* Don't care if we're bound or not (due to netdev problems) */
+ /* can_rx_unregister() is always a save thing to do here */
+ if (op->ifindex) {
+ struct net_device *dev = dev_get_by_index(op->ifindex);
if (dev) {
can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
dev_put(dev);
}
} else
- DBG("sock %p not bound for can_rx_unregister()\n", sk);
+ can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
bcm_remove_op(op);
}
}
/* remove device notifier */
- if (sk->sk_bound_dev_if) {
- struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+ if (bo->ifindex) {
+ struct net_device *dev = dev_get_by_index(bo->ifindex);
if (dev) {
can_dev_unregister(dev, bcm_notifier, sk);
dev_put(dev);
}
- } else
- DBG("sock %p not bound for can_dev_unregister()\n", sk);
+ }
sock_put(sk);
struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
struct sock *sk = sock->sk;
struct bcm_opt *bo = bcm_sk(sk);
- struct net_device *dev;
+
+ if (bo->bound)
+ return -EISCONN;
/* bind a device to this socket */
- dev = dev_get_by_index(addr->can_ifindex);
- if (!dev) {
- DBG("could not find device %d\n", addr->can_ifindex);
- return -ENODEV;
- }
- sk->sk_bound_dev_if = dev->ifindex;
- can_dev_register(dev, bcm_notifier, sk);
- dev_put(dev);
+ if (addr->can_ifindex) {
+ struct net_device *dev = dev_get_by_index(addr->can_ifindex);
+ if (!dev) {
+ DBG("could not find device index %d\n", addr->can_ifindex);
+ return -ENODEV;
+ }
+ bo->ifindex = dev->ifindex;
+ can_dev_register(dev, bcm_notifier, sk); /* register notifier */
+ dev_put(dev);
+
+ DBG("socket %p bound to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
+ } else
+ bo->ifindex = 0; /* no notifier for ifindex = 0 ('any' CAN device) */
- DBG("socket %p to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
+ bo->bound = 1;
if (proc_dir) {
- sprintf(bo->procname, "%p", bo);
+ sprintf(bo->procname, "%p", sock); /* unique socket address as filename */
bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
- proc_dir, bcm_read_proc, bo);
+ proc_dir, bcm_read_proc, sk);
}
return 0;
}
-static int bcm_read_proc(char *page, char **start, off_t off,
- int count, int *eof, void *data)
+static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
+ struct msghdr *msg, size_t size)
{
- int len = 0;
- struct bcm_opt *bo = (struct bcm_opt *) data;
- struct bcm_op *op;
- struct net_device *dev = NULL;
-
- len += snprintf(page + len, PAGE_SIZE - len,">>> bo %p", bo);
- len += snprintf(page + len, PAGE_SIZE - len," / dropped %lu", bo->dropped_usr_msgs);
-
- if (!list_empty(&bo->rx_ops)) {
- struct sock *sk;
- op = list_entry(bo->rx_ops.next, struct bcm_op, list);
- sk = op->sk;
- if (sk->sk_bound_dev_if)
- dev = dev_get_by_index(sk->sk_bound_dev_if);
- len += snprintf(page + len, PAGE_SIZE - len,
- " / sk %p / socket %p", sk, sk->sk_socket);
- } else if (!list_empty(&bo->tx_ops)) {
- struct sock *sk;
- op = list_entry(bo->tx_ops.next, struct bcm_op, list);
- sk = op->sk;
- if (sk->sk_bound_dev_if)
- dev = dev_get_by_index(sk->sk_bound_dev_if);
- len += snprintf(page + len, PAGE_SIZE - len,
- " / sk %p / socket %p", sk, sk->sk_socket);
+ struct sock *sk = sock->sk;
+ struct bcm_opt *bo = bcm_sk(sk);
+ int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
+ struct bcm_msg_head msg_head;
+ int ret; /* read bytes or error codes as return value */
+
+ if (!bo->bound) {
+ DBG("sock %p not bound\n", sk);
+ return -ENOTCONN;
}
- if (dev) {
- len += snprintf(page + len, PAGE_SIZE - len, " / %s", dev->name);
- dev_put(dev);
+ /* check for alternative ifindex for this bcm_op */
+
+ if (!ifindex && msg->msg_name) { /* no bound device as default */
+ struct sockaddr_can *addr = (struct sockaddr_can *)msg->msg_name;
+ if (addr->can_family != AF_CAN)
+ return -EINVAL;
+ ifindex = addr->can_ifindex; /* ifindex from sendto() */
+
+ if (ifindex && !dev_get_by_index(ifindex)) {
+ DBG("device %d not found\n", ifindex);
+ return -ENODEV;
+ }
}
- len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
+ /* read message head information */
- list_for_each_entry(op, &bo->rx_ops, list) {
+ if ((ret = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
+ MHSIZ)) < 0)
+ return ret;
- unsigned long reduction;
+ DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
- /* print only active entries & prevent division by zero */
- if (!op->frames_abs)
- continue;
+ switch (msg_head.opcode) {
- len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X [%d]%c ",
- op->can_id, op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
- if (op->j_ival1)
- len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
+ case TX_SETUP:
- if (op->j_ival2)
- len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
+ ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
+ break;
- len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
- op->frames_filtered, op->frames_abs);
+ case RX_SETUP:
- reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
+ ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
+ break;
- len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
- (reduction == 100)?"near ":"", reduction);
+ case TX_DELETE:
- if (len > PAGE_SIZE - 200) {
- /* mark output cut off */
- len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
- break;
- }
- }
+ if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
+ ret = MHSIZ;
+ else
+ ret = -EINVAL;
+ break;
+
+ case RX_DELETE:
- list_for_each_entry(op, &bo->tx_ops, list) {
+ if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
+ ret = MHSIZ;
+ else
+ ret = -EINVAL;
+ break;
- len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X [%d] ",
- op->can_id, op->nframes);
- if (op->j_ival1)
- len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
+ case TX_READ:
- if (op->j_ival2)
- len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
+ /* reuse msg_head for the reply */
+ msg_head.opcode = TX_STATUS; /* reply to TX_READ */
+ ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
+ break;
- len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
+ case RX_READ:
- if (len > PAGE_SIZE - 100) {
- /* mark output cut off */
- len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
- break;
- }
- }
+ /* reuse msg_head for the reply */
+ msg_head.opcode = RX_STATUS; /* reply to RX_READ */
+ ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
+ break;
- len += snprintf(page + len, PAGE_SIZE - len, "\n");
+ case TX_SEND:
- *eof = 1;
- return len;
-}
+ if (msg_head.nframes < 1) /* we need at least one can_frame */
+ return -EINVAL;
-static unsigned int bcm_poll(struct file *file, struct socket *sock,
- poll_table *wait)
-{
- unsigned int mask = 0;
+ ret = bcm_tx_send(msg, ifindex, sk);
+ break;
- DBG("socket %p\n", sock);
+ default:
- mask = datagram_poll(file, sock, wait);
- return mask;
+ DBG("Unknown opcode %d\n", msg_head.opcode);
+ ret = -EINVAL;
+ break;
+ }
+
+ return ret;
}
-static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size)
+static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
+ struct msghdr *msg, size_t size, int flags)
{
- struct bcm_msg_head msg_head;
- int i;
- struct bcm_op *op;
- int err;
struct sock *sk = sock->sk;
- struct bcm_opt *bo = bcm_sk(sk);
- char c;
- int rbytes = 0; /* read bytes as return value */
+ struct sk_buff *skb;
+ int error = 0;
+ int noblock;
+ int err;
- /* read message head information */
+ DBG("socket %p, sk %p\n", sock, sk);
- if ((err = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
- sizeof(msg_head))) < 0)
- return err;
+ noblock = flags & MSG_DONTWAIT;
+ flags &= ~MSG_DONTWAIT;
+ if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
+ return error;
+ }
- DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
+ DBG("delivering skbuff %p\n", skb);
+ DBG_SKB(skb);
- if (!sk->sk_bound_dev_if) {
- DBG("sock %p not bound\n", sk);
- return -ENOTCONN;
+ if (skb->len < size)
+ size = skb->len;
+ if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
+ skb_free_datagram(sk, skb);
+ return err;
}
- switch (msg_head.opcode) {
+ sock_recv_timestamp(msg, sk, skb);
- case TX_SETUP:
+ if (msg->msg_name) {
+ msg->msg_namelen = sizeof(struct sockaddr_can);
+ memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
+ }
- if (msg_head.nframes < 1) /* we need at least one can_frame */
- return -EINVAL;
+ DBG("freeing sock %p, skbuff %p\n", sk, skb);
+ skb_free_datagram(sk, skb);
- /* check the given can_id */
+ return size;
+}
- if (!(op = bcm_find_op(&bo->tx_ops, msg_head.can_id))) {
+static unsigned int bcm_poll(struct file *file, struct socket *sock,
+ poll_table *wait)
+{
+ unsigned int mask = 0;
- /* insert new BCM operation for the given can_id */
+ DBG("socket %p\n", sock);
- if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
- return -ENOMEM;
+ mask = datagram_poll(file, sock, wait);
+ return mask;
+}
- memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
+/**************************************************/
+/* helper functions for bcm_sendmsg() */
+/**************************************************/
- DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
- op, msg_head.can_id);
+static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+ int ifindex, struct sock *sk)
+{
+ struct bcm_opt *bo = bcm_sk(sk);
+ struct bcm_op *op;
+ int i, err;
- op->can_id = msg_head.can_id;
+ if (!ifindex) /* we need a real device to send frames */
+ return -ENODEV;
- /* create array for can_frames and copy the data */
- if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
- kfree(op);
- return -ENOMEM;
- }
+ if (msg_head->nframes < 1) /* we need at least one can_frame */
+ return -EINVAL;
- for (i = 0; i < msg_head.nframes; i++) {
- if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame))) < 0) {
- kfree(op->frames);
- kfree(op);
- return err;
- }
+ /* check the given can_id */
- if (msg_head.flags & TX_CP_CAN_ID)
- op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
- }
+ if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
- op->last_frames = NULL; /* tx_ops never compare with previous received messages */
+ /* update existing BCM operation */
- op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
+ DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
+ op, msg_head->can_id);
- init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
- init_timer(&op->thrtimer); /* currently unused in tx_ops */
+ /* Do we need more space for the can_frames than currently */
+ /* allocated? -> This is a _really_ unusual use-case and */
+ /* therefore (complexity / locking) it is not supported. */
+ if (msg_head->nframes > op->nframes)
+ return -E2BIG;
- op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
- op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
+ /* update can_frames content */
+ for (i = 0; i < msg_head->nframes; i++) {
+ if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0)
+ return err;
- /* add this bcm_op to the list of the tx_ops */
- list_add(&op->list, &bo->tx_ops);
+ if (msg_head->flags & TX_CP_CAN_ID)
+ op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
+ }
- } else {
- /* update existing BCM operation */
+ } else {
+ /* insert new BCM operation for the given can_id */
- DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
- op, msg_head.can_id);
+ if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
+ return -ENOMEM;
- /* do we need more space for the can_frames? */
- if (msg_head.nframes > op->nframes) {
+ memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
- /* yes => create new array */
+ DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
+ op, msg_head->can_id);
- struct can_frame *p;
- if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
- return -ENOMEM;
+ op->can_id = msg_head->can_id;
- kfree (op->frames);
- op->frames = p;
- }
+ /* create array for can_frames and copy the data */
+ if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+ kfree(op);
+ return -ENOMEM;
+ }
- /* update can_frames content */
- for (i = 0; i < msg_head.nframes; i++) {
- memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
- if (msg_head.flags & TX_CP_CAN_ID)
- op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
+ for (i = 0; i < msg_head->nframes; i++) {
+ if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0) {
+ kfree(op->frames);
+ kfree(op);
+ return err;
}
+ if (msg_head->flags & TX_CP_CAN_ID)
+ op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
}
- if (op->nframes != msg_head.nframes) {
- op->nframes = msg_head.nframes;
- op->currframe = 0; /* start multiple frame transmission with index 0 */
- }
+ op->last_frames = NULL; /* tx_ops never compare with previous received messages */
- /* check flags */
+ op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
+ op->ifindex = ifindex;
- op->flags = msg_head.flags;
+ init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
+ init_timer(&op->thrtimer); /* currently unused in tx_ops */
- if (op->flags & TX_RESET_MULTI_IDX)
- op->currframe = 0; /* start multiple frame transmission with index 0 */
+ op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
+ op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
- if (op->flags & SETTIMER) {
+ /* add this bcm_op to the list of the tx_ops */
+ list_add(&op->list, &bo->tx_ops);
- /* set timer values */
+ } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
- op->count = msg_head.count;
- op->ival1 = msg_head.ival1;
- op->ival2 = msg_head.ival2;
- op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
- op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
+ if (op->nframes != msg_head->nframes) {
+ op->nframes = msg_head->nframes;
+ op->currframe = 0; /* start multiple frame transmission with index 0 */
+ }
- DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
- op->count, op->j_ival1, op->j_ival2);
+ /* check flags */
- /* disable an active timer due to zero values? */
- if (!op->j_ival1 && !op->j_ival2) {
- del_timer(&op->timer);
- DBG("TX_SETUP: SETTIMER disabled timer.\n");
- }
+ op->flags = msg_head->flags;
- }
+ if (op->flags & TX_RESET_MULTI_IDX)
+ op->currframe = 0; /* start multiple frame transmission with index 0 */
- if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
+ if (op->flags & SETTIMER) {
- del_timer(&op->timer);
+ /* set timer values */
- op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
- if (op->j_ival1 && (op->count > 0)){
- op->timer.expires = jiffies + op->j_ival1;
- /* op->count-- is done in bcm_tx_timeout_handler */
- DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
- op->timer.function,
- (char*) op->timer.data,
- (unsigned int) op->timer.expires);
- } else{
- op->timer.expires = jiffies + op->j_ival2;
- DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
- op->timer.function,
- (char*) op->timer.data,
- (unsigned int) op->timer.expires);
- }
+ op->count = msg_head->count;
+ op->ival1 = msg_head->ival1;
+ op->ival2 = msg_head->ival2;
+ op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
+ op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
- add_timer(&op->timer);
- }
+ DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
+ op->count, op->j_ival1, op->j_ival2);
- if (op->flags & TX_ANNOUNCE)
- bcm_can_tx(op);
+ /* disable an active timer due to zero values? */
+ if (!op->j_ival1 && !op->j_ival2) {
+ del_timer(&op->timer);
+ DBG("TX_SETUP: SETTIMER disabled timer.\n");
+ }
+ }
- rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
+ if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
- break; /* TX_SETUP */
+ del_timer(&op->timer);
- case TX_DELETE:
+ op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
+ if (op->j_ival1 && (op->count > 0)){
+ op->timer.expires = jiffies + op->j_ival1;
+ /* op->count-- is done in bcm_tx_timeout_handler */
+ DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
+ op->timer.function,
+ (char*) op->timer.data,
+ (unsigned int) op->timer.expires);
+ } else{
+ op->timer.expires = jiffies + op->j_ival2;
+ DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
+ op->timer.function,
+ (char*) op->timer.data,
+ (unsigned int) op->timer.expires);
+ }
- bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id);
+ add_timer(&op->timer);
+ }
- rbytes = sizeof(struct bcm_msg_head);
+ if (op->flags & TX_ANNOUNCE)
+ bcm_can_tx(op);
- break; /* TX_DELETE */
+ return msg_head->nframes * CFSIZ + MHSIZ;
+}
- case TX_READ:
+static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+ int ifindex, struct sock *sk)
+{
+ struct bcm_opt *bo = bcm_sk(sk);
+ struct bcm_op *op;
+ int do_rx_register;
+ int err;
- /* reuse msg_head for the reply */
- msg_head.opcode = TX_STATUS; /* reply to TX_READ */
- op = bcm_find_op(&bo->tx_ops, msg_head.can_id);
- c = 'T'; /* for nice debug output ... */
+ if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
+ /* be robust against wrong usage ... */
+ msg_head->flags |= RX_FILTER_ID;
+ msg_head->nframes = 0; /* ignore trailing garbage */
+ }
- goto TRX_READ;
+ if ((msg_head->flags & RX_RTR_FRAME) &&
+ ((msg_head->nframes != 1) || (!(msg_head->can_id & CAN_RTR_FLAG)))) {
- case RX_READ:
+ DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
+ return -EINVAL;
+ }
- /* reuse msg_head for the reply */
- msg_head.opcode = RX_STATUS; /* reply to RX_READ */
- op = bcm_find_op(&bo->rx_ops, msg_head.can_id);
- c = 'R'; /* for nice debug output ... */
+ /* check the given can_id */
- TRX_READ:
+ if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
- /* check the given can_id */
+ /* update existing BCM operation */
- if (!op) {
- DBG("%cX_READ: did not find op for can_id <%03X>\n",
- c, msg_head.can_id);
+ DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
+ op, msg_head->can_id);
- msg_head.flags |= CMD_ERROR;
- msg_head.nframes = 0;
- bcm_send_to_user(sk, &msg_head, NULL, NULL);
- }
- else {
- DBG("%cX_READ: sending status for can_id <%03X>\n",
- c, msg_head.can_id);
+ /* Do we need more space for the can_frames than currently */
+ /* allocated? -> This is a _really_ unusual use-case and */
+ /* therefore (complexity / locking) it is not supported. */
+ if (msg_head->nframes > op->nframes)
+ return -E2BIG;
- /* put current values into msg_head */
- msg_head.flags = op->flags;
- msg_head.count = op->count;
- msg_head.ival1 = op->ival1;
- msg_head.ival2 = op->ival2;
- msg_head.nframes = op->nframes;
+ if (msg_head->nframes) {
+ /* update can_frames content */
+ if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ) < 0))
+ return err;
- bcm_send_to_user(sk, &msg_head, op->frames, NULL);
+ /* clear received can_frames to indicate 'nothing received' */
+ memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
}
- rbytes = sizeof(struct bcm_msg_head);
+ op->nframes = msg_head->nframes;
+ /* Only an update -> do not call can_rx_register() at end of RX_SETUP */
+ do_rx_register = 0;
- break; /* [T|R]X_READ */
+ } else {
+ /* insert new BCM operation for the given can_id */
- case TX_SEND:
- {
- struct sk_buff *skb;
- struct net_device *dev;
+ if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
+ return -ENOMEM;
- /* just copy and send one can_frame */
+ memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
- if (msg_head.nframes < 1) /* we need at least one can_frame */
- return -EINVAL;
+ DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
+ op, msg_head->can_id);
- skb = alloc_skb(sizeof(struct can_frame), GFP_KERNEL);
+ op->can_id = msg_head->can_id;
+ op->nframes = msg_head->nframes;
- if (!skb)
- return -ENOMEM;
+ if (msg_head->nframes) {
- if ((err = memcpy_fromiovec(skb_put(skb, sizeof(struct can_frame)), msg->msg_iov, sizeof(struct can_frame))) < 0) {
- kfree_skb(skb);
- return err;
- }
+ /* create array for can_frames and copy the data */
+ if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+ kfree(op);
+ return -ENOMEM;
+ }
- DBG_FRAME("BCM: TX_SEND: sending frame",
- (struct can_frame *)skb->data);
- dev = dev_get_by_index(sk->sk_bound_dev_if);
+ if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ)) < 0) {
+ kfree(op->frames);
+ kfree(op);
+ return err;
+ }
- if (dev) {
- skb->dev = dev;
- skb->sk = sk;
- can_send(skb, 1); /* send with loopback */
- dev_put(dev);
- }
+ /* create array for received can_frames */
+ if (!(op->last_frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+ kfree(op->frames);
+ kfree(op);
+ return -ENOMEM;
+ }
- rbytes = sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
- }
- break;
+ /* clear received can_frames to indicate 'nothing received' */
+ memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
+ } else {
+ /* op->frames = NULL due to memset */
- case RX_SETUP:
+ /* even when we have the RX_FILTER_ID case, we need to store the last frame */
+ /* for the throttle functionality */
- if ((msg_head.flags & RX_FILTER_ID) || (!(msg_head.nframes))) {
- /* be robust against wrong usage ... */
- msg_head.flags |= RX_FILTER_ID;
- msg_head.nframes = 0; /* ignore trailing garbage */
- }
+ /* create array for received can_frames */
+ if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
+ kfree(op);
+ return -ENOMEM;
+ }
- if ((msg_head.flags & RX_RTR_FRAME) &&
- ((msg_head.nframes != 1) || (!(msg_head.can_id & CAN_RTR_FLAG)))) {
+ /* clear received can_frames to indicate 'nothing received' */
+ memset(op->last_frames, 0, CFSIZ);
+ }
- DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
+ op->sk = sk; /* bcm_delete_rx_op() needs this */
+ op->ifindex = ifindex;
- msg_head.flags |= CMD_ERROR; /* return msg_head back to sender */
- msg_head.nframes = 0;
- bcm_send_to_user(sk, &msg_head, NULL, NULL);
+ init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
+ init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
- rbytes = sizeof(struct bcm_msg_head);
+ op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
+ op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
- break;
- }
+ op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
+ op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
+ op->thrtimer.expires = 0; /* mark disabled timer */
- /* check the given can_id */
+ /* add this bcm_op to the list of the tx_ops */
+ list_add(&op->list, &bo->rx_ops);
- if (!(op = bcm_find_op(&bo->rx_ops, msg_head.can_id))) {
+ do_rx_register = 1; /* call can_rx_register() at end of RX_SETUP */
- /* insert new BCM operation for the given can_id */
+ } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
- if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
- return -ENOMEM;
- memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
+ /* check flags */
- DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
- op, msg_head.can_id);
+ op->flags = msg_head->flags;
- op->can_id = msg_head.can_id;
- op->nframes = msg_head.nframes;
+ if (op->flags & RX_RTR_FRAME) {
- if (op->nframes) {
+ /* no timers in RTR-mode */
+ del_timer(&op->thrtimer);
+ del_timer(&op->timer);
- /* create array for can_frames and copy the data */
- if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
- kfree(op);
- return -ENOMEM;
- }
+ /* funny feature in RX(!)_SETUP only for RTR-mode: */
+ /* copy can_id into frame BUT without RTR-flag to */
+ /* prevent a full-load-loopback-test ... ;-] */
+ if ((op->flags & TX_CP_CAN_ID) ||
+ (op->frames[0].can_id == op->can_id))
+ op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
- for (i = 0; i < msg_head.nframes; i++)
- if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame))) < 0) {
- kfree(op->frames);
- kfree(op);
- return err;
- }
-
- /* create array for received can_frames */
- if (!(op->last_frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
- kfree(op->frames);
- kfree(op);
- return -ENOMEM;
- }
+ } else {
+ if (op->flags & SETTIMER) {
- /* clear received can_frames to indicate 'nothing received' */
- memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
- } else {
- /* op->frames = NULL due to memset */
+ /* set timer value */
- /* even when we have the RX_FILTER_ID case, we need to store the last frame */
- /* for the throttle functionality */
+ op->ival1 = msg_head->ival1;
+ op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
+ op->ival2 = msg_head->ival2;
+ op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
- /* create array for received can_frames */
- if (!(op->last_frames = kmalloc(sizeof(struct can_frame), GFP_KERNEL))) {
- kfree(op);
- return -ENOMEM;
- }
+ DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
+ op->j_ival1, op->j_ival2);
- /* clear received can_frames to indicate 'nothing received' */
- memset(op->last_frames, 0, sizeof(struct can_frame));
+ /* disable an active timer due to zero value? */
+ if (!op->j_ival1) {
+ del_timer(&op->timer);
+ DBG("RX_SETUP: disabled timer for rx timeouts.\n");
}
- op->sk = sk; /* bcm_delete_rx_op() needs this */
+ /* free currently blocked msgs ? */
+ if (op->thrtimer.expires) { /* running throttle timer? */
+ DBG("RX_SETUP: unblocking throttled msgs.\n");
+ del_timer(&op->thrtimer);
+ op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
+ add_timer(&op->thrtimer);
+ }
+ /* if (op->j_ival2) is zero, no (new) throttling will happen */
+ /* see bcm_rx_update_and_send() and bcm_rx_thr_handler() */
+ }
- init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
- init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
+ if ((op->flags & STARTTIMER) && op->j_ival1) {
- op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
- op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
+ del_timer(&op->timer);
- op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
- op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
- op->thrtimer.expires = 0; /* mark disabled timer */
+ op->timer.expires = jiffies + op->j_ival1;
- /* add this bcm_op to the list of the tx_ops */
- list_add(&op->list, &bo->rx_ops);
+ DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
+ (char *) op->timer.function,
+ (char *) op->timer.data,
+ (unsigned int) op->timer.expires);
- c=1; /* call can_rx_register() at end of RX_SETUP */
+ add_timer(&op->timer);
+ }
+ }
- } else {
- /* update existing BCM operation */
+ /* now we can register for can_ids, if we added a new bcm_op */
+ if (do_rx_register) {
+ DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
- DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
- op, msg_head.can_id);
+ if (ifindex) {
+ struct net_device *dev = dev_get_by_index(ifindex);
- /* do we need more space for the can_frames? */
- if (msg_head.nframes > op->nframes) {
+ if (dev) {
+ can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
+ dev_put(dev);
+ }
+ } else
+ can_rx_register(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
+ }
- /* yes => create new arrays */
+ return msg_head->nframes * CFSIZ + MHSIZ;
+}
- struct can_frame *p;
+static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
+{
+ struct sk_buff *skb;
+ struct net_device *dev;
+ int err;
- if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
- return -ENOMEM;
+ /* just copy and send one can_frame */
- if (op->frames)
- kfree (op->frames);
- op->frames = p;
+ if (!ifindex) /* we need a real device to send frames */
+ return -ENODEV;
- if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
- return -ENOMEM;
- if (op->last_frames)
- kfree (op->last_frames);
- op->last_frames = p;
- }
+ skb = alloc_skb(CFSIZ, GFP_KERNEL);
- if (msg_head.nframes) {
- /* update can_frames content */
- for (i = 0; i < msg_head.nframes; i++)
- memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
+ if (!skb)
+ return -ENOMEM;
- /* clear received can_frames to indicate 'nothing received' */
- memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
- }
+ if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ)) < 0) {
+ kfree_skb(skb);
+ return err;
+ }
- op->nframes = msg_head.nframes;
- c=0; /* do not call can_rx_register() at end of RX_SETUP */
+ DBG_FRAME("BCM: TX_SEND: sending frame",
+ (struct can_frame *)skb->data);
+ dev = dev_get_by_index(ifindex);
- } /* if (!bcm_find_op(&bo->tx_ops, msg_head.can_id)) */
+ if (!dev) {
+ kfree_skb(skb);
+ return -ENODEV;
+ }
+ skb->dev = dev;
+ skb->sk = sk;
+ can_send(skb, 1); /* send with loopback */
+ dev_put(dev);
- /* check flags */
+ return CFSIZ + MHSIZ;
+}
- op->flags = msg_head.flags;
+static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex)
+{
+ struct bcm_op *op;
+ int ret;
- if (op->flags & RX_RTR_FRAME) {
+ if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
- /* no timers in RTR-mode */
- del_timer(&op->thrtimer);
- del_timer(&op->timer);
+ DBG("TRX_READ: sending status for can_id <%03X>\n", msg_head->can_id);
+ /* put current values into msg_head */
+ msg_head->flags = op->flags;
+ msg_head->count = op->count;
+ msg_head->ival1 = op->ival1;
+ msg_head->ival2 = op->ival2;
+ msg_head->nframes = op->nframes;
- /* funny feature in RX(!)_SETUP only for RTR-mode: */
- /* copy can_id into frame BUT without RTR-flag to */
- /* prevent a full-load-loopback-test ... ;-] */
- if ((op->flags & TX_CP_CAN_ID) ||
- (op->frames[0].can_id == op->can_id))
- op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
+ bcm_send_to_user(op, msg_head, op->frames, NULL);
- } else {
- if (op->flags & SETTIMER) {
+ ret = MHSIZ;
- /* set timer value */
+ } else {
- op->ival1 = msg_head.ival1;
- op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
- op->ival2 = msg_head.ival2;
- op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
+ DBG("TRX_READ: did not find op for can_id <%03X>\n",
+ msg_head->can_id);
+ ret = -EINVAL;
+ }
- DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
- op->j_ival1, op->j_ival2);
+ return ret;
+}
- /* disable an active timer due to zero value? */
- if (!op->j_ival1) {
- del_timer(&op->timer);
- DBG("RX_SETUP: disabled timer for rx timeouts.\n");
- }
+/**************************************************/
+/* procfs functions */
+/**************************************************/
- /* free currently blocked msgs ? */
- if (op->thrtimer.expires) { /* running throttle timer? */
- DBG("RX_SETUP: unblocking throttled msgs.\n");
- del_timer(&op->thrtimer);
- op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
- add_timer(&op->thrtimer);
- }
- /* if (op->j_ival2) is zero, no (new) throttling will happen */
- /* see bcm_rx_update_and_send() and bcm_rx_thr_handler() */
- }
+static char *bcm_proc_getifname(int ifindex)
+{
+ struct net_device *dev;
- if ((op->flags & STARTTIMER) && op->j_ival1) {
+ if (!ifindex)
+ return "any";
- del_timer(&op->timer);
+ dev = __dev_get_by_index(ifindex); /* no usage counting */
+ if (dev)
+ return dev->name;
- op->timer.expires = jiffies + op->j_ival1;
+ return "???";
+}
- DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
- (char *) op->timer.function,
- (char *) op->timer.data,
- (unsigned int) op->timer.expires);
+static int bcm_read_proc(char *page, char **start, off_t off,
+ int count, int *eof, void *data)
+{
+ int len = 0;
+ struct sock *sk = (struct sock *)data;
+ struct bcm_opt *bo = bcm_sk(sk);
+ struct bcm_op *op;
- add_timer(&op->timer);
- }
- }
+ len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p", sk->sk_socket);
+ len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
+ len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
+ len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu", bo->dropped_usr_msgs);
+ len += snprintf(page + len, PAGE_SIZE - len, " / bound %s", bcm_proc_getifname(bo->ifindex));
+ len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
- /* now we can register for can_ids, if we added a new bcm_op */
- if (c) {
- struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+ list_for_each_entry(op, &bo->rx_ops, list) {
- DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
+ unsigned long reduction;
- if (dev) {
- can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
- dev_put(dev);
- }
- }
+ /* print only active entries & prevent division by zero */
+ if (!op->frames_abs)
+ continue;
- rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
+ len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X %-5s ",
+ op->can_id, bcm_proc_getifname(op->ifindex));
+ len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
+ op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
+ if (op->j_ival1)
+ len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
- break; /* RX_SETUP */
+ if (op->j_ival2)
+ len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
- case RX_DELETE:
+ len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
+ op->frames_filtered, op->frames_abs);
- bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id);
+ reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
- rbytes = sizeof(struct bcm_msg_head);
+ len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
+ (reduction == 100)?"near ":"", reduction);
- break; /* RX_DELETE */
+ if (len > PAGE_SIZE - 200) {
+ /* mark output cut off */
+ len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
+ break;
+ }
+ }
- default:
+ list_for_each_entry(op, &bo->tx_ops, list) {
- DBG("Unknown opcode %d\n", msg_head.opcode);
+ len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X %s [%d] ",
+ op->can_id, bcm_proc_getifname(op->ifindex), op->nframes);
+ if (op->j_ival1)
+ len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
- msg_head.flags |= CMD_ERROR; /* return msg_head back to sender */
- msg_head.nframes = 0;
- bcm_send_to_user(sk, &msg_head, NULL, NULL);
+ if (op->j_ival2)
+ len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
- rbytes = sizeof(struct bcm_msg_head);
+ len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
- break;
+ if (len > PAGE_SIZE - 100) {
+ /* mark output cut off */
+ len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
+ break;
+ }
}
- return rbytes;
+ len += snprintf(page + len, PAGE_SIZE - len, "\n");
+
+ *eof = 1;
+ return len;
}
-static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size, int flags)
+/**************************************************/
+/* bcm_op handling tx path */
+/**************************************************/
+
+static void bcm_can_tx(struct bcm_op *op)
{
- struct sock *sk = sock->sk;
struct sk_buff *skb;
- int error = 0;
- int noblock;
- int err;
+ struct net_device *dev;
+ struct can_frame *cf = &op->frames[op->currframe];
- DBG("socket %p, sk %p\n", sock, sk);
+ DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
- noblock = flags & MSG_DONTWAIT;
- flags &= ~MSG_DONTWAIT;
- if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
- return error;
- }
+ if (!op->ifindex)
+ return; /* no target device -> exit */
- DBG("delivering skbuff %p\n", skb);
- DBG_SKB(skb);
+ dev = dev_get_by_index(op->ifindex);
- if (skb->len < size)
- size = skb->len;
- if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
- skb_free_datagram(sk, skb);
- return err;
- }
+ if (!dev)
+ return; /* should this bcm_op remove itself here? */
- sock_recv_timestamp(msg, sk, skb);
+ skb = alloc_skb(CFSIZ,
+ in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
- DBG("freeing sock %p, skbuff %p\n", sk, skb);
- skb_free_datagram(sk, skb);
+ if (!skb)
+ goto out; /* no memory */
- return size;
+ memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
+
+ skb->dev = dev;
+ skb->sk = op->sk;
+ can_send(skb, 1); /* send with loopback */
+
+ op->currframe++;
+ op->frames_abs++; /* statistics */
+
+ /* reached last frame? */
+ if (op->currframe >= op->nframes)
+ op->currframe = 0;
+ out:
+ dev_put(dev);
}
static void bcm_tx_timeout_handler(unsigned long data)
msg_head.can_id = op->can_id;
msg_head.nframes = 0;
- bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
+ bcm_send_to_user(op, &msg_head, NULL, NULL);
}
}
}
+/**************************************************/
+/* bcm_op handling rx path */
+/**************************************************/
+
static void bcm_rx_handler(struct sk_buff *skb, void *data)
{
struct bcm_op *op = (struct bcm_op*)data;
if (skb->len == sizeof(rxframe)) {
memcpy(&rxframe, skb->data, sizeof(rxframe));
- skb_get_timestamp(skb, &op->stamp); /* save rx timestamp */
+ skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
+ op->rx_ifindex = skb->dev->ifindex; /* save originator for recvfrom() */
op->frames_abs++; /* statistics */
kfree_skb(skb);
DBG("got can_frame with can_id <%03X>\n", rxframe.can_id);
{
unsigned long nexttx = op->j_lastmsg + op->j_ival2;
- memcpy(lastdata, rxdata, sizeof(struct can_frame));
+ memcpy(lastdata, rxdata, CFSIZ);
lastdata->can_dlc |= RX_RECV; /* mark as used */
/* throttle bcm_rx_changed ? */
head.can_id = op->can_id;
head.nframes = 1;
- bcm_send_to_user(op->sk, &head, data, &op->stamp);
+ bcm_send_to_user(op, &head, data, &op->rx_stamp);
}
msg_head.can_id = op->can_id;
msg_head.nframes = 0;
- bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
+ bcm_send_to_user(op, &msg_head, NULL, NULL);
/* no restart of the timer is done here! */
/* if the user wants to be informed, when cyclic CAN-Messages come back ... */
if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
/* clear received can_frames to indicate 'nothing received' */
- memset(op->last_frames, 0, op->nframes * sizeof(struct can_frame));
+ memset(op->last_frames, 0, op->nframes * CFSIZ);
DBG("RX_ANNOUNCE_RESTART\n");
}
}
}
-static void bcm_can_tx(struct bcm_op *op)
-{
- struct sk_buff *skb;
- struct net_device *dev;
- struct can_frame *cf = &op->frames[op->currframe];
-
- DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
-
- skb = alloc_skb(sizeof(struct can_frame),
- in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
-
- if (!skb)
- return;
-
- memcpy(skb_put(skb, sizeof(struct can_frame)), cf, sizeof(struct can_frame));
-
- if (op->sk->sk_bound_dev_if) {
- dev = dev_get_by_index(op->sk->sk_bound_dev_if);
-
- if (dev) {
- skb->dev = dev;
- skb->sk = op->sk;
- can_send(skb, 1); /* send with loopback */
- dev_put(dev);
- }
- }
-
- op->currframe++;
- op->frames_abs++; /* statistics */
-
- /* reached last frame? */
- if (op->currframe >= op->nframes)
- op->currframe = 0;
-
-}
-
-static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
+static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
struct can_frame *frames, struct timeval *tv)
{
struct sk_buff *skb;
struct can_frame *firstframe;
- int datalen = head->nframes * sizeof(struct can_frame);
+ struct sock *sk = op->sk;
+ int datalen = head->nframes * CFSIZ;
+ struct sockaddr_can *addr;
int err;
- if (!sk) {
- DBG("no sk available\n");
- return;
- }
-
skb = alloc_skb(sizeof(*head) + datalen,
in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
if (!skb)
firstframe = (struct can_frame *) skb->tail; /* can_frames starting here */
if (tv)
- skb_set_timestamp(skb, tv);
+ skb_set_timestamp(skb, tv); /* restore timestamp */
+
+ addr = (struct sockaddr_can *)skb->cb;
+ memset(addr, 0, sizeof(*addr));
+ addr->can_family = AF_CAN;
+ addr->can_ifindex = op->rx_ifindex; /* restore originator for recvfrom() */
if (head->nframes){
memcpy(skb_put(skb, datalen), frames, datalen);
}
}
-static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id)
+/**************************************************/
+/* bcm_op handling: find & delete bcm_op elements */
+/**************************************************/
+
+static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex)
{
struct bcm_op *op;
list_for_each_entry(op, ops, list)
- if (op->can_id == can_id)
+ if ((op->can_id == can_id) && (op->ifindex == ifindex))
return op;
return NULL;
}
-static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id)
+static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
{
struct bcm_op *op, *n;
list_for_each_entry_safe(op, n, ops, list) {
- if (op->can_id == can_id) {
+ if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
- if (op->sk->sk_bound_dev_if) {
- struct net_device *dev = dev_get_by_index(op->sk->sk_bound_dev_if);
+ /* Don't care if we're bound or not (due to netdev problems) */
+ /* can_rx_unregister() is always a save thing to do here */
+ if (op->ifindex) {
+ struct net_device *dev = dev_get_by_index(op->ifindex);
if (dev) {
can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
dev_put(dev);
}
} else
- DBG("sock %p not bound for can_rx_unregister()\n", op->sk);
+ can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
list_del(&op->list);
bcm_remove_op(op);
- return;
+ return 1; /* done */
}
}
+
+ return 0; /* not found */
}
-static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id)
+static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
{
struct bcm_op *op, *n;
list_for_each_entry_safe(op, n, ops, list) {
- if (op->can_id == can_id) {
+ if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
DBG("removing rx_op (%p) for can_id <%03X>\n",
op, op->can_id);
list_del(&op->list);
bcm_remove_op(op);
- return;
+ return 1; /* done */
}
}
+
+ return 0; /* not found */
}
static void bcm_remove_op(struct bcm_op *op)