debug.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *
00003  *  $Id: debug.c 460 2006-07-06 08:31:55Z 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 struct net_device_stats *ec_dbgdev_stats(struct net_device *);
00053 
00054 /*****************************************************************************/
00055 
00061 int ec_debug_init(ec_debug_t *dbg )
00062 {
00063     int result;
00064 
00065     dbg->opened = 0;
00066     memset(&dbg->stats, 0, sizeof(struct net_device_stats));
00067 
00068     if (!(dbg->dev =
00069           alloc_netdev(sizeof(ec_debug_t *), "ec%d", ether_setup))) {
00070         EC_ERR("Unable to allocate net_device for debug object!\n");
00071         goto out_return;
00072     }
00073 
00074     // initialize net_device
00075     dbg->dev->open = ec_dbgdev_open;
00076     dbg->dev->stop = ec_dbgdev_stop;
00077     dbg->dev->get_stats = ec_dbgdev_stats;
00078 
00079     // initialize private data
00080     *((ec_debug_t **) netdev_priv(dbg->dev)) = dbg;
00081 
00082     // connect the net_device to the kernel
00083     if ((result = register_netdev(dbg->dev))) {
00084         EC_ERR("Unable to register net_device: error %i\n", result);
00085         goto out_free;
00086     }
00087 
00088     return 0;
00089 
00090  out_free:
00091     free_netdev(dbg->dev);
00092     dbg->dev = NULL;
00093  out_return:
00094     return -1;
00095 }
00096 
00097 /*****************************************************************************/
00098 
00104 void ec_debug_clear(ec_debug_t *dbg )
00105 {
00106     if (dbg->dev) {
00107         unregister_netdev(dbg->dev);
00108         free_netdev(dbg->dev);
00109     }
00110 }
00111 
00112 /*****************************************************************************/
00113 
00118 void ec_debug_send(ec_debug_t *dbg, 
00119                    const uint8_t *data, 
00120                    size_t size 
00121                    )
00122 {
00123     struct sk_buff *skb;
00124 
00125     if (!dbg->opened) return;
00126 
00127     // allocate socket buffer
00128     if (!(skb = dev_alloc_skb(size))) {
00129         dbg->stats.rx_dropped++;
00130         return;
00131     }
00132 
00133     // copy frame contents into socket buffer
00134     memcpy(skb_put(skb, size), data, size);
00135 
00136     // update device statistics
00137     dbg->stats.rx_packets++;
00138     dbg->stats.rx_bytes += size;
00139 
00140     // pass socket buffer to network stack
00141     skb->dev = dbg->dev;
00142     skb->protocol = eth_type_trans(skb, dbg->dev);
00143     skb->ip_summed = CHECKSUM_UNNECESSARY;
00144     netif_rx(skb);
00145 }
00146 
00147 /******************************************************************************
00148  *  NET_DEVICE functions
00149  *****************************************************************************/
00150 
00155 int ec_dbgdev_open(struct net_device *dev )
00156 {
00157     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00158     dbg->opened = 1;
00159     EC_INFO("debug interface %s opened.\n", dev->name);
00160     return 0;
00161 }
00162 
00163 /*****************************************************************************/
00164 
00169 int ec_dbgdev_stop(struct net_device *dev )
00170 {
00171     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00172     dbg->opened = 0;
00173     EC_INFO("debug interface %s stopped.\n", dev->name);
00174     return 0;
00175 }
00176 
00177 /*****************************************************************************/
00178 
00183 struct net_device_stats *ec_dbgdev_stats(struct net_device *dev)
00185 {
00186     ec_debug_t *dbg = *((ec_debug_t **) netdev_priv(dev));
00187     return &dbg->stats;
00188 }
00189 
00190 /*****************************************************************************/

Generated on Fri Sep 1 14:56:56 2006 for IgH EtherCAT master by  doxygen 1.4.6