From 09d537e8e5313cd0f2c7bf6620ca70454de8a04a Mon Sep 17 00:00:00 2001
From: Abuzer Rafey <abuzer@rafey.ch>
Date: Tue, 8 Dec 2015 12:01:05 -0500
Subject: [PATCH] Added nvidia_modeset detection for issue #699

---
 src/module.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/module.c b/src/module.c
index 6b6cb09..eea0139 100644
--- a/src/module.c
+++ b/src/module.c
@@ -92,13 +92,15 @@ int module_load(char *module_name, char *driver) {
  * @return 1 if the driver is succesfully unloaded, 0 otherwise
  */
 int module_unload(char *driver) {
-  if (module_is_loaded(driver) == 1) {
+
+  if (module_is_loaded(driver) == 1 &&
+            module_is_loaded("nvidia_modeset") == 1) {
     int retries = 30;
     bb_log(LOG_INFO, "Unloading %s driver\n", driver);
     char *mod_argv[] = {
       "modprobe",
       "-r",
-      "nvidia_modeset",
+      "nvidia-modeset",
       driver,
       NULL
     };
@@ -111,6 +113,24 @@ 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;
 }
 
