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 | 113 +++++
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, 1966 insertions(+)
24 Index: net-2.6/include/linux/can.h
25 ===================================================================
26 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
27 +++ net-2.6/include/linux/can.h 2007-08-03 11:21:46.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 */
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 */
98 +#define CAN_BAP 7 /* VAG Bedien- und Anzeigeprotokoll */
101 +#define SOL_CAN_BASE 100
104 + * struct sockaddr_can - the sockaddr structure for CAN sockets
105 + * @can_family: address family number AF_CAN.
106 + * @can_ifindex: CAN network interface index.
107 + * @can_addr: transport protocol specific address, mostly CAN IDs.
109 +struct sockaddr_can {
110 + sa_family_t can_family;
113 + struct { canid_t rx_id, tx_id; } tp16;
114 + struct { canid_t rx_id, tx_id; } tp20;
115 + struct { canid_t rx_id, tx_id; } mcnet;
116 + struct { canid_t rx_id, tx_id; } isotp;
117 + struct { int lcu, type; } bap;
122 + * struct can_filter - CAN ID based filter in can_register().
123 + * @can_id: relevant bits of CAN ID which are not masked out.
124 + * @can_mask: CAN mask (see description)
127 + * A filter matches, when
129 + * <received_can_id> & mask == can_id & mask
131 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
132 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
139 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
142 Index: net-2.6/include/linux/can/core.h
143 ===================================================================
144 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
145 +++ net-2.6/include/linux/can/core.h 2007-08-03 11:21:46.000000000 +0200
150 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
152 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
153 + * Urs Thuermann <urs.thuermann@volkswagen.de>
154 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
155 + * All rights reserved.
157 + * Send feedback to <socketcan-users@lists.berlios.de>
164 +#include <linux/can.h>
165 +#include <linux/skbuff.h>
166 +#include <linux/netdevice.h>
168 +#define CAN_VERSION "20070802"
170 +/* increment this number each time you change some user-space interface */
171 +#define CAN_ABI_VERSION "8"
173 +#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
175 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
177 +#define CAN_PROC_DIR "net/can" /* /proc/... */
180 + * struct can_proto - CAN protocol structure
181 + * @type: type argument in socket() syscall, e.g. SOCK_DGRAM.
182 + * @protocol: protocol number in socket() syscall.
183 + * @capability: capability needed to open the socket, or -1 for no restriction.
184 + * @ops: pointer to struct proto_ops for sock->ops.
185 + * @prot: pointer to struct proto structure.
191 + struct proto_ops *ops;
192 + struct proto *prot;
195 +/* function prototypes for the CAN networklayer core (af_can.c) */
197 +extern int can_proto_register(struct can_proto *cp);
198 +extern int can_proto_unregister(struct can_proto *cp);
200 +extern int can_rx_register(struct net_device *dev, canid_t can_id,
202 + void (*func)(struct sk_buff *, void *),
203 + void *data, char *ident);
205 +extern int can_rx_unregister(struct net_device *dev, canid_t can_id,
207 + void (*func)(struct sk_buff *, void *),
210 +extern int can_send(struct sk_buff *skb, int loop);
212 +#ifdef CONFIG_CAN_DEBUG_CORE
213 +extern void can_debug_skb(struct sk_buff *skb);
214 +extern void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
215 +#define DBG(args...) (debug & 1 ? \
216 + (printk(KERN_DEBUG "can-%s %s: ", \
217 + IDENT, __func__), printk(args)) : 0)
218 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
219 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
221 +#define DBG(args...)
222 +#define DBG_FRAME(args...)
223 +#define DBG_SKB(skb)
226 +#endif /* CAN_CORE_H */
227 Index: net-2.6/net/Kconfig
228 ===================================================================
229 --- net-2.6.orig/net/Kconfig 2007-08-03 11:21:32.000000000 +0200
230 +++ net-2.6/net/Kconfig 2007-08-03 11:21:46.000000000 +0200
234 source "net/ax25/Kconfig"
235 +source "net/can/Kconfig"
236 source "net/irda/Kconfig"
237 source "net/bluetooth/Kconfig"
238 source "net/rxrpc/Kconfig"
239 Index: net-2.6/net/Makefile
240 ===================================================================
241 --- net-2.6.orig/net/Makefile 2007-08-03 11:21:32.000000000 +0200
242 +++ net-2.6/net/Makefile 2007-08-03 11:21:46.000000000 +0200
244 obj-$(CONFIG_NETROM) += netrom/
245 obj-$(CONFIG_ROSE) += rose/
246 obj-$(CONFIG_AX25) += ax25/
247 +obj-$(CONFIG_CAN) += can/
248 obj-$(CONFIG_IRDA) += irda/
249 obj-$(CONFIG_BT) += bluetooth/
250 obj-$(CONFIG_SUNRPC) += sunrpc/
251 Index: net-2.6/net/can/Kconfig
252 ===================================================================
253 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
254 +++ net-2.6/net/can/Kconfig 2007-08-03 11:21:46.000000000 +0200
257 +# Controller Area Network (CAN) network layer core configuration
262 + tristate "CAN bus subsystem support"
264 + Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
265 + communications protocol, which was developed by Bosch at
266 + 1991 mainly for automotive, but now widely used in marine
267 + (NMEA2000), industrial and medical applications.
268 + More information on the CAN network protocol family PF_CAN
269 + is contained in <Documentation/networking/can.txt>.
271 + If you want CAN support, you should say Y here and also to the
272 + specific driver for your controller(s) below.
274 +config CAN_DEBUG_CORE
275 + bool "CAN Core debugging messages"
278 + Say Y here if you want the CAN core to produce a bunch of debug
279 + messages to the system log. Select this if you are having a
280 + problem with CAN support and want to see more of what is going on.
281 Index: net-2.6/net/can/Makefile
282 ===================================================================
283 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
284 +++ net-2.6/net/can/Makefile 2007-08-03 11:21:46.000000000 +0200
287 +# Makefile for the Linux Controller Area Network core.
290 +obj-$(CONFIG_CAN) += can.o
291 +can-objs := af_can.o proc.o
292 Index: net-2.6/net/can/af_can.c
293 ===================================================================
294 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
295 +++ net-2.6/net/can/af_can.c 2007-08-03 11:21:46.000000000 +0200
298 + * af_can.c - Protocol family CAN core module
299 + * (used by different CAN protocol modules)
301 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
302 + * All rights reserved.
304 + * Redistribution and use in source and binary forms, with or without
305 + * modification, are permitted provided that the following conditions
307 + * 1. Redistributions of source code must retain the above copyright
308 + * notice, this list of conditions, the following disclaimer and
309 + * the referenced file 'COPYING'.
310 + * 2. Redistributions in binary form must reproduce the above copyright
311 + * notice, this list of conditions and the following disclaimer in the
312 + * documentation and/or other materials provided with the distribution.
313 + * 3. Neither the name of Volkswagen nor the names of its contributors
314 + * may be used to endorse or promote products derived from this software
315 + * without specific prior written permission.
317 + * Alternatively, provided that this notice is retained in full, this
318 + * software may be distributed under the terms of the GNU General
319 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
320 + * file from the main directory of the linux kernel source.
322 + * The provided data structures and external interfaces from this code
323 + * are not restricted to be used by modules with a GPL compatible license.
325 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
326 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
327 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
328 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
329 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
330 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
331 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
332 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
334 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
335 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
338 + * Send feedback to <socketcan-users@lists.berlios.de>
342 +#include <linux/module.h>
343 +#include <linux/init.h>
344 +#include <linux/kmod.h>
345 +#include <linux/slab.h>
346 +#include <linux/list.h>
347 +#include <linux/spinlock.h>
348 +#include <linux/rcupdate.h>
349 +#include <linux/uaccess.h>
350 +#include <linux/net.h>
351 +#include <linux/netdevice.h>
352 +#include <linux/socket.h>
353 +#include <linux/if_ether.h>
354 +#include <linux/if_arp.h>
355 +#include <linux/skbuff.h>
356 +#include <linux/can.h>
357 +#include <linux/can/core.h>
358 +#include <net/sock.h>
362 +#define IDENT "core"
363 +static __initdata const char banner[] = KERN_INFO
364 + "can: controller area network core (" CAN_VERSION_STRING ")\n";
366 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
367 +MODULE_LICENSE("Dual BSD/GPL");
368 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
369 + "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
371 +MODULE_ALIAS_NETPROTO(PF_CAN);
373 +int stats_timer = 1; /* default: on */
374 +module_param(stats_timer, int, S_IRUGO);
375 +MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
377 +#ifdef CONFIG_CAN_DEBUG_CORE
379 +module_param(debug, int, S_IRUGO);
380 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
383 +HLIST_HEAD(rx_dev_list);
384 +static struct dev_rcv_lists rx_alldev_list;
385 +static DEFINE_SPINLOCK(rcv_lists_lock);
387 +static struct kmem_cache *rcv_cache __read_mostly;
389 +/* table of registered CAN protocols */
390 +static struct can_proto *proto_tab[CAN_NPROTO];
392 +struct timer_list stattimer; /* timer for statistics update */
393 +struct s_stats stats; /* packet statistics */
394 +struct s_pstats pstats; /* receive list statistics */
397 + * af_can socket functions
400 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
402 + struct sock *sk = sock->sk;
407 + return sock_get_timestamp(sk, (struct timeval __user *)arg);
410 + return -ENOIOCTLCMD;
414 +static void can_sock_destruct(struct sock *sk)
416 + DBG("called for sock %p\n", sk);
418 + skb_queue_purge(&sk->sk_receive_queue);
419 + if (sk->sk_protinfo)
420 + kfree(sk->sk_protinfo);
423 +static int can_create(struct socket *sock, int protocol)
426 + struct can_proto *cp;
427 + char module_name[sizeof("can-proto-000")];
430 + DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
432 + sock->state = SS_UNCONNECTED;
434 + if (protocol < 0 || protocol >= CAN_NPROTO)
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 + printk(KERN_INFO "can: request_module(%s) not"
451 + " implemented.\n", module_name);
453 + printk(KERN_ERR "can: request_module(%s) failed\n",
457 + /* check for success and correct type */
458 + cp = proto_tab[protocol];
459 + if (!cp || cp->type != sock->type)
460 + return -EPROTONOSUPPORT;
462 + if (cp->capability >= 0 && !capable(cp->capability))
465 + sock->ops = cp->ops;
467 + sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
471 + sock_init_data(sock, sk);
472 + sk->sk_destruct = can_sock_destruct;
474 + DBG("created sock: %p\n", sk);
476 + if (sk->sk_prot->init)
477 + ret = sk->sk_prot->init(sk);
480 + /* release sk on errors */
493 + * can_send - transmit a CAN frame (optional with local loopback)
494 + * @skb: pointer to socket buffer with CAN frame in data section
495 + * @loop: loopback for listeners on local CAN sockets (recommended default!)
499 + * -ENETDOWN when the selected interface is down
500 + * -ENOBUFS on full driver queue (see net_xmit_errno())
501 + * -ENOMEM when local loopback failed at calling skb_clone()
503 +int can_send(struct sk_buff *skb, int loop)
507 + if (skb->dev->type != ARPHRD_CAN) {
512 + if (!(skb->dev->flags & IFF_UP)) {
517 + skb->protocol = htons(ETH_P_CAN);
520 + /* local loopback of sent CAN frames */
522 + /* indication for the CAN driver: do loopback */
523 + skb->pkt_type = PACKET_LOOPBACK;
526 + * The reference to the originating sock may be required
527 + * by the receiving socket to check whether the frame is
528 + * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
529 + * Therefore we have to ensure that skb->sk remains the
530 + * reference to the originating sock by restoring skb->sk
531 + * after each skb_clone() or skb_orphan() usage.
534 + if (!(skb->dev->flags & IFF_LOOPBACK)) {
536 + * If the interface is not capable to do loopback
537 + * itself, we do it here.
539 + struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
546 + newskb->sk = skb->sk;
547 + newskb->ip_summed = CHECKSUM_UNNECESSARY;
548 + newskb->pkt_type = PACKET_BROADCAST;
552 + /* indication for the CAN driver: no loopback required */
553 + skb->pkt_type = PACKET_HOST;
556 + /* send to netdevice */
557 + err = dev_queue_xmit(skb);
559 + err = net_xmit_errno(err);
561 + /* update statistics */
563 + stats.tx_frames_delta++;
567 +EXPORT_SYMBOL(can_send);
573 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
575 + struct dev_rcv_lists *d;
576 + struct hlist_node *n;
579 + * find receive list for this device
581 + * The hlist_for_each_entry*() macros curse through the list
582 + * using the pointer variable n and set d to the containing
583 + * struct in each list iteration. Therefore, after list
584 + * iteration, d is unmodified when the list is empty, and it
585 + * points to last list element, when the list is non-empty
586 + * but no match in the loop body is found. I.e. d is *not*
587 + * NULL when no match is found. We can, however, use the
588 + * cursor variable n to decide if a match was found.
591 + hlist_for_each_entry(d, n, &rx_dev_list, list) {
596 + return n ? d : NULL;
599 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
600 + struct dev_rcv_lists *d)
602 + canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
604 + /* filter error frames */
605 + if (*mask & CAN_ERR_FLAG) {
606 + /* clear CAN_ERR_FLAG in list entry */
607 + *mask &= CAN_ERR_MASK;
608 + return &d->rx[RX_ERR];
611 + /* ensure valid values in can_mask */
612 + if (*mask & CAN_EFF_FLAG)
613 + *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
615 + *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
617 + /* reduce condition testing at receive time */
620 + /* inverse can_id/can_mask filter */
622 + return &d->rx[RX_INV];
624 + /* mask == 0 => no condition testing at receive time */
626 + return &d->rx[RX_ALL];
628 + /* use extra filterset for the subscription of exactly *ONE* can_id */
629 + if (*can_id & CAN_EFF_FLAG) {
630 + if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
631 + /* RFC: a use-case for hash-tables in the future? */
632 + return &d->rx[RX_EFF];
635 + if (*mask == CAN_SFF_MASK)
636 + return &d->rx_sff[*can_id];
639 + /* default: filter via can_id/can_mask */
640 + return &d->rx[RX_FIL];
644 + * can_rx_register - subscribe CAN frames from a specific interface
645 + * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
646 + * @can_id: CAN identifier (see description)
647 + * @mask: CAN mask (see description)
648 + * @func: callback function on filter match
649 + * @data: returned parameter for callback function
650 + * @ident: string for calling module indentification
653 + * Invokes the callback function with the received sk_buff and the given
654 + * parameter 'data' on a matching receive filter. A filter matches, when
656 + * <received_can_id> & mask == can_id & mask
658 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
659 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
663 + * -ENOMEM on missing cache mem to create subscription entry
664 + * -ENODEV unknown device
666 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
667 + void (*func)(struct sk_buff *, void *), void *data,
670 + struct receiver *r;
671 + struct hlist_head *rl;
672 + struct dev_rcv_lists *d;
675 + /* insert new receiver (dev,canid,mask) -> (func,data) */
677 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
678 + "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
680 + r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
684 + spin_lock_bh(&rcv_lists_lock);
686 + d = find_dev_rcv_lists(dev);
688 + rl = find_rcv_list(&can_id, &mask, d);
690 + r->can_id = can_id;
697 + hlist_add_head_rcu(&r->list, rl);
700 + pstats.rcv_entries++;
701 + if (pstats.rcv_entries_max < pstats.rcv_entries)
702 + pstats.rcv_entries_max = pstats.rcv_entries;
704 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
705 + DNAME(dev), can_id, mask);
706 + kmem_cache_free(rcv_cache, r);
710 + spin_unlock_bh(&rcv_lists_lock);
714 +EXPORT_SYMBOL(can_rx_register);
717 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
719 +static void can_rx_delete_device(struct rcu_head *rp)
721 + struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
723 + DBG("removing dev_rcv_list at %p\n", d);
728 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
730 +static void can_rx_delete_receiver(struct rcu_head *rp)
732 + struct receiver *r = container_of(rp, struct receiver, rcu);
734 + DBG("removing receiver at %p\n", r);
735 + kmem_cache_free(rcv_cache, r);
739 + * can_rx_unregister - unsubscribe CAN frames from a specific interface
740 + * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
741 + * @can_id: CAN identifier
743 + * @func: callback function on filter match
744 + * @data: returned parameter for callback function
747 + * Removes subscription entry depending on given (subscription) values.
751 + * -EINVAL on missing subscription entry
752 + * -ENODEV unknown device
754 +int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
755 + void (*func)(struct sk_buff *, void *), void *data)
757 + struct receiver *r = NULL;
758 + struct hlist_head *rl;
759 + struct hlist_node *next;
760 + struct dev_rcv_lists *d;
763 + DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
764 + dev, DNAME(dev), can_id, mask, func, data);
766 + spin_lock_bh(&rcv_lists_lock);
768 + d = find_dev_rcv_lists(dev);
770 + DBG("receive list not found for dev %s, id %03X, mask %03X\n",
771 + DNAME(dev), can_id, mask);
776 + rl = find_rcv_list(&can_id, &mask, d);
779 + * Search the receiver list for the item to delete. This should
780 + * exist, since no receiver may be unregistered that hasn't
781 + * been registered before.
784 + hlist_for_each_entry(r, next, rl, list) {
785 + if (r->can_id == can_id && r->mask == mask
786 + && r->func == func && r->data == data)
791 + * Check for bug in CAN protocol implementations:
792 + * If no matching list item was found, the list cursor variable next
793 + * will be NULL, while r will point to the last item of the list.
797 + DBG("receive list entry not found for "
798 + "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
805 + hlist_del_rcu(&r->list);
808 + if (pstats.rcv_entries > 0)
809 + pstats.rcv_entries--;
811 + /* remove device structure requested by NETDEV_UNREGISTER */
812 + if (d->remove_on_zero_entries && !d->entries) {
813 + DBG("removing dev_rcv_list for %s on zero entries\n",
815 + hlist_del_rcu(&d->list);
820 + spin_unlock_bh(&rcv_lists_lock);
822 + /* schedule the receiver item for deletion */
824 + call_rcu(&r->rcu, can_rx_delete_receiver);
826 + /* schedule the device structure for deletion */
828 + call_rcu(&d->rcu, can_rx_delete_device);
832 +EXPORT_SYMBOL(can_rx_unregister);
834 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
836 + struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
838 + DBG("skbuff %p cloned to %p\n", skb, clone);
840 + clone->sk = skb->sk;
841 + r->func(clone, r->data);
846 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
848 + struct receiver *r;
849 + struct hlist_node *n;
851 + struct can_frame *cf = (struct can_frame *)skb->data;
852 + canid_t can_id = cf->can_id;
854 + if (d->entries == 0)
857 + if (can_id & CAN_ERR_FLAG) {
858 + /* check for error frame entries only */
859 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
860 + if (can_id & r->mask) {
861 + DBG("match on rx_err skbuff %p\n", skb);
869 + /* check for unfiltered entries */
870 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
871 + DBG("match on rx_all skbuff %p\n", skb);
876 + /* check for can_id/mask entries */
877 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
878 + if ((can_id & r->mask) == r->can_id) {
879 + DBG("match on rx_fil skbuff %p\n", skb);
885 + /* check for inverted can_id/mask entries */
886 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
887 + if ((can_id & r->mask) != r->can_id) {
888 + DBG("match on rx_inv skbuff %p\n", skb);
894 + /* check CAN_ID specific entries */
895 + if (can_id & CAN_EFF_FLAG) {
896 + hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
897 + if (r->can_id == can_id) {
898 + DBG("match on rx_eff skbuff %p\n", skb);
904 + can_id &= CAN_SFF_MASK;
905 + hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
906 + DBG("match on rx_sff skbuff %p\n", skb);
915 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
916 + struct packet_type *pt, struct net_device *orig_dev)
918 + struct dev_rcv_lists *d;
921 + DBG("received skbuff on device %s, ptype %04x\n",
922 + dev->name, ntohs(pt->type));
924 + DBG_FRAME("af_can: can_rcv: received CAN frame",
925 + (struct can_frame *)skb->data);
927 + if (dev->type != ARPHRD_CAN) {
932 + /* update statistics */
934 + stats.rx_frames_delta++;
938 + /* deliver the packet to sockets listening on all devices */
939 + matches = can_rcv_filter(&rx_alldev_list, skb);
941 + /* find receive list for this device */
942 + d = find_dev_rcv_lists(dev);
944 + matches += can_rcv_filter(d, skb);
948 + /* free the skbuff allocated by the netdevice driver */
949 + DBG("freeing skbuff %p\n", skb);
954 + stats.matches_delta++;
961 + * af_can protocol functions
965 + * can_proto_register - register CAN transport protocol
966 + * @cp: pointer to CAN protocol structure
970 + * -EINVAL invalid (out of range) protocol number
971 + * -EBUSY protocol already in use
972 + * -ENOBUF if proto_register() fails
974 +int can_proto_register(struct can_proto *cp)
976 + int proto = cp->protocol;
979 + if (proto < 0 || proto >= CAN_NPROTO) {
980 + printk(KERN_ERR "can: protocol number %d out of range\n",
984 + if (proto_tab[proto]) {
985 + printk(KERN_ERR "can: protocol %d already registered\n",
990 + err = proto_register(cp->prot, 0);
994 + proto_tab[proto] = cp;
996 + /* use generic ioctl function if the module doesn't bring its own */
997 + if (!cp->ops->ioctl)
998 + cp->ops->ioctl = can_ioctl;
1002 +EXPORT_SYMBOL(can_proto_register);
1005 + * can_proto_unregister - unregister CAN transport protocol
1006 + * @cp: pointer to CAN protocol structure
1010 + * -ESRCH protocol number was not registered
1012 +int can_proto_unregister(struct can_proto *cp)
1014 + int proto = cp->protocol;
1016 + if (!proto_tab[proto]) {
1017 + printk(KERN_ERR "can: protocol %d is not registered\n", proto);
1020 + proto_unregister(cp->prot);
1021 + proto_tab[proto] = NULL;
1025 +EXPORT_SYMBOL(can_proto_unregister);
1028 + * af_can notifier to create/remove CAN netdevice specific structs
1030 +static int can_notifier(struct notifier_block *nb, unsigned long msg,
1033 + struct net_device *dev = (struct net_device *)data;
1034 + struct dev_rcv_lists *d;
1036 + DBG("msg %ld for dev %p (%s idx %d)\n",
1037 + msg, dev, dev->name, dev->ifindex);
1039 + if (dev->type != ARPHRD_CAN)
1040 + return NOTIFY_DONE;
1044 + case NETDEV_REGISTER:
1047 + * create new dev_rcv_lists for this device
1049 + * N.B. zeroing the struct is the correct initialization
1050 + * for the embedded hlist_head structs.
1051 + * Another list type, e.g. list_head, would require
1052 + * explicit initialization.
1055 + DBG("creating new dev_rcv_lists for %s\n", dev->name);
1057 + d = kzalloc(sizeof(*d),
1058 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1061 + "can: allocation of receive list failed\n");
1062 + return NOTIFY_DONE;
1066 + spin_lock_bh(&rcv_lists_lock);
1067 + hlist_add_head_rcu(&d->list, &rx_dev_list);
1068 + spin_unlock_bh(&rcv_lists_lock);
1072 + case NETDEV_UNREGISTER:
1073 + spin_lock_bh(&rcv_lists_lock);
1075 + d = find_dev_rcv_lists(dev);
1077 + DBG("remove dev_rcv_list for %s (%d entries)\n",
1078 + dev->name, d->entries);
1081 + d->remove_on_zero_entries = 1;
1084 + hlist_del_rcu(&d->list);
1086 + printk(KERN_ERR "can: notifier: receive list not "
1087 + "found for dev %s\n", dev->name);
1089 + spin_unlock_bh(&rcv_lists_lock);
1092 + call_rcu(&d->rcu, can_rx_delete_device);
1097 + return NOTIFY_DONE;
1101 + * af_can debugging stuff
1104 +#ifdef CONFIG_CAN_DEBUG_CORE
1106 +#define DBG_BSIZE 1024
1109 + * can_debug_cframe - print CAN frame
1110 + * @msg: pointer to message printed before the given CAN frame
1111 + * @cf: pointer to CAN frame
1113 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
1120 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1124 + len = sprintf(buf, KERN_DEBUG);
1126 + len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
1131 + dlc = cf->can_dlc;
1135 + if (cf->can_id & CAN_EFF_FLAG)
1136 + len += sprintf(buf + len, "<%08X> [%X] ",
1137 + cf->can_id & CAN_EFF_MASK, dlc);
1139 + len += sprintf(buf + len, "<%03X> [%X] ",
1140 + cf->can_id & CAN_SFF_MASK, dlc);
1142 + for (i = 0; i < dlc; i++)
1143 + len += sprintf(buf + len, "%02X ", cf->data[i]);
1145 + if (cf->can_id & CAN_RTR_FLAG)
1146 + len += sprintf(buf + len, "(RTR)");
1148 + buf[len++] = '\n';
1153 +EXPORT_SYMBOL(can_debug_cframe);
1156 + * can_debug_skb - print socket buffer content to kernel log
1157 + * @skb: pointer to socket buffer
1159 +void can_debug_skb(struct sk_buff *skb)
1161 + int len, nbytes, i;
1164 + buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1168 + len = sprintf(buf,
1169 + KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
1170 + KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
1171 + "h,d,t,e,l: %p %+d %+d %+d, %d",
1172 + skb, skb->dev ? skb->dev->ifindex : -1,
1173 + ntohs(skb->protocol),
1174 + atomic_read(&skb->users),
1175 + atomic_read(&(skb_shinfo(skb)->dataref)),
1176 + skb_shinfo(skb)->nr_frags,
1177 + skb->head, skb->data - skb->head,
1178 + skb->tail - skb->head, skb->end - skb->head, skb->len);
1179 + nbytes = skb->end - skb->head;
1180 + for (i = 0; i < nbytes; i++) {
1182 + len += sprintf(buf + len, "\n" KERN_DEBUG " ");
1183 + if (len < DBG_BSIZE - 16) {
1184 + len += sprintf(buf + len, " %02x", skb->head[i]);
1186 + len += sprintf(buf + len, "...");
1190 + buf[len++] = '\n';
1195 +EXPORT_SYMBOL(can_debug_skb);
1200 + * af_can module init/exit functions
1203 +static struct packet_type can_packet = {
1204 + .type = __constant_htons(ETH_P_CAN),
1209 +static struct net_proto_family can_family_ops = {
1211 + .create = can_create,
1212 + .owner = THIS_MODULE,
1215 +/* notifier block for netdevice event */
1216 +static struct notifier_block can_netdev_notifier = {
1217 + .notifier_call = can_notifier,
1220 +static __init int can_init(void)
1224 + rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1230 + * Insert rx_alldev_list for reception on all devices.
1231 + * This struct is zero initialized which is correct for the
1232 + * embedded hlist heads, the dev pointer, and the entries counter.
1235 + spin_lock_bh(&rcv_lists_lock);
1236 + hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1237 + spin_unlock_bh(&rcv_lists_lock);
1239 + if (stats_timer) {
1240 + /* the statistics are updated every second (timer triggered) */
1241 + init_timer(&stattimer);
1242 + stattimer.function = can_stat_update;
1243 + stattimer.data = 0;
1244 + /* update every second */
1245 + stattimer.expires = jiffies + HZ;
1246 + /* start statistics timer */
1247 + add_timer(&stattimer);
1249 + stattimer.function = NULL;
1254 + /* protocol register */
1255 + sock_register(&can_family_ops);
1256 + register_netdevice_notifier(&can_netdev_notifier);
1257 + dev_add_pack(&can_packet);
1262 +static __exit void can_exit(void)
1264 + struct dev_rcv_lists *d;
1265 + struct hlist_node *n, *next;
1268 + del_timer(&stattimer);
1270 + /* procfs remove */
1271 + can_remove_proc();
1273 + /* protocol unregister */
1274 + dev_remove_pack(&can_packet);
1275 + unregister_netdevice_notifier(&can_netdev_notifier);
1276 + sock_unregister(PF_CAN);
1278 + /* remove rx_dev_list */
1279 + spin_lock_bh(&rcv_lists_lock);
1280 + hlist_del(&rx_alldev_list.list);
1281 + hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1282 + hlist_del(&d->list);
1285 + spin_unlock_bh(&rcv_lists_lock);
1287 + kmem_cache_destroy(rcv_cache);
1290 +module_init(can_init);
1291 +module_exit(can_exit);
1292 Index: net-2.6/net/can/af_can.h
1293 ===================================================================
1294 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1295 +++ net-2.6/net/can/af_can.h 2007-08-03 11:21:46.000000000 +0200
1298 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1299 + * All rights reserved.
1301 + * Redistribution and use in source and binary forms, with or without
1302 + * modification, are permitted provided that the following conditions
1304 + * 1. Redistributions of source code must retain the above copyright
1305 + * notice, this list of conditions, the following disclaimer and
1306 + * the referenced file 'COPYING'.
1307 + * 2. Redistributions in binary form must reproduce the above copyright
1308 + * notice, this list of conditions and the following disclaimer in the
1309 + * documentation and/or other materials provided with the distribution.
1310 + * 3. Neither the name of Volkswagen nor the names of its contributors
1311 + * may be used to endorse or promote products derived from this software
1312 + * without specific prior written permission.
1314 + * Alternatively, provided that this notice is retained in full, this
1315 + * software may be distributed under the terms of the GNU General
1316 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1317 + * file from the main directory of the linux kernel source.
1319 + * The provided data structures and external interfaces from this code
1320 + * are not restricted to be used by modules with a GPL compatible license.
1322 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1323 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1324 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1325 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1326 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1327 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1328 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1329 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1330 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1331 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1332 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1335 + * Send feedback to <socketcan-users@lists.berlios.de>
1342 +#include <linux/skbuff.h>
1343 +#include <linux/netdevice.h>
1344 +#include <linux/list.h>
1345 +#include <linux/rcupdate.h>
1346 +#include <linux/can.h>
1348 +/* af_can rx dispatcher structures */
1351 + struct hlist_node list;
1352 + struct rcu_head rcu;
1355 + unsigned long matches;
1356 + void (*func)(struct sk_buff *, void *);
1361 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1363 +struct dev_rcv_lists {
1364 + struct hlist_node list;
1365 + struct rcu_head rcu;
1366 + struct net_device *dev;
1367 + struct hlist_head rx[RX_MAX];
1368 + struct hlist_head rx_sff[0x800];
1369 + int remove_on_zero_entries;
1373 +/* statistic structures */
1376 + unsigned long jiffies_init;
1378 + unsigned long rx_frames;
1379 + unsigned long tx_frames;
1380 + unsigned long matches;
1382 + unsigned long total_rx_rate;
1383 + unsigned long total_tx_rate;
1384 + unsigned long total_rx_match_ratio;
1386 + unsigned long current_rx_rate;
1387 + unsigned long current_tx_rate;
1388 + unsigned long current_rx_match_ratio;
1390 + unsigned long max_rx_rate;
1391 + unsigned long max_tx_rate;
1392 + unsigned long max_rx_match_ratio;
1394 + unsigned long rx_frames_delta;
1395 + unsigned long tx_frames_delta;
1396 + unsigned long matches_delta;
1397 +}; /* can be reset e.g. by can_init_stats() */
1400 + unsigned long stats_reset;
1401 + unsigned long user_reset;
1402 + unsigned long rcv_entries;
1403 + unsigned long rcv_entries_max;
1404 +}; /* persistent statistics */
1406 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1407 +extern void can_init_proc(void);
1408 +extern void can_remove_proc(void);
1409 +extern void can_stat_update(unsigned long data);
1411 +/* structures and variables from af_can.c needed in proc.c for reading */
1412 +extern struct timer_list stattimer; /* timer for statistics update */
1413 +extern struct s_stats stats; /* packet statistics */
1414 +extern struct s_pstats pstats; /* receive list statistics */
1415 +extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
1417 +#endif /* AF_CAN_H */
1418 Index: net-2.6/net/can/proc.c
1419 ===================================================================
1420 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1421 +++ net-2.6/net/can/proc.c 2007-08-03 11:21:46.000000000 +0200
1424 + * proc.c - procfs support for Protocol family CAN core module
1426 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1427 + * All rights reserved.
1429 + * Redistribution and use in source and binary forms, with or without
1430 + * modification, are permitted provided that the following conditions
1432 + * 1. Redistributions of source code must retain the above copyright
1433 + * notice, this list of conditions, the following disclaimer and
1434 + * the referenced file 'COPYING'.
1435 + * 2. Redistributions in binary form must reproduce the above copyright
1436 + * notice, this list of conditions and the following disclaimer in the
1437 + * documentation and/or other materials provided with the distribution.
1438 + * 3. Neither the name of Volkswagen nor the names of its contributors
1439 + * may be used to endorse or promote products derived from this software
1440 + * without specific prior written permission.
1442 + * Alternatively, provided that this notice is retained in full, this
1443 + * software may be distributed under the terms of the GNU General
1444 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1445 + * file from the main directory of the linux kernel source.
1447 + * The provided data structures and external interfaces from this code
1448 + * are not restricted to be used by modules with a GPL compatible license.
1450 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1451 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1452 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1453 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1454 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1455 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1456 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1457 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1458 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1459 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1460 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1463 + * Send feedback to <socketcan-users@lists.berlios.de>
1467 +#include <linux/module.h>
1468 +#include <linux/proc_fs.h>
1469 +#include <linux/list.h>
1470 +#include <linux/rcupdate.h>
1471 +#include <linux/can/core.h>
1473 +#include "af_can.h"
1476 + * proc filenames for the PF_CAN core
1479 +#define CAN_PROC_VERSION "version"
1480 +#define CAN_PROC_STATS "stats"
1481 +#define CAN_PROC_RESET_STATS "reset_stats"
1482 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
1483 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
1484 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
1485 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
1486 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
1487 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
1489 +static struct proc_dir_entry *can_dir;
1490 +static struct proc_dir_entry *pde_version;
1491 +static struct proc_dir_entry *pde_stats;
1492 +static struct proc_dir_entry *pde_reset_stats;
1493 +static struct proc_dir_entry *pde_rcvlist_all;
1494 +static struct proc_dir_entry *pde_rcvlist_fil;
1495 +static struct proc_dir_entry *pde_rcvlist_inv;
1496 +static struct proc_dir_entry *pde_rcvlist_sff;
1497 +static struct proc_dir_entry *pde_rcvlist_eff;
1498 +static struct proc_dir_entry *pde_rcvlist_err;
1500 +static int user_reset;
1502 +static const char *rx_list_name[] = {
1503 + [RX_ERR] = "rx_err",
1504 + [RX_ALL] = "rx_all",
1505 + [RX_FIL] = "rx_fil",
1506 + [RX_INV] = "rx_inv",
1507 + [RX_EFF] = "rx_eff",
1511 + * af_can statistics stuff
1514 +static void can_init_stats(void)
1517 + * This memset function is called from a timer context (when
1518 + * stattimer is active which is the default) OR in a process
1519 + * context (reading the proc_fs when stattimer is disabled).
1521 + memset(&stats, 0, sizeof(stats));
1522 + stats.jiffies_init = jiffies;
1524 + pstats.stats_reset++;
1528 + pstats.user_reset++;
1532 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1533 + unsigned long count)
1535 + unsigned long ret = 0;
1537 + if (oldjif == newjif)
1540 + /* see can_rcv() - this should NEVER happen! */
1541 + if (count > (ULONG_MAX / HZ)) {
1542 + printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
1547 + ret = (count * HZ) / (newjif - oldjif);
1552 +void can_stat_update(unsigned long data)
1554 + unsigned long j = jiffies; /* snapshot */
1556 + /* restart counting in timer context on user request */
1560 + /* restart counting on jiffies overflow */
1561 + if (j < stats.jiffies_init)
1564 + /* stats.rx_frames is the definitively max. statistic value */
1566 + /* prevent overflow in calc_rate() */
1567 + if (stats.rx_frames > (ULONG_MAX / HZ))
1570 + /* matches overflow - very improbable */
1571 + if (stats.matches > (ULONG_MAX / 100))
1574 + /* calc total values */
1575 + if (stats.rx_frames)
1576 + stats.total_rx_match_ratio = (stats.matches * 100) /
1579 + stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1581 + stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1584 + /* calc current values */
1585 + if (stats.rx_frames_delta)
1586 + stats.current_rx_match_ratio =
1587 + (stats.matches_delta * 100) / stats.rx_frames_delta;
1589 + stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
1590 + stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
1592 + /* check / update maximum values */
1593 + if (stats.max_tx_rate < stats.current_tx_rate)
1594 + stats.max_tx_rate = stats.current_tx_rate;
1596 + if (stats.max_rx_rate < stats.current_rx_rate)
1597 + stats.max_rx_rate = stats.current_rx_rate;
1599 + if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1600 + stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1602 + /* clear values for 'current rate' calculation */
1603 + stats.tx_frames_delta = 0;
1604 + stats.rx_frames_delta = 0;
1605 + stats.matches_delta = 0;
1607 + /* restart timer (one second) */
1608 + stattimer.expires = jiffies + HZ;
1609 + add_timer(&stattimer);
1613 + * proc read functions
1615 + * From known use-cases we expect about 10 entries in a receive list to be
1616 + * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
1620 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1621 + struct net_device *dev)
1623 + struct receiver *r;
1624 + struct hlist_node *n;
1627 + hlist_for_each_entry_rcu(r, n, rx_list, list) {
1628 + char *fmt = (r->can_id & CAN_EFF_FLAG)?
1629 + " %-5s %08X %08x %08x %08x %8ld %s\n" :
1630 + " %-5s %03X %08x %08lx %08lx %8ld %s\n";
1632 + len += snprintf(page + len, PAGE_SIZE - len, fmt,
1633 + DNAME(dev), r->can_id, r->mask,
1634 + (unsigned long)r->func, (unsigned long)r->data,
1635 + r->matches, r->ident);
1637 + /* does a typical line fit into the current buffer? */
1639 + /* 100 Bytes before end of buffer */
1640 + if (len > PAGE_SIZE - 100) {
1641 + /* mark output cut off */
1642 + len += snprintf(page + len, PAGE_SIZE - len,
1647 + rcu_read_unlock();
1652 +static int can_print_recv_banner(char *page, int len)
1655 + * can1. 00000000 00000000 00000000
1658 + len += snprintf(page + len, PAGE_SIZE - len,
1659 + " device can_id can_mask function"
1660 + " userdata matches ident\n");
1665 +static int can_proc_read_stats(char *page, char **start, off_t off,
1666 + int count, int *eof, void *data)
1670 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1671 + len += snprintf(page + len, PAGE_SIZE - len,
1672 + " %8ld transmitted frames (TXF)\n", stats.tx_frames);
1673 + len += snprintf(page + len, PAGE_SIZE - len,
1674 + " %8ld received frames (RXF)\n", stats.rx_frames);
1675 + len += snprintf(page + len, PAGE_SIZE - len,
1676 + " %8ld matched frames (RXMF)\n", stats.matches);
1678 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1680 + if (stattimer.function == can_stat_update) {
1681 + len += snprintf(page + len, PAGE_SIZE - len,
1682 + " %8ld %% total match ratio (RXMR)\n",
1683 + stats.total_rx_match_ratio);
1685 + len += snprintf(page + len, PAGE_SIZE - len,
1686 + " %8ld frames/s total tx rate (TXR)\n",
1687 + stats.total_tx_rate);
1688 + len += snprintf(page + len, PAGE_SIZE - len,
1689 + " %8ld frames/s total rx rate (RXR)\n",
1690 + stats.total_rx_rate);
1692 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1694 + len += snprintf(page + len, PAGE_SIZE - len,
1695 + " %8ld %% current match ratio (CRXMR)\n",
1696 + stats.current_rx_match_ratio);
1698 + len += snprintf(page + len, PAGE_SIZE - len,
1699 + " %8ld frames/s current tx rate (CTXR)\n",
1700 + stats.current_tx_rate);
1701 + len += snprintf(page + len, PAGE_SIZE - len,
1702 + " %8ld frames/s current rx rate (CRXR)\n",
1703 + stats.current_rx_rate);
1705 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1707 + len += snprintf(page + len, PAGE_SIZE - len,
1708 + " %8ld %% max match ratio (MRXMR)\n",
1709 + stats.max_rx_match_ratio);
1711 + len += snprintf(page + len, PAGE_SIZE - len,
1712 + " %8ld frames/s max tx rate (MTXR)\n",
1713 + stats.max_tx_rate);
1714 + len += snprintf(page + len, PAGE_SIZE - len,
1715 + " %8ld frames/s max rx rate (MRXR)\n",
1716 + stats.max_rx_rate);
1718 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1721 + len += snprintf(page + len, PAGE_SIZE - len,
1722 + " %8ld current receive list entries (CRCV)\n",
1723 + pstats.rcv_entries);
1724 + len += snprintf(page + len, PAGE_SIZE - len,
1725 + " %8ld maximum receive list entries (MRCV)\n",
1726 + pstats.rcv_entries_max);
1728 + if (pstats.stats_reset)
1729 + len += snprintf(page + len, PAGE_SIZE - len,
1730 + "\n %8ld statistic resets (STR)\n",
1731 + pstats.stats_reset);
1733 + if (pstats.user_reset)
1734 + len += snprintf(page + len, PAGE_SIZE - len,
1735 + " %8ld user statistic resets (USTR)\n",
1736 + pstats.user_reset);
1738 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1744 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1745 + int count, int *eof, void *data)
1751 + if (stattimer.function == can_stat_update) {
1752 + len += snprintf(page + len, PAGE_SIZE - len,
1753 + "Scheduled statistic reset #%ld.\n",
1754 + pstats.stats_reset + 1);
1757 + if (stats.jiffies_init != jiffies)
1760 + len += snprintf(page + len, PAGE_SIZE - len,
1761 + "Performed statistic reset #%ld.\n",
1762 + pstats.stats_reset);
1769 +static int can_proc_read_version(char *page, char **start, off_t off,
1770 + int count, int *eof, void *data)
1774 + len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1775 + CAN_VERSION_STRING);
1780 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1781 + int count, int *eof, void *data)
1783 + /* double cast to prevent GCC warning */
1784 + int idx = (int)(long)data;
1786 + struct dev_rcv_lists *d;
1787 + struct hlist_node *n;
1789 + len += snprintf(page + len, PAGE_SIZE - len,
1790 + "\nreceive list '%s':\n", rx_list_name[idx]);
1793 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1795 + if (!hlist_empty(&d->rx[idx])) {
1796 + len = can_print_recv_banner(page, len);
1797 + len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
1799 + len += snprintf(page + len, PAGE_SIZE - len,
1800 + " (%s: no entry)\n", DNAME(d->dev));
1802 + /* exit on end of buffer? */
1803 + if (len > PAGE_SIZE - 100)
1806 + rcu_read_unlock();
1808 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1814 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1815 + int count, int *eof, void *data)
1818 + struct dev_rcv_lists *d;
1819 + struct hlist_node *n;
1822 + len += snprintf(page + len, PAGE_SIZE - len,
1823 + "\nreceive list 'rx_sff':\n");
1826 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1827 + int i, all_empty = 1;
1828 + /* check wether at least one list is non-empty */
1829 + for (i = 0; i < 0x800; i++)
1830 + if (!hlist_empty(&d->rx_sff[i])) {
1836 + len = can_print_recv_banner(page, len);
1837 + for (i = 0; i < 0x800; i++) {
1838 + if (!hlist_empty(&d->rx_sff[i]) &&
1839 + len < PAGE_SIZE - 100)
1840 + len = can_print_rcvlist(page, len,
1845 + len += snprintf(page + len, PAGE_SIZE - len,
1846 + " (%s: no entry)\n", DNAME(d->dev));
1848 + /* exit on end of buffer? */
1849 + if (len > PAGE_SIZE - 100)
1852 + rcu_read_unlock();
1854 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
1861 + * proc utility functions
1864 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1866 + read_proc_t *read_proc,
1870 + return create_proc_read_entry(name, mode, can_dir, read_proc,
1876 +static void can_remove_proc_readentry(const char *name)
1879 + remove_proc_entry(name, can_dir);
1883 + * can_init_proc - create main CAN proc directory and procfs entries
1885 +void can_init_proc(void)
1887 + /* create /proc/net/can directory */
1888 + can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
1891 + printk(KERN_INFO "can: failed to create /proc/%s . "
1892 + "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
1896 + can_dir->owner = THIS_MODULE;
1898 + /* own procfs entries from the AF_CAN core */
1899 + pde_version = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
1900 + can_proc_read_version, NULL);
1901 + pde_stats = can_create_proc_readentry(CAN_PROC_STATS, 0644,
1902 + can_proc_read_stats, NULL);
1903 + pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
1904 + can_proc_read_reset_stats, NULL);
1905 + pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
1906 + can_proc_read_rcvlist, (void *)RX_ERR);
1907 + pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
1908 + can_proc_read_rcvlist, (void *)RX_ALL);
1909 + pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
1910 + can_proc_read_rcvlist, (void *)RX_FIL);
1911 + pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
1912 + can_proc_read_rcvlist, (void *)RX_INV);
1913 + pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
1914 + can_proc_read_rcvlist, (void *)RX_EFF);
1915 + pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
1916 + can_proc_read_rcvlist_sff, NULL);
1920 + * can_remove_proc - remove procfs entries and main CAN proc directory
1922 +void can_remove_proc(void)
1925 + can_remove_proc_readentry(CAN_PROC_VERSION);
1928 + can_remove_proc_readentry(CAN_PROC_STATS);
1930 + if (pde_reset_stats)
1931 + can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1933 + if (pde_rcvlist_err)
1934 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
1936 + if (pde_rcvlist_all)
1937 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
1939 + if (pde_rcvlist_fil)
1940 + can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
1942 + if (pde_rcvlist_inv)
1943 + can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
1945 + if (pde_rcvlist_eff)
1946 + can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
1948 + if (pde_rcvlist_sff)
1949 + can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
1952 + remove_proc_entry(CAN_PROC_DIR, NULL);
1954 Index: net-2.6/include/linux/can/error.h
1955 ===================================================================
1956 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1957 +++ net-2.6/include/linux/can/error.h 2007-08-03 11:21:46.000000000 +0200
1960 + * linux/can/error.h
1962 + * Definitions of the CAN error frame to be filtered and passed to the user.
1964 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
1965 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1966 + * All rights reserved.
1968 + * Send feedback to <socketcan-users@lists.berlios.de>
1972 +#ifndef CAN_ERROR_H
1973 +#define CAN_ERROR_H
1975 +#define CAN_ERR_DLC 8 /* dlc for error frames */
1977 +/* error class (mask) in can_id */
1978 +#define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */
1979 +#define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */
1980 +#define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */
1981 +#define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */
1982 +#define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */
1983 +#define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */
1984 +#define CAN_ERR_BUSOFF 0x00000040U /* bus off */
1985 +#define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */
1986 +#define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */
1988 +/* arbitration lost in bit ... / data[0] */
1989 +#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
1990 + /* else bit number in bitstream */
1992 +/* error status of CAN-controller / data[1] */
1993 +#define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */
1994 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
1995 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
1996 +#define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */
1997 +#define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */
1998 +#define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */
1999 +#define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */
2000 + /* (at least one error counter exceeds */
2001 + /* the protocol-defined level of 127) */
2003 +/* error in CAN protocol (type) / data[2] */
2004 +#define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */
2005 +#define CAN_ERR_PROT_BIT 0x01 /* single bit error */
2006 +#define CAN_ERR_PROT_FORM 0x02 /* frame format error */
2007 +#define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */
2008 +#define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */
2009 +#define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */
2010 +#define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */
2011 +#define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */
2012 +#define CAN_ERR_PROT_TX 0x80 /* error occured on transmission */
2014 +/* error in CAN protocol (location) / data[3] */
2015 +#define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */
2016 +#define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */
2017 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
2018 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
2019 +#define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */
2020 +#define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */
2021 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
2022 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
2023 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
2024 +#define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */
2025 +#define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */
2026 +#define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */
2027 +#define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */
2028 +#define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */
2029 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
2030 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
2031 +#define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */
2032 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
2033 +#define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */
2034 +#define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */
2036 +/* error status of CAN-transceiver / data[4] */
2038 +#define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */
2039 +#define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */
2040 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */
2041 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */
2042 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */
2043 +#define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */
2044 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */
2045 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */
2046 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */
2047 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
2049 +/* controller specific additional information / data[5..7] */
2051 +#endif /* CAN_ERROR_H */