fp@231: /****************************************************************************** fp@231: * fp@231: * $Id$ fp@231: * fp@231: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@231: * fp@231: * This file is part of the IgH EtherCAT Master. fp@231: * fp@231: * The IgH EtherCAT Master is free software; you can redistribute it fp@231: * and/or modify it under the terms of the GNU General Public License fp@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. fp@231: * fp@231: * The IgH EtherCAT Master is distributed in the hope that it will be fp@231: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@231: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@231: * GNU General Public License for more details. fp@231: * fp@231: * You should have received a copy of the GNU General Public License fp@231: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@231: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@231: * fp@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@231: *****************************************************************************/ fp@231: fp@231: /** fp@231: \file fp@231: Ethernet interface for debugging purposes. fp@231: */ fp@231: fp@231: /*****************************************************************************/ fp@231: fp@294: #include fp@231: #include fp@231: fp@231: #include "globals.h" fp@231: #include "debug.h" fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: // net_device functions fp@231: int ec_dbgdev_open(struct net_device *); fp@231: int ec_dbgdev_stop(struct net_device *); fp@1732: int ec_dbgdev_tx(struct sk_buff *, struct net_device *); fp@231: struct net_device_stats *ec_dbgdev_stats(struct net_device *); fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: /** fp@231: Debug constructor. fp@231: Initializes the debug object, creates a net_device and registeres it. fp@231: */ fp@231: fp@1744: int ec_debug_init( fp@1744: ec_debug_t *dbg, /**< debug object */ fp@1744: const char *name /**< interface name */ fp@1744: ) fp@231: { fp@231: int result; fp@231: fp@231: dbg->opened = 0; fp@231: memset(&dbg->stats, 0, sizeof(struct net_device_stats)); fp@231: fp@231: if (!(dbg->dev = fp@1744: alloc_netdev(sizeof(ec_debug_t *), name, ether_setup))) { fp@231: EC_ERR("Unable to allocate net_device for debug object!\n"); fp@231: goto out_return; fp@231: } fp@231: fp@231: // initialize net_device fp@231: dbg->dev->open = ec_dbgdev_open; fp@231: dbg->dev->stop = ec_dbgdev_stop; fp@1732: dbg->dev->hard_start_xmit = ec_dbgdev_tx; fp@231: dbg->dev->get_stats = ec_dbgdev_stats; fp@231: fp@231: // initialize private data fp@231: *((ec_debug_t **) netdev_priv(dbg->dev)) = dbg; fp@231: fp@231: // connect the net_device to the kernel fp@231: if ((result = register_netdev(dbg->dev))) { fp@231: EC_ERR("Unable to register net_device: error %i\n", result); fp@231: goto out_free; fp@231: } fp@231: fp@231: return 0; fp@231: fp@231: out_free: fp@231: free_netdev(dbg->dev); fp@231: dbg->dev = NULL; fp@231: out_return: fp@231: return -1; fp@231: } fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: /** fp@231: Debug destructor. fp@231: Unregisteres the net_device and frees allocated memory. fp@231: */ fp@231: fp@231: void ec_debug_clear(ec_debug_t *dbg /**< debug object */) fp@231: { fp@231: if (dbg->dev) { fp@231: unregister_netdev(dbg->dev); fp@231: free_netdev(dbg->dev); fp@231: } fp@231: } fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: /** fp@231: Sends frame data to the interface. fp@231: */ fp@231: fp@231: void ec_debug_send(ec_debug_t *dbg, /**< debug object */ fp@231: const uint8_t *data, /**< frame data */ fp@231: size_t size /**< size of the frame data */ fp@231: ) fp@231: { fp@231: struct sk_buff *skb; fp@231: fp@231: if (!dbg->opened) return; fp@231: fp@231: // allocate socket buffer fp@231: if (!(skb = dev_alloc_skb(size))) { fp@231: dbg->stats.rx_dropped++; fp@231: return; fp@231: } fp@231: fp@231: // copy frame contents into socket buffer fp@231: memcpy(skb_put(skb, size), data, size); fp@231: fp@231: // update device statistics fp@231: dbg->stats.rx_packets++; fp@231: dbg->stats.rx_bytes += size; fp@231: fp@231: // pass socket buffer to network stack fp@231: skb->dev = dbg->dev; fp@231: skb->protocol = eth_type_trans(skb, dbg->dev); fp@231: skb->ip_summed = CHECKSUM_UNNECESSARY; fp@231: netif_rx(skb); fp@231: } fp@231: fp@231: /****************************************************************************** fp@231: * NET_DEVICE functions fp@231: *****************************************************************************/ fp@231: fp@231: /** fp@231: Opens the virtual network device. fp@231: */ fp@231: fp@231: int ec_dbgdev_open(struct net_device *dev /**< debug net_device */) fp@231: { fp@231: ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev)); fp@231: dbg->opened = 1; fp@1732: EC_INFO("Debug interface %s opened.\n", dev->name); fp@231: return 0; fp@231: } fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: /** fp@231: Stops the virtual network device. fp@231: */ fp@231: fp@231: int ec_dbgdev_stop(struct net_device *dev /**< debug net_device */) fp@231: { fp@231: ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev)); fp@231: dbg->opened = 0; fp@1732: EC_INFO("Debug interface %s stopped.\n", dev->name); fp@1732: return 0; fp@1732: } fp@1732: fp@1732: /*****************************************************************************/ fp@1732: fp@1732: /** fp@1732: Transmits data via the virtual network device. fp@1732: */ fp@1732: fp@1732: int ec_dbgdev_tx(struct sk_buff *skb, /**< transmit socket buffer */ fp@1732: struct net_device *dev /**< EoE net_device */ fp@1732: ) fp@1732: { fp@1732: ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev)); fp@1732: fp@1732: dev_kfree_skb(skb); fp@1732: dbg->stats.tx_dropped++; fp@231: return 0; fp@231: } fp@231: fp@231: /*****************************************************************************/ fp@231: fp@231: /** fp@231: Gets statistics about the virtual network device. fp@231: */ fp@231: fp@231: struct net_device_stats *ec_dbgdev_stats(struct net_device *dev fp@231: /**< debug net_device */) fp@231: { fp@231: ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev)); fp@231: return &dbg->stats; fp@231: } fp@231: fp@231: /*****************************************************************************/