]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - patches/linux-2.6.21-rc3/can-networking-R180-2.6.21-rc3_2_New_Source.diff
Added patches for socketcan Revision 180 for the current Kernel 2.6.21-rc3 .
[socketcan-devel.git] / patches / linux-2.6.21-rc3 / can-networking-R180-2.6.21-rc3_2_New_Source.diff
1 diff -N -u -r b/drivers/net/can/Kconfig c/drivers/net/can/Kconfig
2 --- b/drivers/net/can/Kconfig   1970-01-01 01:00:00.000000000 +0100
3 +++ c/drivers/net/can/Kconfig   2007-03-09 13:45:53.000000000 +0100
4 @@ -0,0 +1,26 @@
5 +menu "CAN Device Drivers"
6 +       depends on CAN
7 +
8 +config CAN_VCAN
9 +       tristate "Virtual Local CAN Interface (vcan)"
10 +       depends on CAN
11 +       default N
12 +       ---help---
13 +         Similar to the network loopback devices, vcan offers a
14 +         virtual local CAN interface.
15 +
16 +         This driver can also be built as a module.  If so, the module
17 +         will be called vcan.
18 +
19 +config CAN_DEBUG_DEVICES
20 +       bool "CAN devices debugging messages"
21 +       depends on CAN
22 +       default N
23 +       ---help---
24 +         Say Y here if you want the CAN device drivers to produce a bunch of
25 +         debug messages to the system log.  Select this if you are having
26 +         a problem with CAN support and want to see more of what is going
27 +         on.
28 +
29 +endmenu
30 +
31 diff -N -u -r b/drivers/net/can/Makefile c/drivers/net/can/Makefile
32 --- b/drivers/net/can/Makefile  1970-01-01 01:00:00.000000000 +0100
33 +++ c/drivers/net/can/Makefile  2007-03-09 13:45:53.000000000 +0100
34 @@ -0,0 +1,5 @@
35 +#
36 +#  Makefile for the Linux Controller Area Network drivers.
37 +#
38 +
39 +obj-$(CONFIG_CAN_VCAN)         += vcan.o
40 diff -N -u -r b/drivers/net/can/vcan.c c/drivers/net/can/vcan.c
41 --- b/drivers/net/can/vcan.c    1970-01-01 01:00:00.000000000 +0100
42 +++ c/drivers/net/can/vcan.c    2007-03-09 13:45:53.000000000 +0100
43 @@ -0,0 +1,301 @@
44 +/*
45 + * vcan.c - Virtual CAN interface
46 + *
47 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
48 + * All rights reserved.
49 + *
50 + * Redistribution and use in source and binary forms, with or without
51 + * modification, are permitted provided that the following conditions
52 + * are met:
53 + * 1. Redistributions of source code must retain the above copyright
54 + *    notice, this list of conditions, the following disclaimer and
55 + *    the referenced file 'COPYING'.
56 + * 2. Redistributions in binary form must reproduce the above copyright
57 + *    notice, this list of conditions and the following disclaimer in the
58 + *    documentation and/or other materials provided with the distribution.
59 + * 3. Neither the name of Volkswagen nor the names of its contributors
60 + *    may be used to endorse or promote products derived from this software
61 + *    without specific prior written permission.
62 + *
63 + * Alternatively, provided that this notice is retained in full, this
64 + * software may be distributed under the terms of the GNU General
65 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
66 + * file from the main directory of the linux kernel source.
67 + *
68 + * The provided data structures and external interfaces from this code
69 + * are not restricted to be used by modules with a GPL compatible license.
70 + *
71 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
72 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
73 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
74 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
75 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
78 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
79 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
80 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
82 + * DAMAGE.
83 + *
84 + * Send feedback to <socketcan-users@lists.berlios.de>
85 + *
86 + */
87 +
88 +#include <linux/autoconf.h>
89 +#include <linux/module.h>
90 +#include <linux/init.h>
91 +#include <linux/netdevice.h>
92 +#include <linux/if_arp.h>
93 +#include <linux/if_ether.h>
94 +
95 +#include <linux/can.h>
96 +#include <linux/can/version.h>
97 +
98 +RCSID("$Id: vcan.c 168 2007-03-05 13:33:59Z hartkopp $");
99 +
100 +static __initdata const char banner[] = KERN_INFO "CAN: virtual CAN "
101 +                                       "interface " VERSION "\n"; 
102 +
103 +MODULE_DESCRIPTION("virtual CAN interface");
104 +MODULE_LICENSE("Dual BSD/GPL");
105 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
106 +
107 +#ifdef CONFIG_CAN_DEBUG_DEVICES
108 +static int debug = 0;
109 +module_param(debug, int, S_IRUGO);
110 +#define DBG(args...)       (debug & 1 ? \
111 +                              (printk(KERN_DEBUG "VCAN %s: ", __func__), \
112 +                               printk(args)) : 0)
113 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
114 +#define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
115 +#else
116 +#define DBG(args...)
117 +#define DBG_FRAME(args...)
118 +#define DBG_SKB(skb)
119 +#endif
120 +
121 +/* Indicate if this VCAN driver should do a real loopback, or if this */
122 +/* should be done in af_can.c */
123 +#undef  DO_LOOPBACK
124 +
125 +#define STATSIZE sizeof(struct net_device_stats)
126 +
127 +static int numdev = 4; /* default number of virtual CAN interfaces */
128 +module_param(numdev, int, S_IRUGO);
129 +MODULE_PARM_DESC(numdev, "Number of virtual CAN devices");
130 +
131 +static struct net_device **vcan_devs; /* root pointer to netdevice structs */
132 +
133 +static int vcan_open(struct net_device *dev)
134 +{
135 +       DBG("%s: interface up\n", dev->name);
136 +
137 +       netif_start_queue(dev);
138 +       return 0;
139 +}
140 +
141 +static int vcan_stop(struct net_device *dev)
142 +{
143 +       DBG("%s: interface down\n", dev->name);
144 +
145 +       netif_stop_queue(dev);
146 +       return 0;
147 +}
148 +
149 +#ifdef DO_LOOPBACK
150 +
151 +static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
152 +{
153 +       struct net_device_stats *stats = netdev_priv(dev);
154 +       stats->rx_packets++;
155 +       stats->rx_bytes += skb->len;
156 +
157 +       skb->protocol  = htons(ETH_P_CAN);
158 +       skb->dev       = dev;
159 +       skb->ip_summed = CHECKSUM_UNNECESSARY;
160 +
161 +       DBG("received skbuff on interface %d\n", dev->ifindex);
162 +       DBG_SKB(skb);
163 +
164 +       netif_rx(skb);
165 +}
166 +
167 +#endif
168 +
169 +static int vcan_tx(struct sk_buff *skb, struct net_device *dev)
170 +{
171 +       struct net_device_stats *stats = netdev_priv(dev);
172 +       int loop;
173 +
174 +       DBG("sending skbuff on interface %s\n", dev->name);
175 +       DBG_SKB(skb);
176 +       DBG_FRAME("VCAN: transmit CAN frame", (struct can_frame *)skb->data);
177 +
178 +       stats->tx_packets++;
179 +       stats->tx_bytes += skb->len;
180 +
181 +       loop = *(struct sock **)skb->cb != NULL;   /* loopback required */
182 +
183 +#ifdef DO_LOOPBACK
184 +       if (loop) {
185 +               if (atomic_read(&skb->users) != 1) {
186 +                       struct sk_buff *old_skb = skb;
187 +                       skb = skb_clone(old_skb, GFP_ATOMIC);
188 +                       DBG("  freeing old skbuff %p, using new skbuff %p\n",
189 +                           old_skb, skb);
190 +                       kfree_skb(old_skb);
191 +                       if (!skb) {
192 +                               return 0;
193 +                       }
194 +               } else
195 +                       skb_orphan(skb);
196 +
197 +               vcan_rx(skb, dev); /* with packet counting */
198 +       } else {
199 +               /* no looped packets => no counting */
200 +               kfree_skb(skb);
201 +       }
202 +#else
203 +       /* only count, when the CAN core did a loopback */
204 +       if (loop) {
205 +               stats->rx_packets++;
206 +               stats->rx_bytes += skb->len;
207 +       }
208 +       kfree_skb(skb);
209 +#endif
210 +       return 0;
211 +}
212 +
213 +static int vcan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
214 +{
215 +       return -EOPNOTSUPP;
216 +}
217 +
218 +static int vcan_rebuild_header(struct sk_buff *skb)
219 +{
220 +       DBG("skbuff %p\n", skb);
221 +       DBG_SKB(skb);
222 +       return 0;
223 +}
224 +
225 +static int vcan_header(struct sk_buff *skb, struct net_device *dev,
226 +                      unsigned short type, void *daddr, void *saddr,
227 +                      unsigned int len)
228 +{
229 +       DBG("skbuff %p, device %p\n", skb, dev);
230 +       DBG_SKB(skb);
231 +       return 0;
232 +}
233 +
234 +
235 +static struct net_device_stats *vcan_get_stats(struct net_device *dev)
236 +{
237 +       struct net_device_stats *stats = netdev_priv(dev);
238 +       return stats;
239 +}
240 +
241 +static void vcan_init(struct net_device *dev)
242 +{
243 +       DBG("dev %s\n", dev->name);
244 +
245 +       ether_setup(dev);
246 +
247 +       memset(dev->priv, 0, STATSIZE);
248 +
249 +       dev->type              = ARPHRD_CAN;
250 +       dev->mtu               = sizeof(struct can_frame);
251 +       dev->flags             = IFF_NOARP;
252 +#ifdef DO_LOOPBACK
253 +       dev->flags            |= IFF_LOOPBACK;
254 +#endif
255 +
256 +       dev->open              = vcan_open;
257 +       dev->stop              = vcan_stop;
258 +       dev->set_config        = NULL;
259 +       dev->hard_start_xmit   = vcan_tx;
260 +       dev->do_ioctl          = vcan_ioctl;
261 +       dev->get_stats         = vcan_get_stats;
262 +       dev->hard_header       = vcan_header;
263 +       dev->rebuild_header    = vcan_rebuild_header;
264 +       dev->hard_header_cache = NULL;
265 +
266 +       SET_MODULE_OWNER(dev);
267 +}
268 +
269 +static __init int vcan_init_module(void)
270 +{
271 +       int i, ndev = 0, result = 0;
272 +
273 +       printk(banner);
274 +
275 +       if (numdev < 1)
276 +               numdev = 1; /* register at least one interface */
277 +
278 +       printk(KERN_INFO "vcan: registering %d virtual CAN interfaces.\n",
279 +              numdev );
280 +
281 +       vcan_devs = kmalloc(numdev * sizeof(struct net_device *), GFP_KERNEL);
282 +       if (!vcan_devs) {
283 +               printk(KERN_ERR "vcan: Can't allocate vcan devices array!\n");
284 +               return -ENOMEM;
285 +       }
286 +
287 +       /* Clear the pointer array */
288 +       memset(vcan_devs, 0, numdev * sizeof(struct net_device *));
289 +
290 +       for (i = 0; i < numdev; i++) {
291 +               if (!(vcan_devs[i] = alloc_netdev(STATSIZE, "vcan%d",
292 +                                                 vcan_init))) {
293 +                       printk(KERN_ERR "vcan: error allocating net_device\n");
294 +                       result = -ENOMEM;
295 +                       goto out;
296 +               } else if ((result = register_netdev(vcan_devs[i])) < 0) {
297 +                       printk(KERN_ERR "vcan: error %d registering "
298 +                              "interface %s\n",
299 +                              result, vcan_devs[i]->name);
300 +                       free_netdev(vcan_devs[i]);
301 +                       vcan_devs[i] = NULL;
302 +                       goto out;
303 +               } else {
304 +                       DBG("successfully registered interface %s\n",
305 +                           vcan_devs[i]->name);
306 +                       ndev++;
307 +               }
308 +       }
309 +
310 +       if (ndev)
311 +               return 0;
312 +
313 + out:
314 +       for (i = 0; i < numdev; i++) {
315 +               if (vcan_devs[i]) {
316 +                       unregister_netdev(vcan_devs[i]);
317 +                       free_netdev(vcan_devs[i]);
318 +               }
319 +       }
320 +
321 +       kfree(vcan_devs);
322 +
323 +       return result;
324 +}
325 +
326 +static __exit void vcan_cleanup_module(void)
327 +{
328 +       int i;
329 +
330 +       if (!vcan_devs)
331 +               return;
332 +
333 +       for (i = 0; i < numdev; i++) {
334 +               if (vcan_devs[i]) {
335 +                       unregister_netdev(vcan_devs[i]);
336 +                       free_netdev(vcan_devs[i]);
337 +               }
338 +       }
339 +
340 +       kfree(vcan_devs);
341 +}
342 +
343 +module_init(vcan_init_module);
344 +module_exit(vcan_cleanup_module);
345 diff -N -u -r b/include/linux/can/bcm.h c/include/linux/can/bcm.h
346 --- b/include/linux/can/bcm.h   1970-01-01 01:00:00.000000000 +0100
347 +++ c/include/linux/can/bcm.h   2007-03-07 17:13:13.000000000 +0100
348 @@ -0,0 +1,56 @@
349 +/*
350 + * linux/can/bcm.h
351 + *
352 + * Definitions for CAN Broadcast Manager (BCM)
353 + *
354 + * $Id: bcm.h 176 2007-03-07 16:12:46Z hartkopp $
355 + *
356 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
357 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
358 + * All rights reserved.
359 + *
360 + * Send feedback to <socketcan-users@lists.berlios.de>
361 + *
362 + */
363 +
364 +#ifndef CAN_BCM_H
365 +#define CAN_BCM_H
366 +
367 +struct bcm_msg_head {
368 +       int opcode;                   /* command */
369 +       int flags;                    /* special flags */
370 +       int count;                    /* run 'count' times ival1 then ival2 */
371 +       struct timeval ival1, ival2;  /* intervals */
372 +       canid_t can_id;               /* 32 Bit SFF/EFF. MSB set at EFF */
373 +       int nframes;                  /* number of following can_frame's */
374 +       struct can_frame frames[0];
375 +};
376 +
377 +enum {
378 +       TX_SETUP = 1,   /* create (cyclic) transmission task */
379 +       TX_DELETE,      /* remove (cyclic) transmission task */
380 +       TX_READ,        /* read properties of (cyclic) transmission task */
381 +       TX_SEND,        /* send one CAN frame */
382 +       RX_SETUP,       /* create RX content filter subscription */
383 +       RX_DELETE,      /* remove RX content filter subscription */
384 +       RX_READ,        /* read properties of RX content filter subscription */
385 +       TX_STATUS,      /* reply to TX_READ request */
386 +       TX_EXPIRED,     /* notification on performed transmissions (count=0) */
387 +       RX_STATUS,      /* reply to RX_READ request */
388 +       RX_TIMEOUT,     /* cyclic message is absent */
389 +       RX_CHANGED      /* updated CAN frame (detected content change) */
390 +};
391 +
392 +#define SETTIMER            0x0001
393 +#define STARTTIMER          0x0002
394 +#define TX_COUNTEVT         0x0004
395 +#define TX_ANNOUNCE         0x0008
396 +#define TX_CP_CAN_ID        0x0010
397 +#define RX_FILTER_ID        0x0020
398 +#define RX_CHECK_DLC        0x0040
399 +#define RX_NO_AUTOTIMER     0x0080
400 +#define RX_ANNOUNCE_RESUME  0x0100
401 +#define TX_RESET_MULTI_IDX  0x0200
402 +#define RX_RTR_FRAME        0x0400
403 +
404 +#endif /* CAN_BCM_H */
405 diff -N -u -r b/include/linux/can/core.h c/include/linux/can/core.h
406 --- b/include/linux/can/core.h  1970-01-01 01:00:00.000000000 +0100
407 +++ c/include/linux/can/core.h  2007-03-08 12:03:06.000000000 +0100
408 @@ -0,0 +1,62 @@
409 +/*
410 + * linux/can/core.h
411 + *
412 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
413 + *
414 + * $Id: core.h 177 2007-03-08 11:02:43Z hartkopp $
415 + *
416 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
417 + *          Urs Thuermann   <urs.thuermann@volkswagen.de>
418 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
419 + * All rights reserved.
420 + *
421 + * Send feedback to <socketcan-users@lists.berlios.de>
422 + *
423 + */
424 +
425 +#ifndef CAN_CORE_H
426 +#define CAN_CORE_H
427 +
428 +#include <linux/can.h>
429 +#include <linux/skbuff.h>
430 +#include <linux/netdevice.h>
431 +
432 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
433 +
434 +#define CAN_PROC_DIR "net/can" /* /proc/... */
435 +
436 +struct can_proto {
437 +       int              type;
438 +       int              protocol;
439 +       int              capability;
440 +       struct proto_ops *ops;
441 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
442 +       struct proto     *prot;
443 +#else
444 +       struct module    *owner;
445 +       int              (*init)(struct sock *sk);
446 +       size_t           obj_size;
447 +#endif
448 +};
449 +
450 +/* function prototypes for the CAN networklayer core (af_can.c) */
451 +
452 +void can_debug_skb(struct sk_buff *skb);
453 +void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
454 +
455 +void can_proto_register(struct can_proto *cp);
456 +void can_proto_unregister(struct can_proto *cp);
457 +int  can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
458 +                    void (*func)(struct sk_buff *, void *), void *data,
459 +                    char *ident);
460 +int  can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
461 +                      void (*func)(struct sk_buff *, void *), void *data);
462 +void can_dev_register(struct net_device *dev,
463 +                     void (*func)(unsigned long msg, void *), void *data);
464 +void can_dev_unregister(struct net_device *dev,
465 +                       void (*func)(unsigned long msg, void *), void *data);
466 +int  can_send(struct sk_buff *skb, int loop);
467 +
468 +unsigned long timeval2jiffies(struct timeval *tv, int round_up);
469 +
470 +#endif /* CAN_CORE_H */
471 diff -N -u -r b/include/linux/can/error.h c/include/linux/can/error.h
472 --- b/include/linux/can/error.h 1970-01-01 01:00:00.000000000 +0100
473 +++ c/include/linux/can/error.h 2007-03-05 09:25:43.000000000 +0100
474 @@ -0,0 +1,95 @@
475 +/*
476 + * linux/can/error.h
477 + *
478 + * Definitions of the CAN error frame to be filtered and passed to the user.
479 + *
480 + * $Id: error.h 162 2007-03-05 08:25:02Z hartkopp $
481 + *
482 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
483 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
484 + * All rights reserved.
485 + *
486 + * Send feedback to <socketcan-users@lists.berlios.de>
487 + *
488 + */
489 +
490 +#ifndef CAN_ERROR_H
491 +#define CAN_ERROR_H
492 +
493 +#define CAN_ERR_DLC 8 /* dlc for error frames */
494 +
495 +/* error class (mask) in can_id */
496 +#define CAN_ERR_TX_TIMEOUT   0x00000001U /* TX timeout (by netdevice driver) */
497 +#define CAN_ERR_LOSTARB      0x00000002U /* lost arbitration    / data[0]    */
498 +#define CAN_ERR_CRTL         0x00000004U /* controller problems / data[1]    */
499 +#define CAN_ERR_PROT         0x00000008U /* protocol violations / data[2..3] */
500 +#define CAN_ERR_TRX          0x00000010U /* transceiver status  / data[4]    */
501 +#define CAN_ERR_ACK          0x00000020U /* received no ACK on transmission */
502 +#define CAN_ERR_BUSOFF       0x00000040U /* bus off */
503 +#define CAN_ERR_BUSERROR     0x00000080U /* bus error (may flood!) */
504 +#define CAN_ERR_RESTARTED    0x00000100U /* controller restarted */
505 +
506 +/* arbitration lost in bit ... / data[0] */
507 +#define CAN_ERR_LOSTARB_UNSPEC   0x00 /* unspecified */
508 +                                     /* else bit number in bitstream */
509 +
510 +/* error status of CAN-controller / data[1] */
511 +#define CAN_ERR_CRTL_UNSPEC      0x00 /* unspecified */
512 +#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
513 +#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
514 +#define CAN_ERR_CRTL_RX_WARNING  0x04 /* reached warning level for RX errors */
515 +#define CAN_ERR_CRTL_TX_WARNING  0x08 /* reached warning level for TX errors */
516 +#define CAN_ERR_CRTL_RX_PASSIVE  0x10 /* reached error passive status RX */
517 +#define CAN_ERR_CRTL_TX_PASSIVE  0x20 /* reached error passive status TX */
518 +                                     /* (at least one error counter exceeds */
519 +                                     /* the protocol-defined level of 127)  */
520 +
521 +/* error in CAN protocol (type) / data[2] */
522 +#define CAN_ERR_PROT_UNSPEC      0x00 /* unspecified */
523 +#define CAN_ERR_PROT_BIT         0x01 /* single bit error */
524 +#define CAN_ERR_PROT_FORM        0x02 /* frame format error */
525 +#define CAN_ERR_PROT_STUFF       0x04 /* bit stuffing error */
526 +#define CAN_ERR_PROT_BIT0        0x08 /* unable to send dominant bit */
527 +#define CAN_ERR_PROT_BIT1        0x10 /* unable to send recessive bit */
528 +#define CAN_ERR_PROT_OVERLOAD    0x20 /* bus overload */
529 +#define CAN_ERR_PROT_ACTIVE      0x40 /* active error announcement */
530 +#define CAN_ERR_PROT_TX          0x80 /* error occured on transmission */
531 +
532 +/* error in CAN protocol (location) / data[3] */
533 +#define CAN_ERR_PROT_LOC_UNSPEC  0x00 /* unspecified */
534 +#define CAN_ERR_PROT_LOC_SOF     0x03 /* start of frame */
535 +#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
536 +#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
537 +#define CAN_ERR_PROT_LOC_SRTR    0x04 /* substitute RTR (SFF: RTR) */
538 +#define CAN_ERR_PROT_LOC_IDE     0x05 /* identifier extension */
539 +#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
540 +#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
541 +#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
542 +#define CAN_ERR_PROT_LOC_RTR     0x0C /* RTR */
543 +#define CAN_ERR_PROT_LOC_RES1    0x0D /* reserved bit 1 */
544 +#define CAN_ERR_PROT_LOC_RES0    0x09 /* reserved bit 0 */
545 +#define CAN_ERR_PROT_LOC_DLC     0x0B /* data length code */
546 +#define CAN_ERR_PROT_LOC_DATA    0x0A /* data section */
547 +#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
548 +#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
549 +#define CAN_ERR_PROT_LOC_ACK     0x19 /* ACK slot */
550 +#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
551 +#define CAN_ERR_PROT_LOC_EOF     0x1A /* end of frame */
552 +#define CAN_ERR_PROT_LOC_INTERM  0x12 /* intermission */
553 +
554 +/* error status of CAN-transceiver / data[4] */
555 +/*                                             CANH CANL */
556 +#define CAN_ERR_TRX_UNSPEC             0x00 /* 0000 0000 */
557 +#define CAN_ERR_TRX_CANH_NO_WIRE       0x04 /* 0000 0100 */
558 +#define CAN_ERR_TRX_CANH_SHORT_TO_BAT  0x05 /* 0000 0101 */
559 +#define CAN_ERR_TRX_CANH_SHORT_TO_VCC  0x06 /* 0000 0110 */
560 +#define CAN_ERR_TRX_CANH_SHORT_TO_GND  0x07 /* 0000 0111 */
561 +#define CAN_ERR_TRX_CANL_NO_WIRE       0x40 /* 0100 0000 */
562 +#define CAN_ERR_TRX_CANL_SHORT_TO_BAT  0x50 /* 0101 0000 */
563 +#define CAN_ERR_TRX_CANL_SHORT_TO_VCC  0x60 /* 0110 0000 */
564 +#define CAN_ERR_TRX_CANL_SHORT_TO_GND  0x70 /* 0111 0000 */
565 +#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
566 +
567 +/* controller specific additional information / data[5..7] */
568 +
569 +#endif /* CAN_ERROR_H */
570 diff -N -u -r b/include/linux/can/raw.h c/include/linux/can/raw.h
571 --- b/include/linux/can/raw.h   1970-01-01 01:00:00.000000000 +0100
572 +++ c/include/linux/can/raw.h   2007-03-07 17:13:13.000000000 +0100
573 @@ -0,0 +1,31 @@
574 +/*
575 + * linux/can/raw.h
576 + *
577 + * Definitions for raw CAN sockets
578 + *
579 + * $Id: raw.h 176 2007-03-07 16:12:46Z hartkopp $
580 + *
581 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
582 + *          Urs Thuermann   <urs.thuermann@volkswagen.de>
583 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
584 + * All rights reserved.
585 + *
586 + * Send feedback to <socketcan-users@lists.berlios.de>
587 + *
588 + */
589 +
590 +#ifndef CAN_RAW_H
591 +#define CAN_RAW_H
592 +
593 +#include <linux/can.h>
594 +
595 +#define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW)
596 +
597 +/* for socket options affecting the socket (not the global system) */
598 +
599 +#define CAN_RAW_FILTER         1       /* set 0 .. n can_filter(s)          */
600 +#define CAN_RAW_ERR_FILTER     2       /* set filter for error frames       */
601 +#define CAN_RAW_LOOPBACK       3       /* local loopback (default:on)       */
602 +#define CAN_RAW_RECV_OWN_MSGS  4       /* receive my own msgs (default:off) */
603 +
604 +#endif
605 diff -N -u -r b/include/linux/can/version.h c/include/linux/can/version.h
606 --- b/include/linux/can/version.h       1970-01-01 01:00:00.000000000 +0100
607 +++ c/include/linux/can/version.h       2007-03-05 14:59:06.000000000 +0100
608 @@ -0,0 +1,40 @@
609 +/*
610 + * linux/can/version.h
611 + *
612 + * Version information for the CAN network layer implementation
613 +
614 + * Author: Urs Thuermann   <urs.thuermann@volkswagen.de>
615 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
616 + * All rights reserved.
617 + *
618 + * Send feedback to <socketcan-users@lists.berlios.de>
619 + *
620 + */
621 +
622 +#ifndef CAN_VERSION_H
623 +#define CAN_VERSION_H
624 +
625 +#define RCSID(s) asm(".section .rodata.str1.1,\"aMS\",@progbits,1\n\t" \
626 +                    ".string \"" s "\"\n\t.previous\n")
627 +
628 +RCSID("$Id: version.h 170 2007-03-05 13:58:26Z hartkopp $");
629 +
630 +#define MAJORVERSION 2
631 +#define MINORVERSION 0
632 +#define PATCHLEVEL   0
633 +#define EXTRAVERSION "-pre6"
634 +
635 +#define LLCF_VERSION_CODE (((MAJORVERSION) << 16) + ((MINORVERSION) << 8) \
636 +                               + (PATCHLEVEL))
637 +
638 +/* stringification:  these are the usual macros to stringify with macro
639 +   expansion.   The str() macro does the expansion, the xstr() macro is
640 +   for the actual stringification.
641 +*/
642 +#define str(arg) xstr(arg)
643 +#define xstr(arg) #arg
644 +
645 +#define VERSION str(MAJORVERSION) "." str(MINORVERSION) "." str(PATCHLEVEL) \
646 +       EXTRAVERSION
647 +
648 +#endif /* CAN_VERSION_H */
649 diff -N -u -r b/include/linux/can.h c/include/linux/can.h
650 --- b/include/linux/can.h       1970-01-01 01:00:00.000000000 +0100
651 +++ c/include/linux/can.h       2007-03-05 11:14:57.000000000 +0100
652 @@ -0,0 +1,77 @@
653 +/*
654 + * linux/can.h
655 + *
656 + * Definitions for CAN networklayer (socket addr / CAN frame / CAN filter)
657 + *
658 + * $Id: can.h 165 2007-03-05 10:14:18Z hartkopp $
659 + *
660 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
661 + *          Urs Thuermann   <urs.thuermann@volkswagen.de>
662 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
663 + * All rights reserved.
664 + *
665 + * Send feedback to <socketcan-users@lists.berlios.de>
666 + *
667 + */
668 +
669 +#ifndef CAN_H
670 +#define CAN_H
671 +
672 +#include <linux/version.h>
673 +#include <linux/types.h>
674 +#include <linux/socket.h>
675 +
676 +/* controller area network (CAN) kernel definitions */
677 +
678 +/* special address description flags for the CAN_ID */
679 +#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
680 +#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
681 +#define CAN_ERR_FLAG 0x20000000U /* error frame */
682 +
683 +/* valid bits in CAN ID for frame formats */
684 +#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
685 +#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
686 +#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
687 +
688 +typedef __u32 canid_t;
689 +
690 +struct can_frame {
691 +       canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
692 +       __u8    can_dlc; /* data length code: 0 .. 8 */
693 +       __u8    data[8] __attribute__ ((aligned(8)));
694 +};
695 +
696 +/* particular protocols of the protocol family PF_CAN */
697 +#define CAN_RAW                1 /* RAW sockets */
698 +#define CAN_BCM                2 /* Broadcast Manager */
699 +#define CAN_TP16       3 /* VAG Transport Protocol v1.6 */
700 +#define CAN_TP20       4 /* VAG Transport Protocol v2.0 */
701 +#define CAN_MCNET      5 /* Bosch MCNet */
702 +#define CAN_ISOTP      6 /* ISO 15765-2 Transport Protocol */
703 +#define CAN_BAP                7 /* VAG Bedien- und Anzeigeprotokoll */
704 +#define CAN_NPROTO     8
705 +
706 +#define SOL_CAN_BASE 100
707 +
708 +struct sockaddr_can {
709 +       sa_family_t   can_family;
710 +       int           can_ifindex;
711 +       union {
712 +               struct { canid_t rx_id, tx_id;   } tp16;
713 +               struct { canid_t rx_id, tx_id;   } tp20;
714 +               struct { canid_t rx_id, tx_id;   } mcnet;
715 +               struct { canid_t rx_id, tx_id;   } isotp;
716 +               struct { int     sg_id, sg_type; } bap;
717 +       } can_addr;
718 +};
719 +
720 +typedef canid_t can_err_mask_t;
721 +
722 +struct can_filter {
723 +       canid_t can_id;
724 +       canid_t can_mask;
725 +};
726 +
727 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
728 +
729 +#endif /* CAN_H */
730 diff -N -u -r b/net/can/af_can.c c/net/can/af_can.c
731 --- b/net/can/af_can.c  1970-01-01 01:00:00.000000000 +0100
732 +++ c/net/can/af_can.c  2007-03-09 13:47:12.000000000 +0100
733 @@ -0,0 +1,973 @@
734 +/*
735 + * af_can.c - Protocol family CAN core module
736 + *            (used by different CAN protocol modules)
737 + *
738 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
739 + * All rights reserved.
740 + *
741 + * Redistribution and use in source and binary forms, with or without
742 + * modification, are permitted provided that the following conditions
743 + * are met:
744 + * 1. Redistributions of source code must retain the above copyright
745 + *    notice, this list of conditions, the following disclaimer and
746 + *    the referenced file 'COPYING'.
747 + * 2. Redistributions in binary form must reproduce the above copyright
748 + *    notice, this list of conditions and the following disclaimer in the
749 + *    documentation and/or other materials provided with the distribution.
750 + * 3. Neither the name of Volkswagen nor the names of its contributors
751 + *    may be used to endorse or promote products derived from this software
752 + *    without specific prior written permission.
753 + *
754 + * Alternatively, provided that this notice is retained in full, this
755 + * software may be distributed under the terms of the GNU General
756 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
757 + * file from the main directory of the linux kernel source.
758 + *
759 + * The provided data structures and external interfaces from this code
760 + * are not restricted to be used by modules with a GPL compatible license.
761 + *
762 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
763 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
764 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
765 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
766 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
767 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
768 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
769 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
770 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
771 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
772 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
773 + * DAMAGE.
774 + *
775 + * Send feedback to <socketcan-users@lists.berlios.de>
776 + *
777 + */
778 +
779 +#include <linux/autoconf.h>
780 +#include <linux/module.h>
781 +#include <linux/version.h>
782 +#include <linux/slab.h>
783 +#include <linux/kmod.h>
784 +#include <linux/init.h>
785 +#include <linux/list.h>
786 +#include <linux/spinlock.h>
787 +#include <linux/rcupdate.h>
788 +#include <linux/socket.h>
789 +#include <linux/if_ether.h>
790 +#include <linux/if_arp.h>
791 +#include <linux/skbuff.h>
792 +#include <linux/net.h>
793 +#include <linux/netdevice.h>
794 +#include <net/sock.h>
795 +#include <asm/uaccess.h>
796 +
797 +#include <linux/can.h>
798 +#include <linux/can/core.h>
799 +#include <linux/can/version.h>
800 +
801 +#include "af_can.h"
802 +
803 +
804 +RCSID("$Id: af_can.c 177 2007-03-08 11:02:43Z hartkopp $");
805 +
806 +#define IDENT "af_can"
807 +static __initdata const char banner[] = KERN_INFO "CAN: Controller Area "
808 +                                       "Network PF_CAN core " VERSION "\n"; 
809 +
810 +MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
811 +MODULE_LICENSE("Dual BSD/GPL");
812 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
813 +             "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
814 +
815 +int stats_timer = 1; /* default: on */
816 +module_param(stats_timer, int, S_IRUGO);
817 +
818 +#ifdef CONFIG_CAN_DEBUG_CORE
819 +static int debug = 0;
820 +module_param(debug, int, S_IRUGO);
821 +#define DBG(args...)       (debug & 1 ? \
822 +                              (printk(KERN_DEBUG "CAN %s: ", __func__), \
823 +                               printk(args)) : 0)
824 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
825 +#define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
826 +#else
827 +#define DBG(args...)
828 +#define DBG_FRAME(args...)
829 +#define DBG_SKB(skb)
830 +#endif
831 +
832 +static __init int  can_init(void);
833 +static __exit void can_exit(void);
834 +
835 +static int can_create(struct socket *sock, int protocol);
836 +static int can_notifier(struct notifier_block *nb,
837 +                       unsigned long msg, void *data);
838 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
839 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
840 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
841 +                  struct packet_type *pt, struct net_device *orig_dev);
842 +#else
843 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
844 +                  struct packet_type *pt);
845 +#endif
846 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb);
847 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev);
848 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
849 +                                       struct dev_rcv_lists *d);
850 +static void can_rcv_lists_delete(struct rcu_head *rp);
851 +static void can_rx_delete(struct rcu_head *rp);
852 +static void can_rx_delete_all(struct hlist_head *rl);
853 +
854 +
855 +struct notifier {
856 +       struct list_head list;
857 +       struct net_device *dev;
858 +       void (*func)(unsigned long msg, void *data);
859 +       void *data;
860 +};
861 +
862 +static LIST_HEAD(notifier_list);
863 +static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
864 +
865 +HLIST_HEAD(rx_dev_list);
866 +static struct dev_rcv_lists rx_alldev_list;
867 +static spinlock_t rcv_lists_lock = SPIN_LOCK_UNLOCKED;
868 +
869 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
870 +static struct kmem_cache *rcv_cache __read_mostly;
871 +#else
872 +static kmem_cache_t *rcv_cache;
873 +#endif
874 +
875 +static struct packet_type can_packet = {
876 +       .type = __constant_htons(ETH_P_CAN),
877 +       .dev  = NULL,
878 +       .func = can_rcv,
879 +};
880 +
881 +static struct net_proto_family can_family_ops = {
882 +       .family = PF_CAN,
883 +       .create = can_create,
884 +       .owner  = THIS_MODULE,
885 +};
886 +
887 +/* notifier block for netdevice event */
888 +static struct notifier_block can_netdev_notifier = {
889 +       .notifier_call = can_notifier,
890 +};
891 +
892 +/* table of registered CAN protocols */
893 +static struct can_proto *proto_tab[CAN_NPROTO];
894 +
895 +extern struct timer_list stattimer; /* timer for statistics update */
896 +extern struct s_stats  stats;       /* packet statistics */
897 +extern struct s_pstats pstats;      /* receive list statistics */
898 +
899 +module_init(can_init);
900 +module_exit(can_exit);
901 +
902 +/**************************************************/
903 +/* af_can module init/exit functions              */
904 +/**************************************************/
905 +
906 +static __init int can_init(void)
907 +{
908 +       printk(banner);
909 +
910 +       rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
911 +                                     0, 0, NULL, NULL);
912 +       if (!rcv_cache)
913 +               return -ENOMEM;
914 +
915 +       /* Insert struct dev_rcv_lists for reception on all devices.
916 +          This struct is zero initialized which is correct for the 
917 +          embedded hlist heads, the dev pointer, and the entries counter.
918 +       */
919 +
920 +       spin_lock_bh(&rcv_lists_lock);
921 +       hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
922 +       spin_unlock_bh(&rcv_lists_lock);
923 +
924 +       if (stats_timer) {
925 +               /* statistics init */
926 +               init_timer(&stattimer);
927 +       }
928 +
929 +       /* procfs init */
930 +       can_init_proc();
931 +
932 +       /* protocol register */
933 +       sock_register(&can_family_ops);
934 +       register_netdevice_notifier(&can_netdev_notifier);
935 +       dev_add_pack(&can_packet);
936 +
937 +       return 0;
938 +}
939 +
940 +static __exit void can_exit(void)
941 +{
942 +       struct dev_rcv_lists *d;
943 +       struct hlist_node *n, *next;
944 +
945 +       if (stats_timer) {
946 +               /* stop statistics timer */
947 +               del_timer(&stattimer);
948 +       }
949 +
950 +       /* procfs remove */
951 +       can_remove_proc();
952 +
953 +       /* protocol unregister */
954 +       dev_remove_pack(&can_packet);
955 +       unregister_netdevice_notifier(&can_netdev_notifier);
956 +       sock_unregister(PF_CAN);
957 +
958 +       /* remove rx_dev_list */
959 +       spin_lock_bh(&rcv_lists_lock);
960 +       hlist_del(&rx_alldev_list.list);
961 +       hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
962 +               hlist_del(&d->list);
963 +               kfree(d);
964 +       }
965 +       spin_unlock_bh(&rcv_lists_lock);
966 +
967 +       kmem_cache_destroy(rcv_cache);
968 +}
969 +
970 +/**************************************************/
971 +/* af_can protocol functions                      */
972 +/**************************************************/
973 +
974 +void can_proto_register(struct can_proto *cp)
975 +{
976 +       int proto = cp->protocol;
977 +       if (proto < 0 || proto >= CAN_NPROTO) {
978 +               printk(KERN_ERR "CAN: protocol number %d out "
979 +                      "of range\n", proto);
980 +               return;
981 +       }
982 +       if (proto_tab[proto]) {
983 +               printk(KERN_ERR "CAN: protocol %d already "
984 +                      "registered\n", proto);
985 +               return;
986 +       }
987 +
988 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
989 +       if (proto_register(cp->prot, 0) != 0) {
990 +               return;
991 +       }
992 +#endif
993 +       proto_tab[proto] = cp;
994 +
995 +       /* use generic ioctl function if the module doesn't bring its own */
996 +       if (!cp->ops->ioctl)
997 +               cp->ops->ioctl = can_ioctl;
998 +}
999 +
1000 +void can_proto_unregister(struct can_proto *cp)
1001 +{
1002 +       int proto = cp->protocol;
1003 +       if (!proto_tab[proto]) {
1004 +               printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
1005 +               return;
1006 +       }
1007 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1008 +       proto_unregister(cp->prot);
1009 +#endif
1010 +       proto_tab[proto] = NULL;
1011 +}
1012 +
1013 +void can_dev_register(struct net_device *dev,
1014 +                     void (*func)(unsigned long msg, void *), void *data)
1015 +{
1016 +       struct notifier *n;
1017 +
1018 +       DBG("called for %s\n", dev->name);
1019 +
1020 +       if (!(n = kmalloc(sizeof(*n), GFP_KERNEL)))
1021 +               return;
1022 +
1023 +       n->dev  = dev;
1024 +       n->func = func;
1025 +       n->data = data;
1026 +
1027 +       write_lock(&notifier_lock);
1028 +       list_add(&n->list, &notifier_list);
1029 +       write_unlock(&notifier_lock);
1030 +}
1031 +
1032 +void can_dev_unregister(struct net_device *dev,
1033 +                       void (*func)(unsigned long msg, void *), void *data)
1034 +{
1035 +       struct notifier *n, *next;
1036 +
1037 +       DBG("called for %s\n", dev->name);
1038 +
1039 +       write_lock(&notifier_lock);
1040 +       list_for_each_entry_safe(n, next, &notifier_list, list) {
1041 +               if (n->dev == dev && n->func == func && n->data == data) {
1042 +                       list_del(&n->list);
1043 +                       kfree(n);
1044 +                       break;
1045 +               }
1046 +       }
1047 +       write_unlock(&notifier_lock);
1048 +}
1049 +
1050 +/**************************************************/
1051 +/* af_can socket functions                        */
1052 +/**************************************************/
1053 +
1054 +static void can_sock_destruct(struct sock *sk)
1055 +{
1056 +       DBG("called for sock %p\n", sk);
1057 +
1058 +       skb_queue_purge(&sk->sk_receive_queue);
1059 +       if (sk->sk_protinfo)
1060 +               kfree(sk->sk_protinfo);
1061 +}
1062 +
1063 +static int can_create(struct socket *sock, int protocol)
1064 +{
1065 +       struct sock *sk;
1066 +       struct can_proto *cp;
1067 +       int ret;
1068 +
1069 +       DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
1070 +
1071 +       sock->state = SS_UNCONNECTED;
1072 +
1073 +       if (protocol < 0 || protocol >= CAN_NPROTO)
1074 +               return -EINVAL;
1075 +
1076 +       DBG("looking up proto %d in proto_tab[]\n", protocol);
1077 +
1078 +       /* try to load protocol module, when CONFIG_KMOD is defined */
1079 +       if (!proto_tab[protocol]) {
1080 +               char module_name[30];
1081 +               sprintf(module_name, "can-proto-%d", protocol);
1082 +               if (request_module(module_name) == -ENOSYS)
1083 +                       printk(KERN_INFO "CAN: request_module(%s) not"
1084 +                              " implemented.\n", module_name);
1085 +       }
1086 +
1087 +       /* check for success and correct type */
1088 +       if (!(cp = proto_tab[protocol]) || cp->type != sock->type)
1089 +               return -EPROTONOSUPPORT;
1090 +
1091 +       if (cp->capability >= 0 && !capable(cp->capability))
1092 +               return -EPERM;
1093 +
1094 +       sock->ops = cp->ops;
1095 +
1096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1097 +       sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
1098 +       if (!sk)
1099 +               goto oom;
1100 +#else
1101 +       sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
1102 +       if (!sk)
1103 +               goto oom;
1104 +       if (cp->obj_size &&
1105 +           !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
1106 +               sk_free(sk);
1107 +               goto oom;
1108 +       }
1109 +       sk_set_owner(sk, proto_tab[protocol]->owner);
1110 +#endif
1111 +       sock_init_data(sock, sk);
1112 +       sk->sk_destruct = can_sock_destruct;
1113 +
1114 +       DBG("created sock: %p\n", sk);
1115 +
1116 +       ret = 0;
1117 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1118 +       if (sk->sk_prot->init)
1119 +               ret = sk->sk_prot->init(sk);
1120 +#else
1121 +       if (cp->init)
1122 +               ret = cp->init(sk);
1123 +#endif
1124 +       if (ret) {
1125 +               /* we must release sk */
1126 +               sock_orphan(sk);
1127 +               sock_put(sk);
1128 +               return ret;
1129 +       }
1130 +
1131 +       return 0;
1132 +
1133 + oom:
1134 +       return -ENOMEM;
1135 +}
1136 +
1137 +static int can_notifier(struct notifier_block *nb,
1138 +                       unsigned long msg, void *data)
1139 +{
1140 +       struct net_device *dev = (struct net_device *)data;
1141 +       struct notifier *n;
1142 +
1143 +       DBG("called for %s, msg = %lu\n", dev->name, msg);
1144 +
1145 +       if (dev->type != ARPHRD_CAN)
1146 +               return NOTIFY_DONE;
1147 +
1148 +       switch (msg) {
1149 +               struct dev_rcv_lists *d;
1150 +               int i;
1151 +
1152 +       case NETDEV_REGISTER:
1153 +
1154 +               /* create new dev_rcv_lists for this device */
1155 +
1156 +               DBG("creating new dev_rcv_lists for %s\n", dev->name);
1157 +               if (!(d = kmalloc(sizeof(*d),
1158 +                                 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL))) {
1159 +                       printk(KERN_ERR "CAN: allocation of receive "
1160 +                              "list failed\n");
1161 +                       return NOTIFY_DONE;
1162 +               }
1163 +               /* N.B. zeroing the struct is the correct initialization
1164 +                       for the embedded hlist_head structs.
1165 +                       Another list type, e.g. list_head, would require
1166 +                       explicit initialization. */
1167 +               memset(d, 0, sizeof(*d));
1168 +               d->dev = dev;
1169 +
1170 +               spin_lock_bh(&rcv_lists_lock);
1171 +               hlist_add_head_rcu(&d->list, &rx_dev_list);
1172 +               spin_unlock_bh(&rcv_lists_lock);
1173 +
1174 +               break;
1175 +
1176 +       case NETDEV_UNREGISTER:
1177 +               spin_lock_bh(&rcv_lists_lock);
1178 +
1179 +               if (!(d = find_dev_rcv_lists(dev))) {
1180 +                       printk(KERN_ERR "CAN: notifier: receive list not "
1181 +                              "found for dev %s\n", dev->name);
1182 +                       goto unreg_out;
1183 +               }
1184 +
1185 +               hlist_del_rcu(&d->list);
1186 +
1187 +               /* remove all receivers hooked at this netdevice */
1188 +               can_rx_delete_all(&d->rx_err);
1189 +               can_rx_delete_all(&d->rx_all);
1190 +               can_rx_delete_all(&d->rx_fil);
1191 +               can_rx_delete_all(&d->rx_inv);
1192 +               can_rx_delete_all(&d->rx_eff);
1193 +               for (i = 0; i < 2048; i++)
1194 +                       can_rx_delete_all(&d->rx_sff[i]);
1195 +
1196 +       unreg_out:
1197 +               spin_unlock_bh(&rcv_lists_lock);
1198 +
1199 +               if (d)
1200 +                       call_rcu(&d->rcu, can_rcv_lists_delete);
1201 +
1202 +               break;
1203 +       }
1204 +
1205 +       read_lock(&notifier_lock);
1206 +       list_for_each_entry(n, &notifier_list, list) {
1207 +               if (n->dev == dev)
1208 +                       n->func(msg, n->data);
1209 +       }
1210 +       read_unlock(&notifier_lock);
1211 +
1212 +       return NOTIFY_DONE;
1213 +}
1214 +
1215 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1216 +{
1217 +       struct sock *sk = sock->sk;
1218 +
1219 +       switch (cmd) {
1220 +       case SIOCGSTAMP:
1221 +               return sock_get_timestamp(sk, (struct timeval __user *)arg);
1222 +       default:
1223 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1224 +               return -ENOIOCTLCMD;
1225 +#else
1226 +               return dev_ioctl(cmd, (void __user *)arg);
1227 +#endif
1228 +       }
1229 +       return 0;
1230 +}
1231 +
1232 +/**************************************************/
1233 +/* af_can tx path                                 */
1234 +/**************************************************/
1235 +
1236 +int can_send(struct sk_buff *skb, int loop)
1237 +{
1238 +       int err;
1239 +
1240 +       if (loop) { /* local loopback (default) */
1241 +               *(struct sock **)skb->cb = skb->sk; /* tx sock reference */
1242 +
1243 +               /* interface not capabable to do the loopback itself? */
1244 +               if (!(skb->dev->flags & IFF_LOOPBACK)) {
1245 +                       struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
1246 +                       newskb->protocol  = htons(ETH_P_CAN);
1247 +                       newskb->ip_summed = CHECKSUM_UNNECESSARY;
1248 +                       netif_rx(newskb); /* perform local loopback here */
1249 +               }
1250 +       } else
1251 +               *(struct sock **)skb->cb = NULL; /* no loopback required */
1252 +
1253 +       if (!(skb->dev->flags & IFF_UP))
1254 +               err = -ENETDOWN;
1255 +       else if ((err = dev_queue_xmit(skb)) > 0)  /* send to netdevice */
1256 +               err = net_xmit_errno(err);
1257 +
1258 +       /* update statistics */
1259 +       stats.tx_frames++;
1260 +       stats.tx_frames_delta++;
1261 +
1262 +       return err;
1263 +}
1264 +
1265 +/**************************************************/
1266 +/* af_can rx path                                 */
1267 +/**************************************************/
1268 +
1269 +int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
1270 +                   void (*func)(struct sk_buff *, void *), void *data,
1271 +                   char *ident)
1272 +{
1273 +       struct receiver *r;
1274 +       struct hlist_head *rl;
1275 +       struct dev_rcv_lists *d;
1276 +       int ret = 0;
1277 +
1278 +       /* insert new receiver  (dev,canid,mask) -> (func,data) */
1279 +
1280 +       DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
1281 +           dev, can_id, mask, func, data, ident);
1282 +
1283 +       if (!(r = kmem_cache_alloc(rcv_cache, GFP_KERNEL))) {
1284 +               ret = -ENOMEM;
1285 +               goto out;
1286 +       }
1287 +
1288 +       spin_lock_bh(&rcv_lists_lock);
1289 +
1290 +       if (!(d = find_dev_rcv_lists(dev))) {
1291 +               DBG("receive list not found for dev %s, id %03X, mask %03X\n",
1292 +                   DNAME(dev), can_id, mask);
1293 +               kmem_cache_free(rcv_cache, r);
1294 +               ret = -ENODEV;
1295 +               goto out_unlock;
1296 +       }
1297 +
1298 +       rl = find_rcv_list(&can_id, &mask, d);
1299 +
1300 +       r->can_id  = can_id;
1301 +       r->mask    = mask;
1302 +       r->matches = 0;
1303 +       r->func    = func;
1304 +       r->data    = data;
1305 +       r->ident   = ident;
1306 +
1307 +       hlist_add_head_rcu(&r->list, rl);
1308 +       d->entries++;
1309 +
1310 +       pstats.rcv_entries++;
1311 +       if (pstats.rcv_entries_max < pstats.rcv_entries)
1312 +               pstats.rcv_entries_max = pstats.rcv_entries;
1313 +
1314 + out_unlock:
1315 +       spin_unlock_bh(&rcv_lists_lock);
1316 + out:
1317 +       return ret;
1318 +}
1319 +
1320 +static void can_rcv_lists_delete(struct rcu_head *rp)
1321 +{
1322 +       struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
1323 +       kfree(d);
1324 +}
1325 +
1326 +static void can_rx_delete(struct rcu_head *rp)
1327 +{
1328 +       struct receiver *r = container_of(rp, struct receiver, rcu);
1329 +       kmem_cache_free(rcv_cache, r);
1330 +}
1331 +
1332 +static void can_rx_delete_all(struct hlist_head *rl)
1333 +{
1334 +       struct receiver *r;
1335 +       struct hlist_node *n;
1336 +
1337 +       hlist_for_each_entry_rcu(r, n, rl, list) {
1338 +               hlist_del_rcu(&r->list);
1339 +               call_rcu(&r->rcu, can_rx_delete);
1340 +       }
1341 +}
1342 +
1343 +int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
1344 +                     void (*func)(struct sk_buff *, void *), void *data)
1345 +{
1346 +       struct receiver *r;
1347 +       struct hlist_head *rl;
1348 +       struct hlist_node *next;
1349 +       struct dev_rcv_lists *d;
1350 +       int ret = 0;
1351 +
1352 +       DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
1353 +           dev, can_id, mask, func, data);
1354 +
1355 +       r = NULL;
1356 +
1357 +       spin_lock_bh(&rcv_lists_lock);
1358 +
1359 +       if (!(d = find_dev_rcv_lists(dev))) {
1360 +               DBG("receive list not found for dev %s, id %03X, mask %03X\n",
1361 +                   DNAME(dev), can_id, mask);
1362 +               ret = -ENODEV;
1363 +               goto out;
1364 +       }
1365 +
1366 +       rl = find_rcv_list(&can_id, &mask, d);
1367 +
1368 +       /*  Search the receiver list for the item to delete.  This should
1369 +        *  exist, since no receiver may be unregistered that hasn't
1370 +        *  been registered before.
1371 +        */
1372 +
1373 +       hlist_for_each_entry(r, next, rl, list) {
1374 +               if (r->can_id == can_id && r->mask == mask
1375 +                   && r->func == func && r->data == data)
1376 +                       break;
1377 +       }
1378 +
1379 +       /*  Check for bug in CAN protocol implementations:
1380 +        *  If no matching list item was found, the list cursor variable next
1381 +        *  will be NULL, while r will point to the last item of the list.
1382 +        */
1383 +
1384 +       if (!next) {
1385 +               DBG("receive list entry not found for "
1386 +                   "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
1387 +               ret = -EINVAL;
1388 +               r = NULL;
1389 +               goto out;
1390 +       }
1391 +
1392 +       hlist_del_rcu(&r->list);
1393 +       d->entries--;
1394 +
1395 +       if (pstats.rcv_entries > 0)
1396 +               pstats.rcv_entries--;
1397 +
1398 + out:
1399 +       spin_unlock_bh(&rcv_lists_lock);
1400 +
1401 +       /* schedule the receiver item for deletion */
1402 +       if (r)
1403 +               call_rcu(&r->rcu, can_rx_delete);
1404 +
1405 +       return ret;
1406 +}
1407 +
1408 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
1409 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
1410 +                  struct packet_type *pt, struct net_device *orig_dev)
1411 +#else
1412 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
1413 +                  struct packet_type *pt)
1414 +#endif
1415 +{
1416 +       struct dev_rcv_lists *d;
1417 +       int matches;
1418 +
1419 +       DBG("received skbuff on device %s, ptype %04x\n",
1420 +           dev->name, ntohs(pt->type));
1421 +       DBG_SKB(skb);
1422 +       DBG_FRAME("af_can: can_rcv: received CAN frame",
1423 +                 (struct can_frame *)skb->data);
1424 +
1425 +       /* update statistics */
1426 +       stats.rx_frames++;
1427 +       stats.rx_frames_delta++;
1428 +
1429 +       rcu_read_lock();
1430 +
1431 +       /* deliver the packet to sockets listening on all devices */
1432 +       matches = can_rcv_filter(&rx_alldev_list, skb);
1433 +
1434 +       /* find receive list for this device */
1435 +       if ((d = find_dev_rcv_lists(dev)))
1436 +               matches += can_rcv_filter(d, skb);
1437 +
1438 +       rcu_read_unlock();
1439 +
1440 +       /* free the skbuff allocated by the netdevice driver */
1441 +       DBG("freeing skbuff %p\n", skb);
1442 +       kfree_skb(skb);
1443 +
1444 +       if (matches > 0) {
1445 +               stats.matches++;
1446 +               stats.matches_delta++;
1447 +       }
1448 +
1449 +       return 0;
1450 +}
1451 +
1452 +
1453 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
1454 +{
1455 +       struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
1456 +       DBG("skbuff %p cloned to %p\n", skb, clone);
1457 +       if (clone) {
1458 +               r->func(clone, r->data);
1459 +               r->matches++;    /* update specific statistics */
1460 +       }
1461 +}
1462 +
1463 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
1464 +{
1465 +       struct receiver *r;
1466 +       struct hlist_node *n;
1467 +       int matches = 0;
1468 +       struct can_frame *cf = (struct can_frame*)skb->data;
1469 +       canid_t can_id = cf->can_id;
1470 +
1471 +       if (d->entries == 0)
1472 +               return 0;
1473 +
1474 +       if (can_id & CAN_ERR_FLAG) {
1475 +               /* check for error frame entries only */
1476 +               hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
1477 +                       if (can_id & r->mask) {
1478 +                               DBG("match on rx_err skbuff %p\n", skb);
1479 +                               deliver(skb, r);
1480 +                               matches++;
1481 +                       }
1482 +               }
1483 +               goto out;
1484 +       }
1485 +
1486 +       /* check for unfiltered entries */
1487 +       hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
1488 +               DBG("match on rx_all skbuff %p\n", skb);
1489 +               deliver(skb, r);
1490 +               matches++;
1491 +       }
1492 +
1493 +       /* check for can_id/mask entries */
1494 +       hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
1495 +               if ((can_id & r->mask) == r->can_id) {
1496 +                       DBG("match on rx_fil skbuff %p\n", skb);
1497 +                       deliver(skb, r);
1498 +                       matches++;
1499 +               }
1500 +       }
1501 +
1502 +       /* check for inverted can_id/mask entries */
1503 +       hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
1504 +               if ((can_id & r->mask) != r->can_id) {
1505 +                       DBG("match on rx_inv skbuff %p\n", skb);
1506 +                       deliver(skb, r);
1507 +                       matches++;
1508 +               }
1509 +       }
1510 +
1511 +       /* check CAN_ID specific entries */
1512 +       if (can_id & CAN_EFF_FLAG) {
1513 +               hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
1514 +                       if (r->can_id == can_id) {
1515 +                               DBG("match on rx_eff skbuff %p\n", skb);
1516 +                               deliver(skb, r);
1517 +                               matches++;
1518 +                       }
1519 +               }
1520 +       } else {
1521 +               can_id &= CAN_SFF_MASK;
1522 +               hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
1523 +                       DBG("match on rx_sff skbuff %p\n", skb);
1524 +                       deliver(skb, r);
1525 +                       matches++;
1526 +               }
1527 +       }
1528 +
1529 + out:
1530 +       return matches;
1531 +}
1532 +
1533 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
1534 +{
1535 +       struct dev_rcv_lists *d;
1536 +       struct hlist_node *n;
1537 +
1538 +       /* find receive list for this device */
1539 +
1540 +       /*  The hlist_for_each_entry*() macros curse through the list
1541 +        *  using the pointer variable n and set d to the containing
1542 +        *  struct in each list iteration.  Therefore, after list
1543 +        *  iteration, d is unmodified when the list is empty, and it
1544 +        *  points to last list element, when the list is non-empty
1545 +        *  but no match in the loop body is found.  I.e. d is *not*
1546 +        *  NULL when no match is found.  We can, however, use the
1547 +        *  cursor variable n to decide if a match was found.
1548 +        */
1549 +
1550 +       hlist_for_each_entry(d, n, &rx_dev_list, list)
1551 +               if (d->dev == dev)
1552 +                       break;
1553 +
1554 +       return n ? d : NULL;
1555 +}
1556 +
1557 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
1558 +                                       struct dev_rcv_lists *d)
1559 +{
1560 +       canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
1561 +
1562 +       if (*mask & CAN_ERR_FLAG) { /* filter error frames */
1563 +               *mask &= CAN_ERR_MASK; /* clear CAN_ERR_FLAG in list entry */
1564 +               return &d->rx_err;
1565 +       }
1566 +
1567 +       /* ensure valid values in can_mask */
1568 +       if (*mask & CAN_EFF_FLAG)
1569 +               *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
1570 +       else
1571 +               *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
1572 +
1573 +       *can_id &= *mask; /* reduce condition testing at receive time */
1574 +
1575 +       if (inv) /* inverse can_id/can_mask filter */
1576 +               return &d->rx_inv;
1577 +
1578 +       if (!(*mask)) /* mask == 0 => no condition testing at receive time */
1579 +               return &d->rx_all;
1580 +
1581 +       /* use extra filterset for the subscription of exactly *one* can_id */
1582 +       if (*can_id & CAN_EFF_FLAG) {
1583 +               if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG))
1584 +                       return &d->rx_eff; /* use-case for hash-table here? */
1585 +       } else {
1586 +               if (*mask == CAN_SFF_MASK)
1587 +                       return &d->rx_sff[*can_id];
1588 +       }
1589 +
1590 +       return &d->rx_fil;  /* default: filter via can_id/can_mask */
1591 +}
1592 +
1593 +/**************************************************/
1594 +/* af_can utility stuff                           */
1595 +/**************************************************/
1596 +
1597 +unsigned long timeval2jiffies(struct timeval *tv, int round_up)
1598 +{
1599 +       unsigned long jif;
1600 +       unsigned long sec  = tv->tv_sec;
1601 +       unsigned long usec = tv->tv_usec;
1602 +
1603 +       if (sec > ULONG_MAX / HZ)          /* check for overflow */
1604 +               return ULONG_MAX;
1605 +
1606 +       if (round_up)                      /* any usec below one HZ? */
1607 +               usec += 1000000 / HZ - 1;  /* pump it up */
1608 +
1609 +       jif = usec / (1000000 / HZ);
1610 +
1611 +       if (sec * HZ > ULONG_MAX - jif)    /* check for overflow */
1612 +               return ULONG_MAX;
1613 +       else
1614 +               return jif + sec * HZ;
1615 +}
1616 +
1617 +
1618 +/**************************************************/
1619 +/* af_can debugging stuff                         */
1620 +/**************************************************/
1621 +
1622 +#ifdef CONFIG_CAN_DEBUG_CORE
1623 +
1624 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
1625 +{
1626 +       va_list ap;
1627 +       int len;
1628 +       int dlc, i;
1629 +       char buf[1024];
1630 +
1631 +       len = sprintf(buf, KERN_DEBUG);
1632 +       va_start(ap, cf);
1633 +       len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
1634 +       buf[len++] = ':';
1635 +       buf[len++] = ' ';
1636 +       va_end(ap);
1637 +
1638 +       if ((dlc = cf->can_dlc) > 8)
1639 +               dlc = 8;
1640 +
1641 +       if (cf->can_id & CAN_EFF_FLAG)
1642 +               len += sprintf(buf + len, "<%08X> [%X] ",
1643 +                              cf->can_id & CAN_EFF_MASK, dlc);
1644 +       else
1645 +               len += sprintf(buf + len, "<%03X> [%X] ",
1646 +                              cf->can_id & CAN_SFF_MASK, dlc);
1647 +
1648 +       for (i = 0; i < dlc; i++)
1649 +               len += sprintf(buf + len, "%02X ", cf->data[i]);
1650 +
1651 +       if (cf->can_id & CAN_RTR_FLAG)
1652 +               len += sprintf(buf + len, "(RTR)");
1653 +
1654 +       buf[len++] = '\n';
1655 +       buf[len]   = '\0';
1656 +       printk(buf);
1657 +}
1658 +
1659 +void can_debug_skb(struct sk_buff *skb)
1660 +{
1661 +       int len, nbytes, i;
1662 +       char buf[1024];
1663 +
1664 +       len = sprintf(buf,
1665 +                     KERN_DEBUG "  skbuff at %p, dev: %d, proto: %04x\n"
1666 +                     KERN_DEBUG "  users: %d, dataref: %d, nr_frags: %d, "
1667 +                     "h,d,t,e,l: %p %+d %+d %+d, %d",
1668 +                     skb, skb->dev ? skb->dev->ifindex : -1,
1669 +                     ntohs(skb->protocol),
1670 +                     atomic_read(&skb->users),
1671 +                     atomic_read(&(skb_shinfo(skb)->dataref)),
1672 +                     skb_shinfo(skb)->nr_frags,
1673 +                     skb->head, skb->data - skb->head,
1674 +                     skb->tail - skb->head, skb->end - skb->head, skb->len);
1675 +       nbytes = skb->end - skb->head;
1676 +       for (i = 0; i < nbytes; i++) {
1677 +               if (i % 16 == 0)
1678 +                       len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
1679 +               if (len < sizeof(buf) - 16) {
1680 +                       len += sprintf(buf + len, " %02x", skb->head[i]);
1681 +               } else {
1682 +                       len += sprintf(buf + len, "...");
1683 +                       break;
1684 +               }
1685 +       }
1686 +       buf[len++] = '\n';
1687 +       buf[len]   = '\0';
1688 +       printk(buf);
1689 +}
1690 +
1691 +EXPORT_SYMBOL(can_debug_cframe);
1692 +EXPORT_SYMBOL(can_debug_skb);
1693 +
1694 +#endif
1695 +
1696 +/**************************************************/
1697 +/* Exported symbols                               */
1698 +/**************************************************/
1699 +EXPORT_SYMBOL(can_proto_register);
1700 +EXPORT_SYMBOL(can_proto_unregister);
1701 +EXPORT_SYMBOL(can_rx_register);
1702 +EXPORT_SYMBOL(can_rx_unregister);
1703 +EXPORT_SYMBOL(can_dev_register);
1704 +EXPORT_SYMBOL(can_dev_unregister);
1705 +EXPORT_SYMBOL(can_send);
1706 +EXPORT_SYMBOL(timeval2jiffies);
1707 diff -N -u -r b/net/can/af_can.h c/net/can/af_can.h
1708 --- b/net/can/af_can.h  1970-01-01 01:00:00.000000000 +0100
1709 +++ c/net/can/af_can.h  2007-03-09 13:47:12.000000000 +0100
1710 @@ -0,0 +1,119 @@
1711 +/*
1712 + * $Id: af_can.h 177 2007-03-08 11:02:43Z hartkopp $
1713 + *
1714 + * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
1715 + * All rights reserved.
1716 + *
1717 + * Redistribution and use in source and binary forms, with or without
1718 + * modification, are permitted provided that the following conditions
1719 + * are met:
1720 + * 1. Redistributions of source code must retain the above copyright
1721 + *    notice, this list of conditions, the following disclaimer and
1722 + *    the referenced file 'COPYING'.
1723 + * 2. Redistributions in binary form must reproduce the above copyright
1724 + *    notice, this list of conditions and the following disclaimer in the
1725 + *    documentation and/or other materials provided with the distribution.
1726 + * 3. Neither the name of Volkswagen nor the names of its contributors
1727 + *    may be used to endorse or promote products derived from this software
1728 + *    without specific prior written permission.
1729 + *
1730 + * Alternatively, provided that this notice is retained in full, this
1731 + * software may be distributed under the terms of the GNU General
1732 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1733 + * file from the main directory of the linux kernel source.
1734 + *
1735 + * The provided data structures and external interfaces from this code
1736 + * are not restricted to be used by modules with a GPL compatible license.
1737 + *
1738 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1739 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1740 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1741 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1742 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1743 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1744 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1745 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1746 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1747 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1748 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1749 + * DAMAGE.
1750 + *
1751 + * Send feedback to <socketcan-users@lists.berlios.de>
1752 + *
1753 + */
1754 +
1755 +#ifndef AF_CAN_H
1756 +#define AF_CAN_H
1757 +
1758 +#include <linux/skbuff.h>
1759 +#include <linux/netdevice.h>
1760 +#include <linux/list.h>
1761 +#include <linux/rcupdate.h>
1762 +
1763 +#include <linux/can.h>
1764 +
1765 +/* af_can rx dispatcher structures */
1766 +
1767 +struct receiver {
1768 +       struct hlist_node list;
1769 +       struct rcu_head rcu;
1770 +       canid_t can_id;
1771 +       canid_t mask;
1772 +       unsigned long matches;
1773 +       void (*func)(struct sk_buff *, void *);
1774 +       void *data;
1775 +       char *ident;
1776 +};
1777 +
1778 +struct dev_rcv_lists {
1779 +       struct hlist_node list;
1780 +       struct rcu_head rcu;
1781 +       struct net_device *dev;
1782 +       struct hlist_head rx_err;
1783 +       struct hlist_head rx_all;
1784 +       struct hlist_head rx_fil;
1785 +       struct hlist_head rx_inv;
1786 +       struct hlist_head rx_sff[0x800];
1787 +       struct hlist_head rx_eff;
1788 +       int entries;
1789 +};
1790 +
1791 +/* statistic structures */
1792 +
1793 +struct s_stats {
1794 +       unsigned long jiffies_init;
1795 +
1796 +       unsigned long rx_frames;
1797 +       unsigned long tx_frames;
1798 +       unsigned long matches;
1799 +
1800 +       unsigned long total_rx_rate;
1801 +       unsigned long total_tx_rate;
1802 +       unsigned long total_rx_match_ratio;
1803 +
1804 +       unsigned long current_rx_rate;
1805 +       unsigned long current_tx_rate;
1806 +       unsigned long current_rx_match_ratio;
1807 +
1808 +       unsigned long max_rx_rate;
1809 +       unsigned long max_tx_rate;
1810 +       unsigned long max_rx_match_ratio;
1811 +
1812 +       unsigned long rx_frames_delta;
1813 +       unsigned long tx_frames_delta;
1814 +       unsigned long matches_delta;
1815 +}; /* can be reset e.g. by can_init_stats() */
1816 +
1817 +struct s_pstats {
1818 +       unsigned long stats_reset;
1819 +       unsigned long rcv_entries;
1820 +       unsigned long rcv_entries_max;
1821 +}; /* persistent statistics */
1822 +
1823 +
1824 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1825 +
1826 +void can_init_proc(void);
1827 +void can_remove_proc(void);
1828 +
1829 +#endif /* AF_CAN_H */
1830 diff -N -u -r b/net/can/bcm.c c/net/can/bcm.c
1831 --- b/net/can/bcm.c     1970-01-01 01:00:00.000000000 +0100
1832 +++ c/net/can/bcm.c     2007-03-09 13:47:12.000000000 +0100
1833 @@ -0,0 +1,1614 @@
1834 +/*
1835 + * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
1836 + *
1837 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1838 + * All rights reserved.
1839 + *
1840 + * Redistribution and use in source and binary forms, with or without
1841 + * modification, are permitted provided that the following conditions
1842 + * are met:
1843 + * 1. Redistributions of source code must retain the above copyright
1844 + *    notice, this list of conditions, the following disclaimer and
1845 + *    the referenced file 'COPYING'.
1846 + * 2. Redistributions in binary form must reproduce the above copyright
1847 + *    notice, this list of conditions and the following disclaimer in the
1848 + *    documentation and/or other materials provided with the distribution.
1849 + * 3. Neither the name of Volkswagen nor the names of its contributors
1850 + *    may be used to endorse or promote products derived from this software
1851 + *    without specific prior written permission.
1852 + *
1853 + * Alternatively, provided that this notice is retained in full, this
1854 + * software may be distributed under the terms of the GNU General
1855 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
1856 + * file from the main directory of the linux kernel source.
1857 + *
1858 + * The provided data structures and external interfaces from this code
1859 + * are not restricted to be used by modules with a GPL compatible license.
1860 + *
1861 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1862 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1863 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1864 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1865 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1866 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1867 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1868 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1869 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1870 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1871 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1872 + * DAMAGE.
1873 + *
1874 + * Send feedback to <socketcan-users@lists.berlios.de>
1875 + *
1876 + */
1877 +
1878 +#include <linux/autoconf.h>
1879 +#include <linux/module.h>
1880 +#include <linux/init.h>
1881 +#include <linux/net.h>
1882 +#include <linux/netdevice.h>
1883 +#include <linux/proc_fs.h>
1884 +#include <linux/poll.h>
1885 +#include <net/sock.h>
1886 +
1887 +#include <linux/can.h>
1888 +#include <linux/can/core.h>
1889 +#include <linux/can/bcm.h>
1890 +#include <linux/can/version.h>
1891 +
1892 +RCSID("$Id: bcm.c 179 2007-03-09 07:36:49Z hartkopp $");
1893 +
1894 +#ifdef CONFIG_CAN_DEBUG_CORE
1895 +static int debug = 0;
1896 +module_param(debug, int, S_IRUGO);
1897 +#define DBG(args...)       (debug & 1 ? \
1898 +                              (printk(KERN_DEBUG "BCM %s: ", __func__), \
1899 +                               printk(args)) : 0)
1900 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
1901 +#define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
1902 +#else
1903 +#define DBG(args...)
1904 +#define DBG_FRAME(args...)
1905 +#define DBG_SKB(skb)
1906 +#endif
1907 +
1908 +/* use of last_frames[index].can_dlc */
1909 +#define RX_RECV    0x40 /* received data for this element */
1910 +#define RX_THR     0x80 /* element not been sent due to throttle feature */
1911 +#define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
1912 +
1913 +/* get best masking value for can_rx_register() for a given single can_id */
1914 +#define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
1915 +                       (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
1916 +
1917 +#define IDENT "bcm"
1918 +static __initdata const char banner[] = KERN_INFO
1919 +       "CAN: broadcast manager (bcm) socket protocol " VERSION "\n"; 
1920 +
1921 +MODULE_DESCRIPTION("PF_CAN bcm sockets");
1922 +MODULE_LICENSE("Dual BSD/GPL");
1923 +MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
1924 +
1925 +#define GET_U64(p) (*(u64*)(p)->data) /* easy access */
1926 +
1927 +struct bcm_op {
1928 +       struct list_head list;
1929 +       int ifindex;
1930 +       canid_t can_id;
1931 +       int flags;
1932 +       unsigned long j_ival1, j_ival2, j_lastmsg;
1933 +       unsigned long frames_abs, frames_filtered;
1934 +       struct timer_list timer, thrtimer;
1935 +       struct timeval ival1, ival2;
1936 +       struct timeval rx_stamp;
1937 +       int rx_ifindex;
1938 +       int count;
1939 +       int nframes;
1940 +       int currframe;
1941 +       struct can_frame *frames;
1942 +       struct can_frame *last_frames;
1943 +       struct sock *sk;
1944 +};
1945 +
1946 +struct bcm_opt {
1947 +       int bound;
1948 +       int ifindex;
1949 +       struct list_head rx_ops;
1950 +       struct list_head tx_ops;
1951 +       unsigned long dropped_usr_msgs;
1952 +       struct proc_dir_entry *bcm_proc_read;
1953 +       char procname [9]; /* pointer printed in ASCII with \0 */
1954 +};
1955 +
1956 +static struct proc_dir_entry *proc_dir = NULL;
1957 +
1958 +static int  bcm_init(struct sock *sk);
1959 +static void bcm_notifier(unsigned long msg, void *data);
1960 +static int  bcm_release(struct socket *sock);
1961 +static int  bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1962 +                       int flags);
1963 +static int  bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1964 +                       struct msghdr *msg, size_t size);
1965 +static int  bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1966 +                       struct msghdr *msg, size_t size, int flags);
1967 +static unsigned int bcm_poll(struct file *file, struct socket *sock,
1968 +                            poll_table *wait);
1969 +
1970 +static int  bcm_read_proc(char *page, char **start, off_t off,
1971 +                         int count, int *eof, void *data);
1972 +
1973 +static void bcm_tx_timeout_handler(unsigned long data);
1974 +static int  bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
1975 +static int  bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1976 +                        int ifindex, struct sock *sk);
1977 +static void bcm_can_tx(struct bcm_op *op);
1978 +
1979 +static int  bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1980 +                        int ifindex, struct sock *sk);
1981 +static void bcm_rx_handler(struct sk_buff *skb, void *op);
1982 +static void bcm_rx_timeout_handler(unsigned long data);
1983 +static void bcm_rx_thr_handler(unsigned long data);
1984 +static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1985 +                               struct can_frame *rxdata);
1986 +static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
1987 +static void bcm_rx_starttimer(struct bcm_op *op);
1988 +static void bcm_rx_update_and_send(struct bcm_op *op,
1989 +                                  struct can_frame *lastdata,
1990 +                                  struct can_frame *rxdata);
1991 +static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1992 +                            struct can_frame *frames, struct timeval *tv);
1993 +
1994 +static int  bcm_delete_tx_op(struct list_head *ops, canid_t can_id,
1995 +                            int ifindex);
1996 +static int  bcm_delete_rx_op(struct list_head *ops, canid_t can_id,
1997 +                            int ifindex);
1998 +static void bcm_remove_op(struct bcm_op *op);
1999 +static int  bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
2000 +                       int ifindex);
2001 +static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
2002 +                                 int ifindex);
2003 +
2004 +static struct proto_ops bcm_ops = {
2005 +       .family        = PF_CAN,
2006 +       .release       = bcm_release,
2007 +       .bind          = sock_no_bind,
2008 +       .connect       = bcm_connect,
2009 +       .socketpair    = sock_no_socketpair,
2010 +       .accept        = sock_no_accept,
2011 +       .getname       = sock_no_getname,
2012 +       .poll          = bcm_poll,
2013 +       .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
2014 +       .listen        = sock_no_listen,
2015 +       .shutdown      = sock_no_shutdown,
2016 +       .setsockopt    = sock_no_setsockopt,
2017 +       .getsockopt    = sock_no_getsockopt,
2018 +       .sendmsg       = bcm_sendmsg,
2019 +       .recvmsg       = bcm_recvmsg,
2020 +       .mmap          = sock_no_mmap,
2021 +       .sendpage      = sock_no_sendpage,
2022 +};
2023 +
2024 +#ifdef CONFIG_CAN_BCM_USER
2025 +#define BCM_CAP (-1)
2026 +#else
2027 +#define BCM_CAP CAP_NET_RAW
2028 +#endif
2029 +
2030 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
2031 +struct bcm_sock {
2032 +       struct sock    sk;
2033 +       struct bcm_opt opt;
2034 +};
2035 +
2036 +#define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
2037 +
2038 +static struct proto bcm_proto = {
2039 +       .name       = "CAN_BCM",
2040 +       .owner      = THIS_MODULE,
2041 +       .obj_size   = sizeof(struct bcm_sock),
2042 +       .init       = bcm_init,
2043 +};
2044 +
2045 +static struct can_proto bcm_can_proto = {
2046 +       .type       = SOCK_DGRAM,
2047 +       .protocol   = CAN_BCM,
2048 +       .capability = BCM_CAP,
2049 +       .ops        = &bcm_ops,
2050 +       .prot       = &bcm_proto,
2051 +};
2052 +#else
2053 +#define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
2054 +
2055 +static struct can_proto bcm_can_proto = {
2056 +       .type       = SOCK_DGRAM,
2057 +       .protocol   = CAN_BCM,
2058 +       .capability = BCM_CAP,
2059 +       .ops        = &bcm_ops,
2060 +       .owner      = THIS_MODULE,
2061 +       .obj_size   = sizeof(struct bcm_opt),
2062 +       .init       = bcm_init,
2063 +};
2064 +#endif
2065 +
2066 +#define CFSIZ sizeof(struct can_frame)
2067 +#define OPSIZ sizeof(struct bcm_op)
2068 +#define MHSIZ sizeof(struct bcm_msg_head)
2069 +
2070 +static int __init bcm_module_init(void)
2071 +{
2072 +       printk(banner);
2073 +
2074 +       can_proto_register(&bcm_can_proto);
2075 +
2076 +       /* create /proc/net/can/bcm directory */
2077 +       proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
2078 +
2079 +       if (proc_dir)
2080 +               proc_dir->owner = THIS_MODULE;
2081 +
2082 +       return 0;
2083 +}
2084 +
2085 +static void __exit bcm_module_exit(void)
2086 +{
2087 +       can_proto_unregister(&bcm_can_proto);
2088 +
2089 +       if (proc_dir)
2090 +               remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
2091 +
2092 +}
2093 +
2094 +/**************************************************/
2095 +/* initial settings at socket creation time       */
2096 +/**************************************************/
2097 +
2098 +static int bcm_init(struct sock *sk)
2099 +{
2100 +       struct bcm_opt *bo = bcm_sk(sk);
2101 +
2102 +       bo->bound            = 0;
2103 +       bo->ifindex          = 0;
2104 +       bo->dropped_usr_msgs = 0;
2105 +       bo->bcm_proc_read    = NULL;
2106 +
2107 +       INIT_LIST_HEAD(&bo->tx_ops);
2108 +       INIT_LIST_HEAD(&bo->rx_ops);
2109 +
2110 +       return 0;
2111 +}
2112 +
2113 +/**************************************************/
2114 +/* handling of netdevice problems                 */
2115 +/**************************************************/
2116 +
2117 +static void bcm_notifier(unsigned long msg, void *data)
2118 +{
2119 +       struct sock *sk = (struct sock *)data;
2120 +       struct bcm_opt *bo = bcm_sk(sk);
2121 +
2122 +       DBG("called for sock %p\n", sk);
2123 +
2124 +       switch (msg) {
2125 +       case NETDEV_UNREGISTER:
2126 +               bo->bound   = 0;
2127 +               bo->ifindex = 0;
2128 +               /* fallthrough */
2129 +       case NETDEV_DOWN:
2130 +               sk->sk_err = ENETDOWN;
2131 +               if (!sock_flag(sk, SOCK_DEAD))
2132 +                       sk->sk_error_report(sk);
2133 +       }
2134 +}
2135 +
2136 +/**************************************************/
2137 +/* standard socket functions                      */
2138 +/**************************************************/
2139 +
2140 +static int bcm_release(struct socket *sock)
2141 +{
2142 +       struct sock *sk = sock->sk;
2143 +       struct bcm_opt *bo = bcm_sk(sk);
2144 +       struct bcm_op *op, *next;
2145 +
2146 +       DBG("socket %p, sk %p\n", sock, sk);
2147 +
2148 +       /* remove bcm_ops, timer, rx_unregister(), etc. */
2149 +
2150 +       list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
2151 +               DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
2152 +               bcm_remove_op(op);
2153 +       }
2154 +
2155 +       list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
2156 +               DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
2157 +
2158 +               /* Don't care if we're bound or not (due to netdev problems) */
2159 +               /* can_rx_unregister() is always a save thing to do here     */
2160 +               if (op->ifindex) {
2161 +                       struct net_device *dev = dev_get_by_index(op->ifindex);
2162 +                       if (dev) {
2163 +                               can_rx_unregister(dev, op->can_id,
2164 +                                                 REGMASK(op->can_id),
2165 +                                                 bcm_rx_handler, op);
2166 +                               dev_put(dev);
2167 +                       }
2168 +               } else
2169 +                       can_rx_unregister(NULL, op->can_id,
2170 +                                         REGMASK(op->can_id),
2171 +                                         bcm_rx_handler, op);
2172 +
2173 +               bcm_remove_op(op);
2174 +       }
2175 +
2176 +       /* remove procfs entry */
2177 +       if ((proc_dir) && (bo->bcm_proc_read)) {
2178 +               remove_proc_entry(bo->procname, proc_dir);
2179 +       }
2180 +
2181 +       /* remove device notifier */
2182 +       if (bo->ifindex) {
2183 +               struct net_device *dev = dev_get_by_index(bo->ifindex);
2184 +               if (dev) {
2185 +                       can_dev_unregister(dev, bcm_notifier, sk);
2186 +                       dev_put(dev);
2187 +               }
2188 +       }
2189 +
2190 +       sock_put(sk);
2191 +
2192 +       return 0;
2193 +}
2194 +
2195 +static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
2196 +                      int flags)
2197 +{
2198 +       struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
2199 +       struct sock *sk = sock->sk;
2200 +       struct bcm_opt *bo = bcm_sk(sk);
2201 +
2202 +       if (bo->bound)
2203 +               return -EISCONN;
2204 +
2205 +       /* bind a device to this socket */
2206 +       if (addr->can_ifindex) {
2207 +               struct net_device *dev = dev_get_by_index(addr->can_ifindex);
2208 +               if (!dev) {
2209 +                       DBG("could not find device index %d\n",
2210 +                           addr->can_ifindex);
2211 +                       return -ENODEV;
2212 +               }
2213 +               bo->ifindex = dev->ifindex;
2214 +               can_dev_register(dev, bcm_notifier, sk); /* register notif. */
2215 +               dev_put(dev);
2216 +
2217 +               DBG("socket %p bound to device %s (idx %d)\n",
2218 +                   sock, dev->name, dev->ifindex);
2219 +       } else {
2220 +               /* no notifier for ifindex = 0 ('any' CAN device) */
2221 +               bo->ifindex = 0;
2222 +       }
2223 +
2224 +       bo->bound = 1;
2225 +
2226 +       if (proc_dir) {
2227 +               /* unique socket address as filename */
2228 +               sprintf(bo->procname, "%p", sock);
2229 +               bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
2230 +                                                          proc_dir,
2231 +                                                          bcm_read_proc, sk);
2232 +       }
2233 +
2234 +       return 0;
2235 +}
2236 +
2237 +static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
2238 +                      struct msghdr *msg, size_t size)
2239 +{
2240 +       struct sock *sk = sock->sk;
2241 +       struct bcm_opt *bo = bcm_sk(sk);
2242 +       int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
2243 +       struct bcm_msg_head msg_head;
2244 +       int ret; /* read bytes or error codes as return value */
2245 +
2246 +       if (!bo->bound) {
2247 +               DBG("sock %p not bound\n", sk);
2248 +               return -ENOTCONN;
2249 +       }
2250 +
2251 +       /* check for alternative ifindex for this bcm_op */
2252 +
2253 +       if (!ifindex && msg->msg_name) { /* no bound device as default */
2254 +               struct sockaddr_can *addr = 
2255 +                       (struct sockaddr_can *)msg->msg_name;
2256 +               if (addr->can_family != AF_CAN)
2257 +                       return -EINVAL;
2258 +               ifindex = addr->can_ifindex; /* ifindex from sendto() */
2259 +
2260 +               if (ifindex && !dev_get_by_index(ifindex)) {
2261 +                       DBG("device %d not found\n", ifindex);
2262 +                       return -ENODEV;
2263 +               }
2264 +       }
2265 +
2266 +       /* read message head information */
2267 +
2268 +       if ((ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov,
2269 +                                   MHSIZ)) < 0)
2270 +               return ret;
2271 +
2272 +       DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
2273 +
2274 +       switch (msg_head.opcode) {
2275 +
2276 +       case TX_SETUP:
2277 +
2278 +               ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
2279 +               break;
2280 +
2281 +       case RX_SETUP:
2282 +
2283 +               ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
2284 +               break;
2285 +
2286 +       case TX_DELETE:
2287 +
2288 +               if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
2289 +                       ret = MHSIZ;
2290 +               else
2291 +                       ret = -EINVAL;
2292 +               break;
2293 +                   
2294 +       case RX_DELETE:
2295 +
2296 +               if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
2297 +                       ret = MHSIZ;
2298 +               else
2299 +                       ret = -EINVAL;
2300 +               break;
2301 +
2302 +       case TX_READ:
2303 +
2304 +               /* reuse msg_head for the reply */
2305 +               msg_head.opcode  = TX_STATUS; /* reply to TX_READ */
2306 +               ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
2307 +               break;
2308 +
2309 +       case RX_READ:
2310 +
2311 +               /* reuse msg_head for the reply */
2312 +               msg_head.opcode  = RX_STATUS; /* reply to RX_READ */
2313 +               ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
2314 +               break;
2315 +
2316 +       case TX_SEND:
2317 +
2318 +               if (msg_head.nframes < 1) /* we need at least one can_frame */
2319 +                       return -EINVAL;
2320 +
2321 +               ret = bcm_tx_send(msg, ifindex, sk);
2322 +               break;
2323 +
2324 +       default:
2325 +
2326 +               DBG("Unknown opcode %d\n", msg_head.opcode);
2327 +               ret = -EINVAL;
2328 +               break;
2329 +       }
2330 +
2331 +       return ret;
2332 +}
2333 +
2334 +static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
2335 +                      struct msghdr *msg, size_t size, int flags)
2336 +{
2337 +       struct sock *sk = sock->sk;
2338 +       struct sk_buff *skb;
2339 +       int error = 0;
2340 +       int noblock;
2341 +       int err;
2342 +
2343 +       DBG("socket %p, sk %p\n", sock, sk);
2344 +
2345 +       noblock =  flags & MSG_DONTWAIT;
2346 +       flags   &= ~MSG_DONTWAIT;
2347 +       if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
2348 +               return error;
2349 +       }
2350 +
2351 +       DBG("delivering skbuff %p\n", skb);
2352 +       DBG_SKB(skb);
2353 +
2354 +       if (skb->len < size)
2355 +               size = skb->len;
2356 +       if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
2357 +               skb_free_datagram(sk, skb);
2358 +               return err;
2359 +       }
2360 +
2361 +       sock_recv_timestamp(msg, sk, skb);
2362 +
2363 +       if (msg->msg_name) {
2364 +               msg->msg_namelen = sizeof(struct sockaddr_can);
2365 +               memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
2366 +       }
2367 +
2368 +       DBG("freeing sock %p, skbuff %p\n", sk, skb);
2369 +       skb_free_datagram(sk, skb);
2370 +
2371 +       return size;
2372 +}
2373 +
2374 +static unsigned int bcm_poll(struct file *file, struct socket *sock,
2375 +                            poll_table *wait)
2376 +{
2377 +       unsigned int mask = 0;
2378 +
2379 +       DBG("socket %p\n", sock);
2380 +
2381 +       mask = datagram_poll(file, sock, wait);
2382 +       return mask;
2383 +}
2384 +
2385 +/**************************************************/
2386 +/* helper functions for bcm_sendmsg()             */
2387 +/**************************************************/
2388 +
2389 +static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
2390 +                       int ifindex, struct sock *sk)
2391 +{
2392 +       struct bcm_opt *bo = bcm_sk(sk);
2393 +       struct bcm_op *op;
2394 +       int i, err;
2395 +
2396 +       if (!ifindex) /* we need a real device to send frames */
2397 +               return -ENODEV;
2398 +
2399 +       if (msg_head->nframes < 1) /* we need at least one can_frame */
2400 +               return -EINVAL;
2401 +
2402 +       /* check the given can_id */
2403 +
2404 +       if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
2405 +
2406 +               /* update existing BCM operation */
2407 +
2408 +               DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
2409 +                   op, msg_head->can_id);
2410 +
2411 +               /* Do we need more space for the can_frames than currently */
2412 +               /* allocated? -> This is a _really_ unusual use-case and   */
2413 +               /* therefore (complexity / locking) it is not supported.   */
2414 +               if (msg_head->nframes > op->nframes)
2415 +                       return -E2BIG;
2416 +
2417 +               /* update can_frames content */
2418 +               for (i = 0; i < msg_head->nframes; i++) {
2419 +                       if ((err = memcpy_fromiovec((u8*)&op->frames[i],
2420 +                                                   msg->msg_iov, CFSIZ)) < 0)
2421 +                               return err;
2422 +
2423 +                       if (msg_head->flags & TX_CP_CAN_ID) {
2424 +                               /* copy can_id into frame */
2425 +                               op->frames[i].can_id = msg_head->can_id;
2426 +                       }
2427 +               }
2428 +
2429 +       } else {
2430 +               /* insert new BCM operation for the given can_id */
2431 +
2432 +               if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
2433 +                       return -ENOMEM;
2434 +
2435 +               memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
2436 +
2437 +               DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
2438 +                   op, msg_head->can_id);
2439 +
2440 +               op->can_id    = msg_head->can_id;
2441 +
2442 +               /* create array for can_frames and copy the data */
2443 +               if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
2444 +                                          GFP_KERNEL))) {
2445 +                       kfree(op);
2446 +                       return -ENOMEM;
2447 +               }
2448 +
2449 +               for (i = 0; i < msg_head->nframes; i++) {
2450 +                       if ((err = memcpy_fromiovec((u8*)&op->frames[i],
2451 +                                                   msg->msg_iov,
2452 +                                                   CFSIZ)) < 0) {
2453 +                               kfree(op->frames);
2454 +                               kfree(op);
2455 +                               return err;
2456 +                       }
2457 +
2458 +                       if (msg_head->flags & TX_CP_CAN_ID) {
2459 +                               /* copy can_id into frame */
2460 +                               op->frames[i].can_id = msg_head->can_id;
2461 +                       }
2462 +               }
2463 +
2464 +               /* tx_ops never compare with previous received messages */
2465 +               op->last_frames = NULL;
2466 +
2467 +               /* bcm_can_tx / bcm_tx_timeout_handler needs this */
2468 +               op->sk = sk;
2469 +
2470 +               op->ifindex = ifindex;
2471 +
2472 +               /* initialize uninitialized (kmalloc) structure */
2473 +               init_timer(&op->timer);
2474 +
2475 +               /* currently unused in tx_ops */
2476 +               init_timer(&op->thrtimer);
2477 +
2478 +               /* handler for tx_ops */
2479 +               op->timer.function = bcm_tx_timeout_handler;
2480 +
2481 +               /* timer.data points to this op-structure */
2482 +               op->timer.data = (unsigned long)op;
2483 +
2484 +               /* add this bcm_op to the list of the tx_ops */
2485 +               list_add(&op->list, &bo->tx_ops);
2486 +
2487 +       } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
2488 +
2489 +       if (op->nframes != msg_head->nframes) {
2490 +               op->nframes   = msg_head->nframes;
2491 +               /* start multiple frame transmission with index 0 */
2492 +               op->currframe = 0;
2493 +       }
2494 +
2495 +       /* check flags */
2496 +
2497 +       op->flags = msg_head->flags;
2498 +
2499 +       if (op->flags & TX_RESET_MULTI_IDX) {
2500 +               /* start multiple frame transmission with index 0 */
2501 +               op->currframe = 0; 
2502 +       }
2503 +
2504 +       if (op->flags & SETTIMER) {
2505 +
2506 +               /* set timer values */
2507 +
2508 +               op->count   = msg_head->count;
2509 +               op->ival1   = msg_head->ival1;
2510 +               op->ival2   = msg_head->ival2;
2511 +               op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
2512 +               op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
2513 +
2514 +               DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
2515 +                   op->count, op->j_ival1, op->j_ival2);
2516 +
2517 +               /* disable an active timer due to zero values? */
2518 +               if (!op->j_ival1 && !op->j_ival2) {
2519 +                       del_timer(&op->timer);
2520 +                       DBG("TX_SETUP: SETTIMER disabled timer.\n");
2521 +               }
2522 +       }
2523 +
2524 +       if ((op->flags & STARTTIMER) &&
2525 +           ((op->j_ival1 && op->count) || op->j_ival2)) {
2526 +
2527 +               del_timer(&op->timer);
2528 +
2529 +               /* spec: send can_frame when starting timer */
2530 +               op->flags |= TX_ANNOUNCE;
2531 +
2532 +               if (op->j_ival1 && (op->count > 0)){
2533 +                       op->timer.expires = jiffies + op->j_ival1;
2534 +                       /* op->count-- is done in bcm_tx_timeout_handler */
2535 +                       DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
2536 +                           "exp=0x%08X\n",
2537 +                           op->timer.function,
2538 +                           (char*) op->timer.data,
2539 +                           (unsigned int) op->timer.expires);
2540 +               } else{
2541 +                       op->timer.expires = jiffies + op->j_ival2;
2542 +                       DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
2543 +                           "exp=0x%08X\n",
2544 +                           op->timer.function,
2545 +                           (char*) op->timer.data,
2546 +                           (unsigned int) op->timer.expires);
2547 +               }
2548 +
2549 +               add_timer(&op->timer);
2550 +       }
2551 +
2552 +       if (op->flags & TX_ANNOUNCE)
2553 +               bcm_can_tx(op);
2554 +
2555 +       return msg_head->nframes * CFSIZ + MHSIZ;
2556 +}
2557 +
2558 +static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
2559 +                       int ifindex, struct sock *sk)
2560 +{
2561 +       struct bcm_opt *bo = bcm_sk(sk);
2562 +       struct bcm_op *op;
2563 +       int do_rx_register;
2564 +       int err;
2565 +
2566 +       if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
2567 +               /* be robust against wrong usage ... */
2568 +               msg_head->flags |= RX_FILTER_ID;
2569 +               msg_head->nframes = 0; /* ignore trailing garbage */
2570 +       }
2571 +
2572 +       if ((msg_head->flags & RX_RTR_FRAME) &&
2573 +           ((msg_head->nframes != 1) ||
2574 +            (!(msg_head->can_id & CAN_RTR_FLAG)))) {
2575 +
2576 +               DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
2577 +               return -EINVAL;
2578 +       }
2579 +
2580 +       /* check the given can_id */
2581 +
2582 +       if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
2583 +
2584 +               /* update existing BCM operation */
2585 +
2586 +               DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
2587 +                   op, msg_head->can_id);
2588 +
2589 +               /* Do we need more space for the can_frames than currently */
2590 +               /* allocated? -> This is a _really_ unusual use-case and   */
2591 +               /* therefore (complexity / locking) it is not supported.   */
2592 +               if (msg_head->nframes > op->nframes)
2593 +                       return -E2BIG;
2594 +
2595 +               if (msg_head->nframes) {
2596 +                       /* update can_frames content */
2597 +                       if ((err = memcpy_fromiovec((u8*)op->frames,
2598 +                                                   msg->msg_iov,
2599 +                                                   msg_head->nframes
2600 +                                                   * CFSIZ) < 0))
2601 +                               return err;
2602 +
2603 +                       /* clear last_frames to indicate 'nothing received' */
2604 +                       memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
2605 +               }
2606 +
2607 +               op->nframes = msg_head->nframes;
2608 +               /* Only an update -> do not call can_rx_register() */
2609 +               do_rx_register = 0;
2610 +
2611 +       } else {
2612 +               /* insert new BCM operation for the given can_id */
2613 +
2614 +               if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
2615 +                       return -ENOMEM;
2616 +
2617 +               memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
2618 +
2619 +               DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
2620 +                   op, msg_head->can_id);
2621 +
2622 +               op->can_id    = msg_head->can_id;
2623 +               op->nframes   = msg_head->nframes;
2624 +
2625 +               if (msg_head->nframes) {
2626 +
2627 +                       /* create array for can_frames and copy the data */
2628 +                       if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
2629 +                                                  GFP_KERNEL))) {
2630 +                               kfree(op);
2631 +                               return -ENOMEM;
2632 +                       }
2633 +
2634 +                       if ((err = memcpy_fromiovec((u8*)op->frames,
2635 +                                                   msg->msg_iov,
2636 +                                                   msg_head->nframes
2637 +                                                   * CFSIZ)) < 0) {
2638 +                               kfree(op->frames);
2639 +                               kfree(op);
2640 +                               return err;
2641 +                       }
2642 +
2643 +                       /* create array for received can_frames */
2644 +                       if (!(op->last_frames = kmalloc(msg_head->nframes
2645 +                                                       * CFSIZ,
2646 +                                                       GFP_KERNEL))) {
2647 +                               kfree(op->frames);
2648 +                               kfree(op);
2649 +                               return -ENOMEM;
2650 +                       }
2651 +
2652 +                       /* clear last_frames to indicate 'nothing received' */
2653 +                       memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
2654 +               } else {
2655 +                       /* op->frames = NULL due to memset */
2656 +
2657 +                       /* even when we have the RX_FILTER_ID case, we need */
2658 +                       /* to store the last frame for the throttle feature */
2659 +
2660 +                       /* create array for received can_frames */
2661 +                       if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
2662 +                               kfree(op);
2663 +                               return -ENOMEM;
2664 +                       }
2665 +
2666 +                       /* clear last_frames to indicate 'nothing received' */
2667 +                       memset(op->last_frames, 0, CFSIZ);
2668 +               }
2669 +
2670 +               op->sk = sk; /* bcm_delete_rx_op() needs this */
2671 +               op->ifindex = ifindex;
2672 +
2673 +               /* initialize uninitialized (kmalloc) structure */
2674 +               init_timer(&op->timer);
2675 +
2676 +               /* init throttle timer for RX_CHANGED */
2677 +               init_timer(&op->thrtimer);
2678 +
2679 +               /* handler for rx timeouts */
2680 +               op->timer.function = bcm_rx_timeout_handler;
2681 +
2682 +               /* timer.data points to this op-structure */
2683 +               op->timer.data = (unsigned long)op;
2684 +
2685 +               /* handler for RX_CHANGED throttle timeouts */
2686 +               op->thrtimer.function = bcm_rx_thr_handler;
2687 +
2688 +               /* timer.data points to this op-structure */
2689 +               op->thrtimer.data = (unsigned long)op;
2690 +
2691 +               op->thrtimer.expires = 0; /* mark disabled timer */
2692 +
2693 +               /* add this bcm_op to the list of the tx_ops */
2694 +               list_add(&op->list, &bo->rx_ops);
2695 +
2696 +               do_rx_register = 1; /* call can_rx_register() */
2697 +
2698 +       } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
2699 +
2700 +
2701 +       /* check flags */
2702 +
2703 +       op->flags = msg_head->flags;
2704 +
2705 +       if (op->flags & RX_RTR_FRAME) {
2706 +
2707 +               /* no timers in RTR-mode */
2708 +               del_timer(&op->thrtimer);
2709 +               del_timer(&op->timer);
2710 +
2711 +               /* funny feature in RX(!)_SETUP only for RTR-mode: */
2712 +               /* copy can_id into frame BUT without RTR-flag to  */
2713 +               /* prevent a full-load-loopback-test ... ;-]       */
2714 +               if ((op->flags & TX_CP_CAN_ID) ||
2715 +                   (op->frames[0].can_id == op->can_id))
2716 +                       op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
2717 +
2718 +       } else {
2719 +               if (op->flags & SETTIMER) {
2720 +
2721 +                       /* set timer value */
2722 +
2723 +                       op->ival1   = msg_head->ival1;
2724 +                       op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
2725 +                       op->ival2   = msg_head->ival2;
2726 +                       op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
2727 +
2728 +                       DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
2729 +                           op->j_ival1, op->j_ival2);
2730 +
2731 +                       /* disable an active timer due to zero value? */
2732 +                       if (!op->j_ival1) {
2733 +                               del_timer(&op->timer);
2734 +                               DBG("RX_SETUP: disabled timer rx timeouts.\n");
2735 +                       }
2736 +
2737 +                       /* free currently blocked msgs ? */
2738 +                       if (op->thrtimer.expires) { /* blocked by timer? */
2739 +                               DBG("RX_SETUP: unblocking throttled msgs.\n");
2740 +                               del_timer(&op->thrtimer);
2741 +                               /* send blocked msgs hereafter */
2742 +                               op->thrtimer.expires = jiffies + 2;
2743 +                               add_timer(&op->thrtimer);
2744 +                       }
2745 +                       /* if (op->j_ival2) is zero, no (new) throttling     */
2746 +                       /* will happen. For details see functions            */
2747 +                       /* bcm_rx_update_and_send() and bcm_rx_thr_handler() */
2748 +               }
2749 +
2750 +               if ((op->flags & STARTTIMER) && op->j_ival1) {
2751 +
2752 +                       del_timer(&op->timer);
2753 +
2754 +                       op->timer.expires = jiffies + op->j_ival1;
2755 +
2756 +                       DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
2757 +                           " exp=0x%08X\n",
2758 +                           (char *) op->timer.function,
2759 +                           (char *) op->timer.data,
2760 +                           (unsigned int) op->timer.expires);
2761 +
2762 +                       add_timer(&op->timer);
2763 +               }
2764 +       }
2765 +
2766 +       /* now we can register for can_ids, if we added a new bcm_op */
2767 +       if (do_rx_register) {
2768 +               DBG("RX_SETUP: can_rx_register() for can_id %03X. "
2769 +                   "rx_op is %p\n", op->can_id, op);
2770 +
2771 +               if (ifindex) {
2772 +                       struct net_device *dev = dev_get_by_index(ifindex);
2773 +
2774 +                       if (dev) {
2775 +                               can_rx_register(dev, op->can_id,
2776 +                                               REGMASK(op->can_id),
2777 +                                               bcm_rx_handler, op, IDENT);
2778 +                               dev_put(dev);
2779 +                       }
2780 +               } else 
2781 +                       can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
2782 +                                       bcm_rx_handler, op, IDENT);
2783 +       }
2784 +
2785 +       return msg_head->nframes * CFSIZ + MHSIZ;
2786 +}
2787 +
2788 +static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
2789 +{
2790 +       struct sk_buff *skb;
2791 +       struct net_device *dev;
2792 +       int err;
2793 +
2794 +       /* just copy and send one can_frame */
2795 +
2796 +       if (!ifindex) /* we need a real device to send frames */
2797 +               return -ENODEV;
2798 +
2799 +       skb = alloc_skb(CFSIZ, GFP_KERNEL);
2800 +
2801 +       if (!skb)
2802 +               return -ENOMEM;
2803 +
2804 +       if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov,
2805 +                                   CFSIZ)) < 0) {
2806 +               kfree_skb(skb);
2807 +               return err;
2808 +       }
2809 +
2810 +       DBG_FRAME("BCM: TX_SEND: sending frame",
2811 +                 (struct can_frame *)skb->data);
2812 +       dev = dev_get_by_index(ifindex);
2813 +
2814 +       if (!dev) {
2815 +               kfree_skb(skb);
2816 +               return -ENODEV;
2817 +       }
2818 +
2819 +       skb->dev = dev;
2820 +       skb->sk  = sk;
2821 +       can_send(skb, 1); /* send with loopback */
2822 +       dev_put(dev);
2823 +
2824 +       return CFSIZ + MHSIZ;
2825 +}
2826 +
2827 +static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
2828 +                      int ifindex)
2829 +{
2830 +       struct bcm_op *op;
2831 +       int ret;
2832 +
2833 +       if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
2834 +
2835 +               DBG("TRX_READ: sending status for can_id %03X\n",
2836 +                   msg_head->can_id);
2837 +               /* put current values into msg_head */
2838 +               msg_head->flags   = op->flags;
2839 +               msg_head->count   = op->count;
2840 +               msg_head->ival1   = op->ival1;
2841 +               msg_head->ival2   = op->ival2;
2842 +               msg_head->nframes = op->nframes;
2843 +
2844 +               bcm_send_to_user(op, msg_head, op->frames, NULL);
2845 +
2846 +               ret = MHSIZ;
2847 +
2848 +       } else {
2849 +
2850 +               DBG("TRX_READ: did not find op for can_id %03X\n",
2851 +                   msg_head->can_id);
2852 +               ret = -EINVAL;
2853 +       }
2854 +
2855 +       return ret;
2856 +}
2857 +
2858 +/**************************************************/
2859 +/* procfs functions                               */
2860 +/**************************************************/
2861 +
2862 +static char *bcm_proc_getifname(int ifindex)
2863 +{
2864 +       struct net_device *dev;
2865 +
2866 +       if (!ifindex)
2867 +               return "any";
2868 +
2869 +       dev = __dev_get_by_index(ifindex); /* no usage counting */
2870 +       if (dev)
2871 +               return dev->name;
2872 +
2873 +       return "???";
2874 +}
2875 +
2876 +static int bcm_read_proc(char *page, char **start, off_t off,
2877 +                        int count, int *eof, void *data)
2878 +{
2879 +       int len = 0;
2880 +       struct sock *sk = (struct sock *)data;
2881 +       struct bcm_opt *bo = bcm_sk(sk);
2882 +       struct bcm_op *op;
2883 +
2884 +       len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
2885 +                       sk->sk_socket);
2886 +       len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
2887 +       len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
2888 +       len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
2889 +                       bo->dropped_usr_msgs);
2890 +       len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
2891 +                       bcm_proc_getifname(bo->ifindex));
2892 +       len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
2893 +
2894 +       list_for_each_entry(op, &bo->rx_ops, list) {
2895 +
2896 +               unsigned long reduction;
2897 +
2898 +               /* print only active entries & prevent division by zero */
2899 +               if (!op->frames_abs)
2900 +                       continue;
2901 +
2902 +               len += snprintf(page + len, PAGE_SIZE - len,
2903 +                               "rx_op: %03X %-5s ",
2904 +                               op->can_id, bcm_proc_getifname(op->ifindex));
2905 +               len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
2906 +                               op->nframes,
2907 +                               (op->flags & RX_CHECK_DLC)?'d':' ');
2908 +               if (op->j_ival1)
2909 +                       len += snprintf(page + len, PAGE_SIZE - len,
2910 +                                       "timeo=%ld ", op->j_ival1);
2911 +
2912 +               if (op->j_ival2)
2913 +                       len += snprintf(page + len, PAGE_SIZE - len,
2914 +                                       "thr=%ld ", op->j_ival2);
2915 +
2916 +               len += snprintf(page + len, PAGE_SIZE - len,
2917 +                               "# recv %ld (%ld) => reduction: ",
2918 +                               op->frames_filtered, op->frames_abs);
2919 +
2920 +               reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
2921 +
2922 +               len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
2923 +                               (reduction == 100)?"near ":"", reduction);
2924 +
2925 +               if (len > PAGE_SIZE - 200) {
2926 +                       /* mark output cut off */
2927 +                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
2928 +                       break;
2929 +               }
2930 +       }
2931 +
2932 +       list_for_each_entry(op, &bo->tx_ops, list) {
2933 +
2934 +               len += snprintf(page + len, PAGE_SIZE - len,
2935 +                               "tx_op: %03X %s [%d] ",
2936 +                               op->can_id, bcm_proc_getifname(op->ifindex),
2937 +                               op->nframes);
2938 +               if (op->j_ival1)
2939 +                       len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
2940 +                                       op->j_ival1);
2941 +
2942 +               if (op->j_ival2)
2943 +                       len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
2944 +                                       op->j_ival2);
2945 +
2946 +               len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
2947 +                               op->frames_abs);
2948 +
2949 +               if (len > PAGE_SIZE - 100) {
2950 +                       /* mark output cut off */
2951 +                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
2952 +                       break;
2953 +               }
2954 +       }
2955 +
2956 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
2957 +
2958 +       *eof = 1;
2959 +       return len;
2960 +}
2961 +
2962 +/**************************************************/
2963 +/* bcm_op handling tx path                        */
2964 +/**************************************************/
2965 +
2966 +static void bcm_can_tx(struct bcm_op *op)
2967 +{
2968 +       struct sk_buff *skb;
2969 +       struct net_device *dev;
2970 +       struct can_frame *cf = &op->frames[op->currframe];
2971 +
2972 +       DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
2973 +
2974 +       if (!op->ifindex)
2975 +               return; /* no target device -> exit */
2976 +
2977 +       dev = dev_get_by_index(op->ifindex);
2978 +
2979 +       if (!dev)
2980 +               return; /* should this bcm_op remove itself here? */
2981 +
2982 +       skb = alloc_skb(CFSIZ,
2983 +                       in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
2984 +
2985 +       if (!skb)
2986 +               goto out; /* no memory */
2987 +
2988 +       memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
2989 +
2990 +       skb->dev = dev;
2991 +       skb->sk = op->sk;
2992 +       can_send(skb, 1); /* send with loopback */
2993 +
2994 +       op->currframe++;
2995 +       op->frames_abs++; /* statistics */
2996 +
2997 +       /* reached last frame? */
2998 +       if (op->currframe >= op->nframes)
2999 +               op->currframe = 0;
3000 + out:
3001 +       dev_put(dev);
3002 +}
3003 +
3004 +static void bcm_tx_timeout_handler(unsigned long data)
3005 +{
3006 +       struct bcm_op *op = (struct bcm_op*)data;
3007 +
3008 +       DBG("Called with bcm_op %p\n", op);
3009 +
3010 +       if (op->j_ival1 && (op->count > 0)) {
3011 +
3012 +               op->count--;
3013 +
3014 +               if (!op->count && (op->flags & TX_COUNTEVT)) {
3015 +                       /* create notification to user */
3016 +
3017 +                       struct bcm_msg_head msg_head;
3018 +
3019 +                       DBG("sending TX_EXPIRED for can_id %03X\n",
3020 +                           op->can_id);
3021 +
3022 +                       msg_head.opcode  = TX_EXPIRED;
3023 +                       msg_head.flags   = op->flags;
3024 +                       msg_head.count   = op->count;
3025 +                       msg_head.ival1   = op->ival1;
3026 +                       msg_head.ival2   = op->ival2;
3027 +                       msg_head.can_id  = op->can_id;
3028 +                       msg_head.nframes = 0;
3029 +
3030 +                       bcm_send_to_user(op, &msg_head, NULL, NULL);
3031 +               }
3032 +       }
3033 +
3034 +       DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
3035 +           op->count, op->j_ival1, op->j_ival2);
3036 +
3037 +       if (op->j_ival1 && (op->count > 0)) {
3038 +
3039 +               op->timer.expires = jiffies + op->j_ival1;
3040 +               add_timer(&op->timer);
3041 +
3042 +               DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
3043 +                   op->timer.function,
3044 +                   (char*) op->timer.data,
3045 +                   (unsigned int) op->timer.expires);
3046 +
3047 +               bcm_can_tx(op); /* send (next) frame */
3048 +       } else {
3049 +               if (op->j_ival2) {
3050 +                       op->timer.expires = jiffies + op->j_ival2;
3051 +                       add_timer(&op->timer);
3052 +
3053 +                       DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
3054 +                           op->timer.function,
3055 +                           (char*) op->timer.data,
3056 +                           (unsigned int) op->timer.expires);
3057 +
3058 +                       bcm_can_tx(op); /* send (next) frame */
3059 +               } else
3060 +                       DBG("no timer restart\n");
3061 +       }
3062 +
3063 +       return;
3064 +
3065 +}
3066 +
3067 +/**************************************************/
3068 +/* bcm_op handling rx path                        */
3069 +/**************************************************/
3070 +
3071 +static void bcm_rx_handler(struct sk_buff *skb, void *data)
3072 +{
3073 +       struct bcm_op *op = (struct bcm_op*)data;
3074 +       struct can_frame rxframe;
3075 +       int i;
3076 +
3077 +       del_timer(&op->timer); /* disable timeout */
3078 +
3079 +       DBG("Called with bcm_op %p\n", op);
3080 +
3081 +       if (skb->len == sizeof(rxframe)) {
3082 +               memcpy(&rxframe, skb->data, sizeof(rxframe));
3083 +               skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
3084 +               /* save originator for recvfrom() */
3085 +               op->rx_ifindex = skb->dev->ifindex;
3086 +               op->frames_abs++; /* statistics */
3087 +               kfree_skb(skb);
3088 +               DBG("got can_frame with can_id %03X\n", rxframe.can_id);
3089 +       } else {
3090 +               DBG("Wrong skb->len = %d\n", skb->len);
3091 +               kfree_skb(skb);
3092 +               return;
3093 +       }
3094 +
3095 +       DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
3096 +
3097 +       if (op->can_id != rxframe.can_id) {
3098 +               DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
3099 +                   rxframe.can_id, op->can_id);
3100 +               return;
3101 +       }
3102 +
3103 +       if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
3104 +               DBG("RTR-request\n");
3105 +               bcm_can_tx(op); /* send op->frames[0] to CAN device */
3106 +               return;
3107 +       }
3108 +
3109 +       if (op->flags & RX_FILTER_ID) { /* the easiest case */
3110 +               DBG("Easy does it with RX_FILTER_ID\n");
3111 +               bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
3112 +               bcm_rx_starttimer(op);
3113 +               return;
3114 +       }
3115 +
3116 +       if (op->nframes == 1) { /* simple compare with index 0 */
3117 +               DBG("Simple compare\n");
3118 +               bcm_rx_cmp_to_index(op, 0, &rxframe);
3119 +               bcm_rx_starttimer(op);
3120 +               return;
3121 +       }
3122 +
3123 +       if (op->nframes > 1) { /* multiplex compare */
3124 +
3125 +               DBG("Multiplex compare\n");
3126 +               /* find the first multiplex mask that fits */
3127 +               /* MUX-mask is in index 0 */
3128 +
3129 +               for (i=1; i < op->nframes; i++) {
3130 +
3131 +                       if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
3132 +                           (GET_U64(&op->frames[0]) &
3133 +                            GET_U64(&op->frames[i]))) {
3134 +                               DBG("found MUX index %d\n", i);
3135 +                               bcm_rx_cmp_to_index(op, i, &rxframe);
3136 +                               break;
3137 +                       }
3138 +               }
3139 +               bcm_rx_starttimer(op);
3140 +       }
3141 +}
3142 +
3143 +static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
3144 +                               struct can_frame *rxdata)
3145 +{
3146 +       /* no one uses the MSBs of can_dlc for comparation, */
3147 +       /* so we use it here to detect the first time of reception */
3148 +
3149 +       if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
3150 +               DBG("first time :)\n");
3151 +               bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
3152 +               return;
3153 +       }
3154 +
3155 +       /* do a real check in can_data */
3156 +
3157 +       DBG("op->frames[index].data = 0x%016llx\n",
3158 +           GET_U64(&op->frames[index]));
3159 +       DBG("op->last_frames[index].data = 0x%016llx\n",
3160 +           GET_U64(&op->last_frames[index]));
3161 +       DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
3162 +
3163 +       if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
3164 +           (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
3165 +               DBG("relevant data change :)\n");
3166 +               bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
3167 +               return;
3168 +       }
3169 +
3170 +
3171 +       if (op->flags & RX_CHECK_DLC) {
3172 +
3173 +               /* do a real check in dlc */
3174 +
3175 +               if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
3176 +                                       BCM_CAN_DLC_MASK)) {
3177 +                       DBG("dlc change :)\n");
3178 +                       bcm_rx_update_and_send(op, &op->last_frames[index],
3179 +                                              rxdata);
3180 +                       return;
3181 +               }
3182 +       }
3183 +       DBG("no relevant change :(\n");
3184 +}
3185 +
3186 +static void bcm_rx_update_and_send(struct bcm_op *op,
3187 +                                  struct can_frame *lastdata,
3188 +                                  struct can_frame *rxdata)
3189 +{
3190 +       unsigned long nexttx = op->j_lastmsg + op->j_ival2;
3191 +
3192 +       memcpy(lastdata, rxdata, CFSIZ);
3193 +       lastdata->can_dlc |= RX_RECV; /* mark as used */
3194 +
3195 +       /* throttle bcm_rx_changed ? */
3196 +       if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
3197 +           ((op->j_ival2) && (nexttx > jiffies))) {      /* we have to wait */
3198 +
3199 +               lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
3200 +
3201 +               if (!(op->thrtimer.expires)) { /* start only the first time */
3202 +                       op->thrtimer.expires = nexttx;
3203 +                       add_timer(&op->thrtimer);
3204 +
3205 +                       DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
3206 +                           op->thrtimer.function,
3207 +                           (char*) op->thrtimer.data,
3208 +                           (unsigned int) op->thrtimer.expires);
3209 +               }
3210 +       } else
3211 +               bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
3212 +}
3213 +
3214 +static void bcm_rx_starttimer(struct bcm_op *op)
3215 +{
3216 +       if (op->flags & RX_NO_AUTOTIMER)
3217 +               return;
3218 +
3219 +       if (op->j_ival1) {
3220 +
3221 +               op->timer.expires = jiffies + op->j_ival1;
3222 +
3223 +               DBG("adding rx timeout timer ival1. func=%p data=%p "
3224 +                   "exp=0x%08X\n",
3225 +                   op->timer.function,
3226 +                   (char*) op->timer.data,
3227 +                   (unsigned int) op->timer.expires);
3228 +
3229 +               add_timer(&op->timer);
3230 +       }
3231 +}
3232 +
3233 +
3234 +static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
3235 +{
3236 +       struct bcm_msg_head head;
3237 +
3238 +       op->j_lastmsg = jiffies;
3239 +       op->frames_filtered++; /* statistics */
3240 +
3241 +       if (op->frames_filtered > ULONG_MAX/100)
3242 +               op->frames_filtered = op->frames_abs = 0; /* restart */
3243 +
3244 +       DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
3245 +           (unsigned int) op->j_lastmsg, op);
3246 +       DBG("sending notification\n");
3247 +
3248 +       head.opcode  = RX_CHANGED;
3249 +       head.flags   = op->flags;
3250 +       head.count   = op->count;
3251 +       head.ival1   = op->ival1;
3252 +       head.ival2   = op->ival2;
3253 +       head.can_id  = op->can_id;
3254 +       head.nframes = 1;
3255 +
3256 +       bcm_send_to_user(op, &head, data, &op->rx_stamp);
3257 +}
3258 +
3259 +
3260 +static void bcm_rx_timeout_handler(unsigned long data)
3261 +{
3262 +       struct bcm_op *op = (struct bcm_op*)data;
3263 +       struct bcm_msg_head msg_head;
3264 +
3265 +       DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
3266 +
3267 +       msg_head.opcode  = RX_TIMEOUT;
3268 +       msg_head.flags   = op->flags;
3269 +       msg_head.count   = op->count;
3270 +       msg_head.ival1   = op->ival1;
3271 +       msg_head.ival2   = op->ival2;
3272 +       msg_head.can_id  = op->can_id;
3273 +       msg_head.nframes = 0;
3274 +
3275 +       bcm_send_to_user(op, &msg_head, NULL, NULL);
3276 +
3277 +       /* no restart of the timer is done here! */
3278 +
3279 +       /* if user wants to be informed, when cyclic CAN-Messages come back */
3280 +       if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
3281 +               /* clear received can_frames to indicate 'nothing received' */
3282 +               memset(op->last_frames, 0, op->nframes * CFSIZ);
3283 +               DBG("RX_ANNOUNCE_RESTART\n");
3284 +       }
3285 +
3286 +}
3287 +
3288 +static void bcm_rx_thr_handler(unsigned long data)
3289 +{
3290 +       struct bcm_op *op = (struct bcm_op*)data;
3291 +       int i = 0;
3292 +
3293 +       op->thrtimer.expires = 0; /* mark disabled / consumed timer */
3294 +
3295 +       if (op->nframes > 1){
3296 +
3297 +               DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
3298 +                   op->can_id, op);
3299 +               /* for MUX filter we start at index 1 */
3300 +               for (i=1; i<op->nframes; i++){
3301 +                       if ((op->last_frames) &&
3302 +                           (op->last_frames[i].can_dlc & RX_THR)){
3303 +                               op->last_frames[i].can_dlc &= ~RX_THR;
3304 +                               bcm_rx_changed(op, &op->last_frames[i]);
3305 +                       }
3306 +               }
3307 +       } else {
3308 +
3309 +               DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
3310 +                   op->can_id, op);
3311 +               /* for RX_FILTER_ID and simple filter */
3312 +               if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
3313 +                       op->last_frames[0].can_dlc &= ~RX_THR;
3314 +                       bcm_rx_changed(op, &op->last_frames[0]);
3315 +               }
3316 +       }
3317 +}
3318 +
3319 +static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
3320 +                            struct can_frame *frames, struct timeval *tv)
3321 +{
3322 +       struct sk_buff *skb;
3323 +       struct can_frame *firstframe;
3324 +       struct sock *sk = op->sk;
3325 +       int datalen = head->nframes * CFSIZ;
3326 +       struct sockaddr_can *addr;
3327 +       int err;
3328 +
3329 +       skb = alloc_skb(sizeof(*head) + datalen,
3330 +                       in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
3331 +       if (!skb)
3332 +               return;
3333 +
3334 +       memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
3335 +       /* can_frames starting here */
3336 +       firstframe = (struct can_frame *) skb->tail;
3337 +
3338 +       if (tv)
3339 +               skb_set_timestamp(skb, tv); /* restore timestamp */
3340 +
3341 +       addr = (struct sockaddr_can *)skb->cb;
3342 +       memset(addr, 0, sizeof(*addr));
3343 +       addr->can_family  = AF_CAN;
3344 +       /* restore originator for recvfrom() */
3345 +       addr->can_ifindex = op->rx_ifindex;
3346 +
3347 +       if (head->nframes){
3348 +               memcpy(skb_put(skb, datalen), frames, datalen);
3349 +
3350 +               /* the BCM uses the can_dlc-element of the can_frame */
3351 +               /* structure for internal purposes. This is only     */
3352 +               /* relevant for updates that are generated by the    */
3353 +               /* BCM, where nframes is 1                           */
3354 +               if (head->nframes == 1)
3355 +                       firstframe->can_dlc &= BCM_CAN_DLC_MASK;
3356 +       }
3357 +       if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
3358 +               struct bcm_opt *bo = bcm_sk(sk);
3359 +               DBG("sock_queue_rcv_skb failed: %d\n", err);
3360 +               kfree_skb(skb);
3361 +               bo->dropped_usr_msgs++; /* don't care about overflows */
3362 +       }
3363 +}
3364 +
3365 +/**************************************************/
3366 +/* bcm_op handling: find & delete bcm_op elements */
3367 +/**************************************************/
3368 +
3369 +static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
3370 +                                 int ifindex)
3371 +{
3372 +       struct bcm_op *op;
3373 +
3374 +       list_for_each_entry(op, ops, list)
3375 +               if ((op->can_id == can_id) && (op->ifindex == ifindex))
3376 +                       return op;
3377 +
3378 +       return NULL;
3379 +}
3380 +
3381 +static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
3382 +{
3383 +       struct bcm_op *op, *n;
3384 +
3385 +       list_for_each_entry_safe(op, n, ops, list) {
3386 +               if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
3387 +                       DBG("removing rx_op %p for can_id %03X\n",
3388 +                           op, op->can_id);
3389 +
3390 +                       /* Don't care if we're bound or not (due to netdev */
3391 +                       /* problems) can_rx_unregister() is always a save  */
3392 +                       /* thing to do here.                               */
3393 +                       if (op->ifindex) {
3394 +                               struct net_device *dev =
3395 +                                       dev_get_by_index(op->ifindex);
3396 +                               if (dev) {
3397 +                                       can_rx_unregister(dev, op->can_id,
3398 +                                                         REGMASK(op->can_id),
3399 +                                                         bcm_rx_handler, op);
3400 +                                       dev_put(dev);
3401 +                               }
3402 +                       } else
3403 +                               can_rx_unregister(NULL, op->can_id,
3404 +                                                 REGMASK(op->can_id),
3405 +                                                 bcm_rx_handler, op);
3406 +
3407 +                       list_del(&op->list);
3408 +                       bcm_remove_op(op);
3409 +                       return 1; /* done */
3410 +               }
3411 +       }
3412 +
3413 +       return 0; /* not found */
3414 +}
3415 +
3416 +static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
3417 +{
3418 +       struct bcm_op *op, *n;
3419 +
3420 +       list_for_each_entry_safe(op, n, ops, list) {
3421 +               if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
3422 +                       DBG("removing rx_op %p for can_id %03X\n",
3423 +                           op, op->can_id);
3424 +                       list_del(&op->list);
3425 +                       bcm_remove_op(op);
3426 +                       return 1; /* done */
3427 +               }
3428 +       }
3429 +
3430 +       return 0; /* not found */
3431 +}
3432 +
3433 +static void bcm_remove_op(struct bcm_op *op)
3434 +{
3435 +       del_timer(&op->timer);
3436 +       del_timer(&op->thrtimer);
3437 +       if (op->frames)
3438 +               kfree(op->frames);
3439 +       if (op->last_frames)
3440 +               kfree(op->last_frames);
3441 +       kfree(op);
3442 +
3443 +       return;
3444 +}
3445 +
3446 +module_init(bcm_module_init);
3447 +module_exit(bcm_module_exit);
3448 diff -N -u -r b/net/can/Kconfig c/net/can/Kconfig
3449 --- b/net/can/Kconfig   1970-01-01 01:00:00.000000000 +0100
3450 +++ c/net/can/Kconfig   2007-03-09 13:47:12.000000000 +0100
3451 @@ -0,0 +1,71 @@
3452 +#
3453 +# Controller Area Network (CAN) network layer core configuration
3454 +#
3455 +
3456 +menuconfig CAN
3457 +       depends on NET
3458 +       tristate "CAN bus subsystem support"
3459 +       ---help---
3460 +         Controller Area Network (CAN) is a slow (up to 1Mbit/s) serial
3461 +         communications protocol was developed by Bosch at 1991 mainly for
3462 +         automotive, but now widely used in marine (NMEA2000), industrial and
3463 +         medical applications.
3464 +         More information is contained in the directory <Documentation/can/>.
3465 +
3466 +         If you want CAN support, you should say Y here and also to the
3467 +         specific driver for your controller(s) below.
3468 +
3469 +         This CAN support can also be built as a module.  If so, the module
3470 +         will be called can.ko.
3471 +
3472 +config CAN_RAW
3473 +       tristate "Raw CAN Protocol (raw access with CAN-ID filtering)"
3474 +       depends on CAN
3475 +       default N
3476 +       ---help---
3477 +         The Raw CAN protocol option offers access to the CAN bus via
3478 +         the BSD socket API. You probably want to use the raw socket in
3479 +         most cases where no higher level protocol is being used. The raw 
3480 +         socket has several filter options e.g. ID-Masking / Errorframes.
3481 +         To receive/send raw CAN messages, use AF_CAN with protocol CAN_RAW.
3482 +
3483 +config CAN_RAW_USER
3484 +       bool "Allow non-root users to access Raw CAN Protocol sockets"
3485 +       depends on CAN_RAW
3486 +       default N
3487 +       ---help---
3488 +         Say Y here if you want non-root users to be able to access CAN_RAW-
3489 +         sockets. This may lead to a security hole.
3490 +
3491 +config CAN_BCM
3492 +       tristate "Broadcast Manager CAN Protocol (with content filtering)"
3493 +       depends on CAN
3494 +       default N
3495 +       ---help---
3496 +         The Broadcast Manager offers content filtering, timeout monitoring,
3497 +         sending of RTR-frames and cyclic CAN messages without permanent user
3498 +         interaction. The BCM can be 'programmed' via the BSD socket API and
3499 +         informs you on demand e.g. only on content updates / timeouts.
3500 +         You probably want to use the bcm socket in most cases where cyclic
3501 +         CAN messages are used on the bus (e.g. in automotive environments).
3502 +         To use the Broadcast Manager, use AF_CAN with protocol CAN_BCM.
3503 +
3504 +config CAN_BCM_USER
3505 +       bool "Allow non-root users to access CAN broadcast manager sockets"
3506 +       depends on CAN_BCM
3507 +       default N
3508 +       ---help---
3509 +         Say Y here if you want non-root users to be able to access CAN_BCM-
3510 +         sockets. This may lead to a security hole.
3511 +
3512 +config CAN_DEBUG_CORE
3513 +       bool "CAN Core debugging messages"
3514 +       depends on CAN
3515 +       ---help---
3516 +         Say Y here if you want the CAN core to produce a bunch of debug
3517 +         messages to the system log.  Select this if you are having a
3518 +         problem with CAN support and want to see more of what is going on.
3519 +
3520 +
3521 +source "drivers/net/can/Kconfig"
3522 +
3523 diff -N -u -r b/net/can/Makefile c/net/can/Makefile
3524 --- b/net/can/Makefile  1970-01-01 01:00:00.000000000 +0100
3525 +++ c/net/can/Makefile  2007-03-09 13:47:12.000000000 +0100
3526 @@ -0,0 +1,12 @@
3527 +#
3528 +#  Makefile for the Linux Controller Area Network core.
3529 +#
3530 +
3531 +obj-$(CONFIG_CAN)      += can.o
3532 +can-objs               := af_can.o proc.o
3533 +
3534 +obj-$(CONFIG_CAN_RAW)  += can-raw.o
3535 +can-raw-objs           := raw.o
3536 +
3537 +obj-$(CONFIG_CAN_BCM)  += can-bcm.o
3538 +can-bcm-objs           := bcm.o
3539 diff -N -u -r b/net/can/proc.c c/net/can/proc.c
3540 --- b/net/can/proc.c    1970-01-01 01:00:00.000000000 +0100
3541 +++ c/net/can/proc.c    2007-03-09 13:47:12.000000000 +0100
3542 @@ -0,0 +1,663 @@
3543 +/*
3544 + * proc.c - procfs support for Protocol family CAN core module
3545 + *
3546 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
3547 + * All rights reserved.
3548 + *
3549 + * Redistribution and use in source and binary forms, with or without
3550 + * modification, are permitted provided that the following conditions
3551 + * are met:
3552 + * 1. Redistributions of source code must retain the above copyright
3553 + *    notice, this list of conditions, the following disclaimer and
3554 + *    the referenced file 'COPYING'.
3555 + * 2. Redistributions in binary form must reproduce the above copyright
3556 + *    notice, this list of conditions and the following disclaimer in the
3557 + *    documentation and/or other materials provided with the distribution.
3558 + * 3. Neither the name of Volkswagen nor the names of its contributors
3559 + *    may be used to endorse or promote products derived from this software
3560 + *    without specific prior written permission.
3561 + *
3562 + * Alternatively, provided that this notice is retained in full, this
3563 + * software may be distributed under the terms of the GNU General
3564 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
3565 + * file from the main directory of the linux kernel source.
3566 + *
3567 + * The provided data structures and external interfaces from this code
3568 + * are not restricted to be used by modules with a GPL compatible license.
3569 + *
3570 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3571 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3572 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3573 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3574 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3575 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3576 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3577 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3578 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3579 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3580 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
3581 + * DAMAGE.
3582 + *
3583 + * Send feedback to <socketcan-users@lists.berlios.de>
3584 + *
3585 + */
3586 +
3587 +#include <linux/module.h>
3588 +#include <linux/proc_fs.h>
3589 +#include <linux/list.h>
3590 +#include <linux/rcupdate.h>
3591 +
3592 +#include <linux/can/core.h>
3593 +#include <linux/can/version.h>
3594 +
3595 +#include "af_can.h"
3596 +
3597 +RCSID("$Id: proc.c 177 2007-03-08 11:02:43Z hartkopp $");
3598 +
3599 +/* proc filenames */
3600 +
3601 +#define CAN_PROC_VERSION     "version"
3602 +#define CAN_PROC_STATS       "stats"
3603 +#define CAN_PROC_RESET_STATS "reset_stats"
3604 +#define CAN_PROC_RCVLIST_ALL "rcvlist_all"
3605 +#define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
3606 +#define CAN_PROC_RCVLIST_INV "rcvlist_inv"
3607 +#define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
3608 +#define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
3609 +#define CAN_PROC_RCVLIST_ERR "rcvlist_err"
3610 +
3611 +static void can_init_stats(int caller);
3612 +static void can_stat_update(unsigned long data);
3613 +
3614 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
3615 +       mode_t mode, read_proc_t* read_proc, void *data);
3616 +static void can_remove_proc_readentry(const char *name);
3617 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
3618 +                              unsigned long count);
3619 +
3620 +static int can_proc_read_version(char *page, char **start, off_t off,
3621 +                                    int count, int *eof, void *data);
3622 +static int can_proc_read_stats(char *page, char **start, off_t off,
3623 +                                    int count, int *eof, void *data);
3624 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
3625 +                                    int count, int *eof, void *data);
3626 +static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
3627 +                                    int count, int *eof, void *data);
3628 +static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
3629 +                                    int count, int *eof, void *data);
3630 +static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
3631 +                                    int count, int *eof, void *data);
3632 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
3633 +                                    int count, int *eof, void *data);
3634 +static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
3635 +                                    int count, int *eof, void *data);
3636 +static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
3637 +                                    int count, int *eof, void *data);
3638 +
3639 +static struct proc_dir_entry *can_dir         = NULL;
3640 +static struct proc_dir_entry *pde_version     = NULL;
3641 +static struct proc_dir_entry *pde_stats       = NULL;
3642 +static struct proc_dir_entry *pde_reset_stats = NULL;
3643 +static struct proc_dir_entry *pde_rcvlist_all = NULL;
3644 +static struct proc_dir_entry *pde_rcvlist_fil = NULL;
3645 +static struct proc_dir_entry *pde_rcvlist_inv = NULL;
3646 +static struct proc_dir_entry *pde_rcvlist_sff = NULL;
3647 +static struct proc_dir_entry *pde_rcvlist_eff = NULL;
3648 +static struct proc_dir_entry *pde_rcvlist_err = NULL;
3649 +
3650 +struct timer_list stattimer; /* timer for statistics update */
3651 +
3652 +struct s_stats  stats; /* statistics */
3653 +struct s_pstats pstats;
3654 +
3655 +extern struct hlist_head rx_dev_list;    /* rx dispatcher structures */
3656 +extern int stats_timer;                  /* module parameter. default: on */
3657 +
3658 +/**************************************************/
3659 +/* procfs init / remove                           */
3660 +/**************************************************/
3661 +
3662 +void can_init_proc(void)
3663 +{
3664 +
3665 +       /* procfs init */
3666 +
3667 +       /* create /proc/can directory */
3668 +       can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
3669 +
3670 +       if (!can_dir) {
3671 +               printk(KERN_INFO "CAN: failed to create CAN_PROC_DIR. "
3672 +                      "CONFIG_PROC_FS missing?\n");
3673 +               return;
3674 +       }
3675 +
3676 +       can_dir->owner = THIS_MODULE;
3677 +
3678 +       /* own procfs entries from the AF_CAN core */
3679 +       pde_version     = can_create_proc_readentry(
3680 +               CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
3681 +       pde_stats       = can_create_proc_readentry(
3682 +               CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
3683 +       pde_reset_stats = can_create_proc_readentry(
3684 +               CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
3685 +       pde_rcvlist_all = can_create_proc_readentry(
3686 +               CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
3687 +       pde_rcvlist_fil = can_create_proc_readentry(
3688 +               CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
3689 +       pde_rcvlist_inv = can_create_proc_readentry(
3690 +               CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
3691 +       pde_rcvlist_sff = can_create_proc_readentry(
3692 +               CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
3693 +       pde_rcvlist_eff = can_create_proc_readentry(
3694 +               CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
3695 +       pde_rcvlist_err = can_create_proc_readentry(
3696 +               CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
3697 +
3698 +       if (stats_timer) {
3699 +               /* the statistics are updated every second (timer triggered) */
3700 +               stattimer.function = can_stat_update;
3701 +               stattimer.data = 0;
3702 +               stattimer.expires = jiffies + HZ; /* every second */
3703 +               add_timer(&stattimer); /* start statistics timer */
3704 +       }
3705 +}
3706 +
3707 +void can_remove_proc(void)
3708 +{
3709 +       /* procfs remove */
3710 +       if (pde_version)
3711 +               can_remove_proc_readentry(CAN_PROC_VERSION);
3712 +
3713 +       if (pde_stats)
3714 +               can_remove_proc_readentry(CAN_PROC_STATS);
3715 +
3716 +       if (pde_reset_stats)
3717 +               can_remove_proc_readentry(CAN_PROC_RESET_STATS);
3718 +
3719 +       if (pde_rcvlist_all)
3720 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
3721 +
3722 +       if (pde_rcvlist_fil)
3723 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
3724 +
3725 +       if (pde_rcvlist_inv)
3726 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
3727 +
3728 +       if (pde_rcvlist_sff)
3729 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
3730 +
3731 +       if (pde_rcvlist_eff)
3732 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
3733 +
3734 +       if (pde_rcvlist_err)
3735 +               can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
3736 +
3737 +       if (can_dir)
3738 +               remove_proc_entry(CAN_PROC_DIR, NULL);
3739 +}
3740 +
3741 +/**************************************************/
3742 +/* proc read functions                            */
3743 +/**************************************************/
3744 +
3745 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
3746 +                            struct net_device *dev)
3747 +{
3748 +       struct receiver *r;
3749 +       struct hlist_node *n;
3750 +
3751 +       rcu_read_lock();
3752 +       hlist_for_each_entry_rcu(r, n, rx_list, list) {
3753 +               char *fmt = r->can_id & CAN_EFF_FLAG ? /* EFF & CAN_ID_ALL */
3754 +                       "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
3755 +                       "   %-5s     %03X    %08x  %08x  %08x  %8ld  %s\n";
3756 +
3757 +               len += snprintf(page + len, PAGE_SIZE - len, fmt,
3758 +                               DNAME(dev), r->can_id, r->mask,
3759 +                               (unsigned int)r->func, (unsigned int)r->data,
3760 +                               r->matches, r->ident);
3761 +
3762 +               /* does a typical line fit into the current buffer? */
3763 +               /* 100 Bytes before end of buffer */
3764 +               if (len > PAGE_SIZE - 100) {
3765 +                       /* mark output cut off */
3766 +                       len += snprintf(page + len, PAGE_SIZE - len,
3767 +                                       "   (..)\n");
3768 +                       break;
3769 +               }
3770 +       }
3771 +       rcu_read_unlock();
3772 +
3773 +       return len;
3774 +}
3775 +
3776 +static int can_print_recv_banner(char *page, int len)
3777 +{
3778 +       /*                  can1.  00000000  00000000  00000000
3779 +                          .......          0  tp20 */
3780 +       len += snprintf(page + len, PAGE_SIZE - len,
3781 +                       "  device   can_id   can_mask  function"
3782 +                       "  userdata   matches  ident\n");
3783 +
3784 +       return len;
3785 +}
3786 +
3787 +static int can_proc_read_stats(char *page, char **start, off_t off,
3788 +                              int count, int *eof, void *data)
3789 +{
3790 +       int len = 0;
3791 +
3792 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3793 +       len += snprintf(page + len, PAGE_SIZE - len,
3794 +                       " %8ld transmitted frames (TXF)\n", stats.tx_frames);
3795 +       len += snprintf(page + len, PAGE_SIZE - len,
3796 +                       " %8ld received frames (RXF)\n", stats.rx_frames);
3797 +       len += snprintf(page + len, PAGE_SIZE - len,
3798 +                       " %8ld matched frames (RXMF)\n", stats.matches);
3799 +
3800 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3801 +
3802 +       len += snprintf(page + len, PAGE_SIZE - len,
3803 +                       " %8ld %% total match ratio (RXMR)\n",
3804 +                       stats.total_rx_match_ratio);
3805 +
3806 +       len += snprintf(page + len, PAGE_SIZE - len,
3807 +                       " %8ld frames/s total tx rate (TXR)\n",
3808 +                       stats.total_tx_rate);
3809 +       len += snprintf(page + len, PAGE_SIZE - len,
3810 +                       " %8ld frames/s total rx rate (RXR)\n",
3811 +                       stats.total_rx_rate);
3812 +
3813 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3814 +
3815 +       len += snprintf(page + len, PAGE_SIZE - len,
3816 +                       " %8ld %% current match ratio (CRXMR)\n",
3817 +                       stats.current_rx_match_ratio);
3818 +
3819 +       len += snprintf(page + len, PAGE_SIZE - len,
3820 +                       " %8ld frames/s current tx rate (CTXR)\n",
3821 +                       stats.current_tx_rate);
3822 +       len += snprintf(page + len, PAGE_SIZE - len,
3823 +                       " %8ld frames/s current rx rate (CRXR)\n",
3824 +                       stats.current_rx_rate);
3825 +
3826 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3827 +
3828 +       len += snprintf(page + len, PAGE_SIZE - len,
3829 +                       " %8ld %% max match ratio (MRXMR)\n",
3830 +                       stats.max_rx_match_ratio);
3831 +
3832 +       len += snprintf(page + len, PAGE_SIZE - len,
3833 +                       " %8ld frames/s max tx rate (MTXR)\n",
3834 +                       stats.max_tx_rate);
3835 +       len += snprintf(page + len, PAGE_SIZE - len,
3836 +                       " %8ld frames/s max rx rate (MRXR)\n",
3837 +                       stats.max_rx_rate);
3838 +
3839 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3840 +
3841 +       len += snprintf(page + len, PAGE_SIZE - len,
3842 +                       " %8ld current receive list entries (CRCV)\n",
3843 +                       pstats.rcv_entries);
3844 +       len += snprintf(page + len, PAGE_SIZE - len,
3845 +                       " %8ld maximum receive list entries (MRCV)\n",
3846 +                       pstats.rcv_entries_max);
3847 +
3848 +       if (pstats.stats_reset)
3849 +               len += snprintf(page + len, PAGE_SIZE - len,
3850 +                               "\n %8ld statistic resets (STR)\n",
3851 +                               pstats.stats_reset);
3852 +
3853 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3854 +
3855 +       *eof = 1;
3856 +       return len;
3857 +}
3858 +
3859 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
3860 +                                    int count, int *eof, void *data)
3861 +{
3862 +       int len = 0;
3863 +
3864 +       can_init_stats(1);
3865 +
3866 +       len += snprintf(page + len, PAGE_SIZE - len,
3867 +                       "CAN statistic reset #%ld done.\n",
3868 +                       pstats.stats_reset);
3869 +
3870 +       *eof = 1;
3871 +       return len;
3872 +}
3873 +
3874 +static int can_proc_read_version(char *page, char **start, off_t off,
3875 +                                int count, int *eof, void *data)
3876 +{
3877 +       int len = 0;
3878 +
3879 +       len += snprintf(page + len, PAGE_SIZE - len,
3880 +                       "%06X [ Volkswagen Group - Low Level CAN Framework"
3881 +                       " (LLCF) v%s ]\n", LLCF_VERSION_CODE, VERSION);
3882 +       *eof = 1;
3883 +       return len;
3884 +}
3885 +
3886 +static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
3887 +                                    int count, int *eof, void *data)
3888 +{
3889 +       int len = 0;
3890 +       struct dev_rcv_lists *d;
3891 +       struct hlist_node *n;
3892 +
3893 +       /* RX_ALL */
3894 +       len += snprintf(page + len, PAGE_SIZE - len,
3895 +                       "\nreceive list 'rx_all':\n");
3896 +
3897 +       /* find receive list for this device */
3898 +       rcu_read_lock();
3899 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
3900 +
3901 +               if (!hlist_empty(&d->rx_all)) {
3902 +                       len = can_print_recv_banner(page, len);
3903 +                       len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
3904 +               } else
3905 +                       len += snprintf(page + len, PAGE_SIZE - len,
3906 +                                       "  (%s: no entry)\n", DNAME(d->dev));
3907 +
3908 +               if (len > PAGE_SIZE - 100)
3909 +                       break; /* exit on end of buffer */
3910 +       }
3911 +       rcu_read_unlock();
3912 +
3913 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3914 +
3915 +       *eof = 1;
3916 +       return len;
3917 +}
3918 +
3919 +static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
3920 +                                    int count, int *eof, void *data)
3921 +{
3922 +       int len = 0;
3923 +       struct dev_rcv_lists *d;
3924 +       struct hlist_node *n;
3925 +
3926 +       /* RX_FIL */
3927 +       len += snprintf(page + len, PAGE_SIZE - len,
3928 +                       "\nreceive list 'rx_fil':\n");
3929 +
3930 +       /* find receive list for this device */
3931 +       rcu_read_lock();
3932 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
3933 +
3934 +               if (!hlist_empty(&d->rx_fil)) {
3935 +                       len = can_print_recv_banner(page, len);
3936 +                       len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
3937 +               } else
3938 +                       len += snprintf(page + len, PAGE_SIZE - len,
3939 +                                       "  (%s: no entry)\n", DNAME(d->dev));
3940 +
3941 +               if (len > PAGE_SIZE - 100)
3942 +                       break; /* exit on end of buffer */
3943 +       }
3944 +       rcu_read_unlock();
3945 +
3946 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3947 +
3948 +       *eof = 1;
3949 +       return len;
3950 +}
3951 +
3952 +static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
3953 +                                    int count, int *eof, void *data)
3954 +{
3955 +       int len = 0;
3956 +       struct dev_rcv_lists *d;
3957 +       struct hlist_node *n;
3958 +
3959 +       /* RX_INV */
3960 +       len += snprintf(page + len, PAGE_SIZE - len,
3961 +                       "\nreceive list 'rx_inv':\n");
3962 +
3963 +       /* find receive list for this device */
3964 +       rcu_read_lock();
3965 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
3966 +
3967 +               if (!hlist_empty(&d->rx_inv)) {
3968 +                       len = can_print_recv_banner(page, len);
3969 +                       len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
3970 +               } else
3971 +                       len += snprintf(page + len, PAGE_SIZE - len,
3972 +                                       "  (%s: no entry)\n", DNAME(d->dev));
3973 +
3974 +               if (len > PAGE_SIZE - 100)
3975 +                       break; /* exit on end of buffer */
3976 +       }
3977 +       rcu_read_unlock();
3978 +
3979 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
3980 +
3981 +       *eof = 1;
3982 +       return len;
3983 +}
3984 +
3985 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
3986 +                                    int count, int *eof, void *data)
3987 +{
3988 +       int len = 0;
3989 +       struct dev_rcv_lists *d;
3990 +       struct hlist_node *n;
3991 +
3992 +       /* RX_SFF */
3993 +       len += snprintf(page + len, PAGE_SIZE - len,
3994 +                       "\nreceive list 'rx_sff':\n");
3995 +
3996 +       /* find receive list for this device */
3997 +       rcu_read_lock();
3998 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
3999 +               int i, all_empty = 1;
4000 +               /* check wether at least one list is non-empty */
4001 +               for (i = 0; i < 0x800; i++)
4002 +                       if (!hlist_empty(&d->rx_sff[i])) {
4003 +                               all_empty = 0;
4004 +                               break;
4005 +                       }
4006 +
4007 +               if (!all_empty) {
4008 +                       len = can_print_recv_banner(page, len);
4009 +                       for (i = 0; i < 0x800; i++) {
4010 +                               if (!hlist_empty(&d->rx_sff[i]) &&
4011 +                                   len < PAGE_SIZE - 100)
4012 +                                       len = can_print_rcvlist(page, len,
4013 +                                                               &d->rx_sff[i],
4014 +                                                               d->dev);
4015 +                       }
4016 +               } else
4017 +                       len += snprintf(page + len, PAGE_SIZE - len,
4018 +                                       "  (%s: no entry)\n", DNAME(d->dev));
4019 +
4020 +               if (len > PAGE_SIZE - 100)
4021 +                       break; /* exit on end of buffer */
4022 +       }
4023 +       rcu_read_unlock();
4024 +
4025 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
4026 +
4027 +       *eof = 1;
4028 +       return len;
4029 +}
4030 +
4031 +static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
4032 +                                    int count, int *eof, void *data)
4033 +{
4034 +       int len = 0;
4035 +       struct dev_rcv_lists *d;
4036 +       struct hlist_node *n;
4037 +
4038 +       /* RX_EFF */
4039 +       len += snprintf(page + len, PAGE_SIZE - len,
4040 +                       "\nreceive list 'rx_eff':\n");
4041 +
4042 +       /* find receive list for this device */
4043 +       rcu_read_lock();
4044 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
4045 +
4046 +               if (!hlist_empty(&d->rx_eff)) {
4047 +                       len = can_print_recv_banner(page, len);
4048 +                       len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
4049 +               } else
4050 +                       len += snprintf(page + len, PAGE_SIZE - len,
4051 +                                       "  (%s: no entry)\n", DNAME(d->dev));
4052 +
4053 +               if (len > PAGE_SIZE - 100)
4054 +                       break; /* exit on end of buffer */
4055 +       }
4056 +       rcu_read_unlock();
4057 +
4058 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
4059 +
4060 +       *eof = 1;
4061 +       return len;
4062 +}
4063 +
4064 +static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
4065 +                                    int count, int *eof, void *data)
4066 +{
4067 +       int len = 0;
4068 +       struct dev_rcv_lists *d;
4069 +       struct hlist_node *n;
4070 +
4071 +       /* RX_ERR */
4072 +       len += snprintf(page + len, PAGE_SIZE - len,
4073 +                       "\nreceive list 'rx_err':\n");
4074 +
4075 +       /* find receive list for this device */
4076 +       rcu_read_lock();
4077 +       hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
4078 +
4079 +               if (!hlist_empty(&d->rx_err)) {
4080 +                       len = can_print_recv_banner(page, len);
4081 +                       len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
4082 +               } else
4083 +                       len += snprintf(page + len, PAGE_SIZE - len,
4084 +                                       "  (%s: no entry)\n", DNAME(d->dev));
4085 +
4086 +               if (len > PAGE_SIZE - 100)
4087 +                       break; /* exit on end of buffer */
4088 +       }
4089 +       rcu_read_unlock();
4090 +
4091 +       len += snprintf(page + len, PAGE_SIZE - len, "\n");
4092 +
4093 +       *eof = 1;
4094 +       return len;
4095 +}
4096 +
4097 +/**************************************************/
4098 +/* proc utility functions                         */
4099 +/**************************************************/
4100 +
4101 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
4102 +                                                       mode_t mode,
4103 +                                                       read_proc_t* read_proc,
4104 +                                                       void *data)
4105 +{
4106 +       if (can_dir)
4107 +               return create_proc_read_entry(name, mode, can_dir, read_proc,
4108 +                                             data);
4109 +       else
4110 +               return NULL;
4111 +}
4112 +
4113 +static void can_remove_proc_readentry(const char *name)
4114 +{
4115 +       if (can_dir)
4116 +               remove_proc_entry(name, can_dir);
4117 +}
4118 +
4119 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
4120 +                              unsigned long count)
4121 +{
4122 +       unsigned long ret = 0;
4123 +
4124 +       if (oldjif == newjif)
4125 +               return 0;
4126 +
4127 +       /* see can_rcv() - this should NEVER happen! */
4128 +       if (count > (ULONG_MAX / HZ)) {
4129 +               printk(KERN_ERR "CAN: calc_rate: count exceeded! %ld\n",
4130 +                      count);
4131 +               return 99999999;
4132 +       }
4133 +
4134 +       ret = (count * HZ) / (newjif - oldjif);
4135 +
4136 +       return ret;
4137 +}
4138 +
4139 +/**************************************************/
4140 +/* af_can statistics stuff                        */
4141 +/**************************************************/
4142 +
4143 +static void can_init_stats(int caller)
4144 +{
4145 +       memset(&stats, 0, sizeof(stats));
4146 +       stats.jiffies_init  = jiffies;
4147 +       pstats.stats_reset++;
4148 +}
4149 +
4150 +static void can_stat_update(unsigned long data)
4151 +{
4152 +       unsigned long j = jiffies; /* snapshot */
4153 +
4154 +       //DBG("CAN: can_stat_update() jiffies = %ld\n", j);
4155 +
4156 +       if (j < stats.jiffies_init) /* jiffies overflow */
4157 +               can_init_stats(2);
4158 +
4159 +       /* stats.rx_frames is the definitively max. statistic value */
4160 +
4161 +       /* prevent overflow in calc_rate() */
4162 +       if (stats.rx_frames > (ULONG_MAX / HZ))
4163 +               can_init_stats(3); /* restart */
4164 +
4165 +       /* matches overflow - very improbable */
4166 +       if (stats.matches > (ULONG_MAX / 100))
4167 +               can_init_stats(4);
4168 +
4169 +       /* calc total values */
4170 +       if (stats.rx_frames)
4171 +               stats.total_rx_match_ratio = (stats.matches * 100) / 
4172 +                                               stats.rx_frames;
4173 +
4174 +       stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
4175 +                                       stats.tx_frames);
4176 +       stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
4177 +                                       stats.rx_frames);
4178 +
4179 +       /* calc current values */
4180 +       if (stats.rx_frames_delta)
4181 +               stats.current_rx_match_ratio =
4182 +                       (stats.matches_delta * 100) / stats.rx_frames_delta;
4183 +
4184 +       stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
4185 +       stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
4186 +
4187 +       /* check / update maximum values */
4188 +       if (stats.max_tx_rate < stats.current_tx_rate)
4189 +               stats.max_tx_rate = stats.current_tx_rate;
4190 +
4191 +       if (stats.max_rx_rate < stats.current_rx_rate)
4192 +               stats.max_rx_rate = stats.current_rx_rate;
4193 +
4194 +       if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
4195 +               stats.max_rx_match_ratio = stats.current_rx_match_ratio;
4196 +
4197 +       /* clear values for 'current rate' calculation */
4198 +       stats.tx_frames_delta = 0;
4199 +       stats.rx_frames_delta = 0;
4200 +       stats.matches_delta   = 0;
4201 +
4202 +       /* restart timer */
4203 +       stattimer.expires = jiffies + HZ; /* every second */
4204 +       add_timer(&stattimer);
4205 +}
4206 diff -N -u -r b/net/can/raw.c c/net/can/raw.c
4207 --- b/net/can/raw.c     1970-01-01 01:00:00.000000000 +0100
4208 +++ c/net/can/raw.c     2007-03-09 13:47:12.000000000 +0100
4209 @@ -0,0 +1,738 @@
4210 +/*
4211 + * raw.c - Raw sockets for protocol family CAN
4212 + *
4213 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
4214 + * All rights reserved.
4215 + *
4216 + * Redistribution and use in source and binary forms, with or without
4217 + * modification, are permitted provided that the following conditions
4218 + * are met:
4219 + * 1. Redistributions of source code must retain the above copyright
4220 + *    notice, this list of conditions, the following disclaimer and
4221 + *    the referenced file 'COPYING'.
4222 + * 2. Redistributions in binary form must reproduce the above copyright
4223 + *    notice, this list of conditions and the following disclaimer in the
4224 + *    documentation and/or other materials provided with the distribution.
4225 + * 3. Neither the name of Volkswagen nor the names of its contributors
4226 + *    may be used to endorse or promote products derived from this software
4227 + *    without specific prior written permission.
4228 + *
4229 + * Alternatively, provided that this notice is retained in full, this
4230 + * software may be distributed under the terms of the GNU General
4231 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
4232 + * file from the main directory of the linux kernel source.
4233 + *
4234 + * The provided data structures and external interfaces from this code
4235 + * are not restricted to be used by modules with a GPL compatible license.
4236 + *
4237 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4238 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4239 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4240 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4241 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4242 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4243 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4244 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4245 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4246 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4247 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
4248 + * DAMAGE.
4249 + *
4250 + * Send feedback to <socketcan-users@lists.berlios.de>
4251 + *
4252 + */
4253 +
4254 +#include <linux/autoconf.h>
4255 +#include <linux/module.h>
4256 +#include <linux/version.h>
4257 +#include <linux/init.h>
4258 +#include <linux/net.h>
4259 +#include <linux/netdevice.h>
4260 +#include <linux/uio.h>
4261 +#include <linux/poll.h>
4262 +#include <net/sock.h>
4263 +
4264 +#include <linux/can.h>
4265 +#include <linux/can/error.h>
4266 +#include <linux/can/core.h>
4267 +#include <linux/can/raw.h>
4268 +#include <linux/can/version.h>
4269 +
4270 +RCSID("$Id: raw.c 177 2007-03-08 11:02:43Z hartkopp $");
4271 +
4272 +#define IDENT "raw"
4273 +static __initdata const char banner[] = KERN_INFO "CAN: raw socket protocol"
4274 +                                       " " VERSION "\n"; 
4275 +
4276 +MODULE_DESCRIPTION("PF_CAN raw sockets");
4277 +MODULE_LICENSE("Dual BSD/GPL");
4278 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
4279 +
4280 +#ifdef CONFIG_CAN_DEBUG_CORE
4281 +static int debug = 0;
4282 +module_param(debug, int, S_IRUGO);
4283 +#define DBG(args...)       (debug & 1 ? \
4284 +                              (printk(KERN_DEBUG "RAW %s: ", __func__), \
4285 +                               printk(args)) : 0)
4286 +#define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
4287 +#else
4288 +#define DBG(args...)
4289 +#define DBG_SKB(skb)
4290 +#endif
4291 +
4292 +static int raw_init(struct sock *sk);
4293 +static int raw_release(struct socket *sock);
4294 +static int raw_bind   (struct socket *sock, struct sockaddr *uaddr, int len);
4295 +static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
4296 +                      int *len, int peer);
4297 +static unsigned int raw_poll(struct file *file, struct socket *sock,
4298 +                            poll_table *wait);
4299 +static int raw_setsockopt(struct socket *sock, int level, int optname,
4300 +                         char __user *optval, int optlen);
4301 +static int raw_getsockopt(struct socket *sock, int level, int optname,
4302 +                         char __user *optval, int __user *optlen);
4303 +static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
4304 +                      struct msghdr *msg, size_t size);
4305 +static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
4306 +                      struct msghdr *msg, size_t size, int flags);
4307 +static void raw_rcv(struct sk_buff *skb, void *data);
4308 +static void raw_notifier(unsigned long msg, void *data);
4309 +
4310 +static void raw_add_filters(struct net_device *dev, struct sock *sk);
4311 +static void raw_remove_filters(struct net_device *dev, struct sock *sk);
4312 +
4313 +
4314 +static struct proto_ops raw_ops = {
4315 +       .family        = PF_CAN,
4316 +       .release       = raw_release,
4317 +       .bind          = raw_bind,
4318 +       .connect       = sock_no_connect,
4319 +       .socketpair    = sock_no_socketpair,
4320 +       .accept        = sock_no_accept,
4321 +       .getname       = raw_getname,
4322 +       .poll          = raw_poll,
4323 +       .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
4324 +       .listen        = sock_no_listen,
4325 +       .shutdown      = sock_no_shutdown,
4326 +       .setsockopt    = raw_setsockopt,
4327 +       .getsockopt    = raw_getsockopt,
4328 +       .sendmsg       = raw_sendmsg,
4329 +       .recvmsg       = raw_recvmsg,
4330 +       .mmap          = sock_no_mmap,
4331 +       .sendpage      = sock_no_sendpage,
4332 +};
4333 +
4334 +
4335 +/* A raw socket has a list of can_filters attached to it, each receiving
4336 +   the CAN frames matching that filter.  If the filter list is empty,
4337 +   no CAN frames will be received by the socket.  The default after
4338 +   opening the socket, is to have one filter which receives all frames.
4339 +   The filter list is allocated dynamically with the exception of the
4340 +   list containing only one item.  This common case is optimized by
4341 +   storing the single filter in dfilter, to avoid using dynamic memory.
4342 +*/
4343 +
4344 +struct raw_opt {
4345 +       int bound;
4346 +       int ifindex;
4347 +       int loopback;
4348 +       int recv_own_msgs;
4349 +       int count;                 /* number of active filters */
4350 +       struct can_filter dfilter; /* default/single filter */
4351 +       struct can_filter *filter; /* pointer to filter(s) */
4352 +       can_err_mask_t err_mask;
4353 +};
4354 +
4355 +#ifdef CONFIG_CAN_RAW_USER
4356 +#define RAW_CAP (-1)
4357 +#else
4358 +#define RAW_CAP CAP_NET_RAW
4359 +#endif
4360 +
4361 +#undef CAN_RAW_SUPPORT_REBIND /* use bind on already bound socket */
4362 +
4363 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
4364 +struct raw_sock {
4365 +       struct sock    sk;
4366 +       struct raw_opt opt;
4367 +};
4368 +
4369 +#define raw_sk(sk) (&((struct raw_sock *)(sk))->opt)
4370 +
4371 +static struct proto raw_proto = {
4372 +       .name       = "CAN_RAW",
4373 +       .owner      = THIS_MODULE,
4374 +       .obj_size   = sizeof(struct raw_sock),
4375 +       .init       = raw_init,
4376 +};
4377 +
4378 +static struct can_proto raw_can_proto = {
4379 +       .type       = SOCK_RAW,
4380 +       .protocol   = CAN_RAW,
4381 +       .capability = RAW_CAP,
4382 +       .ops        = &raw_ops,
4383 +       .prot       = &raw_proto,
4384 +};
4385 +#else
4386 +#define raw_sk(sk) ((struct raw_opt *)(sk)->sk_protinfo)
4387 +
4388 +static struct can_proto raw_can_proto = {
4389 +       .type       = SOCK_RAW,
4390 +       .protocol   = CAN_RAW,
4391 +       .capability = RAW_CAP,
4392 +       .ops        = &raw_ops,
4393 +       .owner      = THIS_MODULE,
4394 +       .obj_size   = sizeof(struct raw_opt),
4395 +       .init       = raw_init,
4396 +};
4397 +#endif
4398 +
4399 +#define MASK_ALL 0
4400 +
4401 +static __init int raw_module_init(void)
4402 +{
4403 +       printk(banner);
4404 +
4405 +       can_proto_register(&raw_can_proto);
4406 +       return 0;
4407 +}
4408 +
4409 +static __exit void raw_module_exit(void)
4410 +{
4411 +       can_proto_unregister(&raw_can_proto);
4412 +}
4413 +
4414 +static int raw_init(struct sock *sk)
4415 +{
4416 +       struct raw_opt *ro = raw_sk(sk);
4417 +
4418 +       ro->bound            = 0;
4419 +
4420 +       /* set default filter to single entry dfilter */
4421 +       ro->dfilter.can_id   = 0;
4422 +       ro->dfilter.can_mask = MASK_ALL;
4423 +       ro->filter           = &ro->dfilter;
4424 +       ro->count            = 1;
4425 +
4426 +       /* set default loopback behaviour */
4427 +       ro->loopback         = 1;
4428 +       ro->recv_own_msgs    = 0;
4429 +
4430 +       return 0;
4431 +}
4432 +
4433 +static int raw_release(struct socket *sock)
4434 +{
4435 +       struct sock *sk = sock->sk;
4436 +       struct raw_opt *ro = raw_sk(sk);
4437 +       struct net_device *dev = NULL;
4438 +
4439 +       DBG("socket %p, sk %p, refcnt %d\n", sock, sk,
4440 +           atomic_read(&sk->sk_refcnt));
4441 +
4442 +       if (ro->bound && ro->ifindex)
4443 +               dev = dev_get_by_index(ro->ifindex);
4444 +
4445 +       /* remove current filters & unregister */
4446 +       if (ro->bound)
4447 +               raw_remove_filters(dev, sk);
4448 +       if (ro->count > 1)
4449 +               kfree(ro->filter);
4450 +
4451 +       /* remove current error mask */
4452 +       if (ro->err_mask && ro->bound)
4453 +               can_rx_unregister(dev, 0, ro->err_mask | CAN_ERR_FLAG,
4454 +                                 raw_rcv, sk);
4455 +
4456 +       if (dev) {
4457 +               can_dev_unregister(dev, raw_notifier, sk);
4458 +               dev_put(dev);
4459 +       }
4460 +
4461 +       sock_put(sk);
4462 +
4463 +       return 0;
4464 +}
4465 +
4466 +static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
4467 +{
4468 +       struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
4469 +       struct sock *sk = sock->sk;
4470 +       struct raw_opt *ro = raw_sk(sk);
4471 +       struct net_device *dev;
4472 +
4473 +       DBG("socket %p to device %d\n", sock, addr->can_ifindex);
4474 +
4475 +       if (len < sizeof(*addr))
4476 +               return -EINVAL;
4477 +
4478 +       if (ro->bound) {
4479 +#ifdef CAN_RAW_SUPPORT_REBIND
4480 +               /* remove current bindings / notifier */
4481 +               if (ro->ifindex) {
4482 +                       dev = dev_get_by_index(ro->ifindex);
4483 +                       if (!dev) {
4484 +                               DBG("could not find device %d\n",
4485 +                                   addr->can_ifindex);
4486 +                               return -ENODEV;
4487 +                       }
4488 +                       if (!(dev->flags & IFF_UP)) {
4489 +                               sk->sk_err = ENETDOWN;
4490 +                               if (!sock_flag(sk, SOCK_DEAD))
4491 +                                       sk->sk_error_report(sk);
4492 +                               goto out;
4493 +                       }
4494 +                       can_dev_unregister(dev, raw_notifier, sk);
4495 +               } else
4496 +                       dev = NULL;
4497 +
4498 +               /* unregister current filters for this device */
4499 +               raw_remove_filters(dev, sk);
4500 +
4501 +               if (dev)
4502 +                       dev_put(dev);
4503 +
4504 +               ro->bound = 0;
4505 +#else
4506 +               return -EINVAL;
4507 +#endif
4508 +       }
4509 +
4510 +       if (addr->can_ifindex) {
4511 +               dev = dev_get_by_index(addr->can_ifindex);
4512 +               if (!dev) {
4513 +                       DBG("could not find device %d\n", addr->can_ifindex);
4514 +                       return -ENODEV;
4515 +               }
4516 +               if (!(dev->flags & IFF_UP)) {
4517 +                       sk->sk_err = ENETDOWN;
4518 +                       if (!sock_flag(sk, SOCK_DEAD))
4519 +                               sk->sk_error_report(sk);
4520 +                       goto out;
4521 +               }
4522 +               can_dev_register(dev, raw_notifier, sk);
4523 +       } else
4524 +               dev = NULL;
4525 +
4526 +       ro->ifindex = addr->can_ifindex;
4527 +
4528 +       raw_add_filters(dev, sk); /* filters set by default/setsockopt */
4529 +
4530 +       if (ro->err_mask) /* error frame filter set by setsockopt */
4531 +               can_rx_register(dev, 0, ro->err_mask | CAN_ERR_FLAG,
4532 +                               raw_rcv, sk, IDENT);
4533 +
4534 +       ro->bound = 1;
4535 +
4536 + out:
4537 +       if (dev)
4538 +               dev_put(dev);
4539 +
4540 +       return 0;
4541 +}
4542 +
4543 +static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
4544 +                      int *len, int peer)
4545 +{
4546 +       struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
4547 +       struct sock *sk = sock->sk;
4548 +       struct raw_opt *ro = raw_sk(sk);
4549 +
4550 +       if (peer)
4551 +               return -EOPNOTSUPP;
4552 +
4553 +       addr->can_family  = AF_CAN;
4554 +       addr->can_ifindex = ro->ifindex;
4555 +       *len = sizeof(*addr);
4556 +
4557 +       return 0;
4558 +}
4559 +
4560 +static unsigned int raw_poll(struct file *file, struct socket *sock,
4561 +                            poll_table *wait)
4562 +{
4563 +       unsigned int mask = 0;
4564 +
4565 +       DBG("socket %p\n", sock);
4566 +
4567 +       mask = datagram_poll(file, sock, wait);
4568 +       return mask;
4569 +}
4570 +
4571 +static int raw_setsockopt(struct socket *sock, int level, int optname,
4572 +                         char __user *optval, int optlen)
4573 +{
4574 +       struct sock *sk = sock->sk;
4575 +       struct raw_opt *ro = raw_sk(sk);
4576 +       struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
4577 +       struct can_filter sfilter;         /* single filter */
4578 +       struct net_device *dev = NULL;
4579 +       can_err_mask_t err_mask = 0;
4580 +       int count = 0;
4581 +       int err;
4582 +
4583 +       if (level != SOL_CAN_RAW)
4584 +               return -EINVAL;
4585 +
4586 +       switch (optname) {
4587 +       case CAN_RAW_FILTER:
4588 +               if (optlen % sizeof(struct can_filter) != 0)
4589 +                       return -EINVAL;
4590 +
4591 +               count = optlen / sizeof(struct can_filter);
4592 +
4593 +               if (count > 1) { /* does not fit into dfilter */
4594 +                       if (!(filter = kmalloc(optlen, GFP_KERNEL)))
4595 +                               return -ENOMEM;
4596 +                       if ((err = copy_from_user(filter, optval, optlen))) {
4597 +                               kfree(filter);
4598 +                               return err;
4599 +                       }
4600 +               } else if (count == 1) {
4601 +                       if ((err = copy_from_user(&sfilter, optval, optlen)))
4602 +                               return err;
4603 +               }
4604 +
4605 +               if (ro->bound && ro->ifindex)
4606 +                       dev = dev_get_by_index(ro->ifindex);
4607 +
4608 +               /* remove current filters & unregister */
4609 +               if (ro->bound)
4610 +                       raw_remove_filters(dev, sk);
4611 +               if (ro->count > 1)
4612 +                       kfree(ro->filter);
4613 +
4614 +               if (count == 1) { /* copy data for single filter */
4615 +                       ro->dfilter = sfilter;
4616 +                       filter = &ro->dfilter;
4617 +               }
4618 +
4619 +               /* add new filters & register */
4620 +               ro->filter = filter;
4621 +               ro->count  = count;
4622 +               if (ro->bound)
4623 +                       raw_add_filters(dev, sk);
4624 +
4625 +               if (dev)
4626 +                       dev_put(dev);
4627 +
4628 +               break;
4629 +
4630 +       case CAN_RAW_ERR_FILTER:
4631 +               if (optlen != sizeof(err_mask))
4632 +                       return -EINVAL;
4633 +               if ((err = copy_from_user(&err_mask, optval, optlen)))
4634 +                       return err;
4635 +
4636 +               err_mask &= CAN_ERR_MASK;
4637 +
4638 +               if (ro->bound && ro->ifindex)
4639 +                       dev = dev_get_by_index(ro->ifindex);
4640 +
4641 +               /* remove current error mask */
4642 +               if (ro->err_mask && ro->bound)
4643 +                       can_rx_unregister(dev, 0, ro->err_mask | CAN_ERR_FLAG,
4644 +                                         raw_rcv, sk);
4645 +
4646 +               /* add new error mask */
4647 +               ro->err_mask = err_mask;
4648 +               if (ro->err_mask && ro->bound)
4649 +                       can_rx_register(dev, 0, ro->err_mask | CAN_ERR_FLAG,
4650 +                                       raw_rcv, sk, IDENT);
4651 +
4652 +               if (dev)
4653 +                       dev_put(dev);
4654 +
4655 +               break;
4656 +
4657 +       case CAN_RAW_LOOPBACK:
4658 +               if (optlen != sizeof(ro->loopback))
4659 +                       return -EINVAL;
4660 +               if ((err = copy_from_user(&ro->loopback, optval, optlen)))
4661 +                       return err;
4662 +               break;
4663 +
4664 +       case CAN_RAW_RECV_OWN_MSGS:
4665 +               if (optlen != sizeof(ro->recv_own_msgs))
4666 +                       return -EINVAL;
4667 +               if ((err = copy_from_user(&ro->recv_own_msgs, optval, optlen)))
4668 +                       return err;
4669 +               break;
4670 +
4671 +       default:
4672 +               return -ENOPROTOOPT;
4673 +       }
4674 +       return 0;
4675 +}
4676 +
4677 +static int raw_getsockopt(struct socket *sock, int level, int optname,
4678 +                         char __user *optval, int __user *optlen)
4679 +{
4680 +       struct sock *sk = sock->sk;
4681 +       struct raw_opt *ro = raw_sk(sk);
4682 +       struct can_filter *filter = ro->filter;
4683 +       int count = ro->count;
4684 +       int len;
4685 +
4686 +       if (level != SOL_CAN_RAW)
4687 +               return -EINVAL;
4688 +
4689 +       switch (optname) {
4690 +       case CAN_RAW_FILTER:
4691 +               if (get_user(len, optlen))
4692 +                       return -EFAULT;
4693 +
4694 +               if (count && filter) {
4695 +                       int filter_size = count * sizeof(struct can_filter);
4696 +                       if (len < filter_size)
4697 +                               return -EINVAL;
4698 +                       if (len > filter_size)
4699 +                               len = filter_size;
4700 +                       if (copy_to_user(optval, filter, len))
4701 +                               return -EFAULT;
4702 +               } else
4703 +                       len = 0;
4704 +
4705 +               if (put_user(len, optlen))
4706 +                       return -EFAULT;
4707 +
4708 +               break;
4709 +
4710 +       case CAN_RAW_ERR_FILTER:
4711 +               if (get_user(len, optlen))
4712 +                       return -EFAULT;
4713 +
4714 +               if (len < sizeof(can_err_mask_t))
4715 +                       return -EINVAL;
4716 +
4717 +               if (len > sizeof(can_err_mask_t))
4718 +                       len = sizeof(can_err_mask_t);
4719 +
4720 +               if (copy_to_user(optval, &ro->err_mask, len))
4721 +                       return -EFAULT;
4722 +
4723 +               if (put_user(len, optlen))
4724 +                       return -EFAULT;
4725 +
4726 +               break;
4727 +
4728 +       case CAN_RAW_LOOPBACK:
4729 +               if (get_user(len, optlen))
4730 +                       return -EFAULT;
4731 +
4732 +               if (len < sizeof(int))
4733 +                       return -EINVAL;
4734 +
4735 +               if (len > sizeof(int))
4736 +                       len = sizeof(int);
4737 +
4738 +               if (copy_to_user(optval, &ro->loopback, len))
4739 +                       return -EFAULT;
4740 +
4741 +               if (put_user(len, optlen))
4742 +                       return -EFAULT;
4743 +
4744 +               break;
4745 +
4746 +       case CAN_RAW_RECV_OWN_MSGS:
4747 +               if (get_user(len, optlen))
4748 +                       return -EFAULT;
4749 +
4750 +               if (len < sizeof(int))
4751 +                       return -EINVAL;
4752 +
4753 +               if (len > sizeof(int))
4754 +                       len = sizeof(int);
4755 +
4756 +               if (copy_to_user(optval, &ro->recv_own_msgs, len))
4757 +                       return -EFAULT;
4758 +
4759 +               if (put_user(len, optlen))
4760 +                       return -EFAULT;
4761 +
4762 +               break;
4763 +
4764 +       default:
4765 +               return -ENOPROTOOPT;
4766 +       }
4767 +       return 0;
4768 +}
4769 +
4770 +static void raw_add_filters(struct net_device *dev, struct sock *sk)
4771 +{
4772 +       struct raw_opt *ro = raw_sk(sk);
4773 +       struct can_filter *filter = ro->filter;
4774 +       int i;
4775 +
4776 +       for (i = 0; i < ro->count; i++) {
4777 +               can_rx_register(dev, filter[i].can_id, filter[i].can_mask,
4778 +                               raw_rcv, sk, IDENT);
4779 +               DBG("filter can_id %08X, can_mask %08X%s, sk %p\n",
4780 +                   filter[i].can_id, filter[i].can_mask,
4781 +                   filter[i].can_id & CAN_INV_FILTER ? " (inv)" : "", sk);
4782 +       }
4783 +}
4784 +
4785 +static void raw_remove_filters(struct net_device *dev, struct sock *sk)
4786 +{
4787 +       struct raw_opt *ro = raw_sk(sk);
4788 +       struct can_filter *filter = ro->filter;
4789 +       int i;
4790 +
4791 +       for (i = 0; i < ro->count; i++) {
4792 +               can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
4793 +                                 raw_rcv, sk);
4794 +               DBG("filter can_id %08X, can_mask %08X%s, sk %p\n",
4795 +                   filter[i].can_id, filter[i].can_mask,
4796 +                   filter[i].can_id & CAN_INV_FILTER ? " (inv)" : "", sk);
4797 +       }
4798 +}
4799 +
4800 +static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
4801 +                      struct msghdr *msg, size_t size)
4802 +{
4803 +       struct sock *sk = sock->sk;
4804 +       struct raw_opt *ro = raw_sk(sk);
4805 +       struct sk_buff *skb;
4806 +       struct net_device *dev;
4807 +       int ifindex;
4808 +       int err;
4809 +
4810 +       DBG("socket %p, sk %p\n", sock, sk);
4811 +
4812 +       if (msg->msg_name) {
4813 +               struct sockaddr_can *addr =
4814 +                       (struct sockaddr_can *)msg->msg_name;
4815 +               if (addr->can_family != AF_CAN)
4816 +                       return -EINVAL;
4817 +               ifindex = addr->can_ifindex;
4818 +       } else
4819 +               ifindex = ro->ifindex;
4820 +
4821 +       if (!(dev = dev_get_by_index(ifindex))) {
4822 +               DBG("device %d not found\n", ifindex);
4823 +               return -ENXIO;
4824 +       }
4825 +
4826 +       if (!(skb = alloc_skb(size, GFP_KERNEL))) {
4827 +               dev_put(dev);
4828 +               return -ENOMEM;
4829 +       }
4830 +
4831 +       if ((err = memcpy_fromiovec(skb_put(skb, size),
4832 +                                   msg->msg_iov, size)) < 0) {
4833 +               kfree_skb(skb);
4834 +               dev_put(dev);
4835 +               return err;
4836 +       }
4837 +       skb->dev = dev;
4838 +       skb->sk  = sk;
4839 +
4840 +       DBG("sending skbuff to interface %d\n", ifindex);
4841 +       DBG_SKB(skb);
4842 +
4843 +       err = can_send(skb, ro->loopback);
4844 +
4845 +       dev_put(dev);
4846 +
4847 +       if (err)
4848 +               return err;
4849 +
4850 +       return size;
4851 +}
4852 +
4853 +static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
4854 +                      struct msghdr *msg, size_t size, int flags)
4855 +{
4856 +       struct sock *sk = sock->sk;
4857 +       struct sk_buff *skb;
4858 +       int error = 0;
4859 +       int noblock;
4860 +
4861 +       DBG("socket %p, sk %p\n", sock, sk);
4862 +
4863 +       noblock =  flags & MSG_DONTWAIT;
4864 +       flags   &= ~MSG_DONTWAIT;
4865 +
4866 +       if (!(skb = skb_recv_datagram(sk, flags, noblock, &error)))
4867 +               return error;
4868 +
4869 +       DBG("delivering skbuff %p\n", skb);
4870 +       DBG_SKB(skb);
4871 +
4872 +       if (size < skb->len)
4873 +               msg->msg_flags |= MSG_TRUNC;
4874 +       else
4875 +               size = skb->len;
4876 +
4877 +       if ((error = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
4878 +               skb_free_datagram(sk, skb);
4879 +               return error;
4880 +       }
4881 +
4882 +       sock_recv_timestamp(msg, sk, skb);
4883 +
4884 +       if (msg->msg_name) {
4885 +               msg->msg_namelen = sizeof(struct sockaddr_can);
4886 +               memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
4887 +       }
4888 +
4889 +       DBG("freeing sock %p, skbuff %p\n", sk, skb);
4890 +       skb_free_datagram(sk, skb);
4891 +
4892 +       return size;
4893 +}
4894 +
4895 +static void raw_rcv(struct sk_buff *skb, void *data)
4896 +{
4897 +       struct sock *sk = (struct sock*)data;
4898 +       struct raw_opt *ro = raw_sk(sk);
4899 +       struct sockaddr_can *addr;
4900 +       int error;
4901 +
4902 +       DBG("received skbuff %p, sk %p\n", skb, sk);
4903 +       DBG_SKB(skb);
4904 +
4905 +       if (!ro->recv_own_msgs) {
4906 +               if (*(struct sock **)skb->cb == sk) { /* tx sock reference */
4907 +                       DBG("trashed own tx msg\n");
4908 +                       kfree_skb(skb);
4909 +                       return;
4910 +               }
4911 +       }
4912 +
4913 +       addr = (struct sockaddr_can *)skb->cb;
4914 +       memset(addr, 0, sizeof(*addr));
4915 +       addr->can_family  = AF_CAN;
4916 +       addr->can_ifindex = skb->dev->ifindex;
4917 +
4918 +       if ((error = sock_queue_rcv_skb(sk, skb)) < 0) {
4919 +               DBG("sock_queue_rcv_skb failed: %d\n", error);
4920 +               DBG("freeing skbuff %p\n", skb);
4921 +               kfree_skb(skb);
4922 +       }
4923 +}
4924 +
4925 +static void raw_notifier(unsigned long msg, void *data)
4926 +{
4927 +       struct sock *sk = (struct sock *)data;
4928 +       struct raw_opt *ro = raw_sk(sk);
4929 +
4930 +       DBG("called for sock %p\n", sk);
4931 +
4932 +       switch (msg) {
4933 +       case NETDEV_UNREGISTER:
4934 +               ro->ifindex = 0;
4935 +               ro->bound   = 0;
4936 +               /* fallthrough */
4937 +       case NETDEV_DOWN:
4938 +               sk->sk_err = ENETDOWN;
4939 +               if (!sock_flag(sk, SOCK_DEAD))
4940 +                       sk->sk_error_report(sk);
4941 +               break;
4942 +       }
4943 +}
4944 +
4945 +
4946 +module_init(raw_module_init);
4947 +module_exit(raw_module_exit);