[PATCH v3 5/6] libusbg: Add remove gadget functionality.

2014-03-18 Thread Krzysztof Opasiak
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 
---
 include/usbg/usbg.h |   10 ++
 src/usbg.c  |   54 +++
 2 files changed, 64 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index f79ace4..cd64eb1 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -300,6 +300,16 @@ extern int usbg_remove_config(usbg_config *c, int 
recursive);
 extern int usbg_remove_function(usbg_function *f, int recursive);
 
 /**
+ * @brief Remove existing USB gadget
+ * @details This function frees also the memory allocated for gadget
+ * @param g Gadget to be removed
+ * @param recursive If different than 0 all configs, functions
+ * and strings will be also removed before removing gadget
+ * @return 0 on success, usbg_error if error occurred
+ */
+extern int usbg_remove_gadget(usbg_gadget *g, int recursive);
+
+/**
  * @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 36237c7..f1671d7 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -1268,6 +1268,60 @@ int usbg_remove_function(usbg_function *f, int recursive)
return ret;
 }
 
+int usbg_remove_gadget(usbg_gadget *g, int recursive)
+{
+   int ret = USBG_ERROR_INVALID_PARAM;
+   usbg_state *s;
+   if (!g)
+   goto out;
+
+   s = g->parent;
+
+   if (recursive) {
+   /* 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_remove_config(c, 1);
+   if (ret != USBG_SUCCESS)
+   goto out;
+   }
+
+   while (!TAILQ_EMPTY(&g->functions)) {
+   f = TAILQ_FIRST(&g->functions);
+   ret = usbg_remove_function(f, 1);
+   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_remove_all_dirs(spath);
+   if (ret != USBG_SUCCESS)
+   goto out;
+   }
+
+   ret = usbg_remove_dir(g->path, g->name);
+   if (ret == USBG_SUCCESS) {
+   TAILQ_REMOVE(&(s->gadgets), g, gnode);
+   usbg_free_gadget(g);
+   }
+
+out:
+   return ret;
+}
+
 int usbg_remove_config_strs(usbg_config *c, int lang)
 {
int ret = USBG_SUCCESS;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v3 5/6] libusbg: Add remove gadget functionality.

2014-04-03 Thread Matt Porter
On Tue, Mar 18, 2014 at 09:29:05PM +0100, Krzysztof Opasiak wrote:
> 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 
> ---
>  include/usbg/usbg.h |   10 ++
>  src/usbg.c  |   54 
> +++
>  2 files changed, 64 insertions(+)
> 
> diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
> index f79ace4..cd64eb1 100644
> --- a/include/usbg/usbg.h
> +++ b/include/usbg/usbg.h
> @@ -300,6 +300,16 @@ extern int usbg_remove_config(usbg_config *c, int 
> recursive);
>  extern int usbg_remove_function(usbg_function *f, int recursive);
>  
>  /**
> + * @brief Remove existing USB gadget
> + * @details This function frees also the memory allocated for gadget
> + * @param g Gadget to be removed
> + * @param recursive If different than 0 all configs, functions
> + * and strings will be also removed before removing gadget

So, regarding my comment on the example's use of API. I think it
you should define USBG_RM_RECURSE for use here.

-Matt
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


RE: [PATCH v3 5/6] libusbg: Add remove gadget functionality.

2014-04-03 Thread Krzysztof Opasiak
> -Original Message-
> From: Matt Porter [mailto:mpor...@linaro.org]
> Sent: Thursday, April 03, 2014 3:02 PM
> To: Krzysztof Opasiak
> Cc: linux-usb@vger.kernel.org; Andrzej Pietrasiewicz; Karol
> Lewandowski; Stanislaw Wadas; ty317@samsung.org; Marek
> Szyprowski; Robert Baldyga
> Subject: Re: [PATCH v3 5/6] libusbg: Add remove gadget
> functionality.
> 
> On Tue, Mar 18, 2014 at 09:29:05PM +0100, Krzysztof Opasiak wrote:
> > 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 
> > ---
> >  include/usbg/usbg.h |   10 ++
> >  src/usbg.c  |   54
> +++
> >  2 files changed, 64 insertions(+)
> >
> > diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
> > index f79ace4..cd64eb1 100644
> > --- a/include/usbg/usbg.h
> > +++ b/include/usbg/usbg.h
> > @@ -300,6 +300,16 @@ extern int usbg_remove_config(usbg_config
> *c, int recursive);
> >  extern int usbg_remove_function(usbg_function *f, int
> recursive);
> >
> >  /**
> > + * @brief Remove existing USB gadget
> > + * @details This function frees also the memory allocated for
> gadget
> > + * @param g Gadget to be removed
> > + * @param recursive If different than 0 all configs, functions
> > + * and strings will be also removed before removing gadget
> 
> So, regarding my comment on the example's use of API. I think it
> you should define USBG_RM_RECURSE for use here.
> 

Yes, I agree with you. I have renamed this param to opts and provide
suitable define for recurse option.

--
BR's
Krzysiek


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html