From fcfe596eb13f62ca9dd7de272a5a87ae843b2a00 Mon Sep 17 00:00:00 2001
From: Abuzer Rafey <abuzer@rafey.xyz>
Date: Wed, 13 Apr 2016 03:09:12 -0400
Subject: [PATCH] Simplify module_unload

---
 src/module.c | 72 ++++++++++--------------------------------------------------
 1 file changed, 12 insertions(+), 60 deletions(-)

diff --git a/src/module.c b/src/module.c
index db9736a..38e2daa 100644
--- a/src/module.c
+++ b/src/module.c
@@ -93,56 +93,27 @@ int module_load(char *module_name, char *driver) {
  */
 int module_unload(char *driver) {
 
-  if (module_is_loaded("nvidia_uvm") == 1) {
-    int retries = 30;
-    bb_log(LOG_INFO, "Unloading nvidia_uvm driver\n");
-    char *mod_argv[] = {
-      "modprobe",
-      "-r",
-      "nvidia_uvm",
-      "nvidia_modeset",
-      NULL
-    };
-    bb_run_fork_wait(mod_argv, 10);
-    while (retries-- > 0 && module_is_loaded("nvidia_uvm") == 1) {
-      usleep(100000);
-    }
-    if (module_is_loaded(driver) == 1) {
-      bb_log(LOG_ERR, "Unloading %s driver timed out.\n", driver);
-      return 0;
-    }
-  }
+	char uvm[] = "nvidia_uvm";
+	char drm[] = "nvidia_drm";
+	char modeset[] = "nvidia_modeset";
+	int uvm_is_loaded = module_is_loaded(uvm);
+	int drm_is_loaded = module_is_loaded(drm);
+	int modeset_is_loaded = module_is_loaded(modeset);
 
-  else if (module_is_loaded("nvidia_drm") == 1) {
+  if (uvm_is_loaded || drm_is_loaded || modeset_is_loaded || *driver == 1) {
     int retries = 30;
-    bb_log(LOG_INFO, "Unloading nvidia_drm driver\n");
+    bb_log(LOG_INFO, "Unloading UVM/DRM/MODESET driver\n");
     char *mod_argv[] = {
       "modprobe",
       "-r",
+      "nvidia_uvm",
       "nvidia_drm",
-      NULL
-    };
-    bb_run_fork_wait(mod_argv, 10);
-    while (retries-- > 0 && module_is_loaded("nvidia_drm") == 1) {
-      usleep(100000);
-    }
-    if (module_is_loaded(driver) == 1) {
-      bb_log(LOG_ERR, "Unloading %s driver timed out.\n", driver);
-      return 0;
-    }
-  }
-
-  else if (module_is_loaded("nvidia_modeset") == 1) {
-    int retries = 30;
-    bb_log(LOG_INFO, "Unloading nvidia_modeset driver\n");
-    char *mod_argv[] = {
-      "modprobe",
-      "-r",
       "nvidia_modeset",
+			driver,
       NULL
     };
     bb_run_fork_wait(mod_argv, 10);
-    while (retries-- > 0 && module_is_loaded("nvidia_modeset") == 1) {
+    while (retries-- > 0 && module_is_loaded(driver) == 1) {
       usleep(100000);
     }
     if (module_is_loaded(driver) == 1) {
@@ -150,26 +121,7 @@ int module_unload(char *driver) {
       return 0;
     }
   }
-
-  else if (module_is_loaded(driver) == 1) {
-      int retries = 30;
-      bb_log(LOG_INFO, "Unloading %s driver\n", driver);
-      char *mod_argv[] = {
-        "modprobe",
-        "-r",
-        driver,
-        NULL
-      };
-      bb_run_fork_wait(mod_argv, 10);
-      while (retries-- > 0 && module_is_loaded(driver) == 1) {
-        usleep(100000);
-      }
-      if (module_is_loaded(driver) == 1) {
-        bb_log(LOG_ERR, "Unloading %s driver timed out.\n", driver);
-        return 0;
-      }
-  }
-  return 1;
+ 	return 1;
 }
 
 /**
