From f105a847c435349cc3a98a743cce494170026d16 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Thu, 3 Apr 2014 16:57:26 +0200 Subject: [PATCH] libusbgx: Add remove gadget functionality. Add function which allow to remove USB gadget. This functions also remove gadget from internal library structures what means that after this operation all pointers to removed gadget are invalid. Signed-off-by: Krzysztof Opasiak [Port from libusbg and update description] Signed-off-by: Krzysztof Opasiak --- include/usbg/usbg.h | 9 +++++++++ src/usbg.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index 5f2af6d..399acdd 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -312,6 +312,15 @@ extern int usbg_rm_config(usbg_config *c, int opts); extern int usbg_rm_function(usbg_function *f, int opts); /** + * @brief Remove existing USB gadget + * @details This function frees also the memory allocated for gadget + * @param g Gadget to be removed + * @param opts Additional options for configuration removal. + * @return 0 on success, usbg_error if error occurred + */ +extern int usbg_rm_gadget(usbg_gadget *g, int opts); + +/** * @brief Remove configuration strings for given language * @param c Pointer to configuration * @param lang Language of strings which should be deleted diff --git a/src/usbg.c b/src/usbg.c index c5b4e3e..fe525e3 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -1429,6 +1429,60 @@ int usbg_rm_function(usbg_function *f, int opts) return ret; } +int usbg_rm_gadget(usbg_gadget *g, int opts) +{ + int ret = USBG_ERROR_INVALID_PARAM; + usbg_state *s; + if (!g) + goto out; + + s = g->parent; + + if (opts & USBG_RM_RECURSE) { + /* Recursive flag was given + * so remove all configs and functions + * using recursive flags */ + usbg_config *c; + usbg_function *f; + int nmb; + char spath[USBG_MAX_PATH_LENGTH]; + + while (!TAILQ_EMPTY(&g->configs)) { + c = TAILQ_FIRST(&g->configs); + ret = usbg_rm_config(c, opts); + if (ret != USBG_SUCCESS) + goto out; + } + + while (!TAILQ_EMPTY(&g->functions)) { + f = TAILQ_FIRST(&g->functions); + ret = usbg_rm_function(f, opts); + if (ret != USBG_SUCCESS) + goto out; + } + + nmb = snprintf(spath, sizeof(spath), "%s/%s/%s", g->path, + g->name, STRINGS_DIR); + if (nmb >= sizeof(spath)) { + ret = USBG_ERROR_PATH_TOO_LONG; + goto out; + } + + ret = usbg_rm_all_dirs(spath); + if (ret != USBG_SUCCESS) + goto out; + } + + ret = usbg_rm_dir(g->path, g->name); + if (ret == USBG_SUCCESS) { + TAILQ_REMOVE(&(s->gadgets), g, gnode); + usbg_free_gadget(g); + } + +out: + return ret; +} + int usbg_rm_config_strs(usbg_config *c, int lang) { int ret = USBG_SUCCESS; -- 2.7.4