Support both functionfs and sdb gadget 79/27179/1
authorŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 4 Sep 2014 12:27:24 +0000 (14:27 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Fri, 5 Sep 2014 14:13:12 +0000 (16:13 +0200)
Automatically detect the kernel interface to use for communication
with host.

Change-Id: I51834991063d9bbb686598490ddc7a0b607fc282
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
CMakeLists.txt
src/sdb.c
src/sdb.h
src/usb_funcfs_client.c
src/usb_linux_client.c

index 1928023..681a995 100644 (file)
@@ -42,16 +42,12 @@ set(sdbd_SRCS
        src/init.c
        src/fileutils.c
        src/commandline_sdbd.c
+       src/usb_linux_client.c
+       src/usb_funcfs_client.c
 )
 
 include_directories(src)
 
-if(USE_FUNCTION_FS)
-       list(APPEND sdbd_SRCS src/usb_funcfs_client.c)
-else()
-       list(APPEND sdbd_SRCS src/usb_linux_client.c)
-endif()
-
 add_executable(sdbd ${sdbd_SRCS})
 
 set_property(
index 1018430..3fbe423 100644 (file)
--- a/src/sdb.c
+++ b/src/sdb.c
@@ -1,4 +1,5 @@
-/*
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*-
+ *
  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
@@ -55,6 +56,13 @@ int HOST = 0;
 SdbdCommandlineArgs sdbd_commandline_args;
 #endif
 
+void (*usb_init)() = NULL;
+void (*usb_cleanup)() = NULL;
+int (*usb_write)(usb_handle *h, const void *data, int len) = NULL;
+int (*usb_read)(usb_handle *h, void *data, int len) = NULL;
+int (*usb_close)(usb_handle *h) = NULL;
+void (*usb_kick)(usb_handle *h) = NULL;
+
 int is_emulator(void) {
 #if SDB_HOST
        return 0;
@@ -1233,6 +1241,24 @@ int sdb_main(int is_daemon, int server_port)
     }
 
     if (!is_emulator()) {
+        /* choose the usb gadget backend */
+        if (access(USB_NODE_FILE, F_OK) == 0) {
+            /* legacy kernel-based sdb gadget */
+            usb_init =    &linux_usb_init;
+            usb_cleanup = &linux_usb_cleanup;
+            usb_write =   &linux_usb_write;
+            usb_read =    &linux_usb_read;
+            usb_close =   &linux_usb_close;
+            usb_kick =    &linux_usb_kick;
+        } else {
+            /* functionfs based gadget */
+            usb_init =    &ffs_usb_init;
+            usb_cleanup = &ffs_usb_cleanup;
+            usb_write =   &ffs_usb_write;
+            usb_read =    &ffs_usb_read;
+            usb_close =   &ffs_usb_close;
+            usb_kick =    &ffs_usb_kick;
+        }
         // listen on USB
         usb_init();
     }
index ccee503..8f7b5f9 100644 (file)
--- a/src/sdb.h
+++ b/src/sdb.h
@@ -462,12 +462,39 @@ int  local_connect(int  port, const char *device_name);
 int  local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
 
 /* usb host/client interface */
+#if SDB_HOST
 void usb_init();
 void usb_cleanup();
 int usb_write(usb_handle *h, const void *data, int len);
 int usb_read(usb_handle *h, void *data, int len);
 int usb_close(usb_handle *h);
 void usb_kick(usb_handle *h);
+#else
+
+extern void (*usb_init)();
+extern void (*usb_cleanup)();
+extern int (*usb_write)(usb_handle *h, const void *data, int len);
+extern int (*usb_read)(usb_handle *h, void *data, int len);
+extern int (*usb_close)(usb_handle *h);
+extern void (*usb_kick)(usb_handle *h);
+
+/* functionfs backend */
+void ffs_usb_init();
+void ffs_usb_cleanup();
+int ffs_usb_write(usb_handle *h, const void *data, int len);
+int ffs_usb_read(usb_handle *h, void *data, int len);
+int ffs_usb_close(usb_handle *h);
+void ffs_usb_kick(usb_handle *h);
+
+/* kernel sdb gadget backend */
+void linux_usb_init();
+void linux_usb_cleanup();
+int linux_usb_write(usb_handle *h, const void *data, int len);
+int linux_usb_read(usb_handle *h, void *data, int len);
+int linux_usb_close(usb_handle *h);
+void linux_usb_kick(usb_handle *h);
+
+#endif
 
 /* used for USB device detection */
 #if SDB_HOST
index 0a2ff7e..af9fd2b 100644 (file)
@@ -218,13 +218,6 @@ error:
     sdb_mutex_unlock(&h->control_lock);
 }
 
