master/ethernet.c
changeset 203 c1f305e339c6
parent 199 04ecf40fc2e9
child 208 b7797f8a813d
equal deleted inserted replaced
202:d5141123a5af 203:c1f305e339c6
    36 #include "ethernet.h"
    36 #include "ethernet.h"
    37 
    37 
    38 /*****************************************************************************/
    38 /*****************************************************************************/
    39 
    39 
    40 /**
    40 /**
       
    41    Contains the private data of an EoE net_device.
       
    42 */
       
    43 
       
    44 typedef struct
       
    45 {
       
    46     struct net_device_stats stats; /**< device statistics */
       
    47     ec_eoe_t *eoe; /**< pointer to parent eoe object */
       
    48 }
       
    49 ec_eoedev_priv_t;
       
    50 
       
    51 /*****************************************************************************/
       
    52 
       
    53 void ec_eoedev_init(struct net_device *);
       
    54 int ec_eoedev_open(struct net_device *);
       
    55 int ec_eoedev_stop(struct net_device *);
       
    56 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
       
    57 struct net_device_stats *ec_eoedev_stats(struct net_device *);
       
    58 
       
    59 /*****************************************************************************/
       
    60 
       
    61 /**
    41    EoE constructor.
    62    EoE constructor.
    42 */
    63 */
    43 
    64 
    44 void ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
    65 int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
    45 {
    66 {
       
    67     ec_eoedev_priv_t *priv;
       
    68     int result;
       
    69 
    46     eoe->slave = slave;
    70     eoe->slave = slave;
    47     eoe->rx_state = EC_EOE_IDLE;
    71     eoe->rx_state = EC_EOE_IDLE;
       
    72 
       
    73     if (!(eoe->dev =
       
    74           alloc_netdev(sizeof(ec_eoedev_priv_t), "eoe%d", ec_eoedev_init))) {
       
    75         EC_ERR("Unable to allocate net_device for EoE object!\n");
       
    76         goto out_return;
       
    77     }
       
    78 
       
    79     // set EoE object reference
       
    80     priv = netdev_priv(eoe->dev);
       
    81     priv->eoe = eoe;
       
    82 
       
    83     // connect the net_device to the kernel
       
    84     if ((result = register_netdev(eoe->dev))) {
       
    85         EC_ERR("Unable to register net_device: error %i\n", result);
       
    86         goto out_free;
       
    87     }
       
    88 
       
    89     return 0;
       
    90 
       
    91  out_free:
       
    92     free_netdev(eoe->dev);
       
    93     eoe->dev = NULL;
       
    94  out_return:
       
    95     return -1;
    48 }
    96 }
    49 
    97 
    50 /*****************************************************************************/
    98 /*****************************************************************************/
    51 
    99 
    52 /**
   100 /**
    53    EoE destructor.
   101    EoE destructor.
    54 */
   102 */
    55 
   103 
    56 void ec_eoe_clear(ec_eoe_t *eoe)
   104 void ec_eoe_clear(ec_eoe_t *eoe)
    57 {
   105 {
       
   106     if (eoe->dev) {
       
   107         unregister_netdev(eoe->dev);
       
   108         free_netdev(eoe->dev);
       
   109     }
    58 }
   110 }
    59 
   111 
    60 /*****************************************************************************/
   112 /*****************************************************************************/
    61 
   113 
    62 /**
   114 /**
   151     EC_INFO("  EoE slave %i\n", eoe->slave->ring_position);
   203     EC_INFO("  EoE slave %i\n", eoe->slave->ring_position);
   152     EC_INFO("    RX State %i\n", eoe->rx_state);
   204     EC_INFO("    RX State %i\n", eoe->rx_state);
   153 }
   205 }
   154 
   206 
   155 /*****************************************************************************/
   207 /*****************************************************************************/
       
   208 
       
   209 /**
       
   210    Initializes a net_device structure for an EoE object.
       
   211 */
       
   212 
       
   213 void ec_eoedev_init(struct net_device *dev /**< pointer to the net_device */)
       
   214 {
       
   215     ec_eoedev_priv_t *priv;
       
   216 
       
   217     // initialize net_device
       
   218     ether_setup(dev);
       
   219     dev->open = ec_eoedev_open;
       
   220     dev->stop = ec_eoedev_stop;
       
   221     dev->hard_start_xmit = ec_eoedev_tx;
       
   222     dev->get_stats = ec_eoedev_stats;
       
   223 
       
   224     // initialize private data
       
   225     priv = netdev_priv(dev);
       
   226     memset(priv, 0, sizeof(ec_eoedev_priv_t));
       
   227 }
       
   228 
       
   229 /*****************************************************************************/
       
   230 
       
   231 /**
       
   232    Opens the virtual network device.
       
   233 */
       
   234 
       
   235 int ec_eoedev_open(struct net_device *dev /**< EoE net_device */)
       
   236 {
       
   237     ec_eoedev_priv_t *priv = netdev_priv(dev);
       
   238     netif_start_queue(dev);
       
   239     EC_INFO("%s (slave %i) opened.\n", dev->name,
       
   240             priv->eoe->slave->ring_position);
       
   241     return 0;
       
   242 }
       
   243 
       
   244 /*****************************************************************************/
       
   245 
       
   246 /**
       
   247    Stops the virtual network device.
       
   248 */
       
   249 
       
   250 int ec_eoedev_stop(struct net_device *dev /**< EoE net_device */)
       
   251 {
       
   252     ec_eoedev_priv_t *priv = netdev_priv(dev);
       
   253     netif_stop_queue(dev);
       
   254     EC_INFO("%s (slave %i) stopped.\n", dev->name,
       
   255             priv->eoe->slave->ring_position);
       
   256     return 0;
       
   257 }
       
   258 
       
   259 /*****************************************************************************/
       
   260 
       
   261 /**
       
   262    Transmits data via the virtual network device.
       
   263 */
       
   264 
       
   265 int ec_eoedev_tx(struct sk_buff *skb, /**< transmit socket buffer */
       
   266                  struct net_device *dev /**< EoE net_device */
       
   267                  )
       
   268 {
       
   269     ec_eoedev_priv_t *priv = netdev_priv(dev);
       
   270     priv->stats.tx_packets++;
       
   271     dev_kfree_skb(skb);
       
   272     EC_INFO("EoE device sent %i octets.\n", skb->len);
       
   273     return 0;
       
   274 }
       
   275 
       
   276 /*****************************************************************************/
       
   277 
       
   278 /**
       
   279    Gets statistics about the virtual network device.
       
   280 */
       
   281 
       
   282 struct net_device_stats *ec_eoedev_stats(struct net_device *dev
       
   283                                          /**< EoE net_device */)
       
   284 {
       
   285     ec_eoedev_priv_t *priv = netdev_priv(dev);
       
   286     return &priv->stats;
       
   287 }
       
   288 
       
   289 /*****************************************************************************/