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 | 80 +++
14 include/linux/can/error.h | 93 ++++
17 net/can/Kconfig | 25 +
19 net/can/af_can.c | 995 ++++++++++++++++++++++++++++++++++++++++++++++
20 net/can/af_can.h | 121 +++++
21 net/can/proc.c | 531 ++++++++++++++++++++++++
22 10 files changed, 1951 insertions(+)
24 Index: net-2.6.23/include/linux/can.h
25 ===================================================================
26 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
27 +++ net-2.6.23/include/linux/can.h 2007-07-09 13:23:28.000000000 +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: net-2.6.23/include/linux/can/core.h
128 ===================================================================
129 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
130 +++ net-2.6.23/include/linux/can/core.h 2007-07-09 13:23:28.000000000 +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 "20070709"
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_send(struct sk_buff *skb, int loop);
197 +#ifdef CONFIG_CAN_DEBUG_CORE
198 +extern void can_debug_skb(struct sk_buff *skb);
199 +extern void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
200 +#define DBG(args...) (debug & 1 ? \
201 + (printk(KERN_DEBUG "can-%s %s: ", \
202 + IDENT, __func__), printk(args)) : 0)
203 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
204 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
206 +#define DBG(args...)
207 +#define DBG_FRAME(args...)
208 +#define DBG_SKB(skb)
211 +#endif /* CAN_CORE_H */
212 Index: net-2.6.23/net/Kconfig
213 ===================================================================
214 --- net-2.6.23.orig/net/Kconfig 2007-07-09 13:23:24.000000000 +0200
215 +++ net-2.6.23/net/Kconfig 2007-07-09 13:23:28.000000000 +0200
219 source "net/ax25/Kconfig"
220 +source "net/can/Kconfig"
221 source "net/irda/Kconfig"
222 source "net/bluetooth/Kconfig"
223 source "net/rxrpc/Kconfig"
224 Index: net-2.6.23/net/Makefile
225 ===================================================================
226 --- net-2.6.23.orig/net/Makefile 2007-07-09 13:23:24.000000000 +0200
227 +++ net-2.6.23/net/Makefile 2007-07-09 13:23:28.000000000 +0200
229 obj-$(CONFIG_NETROM) += netrom/
230 obj-$(CONFIG_ROSE) += rose/
231 obj-$(CONFIG_AX25) += ax25/
232 +obj-$(CONFIG_CAN) += can/
233 obj-$(CONFIG_IRDA) += irda/
234 obj-$(CONFIG_BT) += bluetooth/
235 obj-$(CONFIG_SUNRPC) += sunrpc/
236 Index: net-2.6.23/net/can/Kconfig
237 ===================================================================
238 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
239 +++ net-2.6.23/net/can/Kconfig 2007-07-16 21:42:33.000000000 +0200
242 +# Controller Area Network (CAN) network layer core configuration
247 + tristate "CAN bus subsystem support"
249 + Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
250 + communications protocol, which was developed by Bosch at
251 + 1991 mainly for automotive, but now widely used in marine
252 + (NMEA2000), industrial and medical applications.
253 + More information on the CAN network protocol family PF_CAN
254 + is contained in <Documentation/networking/can.txt>.
256 + If you want CAN support, you should say Y here and also to the
257 + specific driver for your controller(s) below.
259 +config CAN_DEBUG_CORE
260 + bool "CAN Core debugging messages"
263 + Say Y here if you want the CAN core to produce a bunch of debug
264 + messages to the system log. Select this if you are having a
265 + problem with CAN support and want to see more of what is going on.
266 Index: net-2.6.23/net/can/Makefile
267 ===================================================================
268 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
269 +++ net-2.6.23/net/can/Makefile 2007-07-16 21:42:33.000000000 +0200
272 +# Makefile for the Linux Controller Area Network core.
275 +obj-$(CONFIG_CAN) += can.o
276 +can-objs := af_can.o proc.o
277 Index: net-2.6.23/net/can/af_can.c
278 ===================================================================
279 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
280 +++ net-2.6.23/net/can/af_can.c 2007-07-16 21:45:37.000000000 +0200
283 + * af_can.c - Protocol family CAN core module
284 + * (used by different CAN protocol modules)
286 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
287 + * All rights reserved.
289 + * Redistribution and use in source and binary forms, with or without
290 + * modification, are permitted provided that the following conditions
292 + * 1. Redistributions of source code must retain the above copyright
293 + * notice, this list of conditions, the following disclaimer and
294 + * the referenced file 'COPYING'.
295 + * 2. Redistributions in binary form must reproduce the above copyright
296 + * notice, this list of conditions and the following disclaimer in the
297 + * documentation and/or other materials provided with the distribution.
298 + * 3. Neither the name of Volkswagen nor the names of its contributors
299 + * may be used to endorse or promote products derived from this software
300 + * without specific prior written permission.
302 + * Alternatively, provided that this notice is retained in full, this
303 + * software may be distributed under the terms of the GNU General
304 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
305 + * file from the main directory of the linux kernel source.
307 + * The provided data structures and external interfaces from this code
308 + * are not restricted to be used by modules with a GPL compatible license.
310 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
311 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
313 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
314 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
315 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
316 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
317 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
318 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
319 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
320 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
323 + * Send feedback to <socketcan-users@lists.berlios.de>
327 +#include <linux/module.h>
328 +#include <linux/init.h>
329 +#include <linux/kmod.h>
330 +#include <linux/slab.h>
331 +#include <linux/list.h>
332 +#include <linux/spinlock.h>
333 +#include <linux/rcupdate.h>
334 +#include <linux/uaccess.h>
335 +#include <linux/net.h>
336 +#include <linux/netdevice.h>
337 +#include <linux/socket.h>
338 +#include <linux/if_ether.h>
339 +#include <linux/if_arp.h>
340 +#include <linux/skbuff.h>
341 +#include <linux/can.h>
342 +#include <linux/can/core.h>
343 +#include <net/sock.h>
347 +#define IDENT "core"
348 +static __initdata const char banner[] = KERN_INFO
349 + "can: controller area network core (" CAN_VERSION_STRING ")\n";
351 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
352 +MODULE_LICENSE("Dual BSD/GPL");
353 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
354 + "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
356 +MODULE_ALIAS_NETPROTO(PF_CAN);
358 +int stats_timer = 1; /* default: on */
359 +module_param(stats_timer, int, S_IRUGO);
360 +MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
362 +#ifdef CONFIG_CAN_DEBUG_CORE
364 +module_param(debug, int, S_IRUGO);
365 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
368 +HLIST_HEAD(rx_dev_list);
369 +static struct dev_rcv_lists rx_alldev_list;
370 +static DEFINE_SPINLOCK(rcv_lists_lock);
372 +static struct kmem_cache *rcv_cache __read_mostly;
374 +/* table of registered CAN protocols */
375 +static struct can_proto *proto_tab[CAN_NPROTO];
377 +struct timer_list stattimer; /* timer for statistics update */
378 +struct s_stats stats; /* packet statistics */
379 +struct s_pstats pstats; /* receive list statistics */
382 + * af_can socket functions
385 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
387 + struct sock *sk = sock->sk;
392 + return sock_get_timestamp(sk, (struct timeval __user *)arg);
395 + return -ENOIOCTLCMD;
399 +static void can_sock_destruct(struct sock *sk)
401 + DBG("called for sock %p\n", sk);
403 + skb_queue_purge(&sk->sk_receive_queue);
404 + if (sk->sk_protinfo)
405 + kfree(sk->sk_protinfo);
408 +static int can_create(struct socket *sock, int protocol)
411 + struct can_proto *cp;
412 + char module_name[sizeof("can-proto-000")];
415 + DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
417 + sock->state = SS_UNCONNECTED;
419 + if (protocol < 0 || protocol >= CAN_NPROTO)
422 + DBG("looking up proto %d in proto_tab[]\n", protocol);
424 + /* try to load protocol module, when CONFIG_KMOD is defined */
425 + if (!proto_tab[protocol]) {
426 + sprintf(module_name, "can-proto-%d", protocol);
427 + ret = request_module(module_name);
430 + * In case of error we only print a message but don't
431 + * return the error code immediately. Below we will
432 + * return -EPROTONOSUPPORT
434 + if (ret == -ENOSYS)
435 + printk(KERN_INFO "can: request_module(%s) not"
436 + " implemented.\n", module_name);
438 + printk(KERN_ERR "can: request_module(%s) failed\n",
442 + /* check for success and correct type */
443 + cp = proto_tab[protocol];
444 + if (!cp || cp->type != sock->type)
445 + return -EPROTONOSUPPORT;
447 + if (cp->capability >= 0 && !capable(cp->capability))
450 + sock->ops = cp->ops;
452 + sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
456 + sock_init_data(sock, sk);
457 + sk->sk_destruct = can_sock_destruct;
459 + DBG("created sock: %p\n", sk);
461 + if (sk->sk_prot->init)
462 + ret = sk->sk_prot->init(sk);
465 + /* release sk on errors */
478 + * can_send - transmit a CAN frame (optional with local loopback)
479 + * @skb: pointer to socket buffer with CAN frame in data section
480 + * @loop: loopback for listeners on local CAN sockets (recommended default!)
484 + * -ENETDOWN when the selected interface is down
485 + * -ENOBUFS on full driver queue (see net_xmit_errno())
486 + * -ENOMEM when local loopback failed at calling skb_clone()
488 +int can_send(struct sk_buff *skb, int loop)
492 + if (skb->dev->type != ARPHRD_CAN) {
497 + if (!(skb->dev->flags & IFF_UP)) {
502 + skb->protocol = htons(ETH_P_CAN);
505 + /* local loopback of sent CAN frames */
507 + /* indication for the CAN driver: do loopback */
508 + skb->pkt_type = PACKET_LOOPBACK;
511 + * The reference to the originating sock may be required
512 + * by the receiving socket to check whether the frame is
513 + * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
514 + * Therefore we have to ensure that skb->sk remains the
515 + * reference to the originating sock by restoring skb->sk
516 + * after each skb_clone() or skb_orphan() usage.
519 + if (!(skb->dev->flags & IFF_LOOPBACK)) {
521 + * If the interface is not capable to do loopback
522 + * itself, we do it here.
524 + struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
531 + newskb->sk = skb->sk;
532 + newskb->ip_summed = CHECKSUM_UNNECESSARY;
533 + newskb->pkt_type = PACKET_BROADCAST;
537 + /* indication for the CAN driver: no loopback required */
538 + skb->pkt_type = PACKET_HOST;
541 + /* send to netdevice */
542 + err = dev_queue_xmit(skb);
544 + err = net_xmit_errno(err);
546 + /* update statistics */
548 + stats.tx_frames_delta++;
552 +EXPORT_SYMBOL(can_send);
558 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
560 + struct dev_rcv_lists *d;
561 + struct hlist_node *n;
564 + * find receive list for this device
566 + * The hlist_for_each_entry*() macros curse through the list
567 + * using the pointer variable n and set d to the containing
568 + * struct in each list iteration. Therefore, after list
569 + * iteration, d is unmodified when the list is empty, and it
570 + * points to last list element, when the list is non-empty
571 + * but no match in the loop body is found. I.e. d is *not*
572 + * NULL when no match is found. We can, however, use the
573 + * cursor variable n to decide if a match was found.
576 + hlist_for_each_entry(d, n, &rx_dev_list, list) {
581 + return n ? d : NULL;
584 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
585 + struct dev_rcv_lists *d)
587 + canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
589 + /* filter error frames */
590 + if (*mask & CAN_ERR_FLAG) {
591 + /* clear CAN_ERR_FLAG in list entry */
592 + *mask &= CAN_ERR_MASK;
593 + return &d->rx[RX_ERR];
596 + /* ensure valid values in can_mask */
597 + if (*mask & CAN_EFF_FLAG)
598 + *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
600 + *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
602 + /* reduce condition testing at receive time */
605 + /* inverse can_id/can_mask filter */
607 + return &d->rx[RX_INV];
609 + /* mask == 0 => no condition testing at receive time */
611 + return &d->rx[RX_ALL];
613 + /* use extra filterset for the subscription of exactly *ONE* can_id */
614 + if (*can_id & CAN_EFF_FLAG) {
615 + if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
616 + /* RFC: a use-case for hash-tables in the future? */
617 + return &d->rx[RX_EFF];
620 + if (*mask == CAN_SFF_MASK)
621 + return &d->rx_sff[*can_id];
624 + /* default: filter via can_id/can_mask */
625 + return &d->rx[RX_FIL];
629 + * can_rx_register - subscribe CAN frames from a specific interface
630 + * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
631 + * @can_id: CAN identifier (see description)
632 + * @mask: CAN mask (see description)
633 + * @func: callback function on filter match
634 + * @data: returned parameter for callback function
635 + * @ident: string for calling module indentification
638 + * Invokes the callback function with the received sk_buff and the given
639 + * parameter 'data' on a matching receive filter. A filter matches, when
641 + * <received_can_id> & mask == can_id & mask
643 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
644 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
648 + * -ENOMEM on missing cache mem to create subscription entry
649 + * -ENODEV unknown device
651 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
652 + void (*func)(struct sk_buff *, void *), void *data,
655 + struct receiver *r;
656 + struct hlist_head *rl;
657 + struct dev_rcv_lists *d;
660 + /* insert new receiver (dev,canid,mask) -> (func,data) */
662 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
663 + "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
665 + r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
669 + spin_lock_bh(&rcv_lists_lock);
671 + d = find_dev_rcv_lists(dev);
673 + rl = find_rcv_list(&can_id, &mask, d);
675 + r->can_id = can_id;
682 + hlist_add_head_rcu(&r->list, rl);
685 + pstats.rcv_entries++;
686 + if (pstats.rcv_entries_max < pstats.rcv_entries)
687 + pstats.rcv_entries_max = pstats.rcv_entries;
689 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
690 + DNAME(dev), can_id, mask);
691 + kmem_cache_free(rcv_cache, r);
695 + spin_unlock_bh(&rcv_lists_lock);
699 +EXPORT_SYMBOL(can_rx_register);
702 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
704 +static void can_rx_delete_device(struct rcu_head *rp)
706 + struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
708 + DBG("removing dev_rcv_list at %p\n", d);
713 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
715 +static void can_rx_delete_receiver(struct rcu_head *rp)
717 + struct receiver *r = container_of(rp, struct receiver, rcu);
719 + DBG("removing receiver at %p\n", r);
720 + kmem_cache_free(rcv_cache, r);
724 + * can_rx_unregister - unsubscribe CAN frames from a specific interface
725 + * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
726 + * @can_id: CAN identifier
728 + * @func: callback function on filter match
729 + * @data: returned parameter for callback function
732 + * Removes subscription entry depending on given (subscription) values.
736 + * -EINVAL on missing subscription entry
737 + * -ENODEV unknown device
739 +int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
740 + void (*func)(struct sk_buff *, void *), void *data)
742 + struct receiver *r = NULL;
743 + struct hlist_head *rl;
744 + struct hlist_node *next;
745 + struct dev_rcv_lists *d;
748 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
749 + dev, DNAME(dev), can_id, mask, func, data);
751 + spin_lock_bh(&rcv_lists_lock);
753 + d = find_dev_rcv_lists(dev);
755 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
756 + DNAME(dev), can_id, mask);
761 + rl = find_rcv_list(&can_id, &mask, d);
764 + * Search the receiver list for the item to delete. This should
765 + * exist, since no receiver may be unregistered that hasn't
766 + * been registered before.
769 + hlist_for_each_entry(r, next, rl, list) {
770 + if (r->can_id == can_id && r->mask == mask
771 + && r->func == func && r->data == data)
776 + * Check for bug in CAN protocol implementations:
777 + * If no matching list item was found, the list cursor variable next
778 + * will be NULL, while r will point to the last item of the list.
782 + DBG("receive list entry not found for "
783 + "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
790 + hlist_del_rcu(&r->list);
793 + if (pstats.rcv_entries > 0)
794 + pstats.rcv_entries--;
796 + /* remove device structure requested by NETDEV_UNREGISTER */
797 + if (d->remove_on_zero_entries && !d->entries) {
798 + DBG("removing dev_rcv_list for %s on zero entries\n",
800 + hlist_del_rcu(&d->list);
805 + spin_unlock_bh(&rcv_lists_lock);
807 + /* schedule the receiver item for deletion */
809 + call_rcu(&r->rcu, can_rx_delete_receiver);
811 + /* schedule the device structure for deletion */
813 + call_rcu(&d->rcu, can_rx_delete_device);
817 +EXPORT_SYMBOL(can_rx_unregister);
819 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
821 + struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
823 + DBG("skbuff %p cloned to %p\n", skb, clone);
825 + clone->sk = skb->sk;
826 + r->func(clone, r->data);
831 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
833 + struct receiver *r;
834 + struct hlist_node *n;
836 + struct can_frame *cf = (struct can_frame *)skb->data;
837 + canid_t can_id = cf->can_id;
839 + if (d->entries == 0)
842 + if (can_id & CAN_ERR_FLAG) {
843 + /* check for error frame entries only */
844 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
845 + if (can_id & r->mask) {
846 + DBG("match on rx_err skbuff %p\n", skb);
854 + /* check for unfiltered entries */
855 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
856 + DBG("match on rx_all skbuff %p\n", skb);
861 + /* check for can_id/mask entries */
862 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
863 + if ((can_id & r->mask) == r->can_id) {
864 + DBG("match on rx_fil skbuff %p\n", skb);
870 + /* check for inverted can_id/mask entries */
871 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
872 + if ((can_id & r->mask) != r->can_id) {
873 + DBG("match on rx_inv skbuff %p\n", skb);
879 + /* check CAN_ID specific entries */
880 + if (can_id & CAN_EFF_FLAG) {
881 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
882 + if (r->can_id == can_id) {
883 + DBG("match on rx_eff skbuff %p\n", skb);
889 + can_id &= CAN_SFF_MASK;
890 + hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
891 + DBG("match on rx_sff skbuff %p\n", skb);
900 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
901 + struct packet_type *pt, struct net_device *orig_dev)
903 + struct dev_rcv_lists *d;
906 + DBG("received skbuff on device %s, ptype %04x\n",
907 + dev->name, ntohs(pt->type));
909 + DBG_FRAME("af_can: can_rcv: received CAN frame",
910 + (struct can_frame *)skb->data);
912 + if (dev->type != ARPHRD_CAN) {
917 + /* update statistics */
919 + stats.rx_frames_delta++;
923 + /* deliver the packet to sockets listening on all devices */
924 + matches = can_rcv_filter(&rx_alldev_list, skb);
926 + /* find receive list for this device */
927 + d = find_dev_rcv_lists(dev);
929 + matches += can_rcv_filter(d, skb);
933 + /* free the skbuff allocated by the netdevice driver */
934 + DBG("freeing skbuff %p\n", skb);
939 + stats.matches_delta++;
946 + * af_can protocol functions
950 + * can_proto_register - register CAN transport protocol
951 + * @cp: pointer to CAN protocol structure
955 + * -EINVAL invalid (out of range) protocol number
956 + * -EBUSY protocol already in use
957 + * -ENOBUF if proto_register() fails
959 +int can_proto_register(struct can_proto *cp)
961 + int proto = cp->protocol;
964 + if (proto < 0 || proto >= CAN_NPROTO) {
965 + printk(KERN_ERR "can: protocol number %d out of range\n",
969 + if (proto_tab[proto]) {
970 + printk(KERN_ERR "can: protocol %d already registered\n",
975 + err = proto_register(cp->prot, 0);
979 + proto_tab[proto] = cp;
981 + /* use generic ioctl function if the module doesn't bring its own */
982 + if (!cp->ops->ioctl)
983 + cp->ops->ioctl = can_ioctl;
987 +EXPORT_SYMBOL(can_proto_register);
990 + * can_proto_unregister - unregister CAN transport protocol
991 + * @cp: pointer to CAN protocol structure
995 + * -ESRCH protocol number was not registered
997 +int can_proto_unregister(struct can_proto *cp)
999 + int proto = cp->protocol;
1001 + if (!proto_tab[proto]) {
1002 + printk(KERN_ERR "can: protocol %d is not registered\n", proto);
1005 + proto_unregister(cp->prot);
1006 + proto_tab[proto] = NULL;
1010 +EXPORT_SYMBOL(can_proto_unregister);
1013 + * af_can notifier to create/remove CAN netdevice specific structs
1015 +static int can_notifier(struct notifier_block *nb, unsigned long msg,
1018 + struct net_device *dev = (struct net_device *)data;
1019 + struct dev_rcv_lists *d;
1021 + DBG("msg %ld for dev %p (%s idx %d)\n",
1022 + msg, dev, dev->name, dev->ifindex);
1024 + if (dev->type != ARPHRD_CAN)
1025 + return NOTIFY_DONE;
1029 + case NETDEV_REGISTER:
1032 + * create new dev_rcv_lists for this device
1034 + * N.B. zeroing the struct is the correct initialization
1035 + * for the embedded hlist_head structs.
1036 + * Another list type, e.g. list_head, would require
1037 + * explicit initialization.
1040 + DBG("creating new dev_rcv_lists for %s\n", dev->name);
1042 + d = kzalloc(sizeof(*d),
1043 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1046 + "can: allocation of receive list failed\n");
1047 + return NOTIFY_DONE;
1051 + spin_lock_bh(&rcv_lists_lock);
1052 + hlist_add_head_rcu(&d->list, &rx_dev_list);
1053 + spin_unlock_bh(&rcv_lists_lock);
1057 + case NETDEV_UNREGISTER:
1058 + spin_lock_bh(&rcv_lists_lock);
1060 + d = find_dev_rcv_lists(dev);
1062 + DBG("remove dev_rcv_list for %s (%d entries)\n",
1063 + dev->name, d->entries);
1066 + d->remove_on_zero_entries = 1;
1069 + hlist_del_rcu(&d->list);
1071 + printk(KERN_ERR "can: notifier: receive list not "
1072 + "found for dev %s\n", dev->name);
1074 + spin_unlock_bh(&rcv_lists_lock);
1077 + call_rcu(&d->rcu, can_rx_delete_device);
1082 + return NOTIFY_DONE;
1086 + * af_can debugging stuff
1089 +#ifdef CONFIG_CAN_DEBUG_CORE
1091 +#define DBG_BSIZE 1024
1094 + * can_debug_cframe - print CAN frame
1095 + * @msg: pointer to message printed before the given CAN frame
1096 + * @cf: pointer to CAN frame
1098 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
1105 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1109 + len = sprintf(buf, KERN_DEBUG);
1111 + len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
1116 + dlc = cf->can_dlc;
1120 + if (cf->can_id & CAN_EFF_FLAG)
1121 + len += sprintf(buf + len, "<%08X> [%X] ",
1122 + cf->can_id & CAN_EFF_MASK, dlc);
1124 + len += sprintf(buf + len, "<%03X> [%X] ",
1125 + cf->can_id & CAN_SFF_MASK, dlc);
1127 + for (i = 0; i < dlc; i++)
1128 + len += sprintf(buf + len, "%02X ", cf->data[i]);
1130 + if (cf->can_id & CAN_RTR_FLAG)
1131 + len += sprintf(buf + len, "(RTR)");
1133 + buf[len++] = '\n';
1138 +EXPORT_SYMBOL(can_debug_cframe);
1141 + * can_debug_skb - print socket buffer content to kernel log
1142 + * @skb: pointer to socket buffer
1144 +void can_debug_skb(struct sk_buff *skb)
1146 + int len, nbytes, i;
1149 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1153 + len = sprintf(buf,
1154 + KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
1155 + KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
1156 + "h,d,t,e,l: %p %+d %+d %+d, %d",
1157 + skb, skb->dev ? skb->dev->ifindex : -1,
1158 + ntohs(skb->protocol),
1159 + atomic_read(&skb->users),
1160 + atomic_read(&(skb_shinfo(skb)->dataref)),
1161 + skb_shinfo(skb)->nr_frags,
1162 + skb->head, skb->data - skb->head,
1163 + skb->tail - skb->head, skb->end - skb->head, skb->len);
1164 + nbytes = skb->end - skb->head;
1165 + for (i = 0; i < nbytes; i++) {
1167 + len += sprintf(buf + len, "\n" KERN_DEBUG " ");
1168 + if (len < DBG_BSIZE - 16) {
1169 + len += sprintf(buf + len, " %02x", skb->head[i]);
1171 + len += sprintf(buf + len, "...");
1175 + buf[len++] = '\n';
1180 +EXPORT_SYMBOL(can_debug_skb);
1185 + * af_can module init/exit functions
1188 +static struct packet_type can_packet = {
1189 + .type = __constant_htons(ETH_P_CAN),
1194 +static struct net_proto_family can_family_ops = {
1196 + .create = can_create,
1197 + .owner = THIS_MODULE,
1200 +/* notifier block for netdevice event */
1201 +static struct notifier_block can_netdev_notifier = {
1202 + .notifier_call = can_notifier,
1205 +static __init int can_init(void)
1209 + rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1210 + 0, 0, NULL, NULL);
1215 + * Insert rx_alldev_list for reception on all devices.
1216 + * This struct is zero initialized which is correct for the
1217 + * embedded hlist heads, the dev pointer, and the entries counter.
1220 + spin_lock_bh(&rcv_lists_lock);
1221 + hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1222 + spin_unlock_bh(&rcv_lists_lock);
1224 + if (stats_timer) {
1225 + /* the statistics are updated every second (timer triggered) */
1226 + init_timer(&stattimer);
1227 + stattimer.function = can_stat_update;
1228 + stattimer.data = 0;
1229 + /* update every second */
1230 + stattimer.expires = jiffies + HZ;
1231 + /* start statistics timer */
1232 + add_timer(&stattimer);
1234 + stattimer.function = NULL;
1239 + /* protocol register */
1240 + sock_register(&can_family_ops);
1241 + register_netdevice_notifier(&can_netdev_notifier);
1242 + dev_add_pack(&can_packet);
1247 +static __exit void can_exit(void)
1249 + struct dev_rcv_lists *d;
1250 + struct hlist_node *n, *next;
1253 + del_timer(&stattimer);
1255 + /* procfs remove */
1256 + can_remove_proc();
1258 + /* protocol unregister */
1259 + dev_remove_pack(&can_packet);
1260 + unregister_netdevice_notifier(&can_netdev_notifier);
1261 + sock_unregister(PF_CAN);
1263 + /* remove rx_dev_list */
1264 + spin_lock_bh(&rcv_lists_lock);
1265 + hlist_del(&rx_alldev_list.list);
1266 + hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1267 + hlist_del(&d->list);
1270 + spin_unlock_bh(&rcv_lists_lock);
1272 + kmem_cache_destroy(rcv_cache);
1275 +module_init(can_init);
1276 +module_exit(can_exit);
1277 Index: net-2.6.23/net/can/af_can.h
1278 ===================================================================
1279 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1280 +++ net-2.6.23/net/can/af_can.h 2007-07-09 13:23:28.000000000 +0200
1283 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1284 + * All rights reserved.
1286 + * Redistribution and use in source and binary forms, with or without
1287 + * modification, are permitted provided that the following conditions
1289 + * 1. Redistributions of source code must retain the above copyright
1290 + * notice, this list of conditions, the following disclaimer and
1291 + * the referenced file 'COPYING'.
1292 + * 2. Redistributions in binary form must reproduce the above copyright
1293 + * notice, this list of conditions and the following disclaimer in the
1294 + * documentation and/or other materials provided with the distribution.
1295 + * 3. Neither the name of Volkswagen nor the names of its contributors
1296 + * may be used to endorse or promote products derived from this software
1297 + * without specific prior written permission.
1299 + * Alternatively, provided that this notice is retained in full, this
1300 + * software may be distributed under the terms of the GNU General
1301 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1302 + * file from the main directory of the linux kernel source.
1304 + * The provided data structures and external interfaces from this code
1305 + * are not restricted to be used by modules with a GPL compatible license.
1307 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1308 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1309 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1310 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1311 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1312 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1313 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1314 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1315 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1316 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1317 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1320 + * Send feedback to <socketcan-users@lists.berlios.de>
1327 +#include <linux/skbuff.h>
1328 +#include <linux/netdevice.h>
1329 +#include <linux/list.h>
1330 +#include <linux/rcupdate.h>
1331 +#include <linux/can.h>
1333 +/* af_can rx dispatcher structures */
1336 + struct hlist_node list;
1337 + struct rcu_head rcu;
1340 + unsigned long matches;
1341 + void (*func)(struct sk_buff *, void *);
1346 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1348 +struct dev_rcv_lists {
1349 + struct hlist_node list;
1350 + struct rcu_head rcu;
1351 + struct net_device *dev;
1352 + struct hlist_head rx[RX_MAX];
1353 + struct hlist_head rx_sff[0x800];
1354 + int remove_on_zero_entries;
1358 +/* statistic structures */
1361 + unsigned long jiffies_init;
1363 + unsigned long rx_frames;
1364 + unsigned long tx_frames;
1365 + unsigned long matches;
1367 + unsigned long total_rx_rate;
1368 + unsigned long total_tx_rate;
1369 + unsigned long total_rx_match_ratio;
1371 + unsigned long current_rx_rate;
1372 + unsigned long current_tx_rate;
1373 + unsigned long current_rx_match_ratio;
1375 + unsigned long max_rx_rate;
1376 + unsigned long max_tx_rate;
1377 + unsigned long max_rx_match_ratio;
1379 + unsigned long rx_frames_delta;
1380 + unsigned long tx_frames_delta;
1381 + unsigned long matches_delta;
1382 +}; /* can be reset e.g. by can_init_stats() */
1385 + unsigned long stats_reset;
1386 + unsigned long user_reset;
1387 + unsigned long rcv_entries;
1388 + unsigned long rcv_entries_max;
1389 +}; /* persistent statistics */
1391 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1392 +extern void can_init_proc(void);
1393 +extern void can_remove_proc(void);
1394 +extern void can_stat_update(unsigned long data);
1396 +/* structures and variables from af_can.c needed in proc.c for reading */
1397 +extern struct timer_list stattimer; /* timer for statistics update */
1398 +extern struct s_stats stats; /* packet statistics */
1399 +extern struct s_pstats pstats; /* receive list statistics */
1400 +extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
1402 +#endif /* AF_CAN_H */
1403 Index: net-2.6.23/net/can/proc.c
1404 ===================================================================
1405 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1406 +++ net-2.6.23/net/can/proc.c 2007-07-16 21:45:37.000000000 +0200
1409 + * proc.c - procfs support for Protocol family CAN core module
1411 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1412 + * All rights reserved.
1414 + * Redistribution and use in source and binary forms, with or without
1415 + * modification, are permitted provided that the following conditions
1417 + * 1. Redistributions of source code must retain the above copyright
1418 + * notice, this list of conditions, the following disclaimer and
1419 + * the referenced file 'COPYING'.
1420 + * 2. Redistributions in binary form must reproduce the above copyright
1421 + * notice, this list of conditions and the following disclaimer in the
1422 + * documentation and/or other materials provided with the distribution.
1423 + * 3. Neither the name of Volkswagen nor the names of its contributors
1424 + * may be used to endorse or promote products derived from this software
1425 + * without specific prior written permission.
1427 + * Alternatively, provided that this notice is retained in full, this
1428 + * software may be distributed under the terms of the GNU General
1429 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1430 + * file from the main directory of the linux kernel source.
1432 + * The provided data structures and external interfaces from this code
1433 + * are not restricted to be used by modules with a GPL compatible license.
1435 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1436 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1437 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1438 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1439 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1440 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1441 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1442 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1443 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1444 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1445 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1448 + * Send feedback to <socketcan-users@lists.berlios.de>
1452 +#include <linux/module.h>
1453 +#include <linux/proc_fs.h>
1454 +#include <linux/list.h>
1455 +#include <linux/rcupdate.h>
1456 +#include <linux/can/core.h>
1458 +#include "af_can.h"
1461 + * proc filenames for the PF_CAN core
1464 +#define CAN_PROC_VERSION "version"
1465 +#define CAN_PROC_STATS "stats"
1466 +#define CAN_PROC_RESET_STATS "reset_stats"
1467 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
1468 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
1469 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
1470 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
1471 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
1472 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
1474 +static struct proc_dir_entry *can_dir;
1475 +static struct proc_dir_entry *pde_version;
1476 +static struct proc_dir_entry *pde_stats;
1477 +static struct proc_dir_entry *pde_reset_stats;
1478 +static struct proc_dir_entry *pde_rcvlist_all;
1479 +static struct proc_dir_entry *pde_rcvlist_fil;
1480 +static struct proc_dir_entry *pde_rcvlist_inv;
1481 +static struct proc_dir_entry *pde_rcvlist_sff;
1482 +static struct proc_dir_entry *pde_rcvlist_eff;
1483 +static struct proc_dir_entry *pde_rcvlist_err;
1485 +static int user_reset;
1487 +static const char *rx_list_name[] = {
1488 + [RX_ERR] = "rx_err",
1489 + [RX_ALL] = "rx_all",
1490 + [RX_FIL] = "rx_fil",
1491 + [RX_INV] = "rx_inv",
1492 + [RX_EFF] = "rx_eff",
1496 + * af_can statistics stuff
1499 +static void can_init_stats(void)
1502 + * This memset function is called from a timer context (when
1503 + * stattimer is active which is the default) OR in a process
1504 + * context (reading the proc_fs when stattimer is disabled).
1506 + memset(&stats, 0, sizeof(stats));
1507 + stats.jiffies_init = jiffies;
1509 + pstats.stats_reset++;
1513 + pstats.user_reset++;
1517 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1518 + unsigned long count)
1520 + unsigned long ret = 0;
1522 + if (oldjif == newjif)
1525 + /* see can_rcv() - this should NEVER happen! */
1526 + if (count > (ULONG_MAX / HZ)) {
1527 + printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
1532 + ret = (count * HZ) / (newjif - oldjif);
1537 +void can_stat_update(unsigned long data)
1539 + unsigned long j = jiffies; /* snapshot */
1541 + /* restart counting in timer context on user request */
1545 + /* restart counting on jiffies overflow */
1546 + if (j < stats.jiffies_init)
1549 + /* stats.rx_frames is the definitively max. statistic value */
1551 + /* prevent overflow in calc_rate() */
1552 + if (stats.rx_frames > (ULONG_MAX / HZ))
1555 + /* matches overflow - very improbable */
1556 + if (stats.matches > (ULONG_MAX / 100))
1559 + /* calc total values */
1560 + if (stats.rx_frames)
1561 + stats.total_rx_match_ratio = (stats.matches * 100) /
1564 + stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1566 + stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1569 + /* calc current values */
1570 + if (stats.rx_frames_delta)
1571 + stats.current_rx_match_ratio =
1572 + (stats.matches_delta * 100) / stats.rx_frames_delta;
1574 + stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
1575 + stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
1577 + /* check / update maximum values */
1578 + if (stats.max_tx_rate < stats.current_tx_rate)
1579 + stats.max_tx_rate = stats.current_tx_rate;
1581 + if (stats.max_rx_rate < stats.current_rx_rate)
1582 + stats.max_rx_rate = stats.current_rx_rate;
1584 + if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1585 + stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1587 + /* clear values for 'current rate' calculation */
1588 + stats.tx_frames_delta = 0;
1589 + stats.rx_frames_delta = 0;
1590 + stats.matches_delta = 0;
1592 + /* restart timer (one second) */
1593 + stattimer.expires = jiffies + HZ;
1594 + add_timer(&stattimer);
1598 + * proc read functions
1600 + * From known use-cases we expect about 10 entries in a receive list to be
1601 + * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
1605 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1606 + struct net_device *dev)
1608 + struct receiver *r;
1609 + struct hlist_node *n;
1612 + hlist_for_each_entry_rcu(r, n, rx_list, list) {
1613 + char *fmt = (r->can_id & CAN_EFF_FLAG)?
1614 + " %-5s %08X %08x %08x %08x %8ld %s\n" :
1615 + " %-5s %03X %08x %08lx %08lx %8ld %s\n";
1617 + len += snprintf(page + len, PAGE_SIZE - len, fmt,
1618 + DNAME(dev), r->can_id, r->mask,
1619 + (unsigned long)r->func, (unsigned long)r->data,
1620 + r->matches, r->ident);
1622 + /* does a typical line fit into the current buffer? */
1624 + /* 100 Bytes before end of buffer */
1625 + if (len > PAGE_SIZE - 100) {
1626 + /* mark output cut off */
1627 + len += snprintf(page + len, PAGE_SIZE - len,
1632 + rcu_read_unlock();
1637 +static int can_print_recv_banner(char *page, int len)
1640 + * can1. 00000000 00000000 00000000
1643 + len += snprintf(page + len, PAGE_SIZE - len,
1644 + " device can_id can_mask function"
1645 + " userdata matches ident\n");
1650 +static int can_proc_read_stats(char *page, char **start, off_t off,
1651 + int count, int *eof, void *data)
1655 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1656 + len += snprintf(page + len, PAGE_SIZE - len,
1657 + " %8ld transmitted frames (TXF)\n", stats.tx_frames);
1658 + len += snprintf(page + len, PAGE_SIZE - len,
1659 + " %8ld received frames (RXF)\n", stats.rx_frames);
1660 + len += snprintf(page + len, PAGE_SIZE - len,
1661 + " %8ld matched frames (RXMF)\n", stats.matches);
1663 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1665 + if (stattimer.function == can_stat_update) {
1666 + len += snprintf(page + len, PAGE_SIZE - len,
1667 + " %8ld %% total match ratio (RXMR)\n",
1668 + stats.total_rx_match_ratio);
1670 + len += snprintf(page + len, PAGE_SIZE - len,
1671 + " %8ld frames/s total tx rate (TXR)\n",
1672 + stats.total_tx_rate);
1673 + len += snprintf(page + len, PAGE_SIZE - len,
1674 + " %8ld frames/s total rx rate (RXR)\n",
1675 + stats.total_rx_rate);
1677 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1679 + len += snprintf(page + len, PAGE_SIZE - len,
1680 + " %8ld %% current match ratio (CRXMR)\n",
1681 + stats.current_rx_match_ratio);
1683 + len += snprintf(page + len, PAGE_SIZE - len,
1684 + " %8ld frames/s current tx rate (CTXR)\n",
1685 + stats.current_tx_rate);
1686 + len += snprintf(page + len, PAGE_SIZE - len,
1687 + " %8ld frames/s current rx rate (CRXR)\n",
1688 + stats.current_rx_rate);
1690 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1692 + len += snprintf(page + len, PAGE_SIZE - len,
1693 + " %8ld %% max match ratio (MRXMR)\n",
1694 + stats.max_rx_match_ratio);
1696 + len += snprintf(page + len, PAGE_SIZE - len,
1697 + " %8ld frames/s max tx rate (MTXR)\n",
1698 + stats.max_tx_rate);
1699 + len += snprintf(page + len, PAGE_SIZE - len,
1700 + " %8ld frames/s max rx rate (MRXR)\n",
1701 + stats.max_rx_rate);
1703 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1706 + len += snprintf(page + len, PAGE_SIZE - len,
1707 + " %8ld current receive list entries (CRCV)\n",
1708 + pstats.rcv_entries);
1709 + len += snprintf(page + len, PAGE_SIZE - len,
1710 + " %8ld maximum receive list entries (MRCV)\n",
1711 + pstats.rcv_entries_max);
1713 + if (pstats.stats_reset)
1714 + len += snprintf(page + len, PAGE_SIZE - len,
1715 + "\n %8ld statistic resets (STR)\n",
1716 + pstats.stats_reset);
1718 + if (pstats.user_reset)
1719 + len += snprintf(page + len, PAGE_SIZE - len,
1720 + " %8ld user statistic resets (USTR)\n",
1721 + pstats.user_reset);
1723 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1729 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1730 + int count, int *eof, void *data)
1736 + if (stattimer.function == can_stat_update) {
1737 + len += snprintf(page + len, PAGE_SIZE - len,
1738 + "Scheduled statistic reset #%ld.\n",
1739 + pstats.stats_reset + 1);
1742 + if (stats.jiffies_init != jiffies)
1745 + len += snprintf(page + len, PAGE_SIZE - len,
1746 + "Performed statistic reset #%ld.\n",
1747 + pstats.stats_reset);
1754 +static int can_proc_read_version(char *page, char **start, off_t off,
1755 + int count, int *eof, void *data)
1759 + len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1760 + CAN_VERSION_STRING);
1765 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1766 + int count, int *eof, void *data)
1768 + /* double cast to prevent GCC warning */
1769 + int idx = (int)(long)data;
1771 + struct dev_rcv_lists *d;
1772 + struct hlist_node *n;
1774 + len += snprintf(page + len, PAGE_SIZE - len,
1775 + "\nreceive list '%s':\n", rx_list_name[idx]);
1778 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1780 + if (!hlist_empty(&d->rx[idx])) {
1781 + len = can_print_recv_banner(page, len);
1782 + len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
1784 + len += snprintf(page + len, PAGE_SIZE - len,
1785 + " (%s: no entry)\n", DNAME(d->dev));
1787 + /* exit on end of buffer? */
1788 + if (len > PAGE_SIZE - 100)
1791 + rcu_read_unlock();
1793 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1799 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1800 + int count, int *eof, void *data)
1803 + struct dev_rcv_lists *d;
1804 + struct hlist_node *n;
1807 + len += snprintf(page + len, PAGE_SIZE - len,
1808 + "\nreceive list 'rx_sff':\n");
1811 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1812 + int i, all_empty = 1;
1813 + /* check wether at least one list is non-empty */
1814 + for (i = 0; i < 0x800; i++)
1815 + if (!hlist_empty(&d->rx_sff[i])) {
1821 + len = can_print_recv_banner(page, len);
1822 + for (i = 0; i < 0x800; i++) {
1823 + if (!hlist_empty(&d->rx_sff[i]) &&
1824 + len < PAGE_SIZE - 100)
1825 + len = can_print_rcvlist(page, len,
1830 + len += snprintf(page + len, PAGE_SIZE - len,
1831 + " (%s: no entry)\n", DNAME(d->dev));
1833 + /* exit on end of buffer? */
1834 + if (len > PAGE_SIZE - 100)
1837 + rcu_read_unlock();
1839 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1846 + * proc utility functions
1849 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1851 + read_proc_t *read_proc,
1855 + return create_proc_read_entry(name, mode, can_dir, read_proc,
1861 +static void can_remove_proc_readentry(const char *name)
1864 + remove_proc_entry(name, can_dir);
1868 + * can_init_proc - create main CAN proc directory and procfs entries
1870 +void can_init_proc(void)
1872 + /* create /proc/net/can directory */
1873 + can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
1876 + printk(KERN_INFO "can: failed to create /proc/%s . "
1877 + "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
1881 + can_dir->owner = THIS_MODULE;
1883 + /* own procfs entries from the AF_CAN core */
1884 + pde_version = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
1885 + can_proc_read_version, NULL);
1886 + pde_stats = can_create_proc_readentry(CAN_PROC_STATS, 0644,
1887 + can_proc_read_stats, NULL);
1888 + pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
1889 + can_proc_read_reset_stats, NULL);
1890 + pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
1891 + can_proc_read_rcvlist, (void *)RX_ERR);
1892 + pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
1893 + can_proc_read_rcvlist, (void *)RX_ALL);
1894 + pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
1895 + can_proc_read_rcvlist, (void *)RX_FIL);
1896 + pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
1897 + can_proc_read_rcvlist, (void *)RX_INV);
1898 + pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
1899 + can_proc_read_rcvlist, (void *)RX_EFF);
1900 + pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
1901 + can_proc_read_rcvlist_sff, NULL);
1905 + * can_remove_proc - remove procfs entries and main CAN proc directory
1907 +void can_remove_proc(void)
1910 + can_remove_proc_readentry(CAN_PROC_VERSION);
1913 + can_remove_proc_readentry(CAN_PROC_STATS);
1915 + if (pde_reset_stats)
1916 + can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1918 + if (pde_rcvlist_err)
1919 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
1921 + if (pde_rcvlist_all)
1922 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
1924 + if (pde_rcvlist_fil)
1925 + can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
1927 + if (pde_rcvlist_inv)
1928 + can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
1930 + if (pde_rcvlist_eff)
1931 + can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
1933 + if (pde_rcvlist_sff)
1934 + can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
1937 + remove_proc_entry(CAN_PROC_DIR, NULL);
1939 Index: net-2.6.23/include/linux/can/error.h
1940 ===================================================================
1941 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1942 +++ net-2.6.23/include/linux/can/error.h 2007-07-09 13:23:28.000000000 +0200
1945 + * linux/can/error.h
1947 + * Definitions of the CAN error frame to be filtered and passed to the user.
1949 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
1950 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1951 + * All rights reserved.
1953 + * Send feedback to <socketcan-users@lists.berlios.de>
1957 +#ifndef CAN_ERROR_H
1958 +#define CAN_ERROR_H
1960 +#define CAN_ERR_DLC 8 /* dlc for error frames */
1962 +/* error class (mask) in can_id */
1963 +#define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */
1964 +#define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */
1965 +#define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */
1966 +#define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */
1967 +#define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */
1968 +#define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */
1969 +#define CAN_ERR_BUSOFF 0x00000040U /* bus off */
1970 +#define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */
1971 +#define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */
1973 +/* arbitration lost in bit ... / data[0] */
1974 +#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
1975 + /* else bit number in bitstream */
1977 +/* error status of CAN-controller / data[1] */
1978 +#define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */
1979 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
1980 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
1981 +#define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */
1982 +#define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */
1983 +#define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */
1984 +#define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */
1985 + /* (at least one error counter exceeds */
1986 + /* the protocol-defined level of 127) */
1988 +/* error in CAN protocol (type) / data[2] */
1989 +#define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */
1990 +#define CAN_ERR_PROT_BIT 0x01 /* single bit error */
1991 +#define CAN_ERR_PROT_FORM 0x02 /* frame format error */
1992 +#define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */
1993 +#define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */
1994 +#define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */
1995 +#define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */
1996 +#define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */
1997 +#define CAN_ERR_PROT_TX 0x80 /* error occured on transmission */
1999 +/* error in CAN protocol (location) / data[3] */
2000 +#define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */
2001 +#define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */
2002 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
2003 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
2004 +#define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */
2005 +#define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */
2006 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
2007 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
2008 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
2009 +#define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */
2010 +#define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */
2011 +#define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */
2012 +#define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */
2013 +#define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */
2014 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
2015 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
2016 +#define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */
2017 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
2018 +#define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */
2019 +#define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */
2021 +/* error status of CAN-transceiver / data[4] */
2023 +#define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */
2024 +#define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */
2025 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */
2026 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */
2027 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */
2028 +#define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */
2029 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */
2030 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */
2031 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */
2032 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
2034 +/* controller specific additional information / data[5..7] */
2036 +#endif /* CAN_ERROR_H */