From 0553c45c78c6865a1b1e3386bbe686e0fffd9c87 Mon Sep 17 00:00:00 2001
From: Pawel Szewczyk
Date: Tue, 19 May 2015 10:48:11 +0200
Subject: libusbg: examples: add example of midi function usage
Support for midi function was recently added. This commit adds an example
program which shows how to create midi gadget with this function.
Signed-off-by: Pawel Szewczyk
Reviewed-by: Krzysztof Opasiak
---
examples/Makefile.am | 2 +-
examples/gadget-midi.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 119 insertions(+), 1 deletion(-)
create mode 100644 examples/gadget-midi.c
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 3164256..36e0b9b 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
+bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget-import show-udcs gadget-ms gadget-midi
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-midi.c b/examples/gadget-midi.c
new file mode 100644
index 0000000..44699f3
--- /dev/null
+++ b/examples/gadget-midi.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2014 Samsung Electronics
+ *
+ * Pawel Szewczyk
+ *
+ * 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
+#include
+#include
+
+#define VENDOR 0x1d6b
+#define PRODUCT 0x0104
+
+int main() {
+ usbg_state *s;
+ usbg_gadget *g;
+ usbg_config *c;
+ usbg_function *f_midi;
+ int ret = -EINVAL;
+ int usbg_ret;
+
+ usbg_gadget_attrs g_attrs = {
+ 0x0200, /* bcdUSB */
+ 0x00, /* Defined at interface level */
+ 0x00, /* subclass */
+ 0x00, /* device protocol */
+ 0x0040, /* Max allowed packet size */
+ VENDOR,
+ PRODUCT,
+ 0x0001, /* Verson of device */
+ };
+
+ usbg_gadget_strs g_strs = {
+ "0123456789", /* Serial number */
+ "Foo Inc.", /* Manufacturer */
+ "Bar Gadget" /* Product string */
+ };
+
+ usbg_config_strs c_strs = {
+ "1xMIDI"
+ };
+
+ usbg_function_attrs f_attrs = {
+ .header.attrs_type = USBG_F_ATTRS_MIDI,
+ .attrs.midi = {
+ .index = 0,
+ .id = "usb0",
+ .buflen = 128,
+ .qlen = 16,
+ .in_ports = 2,
+ .out_ports = 3,
+ },
+ };
+
+ 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, F_MIDI, "usb0", &f_attrs, &f_midi);
+ 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_midi);
+ 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;
+}
--
cgit v1.2.3