Used ERR_PTR() macro for return value of ecrt_request_master().
authorFlorian Pose <fp@igh-essen.com>
Wed, 01 Oct 2008 10:06:51 +0000
changeset 1239 b50b93faaf3e
parent 1238 02711963a059
child 1240 982eb5bee593
Used ERR_PTR() macro for return value of ecrt_request_master().
examples/mini/mini.c
examples/rtai/rtai_sample.c
include/ecrt.h
master/module.c
--- a/examples/mini/mini.c	Wed Oct 01 08:45:16 2008 +0000
+++ b/examples/mini/mini.c	Wed Oct 01 10:06:51 2008 +0000
@@ -35,6 +35,7 @@
 #include <linux/timer.h>
 #include <linux/spinlock.h>
 #include <linux/interrupt.h>
+#include <linux/err.h>
 
 #include "../../include/ecrt.h" // EtherCAT realtime interface
 
@@ -368,6 +369,7 @@
 
 int __init init_mini_module(void)
 {
+    int ret = -1;
 #if CONFIGURE_PDOS
     ec_slave_config_t *sc;
 #endif
@@ -377,8 +379,10 @@
     
     printk(KERN_INFO PFX "Starting...\n");
 
-    if (!(master = ecrt_request_master(0))) {
-        printk(KERN_ERR PFX "Requesting master 0 failed!\n");
+    master = ecrt_request_master(0);
+    if (IS_ERR(master)) {
+        ret = PTR_ERR(master); 
+        printk(KERN_ERR PFX "Requesting master 0 failed.\n");
         goto out_return;
     }
 
@@ -493,7 +497,7 @@
     ecrt_release_master(master);
 out_return:
     printk(KERN_ERR PFX "Failed to load. Aborting.\n");
-    return -1;
+    return ret;
 }
 
 /*****************************************************************************/
--- a/examples/rtai/rtai_sample.c	Wed Oct 01 08:45:16 2008 +0000
+++ b/examples/rtai/rtai_sample.c	Wed Oct 01 10:06:51 2008 +0000
@@ -35,6 +35,7 @@
 
 // Linux
 #include <linux/module.h>
+#include <linux/err.h>
 
 // RTAI
 #include <rtai_sched.h>
@@ -268,6 +269,7 @@
 
 int __init init_mod(void)
 {
+    int ret = -1;
     RTIME tick_period, requested_ticks, now;
 #ifdef CONFIGURE_PDOS
     ec_slave_config_t *sc;
@@ -279,7 +281,10 @@
 
     t_critical = cpu_khz * 1000 / FREQUENCY - cpu_khz * INHIBIT_TIME / 1000;
 
-    if (!(master = ecrt_request_master(0))) {
+
+    master = ecrt_request_master(0);
+    if (IS_ERR(master)) {
+        ret = PTR_ERR(master); 
         printk(KERN_ERR PFX "Requesting master 0 failed!\n");
         goto out_return;
     }
@@ -361,7 +366,7 @@
  out_return:
     rt_sem_delete(&master_sem);
     printk(KERN_ERR PFX "Failed to load. Aborting.\n");
-    return -1;
+    return ret;
 }
 
 /*****************************************************************************/
--- a/include/ecrt.h	Wed Oct 01 08:45:16 2008 +0000
+++ b/include/ecrt.h	Wed Oct 01 10:06:51 2008 +0000
@@ -42,6 +42,10 @@
  * request a master, to map process data, to communicate with slaves via CoE
  * and to configure and activate the bus.
  *
+ * Changes in version 1.5:
+ *
+ * - Changed the return value of ecrt_request_master().
+ *
  * Changes in Version 1.4:
  *
  * - Replaced ec_slave_t with ec_slave_config_t, separating the bus
@@ -340,7 +344,11 @@
  * The first master has index 0, the n-th master has index n - 1. The number
  * of masters has to be specified when loading the master module.
  *
- * \return Pointer to reserved master, or \a NULL on error.
+ * \attention In kernel context, the returned pointer has to be checked for
+ * errors using the IS_ERR() macro.
+ *
+ * \return If \a IS_ERR() returns zero, the result is a pointer to the
+ * reserved master, otherwise, the result is an error code.
  */
 ec_master_t *ecrt_request_master(
         unsigned int master_index /**< Index of the master to request. */
--- a/master/module.c	Wed Oct 01 08:45:16 2008 +0000
+++ b/master/module.c	Wed Oct 01 10:06:51 2008 +0000
@@ -39,6 +39,7 @@
 
 #include <linux/module.h>
 #include <linux/device.h>
+#include <linux/err.h>
 
 #include "globals.h"
 #include "master.h"
@@ -463,39 +464,47 @@
 
 ec_master_t *ecrt_request_master(unsigned int master_index)
 {
-    ec_master_t *master;
+    ec_master_t *master, *errptr = NULL;
 
     EC_INFO("Requesting master %u...\n", master_index);
 
     if (master_index >= master_count) {
         EC_ERR("Invalid master index %u.\n", master_index);
+        errptr = ERR_PTR(-EINVAL);
         goto out_return;
     }
     master = &masters[master_index];
 
-    if (down_interruptible(&master_sem))
+    if (down_interruptible(&master_sem)) {
+        errptr = ERR_PTR(-EINTR);
         goto out_return;
+    }
 
     if (master->reserved) {
         up(&master_sem);
         EC_ERR("Master %u is already in use!\n", master_index);
+        errptr = ERR_PTR(-EBUSY);
         goto out_return;
     }
     master->reserved = 1;
     up(&master_sem);
 
-    if (down_interruptible(&master->device_sem))
+    if (down_interruptible(&master->device_sem)) {
+        errptr = ERR_PTR(-EINTR);
         goto out_release;
+    }
     
     if (master->phase != EC_IDLE) {
         up(&master->device_sem);
         EC_ERR("Master %u still waiting for devices!\n", master_index);
+        errptr = ERR_PTR(-ENODEV);
         goto out_release;
     }
 
     if (!try_module_get(master->main_device.module)) {
         up(&master->device_sem);
         EC_ERR("Device module is unloading!\n");
+        errptr = ERR_PTR(-ENODEV);
         goto out_release;
     }
 
@@ -503,6 +512,7 @@
 
     if (ec_master_enter_operation_phase(master)) {
         EC_ERR("Failed to enter OPERATION phase!\n");
+        errptr = ERR_PTR(-EIO);
         goto out_module_put;
     }
 
@@ -514,7 +524,7 @@
  out_release:
     master->reserved = 0;
  out_return:
-    return NULL;
+    return errptr;
 }
 
 /*****************************************************************************/