interface: usb: Add support for USB interface 29/223029/12
authorDongwoo Lee <dwoo08.lee@samsung.com>
Tue, 17 Mar 2020 04:53:32 +0000 (13:53 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Wed, 18 Mar 2020 09:31:54 +0000 (18:31 +0900)
USB interface is added and enabled by configuring gadget driver
through configfs with support of libusbgx.

Change-Id: I638bfa59e127f7896f8ed4b6fa11ba3d37d4548e
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
CMakeLists.txt
packaging/initrd-flash.spec
scripts/flash-init.sh
src/usb.c [new file with mode: 0644]

index 9fcc572..7c33f4a 100644 (file)
@@ -6,10 +6,10 @@ SET(CMAKE_EXE_LINKER_FLAGS "-pie")
 
 FIND_PACKAGE(Threads REQUIRED)
 
-ADD_EXECUTABLE(${PROJECT_NAME} src/main.c src/thor.c src/dfu.c src/interface.c src/net.c)
+ADD_EXECUTABLE(${PROJECT_NAME} src/main.c src/thor.c src/dfu.c src/interface.c src/net.c src/usb.c)
 
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src)
 
-TARGET_LINK_LIBRARIES(flash-manager "${CMAKE_THREAD_LIBS_INIT}" blkid)
+TARGET_LINK_LIBRARIES(flash-manager "${CMAKE_THREAD_LIBS_INIT}" blkid usbgx)
 
 INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin)
index d6dc741..a2b1889 100644 (file)
@@ -8,6 +8,7 @@ Source0:    %{name}-%{version}.tar.gz
 ExclusiveArch: %{arm}
 BuildRequires: cmake
 BuildRequires: libblkid-devel
+BuildRequires: pkgconfig(libusbgx)
 
 Requires: util-linux
 Requires: bash
@@ -16,6 +17,7 @@ Requires: fm-data
 Requires: module-init-tools
 Requires: wpa_supplicant
 Requires: dnsmasq
+Requires: libusbgx
 
 %description
 Provide kernel-based target image downloader.
index 799e80a..6473dd7 100755 (executable)
@@ -3,6 +3,8 @@
 INFORM_FILE=reboot-param.bin
 INFORM_MOUNT_PATH=/mnt/inform
 TMP_PATH=/tmp
+SYSFS_PATH=/sys
+CONFIGFS_PATH=/sys/kernel/config
 
 BLKID="/usr/sbin/blkid"
 FLASH_MANAGER="/usr/bin/flash-manager"
@@ -33,6 +35,22 @@ clear_bootmode() {
 }
 
 #------------------------------------------------
