]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - patch-series/2.6.22-rc3/02-can-core.diff
a1f946cd9d546c6f43b3cfbf9c5596f87716655f
[socketcan-devel.git] / patch-series / 2.6.22-rc3 / 02-can-core.diff
1 DESC
2 CAN: Add PF_CAN core module
3 EDESC
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.
7
8 Signed-Off-By: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
9 Signed-Off-By: Urs Thuermann <urs.thuermann@volkswagen.de>
10
11 ---
12  include/linux/can.h       |   98 ++++
13  include/linux/can/core.h  |   88 +++
14  include/linux/can/error.h |   95 ++++
15  net/Kconfig               |    1 
16  net/Makefile              |    1 
17  net/can/Kconfig           |   25 +
18  net/can/Makefile          |    6 
19  net/can/af_can.c          | 1070 ++++++++++++++++++++++++++++++++++++++++++++++
20  net/can/af_can.h          |  120 +++++
21  net/can/proc.c            |  530 ++++++++++++++++++++++
22  10 files changed, 2034 insertions(+)
23
24 Index: linux-2.6.22-rc3/include/linux/can.h
25 ===================================================================
26 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
27 +++ linux-2.6.22-rc3/include/linux/can.h        2007-05-29 10:14:54.%N +0200
28 @@ -0,0 +1,98 @@
29 +/*
30 + * linux/can.h
31 + *
32 + * Definitions for CAN networklayer (socket addr / CAN frame / CAN filter)
33 + *
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.
38 + *
39 + * Send feedback to <socketcan-users@lists.berlios.de>
40 + *
41 + */
42 +
43 +#ifndef CAN_H
44 +#define CAN_H
45 +
46 +#include <linux/types.h>
47 +#include <linux/socket.h>
48 +
49 +/* controller area network (CAN) kernel definitions */
50 +
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 */
55 +
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 */
60 +
61 +typedef __u32 canid_t;
62 +typedef __u32 can_err_mask_t;
63 +
64 +/**
65 + * struct can_frame - basic CAN frame structure
66 + * @can_id:  the CAN ID of the frame and CAN_*_FLAG flags, see above.
67 + * @can_dlc: the data length field of the CAN frame
68 + * @data:    the CAN frame payload.
69 + */
70 +struct can_frame {
71 +       canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
72 +       __u8    can_dlc; /* data length code: 0 .. 8 */
73 +       __u8    data[8] __attribute__((aligned(8)));
74 +};
75 +
76 +/* particular protocols of the protocol family PF_CAN */
77 +#define CAN_RAW                1 /* RAW sockets */
78 +#define CAN_BCM                2 /* Broadcast Manager */
79 +#define CAN_TP16       3 /* VAG Transport Protocol v1.6 */
80 +#define CAN_TP20       4 /* VAG Transport Protocol v2.0 */
81 +#define CAN_MCNET      5 /* Bosch MCNet */
82 +#define CAN_ISOTP      6 /* ISO 15765-2 Transport Protocol */
83 +#define CAN_BAP                7 /* VAG Bedien- und Anzeigeprotokoll */
84 +#define CAN_NPROTO     8
85 +
86 +#define SOL_CAN_BASE 100
87 +
88 +/**
89 + * struct sockaddr_can - the sockaddr structure for CAN sockets
90 + * @can_family:  address family number AF_CAN.
91 + * @can_ifindex: CAN network interface index.
92 + * @can_addr:    transport protocol specific address, mostly CAN IDs.
93 + */
94 +struct sockaddr_can {
95 +       sa_family_t can_family;
96 +       int         can_ifindex;
97 +       union {
98 +               struct { canid_t rx_id, tx_id; } tp16;
99 +               struct { canid_t rx_id, tx_id; } tp20;
100 +               struct { canid_t rx_id, tx_id; } mcnet;
101 +               struct { canid_t rx_id, tx_id; } isotp;
102 +               struct { int     lcu,   type;  } bap;
103 +       } can_addr;
104 +};
105 +
106 +/**
107 + * struct can_filter - CAN ID based filter in can_register().
108 + * @can_id:   relevant bits of CAN ID which are not masked out.
109 + * @can_mask: CAN mask (see description)
110 + *
111 + * Description:
112 + * A filter matches, when
113 + *
114 + *          <received_can_id> & mask == can_id & mask
115 + *
116 + * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
117 + * filter for error frames (CAN_ERR_FLAG bit set in mask).
118 + */
119 +struct can_filter {
120 +       canid_t can_id;
121 +       canid_t can_mask;
122 +};
123 +
124 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
125 +
126 +#endif /* CAN_H */
127 Index: linux-2.6.22-rc3/include/linux/can/core.h
128 ===================================================================
129 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
130 +++ linux-2.6.22-rc3/include/linux/can/core.h   2007-05-29 10:14:54.%N +0200
131 @@ -0,0 +1,88 @@
132 +/*
133 + * linux/can/core.h
134 + *
135 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
136 + *
137 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
138 + *          Urs Thuermann   <urs.thuermann@volkswagen.de>
139 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
140 + * All rights reserved.
141 + *
142 + * Send feedback to <socketcan-users@lists.berlios.de>
143 + *
144 + */
145 +
146 +#ifndef CAN_CORE_H
147 +#define CAN_CORE_H
148 +
149 +#include <linux/can.h>
150 +#include <linux/skbuff.h>
151 +#include <linux/netdevice.h>
152 +
153 +#define CAN_VERSION "20070523"
154 +
155 +/* increment this number each time you change some user-space interface */
156 +#define CAN_ABI_VERSION "8"
157 +
158 +#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
159 +
160 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
161 +
162 +#define CAN_PROC_DIR "net/can" /* /proc/... */
163 +
164 +/**
165 + * struct can_proto - CAN protocol structure
166 + * @type:       type argument in socket() syscall, e.g. SOCK_DGRAM.
167 + * @protocol:   protocol number in socket() syscall.
168 + * @capability: capability needed to open the socket, or -1 for no restriction.
169 + * @ops:        pointer to struct proto_ops for sock->ops.
170 + * @prot:       pointer to struct proto structure.
171 + */
172 +struct can_proto {
173 +       int              type;
174 +       int              protocol;
175 +       int              capability;
176 +       struct proto_ops *ops;
177 +       struct proto     *prot;
178 +};
179 +
180 +/* function prototypes for the CAN networklayer core (af_can.c) */
181 +
182 +extern int can_proto_register(struct can_proto *cp);
183 +extern int can_proto_unregister(struct can_proto *cp);
184 +
185 +extern int can_rx_register(struct net_device *dev, canid_t can_id,
186 +                          canid_t mask,
187 +                          void (*func)(struct sk_buff *, void *),
188 +                          void *data, char *ident);
189 +
190 +extern int can_rx_unregister(struct net_device *dev, canid_t can_id,
191 +                            canid_t mask,
192 +                            void (*func)(struct sk_buff *, void *),
193 +                            void *data);
194 +
195 +extern int can_dev_register(struct net_device *dev,
196 +                           void (*func)(unsigned long msg, void *),
197 +                           void *data);
198 +
199 +extern int can_dev_unregister(struct net_device *dev,
200 +                             void (*func)(unsigned long msg, void *),
201 +                             void *data);
202 +
203 +extern int can_send(struct sk_buff *skb, int loop);
204 +
205 +#ifdef CONFIG_CAN_DEBUG_CORE
206 +extern void can_debug_skb(struct sk_buff *skb);
207 +extern void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
208 +#define DBG(args...)       (debug & 1 ? \
209 +                              (printk(KERN_DEBUG "can-%s %s: ", \
210 +                               IDENT, __func__), printk(args)) : 0)
211 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
212 +#define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
213 +#else
214 +#define DBG(args...)
215 +#define DBG_FRAME(args...)
216 +#define DBG_SKB(skb)
217 +#endif
218 +
219 +#endif /* CAN_CORE_H */
220 Index: linux-2.6.22-rc3/net/Kconfig
221 ===================================================================
222 --- linux-2.6.22-rc3.orig/net/Kconfig   2007-05-29 10:03:02.%N +0200
223 +++ linux-2.6.22-rc3/net/Kconfig        2007-05-29 10:14:54.%N +0200
224 @@ -210,6 +210,7 @@
225  endmenu
226  
227  source "net/ax25/Kconfig"
228 +source "net/can/Kconfig"
229  source "net/irda/Kconfig"
230  source "net/bluetooth/Kconfig"
231  source "net/rxrpc/Kconfig"
232 Index: linux-2.6.22-rc3/net/Makefile
233 ===================================================================
234 --- linux-2.6.22-rc3.orig/net/Makefile  2007-05-29 10:03:02.%N +0200
235 +++ linux-2.6.22-rc3/net/Makefile       2007-05-29 10:14:54.%N +0200
236 @@ -34,6 +34,7 @@
237  obj-$(CONFIG_NETROM)           += netrom/
238  obj-$(CONFIG_ROSE)             += rose/
239  obj-$(CONFIG_AX25)             += ax25/
240 +obj-$(CONFIG_CAN)              += can/
241  obj-$(CONFIG_IRDA)             += irda/
242  obj-$(CONFIG_BT)               += bluetooth/
243  obj-$(CONFIG_SUNRPC)           += sunrpc/
244 Index: linux-2.6.22-rc3/net/can/Kconfig
245 ===================================================================
246 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
247 +++ linux-2.6.22-rc3/net/can/Kconfig    2007-05-30 12:38:26.%N +0200
248 @@ -0,0 +1,25 @@
249 +#
250 +# Controller Area Network (CAN) network layer core configuration
251 +#
252 +
253 +menuconfig CAN
254 +       depends on NET
255 +       tristate "CAN bus subsystem support"
256 +       ---help---
257 +         Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
258 +         communications protocol, which was developed by Bosch at
259 +         1991 mainly for automotive, but now widely used in marine
260 +         (NMEA2000), industrial and medical applications.
261 +         More information on the CAN network protocol family PF_CAN
262 +         is contained in <Documentation/networking/can.txt>.
263 +
264 +         If you want CAN support, you should say Y here and also to the
265 +         specific driver for your controller(s) below.
266 +
267 +config CAN_DEBUG_CORE
268 +       bool "CAN Core debugging messages"
269 +       depends on CAN
270 +       ---help---
271 +         Say Y here if you want the CAN core to produce a bunch of debug
272 +         messages to the system log.  Select this if you are having a
273 +         problem with CAN support and want to see more of what is going on.
274 Index: linux-2.6.22-rc3/net/can/Makefile
275 ===================================================================
276 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
277 +++ linux-2.6.22-rc3/net/can/Makefile   2007-05-30 12:38:26.%N +0200
278 @@ -0,0 +1,6 @@
279 +#
280 +#  Makefile for the Linux Controller Area Network core.
281 +#
282 +
283 +obj-$(CONFIG_CAN)      += can.o
284 +can-objs               := af_can.o proc.o
285 Index: linux-2.6.22-rc3/net/can/af_can.c
286 ===================================================================
287 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
288 +++ linux-2.6.22-rc3/net/can/af_can.c   2007-05-29 10:14:54.%N +0200
289 @@ -0,0 +1,1070 @@
290 +/*
291 + * af_can.c - Protocol family CAN core module
292 + *            (used by different CAN protocol modules)
293 + *
294 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
295 + * All rights reserved.
296 + *
297 + * Redistribution and use in source and binary forms, with or without
298 + * modification, are permitted provided that the following conditions
299 + * are met:
300 + * 1. Redistributions of source code must retain the above copyright
301 + *    notice, this list of conditions, the following disclaimer and
302 + *    the referenced file 'COPYING'.
303 + * 2. Redistributions in binary form must reproduce the above copyright
304 + *    notice, this list of conditions and the following disclaimer in the
305 + *    documentation and/or other materials provided with the distribution.
306 + * 3. Neither the name of Volkswagen nor the names of its contributors
307 + *    may be used to endorse or promote products derived from this software
308 + *    without specific prior written permission.
309 + *
310 + * Alternatively, provided that this notice is retained in full, this
311 + * software may be distributed under the terms of the GNU General
312 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
313 + * file from the main directory of the linux kernel source.
314 + *
315 + * The provided data structures and external interfaces from this code
316 + * are not restricted to be used by modules with a GPL compatible license.
317 + *
318 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
319 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
320 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
321 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
323 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
324 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
325 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
326 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
327 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
328 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
329 + * DAMAGE.
330 + *
331 + * Send feedback to <socketcan-users@lists.berlios.de>
332 + *
333 + */
334 +
335 +#include <linux/module.h>
336 +#include <linux/slab.h>
337 +#include <linux/kmod.h>
338 +#include <linux/init.h>
339 +#include <linux/list.h>
340 +#include <linux/spinlock.h>
341 +#include <linux/rcupdate.h>
342 +#include <linux/socket.h>
343 +#include <linux/if_ether.h>
344 +#include <linux/if_arp.h>
345 +#include <linux/skbuff.h>
346 +#include <linux/net.h>
347 +#include <linux/netdevice.h>
348 +#include <linux/can.h>
349 +#include <linux/can/core.h>
350 +#include <net/sock.h>
351 +#include <asm/uaccess.h>
352 +
353 +#include "af_can.h"
354 +
355 +#define IDENT "core"
356 +static __initdata const char banner[] =
357 +       KERN_INFO "can: controller area network core # "
358 +       CAN_VERSION_STRING "\n";
359 +
360 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
361 +MODULE_LICENSE("Dual BSD/GPL");
362 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
363 +             "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
364 +
365 +MODULE_ALIAS_NETPROTO(PF_CAN);
366 +
367 +int stats_timer = 1; /* default: on */
368 +module_param(stats_timer, int, S_IRUGO);
369 +MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
370 +
371 +#ifdef CONFIG_CAN_DEBUG_CORE
372 +static int debug = 0;
373 +module_param(debug, int, S_IRUGO);
374 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
375 +#endif
376 +
377 +struct notifier {
378 +       struct list_head list;
379 +       struct net_device *dev;
380 +       void (*func)(unsigned long msg, void *data);
381 +       void *data;
382 +};
383 +
384 +static LIST_HEAD(notifier_list);
385 +static DEFINE_RWLOCK(notifier_lock);
386 +
387 +HLIST_HEAD(rx_dev_list);
388 +static struct dev_rcv_lists rx_alldev_list;
389 +static DEFINE_SPINLOCK(rcv_lists_lock);
390 +
391 +static struct kmem_cache *rcv_cache __read_mostly;
392 +
393 +/* table of registered CAN protocols */
394 +static struct can_proto *proto_tab[CAN_NPROTO];
395 +
396 +struct timer_list stattimer; /* timer for statistics update */
397 +struct s_stats  stats;       /* packet statistics */
398 +struct s_pstats pstats;      /* receive list statistics */
399 +
400 +/*
401 + * af_can socket functions
402 + */
403 +
404 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
405 +{
406 +       struct sock *sk = sock->sk;
407 +
408 +       switch (cmd) {
409 +
410 +       case SIOCGSTAMP:
411 +               return sock_get_timestamp(sk, (struct timeval __user *)arg);
412 +
413 +       default:
414 +               return -ENOIOCTLCMD;
415 +       }
416 +}
417 +
418 +static void can_sock_destruct(struct sock *sk)
419 +{
420 +       DBG("called for sock %p\n", sk);
421 +
422 +       skb_queue_purge(&sk->sk_receive_queue);
423 +       if (sk->sk_protinfo)
424 +               kfree(sk->sk_protinfo);
425 +}
426 +
427 +static int can_create(struct socket *sock, int protocol)
428 +{
429 +       struct sock *sk;
430 +       struct can_proto *cp;
431 +       char module_name[sizeof("can-proto-000")];
432 +       int ret = 0;
433 +
434 +       DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
435 +
436 +       sock->state = SS_UNCONNECTED;
437 +
438 +       if (protocol < 0 || protocol >= CAN_NPROTO)
439 +               return -EINVAL;
440 +
441 +       DBG("looking up proto %d in proto_tab[]\n", protocol);
442 +
443 +       /* try to load protocol module, when CONFIG_KMOD is defined */
444 +       if (!proto_tab[protocol]) {
445 +               sprintf(module_name, "can-proto-%d", protocol);
446 +               ret = request_module(module_name);
447 +
448 +               /* In case of error we only print a message but don't
449 +                * return the error code immediately.  Below we will
450 +                * return -EPROTONOSUPPORT
451 +                */
452 +               if (ret == -ENOSYS)
453 +                       printk(KERN_INFO "can: request_module(%s) not"
454 +                              " implemented.\n", module_name);
455 +               else if (ret)
456 +                       printk(KERN_ERR "can: request_module(%s) failed\n",
457 +                              module_name);
458 +       }
459 +
460 +       /* check for success and correct type */
461 +       cp = proto_tab[protocol];
462 +       if (!cp || cp->type != sock->type)
463 +               return -EPROTONOSUPPORT;
464 +
465 +       if (cp->capability >= 0 && !capable(cp->capability))
466 +               return -EPERM;
467 +
468 +       sock->ops = cp->ops;
469 +
470 +       sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
471 +       if (!sk)
472 +               return -ENOMEM;
473 +
474 +       sock_init_data(sock, sk);
475 +       sk->sk_destruct = can_sock_destruct;
476 +
477 +       DBG("created sock: %p\n", sk);
478 +
479 +       if (sk->sk_prot->init)
480 +               ret = sk->sk_prot->init(sk);
481 +
482 +       if (ret) {
483 +               /* release sk on errors */
484 +               sock_orphan(sk);
485 +               sock_put(sk);
486 +       }
487 +
488 +       return ret;
489 +}
490 +
491 +/*
492 + * af_can tx path
493 + */
494 +
495 +/**
496 + * can_send - transmit a CAN frame (optional with local loopback)
497 + * @skb: pointer to socket buffer with CAN frame in data section
498 + * @loop: loopback for listeners on local CAN sockets (recommended default!)
499 + *
500 + * Return:
501 + *  0 on success
502 + *  -ENETDOWN when the selected interface is down
503 + *  -ENOBUFS on full driver queue (see net_xmit_errno())
504 + */
505 +int can_send(struct sk_buff *skb, int loop)
506 +{
507 +       struct sock **tx_sk = (struct sock **)skb->cb;
508 +       int err;
509 +
510 +       if (skb->dev->type != ARPHRD_CAN) {
511 +               kfree_skb(skb);
512 +               return -EPERM;
513 +       }
514 +
515 +       if (loop) {
516 +               /* local loopback of sent CAN frames (default) */
517 +
518 +               /* indication for the CAN driver: do loopback */
519 +               *tx_sk = skb->sk;
520 +
521 +               /*
522 +                * The reference to the originating sock may be also required
523 +                * by the receiving socket to indicate (and ignore) his own
524 +                * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
525 +                */
526 +
527 +               /* interface not capabable to do the loopback itself? */
528 +               if (!(skb->dev->flags & IFF_LOOPBACK)) {
529 +                       struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
530 +
531 +                       /* perform the local loopback here */
532 +                       newskb->protocol  = htons(ETH_P_CAN);
533 +                       newskb->ip_summed = CHECKSUM_UNNECESSARY;
534 +                       netif_rx(newskb);
535 +               }
536 +       } else {
537 +               /* indication for the CAN driver: no loopback required */
538 +               *tx_sk = NULL;
539 +       }
540 +
541 +       if (!(skb->dev->flags & IFF_UP))
542 +               return -ENETDOWN;
543 +
544 +       /* send to netdevice */
545 +       err = dev_queue_xmit(skb);
546 +       if (err > 0)
547 +               err = net_xmit_errno(err);
548 +
549 +       /* update statistics */
550 +       stats.tx_frames++;
551 +       stats.tx_frames_delta++;
552 +
553 +       return err;
554 +}
555 +EXPORT_SYMBOL(can_send);
556 +
557 +/*
558 + * af_can rx path
559 + */
560 +
561 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
562 +{
563 +       struct dev_rcv_lists *d;
564 +       struct hlist_node *n;
565 +
566 +       /*
567 +        * find receive list for this device
568 +        *
569 +        * The hlist_for_each_entry*() macros curse through the list
570 +        * using the pointer variable n and set d to the containing
571 +        * struct in each list iteration.  Therefore, after list
572 +        * iteration, d is unmodified when the list is empty, and it
573 +        * points to last list element, when the list is non-empty
574 +        * but no match in the loop body is found.  I.e. d is *not*
575 +        * NULL when no match is found.  We can, however, use the
576 +        * cursor variable n to decide if a match was found.
577 +        */
578 +
579 +       hlist_for_each_entry(d, n, &rx_dev_list, list) {
580 +               if (d->dev == dev)
581 +                       break;
582 +       }
583 +
584 +       return n ? d : NULL;
585 +}
586 +
587 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
588 +                                       struct dev_rcv_lists *d)
589 +{
590 +       canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
591 +
592 +       /* filter error frames */
593 +       if (*mask & CAN_ERR_FLAG) {
594 +               /* clear CAN_ERR_FLAG in list entry */
595 +               *mask &= CAN_ERR_MASK;
596 +               return &d->rx[RX_ERR];
597 +       }
598 +
599 +       /* ensure valid values in can_mask */
600 +       if (*mask & CAN_EFF_FLAG)
601 +               *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
602 +       else
603 +               *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
604 +
605 +       /* reduce condition testing at receive time */
606 +       *can_id &= *mask;
607 +
608 +       /* inverse can_id/can_mask filter */
609 +       if (inv)
610 +               return &d->rx[RX_INV];
611 +
612 +       /* mask == 0 => no condition testing at receive time */
613 +       if (!(*mask))
614 +               return &d->rx[RX_ALL];
615 +
616 +       /* use extra filterset for the subscription of exactly *ONE* can_id */
617 +       if (*can_id & CAN_EFF_FLAG) {
618 +               if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
619 +                       /* RFC: a use-case for hash-tables in the future? */
620 +                       return &d->rx[RX_EFF];
621 +               }
622 +       } else {
623 +               if (*mask == CAN_SFF_MASK)
624 +                       return &d->rx_sff[*can_id];
625 +       }
626 +
627 +       /* default: filter via can_id/can_mask */
628 +       return &d->rx[RX_FIL];
629 +}
630 +
631 +/**
632 + * can_rx_register - subscribe CAN frames from a specific interface
633 + * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
634 + * @can_id: CAN identifier (see description)
635 + * @mask: CAN mask (see description)
636 + * @func: callback function on filter match
637 + * @data: returned parameter for callback function
638 + * @ident: string for calling module indentification
639 + *
640 + * Description:
641 + *  Invokes the callback function with the received sk_buff and the given
642 + *  parameter 'data' on a matching receive filter. A filter matches, when
643 + *
644 + *          <received_can_id> & mask == can_id & mask
645 + *
646 + *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
647 + *  filter for error frames (CAN_ERR_FLAG bit set in mask).
648 + *
649 + * Return:
650 + *  0 on success
651 + *  -ENOMEM on missing cache mem to create subscription entry
652 + *  -ENODEV unknown device
653 + */
654 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
655 +                   void (*func)(struct sk_buff *, void *), void *data,
656 +                   char *ident)
657 +{
658 +       struct receiver *r;
659 +       struct hlist_head *rl;
660 +       struct dev_rcv_lists *d;
661 +       int ret = 0;
662 +
663 +       /* insert new receiver  (dev,canid,mask) -> (func,data) */
664 +
665 +       DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
666 +           dev, can_id, mask, func, data, ident);
667 +
668 +       r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
669 +       if (!r)
670 +               return -ENOMEM;
671 +
672 +       spin_lock_bh(&rcv_lists_lock);
673 +
674 +       d = find_dev_rcv_lists(dev);
675 +       if (d) {
676 +               rl = find_rcv_list(&can_id, &mask, d);
677 +
678 +               r->can_id  = can_id;
679 +               r->mask    = mask;
680 +               r->matches = 0;
681 +               r->func    = func;
682 +               r->data    = data;
683 +               r->ident   = ident;
684 +
685 +               hlist_add_head_rcu(&r->list, rl);
686 +               d->entries++;
687 +
688 +               pstats.rcv_entries++;
689 +               if (pstats.rcv_entries_max < pstats.rcv_entries)
690 +                       pstats.rcv_entries_max = pstats.rcv_entries;
691 +       } else {
692 +               DBG("receive list not found for dev %s, id %03X, mask %03X\n",
693 +                   DNAME(dev), can_id, mask);
694 +               kmem_cache_free(rcv_cache, r);
695 +               ret = -ENODEV;
696 +       }
697 +
698 +       spin_unlock_bh(&rcv_lists_lock);
699 +
700 +       return ret;
701 +}
702 +EXPORT_SYMBOL(can_rx_register);
703 +
704 +static void can_rx_delete_list(struct hlist_head *rl)
705 +{
706 +       struct receiver *r;
707 +       struct hlist_node *n;
708 +
709 +       hlist_for_each_entry_rcu(r, n, rl, list) {
710 +               hlist_del_rcu(&r->list);
711 +               kmem_cache_free(rcv_cache, r);
712 +       }
713 +}
714 +
715 +/*
716 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
717 + */
718 +static void can_rx_delete_device(struct rcu_head *rp)
719 +{
720 +       struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
721 +       int i;
722 +
723 +       /* remove all receivers hooked at this netdevice */
724 +       can_rx_delete_list(&d->rx[RX_ERR]);
725 +       can_rx_delete_list(&d->rx[RX_ALL]);
726 +       can_rx_delete_list(&d->rx[RX_FIL]);
727 +       can_rx_delete_list(&d->rx[RX_INV]);
728 +       can_rx_delete_list(&d->rx[RX_EFF]);
729 +
730 +       for (i = 0; i < 2048; i++)
731 +               can_rx_delete_list(&d->rx_sff[i]);
732 +
733 +       kfree(d);
734 +}
735 +
736 +/*
737 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
738 + */
739 +static void can_rx_delete_receiver(struct rcu_head *rp)
740 +{
741 +       struct receiver *r = container_of(rp, struct receiver, rcu);
742 +
743 +       kmem_cache_free(rcv_cache, r);
744 +}
745 +
746 +/**
747 + * can_rx_unregister - unsubscribe CAN frames from a specific interface
748 + * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
749 + * @can_id: CAN identifier
750 + * @mask: CAN mask
751 + * @func: callback function on filter match
752 + * @data: returned parameter for callback function
753 + *
754 + * Description:
755 + *  Removes subscription entry depending on given (subscription) values.
756 + *
757 + * Return:
758 + *  0 on success
759 + *  -EINVAL on missing subscription entry
760 + *  -ENODEV unknown device
761 + */
762 +int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
763 +                     void (*func)(struct sk_buff *, void *), void *data)
764 +{
765 +       struct receiver *r = NULL;
766 +       struct hlist_head *rl;
767 +       struct hlist_node *next;
768 +       struct dev_rcv_lists *d;
769 +       int ret = 0;
770 +
771 +       DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
772 +           dev, can_id, mask, func, data);
773 +
774 +       spin_lock_bh(&rcv_lists_lock);
775 +
776 +       d = find_dev_rcv_lists(dev);
777 +       if (!d) {
778 +               DBG("receive list not found for dev %s, id %03X, mask %03X\n",
779 +                   DNAME(dev), can_id, mask);
780 +               ret = -ENODEV;
781 +               goto out;
782 +       }
783 +
784 +       rl = find_rcv_list(&can_id, &mask, d);
785 +
786 +       /*
787 +        * Search the receiver list for the item to delete.  This should
788 +        * exist, since no receiver may be unregistered that hasn't
789 +        * been registered before.
790 +        */
791 +
792 +       hlist_for_each_entry(r, next, rl, list) {
793 +               if (r->can_id == can_id && r->mask == mask
794 +                   && r->func == func && r->data == data)
795 +                       break;
796 +       }
797 +
798 +       /*
799 +        * Check for bug in CAN protocol implementations:
800 +        * If no matching list item was found, the list cursor variable next
801 +        * will be NULL, while r will point to the last item of the list.
802 +        */
803 +
804 +       if (!next) {
805 +               DBG("receive list entry not found for "
806 +                   "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
807 +               ret = -EINVAL;
808 +               r = NULL;
809 +               goto out;
810 +       }
811 +
812 +       hlist_del_rcu(&r->list);
813 +       d->entries--;
814 +
815 +       if (pstats.rcv_entries > 0)
816 +               pstats.rcv_entries--;
817 +
818 + out:
819 +       spin_unlock_bh(&rcv_lists_lock);
820 +
821 +       /* schedule the receiver item for deletion */
822 +       if (r)
823 +               call_rcu(&r->rcu, can_rx_delete_receiver);
824 +
825 +       return ret;
826 +}
827 +EXPORT_SYMBOL(can_rx_unregister);
828 +
829 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
830 +{
831 +       struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
832 +
833 +       DBG("skbuff %p cloned to %p\n", skb, clone);
834 +       if (clone) {
835 +               r->func(clone, r->data);
836 +               r->matches++;
837 +       }
838 +}
839 +
840 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
841 +{
842 +       struct receiver *r;
843 +       struct hlist_node *n;
844 +       int matches = 0;
845 +       struct can_frame *cf = (struct can_frame*)skb->data;
846 +       canid_t can_id = cf->can_id;
847 +
848 +       if (d->entries == 0)
849 +               return 0;
850 +
851 +       if (can_id & CAN_ERR_FLAG) {
852 +               /* check for error frame entries only */
853 +               hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
854 +                       if (can_id & r->mask) {
855 +                               DBG("match on rx_err skbuff %p\n", skb);
856 +                               deliver(skb, r);
857 +                               matches++;
858 +                       }
859 +               }
860 +               return matches;
861 +       }
862 +
863 +       /* check for unfiltered entries */
864 +       hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
865 +               DBG("match on rx_all skbuff %p\n", skb);
866 +               deliver(skb, r);
867 +               matches++;
868 +       }
869 +
870 +       /* check for can_id/mask entries */
871 +       hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
872 +               if ((can_id & r->mask) == r->can_id) {
873 +                       DBG("match on rx_fil skbuff %p\n", skb);
874 +                       deliver(skb, r);
875 +                       matches++;
876 +               }
877 +       }
878 +
879 +       /* check for inverted can_id/mask entries */
880 +       hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
881 +               if ((can_id & r->mask) != r->can_id) {
882 +                       DBG("match on rx_inv skbuff %p\n", skb);
883 +                       deliver(skb, r);
884 +                       matches++;
885 +               }
886 +       }
887 +
888 +       /* check CAN_ID specific entries */
889 +       if (can_id & CAN_EFF_FLAG) {
890 +               hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
891 +                       if (r->can_id == can_id) {
892 +                               DBG("match on rx_eff skbuff %p\n", skb);
893 +                               deliver(skb, r);
894 +                               matches++;
895 +                       }
896 +               }
897 +       } else {
898 +               can_id &= CAN_SFF_MASK;
899 +               hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
900 +                       DBG("match on rx_sff skbuff %p\n", skb);
901 +                       deliver(skb, r);
902 +                       matches++;
903 +               }
904 +       }
905 +
906 +       return matches;
907 +}
908 +
909 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
910 +                  struct packet_type *pt, struct net_device *orig_dev)
911 +{
912 +       struct dev_rcv_lists *d;
913 +       int matches;
914 +
915 +       DBG("received skbuff on device %s, ptype %04x\n",
916 +           dev->name, ntohs(pt->type));
917 +       DBG_SKB(skb);
918 +       DBG_FRAME("af_can: can_rcv: received CAN frame",
919 +                 (struct can_frame *)skb->data);
920 +
921 +       if (dev->type != ARPHRD_CAN) {
922 +               kfree_skb(skb);
923 +               return 0;
924 +       }
925 +
926 +       /* update statistics */
927 +       stats.rx_frames++;
928 +       stats.rx_frames_delta++;
929 +
930 +       rcu_read_lock();
931 +
932 +       /* deliver the packet to sockets listening on all devices */
933 +       matches = can_rcv_filter(&rx_alldev_list, skb);
934 +
935 +       /* find receive list for this device */
936 +       d = find_dev_rcv_lists(dev);
937 +       if (d)
938 +               matches += can_rcv_filter(d, skb);
939 +
940 +       rcu_read_unlock();
941 +
942 +       /* free the skbuff allocated by the netdevice driver */
943 +       DBG("freeing skbuff %p\n", skb);
944 +       kfree_skb(skb);
945 +
946 +       if (matches > 0) {
947 +               stats.matches++;
948 +               stats.matches_delta++;
949 +       }
950 +
951 +       return 0;
952 +}
953 +
954 +/*
955 + * af_can debugging stuff
956 + */
957 +
958 +#ifdef CONFIG_CAN_DEBUG_CORE
959 +
960 +#define DBG_BSIZE 1024
961 +
962 +/**
963 + * can_debug_cframe - print CAN frame
964 + * @msg: pointer to message printed before the given CAN frame
965 + * @cf: pointer to CAN frame
966 + */
967 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
968 +{
969 +       va_list ap;
970 +       int len;
971 +       int dlc, i;
972 +       char *buf;
973 +
974 +       buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
975 +       if (!buf)
976 +               return;
977 +
978 +       len = sprintf(buf, KERN_DEBUG);
979 +       va_start(ap, cf);
980 +       len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
981 +       buf[len++] = ':';
982 +       buf[len++] = ' ';
983 +       va_end(ap);
984 +
985 +       dlc = cf->can_dlc;
986 +       if (dlc > 8)
987 +               dlc = 8;
988 +
989 +       if (cf->can_id & CAN_EFF_FLAG)
990 +               len += sprintf(buf + len, "<%08X> [%X] ",
991 +                              cf->can_id & CAN_EFF_MASK, dlc);
992 +       else
993 +               len += sprintf(buf + len, "<%03X> [%X] ",
994 +                              cf->can_id & CAN_SFF_MASK, dlc);
995 +
996 +       for (i = 0; i < dlc; i++)
997 +               len += sprintf(buf + len, "%02X ", cf->data[i]);
998 +
999 +       if (cf->can_id & CAN_RTR_FLAG)
1000 +               len += sprintf(buf + len, "(RTR)");
1001 +
1002 +       buf[len++] = '\n';
1003 +       buf[len]   = '\0';
1004 +       printk(buf);
1005 +       kfree(buf);
1006 +}
1007 +EXPORT_SYMBOL(can_debug_cframe);
1008 +
1009 +/**
1010 + * can_debug_skb - print socket buffer content to kernel log
1011 + * @skb: pointer to socket buffer
1012 + */
1013 +void can_debug_skb(struct sk_buff *skb)
1014 +{
1015 +       int len, nbytes, i;
1016 +       char *buf;
1017 +
1018 +       buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1019 +       if (!buf)
1020 +               return;
1021 +
1022 +       len = sprintf(buf,
1023 +                     KERN_DEBUG "  skbuff at %p, dev: %d, proto: %04x\n"
1024 +                     KERN_DEBUG "  users: %d, dataref: %d, nr_frags: %d, "
1025 +                     "h,d,t,e,l: %p %+d %+d %+d, %d",
1026 +                     skb, skb->dev ? skb->dev->ifindex : -1,
1027 +                     ntohs(skb->protocol),
1028 +                     atomic_read(&skb->users),
1029 +                     atomic_read(&(skb_shinfo(skb)->dataref)),
1030 +                     skb_shinfo(skb)->nr_frags,
1031 +                     skb->head, skb->data - skb->head,
1032 +                     skb->tail - skb->head, skb->end - skb->head, skb->len);
1033 +       nbytes = skb->end - skb->head;
1034 +       for (i = 0; i < nbytes; i++) {
1035 +               if (i % 16 == 0)
1036 +                       len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
1037 +               if (len < DBG_BSIZE - 16) {
1038 +                       len += sprintf(buf + len, " %02x", skb->head[i]);
1039 +               } else {
1040 +                       len += sprintf(buf + len, "...");
1041 +                       break;
1042 +               }
1043 +       }
1044 +       buf[len++] = '\n';
1045 +       buf[len]   = '\0';
1046 +       printk(buf);
1047 +       kfree(buf);
1048 +}
1049 +EXPORT_SYMBOL(can_debug_skb);
1050 +
1051 +#endif
1052 +
1053 +/*
1054 + * af_can protocol functions
1055 + */
1056 +
1057 +/**
1058 + * can_proto_register - register CAN transport protocol
1059 + * @cp: pointer to CAN protocol structure
1060 + *
1061 + * Return:
1062 + *  0 on success
1063 + *  -EINVAL invalid (out of range) protocol number
1064 + *  -EBUSY  protocol already in use
1065 + *  -ENOBUF if proto_register() fails
1066 + */
1067 +int can_proto_register(struct can_proto *cp)
1068 +{
1069 +       int proto = cp->protocol;
1070 +       int err = 0;
1071 +
1072 +       if (proto < 0 || proto >= CAN_NPROTO) {
1073 +               printk(KERN_ERR "can: protocol number %d out "
1074 +                      "of range\n", proto);
1075 +               return -EINVAL;
1076 +       }
1077 +       if (proto_tab[proto]) {
1078 +               printk(KERN_ERR "can: protocol %d already "
1079 +                      "registered\n", proto);
1080 +               return -EBUSY;
1081 +       }
1082 +
1083 +       err = proto_register(cp->prot, 0);
1084 +       if (err < 0)
1085 +               return err;
1086 +
1087 +       proto_tab[proto] = cp;
1088 +
1089 +       /* use generic ioctl function if the module doesn't bring its own */
1090 +       if (!cp->ops->ioctl)
1091 +               cp->ops->ioctl = can_ioctl;
1092 +
1093 +       return err;
1094 +}
1095 +EXPORT_SYMBOL(can_proto_register);
1096 +
1097 +/**
1098 + * can_proto_unregister - unregister CAN transport protocol
1099 + * @cp: pointer to CAN protocol structure
1100 + *
1101 + * Return:
1102 + *  0 on success
1103 + *  -ESRCH protocol number was not registered
1104 + */
1105 +int can_proto_unregister(struct can_proto *cp)
1106 +{
1107 +       int proto = cp->protocol;
1108 +
1109 +       if (!proto_tab[proto]) {
1110 +               printk(KERN_ERR "can: protocol %d is not registered\n", proto);
1111 +               return -ESRCH;
1112 +       }
1113 +       proto_unregister(cp->prot);
1114 +       proto_tab[proto] = NULL;
1115 +
1116 +       return 0;
1117 +}
1118 +EXPORT_SYMBOL(can_proto_unregister);
1119 +
1120 +/**
1121 + * can_dev_register - subscribe notifier for CAN device status changes
1122 + * @dev: pointer to netdevice
1123 + * @func: callback function on status change
1124 + * @data: returned parameter for callback function
1125 + *
1126 + * Description:
1127 + *  Invokes the callback function with the status 'msg' and the given
1128 + *  parameter 'data' on a status change of the given CAN network device.
1129 + *
1130 + * Return:
1131 + *  0 on success
1132 + *  -ENOMEM on missing mem to create subscription entry
1133 + *  -ENODEV unknown device
1134 + */
1135 +int can_dev_register(struct net_device *dev,
1136 +                    void (*func)(unsigned long msg, void *), void *data)
1137 +{
1138 +       struct notifier *n;
1139 +
1140 +       DBG("called for %s\n", dev->name);
1141 +
1142 +       if (!dev || dev->type != ARPHRD_CAN)
1143 +               return -ENODEV;
1144 +
1145 +       n = kmalloc(sizeof(*n), GFP_KERNEL);
1146 +       if (!n)
1147 +               return -ENOMEM;
1148 +
1149 +       n->dev  = dev;
1150 +       n->func = func;
1151 +       n->data = data;
1152 +
1153 +       write_lock(&notifier_lock);
1154 +       list_add(&n->list, &notifier_list);
1155 +       write_unlock(&notifier_lock);
1156 +
1157 +       return 0;
1158 +}
1159 +EXPORT_SYMBOL(can_dev_register);
1160 +
1161 +/**
1162 + * can_dev_unregister - unsubscribe notifier for CAN device status changes
1163 + * @dev: pointer to netdevice
1164 + * @func: callback function on filter match
1165 + * @data: returned parameter for callback function
1166 + *
1167 + * Description:
1168 + *  Removes subscription entry depending on given (subscription) values.
1169 + *
1170 + * Return:
1171 + *  0 on success
1172 + *  -EINVAL on missing subscription entry
1173 + */
1174 +int can_dev_unregister(struct net_device *dev,
1175 +                      void (*func)(unsigned long msg, void *), void *data)
1176 +{
1177 +       struct notifier *n, *next;
1178 +       int ret = -EINVAL;
1179 +
1180 +       DBG("called for %s\n", dev->name);
1181 +
1182 +       write_lock(&notifier_lock);
1183 +       list_for_each_entry_safe(n, next, &notifier_list, list) {
1184 +               if (n->dev == dev && n->func == func && n->data == data) {
1185 +                       list_del(&n->list);
1186 +                       kfree(n);
1187 +                       ret = 0;
1188 +                       break;
1189 +               }
1190 +       }
1191 +       write_unlock(&notifier_lock);
1192 +
1193 +       return ret;
1194 +}
1195 +EXPORT_SYMBOL(can_dev_unregister);
1196 +
1197 +static int can_notifier(struct notifier_block *nb,
1198 +                       unsigned long msg, void *data)
1199 +{
1200 +       struct net_device *dev = (struct net_device *)data;
1201 +       struct notifier *n;
1202 +       struct dev_rcv_lists *d;
1203 +
1204 +       DBG("called for %s, msg = %lu\n", dev->name, msg);
1205 +
1206 +       if (dev->type != ARPHRD_CAN)
1207 +               return NOTIFY_DONE;
1208 +
1209 +       switch (msg) {
1210 +
1211 +       case NETDEV_REGISTER:
1212 +
1213 +               /*
1214 +                * create new dev_rcv_lists for this device
1215 +                *
1216 +                * N.B. zeroing the struct is the correct initialization
1217 +                * for the embedded hlist_head structs.
1218 +                * Another list type, e.g. list_head, would require
1219 +                * explicit initialization.
1220 +                */
1221 +
1222 +               DBG("creating new dev_rcv_lists for %s\n", dev->name);
1223 +
1224 +               d = kzalloc(sizeof(*d),
1225 +                           in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1226 +               if (!d) {
1227 +                       printk(KERN_ERR "can: allocation of receive "
1228 +                              "list failed\n");
1229 +                       return NOTIFY_DONE;
1230 +               }
1231 +               d->dev = dev;
1232 +
1233 +               spin_lock_bh(&rcv_lists_lock);
1234 +               hlist_add_head_rcu(&d->list, &rx_dev_list);
1235 +               spin_unlock_bh(&rcv_lists_lock);
1236 +
1237 +               break;
1238 +
1239 +       case NETDEV_UNREGISTER:
1240 +               spin_lock_bh(&rcv_lists_lock);
1241 +
1242 +               d = find_dev_rcv_lists(dev);
1243 +               if (d)
1244 +                       hlist_del_rcu(&d->list);
1245 +               else
1246 +                       printk(KERN_ERR "can: notifier: receive list not "
1247 +                              "found for dev %s\n", dev->name);
1248 +
1249 +               spin_unlock_bh(&rcv_lists_lock);
1250 +
1251 +               if (d)
1252 +                       call_rcu(&d->rcu, can_rx_delete_device);
1253 +
1254 +               break;
1255 +       }
1256 +
1257 +       read_lock(&notifier_lock);
1258 +       list_for_each_entry(n, &notifier_list, list) {
1259 +               if (n->dev == dev)
1260 +                       n->func(msg, n->data);
1261 +       }
1262 +       read_unlock(&notifier_lock);
1263 +
1264 +       return NOTIFY_DONE;
1265 +}
1266 +
1267 +/*
1268 + * af_can module init/exit functions
1269 + */
1270 +
1271 +static struct packet_type can_packet = {
1272 +       .type = __constant_htons(ETH_P_CAN),
1273 +       .dev  = NULL,
1274 +       .func = can_rcv,
1275 +};
1276 +
1277 +static struct net_proto_family can_family_ops = {
1278 +       .family = PF_CAN,
1279 +       .create = can_create,
1280 +       .owner  = THIS_MODULE,
1281 +};
1282 +
1283 +/* notifier block for netdevice event */
1284 +static struct notifier_block can_netdev_notifier = {
1285 +       .notifier_call = can_notifier,
1286 +};
1287 +
1288 +static __init int can_init(void)
1289 +{
1290 +       printk(banner);
1291 +
1292 +       rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1293 +                                     0, 0, NULL, NULL);
1294 +       if (!rcv_cache)
1295 +               return -ENOMEM;
1296 +
1297 +       /*
1298 +        * Insert struct dev_rcv_lists for reception on all devices.
1299 +        * This struct is zero initialized which is correct for the
1300 +        * embedded hlist heads, the dev pointer, and the entries counter.
1301 +        */
1302 +
1303 +       spin_lock_bh(&rcv_lists_lock);
1304 +       hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1305 +       spin_unlock_bh(&rcv_lists_lock);
1306 +
1307 +       if (stats_timer) {
1308 +               /* the statistics are updated every second (timer triggered) */
1309 +               init_timer(&stattimer);
1310 +               stattimer.function = can_stat_update;
1311 +               stattimer.data = 0;
1312 +               /* update every second */
1313 +               stattimer.expires = jiffies + HZ;
1314 +               /* start statistics timer */
1315 +               add_timer(&stattimer);
1316 +       } else
1317 +               stattimer.function = NULL;
1318 +
1319 +       /* procfs init */
1320 +       can_init_proc();
1321 +
1322 +       /* protocol register */
1323 +       sock_register(&can_family_ops);
1324 +       register_netdevice_notifier(&can_netdev_notifier);
1325 +       dev_add_pack(&can_packet);
1326 +
1327 +       return 0;
1328 +}
1329 +
1330 +static __exit void can_exit(void)
1331 +{
1332 +       struct dev_rcv_lists *d;
1333 +       struct hlist_node *n, *next;
1334 +
1335 +       if (stats_timer)
1336 +               del_timer(&stattimer);
1337 +
1338 +       /* procfs remove */
1339 +       can_remove_proc();
1340 +
1341 +       /* protocol unregister */
1342 +       dev_remove_pack(&can_packet);
1343 +       unregister_netdevice_notifier(&can_netdev_notifier);
1344 +       sock_unregister(PF_CAN);
1345 +
1346 +       /* remove rx_dev_list */
1347 +       spin_lock_bh(&rcv_lists_lock);
1348 +       hlist_del(&rx_alldev_list.list);
1349 +       hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1350 +               hlist_del(&d->list);
1351 +               kfree(d);
1352 +       }
1353 +       spin_unlock_bh(&rcv_lists_lock);
1354 +
1355 +       kmem_cache_destroy(rcv_cache);
1356 +}
1357 +
1358 +module_init(can_init);
1359 +module_exit(can_exit);
1360 Index: linux-2.6.22-rc3/net/can/af_can.h
1361 ===================================================================
1362 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1363 +++ linux-2.6.22-rc3/net/can/af_can.h   2007-05-29 10:14:54.%N +0200
1364 @@ -0,0 +1,120 @@
1365 +/*
1366 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1367 + * All rights reserved.
1368 + *
1369 + * Redistribution and use in source and binary forms, with or without
1370 + * modification, are permitted provided that the following conditions
1371 + * are met:
1372 + * 1. Redistributions of source code must retain the above copyright
1373 + *    notice, this list of conditions, the following disclaimer and
1374 + *    the referenced file 'COPYING'.
1375 + * 2. Redistributions in binary form must reproduce the above copyright
1376 + *    notice, this list of conditions and the following disclaimer in the
1377 + *    documentation and/or other materials provided with the distribution.
1378 + * 3. Neither the name of Volkswagen nor the names of its contributors
1379 + *    may be used to endorse or promote products derived from this software
1380 + *    without specific prior written permission.
1381 + *
1382 + * Alternatively, provided that this notice is retained in full, this
1383 + * software may be distributed under the terms of the GNU General
1384 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1385 + * file from the main directory of the linux kernel source.
1386 + *
1387 + * The provided data structures and external interfaces from this code
1388 + * are not restricted to be used by modules with a GPL compatible license.
1389 + *
1390 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1391 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1392 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1393 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1394 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1395 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1396 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1397 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1398 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1399 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1400 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1401 + * DAMAGE.
1402 + *
1403 + * Send feedback to <socketcan-users@lists.berlios.de>
1404 + *
1405 + */
1406 +
1407 +#ifndef AF_CAN_H
1408 +#define AF_CAN_H
1409 +
1410 +#include <linux/skbuff.h>
1411 +#include <linux/netdevice.h>
1412 +#include <linux/list.h>
1413 +#include <linux/rcupdate.h>
1414 +#include <linux/can.h>
1415 +
1416 +/* af_can rx dispatcher structures */
1417 +
1418 +struct receiver {
1419 +       struct hlist_node list;
1420 +       struct rcu_head rcu;
1421 +       canid_t can_id;
1422 +       canid_t mask;
1423 +       unsigned long matches;
1424 +       void (*func)(struct sk_buff *, void *);
1425 +       void *data;
1426 +       char *ident;
1427 +};
1428 +
1429 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1430 +
1431 +struct dev_rcv_lists {
1432 +       struct hlist_node list;
1433 +       struct rcu_head rcu;
1434 +       struct net_device *dev;
1435 +       struct hlist_head rx[RX_MAX];
1436 +       struct hlist_head rx_sff[0x800];
1437 +       int entries;
1438 +};
1439 +
1440 +/* statistic structures */
1441 +
1442 +struct s_stats {
1443 +       unsigned long jiffies_init;
1444 +
1445 +       unsigned long rx_frames;
1446 +       unsigned long tx_frames;
1447 +       unsigned long matches;
1448 +
1449 +       unsigned long total_rx_rate;
1450 +       unsigned long total_tx_rate;
1451 +       unsigned long total_rx_match_ratio;
1452 +
1453 +       unsigned long current_rx_rate;
1454 +       unsigned long current_tx_rate;
1455 +       unsigned long current_rx_match_ratio;
1456 +
1457 +       unsigned long max_rx_rate;
1458 +       unsigned long max_tx_rate;
1459 +       unsigned long max_rx_match_ratio;
1460 +
1461 +       unsigned long rx_frames_delta;
1462 +       unsigned long tx_frames_delta;
1463 +       unsigned long matches_delta;
1464 +}; /* can be reset e.g. by can_init_stats() */
1465 +
1466 +struct s_pstats {
1467 +       unsigned long stats_reset;
1468 +       unsigned long user_reset;
1469 +       unsigned long rcv_entries;
1470 +       unsigned long rcv_entries_max;
1471 +}; /* persistent statistics */
1472 +
1473 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1474 +extern void can_init_proc(void);
1475 +extern void can_remove_proc(void);
1476 +extern void can_stat_update(unsigned long data);
1477 +
1478 +/* structures and variables from af_can.c needed in proc.c for reading */
1479 +extern struct timer_list stattimer;    /* timer for statistics update */
1480 +extern struct s_stats  stats;          /* packet statistics */
1481 +extern struct s_pstats pstats;         /* receive list statistics */
1482 +extern struct hlist_head rx_dev_list;  /* rx dispatcher structures */
1483 +
1484 +#endif /* AF_CAN_H */
1485 Index: linux-2.6.22-rc3/net/can/proc.c
1486 ===================================================================
1487 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1488 +++ linux-2.6.22-rc3/net/can/proc.c     2007-05-29 10:14:54.%N +0200
1489 @@ -0,0 +1,530 @@
1490 +/*
1491 + * proc.c - procfs support for Protocol family CAN core module
1492 + *
1493 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1494 + * All rights reserved.
1495 + *
1496 + * Redistribution and use in source and binary forms, with or without
1497 + * modification, are permitted provided that the following conditions
1498 + * are met:
1499 + * 1. Redistributions of source code must retain the above copyright
1500 + *    notice, this list of conditions, the following disclaimer and
1501 + *    the referenced file 'COPYING'.
1502 + * 2. Redistributions in binary form must reproduce the above copyright
1503 + *    notice, this list of conditions and the following disclaimer in the
1504 + *    documentation and/or other materials provided with the distribution.
1505 + * 3. Neither the name of Volkswagen nor the names of its contributors
1506 + *    may be used to endorse or promote products derived from this software
1507 + *    without specific prior written permission.
1508 + *
1509 + * Alternatively, provided that this notice is retained in full, this
1510 + * software may be distributed under the terms of the GNU General
1511 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1512 + * file from the main directory of the linux kernel source.
1513 + *
1514 + * The provided data structures and external interfaces from this code
1515 + * are not restricted to be used by modules with a GPL compatible license.
1516 + *
1517 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1518 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1519 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1520 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1521 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1522 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1523 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1524 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1525 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1526 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1527 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1528 + * DAMAGE.
1529 + *
1530 + * Send feedback to <socketcan-users@lists.berlios.de>
1531 + *
1532 + */
1533 +
1534 +#include <linux/module.h>
1535 +#include <linux/proc_fs.h>
1536 +#include <linux/list.h>
1537 +#include <linux/rcupdate.h>
1538 +#include <linux/can/core.h>
1539 +
1540 +#include "af_can.h"
1541 +
1542 +/*
1543 + * proc filenames for the PF_CAN core
1544 + */
1545 +
1546 +#define CAN_PROC_VERSION     "version"
1547 +#define CAN_PROC_STATS       "stats"
1548 +#define CAN_PROC_RESET_STATS "reset_stats"
1549 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
1550 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
1551 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
1552 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
1553 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
1554 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
1555 +
1556 +static struct proc_dir_entry *can_dir         = NULL;
1557 +static struct proc_dir_entry *pde_version     = NULL;
1558 +static struct proc_dir_entry *pde_stats       = NULL;
1559 +static struct proc_dir_entry *pde_reset_stats = NULL;
1560 +static struct proc_dir_entry *pde_rcvlist_all = NULL;
1561 +static struct proc_dir_entry *pde_rcvlist_fil = NULL;
1562 +static struct proc_dir_entry *pde_rcvlist_inv = NULL;
1563 +static struct proc_dir_entry *pde_rcvlist_sff = NULL;
1564 +static struct proc_dir_entry *pde_rcvlist_eff = NULL;
1565 +static struct proc_dir_entry *pde_rcvlist_err = NULL;
1566 +
1567 +static int user_reset = 0;
1568 +
1569 +static const char *rx_list_name[] = {
1570 +       [RX_ERR] = "rx_err",
1571 +       [RX_ALL] = "rx_all",
1572 +       [RX_FIL] = "rx_fil",
1573 +       [RX_INV] = "rx_inv",
1574 +       [RX_EFF] = "rx_eff",
1575 +};
1576 +
1577 +/*
1578 + * af_can statistics stuff
1579 + */
1580 +
1581 +static void can_init_stats(void)
1582 +{
1583 +       /*
1584 +        * This memset function is called from a timer context (when
1585 +        * stattimer is active which is the default) OR in a process
1586 +        * context (reading the proc_fs when stattimer is disabled).
1587 +        */
1588 +       memset(&stats, 0, sizeof(stats));
1589 +       stats.jiffies_init = jiffies;
1590 +
1591 +       pstats.stats_reset++;
1592 +
1593 +       if (user_reset) {
1594 +               user_reset = 0;
1595 +               pstats.user_reset++;
1596 +       }
1597 +}
1598 +
1599 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1600 +                              unsigned long count)
1601 +{
1602 +       unsigned long ret = 0;
1603 +
1604 +       if (oldjif == newjif)
1605 +               return 0;
1606 +
1607 +       /* see can_rcv() - this should NEVER happen! */
1608 +       if (count > (ULONG_MAX / HZ)) {
1609 +               printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
1610 +                      count);
1611 +               return 99999999;
1612 +       }
1613 +
1614 +       ret = (count * HZ) / (newjif - oldjif);
1615 +
1616 +       return ret;
1617 +}
1618 +
1619 +void can_stat_update(unsigned long data)
1620 +{
1621 +       unsigned long j = jiffies; /* snapshot */
1622 +
1623 +       /* restart counting in timer context on user request */
1624 +       if (user_reset)
1625 +               can_init_stats();
1626 +
1627 +       /* restart counting on jiffies overflow */
1628 +       if (j < stats.jiffies_init)
1629 +               can_init_stats();
1630 +
1631 +       /* stats.rx_frames is the definitively max. statistic value */
1632 +
1633 +       /* prevent overflow in calc_rate() */
1634 +       if (stats.rx_frames > (ULONG_MAX / HZ))
1635 +               can_init_stats();
1636 +
1637 +       /* matches overflow - very improbable */
1638 +       if (stats.matches > (ULONG_MAX / 100))
1639 +               can_init_stats();
1640 +
1641 +       /* calc total values */
1642 +       if (stats.rx_frames)
1643 +               stats.total_rx_match_ratio = (stats.matches * 100) /
1644 +                                               stats.rx_frames;
1645 +
1646 +       stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1647 +                                       stats.tx_frames);
1648 +       stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1649 +                                       stats.rx_frames);
1650 +
1651 +       /* calc current values */
1652 +       if (stats.rx_frames_delta)
1653 +               stats.current_rx_match_ratio =
1654 +                       (stats.matches_delta * 100) / stats.rx_frames_delta;
1655 +
1656 +       stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
1657 +       stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
1658 +
1659 +       /* check / update maximum values */
1660 +       if (stats.max_tx_rate < stats.current_tx_rate)
1661 +               stats.max_tx_rate = stats.current_tx_rate;
1662 +
1663 +       if (stats.max_rx_rate < stats.current_rx_rate)
1664 +               stats.max_rx_rate = stats.current_rx_rate;
1665 +
1666 +       if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1667 +               stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1668 +
1669 +       /* clear values for 'current rate' calculation */
1670 +       stats.tx_frames_delta = 0;
1671 +       stats.rx_frames_delta = 0;
1672 +       stats.matches_delta   = 0;
1673 +
1674 +       /* restart timer (one second) */
1675 +       stattimer.expires = jiffies + HZ;
1676 +       add_timer(&stattimer);
1677 +}
1678 +
1679 +/*
1680 + * proc read functions
1681 + *
1682 + * From known use-cases we expect about 10 entries in a receive list to be
1683 + * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
1684 + *
1685 + */
1686 +
1687 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1688 +                            struct net_device *dev)
1689 +{
1690 +       struct receiver *r;
1691 +       struct hlist_node *n;
1692 +
1693 +       rcu_read_lock();
1694 +       hlist_for_each_entry_rcu(r, n, rx_list, list) {
1695 +               char *fmt = (r->can_id & CAN_EFF_FLAG)?
1696 +                       "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
1697 +                       "   %-5s     %03X    %08x  %08x  %08x  %8ld  %s\n";
1698 +
1699 +               len += snprintf(page + len, PAGE_SIZE - len, fmt,
1700 +                               DNAME(dev), r->can_id, r->mask,
1701 +                               (unsigned int)r->func, (unsigned int)r->data,
1702 +                               r->matches, r->ident);
1703 +
1704 +               /* does a typical line fit into the current buffer? */
1705 +
1706 +               /* 100 Bytes before end of buffer */
1707 +               if (len > PAGE_SIZE - 100) {
1708 +                       /* mark output cut off */
1709 +                       len += snprintf(page + len, PAGE_SIZE - len,
1710 +                                       "   (..)\n");
1711 +                       break;
1712 +               }
1713 +       }
1714 +       rcu_read_unlock();
1715 +
1716 +       return len;
1717 +}
1718 +
1719 +static int can_print_recv_banner(char *page, int len)
1720 +{
1721 +       /*
1722 +        *                  can1.  00000000  00000000  00000000
1723 +        *                 .......          0  tp20
1724 +        */
1725 +       len += snprintf(page + len, PAGE_SIZE - len,
1726 +                       "  device   can_id   can_mask  function"
1727 +                       "  userdata   matches  ident\n");
1728 +
1729 +       return len;
1730 +}
1731 +
1732 +static int can_proc_read_stats(char *page, char **start, off_t off,
1733 +                              int count, int *eof, void *data)
1734 +{
1735 +       int len = 0;
1736 +
1737 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1738 +       len += snprintf(page + len, PAGE_SIZE - len,
1739 +                       " %8ld transmitted frames (TXF)\n", stats.tx_frames);
1740 +       len += snprintf(page + len, PAGE_SIZE - len,
1741 +                       " %8ld received frames (RXF)\n", stats.rx_frames);
1742 +       len += snprintf(page + len, PAGE_SIZE - len,
1743 +                       " %8ld matched frames (RXMF)\n", stats.matches);
1744 +
1745 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1746 +
1747 +       if (stattimer.function == can_stat_update) {
1748 +               len += snprintf(page + len, PAGE_SIZE - len,
1749 +                               " %8ld %% total match ratio (RXMR)\n",
1750 +                               stats.total_rx_match_ratio);
1751 +
1752 +               len += snprintf(page + len, PAGE_SIZE - len,
1753 +                               " %8ld frames/s total tx rate (TXR)\n",
1754 +                               stats.total_tx_rate);
1755 +               len += snprintf(page + len, PAGE_SIZE - len,
1756 +                               " %8ld frames/s total rx rate (RXR)\n",
1757 +                               stats.total_rx_rate);
1758 +
1759 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1760 +
1761 +               len += snprintf(page + len, PAGE_SIZE - len,
1762 +                               " %8ld %% current match ratio (CRXMR)\n",
1763 +                               stats.current_rx_match_ratio);
1764 +
1765 +               len += snprintf(page + len, PAGE_SIZE - len,
1766 +                               " %8ld frames/s current tx rate (CTXR)\n",
1767 +                               stats.current_tx_rate);
1768 +               len += snprintf(page + len, PAGE_SIZE - len,
1769 +                               " %8ld frames/s current rx rate (CRXR)\n",
1770 +                               stats.current_rx_rate);
1771 +
1772 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1773 +
1774 +               len += snprintf(page + len, PAGE_SIZE - len,
1775 +                               " %8ld %% max match ratio (MRXMR)\n",
1776 +                               stats.max_rx_match_ratio);
1777 +
1778 +               len += snprintf(page + len, PAGE_SIZE - len,
1779 +                               " %8ld frames/s max tx rate (MTXR)\n",
1780 +                               stats.max_tx_rate);
1781 +               len += snprintf(page + len, PAGE_SIZE - len,
1782 +                               " %8ld frames/s max rx rate (MRXR)\n",
1783 +                               stats.max_rx_rate);
1784 +
1785 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1786 +       }
1787 +
1788 +       len += snprintf(page + len, PAGE_SIZE - len,
1789 +                       " %8ld current receive list entries (CRCV)\n",
1790 +                       pstats.rcv_entries);
1791 +       len += snprintf(page + len, PAGE_SIZE - len,
1792 +                       " %8ld maximum receive list entries (MRCV)\n",
1793 +                       pstats.rcv_entries_max);
1794 +
1795 +       if (pstats.stats_reset)
1796 +               len += snprintf(page + len, PAGE_SIZE - len,
1797 +                               "\n %8ld statistic resets (STR)\n",
1798 +                               pstats.stats_reset);
1799 +
1800 +       if (pstats.user_reset)
1801 +               len += snprintf(page + len, PAGE_SIZE - len,
1802 +                               " %8ld user statistic resets (USTR)\n",
1803 +                               pstats.user_reset);
1804 +
1805 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1806 +
1807 +       *eof = 1;
1808 +       return len;
1809 +}
1810 +
1811 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1812 +                                    int count, int *eof, void *data)
1813 +{
1814 +       int len = 0;
1815 +
1816 +       user_reset = 1;
1817 +
1818 +       if (stattimer.function == can_stat_update) {
1819 +               len += snprintf(page + len, PAGE_SIZE - len,
1820 +                               "Scheduled statistic reset #%ld.\n",
1821 +                               pstats.stats_reset + 1);
1822 +
1823 +       } else {
1824 +               if (stats.jiffies_init != jiffies)
1825 +                       can_init_stats();
1826 +
1827 +               len += snprintf(page + len, PAGE_SIZE - len,
1828 +                               "Performed statistic reset #%ld.\n",
1829 +                               pstats.stats_reset);
1830 +       }
1831 +
1832 +       *eof = 1;
1833 +       return len;
1834 +}
1835 +
1836 +static int can_proc_read_version(char *page, char **start, off_t off,
1837 +                                int count, int *eof, void *data)
1838 +{
1839 +       int len = 0;
1840 +
1841 +       len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1842 +                       CAN_VERSION_STRING);
1843 +       *eof = 1;
1844 +       return len;
1845 +}
1846 +
1847 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1848 +                                int count, int *eof, void *data)
1849 +{
1850 +       int idx = (int)data;
1851 +       int len = 0;
1852 +       struct dev_rcv_lists *d;
1853 +       struct hlist_node *n;
1854 +
1855 +       len += snprintf(page + len, PAGE_SIZE - len,
1856 +                       "\nreceive list '%s':\n", rx_list_name[idx]);
1857 +
1858 +       rcu_read_lock();
1859 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1860 +
1861 +               if (!hlist_empty(&d->rx[idx])) {
1862 +                       len = can_print_recv_banner(page, len);
1863 +                       len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
1864 +               } else
1865 +                       len += snprintf(page + len, PAGE_SIZE - len,
1866 +                                       "  (%s: no entry)\n", DNAME(d->dev));
1867 +
1868 +               /* exit on end of buffer? */
1869 +               if (len > PAGE_SIZE - 100)
1870 +                       break;
1871 +       }
1872 +       rcu_read_unlock();
1873 +
1874 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1875 +
1876 +       *eof = 1;
1877 +       return len;
1878 +}
1879 +
1880 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1881 +                                    int count, int *eof, void *data)
1882 +{
1883 +       int len = 0;
1884 +       struct dev_rcv_lists *d;
1885 +       struct hlist_node *n;
1886 +
1887 +       /* RX_SFF */
1888 +       len += snprintf(page + len, PAGE_SIZE - len,
1889 +                       "\nreceive list 'rx_sff':\n");
1890 +
1891 +       rcu_read_lock();
1892 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1893 +               int i, all_empty = 1;
1894 +               /* check wether at least one list is non-empty */
1895 +               for (i = 0; i < 0x800; i++)
1896 +                       if (!hlist_empty(&d->rx_sff[i])) {
1897 +                               all_empty = 0;
1898 +                               break;
1899 +                       }
1900 +
1901 +               if (!all_empty) {
1902 +                       len = can_print_recv_banner(page, len);
1903 +                       for (i = 0; i < 0x800; i++) {
1904 +                               if (!hlist_empty(&d->rx_sff[i]) &&
1905 +                                   len < PAGE_SIZE - 100)
1906 +                                       len = can_print_rcvlist(page, len,
1907 +                                                               &d->rx_sff[i],
1908 +                                                               d->dev);
1909 +                       }
1910 +               } else
1911 +                       len += snprintf(page + len, PAGE_SIZE - len,
1912 +                                       "  (%s: no entry)\n", DNAME(d->dev));
1913 +
1914 +               /* exit on end of buffer? */
1915 +               if (len > PAGE_SIZE - 100)
1916 +                       break;
1917 +       }
1918 +       rcu_read_unlock();
1919 +
1920 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1921 +
1922 +       *eof = 1;
1923 +       return len;
1924 +}
1925 +
1926 +/*
1927 + * proc utility functions
1928 + */
1929 +
1930 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1931 +                                                       mode_t mode,
1932 +                                                       read_proc_t* read_proc,
1933 +                                                       void *data)
1934 +{
1935 +       if (can_dir)
1936 +               return create_proc_read_entry(name, mode, can_dir, read_proc,
1937 +                                             data);
1938 +       else
1939 +               return NULL;
1940 +}
1941 +
1942 +static void can_remove_proc_readentry(const char *name)
1943 +{
1944 +       if (can_dir)
1945 +               remove_proc_entry(name, can_dir);
1946 +}
1947 +
1948 +/*
1949 + * can_init_proc - create main CAN proc directory and procfs entries
1950 + */
1951 +void can_init_proc(void)
1952 +{
1953 +       /* create /proc/net/can directory */
1954 +       can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
1955 +
1956 +       if (!can_dir) {
1957 +               printk(KERN_INFO "can: failed to create /proc/%s . "
1958 +                      "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
1959 +               return;
1960 +       }
1961 +
1962 +       can_dir->owner = THIS_MODULE;
1963 +
1964 +       /* own procfs entries from the AF_CAN core */
1965 +       pde_version     = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
1966 +                                       can_proc_read_version, NULL);
1967 +       pde_stats       = can_create_proc_readentry(CAN_PROC_STATS, 0644,
1968 +                                       can_proc_read_stats, NULL);
1969 +       pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
1970 +                                       can_proc_read_reset_stats, NULL);
1971 +       pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
1972 +                                       can_proc_read_rcvlist, (void*)RX_ERR);
1973 +       pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
1974 +                                       can_proc_read_rcvlist, (void*)RX_ALL);
1975 +       pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
1976 +                                       can_proc_read_rcvlist, (void*)RX_FIL);
1977 +       pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
1978 +                                       can_proc_read_rcvlist, (void*)RX_INV);
1979 +       pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
1980 +                                       can_proc_read_rcvlist, (void*)RX_EFF);
1981 +       pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
1982 +                                       can_proc_read_rcvlist_sff, NULL);
1983 +}
1984 +
1985 +/*
1986 + * can_remove_proc - remove procfs entries and main CAN proc directory
1987 + */
1988 +void can_remove_proc(void)
1989 +{
1990 +       if (pde_version)
1991 +               can_remove_proc_readentry(CAN_PROC_VERSION);
1992 +
1993 +       if (pde_stats)
1994 +               can_remove_proc_readentry(CAN_PROC_STATS);
1995 +
1996 +       if (pde_reset_stats)
1997 +               can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1998 +
1999 +       if (pde_rcvlist_err)
2000 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
2001 +
2002 +       if (pde_rcvlist_all)
2003 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
2004 +
2005 +       if (pde_rcvlist_fil)
2006 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
2007 +
2008 +       if (pde_rcvlist_inv)
2009 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
2010 +
2011 +       if (pde_rcvlist_eff)
2012 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
2013 +
2014 +       if (pde_rcvlist_sff)
2015 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
2016 +
2017 +       if (can_dir)
2018 +               remove_proc_entry(CAN_PROC_DIR, NULL);
2019 +}
2020 Index: linux-2.6.22-rc3/include/linux/can/error.h
2021 ===================================================================
2022 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
2023 +++ linux-2.6.22-rc3/include/linux/can/error.h  2007-05-30 12:42:27.%N +0200
2024 @@ -0,0 +1,95 @@
2025 +/*
2026 + * linux/can/error.h
2027 + *
2028 + * Definitions of the CAN error frame to be filtered and passed to the user.
2029 + *
2030 + * $Id: error.h 162 2007-03-05 08:25:02Z hartkopp $
2031 + *
2032 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
2033 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
2034 + * All rights reserved.
2035 + *
2036 + * Send feedback to <socketcan-users@lists.berlios.de>
2037 + *
2038 + */
2039 +
2040 +#ifndef CAN_ERROR_H
2041 +#define CAN_ERROR_H
2042 +
2043 +#define CAN_ERR_DLC 8 /* dlc for error frames */
2044 +
2045 +/* error class (mask) in can_id */
2046 +#define CAN_ERR_TX_TIMEOUT   0x00000001U /* TX timeout (by netdevice driver) */
2047 +#define CAN_ERR_LOSTARB      0x00000002U /* lost arbitration    / data[0]    */
2048 +#define CAN_ERR_CRTL         0x00000004U /* controller problems / data[1]    */
2049 +#define CAN_ERR_PROT         0x00000008U /* protocol violations / data[2..3] */
2050 +#define CAN_ERR_TRX          0x00000010U /* transceiver status  / data[4]    */
2051 +#define CAN_ERR_ACK          0x00000020U /* received no ACK on transmission */
2052 +#define CAN_ERR_BUSOFF       0x00000040U /* bus off */
2053 +#define CAN_ERR_BUSERROR     0x00000080U /* bus error (may flood!) */
2054 +#define CAN_ERR_RESTARTED    0x00000100U /* controller restarted */
2055 +
2056 +/* arbitration lost in bit ... / data[0] */
2057 +#define CAN_ERR_LOSTARB_UNSPEC   0x00 /* unspecified */
2058 +                                     /* else bit number in bitstream */
2059 +
2060 +/* error status of CAN-controller / data[1] */
2061 +#define CAN_ERR_CRTL_UNSPEC      0x00 /* unspecified */
2062 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
2063 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
2064 +#define CAN_ERR_CRTL_RX_WARNING  0x04 /* reached warning level for RX errors */
2065 +#define CAN_ERR_CRTL_TX_WARNING  0x08 /* reached warning level for TX errors */
2066 +#define CAN_ERR_CRTL_RX_PASSIVE  0x10 /* reached error passive status RX */
2067 +#define CAN_ERR_CRTL_TX_PASSIVE  0x20 /* reached error passive status TX */
2068 +                                     /* (at least one error counter exceeds */
2069 +                                     /* the protocol-defined level of 127)  */
2070 +
2071 +/* error in CAN protocol (type) / data[2] */
2072 +#define CAN_ERR_PROT_UNSPEC      0x00 /* unspecified */
2073 +#define CAN_ERR_PROT_BIT         0x01 /* single bit error */
2074 +#define CAN_ERR_PROT_FORM        0x02 /* frame format error */
2075 +#define CAN_ERR_PROT_STUFF       0x04 /* bit stuffing error */
2076 +#define CAN_ERR_PROT_BIT0        0x08 /* unable to send dominant bit */
2077 +#define CAN_ERR_PROT_BIT1        0x10 /* unable to send recessive bit */
2078 +#define CAN_ERR_PROT_OVERLOAD    0x20 /* bus overload */
2079 +#define CAN_ERR_PROT_ACTIVE      0x40 /* active error announcement */
2080 +#define CAN_ERR_PROT_TX          0x80 /* error occured on transmission */
2081 +
2082 +/* error in CAN protocol (location) / data[3] */
2083 +#define CAN_ERR_PROT_LOC_UNSPEC  0x00 /* unspecified */
2084 +#define CAN_ERR_PROT_LOC_SOF     0x03 /* start of frame */
2085 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
2086 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
2087 +#define CAN_ERR_PROT_LOC_SRTR    0x04 /* substitute RTR (SFF: RTR) */
2088 +#define CAN_ERR_PROT_LOC_IDE     0x05 /* identifier extension */
2089 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
2090 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
2091 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
2092 +#define CAN_ERR_PROT_LOC_RTR     0x0C /* RTR */
2093 +#define CAN_ERR_PROT_LOC_RES1    0x0D /* reserved bit 1 */
2094 +#define CAN_ERR_PROT_LOC_RES0    0x09 /* reserved bit 0 */
2095 +#define CAN_ERR_PROT_LOC_DLC     0x0B /* data length code */
2096 +#define CAN_ERR_PROT_LOC_DATA    0x0A /* data section */
2097 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
2098 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
2099 +#define CAN_ERR_PROT_LOC_ACK     0x19 /* ACK slot */
2100 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
2101 +#define CAN_ERR_PROT_LOC_EOF     0x1A /* end of frame */
2102 +#define CAN_ERR_PROT_LOC_INTERM  0x12 /* intermission */
2103 +
2104 +/* error status of CAN-transceiver / data[4] */
2105 +/*                                             CANH CANL */
2106 +#define CAN_ERR_TRX_UNSPEC             0x00 /* 0000 0000 */
2107 +#define CAN_ERR_TRX_CANH_NO_WIRE       0x04 /* 0000 0100 */
2108 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT  0x05 /* 0000 0101 */
2109 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC  0x06 /* 0000 0110 */
2110 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND  0x07 /* 0000 0111 */
2111 +#define CAN_ERR_TRX_CANL_NO_WIRE       0x40 /* 0100 0000 */
2112 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT  0x50 /* 0101 0000 */
2113 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC  0x60 /* 0110 0000 */
2114 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND  0x70 /* 0111 0000 */
2115 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
2116 +
2117 +/* controller specific additional information / data[5..7] */
2118 +
2119 +#endif /* CAN_ERROR_H */