drivers/ec_device.c
branchkernel2.6
changeset 26 60435f959e5c
parent 25 7d124bfba3ce
child 27 d75ef6b46e33
equal deleted inserted replaced
25:7d124bfba3ce 26:60435f959e5c
    14 #include <linux/if_ether.h>
    14 #include <linux/if_ether.h>
    15 #include <linux/netdevice.h>
    15 #include <linux/netdevice.h>
    16 #include <linux/delay.h>
    16 #include <linux/delay.h>
    17 
    17 
    18 #include "ec_device.h"
    18 #include "ec_device.h"
    19 #include "ec_dbg.h"
       
    20 
    19 
    21 /***************************************************************/
    20 /***************************************************************/
    22 
    21 
    23 /**
    22 /**
    24    EtherCAT-Geräte-Konstuktor.
    23    EtherCAT-Geräte-Konstuktor.
    90 int EtherCAT_device_assign(EtherCAT_device_t *ecd,
    89 int EtherCAT_device_assign(EtherCAT_device_t *ecd,
    91                            struct net_device *dev)
    90                            struct net_device *dev)
    92 {
    91 {
    93   if (!dev)
    92   if (!dev)
    94   {
    93   {
    95     EC_DBG("EtherCAT: Device is NULL!\n");
    94     printk("EtherCAT: Device is NULL!\n");
    96     return -1;
    95     return -1;
    97   }
    96   }
    98 
    97 
    99   if ((ecd->tx_skb = dev_alloc_skb(ECAT_FRAME_BUFFER_SIZE)) == NULL)
    98   if ((ecd->tx_skb = dev_alloc_skb(ECAT_FRAME_BUFFER_SIZE)) == NULL)
   100   {
    99   {
   101     EC_DBG(KERN_ERR "EtherCAT: Could not allocate device tx socket buffer!\n");
   100     printk(KERN_ERR "EtherCAT: Could not allocate device tx socket buffer!\n");
   102     return -1;
   101     return -1;
   103   }
   102   }
   104 
   103 
   105   if ((ecd->rx_skb = dev_alloc_skb(ECAT_FRAME_BUFFER_SIZE)) == NULL)
   104   if ((ecd->rx_skb = dev_alloc_skb(ECAT_FRAME_BUFFER_SIZE)) == NULL)
   106   {
   105   {
   107     dev_kfree_skb(ecd->tx_skb);
   106     dev_kfree_skb(ecd->tx_skb);
   108     ecd->tx_skb = NULL;
   107     ecd->tx_skb = NULL;
   109 
   108 
   110     EC_DBG(KERN_ERR "EtherCAT: Could not allocate device rx socket buffer!\n");
   109     printk(KERN_ERR "EtherCAT: Could not allocate device rx socket buffer!\n");
   111     return -1;
   110     return -1;
   112   }
   111   }
   113 
   112 
   114   ecd->dev = dev;
   113   ecd->dev = dev;
   115   ecd->tx_skb->dev = dev;
   114   ecd->tx_skb->dev = dev;
   116   ecd->rx_skb->dev = dev;
   115   ecd->rx_skb->dev = dev;
   117 
   116 
   118   EC_DBG("EtherCAT: Assigned Device %X.\n", (unsigned) dev);
   117   printk("EtherCAT: Assigned Device %X.\n", (unsigned) dev);
   119 
   118 
   120   return 0;
   119   return 0;
   121 }
   120 }
   122 
   121 
   123 /***************************************************************/
   122 /***************************************************************/
   137 
   136 
   138 int EtherCAT_device_open(EtherCAT_device_t *ecd)
   137 int EtherCAT_device_open(EtherCAT_device_t *ecd)
   139 {
   138 {
   140   if (!ecd)
   139   if (!ecd)
   141   {
   140   {
   142     EC_DBG(KERN_ERR "EtherCAT: Trying to open a NULL device!\n");
   141     printk(KERN_ERR "EtherCAT: Trying to open a NULL device!\n");
   143     return -1;
   142     return -1;
   144   }
   143   }
   145 
   144 
   146   if (!ecd->dev)
   145   if (!ecd->dev)
   147   {
   146   {
   148     EC_DBG(KERN_ERR "EtherCAT: No device to open!\n");
   147     printk(KERN_ERR "EtherCAT: No device to open!\n");
   149     return -1;
   148     return -1;
   150   }
   149   }
   151 
   150 
   152   // Reset old device state
   151   // Reset old device state
   153   ecd->state = ECAT_DS_READY;
   152   ecd->state = ECAT_DS_READY;
   169 
   168 
   170 int EtherCAT_device_close(EtherCAT_device_t *ecd)
   169 int EtherCAT_device_close(EtherCAT_device_t *ecd)
   171 {
   170 {
   172   if (!ecd->dev)
   171   if (!ecd->dev)
   173   {
   172   {
   174     EC_DBG("EtherCAT: No device to close!\n");
   173     printk("EtherCAT: No device to close!\n");
   175     return -1;
   174     return -1;
   176   }
   175   }
   177 
   176 
   178   EC_DBG("EtherCAT: txcnt: %u, rxcnt: %u\n",
   177   printk("EtherCAT: txcnt: %u, rxcnt: %u\n",
   179          (unsigned int) ecd->tx_intr_cnt,
   178          (unsigned int) ecd->tx_intr_cnt,
   180          (unsigned int) ecd->rx_intr_cnt);
   179          (unsigned int) ecd->rx_intr_cnt);
   181 
   180 
   182   EC_DBG("EtherCAT: Stopping device at 0x%X\n",
   181   printk("EtherCAT: Stopping device at 0x%X\n",
   183          (unsigned int) ecd->dev);
   182          (unsigned int) ecd->dev);
   184 
   183 
   185   return ecd->dev->stop(ecd->dev);
   184   return ecd->dev->stop(ecd->dev);
   186 }
   185 }
   187 
   186 
   209   unsigned char *frame_data;
   208   unsigned char *frame_data;
   210   struct ethhdr *eth;
   209   struct ethhdr *eth;
   211 
   210 
   212   if (ecd->state == ECAT_DS_SENT)
   211   if (ecd->state == ECAT_DS_SENT)
   213   {
   212   {
   214     EC_DBG(KERN_WARNING "EtherCAT: Trying to send frame while last was not received!\n");
   213     printk(KERN_WARNING "EtherCAT: Trying to send frame while last was not received!\n");
   215   }
   214   }
   216 
   215 
   217   skb_trim(ecd->tx_skb, 0); // Clear transmit socket buffer
   216   skb_trim(ecd->tx_skb, 0); // Clear transmit socket buffer
   218   skb_reserve(ecd->tx_skb, ETH_HLEN); // Reserve space for Ethernet-II header
   217   skb_reserve(ecd->tx_skb, ETH_HLEN); // Reserve space for Ethernet-II header
   219 
   218 
   222   memcpy(frame_data, data, length);
   221   memcpy(frame_data, data, length);
   223 
   222 
   224   // Add Ethernet-II-Header
   223   // Add Ethernet-II-Header
   225   if ((eth = (struct ethhdr *) skb_push(ecd->tx_skb, ETH_HLEN)) == NULL)
   224   if ((eth = (struct ethhdr *) skb_push(ecd->tx_skb, ETH_HLEN)) == NULL)
   226   {
   225   {
   227     EC_DBG(KERN_ERR "EtherCAT: device_send - Could not allocate Ethernet-II header!\n");
   226     printk(KERN_ERR "EtherCAT: device_send - Could not allocate Ethernet-II header!\n");
   228     return -1;
   227     return -1;
   229   }
   228   }
   230 
   229 
   231   eth->h_proto = htons(0x88A4); // Protocol type
   230   eth->h_proto = htons(0x88A4); // Protocol type
   232   memcpy(eth->h_source, ecd->dev->dev_addr, ecd->dev->addr_len); // Hardware address
   231   memcpy(eth->h_source, ecd->dev->dev_addr, ecd->dev->addr_len); // Hardware address
   260 int EtherCAT_device_receive(EtherCAT_device_t *ecd,
   259 int EtherCAT_device_receive(EtherCAT_device_t *ecd,
   261                             unsigned char *data)
   260                             unsigned char *data)
   262 {
   261 {
   263   if (ecd->state != ECAT_DS_RECEIVED)
   262   if (ecd->state != ECAT_DS_RECEIVED)
   264   {
   263   {
   265     EC_DBG(KERN_ERR "EtherCAT: receive - Nothing received!\n");
   264     printk(KERN_ERR "EtherCAT: receive - Nothing received!\n");
   266     return -1;
   265     return -1;
   267   }
   266   }
   268 
   267 
   269   if (ecd->rx_data_length > ECAT_FRAME_BUFFER_SIZE)
   268   if (ecd->rx_data_length > ECAT_FRAME_BUFFER_SIZE)
   270   {
   269   {
   271     EC_DBG(KERN_ERR "EtherCAT: receive - Reveived frame too long (%i Bytes)!\n",
   270     printk(KERN_ERR "EtherCAT: receive - Reveived frame too long (%i Bytes)!\n",
   272            ecd->rx_data_length);
   271            ecd->rx_data_length);
   273     return -1;
   272     return -1;
   274   }
   273   }
   275 
   274 
   276   memcpy(data, ecd->rx_data, ecd->rx_data_length);
   275   memcpy(data, ecd->rx_data, ecd->rx_data_length);
   301    @param ecd EtherCAT-Gerät
   300    @param ecd EtherCAT-Gerät
   302 */
   301 */
   303 
   302 
   304 void EtherCAT_device_debug(EtherCAT_device_t *ecd)
   303 void EtherCAT_device_debug(EtherCAT_device_t *ecd)
   305 {
   304 {
   306   EC_DBG(KERN_DEBUG "---EtherCAT device information begin---\n");
   305   printk(KERN_DEBUG "---EtherCAT device information begin---\n");
   307 
   306 
   308   if (ecd)
   307   if (ecd)
   309   {
   308   {
   310     EC_DBG(KERN_DEBUG "Assigned net_device: %X\n", (unsigned) ecd->dev);
   309     printk(KERN_DEBUG "Assigned net_device: %X\n", (unsigned) ecd->dev);
   311     EC_DBG(KERN_DEBUG "Transmit socket buffer: %X\n", (unsigned) ecd->tx_skb);
   310     printk(KERN_DEBUG "Transmit socket buffer: %X\n", (unsigned) ecd->tx_skb);
   312     EC_DBG(KERN_DEBUG "Receive socket buffer: %X\n", (unsigned) ecd->rx_skb);
   311     printk(KERN_DEBUG "Receive socket buffer: %X\n", (unsigned) ecd->rx_skb);
   313     EC_DBG(KERN_DEBUG "Time of last transmission: %u\n", (unsigned) ecd->tx_time);
   312     printk(KERN_DEBUG "Time of last transmission: %u\n", (unsigned) ecd->tx_time);
   314     EC_DBG(KERN_DEBUG "Time of last receive: %u\n", (unsigned) ecd->rx_time);
   313     printk(KERN_DEBUG "Time of last receive: %u\n", (unsigned) ecd->rx_time);
   315     EC_DBG(KERN_DEBUG "Number of transmit interrupts: %u\n", (unsigned) ecd->tx_intr_cnt);
   314     printk(KERN_DEBUG "Number of transmit interrupts: %u\n", (unsigned) ecd->tx_intr_cnt);
   316     EC_DBG(KERN_DEBUG "Number of receive interrupts: %u\n", (unsigned) ecd->rx_intr_cnt);
   315     printk(KERN_DEBUG "Number of receive interrupts: %u\n", (unsigned) ecd->rx_intr_cnt);
   317     EC_DBG(KERN_DEBUG "Total Number of interrupts: %u\n", (unsigned) ecd->intr_cnt);
   316     printk(KERN_DEBUG "Total Number of interrupts: %u\n", (unsigned) ecd->intr_cnt);
   318     EC_DBG(KERN_DEBUG "Actual device state: %i\n", (int) ecd->state);
   317     printk(KERN_DEBUG "Actual device state: %i\n", (int) ecd->state);
   319     EC_DBG(KERN_DEBUG "Receive buffer: %X\n", (unsigned) ecd->rx_data);
   318     printk(KERN_DEBUG "Receive buffer: %X\n", (unsigned) ecd->rx_data);
   320     EC_DBG(KERN_DEBUG "Receive buffer fill state: %u/%u\n",
   319     printk(KERN_DEBUG "Receive buffer fill state: %u/%u\n",
   321            (unsigned) ecd->rx_data_length, ECAT_FRAME_BUFFER_SIZE);
   320            (unsigned) ecd->rx_data_length, ECAT_FRAME_BUFFER_SIZE);
   322   }
   321   }
   323   else
   322   else
   324   {
   323   {
   325     EC_DBG(KERN_DEBUG "Device is NULL!\n");
   324     printk(KERN_DEBUG "Device is NULL!\n");
   326   }
   325   }
   327 
   326 
   328   EC_DBG(KERN_DEBUG "---EtherCAT device information end---\n");
   327   printk(KERN_DEBUG "---EtherCAT device information end---\n");
   329 }
   328 }
   330 
   329 
   331 /***************************************************************/
   330 /***************************************************************/
   332 
   331 
   333 EXPORT_SYMBOL(EtherCAT_device_open);
   332 EXPORT_SYMBOL(EtherCAT_device_open);