From 57ad06c4abf6eab3696808d3ecef5076011edad4 Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Tue, 26 Sep 2006 16:16:13 +0000
Subject: [PATCH] Changes in ecrt_request_master(); minor output changes.

---
 master/domain.c |  2 +-
 master/master.c |  7 +++++--
 master/master.h |  2 +-
 master/module.c | 29 ++++++++++++++++++-----------
 4 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/master/domain.c b/master/domain.c
index e430f2c3..c622dacc 100644
--- a/master/domain.c
+++ b/master/domain.c
@@ -357,7 +357,7 @@ int ec_domain_alloc(ec_domain_t *domain, /**< EtherCAT domain */
         }
     }
 
-    EC_INFO("Domain %i - Allocated %i bytes in %i datagram%s\n",
+    EC_INFO("Domain %i - Allocated %i bytes in %i datagram%s.\n",
             domain->index, domain->data_size, datagram_count,
             datagram_count == 1 ? "" : "s");
 
diff --git a/master/master.c b/master/master.c
index 521dbc61..ca3ee727 100644
--- a/master/master.c
+++ b/master/master.c
@@ -1051,9 +1051,10 @@ void ec_master_calc_addressing(ec_master_t *master /**< EtherCAT master */)
 
 /**
    Measures the time, a frame is on the bus.
+   \return 0 in case of success, else < 0
 */
 
-void ec_master_measure_bus_time(ec_master_t *master)
+int ec_master_measure_bus_time(ec_master_t *master)
 {
     ec_datagram_t datagram;
     cycles_t cycles_start, cycles_end, cycles_timeout;
@@ -1064,7 +1065,7 @@ void ec_master_measure_bus_time(ec_master_t *master)
     if (ec_datagram_brd(&datagram, 0x130, 2)) {
         EC_ERR("Failed to allocate datagram for bus time measuring.\n");
         ec_datagram_clear(&datagram);
-        return;
+        return -1;
     }
 
     cycles_timeout = (cycles_t) EC_IO_TIMEOUT * (cpu_khz / 1000);
@@ -1103,11 +1104,13 @@ void ec_master_measure_bus_time(ec_master_t *master)
 
     EC_INFO("Bus time is (min/avg/max) %u/%u.%u/%u us.\n",
             min, sum / 100, sum % 100, max);
+    return 0;
 
   error:
     // Dequeue and free datagram
     list_del(&datagram.queue);
     ec_datagram_clear(&datagram);
+    return -1;
 }
 
 /******************************************************************************
diff --git a/master/master.h b/master/master.h
index 1ba444d1..e489d829 100644
--- a/master/master.h
+++ b/master/master.h
@@ -161,7 +161,7 @@ int ec_master_bus_scan(ec_master_t *);
 // misc.
 void ec_master_output_stats(ec_master_t *);
 void ec_master_clear_slaves(ec_master_t *);
-void ec_master_measure_bus_time(ec_master_t *);
+int ec_master_measure_bus_time(ec_master_t *);
 
 // other methods
 void ec_sync_config(const ec_sii_sync_t *, const ec_slave_t *, uint8_t *);
diff --git a/master/module.c b/master/module.c
index 67e6e779..fd0dda74 100644
--- a/master/module.c
+++ b/master/module.c
@@ -420,34 +420,41 @@ ec_master_t *ecrt_request_master(unsigned int master_index
         goto out_release;
     }
 
-    if (!try_module_get(master->device->module)) {
+    if (!try_module_get(master->device->module)) { // possible race?
         EC_ERR("Failed to reserve device module!\n");
         goto out_release;
     }
 
-    ec_master_measure_bus_time(master);
-    ec_master_idle_stop(master);
-    ec_master_reset(master);
+    if (!master->device->link_state) {
+        EC_ERR("Link is DOWN.\n");
+        goto out_module_put;
+    }
+
+    ec_master_reset(master); // also stops idle mode
     master->mode = EC_MASTER_MODE_OPERATION;
 
-    if (!master->device->link_state) EC_WARN("Link is DOWN.\n");
+    if (ec_master_measure_bus_time(master)) {
+        EC_ERR("Bus time measuring failed!\n");
+        goto out_reset;
+    }
 
     if (ec_master_bus_scan(master)) {
         EC_ERR("Bus scan failed!\n");
-        goto out_module_put;
+        goto out_reset;
     }
 
-    EC_INFO("Master %i is ready.\n", master_index);
+    EC_INFO("Successfully requested master %i.\n", master_index);
     return master;
 
- out_module_put:
-    module_put(master->device->module);
+ out_reset:
     ec_master_reset(master);
     ec_master_idle_start(master);
+ out_module_put:
+    module_put(master->device->module);
  out_release:
     master->reserved = 0;
  out_return:
-    EC_ERR("Failed requesting master %i.\n", master_index);
+    EC_ERR("Failed to request master %i.\n", master_index);
     return NULL;
 }
 
@@ -473,7 +480,7 @@ void ecrt_release_master(ec_master_t *master /**< EtherCAT master */)
     module_put(master->device->module);
     master->reserved = 0;
 
-    EC_INFO("Released master %i.\n", master->index);
+    EC_INFO("Successfully released master %i.\n", master->index);
     return;
 }
 
-- 
GitLab