2 CAN: Add virtual CAN netdevice driver
4 This patch adds the virtual CAN bus (vcan) network driver.
5 The vcan device is just a loopback device for CAN frames, no
6 real CAN hardware is involved.
8 Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
9 Signed-off-by: Urs Thuermann <urs.thuermann@volkswagen.de>
12 drivers/net/Makefile | 1
13 drivers/net/can/Kconfig | 25 +++++
14 drivers/net/can/Makefile | 5 +
15 drivers/net/can/vcan.c | 207 +++++++++++++++++++++++++++++++++++++++++++++++
17 5 files changed, 241 insertions(+)
19 Index: net-2.6.24/drivers/net/Makefile
20 ===================================================================
21 --- net-2.6.24.orig/drivers/net/Makefile 2007-10-05 11:08:03.000000000 +0200
22 +++ net-2.6.24/drivers/net/Makefile 2007-10-05 11:17:41.000000000 +0200
24 obj-$(CONFIG_CHELSIO_T1) += chelsio/
25 obj-$(CONFIG_CHELSIO_T3) += cxgb3/
26 obj-$(CONFIG_EHEA) += ehea/
27 +obj-$(CONFIG_CAN) += can/
28 obj-$(CONFIG_BONDING) += bonding/
29 obj-$(CONFIG_ATL1) += atl1/
30 obj-$(CONFIG_GIANFAR) += gianfar_driver.o
31 Index: net-2.6.24/drivers/net/can/Kconfig
32 ===================================================================
33 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
34 +++ net-2.6.24/drivers/net/can/Kconfig 2007-10-05 11:17:41.000000000 +0200
36 +menu "CAN Device Drivers"
40 + tristate "Virtual Local CAN Interface (vcan)"
44 + Similar to the network loopback devices, vcan offers a
45 + virtual local CAN interface.
47 + This driver can also be built as a module. If so, the module
48 + will be called vcan.
50 +config CAN_DEBUG_DEVICES
51 + bool "CAN devices debugging messages"
55 + Say Y here if you want the CAN device drivers to produce a bunch of
56 + debug messages to the system log. Select this if you are having
57 + a problem with CAN support and want to see more of what is going
61 Index: net-2.6.24/drivers/net/can/Makefile
62 ===================================================================
63 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
64 +++ net-2.6.24/drivers/net/can/Makefile 2007-10-05 11:17:41.000000000 +0200
67 +# Makefile for the Linux Controller Area Network drivers.
70 +obj-$(CONFIG_CAN_VCAN) += vcan.o
71 Index: net-2.6.24/drivers/net/can/vcan.c
72 ===================================================================
73 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
74 +++ net-2.6.24/drivers/net/can/vcan.c 2007-10-05 11:17:41.000000000 +0200
77 + * vcan.c - Virtual CAN interface
79 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
80 + * All rights reserved.
82 + * Redistribution and use in source and binary forms, with or without
83 + * modification, are permitted provided that the following conditions
85 + * 1. Redistributions of source code must retain the above copyright
86 + * notice, this list of conditions and the following disclaimer.
87 + * 2. Redistributions in binary form must reproduce the above copyright
88 + * notice, this list of conditions and the following disclaimer in the
89 + * documentation and/or other materials provided with the distribution.
90 + * 3. Neither the name of Volkswagen nor the names of its contributors
91 + * may be used to endorse or promote products derived from this software
92 + * without specific prior written permission.
94 + * Alternatively, provided that this notice is retained in full, this
95 + * software may be distributed under the terms of the GNU General
96 + * Public License ("GPL") version 2, in which case the provisions of the
97 + * GPL apply INSTEAD OF those given above.
99 + * The provided data structures and external interfaces from this code
100 + * are not restricted to be used by modules with a GPL compatible license.
102 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
103 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
104 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
105 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
106 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
107 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
108 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
109 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
110 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
111 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
112 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
115 + * Send feedback to <socketcan-users@lists.berlios.de>
119 +#include <linux/module.h>
120 +#include <linux/init.h>
121 +#include <linux/netdevice.h>
122 +#include <linux/if_arp.h>
123 +#include <linux/if_ether.h>
124 +#include <linux/can.h>
125 +#include <net/rtnetlink.h>
127 +static __initdata const char banner[] =
128 + KERN_INFO "vcan: Virtual CAN interface driver\n";
130 +MODULE_DESCRIPTION("virtual CAN interface");
131 +MODULE_LICENSE("Dual BSD/GPL");
132 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
134 +#ifdef CONFIG_CAN_DEBUG_DEVICES
135 +static int vcan_debug;
136 +module_param_named(debug, vcan_debug, int, S_IRUGO);
139 +/* To be moved to linux/can/dev.h */
140 +#ifdef CONFIG_CAN_DEBUG_DEVICES
141 +#define DBG(fmt, args...) (vcan_debug & 1 ? \
142 + printk(KERN_DEBUG "vcan %s: " fmt, \
143 + __func__, ##args) : 0)
145 +#define DBG(fmt, args...)
150 + * CAN test feature:
151 + * Enable the echo on driver level for testing the CAN core echo modes.
152 + * See Documentation/networking/can.txt for details.
155 +static int echo; /* echo testing. Default: 0 (Off) */
156 +module_param(echo, bool, S_IRUGO);
157 +MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
160 +static int vcan_open(struct net_device *dev)
162 + DBG("%s: interface up\n", dev->name);
164 + netif_start_queue(dev);
168 +static int vcan_stop(struct net_device *dev)
170 + DBG("%s: interface down\n", dev->name);
172 + netif_stop_queue(dev);
176 +static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
178 + struct net_device_stats *stats = &dev->stats;
180 + stats->rx_packets++;
181 + stats->rx_bytes += skb->len;
183 + skb->protocol = htons(ETH_P_CAN);
184 + skb->pkt_type = PACKET_BROADCAST;
186 + skb->ip_summed = CHECKSUM_UNNECESSARY;
188 + DBG("received skbuff on interface %d\n", dev->ifindex);
193 +static int vcan_tx(struct sk_buff *skb, struct net_device *dev)
195 + struct net_device_stats *stats = &dev->stats;
198 + DBG("sending skbuff on interface %s\n", dev->name);
200 + stats->tx_packets++;
201 + stats->tx_bytes += skb->len;
203 + /* set flag whether this packet has to be looped back */
204 + loop = skb->pkt_type == PACKET_LOOPBACK;
207 + /* no echo handling available inside this driver */
211 + * only count the packets here, because the
212 + * CAN core already did the echo for us
214 + stats->rx_packets++;
215 + stats->rx_bytes += skb->len;
221 + /* perform standard echo handling for CAN network interfaces */
224 + struct sock *srcsk = skb->sk;
226 + skb = skb_share_check(skb, GFP_ATOMIC);
230 + /* receive with packet counting */
234 + /* no looped packets => no counting */
240 +static void vcan_setup(struct net_device *dev)
242 + DBG("dev %s\n", dev->name);
244 + dev->type = ARPHRD_CAN;
245 + dev->mtu = sizeof(struct can_frame);
246 + dev->hard_header_len = 0;
248 + dev->tx_queue_len = 0;
249 + dev->flags = IFF_NOARP;
251 + /* set flags according to driver capabilities */
253 + dev->flags |= IFF_ECHO;
255 + dev->open = vcan_open;
256 + dev->stop = vcan_stop;
257 + dev->hard_start_xmit = vcan_tx;
258 + dev->destructor = free_netdev;
261 +static struct rtnl_link_ops vcan_link_ops __read_mostly = {
263 + .setup = vcan_setup,
266 +static __init int vcan_init_module(void)
271 + printk(KERN_INFO "vcan: enabled echo on driver level.\n");
273 + return rtnl_link_register(&vcan_link_ops);
276 +static __exit void vcan_cleanup_module(void)
278 + rtnl_link_unregister(&vcan_link_ops);
281 +module_init(vcan_init_module);
282 +module_exit(vcan_cleanup_module);
283 Index: net-2.6.24/net/can/Kconfig
284 ===================================================================
285 --- net-2.6.24.orig/net/can/Kconfig 2007-10-05 11:17:28.000000000 +0200
286 +++ net-2.6.24/net/can/Kconfig 2007-10-05 11:18:19.000000000 +0200
288 Say Y here if you want the CAN core to produce a bunch of debug
289 messages. Select this if you are having a problem with CAN
290 support and want to see more of what is going on.
293 +source "drivers/net/can/Kconfig"