summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keeping <john@metanate.com>2018-01-11 17:22:47 +0000
committerKrzysztof Opasiak <k.opasiak@samsung.com>2018-01-17 16:12:56 +0100
commit88cff3cbf7d557b73ca3cfabedd7c22ba2623be7 (patch)
tree755c1841581461d48763de102589cb078e149e77
parent5740ca8a5ea96316cc9363aed7d2fe8268201b15 (diff)
downloadlibusbg-88cff3cbf7d557b73ca3cfabedd7c22ba2623be7.tar.gz
libusbg-88cff3cbf7d557b73ca3cfabedd7c22ba2623be7.tar.bz2
libusbg-88cff3cbf7d557b73ca3cfabedd7c22ba2623be7.zip
libusbgx: examples: Add UAC2 function example
Show how to create a USB Audio Class 2 gadget. Signed-off-by: John Keeping <john@metanate.com> Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r--examples/Makefile.am2
-rw-r--r--examples/gadget-uac2.c117
-rw-r--r--packaging/libusbgx.spec1
3 files changed, 119 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 52bbc27..9934321 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-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
+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-uac2
gadget_acm_ecm_SOURCES = gadget-acm-ecm.c
show_gadgets_SOURCES = show-gadgets.c
gadget_vid_pid_remove_SOURCES = gadget-vid-pid-remove.c
diff --git a/examples/gadget-uac2.c b/examples/gadget-uac2.c
new file mode 100644
index 0000000..22f1802
--- /dev/null
+++ b/examples/gadget-uac2.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2018 Metanate Ltd
+ *
+ * John Keeping <john@metanate.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>
+#include <usbg/function/uac2.h>
+
+#define VENDOR 0x1d6b
+#define PRODUCT 0x0104
+
+int main() {
+ usbg_state *s;
+ usbg_gadget *g;
+ usbg_config *c;
+ usbg_function *f_uac2;
+ 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 = "1xUAC2"
+ };
+
+ struct usbg_f_uac2_attrs f_attrs = {
+ .c_chmask = 3,
+ .c_srate = 44100,
+ .c_ssize = 4,
+ .p_chmask = 3,
+ .p_srate = 44100,
+ .p_ssize = 4,
+ };
+
+ usbg_ret = usbg_init("/sys/kernel/config", &s);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error on usbg 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 creating 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_UAC2, "usb0", &f_attrs, &f_uac2);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error creating function\n");
+ fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
+ usbg_strerror(usbg_ret));
+ goto out2;
+ }
+
+ 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, "some_name", f_uac2);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error adding function\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 84c878e..02e503a 100644
--- a/packaging/libusbgx.spec
+++ b/packaging/libusbgx.spec
@@ -79,6 +79,7 @@ make
%{_bindir}/gadget-hid
%{_bindir}/gadget-export
%{_bindir}/gadget-import
+%{_bindir}/gadget-uac2
%{_bindir}/show-udcs
%{_bindir}/gadget-rndis-os-desc