]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - patch-series/net-2.6/02-can-core.diff
Update to patch-series to revision 455.
[socketcan-devel.git] / patch-series / net-2.6 / 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       |  113 +++++
13  include/linux/can/core.h  |   80 +++
14  include/linux/can/error.h |   93 ++++
15  net/Kconfig               |    1 
16  net/Makefile              |    1 
17  net/can/Kconfig           |   25 +
18  net/can/Makefile          |    6 
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(+)
23
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
28 @@ -0,0 +1,113 @@
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 +/*
62 + * Controller Area Network Identifier structure
63 + *
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)
68 + */
69 +typedef __u32 canid_t;
70 +
71 +/*
72 + * Controller Area Network Error Frame Mask structure
73 + *
74 + * bit 0-28    : error class mask (see include/linux/can/error.h)
75 + * bit 29-31   : set to zero
76 + */
77 +typedef __u32 can_err_mask_t;
78 +
79 +/**
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.
84 + */
85 +struct can_frame {
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)));
89 +};
90 +
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 */
99 +#define CAN_NPROTO     8
100 +
101 +#define SOL_CAN_BASE 100
102 +
103 +/**
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.
108 + */
109 +struct sockaddr_can {
110 +       sa_family_t can_family;
111 +       int         can_ifindex;
112 +       union {
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;
118 +       } can_addr;
119 +};
120 +
121 +/**
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)
125 + *
126 + * Description:
127 + * A filter matches, when
128 + *
129 + *          <received_can_id> & mask == can_id & mask
130 + *
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).
133 + */
134 +struct can_filter {
135 +       canid_t can_id;
136 +       canid_t can_mask;
137 +};
138 +
139 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
140 +
141 +#endif /* CAN_H */
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
146 @@ -0,0 +1,80 @@
147 +/*
148 + * linux/can/core.h
149 + *
150 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
151 + *
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.
156 + *
157 + * Send feedback to <socketcan-users@lists.berlios.de>
158 + *
159 + */
160 +
161 +#ifndef CAN_CORE_H
162 +#define CAN_CORE_H
163 +
164 +#include <linux/can.h>
165 +#include <linux/skbuff.h>
166 +#include <linux/netdevice.h>
167 +
168 +#define CAN_VERSION "20070802"
169 +
170 +/* increment this number each time you change some user-space interface */
171 +#define CAN_ABI_VERSION "8"
172 +
173 +#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
174 +
175 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
176 +
177 +#define CAN_PROC_DIR "net/can" /* /proc/... */
178 +
179 +/**
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.
186 + */
187 +struct can_proto {
188 +       int              type;
189 +       int              protocol;
190 +       int              capability;
191 +       struct proto_ops *ops;
192 +       struct proto     *prot;
193 +};
194 +
195 +/* function prototypes for the CAN networklayer core (af_can.c) */
196 +
197 +extern int can_proto_register(struct can_proto *cp);
198 +extern int can_proto_unregister(struct can_proto *cp);
199 +
200 +extern int can_rx_register(struct net_device *dev, canid_t can_id,
201 +                          canid_t mask,
202 +                          void (*func)(struct sk_buff *, void *),
203 +                          void *data, char *ident);
204 +
205 +extern int can_rx_unregister(struct net_device *dev, canid_t can_id,
206 +                            canid_t mask,
207 +                            void (*func)(struct sk_buff *, void *),
208 +                            void *data);
209 +
210 +extern int can_send(struct sk_buff *skb, int loop);
211 +
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)
220 +#else
221 +#define DBG(args...)
222 +#define DBG_FRAME(args...)
223 +#define DBG_SKB(skb)
224 +#endif
225 +
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
231 @@ -210,6 +210,7 @@
232  endmenu
233  
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
243 @@ -34,6 +34,7 @@
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
255 @@ -0,0 +1,25 @@
256 +#
257 +# Controller Area Network (CAN) network layer core configuration
258 +#
259 +
260 +menuconfig CAN
261 +       depends on NET
262 +       tristate "CAN bus subsystem support"
263 +       ---help---
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>.
270 +
271 +         If you want CAN support, you should say Y here and also to the
272 +         specific driver for your controller(s) below.
273 +
274 +config CAN_DEBUG_CORE
275 +       bool "CAN Core debugging messages"
276 +       depends on CAN
277 +       ---help---
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
285 @@ -0,0 +1,6 @@
286 +#
287 +#  Makefile for the Linux Controller Area Network core.
288 +#
289 +
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
296 @@ -0,0 +1,995 @@
297 +/*
298 + * af_can.c - Protocol family CAN core module
299 + *            (used by different CAN protocol modules)
300 + *
301 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
302 + * All rights reserved.
303 + *
304 + * Redistribution and use in source and binary forms, with or without
305 + * modification, are permitted provided that the following conditions
306 + * are met:
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.
316 + *
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.
321 + *
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.
324 + *
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
336 + * DAMAGE.
337 + *
338 + * Send feedback to <socketcan-users@lists.berlios.de>
339 + *
340 + */
341 +
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>
359 +
360 +#include "af_can.h"
361 +
362 +#define IDENT "core"
363 +static __initdata const char banner[] = KERN_INFO
364 +       "can: controller area network core (" CAN_VERSION_STRING ")\n";
365 +
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>");
370 +
371 +MODULE_ALIAS_NETPROTO(PF_CAN);
372 +
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)");
376 +
377 +#ifdef CONFIG_CAN_DEBUG_CORE
378 +static int debug;
379 +module_param(debug, int, S_IRUGO);
380 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
381 +#endif
382 +
383 +HLIST_HEAD(rx_dev_list);
384 +static struct dev_rcv_lists rx_alldev_list;
385 +static DEFINE_SPINLOCK(rcv_lists_lock);
386 +
387 +static struct kmem_cache *rcv_cache __read_mostly;
388 +
389 +/* table of registered CAN protocols */
390 +static struct can_proto *proto_tab[CAN_NPROTO];
391 +
392 +struct timer_list stattimer; /* timer for statistics update */
393 +struct s_stats  stats;       /* packet statistics */
394 +struct s_pstats pstats;      /* receive list statistics */
395 +
396 +/*
397 + * af_can socket functions
398 + */
399 +
400 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
401 +{
402 +       struct sock *sk = sock->sk;
403 +
404 +       switch (cmd) {
405 +
406 +       case SIOCGSTAMP:
407 +               return sock_get_timestamp(sk, (struct timeval __user *)arg);
408 +
409 +       default:
410 +               return -ENOIOCTLCMD;
411 +       }
412 +}
413 +
414 +static void can_sock_destruct(struct sock *sk)
415 +{
416 +       DBG("called for sock %p\n", sk);
417 +
418 +       skb_queue_purge(&sk->sk_receive_queue);
419 +       if (sk->sk_protinfo)
420 +               kfree(sk->sk_protinfo);
421 +}
422 +
423 +static int can_create(struct socket *sock, int protocol)
424 +{
425 +       struct sock *sk;
426 +       struct can_proto *cp;
427 +       char module_name[sizeof("can-proto-000")];
428 +       int ret = 0;
429 +
430 +       DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
431 +
432 +       sock->state = SS_UNCONNECTED;
433 +
434 +       if (protocol < 0 || protocol >= CAN_NPROTO)
435 +               return -EINVAL;
436 +
437 +       DBG("looking up proto %d in proto_tab[]\n", protocol);
438 +
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);
443 +
444 +               /*
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
448 +                */
449 +               if (ret == -ENOSYS)
450 +                       printk(KERN_INFO "can: request_module(%s) not"
451 +                              " implemented.\n", module_name);
452 +               else if (ret)
453 +                       printk(KERN_ERR "can: request_module(%s) failed\n",
454 +                              module_name);
455 +       }
456 +
457 +       /* check for success and correct type */
458 +       cp = proto_tab[protocol];
459 +       if (!cp || cp->type != sock->type)
460 +               return -EPROTONOSUPPORT;
461 +
462 +       if (cp->capability >= 0 && !capable(cp->capability))
463 +               return -EPERM;
464 +
465 +       sock->ops = cp->ops;
466 +
467 +       sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
468 +       if (!sk)
469 +               return -ENOMEM;
470 +
471 +       sock_init_data(sock, sk);
472 +       sk->sk_destruct = can_sock_destruct;
473 +
474 +       DBG("created sock: %p\n", sk);
475 +
476 +       if (sk->sk_prot->init)
477 +               ret = sk->sk_prot->init(sk);
478 +
479 +       if (ret) {
480 +               /* release sk on errors */
481 +               sock_orphan(sk);
482 +               sock_put(sk);
483 +       }
484 +
485 +       return ret;
486 +}
487 +
488 +/*
489 + * af_can tx path
490 + */
491 +
492 +/**
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!)
496 + *
497 + * Return:
498 + *  0 on success
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()
502 + */
503 +int can_send(struct sk_buff *skb, int loop)
504 +{
505 +       int err;
506 +
507 +       if (skb->dev->type != ARPHRD_CAN) {
508 +               kfree_skb(skb);
509 +               return -EPERM;
510 +       }
511 +
512 +       if (!(skb->dev->flags & IFF_UP)) {
513 +               kfree_skb(skb);
514 +               return -ENETDOWN;
515 +       }
516 +
517 +       skb->protocol = htons(ETH_P_CAN);
518 +
519 +       if (loop) {
520 +               /* local loopback of sent CAN frames */
521 +
522 +               /* indication for the CAN driver: do loopback */
523 +               skb->pkt_type = PACKET_LOOPBACK;
524 +
525 +               /*
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.
532 +                */
533 +
534 +               if (!(skb->dev->flags & IFF_LOOPBACK)) {
535 +                       /*
536 +                        * If the interface is not capable to do loopback
537 +                        * itself, we do it here.
538 +                        */
539 +                       struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
540 +
541 +                       if (!newskb) {
542 +                               kfree_skb(skb);
543 +                               return -ENOMEM;
544 +                       }
545 +
546 +                       newskb->sk = skb->sk;
547 +                       newskb->ip_summed = CHECKSUM_UNNECESSARY;
548 +                       newskb->pkt_type = PACKET_BROADCAST;
549 +                       netif_rx(newskb);
550 +               }
551 +       } else {
552 +               /* indication for the CAN driver: no loopback required */
553 +               skb->pkt_type = PACKET_HOST;
554 +       }
555 +
556 +       /* send to netdevice */
557 +       err = dev_queue_xmit(skb);
558 +       if (err > 0)
559 +               err = net_xmit_errno(err);
560 +
561 +       /* update statistics */
562 +       stats.tx_frames++;
563 +       stats.tx_frames_delta++;
564 +
565 +       return err;
566 +}
567 +EXPORT_SYMBOL(can_send);
568 +
569 +/*
570 + * af_can rx path
571 + */
572 +
573 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
574 +{
575 +       struct dev_rcv_lists *d;
576 +       struct hlist_node *n;
577 +
578 +       /*
579 +        * find receive list for this device
580 +        *
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.
589 +        */
590 +
591 +       hlist_for_each_entry(d, n, &rx_dev_list, list) {
592 +               if (d->dev == dev)
593 +                       break;
594 +       }
595 +
596 +       return n ? d : NULL;
597 +}
598 +
599 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
600 +                                       struct dev_rcv_lists *d)
601 +{
602 +       canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
603 +
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];
609 +       }
610 +
611 +       /* ensure valid values in can_mask */
612 +       if (*mask & CAN_EFF_FLAG)
613 +               *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
614 +       else
615 +               *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
616 +
617 +       /* reduce condition testing at receive time */
618 +       *can_id &= *mask;
619 +
620 +       /* inverse can_id/can_mask filter */
621 +       if (inv)
622 +               return &d->rx[RX_INV];
623 +
624 +       /* mask == 0 => no condition testing at receive time */
625 +       if (!(*mask))
626 +               return &d->rx[RX_ALL];
627 +
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];
633 +               }
634 +       } else {
635 +               if (*mask == CAN_SFF_MASK)
636 +                       return &d->rx_sff[*can_id];
637 +       }
638 +
639 +       /* default: filter via can_id/can_mask */
640 +       return &d->rx[RX_FIL];
641 +}
642 +
643 +/**
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
651 + *
652 + * Description:
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
655 + *
656 + *          <received_can_id> & mask == can_id & mask
657 + *
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).
660 + *
661 + * Return:
662 + *  0 on success
663 + *  -ENOMEM on missing cache mem to create subscription entry
664 + *  -ENODEV unknown device
665 + */
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,
668 +                   char *ident)
669 +{
670 +       struct receiver *r;
671 +       struct hlist_head *rl;
672 +       struct dev_rcv_lists *d;
673 +       int ret = 0;
674 +
675 +       /* insert new receiver  (dev,canid,mask) -> (func,data) */
676 +
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);
679 +
680 +       r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
681 +       if (!r)
682 +               return -ENOMEM;
683 +
684 +       spin_lock_bh(&rcv_lists_lock);
685 +
686 +       d = find_dev_rcv_lists(dev);
687 +       if (d) {
688 +               rl = find_rcv_list(&can_id, &mask, d);
689 +
690 +               r->can_id  = can_id;
691 +               r->mask    = mask;
692 +               r->matches = 0;
693 +               r->func    = func;
694 +               r->data    = data;
695 +               r->ident   = ident;
696 +
697 +               hlist_add_head_rcu(&r->list, rl);
698 +               d->entries++;
699 +
700 +               pstats.rcv_entries++;
701 +               if (pstats.rcv_entries_max < pstats.rcv_entries)
702 +                       pstats.rcv_entries_max = pstats.rcv_entries;
703 +       } else {
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);
707 +               ret = -ENODEV;
708 +       }
709 +
710 +       spin_unlock_bh(&rcv_lists_lock);
711 +
712 +       return ret;
713 +}
714 +EXPORT_SYMBOL(can_rx_register);
715 +
716 +/*
717 + * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
718 + */
719 +static void can_rx_delete_device(struct rcu_head *rp)
720 +{
721 +       struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
722 +
723 +       DBG("removing dev_rcv_list at %p\n", d);
724 +       kfree(d);
725 +}
726 +
727 +/*
728 + * can_rx_delete_receiver - rcu callback for single receiver entry removal
729 + */
730 +static void can_rx_delete_receiver(struct rcu_head *rp)
731 +{
732 +       struct receiver *r = container_of(rp, struct receiver, rcu);
733 +
734 +       DBG("removing receiver at %p\n", r);
735 +       kmem_cache_free(rcv_cache, r);
736 +}
737 +
738 +/**
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
742 + * @mask: CAN mask
743 + * @func: callback function on filter match
744 + * @data: returned parameter for callback function
745 + *
746 + * Description:
747 + *  Removes subscription entry depending on given (subscription) values.
748 + *
749 + * Return:
750 + *  0 on success
751 + *  -EINVAL on missing subscription entry
752 + *  -ENODEV unknown device
753 + */
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)
756 +{
757 +       struct receiver *r = NULL;
758 +       struct hlist_head *rl;
759 +       struct hlist_node *next;
760 +       struct dev_rcv_lists *d;
761 +       int ret = 0;
762 +
763 +       DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
764 +           dev, DNAME(dev), can_id, mask, func, data);
765 +
766 +       spin_lock_bh(&rcv_lists_lock);
767 +
768 +       d = find_dev_rcv_lists(dev);
769 +       if (!d) {
770 +               DBG("receive list not found for dev %s, id %03X, mask %03X\n",
771 +                   DNAME(dev), can_id, mask);
772 +               ret = -ENODEV;
773 +               goto out;
774 +       }
775 +
776 +       rl = find_rcv_list(&can_id, &mask, d);
777 +
778 +       /*
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.
782 +        */
783 +
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)
787 +                       break;
788 +       }
789 +
790 +       /*
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.
794 +        */
795 +
796 +       if (!next) {
797 +               DBG("receive list entry not found for "
798 +                   "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
799 +               ret = -EINVAL;
800 +               r = NULL;
801 +               d = NULL;
802 +               goto out;
803 +       }
804 +
805 +       hlist_del_rcu(&r->list);
806 +       d->entries--;
807 +
808 +       if (pstats.rcv_entries > 0)
809 +               pstats.rcv_entries--;
810 +
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",
814 +                   dev->name);
815 +               hlist_del_rcu(&d->list);
816 +       } else
817 +               d = NULL;
818 +
819 + out:
820 +       spin_unlock_bh(&rcv_lists_lock);
821 +
822 +       /* schedule the receiver item for deletion */
823 +       if (r)
824 +               call_rcu(&r->rcu, can_rx_delete_receiver);
825 +
826 +       /* schedule the device structure for deletion */
827 +       if (d)
828 +               call_rcu(&d->rcu, can_rx_delete_device);
829 +
830 +       return ret;
831 +}
832 +EXPORT_SYMBOL(can_rx_unregister);
833 +
834 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
835 +{
836 +       struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
837 +
838 +       DBG("skbuff %p cloned to %p\n", skb, clone);
839 +       if (clone) {
840 +               clone->sk = skb->sk;
841 +               r->func(clone, r->data);
842 +               r->matches++;
843 +       }
844 +}
845 +
846 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
847 +{
848 +       struct receiver *r;
849 +       struct hlist_node *n;
850 +       int matches = 0;
851 +       struct can_frame *cf = (struct can_frame *)skb->data;
852 +       canid_t can_id = cf->can_id;
853 +
854 +       if (d->entries == 0)
855 +               return 0;
856 +
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);
862 +                               deliver(skb, r);
863 +                               matches++;
864 +                       }
865 +               }
866 +               return matches;
867 +       }
868 +
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);
872 +               deliver(skb, r);
873 +               matches++;
874 +       }
875 +
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);
880 +                       deliver(skb, r);
881 +                       matches++;
882 +               }
883 +       }
884 +
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);
889 +                       deliver(skb, r);
890 +                       matches++;
891 +               }
892 +       }
893 +
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);
899 +                               deliver(skb, r);
900 +                               matches++;
901 +                       }
902 +               }
903 +       } else {
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);
907 +                       deliver(skb, r);
908 +                       matches++;
909 +               }
910 +       }
911 +
912 +       return matches;
913 +}
914 +
915 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
916 +                  struct packet_type *pt, struct net_device *orig_dev)
917 +{
918 +       struct dev_rcv_lists *d;
919 +       int matches;
920 +
921 +       DBG("received skbuff on device %s, ptype %04x\n",
922 +           dev->name, ntohs(pt->type));
923 +       DBG_SKB(skb);
924 +       DBG_FRAME("af_can: can_rcv: received CAN frame",
925 +                 (struct can_frame *)skb->data);
926 +
927 +       if (dev->type != ARPHRD_CAN) {
928 +               kfree_skb(skb);
929 +               return 0;
930 +       }
931 +
932 +       /* update statistics */
933 +       stats.rx_frames++;
934 +       stats.rx_frames_delta++;
935 +
936 +       rcu_read_lock();
937 +
938 +       /* deliver the packet to sockets listening on all devices */
939 +       matches = can_rcv_filter(&rx_alldev_list, skb);
940 +
941 +       /* find receive list for this device */
942 +       d = find_dev_rcv_lists(dev);
943 +       if (d)
944 +               matches += can_rcv_filter(d, skb);
945 +
946 +       rcu_read_unlock();
947 +
948 +       /* free the skbuff allocated by the netdevice driver */
949 +       DBG("freeing skbuff %p\n", skb);
950 +       kfree_skb(skb);
951 +
952 +       if (matches > 0) {
953 +               stats.matches++;
954 +               stats.matches_delta++;
955 +       }
956 +
957 +       return 0;
958 +}
959 +
960 +/*
961 + * af_can protocol functions
962 + */
963 +
964 +/**
965 + * can_proto_register - register CAN transport protocol
966 + * @cp: pointer to CAN protocol structure
967 + *
968 + * Return:
969 + *  0 on success
970 + *  -EINVAL invalid (out of range) protocol number
971 + *  -EBUSY  protocol already in use
972 + *  -ENOBUF if proto_register() fails
973 + */
974 +int can_proto_register(struct can_proto *cp)
975 +{
976 +       int proto = cp->protocol;
977 +       int err = 0;
978 +
979 +       if (proto < 0 || proto >= CAN_NPROTO) {
980 +               printk(KERN_ERR "can: protocol number %d out of range\n",
981 +                      proto);
982 +               return -EINVAL;
983 +       }
984 +       if (proto_tab[proto]) {
985 +               printk(KERN_ERR "can: protocol %d already registered\n",
986 +                      proto);
987 +               return -EBUSY;
988 +       }
989 +
990 +       err = proto_register(cp->prot, 0);
991 +       if (err < 0)
992 +               return err;
993 +
994 +       proto_tab[proto] = cp;
995 +
996 +       /* use generic ioctl function if the module doesn't bring its own */
997 +       if (!cp->ops->ioctl)
998 +               cp->ops->ioctl = can_ioctl;
999 +
1000 +       return err;
1001 +}
1002 +EXPORT_SYMBOL(can_proto_register);
1003 +
1004 +/**
1005 + * can_proto_unregister - unregister CAN transport protocol
1006 + * @cp: pointer to CAN protocol structure
1007 + *
1008 + * Return:
1009 + *  0 on success
1010 + *  -ESRCH protocol number was not registered
1011 + */
1012 +int can_proto_unregister(struct can_proto *cp)
1013 +{
1014 +       int proto = cp->protocol;
1015 +
1016 +       if (!proto_tab[proto]) {
1017 +               printk(KERN_ERR "can: protocol %d is not registered\n", proto);
1018 +               return -ESRCH;
1019 +       }
1020 +       proto_unregister(cp->prot);
1021 +       proto_tab[proto] = NULL;
1022 +
1023 +       return 0;
1024 +}
1025 +EXPORT_SYMBOL(can_proto_unregister);
1026 +
1027 +/*
1028 + * af_can notifier to create/remove CAN netdevice specific structs
1029 + */
1030 +static int can_notifier(struct notifier_block *nb, unsigned long msg,
1031 +                       void *data)
1032 +{
1033 +       struct net_device *dev = (struct net_device *)data;
1034 +       struct dev_rcv_lists *d;
1035 +
1036 +       DBG("msg %ld for dev %p (%s idx %d)\n",
1037 +           msg, dev, dev->name, dev->ifindex);
1038 +
1039 +       if (dev->type != ARPHRD_CAN)
1040 +               return NOTIFY_DONE;
1041 +
1042 +       switch (msg) {
1043 +
1044 +       case NETDEV_REGISTER:
1045 +
1046 +               /*
1047 +                * create new dev_rcv_lists for this device
1048 +                *
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.
1053 +                */
1054 +
1055 +               DBG("creating new dev_rcv_lists for %s\n", dev->name);
1056 +
1057 +               d = kzalloc(sizeof(*d),
1058 +                           in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1059 +               if (!d) {
1060 +                       printk(KERN_ERR
1061 +                              "can: allocation of receive list failed\n");
1062 +                       return NOTIFY_DONE;
1063 +               }
1064 +               d->dev = dev;
1065 +
1066 +               spin_lock_bh(&rcv_lists_lock);
1067 +               hlist_add_head_rcu(&d->list, &rx_dev_list);
1068 +               spin_unlock_bh(&rcv_lists_lock);
1069 +
1070 +               break;
1071 +
1072 +       case NETDEV_UNREGISTER:
1073 +               spin_lock_bh(&rcv_lists_lock);
1074 +
1075 +               d = find_dev_rcv_lists(dev);
1076 +               if (d) {
1077 +                       DBG("remove dev_rcv_list for %s (%d entries)\n",
1078 +                           dev->name, d->entries);
1079 +
1080 +                       if (d->entries) {
1081 +                               d->remove_on_zero_entries = 1;
1082 +                               d = NULL;
1083 +                       } else
1084 +                               hlist_del_rcu(&d->list);
1085 +               } else
1086 +                       printk(KERN_ERR "can: notifier: receive list not "
1087 +                              "found for dev %s\n", dev->name);
1088 +
1089 +               spin_unlock_bh(&rcv_lists_lock);
1090 +
1091 +               if (d)
1092 +                       call_rcu(&d->rcu, can_rx_delete_device);
1093 +
1094 +               break;
1095 +       }
1096 +
1097 +       return NOTIFY_DONE;
1098 +}
1099 +
1100 +/*
1101 + * af_can debugging stuff
1102 + */
1103 +
1104 +#ifdef CONFIG_CAN_DEBUG_CORE
1105 +
1106 +#define DBG_BSIZE 1024
1107 +
1108 +/**
1109 + * can_debug_cframe - print CAN frame
1110 + * @msg: pointer to message printed before the given CAN frame
1111 + * @cf: pointer to CAN frame
1112 + */
1113 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
1114 +{
1115 +       va_list ap;
1116 +       int len;
1117 +       int dlc, i;
1118 +       char *buf;
1119 +
1120 +       buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1121 +       if (!buf)
1122 +               return;
1123 +
1124 +       len = sprintf(buf, KERN_DEBUG);
1125 +       va_start(ap, cf);
1126 +       len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
1127 +       buf[len++] = ':';
1128 +       buf[len++] = ' ';
1129 +       va_end(ap);
1130 +
1131 +       dlc = cf->can_dlc;
1132 +       if (dlc > 8)
1133 +               dlc = 8;
1134 +
1135 +       if (cf->can_id & CAN_EFF_FLAG)
1136 +               len += sprintf(buf + len, "<%08X> [%X] ",
1137 +                              cf->can_id & CAN_EFF_MASK, dlc);
1138 +       else
1139 +               len += sprintf(buf + len, "<%03X> [%X] ",
1140 +                              cf->can_id & CAN_SFF_MASK, dlc);
1141 +
1142 +       for (i = 0; i < dlc; i++)
1143 +               len += sprintf(buf + len, "%02X ", cf->data[i]);
1144 +
1145 +       if (cf->can_id & CAN_RTR_FLAG)
1146 +               len += sprintf(buf + len, "(RTR)");
1147 +
1148 +       buf[len++] = '\n';
1149 +       buf[len]   = '\0';
1150 +       printk(buf);
1151 +       kfree(buf);
1152 +}
1153 +EXPORT_SYMBOL(can_debug_cframe);
1154 +
1155 +/**
1156 + * can_debug_skb - print socket buffer content to kernel log
1157 + * @skb: pointer to socket buffer
1158 + */
1159 +void can_debug_skb(struct sk_buff *skb)
1160 +{
1161 +       int len, nbytes, i;
1162 +       char *buf;
1163 +
1164 +       buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
1165 +       if (!buf)
1166 +               return;
1167 +
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++) {
1181 +               if (i % 16 == 0)
1182 +                       len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
1183 +               if (len < DBG_BSIZE - 16) {
1184 +                       len += sprintf(buf + len, " %02x", skb->head[i]);
1185 +               } else {
1186 +                       len += sprintf(buf + len, "...");
1187 +                       break;
1188 +               }
1189 +       }
1190 +       buf[len++] = '\n';
1191 +       buf[len]   = '\0';
1192 +       printk(buf);
1193 +       kfree(buf);
1194 +}
1195 +EXPORT_SYMBOL(can_debug_skb);
1196 +
1197 +#endif
1198 +
1199 +/*
1200 + * af_can module init/exit functions
1201 + */
1202 +
1203 +static struct packet_type can_packet = {
1204 +       .type = __constant_htons(ETH_P_CAN),
1205 +       .dev  = NULL,
1206 +       .func = can_rcv,
1207 +};
1208 +
1209 +static struct net_proto_family can_family_ops = {
1210 +       .family = PF_CAN,
1211 +       .create = can_create,
1212 +       .owner  = THIS_MODULE,
1213 +};
1214 +
1215 +/* notifier block for netdevice event */
1216 +static struct notifier_block can_netdev_notifier = {
1217 +       .notifier_call = can_notifier,
1218 +};
1219 +
1220 +static __init int can_init(void)
1221 +{
1222 +       printk(banner);
1223 +
1224 +       rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1225 +                                     0, 0, NULL);
1226 +       if (!rcv_cache)
1227 +               return -ENOMEM;
1228 +
1229 +       /*
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.
1233 +        */
1234 +
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);
1238 +
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);
1248 +       } else
1249 +               stattimer.function = NULL;
1250 +
1251 +       /* procfs init */
1252 +       can_init_proc();
1253 +
1254 +       /* protocol register */
1255 +       sock_register(&can_family_ops);
1256 +       register_netdevice_notifier(&can_netdev_notifier);
1257 +       dev_add_pack(&can_packet);
1258 +
1259 +       return 0;
1260 +}
1261 +
1262 +static __exit void can_exit(void)
1263 +{
1264 +       struct dev_rcv_lists *d;
1265 +       struct hlist_node *n, *next;
1266 +
1267 +       if (stats_timer)
1268 +               del_timer(&stattimer);
1269 +
1270 +       /* procfs remove */
1271 +       can_remove_proc();
1272 +
1273 +       /* protocol unregister */
1274 +       dev_remove_pack(&can_packet);
1275 +       unregister_netdevice_notifier(&can_netdev_notifier);
1276 +       sock_unregister(PF_CAN);
1277 +
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);
1283 +               kfree(d);
1284 +       }
1285 +       spin_unlock_bh(&rcv_lists_lock);
1286 +
1287 +       kmem_cache_destroy(rcv_cache);
1288 +}
1289 +
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
1296 @@ -0,0 +1,121 @@
1297 +/*
1298 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1299 + * All rights reserved.
1300 + *
1301 + * Redistribution and use in source and binary forms, with or without
1302 + * modification, are permitted provided that the following conditions
1303 + * are met:
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.
1313 + *
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.
1318 + *
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.
1321 + *
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
1333 + * DAMAGE.
1334 + *
1335 + * Send feedback to <socketcan-users@lists.berlios.de>
1336 + *
1337 + */
1338 +
1339 +#ifndef AF_CAN_H
1340 +#define AF_CAN_H
1341 +
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>
1347 +
1348 +/* af_can rx dispatcher structures */
1349 +
1350 +struct receiver {
1351 +       struct hlist_node list;
1352 +       struct rcu_head rcu;
1353 +       canid_t can_id;
1354 +       canid_t mask;
1355 +       unsigned long matches;
1356 +       void (*func)(struct sk_buff *, void *);
1357 +       void *data;
1358 +       char *ident;
1359 +};
1360 +
1361 +enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_EFF, RX_MAX };
1362 +
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;
1370 +       int entries;
1371 +};
1372 +
1373 +/* statistic structures */
1374 +
1375 +struct s_stats {
1376 +       unsigned long jiffies_init;
1377 +
1378 +       unsigned long rx_frames;
1379 +       unsigned long tx_frames;
1380 +       unsigned long matches;
1381 +
1382 +       unsigned long total_rx_rate;
1383 +       unsigned long total_tx_rate;
1384 +       unsigned long total_rx_match_ratio;
1385 +
1386 +       unsigned long current_rx_rate;
1387 +       unsigned long current_tx_rate;
1388 +       unsigned long current_rx_match_ratio;
1389 +
1390 +       unsigned long max_rx_rate;
1391 +       unsigned long max_tx_rate;
1392 +       unsigned long max_rx_match_ratio;
1393 +
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() */
1398 +
1399 +struct s_pstats {
1400 +       unsigned long stats_reset;
1401 +       unsigned long user_reset;
1402 +       unsigned long rcv_entries;
1403 +       unsigned long rcv_entries_max;
1404 +}; /* persistent statistics */
1405 +
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);
1410 +
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 */
1416 +
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
1422 @@ -0,0 +1,531 @@
1423 +/*
1424 + * proc.c - procfs support for Protocol family CAN core module
1425 + *
1426 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1427 + * All rights reserved.
1428 + *
1429 + * Redistribution and use in source and binary forms, with or without
1430 + * modification, are permitted provided that the following conditions
1431 + * are met:
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.
1441 + *
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.
1446 + *
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.
1449 + *
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
1461 + * DAMAGE.
1462 + *
1463 + * Send feedback to <socketcan-users@lists.berlios.de>
1464 + *
1465 + */
1466 +
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>
1472 +
1473 +#include "af_can.h"
1474 +
1475 +/*
1476 + * proc filenames for the PF_CAN core
1477 + */
1478 +
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"
1488 +
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;
1499 +
1500 +static int user_reset;
1501 +
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",
1508 +};
1509 +
1510 +/*
1511 + * af_can statistics stuff
1512 + */
1513 +
1514 +static void can_init_stats(void)
1515 +{
1516 +       /*
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).
1520 +        */
1521 +       memset(&stats, 0, sizeof(stats));
1522 +       stats.jiffies_init = jiffies;
1523 +
1524 +       pstats.stats_reset++;
1525 +
1526 +       if (user_reset) {
1527 +               user_reset = 0;
1528 +               pstats.user_reset++;
1529 +       }
1530 +}
1531 +
1532 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
1533 +                              unsigned long count)
1534 +{
1535 +       unsigned long ret = 0;
1536 +
1537 +       if (oldjif == newjif)
1538 +               return 0;
1539 +
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",
1543 +                      count);
1544 +               return 99999999;
1545 +       }
1546 +
1547 +       ret = (count * HZ) / (newjif - oldjif);
1548 +
1549 +       return ret;
1550 +}
1551 +
1552 +void can_stat_update(unsigned long data)
1553 +{
1554 +       unsigned long j = jiffies; /* snapshot */
1555 +
1556 +       /* restart counting in timer context on user request */
1557 +       if (user_reset)
1558 +               can_init_stats();
1559 +
1560 +       /* restart counting on jiffies overflow */
1561 +       if (j < stats.jiffies_init)
1562 +               can_init_stats();
1563 +
1564 +       /* stats.rx_frames is the definitively max. statistic value */
1565 +
1566 +       /* prevent overflow in calc_rate() */
1567 +       if (stats.rx_frames > (ULONG_MAX / HZ))
1568 +               can_init_stats();
1569 +
1570 +       /* matches overflow - very improbable */
1571 +       if (stats.matches > (ULONG_MAX / 100))
1572 +               can_init_stats();
1573 +
1574 +       /* calc total values */
1575 +       if (stats.rx_frames)
1576 +               stats.total_rx_match_ratio = (stats.matches * 100) /
1577 +                                               stats.rx_frames;
1578 +
1579 +       stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
1580 +                                       stats.tx_frames);
1581 +       stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
1582 +                                       stats.rx_frames);
1583 +
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;
1588 +
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);
1591 +
1592 +       /* check / update maximum values */
1593 +       if (stats.max_tx_rate < stats.current_tx_rate)
1594 +               stats.max_tx_rate = stats.current_tx_rate;
1595 +
1596 +       if (stats.max_rx_rate < stats.current_rx_rate)
1597 +               stats.max_rx_rate = stats.current_rx_rate;
1598 +
1599 +       if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
1600 +               stats.max_rx_match_ratio = stats.current_rx_match_ratio;
1601 +
1602 +       /* clear values for 'current rate' calculation */
1603 +       stats.tx_frames_delta = 0;
1604 +       stats.rx_frames_delta = 0;
1605 +       stats.matches_delta   = 0;
1606 +
1607 +       /* restart timer (one second) */
1608 +       stattimer.expires = jiffies + HZ;
1609 +       add_timer(&stattimer);
1610 +}
1611 +
1612 +/*
1613 + * proc read functions
1614 + *
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.
1617 + *
1618 + */
1619 +
1620 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
1621 +                            struct net_device *dev)
1622 +{
1623 +       struct receiver *r;
1624 +       struct hlist_node *n;
1625 +
1626 +       rcu_read_lock();
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";
1631 +
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);
1636 +
1637 +               /* does a typical line fit into the current buffer? */
1638 +
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,
1643 +                                       "   (..)\n");
1644 +                       break;
1645 +               }
1646 +       }
1647 +       rcu_read_unlock();
1648 +
1649 +       return len;
1650 +}
1651 +
1652 +static int can_print_recv_banner(char *page, int len)
1653 +{
1654 +       /*
1655 +        *                  can1.  00000000  00000000  00000000
1656 +        *                 .......          0  tp20
1657 +        */
1658 +       len += snprintf(page + len, PAGE_SIZE - len,
1659 +                       "  device   can_id   can_mask  function"
1660 +                       "  userdata   matches  ident\n");
1661 +
1662 +       return len;
1663 +}
1664 +
1665 +static int can_proc_read_stats(char *page, char **start, off_t off,
1666 +                              int count, int *eof, void *data)
1667 +{
1668 +       int len = 0;
1669 +
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);
1677 +
1678 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1679 +
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);
1684 +
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);
1691 +
1692 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1693 +
1694 +               len += snprintf(page + len, PAGE_SIZE - len,
1695 +                               " %8ld %% current match ratio (CRXMR)\n",
1696 +                               stats.current_rx_match_ratio);
1697 +
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);
1704 +
1705 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1706 +
1707 +               len += snprintf(page + len, PAGE_SIZE - len,
1708 +                               " %8ld %% max match ratio (MRXMR)\n",
1709 +                               stats.max_rx_match_ratio);
1710 +
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);
1717 +
1718 +               len += snprintf(page + len, PAGE_SIZE - len, "\n");
1719 +       }
1720 +
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);
1727 +
1728 +       if (pstats.stats_reset)
1729 +               len += snprintf(page + len, PAGE_SIZE - len,
1730 +                               "\n %8ld statistic resets (STR)\n",
1731 +                               pstats.stats_reset);
1732 +
1733 +       if (pstats.user_reset)
1734 +               len += snprintf(page + len, PAGE_SIZE - len,
1735 +                               " %8ld user statistic resets (USTR)\n",
1736 +                               pstats.user_reset);
1737 +
1738 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1739 +
1740 +       *eof = 1;
1741 +       return len;
1742 +}
1743 +
1744 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
1745 +                                    int count, int *eof, void *data)
1746 +{
1747 +       int len = 0;
1748 +
1749 +       user_reset = 1;
1750 +
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);
1755 +
1756 +       } else {
1757 +               if (stats.jiffies_init != jiffies)
1758 +                       can_init_stats();
1759 +
1760 +               len += snprintf(page + len, PAGE_SIZE - len,
1761 +                               "Performed statistic reset #%ld.\n",
1762 +                               pstats.stats_reset);
1763 +       }
1764 +
1765 +       *eof = 1;
1766 +       return len;
1767 +}
1768 +
1769 +static int can_proc_read_version(char *page, char **start, off_t off,
1770 +                                int count, int *eof, void *data)
1771 +{
1772 +       int len = 0;
1773 +
1774 +       len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
1775 +                       CAN_VERSION_STRING);
1776 +       *eof = 1;
1777 +       return len;
1778 +}
1779 +
1780 +static int can_proc_read_rcvlist(char *page, char **start, off_t off,
1781 +                                int count, int *eof, void *data)
1782 +{
1783 +       /* double cast to prevent GCC warning */
1784 +       int idx = (int)(long)data;
1785 +       int len = 0;
1786 +       struct dev_rcv_lists *d;
1787 +       struct hlist_node *n;
1788 +
1789 +       len += snprintf(page + len, PAGE_SIZE - len,
1790 +                       "\nreceive list '%s':\n", rx_list_name[idx]);
1791 +
1792 +       rcu_read_lock();
1793 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
1794 +
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);
1798 +               } else
1799 +                       len += snprintf(page + len, PAGE_SIZE - len,
1800 +                                       "  (%s: no entry)\n", DNAME(d->dev));
1801 +
1802 +               /* exit on end of buffer? */
1803 +               if (len > PAGE_SIZE - 100)
1804 +                       break;
1805 +       }
1806 +       rcu_read_unlock();
1807 +
1808 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1809 +
1810 +       *eof = 1;
1811 +       return len;
1812 +}
1813 +
1814 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
1815 +                                    int count, int *eof, void *data)
1816 +{
1817 +       int len = 0;
1818 +       struct dev_rcv_lists *d;
1819 +       struct hlist_node *n;
1820 +
1821 +       /* RX_SFF */
1822 +       len += snprintf(page + len, PAGE_SIZE - len,
1823 +                       "\nreceive list 'rx_sff':\n");
1824 +
1825 +       rcu_read_lock();
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])) {
1831 +                               all_empty = 0;
1832 +                               break;
1833 +                       }
1834 +
1835 +               if (!all_empty) {
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,
1841 +                                                               &d->rx_sff[i],
1842 +                                                               d->dev);
1843 +                       }
1844 +               } else
1845 +                       len += snprintf(page + len, PAGE_SIZE - len,
1846 +                                       "  (%s: no entry)\n", DNAME(d->dev));
1847 +
1848 +               /* exit on end of buffer? */
1849 +               if (len > PAGE_SIZE - 100)
1850 +                       break;
1851 +       }
1852 +       rcu_read_unlock();
1853 +
1854 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
1855 +
1856 +       *eof = 1;
1857 +       return len;
1858 +}
1859 +
1860 +/*
1861 + * proc utility functions
1862 + */
1863 +
1864 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
1865 +                                                       mode_t mode,
1866 +                                                       read_proc_t *read_proc,
1867 +                                                       void *data)
1868 +{
1869 +       if (can_dir)
1870 +               return create_proc_read_entry(name, mode, can_dir, read_proc,
1871 +                                             data);
1872 +       else
1873 +               return NULL;
1874 +}
1875 +
1876 +static void can_remove_proc_readentry(const char *name)
1877 +{
1878 +       if (can_dir)
1879 +               remove_proc_entry(name, can_dir);
1880 +}
1881 +
1882 +/*
1883 + * can_init_proc - create main CAN proc directory and procfs entries
1884 + */
1885 +void can_init_proc(void)
1886 +{
1887 +       /* create /proc/net/can directory */
1888 +       can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
1889 +
1890 +       if (!can_dir) {
1891 +               printk(KERN_INFO "can: failed to create /proc/%s . "
1892 +                      "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
1893 +               return;
1894 +       }
1895 +
1896 +       can_dir->owner = THIS_MODULE;
1897 +
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);
1917 +}
1918 +
1919 +/*
1920 + * can_remove_proc - remove procfs entries and main CAN proc directory
1921 + */
1922 +void can_remove_proc(void)
1923 +{
1924 +       if (pde_version)
1925 +               can_remove_proc_readentry(CAN_PROC_VERSION);
1926 +
1927 +       if (pde_stats)
1928 +               can_remove_proc_readentry(CAN_PROC_STATS);
1929 +
1930 +       if (pde_reset_stats)
1931 +               can_remove_proc_readentry(CAN_PROC_RESET_STATS);
1932 +
1933 +       if (pde_rcvlist_err)
1934 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
1935 +
1936 +       if (pde_rcvlist_all)
1937 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
1938 +
1939 +       if (pde_rcvlist_fil)
1940 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
1941 +
1942 +       if (pde_rcvlist_inv)
1943 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
1944 +
1945 +       if (pde_rcvlist_eff)
1946 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
1947 +
1948 +       if (pde_rcvlist_sff)
1949 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
1950 +
1951 +       if (can_dir)
1952 +               remove_proc_entry(CAN_PROC_DIR, NULL);
1953 +}
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
1958 @@ -0,0 +1,93 @@
1959 +/*
1960 + * linux/can/error.h
1961 + *
1962 + * Definitions of the CAN error frame to be filtered and passed to the user.
1963 + *
1964 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
1965 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1966 + * All rights reserved.
1967 + *
1968 + * Send feedback to <socketcan-users@lists.berlios.de>
1969 + *
1970 + */
1971 +
1972 +#ifndef CAN_ERROR_H
1973 +#define CAN_ERROR_H
1974 +
1975 +#define CAN_ERR_DLC 8 /* dlc for error frames */
1976 +
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 */
1987 +
1988 +/* arbitration lost in bit ... / data[0] */
1989 +#define CAN_ERR_LOSTARB_UNSPEC   0x00 /* unspecified */
1990 +                                     /* else bit number in bitstream */
1991 +
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)  */
2002 +
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 */
2013 +
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 */
2035 +
2036 +/* error status of CAN-transceiver / data[4] */
2037 +/*                                             CANH CANL */
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 */
2048 +
2049 +/* controller specific additional information / data[5..7] */
2050 +
2051 +#endif /* CAN_ERROR_H */