From 22dc1b6d146ee890cfa297abf0088f62dc0f4461 Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Mon, 12 Mar 2007 08:42:02 +0000
Subject: [PATCH] Replaced master variable 'atomic_t available' by integer and
 used semaphore in module.c instead.

---
 master/master.c |  2 +-
 master/master.h |  6 ++----
 master/module.c | 26 +++++++++++++++-----------
 3 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/master/master.c b/master/master.c
index fe8ceaa2..89adc7a4 100644
--- a/master/master.c
+++ b/master/master.c
@@ -108,8 +108,8 @@ int ec_master_init(ec_master_t *master, /**< EtherCAT master */
     ec_eoe_t *eoe, *next_eoe;
     unsigned int i;
 
-    atomic_set(&master->available, 1);
     master->index = index;
+    master->reserved = 0;
 
     master->main_mac = main_mac;
     master->backup_mac = backup_mac;
diff --git a/master/master.h b/master/master.h
index 9984b96c..d7de13ed 100644
--- a/master/master.h
+++ b/master/master.h
@@ -45,7 +45,6 @@
 #include <linux/sysfs.h>
 #include <linux/timer.h>
 #include <linux/wait.h>
-#include <asm/atomic.h>
 #include <asm/semaphore.h>
 
 #include "device.h"
@@ -93,10 +92,9 @@ ec_stats_t;
 
 struct ec_master
 {
-    atomic_t available; /**< zero, if the master is reserved for RT */
-    unsigned int index; /**< master index */
-
     struct kobject kobj; /**< kobject */
+    unsigned int index; /**< master index */
+    unsigned int reserved; /**< non-zero, if the master is reserved for RT */
 
     ec_device_t main_device; /**< EtherCAT device */
     const uint8_t *main_mac; /**< MAC address of main device */
diff --git a/master/module.c b/master/module.c
index c9f6e871..164fe45b 100644
--- a/master/module.c
+++ b/master/module.c
@@ -66,6 +66,7 @@ static char *main[MAX_MASTERS]; /**< main devices parameter */
 static char *backup[MAX_MASTERS]; /**< backup devices parameter */
 
 static ec_master_t *masters; /**< master array */
+static struct semaphore master_sem; /**< master semaphore */
 static unsigned int master_count; /**< number of masters */
 static unsigned int backup_count; /**< number of backup devices */
 
@@ -524,7 +525,7 @@ ec_master_t *ecrt_request_master(unsigned int master_index
 {
     ec_master_t *master;
 
-    EC_INFO("Requesting master %i...\n", master_index);
+    EC_INFO("Requesting master %u...\n", master_index);
 
     if (master_index >= master_count) {
         EC_ERR("Invalid master index %u.\n", master_index);
@@ -532,11 +533,14 @@ ec_master_t *ecrt_request_master(unsigned int master_index
     }
     master = &masters[master_index];
 
-    if (!atomic_dec_and_test(&master->available)) {
-        atomic_inc(&master->available);
-        EC_ERR("Master %i is already in use!\n", master_index);
+    down(&master_sem);
+    if (master->reserved) {
+        up(&master_sem);
+        EC_ERR("Master %u is already in use!\n", master_index);
         goto out_return;
     }
+    master->reserved = 1;
+    up(&master_sem);
 
     if (down_interruptible(&master->device_sem)) {
         EC_ERR("Interrupted while waiting for device!\n");
@@ -545,7 +549,7 @@ ec_master_t *ecrt_request_master(unsigned int master_index
 
     if (master->mode != EC_MASTER_MODE_IDLE) {
         up(&master->device_sem);
-        EC_ERR("Master %i still waiting for devices!\n", master_index);
+        EC_ERR("Master %u still waiting for devices!\n", master_index);
         goto out_release;
     }
 
@@ -567,13 +571,13 @@ ec_master_t *ecrt_request_master(unsigned int master_index
         goto out_module_put;
     }
 
-    EC_INFO("Successfully requested master %i.\n", master_index);
+    EC_INFO("Successfully requested master %u.\n", master_index);
     return master;
 
  out_module_put:
     module_put(master->main_device.module);
  out_release:
-    atomic_inc(&master->available);
+    master->reserved = 0;
  out_return:
     return NULL;
 }
@@ -587,19 +591,19 @@ ec_master_t *ecrt_request_master(unsigned int master_index
 
 void ecrt_release_master(ec_master_t *master /**< EtherCAT master */)
 {
-    EC_INFO("Releasing master %i...\n", master->index);
+    EC_INFO("Releasing master %u...\n", master->index);
 
     if (master->mode != EC_MASTER_MODE_OPERATION) {
-        EC_WARN("Master %i was was not requested!\n", master->index);
+        EC_WARN("Master %u was was not requested!\n", master->index);
         return;
     }
 
     ec_master_leave_operation_mode(master);
 
     module_put(master->main_device.module);
-    atomic_inc(&master->available);
+    master->reserved = 0;
 
-    EC_INFO("Released master %i.\n", master->index);
+    EC_INFO("Released master %u.\n", master->index);
 }
 
 /*****************************************************************************/
-- 
GitLab