[PATCH 13/13] USB/IP: USB over WebSocket

2015-04-02 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, port, 
bind, unbind, attach and detach). It uses --url option to specify remote 
address and port number. 

Implementation of this patch depends on Poco C++ (http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/websocket/AUTHORS  |   1 +
 tools/usb/usbip/websocket/COPYING  | 340 +
 tools/usb/usbip/websocket/INSTALL  | 237 
 tools/usb/usbip/websocket/Makefile.am  |   3 +
 tools/usb/usbip/websocket/README   | 184 +
 tools/usb/usbip/websocket/autogen.sh   |   9 +
 tools/usb/usbip/websocket/cleanup.sh   |  12 +
 tools/usb/usbip/websocket/configure.ac |  61 +++
 tools/usb/usbip/websocket/doc/usbws.8  | 192 ++
 tools/usb/usbip/websocket/doc/usbwsa.8 | 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8 | 109 ++
 tools/usb/usbip/websocket/poco/Makefile.am |  18 +
 tools/usb/usbip/websocket/poco/USBWSCommand.cpp| 410 +
 tools/usb/usbip/websocket/poco/USBWSCommand.h  |  99 +
 tools/usb/usbip/websocket/poco/USBWSDaemon.cpp | 228 
 tools/usb/usbip/websocket/poco/USBWSDaemon.h   |  80 
 .../usbip/websocket/poco/USBWSRequestHandler.cpp   |  90 +
 .../usb/usbip/websocket/poco/USBWSRequestHandler.h |  49 +++
 .../websocket/poco/USBWSRequestHandlerFactory.cpp  |  47 +++
 .../websocket/poco/USBWSRequestHandlerFactory.h|  48 +++
 tools/usb/usbip/websocket/poco/USBWSUtil.h |  52 +++
 tools/usb/usbip/websocket/poco/USBWSWebSocket.cpp  | 201 ++
 tools/usb/usbip/websocket/poco/USBWSWebSocket.h|  69 
 23 files changed, 2640 insertions(+)
 create mode 100644 tools/usb/usbip/websocket/AUTHORS
 create mode 100644 tools/usb/usbip/websocket/COPYING
 create mode 100644 tools/usb/usbip/websocket/INSTALL
 create mode 100644 tools/usb/usbip/websocket/Makefile.am
 create mode 100644 tools/usb/usbip/websocket/README
 create mode 100755 tools/usb/usbip/websocket/autogen.sh
 create mode 100755 tools/usb/usbip/websocket/cleanup.sh
 create mode 100644 tools/usb/usbip/websocket/configure.ac
 create mode 100644 tools/usb/usbip/websocket/doc/usbws.8
 create mode 100644 tools/usb/usbip/websocket/doc/usbwsa.8
 create mode 100644 tools/usb/usbip/websocket/doc/usbwsd.8
 create mode 100644 tools/usb/usbip/websocket/poco/Makefile.am
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSCommand.cpp
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSCommand.h
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSDaemon.cpp
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSDaemon.h
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSRequestHandler.cpp
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSRequestHandler.h
 create mode 100644 
tools/usb/usbip/websocket/poco/USBWSRequestHandlerFactory.cpp
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSRequestHandlerFactory.h
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSUtil.h
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSWebSocket.cpp
 create mode 100644 tools/usb/usbip/websocket/poco/USBWSWebSocket.h

diff --git a/tools/usb/usbip/websocket/AUTHORS 
b/tools/usb/usbip/websocket/AUTHORS
new file mode 100644
index 000..be60f78
--- /dev/null
+++ b/tools/usb/usbip/websocket/AUTHORS
@@ -0,0 +1 @@
+Nobuo Iwata
diff --git a/tools/usb/usbip/websocket/COPYING 
b/tools/usb/usbip/websocket/COPYING
new file mode 100644
index 000..c5611e4
--- /dev/null
+++ b/tools/usb/usbip/websocket/COPYING
@@ -0,0 +1,340 @@
+   GNU GENERAL PUBLIC LICENSE
+  Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+   Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other

[PATCH 07/13] USB/IP: letting send and receive replaceable

2015-04-02 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for both a) 
request/response PDUs among utilities and b) user space URBs transmission.

usbip_sock_t is introduced instead of sockfd. it includes function pointers of 
send/receive/shutdown routines, an argument for the routines, and a sockfd. The 
argument is needed for the routines. The sockfd is needed to bind connection to 
USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 
 tools/usb/usbip/libsrc/usbip_common.h  | 14 
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +--
 tools/usb/usbip/src/usbip_attach.c | 30 
 tools/usb/usbip/src/usbip_connect.c| 30 
 tools/usb/usbip/src/usbip_disconnect.c | 26 +++---
 tools/usb/usbip/src/usbip_list.c   | 26 +++---
 tools/usb/usbip/src/usbip_network.c| 66 +++---
 tools/usb/usbip/src/usbip_network.h| 11 +++---
 tools/usb/usbip/src/usbipd.c   |  8 +++--
 tools/usb/usbip/src/usbipd_app.c   | 36 +--
 tools/usb/usbip/src/usbipd_dev.c   | 40 ++---
 13 files changed, 199 insertions(+), 130 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include libudev.h
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, %s / %s / %s (%02x/%02x/%02x), c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock-fd = fd;
+   sock-arg = arg;
+   sock-send = send;
+   sock-recv = recv;
+   sock-shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 183be7c..8c4e691 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (ux-sock-recv) {
+   received = ux-sock-recv(ux-sock-arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux-sock-fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, ux sending);
-   sent = send(ux-sockfd, buf, reads, 0);
+   if (ux-sock-send) {
+   sent = ux-sock-send(ux-sock-arg, buf, reads);
+   } else {
+   sent = send(ux-sock-fd, buf, reads, 0);
+   }
if (sent  0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg(end of ux-tx for sock:%p, ux-kaddr.sock);
-   shutdown(ux-sockfd, SHUT_RDWR);
+   if (ux-sock-shutdown) {
+   ux-sock-shutdown(ux-sock-arg);
+   } else {
+   shutdown(ux-sock-fd, SHUT_RDWR);
+   }
return 0;
 }
 
@@ -120,7

[PATCH 12/13] USB/IP: added const qualifier to arguments of some functions

2015-04-02 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library interfaces to 
make acceptable std::string.c_str(). Essentially, these qualifiers are better 
to be used even if not to use C++. Although, I just added to functions related 
to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/list.h  | 24 +---
 tools/usb/usbip/libsrc/usbip_common.c  | 16 
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 +++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 +---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 +
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 23 files changed, 86 insertions(+), 69 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next-prev = new;
-   new-next = next;
-   new-prev = prev;
-   prev-next = new;
+   next-prev = neo;
+   neo-next = next;
+   neo-prev = prev;
+   prev-next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head-next);
+   __list_add(neo, head, head-next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry-prev, entry-next);
-   entry-next = LIST_POISON1;
-   entry-prev = LIST_POISON2;
+   entry-next = (struct list_head *)LIST_POISON1;
+   entry-prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)-next, n = pos-next; pos != (head); \
pos = n, n = pos-next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index 54efa10..bf577b7 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -260,29 +260,29 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
snprintf(buff, size, %s : %s (%04x:%04x), vend, prod, vendor, 
product);
 }
 
-void usbip_names_get_class(char *buff, size_t size, uint8_t class,
+void usbip_names_get_class(char *buff, size_t size, uint8_t clazz,
   uint8_t subclass, uint8_t protocol)
 {
const char *c, *s, *p;
 
-   if (class == 0  subclass == 0  protocol == 0) {
-   snprintf(buff, size, (Defined at Interface level) 
(%02x/%02x/%02x), class, subclass, protocol);
+   if (clazz == 0  subclass == 0  protocol == 0) {
+   snprintf

[PATCH 02/13] USB/IP: readme and manuals about exporting devices

2015-04-02 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client', is changed also.

For existing attach command, the daemon runs device side machine and attach 
command is executed in application side machine. Then 'server' is used for 
device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine and 
connect command is executed in device side machine. Now, 'server' and 'client' 
run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are used 
instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 
 tools/usb/usbip/doc/usbip.8  | 74 +-
 tools/usb/usbip/doc/usbipa.8 | 77 
 tools/usb/usbip/doc/usbipd.8 | 29 -
 5 files changed, 200 insertions(+), 52 deletions(-)
 create mode 100644 tools/usb/usbip/doc/usbipa.8

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney m...@muteddisk.com
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney m...@muteddisk.com
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device with busid is now exportable to other hosts!
+   - Use `usbip unbind --busid busid` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote host
+app:# usbip list --remote host
- List exported USB devices on the host.
 
-client:# usbip attach --remote host --busid 1-2
+app:# usbip attach --remote host --busid busid
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port port
+app:# usbip detach --port port
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote host --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device of busid is connected to remote host!
+
+dev:# usbip disconnect --remote host --busid busid
+   - The USB device with busid is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0

[PATCH 05/13] USB/IP: tools for userspace URBs transmission

2015-04-02 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation to 
transfer URBs in user space, after completion of the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like SSL, WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. Then, 
the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to read/wite 
URBs from usbip_ux.ko and transfer URBs in userspace. When usbip_ux.ko is 
installed, /dev/usbip-ux will be found, then the threads will be started. 
Otherwise, threads will not be started and original kernel space transmission 
is valid.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 +++-
 tools/usb/usbip/src/usbip_connect.c|  18 ++-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 +++-
 10 files changed, 370 insertions(+), 23 deletions(-)
 create mode 100644 tools/usb/usbip/libsrc/usbip_ux.c
 create mode 100644 tools/usb/usbip/libsrc/usbip_ux.h

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..183be7c
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include sys/types.h
+#include sys/stat.h
+#include fcntl.h
+#include sys/ioctl.h
+#include sys/socket.h
+#include usbip_common.h
+#include usbip_ux.h
+
+#undef  PROGNAME
+#define PROGNAME libusbip
+
+#define DEVNAME /dev/ USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg(dump %s for %zd bytes, label, bufflen);
+   for(i=0;ibufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, %04zx  %02x, i, *(buff+i)  0xff);
+   } else if (j == 8) {
+   sprintf(bb,   %02x, *(buff+i)  0xff);
+   } else {
+   sprintf(bb,  %02x, *(buff+i)  0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg(%s, b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg(connection closed on sock:%p, ux-kaddr.sock);
+   break;
+   } else if (received  0) {
+   dbg(receive error on sock:%p, ux-kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, ux received);
+   written = 0;
+   while(written  received) {
+   ret = write(ux-devfd, buf+written, received-written);
+   if (ret  0) {
+   dbg(write error for sock:%p, ux-kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg(end of ux-rx for sock:%p, ux-kaddr.sock);
+   ioctl(ux-devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux-devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg(end of read on sock:%p continue., ux-kaddr.sock);
+#endif
+   sched_yield

[PATCH 08/13] USB/IP: USB/IP with SSL

2015-04-02 Thread Nobuo Iwata
This patch allows to transfer both request/response between utilities and 
userspace URBs transfer over SSL. OpenSSL is used for the implementation.

Options --with-ssl=yes at ./configure and --ssl to command and daemon enable 
SSL. Default of --with-ssl is depends on existence of OpenSSL package. If it 
exists, default is --with-ssl=yes otherwise no.

If user space URBs transfer is not activated, ie. usbip_ux.ko is not installed, 
the utilites will fail in socket preparation.

To compile and execute the utilities, OpenSSL headers and libraries 
(openssl-devel or openssl-dev package) are needed.

Supported certificate files format is PEM.

Related options and their default are as following.

  --ssl
Activate SSL. Default is non SSL.

  --tcp-port
TCP port number. Default is 3240 for non SSL or 443 for SSL.

  --key
Private key file. Default is ./cert/server.key (for usbipd and usbipa)
or ./cert/client.key (for usbip).

  --cert
Certificate file. Default is ./cert/server.crt (for usbipd and usbipa)
or ./cert/client.crt (for usbip).

  --root
Root CA's certification. Default is none.

  --verification
Verification mode from none | relaxed | strict | once
(for usbipd and usbipa) or none | relaxed   (for usbip).
Default is none.


Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/configure.ac|  34 ++
 tools/usb/usbip/src/Makefile.am |  10 +-
 tools/usb/usbip/src/usbip.c |  55 -
 tools/usb/usbip/src/usbip_network.c |  47 
 tools/usb/usbip/src/usbip_network.h |   3 +
 tools/usb/usbip/src/usbip_ssl.c | 232 
 tools/usb/usbip/src/usbip_ssl.h |  35 ++
 tools/usb/usbip/src/usbipd.c| 108 -
 8 files changed, 516 insertions(+), 8 deletions(-)
 create mode 100644 tools/usb/usbip/src/usbip_ssl.c
 create mode 100644 tools/usb/usbip/src/usbip_ssl.h

diff --git a/tools/usb/usbip/configure.ac b/tools/usb/usbip/configure.ac
index 607d05c..3b8277e 100644
--- a/tools/usb/usbip/configure.ac
+++ b/tools/usb/usbip/configure.ac
@@ -90,6 +90,40 @@ AC_ARG_WITH([usbids-dir],
[USBIDS_DIR=$withval], [USBIDS_DIR=/usr/share/hwdata/])
 AC_SUBST([USBIDS_DIR])
 
+# Checks for SSL.
+AC_MSG_CHECKING([whether to use SSL])
+AC_ARG_WITH([ssl],
+   [AS_HELP_STRING([--with-ssl],
+   [use SSL (OpenSSL)])],
+   dnl [ACTION-IF-GIVEN]
+   [if test $withval = yes; then
+AC_MSG_RESULT([yes])
+AC_MSG_CHECKING([for SSL_METHOD])
+saved_LIBS=$LIBS
+LIBS=-lssl -lcrypt $saved_LIBS
+AC_TRY_LINK(
+  [#include openssl/ssl.h],
+  [SSL_METHOD *m = SSLv23_method();],
+  [AC_MSG_RESULT([yes]);
+   AC_DEFINE([USE_SSL], [1], [use ssl])
+   AC_SUBST([SSL_LIBS], [-lssl -lcrypto])],
+  [AC_MSG_RESULT([not found]); exit 1])
+else
+AC_MSG_RESULT([no]);
+fi],
+   dnl [ACTION-IF-NOT-GIVEN]
+   [AC_MSG_RESULT([(default)])
+AC_MSG_CHECKING([for SSL_METHOD])
+saved_LIBS=$LIBS
+LIBS=-lssl -lcrypt $saved_LIBS
+AC_TRY_LINK(
+  [#include openssl/ssl.h],
+  [SSL_METHOD *m = SSLv23_method();],
+  [AC_MSG_RESULT([yes]);
+   AC_DEFINE([USE_SSL], [1], [use ssl])
+   AC_SUBST([SSL_LIBS], [-lssl -lcrypto])],
+  [AC_MSG_RESULT([no]); LIBS=$saved_LIBS])])
+
 # use _FORTIFY_SOURCE
 AC_MSG_CHECKING([whether to use fortify])
 AC_ARG_WITH([fortify],
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..cbe55ec 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,18 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
 AM_CFLAGS   = @EXTRA_CFLAGS@
+AM_LDFLAGS  = @SSL_LIBS@
 LDADD   = $(top_builddir)/libsrc/libusbip.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_ssl.c \
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
 
-usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c \
+usbip_network.c usbip_ssl.c
 
-usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c \
+usbip_network.c usbip_ssl.c
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index

[PATCH 04/13] USB/IP: kernel module for userspace URBs transmission

2015-04-02 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation to 
transfer URBs in user space, after the preparation, URBs are transmitted in 
kernel space.

To make easy to introduce application network protocols like SSL, WebSocket and 
so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is valid. 
usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export+---+
space   |uspipd,|-|usbip, |
|usbip  |   |usbipa |
+---+---+   +---+---+
|   |
2)Set sockfd|   |2)Set sockfd
  thru sysfs|   |  thru sysfs
V   V
Kernel  +---+4)URBs +---+
space   |usbip  |-|vhci   |
|host.ko|   |hcd.ko |
+---+   +---+
3)link to kernel trx_ops3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export+---+
space   |uspipd,|-|usbip, |
+---|usbip  |-|usbipa |---+
2)Set sockfd|+--+---+6)URBs +---+--+|2)Set sockfd
  thru ioctl||  ^  ^   ||  thru ioctl
  (right)   ||  |5)read/write  5)read/write|   ||  (left)
3)Set sockfd||  +---+   +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux |  +---+|  thru sysfs
  (left)VV  V   V  VV  (right)
Kernel  +---+   +---+   +---+   +---+
space   |usbip  |-|usbip  |   |usbip  |-|vhci   |
|host.ko|5)send |ux.ko  |   |ux.ko  |5)send |hcd.ko |
+---+  recv +---+   +---+  recv +---+
4)link to user trx_ops  4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 -
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 602 +++
 drivers/usb/usbip/usbip_ux.h |  82 ++
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 +--
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 +++
 14 files changed, 869 insertions(+), 57 deletions(-)
 create mode 100644 drivers/usb/usbip/usbip_ux.c
 create mode 100644 drivers/usb/usbip/usbip_ux.h
 create mode 100644 include/uapi/linux/usbip_ux.h

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate USB/IP userspace URB transmission
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.
+
 config USBIP_VHCI_HCD
tristate VHCI hcd
depends on USBIP_CORE
diff --git a/drivers/usb/usbip/Makefile b/drivers/usb/usbip/Makefile
index 9ecd615..fd5c1a5 100644
--- a/drivers/usb/usbip/Makefile
+++ b/drivers/usb/usbip/Makefile
@@ -3,6 +3,9 @@ ccflags-$(CONFIG_USBIP_DEBUG) := -DDEBUG
 obj-$(CONFIG_USBIP_CORE) += usbip-core.o
 usbip-core-y := usbip_common.o usbip_event.o
 
+obj-$(CONFIG_USBIP_UX) += usbip-ux.o
+usbip-ux-y := usbip_ux.o
+
 obj-$(CONFIG_USBIP_VHCI_HCD) += vhci-hcd.o
 vhci-hcd-y := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
 
diff --git

[PATCH 01/13] USB/IP: exporting devices

2015-04-02 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side machine 
by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D // Start usbip daemon.
dev:# usbip list -l // List local USB devices and their busid.
dev:# usbip bind --busid busid // Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote host // List importable USB devices from the host.
app:# usbip attach --remote host --busid busid // Import a device

By attach command, connection will be established from application-side to 
device-side.

This patch introduces a function to export devices form device-side machine to 
application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D // Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l // List local USB devices and their busid.
dev:# usbip connect --remote host --busid busid // Export a device to 
host.

For this, export function, connection is established from device-side machine 
to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of firewall.

Probably, the export function was planned because the packets have been defined 
in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to uinit32_t) 
and use them.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 124 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +-
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 +
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +++
 tools/usb/usbip/src/usbip_list.c   |  22 +--
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |   5 +
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 232 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 19 files changed, 1132 insertions(+), 297 deletions(-)
 create mode 100644 tools/usb/usbip/src/usbip_connect.c
 create mode 100644 tools/usb/usbip/src/usbip_disconnect.c
 create mode 100644 tools/usb/usbip/src/usbipd_app.c
 create mode 100644 tools/usb/usbip/src/usbipd_dev.c

diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, host_driver-edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev-udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..797949d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2005-2007 Takahiro Hirofuchi
  */
 
 #include usbip_common.h
@@ -7,6 +8,8 @@
 #include limits.h
 #include

[PATCH 09/13] USB/IP: readme and manuals about USB/IP with SSL

2015-04-02 Thread Nobuo Iwata
Addition to README and manuals regarding USB/IP with SSL.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/README   | 13 -
 tools/usb/usbip/doc/usbip.8  | 38 +++---
 tools/usb/usbip/doc/usbipa.8 | 34 ++
 tools/usb/usbip/doc/usbipd.8 | 34 ++
 4 files changed, 107 insertions(+), 12 deletions(-)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 6b61da5..126b1f0 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -24,15 +24,21 @@
 - hwdata
 Contains USB device identification data.
 
+- openssl
+OpenSSL library
 
 [Install]
 0. Generate configuration scripts.
$ ./autogen.sh
 
 1. Compile  install the userspace utilities.
-   $ ./configure [--with-tcp-wrappers=no] [--with-usbids-dir=dir]
+   $ ./configure [--with-tcp-wrappers=yes|no] [--with-usbids-dir=dir] \
+  [--with-ssl=yes|no]
$ make install
 
+   Defaults of --with-tcp-wrappers and --with-ssl depend library.
+   It assumed as 'yes' when depending library is available otherwise 'no'. 
+
 2. Compile  install USB/IP drivers.
 
 
@@ -120,6 +126,11 @@ In usage shown above, once USB devices are imported or 
exported, USP/IP drivers
- Stops transission, quits connect command and disconnect device.
 
 
+[Secure Transmission]
+
+When --with-ssl=yes is specified for ./configure and --ssl option is specified 
for usbipa, usbipd and usbip, SSL is applied for userspace transmission. PEM 
format private key and certificate file are used. Check help for detail of 
options and default.
+
+
 [Example]
 ---
DEVICE SIDE
diff --git a/tools/usb/usbip/doc/usbip.8 b/tools/usb/usbip/doc/usbip.8
index dfaabba..9ae4867 100644
--- a/tools/usb/usbip/doc/usbip.8
+++ b/tools/usb/usbip/doc/usbip.8
@@ -15,23 +15,55 @@ lists devices importable from a remote computer, attaches a 
remote device and de
 
 .SH OPTIONS
 .HP
-\fB\-\-debug\fR
+\fB\-d\fR, \fB\-\-debug\fR
 .IP
 Print debugging information.
 .PP
 
 .HP
-\fB\-\-log\fR
+\fB\-l\fR, \fB\-\-log\fR
 .IP
 Log to syslog.
 .PP
 
 .HP
-\fB\-\-tcp-port PORT\fR
+\fB\-t\fR, \fB\-\-tcp-port PORT\fR
 .IP
 TCP port number used by remote usbip daemon. Default is 3240.
 .PP
 
+Following options are available if SSL has been enabled on build. It can be 
checked by help.
+
+.HP
+\fB\-s\fR, \fB\-\-ssl\fR
+.IP
+Use SSL.
+.PP
+
+.HP
+\fB\-kKEY-FILE\fR, \fB\-\-key KEY-FILE\fR
+.IP
+Private key file. Default is cert/client.key.
+.PP
+
+.HP
+\fB\-cCERT-FILE\fR, \fB\-\-cert CERT-FILE\fR
+.IP
+Certificate file. Default is cert/client.crt.
+.PP
+
+.HP
+\fB\-rROOT-CERT-FILE\fR, \fB\-\-root ROOT-CERT-FILE\fR
+.IP
+Trusted CA file. Default is none.
+.PP
+
+.HP
+\fB\-VVERIFICATION-MODE\fR, \fB\-\-verification VERIFICATION-MODE\fR
+.IP
+Verification mode: none(default) or once.
+.PP
+
 .SH COMMANDS
 .HP
 \fBversion\fR
diff --git a/tools/usb/usbip/doc/usbipa.8 b/tools/usb/usbip/doc/usbipa.8
index 54fca78..76d60c1 100644
--- a/tools/usb/usbip/doc/usbipa.8
+++ b/tools/usb/usbip/doc/usbipa.8
@@ -58,11 +58,37 @@ Print the program help message and exit.
 Show version.
 .PP
 
-.SH LIMITATIONS
+Following options are available if SSL has been enabled on build. It can be 
checked by help.
 
-.B usbipa
-offers no authentication or authorization for USB/IP. Any
-USB/IP client can connect and use exported devices.
+.HP
+\fB\-s\fR, \fB\-\-ssl\fR
+.IP
+Use SSL.
+.PP
+
+.HP
+\fB\-kKEY-FILE\fR, \fB\-\-key KEY-FILE\fR
+.IP
+Private key file. Default is cert/server.key.
+.PP
+
+.HP
+\fB\-cCERT-FILE\fR, \fB\-\-cert CERT-FILE\fR
+.IP
+Certificate file. Default is cert/server.crt.
+.PP
+
+.HP
+\fB\-rROOT-CERT-FILE\fR, \fB\-\-root ROOT-CERT-FILE\fR
+.IP
+Trusted CA file. Default is none.
+.PP
+
+.HP
+\fB\-VVERIFICATION-MODE\fR, \fB\-\-verification VERIFICATION-MODE\fR
+.IP
+Verification mode: none(default), relaxed, strict or once.
+.PP
 
 .SH EXAMPLES
 
diff --git a/tools/usb/usbip/doc/usbipd.8 b/tools/usb/usbip/doc/usbipd.8
index c3fa023..3bc40c7 100644
--- a/tools/usb/usbip/doc/usbipd.8
+++ b/tools/usb/usbip/doc/usbipd.8
@@ -60,11 +60,37 @@ Print the program help message and exit.
 Show version.
 .PP
 
-.SH LIMITATIONS
+Following options are available if SSL has been enabled on build. It can be 
checked by help.
 
-.B usbipd
-offers no authentication or authorization for USB/IP. Any
-USB/IP client can connect and use exported devices.
+.HP
+\fB\-s\fR, \fB\-\-ssl\fR
+.IP
+Use SSL.
+.PP
+
+.HP
+\fB\-kKEY-FILE\fR, \fB\-\-key KEY-FILE\fR
+.IP
+Private key file. Default is cert/server.key.
+.PP
+
+.HP
+\fB\-cCERT-FILE\fR, \fB\-\-cert CERT-FILE\fR
+.IP
+Certificate file. Default is cert/server.crt.
+.PP
+
+.HP
+\fB\-rROOT-CERT-FILE\fR, \fB\-\-root ROOT-CERT-FILE\fR
+.IP
+Trusted CA file. Default is none.
+.PP
+
+.HP
+\fB\-VVERIFICATION-MODE\fR, \fB\-\-verification VERIFICATION-MODE\fR
+.IP

[PATCH 10/13] USB/IP: letting connection establishment replaceable

2015-04-02 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch allows to 
substitute connection establishment and termination. In combination with 
previous patch, both connection and transmission can be replaced.

usbip_connection_operations_t includes open and close operation. Open method 
returns usbip_sock_t which includes send, receive and close method. Then, 
transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock-shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index e743d82..cc83455a 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -251,6 +251,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command(cmds[i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index fe037de..e1c298e 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err0;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err2:
usbip_ux_cleanup(ux);
 err1:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err0:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 4511990..ae8104b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err0;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err1;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err3:
usbip_ux_cleanup(ux);
 err2:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err1:
usbip_unbind_device(busid);
 err0:
diff --git a/tools/usb/usbip/src/usbip_disconnect.c 
b/tools/usb/usbip/src/usbip_disconnect.c
index 8da1717..1bda962 100644
--- a/tools/usb/usbip/src

[PATCH 00/13] USB/IP: features to USB over WebSocket

2015-04-02 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 
It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to a Linux 
based cloud service.
In this senario, it's needed to establish connection from a device inside of 
firewall to a service outside. Exporting is suit for the senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but difficult 
to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and kernel_recvmsg() 
with user spcace interface. When USB/IP utilities find usbip_ux.ko, they start 
threads to read/write PDUs from/to usbip_ux.ko and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are replaceable.

4) SSL

OpenSSL is applied when configured --with-ssl=yes and --ssl option is specified.

5) a WebSocket implementation

It's made with Poco C++.

---
I published scripts I used for the development.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-make-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

Thank you,

Nobuo Iwata nobuo.iw...@fujixerox.co.jp
//

*** BLURB HERE ***

Nobuo Iwata (13):
  USB/IP: exporting devices
  USB/IP: readme and manuals about exporting devices
  USB/IP: safe completion against usb_kill_urb()
  USB/IP: kernel module for userspace URBs transmission
  USB/IP: tools for userspace URBs transmission
  USB/IP: readme about user space URBs transmission
  USB/IP: letting send and receive replaceable
  USB/IP: USB/IP with SSL
  USB/IP: readme and manuals about USB/IP with SSL
  USB/IP: letting connection establishment replaceable
  USB/IP: deriving functions as libraries
  USB/IP: added const qualifier to arguments of some functions
  USB/IP: USB over WebSocket

 drivers/usb/usbip/Kconfig  |  10 +
 drivers/usb/usbip/Makefile |   3 +
 drivers/usb/usbip/stub_dev.c   |  16 +-
 drivers/usb/usbip/stub_rx.c|   3 +-
 drivers/usb/usbip/stub_tx.c|   9 +-
 drivers/usb/usbip/usbip_common.c   |  79 ++-
 drivers/usb/usbip/usbip_common.h   |  29 +-
 drivers/usb/usbip/usbip_ux.c   | 602 +
 drivers/usb/usbip/usbip_ux.h   |  82 +++
 drivers/usb/usbip/vhci_hcd.c   |   9 +-
 drivers/usb/usbip/vhci_rx.c|   3 +-
 drivers/usb/usbip/vhci_sysfs.c |  40 +-
 drivers/usb/usbip/vhci_tx.c|   6 +-
 include/uapi/linux/usbip_ux.h  |  39 ++
 tools/usb/usbip/Makefile.am|   2 +-
 tools/usb/usbip/README | 105 +++-
 tools/usb/usbip/configure.ac   |  34 ++
 tools/usb/usbip/doc/usbip.8| 112 +++-
 tools/usb/usbip/doc/usbipa.8   | 103 
 tools/usb/usbip/doc/usbipd.8   |  63 ++-
 tools/usb/usbip/libsrc/Makefile.am |  11 +-
 tools/usb/usbip/libsrc/list.h  |  24 +-
 tools/usb/usbip/libsrc/usbip_common.c  |  38 +-
 tools/usb/usbip/libsrc/usbip_common.h  |  27 +-
 tools/usb/usbip/libsrc/usbip_host_driver.c |  24 +-
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 256 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 141 -
 tools/usb/usbip/libsrc/vhci_driver.h   |  10 +-
 tools/usb/usbip/src/Makefile.am|  37 +-
 tools/usb/usbip/src/usbip.c|  73 ++-
 tools/usb/usbip/src/usbip.h|  20 +-
 tools/usb/usbip/src/usbip_attach.c | 111 ++--
 tools/usb/usbip/src/usbip_bind.c   |  17 +-
 tools/usb/usbip/src/usbip_connect.c| 236 
 tools/usb/usbip/src/usbip_detach.c |  26 +-
 tools/usb/usbip/src/usbip_disconnect.c | 212 
 tools/usb/usbip/src/usbip_list.c   |  64 ++-
 tools/usb/usbip/src/usbip_netconn.c| 133 +
 tools/usb/usbip/src/usbip_network.c| 112 ++--
 tools/usb/usbip/src/usbip_network.h|  18 +-
 tools/usb/usbip/src/usbip_port.c   |  17 +-
 tools/usb/usbip/src/usbip_ssl.c| 232 
 tools/usb/usbip/src/usbip_ssl.h|  35 ++
 tools/usb/usbip/src/usbip_unbind.c |  13 +-
 tools/usb/usbip/src/usbipd.c