+#      setup_usb
+#------------------------------------------------
+setup_usb() {
+       SYSFS=$($MOUNT | $GREP sysfs)
+       if [ -z "$SYSFS" ]; then
+               $MOUNT -t sysfs none $SYSFS_PATH
+       fi
+
+       CONFIGFS=$($MOUNT | $GREP configfs)
+       if [ -z "$CONFIGFS" ]; then
+               $MOUNT -t configfs none $CONFIGFS_PATH
+       fi
+}
+
+
+#------------------------------------------------
 # Copy Module and Firmware file
 #------------------------------------------------
 setup_wireless() {
@@ -126,6 +144,7 @@ do_flash() {
 #------------------------------------------------
 setup_wired_nic
 setup_wireless
+setup_usb
 clear_bootmode
 print_info
 do_flash
diff --git a/src/usb.c b/src/usb.c
new file mode 100644 (file)
index 0000000..f7920bb
--- /dev/null
+++ b/src/usb.c
@@ -0,0 +1,492 @@
+/*
+ * flash-manager - Tizen kernel-level image flashing solution
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <endian.h>
+#include <pthread.h>
+#include <usbg/usbg.h>
+#include <asm/byteorder.h>
+
+#include <linux/usb/functionfs.h>
+#include <linux/usb/cdc.h>
+
+#include "interface.h"
+
+#define TFMFFS_PATH            "/dev/tfmffs"
+#define TFMFFS_GADGET          "g1"
+#define TFMFFS_DEV             "tfm"
+#define TFMFFS_BIND_LINK       "tfm.ffs"
+#define TFMFFS_VID 0x04e8
+#define TFMFFS_PID 0x685d
+
+enum {
+       TFMFFS_EP0,
+       TFMFFS_EP_INT_IN,
+       TFMFFS_EP_BULK_IN,
+       TFMFFS_EP_BULK_OUT,
+       TFMFFS_EP_MAX,
+};
+
+struct usb_context {
+       usbg_state *usbg;
+       pthread_t ep0_thread;
+       int eps[TFMFFS_EP_MAX];
+
+       int enabled;
+
+       pthread_mutex_t mutex;
+       pthread_cond_t ready;
+};
+
+struct usbg_gadget_attrs g_attrs = {
+       .idVendor = TFMFFS_VID,
+       .idProduct = TFMFFS_PID,
+};
+
+struct usbg_gadget_strs g_strs = {
+       .serial = "Unavailable", /* Serial number */
+       .manufacturer = "Tizen", /* Manufacturer */
+       .product = "iot-refboard" /* Product string */
+};
+
+struct usbg_config_strs c_strs = {
+       .configuration = "Conf 1"
+};
+
+/******************** Descriptors and Strings *******************************/
+
+static const struct {
+       struct usb_functionfs_descs_head header;
+       struct {
+               struct usb_interface_descriptor acm;
+               struct usb_endpoint_descriptor_no_audio int_in;
+               struct usb_interface_descriptor intf;
+               struct usb_endpoint_descriptor_no_audio bulk_in;
+               struct usb_endpoint_descriptor_no_audio bulk_out;
+       } __attribute__ ((__packed__)) fs_descs, hs_descs;
+} __attribute__ ((__packed__)) descriptors = {
+       .header = {
+               .magic = __cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
+               .length = __cpu_to_le32(sizeof(descriptors)),
+               .fs_count = __cpu_to_le32(5),
+               .hs_count = __cpu_to_le32(5),
+       },
+       .fs_descs = {
+               .acm = {
+                       .bLength = sizeof(descriptors.fs_descs.intf),
+                       .bDescriptorType = USB_DT_INTERFACE,
+                       .bInterfaceNumber = 0,
+                       .bNumEndpoints = 1,
+                       .bInterfaceClass = USB_CLASS_COMM,
+                       .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
+                       .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
+                       .iInterface = 1,
+               },
+               .int_in = {
+                       .bLength = sizeof(descriptors.fs_descs.bulk_in),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 3 | USB_DIR_IN,
+                       .bmAttributes = USB_ENDPOINT_XFER_INT,
+               },
+               .intf = {
+                       .bLength = sizeof(descriptors.fs_descs.intf),
+                       .bDescriptorType = USB_DT_INTERFACE,
+                       .bInterfaceNumber = 1,
+                       .bNumEndpoints = 2,
+                       .bInterfaceClass = USB_CLASS_CDC_DATA,
+                       .iInterface = 1,
+               },
+               .bulk_in = {
+                       .bLength = sizeof(descriptors.fs_descs.bulk_in),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 1 | USB_DIR_IN,
+                       .bmAttributes = USB_ENDPOINT_XFER_BULK,
+               },
+               .bulk_out = {
+                       .bLength = sizeof(descriptors.fs_descs.bulk_out),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 2 | USB_DIR_OUT,
+                       .bmAttributes = USB_ENDPOINT_XFER_BULK,
+               },
+       },
+       .hs_descs = {
+               .acm = {
+                       .bLength = sizeof(descriptors.fs_descs.intf),
+                       .bDescriptorType = USB_DT_INTERFACE,
+                       .bInterfaceNumber = 0,
+                       .bNumEndpoints = 1,
+                       .bInterfaceClass = USB_CLASS_COMM,
+                       .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
+                       .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
+                       .iInterface = 1,
+               },
+               .int_in = {
+                       .bLength = sizeof(descriptors.fs_descs.bulk_in),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 3 | USB_DIR_IN,
+                       .bmAttributes = USB_ENDPOINT_XFER_INT,
+                       .wMaxPacketSize = __cpu_to_le16(16),
+                       .bInterval = 10,
+               },
+               .intf = {
+                       .bLength = sizeof(descriptors.hs_descs.intf),
+                       .bDescriptorType = USB_DT_INTERFACE,
+                       .bInterfaceNumber = 1,
+                       .bNumEndpoints = 2,
+                       .bInterfaceClass = USB_CLASS_CDC_DATA,
+                       .iInterface = 1,
+               },
+               .bulk_in = {
+                       .bLength = sizeof(descriptors.hs_descs.bulk_in),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 1 | USB_DIR_IN,
+                       .bmAttributes = USB_ENDPOINT_XFER_BULK,
+                       .wMaxPacketSize = __cpu_to_le16(512),
+               },
+               .bulk_out = {
+                       .bLength = sizeof(descriptors.hs_descs.bulk_out),
+                       .bDescriptorType = USB_DT_ENDPOINT,
+                       .bEndpointAddress = 2 | USB_DIR_OUT,
+                       .bmAttributes = USB_ENDPOINT_XFER_BULK,
+                       .wMaxPacketSize = __cpu_to_le16(512),
+               },
+       },
+};
+
+#define STR_INTERFACE "TIZEN USB DRIVER"
+
+static const struct {
+       struct usb_functionfs_strings_head header;
+       struct {
+               __le16 code;
+               const char str1[sizeof(STR_INTERFACE)];
+       } __attribute__ ((__packed__)) lang0;
+} __attribute__ ((__packed__)) strings = {
+       .header = {
+               .magic = __cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
+               .length = __cpu_to_le32(sizeof(strings)),
+               .str_count = __cpu_to_le32(1),
+               .lang_count = __cpu_to_le32(1),
+       },
+       .lang0 = {
+               __cpu_to_le16(0x0409), /* en-us */
+               STR_INTERFACE,
+       },
+};
+
+static ssize_t usb_rx_data(int fd, void *buf, ssize_t len)
+{
+       return read(fd, buf, len);
+}
+
+static ssize_t usb_tx_data(int fd, void *buf, ssize_t len)
+{
+       return write(fd, buf, len);
+}
+
+static void *ep0_handler(void *data)
+{
+       struct usb_context *ctx = data;
+       struct usb_functionfs_event evt;
+       int ret, ep0 = ctx->eps[TFMFFS_EP0];
+
+       pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+
+       for (;;) {
+               ret = read(ep0, &evt, sizeof(evt));
+               if (ret <= 0)
+                       goto err;
+
+               switch (evt.type) {
+               case FUNCTIONFS_SETUP:
+               {
+                       struct usb_ctrlrequest *ctrl = &evt.u.setup;
+
+                       if (ctrl->bRequestType & USB_DIR_IN) {
+                               if (write(ep0, NULL, 0) < 0)
+                                       goto err;
+                       } else { /* ctrl->bRequestType & USB_DIR_OUT */
+                               switch (ctrl->bRequest) {
+                               /* handle CDC ACM setup request */
+                               case USB_CDC_REQ_SET_LINE_CODING:
+                               {
+                                       int value = __le16_to_cpu(ctrl->wLength);
+                                       char *buf;
+
+                                       buf = malloc(value);
+                                       if (!buf)
+                                               goto err;
+
+                                       if (read(ep0, buf, value) < 0) {
+                                               free(buf);
+                                               goto err;
+                                       }
+                                       free(buf);
+                                       break;
+                               }
+                               case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
+                               {
+                                       int buf;
+
+                                       if (read(ep0, &buf, sizeof(buf)) < 0)
+                                               goto err;
+                                       break;
+                               }
+                               default:
+                                       if (read(ep0, NULL, 0) < 0)
+                                               goto err;
+                               }
+                       }
+                       break;
+               }
+               case FUNCTIONFS_ENABLE:
+                       pthread_mutex_lock(&ctx->mutex);
+                       ctx->enabled = 1;
+                       pthread_mutex_unlock(&ctx->mutex);
+                       pthread_cond_signal(&ctx->ready);
+                       break;
+               case FUNCTIONFS_DISABLE:
+               case FUNCTIONFS_BIND:
+               case FUNCTIONFS_UNBIND:
+               default:
+                       /* do nothing */
+                       break;
+               }
+       }
+err:
+       fprintf(stderr, "Error occurs while handling ep0 event");
+       return NULL;
+}
+
+static int usb_setup_ffs_daemon(struct usb_context *ctx)
+{
+       char *ep_path;
+       int *ep = ctx->eps;
+       int ep_path_len = strlen(TFMFFS_PATH) + 4 /* "/ep#" */ + 1 /* '\0' */;
+       int i, ret;
+
+       ep_path = malloc(ep_path_len);
+       if (!ep_path)
+               return -ENOMEM;
+
+       snprintf(ep_path, ep_path_len, TFMFFS_PATH"/ep%d", TFMFFS_EP0);
+
+       ep[TFMFFS_EP0] = open(ep_path, O_RDWR);
+       if (ep[TFMFFS_EP0] < 0) {
+               fprintf(stderr, "failed to open ffs ep0\n");
+               ret = -ENOENT;
+               goto err_free;
+       }
+
+       if (write(ep[TFMFFS_EP0], &descriptors, sizeof(descriptors)) < 0) {
+               fprintf(stderr, "failed to write descriptors\n");
+               ret = -EIO;
+               goto err_close;
+       }
+       if (write(ep[TFMFFS_EP0], &strings, sizeof(strings)) < 0) {
+               fprintf(stderr, "failed to write strings\n");
+               ret = -EIO;
+               goto err_close;
+       }
+
+       for (i = 1; i < TFMFFS_EP_MAX; i++) {
+               snprintf(ep_path, ep_path_len, TFMFFS_PATH"/ep%d", i);
+
+               ep[i] = open(ep_path, O_RDWR);
+               if (ep[i] < 0) {
+                       fprintf(stderr, "failed to open ffs ep%d\n", i);
+                       /* close opened endpoints */
+                       for (i -= 1; i >= 1; i--)
+                               close(ep[i]);
+                       ret = -EIO;
+                       goto err_close;
+               }
+       }
+
+       pthread_mutex_init(&ctx->mutex, NULL);
+       pthread_cond_init(&ctx->ready, NULL);
+
+       pthread_create(&ctx->ep0_thread, NULL, ep0_handler, ctx);
+       pthread_detach(ctx->ep0_thread);
+
+       free(ep_path);
+
+       return 0;
+
+err_close:
+       close(ep[TFMFFS_EP0]);
+
+err_free:
+       free(ep_path);
+
+       return ret;
+}
+
+static void usb_cleanup_ffs_daemon(struct usb_context *ctx)
+{
+       int *ep = ctx->eps;
+
+       pthread_cancel(ctx->ep0_thread);
+       close(ep[TFMFFS_EP0]);
+}
+
+static int mount_ffs(void)
+{
+       int ret;
+
+       ret = mkdir(TFMFFS_PATH, 0770);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to create ffs directory\n");
+               return ret;
+       }
+
+       ret = mount("tfm", TFMFFS_PATH, "functionfs", 0, NULL);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to mount ffs function\n");
+               rmdir(TFMFFS_PATH);
+       }
+
+       return ret;
+}
+
+static void umount_ffs(void)
+{
+       umount(TFMFFS_PATH);
+       rmdir(TFMFFS_PATH);
+}
+
+static int usb_connect(struct tfm_interface *intf)
+{
+       struct usb_context *ctx;
+       usbg_state *s;
+       usbg_gadget *g;
+       usbg_config *c;
+       usbg_function *f;
+       int ret;
+
+       ctx = (struct usb_context *)malloc(sizeof(*ctx));
+       if (!ctx)
+               return -ENOMEM;
+
+       ret = usbg_init("/sys/kernel/config", &s);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to init USB gadget\n");
+               goto err_freectx;
+       }
+
+       ret = usbg_create_gadget(s, TFMFFS_GADGET, &g_attrs, &g_strs, &g);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to create USB gadget\n");
+               goto err_cleanup;
+       }
+
+       ret = usbg_create_function(g, USBG_F_FFS, TFMFFS_DEV, NULL, &f);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to create USB function\n");
+               goto err_cleanup;
+       }
+
+       ret = usbg_create_config(g, 1, NULL, NULL, &c_strs, &c);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to create USB config\n");
+               goto err_cleanup;
+       }
+
+       ret = usbg_add_config_function(c, TFMFFS_BIND_LINK, f);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to bind USB function\n");
+               goto err_cleanup;
+       }
+
+       if (mount_ffs() < 0) {
+               fprintf(stderr, "Failed to mount functionfs\n");
+               goto err_cleanup;
+       }
+
+       if (usb_setup_ffs_daemon(ctx) < 0) {
+               fprintf(stderr, "Failed to setup ffs daemon\n");
+               goto err_umount;
+       }
+
+       ret = usbg_enable_gadget(g, DEFAULT_UDC);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to enable USB gadget\n");
+               goto err_ffs;
+       }
+
+       pthread_mutex_lock(&ctx->mutex);
+       while (!ctx->enabled)
+               pthread_cond_wait(&ctx->ready, &ctx->mutex);
+       pthread_mutex_unlock(&ctx->mutex);
+
+       ctx->usbg = s;
+
+       intf->txd = ctx->eps[TFMFFS_EP_BULK_IN];
+       intf->rxd = ctx->eps[TFMFFS_EP_BULK_OUT];
+       intf->priv = (void *)ctx;
+
+       return 0;
+
+err_ffs:
+       usb_cleanup_ffs_daemon(ctx);
+
+err_umount:
+       umount_ffs();
+
+err_cleanup:
+       usbg_cleanup(s);
+
+err_freectx:
+       free(ctx);
+
+       return -EINVAL;
+}
+
+static int usb_disconnect(struct tfm_interface *intf)
+{
+       struct usb_context *ctx = intf->priv;
+       int *ep = ctx->eps;
+       int i;
+
+       for (i = 1; i < TFMFFS_EP_MAX; i++)
+               close(ep[i]);
+
+       usb_cleanup_ffs_daemon(ctx);
+       umount_ffs();
+       if (ctx->usbg)
+               usbg_cleanup(ctx->usbg);
+
+       return 0;
+}
+
+static struct tfm_interface_driver usb = {
+       .name = "usb",
+       .ops = {
+               .connect = usb_connect,
+               .disconnect = usb_disconnect,
+               .rx_data = usb_rx_data,
+               .tx_data = usb_tx_data,
+       },
+};
+INTERFACE_REGISTER(usb)