-
-void usb_cleanup()
-{
-    /* nothing to do here */
-}
-
-
 static void *usb_open_thread(void *x)
 {
     struct usb_handle *usb = (struct usb_handle *)x;
@@ -405,29 +398,6 @@ static int bulk_write(int bulkin_fd, const void *buf, size_t length)
 
 
 /*
- * Writes data to bulk_in descriptor
- *
- * In fact, data is forwarded to bulk_write.
- *
- * @returns 0 on success and -1 on failure (errno is set)
- */
-int usb_write(usb_handle *h, const void *data, int len)
-{
-    int n;
-
-    D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
-    n = bulk_write(h->bulk_in, data, len);
-    if(n != len) {
-        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
-            h->bulk_in, n, errno, strerror(errno));
-        return -1;
-    }
-    D("[ done fd=%d ]\n", h->bulk_in);
-    return 0;
-}
-
-
-/*
  * Reads data from bulkout_fd
  *
  * Blocks until length data is read or error occurs.
@@ -458,34 +428,10 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length)
     return count;
 }
 
-
-/*
- * Reads data from bulk_out descriptor
- *
- * In fact, reading task is forwarded to bulk_read.
- *
- * @returns 0 on success and -1 on failure (errno is set)
- */
-int usb_read(usb_handle *h, void *data, int len)
-{
-    int n;
-
-    D("%d: about to read (fd=%d, len=%d)\n", getpid(), h->bulk_out, len);
-    n = bulk_read(h->bulk_out, data, len);
-    if(n != len) {
-        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
-            h->bulk_out, n, errno, strerror(errno));
-        return -1;
-    }
-    D("[ done fd=%d ]\n", h->bulk_out);
-    return 0;
-}
-
-
 /*
  * Checks if EP0 exists on filesystem
  */
-int ep0_exists()
+static int ep0_exists()
 {
        struct stat statb;
        return stat(ep0_path, &statb) == 0;
@@ -513,9 +459,14 @@ static int autoconfig(struct usb_handle *h)
 
 
 /*
+ * Public host/client interface
+ */
+
+
+/*
  * Creates and starts USB threads
  */
-void usb_init()
+void ffs_usb_init()
 {
     usb_handle *h;
     sdb_thread_t tid;
@@ -550,7 +501,65 @@ void usb_init()
 }
 
 
-void usb_kick(usb_handle *h)
+void ffs_usb_cleanup()
+{
+    /* nothing to do here */
+}
+
+
+/*
+ * Writes data to bulk_in descriptor
+ *
+ * In fact, data is forwarded to bulk_write.
+ *
+ * @returns 0 on success and -1 on failure (errno is set)
+ */
+int ffs_usb_write(usb_handle *h, const void *data, int len)
+{
+    int n;
+
+    D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
+    n = bulk_write(h->bulk_in, data, len);
+    if(n != len) {
+        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
+            h->bulk_in, n, errno, strerror(errno));
+        return -1;
+    }
+    D("[ done fd=%d ]\n", h->bulk_in);
+    return 0;
+}
+
+
+/*
+ * Reads data from bulk_out descriptor
+ *
+ * In fact, reading task is forwarded to bulk_read.
+ *
+ * @returns 0 on success and -1 on failure (errno is set)
+ */
+int ffs_usb_read(usb_handle *h, void *data, int len)
+{
+    int n;
+
+    D("%d: about to read (fd=%d, len=%d)\n", getpid(), h->bulk_out, len);
+    n = bulk_read(h->bulk_out, data, len);
+    if(n != len) {
+        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
+            h->bulk_out, n, errno, strerror(errno));
+        return -1;
+    }
+    D("[ done fd=%d ]\n", h->bulk_out);
+    return 0;
+}
+
+
+int ffs_usb_close(usb_handle *h)
+{
+    return 0;
+}
+
+
+void ffs_usb_kick(usb_handle *h)
 {
     int err;
 
@@ -569,9 +578,3 @@ void usb_kick(usb_handle *h)
     sdb_cond_signal(&h->kick_notify);
     sdb_mutex_unlock(&h->kick_lock);
 }
-
-
-int usb_close(usb_handle *h)
-{
-    return 0;
-}
index 96a3f75..fa813fc 100644 (file)
@@ -37,11 +37,6 @@ struct usb_handle
     sdb_mutex_t lock;
 };
 
-void usb_cleanup()
-{
-    // nothing to do here
-}
-
 static void *usb_open_thread(void *x)
 {
     struct usb_handle *usb = (struct usb_handle *)x;
@@ -82,7 +77,9 @@ static void *usb_open_thread(void *x)
     return 0;
 }
 
-int usb_write(usb_handle *h, const void *data, int len)
+// Public host/client interface
+
+int linux_usb_write(usb_handle *h, const void *data, int len)
 {
     int n;
 
@@ -97,7 +94,7 @@ int usb_write(usb_handle *h, const void *data, int len)
     return 0;
 }
 
-int usb_read(usb_handle *h, void *data, int len)
+int linux_usb_read(usb_handle *h, void *data, int len)
 {
     int n;
 
@@ -112,7 +109,7 @@ int usb_read(usb_handle *h, void *data, int len)
     return 0;
 }
 
-void usb_init()
+void linux_usb_init()
 {
     usb_handle *h;
     sdb_thread_t tid;
@@ -142,7 +139,7 @@ void usb_init()
     }
 }
 
-void usb_kick(usb_handle *h)
+void linux_usb_kick(usb_handle *h)
 {
     D("usb_kick\n");
     sdb_mutex_lock(&h->lock);
@@ -154,8 +151,13 @@ void usb_kick(usb_handle *h)
     sdb_mutex_unlock(&h->lock);
 }
 
-int usb_close(usb_handle *h)
+int linux_usb_close(usb_handle *h)
 {
     // nothing to do here
     return 0;
 }
+
+void linux_usb_cleanup()
+{
+    // nothing to do here
+}