summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Opasiak <k.opasiak@samsung.com>2015-04-14 11:59:01 +0200
committerKrzysztof Opasiak <k.opasiak@samsung.com>2015-04-27 10:14:44 +0200
commitdc076f0c9b041107dcafde9db1bb6023f108938a (patch)
treecb5edab58ae02d12faa74aa522f9ee952cae33e2
parent50b6201178d6ebc04874bb220c85dbe29d736f5a (diff)
downloadlibusbg-dc076f0c9b041107dcafde9db1bb6023f108938a.tar.gz
libusbg-dc076f0c9b041107dcafde9db1bb6023f108938a.tar.bz2
libusbg-dc076f0c9b041107dcafde9db1bb6023f108938a.zip
libusbg: Add example with mass storage function
Mass storage support has been recently added to libusbg. This commit adds an example program which show how to create gadget with this function. Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com> Reviewed-by: Pawel Szewczyk <p.szewczyk@samsung.com>
-rw-r--r--examples/Makefile.am2
-rw-r--r--examples/gadget-ms.c159
2 files changed, 160 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index d7a3b48..3164256 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
+bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget-import show-udcs gadget-ms
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-ms.c b/examples/gadget-ms.c
new file mode 100644
index 0000000..a37b163
--- /dev/null
+++ b/examples/gadget-ms.c
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+/**
+ * @file gadget-ms.c
+ * @example gadget-ms.c
+ * This is an example of how to create gadget with mass storage function
+ * with two luns.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <usbg/usbg.h>
+
+#define VENDOR 0x1d6b
+#define PRODUCT 0x0104
+
+int main(int argc, char **argv)
+{
+ usbg_state *s;
+ usbg_gadget *g;
+ usbg_config *c;
+ usbg_function *f_ms;
+ int ret = -EINVAL;
+ int usbg_ret;
+ usbg_f_ms_lun_attrs f_ms_luns_array[] = {
+ {
+ .id = -1, /* allows to place in any position */
+ .cdrom = 1,
+ .ro = 0,
+ .nofua = 0,
+ .removable = 1,
+ .filename = "",
+ }, {
+ .id = -1, /* allows to place in any position */
+ .cdrom = 0,
+ .ro = 0,
+ .nofua = 0,
+ .removable = 1,
+ .filename = argv[1],
+ }
+ };
+
+ usbg_f_ms_lun_attrs *f_ms_luns[] = {
+ /*
+ * When id in lun structure is below 0 we can place it in any
+ * arbitrary position
+ */
+ &f_ms_luns_array[1],
+ &f_ms_luns_array[0],
+ NULL,
+ };
+
+ usbg_function_attrs f_attrs = {
+ .header.attrs_type = USBG_F_ATTRS_MS,
+ .attrs.ms = {
+ .stall = 0,
+ .nluns = 2,
+ .luns = f_ms_luns,
+ },
+ };
+
+ 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 = {
+ "1xMass Storage"
+ };
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage: gadget-ms file\n");
+ goto out1;
+ }
+
+ 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, F_MASS_STORAGE, "my_reader",
+ &f_attrs, &f_ms);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error creating mass storage function\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, "some_name_here", f_ms);
+ if (usbg_ret != USBG_SUCCESS) {
+ fprintf(stderr, "Error adding ms 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;
+}