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
5 +menu "CAN Device Drivers"
9 + tristate "Virtual Local CAN Interface (vcan)"
13 + Similar to the network loopback devices, vcan offers a
14 + virtual local CAN interface.
16 + This driver can also be built as a module. If so, the module
17 + will be called vcan.
19 +config CAN_DEBUG_DEVICES
20 + bool "CAN devices debugging messages"
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
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
36 +# Makefile for the Linux Controller Area Network drivers.
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
45 + * vcan.c - Virtual CAN interface
47 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
48 + * All rights reserved.
50 + * Redistribution and use in source and binary forms, with or without
51 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
84 + * Send feedback to <socketcan-users@lists.berlios.de>
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>
95 +#include <linux/can.h>
96 +#include <linux/can/version.h>
98 +RCSID("$Id: vcan.c 168 2007-03-05 13:33:59Z hartkopp $");
100 +static __initdata const char banner[] = KERN_INFO "CAN: virtual CAN "
101 + "interface " VERSION "\n";
103 +MODULE_DESCRIPTION("virtual CAN interface");
104 +MODULE_LICENSE("Dual BSD/GPL");
105 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
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__), \
113 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
114 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
116 +#define DBG(args...)
117 +#define DBG_FRAME(args...)
118 +#define DBG_SKB(skb)
121 +/* Indicate if this VCAN driver should do a real loopback, or if this */
122 +/* should be done in af_can.c */
125 +#define STATSIZE sizeof(struct net_device_stats)
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");
131 +static struct net_device **vcan_devs; /* root pointer to netdevice structs */
133 +static int vcan_open(struct net_device *dev)
135 + DBG("%s: interface up\n", dev->name);
137 + netif_start_queue(dev);
141 +static int vcan_stop(struct net_device *dev)
143 + DBG("%s: interface down\n", dev->name);
145 + netif_stop_queue(dev);
151 +static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
153 + struct net_device_stats *stats = netdev_priv(dev);
154 + stats->rx_packets++;
155 + stats->rx_bytes += skb->len;
157 + skb->protocol = htons(ETH_P_CAN);
159 + skb->ip_summed = CHECKSUM_UNNECESSARY;
161 + DBG("received skbuff on interface %d\n", dev->ifindex);
169 +static int vcan_tx(struct sk_buff *skb, struct net_device *dev)
171 + struct net_device_stats *stats = netdev_priv(dev);
174 + DBG("sending skbuff on interface %s\n", dev->name);
176 + DBG_FRAME("VCAN: transmit CAN frame", (struct can_frame *)skb->data);
178 + stats->tx_packets++;
179 + stats->tx_bytes += skb->len;
181 + loop = *(struct sock **)skb->cb != NULL; /* loopback required */
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",
190 + kfree_skb(old_skb);
197 + vcan_rx(skb, dev); /* with packet counting */
199 + /* no looped packets => no counting */
203 + /* only count, when the CAN core did a loopback */
205 + stats->rx_packets++;
206 + stats->rx_bytes += skb->len;
213 +static int vcan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
215 + return -EOPNOTSUPP;
218 +static int vcan_rebuild_header(struct sk_buff *skb)
220 + DBG("skbuff %p\n", skb);
225 +static int vcan_header(struct sk_buff *skb, struct net_device *dev,
226 + unsigned short type, void *daddr, void *saddr,
229 + DBG("skbuff %p, device %p\n", skb, dev);
235 +static struct net_device_stats *vcan_get_stats(struct net_device *dev)
237 + struct net_device_stats *stats = netdev_priv(dev);
241 +static void vcan_init(struct net_device *dev)
243 + DBG("dev %s\n", dev->name);
247 + memset(dev->priv, 0, STATSIZE);
249 + dev->type = ARPHRD_CAN;
250 + dev->mtu = sizeof(struct can_frame);
251 + dev->flags = IFF_NOARP;
253 + dev->flags |= IFF_LOOPBACK;
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;
266 + SET_MODULE_OWNER(dev);
269 +static __init int vcan_init_module(void)
271 + int i, ndev = 0, result = 0;
276 + numdev = 1; /* register at least one interface */
278 + printk(KERN_INFO "vcan: registering %d virtual CAN interfaces.\n",
281 + vcan_devs = kmalloc(numdev * sizeof(struct net_device *), GFP_KERNEL);
283 + printk(KERN_ERR "vcan: Can't allocate vcan devices array!\n");
287 + /* Clear the pointer array */
288 + memset(vcan_devs, 0, numdev * sizeof(struct net_device *));
290 + for (i = 0; i < numdev; i++) {
291 + if (!(vcan_devs[i] = alloc_netdev(STATSIZE, "vcan%d",
293 + printk(KERN_ERR "vcan: error allocating net_device\n");
296 + } else if ((result = register_netdev(vcan_devs[i])) < 0) {
297 + printk(KERN_ERR "vcan: error %d registering "
299 + result, vcan_devs[i]->name);
300 + free_netdev(vcan_devs[i]);
301 + vcan_devs[i] = NULL;
304 + DBG("successfully registered interface %s\n",
305 + vcan_devs[i]->name);
314 + for (i = 0; i < numdev; i++) {
315 + if (vcan_devs[i]) {
316 + unregister_netdev(vcan_devs[i]);
317 + free_netdev(vcan_devs[i]);
326 +static __exit void vcan_cleanup_module(void)
333 + for (i = 0; i < numdev; i++) {
334 + if (vcan_devs[i]) {
335 + unregister_netdev(vcan_devs[i]);
336 + free_netdev(vcan_devs[i]);
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
352 + * Definitions for CAN Broadcast Manager (BCM)
354 + * $Id: bcm.h 176 2007-03-07 16:12:46Z hartkopp $
356 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
357 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
358 + * All rights reserved.
360 + * Send feedback to <socketcan-users@lists.berlios.de>
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];
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) */
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
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
412 + * Protoypes and definitions for CAN protocol modules using the PF_CAN core
414 + * $Id: core.h 177 2007-03-08 11:02:43Z hartkopp $
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.
421 + * Send feedback to <socketcan-users@lists.berlios.de>
428 +#include <linux/can.h>
429 +#include <linux/skbuff.h>
430 +#include <linux/netdevice.h>
432 +#define DNAME(dev) ((dev) ? (dev)->name : "any")
434 +#define CAN_PROC_DIR "net/can" /* /proc/... */
440 + struct proto_ops *ops;
441 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
442 + struct proto *prot;
444 + struct module *owner;
445 + int (*init)(struct sock *sk);
450 +/* function prototypes for the CAN networklayer core (af_can.c) */
452 +void can_debug_skb(struct sk_buff *skb);
453 +void can_debug_cframe(const char *msg, struct can_frame *cframe, ...);
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,
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);
468 +unsigned long timeval2jiffies(struct timeval *tv, int round_up);
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
476 + * linux/can/error.h
478 + * Definitions of the CAN error frame to be filtered and passed to the user.
480 + * $Id: error.h 162 2007-03-05 08:25:02Z hartkopp $
482 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
483 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
484 + * All rights reserved.
486 + * Send feedback to <socketcan-users@lists.berlios.de>
493 +#define CAN_ERR_DLC 8 /* dlc for error frames */
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 */
506 +/* arbitration lost in bit ... / data[0] */
507 +#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
508 + /* else bit number in bitstream */
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) */
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 */
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 */
554 +/* error status of CAN-transceiver / data[4] */
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 */
567 +/* controller specific additional information / data[5..7] */
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
577 + * Definitions for raw CAN sockets
579 + * $Id: raw.h 176 2007-03-07 16:12:46Z hartkopp $
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.
586 + * Send feedback to <socketcan-users@lists.berlios.de>
593 +#include <linux/can.h>
595 +#define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW)
597 +/* for socket options affecting the socket (not the global system) */
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) */
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
610 + * linux/can/version.h
612 + * Version information for the CAN network layer implementation
614 + * Author: Urs Thuermann <urs.thuermann@volkswagen.de>
615 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
616 + * All rights reserved.
618 + * Send feedback to <socketcan-users@lists.berlios.de>
622 +#ifndef CAN_VERSION_H
623 +#define CAN_VERSION_H
625 +#define RCSID(s) asm(".section .rodata.str1.1,\"aMS\",@progbits,1\n\t" \
626 + ".string \"" s "\"\n\t.previous\n")
628 +RCSID("$Id: version.h 170 2007-03-05 13:58:26Z hartkopp $");
630 +#define MAJORVERSION 2
631 +#define MINORVERSION 0
632 +#define PATCHLEVEL 0
633 +#define EXTRAVERSION "-pre6"
635 +#define LLCF_VERSION_CODE (((MAJORVERSION) << 16) + ((MINORVERSION) << 8) \
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.
642 +#define str(arg) xstr(arg)
643 +#define xstr(arg) #arg
645 +#define VERSION str(MAJORVERSION) "." str(MINORVERSION) "." str(PATCHLEVEL) \
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
656 + * Definitions for CAN networklayer (socket addr / CAN frame / CAN filter)
658 + * $Id: can.h 165 2007-03-05 10:14:18Z hartkopp $
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.
665 + * Send feedback to <socketcan-users@lists.berlios.de>
672 +#include <linux/version.h>
673 +#include <linux/types.h>
674 +#include <linux/socket.h>
676 +/* controller area network (CAN) kernel definitions */
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 */
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 */
688 +typedef __u32 canid_t;
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)));
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
706 +#define SOL_CAN_BASE 100
708 +struct sockaddr_can {
709 + sa_family_t can_family;
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;
720 +typedef canid_t can_err_mask_t;
727 +#define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
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
735 + * af_can.c - Protocol family CAN core module
736 + * (used by different CAN protocol modules)
738 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
739 + * All rights reserved.
741 + * Redistribution and use in source and binary forms, with or without
742 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
775 + * Send feedback to <socketcan-users@lists.berlios.de>
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>
797 +#include <linux/can.h>
798 +#include <linux/can/core.h>
799 +#include <linux/can/version.h>
804 +RCSID("$Id: af_can.c 177 2007-03-08 11:02:43Z hartkopp $");
806 +#define IDENT "af_can"
807 +static __initdata const char banner[] = KERN_INFO "CAN: Controller Area "
808 + "Network PF_CAN core " VERSION "\n";
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>");
815 +int stats_timer = 1; /* default: on */
816 +module_param(stats_timer, int, S_IRUGO);
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__), \
824 +#define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
825 +#define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
827 +#define DBG(args...)
828 +#define DBG_FRAME(args...)
829 +#define DBG_SKB(skb)
832 +static __init int can_init(void);
833 +static __exit void can_exit(void);
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);
843 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
844 + struct packet_type *pt);
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);
856 + struct list_head list;
857 + struct net_device *dev;
858 + void (*func)(unsigned long msg, void *data);
862 +static LIST_HEAD(notifier_list);
863 +static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
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;
869 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
870 +static struct kmem_cache *rcv_cache __read_mostly;
872 +static kmem_cache_t *rcv_cache;
875 +static struct packet_type can_packet = {
876 + .type = __constant_htons(ETH_P_CAN),
881 +static struct net_proto_family can_family_ops = {
883 + .create = can_create,
884 + .owner = THIS_MODULE,
887 +/* notifier block for netdevice event */
888 +static struct notifier_block can_netdev_notifier = {
889 + .notifier_call = can_notifier,
892 +/* table of registered CAN protocols */
893 +static struct can_proto *proto_tab[CAN_NPROTO];
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 */
899 +module_init(can_init);
900 +module_exit(can_exit);
902 +/**************************************************/
903 +/* af_can module init/exit functions */
904 +/**************************************************/
906 +static __init int can_init(void)
910 + rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
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.
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);
925 + /* statistics init */
926 + init_timer(&stattimer);
932 + /* protocol register */
933 + sock_register(&can_family_ops);
934 + register_netdevice_notifier(&can_netdev_notifier);
935 + dev_add_pack(&can_packet);
940 +static __exit void can_exit(void)
942 + struct dev_rcv_lists *d;
943 + struct hlist_node *n, *next;
946 + /* stop statistics timer */
947 + del_timer(&stattimer);
950 + /* procfs remove */
953 + /* protocol unregister */
954 + dev_remove_pack(&can_packet);
955 + unregister_netdevice_notifier(&can_netdev_notifier);
956 + sock_unregister(PF_CAN);
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);
965 + spin_unlock_bh(&rcv_lists_lock);
967 + kmem_cache_destroy(rcv_cache);
970 +/**************************************************/
971 +/* af_can protocol functions */
972 +/**************************************************/
974 +void can_proto_register(struct can_proto *cp)
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);
982 + if (proto_tab[proto]) {
983 + printk(KERN_ERR "CAN: protocol %d already "
984 + "registered\n", proto);
988 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
989 + if (proto_register(cp->prot, 0) != 0) {
993 + proto_tab[proto] = cp;
995 + /* use generic ioctl function if the module doesn't bring its own */
996 + if (!cp->ops->ioctl)
997 + cp->ops->ioctl = can_ioctl;
1000 +void can_proto_unregister(struct can_proto *cp)
1002 + int proto = cp->protocol;
1003 + if (!proto_tab[proto]) {
1004 + printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
1007 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1008 + proto_unregister(cp->prot);
1010 + proto_tab[proto] = NULL;
1013 +void can_dev_register(struct net_device *dev,
1014 + void (*func)(unsigned long msg, void *), void *data)
1016 + struct notifier *n;
1018 + DBG("called for %s\n", dev->name);
1020 + if (!(n = kmalloc(sizeof(*n), GFP_KERNEL)))
1027 + write_lock(¬ifier_lock);
1028 + list_add(&n->list, ¬ifier_list);
1029 + write_unlock(¬ifier_lock);
1032 +void can_dev_unregister(struct net_device *dev,
1033 + void (*func)(unsigned long msg, void *), void *data)
1035 + struct notifier *n, *next;
1037 + DBG("called for %s\n", dev->name);
1039 + write_lock(¬ifier_lock);
1040 + list_for_each_entry_safe(n, next, ¬ifier_list, list) {
1041 + if (n->dev == dev && n->func == func && n->data == data) {
1042 + list_del(&n->list);
1047 + write_unlock(¬ifier_lock);
1050 +/**************************************************/
1051 +/* af_can socket functions */
1052 +/**************************************************/
1054 +static void can_sock_destruct(struct sock *sk)
1056 + DBG("called for sock %p\n", sk);
1058 + skb_queue_purge(&sk->sk_receive_queue);
1059 + if (sk->sk_protinfo)
1060 + kfree(sk->sk_protinfo);
1063 +static int can_create(struct socket *sock, int protocol)
1066 + struct can_proto *cp;
1069 + DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
1071 + sock->state = SS_UNCONNECTED;
1073 + if (protocol < 0 || protocol >= CAN_NPROTO)
1076 + DBG("looking up proto %d in proto_tab[]\n", protocol);
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);
1087 + /* check for success and correct type */
1088 + if (!(cp = proto_tab[protocol]) || cp->type != sock->type)
1089 + return -EPROTONOSUPPORT;
1091 + if (cp->capability >= 0 && !capable(cp->capability))
1094 + sock->ops = cp->ops;
1096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1097 + sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
1101 + sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
1104 + if (cp->obj_size &&
1105 + !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
1109 + sk_set_owner(sk, proto_tab[protocol]->owner);
1111 + sock_init_data(sock, sk);
1112 + sk->sk_destruct = can_sock_destruct;
1114 + DBG("created sock: %p\n", sk);
1117 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
1118 + if (sk->sk_prot->init)
1119 + ret = sk->sk_prot->init(sk);
1122 + ret = cp->init(sk);
1125 + /* we must release sk */
1137 +static int can_notifier(struct notifier_block *nb,
1138 + unsigned long msg, void *data)
1140 + struct net_device *dev = (struct net_device *)data;
1141 + struct notifier *n;
1143 + DBG("called for %s, msg = %lu\n", dev->name, msg);
1145 + if (dev->type != ARPHRD_CAN)
1146 + return NOTIFY_DONE;
1149 + struct dev_rcv_lists *d;
1152 + case NETDEV_REGISTER:
1154 + /* create new dev_rcv_lists for this device */
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 "
1161 + return NOTIFY_DONE;
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));
1170 + spin_lock_bh(&rcv_lists_lock);
1171 + hlist_add_head_rcu(&d->list, &rx_dev_list);
1172 + spin_unlock_bh(&rcv_lists_lock);
1176 + case NETDEV_UNREGISTER:
1177 + spin_lock_bh(&rcv_lists_lock);
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);
1185 + hlist_del_rcu(&d->list);
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]);
1197 + spin_unlock_bh(&rcv_lists_lock);
1200 + call_rcu(&d->rcu, can_rcv_lists_delete);
1205 + read_lock(¬ifier_lock);
1206 + list_for_each_entry(n, ¬ifier_list, list) {
1207 + if (n->dev == dev)
1208 + n->func(msg, n->data);
1210 + read_unlock(¬ifier_lock);
1212 + return NOTIFY_DONE;
1215 +static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1217 + struct sock *sk = sock->sk;
1221 + return sock_get_timestamp(sk, (struct timeval __user *)arg);
1223 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1224 + return -ENOIOCTLCMD;
1226 + return dev_ioctl(cmd, (void __user *)arg);
1232 +/**************************************************/
1233 +/* af_can tx path */
1234 +/**************************************************/
1236 +int can_send(struct sk_buff *skb, int loop)
1240 + if (loop) { /* local loopback (default) */
1241 + *(struct sock **)skb->cb = skb->sk; /* tx sock reference */
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 */
1251 + *(struct sock **)skb->cb = NULL; /* no loopback required */
1253 + if (!(skb->dev->flags & IFF_UP))
1255 + else if ((err = dev_queue_xmit(skb)) > 0) /* send to netdevice */
1256 + err = net_xmit_errno(err);
1258 + /* update statistics */
1259 + stats.tx_frames++;
1260 + stats.tx_frames_delta++;
1265 +/**************************************************/
1266 +/* af_can rx path */
1267 +/**************************************************/
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,
1273 + struct receiver *r;
1274 + struct hlist_head *rl;
1275 + struct dev_rcv_lists *d;
1278 + /* insert new receiver (dev,canid,mask) -> (func,data) */
1280 + DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
1281 + dev, can_id, mask, func, data, ident);
1283 + if (!(r = kmem_cache_alloc(rcv_cache, GFP_KERNEL))) {
1288 + spin_lock_bh(&rcv_lists_lock);
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);
1298 + rl = find_rcv_list(&can_id, &mask, d);
1300 + r->can_id = can_id;
1307 + hlist_add_head_rcu(&r->list, rl);
1310 + pstats.rcv_entries++;
1311 + if (pstats.rcv_entries_max < pstats.rcv_entries)
1312 + pstats.rcv_entries_max = pstats.rcv_entries;
1315 + spin_unlock_bh(&rcv_lists_lock);
1320 +static void can_rcv_lists_delete(struct rcu_head *rp)
1322 + struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
1326 +static void can_rx_delete(struct rcu_head *rp)
1328 + struct receiver *r = container_of(rp, struct receiver, rcu);
1329 + kmem_cache_free(rcv_cache, r);
1332 +static void can_rx_delete_all(struct hlist_head *rl)
1334 + struct receiver *r;
1335 + struct hlist_node *n;
1337 + hlist_for_each_entry_rcu(r, n, rl, list) {
1338 + hlist_del_rcu(&r->list);
1339 + call_rcu(&r->rcu, can_rx_delete);
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)
1346 + struct receiver *r;
1347 + struct hlist_head *rl;
1348 + struct hlist_node *next;
1349 + struct dev_rcv_lists *d;
1352 + DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
1353 + dev, can_id, mask, func, data);
1357 + spin_lock_bh(&rcv_lists_lock);
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);
1366 + rl = find_rcv_list(&can_id, &mask, d);
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.
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)
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.
1385 + DBG("receive list entry not found for "
1386 + "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
1392 + hlist_del_rcu(&r->list);
1395 + if (pstats.rcv_entries > 0)
1396 + pstats.rcv_entries--;
1399 + spin_unlock_bh(&rcv_lists_lock);
1401 + /* schedule the receiver item for deletion */
1403 + call_rcu(&r->rcu, can_rx_delete);
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)
1412 +static int can_rcv(struct sk_buff *skb, struct net_device *dev,
1413 + struct packet_type *pt)
1416 + struct dev_rcv_lists *d;
1419 + DBG("received skbuff on device %s, ptype %04x\n",
1420 + dev->name, ntohs(pt->type));
1422 + DBG_FRAME("af_can: can_rcv: received CAN frame",
1423 + (struct can_frame *)skb->data);
1425 + /* update statistics */
1426 + stats.rx_frames++;
1427 + stats.rx_frames_delta++;
1431 + /* deliver the packet to sockets listening on all devices */
1432 + matches = can_rcv_filter(&rx_alldev_list, skb);
1434 + /* find receive list for this device */
1435 + if ((d = find_dev_rcv_lists(dev)))
1436 + matches += can_rcv_filter(d, skb);
1438 + rcu_read_unlock();
1440 + /* free the skbuff allocated by the netdevice driver */
1441 + DBG("freeing skbuff %p\n", skb);
1444 + if (matches > 0) {
1446 + stats.matches_delta++;
1453 +static inline void deliver(struct sk_buff *skb, struct receiver *r)
1455 + struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
1456 + DBG("skbuff %p cloned to %p\n", skb, clone);
1458 + r->func(clone, r->data);
1459 + r->matches++; /* update specific statistics */
1463 +static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
1465 + struct receiver *r;
1466 + struct hlist_node *n;
1468 + struct can_frame *cf = (struct can_frame*)skb->data;
1469 + canid_t can_id = cf->can_id;
1471 + if (d->entries == 0)
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);
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);
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);
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);
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);
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);
1533 +static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
1535 + struct dev_rcv_lists *d;
1536 + struct hlist_node *n;
1538 + /* find receive list for this device */
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.
1550 + hlist_for_each_entry(d, n, &rx_dev_list, list)
1551 + if (d->dev == dev)
1554 + return n ? d : NULL;
1557 +static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
1558 + struct dev_rcv_lists *d)
1560 + canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
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;
1567 + /* ensure valid values in can_mask */
1568 + if (*mask & CAN_EFF_FLAG)
1569 + *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
1571 + *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
1573 + *can_id &= *mask; /* reduce condition testing at receive time */
1575 + if (inv) /* inverse can_id/can_mask filter */
1576 + return &d->rx_inv;
1578 + if (!(*mask)) /* mask == 0 => no condition testing at receive time */
1579 + return &d->rx_all;
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? */
1586 + if (*mask == CAN_SFF_MASK)
1587 + return &d->rx_sff[*can_id];
1590 + return &d->rx_fil; /* default: filter via can_id/can_mask */
1593 +/**************************************************/
1594 +/* af_can utility stuff */
1595 +/**************************************************/
1597 +unsigned long timeval2jiffies(struct timeval *tv, int round_up)
1599 + unsigned long jif;
1600 + unsigned long sec = tv->tv_sec;
1601 + unsigned long usec = tv->tv_usec;
1603 + if (sec > ULONG_MAX / HZ) /* check for overflow */
1606 + if (round_up) /* any usec below one HZ? */
1607 + usec += 1000000 / HZ - 1; /* pump it up */
1609 + jif = usec / (1000000 / HZ);
1611 + if (sec * HZ > ULONG_MAX - jif) /* check for overflow */
1614 + return jif + sec * HZ;
1618 +/**************************************************/
1619 +/* af_can debugging stuff */
1620 +/**************************************************/
1622 +#ifdef CONFIG_CAN_DEBUG_CORE
1624 +void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
1631 + len = sprintf(buf, KERN_DEBUG);
1633 + len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
1638 + if ((dlc = cf->can_dlc) > 8)
1641 + if (cf->can_id & CAN_EFF_FLAG)
1642 + len += sprintf(buf + len, "<%08X> [%X] ",
1643 + cf->can_id & CAN_EFF_MASK, dlc);
1645 + len += sprintf(buf + len, "<%03X> [%X] ",
1646 + cf->can_id & CAN_SFF_MASK, dlc);
1648 + for (i = 0; i < dlc; i++)
1649 + len += sprintf(buf + len, "%02X ", cf->data[i]);
1651 + if (cf->can_id & CAN_RTR_FLAG)
1652 + len += sprintf(buf + len, "(RTR)");
1654 + buf[len++] = '\n';
1659 +void can_debug_skb(struct sk_buff *skb)
1661 + int len, nbytes, i;
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++) {
1678 + len += sprintf(buf + len, "\n" KERN_DEBUG " ");
1679 + if (len < sizeof(buf) - 16) {
1680 + len += sprintf(buf + len, " %02x", skb->head[i]);
1682 + len += sprintf(buf + len, "...");
1686 + buf[len++] = '\n';
1691 +EXPORT_SYMBOL(can_debug_cframe);
1692 +EXPORT_SYMBOL(can_debug_skb);
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
1712 + * $Id: af_can.h 177 2007-03-08 11:02:43Z hartkopp $
1714 + * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
1715 + * All rights reserved.
1717 + * Redistribution and use in source and binary forms, with or without
1718 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
1751 + * Send feedback to <socketcan-users@lists.berlios.de>
1758 +#include <linux/skbuff.h>
1759 +#include <linux/netdevice.h>
1760 +#include <linux/list.h>
1761 +#include <linux/rcupdate.h>
1763 +#include <linux/can.h>
1765 +/* af_can rx dispatcher structures */
1768 + struct hlist_node list;
1769 + struct rcu_head rcu;
1772 + unsigned long matches;
1773 + void (*func)(struct sk_buff *, void *);
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;
1791 +/* statistic structures */
1794 + unsigned long jiffies_init;
1796 + unsigned long rx_frames;
1797 + unsigned long tx_frames;
1798 + unsigned long matches;
1800 + unsigned long total_rx_rate;
1801 + unsigned long total_tx_rate;
1802 + unsigned long total_rx_match_ratio;
1804 + unsigned long current_rx_rate;
1805 + unsigned long current_tx_rate;
1806 + unsigned long current_rx_match_ratio;
1808 + unsigned long max_rx_rate;
1809 + unsigned long max_tx_rate;
1810 + unsigned long max_rx_match_ratio;
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() */
1818 + unsigned long stats_reset;
1819 + unsigned long rcv_entries;
1820 + unsigned long rcv_entries_max;
1821 +}; /* persistent statistics */
1824 +/* function prototypes for the CAN networklayer procfs (proc.c) */
1826 +void can_init_proc(void);
1827 +void can_remove_proc(void);
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
1835 + * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
1837 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1838 + * All rights reserved.
1840 + * Redistribution and use in source and binary forms, with or without
1841 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
1874 + * Send feedback to <socketcan-users@lists.berlios.de>
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>
1887 +#include <linux/can.h>
1888 +#include <linux/can/core.h>
1889 +#include <linux/can/bcm.h>
1890 +#include <linux/can/version.h>
1892 +RCSID("$Id: bcm.c 179 2007-03-09 07:36:49Z hartkopp $");
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)
1903 +#define DBG(args...)
1904 +#define DBG_FRAME(args...)
1905 +#define DBG_SKB(skb)
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 */
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))
1917 +#define IDENT "bcm"
1918 +static __initdata const char banner[] = KERN_INFO
1919 + "CAN: broadcast manager (bcm) socket protocol " VERSION "\n";
1921 +MODULE_DESCRIPTION("PF_CAN bcm sockets");
1922 +MODULE_LICENSE("Dual BSD/GPL");
1923 +MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
1925 +#define GET_U64(p) (*(u64*)(p)->data) /* easy access */
1928 + struct list_head list;
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;
1941 + struct can_frame *frames;
1942 + struct can_frame *last_frames;
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 */
1956 +static struct proc_dir_entry *proc_dir = NULL;
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,
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);
1970 +static int bcm_read_proc(char *page, char **start, off_t off,
1971 + int count, int *eof, void *data);
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);
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);
1994 +static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id,
1996 +static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id,
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,
2001 +static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
2004 +static struct proto_ops bcm_ops = {
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,
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,
2024 +#ifdef CONFIG_CAN_BCM_USER
2025 +#define BCM_CAP (-1)
2027 +#define BCM_CAP CAP_NET_RAW
2030 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
2033 + struct bcm_opt opt;
2036 +#define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
2038 +static struct proto bcm_proto = {
2039 + .name = "CAN_BCM",
2040 + .owner = THIS_MODULE,
2041 + .obj_size = sizeof(struct bcm_sock),
2045 +static struct can_proto bcm_can_proto = {
2046 + .type = SOCK_DGRAM,
2047 + .protocol = CAN_BCM,
2048 + .capability = BCM_CAP,
2050 + .prot = &bcm_proto,
2053 +#define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
2055 +static struct can_proto bcm_can_proto = {
2056 + .type = SOCK_DGRAM,
2057 + .protocol = CAN_BCM,
2058 + .capability = BCM_CAP,
2060 + .owner = THIS_MODULE,
2061 + .obj_size = sizeof(struct bcm_opt),
2066 +#define CFSIZ sizeof(struct can_frame)
2067 +#define OPSIZ sizeof(struct bcm_op)
2068 +#define MHSIZ sizeof(struct bcm_msg_head)
2070 +static int __init bcm_module_init(void)
2074 + can_proto_register(&bcm_can_proto);
2076 + /* create /proc/net/can/bcm directory */
2077 + proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
2080 + proc_dir->owner = THIS_MODULE;
2085 +static void __exit bcm_module_exit(void)
2087 + can_proto_unregister(&bcm_can_proto);
2090 + remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
2094 +/**************************************************/
2095 +/* initial settings at socket creation time */
2096 +/**************************************************/
2098 +static int bcm_init(struct sock *sk)
2100 + struct bcm_opt *bo = bcm_sk(sk);
2104 + bo->dropped_usr_msgs = 0;
2105 + bo->bcm_proc_read = NULL;
2107 + INIT_LIST_HEAD(&bo->tx_ops);
2108 + INIT_LIST_HEAD(&bo->rx_ops);
2113 +/**************************************************/
2114 +/* handling of netdevice problems */
2115 +/**************************************************/
2117 +static void bcm_notifier(unsigned long msg, void *data)
2119 + struct sock *sk = (struct sock *)data;
2120 + struct bcm_opt *bo = bcm_sk(sk);
2122 + DBG("called for sock %p\n", sk);
2125 + case NETDEV_UNREGISTER:
2130 + sk->sk_err = ENETDOWN;
2131 + if (!sock_flag(sk, SOCK_DEAD))
2132 + sk->sk_error_report(sk);
2136 +/**************************************************/
2137 +/* standard socket functions */
2138 +/**************************************************/
2140 +static int bcm_release(struct socket *sock)
2142 + struct sock *sk = sock->sk;
2143 + struct bcm_opt *bo = bcm_sk(sk);
2144 + struct bcm_op *op, *next;
2146 + DBG("socket %p, sk %p\n", sock, sk);
2148 + /* remove bcm_ops, timer, rx_unregister(), etc. */
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);
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);
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);
2163 + can_rx_unregister(dev, op->can_id,
2164 + REGMASK(op->can_id),
2165 + bcm_rx_handler, op);
2169 + can_rx_unregister(NULL, op->can_id,
2170 + REGMASK(op->can_id),
2171 + bcm_rx_handler, op);
2173 + bcm_remove_op(op);
2176 + /* remove procfs entry */
2177 + if ((proc_dir) && (bo->bcm_proc_read)) {
2178 + remove_proc_entry(bo->procname, proc_dir);
2181 + /* remove device notifier */
2182 + if (bo->ifindex) {
2183 + struct net_device *dev = dev_get_by_index(bo->ifindex);
2185 + can_dev_unregister(dev, bcm_notifier, sk);
2195 +static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
2198 + struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
2199 + struct sock *sk = sock->sk;
2200 + struct bcm_opt *bo = bcm_sk(sk);
2205 + /* bind a device to this socket */
2206 + if (addr->can_ifindex) {
2207 + struct net_device *dev = dev_get_by_index(addr->can_ifindex);
2209 + DBG("could not find device index %d\n",
2210 + addr->can_ifindex);
2213 + bo->ifindex = dev->ifindex;
2214 + can_dev_register(dev, bcm_notifier, sk); /* register notif. */
2217 + DBG("socket %p bound to device %s (idx %d)\n",
2218 + sock, dev->name, dev->ifindex);
2220 + /* no notifier for ifindex = 0 ('any' CAN device) */
2227 + /* unique socket address as filename */
2228 + sprintf(bo->procname, "%p", sock);
2229 + bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
2231 + bcm_read_proc, sk);
2237 +static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
2238 + struct msghdr *msg, size_t size)
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 */
2247 + DBG("sock %p not bound\n", sk);
2251 + /* check for alternative ifindex for this bcm_op */
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)
2258 + ifindex = addr->can_ifindex; /* ifindex from sendto() */
2260 + if (ifindex && !dev_get_by_index(ifindex)) {
2261 + DBG("device %d not found\n", ifindex);
2266 + /* read message head information */
2268 + if ((ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov,
2272 + DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
2274 + switch (msg_head.opcode) {
2278 + ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
2283 + ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
2288 + if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
2296 + if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
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);
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);
2318 + if (msg_head.nframes < 1) /* we need at least one can_frame */
2321 + ret = bcm_tx_send(msg, ifindex, sk);
2326 + DBG("Unknown opcode %d\n", msg_head.opcode);
2334 +static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
2335 + struct msghdr *msg, size_t size, int flags)
2337 + struct sock *sk = sock->sk;
2338 + struct sk_buff *skb;
2343 + DBG("socket %p, sk %p\n", sock, sk);
2345 + noblock = flags & MSG_DONTWAIT;
2346 + flags &= ~MSG_DONTWAIT;
2347 + if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
2351 + DBG("delivering skbuff %p\n", skb);
2354 + if (skb->len < size)
2356 + if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
2357 + skb_free_datagram(sk, skb);
2361 + sock_recv_timestamp(msg, sk, skb);
2363 + if (msg->msg_name) {
2364 + msg->msg_namelen = sizeof(struct sockaddr_can);
2365 + memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
2368 + DBG("freeing sock %p, skbuff %p\n", sk, skb);
2369 + skb_free_datagram(sk, skb);
2374 +static unsigned int bcm_poll(struct file *file, struct socket *sock,
2377 + unsigned int mask = 0;
2379 + DBG("socket %p\n", sock);
2381 + mask = datagram_poll(file, sock, wait);
2385 +/**************************************************/
2386 +/* helper functions for bcm_sendmsg() */
2387 +/**************************************************/
2389 +static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
2390 + int ifindex, struct sock *sk)
2392 + struct bcm_opt *bo = bcm_sk(sk);
2393 + struct bcm_op *op;
2396 + if (!ifindex) /* we need a real device to send frames */
2399 + if (msg_head->nframes < 1) /* we need at least one can_frame */
2402 + /* check the given can_id */
2404 + if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
2406 + /* update existing BCM operation */
2408 + DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
2409 + op, msg_head->can_id);
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)
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)
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;
2430 + /* insert new BCM operation for the given can_id */
2432 + if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
2435 + memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
2437 + DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
2438 + op, msg_head->can_id);
2440 + op->can_id = msg_head->can_id;
2442 + /* create array for can_frames and copy the data */
2443 + if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
2449 + for (i = 0; i < msg_head->nframes; i++) {
2450 + if ((err = memcpy_fromiovec((u8*)&op->frames[i],
2453 + kfree(op->frames);
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;
2464 + /* tx_ops never compare with previous received messages */
2465 + op->last_frames = NULL;
2467 + /* bcm_can_tx / bcm_tx_timeout_handler needs this */
2470 + op->ifindex = ifindex;
2472 + /* initialize uninitialized (kmalloc) structure */
2473 + init_timer(&op->timer);
2475 + /* currently unused in tx_ops */
2476 + init_timer(&op->thrtimer);
2478 + /* handler for tx_ops */
2479 + op->timer.function = bcm_tx_timeout_handler;
2481 + /* timer.data points to this op-structure */
2482 + op->timer.data = (unsigned long)op;
2484 + /* add this bcm_op to the list of the tx_ops */
2485 + list_add(&op->list, &bo->tx_ops);
2487 + } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
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;
2497 + op->flags = msg_head->flags;
2499 + if (op->flags & TX_RESET_MULTI_IDX) {
2500 + /* start multiple frame transmission with index 0 */
2501 + op->currframe = 0;
2504 + if (op->flags & SETTIMER) {
2506 + /* set timer values */
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);
2514 + DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
2515 + op->count, op->j_ival1, op->j_ival2);
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");
2524 + if ((op->flags & STARTTIMER) &&
2525 + ((op->j_ival1 && op->count) || op->j_ival2)) {
2527 + del_timer(&op->timer);
2529 + /* spec: send can_frame when starting timer */
2530 + op->flags |= TX_ANNOUNCE;
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 "
2537 + op->timer.function,
2538 + (char*) op->timer.data,
2539 + (unsigned int) op->timer.expires);
2541 + op->timer.expires = jiffies + op->j_ival2;
2542 + DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
2544 + op->timer.function,
2545 + (char*) op->timer.data,
2546 + (unsigned int) op->timer.expires);
2549 + add_timer(&op->timer);
2552 + if (op->flags & TX_ANNOUNCE)
2555 + return msg_head->nframes * CFSIZ + MHSIZ;
2558 +static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
2559 + int ifindex, struct sock *sk)
2561 + struct bcm_opt *bo = bcm_sk(sk);
2562 + struct bcm_op *op;
2563 + int do_rx_register;
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 */
2572 + if ((msg_head->flags & RX_RTR_FRAME) &&
2573 + ((msg_head->nframes != 1) ||
2574 + (!(msg_head->can_id & CAN_RTR_FLAG)))) {
2576 + DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
2580 + /* check the given can_id */
2582 + if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
2584 + /* update existing BCM operation */
2586 + DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
2587 + op, msg_head->can_id);
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)
2595 + if (msg_head->nframes) {
2596 + /* update can_frames content */
2597 + if ((err = memcpy_fromiovec((u8*)op->frames,
2603 + /* clear last_frames to indicate 'nothing received' */
2604 + memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
2607 + op->nframes = msg_head->nframes;
2608 + /* Only an update -> do not call can_rx_register() */
2609 + do_rx_register = 0;
2612 + /* insert new BCM operation for the given can_id */
2614 + if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
2617 + memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
2619 + DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
2620 + op, msg_head->can_id);
2622 + op->can_id = msg_head->can_id;
2623 + op->nframes = msg_head->nframes;
2625 + if (msg_head->nframes) {
2627 + /* create array for can_frames and copy the data */
2628 + if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
2634 + if ((err = memcpy_fromiovec((u8*)op->frames,
2638 + kfree(op->frames);
2643 + /* create array for received can_frames */
2644 + if (!(op->last_frames = kmalloc(msg_head->nframes
2647 + kfree(op->frames);
2652 + /* clear last_frames to indicate 'nothing received' */
2653 + memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
2655 + /* op->frames = NULL due to memset */
2657 + /* even when we have the RX_FILTER_ID case, we need */
2658 + /* to store the last frame for the throttle feature */
2660 + /* create array for received can_frames */
2661 + if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
2666 + /* clear last_frames to indicate 'nothing received' */
2667 + memset(op->last_frames, 0, CFSIZ);
2670 + op->sk = sk; /* bcm_delete_rx_op() needs this */
2671 + op->ifindex = ifindex;
2673 + /* initialize uninitialized (kmalloc) structure */
2674 + init_timer(&op->timer);
2676 + /* init throttle timer for RX_CHANGED */
2677 + init_timer(&op->thrtimer);
2679 + /* handler for rx timeouts */
2680 + op->timer.function = bcm_rx_timeout_handler;
2682 + /* timer.data points to this op-structure */
2683 + op->timer.data = (unsigned long)op;
2685 + /* handler for RX_CHANGED throttle timeouts */
2686 + op->thrtimer.function = bcm_rx_thr_handler;
2688 + /* timer.data points to this op-structure */
2689 + op->thrtimer.data = (unsigned long)op;
2691 + op->thrtimer.expires = 0; /* mark disabled timer */
2693 + /* add this bcm_op to the list of the tx_ops */
2694 + list_add(&op->list, &bo->rx_ops);
2696 + do_rx_register = 1; /* call can_rx_register() */
2698 + } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
2703 + op->flags = msg_head->flags;
2705 + if (op->flags & RX_RTR_FRAME) {
2707 + /* no timers in RTR-mode */
2708 + del_timer(&op->thrtimer);
2709 + del_timer(&op->timer);
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;
2719 + if (op->flags & SETTIMER) {
2721 + /* set timer value */
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);
2728 + DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
2729 + op->j_ival1, op->j_ival2);
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");
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);
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() */
2750 + if ((op->flags & STARTTIMER) && op->j_ival1) {
2752 + del_timer(&op->timer);
2754 + op->timer.expires = jiffies + op->j_ival1;
2756 + DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
2758 + (char *) op->timer.function,
2759 + (char *) op->timer.data,
2760 + (unsigned int) op->timer.expires);
2762 + add_timer(&op->timer);
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);
2772 + struct net_device *dev = dev_get_by_index(ifindex);
2775 + can_rx_register(dev, op->can_id,
2776 + REGMASK(op->can_id),
2777 + bcm_rx_handler, op, IDENT);
2781 + can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
2782 + bcm_rx_handler, op, IDENT);
2785 + return msg_head->nframes * CFSIZ + MHSIZ;
2788 +static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
2790 + struct sk_buff *skb;
2791 + struct net_device *dev;
2794 + /* just copy and send one can_frame */
2796 + if (!ifindex) /* we need a real device to send frames */
2799 + skb = alloc_skb(CFSIZ, GFP_KERNEL);
2804 + if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov,
2810 + DBG_FRAME("BCM: TX_SEND: sending frame",
2811 + (struct can_frame *)skb->data);
2812 + dev = dev_get_by_index(ifindex);
2821 + can_send(skb, 1); /* send with loopback */
2824 + return CFSIZ + MHSIZ;
2827 +static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
2830 + struct bcm_op *op;
2833 + if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
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;
2844 + bcm_send_to_user(op, msg_head, op->frames, NULL);
2850 + DBG("TRX_READ: did not find op for can_id %03X\n",
2851 + msg_head->can_id);
2858 +/**************************************************/
2859 +/* procfs functions */
2860 +/**************************************************/
2862 +static char *bcm_proc_getifname(int ifindex)
2864 + struct net_device *dev;
2869 + dev = __dev_get_by_index(ifindex); /* no usage counting */
2876 +static int bcm_read_proc(char *page, char **start, off_t off,
2877 + int count, int *eof, void *data)
2880 + struct sock *sk = (struct sock *)data;
2881 + struct bcm_opt *bo = bcm_sk(sk);
2882 + struct bcm_op *op;
2884 + len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
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");
2894 + list_for_each_entry(op, &bo->rx_ops, list) {
2896 + unsigned long reduction;
2898 + /* print only active entries & prevent division by zero */
2899 + if (!op->frames_abs)
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 ",
2907 + (op->flags & RX_CHECK_DLC)?'d':' ');
2909 + len += snprintf(page + len, PAGE_SIZE - len,
2910 + "timeo=%ld ", op->j_ival1);
2913 + len += snprintf(page + len, PAGE_SIZE - len,
2914 + "thr=%ld ", op->j_ival2);
2916 + len += snprintf(page + len, PAGE_SIZE - len,
2917 + "# recv %ld (%ld) => reduction: ",
2918 + op->frames_filtered, op->frames_abs);
2920 + reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
2922 + len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
2923 + (reduction == 100)?"near ":"", reduction);
2925 + if (len > PAGE_SIZE - 200) {
2926 + /* mark output cut off */
2927 + len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
2932 + list_for_each_entry(op, &bo->tx_ops, list) {
2934 + len += snprintf(page + len, PAGE_SIZE - len,
2935 + "tx_op: %03X %s [%d] ",
2936 + op->can_id, bcm_proc_getifname(op->ifindex),
2939 + len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
2943 + len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
2946 + len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
2949 + if (len > PAGE_SIZE - 100) {
2950 + /* mark output cut off */
2951 + len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
2956 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
2962 +/**************************************************/
2963 +/* bcm_op handling tx path */
2964 +/**************************************************/
2966 +static void bcm_can_tx(struct bcm_op *op)
2968 + struct sk_buff *skb;
2969 + struct net_device *dev;
2970 + struct can_frame *cf = &op->frames[op->currframe];
2972 + DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
2975 + return; /* no target device -> exit */
2977 + dev = dev_get_by_index(op->ifindex);
2980 + return; /* should this bcm_op remove itself here? */
2982 + skb = alloc_skb(CFSIZ,
2983 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
2986 + goto out; /* no memory */
2988 + memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
2992 + can_send(skb, 1); /* send with loopback */
2995 + op->frames_abs++; /* statistics */
2997 + /* reached last frame? */
2998 + if (op->currframe >= op->nframes)
2999 + op->currframe = 0;
3004 +static void bcm_tx_timeout_handler(unsigned long data)
3006 + struct bcm_op *op = (struct bcm_op*)data;
3008 + DBG("Called with bcm_op %p\n", op);
3010 + if (op->j_ival1 && (op->count > 0)) {
3014 + if (!op->count && (op->flags & TX_COUNTEVT)) {
3015 + /* create notification to user */
3017 + struct bcm_msg_head msg_head;
3019 + DBG("sending TX_EXPIRED for can_id %03X\n",
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;
3030 + bcm_send_to_user(op, &msg_head, NULL, NULL);
3034 + DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
3035 + op->count, op->j_ival1, op->j_ival2);
3037 + if (op->j_ival1 && (op->count > 0)) {
3039 + op->timer.expires = jiffies + op->j_ival1;
3040 + add_timer(&op->timer);
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);
3047 + bcm_can_tx(op); /* send (next) frame */
3049 + if (op->j_ival2) {
3050 + op->timer.expires = jiffies + op->j_ival2;
3051 + add_timer(&op->timer);
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);
3058 + bcm_can_tx(op); /* send (next) frame */
3060 + DBG("no timer restart\n");
3067 +/**************************************************/
3068 +/* bcm_op handling rx path */
3069 +/**************************************************/
3071 +static void bcm_rx_handler(struct sk_buff *skb, void *data)
3073 + struct bcm_op *op = (struct bcm_op*)data;
3074 + struct can_frame rxframe;
3077 + del_timer(&op->timer); /* disable timeout */
3079 + DBG("Called with bcm_op %p\n", op);
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 */
3088 + DBG("got can_frame with can_id %03X\n", rxframe.can_id);
3090 + DBG("Wrong skb->len = %d\n", skb->len);
3095 + DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
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);
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 */
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);
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);
3123 + if (op->nframes > 1) { /* multiplex compare */
3125 + DBG("Multiplex compare\n");
3126 + /* find the first multiplex mask that fits */
3127 + /* MUX-mask is in index 0 */
3129 + for (i=1; i < op->nframes; i++) {
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);
3139 + bcm_rx_starttimer(op);
3143 +static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
3144 + struct can_frame *rxdata)
3146 + /* no one uses the MSBs of can_dlc for comparation, */
3147 + /* so we use it here to detect the first time of reception */
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);
3155 + /* do a real check in can_data */
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));
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);
3171 + if (op->flags & RX_CHECK_DLC) {
3173 + /* do a real check in dlc */
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],
3183 + DBG("no relevant change :(\n");
3186 +static void bcm_rx_update_and_send(struct bcm_op *op,
3187 + struct can_frame *lastdata,
3188 + struct can_frame *rxdata)
3190 + unsigned long nexttx = op->j_lastmsg + op->j_ival2;
3192 + memcpy(lastdata, rxdata, CFSIZ);
3193 + lastdata->can_dlc |= RX_RECV; /* mark as used */
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 */
3199 + lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
3201 + if (!(op->thrtimer.expires)) { /* start only the first time */
3202 + op->thrtimer.expires = nexttx;
3203 + add_timer(&op->thrtimer);
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);
3211 + bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
3214 +static void bcm_rx_starttimer(struct bcm_op *op)
3216 + if (op->flags & RX_NO_AUTOTIMER)
3219 + if (op->j_ival1) {
3221 + op->timer.expires = jiffies + op->j_ival1;
3223 + DBG("adding rx timeout timer ival1. func=%p data=%p "
3225 + op->timer.function,
3226 + (char*) op->timer.data,
3227 + (unsigned int) op->timer.expires);
3229 + add_timer(&op->timer);
3234 +static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
3236 + struct bcm_msg_head head;
3238 + op->j_lastmsg = jiffies;
3239 + op->frames_filtered++; /* statistics */
3241 + if (op->frames_filtered > ULONG_MAX/100)
3242 + op->frames_filtered = op->frames_abs = 0; /* restart */
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");
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;
3256 + bcm_send_to_user(op, &head, data, &op->rx_stamp);
3260 +static void bcm_rx_timeout_handler(unsigned long data)
3262 + struct bcm_op *op = (struct bcm_op*)data;
3263 + struct bcm_msg_head msg_head;
3265 + DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
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;
3275 + bcm_send_to_user(op, &msg_head, NULL, NULL);
3277 + /* no restart of the timer is done here! */
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");
3288 +static void bcm_rx_thr_handler(unsigned long data)
3290 + struct bcm_op *op = (struct bcm_op*)data;
3293 + op->thrtimer.expires = 0; /* mark disabled / consumed timer */
3295 + if (op->nframes > 1){
3297 + DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
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]);
3309 + DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
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]);
3319 +static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
3320 + struct can_frame *frames, struct timeval *tv)
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;
3329 + skb = alloc_skb(sizeof(*head) + datalen,
3330 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
3334 + memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
3335 + /* can_frames starting here */
3336 + firstframe = (struct can_frame *) skb->tail;
3339 + skb_set_timestamp(skb, tv); /* restore timestamp */
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;
3347 + if (head->nframes){
3348 + memcpy(skb_put(skb, datalen), frames, datalen);
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;
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);
3361 + bo->dropped_usr_msgs++; /* don't care about overflows */
3365 +/**************************************************/
3366 +/* bcm_op handling: find & delete bcm_op elements */
3367 +/**************************************************/
3369 +static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
3372 + struct bcm_op *op;
3374 + list_for_each_entry(op, ops, list)
3375 + if ((op->can_id == can_id) && (op->ifindex == ifindex))
3381 +static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
3383 + struct bcm_op *op, *n;
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",
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);
3397 + can_rx_unregister(dev, op->can_id,
3398 + REGMASK(op->can_id),
3399 + bcm_rx_handler, op);
3403 + can_rx_unregister(NULL, op->can_id,
3404 + REGMASK(op->can_id),
3405 + bcm_rx_handler, op);
3407 + list_del(&op->list);
3408 + bcm_remove_op(op);
3409 + return 1; /* done */
3413 + return 0; /* not found */
3416 +static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
3418 + struct bcm_op *op, *n;
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",
3424 + list_del(&op->list);
3425 + bcm_remove_op(op);
3426 + return 1; /* done */
3430 + return 0; /* not found */
3433 +static void bcm_remove_op(struct bcm_op *op)
3435 + del_timer(&op->timer);
3436 + del_timer(&op->thrtimer);
3438 + kfree(op->frames);
3439 + if (op->last_frames)
3440 + kfree(op->last_frames);
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
3453 +# Controller Area Network (CAN) network layer core configuration
3458 + tristate "CAN bus subsystem support"
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/>.
3466 + If you want CAN support, you should say Y here and also to the
3467 + specific driver for your controller(s) below.
3469 + This CAN support can also be built as a module. If so, the module
3470 + will be called can.ko.
3473 + tristate "Raw CAN Protocol (raw access with CAN-ID filtering)"
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.
3483 +config CAN_RAW_USER
3484 + bool "Allow non-root users to access Raw CAN Protocol sockets"
3485 + depends on CAN_RAW
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.
3492 + tristate "Broadcast Manager CAN Protocol (with content filtering)"
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.
3504 +config CAN_BCM_USER
3505 + bool "Allow non-root users to access CAN broadcast manager sockets"
3506 + depends on CAN_BCM
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.
3512 +config CAN_DEBUG_CORE
3513 + bool "CAN Core debugging messages"
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.
3521 +source "drivers/net/can/Kconfig"
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
3528 +# Makefile for the Linux Controller Area Network core.
3531 +obj-$(CONFIG_CAN) += can.o
3532 +can-objs := af_can.o proc.o
3534 +obj-$(CONFIG_CAN_RAW) += can-raw.o
3535 +can-raw-objs := raw.o
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
3544 + * proc.c - procfs support for Protocol family CAN core module
3546 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
3547 + * All rights reserved.
3549 + * Redistribution and use in source and binary forms, with or without
3550 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
3583 + * Send feedback to <socketcan-users@lists.berlios.de>
3587 +#include <linux/module.h>
3588 +#include <linux/proc_fs.h>
3589 +#include <linux/list.h>
3590 +#include <linux/rcupdate.h>
3592 +#include <linux/can/core.h>
3593 +#include <linux/can/version.h>
3595 +#include "af_can.h"
3597 +RCSID("$Id: proc.c 177 2007-03-08 11:02:43Z hartkopp $");
3599 +/* proc filenames */
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"
3611 +static void can_init_stats(int caller);
3612 +static void can_stat_update(unsigned long data);
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);
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);
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;
3650 +struct timer_list stattimer; /* timer for statistics update */
3652 +struct s_stats stats; /* statistics */
3653 +struct s_pstats pstats;
3655 +extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
3656 +extern int stats_timer; /* module parameter. default: on */
3658 +/**************************************************/
3659 +/* procfs init / remove */
3660 +/**************************************************/
3662 +void can_init_proc(void)
3667 + /* create /proc/can directory */
3668 + can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
3671 + printk(KERN_INFO "CAN: failed to create CAN_PROC_DIR. "
3672 + "CONFIG_PROC_FS missing?\n");
3676 + can_dir->owner = THIS_MODULE;
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);
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 */
3707 +void can_remove_proc(void)
3709 + /* procfs remove */
3711 + can_remove_proc_readentry(CAN_PROC_VERSION);
3714 + can_remove_proc_readentry(CAN_PROC_STATS);
3716 + if (pde_reset_stats)
3717 + can_remove_proc_readentry(CAN_PROC_RESET_STATS);
3719 + if (pde_rcvlist_all)
3720 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
3722 + if (pde_rcvlist_fil)
3723 + can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
3725 + if (pde_rcvlist_inv)
3726 + can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
3728 + if (pde_rcvlist_sff)
3729 + can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
3731 + if (pde_rcvlist_eff)
3732 + can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
3734 + if (pde_rcvlist_err)
3735 + can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
3738 + remove_proc_entry(CAN_PROC_DIR, NULL);
3741 +/**************************************************/
3742 +/* proc read functions */
3743 +/**************************************************/
3745 +static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
3746 + struct net_device *dev)
3748 + struct receiver *r;
3749 + struct hlist_node *n;
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";
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);
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,
3771 + rcu_read_unlock();
3776 +static int can_print_recv_banner(char *page, int len)
3778 + /* can1. 00000000 00000000 00000000
3780 + len += snprintf(page + len, PAGE_SIZE - len,
3781 + " device can_id can_mask function"
3782 + " userdata matches ident\n");
3787 +static int can_proc_read_stats(char *page, char **start, off_t off,
3788 + int count, int *eof, void *data)
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);
3800 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3802 + len += snprintf(page + len, PAGE_SIZE - len,
3803 + " %8ld %% total match ratio (RXMR)\n",
3804 + stats.total_rx_match_ratio);
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);
3813 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3815 + len += snprintf(page + len, PAGE_SIZE - len,
3816 + " %8ld %% current match ratio (CRXMR)\n",
3817 + stats.current_rx_match_ratio);
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);
3826 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3828 + len += snprintf(page + len, PAGE_SIZE - len,
3829 + " %8ld %% max match ratio (MRXMR)\n",
3830 + stats.max_rx_match_ratio);
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);
3839 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
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);
3848 + if (pstats.stats_reset)
3849 + len += snprintf(page + len, PAGE_SIZE - len,
3850 + "\n %8ld statistic resets (STR)\n",
3851 + pstats.stats_reset);
3853 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3859 +static int can_proc_read_reset_stats(char *page, char **start, off_t off,
3860 + int count, int *eof, void *data)
3864 + can_init_stats(1);
3866 + len += snprintf(page + len, PAGE_SIZE - len,
3867 + "CAN statistic reset #%ld done.\n",
3868 + pstats.stats_reset);
3874 +static int can_proc_read_version(char *page, char **start, off_t off,
3875 + int count, int *eof, void *data)
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);
3886 +static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
3887 + int count, int *eof, void *data)
3890 + struct dev_rcv_lists *d;
3891 + struct hlist_node *n;
3894 + len += snprintf(page + len, PAGE_SIZE - len,
3895 + "\nreceive list 'rx_all':\n");
3897 + /* find receive list for this device */
3899 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
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);
3905 + len += snprintf(page + len, PAGE_SIZE - len,
3906 + " (%s: no entry)\n", DNAME(d->dev));
3908 + if (len > PAGE_SIZE - 100)
3909 + break; /* exit on end of buffer */
3911 + rcu_read_unlock();
3913 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3919 +static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
3920 + int count, int *eof, void *data)
3923 + struct dev_rcv_lists *d;
3924 + struct hlist_node *n;
3927 + len += snprintf(page + len, PAGE_SIZE - len,
3928 + "\nreceive list 'rx_fil':\n");
3930 + /* find receive list for this device */
3932 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
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);
3938 + len += snprintf(page + len, PAGE_SIZE - len,
3939 + " (%s: no entry)\n", DNAME(d->dev));
3941 + if (len > PAGE_SIZE - 100)
3942 + break; /* exit on end of buffer */
3944 + rcu_read_unlock();
3946 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3952 +static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
3953 + int count, int *eof, void *data)
3956 + struct dev_rcv_lists *d;
3957 + struct hlist_node *n;
3960 + len += snprintf(page + len, PAGE_SIZE - len,
3961 + "\nreceive list 'rx_inv':\n");
3963 + /* find receive list for this device */
3965 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
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);
3971 + len += snprintf(page + len, PAGE_SIZE - len,
3972 + " (%s: no entry)\n", DNAME(d->dev));
3974 + if (len > PAGE_SIZE - 100)
3975 + break; /* exit on end of buffer */
3977 + rcu_read_unlock();
3979 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
3985 +static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
3986 + int count, int *eof, void *data)
3989 + struct dev_rcv_lists *d;
3990 + struct hlist_node *n;
3993 + len += snprintf(page + len, PAGE_SIZE - len,
3994 + "\nreceive list 'rx_sff':\n");
3996 + /* find receive list for this device */
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])) {
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,
4017 + len += snprintf(page + len, PAGE_SIZE - len,
4018 + " (%s: no entry)\n", DNAME(d->dev));
4020 + if (len > PAGE_SIZE - 100)
4021 + break; /* exit on end of buffer */
4023 + rcu_read_unlock();
4025 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
4031 +static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
4032 + int count, int *eof, void *data)
4035 + struct dev_rcv_lists *d;
4036 + struct hlist_node *n;
4039 + len += snprintf(page + len, PAGE_SIZE - len,
4040 + "\nreceive list 'rx_eff':\n");
4042 + /* find receive list for this device */
4044 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
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);
4050 + len += snprintf(page + len, PAGE_SIZE - len,
4051 + " (%s: no entry)\n", DNAME(d->dev));
4053 + if (len > PAGE_SIZE - 100)
4054 + break; /* exit on end of buffer */
4056 + rcu_read_unlock();
4058 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
4064 +static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
4065 + int count, int *eof, void *data)
4068 + struct dev_rcv_lists *d;
4069 + struct hlist_node *n;
4072 + len += snprintf(page + len, PAGE_SIZE - len,
4073 + "\nreceive list 'rx_err':\n");
4075 + /* find receive list for this device */
4077 + hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
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);
4083 + len += snprintf(page + len, PAGE_SIZE - len,
4084 + " (%s: no entry)\n", DNAME(d->dev));
4086 + if (len > PAGE_SIZE - 100)
4087 + break; /* exit on end of buffer */
4089 + rcu_read_unlock();
4091 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
4097 +/**************************************************/
4098 +/* proc utility functions */
4099 +/**************************************************/
4101 +static struct proc_dir_entry *can_create_proc_readentry(const char *name,
4103 + read_proc_t* read_proc,
4107 + return create_proc_read_entry(name, mode, can_dir, read_proc,
4113 +static void can_remove_proc_readentry(const char *name)
4116 + remove_proc_entry(name, can_dir);
4119 +static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
4120 + unsigned long count)
4122 + unsigned long ret = 0;
4124 + if (oldjif == newjif)
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",
4134 + ret = (count * HZ) / (newjif - oldjif);
4139 +/**************************************************/
4140 +/* af_can statistics stuff */
4141 +/**************************************************/
4143 +static void can_init_stats(int caller)
4145 + memset(&stats, 0, sizeof(stats));
4146 + stats.jiffies_init = jiffies;
4147 + pstats.stats_reset++;
4150 +static void can_stat_update(unsigned long data)
4152 + unsigned long j = jiffies; /* snapshot */
4154 + //DBG("CAN: can_stat_update() jiffies = %ld\n", j);
4156 + if (j < stats.jiffies_init) /* jiffies overflow */
4157 + can_init_stats(2);
4159 + /* stats.rx_frames is the definitively max. statistic value */
4161 + /* prevent overflow in calc_rate() */
4162 + if (stats.rx_frames > (ULONG_MAX / HZ))
4163 + can_init_stats(3); /* restart */
4165 + /* matches overflow - very improbable */
4166 + if (stats.matches > (ULONG_MAX / 100))
4167 + can_init_stats(4);
4169 + /* calc total values */
4170 + if (stats.rx_frames)
4171 + stats.total_rx_match_ratio = (stats.matches * 100) /
4174 + stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
4176 + stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
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;
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);
4187 + /* check / update maximum values */
4188 + if (stats.max_tx_rate < stats.current_tx_rate)
4189 + stats.max_tx_rate = stats.current_tx_rate;
4191 + if (stats.max_rx_rate < stats.current_rx_rate)
4192 + stats.max_rx_rate = stats.current_rx_rate;
4194 + if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
4195 + stats.max_rx_match_ratio = stats.current_rx_match_ratio;
4197 + /* clear values for 'current rate' calculation */
4198 + stats.tx_frames_delta = 0;
4199 + stats.rx_frames_delta = 0;
4200 + stats.matches_delta = 0;
4202 + /* restart timer */
4203 + stattimer.expires = jiffies + HZ; /* every second */
4204 + add_timer(&stattimer);
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
4211 + * raw.c - Raw sockets for protocol family CAN
4213 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
4214 + * All rights reserved.
4216 + * Redistribution and use in source and binary forms, with or without
4217 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
4250 + * Send feedback to <socketcan-users@lists.berlios.de>
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>
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>
4270 +RCSID("$Id: raw.c 177 2007-03-08 11:02:43Z hartkopp $");
4272 +#define IDENT "raw"
4273 +static __initdata const char banner[] = KERN_INFO "CAN: raw socket protocol"
4276 +MODULE_DESCRIPTION("PF_CAN raw sockets");
4277 +MODULE_LICENSE("Dual BSD/GPL");
4278 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
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)
4288 +#define DBG(args...)
4289 +#define DBG_SKB(skb)
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);
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);
4314 +static struct proto_ops raw_ops = {
4316 + .release = raw_release,
4318 + .connect = sock_no_connect,
4319 + .socketpair = sock_no_socketpair,
4320 + .accept = sock_no_accept,
4321 + .getname = raw_getname,
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,
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.
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;
4355 +#ifdef CONFIG_CAN_RAW_USER
4356 +#define RAW_CAP (-1)
4358 +#define RAW_CAP CAP_NET_RAW
4361 +#undef CAN_RAW_SUPPORT_REBIND /* use bind on already bound socket */
4363 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
4366 + struct raw_opt opt;
4369 +#define raw_sk(sk) (&((struct raw_sock *)(sk))->opt)
4371 +static struct proto raw_proto = {
4372 + .name = "CAN_RAW",
4373 + .owner = THIS_MODULE,
4374 + .obj_size = sizeof(struct raw_sock),
4378 +static struct can_proto raw_can_proto = {
4380 + .protocol = CAN_RAW,
4381 + .capability = RAW_CAP,
4383 + .prot = &raw_proto,
4386 +#define raw_sk(sk) ((struct raw_opt *)(sk)->sk_protinfo)
4388 +static struct can_proto raw_can_proto = {
4390 + .protocol = CAN_RAW,
4391 + .capability = RAW_CAP,
4393 + .owner = THIS_MODULE,
4394 + .obj_size = sizeof(struct raw_opt),
4401 +static __init int raw_module_init(void)
4405 + can_proto_register(&raw_can_proto);
4409 +static __exit void raw_module_exit(void)
4411 + can_proto_unregister(&raw_can_proto);
4414 +static int raw_init(struct sock *sk)
4416 + struct raw_opt *ro = raw_sk(sk);
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;
4426 + /* set default loopback behaviour */
4428 + ro->recv_own_msgs = 0;
4433 +static int raw_release(struct socket *sock)
4435 + struct sock *sk = sock->sk;
4436 + struct raw_opt *ro = raw_sk(sk);
4437 + struct net_device *dev = NULL;
4439 + DBG("socket %p, sk %p, refcnt %d\n", sock, sk,
4440 + atomic_read(&sk->sk_refcnt));
4442 + if (ro->bound && ro->ifindex)
4443 + dev = dev_get_by_index(ro->ifindex);
4445 + /* remove current filters & unregister */
4447 + raw_remove_filters(dev, sk);
4448 + if (ro->count > 1)
4449 + kfree(ro->filter);
4451 + /* remove current error mask */
4452 + if (ro->err_mask && ro->bound)
4453 + can_rx_unregister(dev, 0, ro->err_mask | CAN_ERR_FLAG,
4457 + can_dev_unregister(dev, raw_notifier, sk);
4466 +static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
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;
4473 + DBG("socket %p to device %d\n", sock, addr->can_ifindex);
4475 + if (len < sizeof(*addr))
4479 +#ifdef CAN_RAW_SUPPORT_REBIND
4480 + /* remove current bindings / notifier */
4481 + if (ro->ifindex) {
4482 + dev = dev_get_by_index(ro->ifindex);
4484 + DBG("could not find device %d\n",
4485 + addr->can_ifindex);
4488 + if (!(dev->flags & IFF_UP)) {
4489 + sk->sk_err = ENETDOWN;
4490 + if (!sock_flag(sk, SOCK_DEAD))
4491 + sk->sk_error_report(sk);
4494 + can_dev_unregister(dev, raw_notifier, sk);
4498 + /* unregister current filters for this device */
4499 + raw_remove_filters(dev, sk);
4510 + if (addr->can_ifindex) {
4511 + dev = dev_get_by_index(addr->can_ifindex);
4513 + DBG("could not find device %d\n", addr->can_ifindex);
4516 + if (!(dev->flags & IFF_UP)) {
4517 + sk->sk_err = ENETDOWN;
4518 + if (!sock_flag(sk, SOCK_DEAD))
4519 + sk->sk_error_report(sk);
4522 + can_dev_register(dev, raw_notifier, sk);
4526 + ro->ifindex = addr->can_ifindex;
4528 + raw_add_filters(dev, sk); /* filters set by default/setsockopt */
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);
4543 +static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
4544 + int *len, int peer)
4546 + struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
4547 + struct sock *sk = sock->sk;
4548 + struct raw_opt *ro = raw_sk(sk);
4551 + return -EOPNOTSUPP;
4553 + addr->can_family = AF_CAN;
4554 + addr->can_ifindex = ro->ifindex;
4555 + *len = sizeof(*addr);
4560 +static unsigned int raw_poll(struct file *file, struct socket *sock,
4563 + unsigned int mask = 0;
4565 + DBG("socket %p\n", sock);
4567 + mask = datagram_poll(file, sock, wait);
4571 +static int raw_setsockopt(struct socket *sock, int level, int optname,
4572 + char __user *optval, int optlen)
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;
4583 + if (level != SOL_CAN_RAW)
4586 + switch (optname) {
4587 + case CAN_RAW_FILTER:
4588 + if (optlen % sizeof(struct can_filter) != 0)
4591 + count = optlen / sizeof(struct can_filter);
4593 + if (count > 1) { /* does not fit into dfilter */
4594 + if (!(filter = kmalloc(optlen, GFP_KERNEL)))
4596 + if ((err = copy_from_user(filter, optval, optlen))) {
4600 + } else if (count == 1) {
4601 + if ((err = copy_from_user(&sfilter, optval, optlen)))
4605 + if (ro->bound && ro->ifindex)
4606 + dev = dev_get_by_index(ro->ifindex);
4608 + /* remove current filters & unregister */
4610 + raw_remove_filters(dev, sk);
4611 + if (ro->count > 1)
4612 + kfree(ro->filter);
4614 + if (count == 1) { /* copy data for single filter */
4615 + ro->dfilter = sfilter;
4616 + filter = &ro->dfilter;
4619 + /* add new filters & register */
4620 + ro->filter = filter;
4621 + ro->count = count;
4623 + raw_add_filters(dev, sk);
4630 + case CAN_RAW_ERR_FILTER:
4631 + if (optlen != sizeof(err_mask))
4633 + if ((err = copy_from_user(&err_mask, optval, optlen)))
4636 + err_mask &= CAN_ERR_MASK;
4638 + if (ro->bound && ro->ifindex)
4639 + dev = dev_get_by_index(ro->ifindex);
4641 + /* remove current error mask */
4642 + if (ro->err_mask && ro->bound)
4643 + can_rx_unregister(dev, 0, ro->err_mask | CAN_ERR_FLAG,
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);
4657 + case CAN_RAW_LOOPBACK:
4658 + if (optlen != sizeof(ro->loopback))
4660 + if ((err = copy_from_user(&ro->loopback, optval, optlen)))
4664 + case CAN_RAW_RECV_OWN_MSGS:
4665 + if (optlen != sizeof(ro->recv_own_msgs))
4667 + if ((err = copy_from_user(&ro->recv_own_msgs, optval, optlen)))
4672 + return -ENOPROTOOPT;
4677 +static int raw_getsockopt(struct socket *sock, int level, int optname,
4678 + char __user *optval, int __user *optlen)
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;
4686 + if (level != SOL_CAN_RAW)
4689 + switch (optname) {
4690 + case CAN_RAW_FILTER:
4691 + if (get_user(len, optlen))
4694 + if (count && filter) {
4695 + int filter_size = count * sizeof(struct can_filter);
4696 + if (len < filter_size)
4698 + if (len > filter_size)
4699 + len = filter_size;
4700 + if (copy_to_user(optval, filter, len))
4705 + if (put_user(len, optlen))
4710 + case CAN_RAW_ERR_FILTER:
4711 + if (get_user(len, optlen))
4714 + if (len < sizeof(can_err_mask_t))
4717 + if (len > sizeof(can_err_mask_t))
4718 + len = sizeof(can_err_mask_t);
4720 + if (copy_to_user(optval, &ro->err_mask, len))
4723 + if (put_user(len, optlen))
4728 + case CAN_RAW_LOOPBACK:
4729 + if (get_user(len, optlen))
4732 + if (len < sizeof(int))
4735 + if (len > sizeof(int))
4736 + len = sizeof(int);
4738 + if (copy_to_user(optval, &ro->loopback, len))
4741 + if (put_user(len, optlen))
4746 + case CAN_RAW_RECV_OWN_MSGS:
4747 + if (get_user(len, optlen))
4750 + if (len < sizeof(int))
4753 + if (len > sizeof(int))
4754 + len = sizeof(int);
4756 + if (copy_to_user(optval, &ro->recv_own_msgs, len))
4759 + if (put_user(len, optlen))
4765 + return -ENOPROTOOPT;
4770 +static void raw_add_filters(struct net_device *dev, struct sock *sk)
4772 + struct raw_opt *ro = raw_sk(sk);
4773 + struct can_filter *filter = ro->filter;
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);
4785 +static void raw_remove_filters(struct net_device *dev, struct sock *sk)
4787 + struct raw_opt *ro = raw_sk(sk);
4788 + struct can_filter *filter = ro->filter;
4791 + for (i = 0; i < ro->count; i++) {
4792 + can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
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);
4800 +static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
4801 + struct msghdr *msg, size_t size)
4803 + struct sock *sk = sock->sk;
4804 + struct raw_opt *ro = raw_sk(sk);
4805 + struct sk_buff *skb;
4806 + struct net_device *dev;
4810 + DBG("socket %p, sk %p\n", sock, sk);
4812 + if (msg->msg_name) {
4813 + struct sockaddr_can *addr =
4814 + (struct sockaddr_can *)msg->msg_name;
4815 + if (addr->can_family != AF_CAN)
4817 + ifindex = addr->can_ifindex;
4819 + ifindex = ro->ifindex;
4821 + if (!(dev = dev_get_by_index(ifindex))) {
4822 + DBG("device %d not found\n", ifindex);
4826 + if (!(skb = alloc_skb(size, GFP_KERNEL))) {
4831 + if ((err = memcpy_fromiovec(skb_put(skb, size),
4832 + msg->msg_iov, size)) < 0) {
4840 + DBG("sending skbuff to interface %d\n", ifindex);
4843 + err = can_send(skb, ro->loopback);
4853 +static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
4854 + struct msghdr *msg, size_t size, int flags)
4856 + struct sock *sk = sock->sk;
4857 + struct sk_buff *skb;
4861 + DBG("socket %p, sk %p\n", sock, sk);
4863 + noblock = flags & MSG_DONTWAIT;
4864 + flags &= ~MSG_DONTWAIT;
4866 + if (!(skb = skb_recv_datagram(sk, flags, noblock, &error)))
4869 + DBG("delivering skbuff %p\n", skb);
4872 + if (size < skb->len)
4873 + msg->msg_flags |= MSG_TRUNC;
4877 + if ((error = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
4878 + skb_free_datagram(sk, skb);
4882 + sock_recv_timestamp(msg, sk, skb);
4884 + if (msg->msg_name) {
4885 + msg->msg_namelen = sizeof(struct sockaddr_can);
4886 + memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
4889 + DBG("freeing sock %p, skbuff %p\n", sk, skb);
4890 + skb_free_datagram(sk, skb);
4895 +static void raw_rcv(struct sk_buff *skb, void *data)
4897 + struct sock *sk = (struct sock*)data;
4898 + struct raw_opt *ro = raw_sk(sk);
4899 + struct sockaddr_can *addr;
4902 + DBG("received skbuff %p, sk %p\n", skb, sk);
4905 + if (!ro->recv_own_msgs) {
4906 + if (*(struct sock **)skb->cb == sk) { /* tx sock reference */
4907 + DBG("trashed own tx msg\n");
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;
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);
4925 +static void raw_notifier(unsigned long msg, void *data)
4927 + struct sock *sk = (struct sock *)data;
4928 + struct raw_opt *ro = raw_sk(sk);
4930 + DBG("called for sock %p\n", sk);
4933 + case NETDEV_UNREGISTER:
4938 + sk->sk_err = ENETDOWN;
4939 + if (!sock_flag(sk, SOCK_DEAD))
4940 + sk->sk_error_report(sk);
4946 +module_init(raw_module_init);
4947 +module_exit(raw_module_exit);