2 CAN: Add PF_CAN core module
4 This patch adds the CAN core functionality but no protocols or drivers.
5 No protocol implementations are included here. They come as separate
6 patches. Protocol numbers are already in include/linux/can.h.
8 Signed-Off-By: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
9 Signed-Off-By: Urs Thuermann <urs.thuermann@volkswagen.de>
12 include/linux/can.h | 98 ++++
13 include/linux/can/core.h | 88 +++
14 include/linux/can/error.h | 95 ++++
17 net/can/Kconfig | 25 +
19 net/can/af_can.c | 1070 ++++++++++++++++++++++++++++++++++++++++++++++
20 net/can/af_can.h | 120 +++++
21 net/can/proc.c | 530 ++++++++++++++++++++++
22 10 files changed, 2034 insertions(+)
24 Index: linux-2.6.22-rc3/include/linux/can.h
25 ===================================================================
26 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
27 +++ linux-2.6.22-rc3/include/linux/can.h 2007-05-29 10:14:54.%N +0200
32 + * Definitions for CAN networklayer (socket addr / CAN frame / CAN filter)
34 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
35 + * Urs Thuermann <urs.thuermann@volkswagen.de>
36 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
37 + * All rights reserved.
39 + * Send feedback to <socketcan-users@lists.berlios.de>
46 +#include <linux/types.h>
47 +#include <linux/socket.h>
49 +/* controller area network (CAN) kernel definitions */
51 +/* special address description flags for the CAN_ID */
52 +#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
53 +#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
54 +#define CAN_ERR_FLAG 0x20000000U /* error frame */
56 +/* valid bits in CAN ID for frame formats */
57 +#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
58 +#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
59 +#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
61 +typedef __u32 canid_t;
62 +typedef __u32 can_err_mask_t;
65 + * struct can_frame - basic CAN frame structure
66 + * @can_id: the CAN ID of the frame and CAN_*_FLAG flags, see above.
67 + * @can_dlc: the data length field of the CAN frame
68 + * @data: the CAN frame payload.
71 + canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
72 + __u8 can_dlc; /* data length code: 0 .. 8 */
73 + __u8 data[8] __attribute__((aligned(8)));
76 +/* particular protocols of the protocol family PF_CAN */
77 +#define CAN_RAW 1 /* RAW sockets */
78 +#define CAN_BCM 2 /* Broadcast Manager */
79 +#define CAN_TP16 3 /* VAG Transport Protocol v1.6 */
80 +#define CAN_TP20 4 /* VAG Transport Protocol v2.0 */
81 +#define CAN_MCNET 5 /* Bosch MCNet */
82 +#define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */
83 +#define CAN_BAP 7 /* VAG Bedien- und Anzeigeprotokoll */
86 +#define SOL_CAN_BASE 100
89 + * struct sockaddr_can - the sockaddr structure for CAN sockets
90 + * @can_family: address family number AF_CAN.
91 + * @can_ifindex: CAN network interface index.
92 + * @can_addr: transport protocol specific address, mostly CAN IDs.
94 +struct sockaddr_can {
95 + sa_family_t can_family;
98 + struct { canid_t rx_id, tx_id; } tp16;
99 + struct { canid_t rx_id, tx_id; } tp20;
100 + struct { canid_t rx_id, tx_id; } mcnet;
101 + struct { canid_t rx_id, tx_id; } isotp;
102 + struct { int lcu, type; } bap;
107 + * struct can_filter - CAN ID based filter in can_register().
108 + * @can_id: relevant bits of CAN ID which are not masked out.
109 + * @can_mask: CAN mask (see description)
112 + * A filter matches, when
114 + * <received_can_id> & mask == can_id & mask
116 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
117 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
124 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
127 Index: linux-2.6.22-rc3/include/linux/can/core.h
128 ===================================================================
129 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
130 +++ linux-2.6.22-rc3/include/linux/can/core.h 2007-05-29 10:14:54.%N +0200
135 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
137 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
138 + * Urs Thuermann <urs.thuermann@volkswagen.de>
139 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
140 + * All rights reserved.
142 + * Send feedback to <socketcan-users@lists.berlios.de>
149 +#include <linux/can.h>
150 +#include <linux/skbuff.h>
151 +#include <linux/netdevice.h>
153 +#define CAN_VERSION "20070523"
155 +/* increment this number each time you change some user-space interface */
156 +#define CAN_ABI_VERSION "8"
158 +#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
160 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
162 +#define CAN_PROC_DIR "net/can" /* /proc/... */
165 + * struct can_proto - CAN protocol structure
166 + * @type: type argument in socket() syscall, e.g. SOCK_DGRAM.
167 + * @protocol: protocol number in socket() syscall.
168 + * @capability: capability needed to open the socket, or -1 for no restriction.
169 + * @ops: pointer to struct proto_ops for sock->ops.
170 + * @prot: pointer to struct proto structure.
176 + struct proto_ops *ops;
177 + struct proto *prot;
180 +/* function prototypes for the CAN networklayer core (af_can.c) */
182 +extern int can_proto_register(struct can_proto *cp);
183 +extern int can_proto_unregister(struct can_proto *cp);
185 +extern int can_rx_register(struct net_device *dev, canid_t can_id,
187 + void (*func)(struct sk_buff *, void *),
188 + void *data, char *ident);
190 +extern int can_rx_unregister(struct net_device *dev, canid_t can_id,
192 + void (*func)(struct sk_buff *, void *),
195 +extern int can_dev_register(struct net_device *dev,
196 + void (*func)(unsigned long msg, void *),
199 +extern int can_dev_unregister(struct net_device *dev,
200 + void (*func)(unsigned long msg, void *),
203 +extern int can_send(struct sk_buff *skb, int loop);
205 +#ifdef CONFIG_CAN_DEBUG_CORE
206 +extern void can_debug_skb(struct sk_buff *skb);
207 +extern void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
208 +#define DBG(args...) (debug & 1 ? \
209 + (printk(KERN_DEBUG "can-%s %s: ", \
210 + IDENT, __func__), printk(args)) : 0)
211 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
212 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
214 +#define DBG(args...)
215 +#define DBG_FRAME(args...)
216 +#define DBG_SKB(skb)
219 +#endif /* CAN_CORE_H */
220 Index: linux-2.6.22-rc3/net/Kconfig
221 ===================================================================
222 --- linux-2.6.22-rc3.orig/net/Kconfig 2007-05-29 10:03:02.%N +0200
223 +++ linux-2.6.22-rc3/net/Kconfig 2007-05-29 10:14:54.%N +0200
227 source "net/ax25/Kconfig"
228 +source "net/can/Kconfig"
229 source "net/irda/Kconfig"
230 source "net/bluetooth/Kconfig"
231 source "net/rxrpc/Kconfig"
232 Index: linux-2.6.22-rc3/net/Makefile
233 ===================================================================
234 --- linux-2.6.22-rc3.orig/net/Makefile 2007-05-29 10:03:02.%N +0200
235 +++ linux-2.6.22-rc3/net/Makefile 2007-05-29 10:14:54.%N +0200
237 obj-$(CONFIG_NETROM) += netrom/
238 obj-$(CONFIG_ROSE) += rose/
239 obj-$(CONFIG_AX25) += ax25/
240 +obj-$(CONFIG_CAN) += can/
241 obj-$(CONFIG_IRDA) += irda/
242 obj-$(CONFIG_BT) += bluetooth/
243 obj-$(CONFIG_SUNRPC) += sunrpc/
244 Index: linux-2.6.22-rc3/net/can/Kconfig
245 ===================================================================
246 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
247 +++ linux-2.6.22-rc3/net/can/Kconfig 2007-05-30 12:38:26.%N +0200
250 +# Controller Area Network (CAN) network layer core configuration
255 + tristate "CAN bus subsystem support"
257 + Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
258 + communications protocol, which was developed by Bosch at
259 + 1991 mainly for automotive, but now widely used in marine
260 + (NMEA2000), industrial and medical applications.
261 + More information on the CAN network protocol family PF_CAN
262 + is contained in <Documentation/networking/can.txt>.
264 + If you want CAN support, you should say Y here and also to the
265 + specific driver for your controller(s) below.
267 +config CAN_DEBUG_CORE
268 + bool "CAN Core debugging messages"
271 + Say Y here if you want the CAN core to produce a bunch of debug
272 + messages to the system log. Select this if you are having a
273 + problem with CAN support and want to see more of what is going on.
274 Index: linux-2.6.22-rc3/net/can/Makefile
275 ===================================================================
276 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
277 +++ linux-2.6.22-rc3/net/can/Makefile 2007-05-30 12:38:26.%N +0200
280 +# Makefile for the Linux Controller Area Network core.
283 +obj-$(CONFIG_CAN) += can.o
284 +can-objs := af_can.o proc.o
285 Index: linux-2.6.22-rc3/net/can/af_can.c
286 ===================================================================
287 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
288 +++ linux-2.6.22-rc3/net/can/af_can.c 2007-05-29 10:14:54.%N +0200
291 + * af_can.c - Protocol family CAN core module
292 + * (used by different CAN protocol modules)
294 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
295 + * All rights reserved.
297 + * Redistribution and use in source and binary forms, with or without
298 + * modification, are permitted provided that the following conditions
300 + * 1. Redistributions of source code must retain the above copyright
301 + * notice, this list of conditions, the following disclaimer and
302 + * the referenced file 'COPYING'.
303 + * 2. Redistributions in binary form must reproduce the above copyright
304 + * notice, this list of conditions and the following disclaimer in the
305 + * documentation and/or other materials provided with the distribution.
306 + * 3. Neither the name of Volkswagen nor the names of its contributors
307 + * may be used to endorse or promote products derived from this software
308 + * without specific prior written permission.
310 + * Alternatively, provided that this notice is retained in full, this
311 + * software may be distributed under the terms of the GNU General
312 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
313 + * file from the main directory of the linux kernel source.
315 + * The provided data structures and external interfaces from this code
316 + * are not restricted to be used by modules with a GPL compatible license.
318 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
319 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
320 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
321 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
323 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
324 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
325 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
326 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
327 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
328 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
331 + * Send feedback to <socketcan-users@lists.berlios.de>
335 +#include <linux/module.h>
336 +#include <linux/slab.h>
337 +#include <linux/kmod.h>
338 +#include <linux/init.h>
339 +#include <linux/list.h>
340 +#include <linux/spinlock.h>
341 +#include <linux/rcupdate.h>
342 +#include <linux/socket.h>
343 +#include <linux/if_ether.h>
344 +#include <linux/if_arp.h>
345 +#include <linux/skbuff.h>
346 +#include <linux/net.h>
347 +#include <linux/netdevice.h>
348 +#include <linux/can.h>
349 +#include <linux/can/core.h>
350 +#include <net/sock.h>
351 +#include <asm/uaccess.h>
355 +#define IDENT "core"
356 +static __initdata const char banner[] =
357 + KERN_INFO "can: controller area network core # "
358 + CAN_VERSION_STRING "\n";
360 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
361 +MODULE_LICENSE("Dual BSD/GPL");
362 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
363 + "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
365 +MODULE_ALIAS_NETPROTO(PF_CAN);
367 +int stats_timer = 1; /* default: on */
368 +module_param(stats_timer, int, S_IRUGO);
369 +MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
371 +#ifdef CONFIG_CAN_DEBUG_CORE
372 +static int debug = 0;
373 +module_param(debug, int, S_IRUGO);
374 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
378 + struct list_head list;
379 + struct net_device *dev;
380 + void (*func)(unsigned long msg, void *data);
384 +static LIST_HEAD(notifier_list);
385 +static DEFINE_RWLOCK(notifier_lock);
387 +HLIST_HEAD(rx_dev_list);
388 +static struct dev_rcv_lists rx_alldev_list;
389 +static DEFINE_SPINLOCK(rcv_lists_lock);
391 +static struct kmem_cache *rcv_cache __read_mostly;
393 +/* table of registered CAN protocols */
394 +static struct can_proto *proto_tab[CAN_NPROTO];
396 +struct timer_list stattimer; /* timer for statistics update */
397 +struct s_stats stats; /* packet statistics */
398 +struct s_pstats pstats; /* receive list statistics */
401 + * af_can socket functions
404 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
406 + struct sock *sk = sock->sk;
411 + return sock_get_timestamp(sk, (struct timeval __user *)arg);
414 + return -ENOIOCTLCMD;
418 +static void can_sock_destruct(struct sock *sk)
420 + DBG("called for sock %p\n", sk);
422 + skb_queue_purge(&sk->sk_receive_queue);
423 + if (sk->sk_protinfo)
424 + kfree(sk->sk_protinfo);
427 +static int can_create(struct socket *sock, int protocol)
430 + struct can_proto *cp;
431 + char module_name[sizeof("can-proto-000")];
434 + DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
436 + sock->state = SS_UNCONNECTED;
438 + if (protocol < 0 || protocol >= CAN_NPROTO)
441 + DBG("looking up proto %d in proto_tab[]\n", protocol);
443 + /* try to load protocol module, when CONFIG_KMOD is defined */
444 + if (!proto_tab[protocol]) {
445 + sprintf(module_name, "can-proto-%d", protocol);
446 + ret = request_module(module_name);
448 + /* In case of error we only print a message but don't
449 + * return the error code immediately. Below we will
450 + * return -EPROTONOSUPPORT
452 + if (ret == -ENOSYS)
453 + printk(KERN_INFO "can: request_module(%s) not"
454 + " implemented.\n", module_name);
456 + printk(KERN_ERR "can: request_module(%s) failed\n",
460 + /* check for success and correct type */
461 + cp = proto_tab[protocol];
462 + if (!cp || cp->type != sock->type)
463 + return -EPROTONOSUPPORT;
465 + if (cp->capability >= 0 && !capable(cp->capability))
468 + sock->ops = cp->ops;
470 + sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
474 + sock_init_data(sock, sk);
475 + sk->sk_destruct = can_sock_destruct;
477 + DBG("created sock: %p\n", sk);
479 + if (sk->sk_prot->init)
480 + ret = sk->sk_prot->init(sk);
483 + /* release sk on errors */
496 + * can_send - transmit a CAN frame (optional with local loopback)
497 + * @skb: pointer to socket buffer with CAN frame in data section
498 + * @loop: loopback for listeners on local CAN sockets (recommended default!)
502 + * -ENETDOWN when the selected interface is down
503 + * -ENOBUFS on full driver queue (see net_xmit_errno())
505 +int can_send(struct sk_buff *skb, int loop)
507 + struct sock **tx_sk = (struct sock **)skb->cb;
510 + if (skb->dev->type != ARPHRD_CAN) {
516 + /* local loopback of sent CAN frames (default) */
518 + /* indication for the CAN driver: do loopback */
522 + * The reference to the originating sock may be also required
523 + * by the receiving socket to indicate (and ignore) his own
524 + * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
527 + /* interface not capabable to do the loopback itself? */
528 + if (!(skb->dev->flags & IFF_LOOPBACK)) {
529 + struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
531 + /* perform the local loopback here */
532 + newskb->protocol = htons(ETH_P_CAN);
533 + newskb->ip_summed = CHECKSUM_UNNECESSARY;
537 + /* indication for the CAN driver: no loopback required */
541 + if (!(skb->dev->flags & IFF_UP))
544 + /* send to netdevice */
545 + err = dev_queue_xmit(skb);
547 + err = net_xmit_errno(err);
549 + /* update statistics */
551 + stats.tx_frames_delta++;
555 +EXPORT_SYMBOL(can_send);
561 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
563 + struct dev_rcv_lists *d;
564 + struct hlist_node *n;
567 + * find receive list for this device
569 + * The hlist_for_each_entry*() macros curse through the list
570 + * using the pointer variable n and set d to the containing
571 + * struct in each list iteration. Therefore, after list
572 + * iteration, d is unmodified when the list is empty, and it
573 + * points to last list element, when the list is non-empty
574 + * but no match in the loop body is found. I.e. d is *not*
575 + * NULL when no match is found. We can, however, use the
576 + * cursor variable n to decide if a match was found.
579 + hlist_for_each_entry(d, n, &rx_dev_list, list) {
584 + return n ? d : NULL;
587 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
588 + struct dev_rcv_lists *d)
590 + canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
592 + /* filter error frames */
593 + if (*mask & CAN_ERR_FLAG) {
594 + /* clear CAN_ERR_FLAG in list entry */
595 + *mask &= CAN_ERR_MASK;
596 + return &d->rx[RX_ERR];
599 + /* ensure valid values in can_mask */
600 + if (*mask & CAN_EFF_FLAG)
601 + *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
603 + *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
605 + /* reduce condition testing at receive time */
608 + /* inverse can_id/can_mask filter */
610 + return &d->rx[RX_INV];
612 + /* mask == 0 => no condition testing at receive time */
614 + return &d->rx[RX_ALL];
616 + /* use extra filterset for the subscription of exactly *ONE* can_id */
617 + if (*can_id & CAN_EFF_FLAG) {
618 + if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
619 + /* RFC: a use-case for hash-tables in the future? */
620 + return &d->rx[RX_EFF];
623 + if (*mask == CAN_SFF_MASK)
624 + return &d->rx_sff[*can_id];
627 + /* default: filter via can_id/can_mask */
628 + return &d->rx[RX_FIL];
632 + * can_rx_register - subscribe CAN frames from a specific interface
633 + * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
634 + * @can_id: CAN identifier (see description)
635 + * @mask: CAN mask (see description)
636 + * @func: callback function on filter match
637 + * @data: returned parameter for callback function
638 + * @ident: string for calling module indentification
641 + * Invokes the callback function with the received sk_buff and the given
642 + * parameter 'data' on a matching receive filter. A filter matches, when
644 + * <received_can_id> & mask == can_id & mask
646 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
647 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
651 + * -ENOMEM on missing cache mem to create subscription entry
652 + * -ENODEV unknown device
654 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
655 + void (*func)(struct sk_buff *, void *), void *data,
658 + struct receiver *r;
659 + struct hlist_head *rl;
660 + struct dev_rcv_lists *d;
663 + /* insert new receiver (dev,canid,mask) -> (func,data) */
665 + DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
666 + dev, can_id, mask, func, data, ident);
668 + r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
672 + spin_lock_bh(&rcv_lists_lock);
674 + d = find_dev_rcv_lists(dev);
676 + rl = find_rcv_list(&can_id, &mask, d);
678 + r->can_id = can_id;
685 + hlist_add_head_rcu(&r->list, rl);
688 + pstats.rcv_entries++;
689 + if (pstats.rcv_entries_max < pstats.rcv_entries)
690 + pstats.rcv_entries_max = pstats.rcv_entries;
692 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
693 + DNAME(dev), can_id, mask);
694 + kmem_cache_free(rcv_cache, r);
698 + spin_unlock_bh(&rcv_lists_lock);
702 +EXPORT_SYMBOL(can_rx_register);
704 +static void can_rx_delete_list(struct hlist_head *rl)
706 + struct receiver *r;
707 + struct hlist_node *n;
709 + hlist_for_each_entry_rcu(r, n, rl, list) {
710 + hlist_del_rcu(&r->list);
711 + kmem_cache_free(rcv_cache, r);
716 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
718 +static void can_rx_delete_device(struct rcu_head *rp)
720 + struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
723 + /* remove all receivers hooked at this netdevice */
724 + can_rx_delete_list(&d->rx[RX_ERR]);
725 + can_rx_delete_list(&d->rx[RX_ALL]);
726 + can_rx_delete_list(&d->rx[RX_FIL]);
727 + can_rx_delete_list(&d->rx[RX_INV]);
728 + can_rx_delete_list(&d->rx[RX_EFF]);
730 + for (i = 0; i < 2048; i++)
731 + can_rx_delete_list(&d->rx_sff[i]);
737 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
739 +static void can_rx_delete_receiver(struct rcu_head *rp)
741 + struct receiver *r = container_of(rp, struct receiver, rcu);
743 + kmem_cache_free(rcv_cache, r);
747 + * can_rx_unregister - unsubscribe CAN frames from a specific interface
748 + * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
749 + * @can_id: CAN identifier
751 + * @func: callback function on filter match
752 + * @data: returned parameter for callback function
755 + * Removes subscription entry depending on given (subscription) values.
759 + * -EINVAL on missing subscription entry
760 + * -ENODEV unknown device
762 +int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
763 + void (*func)(struct sk_buff *, void *), void *data)
765 + struct receiver *r = NULL;
766 + struct hlist_head *rl;
767 + struct hlist_node *next;
768 + struct dev_rcv_lists *d;
771 + DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
772 + dev, can_id, mask, func, data);
774 + spin_lock_bh(&rcv_lists_lock);
776 + d = find_dev_rcv_lists(dev);
778 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
779 + DNAME(dev), can_id, mask);
784 + rl = find_rcv_list(&can_id, &mask, d);
787 + * Search the receiver list for the item to delete. This should
788 + * exist, since no receiver may be unregistered that hasn't
789 + * been registered before.
792 + hlist_for_each_entry(r, next, rl, list) {
793 + if (r->can_id == can_id && r->mask == mask
794 + && r->func == func && r->data == data)
799 + * Check for bug in CAN protocol implementations:
800 + * If no matching list item was found, the list cursor variable next
801 + * will be NULL, while r will point to the last item of the list.
805 + DBG("receive list entry not found for "
806 + "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
812 + hlist_del_rcu(&r->list);
815 + if (pstats.rcv_entries > 0)
816 + pstats.rcv_entries--;
819 + spin_unlock_bh(&rcv_lists_lock);
821 + /* schedule the receiver item for deletion */
823 + call_rcu(&r->rcu, can_rx_delete_receiver);
827 +EXPORT_SYMBOL(can_rx_unregister);
829 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
831 + struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
833 + DBG("skbuff %p cloned to %p\n", skb, clone);
835 + r->func(clone, r->data);
840 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
842 + struct receiver *r;
843 + struct hlist_node *n;
845 + struct can_frame *cf = (struct can_frame*)skb->data;
846 + canid_t can_id = cf->can_id;
848 + if (d->entries == 0)
851 + if (can_id & CAN_ERR_FLAG) {
852 + /* check for error frame entries only */
853 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
854 + if (can_id & r->mask) {
855 + DBG("match on rx_err skbuff %p\n", skb);
863 + /* check for unfiltered entries */
864 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
865 + DBG("match on rx_all skbuff %p\n", skb);
870 + /* check for can_id/mask entries */
871 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
872 + if ((can_id & r->mask) == r->can_id) {
873 + DBG("match on rx_fil skbuff %p\n", skb);
879 + /* check for inverted can_id/mask entries */
880 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
881 + if ((can_id & r->mask) != r->can_id) {
882 + DBG("match on rx_inv skbuff %p\n", skb);
888 + /* check CAN_ID specific entries */
889 + if (can_id & CAN_EFF_FLAG) {
890 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
891 + if (r->can_id == can_id) {
892 + DBG("match on rx_eff skbuff %p\n", skb);
898 + can_id &= CAN_SFF_MASK;
899 + hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
900 + DBG("match on rx_sff skbuff %p\n", skb);
909 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
910 + struct packet_type *pt, struct net_device *orig_dev)
912 + struct dev_rcv_lists *d;
915 + DBG("received skbuff on device %s, ptype %04x\n",
916 + dev->name, ntohs(pt->type));
918 + DBG_FRAME("af_can: can_rcv: received CAN frame",
919 + (struct can_frame *)skb->data);
921 + if (dev->type != ARPHRD_CAN) {
926 + /* update statistics */
928 + stats.rx_frames_delta++;
932 + /* deliver the packet to sockets listening on all devices */
933 + matches = can_rcv_filter(&rx_alldev_list, skb);
935 + /* find receive list for this device */
936 + d = find_dev_rcv_lists(dev);
938 + matches += can_rcv_filter(d, skb);
942 + /* free the skbuff allocated by the netdevice driver */
943 + DBG("freeing skbuff %p\n", skb);
948 + stats.matches_delta++;
955 + * af_can debugging stuff
958 +#ifdef CONFIG_CAN_DEBUG_CORE
960 +#define DBG_BSIZE 1024
963 + * can_debug_cframe - print CAN frame
964 + * @msg: pointer to message printed before the given CAN frame
965 + * @cf: pointer to CAN frame
967 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
974 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
978 + len = sprintf(buf, KERN_DEBUG);
980 + len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
989 + if (cf->can_id & CAN_EFF_FLAG)
990 + len += sprintf(buf + len, "<%08X> [%X] ",
991 + cf->can_id & CAN_EFF_MASK, dlc);
993 + len += sprintf(buf + len, "<%03X> [%X] ",
994 + cf->can_id & CAN_SFF_MASK, dlc);
996 + for (i = 0; i < dlc; i++)
997 + len += sprintf(buf + len, "%02X ", cf->data[i]);
999 + if (cf->can_id & CAN_RTR_FLAG)
1000 + len += sprintf(buf + len, "(RTR)");
1002 + buf[len++] = '\n';
1007 +EXPORT_SYMBOL(can_debug_cframe);
1010 + * can_debug_skb - print socket buffer content to kernel log
1011 + * @skb: pointer to socket buffer
1013 +void can_debug_skb(struct sk_buff *skb)
1015 + int len, nbytes, i;
1018 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1022 + len = sprintf(buf,
1023 + KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
1024 + KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
1025 + "h,d,t,e,l: %p %+d %+d %+d, %d",
1026 + skb, skb->dev ? skb->dev->ifindex : -1,
1027 + ntohs(skb->protocol),
1028 + atomic_read(&skb->users),
1029 + atomic_read(&(skb_shinfo(skb)->dataref)),
1030 + skb_shinfo(skb)->nr_frags,
1031 + skb->head, skb->data - skb->head,
1032 + skb->tail - skb->head, skb->end - skb->head, skb->len);
1033 + nbytes = skb->end - skb->head;
1034 + for (i = 0; i < nbytes; i++) {
1036 + len += sprintf(buf + len, "\n" KERN_DEBUG " ");
1037 + if (len < DBG_BSIZE - 16) {
1038 + len += sprintf(buf + len, " %02x", skb->head[i]);
1040 + len += sprintf(buf + len, "...");
1044 + buf[len++] = '\n';
1049 +EXPORT_SYMBOL(can_debug_skb);
1054 + * af_can protocol functions
1058 + * can_proto_register - register CAN transport protocol
1059 + * @cp: pointer to CAN protocol structure
1063 + * -EINVAL invalid (out of range) protocol number
1064 + * -EBUSY protocol already in use
1065 + * -ENOBUF if proto_register() fails
1067 +int can_proto_register(struct can_proto *cp)
1069 + int proto = cp->protocol;
1072 + if (proto < 0 || proto >= CAN_NPROTO) {
1073 + printk(KERN_ERR "can: protocol number %d out "
1074 + "of range\n", proto);
1077 + if (proto_tab[proto]) {
1078 + printk(KERN_ERR "can: protocol %d already "
1079 + "registered\n", proto);
1083 + err = proto_register(cp->prot, 0);
1087 + proto_tab[proto] = cp;
1089 + /* use generic ioctl function if the module doesn't bring its own */
1090 + if (!cp->ops->ioctl)
1091 + cp->ops->ioctl = can_ioctl;
1095 +EXPORT_SYMBOL(can_proto_register);
1098 + * can_proto_unregister - unregister CAN transport protocol
1099 + * @cp: pointer to CAN protocol structure
1103 + * -ESRCH protocol number was not registered
1105 +int can_proto_unregister(struct can_proto *cp)
1107 + int proto = cp->protocol;
1109 + if (!proto_tab[proto]) {
1110 + printk(KERN_ERR "can: protocol %d is not registered\n", proto);
1113 + proto_unregister(cp->prot);
1114 + proto_tab[proto] = NULL;
1118 +EXPORT_SYMBOL(can_proto_unregister);
1121 + * can_dev_register - subscribe notifier for CAN device status changes
1122 + * @dev: pointer to netdevice
1123 + * @func: callback function on status change
1124 + * @data: returned parameter for callback function
1127 + * Invokes the callback function with the status 'msg' and the given
1128 + * parameter 'data' on a status change of the given CAN network device.
1132 + * -ENOMEM on missing mem to create subscription entry
1133 + * -ENODEV unknown device
1135 +int can_dev_register(struct net_device *dev,
1136 + void (*func)(unsigned long msg, void *), void *data)
1138 + struct notifier *n;
1140 + DBG("called for %s\n", dev->name);
1142 + if (!dev || dev->type != ARPHRD_CAN)
1145 + n = kmalloc(sizeof(*n), GFP_KERNEL);
1153 + write_lock(¬ifier_lock);
1154 + list_add(&n->list, ¬ifier_list);
1155 + write_unlock(¬ifier_lock);
1159 +EXPORT_SYMBOL(can_dev_register);
1162 + * can_dev_unregister - unsubscribe notifier for CAN device status changes
1163 + * @dev: pointer to netdevice
1164 + * @func: callback function on filter match
1165 + * @data: returned parameter for callback function
1168 + * Removes subscription entry depending on given (subscription) values.
1172 + * -EINVAL on missing subscription entry
1174 +int can_dev_unregister(struct net_device *dev,
1175 + void (*func)(unsigned long msg, void *), void *data)
1177 + struct notifier *n, *next;
1178 + int ret = -EINVAL;
1180 + DBG("called for %s\n", dev->name);
1182 + write_lock(¬ifier_lock);
1183 + list_for_each_entry_safe(n, next, ¬ifier_list, list) {
1184 + if (n->dev == dev && n->func == func && n->data == data) {
1185 + list_del(&n->list);
1191 + write_unlock(¬ifier_lock);
1195 +EXPORT_SYMBOL(can_dev_unregister);
1197 +static int can_notifier(struct notifier_block *nb,
1198 + unsigned long msg, void *data)
1200 + struct net_device *dev = (struct net_device *)data;
1201 + struct notifier *n;
1202 + struct dev_rcv_lists *d;
1204 + DBG("called for %s, msg = %lu\n", dev->name, msg);
1206 + if (dev->type != ARPHRD_CAN)
1207 + return NOTIFY_DONE;
1211 + case NETDEV_REGISTER:
1214 + * create new dev_rcv_lists for this device
1216 + * N.B. zeroing the struct is the correct initialization
1217 + * for the embedded hlist_head structs.
1218 + * Another list type, e.g. list_head, would require
1219 + * explicit initialization.
1222 + DBG("creating new dev_rcv_lists for %s\n", dev->name);
1224 + d = kzalloc(sizeof(*d),
1225 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1227 + printk(KERN_ERR "can: allocation of receive "
1229 + return NOTIFY_DONE;
1233 + spin_lock_bh(&rcv_lists_lock);
1234 + hlist_add_head_rcu(&d->list, &rx_dev_list);
1235 + spin_unlock_bh(&rcv_lists_lock);
1239 + case NETDEV_UNREGISTER:
1240 + spin_lock_bh(&rcv_lists_lock);
1242 + d = find_dev_rcv_lists(dev);
1244 + hlist_del_rcu(&d->list);
1246 + printk(KERN_ERR "can: notifier: receive list not "
1247 + "found for dev %s\n", dev->name);
1249 + spin_unlock_bh(&rcv_lists_lock);
1252 + call_rcu(&d->rcu, can_rx_delete_device);
1257 + read_lock(¬ifier_lock);
1258 + list_for_each_entry(n, ¬ifier_list, list) {
1259 + if (n->dev == dev)
1260 + n->func(msg, n->data);
1262 + read_unlock(¬ifier_lock);
1264 + return NOTIFY_DONE;
1268 + * af_can module init/exit functions
1271 +static struct packet_type can_packet = {
1272 + .type = __constant_htons(ETH_P_CAN),
1277 +static struct net_proto_family can_family_ops = {
1279 + .create = can_create,
1280 + .owner = THIS_MODULE,
1283 +/* notifier block for netdevice event */
1284 +static struct notifier_block can_netdev_notifier = {
1285 + .notifier_call = can_notifier,
1288 +static __init int can_init(void)
1292 + rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1293 + 0, 0, NULL, NULL);
1298 + * Insert struct dev_rcv_lists for reception on all devices.
1299 + * This struct is zero initialized which is correct for the
1300 + * embedded hlist heads, the dev pointer, and the entries counter.
1303 + spin_lock_bh(&rcv_lists_lock);
1304 + hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1305 + spin_unlock_bh(&rcv_lists_lock);
1307 + if (stats_timer) {
1308 + /* the statistics are updated every second (timer triggered) */
1309 + init_timer(&stattimer);
1310 + stattimer.function = can_stat_update;
1311 + stattimer.data = 0;
1312 + /* update every second */
1313 + stattimer.expires = jiffies + HZ;
1314 + /* start statistics timer */
1315 + add_timer(&stattimer);
1317 + stattimer.function = NULL;
1322 + /* protocol register */
1323 + sock_register(&can_family_ops);
1324 + register_netdevice_notifier(&can_netdev_notifier);
1325 + dev_add_pack(&can_packet);
1330 +static __exit void can_exit(void)
1332 + struct dev_rcv_lists *d;
1333 + struct hlist_node *n, *next;
1336 + del_timer(&stattimer);
1338 + /* procfs remove */
1339 + can_remove_proc();
1341 + /* protocol unregister */
1342 + dev_remove_pack(&can_packet);
1343 + unregister_netdevice_notifier(&can_netdev_notifier);
1344 + sock_unregister(PF_CAN);
1346 + /* remove rx_dev_list */
1347 + spin_lock_bh(&rcv_lists_lock);
1348 + hlist_del(&rx_alldev_list.list);
1349 + hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1350 + hlist_del(&d->list);
1353 + spin_unlock_bh(&rcv_lists_lock);
1355 + kmem_cache_destroy(rcv_cache);
1358 +module_init(can_init);
1359 +module_exit(can_exit);
1360 Index: linux-2.6.22-rc3/net/can/af_can.h
1361 ===================================================================
1362 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1363 +++ linux-2.6.22-rc3/net/can/af_can.h 2007-05-29 10:14:54.%N +0200
1366 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1367 + * All rights reserved.
1369 + * Redistribution and use in source and binary forms, with or without
1370 + * modification, are permitted provided that the following conditions
1372 + * 1. Redistributions of source code must retain the above copyright
1373 + * notice, this list of conditions, the following disclaimer and
1374 + * the referenced file 'COPYING'.
1375 + * 2. Redistributions in binary form must reproduce the above copyright
1376 + * notice, this list of conditions and the following disclaimer in the
1377 + * documentation and/or other materials provided with the distribution.
1378 + * 3. Neither the name of Volkswagen nor the names of its contributors
1379 + * may be used to endorse or promote products derived from this software
1380 + * without specific prior written permission.
1382 + * Alternatively, provided that this notice is retained in full, this
1383 + * software may be distributed under the terms of the GNU General
1384 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1385 + * file from the main directory of the linux kernel source.
1387 + * The provided data structures and external interfaces from this code
1388 + * are not restricted to be used by modules with a GPL compatible license.
1390 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1391 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1392 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1393 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1394 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1395 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1396 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1397 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1398 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1399 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1400 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1403 + * Send feedback to <socketcan-users@lists.berlios.de>
1410 +#include <linux/skbuff.h>
1411 +#include <linux/netdevice.h>
1412 +#include <linux/list.h>
1413 +#include <linux/rcupdate.h>
1414 +#include <linux/can.h>
1416 +/* af_can rx dispatcher structures */
1419 + struct hlist_node list;
1420 + struct rcu_head rcu;
1423 + unsigned long matches;
1424 + void (*func)(struct sk_buff *, void *);
1429 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1431 +struct dev_rcv_lists {
1432 + struct hlist_node list;
1433 + struct rcu_head rcu;
1434 + struct net_device *dev;
1435 + struct hlist_head rx[RX_MAX];
1436 + struct hlist_head rx_sff[0x800];
1440 +/* statistic structures */
1443 + unsigned long jiffies_init;
1445 + unsigned long rx_frames;
1446 + unsigned long tx_frames;
1447 + unsigned long matches;
1449 + unsigned long total_rx_rate;
1450 + unsigned long total_tx_rate;
1451 + unsigned long total_rx_match_ratio;
1453 + unsigned long current_rx_rate;
1454 + unsigned long current_tx_rate;
1455 + unsigned long current_rx_match_ratio;
1457 + unsigned long max_rx_rate;
1458 + unsigned long max_tx_rate;
1459 + unsigned long max_rx_match_ratio;
1461 + unsigned long rx_frames_delta;
1462 + unsigned long tx_frames_delta;
1463 + unsigned long matches_delta;
1464 +}; /* can be reset e.g. by can_init_stats() */
1467 + unsigned long stats_reset;
1468 + unsigned long user_reset;
1469 + unsigned long rcv_entries;
1470 + unsigned long rcv_entries_max;
1471 +}; /* persistent statistics */
1473 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1474 +extern void can_init_proc(void);
1475 +extern void can_remove_proc(void);
1476 +extern void can_stat_update(unsigned long data);
1478 +/* structures and variables from af_can.c needed in proc.c for reading */
1479 +extern struct timer_list stattimer; /* timer for statistics update */
1480 +extern struct s_stats stats; /* packet statistics */
1481 +extern struct s_pstats pstats; /* receive list statistics */
1482 +extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
1484 +#endif /* AF_CAN_H */
1485 Index: linux-2.6.22-rc3/net/can/proc.c
1486 ===================================================================
1487 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1488 +++ linux-2.6.22-rc3/net/can/proc.c 2007-05-29 10:14:54.%N +0200
1491 + * proc.c - procfs support for Protocol family CAN core module
1493 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1494 + * All rights reserved.
1496 + * Redistribution and use in source and binary forms, with or without
1497 + * modification, are permitted provided that the following conditions
1499 + * 1. Redistributions of source code must retain the above copyright
1500 + * notice, this list of conditions, the following disclaimer and
1501 + * the referenced file 'COPYING'.
1502 + * 2. Redistributions in binary form must reproduce the above copyright
1503 + * notice, this list of conditions and the following disclaimer in the
1504 + * documentation and/or other materials provided with the distribution.
1505 + * 3. Neither the name of Volkswagen nor the names of its contributors
1506 + * may be used to endorse or promote products derived from this software
1507 + * without specific prior written permission.
1509 + * Alternatively, provided that this notice is retained in full, this
1510 + * software may be distributed under the terms of the GNU General
1511 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1512 + * file from the main directory of the linux kernel source.
1514 + * The provided data structures and external interfaces from this code
1515 + * are not restricted to be used by modules with a GPL compatible license.
1517 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1518 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1519 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1520 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1521 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1522 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1523 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1524 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1525 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1526 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1527 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1530 + * Send feedback to <socketcan-users@lists.berlios.de>
1534 +#include <linux/module.h>
1535 +#include <linux/proc_fs.h>
1536 +#include <linux/list.h>
1537 +#include <linux/rcupdate.h>
1538 +#include <linux/can/core.h>
1540 +#include "af_can.h"
1543 + * proc filenames for the PF_CAN core
1546 +#define CAN_PROC_VERSION "version"
1547 +#define CAN_PROC_STATS "stats"
1548 +#define CAN_PROC_RESET_STATS "reset_stats"
1549 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
1550 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
1551 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
1552 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
1553 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
1554 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
1556 +static struct proc_dir_entry *can_dir = NULL;
1557 +static struct proc_dir_entry *pde_version = NULL;
1558 +static struct proc_dir_entry *pde_stats = NULL;
1559 +static struct proc_dir_entry *pde_reset_stats = NULL;
1560 +static struct proc_dir_entry *pde_rcvlist_all = NULL;
1561 +static struct proc_dir_entry *pde_rcvlist_fil = NULL;
1562 +static struct proc_dir_entry *pde_rcvlist_inv = NULL;
1563 +static struct proc_dir_entry *pde_rcvlist_sff = NULL;
1564 +static struct proc_dir_entry *pde_rcvlist_eff = NULL;
1565 +static struct proc_dir_entry *pde_rcvlist_err = NULL;
1567 +static int user_reset = 0;
1569 +static const char *rx_list_name[] = {
1570 + [RX_ERR] = "rx_err",
1571 + [RX_ALL] = "rx_all",
1572 + [RX_FIL] = "rx_fil",
1573 + [RX_INV] = "rx_inv",
1574 + [RX_EFF] = "rx_eff",
1578 + * af_can statistics stuff
1581 +static void can_init_stats(void)
1584 + * This memset function is called from a timer context (when
1585 + * stattimer is active which is the default) OR in a process
1586 + * context (reading the proc_fs when stattimer is disabled).
1588 + memset(&stats, 0, sizeof(stats));
1589 + stats.jiffies_init = jiffies;
1591 + pstats.stats_reset++;
1595 + pstats.user_reset++;
1599 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1600 + unsigned long count)
1602 + unsigned long ret = 0;
1604 + if (oldjif == newjif)
1607 + /* see can_rcv() - this should NEVER happen! */
1608 + if (count > (ULONG_MAX / HZ)) {
1609 + printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
1614 + ret = (count * HZ) / (newjif - oldjif);
1619 +void can_stat_update(unsigned long data)
1621 + unsigned long j = jiffies; /* snapshot */
1623 + /* restart counting in timer context on user request */
1627 + /* restart counting on jiffies overflow */
1628 + if (j < stats.jiffies_init)
1631 + /* stats.rx_frames is the definitively max. statistic value */
1633 + /* prevent overflow in calc_rate() */
1634 + if (stats.rx_frames > (ULONG_MAX / HZ))
1637 + /* matches overflow - very improbable */
1638 + if (stats.matches > (ULONG_MAX / 100))
1641 + /* calc total values */
1642 + if (stats.rx_frames)
1643 + stats.total_rx_match_ratio = (stats.matches * 100) /
1646 + stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1648 + stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1651 + /* calc current values */
1652 + if (stats.rx_frames_delta)
1653 + stats.current_rx_match_ratio =
1654 + (stats.matches_delta * 100) / stats.rx_frames_delta;
1656 + stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
1657 + stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
1659 + /* check / update maximum values */
1660 + if (stats.max_tx_rate < stats.current_tx_rate)
1661 + stats.max_tx_rate = stats.current_tx_rate;
1663 + if (stats.max_rx_rate < stats.current_rx_rate)
1664 + stats.max_rx_rate = stats.current_rx_rate;
1666 + if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1667 + stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1669 + /* clear values for 'current rate' calculation */
1670 + stats.tx_frames_delta = 0;
1671 + stats.rx_frames_delta = 0;
1672 + stats.matches_delta = 0;
1674 + /* restart timer (one second) */
1675 + stattimer.expires = jiffies + HZ;
1676 + add_timer(&stattimer);
1680 + * proc read functions
1682 + * From known use-cases we expect about 10 entries in a receive list to be
1683 + * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
1687 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1688 + struct net_device *dev)
1690 + struct receiver *r;
1691 + struct hlist_node *n;
1694 + hlist_for_each_entry_rcu(r, n, rx_list, list) {
1695 + char *fmt = (r->can_id & CAN_EFF_FLAG)?
1696 + " %-5s %08X %08x %08x %08x %8ld %s\n" :
1697 + " %-5s %03X %08x %08x %08x %8ld %s\n";
1699 + len += snprintf(page + len, PAGE_SIZE - len, fmt,
1700 + DNAME(dev), r->can_id, r->mask,
1701 + (unsigned int)r->func, (unsigned int)r->data,
1702 + r->matches, r->ident);
1704 + /* does a typical line fit into the current buffer? */
1706 + /* 100 Bytes before end of buffer */
1707 + if (len > PAGE_SIZE - 100) {
1708 + /* mark output cut off */
1709 + len += snprintf(page + len, PAGE_SIZE - len,
1714 + rcu_read_unlock();
1719 +static int can_print_recv_banner(char *page, int len)
1722 + * can1. 00000000 00000000 00000000
1725 + len += snprintf(page + len, PAGE_SIZE - len,
1726 + " device can_id can_mask function"
1727 + " userdata matches ident\n");
1732 +static int can_proc_read_stats(char *page, char **start, off_t off,
1733 + int count, int *eof, void *data)
1737 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1738 + len += snprintf(page + len, PAGE_SIZE - len,
1739 + " %8ld transmitted frames (TXF)\n", stats.tx_frames);
1740 + len += snprintf(page + len, PAGE_SIZE - len,
1741 + " %8ld received frames (RXF)\n", stats.rx_frames);
1742 + len += snprintf(page + len, PAGE_SIZE - len,
1743 + " %8ld matched frames (RXMF)\n", stats.matches);
1745 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1747 + if (stattimer.function == can_stat_update) {
1748 + len += snprintf(page + len, PAGE_SIZE - len,
1749 + " %8ld %% total match ratio (RXMR)\n",
1750 + stats.total_rx_match_ratio);
1752 + len += snprintf(page + len, PAGE_SIZE - len,
1753 + " %8ld frames/s total tx rate (TXR)\n",
1754 + stats.total_tx_rate);
1755 + len += snprintf(page + len, PAGE_SIZE - len,
1756 + " %8ld frames/s total rx rate (RXR)\n",
1757 + stats.total_rx_rate);
1759 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1761 + len += snprintf(page + len, PAGE_SIZE - len,
1762 + " %8ld %% current match ratio (CRXMR)\n",
1763 + stats.current_rx_match_ratio);
1765 + len += snprintf(page + len, PAGE_SIZE - len,
1766 + " %8ld frames/s current tx rate (CTXR)\n",
1767 + stats.current_tx_rate);
1768 + len += snprintf(page + len, PAGE_SIZE - len,
1769 + " %8ld frames/s current rx rate (CRXR)\n",
1770 + stats.current_rx_rate);
1772 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1774 + len += snprintf(page + len, PAGE_SIZE - len,
1775 + " %8ld %% max match ratio (MRXMR)\n",
1776 + stats.max_rx_match_ratio);
1778 + len += snprintf(page + len, PAGE_SIZE - len,
1779 + " %8ld frames/s max tx rate (MTXR)\n",
1780 + stats.max_tx_rate);
1781 + len += snprintf(page + len, PAGE_SIZE - len,
1782 + " %8ld frames/s max rx rate (MRXR)\n",
1783 + stats.max_rx_rate);
1785 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1788 + len += snprintf(page + len, PAGE_SIZE - len,
1789 + " %8ld current receive list entries (CRCV)\n",
1790 + pstats.rcv_entries);
1791 + len += snprintf(page + len, PAGE_SIZE - len,
1792 + " %8ld maximum receive list entries (MRCV)\n",
1793 + pstats.rcv_entries_max);
1795 + if (pstats.stats_reset)
1796 + len += snprintf(page + len, PAGE_SIZE - len,
1797 + "\n %8ld statistic resets (STR)\n",
1798 + pstats.stats_reset);
1800 + if (pstats.user_reset)
1801 + len += snprintf(page + len, PAGE_SIZE - len,
1802 + " %8ld user statistic resets (USTR)\n",
1803 + pstats.user_reset);
1805 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1811 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1812 + int count, int *eof, void *data)
1818 + if (stattimer.function == can_stat_update) {
1819 + len += snprintf(page + len, PAGE_SIZE - len,
1820 + "Scheduled statistic reset #%ld.\n",
1821 + pstats.stats_reset + 1);
1824 + if (stats.jiffies_init != jiffies)
1827 + len += snprintf(page + len, PAGE_SIZE - len,
1828 + "Performed statistic reset #%ld.\n",
1829 + pstats.stats_reset);
1836 +static int can_proc_read_version(char *page, char **start, off_t off,
1837 + int count, int *eof, void *data)
1841 + len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1842 + CAN_VERSION_STRING);
1847 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1848 + int count, int *eof, void *data)
1850 + int idx = (int)data;
1852 + struct dev_rcv_lists *d;
1853 + struct hlist_node *n;
1855 + len += snprintf(page + len, PAGE_SIZE - len,
1856 + "\nreceive list '%s':\n", rx_list_name[idx]);
1859 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1861 + if (!hlist_empty(&d->rx[idx])) {
1862 + len = can_print_recv_banner(page, len);
1863 + len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
1865 + len += snprintf(page + len, PAGE_SIZE - len,
1866 + " (%s: no entry)\n", DNAME(d->dev));
1868 + /* exit on end of buffer? */
1869 + if (len > PAGE_SIZE - 100)
1872 + rcu_read_unlock();
1874 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1880 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1881 + int count, int *eof, void *data)
1884 + struct dev_rcv_lists *d;
1885 + struct hlist_node *n;
1888 + len += snprintf(page + len, PAGE_SIZE - len,
1889 + "\nreceive list 'rx_sff':\n");
1892 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1893 + int i, all_empty = 1;
1894 + /* check wether at least one list is non-empty */
1895 + for (i = 0; i < 0x800; i++)
1896 + if (!hlist_empty(&d->rx_sff[i])) {
1902 + len = can_print_recv_banner(page, len);
1903 + for (i = 0; i < 0x800; i++) {
1904 + if (!hlist_empty(&d->rx_sff[i]) &&
1905 + len < PAGE_SIZE - 100)
1906 + len = can_print_rcvlist(page, len,
1911 + len += snprintf(page + len, PAGE_SIZE - len,
1912 + " (%s: no entry)\n", DNAME(d->dev));
1914 + /* exit on end of buffer? */
1915 + if (len > PAGE_SIZE - 100)
1918 + rcu_read_unlock();
1920 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1927 + * proc utility functions
1930 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1932 + read_proc_t* read_proc,
1936 + return create_proc_read_entry(name, mode, can_dir, read_proc,
1942 +static void can_remove_proc_readentry(const char *name)
1945 + remove_proc_entry(name, can_dir);
1949 + * can_init_proc - create main CAN proc directory and procfs entries
1951 +void can_init_proc(void)
1953 + /* create /proc/net/can directory */
1954 + can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
1957 + printk(KERN_INFO "can: failed to create /proc/%s . "
1958 + "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
1962 + can_dir->owner = THIS_MODULE;
1964 + /* own procfs entries from the AF_CAN core */
1965 + pde_version = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
1966 + can_proc_read_version, NULL);
1967 + pde_stats = can_create_proc_readentry(CAN_PROC_STATS, 0644,
1968 + can_proc_read_stats, NULL);
1969 + pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
1970 + can_proc_read_reset_stats, NULL);
1971 + pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
1972 + can_proc_read_rcvlist, (void*)RX_ERR);
1973 + pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
1974 + can_proc_read_rcvlist, (void*)RX_ALL);
1975 + pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
1976 + can_proc_read_rcvlist, (void*)RX_FIL);
1977 + pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
1978 + can_proc_read_rcvlist, (void*)RX_INV);
1979 + pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
1980 + can_proc_read_rcvlist, (void*)RX_EFF);
1981 + pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
1982 + can_proc_read_rcvlist_sff, NULL);
1986 + * can_remove_proc - remove procfs entries and main CAN proc directory
1988 +void can_remove_proc(void)
1991 + can_remove_proc_readentry(CAN_PROC_VERSION);
1994 + can_remove_proc_readentry(CAN_PROC_STATS);
1996 + if (pde_reset_stats)
1997 + can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1999 + if (pde_rcvlist_err)
2000 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
2002 + if (pde_rcvlist_all)
2003 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
2005 + if (pde_rcvlist_fil)
2006 + can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
2008 + if (pde_rcvlist_inv)
2009 + can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
2011 + if (pde_rcvlist_eff)
2012 + can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
2014 + if (pde_rcvlist_sff)
2015 + can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
2018 + remove_proc_entry(CAN_PROC_DIR, NULL);
2020 Index: linux-2.6.22-rc3/include/linux/can/error.h
2021 ===================================================================
2022 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2023 +++ linux-2.6.22-rc3/include/linux/can/error.h 2007-05-30 12:42:27.%N +0200
2026 + * linux/can/error.h
2028 + * Definitions of the CAN error frame to be filtered and passed to the user.
2030 + * $Id: error.h 162 2007-03-05 08:25:02Z hartkopp $
2032 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
2033 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
2034 + * All rights reserved.
2036 + * Send feedback to <socketcan-users@lists.berlios.de>
2040 +#ifndef CAN_ERROR_H
2041 +#define CAN_ERROR_H
2043 +#define CAN_ERR_DLC 8 /* dlc for error frames */
2045 +/* error class (mask) in can_id */
2046 +#define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */
2047 +#define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */
2048 +#define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */
2049 +#define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */
2050 +#define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */
2051 +#define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */
2052 +#define CAN_ERR_BUSOFF 0x00000040U /* bus off */
2053 +#define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */
2054 +#define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */
2056 +/* arbitration lost in bit ... / data[0] */
2057 +#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
2058 + /* else bit number in bitstream */
2060 +/* error status of CAN-controller / data[1] */
2061 +#define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */
2062 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
2063 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
2064 +#define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */
2065 +#define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */
2066 +#define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */
2067 +#define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */
2068 + /* (at least one error counter exceeds */
2069 + /* the protocol-defined level of 127) */
2071 +/* error in CAN protocol (type) / data[2] */
2072 +#define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */
2073 +#define CAN_ERR_PROT_BIT 0x01 /* single bit error */
2074 +#define CAN_ERR_PROT_FORM 0x02 /* frame format error */
2075 +#define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */
2076 +#define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */
2077 +#define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */
2078 +#define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */
2079 +#define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */
2080 +#define CAN_ERR_PROT_TX 0x80 /* error occured on transmission */
2082 +/* error in CAN protocol (location) / data[3] */
2083 +#define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */
2084 +#define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */
2085 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
2086 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
2087 +#define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */
2088 +#define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */
2089 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
2090 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
2091 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
2092 +#define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */
2093 +#define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */
2094 +#define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */
2095 +#define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */
2096 +#define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */
2097 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
2098 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
2099 +#define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */
2100 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
2101 +#define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */
2102 +#define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */
2104 +/* error status of CAN-transceiver / data[4] */
2106 +#define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */
2107 +#define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */
2108 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */
2109 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */
2110 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */
2111 +#define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */
2112 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */
2113 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */
2114 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */
2115 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
2117 +/* controller specific additional information / data[5..7] */
2119 +#endif /* CAN_ERROR_H */