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