]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - patch-series/net-2.6.24/05-can-vcan-driver.diff
Rename some variables for readability.
[socketcan-devel.git] / patch-series / net-2.6.24 / 05-can-vcan-driver.diff
1 DESC
2 CAN: Add virtual CAN netdevice driver
3 EDESC
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.
7
8 Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
9 Signed-off-by: Urs Thuermann <urs.thuermann@volkswagen.de>
10
11 ---
12  drivers/net/Makefile     |    1 
13  drivers/net/can/Kconfig  |   25 +++++
14  drivers/net/can/Makefile |    5 +
15  drivers/net/can/vcan.c   |  207 +++++++++++++++++++++++++++++++++++++++++++++++
16  net/can/Kconfig          |    3 
17  5 files changed, 241 insertions(+)
18
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
23 @@ -12,6 +12,7 @@
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
35 @@ -0,0 +1,25 @@
36 +menu "CAN Device Drivers"
37 +       depends on CAN
38 +
39 +config CAN_VCAN
40 +       tristate "Virtual Local CAN Interface (vcan)"
41 +       depends on CAN
42 +       default N
43 +       ---help---
44 +         Similar to the network loopback devices, vcan offers a
45 +         virtual local CAN interface.
46 +
47 +         This driver can also be built as a module.  If so, the module
48 +         will be called vcan.
49 +
50 +config CAN_DEBUG_DEVICES
51 +       bool "CAN devices debugging messages"
52 +       depends on CAN
53 +       default N
54 +       ---help---
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
58 +         on.
59 +
60 +endmenu
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
65 @@ -0,0 +1,5 @@
66 +#
67 +#  Makefile for the Linux Controller Area Network drivers.
68 +#
69 +
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
75 @@ -0,0 +1,207 @@
76 +/*
77 + * vcan.c - Virtual CAN interface
78 + *
79 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
80 + * All rights reserved.
81 + *
82 + * Redistribution and use in source and binary forms, with or without
83 + * modification, are permitted provided that the following conditions
84 + * are met:
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.
93 + *
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.
98 + *
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.
101 + *
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
113 + * DAMAGE.
114 + *
115 + * Send feedback to <socketcan-users@lists.berlios.de>
116 + *
117 + */
118 +
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>
126 +
127 +static __initdata const char banner[] =
128 +       KERN_INFO "vcan: Virtual CAN interface driver\n";
129 +
130 +MODULE_DESCRIPTION("virtual CAN interface");
131 +MODULE_LICENSE("Dual BSD/GPL");
132 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
133 +
134 +#ifdef CONFIG_CAN_DEBUG_DEVICES
135 +static int vcan_debug;
136 +module_param_named(debug, vcan_debug, int, S_IRUGO);
137 +#endif
138 +
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)
144 +#else
145 +#define DBG(fmt, args...)
146 +#endif
147 +
148 +
149 +/*
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.
153 + */
154 +
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)");
158 +
159 +
160 +static int vcan_open(struct net_device *dev)
161 +{
162 +       DBG("%s: interface up\n", dev->name);
163 +
164 +       netif_start_queue(dev);
165 +       return 0;
166 +}
167 +
168 +static int vcan_stop(struct net_device *dev)
169 +{
170 +       DBG("%s: interface down\n", dev->name);
171 +
172 +       netif_stop_queue(dev);
173 +       return 0;
174 +}
175 +
176 +static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
177 +{
178 +       struct net_device_stats *stats = &dev->stats;
179 +
180 +       stats->rx_packets++;
181 +       stats->rx_bytes += skb->len;
182 +
183 +       skb->protocol  = htons(ETH_P_CAN);
184 +       skb->pkt_type  = PACKET_BROADCAST;
185 +       skb->dev       = dev;
186 +       skb->ip_summed = CHECKSUM_UNNECESSARY;
187 +
188 +       DBG("received skbuff on interface %d\n", dev->ifindex);
189 +
190 +       netif_rx(skb);
191 +}
192 +
193 +static int vcan_tx(struct sk_buff *skb, struct net_device *dev)
194 +{
195 +       struct net_device_stats *stats = &dev->stats;
196 +       int loop;
197 +
198 +       DBG("sending skbuff on interface %s\n", dev->name);
199 +
200 +       stats->tx_packets++;
201 +       stats->tx_bytes += skb->len;
202 +
203 +       /* set flag whether this packet has to be looped back */
204 +       loop = skb->pkt_type == PACKET_LOOPBACK;
205 +
206 +       if (!echo) {
207 +               /* no echo handling available inside this driver */
208 +
209 +               if (loop) {
210 +                       /*
211 +                        * only count the packets here, because the
212 +                        * CAN core already did the echo for us
213 +                        */
214 +                       stats->rx_packets++;
215 +                       stats->rx_bytes += skb->len;
216 +               }
217 +               kfree_skb(skb);
218 +               return 0;
219 +       }
220 +
221 +       /* perform standard echo handling for CAN network interfaces */
222 +
223 +       if (loop) {
224 +               struct sock *srcsk = skb->sk;
225 +
226 +               skb = skb_share_check(skb, GFP_ATOMIC);
227 +               if (!skb)
228 +                       return 0;
229 +
230 +               /* receive with packet counting */
231 +               skb->sk = srcsk;
232 +               vcan_rx(skb, dev);
233 +       } else {
234 +               /* no looped packets => no counting */
235 +               kfree_skb(skb);
236 +       }
237 +       return 0;
238 +}
239 +
240 +static void vcan_setup(struct net_device *dev)
241 +{
242 +       DBG("dev %s\n", dev->name);
243 +
244 +       dev->type              = ARPHRD_CAN;
245 +       dev->mtu               = sizeof(struct can_frame);
246 +       dev->hard_header_len   = 0;
247 +       dev->addr_len          = 0;
248 +       dev->tx_queue_len      = 0;
249 +       dev->flags             = IFF_NOARP;
250 +
251 +       /* set flags according to driver capabilities */
252 +       if (echo)
253 +               dev->flags |= IFF_ECHO;
254 +
255 +       dev->open              = vcan_open;
256 +       dev->stop              = vcan_stop;
257 +       dev->hard_start_xmit   = vcan_tx;
258 +       dev->destructor        = free_netdev;
259 +}
260 +
261 +static struct rtnl_link_ops vcan_link_ops __read_mostly = {
262 +       .kind           = "vcan",
263 +       .setup          = vcan_setup,
264 +};
265 +
266 +static __init int vcan_init_module(void)
267 +{
268 +       printk(banner);
269 +
270 +       if (echo)
271 +               printk(KERN_INFO "vcan: enabled echo on driver level.\n");
272 +
273 +       return rtnl_link_register(&vcan_link_ops);
274 +}
275 +
276 +static __exit void vcan_cleanup_module(void)
277 +{
278 +       rtnl_link_unregister(&vcan_link_ops);
279 +}
280 +
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
287 @@ -47,3 +47,6 @@
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.
291 +
292 +
293 +source "drivers/net/can/Kconfig"