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 | 111 +++++
13 include/linux/can/core.h | 77 +++
14 include/linux/can/error.h | 93 ++++
17 net/can/Kconfig | 25 +
19 net/can/af_can.c | 973 ++++++++++++++++++++++++++++++++++++++++++++++
20 net/can/af_can.h | 121 +++++
21 net/can/proc.c | 533 +++++++++++++++++++++++++
22 10 files changed, 1941 insertions(+)
24 Index: net-2.6.24/include/linux/can.h
25 ===================================================================
26 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
27 +++ net-2.6.24/include/linux/can.h 2007-09-25 13:14:46.000000000 +0200
32 + * Definitions for CAN network layer (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 */
62 + * Controller Area Network Identifier structure
64 + * bit 0-28 : CAN identifier (11/29 bit)
65 + * bit 29 : error frame flag (0 = data frame, 1 = error frame)
66 + * bit 30 : remote transmission request flag (1 = rtr frame)
67 + * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
69 +typedef __u32 canid_t;
72 + * Controller Area Network Error Frame Mask structure
74 + * bit 0-28 : error class mask (see include/linux/can/error.h)
75 + * bit 29-31 : set to zero
77 +typedef __u32 can_err_mask_t;
80 + * struct can_frame - basic CAN frame structure
81 + * @can_id: the CAN ID of the frame and CAN_*_FLAG flags, see above.
82 + * @can_dlc: the data length field of the CAN frame
83 + * @data: the CAN frame payload.
86 + canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
87 + __u8 can_dlc; /* data length code: 0 .. 8 */
88 + __u8 data[8] __attribute__((aligned(8)));
91 +/* particular protocols of the protocol family PF_CAN */
92 +#define CAN_RAW 1 /* RAW sockets */
93 +#define CAN_BCM 2 /* Broadcast Manager */
94 +#define CAN_TP16 3 /* VAG Transport Protocol v1.6 */
95 +#define CAN_TP20 4 /* VAG Transport Protocol v2.0 */
96 +#define CAN_MCNET 5 /* Bosch MCNet */
97 +#define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */
100 +#define SOL_CAN_BASE 100
103 + * struct sockaddr_can - the sockaddr structure for CAN sockets
104 + * @can_family: address family number AF_CAN.
105 + * @can_ifindex: CAN network interface index.
106 + * @can_addr: transport protocol specific address, mostly CAN IDs.
108 +struct sockaddr_can {
109 + sa_family_t can_family;
112 + struct { canid_t rx_id, tx_id; } tp16;
113 + struct { canid_t rx_id, tx_id; } tp20;
114 + struct { canid_t rx_id, tx_id; } mcnet;
115 + struct { canid_t rx_id, tx_id; } isotp;
120 + * struct can_filter - CAN ID based filter in can_register().
121 + * @can_id: relevant bits of CAN ID which are not masked out.
122 + * @can_mask: CAN mask (see description)
125 + * A filter matches, when
127 + * <received_can_id> & mask == can_id & mask
129 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
130 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
137 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
140 Index: net-2.6.24/include/linux/can/core.h
141 ===================================================================
142 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
143 +++ net-2.6.24/include/linux/can/core.h 2007-09-25 13:22:22.000000000 +0200
148 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
150 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
151 + * Urs Thuermann <urs.thuermann@volkswagen.de>
152 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
153 + * All rights reserved.
155 + * Send feedback to <socketcan-users@lists.berlios.de>
162 +#include <linux/can.h>
163 +#include <linux/skbuff.h>
164 +#include <linux/netdevice.h>
166 +#define CAN_VERSION "20070920"
168 +/* increment this number each time you change some user-space interface */
169 +#define CAN_ABI_VERSION "8"
171 +#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
173 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
176 + * struct can_proto - CAN protocol structure
177 + * @type: type argument in socket() syscall, e.g. SOCK_DGRAM.
178 + * @protocol: protocol number in socket() syscall.
179 + * @capability: capability needed to open the socket, or -1 for no restriction.
180 + * @ops: pointer to struct proto_ops for sock->ops.
181 + * @prot: pointer to struct proto structure.
187 + struct proto_ops *ops;
188 + struct proto *prot;
191 +/* function prototypes for the CAN networklayer core (af_can.c) */
193 +extern int can_proto_register(struct can_proto *cp);
194 +extern void can_proto_unregister(struct can_proto *cp);
196 +extern int can_rx_register(struct net_device *dev, canid_t can_id,
198 + void (*func)(struct sk_buff *, void *),
199 + void *data, char *ident);
201 +extern void can_rx_unregister(struct net_device *dev, canid_t can_id,
203 + void (*func)(struct sk_buff *, void *),
206 +extern int can_send(struct sk_buff *skb, int loop);
208 +#ifdef CONFIG_CAN_DEBUG_CORE
209 +extern void can_debug_skb(struct sk_buff *skb);
210 +extern void can_debug_cframe(const char *msg, struct can_frame *cframe);
211 +#define DBG(fmt, args...) (debug & 1 ? printk(KERN_DEBUG "can-" IDENT \
212 + " %s: " fmt, __func__, ##args) : 0)
213 +#define DBG_FRAME(fmt, cf) (debug & 2 ? can_debug_cframe(fmt, cf) : 0)
214 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
216 +#define DBG(fmt, args...)
217 +#define DBG_FRAME(fmt, cf)
218 +#define DBG_SKB(skb)
221 +#endif /* CAN_CORE_H */
222 Index: net-2.6.24/net/Kconfig
223 ===================================================================
224 --- net-2.6.24.orig/net/Kconfig 2007-09-24 17:53:08.000000000 +0200
225 +++ net-2.6.24/net/Kconfig 2007-09-25 13:14:46.000000000 +0200
229 source "net/ax25/Kconfig"
230 +source "net/can/Kconfig"
231 source "net/irda/Kconfig"
232 source "net/bluetooth/Kconfig"
233 source "net/rxrpc/Kconfig"
234 Index: net-2.6.24/net/Makefile
235 ===================================================================
236 --- net-2.6.24.orig/net/Makefile 2007-09-24 17:53:08.000000000 +0200
237 +++ net-2.6.24/net/Makefile 2007-09-25 13:14:46.000000000 +0200
239 obj-$(CONFIG_NETROM) += netrom/
240 obj-$(CONFIG_ROSE) += rose/
241 obj-$(CONFIG_AX25) += ax25/
242 +obj-$(CONFIG_CAN) += can/
243 obj-$(CONFIG_IRDA) += irda/
244 obj-$(CONFIG_BT) += bluetooth/
245 obj-$(CONFIG_SUNRPC) += sunrpc/
246 Index: net-2.6.24/net/can/Kconfig
247 ===================================================================
248 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
249 +++ net-2.6.24/net/can/Kconfig 2007-09-25 13:14:46.000000000 +0200
252 +# Controller Area Network (CAN) network layer core configuration
257 + tristate "CAN bus subsystem support"
259 + Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
260 + communications protocol, which was developed by Bosch at
261 + 1991 mainly for automotive, but now widely used in marine
262 + (NMEA2000), industrial and medical applications.
263 + More information on the CAN network protocol family PF_CAN
264 + is contained in <Documentation/networking/can.txt>.
266 + If you want CAN support, you should say Y here and also to the
267 + specific driver for your controller(s) below.
269 +config CAN_DEBUG_CORE
270 + bool "CAN Core debugging messages"
273 + Say Y here if you want the CAN core to produce a bunch of debug
274 + messages to the system log. Select this if you are having a
275 + problem with CAN support and want to see more of what is going on.
276 Index: net-2.6.24/net/can/Makefile
277 ===================================================================
278 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
279 +++ net-2.6.24/net/can/Makefile 2007-09-25 13:14:46.000000000 +0200
282 +# Makefile for the Linux Controller Area Network core.
285 +obj-$(CONFIG_CAN) += can.o
286 +can-objs := af_can.o proc.o
287 Index: net-2.6.24/net/can/af_can.c
288 ===================================================================
289 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
290 +++ net-2.6.24/net/can/af_can.c 2007-09-25 13:22:22.000000000 +0200
293 + * af_can.c - Protocol family CAN core module
294 + * (used by different CAN protocol modules)
296 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
297 + * All rights reserved.
299 + * Redistribution and use in source and binary forms, with or without
300 + * modification, are permitted provided that the following conditions
302 + * 1. Redistributions of source code must retain the above copyright
303 + * notice, this list of conditions, the following disclaimer and
304 + * the referenced file 'COPYING'.
305 + * 2. Redistributions in binary form must reproduce the above copyright
306 + * notice, this list of conditions and the following disclaimer in the
307 + * documentation and/or other materials provided with the distribution.
308 + * 3. Neither the name of Volkswagen nor the names of its contributors
309 + * may be used to endorse or promote products derived from this software
310 + * without specific prior written permission.
312 + * Alternatively, provided that this notice is retained in full, this
313 + * software may be distributed under the terms of the GNU General
314 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
315 + * file from the main directory of the linux kernel source.
317 + * The provided data structures and external interfaces from this code
318 + * are not restricted to be used by modules with a GPL compatible license.
320 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
321 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
322 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
323 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
324 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
325 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
326 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
328 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
329 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
330 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
333 + * Send feedback to <socketcan-users@lists.berlios.de>
337 +#include <linux/module.h>
338 +#include <linux/init.h>
339 +#include <linux/kmod.h>
340 +#include <linux/slab.h>
341 +#include <linux/list.h>
342 +#include <linux/spinlock.h>
343 +#include <linux/rcupdate.h>
344 +#include <linux/uaccess.h>
345 +#include <linux/net.h>
346 +#include <linux/netdevice.h>
347 +#include <linux/socket.h>
348 +#include <linux/if_ether.h>
349 +#include <linux/if_arp.h>
350 +#include <linux/skbuff.h>
351 +#include <linux/can.h>
352 +#include <linux/can/core.h>
353 +#include <net/net_namespace.h>
354 +#include <net/sock.h>
358 +#define IDENT "core"
359 +static __initdata const char banner[] = KERN_INFO
360 + "can: controller area network core (" CAN_VERSION_STRING ")\n";
362 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
363 +MODULE_LICENSE("Dual BSD/GPL");
364 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
365 + "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
367 +MODULE_ALIAS_NETPROTO(PF_CAN);
369 +static int stats_timer __read_mostly = 1;
370 +module_param(stats_timer, int, S_IRUGO);
371 +MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
373 +#ifdef CONFIG_CAN_DEBUG_CORE
374 +static int debug __read_mostly;
375 +module_param(debug, int, S_IRUGO);
376 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
379 +HLIST_HEAD(rx_dev_list);
380 +static struct dev_rcv_lists rx_alldev_list;
381 +static DEFINE_SPINLOCK(rcv_lists_lock);
383 +static struct kmem_cache *rcv_cache __read_mostly;
385 +/* table of registered CAN protocols */
386 +static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
387 +static DEFINE_SPINLOCK(proto_tab_lock);
389 +struct timer_list stattimer; /* timer for statistics update */
390 +struct s_stats stats; /* packet statistics */
391 +struct s_pstats pstats; /* receive list statistics */
394 + * af_can socket functions
397 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
399 + struct sock *sk = sock->sk;
404 + return sock_get_timestamp(sk, (struct timeval __user *)arg);
407 + return -ENOIOCTLCMD;
411 +static void can_sock_destruct(struct sock *sk)
413 + DBG("called for sock %p\n", sk);
415 + skb_queue_purge(&sk->sk_receive_queue);
416 + if (sk->sk_protinfo)
417 + kfree(sk->sk_protinfo);
420 +static int can_create(struct net *net, struct socket *sock, int protocol)
423 + struct can_proto *cp;
424 + char module_name[sizeof("can-proto-000")];
427 + DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
429 + sock->state = SS_UNCONNECTED;
431 + if (protocol < 0 || protocol >= CAN_NPROTO)
434 + if (net != &init_net)
435 + return -EAFNOSUPPORT;
437 + DBG("looking up proto %d in proto_tab[]\n", protocol);
439 + /* try to load protocol module, when CONFIG_KMOD is defined */
440 + if (!proto_tab[protocol]) {
441 + sprintf(module_name, "can-proto-%d", protocol);
442 + ret = request_module(module_name);
445 + * In case of error we only print a message but don't
446 + * return the error code immediately. Below we will
447 + * return -EPROTONOSUPPORT
449 + if (ret == -ENOSYS) {
450 + if (printk_ratelimit())
451 + printk(KERN_INFO "can: request_module(%s)"
452 + " not implemented.\n", module_name);
454 + if (printk_ratelimit())
455 + printk(KERN_ERR "can: request_module(%s)"
456 + " failed.\n", module_name);
460 + spin_lock(&proto_tab_lock);
461 + cp = proto_tab[protocol];
462 + if (cp && !try_module_get(cp->prot->owner))
464 + spin_unlock(&proto_tab_lock);
466 + /* check for success and correct type */
467 + if (!cp || cp->type != sock->type) {
468 + ret = -EPROTONOSUPPORT;
472 + if (cp->capability >= 0 && !capable(cp->capability)) {
477 + sock->ops = cp->ops;
479 + sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, 1);
485 + sock_init_data(sock, sk);
486 + sk->sk_destruct = can_sock_destruct;
488 + DBG("created sock: %p\n", sk);
490 + if (sk->sk_prot->init)
491 + ret = sk->sk_prot->init(sk);
494 + /* release sk on errors */
500 + module_put(cp->prot->owner);
509 + * can_send - transmit a CAN frame (optional with local loopback)
510 + * @skb: pointer to socket buffer with CAN frame in data section
511 + * @loop: loopback for listeners on local CAN sockets (recommended default!)
515 + * -ENETDOWN when the selected interface is down
516 + * -ENOBUFS on full driver queue (see net_xmit_errno())
517 + * -ENOMEM when local loopback failed at calling skb_clone()
518 + * -EPERM when trying to send on a non-CAN interface
520 +int can_send(struct sk_buff *skb, int loop)
524 + if (skb->dev->type != ARPHRD_CAN) {
529 + if (!(skb->dev->flags & IFF_UP)) {
534 + skb->protocol = htons(ETH_P_CAN);
537 + /* local loopback of sent CAN frames */
539 + /* indication for the CAN driver: do loopback */
540 + skb->pkt_type = PACKET_LOOPBACK;
543 + * The reference to the originating sock may be required
544 + * by the receiving socket to check whether the frame is
545 + * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
546 + * Therefore we have to ensure that skb->sk remains the
547 + * reference to the originating sock by restoring skb->sk
548 + * after each skb_clone() or skb_orphan() usage.
551 + if (!(skb->dev->flags & IFF_LOOPBACK)) {
553 + * If the interface is not capable to do loopback
554 + * itself, we do it here.
556 + struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
563 + newskb->sk = skb->sk;
564 + newskb->ip_summed = CHECKSUM_UNNECESSARY;
565 + newskb->pkt_type = PACKET_BROADCAST;
569 + /* indication for the CAN driver: no loopback required */
570 + skb->pkt_type = PACKET_HOST;
573 + /* send to netdevice */
574 + err = dev_queue_xmit(skb);
576 + err = net_xmit_errno(err);
578 + /* update statistics */
580 + stats.tx_frames_delta++;
584 +EXPORT_SYMBOL(can_send);
590 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
592 + struct dev_rcv_lists *d;
593 + struct hlist_node *n;
596 + * find receive list for this device
598 + * The hlist_for_each_entry*() macros curse through the list
599 + * using the pointer variable n and set d to the containing
600 + * struct in each list iteration. Therefore, after list
601 + * iteration, d is unmodified when the list is empty, and it
602 + * points to last list element, when the list is non-empty
603 + * but no match in the loop body is found. I.e. d is *not*
604 + * NULL when no match is found. We can, however, use the
605 + * cursor variable n to decide if a match was found.
608 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
613 + return n ? d : NULL;
616 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
617 + struct dev_rcv_lists *d)
619 + canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
621 + /* filter error frames */
622 + if (*mask & CAN_ERR_FLAG) {
623 + /* clear CAN_ERR_FLAG in list entry */
624 + *mask &= CAN_ERR_MASK;
625 + return &d->rx[RX_ERR];
628 + /* ensure valid values in can_mask */
629 + if (*mask & CAN_EFF_FLAG)
630 + *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
632 + *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
634 + /* reduce condition testing at receive time */
637 + /* inverse can_id/can_mask filter */
639 + return &d->rx[RX_INV];
641 + /* mask == 0 => no condition testing at receive time */
643 + return &d->rx[RX_ALL];
645 + /* use extra filterset for the subscription of exactly *ONE* can_id */
646 + if (*can_id & CAN_EFF_FLAG) {
647 + if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
648 + /* RFC: a use-case for hash-tables in the future? */
649 + return &d->rx[RX_EFF];
652 + if (*mask == CAN_SFF_MASK)
653 + return &d->rx_sff[*can_id];
656 + /* default: filter via can_id/can_mask */
657 + return &d->rx[RX_FIL];
661 + * can_rx_register - subscribe CAN frames from a specific interface
662 + * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
663 + * @can_id: CAN identifier (see description)
664 + * @mask: CAN mask (see description)
665 + * @func: callback function on filter match
666 + * @data: returned parameter for callback function
667 + * @ident: string for calling module indentification
670 + * Invokes the callback function with the received sk_buff and the given
671 + * parameter 'data' on a matching receive filter. A filter matches, when
673 + * <received_can_id> & mask == can_id & mask
675 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
676 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
680 + * -ENOMEM on missing cache mem to create subscription entry
681 + * -ENODEV unknown device
683 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
684 + void (*func)(struct sk_buff *, void *), void *data,
687 + struct receiver *r;
688 + struct hlist_head *rl;
689 + struct dev_rcv_lists *d;
692 + /* insert new receiver (dev,canid,mask) -> (func,data) */
694 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
695 + "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
697 + r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
701 + spin_lock(&rcv_lists_lock);
703 + d = find_dev_rcv_lists(dev);
705 + rl = find_rcv_list(&can_id, &mask, d);
707 + r->can_id = can_id;
714 + hlist_add_head_rcu(&r->list, rl);
717 + pstats.rcv_entries++;
718 + if (pstats.rcv_entries_max < pstats.rcv_entries)
719 + pstats.rcv_entries_max = pstats.rcv_entries;
721 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
722 + DNAME(dev), can_id, mask);
723 + kmem_cache_free(rcv_cache, r);
727 + spin_unlock(&rcv_lists_lock);
731 +EXPORT_SYMBOL(can_rx_register);
734 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
736 +static void can_rx_delete_device(struct rcu_head *rp)
738 + struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
740 + DBG("removing dev_rcv_list at %p\n", d);
745 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
747 +static void can_rx_delete_receiver(struct rcu_head *rp)
749 + struct receiver *r = container_of(rp, struct receiver, rcu);
751 + DBG("removing receiver at %p\n", r);
752 + kmem_cache_free(rcv_cache, r);
756 + * can_rx_unregister - unsubscribe CAN frames from a specific interface
757 + * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
758 + * @can_id: CAN identifier
760 + * @func: callback function on filter match
761 + * @data: returned parameter for callback function
764 + * Removes subscription entry depending on given (subscription) values.
766 +void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
767 + void (*func)(struct sk_buff *, void *), void *data)
769 + struct receiver *r = NULL;
770 + struct hlist_head *rl;
771 + struct hlist_node *next;
772 + struct dev_rcv_lists *d;
774 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
775 + dev, DNAME(dev), can_id, mask, func, data);
777 + spin_lock(&rcv_lists_lock);
779 + d = find_dev_rcv_lists(dev);
781 + printk(KERN_ERR "BUG: receive list not found for "
782 + "dev %s, id %03X, mask %03X\n",
783 + DNAME(dev), can_id, mask);
787 + rl = find_rcv_list(&can_id, &mask, d);
790 + * Search the receiver list for the item to delete. This should
791 + * exist, since no receiver may be unregistered that hasn't
792 + * been registered before.
795 + hlist_for_each_entry_rcu(r, next, rl, list) {
796 + if (r->can_id == can_id && r->mask == mask
797 + && r->func == func && r->data == data)
802 + * Check for bug in CAN protocol implementations:
803 + * If no matching list item was found, the list cursor variable next
804 + * will be NULL, while r will point to the last item of the list.
808 + printk(KERN_ERR "BUG: receive list entry not found for "
809 + "dev %s, id %03X, mask %03X\n",
810 + DNAME(dev), can_id, mask);
816 + hlist_del_rcu(&r->list);
819 + if (pstats.rcv_entries > 0)
820 + pstats.rcv_entries--;
822 + /* remove device structure requested by NETDEV_UNREGISTER */
823 + if (d->remove_on_zero_entries && !d->entries) {
824 + DBG("removing dev_rcv_list for %s on zero entries\n",
826 + hlist_del_rcu(&d->list);
831 + spin_unlock(&rcv_lists_lock);
833 + /* schedule the receiver item for deletion */
835 + call_rcu(&r->rcu, can_rx_delete_receiver);
837 + /* schedule the device structure for deletion */
839 + call_rcu(&d->rcu, can_rx_delete_device);
841 +EXPORT_SYMBOL(can_rx_unregister);
843 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
845 + struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
847 + DBG("skbuff %p cloned to %p\n", skb, clone);
849 + clone->sk = skb->sk;
850 + r->func(clone, r->data);
855 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
857 + struct receiver *r;
858 + struct hlist_node *n;
860 + struct can_frame *cf = (struct can_frame *)skb->data;
861 + canid_t can_id = cf->can_id;
863 + if (d->entries == 0)
866 + if (can_id & CAN_ERR_FLAG) {
867 + /* check for error frame entries only */
868 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
869 + if (can_id & r->mask) {
870 + DBG("match on rx_err skbuff %p\n", skb);
878 + /* check for unfiltered entries */
879 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
880 + DBG("match on rx_all skbuff %p\n", skb);
885 + /* check for can_id/mask entries */
886 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
887 + if ((can_id & r->mask) == r->can_id) {
888 + DBG("match on rx_fil skbuff %p\n", skb);
894 + /* check for inverted can_id/mask entries */
895 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
896 + if ((can_id & r->mask) != r->can_id) {
897 + DBG("match on rx_inv skbuff %p\n", skb);
903 + /* check CAN_ID specific entries */
904 + if (can_id & CAN_EFF_FLAG) {
905 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
906 + if (r->can_id == can_id) {
907 + DBG("match on rx_eff skbuff %p\n", skb);
913 + can_id &= CAN_SFF_MASK;
914 + hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
915 + DBG("match on rx_sff skbuff %p\n", skb);
924 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
925 + struct packet_type *pt, struct net_device *orig_dev)
927 + struct dev_rcv_lists *d;
930 + DBG("received skbuff on device %s, ptype %04x\n",
931 + dev->name, ntohs(pt->type));
933 + DBG_FRAME("af_can: can_rcv: received CAN frame",
934 + (struct can_frame *)skb->data);
936 + if (dev->type != ARPHRD_CAN || dev->nd_net != &init_net) {
941 + /* update statistics */
943 + stats.rx_frames_delta++;
947 + /* deliver the packet to sockets listening on all devices */
948 + matches = can_rcv_filter(&rx_alldev_list, skb);
950 + /* find receive list for this device */
951 + d = find_dev_rcv_lists(dev);
953 + matches += can_rcv_filter(d, skb);
957 + /* free the skbuff allocated by the netdevice driver */
958 + DBG("freeing skbuff %p\n", skb);
963 + stats.matches_delta++;
970 + * af_can protocol functions
974 + * can_proto_register - register CAN transport protocol
975 + * @cp: pointer to CAN protocol structure
979 + * -EINVAL invalid (out of range) protocol number
980 + * -EBUSY protocol already in use
981 + * -ENOBUF if proto_register() fails
983 +int can_proto_register(struct can_proto *cp)
985 + int proto = cp->protocol;
988 + if (proto < 0 || proto >= CAN_NPROTO) {
989 + printk(KERN_ERR "can: protocol number %d out of range\n",
994 + spin_lock(&proto_tab_lock);
995 + if (proto_tab[proto]) {
996 + printk(KERN_ERR "can: protocol %d already registered\n",
1002 + err = proto_register(cp->prot, 0);
1006 + proto_tab[proto] = cp;
1008 + /* use generic ioctl function if the module doesn't bring its own */
1009 + if (!cp->ops->ioctl)
1010 + cp->ops->ioctl = can_ioctl;
1013 + spin_unlock(&proto_tab_lock);
1017 +EXPORT_SYMBOL(can_proto_register);
1020 + * can_proto_unregister - unregister CAN transport protocol
1021 + * @cp: pointer to CAN protocol structure
1023 +void can_proto_unregister(struct can_proto *cp)
1025 + int proto = cp->protocol;
1027 + spin_lock(&proto_tab_lock);
1028 + if (!proto_tab[proto]) {
1029 + printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
1032 + proto_unregister(cp->prot);
1033 + proto_tab[proto] = NULL;
1034 + spin_unlock(&proto_tab_lock);
1036 +EXPORT_SYMBOL(can_proto_unregister);
1039 + * af_can notifier to create/remove CAN netdevice specific structs
1041 +static int can_notifier(struct notifier_block *nb, unsigned long msg,
1044 + struct net_device *dev = (struct net_device *)data;
1045 + struct dev_rcv_lists *d;
1047 + DBG("msg %ld for dev %p (%s idx %d)\n",
1048 + msg, dev, dev->name, dev->ifindex);
1050 + if (dev->nd_net != &init_net)
1051 + return NOTIFY_DONE;
1053 + if (dev->type != ARPHRD_CAN)
1054 + return NOTIFY_DONE;
1058 + case NETDEV_REGISTER:
1061 + * create new dev_rcv_lists for this device
1063 + * N.B. zeroing the struct is the correct initialization
1064 + * for the embedded hlist_head structs.
1065 + * Another list type, e.g. list_head, would require
1066 + * explicit initialization.
1069 + DBG("creating new dev_rcv_lists for %s\n", dev->name);
1071 + d = kzalloc(sizeof(*d), GFP_KERNEL);
1074 + "can: allocation of receive list failed\n");
1075 + return NOTIFY_DONE;
1079 + spin_lock(&rcv_lists_lock);
1080 + hlist_add_head_rcu(&d->list, &rx_dev_list);
1081 + spin_unlock(&rcv_lists_lock);
1085 + case NETDEV_UNREGISTER:
1086 + spin_lock(&rcv_lists_lock);
1088 + d = find_dev_rcv_lists(dev);
1090 + DBG("remove dev_rcv_list for %s (%d entries)\n",
1091 + dev->name, d->entries);
1094 + d->remove_on_zero_entries = 1;
1097 + hlist_del_rcu(&d->list);
1099 + printk(KERN_ERR "can: notifier: receive list not "
1100 + "found for dev %s\n", dev->name);
1102 + spin_unlock(&rcv_lists_lock);
1105 + call_rcu(&d->rcu, can_rx_delete_device);
1110 + return NOTIFY_DONE;
1114 + * af_can debugging stuff
1117 +#ifdef CONFIG_CAN_DEBUG_CORE
1120 + * can_debug_cframe - print CAN frame
1121 + * @msg: pointer to message printed before the given CAN frame
1122 + * @cf: pointer to CAN frame
1124 +void can_debug_cframe(const char *msg, struct can_frame *cf)
1130 + dlc = cf->can_dlc;
1134 + if (cf->can_id & CAN_EFF_FLAG)
1135 + sprintf(idbuf, "<%08X>", cf->can_id & CAN_EFF_MASK);
1137 + sprintf(idbuf, "<%03X>", cf->can_id & CAN_SFF_MASK);
1139 + if (cf->can_id & CAN_RTR_FLAG)
1140 + sprintf(hexbuf, "(RTR)");
1142 + hex_dump_to_buffer(cf->data, dlc, 16, 1, hexbuf, 28, 0);
1144 + printk(KERN_DEBUG "%s: %s [%d] %s\n", msg, idbuf, dlc, hexbuf);
1146 +EXPORT_SYMBOL(can_debug_cframe);
1149 + * can_debug_skb - print socket buffer content to kernel log
1150 + * @skb: pointer to socket buffer
1152 +void can_debug_skb(struct sk_buff *skb)
1154 + printk(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\n",
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);
1165 + print_hex_dump(KERN_DEBUG, "skb_head: ", DUMP_PREFIX_NONE,
1166 + 16, 1, skb->head, skb->end - skb->head, 0);
1168 +EXPORT_SYMBOL(can_debug_skb);
1173 + * af_can module init/exit functions
1176 +static struct packet_type can_packet __read_mostly = {
1177 + .type = __constant_htons(ETH_P_CAN),
1182 +static struct net_proto_family can_family_ops __read_mostly = {
1184 + .create = can_create,
1185 + .owner = THIS_MODULE,
1188 +/* notifier block for netdevice event */
1189 +static struct notifier_block can_netdev_notifier __read_mostly = {
1190 + .notifier_call = can_notifier,
1193 +static __init int can_init(void)
1197 + rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1203 + * Insert rx_alldev_list for reception on all devices.
1204 + * This struct is zero initialized which is correct for the
1205 + * embedded hlist heads, the dev pointer, and the entries counter.
1208 + spin_lock(&rcv_lists_lock);
1209 + hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1210 + spin_unlock(&rcv_lists_lock);
1212 + if (stats_timer) {
1213 + /* the statistics are updated every second (timer triggered) */
1214 + init_timer(&stattimer);
1215 + stattimer.function = can_stat_update;
1216 + stattimer.data = 0;
1217 + /* update every second */
1218 + stattimer.expires = round_jiffies(jiffies + HZ);
1219 + /* start statistics timer */
1220 + add_timer(&stattimer);
1222 + stattimer.function = NULL;
1227 + /* protocol register */
1228 + sock_register(&can_family_ops);
1229 + register_netdevice_notifier(&can_netdev_notifier);
1230 + dev_add_pack(&can_packet);
1235 +static __exit void can_exit(void)
1237 + struct dev_rcv_lists *d;
1238 + struct hlist_node *n, *next;
1241 + del_timer(&stattimer);
1243 + /* procfs remove */
1244 + can_remove_proc();
1246 + /* protocol unregister */
1247 + dev_remove_pack(&can_packet);
1248 + unregister_netdevice_notifier(&can_netdev_notifier);
1249 + sock_unregister(PF_CAN);
1251 + /* remove rx_dev_list */
1252 + spin_lock(&rcv_lists_lock);
1253 + hlist_del(&rx_alldev_list.list);
1254 + hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1255 + hlist_del(&d->list);
1258 + spin_unlock(&rcv_lists_lock);
1260 + kmem_cache_destroy(rcv_cache);
1263 +module_init(can_init);
1264 +module_exit(can_exit);
1265 Index: net-2.6.24/net/can/af_can.h
1266 ===================================================================
1267 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1268 +++ net-2.6.24/net/can/af_can.h 2007-09-25 13:14:46.000000000 +0200
1271 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1272 + * All rights reserved.
1274 + * Redistribution and use in source and binary forms, with or without
1275 + * modification, are permitted provided that the following conditions
1277 + * 1. Redistributions of source code must retain the above copyright
1278 + * notice, this list of conditions, the following disclaimer and
1279 + * the referenced file 'COPYING'.
1280 + * 2. Redistributions in binary form must reproduce the above copyright
1281 + * notice, this list of conditions and the following disclaimer in the
1282 + * documentation and/or other materials provided with the distribution.
1283 + * 3. Neither the name of Volkswagen nor the names of its contributors
1284 + * may be used to endorse or promote products derived from this software
1285 + * without specific prior written permission.
1287 + * Alternatively, provided that this notice is retained in full, this
1288 + * software may be distributed under the terms of the GNU General
1289 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1290 + * file from the main directory of the linux kernel source.
1292 + * The provided data structures and external interfaces from this code
1293 + * are not restricted to be used by modules with a GPL compatible license.
1295 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1296 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1297 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1298 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1299 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1300 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1301 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1302 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1303 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1304 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1305 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1308 + * Send feedback to <socketcan-users@lists.berlios.de>
1315 +#include <linux/skbuff.h>
1316 +#include <linux/netdevice.h>
1317 +#include <linux/list.h>
1318 +#include <linux/rcupdate.h>
1319 +#include <linux/can.h>
1321 +/* af_can rx dispatcher structures */
1324 + struct hlist_node list;
1325 + struct rcu_head rcu;
1328 + unsigned long matches;
1329 + void (*func)(struct sk_buff *, void *);
1334 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1336 +struct dev_rcv_lists {
1337 + struct hlist_node list;
1338 + struct rcu_head rcu;
1339 + struct net_device *dev;
1340 + struct hlist_head rx[RX_MAX];
1341 + struct hlist_head rx_sff[0x800];
1342 + int remove_on_zero_entries;
1346 +/* statistic structures */
1349 + unsigned long jiffies_init;
1351 + unsigned long rx_frames;
1352 + unsigned long tx_frames;
1353 + unsigned long matches;
1355 + unsigned long total_rx_rate;
1356 + unsigned long total_tx_rate;
1357 + unsigned long total_rx_match_ratio;
1359 + unsigned long current_rx_rate;
1360 + unsigned long current_tx_rate;
1361 + unsigned long current_rx_match_ratio;
1363 + unsigned long max_rx_rate;
1364 + unsigned long max_tx_rate;
1365 + unsigned long max_rx_match_ratio;
1367 + unsigned long rx_frames_delta;
1368 + unsigned long tx_frames_delta;
1369 + unsigned long matches_delta;
1370 +}; /* can be reset e.g. by can_init_stats() */
1373 + unsigned long stats_reset;
1374 + unsigned long user_reset;
1375 + unsigned long rcv_entries;
1376 + unsigned long rcv_entries_max;
1377 +}; /* persistent statistics */
1379 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1380 +extern void can_init_proc(void);
1381 +extern void can_remove_proc(void);
1382 +extern void can_stat_update(unsigned long data);
1384 +/* structures and variables from af_can.c needed in proc.c for reading */
1385 +extern struct timer_list stattimer; /* timer for statistics update */
1386 +extern struct s_stats stats; /* packet statistics */
1387 +extern struct s_pstats pstats; /* receive list statistics */
1388 +extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
1390 +#endif /* AF_CAN_H */
1391 Index: net-2.6.24/net/can/proc.c
1392 ===================================================================
1393 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1394 +++ net-2.6.24/net/can/proc.c 2007-09-25 13:22:22.000000000 +0200
1397 + * proc.c - procfs support for Protocol family CAN core module
1399 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1400 + * All rights reserved.
1402 + * Redistribution and use in source and binary forms, with or without
1403 + * modification, are permitted provided that the following conditions
1405 + * 1. Redistributions of source code must retain the above copyright
1406 + * notice, this list of conditions, the following disclaimer and
1407 + * the referenced file 'COPYING'.
1408 + * 2. Redistributions in binary form must reproduce the above copyright
1409 + * notice, this list of conditions and the following disclaimer in the
1410 + * documentation and/or other materials provided with the distribution.
1411 + * 3. Neither the name of Volkswagen nor the names of its contributors
1412 + * may be used to endorse or promote products derived from this software
1413 + * without specific prior written permission.
1415 + * Alternatively, provided that this notice is retained in full, this
1416 + * software may be distributed under the terms of the GNU General
1417 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1418 + * file from the main directory of the linux kernel source.
1420 + * The provided data structures and external interfaces from this code
1421 + * are not restricted to be used by modules with a GPL compatible license.
1423 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1424 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1425 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1426 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1427 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1428 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1429 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1430 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1431 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1432 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1433 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1436 + * Send feedback to <socketcan-users@lists.berlios.de>
1440 +#include <linux/module.h>
1441 +#include <linux/proc_fs.h>
1442 +#include <linux/list.h>
1443 +#include <linux/rcupdate.h>
1444 +#include <linux/can/core.h>
1446 +#include "af_can.h"
1449 + * proc filenames for the PF_CAN core
1452 +#define CAN_PROC_VERSION "version"
1453 +#define CAN_PROC_STATS "stats"
1454 +#define CAN_PROC_RESET_STATS "reset_stats"
1455 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
1456 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
1457 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
1458 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
1459 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
1460 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
1462 +static struct proc_dir_entry *can_dir;
1463 +static struct proc_dir_entry *pde_version;
1464 +static struct proc_dir_entry *pde_stats;
1465 +static struct proc_dir_entry *pde_reset_stats;
1466 +static struct proc_dir_entry *pde_rcvlist_all;
1467 +static struct proc_dir_entry *pde_rcvlist_fil;
1468 +static struct proc_dir_entry *pde_rcvlist_inv;
1469 +static struct proc_dir_entry *pde_rcvlist_sff;
1470 +static struct proc_dir_entry *pde_rcvlist_eff;
1471 +static struct proc_dir_entry *pde_rcvlist_err;
1473 +static int user_reset;
1475 +static const char rx_list_name[][8] = {
1476 + [RX_ERR] = "rx_err",
1477 + [RX_ALL] = "rx_all",
1478 + [RX_FIL] = "rx_fil",
1479 + [RX_INV] = "rx_inv",
1480 + [RX_EFF] = "rx_eff",
1484 + * af_can statistics stuff
1487 +static void can_init_stats(void)
1490 + * This memset function is called from a timer context (when
1491 + * stattimer is active which is the default) OR in a process
1492 + * context (reading the proc_fs when stattimer is disabled).
1494 + memset(&stats, 0, sizeof(stats));
1495 + stats.jiffies_init = jiffies;
1497 + pstats.stats_reset++;
1501 + pstats.user_reset++;
1505 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1506 + unsigned long count)
1508 + unsigned long ret = 0;
1510 + if (oldjif == newjif)
1513 + /* see can_stat_update() - this should NEVER happen! */
1514 + if (count > (ULONG_MAX / HZ)) {
1515 + printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
1520 + ret = (count * HZ) / (newjif - oldjif);
1525 +void can_stat_update(unsigned long data)
1527 + unsigned long j = jiffies; /* snapshot */
1529 + /* restart counting in timer context on user request */
1533 + /* restart counting on jiffies overflow */
1534 + if (j < stats.jiffies_init)
1537 + /* prevent overflow in calc_rate() */
1538 + if (stats.rx_frames > (ULONG_MAX / HZ))
1541 + /* prevent overflow in calc_rate() */
1542 + if (stats.tx_frames > (ULONG_MAX / HZ))
1545 + /* matches overflow - very improbable */
1546 + if (stats.matches > (ULONG_MAX / 100))
1549 + /* calc total values */
1550 + if (stats.rx_frames)
1551 + stats.total_rx_match_ratio = (stats.matches * 100) /
1554 + stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1556 + stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1559 + /* calc current values */
1560 + if (stats.rx_frames_delta)
1561 + stats.current_rx_match_ratio =
1562 + (stats.matches_delta * 100) / stats.rx_frames_delta;
1564 + stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
1565 + stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
1567 + /* check / update maximum values */
1568 + if (stats.max_tx_rate < stats.current_tx_rate)
1569 + stats.max_tx_rate = stats.current_tx_rate;
1571 + if (stats.max_rx_rate < stats.current_rx_rate)
1572 + stats.max_rx_rate = stats.current_rx_rate;
1574 + if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1575 + stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1577 + /* clear values for 'current rate' calculation */
1578 + stats.tx_frames_delta = 0;
1579 + stats.rx_frames_delta = 0;
1580 + stats.matches_delta = 0;
1582 + /* restart timer (one second) */
1583 + stattimer.expires = round_jiffies(jiffies + HZ);
1584 + add_timer(&stattimer);
1588 + * proc read functions
1590 + * From known use-cases we expect about 10 entries in a receive list to be
1591 + * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
1595 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1596 + struct net_device *dev)
1598 + struct receiver *r;
1599 + struct hlist_node *n;
1602 + hlist_for_each_entry_rcu(r, n, rx_list, list) {
1603 + char *fmt = (r->can_id & CAN_EFF_FLAG)?
1604 + " %-5s %08X %08x %08x %08x %8ld %s\n" :
1605 + " %-5s %03X %08x %08lx %08lx %8ld %s\n";
1607 + len += snprintf(page + len, PAGE_SIZE - len, fmt,
1608 + DNAME(dev), r->can_id, r->mask,
1609 + (unsigned long)r->func, (unsigned long)r->data,
1610 + r->matches, r->ident);
1612 + /* does a typical line fit into the current buffer? */
1614 + /* 100 Bytes before end of buffer */
1615 + if (len > PAGE_SIZE - 100) {
1616 + /* mark output cut off */
1617 + len += snprintf(page + len, PAGE_SIZE - len,
1622 + rcu_read_unlock();
1627 +static int can_print_recv_banner(char *page, int len)
1630 + * can1. 00000000 00000000 00000000
1633 + len += snprintf(page + len, PAGE_SIZE - len,
1634 + " device can_id can_mask function"
1635 + " userdata matches ident\n");
1640 +static int can_proc_read_stats(char *page, char **start, off_t off,
1641 + int count, int *eof, void *data)
1645 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1646 + len += snprintf(page + len, PAGE_SIZE - len,
1647 + " %8ld transmitted frames (TXF)\n", stats.tx_frames);
1648 + len += snprintf(page + len, PAGE_SIZE - len,
1649 + " %8ld received frames (RXF)\n", stats.rx_frames);
1650 + len += snprintf(page + len, PAGE_SIZE - len,
1651 + " %8ld matched frames (RXMF)\n", stats.matches);
1653 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1655 + if (stattimer.function == can_stat_update) {
1656 + len += snprintf(page + len, PAGE_SIZE - len,
1657 + " %8ld %% total match ratio (RXMR)\n",
1658 + stats.total_rx_match_ratio);
1660 + len += snprintf(page + len, PAGE_SIZE - len,
1661 + " %8ld frames/s total tx rate (TXR)\n",
1662 + stats.total_tx_rate);
1663 + len += snprintf(page + len, PAGE_SIZE - len,
1664 + " %8ld frames/s total rx rate (RXR)\n",
1665 + stats.total_rx_rate);
1667 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1669 + len += snprintf(page + len, PAGE_SIZE - len,
1670 + " %8ld %% current match ratio (CRXMR)\n",
1671 + stats.current_rx_match_ratio);
1673 + len += snprintf(page + len, PAGE_SIZE - len,
1674 + " %8ld frames/s current tx rate (CTXR)\n",
1675 + stats.current_tx_rate);
1676 + len += snprintf(page + len, PAGE_SIZE - len,
1677 + " %8ld frames/s current rx rate (CRXR)\n",
1678 + stats.current_rx_rate);
1680 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1682 + len += snprintf(page + len, PAGE_SIZE - len,
1683 + " %8ld %% max match ratio (MRXMR)\n",
1684 + stats.max_rx_match_ratio);
1686 + len += snprintf(page + len, PAGE_SIZE - len,
1687 + " %8ld frames/s max tx rate (MTXR)\n",
1688 + stats.max_tx_rate);
1689 + len += snprintf(page + len, PAGE_SIZE - len,
1690 + " %8ld frames/s max rx rate (MRXR)\n",
1691 + stats.max_rx_rate);
1693 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1696 + len += snprintf(page + len, PAGE_SIZE - len,
1697 + " %8ld current receive list entries (CRCV)\n",
1698 + pstats.rcv_entries);
1699 + len += snprintf(page + len, PAGE_SIZE - len,
1700 + " %8ld maximum receive list entries (MRCV)\n",
1701 + pstats.rcv_entries_max);
1703 + if (pstats.stats_reset)
1704 + len += snprintf(page + len, PAGE_SIZE - len,
1705 + "\n %8ld statistic resets (STR)\n",
1706 + pstats.stats_reset);
1708 + if (pstats.user_reset)
1709 + len += snprintf(page + len, PAGE_SIZE - len,
1710 + " %8ld user statistic resets (USTR)\n",
1711 + pstats.user_reset);
1713 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1719 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1720 + int count, int *eof, void *data)
1726 + if (stattimer.function == can_stat_update) {
1727 + len += snprintf(page + len, PAGE_SIZE - len,
1728 + "Scheduled statistic reset #%ld.\n",
1729 + pstats.stats_reset + 1);
1732 + if (stats.jiffies_init != jiffies)
1735 + len += snprintf(page + len, PAGE_SIZE - len,
1736 + "Performed statistic reset #%ld.\n",
1737 + pstats.stats_reset);
1744 +static int can_proc_read_version(char *page, char **start, off_t off,
1745 + int count, int *eof, void *data)
1749 + len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1750 + CAN_VERSION_STRING);
1755 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1756 + int count, int *eof, void *data)
1758 + /* double cast to prevent GCC warning */
1759 + int idx = (int)(long)data;
1761 + struct dev_rcv_lists *d;
1762 + struct hlist_node *n;
1764 + len += snprintf(page + len, PAGE_SIZE - len,
1765 + "\nreceive list '%s':\n", rx_list_name[idx]);
1768 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1770 + if (!hlist_empty(&d->rx[idx])) {
1771 + len = can_print_recv_banner(page, len);
1772 + len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
1774 + len += snprintf(page + len, PAGE_SIZE - len,
1775 + " (%s: no entry)\n", DNAME(d->dev));
1777 + /* exit on end of buffer? */
1778 + if (len > PAGE_SIZE - 100)
1781 + rcu_read_unlock();
1783 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1789 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1790 + int count, int *eof, void *data)
1793 + struct dev_rcv_lists *d;
1794 + struct hlist_node *n;
1797 + len += snprintf(page + len, PAGE_SIZE - len,
1798 + "\nreceive list 'rx_sff':\n");
1801 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1802 + int i, all_empty = 1;
1803 + /* check wether at least one list is non-empty */
1804 + for (i = 0; i < 0x800; i++)
1805 + if (!hlist_empty(&d->rx_sff[i])) {
1811 + len = can_print_recv_banner(page, len);
1812 + for (i = 0; i < 0x800; i++) {
1813 + if (!hlist_empty(&d->rx_sff[i]) &&
1814 + len < PAGE_SIZE - 100)
1815 + len = can_print_rcvlist(page, len,
1820 + len += snprintf(page + len, PAGE_SIZE - len,
1821 + " (%s: no entry)\n", DNAME(d->dev));
1823 + /* exit on end of buffer? */
1824 + if (len > PAGE_SIZE - 100)
1827 + rcu_read_unlock();
1829 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1836 + * proc utility functions
1839 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1841 + read_proc_t *read_proc,
1845 + return create_proc_read_entry(name, mode, can_dir, read_proc,
1851 +static void can_remove_proc_readentry(const char *name)
1854 + remove_proc_entry(name, can_dir);
1858 + * can_init_proc - create main CAN proc directory and procfs entries
1860 +void can_init_proc(void)
1862 + /* create /proc/net/can directory */
1863 + can_dir = proc_mkdir("can", init_net.proc_net);
1866 + printk(KERN_INFO "can: failed to create /proc/net/can . "
1867 + "CONFIG_PROC_FS missing?\n");
1871 + can_dir->owner = THIS_MODULE;
1873 + /* own procfs entries from the AF_CAN core */
1874 + pde_version = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
1875 + can_proc_read_version, NULL);
1876 + pde_stats = can_create_proc_readentry(CAN_PROC_STATS, 0644,
1877 + can_proc_read_stats, NULL);
1878 + pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
1879 + can_proc_read_reset_stats, NULL);
1880 + pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
1881 + can_proc_read_rcvlist, (void *)RX_ERR);
1882 + pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
1883 + can_proc_read_rcvlist, (void *)RX_ALL);
1884 + pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
1885 + can_proc_read_rcvlist, (void *)RX_FIL);
1886 + pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
1887 + can_proc_read_rcvlist, (void *)RX_INV);
1888 + pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
1889 + can_proc_read_rcvlist, (void *)RX_EFF);
1890 + pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
1891 + can_proc_read_rcvlist_sff, NULL);
1895 + * can_remove_proc - remove procfs entries and main CAN proc directory
1897 +void can_remove_proc(void)
1900 + can_remove_proc_readentry(CAN_PROC_VERSION);
1903 + can_remove_proc_readentry(CAN_PROC_STATS);
1905 + if (pde_reset_stats)
1906 + can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1908 + if (pde_rcvlist_err)
1909 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
1911 + if (pde_rcvlist_all)
1912 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
1914 + if (pde_rcvlist_fil)
1915 + can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
1917 + if (pde_rcvlist_inv)
1918 + can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
1920 + if (pde_rcvlist_eff)
1921 + can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
1923 + if (pde_rcvlist_sff)
1924 + can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
1927 + proc_net_remove(&init_net, "can");
1929 Index: net-2.6.24/include/linux/can/error.h
1930 ===================================================================
1931 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1932 +++ net-2.6.24/include/linux/can/error.h 2007-09-25 13:14:46.000000000 +0200
1935 + * linux/can/error.h
1937 + * Definitions of the CAN error frame to be filtered and passed to the user.
1939 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
1940 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1941 + * All rights reserved.
1943 + * Send feedback to <socketcan-users@lists.berlios.de>
1947 +#ifndef CAN_ERROR_H
1948 +#define CAN_ERROR_H
1950 +#define CAN_ERR_DLC 8 /* dlc for error frames */
1952 +/* error class (mask) in can_id */
1953 +#define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */
1954 +#define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */
1955 +#define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */
1956 +#define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */
1957 +#define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */
1958 +#define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */
1959 +#define CAN_ERR_BUSOFF 0x00000040U /* bus off */
1960 +#define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */
1961 +#define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */
1963 +/* arbitration lost in bit ... / data[0] */
1964 +#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
1965 + /* else bit number in bitstream */
1967 +/* error status of CAN-controller / data[1] */
1968 +#define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */
1969 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
1970 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
1971 +#define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */
1972 +#define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */
1973 +#define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */
1974 +#define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */
1975 + /* (at least one error counter exceeds */
1976 + /* the protocol-defined level of 127) */
1978 +/* error in CAN protocol (type) / data[2] */
1979 +#define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */
1980 +#define CAN_ERR_PROT_BIT 0x01 /* single bit error */
1981 +#define CAN_ERR_PROT_FORM 0x02 /* frame format error */
1982 +#define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */
1983 +#define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */
1984 +#define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */
1985 +#define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */
1986 +#define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */
1987 +#define CAN_ERR_PROT_TX 0x80 /* error occured on transmission */
1989 +/* error in CAN protocol (location) / data[3] */
1990 +#define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */
1991 +#define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */
1992 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
1993 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
1994 +#define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */
1995 +#define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */
1996 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
1997 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
1998 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
1999 +#define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */
2000 +#define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */
2001 +#define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */
2002 +#define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */
2003 +#define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */
2004 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
2005 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
2006 +#define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */
2007 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
2008 +#define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */
2009 +#define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */
2011 +/* error status of CAN-transceiver / data[4] */
2013 +#define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */
2014 +#define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */
2015 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */
2016 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */
2017 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */
2018 +#define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */
2019 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */
2020 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */
2021 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */
2022 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
2024 +/* controller specific additional information / data[5..7] */
2026 +#endif /* CAN_ERROR_H */