]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blobdiff - linux/src/daemon/GateHWSpinlock.c
Linux: Close spinlock device after gate is stopped
[ipc/ipcdev.git] / linux / src / daemon / GateHWSpinlock.c
index 115f78b2cf0e85512f54abddd6fcd30513fd3054..9ea4d09877fb6648ee8ae558952b8eb449f09945 100644 (file)
@@ -69,6 +69,7 @@ typedef UInt32            Error_Block;
  */
 /* GateHWSpinlock Module Local State */
 typedef struct {
+    Int32                           fd;         /* spinlock device handle */
     UInt32 *                        baseAddr;   /* base addr lock registers */
     GateMutex_Handle                gmHandle;   /* handle to gate mutex */
 } GateHWSpinlock_Module_State;
@@ -91,6 +92,7 @@ GateHWSpinlock_Config _GateHWSpinlock_cfgParams;
 
 static GateHWSpinlock_Module_State GateHWSpinlock_state =
 {
+    .fd = -1,
     .baseAddr = NULL,
     .gmHandle = NULL
 };
@@ -116,11 +118,9 @@ Int32 GateHWSpinlock_start(Void)
 {
     Int32               status = GateHWSpinlock_S_SUCCESS;
     UInt32              dst;
-    Int32               fdMem;
 
-    fdMem = open ("/dev/mem", O_RDWR | O_SYNC);
-
-    if (fdMem < 0){
+    Mod->fd = open ("/dev/mem", O_RDWR | O_SYNC);
+    if (Mod->fd < 0){
         LOG0("GateHWSpinlock_start: failed to open the /dev/mem");
         status = GateHWSpinlock_E_OSFAILURE;
     }
@@ -129,12 +129,14 @@ Int32 GateHWSpinlock_start(Void)
     if (status == GateHWSpinlock_S_SUCCESS) {
         dst = (UInt32)mmap(NULL, _GateHWSpinlock_cfgParams.size,
                             (PROT_READ | PROT_WRITE),
-                            (MAP_SHARED), fdMem,
+                            (MAP_SHARED), Mod->fd,
                             (off_t)_GateHWSpinlock_cfgParams.baseAddr);
 
         if (dst == (UInt32)MAP_FAILED) {
             LOG0("GateHWSpinlock_start: Memory map failed")
             status = GateHWSpinlock_E_OSFAILURE;
+            close(Mod->fd);
+            Mod->fd = -1;
         }
         else {
             Mod->baseAddr = (UInt32 *)(dst + _GateHWSpinlock_cfgParams.offset);
@@ -174,6 +176,12 @@ Int GateHWSpinlock_stop(Void)
            _GateHWSpinlock_cfgParams.size);
     }
 
+    /* close the spinlock device file */
+    if (Mod->fd >= 0) {
+        close(Mod->fd);
+        Mod->fd = -1;
+    }
+
     return(status);
 }