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