From 6079aaa843f806d273e1a1d584cdb360c664b856 Mon Sep 17 00:00:00 2001 From: Philippe De Swert Date: Tue, 13 Nov 2012 18:52:19 +0200 Subject: [PATCH] Pre-eliminary (lib)kmod support Signed-off-by: Philippe De Swert --- configure.ac | 1 + src/usb_moded-modules.c | 44 +++++++++++++++++++++++++++++++++++++++-- src/usb_moded.c | 10 +++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 4599a21..a85dddf 100644 --- a/configure.ac +++ b/configure.ac @@ -119,6 +119,7 @@ PKG_CHECK_MODULES([USB_MODED], [ dbus-glib-1 >= 0.78 gobject-2.0 >= 2.16.6 gio-2.0 + libkmod ]) AC_SUBST(USB_MODED_LIBS) diff --git a/src/usb_moded-modules.c b/src/usb_moded-modules.c index 5f6237c..19af753 100644 --- a/src/usb_moded-modules.c +++ b/src/usb_moded-modules.c @@ -27,6 +27,8 @@ #include +#include + #include "usb_moded.h" #include "usb_moded-modules.h" #include "usb_moded-log.h" @@ -39,6 +41,9 @@ #include "usb_moded-modes.h" #endif +/* kmod context - initialized at start in usb_moded_init */ +struct kmod_ctx *ctx; + /** load module * * @param module Name of the module to load @@ -47,8 +52,9 @@ */ int usb_moded_load_module(const char *module) { - gchar *command; int ret = 0; +#ifdef NO_KMOD + gchar *command; command = g_strconcat("modprobe ", module, NULL); ret = system(command); @@ -63,6 +69,30 @@ int usb_moded_load_module(const char *module) ret = system(command); } g_free(command); +#else + const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST; + struct kmod_module *mod; + const char *charging_args = NULL; + + ret = kmod_module_new_from_name(ctx, module, &mod); + if(!strcmp(module, MODULE_MASS_STORAGE) && (ret != 0)) + { + ret = kmod_module_new_from_name(ctx, MODULE_FILE_STORAGE, &mod); + } + if(!strcmp(module, MODULE_CHARGING) && (ret != 0)) + { + /* split the string in module name and arguments, if it still fails do the same + for MODULE_CHARGE_FALLBACK */ + } + + if(!charging_args) + ret = kmod_module_probe_insert_module(mod, probe_flags, NULL, NULL, NULL, NULL); + else + ret = kmod_module_probe_insert_module(mod, probe_flags, charging_args, NULL, NULL, NULL); + + kmod_module_unref(mod); +#endif /* NO_KMOD */ + if( ret == 0) log_info("Module %s loaded successfully\n", module); return(ret); @@ -76,12 +106,22 @@ int usb_moded_load_module(const char *module) */ int usb_moded_unload_module(const char *module) { - gchar *command; int ret = 0; +#ifdef NO_KMOD + gchar *command; + command = g_strconcat("rmmod ", module, NULL); ret = system(command); g_free(command); +#else + struct kmod_module *mod; + + kmod_module_new_from_name(ctx, module, &mod); + ret = kmod_module_remove_module(mod, KMOD_REMOVE_NOWAIT); + kmod_module_unref(mod); + +#endif /* NO_KMOD */ return(ret); } diff --git a/src/usb_moded.c b/src/usb_moded.c index 84d9b08..ddb50fd 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -29,6 +29,8 @@ #include #endif +#include + #include "usb_moded.h" #include "usb_moded-modes.h" #include "usb_moded-dbus.h" @@ -52,7 +54,7 @@ extern const char *log_name; extern int log_level; extern int log_type; -extern gboolean rescue_mode; +gboolean rescue_mode; gboolean runlevel_ignore = FALSE; gboolean hw_fallback = FALSE; @@ -413,6 +415,7 @@ static void usb_moded_init(void) char readbuf[MAX_READ_BUF]; FILE *proc_fd; #endif /* NOKIA_NSU */ + extern struct kmod_ctx *ctx; current_mode.connected = FALSE; current_mode.mounted = FALSE; @@ -443,6 +446,11 @@ static void usb_moded_init(void) if(check_trigger()) trigger_init(); #endif /* UDEV */ + + /* kmod init */ + ctx = kmod_new(NULL, NULL); + kmod_load_resources(ctx); + /* TODO: add more start-up clean-up and init here if needed */ }