Re: [PATCH v3 6/6] libusbg: Update examples to new API functionality.

2014-04-03 Thread Matt Porter
On Tue, Mar 18, 2014 at 09:29:06PM +0100, Krzysztof Opasiak wrote:
> Removing gadget/config/function/binding functionality
> has been added to API so add example of how to use it.
> 
> Signed-off-by: Krzysztof Opasiak 
> ---
>  examples/Makefile.am |3 +-
>  examples/gadget-vid-pid-remove.c |  113 
> ++
>  include/usbg/usbg.h  |1 -
>  3 files changed, 115 insertions(+), 2 deletions(-)
>  create mode 100644 examples/gadget-vid-pid-remove.c
> 
> diff --git a/examples/Makefile.am b/examples/Makefile.am
> index f9f9407..9fc235a 100644
> --- a/examples/Makefile.am
> +++ b/examples/Makefile.am
> @@ -1,5 +1,6 @@
> -bin_PROGRAMS = show-gadgets gadget-acm-ecm 
> +bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove
>  gadget_acm_ecm_SOURCES = gadget-acm-ecm.c
>  show_gadgets_SOURCES = show-gadgets.c
> +gadget_vid_pid_remove_SOURCES = gadget-vid-pid-remove.c
>  AM_CPPFLAGS=-I../include/
>  AM_LDFLAGS=-L../src/ -lusbg
> diff --git a/examples/gadget-vid-pid-remove.c 
> b/examples/gadget-vid-pid-remove.c
> new file mode 100644
> index 000..e62d231
> --- /dev/null
> +++ b/examples/gadget-vid-pid-remove.c
> @@ -0,0 +1,113 @@
> +/*
> + * Copyright (C) 2014 Samsung Electronics
> + *
> + * Krzysztof Opasiak 
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + */
> +
> +/**
> + * @file gadget-vid-pid-remove.c
> + * @example gadget-vid-pid-remove.c
> + * This is an example of how to find and remove an gadget device with given
> + * Vendor ID and product ID.
> + */
> +
> +#include 
> +#include 
> +#include 
> +
> +#define VENDOR   0x1d6b
> +#define PRODUCT  0x0104
> +
> +int remove_gadget(usbg_gadget *g)
> +{
> + int usbg_ret;
> + char udc[USBG_MAX_STR_LENGTH];
> +
> + /* Check if gadget is enabled */
> + usbg_ret = usbg_get_gadget_udc(g, udc, USBG_MAX_STR_LENGTH);
> + if (usbg_ret != USBG_SUCCESS) {
> + fprintf(stderr, "Error on USB get gadget udc\n");
> + fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
> + usbg_strerror(usbg_ret));
> + goto out;
> + }
> +
> + /* If gadget is enable we have to disable it first */
> + if (udc[0] != '\0') {
> + usbg_ret = usbg_disable_gadget(g);
> + if (usbg_ret != USBG_SUCCESS) {
> + fprintf(stderr, "Error on USB disable gadget udc\n");
> + fprintf(stderr, "Error: %s : %s\n", 
> usbg_error_name(usbg_ret),
> + usbg_strerror(usbg_ret));
> + goto out;
> + }
> + }
> +
> + /* Remove gadget */
> + usbg_ret = usbg_remove_gadget(g, 1 /* Remove also content */);

We're going to need a symbol for the remove content argument.
This use of a magic value of 1 with the unusual comment placement
is ugly. Something like:

usbg_ret = usbg_remove_gadget(g, USBG_RM_CONTENT);

would be more appropriate and meaningful here eliminating the need
for the comment.

Other than that, the API looks fine to me.

-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


[PATCH v3 6/6] libusbg: Update examples to new API functionality.

2014-03-18 Thread Krzysztof Opasiak
Removing gadget/config/function/binding functionality
has been added to API so add example of how to use it.

Signed-off-by: Krzysztof Opasiak 
---
 examples/Makefile.am |3 +-
 examples/gadget-vid-pid-remove.c |  113 ++
 include/usbg/usbg.h  |1 -
 3 files changed, 115 insertions(+), 2 deletions(-)
 create mode 100644 examples/gadget-vid-pid-remove.c