[PATCH 11/13] USB/IP: deriving functions as libraries

2015-04-02 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol implementations, this 
patch derives libraries by exposing some functions of utilities and removing 
some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to remove 
option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |  10 +--
 tools/usb/usbip/src/Makefile.am|  26 ++-
 tools/usb/usbip/src/usbip.h|   7 ++
 tools/usb/usbip/src/usbip_attach.c |  14 +++-
 tools/usb/usbip/src/usbip_bind.c   |   6 ++
 tools/usb/usbip/src/usbip_connect.c|  12 ++-
 tools/usb/usbip/src/usbip_detach.c |  13 +++-
 tools/usb/usbip/src/usbip_disconnect.c |  12 ++-
 tools/usb/usbip/src/usbip_list.c   |  30 +---
 tools/usb/usbip/src/usbip_netconn.c| 133 +
 tools/usb/usbip/src/usbip_network.c| 102 -
 tools/usb/usbip/src/usbip_port.c   |  12 ++-
 tools/usb/usbip/src/usbip_unbind.c |   6 ++
 tools/usb/usbip/src/usbipd.c   |  13 +---
 tools/usb/usbip/src/usbipd.h   |  38 ++
 tools/usb/usbip/src/usbipd_app.c   |   1 +
 tools/usb/usbip/src/usbipd_dev.c   |   1 +
 17 files changed, 287 insertions(+), 149 deletions(-)
 create mode 100644 tools/usb/usbip/src/usbip_netconn.c
 create mode 100644 tools/usb/usbip/src/usbipd.h

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index cbe55ec..1b77222 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,18 +1,38 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-AM_LDFLAGS  = @SSL_LIBS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
+
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
+
 
 usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
-usbip_network.c usbip_ssl.c \
+usbip_network.c usbip_netconn.c usbip_ssl.c \
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
+usbip_LDFLAGS := @SSL_LIBS@
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c \
 usbip_network.c usbip_ssl.c
+usbipd_CFLAGS := $(AM_CFLAGS)
+usbipd_LDFLAGS := @SSL_LIBS@
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c \
 usbip_network.c usbip_ssl.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+usbipa_LDFLAGS := @SSL_LIBS@
+
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := -DAS_LIBRARY
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc

[PATCH 06/13] USB/IP: readme about user space URBs transmission

2015-04-02 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote host --busid busid
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote host --busid busid
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 03/13] USB/IP: safe completion against usb_kill_urb()

2015-04-02 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c requests to 
kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs to have 
been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared priv 
lists.
To know the killed URBs in stub_complete(), sdev-ud.tcp_socket which cleared 
before stub_device_cleanup_urbs() is checked.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(sdev-priv_lock, flags);
-   if (priv-unlinking) {
+   if (sdev-ud.tcp_socket == NULL) {
+   dev_info(urb-dev-dev, discard a urb for closed connection);
+   } else if (priv-unlinking) {
stub_enqueue_ret_unlink(sdev, priv-seqnum, urb-status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 10/11] usbip: added const qualifier to arguments of some functions

2015-04-14 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library 
interfaces to make acceptable std::string.c_str(). Essentially, these 
qualifiers are better to be used even if not to use C++. Although, I 
just added to functions related to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/list.h  | 24 --
 tools/usb/usbip/libsrc/usbip_common.c  | 16 +++
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 +
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 ++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 ---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  7 +++
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 +++
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 23 files changed, 86 insertions(+), 70 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next-prev = new;
-   new-next = next;
-   new-prev = prev;
-   prev-next = new;
+   next-prev = neo;
+   neo-next = next;
+   neo-prev = prev;
+   prev-next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head-next);
+   __list_add(neo, head, head-next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry-prev, entry-next);
-   entry-next = LIST_POISON1;
-   entry-prev = LIST_POISON2;
+   entry-next = (struct list_head *)LIST_POISON1;
+   entry-prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)-next, n = pos-next; pos != (head); \
pos = n, n = pos-next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index 54efa10..bf577b7 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -260,29 +260,29 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
snprintf(buff, size, %s : %s (%04x:%04x), vend, prod, vendor, 
product);
 }
 
-void usbip_names_get_class(char *buff, size_t size, uint8_t class,
+void usbip_names_get_class(char *buff, size_t size, uint8_t clazz,
   uint8_t subclass, uint8_t protocol)
 {
const char *c, *s, *p;
 
-   if (class == 0  subclass == 0  protocol == 0) {
-   snprintf(buff, size, (Defined at Interface level) 
(%02x/%02x/%02x), class, subclass, protocol);
+   if (clazz == 0  subclass == 0  protocol == 0) {
+   snprintf(buff

[PATCH v2 11/11] usbip: USB over WebSocket

2015-04-14 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, 
port, bind, unbind, attach and detach). It uses --url option to specify 
remote address and port number. 

Implementation of this patch depends on Poco C++ 
(http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/websocket/AUTHORS |   1 +
 tools/usb/usbip/websocket/COPYING | 340 +++
 tools/usb/usbip/websocket/INSTALL | 237 ++
 tools/usb/usbip/websocket/Makefile.am |   3 +
 tools/usb/usbip/websocket/README  | 184 
 tools/usb/usbip/websocket/autogen.sh  |   9 +
 tools/usb/usbip/websocket/cleanup.sh  |  12 +
 tools/usb/usbip/websocket/configure.ac|  61 +++
 tools/usb/usbip/websocket/doc/usbws.8 | 192 
 tools/usb/usbip/websocket/doc/usbwsa.8| 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8| 109 +
 tools/usb/usbip/websocket/poco/Makefile.am|  18 +
 .../usb/usbip/websocket/poco/USBWSCommand.cpp | 410 ++
 tools/usb/usbip/websocket/poco/USBWSCommand.h |  99 +
 .../usb/usbip/websocket/poco/USBWSDaemon.cpp  | 228 ++
 tools/usb/usbip/websocket/poco/USBWSDaemon.h  |  80 
 .../websocket/poco/USBWSRequestHandler.cpp|  90 
 .../websocket/poco/USBWSRequestHandler.h  |  49 +++
 .../poco/USBWSRequestHandlerFactory.cpp   |  47 ++
 .../poco/USBWSRequestHandlerFactory.h |  48 ++
 tools/usb/usbip/websocket/poco/USBWSUtil.h|  52 +++
 .../usbip/websocket/poco/USBWSWebSocket.cpp   | 201 +
 .../usb/usbip/websocket/poco/USBWSWebSocket.h |  69 +++
 23 files changed, 2640 insertions(+)

diff --git a/tools/usb/usbip/websocket/AUTHORS 
b/tools/usb/usbip/websocket/AUTHORS
new file mode 100644
index 000..be60f78
--- /dev/null
+++ b/tools/usb/usbip/websocket/AUTHORS
@@ -0,0 +1 @@
+Nobuo Iwata
diff --git a/tools/usb/usbip/websocket/COPYING 
b/tools/usb/usbip/websocket/COPYING
new file mode 100644
index 000..c5611e4
--- /dev/null
+++ b/tools/usb/usbip/websocket/COPYING
@@ -0,0 +1,340 @@
+   GNU GENERAL PUBLIC LICENSE
+  Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+   Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients

[PATCH v2 09/11] usbip: deriving functions as libraries

2015-04-14 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |  10 +-
 tools/usb/usbip/src/Makefile.am|  19 +++-
 tools/usb/usbip/src/usbip.h|   7 ++
 tools/usb/usbip/src/usbip_attach.c |  14 ++-
 tools/usb/usbip/src/usbip_bind.c   |   6 ++
 tools/usb/usbip/src/usbip_connect.c|  12 ++-
 tools/usb/usbip/src/usbip_detach.c |  13 ++-
 tools/usb/usbip/src/usbip_disconnect.c |  12 ++-
 tools/usb/usbip/src/usbip_list.c   |  68 -
 tools/usb/usbip/src/usbip_netconn.c| 133 +
 tools/usb/usbip/src/usbip_network.c|  67 -
 tools/usb/usbip/src/usbip_port.c   |  12 ++-
 tools/usb/usbip/src/usbip_unbind.c |   6 ++
 tools/usb/usbip/src/usbipd.c   |  13 +--
 tools/usb/usbip/src/usbipd.h   |  38 +++
 tools/usb/usbip/src/usbipd_app.c   |   1 +
 tools/usb/usbip/src/usbipd_dev.c   |   1 +
 17 files changed, 306 insertions(+), 126 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..bb20cd1 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,29 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_netconn.c\
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int usbip_disconnect(int argc, char *argv[]);
 
+int usbip_attach_device(char *host, char *port, char *busid);
+int usbip_detach_port(char *port);
 int usbip_bind_device(char *busid);
 int usbip_unbind_device(char *busid);
+int

[PATCH v2 04/11] usbip: kernel module for userspace URBs transmission

2015-04-14 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket 
and so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space 
utilities. It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is 
valid. usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export   +---+
space   |uspipd,||usbip, |
|usbip  |  |usbipa |
+---+---+  +---+---+
|  |
2)Set sockfd|  |2)Set sockfd
  thru sysfs|  |  thru sysfs
V  V
Kernel  +---+4)URBs+---+
space   |usbip  ||vhci   |
|host.ko|  |hcd.ko |
+---+  +---+
3)link to kernel trx_ops   3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export   +---+
space   |uspipd,||usbip, |
+---|usbip  ||usbipa |---+
2)Set sockfd|+--+---+6)URBs+---+--+|2)Set sockfd
  thru ioctl||  ^ ^   ||  thru ioctl
  (right)   ||  |5)read/write 5)read/write|   ||  (left)
3)Set sockfd||  +---+  +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux|  +---+|  thru sysfs
  (left)VV  V  V  VV  (right)
Kernel  +---+   +---+  +---+   +---+
space   |usbip  |-|usbip  |  |usbip  |-|vhci   |
|host.ko|5)send |ux.ko  |  |ux.ko  |5)send |hcd.ko |
+---+  recv +---+  +---+  recv +---+
4)link to user trx_ops 4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

The reason why the userspace transmission oter than usbfs is needed.
a) Application(vhci_hcd)-side is needed
Usbfs provides functions to control device. So it can be applied to 
device(usbip_host)-side but not to application(vhci_hcd)-side.
b) Use existing kernel modules as-is
To implement same functionality in userspace with interface like usbfs, 
almost same code to kernel modules must be copied to userspcae. Also 
interfaces between kernel modules and utiities (sysfs) should be 
changed to new one. So utilities must be modified according to the new 
interface too. Modifications to existing code by this patch is small 
and usbip_ux.c handles major part of userspace transmission.

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 +++-
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 602 +++
 drivers/usb/usbip/usbip_ux.h |  82 +
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 +-
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 ++
 14 files changed, 869 insertions(+), 57 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate USB/IP userspace URB transmission
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.
+
 config USBIP_VHCI_HCD
tristate VHCI hcd
depends

[PATCH v2 03/11] usbip: safe completion against usb_kill_urb()

2015-04-14 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev-ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(sdev-priv_lock, flags);
-   if (priv-unlinking) {
+   if (sdev-ud.tcp_socket == NULL) {
+   dev_info(urb-dev-dev, discard a urb for closed connection);
+   } else if (priv-unlinking) {
stub_enqueue_ret_unlink(sdev, priv-seqnum, urb-status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 02/11] usbip: readme and manuals about exporting devices

2015-04-14 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' is changed also.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 ++--
 tools/usb/usbip/doc/usbip.8  | 74 ++
 tools/usb/usbip/doc/usbipa.8 | 77 
 tools/usb/usbip/doc/usbipd.8 | 29 +++---
 5 files changed, 200 insertions(+), 52 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney m...@muteddisk.com
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney m...@muteddisk.com
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device with busid is now exportable to other hosts!
+   - Use `usbip unbind --busid busid` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote host
+app:# usbip list --remote host
- List exported USB devices on the host.
 
-client:# usbip attach --remote host --busid 1-2
+app:# usbip attach --remote host --busid busid
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port port
+app:# usbip detach --port port
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote host --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device of busid is connected to remote host!
+
+dev:# usbip disconnect --remote host --busid busid
+   - The USB device with busid is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show the devices attached to this client:
+Show the devices

[PATCH v2 06/11] usbip: readme about user space URBs transmission

2015-04-14 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote host --busid busid
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote host --busid busid
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 07/11] usbip: letting send and receive replaceable

2015-04-14 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for 
both a) request/response PDUs among utilities and b) user space URBs 
transmission.

