master/device.c
changeset 1989 6aa393418fb3
parent 1921 d9cf40facbc4
child 1956 f461dc0d145a
equal deleted inserted replaced
1988:ea38efeeb7b3 1989:6aa393418fb3
    92     else if (device == &master->backup_device)
    92     else if (device == &master->backup_device)
    93         mb = 'b';
    93         mb = 'b';
    94 
    94 
    95     sprintf(ifname, "ecdbg%c%u", mb, master->index);
    95     sprintf(ifname, "ecdbg%c%u", mb, master->index);
    96 
    96 
    97     ret = ec_debug_init(&device->dbg, ifname);
    97     ret = ec_debug_init(&device->dbg, device, ifname);
    98     if (ret < 0) {
    98     if (ret < 0) {
    99         EC_ERR("Failed to init debug device!\n");
    99         EC_MASTER_ERR(master, "Failed to init debug device!\n");
   100         goto out_return;
   100         goto out_return;
   101     }
   101     }
   102 #endif
   102 #endif
   103 
   103 
   104     for (i = 0; i < EC_TX_RING_SIZE; i++)
   104     for (i = 0; i < EC_TX_RING_SIZE; i++)
   105         device->tx_skb[i] = NULL;
   105         device->tx_skb[i] = NULL;
   106 
   106 
   107     for (i = 0; i < EC_TX_RING_SIZE; i++) {
   107     for (i = 0; i < EC_TX_RING_SIZE; i++) {
   108         if (!(device->tx_skb[i] = dev_alloc_skb(ETH_FRAME_LEN))) {
   108         if (!(device->tx_skb[i] = dev_alloc_skb(ETH_FRAME_LEN))) {
   109             EC_ERR("Error allocating device socket buffer!\n");
   109             EC_MASTER_ERR(master, "Error allocating device socket buffer!\n");
   110             ret = -ENOMEM;
   110             ret = -ENOMEM;
   111             goto out_tx_ring;
   111             goto out_tx_ring;
   112         }
   112         }
   113 
   113 
   114         // add Ethernet-II-header
   114         // add Ethernet-II-header
   218         )
   218         )
   219 {
   219 {
   220     int ret;
   220     int ret;
   221 
   221 
   222     if (!device->dev) {
   222     if (!device->dev) {
   223         EC_ERR("No net_device to open!\n");
   223         EC_MASTER_ERR(device->master, "No net_device to open!\n");
   224         return -ENODEV;
   224         return -ENODEV;
   225     }
   225     }
   226 
   226 
   227     if (device->open) {
   227     if (device->open) {
   228         EC_WARN("Device already opened!\n");
   228         EC_MASTER_WARN(device->master, "Device already opened!\n");
   229         return 0;
   229         return 0;
   230     }
   230     }
   231 
   231 
   232     device->link_state = 0;
   232     device->link_state = 0;
   233 
   233 
   255         )
   255         )
   256 {
   256 {
   257     int ret;
   257     int ret;
   258 
   258 
   259     if (!device->dev) {
   259     if (!device->dev) {
   260         EC_ERR("No device to close!\n");
   260         EC_MASTER_ERR(device->master, "No device to close!\n");
   261         return -ENODEV;
   261         return -ENODEV;
   262     }
   262     }
   263 
   263 
   264     if (!device->open) {
   264     if (!device->open) {
   265         EC_WARN("Device already closed!\n");
   265         EC_MASTER_WARN(device->master, "Device already closed!\n");
   266         return 0;
   266         return 0;
   267     }
   267     }
   268 
   268 
   269 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   269 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   270     ret = device->dev->netdev_ops->ndo_stop(device->dev);
   270     ret = device->dev->netdev_ops->ndo_stop(device->dev);
   335 
   335 
   336     // set the right length for the data
   336     // set the right length for the data
   337     skb->len = ETH_HLEN + size;
   337     skb->len = ETH_HLEN + size;
   338 
   338 
   339     if (unlikely(device->master->debug_level > 1)) {
   339     if (unlikely(device->master->debug_level > 1)) {
   340         EC_DBG("Sending frame:\n");
   340         EC_MASTER_DBG(device->master, 2, "Sending frame:\n");
   341         ec_print_data(skb->data, ETH_HLEN + size);
   341         ec_print_data(skb->data, ETH_HLEN + size);
   342     }
   342     }
   343 
   343 
   344     // start sending
   344     // start sending
   345 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   345 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   432     // calculate index of the newest frame in the ring to get its time
   432     // calculate index of the newest frame in the ring to get its time
   433     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE - 1)
   433     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE - 1)
   434         % EC_DEBUG_RING_SIZE;
   434         % EC_DEBUG_RING_SIZE;
   435     t0 = device->debug_frames[ring_index].t;
   435     t0 = device->debug_frames[ring_index].t;
   436 
   436 
   437     EC_DBG("Debug ring %u:\n", ring_index);
   437     EC_MASTER_DBG(device->master, 1, "Debug ring %u:\n", ring_index);
   438 
   438 
   439     // calculate index of the oldest frame in the ring
   439     // calculate index of the oldest frame in the ring
   440     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE
   440     ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE
   441             - device->debug_frame_count) % EC_DEBUG_RING_SIZE;
   441             - device->debug_frame_count) % EC_DEBUG_RING_SIZE;
   442 
   442 
   443     for (i = 0; i < device->debug_frame_count; i++) {
   443     for (i = 0; i < device->debug_frame_count; i++) {
   444         df = &device->debug_frames[ring_index];
   444         df = &device->debug_frames[ring_index];
   445         timersub(&t0, &df->t, &diff);
   445         timersub(&t0, &df->t, &diff);
   446 
   446 
   447         EC_DBG("Frame %u, dt=%u.%06u s, %s:\n",
   447         EC_MASTER_DBG(device->master, 1, "Frame %u, dt=%u.%06u s, %s:\n",
   448                 i + 1 - device->debug_frame_count,
   448                 i + 1 - device->debug_frame_count,
   449                 (unsigned int) diff.tv_sec,
   449                 (unsigned int) diff.tv_sec,
   450                 (unsigned int) diff.tv_usec,
   450                 (unsigned int) diff.tv_usec,
   451                 (df->dir == TX) ? "TX" : "RX");
   451                 (df->dir == TX) ? "TX" : "RX");
   452         ec_print_data(df->data, df->data_size);
   452         ec_print_data(df->data, df->data_size);
   497 {
   497 {
   498     ec_master_t *master = device->master;
   498     ec_master_t *master = device->master;
   499     char str[20];
   499     char str[20];
   500 
   500 
   501     ec_mac_print(device->dev->dev_addr, str);
   501     ec_mac_print(device->dev->dev_addr, str);
   502     EC_INFO("Master %u releasing main device %s.\n", master->index, str);
   502     EC_MASTER_INFO(master, "Releasing main device %s.\n", str);
   503     
   503     
   504     down(&master->device_sem);
   504     down(&master->device_sem);
   505     ec_device_detach(device);
   505     ec_device_detach(device);
   506     up(&master->device_sem);
   506     up(&master->device_sem);
   507 }
   507 }
   517 {
   517 {
   518     int ret;
   518     int ret;
   519 
   519 
   520     ret = ec_device_open(device);
   520     ret = ec_device_open(device);
   521     if (ret) {
   521     if (ret) {
   522         EC_ERR("Failed to open device!\n");
   522         EC_MASTER_ERR(device->master, "Failed to open device!\n");
   523         return ret;
   523         return ret;
   524     }
   524     }
   525 
   525 
   526     ret = ec_master_enter_idle_phase(device->master);
   526     ret = ec_master_enter_idle_phase(device->master);
   527     if (ret) {
   527     if (ret) {
   528         EC_ERR("Failed to enter IDLE phase!\n");
   528         EC_MASTER_ERR(device->master, "Failed to enter IDLE phase!\n");
   529         return ret;
   529         return ret;
   530     }
   530     }
   531 
   531 
   532     return 0;
   532     return 0;
   533 }
   533 }
   542 void ecdev_close(ec_device_t *device /**< EtherCAT device */)
   542 void ecdev_close(ec_device_t *device /**< EtherCAT device */)
   543 {
   543 {
   544     ec_master_leave_idle_phase(device->master);
   544     ec_master_leave_idle_phase(device->master);
   545 
   545 
   546     if (ec_device_close(device))
   546     if (ec_device_close(device))
   547         EC_WARN("Failed to close device!\n");
   547         EC_MASTER_WARN(device->master, "Failed to close device!\n");
   548 }
   548 }
   549 
   549 
   550 /*****************************************************************************/
   550 /*****************************************************************************/
   551 
   551 
   552 /** Accepts a received frame.
   552 /** Accepts a received frame.
   564 {
   564 {
   565     const void *ec_data = data + ETH_HLEN;
   565     const void *ec_data = data + ETH_HLEN;
   566     size_t ec_size = size - ETH_HLEN;
   566     size_t ec_size = size - ETH_HLEN;
   567 
   567 
   568     if (unlikely(!data)) {
   568     if (unlikely(!data)) {
   569         EC_WARN("%s() called with NULL data.\n", __func__);
   569         EC_MASTER_WARN(device->master, "%s() called with NULL data.\n",
       
   570                 __func__);
   570         return;
   571         return;
   571     }
   572     }
   572 
   573 
   573     device->rx_count++;
   574     device->rx_count++;
   574 
   575 
   575     if (unlikely(device->master->debug_level > 1)) {
   576     if (unlikely(device->master->debug_level > 1)) {
   576         EC_DBG("Received frame:\n");
   577         EC_MASTER_DBG(device->master, 2, "Received frame:\n");
   577         ec_print_data(data, size);
   578         ec_print_data(data, size);
   578     }
   579     }
   579 
   580 
   580 #ifdef EC_DEBUG_IF
   581 #ifdef EC_DEBUG_IF
   581     ec_debug_send(&device->dbg, data, size);
   582     ec_debug_send(&device->dbg, data, size);
   600         ec_device_t *device, /**< EtherCAT device */
   601         ec_device_t *device, /**< EtherCAT device */
   601         uint8_t state /**< new link state */
   602         uint8_t state /**< new link state */
   602         )
   603         )
   603 {
   604 {
   604     if (unlikely(!device)) {
   605     if (unlikely(!device)) {
   605         EC_WARN("ecdev_set_link(): No device!\n");
   606         EC_MASTER_WARN(device->master, "ecdev_set_link(): No device!\n");
   606         return;
   607         return;
   607     }
   608     }
   608 
   609 
   609     if (likely(state != device->link_state)) {
   610     if (likely(state != device->link_state)) {
   610         device->link_state = state;
   611         device->link_state = state;
   611         EC_INFO("Link state changed to %s.\n", (state ? "UP" : "DOWN"));
   612         EC_MASTER_INFO(device->master,
       
   613                 "Link state changed to %s.\n", (state ? "UP" : "DOWN"));
   612     }
   614     }
   613 }
   615 }
   614 
   616 
   615 /*****************************************************************************/
   617 /*****************************************************************************/
   616 
   618 
   621 uint8_t ecdev_get_link(
   623 uint8_t ecdev_get_link(
   622         const ec_device_t *device /**< EtherCAT device */
   624         const ec_device_t *device /**< EtherCAT device */
   623         )
   625         )
   624 {
   626 {
   625     if (unlikely(!device)) {
   627     if (unlikely(!device)) {
   626         EC_WARN("ecdev_get_link(): No device!\n");
   628         EC_MASTER_WARN(device->master, "ecdev_get_link(): No device!\n");
   627         return 0;
   629         return 0;
   628     }
   630     }
   629 
   631 
   630     return device->link_state;
   632     return device->link_state;
   631 }
   633 }