diff --git a/examples/Makefile.am b/examples/Makefile.am
index f9f9407..9fc235a 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,5 +1,6 @@
-bin_PROGRAMS = show-gadgets gadget-acm-ecm 
+bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove
 gadget_acm_ecm_SOURCES = gadget-acm-ecm.c
 show_gadgets_SOURCES = show-gadgets.c
+gadget_vid_pid_remove_SOURCES = gadget-vid-pid-remove.c
 AM_CPPFLAGS=-I../include/
 AM_LDFLAGS=-L../src/ -lusbg
diff --git a/examples/gadget-vid-pid-remove.c b/examples/gadget-vid-pid-remove.c
new file mode 100644
index 000..e62d231
--- /dev/null
+++ b/examples/gadget-vid-pid-remove.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2014 Samsung Electronics
+ *
+ * Krzysztof Opasiak 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+/**
+ * @file gadget-vid-pid-remove.c
+ * @example gadget-vid-pid-remove.c
+ * This is an example of how to find and remove an gadget device with given
+ * Vendor ID and product ID.
+ */
+
+#include 
+#include 
+#include 
+
+#define VENDOR 0x1d6b
+#define PRODUCT0x0104
+
+int remove_gadget(usbg_gadget *g)
+{
+   int usbg_ret;
+   char udc[USBG_MAX_STR_LENGTH];
+
+   /* Check if gadget is enabled */
+   usbg_ret = usbg_get_gadget_udc(g, udc, USBG_MAX_STR_LENGTH);
+   if (usbg_ret != USBG_SUCCESS) {
+   fprintf(stderr, "Error on USB get gadget udc\n");
+   fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+   usbg_strerror(usbg_ret));
+   goto out;
+   }
+
+   /* If gadget is enable we have to disable it first */
+   if (udc[0] != '\0') {
+   usbg_ret = usbg_disable_gadget(g);
+   if (usbg_ret != USBG_SUCCESS) {
+   fprintf(stderr, "Error on USB disable gadget udc\n");
+   fprintf(stderr, "Error: %s : %s\n", 
usbg_error_name(usbg_ret),
+   usbg_strerror(usbg_ret));
+   goto out;
+   }
+   }
+
+   /* Remove gadget */
+   usbg_ret = usbg_remove_gadget(g, 1 /* Remove also content */);
+   if (usbg_ret != USBG_SUCCESS) {
+   fprintf(stderr, "Error on USB gadget remove\n");
+   fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+   usbg_strerror(usbg_ret));
+   }
+
+out:
+   return usbg_ret;
+}
+
+int main(void)
+{
+   int usbg_ret;
+   int ret = -EINVAL;
+   usbg_state *s;
+   usbg_gadget *g;
+   usbg_gadget_attrs g_attrs;
+
+   usbg_ret = usbg_init("/sys/kernel/config", &s);
+   if (usbg_ret != USBG_SUCCESS) {
+   fprintf(stderr, "Error on USB state init\n");
+   fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+   usbg_strerror(usbg_ret));
+   goto out1;
+   }
+
+   g = usbg_get_first_gadget(s);
+   while (g != NULL) {
+   /* Get current gadget attrs to be compared */
+   usbg_ret = usbg_get_gadget_attrs(g, &g_attrs);
+   if (usbg_ret != USBG_SUCCESS) {
+   fprintf(stderr, "Error on USB get gadget attrs\n");
+   fprintf(stderr, "Error: %s : %s\n", 
usbg_error_name(usbg_ret),
+   usbg_strerror(usbg_ret));
+   goto out2;
+   }
+
+   /* Compare attrs with given values and remove if suitable */
+   if (g_attrs.idVendor == VENDOR && g_attrs.idProduct == PRODUCT) 
{
+   usbg_gadget *g_next = usbg_get_next_gadget(g);
+
+   usbg_ret = remove_gadget(g);
+   if (usbg_ret != USBG_SUCCESS)
+   goto out2;
+
+   g = g_next;
+   } else {
+   g = usbg_get_next_gadget(g);
+   }
+   }
+
+out2:
+   usbg_cleanup(s);
+out1:
+   return ret;
+}
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
ind