usbip_sock_t is introduced instead of sockfd. it includes function 
pointers of send/receive/shutdown routines, an argument for the 
routines, and a sockfd. The argument is needed for the routines. The 
sockfd is needed to bind connection to USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 14 ++
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 +++---
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +-
 tools/usb/usbip/src/usbip_attach.c | 30 ++--
 tools/usb/usbip/src/usbip_connect.c| 30 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 26 +-
 tools/usb/usbip/src/usbip_list.c   | 26 +-
 tools/usb/usbip/src/usbip_network.c| 66 ++
 tools/usb/usbip/src/usbip_network.h| 11 +++--
 tools/usb/usbip/src/usbipd.c   |  8 ++--
 tools/usb/usbip/src/usbipd_app.c   | 36 +++---
 tools/usb/usbip/src/usbipd_dev.c   | 40 
 13 files changed, 199 insertions(+), 130 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include libudev.h
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, %s / %s / %s (%02x/%02x/%02x), c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock-fd = fd;
+   sock-arg = arg;
+   sock-send = send;
+   sock-recv = recv;
+   sock-shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 183be7c..8c4e691 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (ux-sock-recv) {
+   received = ux-sock-recv(ux-sock-arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux-sock-fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, ux sending);
-   sent = send(ux-sockfd, buf, reads, 0);
+   if (ux-sock-send) {
+   sent = ux-sock-send(ux-sock-arg, buf, reads);
+   } else {
+   sent = send(ux-sock-fd, buf, reads, 0);
+   }
if (sent  0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg(end of ux-tx for sock:%p, ux-kaddr.sock);
-   shutdown(ux-sockfd, SHUT_RDWR);
+   if (ux-sock-shutdown) {
+   ux-sock-shutdown(ux-sock-arg);
+   } else {
+   shutdown(ux-sock-fd, SHUT_RDWR);
+   }
return 0;
 }
 
@@ -120,7 +132,7 @@ static void *usbip_ux_tx(void *arg

[PATCH v2 08/11] usbip: letting connection establishment replaceable

2015-04-14 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock-shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command(cmds[i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index fe037de..e1c298e 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err0;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err2:
usbip_ux_cleanup(ux);
 err1:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err0:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 4511990..ae8104b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err0;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err1;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err3:
usbip_ux_cleanup(ux);
 err2:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err1:
usbip_unbind_device(busid);
 err0:
diff --git a/tools/usb/usbip/src/usbip_disconnect.c 
b/tools/usb/usbip/src/usbip_disconnect.c
index 8da1717..1bda962 100644
--- a/tools/usb/usbip/src

[PATCH v2 05/11] usbip: tools for userspace URBs transmission

2015-04-14 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after completion of the preparation, 
URBs are transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
Then, the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to 
read/wite URBs from usbip_ux.ko and transfer URBs in userspace. When 
usbip_ux.ko is installed, /dev/usbip-ux will be found, then the threads 
will be started. Otherwise, threads will not be started and original 
kernel space transmission is valid.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +++
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 ++-
 tools/usb/usbip/src/usbip_connect.c|  18 +-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 ++-
 10 files changed, 370 insertions(+), 23 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..183be7c
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include sys/types.h
+#include sys/stat.h
+#include fcntl.h
+#include sys/ioctl.h
+#include sys/socket.h
+#include usbip_common.h
+#include usbip_ux.h
+
+#undef  PROGNAME
+#define PROGNAME libusbip
+
+#define DEVNAME /dev/ USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg(dump %s for %zd bytes, label, bufflen);
+   for(i=0;ibufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, %04zx  %02x, i, *(buff+i)  0xff);
+   } else if (j == 8) {
+   sprintf(bb,   %02x, *(buff+i)  0xff);
+   } else {
+   sprintf(bb,  %02x, *(buff+i)  0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg(%s, b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg(connection closed on sock:%p, ux-kaddr.sock);
+   break;
+   } else if (received  0) {
+   dbg(receive error on sock:%p, ux-kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, ux received);
+   written = 0;
+   while(written  received) {
+   ret = write(ux-devfd, buf+written, received-written);
+   if (ret  0) {
+   dbg(write error for sock:%p, ux-kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg(end of ux-rx for sock:%p, ux-kaddr.sock);
+   ioctl(ux-devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux-devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg(end of read on sock:%p continue., ux-kaddr.sock);
+#endif
+   sched_yield();
+   continue;
+   } else  if (reads  0) {
+   dbg(read error on sock

[PATCH v2 01/11] usbip: exporting devices

2015-04-14 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid busid
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote host
// List importable USB devices from the host.
app:# usbip attach --remote host --busid busid
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote host --busid busid
// Export a device to host.

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets have been 
defined in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 124 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |   5 +
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 232 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 19 files changed, 1132 insertions(+), 297 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, host_driver-edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev-udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..797949d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2005-2007 Takahiro Hirofuchi
  */
 
 #include usbip_common.h
@@ -7,6 +8,8 @@
 #include limits.h
 #include netdb.h
 #include libudev.h
+#include fcntl.h
+#include errno.h
 #include sysfs_utils.h
 
 #undef  PROGNAME
@@ -215,6 +218,23 @@ static int read_record(int rhport, char *host, unsigned 
long host_len,
return 0;
 }
 
+static int open_hc_device(int reopen

[PATCH v2 00/11] usbip: features to USB over WebSocket

2015-04-14 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 

0. Version info

V2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by usbws list 
-l because usbip_names_init() was not called in libusbip.la.

1. Why WebSocket?

It allows to use USB/IP in internet. WebSocket is widely used to 
encapsulate packets in HTTP and to carry them through firewall using 
HTTP port numbers.

Assumed use case is a system that service in internet serves 
distributes devices in home or office networks. Service may be called 
as cloud and devices as ubiquitous.

   Home/SOHO/IntranetInternet  
 ++++
 +--+   +--+ |Router, ||Internet|
+|device|---|Linux |-|proxy,  ||service |
|+--+   +--+ |firewall||on Linux|
+--+   controller++++
ex)
Device  Service 
 sensors ... environment analysis 
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

2. Features included

It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not 
been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to 
a Linux based cloud service.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but 
difficult to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and 
kernel_recvmsg() with user spcace interface. When USB/IP utilities find 
usbip_ux.ko, they start threads to read/write PDUs from/to usbip_ux.ko 
and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are 
replaceable.

4) a WebSocket implementation

It's made with Poco C++. DNS and proxy are supported.


I published scripts I used while developed the patches.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-ma
ke-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

Thank you,

Nobuo Iwata nobuo.iw...@fujixerox.co.jp
//

*** BLURB HERE ***

Nobuo Iwata (11):
  usbip: exporting devices
  usbip: readme and manuals about exporting devices
  usbip: safe completion against usb_kill_urb()
  usbip: kernel module for userspace URBs transmission
  usbip: tools for userspace URBs transmission
  usbip: readme about user space URBs transmission
  usbip: letting send and receive replaceable
  usbip: letting connection establishment replaceable
  usbip: deriving functions as libraries
  usbip: added const qualifier to arguments of some functions
  usbip: USB over WebSocket

 drivers/usb/usbip/Kconfig |  10 +
 drivers/usb/usbip/Makefile|   3 +
 drivers/usb/usbip/stub_dev.c  |  16 +-
 drivers/usb/usbip/stub_rx.c   |   3 +-
 drivers/usb/usbip/stub_tx.c   |   9 +-
 drivers/usb/usbip/usbip_common.c  |  79 ++-
 drivers/usb/usbip/usbip_common.h  |  29 +-
 drivers/usb/usbip/usbip_ux.c  | 602 ++
 drivers/usb/usbip/usbip_ux.h  |  82 +++
 drivers/usb/usbip/vhci_hcd.c  |   9 +-
 drivers/usb/usbip/vhci_rx.c   |   3 +-
 drivers/usb/usbip/vhci_sysfs.c|  40 +-
 drivers/usb/usbip/vhci_tx.c   |   6 +-
 include/uapi/linux/usbip_ux.h |  39 ++
 tools/usb/usbip/Makefile.am   |   2 +-
 tools/usb/usbip/README|  92 ++-
 tools/usb/usbip/doc/usbip.8   |  74 ++-
 tools/usb/usbip/doc/usbipa.8  |  77 +++
 tools/usb/usbip/doc/usbipd.8  |  29 +-
 tools/usb/usbip/libsrc/Makefile.am|  11 +-
 tools/usb/usbip/libsrc/list.h |  24 +-
 tools/usb/usbip/libsrc/usbip_common.c |  38 +-
 tools/usb/usbip/libsrc/usbip_common.h |  27 +-
 tools/usb/usbip/libsrc/usbip_host_driver.c|  24 +-
 tools/usb/usbip/libsrc/usbip_host_driver.h|   1 +
 tools/usb/usbip/libsrc/usbip_ux.c | 256 
 tools/usb/usbip/libsrc/usbip_ux.h |  30 +
 tools/usb/usbip/libsrc/vhci_driver.c  | 141 +++-
 tools/usb/usbip/libsrc/vhci_driver.h  |  10 +-
 tools/usb/usbip/src/Makefile.am   |  28 +-
 tools/usb/usbip/src/usbip.c   |  18 +-
 tools/usb/usbip/src/usbip.h   |  20 +-
 tools/usb

[PATCH v3 11/11] usbip: USB over WebSocket

2015-04-26 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, 
port, bind, unbind, attach and detach). It uses --url option to specify 
remote address and port number. 

Implementation of this patch depends on Poco C++ 
(http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/websocket/INSTALL | 237 ++
 tools/usb/usbip/websocket/Makefile.am |   3 +
 tools/usb/usbip/websocket/README  | 184 
 tools/usb/usbip/websocket/autogen.sh  |   9 +
 tools/usb/usbip/websocket/cleanup.sh  |  12 +
 tools/usb/usbip/websocket/configure.ac|  55 +++
 tools/usb/usbip/websocket/doc/usbws.8 | 192 
 tools/usb/usbip/websocket/doc/usbwsa.8| 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8| 109 +
 tools/usb/usbip/websocket/poco/Makefile.am|  18 +
 .../usb/usbip/websocket/poco/USBWSCommand.cpp | 410 ++
 tools/usb/usbip/websocket/poco/USBWSCommand.h |  99 +
 .../usb/usbip/websocket/poco/USBWSDaemon.cpp  | 228 ++
 tools/usb/usbip/websocket/poco/USBWSDaemon.h  |  80 
 .../websocket/poco/USBWSRequestHandler.cpp|  90 
 .../websocket/poco/USBWSRequestHandler.h  |  49 +++
 .../poco/USBWSRequestHandlerFactory.cpp   |  47 ++
 .../poco/USBWSRequestHandlerFactory.h |  48 ++
 tools/usb/usbip/websocket/poco/USBWSUtil.h|  52 +++
 .../usbip/websocket/poco/USBWSWebSocket.cpp   | 201 +
 .../usb/usbip/websocket/poco/USBWSWebSocket.h |  69 +++
 21 files changed, 2293 insertions(+)

diff --git a/tools/usb/usbip/websocket/INSTALL 
b/tools/usb/usbip/websocket/INSTALL
new file mode 100644
index 000..d3c5b40
--- /dev/null
+++ b/tools/usb/usbip/websocket/INSTALL
@@ -0,0 +1,237 @@
+Installation Instructions
+*
+
+Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+2006, 2007 Free Software Foundation, Inc.
+
+This file is free documentation; the Free Software Foundation gives
+unlimited permission to copy, distribute and modify it.
+
+Basic Installation
+==
+
+Briefly, the shell commands `./configure; make; make install' should
+configure, build, and install this package.  The following
+more-detailed instructions are generic; see the `README' file for
+instructions specific to this package.
+
+   The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation.  It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions.  Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, and a
+file `config.log' containing compiler output (useful mainly for
+debugging `configure').
+
+   It can also use an optional file (typically called `config.cache'
+and enabled with `--cache-file=config.cache' or simply `-C') that saves
+the results of its tests to speed up reconfiguring.  Caching is
+disabled by default to prevent problems with accidental use of stale
+cache files.
+
+   If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release.  If you are using the cache, and at
+some point `config.cache' contains results you don't want to keep, you
+may remove or edit it.
+
+   The file `configure.ac' (or `configure.in') is used to create
+`configure' by a program called `autoconf'.  You need `configure.ac' if
+you want to change it or regenerate `configure' using a newer version
+of `autoconf'.
+
+The simplest way to compile this package is:
+
+  1. `cd' to the directory containing the package's source code and type
+ `./configure' to configure the package for your system.
+
+ Running `configure' might take a while.  While running, it prints
+ some messages telling which features it is checking for.
+
+  2. Type `make' to compile the package.
+
+  3. Optionally, type `make check' to run any self-tests that come with
+ the package.
+
+  4. Type `make install' to install the programs and any data files and
+ documentation.
+
+  5. You can remove the program binaries and object files from the
+ source code directory by typing `make clean'.  To also

[PATCH v3 10/11] usbip: added const qualifier to arguments of some functions

2015-04-26 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library 
interfaces to make acceptable std::string.c_str(). Essentially, these 
qualifiers are better to be used even if not to use C++. Although, I 
just added to functions related to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/list.h  | 24 --
 tools/usb/usbip/libsrc/usbip_common.c  | 16 +++
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 +
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 ++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 ---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  7 +++
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 +++
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 23 files changed, 86 insertions(+), 70 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next-prev = new;
-   new-next = next;
-   new-prev = prev;
-   prev-next = new;
+   next-prev = neo;
+   neo-next = next;
+   neo-prev = prev;
+   prev-next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head-next);
+   __list_add(neo, head, head-next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry-prev, entry-next);
-   entry-next = LIST_POISON1;
-   entry-prev = LIST_POISON2;
+   entry-next = (struct list_head *)LIST_POISON1;
+   entry-prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)-next, n = pos-next; pos != (head); \
pos = n, n = pos-next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index 54efa10..bf577b7 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -260,29 +260,29 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
snprintf(buff, size, %s : %s (%04x:%04x), vend, prod, vendor, 
product);
 }
 
-void usbip_names_get_class(char *buff, size_t size, uint8_t class,
+void usbip_names_get_class(char *buff, size_t size, uint8_t clazz,
   uint8_t subclass, uint8_t protocol)
 {
const char *c, *s, *p;
 
-   if (class == 0  subclass == 0  protocol == 0) {
-   snprintf(buff, size, (Defined at Interface level) 
(%02x/%02x/%02x), class, subclass, protocol);
+   if (clazz == 0  subclass == 0  protocol == 0) {
+   snprintf(buff

[PATCH v3 07/11] usbip: letting send and receive replaceable

2015-04-26 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for 
both a) request/response PDUs among utilities and b) user space URBs 
transmission.

usbip_sock_t is introduced instead of sockfd. it includes function 
pointers of send/receive/shutdown routines, an argument for the 
routines, and a sockfd. The argument is needed for the routines. The 
sockfd is needed to bind connection to USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 14 ++
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 +++---
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +-
 tools/usb/usbip/src/usbip_attach.c | 30 ++--
 tools/usb/usbip/src/usbip_connect.c| 30 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 26 +-
 tools/usb/usbip/src/usbip_list.c   | 26 +-
 tools/usb/usbip/src/usbip_network.c| 66 ++
 tools/usb/usbip/src/usbip_network.h| 11 +++--
 tools/usb/usbip/src/usbipd.c   |  8 ++--
 tools/usb/usbip/src/usbipd_app.c   | 36 +++---
 tools/usb/usbip/src/usbipd_dev.c   | 40 
 13 files changed, 199 insertions(+), 130 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include libudev.h
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, %s / %s / %s (%02x/%02x/%02x), c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock-fd = fd;
+   sock-arg = arg;
+   sock-send = send;
+   sock-recv = recv;
+   sock-shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 3ac45a72..aa3d863 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (ux-sock-recv) {
+   received = ux-sock-recv(ux-sock-arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux-sock-fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, ux sending);
-   sent = send(ux-sockfd, buf, reads, 0);
+   if (ux-sock-send) {
+   sent = ux-sock-send(ux-sock-arg, buf, reads);
+   } else {
+   sent = send(ux-sock-fd, buf, reads, 0);
+   }
if (sent  0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg(end of ux-tx for sock:%p, ux-kaddr.sock);
-   shutdown(ux-sockfd, SHUT_RDWR);
+   if (ux-sock-shutdown) {
+   ux-sock-shutdown(ux-sock-arg);
+   } else {
+   shutdown(ux-sock-fd, SHUT_RDWR);
+   }
return 0;
 }
 
@@ -120,7 +132,7 @@ static void *usbip_ux_tx(void *arg

[PATCH v3 04/11] usbip: kernel module for userspace URBs transmission

2015-04-26 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket 
and so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space 
utilities. It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is 
valid. usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export   +---+
space   |uspipd,||usbip, |
|usbip  |  |usbipa |
+---+---+  +---+---+
|  |
2)Set sockfd|  |2)Set sockfd
  thru sysfs|  |  thru sysfs
V  V
Kernel  +---+4)URBs+---+
space   |usbip  ||vhci   |
|host.ko|  |hcd.ko |
+---+  +---+
3)link to kernel trx_ops   3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export   +---+
space   |uspipd,||usbip, |
+---|usbip  ||usbipa |---+
2)Set sockfd|+--+---+6)URBs+---+--+|2)Set sockfd
  thru ioctl||  ^ ^   ||  thru ioctl
  (right)   ||  |5)read/write 5)read/write|   ||  (left)
3)Set sockfd||  +---+  +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux|  +---+|  thru sysfs
  (left)VV  V  V  VV  (right)
Kernel  +---+   +---+  +---+   +---+
space   |usbip  |-|usbip  |  |usbip  |-|vhci   |
|host.ko|5)send |ux.ko  |  |ux.ko  |5)send |hcd.ko |
+---+  recv +---+  +---+  recv +---+
4)link to user trx_ops 4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

The reason why the userspace transmission oter than usbfs is needed.
a) Application(vhci_hcd)-side is needed
Usbfs provides functions to control device. So it can be applied to 
device(usbip_host)-side but not to application(vhci_hcd)-side.
b) Use existing kernel modules as-is
To implement same functionality in userspace with interface like usbfs, 
almost same code to kernel modules must be copied to userspcae. Also 
interfaces between kernel modules and utiities (sysfs) should be 
changed to new one. So utilities must be modified according to the new 
interface too. Modifications to existing code by this patch is small 
and usbip_ux.c handles major part of userspace transmission.

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 +++-
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 602 +++
 drivers/usb/usbip/usbip_ux.h |  82 +
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 +-
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 ++
 14 files changed, 869 insertions(+), 57 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate USB/IP userspace URB transmission
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.
+
 config USBIP_VHCI_HCD
tristate VHCI hcd
depends

[PATCH v3 05/11] usbip: tools for userspace URBs transmission

2015-04-26 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after completion of the preparation, 
URBs are transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
Then, the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to 
read/wite URBs from usbip_ux.ko and transfer URBs in userspace. When 
usbip_ux.ko is installed, /dev/usbip-ux will be found, then the threads 
will be started. Otherwise, threads will not be started and original 
kernel space transmission is valid.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +++
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 ++-
 tools/usb/usbip/src/usbip_connect.c|  18 +-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 ++-
 10 files changed, 370 insertions(+), 23 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..3ac45a72
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include sys/types.h
+#include sys/stat.h
+#include fcntl.h
+#include sys/ioctl.h
+#include sys/socket.h
+#include usbip_common.h
+#include usbip_ux.h
+
+#undef  PROGNAME
+#define PROGNAME libusbip
+
+#define DEVNAME /dev/ USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg(dump %s for %zd bytes, label, bufflen);
+   for(i=0;ibufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, %04zx  %02x, i, *(buff+i)  0xff);
+   } else if (j == 8) {
+   sprintf(bb,   %02x, *(buff+i)  0xff);
+   } else {
+   sprintf(bb,  %02x, *(buff+i)  0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg(%s, b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg(connection closed on sock:%p, ux-kaddr.sock);
+   break;
+   } else if (received  0) {
+   dbg(receive error on sock:%p, ux-kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, ux received);
+   written = 0;
+   while(written  received) {
+   ret = write(ux-devfd, buf+written, received-written);
+   if (ret  0) {
+   dbg(write error for sock:%p, ux-kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg(end of ux-rx for sock:%p, ux-kaddr.sock);
+   ioctl(ux-devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux-devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg(end of read on sock:%p continue., ux-kaddr.sock);
+#endif
+   sched_yield();
+   continue;
+   } else  if (reads  0) {
+   dbg(read error on sock

[PATCH v3 06/11] usbip: readme about user space URBs transmission

2015-04-26 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote host --busid busid
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote host --busid busid
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 01/11] usbip: exporting devices

2015-04-26 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid busid
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote host
// List importable USB devices from the host.
app:# usbip attach --remote host --busid busid
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote host --busid busid
// Export a device to host.

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets have been 
defined in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |   5 +
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 232 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 19 files changed, 1135 insertions(+), 297 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, host_driver-edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev-udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..5d46806 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2005-2007 Takahiro Hirofuchi
  */
 
 #include usbip_common.h
@@ -7,6 +8,8 @@
 #include limits.h
 #include netdb.h
 #include libudev.h
+#include fcntl.h
+#include errno.h
 #include sysfs_utils.h
 
 #undef  PROGNAME
@@ -215,6 +218,26 @@ static int read_record(int rhport, char *host, unsigned 
long host_len,
return 0;
 }
 
+#define OPEN_HC_MODE_FIRST 0
+#define

[PATCH v3 00/11] usbip: features to USB over WebSocket

2015-04-26 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 

0. Version info

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as linux/usbip_ux.h.
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Removed unnecessary COPYING and AUTHORS file from 
tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

1. Why WebSocket?

It allows to use USB/IP in internet. WebSocket is widely used to 
encapsulate packets in HTTP and to carry them through firewall using 
HTTP port numbers.

Assumed use case is a system that service in internet serves 
distributes devices in home or office networks. Service may be called 
as cloud and devices as ubiquitous.

   Home/SOHO/IntranetInternet  
 ++++
 +--+   +--+ |Router, ||Internet|
+|device|---|Linux |-|proxy,  ||service |
|+--+   +--+ |firewall||on Linux|
+--+   controller++++
ex)
Device  Service 
 sensors ... environment analysis 
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

2. Why exporting devices?

Connection from outside firewall is usually blocked.
So existing import request sent with attach command doesn't work.

# usbipd (blocked)|| - # usbip attach

Firewall opens some ports, usually HTTP(80) and HTTPS(443), from inside.
Then export request sent with new connect command works.

# usbip connect  - # usbipa
 (passed)

3. Features included

It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not 
been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to 
a Linux based cloud service.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but 
difficult to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and 
kernel_recvmsg() with user spcace interface. When USB/IP utilities find 
usbip_ux.ko, they start threads to read/write PDUs from/to usbip_ux.ko 
and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are 
replaceable.

4) a WebSocket implementation

It's made with Poco C++. DNS and proxy client are supported.


I published scripts I used while developed the patches.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-ma
ke-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

Thank you,

Nobuo Iwata nobuo.iw...@fujixerox.co.jp
//

*** BLURB HERE ***

Nobuo Iwata (11):
  usbip: exporting devices
  usbip: readme and manuals about exporting devices
  usbip: safe completion against usb_kill_urb()
  usbip: kernel module for userspace URBs transmission
  usbip: tools for userspace URBs transmission
  usbip: readme about user space URBs transmission
  usbip: letting send and receive replaceable
  usbip: letting connection establishment replaceable
  usbip: deriving functions as libraries
  usbip: added const qualifier to arguments of some functions
  usbip: USB over WebSocket

 drivers/usb/usbip/Kconfig |  10 +
 drivers/usb/usbip/Makefile|   3 +
 drivers/usb/usbip/stub_dev.c  |  16 +-
 drivers/usb/usbip/stub_rx.c   |   3 +-
 drivers/usb/usbip/stub_tx.c   |   9 +-
 drivers/usb/usbip/usbip_common.c  |  79 ++-
 drivers/usb/usbip/usbip_common.h  |  29 +-
 drivers/usb/usbip/usbip_ux.c  | 602 ++
 drivers/usb/usbip/usbip_ux.h  |  82 +++
 drivers/usb/usbip/vhci_hcd.c  |   9 +-
 drivers/usb/usbip/vhci_rx.c   |   3 +-
 drivers/usb/usbip/vhci_sysfs.c|  40 +-
 drivers/usb/usbip/vhci_tx.c   |   6 +-
 include/uapi/linux/usbip_ux.h |  39 ++
 tools/usb/usbip/Makefile.am   |   2 +-
 tools/usb/usbip/README|  92 ++-
 tools/usb/usbip/doc/usbip.8

[PATCH v3 02/11] usbip: readme and manuals about exporting devices

2015-04-26 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' is changed also.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 +---
 tools/usb/usbip/doc/usbip.8  | 82 +---
 tools/usb/usbip/doc/usbipa.8 | 77 ++
 tools/usb/usbip/doc/usbipd.8 | 29 +-
 tools/usb/usbip/libsrc/vhci_driver.c |  2 +-
 6 files changed, 205 insertions(+), 57 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney m...@muteddisk.com
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney m...@muteddisk.com
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device with busid is now exportable to other hosts!
+   - Use `usbip unbind --busid busid` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote host
+app:# usbip list --remote host
- List exported USB devices on the host.
 
-client:# usbip attach --remote host --busid 1-2
+app:# usbip attach --remote host --busid busid
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port port
+app:# usbip detach --port port
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote host --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device of busid is connected to remote host!
+
+dev:# usbip disconnect --remote host --busid busid
+   - The USB device with busid is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show

[PATCH v3 03/11] usbip: safe completion against usb_kill_urb()

2015-04-26 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev-ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

The critial condition will happen by unbind command before detach, 
broken connection in connect command and disconnect command. 

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(sdev-priv_lock, flags);
-   if (priv-unlinking) {
+   if (sdev-ud.tcp_socket == NULL) {
+   dev_info(urb-dev-dev, discard a urb for closed connection);
+   } else if (priv-unlinking) {
stub_enqueue_ret_unlink(sdev, priv-seqnum, urb-status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v3 09/11] usbip: deriving functions as libraries

2015-04-26 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |  10 +-
 tools/usb/usbip/src/Makefile.am|  22 +++-
 tools/usb/usbip/src/usbip.h|   7 ++
 tools/usb/usbip/src/usbip_attach.c |  14 ++-
 tools/usb/usbip/src/usbip_bind.c   |   6 ++
 tools/usb/usbip/src/usbip_connect.c|  12 ++-
 tools/usb/usbip/src/usbip_detach.c |  13 ++-
 tools/usb/usbip/src/usbip_disconnect.c |  12 ++-
 tools/usb/usbip/src/usbip_list.c   |  68 -
 tools/usb/usbip/src/usbip_netconn.c| 133 +
 tools/usb/usbip/src/usbip_network.c|  67 -
 tools/usb/usbip/src/usbip_port.c   |  12 ++-
 tools/usb/usbip/src/usbip_unbind.c |   6 ++
 tools/usb/usbip/src/usbipd.c   |  13 +--
 tools/usb/usbip/src/usbipd.h   |  38 +++
 tools/usb/usbip/src/usbipd_app.c   |   1 +
 tools/usb/usbip/src/usbipd_dev.c   |   1 +
 17 files changed, 309 insertions(+), 126 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..780bdb3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,32 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_netconn.c\
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+libusbipd_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+libusbipa_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int usbip_disconnect(int argc, char *argv[]);
 
+int

[PATCH v3 08/11] usbip: letting connection establishment replaceable

2015-04-26 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock-shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command(cmds[i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index ae0ca6e..c67e3d2 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err_out;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup(ux);
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_out:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 5f9505c..8dabd0b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err_out;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err_unbind_device;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup(ux);
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_unbind_device:
usbip_unbind_device(busid);
 err_out:
diff --git a/tools/usb/usbip/src/usbip_disconnect.c 
b/tools/usb/usbip/src

[PATCH v4 02/11] usbip: readme and manuals about exporting devices

2015-05-27 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' is changed also.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 +---
 tools/usb/usbip/doc/usbip.8  | 82 +---
 tools/usb/usbip/doc/usbipa.8 | 77 ++
 tools/usb/usbip/doc/usbipd.8 | 29 +-
 tools/usb/usbip/libsrc/vhci_driver.c |  2 +-
 6 files changed, 205 insertions(+), 57 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney m...@muteddisk.com
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney m...@muteddisk.com
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device with busid is now exportable to other hosts!
+   - Use `usbip unbind --busid busid` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote host
+app:# usbip list --remote host
- List exported USB devices on the host.
 
-client:# usbip attach --remote host --busid 1-2
+app:# usbip attach --remote host --busid busid
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port port
+app:# usbip detach --port port
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote host --busid busid
+   - Bind usbip-host.ko to the device with busid.
+   - The USB device of busid is connected to remote host!
+
+dev:# usbip disconnect --remote host --busid busid
+   - The USB device with busid is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show

[PATCH v4 00/11] usbip: features to USB over WebSocket

2015-05-27 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 

0. Version info

v4)
# Fixed regression of usbip list --remote

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as linux/usbip_ux.h.
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Remved unnecessary COPYING and AUTHORS fil from tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

1. Features included

It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not 
been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to 
a Linux based cloud service.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but 
difficult to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and 
kernel_recvmsg() with user spcace interface. When USB/IP utilities find 
usbip_ux.ko, they start threads to read/write PDUs from/to usbip_ux.ko 
and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are 
replaceable.

4) a WebSocket implementation

It's made with Poco C++. DNS and proxy client are supported.

I published scripts I used while developed the patches.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-ma
ke-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

2. Why WebSocket?

It allows to use USB/IP in internet. WebSocket is widely used to 
encapsulate packets in HTTP and to carry them through firewall using 
HTTP port numbers.

Assumed use case is a system that service in internet serves 
distributes devices in home or office networks. Service may be called 
as cloud and devices as ubiquitous.

   Home/SOHO/IntranetInternet  
 ++++
 +--+   +--+ |Router, ||Internet|
+|device|---|Linux |-|proxy,  ||service |
|+--+   +--+ |firewall||on Linux|
+--+   controller++++
ex)
Device  Service 
 sensors ... environment analysis 
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

3. Why userspace transmission?

Userspace transmission and APIs provided by this series allow to apply 
application protocols to USB/IP.
 
Why not use usbfs or libusb?
a) Not only device(usbip-host) side, application(vhci-hcd) side must be 
handled.
b) In device side, if using usbfs or libusb, many parts of usbip-common 
and usbip-host driver must be copied to userspace. It's not good for 
maintainability. 

Tunneling daemons can wrap TCP/IP with application protocol. They pass 
packets through loopback so this series has certain advantage regarding 
performance.

4. Why exporting devices?

Connection from outside firewall is usually blocked.
So existing import request sent with attach command doesn't work.

# usbipd (blocked)|| - # usbip attach

Firewall opens some ports, usually HTTP(80) and HTTPS(443), from inside.
Then export request sent with new connect command works.

# usbip connect  - # usbipa
 (passed)

Thank you,

Nobuo Iwata nobuo.iw...@fujixerox.co.jp
//

*** BLURB HERE ***

Nobuo Iwata (11):
  usbip: exporting devices
  usbip: readme and manuals about exporting devices
  usbip: safe completion against usb_kill_urb()
  usbip: kernel module for userspace URBs transmission
  usbip: tools for userspace URBs transmission
  usbip: readme about user space URBs transmission
  usbip: letting send and receive replaceable
  usbip: letting connection establishment replaceable
  usbip: deriving functions as libraries
  usbip: added const qualifier to arguments of some functions
  usbip: USB over WebSocket

 drivers/usb/usbip/Kconfig |  10 +
 drivers/usb/usbip/Makefile|   3 +
 drivers/usb/usbip/stub_dev.c  |  16 +-
 drivers/usb/usbip/stub_rx.c   |   3 +-
 drivers/usb/usbip/stub_tx.c   |   9 +-
 drivers/usb/usbip/usbip_common.c  |  79

[PATCH v4 01/11] usbip: exporting devices

2015-05-27 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid busid
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote host
// List importable USB devices from the host.
app:# usbip attach --remote host --busid busid
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote host --busid busid
// Export a device to host.

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets have been 
defined in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |   5 +
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 232 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 19 files changed, 1135 insertions(+), 297 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, host_driver-edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev-udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..5d46806 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2005-2007 Takahiro Hirofuchi
  */
 
 #include usbip_common.h
@@ -7,6 +8,8 @@
 #include limits.h
 #include netdb.h
 #include libudev.h
+#include fcntl.h
+#include errno.h
 #include sysfs_utils.h
 
 #undef  PROGNAME
@@ -215,6 +218,26 @@ static int read_record(int rhport, char *host, unsigned 
long host_len,
return 0;
 }
 
+#define OPEN_HC_MODE_FIRST 0
+#define

[PATCH v4 05/11] usbip: tools for userspace URBs transmission

2015-05-27 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after completion of the preparation, 
URBs are transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
Then, the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to 
read/wite URBs from usbip_ux.ko and transfer URBs in userspace. When 
usbip_ux.ko is installed, /dev/usbip-ux will be found, then the threads 
will be started. Otherwise, threads will not be started and original 
kernel space transmission is valid.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +++
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 ++-
 tools/usb/usbip/src/usbip_connect.c|  18 +-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 ++-
 10 files changed, 370 insertions(+), 23 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..3ac45a72
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include sys/types.h
+#include sys/stat.h
+#include fcntl.h
+#include sys/ioctl.h
+#include sys/socket.h
+#include usbip_common.h
+#include usbip_ux.h
+
+#undef  PROGNAME
+#define PROGNAME libusbip
+
+#define DEVNAME /dev/ USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg(dump %s for %zd bytes, label, bufflen);
+   for(i=0;ibufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, %04zx  %02x, i, *(buff+i)  0xff);
+   } else if (j == 8) {
+   sprintf(bb,   %02x, *(buff+i)  0xff);
+   } else {
+   sprintf(bb,  %02x, *(buff+i)  0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg(%s, b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg(connection closed on sock:%p, ux-kaddr.sock);
+   break;
+   } else if (received  0) {
+   dbg(receive error on sock:%p, ux-kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, ux received);
+   written = 0;
+   while(written  received) {
+   ret = write(ux-devfd, buf+written, received-written);
+   if (ret  0) {
+   dbg(write error for sock:%p, ux-kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg(end of ux-rx for sock:%p, ux-kaddr.sock);
+   ioctl(ux-devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux-devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg(end of read on sock:%p continue., ux-kaddr.sock);
+#endif
+   sched_yield();
+   continue;
+   } else  if (reads  0) {
+   dbg(read error on sock

[PATCH v4 03/11] usbip: safe completion against usb_kill_urb()

2015-05-27 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev-ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

The critial condition will happen by unbind command before detach, 
broken connection in connect command and disconnect command. 

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(sdev-priv_lock, flags);
-   if (priv-unlinking) {
+   if (sdev-ud.tcp_socket == NULL) {
+   dev_info(urb-dev-dev, discard a urb for closed connection);
+   } else if (priv-unlinking) {
stub_enqueue_ret_unlink(sdev, priv-seqnum, urb-status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 04/11] usbip: kernel module for userspace URBs transmission

2015-05-27 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket 
and so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space 
utilities. It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is 
valid. usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export   +---+
space   |uspipd,||usbip, |
|usbip  |  |usbipa |
+---+---+  +---+---+
|  |
2)Set sockfd|  |2)Set sockfd
  thru sysfs|  |  thru sysfs
V  V
Kernel  +---+4)URBs+---+
space   |usbip  ||vhci   |
|host.ko|  |hcd.ko |
+---+  +---+
3)link to kernel trx_ops   3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export   +---+
space   |uspipd,||usbip, |
+---|usbip  ||usbipa |---+
2)Set sockfd|+--+---+6)URBs+---+--+|2)Set sockfd
  thru ioctl||  ^ ^   ||  thru ioctl
  (right)   ||  |5)read/write 5)read/write|   ||  (left)
3)Set sockfd||  +---+  +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux|  +---+|  thru sysfs
  (left)VV  V  V  VV  (right)
Kernel  +---+   +---+  +---+   +---+
space   |usbip  |-|usbip  |  |usbip  |-|vhci   |
|host.ko|5)send |ux.ko  |  |ux.ko  |5)send |hcd.ko |
+---+  recv +---+  +---+  recv +---+
4)link to user trx_ops 4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

The reason why the userspace transmission oter than usbfs is needed.
a) Application(vhci_hcd)-side is needed
Usbfs provides functions to control device. So it can be applied to 
device(usbip_host)-side but not to application(vhci_hcd)-side.
b) Use existing kernel modules as-is
To implement same functionality in userspace with interface like usbfs, 
almost same code to kernel modules must be copied to userspcae. Also 
interfaces between kernel modules and utiities (sysfs) should be 
changed to new one. So utilities must be modified according to the new 
interface too. Modifications to existing code by this patch is small 
and usbip_ux.c handles major part of userspace transmission.

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 +++-
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 602 +++
 drivers/usb/usbip/usbip_ux.h |  82 +
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 +-
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 ++
 14 files changed, 869 insertions(+), 57 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate USB/IP userspace URB transmission
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.
+
 config USBIP_VHCI_HCD
tristate VHCI hcd
depends

[PATCH v4 08/11] usbip: letting connection establishment replaceable

2015-05-28 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock-shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command(cmds[i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index ae0ca6e..c67e3d2 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err_out;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup(ux);
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_out:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 5f9505c..8dabd0b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err_out;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err(tcp connect);
goto err_unbind_device;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup(ux);
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup(ux);
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_unbind_device:
usbip_unbind_device(busid);
 err_out:
diff --git a/tools/usb/usbip/src/usbip_disconnect.c 
b/tools/usb/usbip/src

[PATCH v4 07/11] usbip: letting send and receive replaceable

2015-05-28 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for 
both a) request/response PDUs among utilities and b) user space URBs 
transmission.

usbip_sock_t is introduced instead of sockfd. it includes function 
pointers of send/receive/shutdown routines, an argument for the 
routines, and a sockfd. The argument is needed for the routines. The 
sockfd is needed to bind connection to USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 14 ++
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 +++---
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +-
 tools/usb/usbip/src/usbip_attach.c | 30 ++--
 tools/usb/usbip/src/usbip_connect.c| 30 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 26 +-
 tools/usb/usbip/src/usbip_list.c   | 26 +-
 tools/usb/usbip/src/usbip_network.c| 66 ++
 tools/usb/usbip/src/usbip_network.h| 11 +++--
 tools/usb/usbip/src/usbipd.c   |  8 ++--
 tools/usb/usbip/src/usbipd_app.c   | 36 +++---
 tools/usb/usbip/src/usbipd_dev.c   | 40 
 13 files changed, 199 insertions(+), 130 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include libudev.h
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, %s / %s / %s (%02x/%02x/%02x), c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock-fd = fd;
+   sock-arg = arg;
+   sock-send = send;
+   sock-recv = recv;
+   sock-shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 3ac45a72..aa3d863 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux-sockfd, buf, BLEN, 0);
+   if (ux-sock-recv) {
+   received = ux-sock-recv(ux-sock-arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux-sock-fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, ux sending);
-   sent = send(ux-sockfd, buf, reads, 0);
+   if (ux-sock-send) {
+   sent = ux-sock-send(ux-sock-arg, buf, reads);
+   } else {
+   sent = send(ux-sock-fd, buf, reads, 0);
+   }
if (sent  0) {
dbg(connection closed on sock:%p, ux-kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg(end of ux-tx for sock:%p, ux-kaddr.sock);
-   shutdown(ux-sockfd, SHUT_RDWR);
+   if (ux-sock-shutdown) {
+   ux-sock-shutdown(ux-sock-arg);
+   } else {
+   shutdown(ux-sock-fd, SHUT_RDWR);
+   }
return 0;
 }
 
@@ -120,7 +132,7 @@ static void *usbip_ux_tx(void *arg

[PATCH v4 06/11] usbip: readme about user space URBs transmission

2015-05-28 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote host --busid busid
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote host --busid busid
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v4 10/11] usbip: added const qualifier to arguments of some functions

2015-05-28 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library 
interfaces to make acceptable std::string.c_str(). Essentially, these 
qualifiers are better to be used even if not to use C++. Although, I 
just added to functions related to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/list.h  | 24 --
 tools/usb/usbip/libsrc/usbip_common.c  | 16 +++
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 +
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 ++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 ---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  7 +++
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 +++
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 23 files changed, 86 insertions(+), 70 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next-prev = new;
-   new-next = next;
-   new-prev = prev;
-   prev-next = new;
+   next-prev = neo;
+   neo-next = next;
+   neo-prev = prev;
+   prev-next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head-next);
+   __list_add(neo, head, head-next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry-prev, entry-next);
-   entry-next = LIST_POISON1;
-   entry-prev = LIST_POISON2;
+   entry-next = (struct list_head *)LIST_POISON1;
+   entry-prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)-next, n = pos-next; pos != (head); \
pos = n, n = pos-next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index 54efa10..bf577b7 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -260,29 +260,29 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
snprintf(buff, size, %s : %s (%04x:%04x), vend, prod, vendor, 
product);
 }
 
-void usbip_names_get_class(char *buff, size_t size, uint8_t class,
+void usbip_names_get_class(char *buff, size_t size, uint8_t clazz,
   uint8_t subclass, uint8_t protocol)
 {
const char *c, *s, *p;
 
-   if (class == 0  subclass == 0  protocol == 0) {
-   snprintf(buff, size, (Defined at Interface level) 
(%02x/%02x/%02x), class, subclass, protocol);
+   if (clazz == 0  subclass == 0  protocol == 0) {
+   snprintf(buff

[PATCH v4 09/11] usbip: deriving functions as libraries

2015-05-28 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/libsrc/Makefile.am |  10 +-
 tools/usb/usbip/src/Makefile.am|  22 +++-
 tools/usb/usbip/src/usbip.h|   7 ++
 tools/usb/usbip/src/usbip_attach.c |  14 ++-
 tools/usb/usbip/src/usbip_bind.c   |   6 ++
 tools/usb/usbip/src/usbip_connect.c|  12 ++-
 tools/usb/usbip/src/usbip_detach.c |  13 ++-
 tools/usb/usbip/src/usbip_disconnect.c |  12 ++-
 tools/usb/usbip/src/usbip_list.c   |  70 -
 tools/usb/usbip/src/usbip_netconn.c| 133 +
 tools/usb/usbip/src/usbip_network.c|  67 -
 tools/usb/usbip/src/usbip_port.c   |  12 ++-
 tools/usb/usbip/src/usbip_unbind.c |   6 ++
 tools/usb/usbip/src/usbipd.c   |  13 +--
 tools/usb/usbip/src/usbipd.h   |  38 +++
 tools/usb/usbip/src/usbipd_app.c   |   1 +
 tools/usb/usbip/src/usbipd_dev.c   |   1 +
 17 files changed, 311 insertions(+), 126 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..780bdb3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,32 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='@USBIDS_DIR@/usb.ids'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_netconn.c\
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+libusbipd_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+libusbipa_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int usbip_disconnect(int argc, char *argv[]);
 
+int

[PATCH v4 11/11] usbip: USB over WebSocket

2015-05-28 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, 
port, bind, unbind, attach and detach). It uses --url option to specify 
remote address and port number. 

Implementation of this patch depends on Poco C++ 
(http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata nobuo.iw...@fujixerox.co.jp
---
 tools/usb/usbip/websocket/INSTALL | 237 ++
 tools/usb/usbip/websocket/Makefile.am |   3 +
 tools/usb/usbip/websocket/README  | 184 
 tools/usb/usbip/websocket/autogen.sh  |   9 +
 tools/usb/usbip/websocket/cleanup.sh  |  12 +
 tools/usb/usbip/websocket/configure.ac|  55 +++
 tools/usb/usbip/websocket/doc/usbws.8 | 192 
 tools/usb/usbip/websocket/doc/usbwsa.8| 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8| 109 +
 tools/usb/usbip/websocket/poco/Makefile.am|  18 +
 .../usb/usbip/websocket/poco/USBWSCommand.cpp | 410 ++
 tools/usb/usbip/websocket/poco/USBWSCommand.h |  99 +
 .../usb/usbip/websocket/poco/USBWSDaemon.cpp  | 228 ++
 tools/usb/usbip/websocket/poco/USBWSDaemon.h  |  80 
 .../websocket/poco/USBWSRequestHandler.cpp|  90 
 .../websocket/poco/USBWSRequestHandler.h  |  49 +++
 .../poco/USBWSRequestHandlerFactory.cpp   |  47 ++
 .../poco/USBWSRequestHandlerFactory.h |  48 ++
 tools/usb/usbip/websocket/poco/USBWSUtil.h|  52 +++
 .../usbip/websocket/poco/USBWSWebSocket.cpp   | 201 +
 .../usb/usbip/websocket/poco/USBWSWebSocket.h |  69 +++
 21 files changed, 2293 insertions(+)

diff --git a/tools/usb/usbip/websocket/INSTALL 
b/tools/usb/usbip/websocket/INSTALL
new file mode 100644
index 000..d3c5b40
--- /dev/null
+++ b/tools/usb/usbip/websocket/INSTALL
@@ -0,0 +1,237 @@
+Installation Instructions
+*
+
+Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+2006, 2007 Free Software Foundation, Inc.
+
+This file is free documentation; the Free Software Foundation gives
+unlimited permission to copy, distribute and modify it.
+
+Basic Installation
+==
+
+Briefly, the shell commands `./configure; make; make install' should
+configure, build, and install this package.  The following
+more-detailed instructions are generic; see the `README' file for
+instructions specific to this package.
+
+   The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation.  It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions.  Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, and a
+file `config.log' containing compiler output (useful mainly for
+debugging `configure').
+
+   It can also use an optional file (typically called `config.cache'
+and enabled with `--cache-file=config.cache' or simply `-C') that saves
+the results of its tests to speed up reconfiguring.  Caching is
+disabled by default to prevent problems with accidental use of stale
+cache files.
+
+   If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release.  If you are using the cache, and at
+some point `config.cache' contains results you don't want to keep, you
+may remove or edit it.
+
+   The file `configure.ac' (or `configure.in') is used to create
+`configure' by a program called `autoconf'.  You need `configure.ac' if
+you want to change it or regenerate `configure' using a newer version
+of `autoconf'.
+
+The simplest way to compile this package is:
+
+  1. `cd' to the directory containing the package's source code and type
+ `./configure' to configure the package for your system.
+
+ Running `configure' might take a while.  While running, it prints
+ some messages telling which features it is checking for.
+
+  2. Type `make' to compile the package.
+
+  3. Optionally, type `make check' to run any self-tests that come with
+ the package.
+
+  4. Type `make install' to install the programs and any data files and
+ documentation.
+
+  5. You can remove the program binaries and object files from the
+ source code directory by typing `make clean'.  To also

[PATCH v6 09/11] usbip: deriving functions as libraries

2016-01-04 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/Makefile.am | 10 +--
 tools/usb/usbip/src/Makefile.am| 22 +-
 tools/usb/usbip/src/usbip.h|  7 ++
 tools/usb/usbip/src/usbip_attach.c | 14 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 ++
 tools/usb/usbip/src/usbip_connect.c| 12 +++-
 tools/usb/usbip/src/usbip_detach.c | 15 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 12 +++-
 tools/usb/usbip/src/usbip_list.c   | 70 +++---
 tools/usb/usbip/src/usbip_netconn.c| 98 ++
 tools/usb/usbip/src/usbip_network.c| 67 --
 tools/usb/usbip/src/usbip_port.c   |  9 ++-
 tools/usb/usbip/src/usbip_unbind.c |  6 ++
 tools/usb/usbip/src/usbipd.c   | 13 +---
 tools/usb/usbip/src/usbipd.h   | 38 ++
 tools/usb/usbip/src/usbipd_app.c   |  1 +
 tools/usb/usbip/src/usbipd_dev.c   |  1 +
 17 files changed, 274 insertions(+), 127 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..780bdb3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,32 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_netconn.c\
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+libusbipd_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+libusbipa_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int u

[PATCH v6 11/11] usbip: USB over WebSocket

2016-01-04 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, 
port, bind, unbind, attach and detach). It uses --url option to specify 
remote address and port number. 

Implementation of this patch depends on Poco C++ 
(http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/websocket/INSTALL | 237 ++
 tools/usb/usbip/websocket/Makefile.am |   3 +
 tools/usb/usbip/websocket/README  | 184 
 tools/usb/usbip/websocket/autogen.sh  |   9 +
 tools/usb/usbip/websocket/cleanup.sh  |  12 +
 tools/usb/usbip/websocket/configure.ac|  55 +++
 tools/usb/usbip/websocket/doc/usbws.8 | 192 
 tools/usb/usbip/websocket/doc/usbwsa.8| 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8| 109 +
 tools/usb/usbip/websocket/poco/Makefile.am|  18 +
 .../usb/usbip/websocket/poco/USBWSCommand.cpp | 410 ++
 tools/usb/usbip/websocket/poco/USBWSCommand.h |  99 +
 .../usb/usbip/websocket/poco/USBWSDaemon.cpp  | 228 ++
 tools/usb/usbip/websocket/poco/USBWSDaemon.h  |  80 
 .../websocket/poco/USBWSRequestHandler.cpp|  90 
 .../websocket/poco/USBWSRequestHandler.h  |  49 +++
 .../poco/USBWSRequestHandlerFactory.cpp   |  49 +++
 .../poco/USBWSRequestHandlerFactory.h |  48 ++
 tools/usb/usbip/websocket/poco/USBWSUtil.h|  52 +++
 .../usbip/websocket/poco/USBWSWebSocket.cpp   | 204 +
 .../usb/usbip/websocket/poco/USBWSWebSocket.h |  69 +++
 21 files changed, 2298 insertions(+)

diff --git a/tools/usb/usbip/websocket/INSTALL 
b/tools/usb/usbip/websocket/INSTALL
new file mode 100644
index 000..d3c5b40
--- /dev/null
+++ b/tools/usb/usbip/websocket/INSTALL
@@ -0,0 +1,237 @@
+Installation Instructions
+*
+
+Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+2006, 2007 Free Software Foundation, Inc.
+
+This file is free documentation; the Free Software Foundation gives
+unlimited permission to copy, distribute and modify it.
+
+Basic Installation
+==
+
+Briefly, the shell commands `./configure; make; make install' should
+configure, build, and install this package.  The following
+more-detailed instructions are generic; see the `README' file for
+instructions specific to this package.
+
+   The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation.  It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions.  Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, and a
+file `config.log' containing compiler output (useful mainly for
+debugging `configure').
+
+   It can also use an optional file (typically called `config.cache'
+and enabled with `--cache-file=config.cache' or simply `-C') that saves
+the results of its tests to speed up reconfiguring.  Caching is
+disabled by default to prevent problems with accidental use of stale
+cache files.
+
+   If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release.  If you are using the cache, and at
+some point `config.cache' contains results you don't want to keep, you
+may remove or edit it.
+
+   The file `configure.ac' (or `configure.in') is used to create
+`configure' by a program called `autoconf'.  You need `configure.ac' if
+you want to change it or regenerate `configure' using a newer version
+of `autoconf'.
+
+The simplest way to compile this package is:
+
+  1. `cd' to the directory containing the package's source code and type
+ `./configure' to configure the package for your system.
+
+ Running `configure' might take a while.  While running, it prints
+ some messages telling which features it is checking for.
+
+  2. Type `make' to compile the package.
+
+  3. Optionally, type `make check' to run any self-tests that come with
+ the package.
+
+  4. Type `make install' to install the programs and any data files and
+ documentation.
+
+  5. You can remove the program binaries and object files from the
+ source code directory by typing `make clean'.  T

[PATCH v6 08/11] usbip: letting connection establishment replaceable

2016-01-04 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock->shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command([i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index ae0ca6e..c67e3d2 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err("tcp connect");
goto err_out;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup();
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup();
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_out:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 5f9505c..8dabd0b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err_out;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err("tcp connect");
goto err_unbind_device;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup();
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup();
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_unbind_device:
usbip_unbind_device(busid);
 err_out:
diff --git a/tools/usb/usbip/src/usbip_discon

[PATCH v6 05/11] usbip: tools for userspace URBs transmission

2016-01-04 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after completion of the preparation, 
URBs are transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
Then, the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to 
read/wite URBs from usbip_ux.ko and transfer URBs in userspace. When 
usbip_ux.ko is installed, /dev/usbip-ux will be found, then the threads 
will be started. Otherwise, threads will not be started and original 
kernel space transmission is valid.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +++
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 ++-
 tools/usb/usbip/src/usbip_connect.c|  18 +-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 ++-
 10 files changed, 370 insertions(+), 23 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..3ac45a72
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "usbip_common.h"
+#include "usbip_ux.h"
+
+#undef  PROGNAME
+#define PROGNAME "libusbip"
+
+#define DEVNAME "/dev/" USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg("dump %s for %zd bytes", label, bufflen);
+   for(i=0;i<bufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, "%04zx  %02x", i, *(buff+i) & 0xff);
+   } else if (j == 8) {
+   sprintf(bb, "  %02x", *(buff+i) & 0xff);
+   } else {
+   sprintf(bb, " %02x", *(buff+i) & 0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg("%s", b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux->sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg("connection closed on sock:%p", ux->kaddr.sock);
+   break;
+   } else if (received < 0) {
+   dbg("receive error on sock:%p", ux->kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, "ux received");
+   written = 0;
+   while(written < received) {
+   ret = write(ux->devfd, buf+written, received-written);
+   if (ret < 0) {
+   dbg("write error for sock:%p", ux->kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg("end of ux-rx for sock:%p", ux->kaddr.sock);
+   ioctl(ux->devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux->devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg("end of read on sock:%p

[PATCH v6 07/11] usbip: letting send and receive replaceable

2016-01-04 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for 
both a) request/response PDUs among utilities and b) user space URBs 
transmission.

usbip_sock_t is introduced instead of sockfd. it includes function 
pointers of send/receive/shutdown routines, an argument for the 
routines, and a sockfd. The argument is needed for the routines. The 
sockfd is needed to bind connection to USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 +
 tools/usb/usbip/libsrc/usbip_common.h  | 14 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 ++--
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +-
 tools/usb/usbip/src/usbip_attach.c | 30 +-
 tools/usb/usbip/src/usbip_connect.c| 30 +-
 tools/usb/usbip/src/usbip_disconnect.c | 26 -
 tools/usb/usbip/src/usbip_list.c   | 27 +
 tools/usb/usbip/src/usbip_network.c| 78 +++---
 tools/usb/usbip/src/usbip_network.h| 12 ++--
 tools/usb/usbip/src/usbipd.c   | 14 +++--
 tools/usb/usbip/src/usbipd_app.c   | 36 ++--
 tools/usb/usbip/src/usbipd_dev.c   | 40 ++---
 13 files changed, 214 insertions(+), 135 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include 
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock->fd = fd;
+   sock->arg = arg;
+   sock->send = send;
+   sock->recv = recv;
+   sock->shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 3ac45a72..aa3d863 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux->sockfd, buf, BLEN, 0);
+   if (ux->sock->recv) {
+   received = ux->sock->recv(ux->sock->arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux->sock->fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg("connection closed on sock:%p", ux->kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, "ux sending");
-   sent = send(ux->sockfd, buf, reads, 0);
+   if (ux->sock->send) {
+   sent = ux->sock->send(ux->sock->arg, buf, reads);
+   } else {
+   sent = send(ux->sock->fd, buf, reads, 0);
+   }
if (sent < 0) {
dbg("connection closed on sock:%p", ux->kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg("end of ux-tx for sock:%p", ux->kaddr.sock);
-   shutdown(ux->sockfd, SHUT_RDWR);
+   if (ux->sock->shutdown) {
+   ux->sock->shutd

[PATCH v6 10/11] usbip: added const qualifier to arguments of some functions

2016-01-04 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library 
interfaces to make acceptable std::string.c_str(). Essentially, these 
qualifiers are better to be used even if not to use C++. Although, I 
just added to functions related to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/list.h  | 24 --
 tools/usb/usbip/libsrc/names.c | 15 +++---
 tools/usb/usbip/libsrc/usbip_common.c  | 16 +++
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 +
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 ++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 ---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  7 +++
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 +++
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 24 files changed, 94 insertions(+), 77 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next->prev = new;
-   new->next = next;
-   new->prev = prev;
-   prev->next = new;
+   next->prev = neo;
+   neo->next = next;
+   neo->prev = prev;
+   prev->next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head->next);
+   __list_add(neo, head, head->next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry->prev, entry->next);
-   entry->next = LIST_POISON1;
-   entry->prev = LIST_POISON2;
+   entry->next = (struct list_head *)LIST_POISON1;
+   entry->prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/names.c b/tools/usb/usbip/libsrc/names.c
index 7d65d28..656005f 100644
--- a/tools/usb/usbip/libsrc/names.c
+++ b/tools/usb/usbip/libsrc/names.c
@@ -23,6 +23,8 @@
  *
  * Copyright (C) 2005 Takahiro Hirofuchi
  *     - names_deinit() is added.
+ * Copyright (C) 2015 Nobuo Iwata
+ * - some modifications for portability.
  *
  */
 
@@ -38,7 +40,6 @@
 #include 
 
 #include "names.h"
-#include "usbip_common.h"
 
 struct vendor {
struct vendor *next;
@@ -52,8 +53,8 @@ struct product {
char name[1];
 };
 
-struct class {
-   struct class *next;
+struct clazz {
+   struct clazz *next;
u_int8_t classid

[PATCH v6 06/11] usbip: readme about user space URBs transmission

2016-01-04 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote  --busid 
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote  --busid 
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 2/2] usbip: single thread event handler

2016-01-04 Thread Nobuo Iwata
This patch reduces number of event handling threads to one.

In existing implementation, event kernel threads are created for each 
port. The functions of the threads are terminationg connection and 
error handling. It's too expensive to have to each port.

With this patch, event handler is a single thread workqueue 
[usbip_event].

Both application (vhci) and device (stub) side are replaced.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/stub_dev.c |   3 +-
 drivers/usb/usbip/usbip_common.c |   7 ++
 drivers/usb/usbip/usbip_common.h |   4 +-
 drivers/usb/usbip/usbip_event.c  | 174 +++
 drivers/usb/usbip/vhci_sysfs.c   |   2 +-
 5 files changed, 142 insertions(+), 48 deletions(-)

diff --git a/drivers/usb/usbip/stub_dev.c b/drivers/usb/usbip/stub_dev.c
index d8d3add..9ec002d 100644
--- a/drivers/usb/usbip/stub_dev.c
+++ b/drivers/usb/usbip/stub_dev.c
@@ -380,7 +380,6 @@ err_files:
 err_port:
dev_set_drvdata(>dev, NULL);
usb_put_dev(udev);
-   kthread_stop_put(sdev->ud.eh);
 
busid_priv->sdev = NULL;
stub_device_free(sdev);
@@ -441,7 +440,7 @@ static void stub_disconnect(struct usb_device *udev)
}
 
/* If usb reset is called from event handler */
-   if (busid_priv->sdev->ud.eh == current)
+   if (usbip_in_eh(current))
return;
 
/* shutdown the current connection */
diff --git a/drivers/usb/usbip/usbip_common.c b/drivers/usb/usbip/usbip_common.c
index 963d8db..8c9fb19 100644
--- a/drivers/usb/usbip/usbip_common.c
+++ b/drivers/usb/usbip/usbip_common.c
@@ -807,12 +807,19 @@ EXPORT_SYMBOL_GPL(usbip_trx_ops);
 
 static int __init usbip_core_init(void)
 {
+   int ret;
+
pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
+   ret = usbip_init_eh();
+   if (ret) {
+   return ret;
+   }
return 0;
 }
 
 static void __exit usbip_core_exit(void)
 {
+   usbip_finish_eh();
return;
 }
 
diff --git a/drivers/usb/usbip/usbip_common.h b/drivers/usb/usbip/usbip_common.h
index ffed6fe..5e56598 100644
--- a/drivers/usb/usbip/usbip_common.h
+++ b/drivers/usb/usbip/usbip_common.h
@@ -275,7 +275,6 @@ struct usbip_device {
struct task_struct *tcp_tx;
 
unsigned long event;
-   struct task_struct *eh;
wait_queue_head_t eh_waitq;
 
struct eh_ops {
@@ -321,10 +320,13 @@ void usbip_pad_iso(struct usbip_device *ud, struct urb 
*urb);
 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
 
 /* usbip_event.c */
+int usbip_init_eh(void);
+void usbip_finish_eh(void);
 int usbip_start_eh(struct usbip_device *ud);
 void usbip_stop_eh(struct usbip_device *ud);
 void usbip_event_add(struct usbip_device *ud, unsigned long event);
 int usbip_event_happened(struct usbip_device *ud);
+int usbip_in_eh(struct task_struct *task);
 
 static inline int interface_to_busnum(struct usb_interface *interface)
 {
diff --git a/drivers/usb/usbip/usbip_event.c b/drivers/usb/usbip/usbip_event.c
index 64933b9..9bbdf1d 100644
--- a/drivers/usb/usbip/usbip_event.c
+++ b/drivers/usb/usbip/usbip_event.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,17 +20,68 @@
 
 #include 
 #include 
+#include 
+#include 
 
 #include "usbip_common.h"
 
-static int event_handler(struct usbip_device *ud)
+struct usbip_event {
+   struct list_head node;
+   struct usbip_device *ud;
+};
+
+static DEFINE_SPINLOCK(event_lock);
+static LIST_HEAD(event_list);
+
+static void set_event(struct usbip_device *ud, unsigned long event)
+{
+   unsigned long flags;
+
+   spin_lock_irqsave(>lock, flags);
+   ud->event |= event;
+   spin_unlock_irqrestore(>lock, flags);
+}
+
+static void unset_event(struct usbip_device *ud, unsigned long event)
 {
-   usbip_dbg_eh("enter\n");
+   unsigned long flags;
+
+   spin_lock_irqsave(>lock, flags);
+   ud->event &= ~event;
+   spin_unlock_irqrestore(>lock, flags);
+}
+
+static struct usbip_device *get_event(void)
+{
+   struct usbip_event *ue = NULL;
+   struct usbip_device *ud = NULL;
+   unsigned long flags;
 
-   /*
-* Events are handled by only this thread.
-*/
-   while (usbip_event_happened(ud)) {
+   spin_lock_irqsave(_lock, flags);
+   if (!list_empty(_list)) {
+   ue = list_first_entry(_list, struct usbip_event, node);
+   list_del(>node);
+   }
+   spin_unlock_irqrestore(_lock, flags);
+
+   if (ue) {
+   ud = ue->ud;
+   kfree(ue);
+   }
+   return ud;
+}
+
+static struct task_struct *worker_context = NULL;
+
+static void event_handler(struct wo

[PATCH v2 1/2] usbip: vhci number of ports extension

2016-01-04 Thread Nobuo Iwata
This patch extends number of ports limitation in application (vhci) 
side.

To do so, vhci driver supports multiple host controllers. The number of 
controllers can be specified as a module parameter 'num_controllers'. 
The default is 1.

ex) # insmod vhci_hcd.ko num_controllers=4

Also, ports per controller is changed from 8 to USB_MAXCHILDREN (31). 
It can be modified with VHCI_NPORTS flag at module compilation.

So number of ports supported by vhci is 'num_controllers' * 31.

Sysfs structure is changes as following.
BEFORE:
/sys/devices/platform
+-- vhci
+-- status
+-- attach
+-- detach
+-- usbip_debug
AFTER: example for num_controllers=4
/sys/devices/platform
+-- vhci.0
|   +-- nports
|   +-- status.0
|   +-- status.1
|   +-- status.2
|   +-- status.3
|   +-- attach
|   +-- detach
|   +-- usbip_debug
+-- vhci.1
+-- vhci.2
+-- vhci.3

vhci.N is shown for each host controller kobj. vhch.1, vhci.2, ... are 
shown only when num_controllers is more than 1. Only vhci.0 has user 
space interfaces. 'nports' is newly added to give ports-per-controller 
and number of controlles. Before that, number of ports is acquired by 
counting status lines. Status is divided for each controller to avoid 
page size (4KB) limitation.

Variable wording relating port has been corrected. 'port' represents id 
across multiple controllers. 'rhport (root hub port)' represents id 
within a controller.

Some unimportant info level messages are changed to debug level because 
they are too busy when using many ports.

NOTE: Syslog error messages "systemd-udevd[390]: error opening USB 
device 'descriptors' file" may be shown. They are not caused by this 
patch. It seems to be a systemd problem.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/usbip_ux.c |   2 +-
 drivers/usb/usbip/vhci.h |  41 +-
 drivers/usb/usbip/vhci_hcd.c | 259 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 298 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 548 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  38 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  56 ++-
 11 files changed, 775 insertions(+), 512 deletions(-)

diff --git a/drivers/usb/usbip/README b/drivers/usb/usbip/README
index 41a2cf2..fce3d7f 100644
--- a/drivers/usb/usbip/README
+++ b/drivers/usb/usbip/README
@@ -1,3 +1,6 @@
+MODULE PARAMS:
+   - num_controllers : number of controllers. Default is 1.
+
 TODO:
- more discussion about the protocol
- testing
diff --git a/drivers/usb/usbip/usbip_ux.c b/drivers/usb/usbip/usbip_ux.c
index 97e6385..4abf0b2 100644
--- a/drivers/usb/usbip/usbip_ux.c
+++ b/drivers/usb/usbip/usbip_ux.c
@@ -396,7 +396,7 @@ static int usbip_ux_unlink(struct usbip_device *ud)
rcu_read_lock();
ux = rcu_dereference(ud->ux);
if (ux == NULL) {
-   pr_err("Unlink to unlinked ux.\n");
+   usbip_dbg_ux("Unlink to unlinked ux.\n");
rcu_read_unlock();
return -EINVAL;
}
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index a863a98..6c34075 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,7 +73,11 @@ struct vhci_unlink {
 };
 
 /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
-#define VHCI_NPORTS 8
+#ifndef VHCI_NPORTS
+#define VHCI_NPORTS USB_MAXCHILDREN
+#endif
+
+#define MAX_STATUS_NAME 16
 
 /* for usb_bus.hcpriv */
 struct vhci_hcd {
@@ -93,11 +98,16 @@ struct vhci_hcd {
struct vhci_device vdev[VHCI_NPORTS];
 };
 
-extern struct vhci_hcd *the_controller;
-extern const struct attribute_group dev_attr_group;
+extern int num_controllers;
+extern struct platform_device **the_pdevs;
+extern struct attribute_group dev_attr_group;
 
 /* vhci_hcd.c */
-void rh_port_connect(int rhport, enum usb_device_speed speed);
+void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
+
+/* vhci_sysfs.c */
+int vhci_init_attr_group(void);
+void vhci_finish_attr_group(void);
 
 /* vhci_rx.c */
 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
@@ -106,9 +116,14 @@ int vhci_rx_loop(void *data);
 /* vhci_tx.c */
 int vhci_tx_loop(void *data);
 
-static inline struct vhci_device *port_to_vdev(__u32 port)
+static inline __u32 port_to_rhport(__u32 port)
+{
+   

[PATCH v2 0/2] usbip: vhci number of ports extension

2016-01-04 Thread Nobuo Iwata
This series of patches extends number of ports limitaion in application 
(vhci) side.

0. Version info

v2)
# Added static to some functions and variables not called from other 
files. 

1. Overview

This series conatins 2 patches.
1/2:
Extends number of ports using multiple host controllers.
'num_controllers=N' module parameter denotes the number.
The default is 1.
Number of ports per controller are extended from 8 to
USB_MAXCHILDREN(31).
It can be altered with -DVHCI_NPORTS=n at compile time.
2/2:
Event handling threads are used to be created for each port.
This patch aggregates them to one thread.
Rewritten with workqueue.

Assumed use case is a system that service in internet serves 
distributes devices in home or office. In the use case, application 
side might be needed to support more ports than 31.

Home/SOHO/Enterprise Intranet/Internet
   ++
 +--+   +--+   |Service |
+|device|---|Linux |---|on  |
|+--+   +--+   |Linux   |
+--+   controller  ++
ex)
Device  Service
 sensors ... environment analysis
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

To increase number of ports, existing implementation has an overhead 
that event handing kernel threads are started for each port. The second 
patch eliminates the overhead.

NOTE: This series depends on "USB/IP over WebSocket" patches.

*** BLURB HERE ***

Nobuo Iwata (2):
  usbip: vhci number of ports extension
  usbip: single thread event handler

 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/stub_dev.c |   3 +-
 drivers/usb/usbip/usbip_common.c |   7 +
 drivers/usb/usbip/usbip_common.h |   4 +-
 drivers/usb/usbip/usbip_event.c  | 174 ++---
 drivers/usb/usbip/usbip_ux.c |   2 +-
 drivers/usb/usbip/vhci.h |  41 +-
 drivers/usb/usbip/vhci_hcd.c | 259 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 298 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 548 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  38 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  56 ++-
 15 files changed, 916 insertions(+), 559 deletions(-)

-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v6 01/11] usbip: exporting devices

2016-01-04 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid 
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote 
// List importable USB devices from the .
app:# usbip attach --remote  --busid 
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote  --busid 
// Export a device to .

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets have been 
defined in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.
Also, vendor/product name converion is added to port commnad as same as 
list command.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/names.c |   4 +-
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |  21 +-
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 233 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 20 files changed, 1151 insertions(+), 302 deletions(-)

diff --git a/tools/usb/usbip/libsrc/names.c b/tools/usb/usbip/libsrc/names.c
index 81ff852..7d65d28 100644
--- a/tools/usb/usbip/libsrc/names.c
+++ b/tools/usb/usbip/libsrc/names.c
@@ -162,7 +162,7 @@ struct pool {
void *mem;
 };
 
-static struct pool *pool_head;
+static struct pool *pool_head = NULL;
 
 static void *my_malloc(size_t size)
 {
@@ -201,6 +201,8 @@ void names_free(void)
pool = pool->next;
free(tmp);
}
+
+   pool_head = NULL;
 }
 
 static int new_vendor(const char *name, u_int16_t vendorid)
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, _driver->edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/

[PATCH v6 04/11] usbip: kernel module for userspace URBs transmission

2016-01-04 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket 
and so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space 
utilities. It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is 
valid. usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export   +---+
space   |uspipd,|<>|usbip, |
|usbip  |  |usbipa |
+---+---+  +---+---+
|  |
2)Set sockfd|  |2)Set sockfd
  thru sysfs|  |  thru sysfs
V  V
Kernel  +---+4)URBs+---+
space   |usbip  |<>|vhci   |
|host.ko|  |hcd.ko |
+---+  +---+
3)link to kernel trx_ops   3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export   +---+
space   |uspipd,|<>|usbip, |
+---|usbip  |<>|usbipa |---+
2)Set sockfd|+--+---+6)URBs+---+--+|2)Set sockfd
  thru ioctl||  ^ ^   ||  thru ioctl
  (right)   ||  |5)read/write 5)read/write|   ||  (left)
3)Set sockfd||  +---+  +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux|  +---+|  thru sysfs
  (left)VV  V  V  VV  (right)
Kernel  +---+   +---+  +---+   +---+
space   |usbip  |<->|usbip  |  |usbip  |<->|vhci   |
|host.ko|5)send |ux.ko  |  |ux.ko  |5)send |hcd.ko |
+---+  recv +---+  +---+  recv +---+
4)link to user trx_ops 4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

The reason why the userspace transmission oter than usbfs is needed.
a) Application(vhci_hcd)-side is needed
Usbfs provides functions to control device. So it can be applied to 
device(usbip_host)-side but not to application(vhci_hcd)-side.
b) Use existing kernel modules as-is
To implement same functionality in userspace with interface like usbfs, 
almost same code to kernel modules must be copied to userspcae. Also 
interfaces between kernel modules and utiities (sysfs) should be 
changed to new one. So utilities must be modified according to the new 
interface too. Modifications to existing code by this patch is small 
and usbip_ux.c handles major part of userspace transmission.

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 +++-
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 598 +++
 drivers/usb/usbip/usbip_ux.h |  82 +
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 ++-
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 ++
 14 files changed, 865 insertions(+), 57 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate "USB/IP userspace URB transmission"
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.

[PATCH v6 00/11] usbip: features to USB over WebSocket

2016-01-04 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 

0. Version info

v6)
# Added __rcu annotation to a RCU pointer to clear sparse warnings.
# Corrected a copy to RCU pointer with rcu_rcu_assign_pointer(). 
# Added __user annotations to arguments of read/write method. 
# Added static to some functions which are not called from other files.
# Removed unnecessary EXPORT_SYMBOLs.

v5)
# Added vendor/pruduct name conversion to port command.
# Put initial value to pool_head in name.c.
# Fixed list command exception when host option is omitted.
# Fixed exception in case gai_strerror() returns NULL.
# Fixed WebSocket connection close via proxy.
# Fixed to stop WebSocket ping-pong on connection close.
# Removed redundant usbipd daemon option.
# Removed redundant SSL code had not been deleted.
# Removed an unused local variable in WebSocket code.
# Modified C++ reserved word in names.c as same as headers.

v4)
# Fixed regression of usbip list --remote

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as .
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Remved unnecessary COPYING and AUTHORS fil from tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

1. Features included

It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not 
been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to 
a Linux based cloud service.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but 
difficult to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and 
kernel_recvmsg() with user spcace interface. When USB/IP utilities find 
usbip_ux.ko, they start threads to read/write PDUs from/to usbip_ux.ko 
and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are 
replaceable.

4) a WebSocket implementation

It's made with Poco C++. DNS and proxy client are supported.

I published scripts I used while developed the patches.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-ma
ke-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

2. Why WebSocket?

It allows to use USB/IP in internet. WebSocket is widely used to 
encapsulate packets in HTTP and to carry them through firewall using 
HTTP port numbers.

Assumed use case is a system that service in internet serves 
distributes devices in home or office networks. Service may be called 
as cloud and devices as ubiquitous.

   Home/SOHO/IntranetInternet  
 ++++
 +--+   +--+ |Router, ||Internet|
+|device|---|Linux |-|proxy,  ||service |
|+--+   +--+ |firewall||on Linux|
+--+   controller++++
ex)
Device  Service 
 sensors ... environment analysis 
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

3. Why userspace transmission?

Userspace transmission and APIs provided by this series allow to apply 
application protocols to USB/IP.
 
Why not use usbfs or libusb?
a) Not only device(usbip-host) side, application(vhci-hcd) side must be 
handled.
b) In device side, if using usbfs or libusb, many parts of usbip-common 
and usbip-host driver must be copied to userspace. It's not good for 
maintainability. 

Tunneling daemons can wrap TCP/IP with application protocol. They pass 
packets through loopback so this series has certain advantage regarding 
performance. It's important for small (IoT) devices.

4. Why exporting devices?

Connection from outside firewall is usually blocked.
So existing import request sent with attach command doesn't work.

# usbipd (blocked)|| <- # usbip attach

Firewall opens some ports, usually HTTP(80) and HTTPS(443), from inside.
Then export request sent with new connect command works.

# usbip connect  -> # usbipa
 (passed)

Thank you,

Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
//

**

[PATCH v6 02/11] usbip: readme and manuals about exporting devices

2016-01-04 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' is changed also.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 +---
 tools/usb/usbip/doc/usbip.8  | 82 +---
 tools/usb/usbip/doc/usbipa.8 | 77 ++
 tools/usb/usbip/doc/usbipd.8 | 29 +-
 tools/usb/usbip/libsrc/vhci_driver.c |  2 +-
 6 files changed, 205 insertions(+), 57 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney <m...@muteddisk.com>
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device with  is now exportable to other hosts!
+   - Use `usbip unbind --busid ` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote 
+app:# usbip list --remote 
- List exported USB devices on the .
 
-client:# usbip attach --remote  --busid 1-2
+app:# usbip attach --remote  --busid 
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port 
+app:# usbip detach --port 
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote  --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device of  is connected to remote host!
+
+dev:# usbip disconnect --remote  --busid 
+   - The USB device with  is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show the devices attached to this client:
+Show the devices attach

[PATCH v6 03/11] usbip: safe completion against usb_kill_urb()

2016-01-04 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev->ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

The critial condition will happen by unbind command before detach, 
broken connection in connect command and disconnect command. 

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(>priv_lock, flags);
-   if (priv->unlinking) {
+   if (sdev->ud.tcp_socket == NULL) {
+   dev_info(>dev->dev, "discard a urb for closed connection");
+   } else if (priv->unlinking) {
stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 09/11] usbip: deriving functions as libraries

2015-12-29 Thread Nobuo Iwata
To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/Makefile.am | 10 +--
 tools/usb/usbip/src/Makefile.am| 22 +-
 tools/usb/usbip/src/usbip.h|  7 ++
 tools/usb/usbip/src/usbip_attach.c | 14 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 ++
 tools/usb/usbip/src/usbip_connect.c| 12 +++-
 tools/usb/usbip/src/usbip_detach.c | 15 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 12 +++-
 tools/usb/usbip/src/usbip_list.c   | 70 +++---
 tools/usb/usbip/src/usbip_netconn.c| 98 ++
 tools/usb/usbip/src/usbip_network.c| 67 --
 tools/usb/usbip/src/usbip_port.c   |  9 ++-
 tools/usb/usbip/src/usbip_unbind.c |  6 ++
 tools/usb/usbip/src/usbipd.c   | 13 +---
 tools/usb/usbip/src/usbipd.h   | 38 ++
 tools/usb/usbip/src/usbipd_app.c   |  1 +
 tools/usb/usbip/src/usbipd_dev.c   |  1 +
 17 files changed, 274 insertions(+), 127 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c 
usbip_host_driver.h \
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
   usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..780bdb3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,32 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD   = $(top_builddir)/libsrc/libusbip.la
+LDADD   = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+usbip_network.c usbip_netconn.c\
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+usbip_attach.c usbip_detach.c usbip_list.c \
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+libusbipd_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+libusbipa_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int u

[PATCH v5 00/11] usbip: features to USB over WebSocket

2015-12-29 Thread Nobuo Iwata
Dear all,

This series of patches introduces WebSocket to USB/IP. 

0. Version info

v5)
# Added vendor/pruduct name conversion to port command.
# Put initial value to pool_head in name.c.
# Fixed list command exception when host option is omitted.
# Fixed exception in case gai_strerror() returns NULL.
# Fixed WebSocket connection close via proxy.
# Fixed to stop WebSocket ping-pong on connection close.
# Removed redundant usbipd daemon option.
# Removed redundant SSL code had not been deleted.
# Removed an unused local variable in WebSocket code.
# Modified C++ reserved word in names.c as same as headers.

v4)
# Fixed regression of usbip list --remote

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as .
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Remved unnecessary COPYING and AUTHORS fil from tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

1. Features included

It also includes some independent features effective in themselves.

1) Exporting devices

Export request and response PDU had been defined in a header but not 
been used.
Now it works!
   
Also, it supports senarios, for example, connect ubiquetous devices to 
a Linux based cloud service.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

2) User space transmission

USB/IP transfer URBs in kernel space. It's better for performance but 
difficult to introduce application protocols.

Like fuse for file systems, it allows to transfer URBs in user space.

When usbip_ux.ko is loaded, it replaces kernel_sendmsg() and 
kernel_recvmsg() with user spcace interface. When USB/IP utilities find 
usbip_ux.ko, they start threads to read/write PDUs from/to usbip_ux.ko 
and send/recv them.

3) Replaceable protocols

Both transmission(send/receive) and connection establishment are 
replaceable.

4) a WebSocket implementation

It's made with Poco C++. DNS and proxy client are supported.

I published scripts I used while developed the patches.
http://linux-usbip-additions.blogspot.jp/2015/03/scripts-to-patch-and-ma
ke-locally.html
http://linux-usbip-additions.blogspot.jp/2015/03/test-scripts.html

2. Why WebSocket?

It allows to use USB/IP in internet. WebSocket is widely used to 
encapsulate packets in HTTP and to carry them through firewall using 
HTTP port numbers.

Assumed use case is a system that service in internet serves 
distributes devices in home or office networks. Service may be called 
as cloud and devices as ubiquitous.

   Home/SOHO/IntranetInternet  
 ++++
 +--+   +--+ |Router, ||Internet|
+|device|---|Linux |-|proxy,  ||service |
|+--+   +--+ |firewall||on Linux|
+--+   controller++++
ex)
Device  Service 
 sensors ... environment analysis 
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

3. Why userspace transmission?

Userspace transmission and APIs provided by this series allow to apply 
application protocols to USB/IP.
 
Why not use usbfs or libusb?
a) Not only device(usbip-host) side, application(vhci-hcd) side must be 
handled.
b) In device side, if using usbfs or libusb, many parts of usbip-common 
and usbip-host driver must be copied to userspace. It's not good for 
maintainability. 

Tunneling daemons can wrap TCP/IP with application protocol. They pass 
packets through loopback so this series has certain advantage regarding 
performance. It's important for small (IoT) devices.

4. Why exporting devices?

Connection from outside firewall is usually blocked.
So existing import request sent with attach command doesn't work.

# usbipd (blocked)|| <- # usbip attach

Firewall opens some ports, usually HTTP(80) and HTTPS(443), from inside.
Then export request sent with new connect command works.

# usbip connect  -> # usbipa
 (passed)

Thank you,

Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
//

*** BLURB HERE ***

Nobuo Iwata (11):
  usbip: exporting devices
  usbip: readme and manuals about exporting devices
  usbip: safe completion against usb_kill_urb()
  usbip: kernel module for userspace URBs transmission
  usbip: tools for userspace URBs transmission
  usbip: readme about user space URBs transmis

[PATCH v1 1/2] usbip: vhci number of ports extension

2015-12-29 Thread Nobuo Iwata
This patch extends number of ports limitation in application (vhci) 
side.

To do so, vhci driver supports multiple host controllers. The number of 
controllers can be specified as a module parameter 'num_controllers'. 
The default is 1.

ex) # insmod vhci_hcd.ko num_controllers=4

Also, ports per controller is changed from 8 to USB_MAXCHILDREN (31). 
It can be modified with VHCI_NPORTS flag at module compilation.

So number of ports supported by vhci is 'num_controllers' * 31.

Sysfs structure is changes as following.
BEFORE:
/sys/devices/platform
+-- vhci
+-- status
+-- attach
+-- detach
+-- usbip_debug
AFTER: example for num_controllers=4
/sys/devices/platform
+-- vhci.0
|   +-- nports
|   +-- status.0
|   +-- status.1
|   +-- status.2
|   +-- status.3
|   +-- attach
|   +-- detach
|   +-- usbip_debug
+-- vhci.1
+-- vhci.2
+-- vhci.3

vhci.N is shown for each host controller kobj. vhch.1, vhci.2, ... are 
shown only when num_controllers is more than 1. Only vhci.0 has user 
space interfaces. 'nports' is newly added to give ports-per-controller 
and number of controlles. Before that, number of ports is acquired by 
counting status lines. Status is divided for each controller to avoid 
page size (4KB) limitation.

Variable wording relating port has been corrected. 'port' represents id 
across multiple controllers. 'rhport (root hub port)' represents id 
within a controller.

Some unimportant info level messages are changed to debug level because 
they are too busy when using many ports.

NOTE: Syslog error messages "systemd-udevd[390]: error opening USB 
device 'descriptors' file" may be shown. They are not caused by this 
patch. It seems to be a systemd problem.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/usbip_ux.c |   2 +-
 drivers/usb/usbip/vhci.h |  41 +-
 drivers/usb/usbip/vhci_hcd.c | 259 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 298 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 548 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  38 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  56 ++-
 11 files changed, 775 insertions(+), 512 deletions(-)

diff --git a/drivers/usb/usbip/README b/drivers/usb/usbip/README
index 41a2cf2..fce3d7f 100644
--- a/drivers/usb/usbip/README
+++ b/drivers/usb/usbip/README
@@ -1,3 +1,6 @@
+MODULE PARAMS:
+   - num_controllers : number of controllers. Default is 1.
+
 TODO:
- more discussion about the protocol
- testing
diff --git a/drivers/usb/usbip/usbip_ux.c b/drivers/usb/usbip/usbip_ux.c
index b82b6f4..40db362 100644
--- a/drivers/usb/usbip/usbip_ux.c
+++ b/drivers/usb/usbip/usbip_ux.c
@@ -399,7 +399,7 @@ int usbip_ux_unlink(struct usbip_device *ud)
rcu_read_lock();
ux = rcu_dereference(ud->ux);
if (ux == NULL) {
-   pr_err("Unlink to unlinked ux.\n");
+   usbip_dbg_ux("Unlink to unlinked ux.\n");
rcu_read_unlock();
return -EINVAL;
}
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index a863a98..6c34075 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,7 +73,11 @@ struct vhci_unlink {
 };
 
 /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
-#define VHCI_NPORTS 8
+#ifndef VHCI_NPORTS
+#define VHCI_NPORTS USB_MAXCHILDREN
+#endif
+
+#define MAX_STATUS_NAME 16
 
 /* for usb_bus.hcpriv */
 struct vhci_hcd {
@@ -93,11 +98,16 @@ struct vhci_hcd {
struct vhci_device vdev[VHCI_NPORTS];
 };
 
-extern struct vhci_hcd *the_controller;
-extern const struct attribute_group dev_attr_group;
+extern int num_controllers;
+extern struct platform_device **the_pdevs;
+extern struct attribute_group dev_attr_group;
 
 /* vhci_hcd.c */
-void rh_port_connect(int rhport, enum usb_device_speed speed);
+void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
+
+/* vhci_sysfs.c */
+int vhci_init_attr_group(void);
+void vhci_finish_attr_group(void);
 
 /* vhci_rx.c */
 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
@@ -106,9 +116,14 @@ int vhci_rx_loop(void *data);
 /* vhci_tx.c */
 int vhci_tx_loop(void *data);
 
-static inline struct vhci_device *port_to_vdev(__u32 port)
+static inline __u32 port_to_rhport(__u32 port)
+{
+   return 

[PATCH v1 2/2] usbip: single thread event handler

2015-12-29 Thread Nobuo Iwata
This patch reduces number of event handling threads to one.

In existing implementation, event kernel threads are created for each 
port. The functions of the threads are terminationg connection and 
error handling. It's too expensive to have to each port.

With this patch, event handler is a single thread workqueue 
[usbip_event].

Both application (vhci) and device (stub) side are replaced.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/stub_dev.c |   3 +-
 drivers/usb/usbip/usbip_common.c |   7 ++
 drivers/usb/usbip/usbip_common.h |   4 +-
 drivers/usb/usbip/usbip_event.c  | 174 +++
 4 files changed, 141 insertions(+), 47 deletions(-)

diff --git a/drivers/usb/usbip/stub_dev.c b/drivers/usb/usbip/stub_dev.c
index d8d3add..9ec002d 100644
--- a/drivers/usb/usbip/stub_dev.c
+++ b/drivers/usb/usbip/stub_dev.c
@@ -380,7 +380,6 @@ err_files:
 err_port:
dev_set_drvdata(>dev, NULL);
usb_put_dev(udev);
-   kthread_stop_put(sdev->ud.eh);
 
busid_priv->sdev = NULL;
stub_device_free(sdev);
@@ -441,7 +440,7 @@ static void stub_disconnect(struct usb_device *udev)
}
 
/* If usb reset is called from event handler */
-   if (busid_priv->sdev->ud.eh == current)
+   if (usbip_in_eh(current))
return;
 
/* shutdown the current connection */
diff --git a/drivers/usb/usbip/usbip_common.c b/drivers/usb/usbip/usbip_common.c
index 963d8db..8c9fb19 100644
--- a/drivers/usb/usbip/usbip_common.c
+++ b/drivers/usb/usbip/usbip_common.c
@@ -807,12 +807,19 @@ EXPORT_SYMBOL_GPL(usbip_trx_ops);
 
 static int __init usbip_core_init(void)
 {
+   int ret;
+
pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
+   ret = usbip_init_eh();
+   if (ret) {
+   return ret;
+   }
return 0;
 }
 
 static void __exit usbip_core_exit(void)
 {
+   usbip_finish_eh();
return;
 }
 
diff --git a/drivers/usb/usbip/usbip_common.h b/drivers/usb/usbip/usbip_common.h
index 33b470c..0c72ecb 100644
--- a/drivers/usb/usbip/usbip_common.h
+++ b/drivers/usb/usbip/usbip_common.h
@@ -275,7 +275,6 @@ struct usbip_device {
struct task_struct *tcp_tx;
 
unsigned long event;
-   struct task_struct *eh;
wait_queue_head_t eh_waitq;
 
struct eh_ops {
@@ -321,10 +320,13 @@ void usbip_pad_iso(struct usbip_device *ud, struct urb 
*urb);
 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
 
 /* usbip_event.c */
+int usbip_init_eh(void);
+void usbip_finish_eh(void);
 int usbip_start_eh(struct usbip_device *ud);
 void usbip_stop_eh(struct usbip_device *ud);
 void usbip_event_add(struct usbip_device *ud, unsigned long event);
 int usbip_event_happened(struct usbip_device *ud);
+int usbip_in_eh(struct task_struct *task);
 
 static inline int interface_to_busnum(struct usb_interface *interface)
 {
diff --git a/drivers/usb/usbip/usbip_event.c b/drivers/usb/usbip/usbip_event.c
index 64933b9..22cb4d8 100644
--- a/drivers/usb/usbip/usbip_event.c
+++ b/drivers/usb/usbip/usbip_event.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,17 +20,68 @@
 
 #include 
 #include 
+#include 
+#include 
 
 #include "usbip_common.h"
 
-static int event_handler(struct usbip_device *ud)
+struct usbip_event {
+   struct list_head node;
+   struct usbip_device *ud;
+};
+
+static DEFINE_SPINLOCK(event_lock);
+static LIST_HEAD(event_list);
+
+void set_event(struct usbip_device *ud, unsigned long event)
+{
+   unsigned long flags;
+
+   spin_lock_irqsave(>lock, flags);
+   ud->event |= event;
+   spin_unlock_irqrestore(>lock, flags);
+}
+
+void unset_event(struct usbip_device *ud, unsigned long event)
 {
-   usbip_dbg_eh("enter\n");
+   unsigned long flags;
+
+   spin_lock_irqsave(>lock, flags);
+   ud->event &= ~event;
+   spin_unlock_irqrestore(>lock, flags);
+}
+
+static struct usbip_device *get_event(void)
+{
+   struct usbip_event *ue = NULL;
+   struct usbip_device *ud = NULL;
+   unsigned long flags;
 
-   /*
-* Events are handled by only this thread.
-*/
-   while (usbip_event_happened(ud)) {
+   spin_lock_irqsave(_lock, flags);
+   if (!list_empty(_list)) {
+   ue = list_first_entry(_list, struct usbip_event, node);
+   list_del(>node);
+   }
+   spin_unlock_irqrestore(_lock, flags);
+
+   if (ue) {
+   ud = ue->ud;
+   kfree(ue);
+   }
+   return ud;
+}
+
+struct task_struct *worker_context = NULL;
+
+static void event_handler(struct work_struct *work)
+{
+   struct usbip_device *ud;
+
+   if (worker_co

[PATCH v5 08/11] usbip: letting connection establishment replaceable

2015-12-29 Thread Nobuo Iwata
To introduce some application protocols like WebSocket, this patch 
allows to substitute connection establishment and termination. In 
combination with previous patch, both connection and transmission can 
be replaced.

usbip_connection_operations_t includes open and close operation. Open 
method returns usbip_sock_t which includes send, receive and close 
method. Then, transmission methods are replaced at the same time. 

Succeeding WebSocket patch uses this feature.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/usbip_common.c  | 10 ++
 tools/usb/usbip/libsrc/usbip_common.h  | 11 +++
 tools/usb/usbip/src/usbip.c|  1 +
 tools/usb/usbip/src/usbip_attach.c |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  6 +++---
 tools/usb/usbip/src/usbip_disconnect.c |  6 +++---
 tools/usb/usbip/src/usbip_list.c   |  6 +++---
 tools/usb/usbip/src/usbip_network.c|  9 +++--
 tools/usb/usbip/src/usbip_network.h|  3 +--
 9 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index dc0712c..54efa10 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -297,3 +297,13 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
sock->shutdown = shutdown;
 }
 
+usbip_connection_operations_t usbip_conn_ops = {NULL, NULL};
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock))
+{
+   usbip_conn_ops.open = open;
+   usbip_conn_ops.close = close;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 0dcbd99..07c411f 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -148,4 +148,15 @@ void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
void (*shutdown)(void *arg));
 
+typedef struct usbip_connection_operations {
+   usbip_sock_t *(*open)(char *host, char *port);
+   void (*close)(usbip_sock_t *sock);
+} usbip_connection_operations_t;
+
+extern usbip_connection_operations_t usbip_conn_ops;
+
+void usbip_conn_init(
+   usbip_sock_t *(*open)(char *host, char *port),
+   void (*close)(usbip_sock_t *sock));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 9d1468f..505e384 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -202,6 +202,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
optind = 0;
+   usbip_net_tcp_conn_init();
rc = run_command([i], argc, argv);
goto out;
}
diff --git a/tools/usb/usbip/src/usbip_attach.c 
b/tools/usb/usbip/src/usbip_attach.c
index ae0ca6e..c67e3d2 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -135,7 +135,7 @@ static int attach_device(char *host, char *busid)
int rc;
int rhport;
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err("tcp connect");
goto err_out;
@@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid)
usbip_ux_join(ux);
}
usbip_ux_cleanup();
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup();
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_out:
return -1;
 }
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
index 5f9505c..8dabd0b 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -150,7 +150,7 @@ static int connect_device(char *host, char *busid)
goto err_out;
}
 
-   sock = usbip_net_tcp_connect(host, usbip_port_string);
+   sock = usbip_conn_ops.open(host, usbip_port_string);
if (!sock) {
err("tcp connect");
goto err_unbind_device;
@@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid)
usbip_unbind_device(busid);
}
usbip_ux_cleanup();
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 
return 0;
 err_cleanup_ux:
usbip_ux_cleanup();
 err_close_conn:
-   usbip_net_tcp_close(sock);
+   usbip_conn_ops.close(sock);
 err_unbind_device:
usbip_unbind_device(busid);
 err_out:
diff --git a/tools/usb/usbip/src/usbip_discon

[PATCH v5 06/11] usbip: readme about user space URBs transmission

2015-12-29 Thread Nobuo Iwata
Addition to README regarding user space URBs transmission.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/README | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 74f4afb..6b61da5 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -98,6 +98,28 @@ Application-side: a machine runs an application software 
uses remote USB device.
- Unbind usbip-host.ko from the device.
 
 
+[Userspace Transmission]
+
+In usage shown above, once USB devices are imported or exported, USP/IP 
drivers send and receive URBs in kernel space. The usbip_ux.ko kernel module 
alternates the route to user space by forwarding USBs through USB/IP utilities 
(ie. usbip, usbipd, usbipa). When userspace transmission enabled, usbip attach 
and connect will continue executing until usbip detach or disconnect is exeuted.
+
+app:# insmod usbip-core.ko
+app:# insmod usbip-ux.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-ux.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip connect --remote  --busid 
+   - Continue running.
+   - Until disconnect command is executed in other terminal window.
+
+dev:# usbip disconnect --remote  --busid 
+   - Stops transission, quits connect command and disconnect device.
+
+
 [Example]
 ---
DEVICE SIDE
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 02/11] usbip: readme and manuals about exporting devices

2015-12-29 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' is changed also.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 +---
 tools/usb/usbip/doc/usbip.8  | 82 +---
 tools/usb/usbip/doc/usbipa.8 | 77 ++
 tools/usb/usbip/doc/usbipd.8 | 29 +-
 tools/usb/usbip/libsrc/vhci_driver.c |  2 +-
 6 files changed, 205 insertions(+), 57 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney <m...@muteddisk.com>
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device with  is now exportable to other hosts!
+   - Use `usbip unbind --busid ` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote 
+app:# usbip list --remote 
- List exported USB devices on the .
 
-client:# usbip attach --remote  --busid 1-2
+app:# usbip attach --remote  --busid 
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port 
+app:# usbip detach --port 
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote  --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device of  is connected to remote host!
+
+dev:# usbip disconnect --remote  --busid 
+   - The USB device with  is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show the devices attached to this client:
+Show the devices attach

[PATCH v5 07/11] usbip: letting send and receive replaceable

2015-12-29 Thread Nobuo Iwata
This patch allows to substitute send, receive and shutdown routines for 
both a) request/response PDUs among utilities and b) user space URBs 
transmission.

usbip_sock_t is introduced instead of sockfd. it includes function 
pointers of send/receive/shutdown routines, an argument for the 
routines, and a sockfd. The argument is needed for the routines. The 
sockfd is needed to bind connection to USB device.

Succeeding SSL and WebSocket patch use this feature.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/usbip_common.c  | 14 +
 tools/usb/usbip/libsrc/usbip_common.h  | 14 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 24 ++--
 tools/usb/usbip/libsrc/usbip_ux.h  |  4 +-
 tools/usb/usbip/src/usbip_attach.c | 30 +-
 tools/usb/usbip/src/usbip_connect.c| 30 +-
 tools/usb/usbip/src/usbip_disconnect.c | 26 -
 tools/usb/usbip/src/usbip_list.c   | 27 +
 tools/usb/usbip/src/usbip_network.c| 78 +++---
 tools/usb/usbip/src/usbip_network.h| 12 ++--
 tools/usb/usbip/src/usbipd.c   | 14 +++--
 tools/usb/usbip/src/usbipd_app.c   | 36 ++--
 tools/usb/usbip/src/usbipd_dev.c   | 40 ++---
 13 files changed, 214 insertions(+), 135 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_common.c 
b/tools/usb/usbip/libsrc/usbip_common.c
index ac73710..dc0712c 100644
--- a/tools/usb/usbip/libsrc/usbip_common.c
+++ b/tools/usb/usbip/libsrc/usbip_common.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #include 
@@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, 
uint8_t class,
 
snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, 
subclass, protocol);
 }
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg))
+{
+   sock->fd = fd;
+   sock->arg = arg;
+   sock->send = send;
+   sock->recv = recv;
+   sock->shutdown = shutdown;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_common.h 
b/tools/usb/usbip/libsrc/usbip_common.h
index 15fe792..0dcbd99 100644
--- a/tools/usb/usbip/libsrc/usbip_common.h
+++ b/tools/usb/usbip/libsrc/usbip_common.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  */
 
 #ifndef __USBIP_COMMON_H
@@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, 
uint16_t vendor,
 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
   uint8_t subclass, uint8_t protocol);
 
+typedef struct usbip_sock {
+   int fd;
+   void *arg;
+   ssize_t (*send)(void *arg, void *buf, size_t len);
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all);
+   void (*shutdown)(void *arg);
+} usbip_sock_t;
+
+void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg,
+   ssize_t (*send)(void *arg, void *buf, size_t len),
+   ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all),
+   void (*shutdown)(void *arg));
+
 #endif /* __USBIP_COMMON_H */
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
index 3ac45a72..aa3d863 100644
--- a/tools/usb/usbip/libsrc/usbip_ux.c
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg)
char buf[BLEN];
 
while(good) {
-   received = recv(ux->sockfd, buf, BLEN, 0);
+   if (ux->sock->recv) {
+   received = ux->sock->recv(ux->sock->arg, buf, BLEN, 0);
+   } else {
+   received = recv(ux->sock->fd, buf, BLEN, 0);
+   }
if (received == 0) {
dbg("connection closed on sock:%p", ux->kaddr.sock);
break;
@@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg)
break;
}
dump_buff(buf, reads, "ux sending");
-   sent = send(ux->sockfd, buf, reads, 0);
+   if (ux->sock->send) {
+   sent = ux->sock->send(ux->sock->arg, buf, reads);
+   } else {
+   sent = send(ux->sock->fd, buf, reads, 0);
+   }
if (sent < 0) {
dbg("connection closed on sock:%p", ux->kaddr.sock);
break;
@@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg)
}
}
dbg("end of ux-tx for sock:%p", ux->kaddr.sock);
-   shutdown(ux->sockfd, SHUT_RDWR);
+   if (ux->sock->shutdown) {
+   ux->sock->shutd

[PATCH v5 01/11] usbip: exporting devices

2015-12-29 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid 
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote 
// List importable USB devices from the .
app:# usbip attach --remote  --busid 
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote  --busid 
// Export a device to .

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets have been 
defined in a header file (usbip_network.h) but it not yet used.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.
Also, vendor/product name converion is added to port commnad as same as 
list command.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/names.c |   4 +-
 tools/usb/usbip/libsrc/usbip_host_driver.c |  16 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |  21 +-
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 233 +++
 tools/usb/usbip/src/usbipd_app.c   | 240 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 20 files changed, 1151 insertions(+), 302 deletions(-)

diff --git a/tools/usb/usbip/libsrc/names.c b/tools/usb/usbip/libsrc/names.c
index 81ff852..7d65d28 100644
--- a/tools/usb/usbip/libsrc/names.c
+++ b/tools/usb/usbip/libsrc/names.c
@@ -162,7 +162,7 @@ struct pool {
void *mem;
 };
 
-static struct pool *pool_head;
+static struct pool *pool_head = NULL;
 
 static void *my_malloc(size_t size)
 {
@@ -201,6 +201,8 @@ void names_free(void)
pool = pool->next;
free(tmp);
}
+
+   pool_head = NULL;
 }
 
 static int new_vendor(const char *name, u_int16_t vendorid)
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..de5541a 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,19 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, _driver->edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
+   return edev;
+   }
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/

[PATCH v5 11/11] usbip: USB over WebSocket

2015-12-29 Thread Nobuo Iwata
This patch adds utilities transmit packets via WebSocket protocol.
WebSocket version of utilities as following.
  usbws : command
  usbwsa : application-side daemon
  usbwsd : device-side daemon

The command supports all sub-command (ie. list, connect, disconnect, 
port, bind, unbind, attach and detach). It uses --url option to specify 
remote address and port number. 

Implementation of this patch depends on Poco C++ 
(http://pocoproject.org/).

The tree is shown below.
  tools
+--usb
 +--usbip
  +--src : command, daemons and their core libraries 
  +--libsrc : common library for command and daemon
  +--websocket : new! WebSocket implementations
   +--poco : new! implementation with Poco C++

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/websocket/INSTALL | 237 ++
 tools/usb/usbip/websocket/Makefile.am |   3 +
 tools/usb/usbip/websocket/README  | 184 
 tools/usb/usbip/websocket/autogen.sh  |   9 +
 tools/usb/usbip/websocket/cleanup.sh  |  12 +
 tools/usb/usbip/websocket/configure.ac|  55 +++
 tools/usb/usbip/websocket/doc/usbws.8 | 192 
 tools/usb/usbip/websocket/doc/usbwsa.8| 101 +
 tools/usb/usbip/websocket/doc/usbwsd.8| 109 +
 tools/usb/usbip/websocket/poco/Makefile.am|  18 +
 .../usb/usbip/websocket/poco/USBWSCommand.cpp | 410 ++
 tools/usb/usbip/websocket/poco/USBWSCommand.h |  99 +
 .../usb/usbip/websocket/poco/USBWSDaemon.cpp  | 228 ++
 tools/usb/usbip/websocket/poco/USBWSDaemon.h  |  80 
 .../websocket/poco/USBWSRequestHandler.cpp|  90 
 .../websocket/poco/USBWSRequestHandler.h  |  49 +++
 .../poco/USBWSRequestHandlerFactory.cpp   |  49 +++
 .../poco/USBWSRequestHandlerFactory.h |  48 ++
 tools/usb/usbip/websocket/poco/USBWSUtil.h|  52 +++
 .../usbip/websocket/poco/USBWSWebSocket.cpp   | 204 +
 .../usb/usbip/websocket/poco/USBWSWebSocket.h |  69 +++
 21 files changed, 2298 insertions(+)

diff --git a/tools/usb/usbip/websocket/INSTALL 
b/tools/usb/usbip/websocket/INSTALL
new file mode 100644
index 000..d3c5b40
--- /dev/null
+++ b/tools/usb/usbip/websocket/INSTALL
@@ -0,0 +1,237 @@
+Installation Instructions
+*
+
+Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+2006, 2007 Free Software Foundation, Inc.
+
+This file is free documentation; the Free Software Foundation gives
+unlimited permission to copy, distribute and modify it.
+
+Basic Installation
+==
+
+Briefly, the shell commands `./configure; make; make install' should
+configure, build, and install this package.  The following
+more-detailed instructions are generic; see the `README' file for
+instructions specific to this package.
+
+   The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation.  It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions.  Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, and a
+file `config.log' containing compiler output (useful mainly for
+debugging `configure').
+
+   It can also use an optional file (typically called `config.cache'
+and enabled with `--cache-file=config.cache' or simply `-C') that saves
+the results of its tests to speed up reconfiguring.  Caching is
+disabled by default to prevent problems with accidental use of stale
+cache files.
+
+   If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release.  If you are using the cache, and at
+some point `config.cache' contains results you don't want to keep, you
+may remove or edit it.
+
+   The file `configure.ac' (or `configure.in') is used to create
+`configure' by a program called `autoconf'.  You need `configure.ac' if
+you want to change it or regenerate `configure' using a newer version
+of `autoconf'.
+
+The simplest way to compile this package is:
+
+  1. `cd' to the directory containing the package's source code and type
+ `./configure' to configure the package for your system.
+
+ Running `configure' might take a while.  While running, it prints
+ some messages telling which features it is checking for.
+
+  2. Type `make' to compile the package.
+
+  3. Optionally, type `make check' to run any self-tests that come with
+ the package.
+
+  4. Type `make install' to install the programs and any data files and
+ documentation.
+
+  5. You can remove the program binaries and object files from the
+ source code directory by typing `make clean'.  T

[PATCH v5 03/11] usbip: safe completion against usb_kill_urb()

2015-12-29 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev->ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

The critial condition will happen by unbind command before detach, 
broken connection in connect command and disconnect command. 

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(>priv_lock, flags);
-   if (priv->unlinking) {
+   if (sdev->ud.tcp_socket == NULL) {
+   dev_info(>dev->dev, "discard a urb for closed connection");
+   } else if (priv->unlinking) {
stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v5 04/11] usbip: kernel module for userspace URBs transmission

2015-12-29 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after the preparation, URBs are 
transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket 
and so on, the driver, usbip_ux.ko, forwards URBs to USB/IP user space 
utilities. It's just like fuse driver for user space file system. 
Then, utilities transfer URBs in user space.

To do so, usbip_trx_ops makes send/receive functions pluggable. 
kernel_sendmsg() and kernel_recvmsg() for kernel mode transfer can be 
substituted by read/write methods to user space utilities.

In the absence of usbip_ux.ko, original kernel space transferring is 
valid. usbip_ux.ko replaces usbip_trx_ops in its init routine.

A) Original - kernel space URBs transfer

User+---+   1) import/export   +---+
space   |uspipd,|<>|usbip, |
|usbip  |  |usbipa |
+---+---+  +---+---+
|  |
2)Set sockfd|  |2)Set sockfd
  thru sysfs|  |  thru sysfs
V  V
Kernel  +---+4)URBs+---+
space   |usbip  |<>|vhci   |
|host.ko|  |hcd.ko |
+---+  +---+
3)link to kernel trx_ops   3)link to kernel trx_ops

B) New - user space URBs transfer

User+---+1)import/export   +---+
space   |uspipd,|<>|usbip, |
+---|usbip  |<>|usbipa |---+
2)Set sockfd|+--+---+6)URBs+---+--+|2)Set sockfd
  thru ioctl||  ^ ^   ||  thru ioctl
  (right)   ||  |5)read/write 5)read/write|   ||  (left)
3)Set sockfd||  +---+  +--+   ||3)Set Sockfd
  thru sysfs|+---+  | /dev/usbip-ux|  +---+|  thru sysfs
  (left)VV  V  V  VV  (right)
Kernel  +---+   +---+  +---+   +---+
space   |usbip  |<->|usbip  |  |usbip  |<->|vhci   |
|host.ko|5)send |ux.ko  |  |ux.ko  |5)send |hcd.ko |
+---+  recv +---+  +---+  recv +---+
4)link to user trx_ops 4)link to user trx_ops

Kernel module configuration for the driver will be shown as below.

USB/IP support  USBIP_CORE
+-- USB/IP userspace URB transmission   USBIP_UX
+-- VHCI hcdUSBIP_HCD
+-- Debug messages for USB/IP   USBIP_DEBUG

The reason why the userspace transmission oter than usbfs is needed.
a) Application(vhci_hcd)-side is needed
Usbfs provides functions to control device. So it can be applied to 
device(usbip_host)-side but not to application(vhci_hcd)-side.
b) Use existing kernel modules as-is
To implement same functionality in userspace with interface like usbfs, 
almost same code to kernel modules must be copied to userspcae. Also 
interfaces between kernel modules and utiities (sysfs) should be 
changed to new one. So utilities must be modified according to the new 
interface too. Modifications to existing code by this patch is small 
and usbip_ux.c handles major part of userspace transmission.

To get diff include/uapi/linux/usbip_ux.h, I used modified dontdiff.txt.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/Kconfig|  10 +
 drivers/usb/usbip/Makefile   |   3 +
 drivers/usb/usbip/stub_dev.c |  16 +-
 drivers/usb/usbip/stub_rx.c  |   3 +-
 drivers/usb/usbip/stub_tx.c  |   5 +-
 drivers/usb/usbip/usbip_common.c |  79 +++-
 drivers/usb/usbip/usbip_common.h |  29 +-
 drivers/usb/usbip/usbip_ux.c | 602 +++
 drivers/usb/usbip/usbip_ux.h |  82 +
 drivers/usb/usbip/vhci_hcd.c |   9 +-
 drivers/usb/usbip/vhci_rx.c  |   3 +-
 drivers/usb/usbip/vhci_sysfs.c   |  40 +-
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 include/uapi/linux/usbip_ux.h|  39 ++
 14 files changed, 869 insertions(+), 57 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index bd99e9e..e847d06 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -14,6 +14,16 @@ config USBIP_CORE
 
  If unsure, say N.
 
+config USBIP_UX
+   tristate "USB/IP userspace URB transmission"
+   depends on USBIP_CORE
+   ---help---
+ This moves USB/IP URB transmission to userspace
+ to apply SSL, WebSocket and etc.
+
+ To compile this driver as a module, choose M here: the
+ module will be called usbip-ux.

[PATCH v5 10/11] usbip: added const qualifier to arguments of some functions

2015-12-29 Thread Nobuo Iwata
This patch adds 'const' qualifier to 'char*' arguments of library 
interfaces to make acceptable std::string.c_str(). Essentially, these 
qualifiers are better to be used even if not to use C++. Although, I 
just added to functions related to previous patch.

Also, it changes C++ reserved words (ie. new and class) in list.h.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/list.h  | 24 --
 tools/usb/usbip/libsrc/names.c | 15 +++---
 tools/usb/usbip/libsrc/usbip_common.c  | 16 +++
 tools/usb/usbip/libsrc/usbip_common.h  |  6 +++---
 tools/usb/usbip/libsrc/usbip_host_driver.c | 10 +
 tools/usb/usbip/libsrc/usbip_host_driver.h |  2 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 11 ++
 tools/usb/usbip/libsrc/vhci_driver.h   |  6 --
 tools/usb/usbip/src/usbip.h| 16 ---
 tools/usb/usbip/src/usbip_attach.c |  4 ++--
 tools/usb/usbip/src/usbip_bind.c   |  6 +++---
 tools/usb/usbip/src/usbip_connect.c|  4 ++--
 tools/usb/usbip/src/usbip_detach.c |  2 +-
 tools/usb/usbip/src/usbip_disconnect.c |  5 +++--
 tools/usb/usbip/src/usbip_list.c   |  7 +++
 tools/usb/usbip/src/usbip_netconn.c|  2 +-
 tools/usb/usbip/src/usbip_network.c| 12 +++
 tools/usb/usbip/src/usbip_unbind.c |  2 +-
 tools/usb/usbip/src/usbipd.c   |  4 ++--
 tools/usb/usbip/src/usbipd.h   |  2 +-
 tools/usb/usbip/src/usbipd_app.c   |  9 
 tools/usb/usbip/src/usbipd_dev.c   |  2 +-
 tools/usb/usbip/src/utils.c|  2 +-
 tools/usb/usbip/src/utils.h|  2 +-
 24 files changed, 94 insertions(+), 77 deletions(-)

diff --git a/tools/usb/usbip/libsrc/list.h b/tools/usb/usbip/libsrc/list.h
index 5eaaa78..b46a98f 100644
--- a/tools/usb/usbip/libsrc/list.h
+++ b/tools/usb/usbip/libsrc/list.h
@@ -36,14 +36,14 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
  * This is only for internal list manipulation where we know
  * the prev/next entries already!
  */
-static inline void __list_add(struct list_head *new,
+static inline void __list_add(struct list_head *neo,
  struct list_head *prev,
  struct list_head *next)
 {
-   next->prev = new;
-   new->next = next;
-   new->prev = prev;
-   prev->next = new;
+   next->prev = neo;
+   neo->next = next;
+   neo->prev = prev;
+   prev->next = neo;
 }
 
 /**
@@ -54,9 +54,9 @@ static inline void __list_add(struct list_head *new,
  * Insert a new entry after the specified head.
  * This is good for implementing stacks.
  */
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_head *neo, struct list_head *head)
 {
-   __list_add(new, head, head->next);
+   __list_add(neo, head, head->next);
 }
 
 /*
@@ -73,8 +73,8 @@ static inline void __list_del(struct list_head * prev, struct 
list_head * next)
 }
 
 #define POISON_POINTER_DELTA 0
-#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
-#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
+#define LIST_POISON1  ((char *) 0x00100100 + POISON_POINTER_DELTA)
+#define LIST_POISON2  ((char *) 0x00200200 + POISON_POINTER_DELTA)
 
 /**
  * list_del - deletes entry from list.
@@ -90,8 +90,8 @@ static inline void __list_del_entry(struct list_head *entry)
 static inline void list_del(struct list_head *entry)
 {
__list_del(entry->prev, entry->next);
-   entry->next = LIST_POISON1;
-   entry->prev = LIST_POISON2;
+   entry->next = (struct list_head *)LIST_POISON1;
+   entry->prev = (struct list_head *)LIST_POISON2;
 }
 
 /**
@@ -120,7 +120,9 @@ static inline void list_del(struct list_head *entry)
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
 
+#ifndef offsetof
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
 
 /**
  * container_of - cast a member of a structure out to the containing structure
diff --git a/tools/usb/usbip/libsrc/names.c b/tools/usb/usbip/libsrc/names.c
index 7d65d28..656005f 100644
--- a/tools/usb/usbip/libsrc/names.c
+++ b/tools/usb/usbip/libsrc/names.c
@@ -23,6 +23,8 @@
  *
  * Copyright (C) 2005 Takahiro Hirofuchi
  *     - names_deinit() is added.
+ * Copyright (C) 2015 Nobuo Iwata
+ * - some modifications for portability.
  *
  */
 
@@ -38,7 +40,6 @@
 #include 
 
 #include "names.h"
-#include "usbip_common.h"
 
 struct vendor {
struct vendor *next;
@@ -52,8 +53,8 @@ struct product {
char name[1];
 };
 
-struct class {
-   struct class *next;
+struct clazz {
+   struct clazz *next;
u_int8_t classid

[PATCH v5 05/11] usbip: tools for userspace URBs transmission

2015-12-29 Thread Nobuo Iwata
Originally, USB/IP transmits requests and response PDUs for preparation 
to transfer URBs in user space, after completion of the preparation, 
URBs are transmitted in kernel space.

To make easy to introduce application network protocols like WebSocket, 
the driver, usbip_ux.ko, forwards URBs to USB/IP user space utilities. 
Then, the utilities exchange URBs in userspace.

To do so, tools/usb/usbip/libsrc/usbip_ux.c includes tx/rx threads to 
read/wite URBs from usbip_ux.ko and transfer URBs in userspace. When 
usbip_ux.ko is installed, /dev/usbip-ux will be found, then the threads 
will be started. Otherwise, threads will not be started and original 
kernel space transmission is valid.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/Makefile.am |   1 +
 tools/usb/usbip/libsrc/usbip_ux.c  | 244 +
 tools/usb/usbip/libsrc/usbip_ux.h  |  30 +++
 tools/usb/usbip/libsrc/vhci_driver.c   |  10 +-
 tools/usb/usbip/src/usbip_attach.c |  30 ++-
 tools/usb/usbip/src/usbip_connect.c|  18 +-
 tools/usb/usbip/src/usbip_disconnect.c |  11 +-
 tools/usb/usbip/src/usbipd.c   |   5 +-
 tools/usb/usbip/src/usbipd_app.c   |  14 ++
 tools/usb/usbip/src/usbipd_dev.c   |  30 ++-
 10 files changed, 370 insertions(+), 23 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am 
b/tools/usb/usbip/libsrc/Makefile.am
index 7c8f8a4..5754425 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -5,4 +5,5 @@ libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 lib_LTLIBRARIES := libusbip.la
 libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h 
\
   usbip_common.c usbip_common.h vhci_driver.c 
vhci_driver.h \
+  usbip_ux.c usbip_ux.h \
   sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/libsrc/usbip_ux.c 
b/tools/usb/usbip/libsrc/usbip_ux.c
new file mode 100644
index 000..3ac45a72
--- /dev/null
+++ b/tools/usb/usbip/libsrc/usbip_ux.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *
+ * USB/IP URB transmission in userspace.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "usbip_common.h"
+#include "usbip_ux.h"
+
+#undef  PROGNAME
+#define PROGNAME "libusbip"
+
+#define DEVNAME "/dev/" USBIP_UX_DEV_NAME
+
+#define BLEN 1500
+
+#ifdef DEBUG
+void dump_buff(char *buff, size_t bufflen, char *label)
+{
+#define DUMP_BUFF 80
+#define WORK_BUFF 16
+   size_t i = 0, j;
+   char b[DUMP_BUFF];
+   char bb[WORK_BUFF];
+
+   dbg("dump %s for %zd bytes", label, bufflen);
+   for(i=0;i<bufflen;i++) {
+   j = i % 16;
+   if (j == 0) {
+   b[0] = 0;
+   sprintf(bb, "%04zx  %02x", i, *(buff+i) & 0xff);
+   } else if (j == 8) {
+   sprintf(bb, "  %02x", *(buff+i) & 0xff);
+   } else {
+   sprintf(bb, " %02x", *(buff+i) & 0xff);
+   }
+   strncat(b, bb, WORK_BUFF);
+   if (j == 15 || i == bufflen-1) {
+   dbg("%s", b);
+   }
+   }
+}
+#else
+#define dump_buff(buff, bufflen, label) while(0){}
+#endif
+
+static void *usbip_ux_rx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t received, written, ret;
+   int good = 1;
+   char buf[BLEN];
+
+   while(good) {
+   received = recv(ux->sockfd, buf, BLEN, 0);
+   if (received == 0) {
+   dbg("connection closed on sock:%p", ux->kaddr.sock);
+   break;
+   } else if (received < 0) {
+   dbg("receive error on sock:%p", ux->kaddr.sock);
+   break;
+   }
+   dump_buff(buf, received, "ux received");
+   written = 0;
+   while(written < received) {
+   ret = write(ux->devfd, buf+written, received-written);
+   if (ret < 0) {
+   dbg("write error for sock:%p", ux->kaddr.sock);
+   good = 0;
+   break;
+   }
+   written += ret;
+   }
+   }
+   dbg("end of ux-rx for sock:%p", ux->kaddr.sock);
+   ioctl(ux->devfd, USBIP_UX_IOCINTR);
+   return 0;
+}
+
+static void *usbip_ux_tx(void *arg)
+{
+   usbip_ux_t *ux = (usbip_ux_t*)arg;
+   ssize_t sent, reads;
+   char buf[BLEN];
+
+   for(;;) {
+   reads = read(ux->devfd, buf, BLEN);
+   if (reads == 0) {
+#ifdef DEBUG
+   dbg("end of read on sock:%p

[PATCH v1 0/2] usbip: vhci number of ports extension

2015-12-29 Thread Nobuo Iwata
This series of patches extends number of ports limitaion in application 
(vhci) side.

This series conatins 2 patches.
1/2:
Extends number of ports using multiple host controllers.
'num_controllers=N' module parameter denotes the number.
The default is 1.
Number of ports per controller are extended from 8 to 
USB_MAXCHILDREN(31).
It can be altered with -DVHCI_NPORTS=n at compile time.
2/2:
Event handling threads are used to be created for each port.
This patch aggregates them to one thread.
Rewritten with workqueue.

Assumed use case is a system that service in internet serves 
distributes devices in home or office. In the use case, application 
side might be needed to support more ports than 31.

Home/SOHO/Enterprise Intranet/Internet
   ++
 +--+   +--+   |Service |
+|device|---|Linux |---|on  |
|+--+   +--+   |Linux   |
+--+   controller  ++
ex)
Device  Service
 sensors ... environment analysis
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

To increase number of ports, existing implementation has an overhead 
that event handing kernel threads are started for each port. The second 
patch eliminates the overhead.

NOTE: This series depends on "USB/IP over WebSocket" patches.

*** BLURB HERE ***

Nobuo Iwata (2):
  usbip: vhci number of ports extension
  usbip: single thread event handler

 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/stub_dev.c |   3 +-
 drivers/usb/usbip/usbip_common.c |   7 +
 drivers/usb/usbip/usbip_common.h |   4 +-
 drivers/usb/usbip/usbip_event.c  | 174 ++---
 drivers/usb/usbip/usbip_ux.c |   2 +-
 drivers/usb/usbip/vhci.h |  41 +-
 drivers/usb/usbip/vhci_hcd.c | 259 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 298 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 548 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  38 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  56 ++-
 15 files changed, 916 insertions(+), 559 deletions(-)

-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 0/9] usbip: exporting devices

2016-06-12 Thread Nobuo Iwata
nd internally. With vUDC, they do not execute bind and unbind. 
They are done by UDC interface.

4. Security consideration

In new operation, when the daemon is running, SSL or Secure WebSocket 
tunneling poxy can reject unauthorized access.

5. Mixed usage

Both existing and new way work in same machines simultaneously. Status 
of devices and ports are controlled in stub and vhci driver.

6. Wording

Adding the new operation, some inconsistnecies in wording are appeared 
in documentation, function name, etc. If needed, they are fixed.

'export' is used for bind and 'exported' is used for bound. They are 
changed to 'make importable' and 'imported' respectively. The words not 
new. For example, in the output of port operation, 'imported devices' 
is already used. They are sorted out.

'client' and 'server' are switched between existing and new operation. 
So, words 'device-side' and 'application-side' are used in 
documentations as needed for clarity. 

---
Version information

This series is divided from "USB/IP over WebSocket" patch set.
Rest of the set will be sent as another series.

v8)
# Divided into smaller patches.
# Excluded low-related patches.
# Improved change log.
# Changed info level logs in usbip_ux.c to debug level logs.
# Added options to vUDC.
# Tested with vUDC. 

v7)
# Removed userspace transmission and WebSocket command/daemon.
# Fixed checkpatch errors and warnings.

v6)
# Added __rcu annotation to a RCU pointer to clear sparse warnings.
# Corrected a copy to RCU pointer with rcu_rcu_assign_pointer(). 
# Added __user annotations to arguments of read/write method. 
# Added static to some functions which are not called from other files.
# Removed unnecessary EXPORT_SYMBOLs.

v5)
# Added vendor/pruduct name conversion to port command.
# Put initial value to pool_head in name.c.
# Fixed list command exception when host option is omitted.
# Fixed exception in case gai_strerror() returns NULL.
# Fixed WebSocket connection close via proxy.
# Fixed to stop WebSocket ping-pong on connection close.
# Removed redundant usbipd daemon option.
# Removed redundant SSL code had not been deleted.
# Removed an unused local variable in WebSocket code.
# Modified C++ reserved word in names.c as same as headers.

v4)
# Fixed regression of usbip list --remote

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as .
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Remved unnecessary COPYING and AUTHORS fil from tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

Thank you,

Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
//

*** BLURB HERE ***

Nobuo Iwata (9):
  usbip: exporting devices: modifications to network header
  usbip: exporting devices: modifications to host side libraries
  usbip: exporting devices: new connect operation
  usbip: exporting devices: new disconnect operation
  usbip: exporting devices: modifications to daemon
  usbip: exporting devices: modifications to attach and detach
  usbip: exporting devices: new application-side daemon
  usbip: exporting devices: change to usbip_list.c
  usbip: exporting devices: chage to documenattion

 Documentation/usb/usbip_protocol.txt   | 204 ++--
 tools/usb/usbip/Makefile.am|   2 +-
 tools/usb/usbip/README |  70 --
 tools/usb/usbip/doc/usbip.8| 136 +--
 tools/usb/usbip/doc/usbipa.8   |  79 +++
 tools/usb/usbip/doc/usbipd.8   |  37 +--
 tools/usb/usbip/libsrc/usbip_host_common.c |   6 +-
 tools/usb/usbip/libsrc/usbip_host_common.h |   8 +-
 tools/usb/usbip/libsrc/vhci_driver.c   | 118 --
 tools/usb/usbip/libsrc/vhci_driver.h   |   7 +-
 tools/usb/usbip/src/Makefile.am|  12 +-
 tools/usb/usbip/src/usbip.c|  15 +-
 tools/usb/usbip/src/usbip.h|  10 +-
 tools/usb/usbip/src/usbip_attach.c |  50 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 228 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 215 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 247 +++-
 tools/usb/usbip/src/usbipd.h   |  39 
 tools/usb/usbip/src/usbipd_app.c   | 243 +++
 tools/usb/usbip/src/usbipd_dev.c   | 256

[PATCH v8 4/9] usbip: exporting devices: new disconnect operation

2016-06-12 Thread Nobuo Iwata
New disconnect operation.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/src/Makefile.am|   2 +-
 tools/usb/usbip/src/usbip.c|   6 +
 tools/usb/usbip/src/usbip.h|   2 +
 tools/usb/usbip/src/usbip_disconnect.c | 215 +
 4 files changed, 224 insertions(+), 1 deletion(-)

diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index 0947476..42760c3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -7,6 +7,6 @@ sbin_PROGRAMS := usbip usbipd
 usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
-usbip_connect.c
+usbip_connect.c usbip_disconnect.c
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbip_network.c
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index 584d7d5..f0e9e06 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -83,6 +83,12 @@ static const struct command cmds[] = {
.usage = usbip_connect_usage
},
{
+   .name  = "disconnect",
+   .fn= usbip_disconnect,
+   .help  = "Disconnect a USB device from a remote computer",
+   .usage = usbip_disconnect_usage
+   },
+   {
.name  = "list",
.fn= usbip_list,
.help  = "List exportable or local USB devices",
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index f365353..a8cbd16 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -32,6 +32,7 @@ int usbip_bind(int argc, char *argv[]);
 int usbip_unbind(int argc, char *argv[]);
 int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
+int usbip_disconnect(int argc, char *argv[]);
 
 void usbip_attach_usage(void);
 void usbip_detach_usage(void);
@@ -39,6 +40,7 @@ void usbip_list_usage(void);
 void usbip_bind_usage(void);
 void usbip_unbind_usage(void);
 void usbip_connect_usage(void);
+void usbip_disconnect_usage(void);
 
 int usbip_bind_device(char *busid);
 int usbip_unbind_device(char *busid);
diff --git a/tools/usb/usbip/src/usbip_disconnect.c 
b/tools/usb/usbip/src/usbip_disconnect.c
new file mode 100644
index 000..8155384
--- /dev/null
+++ b/tools/usb/usbip/src/usbip_disconnect.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
+ *   2005-2007 Takahiro Hirofuchi
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "usbip_host_driver.h"
+#include "usbip_host_common.h"
+#include "usbip_device_driver.h"
+#include "usbip_common.h"
+#include "usbip_network.h"
+#include "usbip.h"
+
+static struct usbip_host_driver *driver = _driver;
+
+static const char usbip_disconnect_usage_string[] =
+   "usbip disconnect \n"
+   "-r, --remote=Address of a remote computer\n"
+   "-b, --busid=Bus ID of a device to be disconnected\n"
+   "-d, --device   Run with an alternate driver, e.g. vUDC\n";
+
+void usbip_disconnect_usage(void)
+{
+   printf("usage: %s", usbip_disconnect_usage_string);
+}
+
+static int send_unexport_device(int sockfd, struct usbip_usb_device *udev)
+{
+   int rc;
+   struct op_unexport_request request;
+   struct op_unexport_reply   reply;
+   uint16_t code = OP_REP_UNEXPORT;
+
+   memset(, 0, sizeof(request));
+   memset(, 0, sizeof(reply));
+
+   /* send a request */
+   rc = usbip_net_send_op_common(sockfd, OP_REQ_UNEXPORT, 0);
+   if (rc < 0) {
+   err("send op_common");
+   return -1;
+   }
+
+   memcpy(, udev, sizeof(struct usbip_usb_device));
+
+   PACK_OP_UNEXPORT_REQUEST(0, );
+
+   rc = usbip_net_send(sockfd, (void *) , sizeof(request));
+   if (rc < 0) {
+   err("send op_export_request");
+ 

[PATCH v8 6/9] usbip: exporting devices: modifications to attach and detach

2016-06-12 Thread Nobuo Iwata
Refactoring to attach and detatch operation. Common parts to new 
application(vhci)-side daemon are moved to libsrc/vhci_driver.c. 

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/vhci_driver.c | 99 
 tools/usb/usbip/libsrc/vhci_driver.h |  6 +-
 tools/usb/usbip/src/usbip_attach.c   | 50 ++
 tools/usb/usbip/src/usbip_detach.c   | 13 ++--
 4 files changed, 100 insertions(+), 68 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..b7ca63d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2005-2007 Takahiro Hirofuchi
  */
 
 #include "usbip_common.h"
@@ -7,6 +8,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include "sysfs_utils.h"
 
 #undef  PROGNAME
@@ -215,6 +218,25 @@ static int read_record(int rhport, char *host, unsigned 
long host_len,
return 0;
 }
 
+#define OPEN_HC_MODE_FIRST 0
+#define OPEN_HC_MODE_REOPEN1
+
+static int open_hc_device(int mode)
+{
+   if (mode == OPEN_HC_MODE_REOPEN)
+   udev_device_unref(vhci_driver->hc_device);
+
+   vhci_driver->hc_device =
+   udev_device_new_from_subsystem_sysname(udev_context,
+  USBIP_VHCI_BUS_TYPE,
+  USBIP_VHCI_DRV_NAME);
+   if (!vhci_driver->hc_device) {
+   err("udev_device_new_from_subsystem_sysname failed");
+   return -1;
+   }
+   return 0;
+}
+
 /* -- */
 
 int usbip_vhci_driver_open(void)
@@ -227,28 +249,21 @@ int usbip_vhci_driver_open(void)
 
vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
 
-   /* will be freed in usbip_driver_close() */
-   vhci_driver->hc_device =
-   udev_device_new_from_subsystem_sysname(udev_context,
-  USBIP_VHCI_BUS_TYPE,
-  USBIP_VHCI_DRV_NAME);
-   if (!vhci_driver->hc_device) {
-   err("udev_device_new_from_subsystem_sysname failed");
-   goto err;
-   }
+   if (open_hc_device(OPEN_HC_MODE_FIRST))
+   goto err_free_driver;
 
vhci_driver->nports = get_nports();
 
dbg("available ports: %d", vhci_driver->nports);
 
if (refresh_imported_device_list())
-   goto err;
+   goto err_unref_device;
 
return 0;
 
-err:
+err_unref_device:
udev_device_unref(vhci_driver->hc_device);
-
+err_free_driver:
if (vhci_driver)
free(vhci_driver);
 
@@ -277,7 +292,8 @@ void usbip_vhci_driver_close(void)
 
 int usbip_vhci_refresh_device_list(void)
 {
-
+   if (open_hc_device(OPEN_HC_MODE_REOPEN))
+   goto err;
if (refresh_imported_device_list())
goto err;
 
@@ -409,3 +425,58 @@ int usbip_vhci_imported_device_dump(struct 
usbip_imported_device *idev)
 
return 0;
 }
+
+#define MAX_BUFF 100
+int usbip_vhci_create_record(char *host, char *port, char *busid, int rhport)
+{
+   int fd;
+   char path[PATH_MAX+1];
+   char buff[MAX_BUFF+1];
+   int ret;
+
+   ret = mkdir(VHCI_STATE_PATH, 0700);
+   if (ret < 0) {
+   /* if VHCI_STATE_PATH exists, then it better be a directory */
+   if (errno == EEXIST) {
+   struct stat s;
+
+   ret = stat(VHCI_STATE_PATH, );
+   if (ret < 0)
+   return -1;
+   if (!(s.st_mode & S_IFDIR))
+   return -1;
+   } else
+   return -1;
+   }
+
+   snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
+
+   fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
+   if (fd < 0)
+   return -1;
+
+   snprintf(buff, MAX_BUFF, "%s %s %s\n",
+   host, port, busid);
+
+   ret = write(fd, buff, strlen(buff));
+   if (ret != (ssize_t) strlen(buff)) {
+   close(fd);
+   return -1;
+   }
+
+   close(fd);
+
+   return 0;
+}
+
+int usbip_vhci_delete_record(int rhport)
+{
+   char path[PATH_MAX+1];
+
+   snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
+
+   remove(path);
+   rmdir(VHCI_STATE_PATH);
+
+   return 0;
+}
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h 
b/tools/usb/usbip/libsrc/vhci_driver.h
index fa2316c..f955ada 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/too

[PATCH v8 1/9] usbip: exporting devices: modifications to network header

2016-06-12 Thread Nobuo Iwata
Modification to export and un-export response in 
tools/usb/usbip/src/usbip_network.h. It just changes return code type 
from int to uint32_t as same as other responses.

Added export and un-export request/response to 
Documentation/usb/usbip_protocol.txt.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 Documentation/usb/usbip_protocol.txt | 204 ---
 tools/usb/usbip/src/usbip_network.h  |   5 +-
 2 files changed, 184 insertions(+), 25 deletions(-)

diff --git a/Documentation/usb/usbip_protocol.txt 
b/Documentation/usb/usbip_protocol.txt
index 16b6fe2..d4be5b6 100644
--- a/Documentation/usb/usbip_protocol.txt
+++ b/Documentation/usb/usbip_protocol.txt
@@ -1,20 +1,26 @@
 PRELIMINARY DRAFT, MAY CONTAIN MISTAKES!
 28 Jun 2011
+MODIFIED FOR CONNECT AND DISCONNECT OPERARION.
+07 March 2016
 
-The USB/IP protocol follows a server/client architecture. The server exports 
the
-USB devices and the clients imports them. The device driver for the exported
-USB device runs on the client machine.
+The USB/IP protocol follows a server/client architecture between two computers
+one has USB devices and the other runs application using the devices. There are
+two ways for initiation.
 
-The client may ask for the list of the exported USB devices. To get the list 
the
-client opens a TCP/IP connection towards the server, and sends an 
OP_REQ_DEVLIST
-packet on top of the TCP/IP connection (so the actual OP_REQ_DEVLIST may be 
sent
-in one or more pieces at the low level transport layer). The server sends back
-the OP_REP_DEVLIST packet which lists the exported USB devices. Finally the
-TCP/IP connection is closed.
+The first way is to import devices from application-side. In this way, the
+server runs in device-side and the client runs in application-side. In device
+side user makes devices importable with 'bind' operation.
 
+The client may ask for the list of the importable USB devices. To get the list
+the client opens a TCP/IP connection towards the server, and sends an
+OP_REQ_DEVLIST packet on top of the TCP/IP connection (so the actual
+OP_REQ_DEVLIST may be sent in one or more pieces at the low level transport
+layer). The server sends back the OP_REP_DEVLIST packet which lists the
+importable USB devices. Finally the TCP/IP connection is closed.
+
+   application-sidedevice-side
  virtual host controller usb host
-  "client"   "server"
-  (imports USB devices) (exports USB devices)
+  "client" (lists importable devices)"server"
   | |
   |  OP_REQ_DEVLIST |
   | --> |
@@ -23,18 +29,13 @@ TCP/IP connection is closed.
   | <-- |
   | |
 
-Once the client knows the list of exported USB devices it may decide to use one
-of them. First the client opens a TCP/IP connection towards the server and
-sends an OP_REQ_IMPORT packet. The server replies with OP_REP_IMPORT. If the
-import was successful the TCP/IP connection remains open and will be used
-to transfer the URB traffic between the client and the server. The client may
-send two types of packets: the USBIP_CMD_SUBMIT to submit an URB, and
-USBIP_CMD_UNLINK to unlink a previously submitted URB. The answers of the
-server may be USBIP_RET_SUBMIT and USBIP_RET_UNLINK respectively.
+Once the client knows the list of importable USB devices it may decide to use
+one of them. First the client opens a TCP/IP connection towards the server and
+sends an OP_REQ_IMPORT packet. The server replies with OP_REP_IMPORT.
 
+   application-sidedevice-side
  virtual host controller usb host
-  "client"   "server"
-  (imports USB devices) (exports USB devices)
+  "client"   (imports a USB device)  "server"
   | |
   |  OP_REQ_IMPORT  |
   | --> |
@@ -42,6 +43,32 @@ server may be USBIP_RET_SUBMIT and USBIP_RET_UNLINK 
respectively.
   |  OP_REP_IMPORT  |
   | <-- |
   | |
+
+The second way is to export devices from device-side. In this way, the server
+runs in application-side and the client runs in device-side. The client binds a
+device to export, opens a TCP/IP connection towards the server

[PATCH v8 2/9] usbip: exporting devices: modifications to host side libraries

2016-06-12 Thread Nobuo Iwata
usbip_host_find_device() is created based on usbip_host_get_device(). 
usbip_host_get_device() was not used yet.

bind and unbind function are exported for new operations.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/usbip_host_common.c | 6 ++
 tools/usb/usbip/libsrc/usbip_host_common.h | 8 
 tools/usb/usbip/src/usbip.h| 3 +++
 tools/usb/usbip/src/usbip_bind.c   | 7 ---
 tools/usb/usbip/src/usbip_unbind.c | 7 ---
 5 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/tools/usb/usbip/libsrc/usbip_host_common.c 
b/tools/usb/usbip/libsrc/usbip_host_common.c
index 9d41522..6a98d6c 100644
--- a/tools/usb/usbip/libsrc/usbip_host_common.c
+++ b/tools/usb/usbip/libsrc/usbip_host_common.c
@@ -256,17 +256,15 @@ int usbip_export_device(struct usbip_exported_device 
*edev, int sockfd)
 }
 
 struct usbip_exported_device *usbip_generic_get_device(
-   struct usbip_host_driver *hdriver, int num)
+   struct usbip_host_driver *hdriver, char *busid)
 {
struct list_head *i;
struct usbip_exported_device *edev;
-   int cnt = 0;
 
list_for_each(i, >edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
-   if (num == cnt)
+   if (!strncmp(busid, edev->udev.busid, SYSFS_BUS_ID_SIZE))
return edev;
-   cnt++;
}
 
return NULL;
diff --git a/tools/usb/usbip/libsrc/usbip_host_common.h 
b/tools/usb/usbip/libsrc/usbip_host_common.h
index a64b803..f9a9def 100644
--- a/tools/usb/usbip/libsrc/usbip_host_common.h
+++ b/tools/usb/usbip/libsrc/usbip_host_common.h
@@ -38,7 +38,7 @@ struct usbip_host_driver_ops {
void (*close)(struct usbip_host_driver *hdriver);
int (*refresh_device_list)(struct usbip_host_driver *hdriver);
struct usbip_exported_device * (*get_device)(
-   struct usbip_host_driver *hdriver, int num);
+   struct usbip_host_driver *hdriver, char *busid);
 
int (*read_device)(struct udev_device *sdev,
   struct usbip_usb_device *dev);
@@ -86,11 +86,11 @@ static inline int usbip_refresh_device_list(struct 
usbip_host_driver *hdriver)
 }
 
 static inline struct usbip_exported_device *
-usbip_get_device(struct usbip_host_driver *hdriver, int num)
+usbip_get_device(struct usbip_host_driver *hdriver, char *busid)
 {
if (!hdriver->ops.get_device)
return NULL;
-   return hdriver->ops.get_device(hdriver, num);
+   return hdriver->ops.get_device(hdriver, busid);
 }
 
 /* Helper functions for implementing driver backend */
@@ -99,6 +99,6 @@ void usbip_generic_driver_close(struct usbip_host_driver 
*hdriver);
 int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver);
 int usbip_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_generic_get_device(
-   struct usbip_host_driver *hdriver, int num);
+   struct usbip_host_driver *hdriver, char *busid);
 
 #endif /* __USBIP_HOST_COMMON_H */
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 84fe66a..c296910 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -37,4 +37,7 @@ void usbip_list_usage(void);
 void usbip_bind_usage(void);
 void usbip_unbind_usage(void);
 
+int usbip_bind_device(char *busid);
+int usbip_unbind_device(char *busid);
+
 #endif /* __USBIP_H */
diff --git a/tools/usb/usbip/src/usbip_bind.c b/tools/usb/usbip/src/usbip_bind.c
index fa46141..1c09338 100644
--- a/tools/usb/usbip/src/usbip_bind.c
+++ b/tools/usb/usbip/src/usbip_bind.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  *
  * This program is free software: you can redistribute it and/or modify
@@ -139,7 +140,7 @@ out:
return status;
 }
 
-static int bind_device(char *busid)
+int usbip_bind_device(char *busid)
 {
int rc;
struct udev *udev;
@@ -200,7 +201,7 @@ int usbip_bind(int argc, char *argv[])
 
switch (opt) {
case 'b':
-   ret = bind_device(optarg);
+   ret = usbip_bind_device(optarg);
goto out;
default:
goto err_out;
diff --git a/tools/usb/usbip/src/usbip_unbind.c 
b/tools/usb/usbip/src/usbip_unbind.c
index a4a496c..cc1ff26 100644
--- a/tools/usb/usbip/src/usbip_unbind.c
+++ b/tools/usb/usbip/src/usbip_unbind.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  *
  * This p

[PATCH v8 9/9] usbip: exporting devices: chage to documenattion

2016-06-12 Thread Nobuo Iwata
This patch adds function and usage of new connect operation, disconnect 
operation and application(vhci)-side daemon to README and manuals.

At this point, the wording, 'server' and 'client' are ambiguous in 
several place.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side (machine)' and 'application 
side (machine)' are used instead of 'client' and 'server'.

Please, see also diagrams in the cover letter.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/Makefile.am  |   2 +-
 tools/usb/usbip/README   |  70 --
 tools/usb/usbip/doc/usbip.8  | 136 ---
 tools/usb/usbip/doc/usbipa.8 |  79 
 tools/usb/usbip/doc/usbipd.8 |  37 ++
 5 files changed, 264 insertions(+), 60 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney <m...@muteddisk.com>
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device with  is now exportable to other hosts!
+   - Use `usbip unbind --busid ` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote 
+app:# usbip list --remote 
- List exported USB devices on the .
 
-client:# usbip attach --remote  --busid 1-2
+app:# usbip attach --remote  --busid 
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port 
+app:# usbip detach --port 
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote  --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device of  is connected to remote host!
+
+dev:# usbip disconnect --remote  --busid 
+   - The USB device with  is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
  

[PATCH v8 7/9] usbip: exporting devices: new application-side daemon

2016-06-12 Thread Nobuo Iwata
New application(vhci)-side daemon.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/vhci_driver.c |  19 +++
 tools/usb/usbip/libsrc/vhci_driver.h |   1 +
 tools/usb/usbip/src/Makefile.am  |   7 +-
 tools/usb/usbip/src/usbipd.c |  12 +-
 tools/usb/usbip/src/usbipd_app.c | 243 +++
 5 files changed, 280 insertions(+), 2 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index b7ca63d..50c723d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -314,6 +314,25 @@ int usbip_vhci_get_free_port(void)
return -1;
 }
 
+struct usbip_imported_device *usbip_vhci_find_device(char *host, char *busid)
+{
+   int ret;
+   char rhost[NI_MAXHOST] = "unknown host";
+   char rserv[NI_MAXSERV] = "unknown port";
+   char rbusid[SYSFS_BUS_ID_SIZE];
+
+   for (int i = 0; i < vhci_driver->nports; i++) {
+   ret = read_record(vhci_driver->idev[i].port, rhost, NI_MAXHOST,
+   rserv, NI_MAXSERV, rbusid);
+   if (!ret &&
+   !strncmp(host, rhost, NI_MAXHOST) &&
+   !strncmp(busid, rbusid, SYSFS_BUS_ID_SIZE)) {
+   return vhci_driver->idev + i;
+   }
+   }
+   return NULL;
+}
+
 int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
uint32_t speed) {
char buff[200]; /* what size should be ? */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h 
b/tools/usb/usbip/libsrc/vhci_driver.h
index f955ada..acb427d 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -46,6 +46,7 @@ int  usbip_vhci_refresh_device_list(void);
 
 
 int usbip_vhci_get_free_port(void);
+struct usbip_imported_device *usbip_vhci_find_device(char *host, char *busid);
 int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
uint32_t speed);
 
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index 1aa5156..8fdebce 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -2,11 +2,16 @@ AM_CPPFLAGS = -I$(top_srcdir)/libsrc 
-DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
 AM_CFLAGS   = @EXTRA_CFLAGS@
 LDADD   = $(top_builddir)/libsrc/libusbip.la
 
-sbin_PROGRAMS := usbip usbipd
+sbin_PROGRAMS := usbip usbipd usbipa
 
 usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
 usbip_attach.c usbip_detach.c usbip_list.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
+
+usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS) -DUSBIP_DAEMON_APP
diff --git a/tools/usb/usbip/src/usbipd.c b/tools/usb/usbip/src/usbipd.c
index ef60026..71e2d52 100644
--- a/tools/usb/usbip/src/usbipd.c
+++ b/tools/usb/usbip/src/usbipd.c
@@ -64,11 +64,13 @@ static const char usbipd_help_string[] =
"   -6, --ipv6\n"
"   Bind to IPv6. Default is both.\n"
"\n"
+#ifndef USBIP_DAEMON_APP
"   -e, --device\n"
"   Run in device mode.\n"
"   Rather than drive an attached device, create\n"
"   a virtual UDC to bind gadgets to.\n"
"\n"
+#endif
"   -D, --daemon\n"
"   Run as a daemon process.\n"
"\n"
@@ -401,7 +403,9 @@ int main(int argc, char *argv[])
{ "ipv6", no_argument,   NULL, '6' },
{ "daemon",   no_argument,   NULL, 'D' },
{ "debug",no_argument,   NULL, 'd' },
+#ifndef USBIP_DAEMON_APP
{ "device",   no_argument,   NULL, 'e' },
+#endif
{ "pid",  optional_argument, NULL, 'P' },
{ "tcp-port", required_argument, NULL, 't' },
{ "help", no_argument,   NULL, 'h' },
@@ -430,7 +434,11 @@ int main(int argc, char *argv[])
cmd = cmd_standalone_mode;
usbip_init_driver();
for (;;) {
-   opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL);
+   opt = getopt_long(argc, argv, "46Dd"
+#ifndef USBIP_DAEMON_APP
+ "e"
+#endif
+ "P::t:hv", longopts, NULL);
 
if (opt == -1)
break;
@@ -460,9 +468,11 @@ int mai

[PATCH v8 8/9] usbip: exporting devices: change to usbip_list.c

2016-06-12 Thread Nobuo Iwata
Correction to wording inconsistency around import and export in 
usbip_list.c.

Please, see also cover letter about wording.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/src/usbip_list.c | 22 --
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c
index f1b38e8..37f9afa 100644
--- a/tools/usb/usbip/src/usbip_list.c
+++ b/tools/usb/usbip/src/usbip_list.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  * Copyright (C) 2015-2016 Samsung Electronics
  *   Igor Kotrasinski <i.kotrasi...@samsung.com>
@@ -42,9 +43,9 @@
 #include "usbip.h"
 
 static const char usbip_list_usage_string[] =
-   "usbip list [-p|--parsable] \n"
+   "usbip list \n"
"-p, --parsable Parsable list format\n"
-   "-r, --remote=List the exportable USB devices on \n"
+   "-r, --remote=List the importable USB devices on \n"
"-l, --localList the local USB devices\n";
 
 void usbip_list_usage(void)
@@ -52,7 +53,7 @@ void usbip_list_usage(void)
printf("usage: %s", usbip_list_usage_string);
 }
 
-static int get_exported_devices(char *host, int sockfd)
+static int get_importable_devices(char *host, int sockfd)
 {
char product_name[100];
char class_name[100];
@@ -82,14 +83,14 @@ static int get_exported_devices(char *host, int sockfd)
return -1;
}
PACK_OP_DEVLIST_REPLY(0, );
-   dbg("exportable devices: %d\n", reply.ndev);
+   dbg("importable devices: %d\n", reply.ndev);
 
if (reply.ndev == 0) {
-   info("no exportable devices found on %s", host);
+   info("no importable devices found on %s", host);
return 0;
}
 
-   printf("Exportable USB devices\n");
+   printf("Importable USB devices\n");
printf("==\n");
printf(" - %s\n", host);
 
@@ -134,7 +135,7 @@ static int get_exported_devices(char *host, int sockfd)
return 0;
 }
 
-static int list_exported_devices(char *host)
+static int list_importable_devices(char *host)
 {
int rc;
int sockfd;
@@ -147,9 +148,10 @@ static int list_exported_devices(char *host)
}
dbg("connected to %s:%s", host, usbip_port_string);
 
-   rc = get_exported_devices(host, sockfd);
+   rc = get_importable_devices(host, sockfd);
if (rc < 0) {
err("failed to get device list from %s", host);
+   close(sockfd);
return -1;
}
 
@@ -351,7 +353,7 @@ int usbip_list(int argc, char *argv[])
parsable = true;
break;
case 'r':
-   ret = list_exported_devices(optarg);
+   ret = list_importable_devices(optarg);
goto out;
case 'l':
ret = list_devices(parsable);
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v8 5/9] usbip: exporting devices: modifications to daemon

2016-06-12 Thread Nobuo Iwata
Refactoring to the daemon.

usbipd_dev.c is device-side specific code extracted from usbipd.c.

usbipd.c is left as common parts for both device(stub)-side and 
application(vhci)-side daemon.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/src/Makefile.am  |   2 +-
 tools/usb/usbip/src/usbipd.c | 235 +++-
 tools/usb/usbip/src/usbipd.h |  39 +
 tools/usb/usbip/src/usbipd_dev.c | 256 +++
 4 files changed, 316 insertions(+), 216 deletions(-)

diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index 42760c3..1aa5156 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -9,4 +9,4 @@ usbip_SOURCES := usbip.h utils.h usbip.c utils.c 
usbip_network.c \
 usbip_bind.c usbip_unbind.c usbip_port.c \
 usbip_connect.c usbip_disconnect.c
 
-usbipd_SOURCES := usbip_network.h usbipd.c usbip_network.c
+usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
diff --git a/tools/usb/usbip/src/usbipd.c b/tools/usb/usbip/src/usbipd.c
index a0972de..ef60026 100644
--- a/tools/usb/usbip/src/usbipd.c
+++ b/tools/usb/usbip/src/usbipd.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  * Copyright (C) 2015-2016 Samsung Electronics
  *   Igor Kotrasinski <i.kotrasi...@samsung.com>
@@ -43,25 +44,19 @@
 #include 
 #include 
 
-#include "usbip_host_driver.h"
-#include "usbip_host_common.h"
-#include "usbip_device_driver.h"
 #include "usbip_common.h"
 #include "usbip_network.h"
+#include "usbipd.h"
 #include "list.h"
 
-#undef  PROGNAME
-#define PROGNAME "usbipd"
 #define MAXSOCKFD 20
 
 #define MAIN_LOOP_TIMEOUT 10
 
-#define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
-
 static const char usbip_version_string[] = PACKAGE_STRING;
 
 static const char usbipd_help_string[] =
-   "usage: usbipd [options]\n"
+   "usage: %s [options]\n"
"\n"
"   -4, --ipv4\n"
"   Bind to IPv4. Default is both.\n"
@@ -82,7 +77,7 @@ static const char usbipd_help_string[] =
"\n"
"   -PFILE, --pid FILE\n"
"   Write process id to FILE.\n"
-   "   If no FILE specified, use " DEFAULT_PID_FILE "\n"
+   "   If no FILE specified, use %s.\n"
"\n"
"   -tPORT, --tcp-port PORT\n"
"   Listen on TCP/IP port PORT.\n"
@@ -93,198 +88,9 @@ static const char usbipd_help_string[] =
"   -v, --version\n"
"   Show version.\n";
 
-static struct usbip_host_driver *driver;
-
 static void usbipd_help(void)
 {
-   printf("%s\n", usbipd_help_string);
-}
-
-static int recv_request_import(int sockfd)
-{
-   struct op_import_request req;
-   struct usbip_exported_device *edev;
-   struct usbip_usb_device pdu_udev;
-   struct list_head *i;
-   int found = 0;
-   int error = 0;
-   int rc;
-
-   memset(, 0, sizeof(req));
-
-   rc = usbip_net_recv(sockfd, , sizeof(req));
-   if (rc < 0) {
-   dbg("usbip_net_recv failed: import request");
-   return -1;
-   }
-   PACK_OP_IMPORT_REQUEST(0, );
-
-   list_for_each(i, >edev_list) {
-   edev = list_entry(i, struct usbip_exported_device, node);
-   if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
-   info("found requested device: %s", req.busid);
-   found = 1;
-   break;
-   }
-   }
-
-   if (found) {
-   /* should set TCP_NODELAY for usbip */
-   usbip_net_set_nodelay(sockfd);
-
-   /* export device needs a TCP/IP socket descriptor */
-   rc = usbip_export_device(edev, sockfd);
-   if (rc < 0)
-   error = 1;
-   } else {
-   info("requested device not found: %s", req.busid);
-   error = 1;
-   }
-
-   rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
- (!error ? ST_OK : ST_NA));
-   if (rc < 0) {
-   dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
-   return -1;
-   }
-
-   if (error) {
-   dbg("import request busid %s: failed", req.busid);
-   return -1;
-   }
-
-   memcpy(_udev, >udev, sizeof(pd

[PATCH v8 3/9] usbip: exporting devices: new connect operation

2016-06-12 Thread Nobuo Iwata
New connect operation.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/src/Makefile.am |   3 +-
 tools/usb/usbip/src/usbip.c |   9 +-
 tools/usb/usbip/src/usbip.h |   5 +-
 tools/usb/usbip/src/usbip_connect.c | 228 
 4 files changed, 242 insertions(+), 3 deletions(-)

diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index e81a4eb..0947476 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -6,6 +6,7 @@ sbin_PROGRAMS := usbip usbipd
 
 usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
 usbip_attach.c usbip_detach.c usbip_list.c \
-usbip_bind.c usbip_unbind.c usbip_port.c
+usbip_bind.c usbip_unbind.c usbip_port.c \
+usbip_connect.c
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbip_network.c
diff --git a/tools/usb/usbip/src/usbip.c b/tools/usb/usbip/src/usbip.c
index d7599d9..584d7d5 100644
--- a/tools/usb/usbip/src/usbip.c
+++ b/tools/usb/usbip/src/usbip.c
@@ -2,7 +2,8 @@
  * command structure borrowed from udev
  * (git://git.kernel.org/pub/scm/linux/hotplug/udev.git)
  *
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  *
  * This program is free software: you can redistribute it and/or modify
@@ -76,6 +77,12 @@ static const struct command cmds[] = {
.usage = usbip_detach_usage
},
{
+   .name  = "connect",
+   .fn= usbip_connect,
+   .help  = "Connect a USB device to a remote computer",
+   .usage = usbip_connect_usage
+   },
+   {
.name  = "list",
.fn= usbip_list,
.help  = "List exportable or local USB devices",
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index c296910..f365353 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
  *   2005-2007 Takahiro Hirofuchi
  *
  * This program is free software: you can redistribute it and/or modify
@@ -30,12 +31,14 @@ int usbip_list(int argc, char *argv[]);
 int usbip_bind(int argc, char *argv[]);
 int usbip_unbind(int argc, char *argv[]);
 int usbip_port_show(int argc, char *argv[]);
+int usbip_connect(int argc, char *argv[]);
 
 void usbip_attach_usage(void);
 void usbip_detach_usage(void);
 void usbip_list_usage(void);
 void usbip_bind_usage(void);
 void usbip_unbind_usage(void);
+void usbip_connect_usage(void);
 
 int usbip_bind_device(char *busid);
 int usbip_unbind_device(char *busid);
diff --git a/tools/usb/usbip/src/usbip_connect.c 
b/tools/usb/usbip/src/usbip_connect.c
new file mode 100644
index 000..bbecc7e
--- /dev/null
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2015 Nobuo Iwata
+ *   2011 matt mooney <m...@muteddisk.com>
+ *   2005-2007 Takahiro Hirofuchi
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "usbip_host_driver.h"
+#include "usbip_host_common.h"
+#include "usbip_device_driver.h"
+#include "usbip_common.h"
+#include "usbip_network.h"
+#include "usbip.h"
+
+static struct usbip_host_driver *driver = _driver;
+
+static const char usbip_connect_usage_string[] =
+   "usbip connect \n"
+   "-r, --remote=Address of a remote computer\n"
+   "-b, --busid=Bus ID of a device to be connected\n"
+   "-d, --device   Run with an alternate driver, e.g. vUDC\n";
+
+void usbip_connect_usage(void)
+{
+   printf("usage: %s", usbip_connect_usage_string);
+}
+
+static int send_export_device(int sockfd, struct usbip_usb_device *udev)
+{
+   int rc;
+   struct op_export_request request;
+   struct op_export_reply   reply

[PATCH v4 2/3] usbip: vhci extension: modifications to userspace

2016-06-12 Thread Nobuo Iwata
Modification to the userspace tools including usbip/libsrc and 
usbip/src.

Changed corresponding to new vhci_sysfs.c.

nports in sysfs is used to get total number of ports. 

Old get_nports() ignores the last status line because 
udev_device_get_sysattr_value() drops last new line. New version uses 
nports attribute so it's doesn't have this problem.

status[.N] in sysfs are used.

parse_status() which reads all status lines is broken into open, close, 
read-line and parse-line. Parse-line is reused to find free port and 
get imported device.

In daemon, status was loaded into memory by 
usbip_vhci_refresh_device_list() at receiving every request. The loaded 
status is used to find free port. It is changed to read status directly 
to find free port.

Wording inconsistencies are fixed according to the rule below.

rhport, HC_PORTS: ports within a controller (or root hub).
port, nports: ports across the controllers.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/vhci_driver.c | 398 +++
 tools/usb/usbip/libsrc/vhci_driver.h |  45 +--
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  49 ++--
 5 files changed, 253 insertions(+), 260 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index 50c723d..4d1b986 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -15,11 +15,24 @@
 #undef  PROGNAME
 #define PROGNAME "libusbip"
 
-struct usbip_vhci_driver *vhci_driver;
-struct udev *udev_context;
+static struct udev_device *vhci_hc_device;
+static struct udev *udev_context;
+static int vhci_nports;
 
-static struct usbip_imported_device *
-imported_device_init(struct usbip_imported_device *idev, char *busid)
+struct usbip_vhci_device {
+   int port;
+   uint32_t status;
+
+   uint32_t devid;
+
+   uint8_t busnum;
+   uint8_t devnum;
+
+   /* usbip_class_device list */
+   struct usbip_usb_device udev;
+};
+
+static int imported_device_init(struct usbip_vhci_device *vdev, char *busid)
 {
struct udev_device *sudev;
 
@@ -27,132 +40,131 @@ imported_device_init(struct usbip_imported_device *idev, 
char *busid)
   "usb", busid);
if (!sudev) {
dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
-   goto err;
+   return -1;
}
-   read_usb_device(sudev, >udev);
+   read_usb_device(sudev, >udev);
udev_device_unref(sudev);
 
-   return idev;
-
-err:
-   return NULL;
+   return 0;
 }
 
+struct status_context {
+   int controller;
+   const char *c;
+};
 
+#define OPEN_MODE_FIRST  0
+#define OPEN_MODE_REOPEN 1
 
-static int parse_status(const char *value)
-{
-   int ret = 0;
-   char *c;
+static int open_hc_device(int mode);
 
+#define MAX_STATUS_NAME 16
 
-   for (int i = 0; i < vhci_driver->nports; i++)
-   memset(_driver->idev[i], 0, sizeof(vhci_driver->idev[i]));
+static int open_status(struct status_context *ctx, int mode)
+{
+   char name[MAX_STATUS_NAME+1];
 
+   if (mode == OPEN_MODE_FIRST)
+   ctx->controller = 0;
+   else
+   (ctx->controller)++;
 
-   /* skip a header line */
-   c = strchr(value, '\n');
-   if (!c)
+   if (open_hc_device(OPEN_MODE_REOPEN))
return -1;
-   c++;
-
-   while (*c != '\0') {
-   int port, status, speed, devid;
-   unsigned long socket;
-   char lbusid[SYSFS_BUS_ID_SIZE];
-
-   ret = sscanf(c, "%d %d %d %x %lx %31s\n",
-   , , ,
-   , , lbusid);
-
-   if (ret < 5) {
-   dbg("sscanf failed: %d", ret);
-   BUG();
-   }
 
-   dbg("port %d status %d speed %d devid %x",
-   port, status, speed, devid);
-   dbg("socket %lx lbusid %s", socket, lbusid);
+   if (ctx->controller == 0)
+   strcpy(name, "status");
+   else
+   snprintf(name, MAX_STATUS_NAME + 1,
+   "status.%d", ctx->controller);
+   ctx->c = udev_device_get_sysattr_value(vhci_hc_device, name);
+   if (ctx->c == NULL)
+   return -1;
 
+   return 0;
+}
 
-   /* if a device is connected, look at it */
-   {
-   struct usbip_imported_device *idev = 
_driver->idev[port];
+static void close_status(struct status_context *ctx)
+{
+   ctx->c = NULL;
+}
 
-   idev->port  = po

[PATCH v4 3/3] usbip: vhci extension: dynamic extension

2016-06-12 Thread Nobuo Iwata
Modification for dynamic device registration and unregistration.

1. kernel config

Followings are added.

USBIP_VHCI_HC_PORTS: Number of ports per USB/IP virtual host 
controller. The default is 8 - same as current VHCI_NPORTS.
USBIP_VHCI_MAX_HCS: Muximum number of USB/IP virtual host controllers. 
The default is 1.
USBIP_VHCI_INIT_HCS: Initial number of USB/IP virtual host controllers. 
The default is 1.
Static number of devices: USBIP_VHCI_NR_HCS in patch 1/3 is removed 
with this patch.

2. view from sysfs

Sysfs structure is changed as following.

BEFORE this patchset:
/sys/devices/platform
+-- vhci
+-- status
+-- attach
+-- detach
+-- usbip_debug

AFTER: example for CONFIG_USBIP_INIT_HCS=2 CONFIG_USBIP_MAX_HCS=4
At the beginning
/sys/devices/platform
+-- vhci
|   +-- nports
|   +-- status
|   +-- status.1
|   +-- status.2
|   +-- status.3
|   +-- attach
|   +-- detach
|   +-- usbip_debug
+-- vhci.1

The status files are shown to the maximum number of devices. Port 
status in status.2 and status.3 represents as free but corresponding 
devices are not yes registered.
When all ports in status and status.1 are used, userspace tool requests 
'attach' to a port in status.2 then vhci.2 will be registred. The limit 
is defined with USBIP_VHCI_MAX_NCS.

By preparing muximum number of status files, there's no need to 
introduce additional operations for userspace tool.

When number of free ports becomes more than USBIP_VHCI_HC_PORTS * 
VHCI_FREE_HCS(2), a free controller other than the first one will be 
unregistered. It will be invoked by 'detach' operation and other error 
situations which ports are released.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/Kconfig  |  17 ++-
 drivers/usb/usbip/vhci.h   |  36 -
 drivers/usb/usbip/vhci_hcd.c   | 251 -
 drivers/usb/usbip/vhci_rx.c|  10 +-
 drivers/usb/usbip/vhci_sysfs.c |  49 ---
 drivers/usb/usbip/vhci_tx.c|   6 +-
 6 files changed, 294 insertions(+), 75 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index 29492c7..d11b548 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -34,8 +34,8 @@ config USBIP_VHCI_HC_PORTS
  host controller driver, this defines number of ports per
  USB/IP virtual host controller.
 
-config USBIP_VHCI_NR_HCS
-   int "Number of USB/IP virtual host controllers"
+config USBIP_VHCI_MAX_HCS
+   int "Maximum number of USB/IP virtual host controllers"
range 1 128
default 1
depends on USBIP_VHCI_HCD
@@ -43,7 +43,18 @@ config USBIP_VHCI_NR_HCS
  To increase number of ports available for USB/IP virtual
  host controller driver, this defines number of USB/IP
  virtual host controllers as if adding physical host
- controllers.
+ controllers. This defines the maximum number.
+
+config USBIP_VHCI_INIT_HCS
+   int "Initial number of USB/IP virtual host controllers"
+   range 1 USBIP_VHCI_MAX_HCS
+   default 1
+   depends on USBIP_VHCI_MAX_HCS
+   ---help---
+ To increase number of ports available for USB/IP virtual
+ host controller driver, this defines number of USB/IP
+ virtual host controllers as if adding physical host
+ controllers. This defines the number at initializing.
 
 config USBIP_HOST
tristate "Host driver"
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index 88b71c4..ba893a7 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -51,6 +51,9 @@ struct vhci_device {
 
/* vhci_tx thread sleeps for this queue */
wait_queue_head_t waitq_tx;
+
+   /* denotes port is in-use */
+   atomic_t using_port;
 };
 
 /* urb->hcpriv, use container_of() */
@@ -79,12 +82,21 @@ struct vhci_unlink {
 #define VHCI_HC_PORTS 8
 #endif
 
-#ifdef CONFIG_USBIP_VHCI_NR_HCS
-#define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
+#ifdef CONFIG_USBIP_VHCI_MAX_HCS
+#define VHCI_MAX_HCS CONFIG_USBIP_VHCI_MAX_HCS
+#else
+#define VHCI_MAX_HCS 1
+#endif
+
+#ifdef CONFIG_USBIP_VHCI_INIT_HCS
+#define VHCI_INIT_HCS CONFIG_USBIP_VHCI_INIT_HCS
 #else
-#define VHCI_NR_HCS 1
+#define VHCI_INIT_HCS 1
 #endif
 
+/* VHCI_FREE_HCS * VHCI_HC_PORTS: ports to keep free at unregister */
+#define VHCI_FREE_HCS 2
+
 #define MAX_STATUS_NAME 16
 
 /* for usb_bus.hcpriv */
@@ -98,6 +110,8 @@ struct vhci_hcd {
 
atomic_t seqnum;
 
+   unsigned int using_ports;
+
/*
 * NOTE:
 * wIndex shows the port number and begins from 1.
@@ -106,12 +120,18 @@ struct vhci_hcd {
struct vhci_device vdev[VHCI_HC_PORTS];
 };
 
+extern int vhci_max_controllers;
+extern int vhci_init_controllers;
 extern int vhci_num_controllers;
 extern st

[PATCH v4 1/3] usbip: vhci extension: modifications to vhci driver

2016-06-12 Thread Nobuo Iwata
Modification to Kconfig, vhci_hc.c, vhci.h and vhci_sysfs.c.

1. kernel config

Followings are added.

USBIP_VHCI_HC_PORTS: Number of ports per USB/IP virtual host 
controller. The default is 8 - same as current VHCI_NPORTS.
USBIP_VHCI_NR_HCS: Number of USB/IP virtual host controllers. The 
default is 1. This paratmeter is replaced with USBIP_VHCI_INIT_HCS and 
USBIP_VHCI_MAX_HCS included in succeeding dynamic extension patch.

2. the_controller to controllers

the_controller is changed to vhci_pdevs: array of struct 
platform_device.

3. vhci_sysfs.c

Sysfs structure is changed as following.

BEFORE:
/sys/devices/platform
+-- vhci
+-- status
+-- attach
+-- detach
+-- usbip_debug

AFTER: example for CONFIG_USBIP_NR_HCS=4
/sys/devices/platform
+-- vhci
|   +-- nports
|   +-- status
|   +-- status.1
|   +-- status.2
|   +-- status.3
|   +-- attach
|   +-- detach
|   +-- usbip_debug
+-- vhci.1
+-- vhci.2
+-- vhci.3

vhci[.N] is shown for each host controller kobj. vhch.1, vhci.2, ... 
are shown only when CONFIG_USBIP_NR_HCS is more than 1. Only 'vhci' 
(without number) has user space interfaces. 'nports' is newly added to 
give ports-per-controller and number of controlles. Before that, number 
of ports is acquired by reading status lines. Status is divided for 
each controller to avoid page size (4KB) limitation.

Old userspace tool binaries work with the first status within the first 
controller.

Inconsistency between status header and content is fixed.
4th and 5th column are
header:  "dev bus"
content(unused): "000 000"
content(used):   "%08x", devid
Only 1st and 2nd column are used by program. In old version, sscanf() 
in parse_status expect no bus column. And bus_id string is shown in the 
last column. Then bus in the header is removed and unused content is 
replaced with 8 zeros. The sscanf() expects more than 5 columns and new 
has 6 columns so there's no compatibility issue in this change.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/Kconfig  |  21 +++
 drivers/usb/usbip/vhci.h   |  54 --
 drivers/usb/usbip/vhci_hcd.c   | 285 ---
 drivers/usb/usbip/vhci_rx.c|  21 +--
 drivers/usb/usbip/vhci_sysfs.c | 296 +
 5 files changed, 497 insertions(+), 180 deletions(-)

diff --git a/drivers/usb/usbip/Kconfig b/drivers/usb/usbip/Kconfig
index 17646b2..29492c7 100644
--- a/drivers/usb/usbip/Kconfig
+++ b/drivers/usb/usbip/Kconfig
@@ -24,6 +24,27 @@ config USBIP_VHCI_HCD
  To compile this driver as a module, choose M here: the
  module will be called vhci-hcd.
 
+config USBIP_VHCI_HC_PORTS
+   int "Number of ports per USB/IP virtual host controller"
+   range 1 31
+   default 8
+   depends on USBIP_VHCI_HCD
+   ---help---
+ To increase number of ports available for USB/IP virtual
+ host controller driver, this defines number of ports per
+ USB/IP virtual host controller.
+
+config USBIP_VHCI_NR_HCS
+   int "Number of USB/IP virtual host controllers"
+   range 1 128
+   default 1
+   depends on USBIP_VHCI_HCD
+   ---help---
+ To increase number of ports available for USB/IP virtual
+ host controller driver, this defines number of USB/IP
+ virtual host controllers as if adding physical host
+ controllers.
+
 config USBIP_HOST
tristate "Host driver"
depends on USBIP_CORE && USB
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index a863a98..88b71c4 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,13 +73,25 @@ struct vhci_unlink {
 };
 
 /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
-#define VHCI_NPORTS 8
+#ifdef CONFIG_USBIP_VHCI_HC_PORTS
+#define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS
+#else
+#define VHCI_HC_PORTS 8
+#endif
+
+#ifdef CONFIG_USBIP_VHCI_NR_HCS
+#define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
+#else
+#define VHCI_NR_HCS 1
+#endif
+
+#define MAX_STATUS_NAME 16
 
 /* for usb_bus.hcpriv */
 struct vhci_hcd {
spinlock_t lock;
 
-   u32 port_status[VHCI_NPORTS];
+   u32 port_status[VHCI_HC_PORTS];
 
unsigned resuming:1;
unsigned long re_timeout;
@@ -90,14 +103,19 @@ struct vhci_hcd {
 * wIndex shows the port number and begins from 1.
 * But, the index of this array begins from 0.
 */
-   struct vhci_device vdev[VHCI_NPORTS];
+   struct vhci_device vdev[VH

[PATCH v4 0/3] usbip: vhci number of ports extension

2016-06-12 Thread Nobuo Iwata
This series of patches extends number of ports limitaion in application 
(vhci) side.

1. Background

Assuming a system shown below that services distributerd devices in 
home or office via USB/IP, if the devices are set at every doors and 
windows, number devices may be up to tens.

Home/SOHO/Enterprise Intranet/Internet
 +--+   +--+ USB/IP+-+
+|device|--+|Linux |---|Service  |
|+--+  |+--+---|on Linux |
+--+   +--++-+
ex)
Device  Service
 sensors ... environment analysis
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

If USB/IP is used for ubiqitous devices or IoT devices, many devices 
might be handled. 

2. About this patch set

In current USB/IP, available number of ports (ie. number of supported 
devices) is VHCI_NPORTS(8) in drivers/usb/usbip/vhci.h. The value of 
the macro can be altered to USB_MAXCHILDREN(31). This limit is came 
from hub status bit array. See also the comment at USB_MAXCHILDREN 
include/uapi/linux/usb/ch11.h.

There are two way to increase number of available ports. The first way 
is to put hub emulator under vhci. This can add ports up to 255 - the 
limit of USB 2.0 host controller. The second way is to add host 
controller. It's as same as machines have several host controllers: 
most desktop or note PCs have more than one host controller.

Current USB/IP supports only one controller defined as 'the_controller' 
in drivers/usb/usbip/vhci_hcd.c. This patch takes the second way 
described above and adds virtual controllers. In this patch, the number 
is specified by kernel configuration. 

3. Dynamic extension

According to kernel configuration, vhci devices (platform device) will 
be added and removed when they are not used. When the vhci driver is 
loaded, USBIP_VHCI_INIT_HCS (default is 1) drivers are registered. They 
will be further registered upto USBIP_VHCI_MAX_HCS (default is 1). They 
are unregistered when number of free devices becomes more than 
VHCI_FREE_HCS(2) except the first device.

4. Wording

Wording inconsistencies in function and variable names are corrected 
according to the rule below. They were not strict because only one host 
controller was handled.

rhport, HC_PORTS: ports within a controller (or root hub).
port, nports: ports across the controllers.

5. Dependencis

This series depends on 'usbip: exporting devices' patch set because 
this includes changes to application side daemon which introduced the 
patch set.

---
Version information

v4)
# Changed the method to set number of controllers from a module 
parameter to kernel config.
# Excluded event thread patch merged to 4.7-rc1.
# Added dynamic extension.

v3)
# Fixed conflicts against linux-next 20160209.
# Changed sysfs object and attribute name for old tools compatibility.
# Changed nports status format not to include num_controllers value.
# Fixed checkpatch errors and warnings.

v2)
# Added static to some functions and variables not called from other 
files. 

*** BLURB HERE ***

Nobuo Iwata (3):
  usbip: vhci extension: modifications to vhci driver
  usbip: vhci extension: modifications to userspace
  usbip: vhci extension: dynamic extension

 drivers/usb/usbip/Kconfig|  32 ++
 drivers/usb/usbip/vhci.h |  84 -
 drivers/usb/usbip/vhci_hcd.c | 468 +--
 drivers/usb/usbip/vhci_rx.c  |  31 +-
 drivers/usb/usbip/vhci_sysfs.c   | 303 +
 drivers/usb/usbip/vhci_tx.c  |   6 +-
 tools/usb/usbip/libsrc/vhci_driver.c | 398 ---
 tools/usb/usbip/libsrc/vhci_driver.h |  45 +--
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  49 ++-
 11 files changed, 983 insertions(+), 454 deletions(-)

-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 1/3] usbip: exporting devices

2016-02-09 Thread Nobuo Iwata
USB/IP supports a function to import USB devices from application-side 
machine by attach command.
The usage is as following.
dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbipd -D
// Start usbip daemon.
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip bind --busid 
// Make a device exportable to other hosts.

app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbip list --remote 
// List importable USB devices from the .
app:# usbip attach --remote  --busid 
// Import a device

By attach command, connection will be established from application-side 
to device-side.

This patch introduces a function to export devices form device-side 
machine to application-side machine.
The usage is as following.
app:# insmod usbip-core.ko and vhci-hcd.ko
app:# usbipa -D
// Start usbip daemon.

dev:# (Physically attach your USB device.)
dev:# insmod usbip-core.ko and usbip-host.ko
dev:# usbip list -l
// List local USB devices and their busid.
dev:# usbip connect --remote  --busid 
// Export a device to .

For this, export function, connection is established from device-side 
machine to application-side machine.

Following use cases are supposed for the export function.
1) Server application or cloud service serves distributed ubiquitous 
devices.
2) Dedicate devices to server application or cloud service.

To connect to cloud service, it needs to connect from inside of 
firewall.

Probably, the export function was planned because the packets were 
originally defined in header file usbip_network.h but they were not 
used yet.
This patch fixes the defined packet structures (ie. int in reply to 
uinit32_t) and use them.
Also, vendor/product name converion is added to port commnad as same as 
list command.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/libsrc/names.c |   2 +
 tools/usb/usbip/libsrc/usbip_host_driver.c |  15 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |  21 +-
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 227 ++-
 tools/usb/usbip/src/usbipd.h   |  36 +++
 tools/usb/usbip/src/usbipd_app.c   | 239 
 tools/usb/usbip/src/usbipd_dev.c   | 247 +
 21 files changed, 1176 insertions(+), 303 deletions(-)

diff --git a/tools/usb/usbip/libsrc/names.c b/tools/usb/usbip/libsrc/names.c
index 81ff852..559f797 100644
--- a/tools/usb/usbip/libsrc/names.c
+++ b/tools/usb/usbip/libsrc/names.c
@@ -201,6 +201,8 @@ void names_free(void)
pool = pool->next;
free(tmp);
}
+
+   pool_head = NULL;
 }
 
 static int new_vendor(const char *name, u_int16_t vendorid)
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.c 
b/tools/usb/usbip/libsrc/usbip_host_driver.c
index bef08d5..b987520 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.c
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.c
@@ -278,3 +278,18 @@ struct usbip_exported_device *usbip_host_get_device(int 
num)
 
return NULL;
 }
+
+struct usbip_exported_device *usbip_host_find_device(char *busid)
+{
+   struct list_head *i;
+   struct usbip_exported_device *edev;
+
+   list_for_each(i, _driver->edev_list) {
+   edev = list_entry(i, struct usbip_exported_device, node);
+   if (!strncmp(busid, edev->udev.busid, SYSFS_BUS_ID_SIZE))
+   return edev;
+   }
+
+   return NULL;
+}
+
diff --git a/tools/usb/usbip/libsrc/usbip_host_driver.h 
b/tools/usb/usbip/libsrc/usbip_host_driver.h
index 2a31f85..69c65a6 100644
--- a/tools/usb/usbip/libsrc/usbip_host_driver.h
+++ b/tools/usb/usbip/libsrc/usbip_host_driver.h
@@ -45,5 +45,6 @@ void usbip_host_driver_close(void);
 int usbip_host_refresh_device_list(void);
 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd);
 struct usbip_exported_device *usbip_host_get_device(int num);
+struct usbip_exported_device *usbip_host_find_device(char *busid);
 
 #endif /* __USBIP_HOST_DRIVER_H */
diff --git a/tools/usb/usbip/libsrc/vhci_driver.c 
b/tools/usb/usbip/libsrc/vhci_driver.c
index ad92047..501a61d 100644
--- a/tools/usb/usbip/libsr

[PATCH v7 3/3] usbip: safe completion against usb_kill_urb()

2016-02-09 Thread Nobuo Iwata
stub_shutdown_connection() : drivers/usb/usbip/stub_dev.c
 stub_device_cleanup_urbs() : drivers/usb/usbip/stub_main.c 
requests to kill pending URBs and clears priv lists.

stub_complete() : drivers/usb/usbip/stub_tx.c might be called with URBs 
to have been requested to kill.

To avoid kernel panic, this patch ignores killed URBs linked to cleared 
priv lists.
To know the killed URBs in stub_complete(), sdev->ud.tcp_socket which 
cleared before stub_device_cleanup_urbs() is checked.

The critial condition will happen by unbind command before detach, 
broken connection in connect command and disconnect command. 

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/stub_tx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/usbip/stub_tx.c b/drivers/usb/usbip/stub_tx.c
index dbcabc9..f19f321 100644
--- a/drivers/usb/usbip/stub_tx.c
+++ b/drivers/usb/usbip/stub_tx.c
@@ -97,7 +97,9 @@ void stub_complete(struct urb *urb)
 
/* link a urb to the queue of tx. */
spin_lock_irqsave(>priv_lock, flags);
-   if (priv->unlinking) {
+   if (sdev->ud.tcp_socket == NULL) {
+   dev_info(>dev->dev, "discard a urb for closed connection");
+   } else if (priv->unlinking) {
stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
stub_free_priv_and_urb(priv);
} else {
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v7 0/3] usbip: exporting devices

2016-02-09 Thread Nobuo Iwata
Dear all,

This series of patches introduces exporting devices. 
This series is divided from "USB/IP over WebSocket" patch set.
Rest of the set will be sent as another series.

0. Version info

v7)
# Removed userspace transmission and WebSocket command/daemon.
# Fixed checkpatch errors and warnings.

v6)
# Added __rcu annotation to a RCU pointer to clear sparse warnings.
# Corrected a copy to RCU pointer with rcu_rcu_assign_pointer(). 
# Added __user annotations to arguments of read/write method. 
# Added static to some functions which are not called from other files.
# Removed unnecessary EXPORT_SYMBOLs.

v5)
# Added vendor/pruduct name conversion to port command.
# Put initial value to pool_head in name.c.
# Fixed list command exception when host option is omitted.
# Fixed exception in case gai_strerror() returns NULL.
# Fixed WebSocket connection close via proxy.
# Fixed to stop WebSocket ping-pong on connection close.
# Removed redundant usbipd daemon option.
# Removed redundant SSL code had not been deleted.
# Removed an unused local variable in WebSocket code.
# Modified C++ reserved word in names.c as same as headers.

v4)
# Fixed regression of usbip list --remote

v3)
# Coding style for goto err labels are fixed.
# Defined magic numbers for open_hc_device() argument.
# Corrected include .../uapi/linux/usbip_ux.h as .
# Modified parameter notation in manuals not to use '='.
# Fixed inappropriate version definition in 
tools/.../websocket/configure.ac.
# Remved unnecessary COPYING and AUTHORS fil from tools/.../websocket/.
# Added -version-info to libraries in tools/.../src.

v2)
# Formatted patches from linux-next.
# Fixed change log word wrapping.
# Removed SSL patches.
# Fixed a bug that vendor and product names are not shown by 'usbws 
list -l' because usbip_names_init() was not called in libusbip.la.

1. Feature

Export request and response PDU had been defined in a header but not 
been impelmented.
Now it works!
   
Also, it supports scenarios, for example, connect ubiquetous devices to 
a Linux based cloud service using WebSocket proxy.
In this senario, it's needed to establish connection from a device 
inside of firewall to a service outside. Exporting is suit for the 
senario.

  Home/SOHO/IntranetInternet  
 ++  ++
 +--+   +--+ |Router, |  |Internet|
+|device|---|Linux |-|proxy,  |--|service |
|+--+   |controller| |firewall|  |on Linux|
+--++--+ ++  ++
  USB/IP + WS proxy   WS proxy + USB/IP
ex)
DeviceService 
 sensors . environment analysis 
 cameras . monitoring, recording
 ID/biometric readers  authentication

2. Why exporting devices?

Connection from outside firewall is usually blocked.
So existing import request sent with attach command doesn't work.

# usbipd (blocked)|| <- # usbip attach

Firewall opens some ports, usually HTTP(80) and HTTPS(443), from inside.
Then export request sent with new connect command works.

# usbip connect  -> # usbipa
 (passed)

Thank you,

Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
//

*** BLURB HERE ***

Nobuo Iwata (3):
  usbip: exporting devices
  usbip: readme and manuals about exporting devices
  usbip: safe completion against usb_kill_urb()

 drivers/usb/usbip/stub_tx.c|   4 +-
 tools/usb/usbip/Makefile.am|   2 +-
 tools/usb/usbip/README |  70 --
 tools/usb/usbip/doc/usbip.8|  82 +--
 tools/usb/usbip/doc/usbipa.8   |  77 +++
 tools/usb/usbip/doc/usbipd.8   |  29 ++-
 tools/usb/usbip/libsrc/names.c |   2 +
 tools/usb/usbip/libsrc/usbip_host_driver.c |  15 ++
 tools/usb/usbip/libsrc/usbip_host_driver.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c   | 127 +--
 tools/usb/usbip/libsrc/vhci_driver.h   |   8 +-
 tools/usb/usbip/src/Makefile.am|   9 +-
 tools/usb/usbip/src/usbip.c|  17 +-
 tools/usb/usbip/src/usbip.h|  11 +-
 tools/usb/usbip/src/usbip_attach.c |  49 +---
 tools/usb/usbip/src/usbip_bind.c   |   7 +-
 tools/usb/usbip/src/usbip_connect.c| 214 ++
 tools/usb/usbip/src/usbip_detach.c |  13 +-
 tools/usb/usbip/src/usbip_disconnect.c | 202 +
 tools/usb/usbip/src/usbip_list.c   |  22 +-
 tools/usb/usbip/src/usbip_network.h|   5 +-
 tools/usb/usbip/src/usbip_port.c   |  21 +-
 tools/usb/usbip/src/usbip_unbind.c |   7 +-
 tools/usb/usbip/src/usbipd.c   | 227 ++

[PATCH v7 2/3] usbip: readme and manuals about exporting devices

2016-02-09 Thread Nobuo Iwata
This patch adds function and usage of export to README and manuals.

The wording, 'server' and 'client' are changed.

For existing attach command, the daemon runs device side machine and 
attach command is executed in application side machine. Then 'server' 
is used for device side and 'client' is for application side.

For the new connect command, the daemon runs applications side machine 
and connect command is executed in device side machine. Now, 'server' 
and 'client' run in different machine than before.

So, to avoid confusion, words 'device side' and 'application side' are 
used instead of 'client' and 'server'.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 tools/usb/usbip/Makefile.am  |  2 +-
 tools/usb/usbip/README   | 70 +-
 tools/usb/usbip/doc/usbip.8  | 82 +++-
 tools/usb/usbip/doc/usbipa.8 | 77 +
 tools/usb/usbip/doc/usbipd.8 | 29 ++---
 5 files changed, 204 insertions(+), 56 deletions(-)

diff --git a/tools/usb/usbip/Makefile.am b/tools/usb/usbip/Makefile.am
index 66f8bf0..f371ed9 100644
--- a/tools/usb/usbip/Makefile.am
+++ b/tools/usb/usbip/Makefile.am
@@ -3,4 +3,4 @@ includedir = @includedir@/usbip
 include_HEADERS := $(addprefix libsrc/, \
 usbip_common.h vhci_driver.h usbip_host_driver.h)
 
-dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8)
+dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbipa.8)
diff --git a/tools/usb/usbip/README b/tools/usb/usbip/README
index 831f49f..74f4afb 100644
--- a/tools/usb/usbip/README
+++ b/tools/usb/usbip/README
@@ -1,7 +1,8 @@
 #
 # README for usbip-utils
 #
-# Copyright (C) 2011 matt mooney <m...@muteddisk.com>
+# Copyright (C) 2015 Nobuo Iwata
+#   2011 matt mooney <m...@muteddisk.com>
 #   2005-2008 Takahiro Hirofuchi
 
 
@@ -36,41 +37,70 @@
 
 
 [Usage]
-server:# (Physically attach your USB device.)
+Device-side: a machine has USB device(s).
+Application-side: a machine runs an application software uses remote USB 
device.
 
-server:# insmod usbip-core.ko
-server:# insmod usbip-host.ko
+1) Connect from application-side to device-side.
 
-server:# usbipd -D
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbipd -D
- Start usbip daemon.
 
-server:# usbip list -l
-   - List driver assignments for USB devices.
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
 
-server:# usbip bind --busid 1-2
-   - Bind usbip-host.ko to the device with busid 1-2.
-   - The USB device 1-2 is now exportable to other hosts!
-   - Use `usbip unbind --busid 1-2' to stop exporting the device.
+dev:# usbip bind --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device with  is now exportable to other hosts!
+   - Use `usbip unbind --busid ` to stop exporting the device.
 
-client:# insmod usbip-core.ko
-client:# insmod vhci-hcd.ko
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
 
-client:# usbip list --remote 
+app:# usbip list --remote 
- List exported USB devices on the .
 
-client:# usbip attach --remote  --busid 1-2
+app:# usbip attach --remote  --busid 
- Connect the remote USB device.
 
-client:# usbip port
+app:# usbip port
- Show virtual port status.
 
-client:# usbip detach --port 
+app:# usbip detach --port 
- Detach the USB device.
 
+2) Connect from device-side to application-side.
+
+app:# insmod usbip-core.ko
+app:# insmod vhci-hcd.ko
+
+app:# usbipa -D
+   - Start usbip daemon.
+
+dev:# (Physically attach your USB device.)
+
+dev:# insmod usbip-core.ko
+dev:# insmod usbip-host.ko
+
+dev:# usbip list -l
+   - List driver assignments for USB devices and their busid.
+
+dev:# usbip connect --remote  --busid 
+   - Bind usbip-host.ko to the device with .
+   - The USB device of  is connected to remote host!
+
+dev:# usbip disconnect --remote  --busid 
+   - The USB device with  is disconnected from remote host.
+   - Unbind usbip-host.ko from the device.
+
 
 [Example]
 ---
-   SERVER SIDE
+   DEVICE SIDE
 ---
 Physically attach your USB devices to this host.
 
@@ -131,7 +161,7 @@ Mark the device of busid 3-3.2 as exportable:
 ...
 
 ---
-   CLIENT SIDE
+ APPLICATION SIDE
 ---
 First, let's list available remote devices that are marked as
 exportable on the host.
@@ -170,7 +200,7 @@ Attach a remote USB device:
 deux:# usbip attach --remote 10.0.0.3 --busid 1-1
 port 0 attached
 
-Show the devices attached to this client:
+Show the devices attached to this machine:
 
 deux:# usbip port
 Port 00:  at Full 

[PATCH v3 1/2] usbip: vhci number of ports extension

2016-02-09 Thread Nobuo Iwata
This patch extends number of ports limitation in application (vhci) 
side.

To do so, vhci driver supports multiple host controllers. The number of 
controllers can be specified as a module parameter 'num_controllers'. 
The default is 1.

ex) # insmod vhci_hcd.ko num_controllers=4

Also, ports per controller is changed from 8 to USB_MAXCHILDREN (31). 
It can be modified with VHCI_NPORTS flag at module compilation.

So number of ports supported by vhci is 'num_controllers' * 31.

Sysfs structure is changes as following.
BEFORE:
/sys/devices/platform
+-- vhci
+-- status
+-- attach
+-- detach
+-- usbip_debug
AFTER: example for num_controllers=4
/sys/devices/platform
+-- vhci
|   +-- nports
|   +-- status
|   +-- status.1
|   +-- status.2
|   +-- status.3
|   +-- attach
|   +-- detach
|   +-- usbip_debug
+-- vhci.1
+-- vhci.2
+-- vhci.3

vhci[.N] is shown for each host controller kobj. vhch.1, vhci.2, ... 
are shown only when num_controllers is more than 1. Only vhci has user 
space interfaces. 'nports' is newly added to give ports-per-controller 
and number of controlles. Before that, number of ports is acquired by 
counting status lines. Status is divided for each controller to avoid 
page size (4KB) limitation.

Variable wording relating port has been corrected. 'port' represents id 
across multiple controllers. 'rhport (root hub port)' represents id 
within a controller.

Some unimportant info level messages are changed to debug level because 
they are too busy when using many ports.

Following bugs are fixed with this patch

1) The last port is ignored
tools/usb/usbip/libsrc/vhci_device.s: get_nports() ignores the last 
status line because udev_device_get_sysattr_value() drops last new 
line. New version uses nports attribute so it's doesn't have this 
problem. 

2) Status header and content inconsistency
4th and 5th column are
header:  "dev bus"
content(unused): "000 000"
content(used):   "%08x", devid
Only 1st and 2nd column are used by program. In old version, sscanf() 
in parse_status expect no bus column. And bus_id string is shown in the 
last column. Then bus in header is removed and unused content is 
replaced with 8 zeros. The sscanf() expects more than 5 columns and new 
has 6 columns. These no compatibility issue for this.

NOTE: Syslog error messages "systemd-udevd[390]: error opening USB 
device 'descriptors' file" may be shown. They are not caused by this 
patch. It seems to be a systemd problem.

Signed-off-by: Nobuo Iwata <nobuo.iw...@fujixerox.co.jp>
---
 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/vhci.h |  42 ++-
 drivers/usb/usbip/vhci_hcd.c | 268 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 294 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 541 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  38 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  49 ++-
 10 files changed, 775 insertions(+), 502 deletions(-)

diff --git a/drivers/usb/usbip/README b/drivers/usb/usbip/README
index 41a2cf2..fce3d7f 100644
--- a/drivers/usb/usbip/README
+++ b/drivers/usb/usbip/README
@@ -1,3 +1,6 @@
+MODULE PARAMS:
+   - num_controllers : number of controllers. Default is 1.
+
 TODO:
- more discussion about the protocol
- testing
diff --git a/drivers/usb/usbip/vhci.h b/drivers/usb/usbip/vhci.h
index a863a98..894c2da 100644
--- a/drivers/usb/usbip/vhci.h
+++ b/drivers/usb/usbip/vhci.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003-2008 Takahiro Hirofuchi
+ * Copyright (C) 2015 Nobuo Iwata
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,7 +73,11 @@ struct vhci_unlink {
 };
 
 /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
-#define VHCI_NPORTS 8
+#ifndef VHCI_NPORTS
+#define VHCI_NPORTS USB_MAXCHILDREN
+#endif
+
+#define MAX_STATUS_NAME 16
 
 /* for usb_bus.hcpriv */
 struct vhci_hcd {
@@ -93,11 +98,16 @@ struct vhci_hcd {
struct vhci_device vdev[VHCI_NPORTS];
 };
 
-extern struct vhci_hcd *the_controller;
-extern const struct attribute_group dev_attr_group;
+extern int num_controllers;
+extern struct platform_device **the_pdevs;
+extern struct attribute_group dev_attr_group;
 
 /* vhci_hcd.c */
-void rh_port_connect(int rhport, enum usb_device_speed speed);
+void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
+
+/* vhci_sysfs.c */
+int vhci_init_attr_group(void);
+void vhci_finish_attr_group(void);
 
 /* vhci_rx.c */
 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
@@

[PATCH v3 0/2] usbip: vhci number of ports extension

2016-02-09 Thread Nobuo Iwata
This series of patches extends number of ports limitaion in application 
(vhci) side.

0. Version info

v3)
# Fixed conflicts against linux-next 20160209.
# Changed sysfs object and attribute name for old tools compatibility.
# Changed nports status format not to include num_controllers value.
# Fixed checkpatch errors and warnings.

v2)
# Added static to some functions and variables not called from other 
files. 

1. Overview

This series conatins 2 patches.
1/2:
Extends number of ports using multiple host controllers.
'num_controllers=N' module parameter denotes the number.
The default is 1.
Number of ports per controller are extended from 8 to
USB_MAXCHILDREN(31).
It can be altered with -DVHCI_NPORTS=n at compile time.
2/2:
Event handling threads are used to be created for each port.
This patch aggregates them to one thread.
Rewritten with workqueue.

Assumed use case is a system that service in internet serves 
distributes devices in home or office. In the use case, application 
side might be needed to support more ports than 31.

Home/SOHO/Enterprise Intranet/Internet
   ++
 +--+   +--+   |Service |
+|device|---|Linux |---|on  |
|+--+   +--+   |Linux   |
+--+   controller  ++
ex)
Device  Service
 sensors ... environment analysis
 cameras ... monitoring, recording
 ID/biometric readers .. authentication

