xref: /OK3568_Linux_fs/kernel/drivers/net/can/vcan.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* vcan.c - Virtual CAN interface
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
4*4882a593Smuzhiyun  * All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
7*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
8*4882a593Smuzhiyun  * are met:
9*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
10*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
11*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
12*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
13*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
14*4882a593Smuzhiyun  * 3. Neither the name of Volkswagen nor the names of its contributors
15*4882a593Smuzhiyun  *    may be used to endorse or promote products derived from this software
16*4882a593Smuzhiyun  *    without specific prior written permission.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Alternatively, provided that this notice is retained in full, this
19*4882a593Smuzhiyun  * software may be distributed under the terms of the GNU General
20*4882a593Smuzhiyun  * Public License ("GPL") version 2, in which case the provisions of the
21*4882a593Smuzhiyun  * GPL apply INSTEAD OF those given above.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * The provided data structures and external interfaces from this code
24*4882a593Smuzhiyun  * are not restricted to be used by modules with a GPL compatible license.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
37*4882a593Smuzhiyun  * DAMAGE.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include <linux/module.h>
44*4882a593Smuzhiyun #include <linux/init.h>
45*4882a593Smuzhiyun #include <linux/netdevice.h>
46*4882a593Smuzhiyun #include <linux/if_arp.h>
47*4882a593Smuzhiyun #include <linux/if_ether.h>
48*4882a593Smuzhiyun #include <linux/can.h>
49*4882a593Smuzhiyun #include <linux/can/can-ml.h>
50*4882a593Smuzhiyun #include <linux/can/dev.h>
51*4882a593Smuzhiyun #include <linux/can/skb.h>
52*4882a593Smuzhiyun #include <linux/slab.h>
53*4882a593Smuzhiyun #include <net/rtnetlink.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #define DRV_NAME "vcan"
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun MODULE_DESCRIPTION("virtual CAN interface");
58*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
59*4882a593Smuzhiyun MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
60*4882a593Smuzhiyun MODULE_ALIAS_RTNL_LINK(DRV_NAME);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* CAN test feature:
63*4882a593Smuzhiyun  * Enable the echo on driver level for testing the CAN core echo modes.
64*4882a593Smuzhiyun  * See Documentation/networking/can.rst for details.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static bool echo; /* echo testing. Default: 0 (Off) */
68*4882a593Smuzhiyun module_param(echo, bool, 0444);
69*4882a593Smuzhiyun MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
70*4882a593Smuzhiyun 
vcan_rx(struct sk_buff * skb,struct net_device * dev)71*4882a593Smuzhiyun static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
74*4882a593Smuzhiyun 	struct net_device_stats *stats = &dev->stats;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	stats->rx_packets++;
77*4882a593Smuzhiyun 	stats->rx_bytes += cfd->len;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	skb->pkt_type  = PACKET_BROADCAST;
80*4882a593Smuzhiyun 	skb->dev       = dev;
81*4882a593Smuzhiyun 	skb->ip_summed = CHECKSUM_UNNECESSARY;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	netif_rx_ni(skb);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
vcan_tx(struct sk_buff * skb,struct net_device * dev)86*4882a593Smuzhiyun static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
89*4882a593Smuzhiyun 	struct net_device_stats *stats = &dev->stats;
90*4882a593Smuzhiyun 	int loop;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (can_dropped_invalid_skb(dev, skb))
93*4882a593Smuzhiyun 		return NETDEV_TX_OK;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	stats->tx_packets++;
96*4882a593Smuzhiyun 	stats->tx_bytes += cfd->len;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* set flag whether this packet has to be looped back */
99*4882a593Smuzhiyun 	loop = skb->pkt_type == PACKET_LOOPBACK;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (!echo) {
102*4882a593Smuzhiyun 		/* no echo handling available inside this driver */
103*4882a593Smuzhiyun 		if (loop) {
104*4882a593Smuzhiyun 			/* only count the packets here, because the
105*4882a593Smuzhiyun 			 * CAN core already did the echo for us
106*4882a593Smuzhiyun 			 */
107*4882a593Smuzhiyun 			stats->rx_packets++;
108*4882a593Smuzhiyun 			stats->rx_bytes += cfd->len;
109*4882a593Smuzhiyun 		}
110*4882a593Smuzhiyun 		consume_skb(skb);
111*4882a593Smuzhiyun 		return NETDEV_TX_OK;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* perform standard echo handling for CAN network interfaces */
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (loop) {
117*4882a593Smuzhiyun 		skb = can_create_echo_skb(skb);
118*4882a593Smuzhiyun 		if (!skb)
119*4882a593Smuzhiyun 			return NETDEV_TX_OK;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		/* receive with packet counting */
122*4882a593Smuzhiyun 		vcan_rx(skb, dev);
123*4882a593Smuzhiyun 	} else {
124*4882a593Smuzhiyun 		/* no looped packets => no counting */
125*4882a593Smuzhiyun 		consume_skb(skb);
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	return NETDEV_TX_OK;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
vcan_change_mtu(struct net_device * dev,int new_mtu)130*4882a593Smuzhiyun static int vcan_change_mtu(struct net_device *dev, int new_mtu)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	/* Do not allow changing the MTU while running */
133*4882a593Smuzhiyun 	if (dev->flags & IFF_UP)
134*4882a593Smuzhiyun 		return -EBUSY;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
137*4882a593Smuzhiyun 		return -EINVAL;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	dev->mtu = new_mtu;
140*4882a593Smuzhiyun 	return 0;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static const struct net_device_ops vcan_netdev_ops = {
144*4882a593Smuzhiyun 	.ndo_start_xmit = vcan_tx,
145*4882a593Smuzhiyun 	.ndo_change_mtu = vcan_change_mtu,
146*4882a593Smuzhiyun };
147*4882a593Smuzhiyun 
vcan_setup(struct net_device * dev)148*4882a593Smuzhiyun static void vcan_setup(struct net_device *dev)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	dev->type		= ARPHRD_CAN;
151*4882a593Smuzhiyun 	dev->mtu		= CANFD_MTU;
152*4882a593Smuzhiyun 	dev->hard_header_len	= 0;
153*4882a593Smuzhiyun 	dev->addr_len		= 0;
154*4882a593Smuzhiyun 	dev->tx_queue_len	= 0;
155*4882a593Smuzhiyun 	dev->flags		= IFF_NOARP;
156*4882a593Smuzhiyun 	can_set_ml_priv(dev, netdev_priv(dev));
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* set flags according to driver capabilities */
159*4882a593Smuzhiyun 	if (echo)
160*4882a593Smuzhiyun 		dev->flags |= IFF_ECHO;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	dev->netdev_ops		= &vcan_netdev_ops;
163*4882a593Smuzhiyun 	dev->needs_free_netdev	= true;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun static struct rtnl_link_ops vcan_link_ops __read_mostly = {
167*4882a593Smuzhiyun 	.kind = DRV_NAME,
168*4882a593Smuzhiyun 	.priv_size = sizeof(struct can_ml_priv),
169*4882a593Smuzhiyun 	.setup = vcan_setup,
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
vcan_init_module(void)172*4882a593Smuzhiyun static __init int vcan_init_module(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	pr_info("Virtual CAN interface driver\n");
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if (echo)
177*4882a593Smuzhiyun 		pr_info("enabled echo on driver level.\n");
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return rtnl_link_register(&vcan_link_ops);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
vcan_cleanup_module(void)182*4882a593Smuzhiyun static __exit void vcan_cleanup_module(void)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	rtnl_link_unregister(&vcan_link_ops);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun module_init(vcan_init_module);
188*4882a593Smuzhiyun module_exit(vcan_cleanup_module);
189