1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/******************************************************************************
*
* $Id$
*
* Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* The IgH EtherCAT Master is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* The license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
*****************************************************************************/
/**
\file
Ethernet interface for debugging purposes.
*/
/*****************************************************************************/
#include <linux/version.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include "globals.h"
#include "master.h"
#include "debug.h"
/*****************************************************************************/
// net_device functions
int ec_dbgdev_open(struct net_device *);
int ec_dbgdev_stop(struct net_device *);
int ec_dbgdev_tx(struct sk_buff *, struct net_device *);
struct net_device_stats *ec_dbgdev_stats(struct net_device *);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
/** Device operations for debug interfaces.
*/
static const struct net_device_ops ec_dbg_netdev_ops =
{
.ndo_open = ec_dbgdev_open,
.ndo_stop = ec_dbgdev_stop,
.ndo_start_xmit = ec_dbgdev_tx,
.ndo_get_stats = ec_dbgdev_stats,
};
#endif
/*****************************************************************************/
/** Debug interface constructor.
*
* Initializes the debug object, creates a net_device and registeres it.
*
* \retval 0 Success.
* \retval <0 Error code.
*/
int ec_debug_init(
ec_debug_t *dbg, /**< Debug object. */
ec_device_t *device, /**< EtherCAT device. */
const char *name /**< Interface name. */
)
{
dbg->device = device;
dbg->registered = 0;
dbg->opened = 0;
memset(&dbg->stats, 0, sizeof(struct net_device_stats));
if (!(dbg->dev =
alloc_netdev(sizeof(ec_debug_t *), name, NET_NAME_UNKNOWN, ether_setup))) {
EC_MASTER_ERR(device->master, "Unable to allocate net_device"
" for debug object!\n");
return -ENODEV;
}
// initialize net_device
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
dbg->dev->netdev_ops = &ec_dbg_netdev_ops;
#else
dbg->dev->open = ec_dbgdev_open;
dbg->dev->stop = ec_dbgdev_stop;
dbg->dev->hard_start_xmit = ec_dbgdev_tx;
dbg->dev->get_stats = ec_dbgdev_stats;
#endif
// initialize private data
*((ec_debug_t **) netdev_priv(dbg->dev)) = dbg;
return 0;
}
/*****************************************************************************/
/** Debug interface destructor.
*
* Unregisters the net_device and frees allocated memory.
*/
void ec_debug_clear(
ec_debug_t *dbg /**< debug object */
)
{
ec_debug_unregister(dbg);
free_netdev(dbg->dev);
}
/*****************************************************************************/
/** Register debug interface.
*/
void ec_debug_register(
ec_debug_t *dbg, /**< debug object */
const struct net_device *net_dev /**< 'Real' Ethernet device. */
)
{
int result;
ec_debug_unregister(dbg);
// use the Ethernet address of the physical device for the debug device
memcpy(dbg->dev->dev_addr, net_dev->dev_addr, ETH_ALEN);
// connect the net_device to the kernel
if ((result = register_netdev(dbg->dev))) {
EC_MASTER_WARN(dbg->device->master, "Unable to register net_device:"
" error %i\n", result);
} else {
dbg->registered = 1;
}
}
/*****************************************************************************/
/** Unregister debug interface.
*/
void ec_debug_unregister(
ec_debug_t *dbg /**< debug object */
)
{
if (dbg->registered) {
dbg->opened = 0;
dbg->registered = 0;
unregister_netdev(dbg->dev);
}
}
/*****************************************************************************/
/** Sends frame data to the interface.
*/
void ec_debug_send(
ec_debug_t *dbg, /**< debug object */
const uint8_t *data, /**< frame data */
size_t size /**< size of the frame data */
)
{
struct sk_buff *skb;
if (!dbg->opened)
return;
// allocate socket buffer
if (!(skb = dev_alloc_skb(size))) {
dbg->stats.rx_dropped++;
return;
}
// copy frame contents into socket buffer
memcpy(skb_put(skb, size), data, size);
// update device statistics
dbg->stats.rx_packets++;
dbg->stats.rx_bytes += size;
// pass socket buffer to network stack
skb->dev = dbg->dev;
skb->protocol = eth_type_trans(skb, dbg->dev);
skb->ip_summed = CHECKSUM_UNNECESSARY;
netif_rx(skb);
}
/******************************************************************************
* NET_DEVICE functions
*****************************************************************************/
/** Opens the virtual network device.
*
* \return Always zero (success).
*/
int ec_dbgdev_open(
struct net_device *dev /**< debug net_device */
)
{
ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
dbg->opened = 1;
EC_MASTER_INFO(dbg->device->master, "Debug interface %s opened.\n",
dev->name);
return 0;
}
/*****************************************************************************/
/** Stops the virtual network device.
*
* \return Always zero (success).
*/
int ec_dbgdev_stop(
struct net_device *dev /**< debug net_device */
)
{
ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
dbg->opened = 0;
EC_MASTER_INFO(dbg->device->master, "Debug interface %s stopped.\n",
dev->name);
return 0;
}
/*****************************************************************************/
/** Transmits data via the virtual network device.
*
* \return Always zero (success).
*/
int ec_dbgdev_tx(
struct sk_buff *skb, /**< transmit socket buffer */
struct net_device *dev /**< EoE net_device */
)
{
ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
dev_kfree_skb(skb);
dbg->stats.tx_dropped++;
return 0;
}
/*****************************************************************************/
/** Gets statistics about the virtual network device.
*
* \return Statistics.
*/
struct net_device_stats *ec_dbgdev_stats(
struct net_device *dev /**< debug net_device */
)
{
ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
return &dbg->stats;
}
/*****************************************************************************/
|