To increase number of ports, existing implementation has an overhead 
that event handing kernel threads are started for each port. The second 
patch eliminates the overhead.

2. TODO

1) Dynamic extension and reduction

If it's only extension, it's not so difficult to implement.
For reduction, many rcu_readlock/unlock are needed.
To keep code simple, this version simply extends the limitaion 
statically.

NOTE: This series depends on "exporting devices" patch set.

*** BLURB HERE ***

Nobuo Iwata (2):
  usbip: vhci number of ports extension
  usbip: single thread event handler

 drivers/usb/usbip/README |   3 +
 drivers/usb/usbip/stub_dev.c |   3 +-
 drivers/usb/usbip/usbip_common.c |   7 +
 drivers/usb/usbip/usbip_common.h |   4 +-
 drivers/usb/usbip/usbip_event.c  | 168 ++---
 drivers/usb/usbip/vhci.h |  42 ++-
 drivers/usb/usbip/vhci_hcd.c | 268 -
 drivers/usb/usbip/vhci_rx.c  |  21 +-
 drivers/usb/usbip/vhci_sysfs.c   | 294 +++
 tools/usb/usbip/libsrc/vhci_driver.c | 541 ++-
 tools/usb/usbip/libsrc/vhci_driver.h |  37 +-
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c |  13 +-
 tools/usb/usbip/src/usbipd_app.c |  49 ++-
 14 files changed, 912 insertions(+), 546 deletions(-)

-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


  1   2   >