summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Opasiak <k.opasiak@samsung.com>2017-12-06 21:10:41 +0100
committerKrzysztof Opasiak <k.opasiak@samsung.com>2017-12-12 14:06:36 +0100
commitbfa2c29daad1d8ce371f36f7786df28698861a11 (patch)
tree01976c0df3338297c277966f82f901e830313cf9
parent1809b5ae60a6eb54af0813b6393192c7756975c0 (diff)
downloadlibusbg-bfa2c29daad1d8ce371f36f7786df28698861a11.tar.gz
libusbg-bfa2c29daad1d8ce371f36f7786df28698861a11.tar.bz2
libusbg-bfa2c29daad1d8ce371f36f7786df28698861a11.zip
libusbgx: examples: Add rndis with OS descriptors support example
Add a program which shows how to use OS descriptors support based on RNDIS example. Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r--examples/Makefile.am3
-rw-r--r--examples/gadget-rndis-os-desc.c152
-rw-r--r--packaging/libusbgx.spec1
3 files changed, 155 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 69d0f9b..52bbc27 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,10 +1,11 @@
-bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget-import show-udcs gadget-ms gadget-midi gadget-hid
+bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget-import show-udcs gadget-ms gadget-midi gadget-hid gadget-rndis-os-desc
gadget_acm_ecm_SOURCES = gadget-acm-ecm.c
show_gadgets_SOURCES = show-gadgets.c
gadget_vid_pid_remove_SOURCES = gadget-vid-pid-remove.c
gadget_ffs_SOURCES = gadget-ffs.c
gadget_export_SOURCE = gadget-export.c
gadget_import_SOURCE = gadget-import.c
+gadget_rndis_os_desc_SOURCES = gadget-rndis-os-desc.c
show_udcs_SOURCE = show-udcs.c
AM_CPPFLAGS=-I$(top_srcdir)/include/
AM_LDFLAGS=-L../src/ -lusbgx
diff --git a/examples/gadget-rndis-os-desc.c b/examples/gadget-rndis-os-desc.c
new file mode 100644
index 0000000..837d3b4
--- /dev/null
+++ b/examples/gadget-rndis-os-desc.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2017 Samsung Electronics
+ *
+ * Krzysztof Opasiak <k.opasiak@samsung.com>
+ *
+ * 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.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <linux/usb/ch9.h>
+#include <usbg/usbg.h>
+
+/**
+ * @file gadget-rndis-os-desc.c
+ * @example gadget-rndis-os-desc.c
+ * This is an example of how to create an gadget with OS String support.
+ */
+
+#define VENDOR 0x1d6b
+#define PRODUCT 0x0104
+
+int main(void)
+{
+ usbg_state *s;
+ usbg_gadget *g;
+ usbg_config *c;
+ usbg_function *f_rndis;
+ int ret = -EINVAL;
+ int usbg_ret;
+
+ struct usbg_gadget_attrs g_attrs = {
+ .bcdUSB = 0x0200,
+ .bDeviceClass = USB_CLASS_PER_INTERFACE,
+ .bDeviceSubClass = 0x00,
+ .bDeviceProtocol = 0x00,
+ .bMaxPacketSize0 = 64, /* Max allowed ep0 packet size */
+ .idVendor = VENDOR,
+ .idProduct = PRODUCT,
+ .bcdDevice = 0x0001, /* Verson of device */
+ };
+
+ struct usbg_gadget_strs g_strs = {
+ .serial = "0123456789", /* Serial number */
+ .manufacturer = "Foo Inc.", /* Manufacturer */
+ .product = "Bar Gadget" /* Product string */
+ };
+
+ struct usbg_config_strs c_strs = {
+ .configuration = "RNDIS",
+ };
+
+ struct usbg_gadget_os_descs g_os_desc = {
+ .use = true,
+ .b_vendor_code = 0xBC,
+ .qw_sign = "MSFT100",
+ };
+
+ struct usbg_function_os_desc f_os_desc = {
+ .compatible_id = "RNDIS",
+ .sub_compatible_id = "5162001",
+ };
+
+ usbg_ret = usbg_init("/sys/kernel/config", &s);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error on USB gadget init\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out1;
+ }
+
+ usbg_ret = usbg_create_gadget(s, "g1", &g_attrs, &g_strs, &g);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error on create gadget\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_create_function(g, USBG_F_RNDIS, "usb0", NULL, &f_rndis);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error creating RNDIS function\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_set_interf_os_desc(f_rndis, "rndis", &f_os_desc);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error setting function OS desc\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ /* NULL can be passed to use kernel defaults */
+ usbg_ret = usbg_create_config(g, 1, "The only one", NULL, &c_strs, &c);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error creating config\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_add_config_function(c, "rndis.usb0", f_rndis);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error adding rndis.usb0\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_set_gadget_os_descs(g, &g_os_desc);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error setting gadget OS desc\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_set_os_desc_config(g, c);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error setting gadget OS desc config\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ usbg_ret = usbg_enable_gadget(g, DEFAULT_UDC);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error enabling gadget\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ ret = 0;
+
+out2:
+ usbg_cleanup(s);
+
+out1:
+ return ret;
+}
diff --git a/packaging/libusbgx.spec b/packaging/libusbgx.spec
index 422ba2f..84c878e 100644
--- a/packaging/libusbgx.spec
+++ b/packaging/libusbgx.spec
@@ -80,5 +80,6 @@ make
%{_bindir}/gadget-export
%{_bindir}/gadget-import
%{_bindir}/show-udcs
+%{_bindir}/gadget-rndis-os-desc
%changelog