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