From 97107d137b03e2979e501d220c63f006561d2e6f Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Thu, 21 Feb 2019 11:03:29 +0200 Subject: [PATCH] [usb-moded] Normalize backend startup/cleanup functionality Use backend_init() and backend_quit() functions for settings up and cleaning up all backends. Signed-off-by: Simo Piiroinen --- src/usb_moded-android.c | 15 ++++++++++++-- src/usb_moded-android.h | 3 ++- src/usb_moded-configfs.c | 45 ++++++++++++++++++++++++++++++++++++++-- src/usb_moded-configfs.h | 3 ++- src/usb_moded-modules.c | 5 ++++- src/usb_moded-modules.h | 2 +- src/usb_moded.c | 8 ++++--- 7 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/usb_moded-android.c b/src/usb_moded-android.c index b726c10..4ec4ded 100644 --- a/src/usb_moded-android.c +++ b/src/usb_moded-android.c @@ -42,7 +42,8 @@ bool android_in_use (void); static bool android_probe (void); gchar *android_get_serial (void); -bool android_init_values (void); +bool android_init (void); +void android_quit (void); bool android_set_enabled (bool enable); bool android_set_charging_mode(void); bool android_set_function (const char *function); @@ -161,9 +162,11 @@ android_get_serial(void) } /** initialize the basic android values + * + * @return true if android usb backend is ready for use, false otherwise */ bool -android_init_values(void) +android_init(void) { LOG_REGISTER_CONTEXT; @@ -224,6 +227,14 @@ android_init_values(void) return android_in_use(); } +/** Cleanup resources allocated by android usb backend + */ +void +android_quit(void) +{ + /* For now this exists for symmetry with other backends only */ +} + bool android_set_enabled(bool enable) { diff --git a/src/usb_moded-android.h b/src/usb_moded-android.h index be33870..b0a7ecd 100644 --- a/src/usb_moded-android.h +++ b/src/usb_moded-android.h @@ -48,7 +48,8 @@ bool android_in_use (void); gchar *android_get_serial (void); -bool android_init_values (void); +bool android_init (void); +void android_quit (void); bool android_set_enabled (bool enable); bool android_set_charging_mode(void); bool android_set_function (const char *function); diff --git a/src/usb_moded-configfs.c b/src/usb_moded-configfs.c index 3a085dc..537237b 100644 --- a/src/usb_moded-configfs.c +++ b/src/usb_moded-configfs.c @@ -88,7 +88,8 @@ static bool configfs_write_file (const char *path, const char static bool configfs_read_file (const char *path, char *buff, size_t size); static bool configfs_write_udc (const char *text); bool configfs_set_udc (bool enable); -bool configfs_init_values (void); +bool configfs_init (void); +void configfs_quit (void); bool configfs_set_charging_mode (void); bool configfs_set_productid (const char *id); bool configfs_set_vendorid (const char *id); @@ -740,9 +741,11 @@ configfs_set_udc(bool enable) } /** initialize the basic configfs values + * + * @return true if configfs backend is ready for use, false otherwise */ bool -configfs_init_values(void) +configfs_init(void) { LOG_REGISTER_CONTEXT; @@ -802,6 +805,44 @@ configfs_init_values(void) return configfs_in_use(); } +/** Cleanup resources allocated by configfs backend + */ +void +configfs_quit(void) +{ + g_free(GADGET_BASE_DIRECTORY), + GADGET_BASE_DIRECTORY = 0; + g_free(GADGET_FUNC_DIRECTORY), + GADGET_FUNC_DIRECTORY = 0; + g_free(GADGET_CONF_DIRECTORY), + GADGET_CONF_DIRECTORY = 0; + + g_free(GADGET_CTRL_UDC), + GADGET_CTRL_UDC = 0; + g_free(GADGET_CTRL_ID_VENDOR), + GADGET_CTRL_ID_VENDOR= 0; + g_free(GADGET_CTRL_ID_PRODUCT), + GADGET_CTRL_ID_PRODUCT= 0; + g_free(GADGET_CTRL_MANUFACTURER), + GADGET_CTRL_MANUFACTURER= 0; + g_free(GADGET_CTRL_PRODUCT), + GADGET_CTRL_PRODUCT = 0; + g_free(GADGET_CTRL_SERIAL), + GADGET_CTRL_SERIAL = 0; + + g_free(FUNCTION_MASS_STORAGE), + FUNCTION_MASS_STORAGE = 0; + g_free(FUNCTION_RNDIS), + FUNCTION_RNDIS = 0; + g_free(FUNCTION_MTP), + FUNCTION_MTP = 0; + + g_free(RNDIS_CTRL_WCEIS), + RNDIS_CTRL_WCEIS = 0; + g_free(RNDIS_CTRL_ETHADDR), + RNDIS_CTRL_ETHADDR= 0; +} + /* Set a charging mode for the configfs gadget * * @return true if successful, false on failure diff --git a/src/usb_moded-configfs.h b/src/usb_moded-configfs.h index 52c45ad..2be0560 100644 --- a/src/usb_moded-configfs.h +++ b/src/usb_moded-configfs.h @@ -33,7 +33,8 @@ bool configfs_in_use (void); bool configfs_set_udc (bool enable); -bool configfs_init_values (void); +bool configfs_init (void); +void configfs_quit (void); bool configfs_set_charging_mode (void); bool configfs_set_productid (const char *id); bool configfs_set_vendorid (const char *id); diff --git a/src/usb_moded-modules.c b/src/usb_moded-modules.c index e65c28f..156acef 100644 --- a/src/usb_moded-modules.c +++ b/src/usb_moded-modules.c @@ -135,7 +135,10 @@ static bool modules_probe(void) return modules_in_use(); } -/* kmod module init */ +/** kmod module init + * + * @return true if modules backend is ready for use, false otherwise + */ bool modules_init(void) { LOG_REGISTER_CONTEXT; diff --git a/src/usb_moded-modules.h b/src/usb_moded-modules.h index 2367c95..b53da16 100644 --- a/src/usb_moded-modules.h +++ b/src/usb_moded-modules.h @@ -48,7 +48,7 @@ /* -- modules -- */ -bool modules_in_use (void); +bool modules_in_use (void); bool modules_init (void); void modules_quit (void); int modules_load_module (const char *module); diff --git a/src/usb_moded.c b/src/usb_moded.c index 9eb6604..99e9d89 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -566,10 +566,10 @@ static bool usbmoded_init(void) * while waiting. */ for( int i = 10; ; ) { - if( configfs_init_values() ) + if( configfs_init() ) break; - if( android_init_values() ) + if( android_init() ) break; /* Must probe / poll since we're not yet running mainloop */ @@ -680,8 +680,10 @@ static void usbmoded_cleanup(void) /* Stop udev listener */ umudev_quit(); - /* Undo modules_init() */ + /* Do backend specific cleanup */ modules_quit(); + android_quit(); + configfs_quit(); /* Undo trigger_init() */ trigger_stop();