Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

debug.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *
00003  *  $Id: debug.c 682 2006-11-07 12:13:30Z fp $
00004  *
00005  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
00006  *
00007  *  This file is part of the IgH EtherCAT Master.
00008  *
00009  *  The IgH EtherCAT Master is free software; you can redistribute it
00010  *  and/or modify it under the terms of the GNU General Public License
00011  *  as published by the Free Software Foundation; either version 2 of the
00012  *  License, or (at your option) any later version.
00013  *
00014  *  The IgH EtherCAT Master is distributed in the hope that it will be
00015  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with the IgH EtherCAT Master; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  *  The right to use EtherCAT Technology is granted and comes free of
00024  *  charge under condition of compatibility of product made by
00025  *  Licensee. People intending to distribute/sell products based on the
00026  *  code, have to sign an agreement to guarantee that products using
00027  *  software based on IgH EtherCAT master stay compatible with the actual
00028  *  EtherCAT specification (which are released themselves as an open
00029  *  standard) as the (only) precondition to have the right to use EtherCAT
00030  *  Technology, IP and trade marks.
00031  *
00032  *****************************************************************************/
00033 
00039 /*****************************************************************************/
00040 
00041 #include <linux/netdevice.h>
00042 #include <linux/etherdevice.h>
00043 
00044 #include "globals.h"
00045 #include "debug.h"
00046 
00047 /*****************************************************************************/
00048 
00049 // net_device functions
00050 int ec_dbgdev_open(struct net_device *);
00051 int ec_dbgdev_stop(struct net_device *);
00052 int ec_dbgdev_tx(struct sk_buff *, struct net_device *);
00053 struct net_device_stats *ec_dbgdev_stats(struct net_device *);
00054 
00055 /*****************************************************************************/
00056 
00062 int ec_debug_init(ec_debug_t *dbg )
00063 {
00064     int result;
00065 
00066     dbg->opened = 0;
00067     memset(&dbg->stats, 0, sizeof(struct net_device_stats));
00068 
00069     if (!(dbg->dev =
00070           alloc_netdev(sizeof(ec_debug_t *), "ec%d", ether_setup))) {
00071         EC_ERR("Unable to allocate net_device for debug object!\n");
00072         goto out_return;
00073     }
00074 
00075     // initialize net_device
00076     dbg->dev->open = ec_dbgdev_open;
00077     dbg->dev->stop = ec_dbgdev_stop;
00078     dbg->dev->hard_start_xmit = ec_dbgdev_tx;
00079     dbg->dev->get_stats = ec_dbgdev_stats;
00080 
00081     // initialize private data
00082     *((ec_debug_t **) netdev_priv(dbg->dev)) = dbg;
00083 
00084     // connect the net_device to the kernel
00085     if ((result = register_netdev(dbg->dev))) {
00086         EC_ERR("Unable to register net_device: error %i\n", result);
00087         goto out_free;
00088     }
00089 
00090     return 0;
00091 
00092  out_free:
00093     free_netdev(dbg->dev);
00094     dbg->dev = NULL;
00095  out_return:
00096     return -1;
00097 }
00098 
00099 /*****************************************************************************/
00100 
00106 void ec_debug_clear(ec_debug_t *dbg )
00107 {
00108     if (dbg->dev) {
00109         unregister_netdev(dbg->dev);
00110         free_netdev(dbg->dev);
00111     }
00112 }
00113 
00114 /*****************************************************************************/
00115 
00120 void ec_debug_send(ec_debug_t *dbg, 
00121                    const uint8_t *data, 
00122                    size_t size 
00123                    )
00124 {
00125     struct sk_buff *skb;
00126 
00127     if (!dbg->opened) return;
00128 
00129     // allocate socket buffer
00130     if (!(skb = dev_alloc_skb(size))) {
00131         dbg->stats.rx_dropped++;
00132         return;
00133     }
00134 
00135     // copy frame contents into socket buffer
00136     memcpy(skb_put(skb, size), data, size);
00137 
00138     // update device statistics
00139     dbg->stats.rx_packets++;
00140     dbg->stats.rx_bytes += size;
00141 
00142     // pass socket buffer to network stack
00143     skb->dev = dbg->dev;
00144     skb->protocol = eth_type_trans(skb, dbg->dev);
00145     skb->ip_summed = CHECKSUM_UNNECESSARY;
00146     netif_rx(skb);
00147 }
00148 
00149 /******************************************************************************
00150  *  NET_DEVICE functions
00151  *****************************************************************************/
00152 
00157 int ec_dbgdev_open(struct net_device *dev )
00158 {
00159     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00160     dbg->opened = 1;
00161     EC_INFO("Debug interface %s opened.\n", dev->name);
00162     return 0;
00163 }
00164 
00165 /*****************************************************************************/
00166 
00171 int ec_dbgdev_stop(struct net_device *dev )
00172 {
00173     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00174     dbg->opened = 0;
00175     EC_INFO("Debug interface %s stopped.\n", dev->name);
00176     return 0;
00177 }
00178 
00179 /*****************************************************************************/
00180 
00185 int ec_dbgdev_tx(struct sk_buff *skb, 
00186                  struct net_device *dev 
00187                  )
00188 {
00189     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00190 
00191     dev_kfree_skb(skb);
00192     dbg->stats.tx_dropped++;
00193     return 0;
00194 }
00195 
00196 /*****************************************************************************/
00197 
00202 struct net_device_stats *ec_dbgdev_stats(struct net_device *dev)
00204 {
00205     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00206     return &dbg->stats;
00207 }
00208 
00209 /*****************************************************************************/

Generated on Tue Nov 7 15:03:35 2006 for IgH EtherCAT master by  doxygen 1.4.4