2011-06-20 05:45:18

by matt mooney

[permalink] [raw]
Subject: [PATCH 00/22] staging: usbip: misc. userspace changes

Hi Greg,

This patch set contains only userspace changes. Next, I will take a look at
attach and detach, which should soon lead to the reason why usbip is not
currently working.

Thanks,
matt

matt mooney (22):
staging: usbip: userspace: update cleanup.sh
staging: usbip: userspace: usbip_common.h: cleanup log macros
staging: usbip: userspace: usbip_common.h: move enums
staging: usbip: userspace: usbip_common.h: fixup header includes
staging: usbip: userspace: libsrc: set program name for logging
staging: usbip: userspace: usbip.c: add log option
staging: usbip: userspace: libsrc: remove usbip.h
staging: usbip: userspace: libsrc: change all output messages to
debug
staging: usbip: userspace: usbip: modify command failure
staging: usbip: userspace: utils: remove libsysfs circumvention
staging: usbip: userspace: utils.c: rewrite modify_match_busid
staging: usbip: userspace: usbip_bind.c: major rewrite of the
implementation
staging: usbip: userspace: usbip_unbind.c: implement using libsysfs
staging: usbip: userspace: usbip list: edit output messages
staging: usbip: userspace: usbip list: move output header
staging: usbip: userspace: rename stub driver files
staging: usbip: userspace: usbip_host: update function and variable
names
staging: usbip: userspace: usbipd: major cleanup of daemon
staging: usbip: userspace: usbip_network.c: coding style cleanup
staging: usbip: userspace: add new prefix for usbip network code
staging: usbip: userspace: fix header installation bug
staging: usbip: userspace: configure.ac: change package data

drivers/staging/usbip/userspace/Makefile.am | 4 +-
drivers/staging/usbip/userspace/cleanup.sh | 10 +-
drivers/staging/usbip/userspace/configure.ac | 2 +-
drivers/staging/usbip/userspace/libsrc/Makefile.am | 8 +-
.../staging/usbip/userspace/libsrc/stub_driver.c | 390 -----------
.../staging/usbip/userspace/libsrc/stub_driver.h | 36 -
drivers/staging/usbip/userspace/libsrc/usbip.h | 19 -
.../staging/usbip/userspace/libsrc/usbip_common.c | 19 +-
.../staging/usbip/userspace/libsrc/usbip_common.h | 118 ++--
.../usbip/userspace/libsrc/usbip_host_driver.c | 401 +++++++++++
.../usbip/userspace/libsrc/usbip_host_driver.h | 48 ++
.../staging/usbip/userspace/libsrc/vhci_driver.c | 99 ++--
.../staging/usbip/userspace/libsrc/vhci_driver.h | 13 +-
drivers/staging/usbip/userspace/src/Makefile.am | 6 +-
drivers/staging/usbip/userspace/src/usbip.c | 26 +-
drivers/staging/usbip/userspace/src/usbip_attach.c | 8 +-
drivers/staging/usbip/userspace/src/usbip_bind.c | 303 +++++----
drivers/staging/usbip/userspace/src/usbip_list.c | 81 ++-
.../staging/usbip/userspace/src/usbip_network.c | 121 ++--
.../staging/usbip/userspace/src/usbip_network.h | 39 +-
drivers/staging/usbip/userspace/src/usbip_unbind.c | 123 +++-
drivers/staging/usbip/userspace/src/usbipd.c | 697 ++++++++++----------
drivers/staging/usbip/userspace/src/utils.c | 317 ++--------
drivers/staging/usbip/userspace/src/utils.h | 31 +-
24 files changed, 1417 insertions(+), 1502 deletions(-)
delete mode 100644 drivers/staging/usbip/userspace/libsrc/stub_driver.c
delete mode 100644 drivers/staging/usbip/userspace/libsrc/stub_driver.h
delete mode 100644 drivers/staging/usbip/userspace/libsrc/usbip.h
create mode 100644 drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
create mode 100644 drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h

--
1.7.5.2


2011-06-20 05:45:40

by matt mooney

[permalink] [raw]
Subject: [PATCH 01/22] staging: usbip: userspace: update cleanup.sh

Modify $FILES to account for the new directory layout. Also, sort the
list of files within the variable to make it human-readable.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/cleanup.sh | 10 ++++++----
1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/usbip/userspace/cleanup.sh b/drivers/staging/usbip/userspace/cleanup.sh
index da2f89b..955c3cc 100755
--- a/drivers/staging/usbip/userspace/cleanup.sh
+++ b/drivers/staging/usbip/userspace/cleanup.sh
@@ -1,10 +1,12 @@
-#!/bin/sh -x
-
+#!/bin/sh

if [ -r Makefile ]; then
make distclean
fi

-FILES="configure cscope.out Makefile.in depcomp compile config.guess config.sub config.h.in~ config.log config.status ltmain.sh libtool config.h.in autom4te.cache missing aclocal.m4 install-sh cmd/Makefile.in lib/Makefile.in Makefile lib/Makefile cmd/Makefile"
+FILES="aclocal.m4 autom4te.cache compile config.guess config.h.in config.log \
+ config.status config.sub configure cscope.out depcomp install-sh \
+ libsrc/Makefile libsrc/Makefile.in libtool ltmain.sh Makefile \
+ Makefile.in missing src/Makefile src/Makefile.in"

-rm -Rf $FILES
+rm -vRf $FILES
--
1.7.5.2

2011-06-20 05:45:33

by matt mooney

[permalink] [raw]
Subject: [PATCH 02/22] staging: usbip: userspace: usbip_common.h: cleanup log macros

Provide better abstraction for easier modification, and align the
macros for readability. Remove notice() because it is not used.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/libsrc/usbip_common.h | 88 ++++++++++----------
1 files changed, 43 insertions(+), 45 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.h b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
index 32b27ed..090b7c5 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.h
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
@@ -58,51 +58,49 @@ extern int usbip_use_syslog;
extern int usbip_use_stderr;
extern int usbip_use_debug ;

-#define err(fmt, args...) do { \
- if (usbip_use_syslog) { \
- syslog(LOG_ERR, "usbip err: %13s:%4d (%-12s) " fmt "\n", \
- __FILE__, __LINE__, __FUNCTION__, ##args); \
- } \
- if (usbip_use_stderr) { \
- fprintf(stderr, "usbip err: %13s:%4d (%-12s) " fmt "\n", \
- __FILE__, __LINE__, __FUNCTION__, ##args); \
- } \
-} while (0)
-
-#define notice(fmt, args...) do { \
- if (usbip_use_syslog) { \
- syslog(LOG_DEBUG, "usbip: " fmt, ##args); \
- } \
- if (usbip_use_stderr) { \
- fprintf(stderr, "usbip: " fmt "\n", ##args); \
- } \
-} while (0)
-
-#define info(fmt, args...) do { \
- if (usbip_use_syslog) { \
- syslog(LOG_DEBUG, fmt, ##args); \
- } \
- if (usbip_use_stderr) { \
- fprintf(stderr, fmt "\n", ##args); \
- } \
-} while (0)
-
-#define dbg(fmt, args...) do { \
- if (usbip_use_debug) { \
- if (usbip_use_syslog) { \
- syslog(LOG_DEBUG, "usbip dbg: %13s:%4d (%-12s) " fmt, \
- __FILE__, __LINE__, __FUNCTION__, ##args); \
- } \
- if (usbip_use_stderr) { \
- fprintf(stderr, "usbip dbg: %13s:%4d (%-12s) " fmt "\n", \
- __FILE__, __LINE__, __FUNCTION__, ##args); \
- } \
- } \
-} while (0)
-
-
-#define BUG() do { err("sorry, it's a bug"); abort(); } while (0)
-
+#define PROGNAME "usbip"
+
+#define pr_fmt(fmt) "%s: %s: " fmt "\n", PROGNAME
+#define dbg_fmt(fmt) pr_fmt("%s:%d:[%s] " fmt), "debug", \
+ __FILE__, __LINE__, __FUNCTION__
+
+#define err(fmt, args...) \
+ do { \
+ if (usbip_use_syslog) { \
+ syslog(LOG_ERR, pr_fmt(fmt), "error", ##args); \
+ } \
+ if (usbip_use_stderr) { \
+ fprintf(stderr, pr_fmt(fmt), "error", ##args); \
+ } \
+ } while (0)
+
+#define info(fmt, args...) \
+ do { \
+ if (usbip_use_syslog) { \
+ syslog(LOG_INFO, pr_fmt(fmt), "info", ##args); \
+ } \
+ if (usbip_use_stderr) { \
+ fprintf(stderr, pr_fmt(fmt), "info", ##args); \
+ } \
+ } while (0)
+
+#define dbg(fmt, args...) \
+ do { \
+ if (usbip_use_debug) { \
+ if (usbip_use_syslog) { \
+ syslog(LOG_DEBUG, dbg_fmt(fmt), ##args); \
+ } \
+ if (usbip_use_stderr) { \
+ fprintf(stderr, dbg_fmt(fmt), ##args); \
+ } \
+ } \
+ } while (0)
+
+#define BUG() \
+ do { \
+ err("sorry, it's a bug!"); \
+ abort(); \
+ } while (0)

struct usbip_usb_interface {
uint8_t bInterfaceClass;
--
1.7.5.2

2011-06-20 05:45:35

by matt mooney

[permalink] [raw]
Subject: [PATCH 03/22] staging: usbip: userspace: usbip_common.h: move enums

Relocate enums to follow logging macros.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/libsrc/usbip_common.h | 50 +++++++++----------
1 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.h b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
index 090b7c5..7dadb21 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.h
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
@@ -30,30 +30,6 @@
#define USBIP_HOST_DRV_NAME "usbip-host"
#define USBIP_VHCI_DRV_NAME "vhci_hcd"

-enum usb_device_speed {
- USB_SPEED_UNKNOWN = 0, /* enumerating */
- USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
- USB_SPEED_HIGH, /* usb 2.0 */
- USB_SPEED_VARIABLE /* wireless (usb 2.5) */
-};
-
-/* FIXME: how to sync with drivers/usbip_common.h ? */
-enum usbip_device_status{
- /* sdev is available. */
- SDEV_ST_AVAILABLE = 0x01,
- /* sdev is now used. */
- SDEV_ST_USED,
- /* sdev is unusable because of a fatal error. */
- SDEV_ST_ERROR,
-
- /* vdev does not connect a remote device. */
- VDEV_ST_NULL,
- /* vdev is used, but the USB address is not assigned yet */
- VDEV_ST_NOTASSIGNED,
- VDEV_ST_USED,
- VDEV_ST_ERROR
-};
-
extern int usbip_use_syslog;
extern int usbip_use_stderr;
extern int usbip_use_debug ;
@@ -102,6 +78,30 @@ extern int usbip_use_debug ;
abort(); \
} while (0)

+enum usb_device_speed {
+ USB_SPEED_UNKNOWN = 0, /* enumerating */
+ USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
+ USB_SPEED_HIGH, /* usb 2.0 */
+ USB_SPEED_VARIABLE /* wireless (usb 2.5) */
+};
+
+/* FIXME: how to sync with drivers/usbip_common.h ? */
+enum usbip_device_status{
+ /* sdev is available. */
+ SDEV_ST_AVAILABLE = 0x01,
+ /* sdev is now used. */
+ SDEV_ST_USED,
+ /* sdev is unusable because of a fatal error. */
+ SDEV_ST_ERROR,
+
+ /* vdev does not connect a remote device. */
+ VDEV_ST_NULL,
+ /* vdev is used, but the USB address is not assigned yet */
+ VDEV_ST_NOTASSIGNED,
+ VDEV_ST_USED,
+ VDEV_ST_ERROR
+};
+
struct usbip_usb_interface {
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
@@ -109,8 +109,6 @@ struct usbip_usb_interface {
uint8_t padding; /* alignment */
} __attribute__((packed));

-
-
struct usbip_usb_device {
char path[SYSFS_PATH_MAX];
char busid[SYSFS_BUS_ID_SIZE];
--
1.7.5.2

2011-06-20 06:01:58

by matt mooney

[permalink] [raw]
Subject: [PATCH 04/22] staging: usbip: userspace: usbip_common.h: fixup header includes

Remove unnecessary headers from the file, and add the now missing
headers into the files that actually need them.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/libsrc/stub_driver.c | 2 ++
.../staging/usbip/userspace/libsrc/usbip_common.h | 18 ++++++++----------
drivers/staging/usbip/userspace/src/utils.c | 1 +
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.c b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
index 4d4d171..6a1272d 100644
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
@@ -4,6 +4,8 @@

#include <sys/types.h>
#include <sys/stat.h>
+
+#include <errno.h>
#include <unistd.h>

#include "usbip.h"
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.h b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
index 7dadb21..eedefbd 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.h
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.h
@@ -2,20 +2,18 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

-#ifndef _USBIP_COMMON_H
-#define _USBIP_COMMON_H
+#ifndef __USBIP_COMMON_H
+#define __USBIP_COMMON_H
+
+#include <sysfs/libsysfs.h>

-#include <unistd.h>
#include <stdint.h>
-#include <syslog.h>
-#include <errno.h>
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>

-#include <sysfs/libsysfs.h>
-#include <netdb.h>
-#include <sys/socket.h>
+#include <syslog.h>
+#include <unistd.h>

#ifndef USBIDS_FILE
#define USBIDS_FILE "/usr/share/hwdata/usb.ids"
@@ -146,4 +144,4 @@ void usbip_names_free(void);
void usbip_names_get_product(char *buff, size_t size, uint16_t vendor, uint16_t product);
void usbip_names_get_class(char *buff, size_t size, uint8_t class, uint8_t subclass, uint8_t protocol);

-#endif
+#endif /* __USBIP_COMMON_H */
diff --git a/drivers/staging/usbip/userspace/src/utils.c b/drivers/staging/usbip/userspace/src/utils.c
index 1da1109..2a25cec 100644
--- a/drivers/staging/usbip/userspace/src/utils.c
+++ b/drivers/staging/usbip/userspace/src/utils.c
@@ -6,6 +6,7 @@
#include <sysfs/libsysfs.h>

#include <limits.h>
+#include <errno.h>
#include <stdio.h>
#include <string.h>

--
1.7.5.2

2011-06-20 05:53:07

by matt mooney

[permalink] [raw]
Subject: [PATCH 05/22] staging: usbip: userspace: libsrc: set program name for logging

Set the program name to "libusbip" to identify that the message is
from the library code.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/libsrc/stub_driver.c | 3 +++
.../staging/usbip/userspace/libsrc/usbip_common.c | 3 +++
.../staging/usbip/userspace/libsrc/vhci_driver.c | 3 +++
3 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.c b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
index 6a1272d..b62f6b2 100644
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
@@ -10,6 +10,9 @@

#include "usbip.h"

+#undef PROGNAME
+#define PROGNAME "libusbip"
+
struct usbip_stub_driver *stub_driver;

static struct sysfs_driver *open_sysfs_stub_driver(void)
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.c b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
index e9d0614..6ac4361 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.c
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
@@ -5,6 +5,9 @@
#include "usbip.h"
#include "names.h"

+#undef PROGNAME
+#define PROGNAME "libusbip"
+
int usbip_use_syslog = 0;
int usbip_use_stderr = 0;
int usbip_use_debug = 0;
diff --git a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
index 386f63b..f10121c 100644
--- a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
@@ -5,6 +5,9 @@

#include "usbip.h"

+#undef PROGNAME
+#define PROGNAME "libusbip"
+
struct usbip_vhci_driver *vhci_driver;

static struct usbip_imported_device *imported_device_init(struct usbip_imported_device *idev, char *busid)
--
1.7.5.2

2011-06-20 05:48:13

by matt mooney

[permalink] [raw]
Subject: [PATCH 06/22] staging: usbip: userspace: usbip.c: add log option

Add option for logging with syslog, and default to use stderr for
error and info messages.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip.c | 13 ++++++++++---
1 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip.c b/drivers/staging/usbip/userspace/src/usbip.c
index 8940cd0..cdfe4c2 100644
--- a/drivers/staging/usbip/userspace/src/usbip.c
+++ b/drivers/staging/usbip/userspace/src/usbip.c
@@ -23,6 +23,7 @@
#include <stdlib.h>

#include <getopt.h>
+#include <syslog.h>

#include "usbip_common.h"
#include "usbip.h"
@@ -33,7 +34,7 @@ static int usbip_version(int argc, char *argv[]);
static const char usbip_version_string[] = PACKAGE_STRING;

static const char usbip_usage_string[] =
- "usbip [--debug] [version]\n"
+ "usbip [--debug] [--log] [version]\n"
" [help] <command> <args>\n";

static void usbip_usage(void)
@@ -138,12 +139,15 @@ int main(int argc, char *argv[])
{
static const struct option opts[] = {
{ "debug", no_argument, NULL, 'd' },
- { NULL, 0, NULL, 0 }
+ { "log", no_argument, NULL, 'l' },
+ { NULL, 0, NULL, 0 }
};
+
char *cmd;
int opt;
int i, rc = -1;

+ usbip_use_stderr = 1;
opterr = 0;
for (;;) {
opt = getopt_long(argc, argv, "+d", opts, NULL);
@@ -154,7 +158,10 @@ int main(int argc, char *argv[])
switch (opt) {
case 'd':
usbip_use_debug = 1;
- usbip_use_stderr = 1;
+ break;
+ case 'l':
+ usbip_use_syslog = 1;
+ openlog("", LOG_PID, LOG_USER);
break;
default:
goto err_out;
--
1.7.5.2

2011-06-20 05:52:04

by matt mooney

[permalink] [raw]
Subject: [PATCH 07/22] staging: usbip: userspace: libsrc: remove usbip.h

Remove the library version of usbip.h because its sole purpose was to
include other headers, which is bad practice. Also modify include
guards for consistency.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/Makefile.am | 2 +-
drivers/staging/usbip/userspace/libsrc/Makefile.am | 2 +-
.../staging/usbip/userspace/libsrc/stub_driver.c | 3 ++-
.../staging/usbip/userspace/libsrc/stub_driver.h | 11 ++++++-----
drivers/staging/usbip/userspace/libsrc/usbip.h | 19 -------------------
.../staging/usbip/userspace/libsrc/usbip_common.c | 2 +-
.../staging/usbip/userspace/libsrc/vhci_driver.c | 4 ++--
.../staging/usbip/userspace/libsrc/vhci_driver.h | 13 ++++++++-----
8 files changed, 21 insertions(+), 35 deletions(-)
delete mode 100644 drivers/staging/usbip/userspace/libsrc/usbip.h

diff --git a/drivers/staging/usbip/userspace/Makefile.am b/drivers/staging/usbip/userspace/Makefile.am
index 4b66cbe..d557fe9 100644
--- a/drivers/staging/usbip/userspace/Makefile.am
+++ b/drivers/staging/usbip/userspace/Makefile.am
@@ -1,6 +1,6 @@
SUBDIRS := libsrc src
includedir := @includedir@/usbip
include_HEADERS := $(addprefix libsrc/, \
- usbip.h usbip_common.h vhci_driver.h stub_driver.h)
+ usbip_common.h vhci_driver.h stub_driver.h)

dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbip_bind_driver.8)
diff --git a/drivers/staging/usbip/userspace/libsrc/Makefile.am b/drivers/staging/usbip/userspace/libsrc/Makefile.am
index 77ecf6b..6696aa7 100644
--- a/drivers/staging/usbip/userspace/libsrc/Makefile.am
+++ b/drivers/staging/usbip/userspace/libsrc/Makefile.am
@@ -3,5 +3,5 @@ libusbip_la_CFLAGS := @EXTRA_CFLAGS@
libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@

lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h stub_driver.c stub_driver.h usbip.h \
+libusbip_la_SOURCES := names.c names.h stub_driver.c stub_driver.h \
usbip_common.c usbip_common.h vhci_driver.c vhci_driver.h
diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.c b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
index b62f6b2..0f9593b 100644
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
@@ -8,7 +8,8 @@
#include <errno.h>
#include <unistd.h>

-#include "usbip.h"
+#include "usbip_common.h"
+#include "stub_driver.h"

#undef PROGNAME
#define PROGNAME "libusbip"
diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.h b/drivers/staging/usbip/userspace/libsrc/stub_driver.h
index 332ebc5..9eaf92c 100644
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.h
+++ b/drivers/staging/usbip/userspace/libsrc/stub_driver.h
@@ -2,11 +2,11 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

-#ifndef _USBIP_STUB_DRIVER_H
-#define _USBIP_STUB_DRIVER_H
-
-#include "usbip.h"
+#ifndef __USBIP_STUB_DRIVER_H
+#define __USBIP_STUB_DRIVER_H

+#include <stdint.h>
+#include "usbip_common.h"

struct usbip_stub_driver {
int ndevs;
@@ -33,4 +33,5 @@ int usbip_stub_refresh_device_list(void);
int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd);

struct usbip_exported_device *usbip_stub_get_device(int num);
-#endif
+
+#endif /* __USBIP_STUB_DRIVER_H */
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip.h b/drivers/staging/usbip/userspace/libsrc/usbip.h
deleted file mode 100644
index 7cb8e6f..0000000
--- a/drivers/staging/usbip/userspace/libsrc/usbip.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
- */
-
-#ifndef _USBIP_H
-#define _USBIP_H
-
-#ifdef HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
-#include "usbip_common.h"
-#include "stub_driver.h"
-#include "vhci_driver.h"
-#ifdef DMALLOC
-#include <dmalloc.h>
-#endif
-
-#endif
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.c b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
index 6ac4361..e0ec23f 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.c
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
@@ -2,7 +2,7 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

-#include "usbip.h"
+#include "usbip_common.h"
#include "names.h"

#undef PROGNAME
diff --git a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
index f10121c..e663fab 100644
--- a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
@@ -2,8 +2,8 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

-
-#include "usbip.h"
+#include "usbip_common.h"
+#include "vhci_driver.h"

#undef PROGNAME
#define PROGNAME "libusbip"
diff --git a/drivers/staging/usbip/userspace/libsrc/vhci_driver.h b/drivers/staging/usbip/userspace/libsrc/vhci_driver.h
index a2f7db1..89949aa 100644
--- a/drivers/staging/usbip/userspace/libsrc/vhci_driver.h
+++ b/drivers/staging/usbip/userspace/libsrc/vhci_driver.h
@@ -2,13 +2,15 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

-#ifndef _VHCI_DRIVER_H
-#define _VHCI_DRIVER_H
+#ifndef __VHCI_DRIVER_H
+#define __VHCI_DRIVER_H

-#include "usbip.h"
+#include <sysfs/libsysfs.h>
+#include <stdint.h>

-#define USBIP_VHCI_BUS_TYPE "platform"
+#include "usbip_common.h"

+#define USBIP_VHCI_BUS_TYPE "platform"
#define MAXNPORT 128

struct usbip_class_device {
@@ -61,4 +63,5 @@ int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
uint8_t devnum, uint32_t speed);

int usbip_vhci_detach_device(uint8_t port);
-#endif
+
+#endif /* __VHCI_DRIVER_H */
--
1.7.5.2

2011-06-20 05:52:07

by matt mooney

[permalink] [raw]
Subject: [PATCH 08/22] staging: usbip: userspace: libsrc: change all output messages to debug

The library should not be displaying random messages intermixed with
those from the programs that use them. So, instead, change all of the
output from the library to debug only, and allow the programs to
decide what to tell the user. This also changes the messages to use
the same form, which makes understanding them easier.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/libsrc/stub_driver.c | 55 ++++++------
.../staging/usbip/userspace/libsrc/usbip_common.c | 14 ++--
.../staging/usbip/userspace/libsrc/vhci_driver.c | 94 ++++++++++----------
3 files changed, 80 insertions(+), 83 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.c b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
index 0f9593b..604aed1 100644
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
@@ -27,7 +27,7 @@ static struct sysfs_driver *open_sysfs_stub_driver(void)

ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
if (ret < 0) {
- err("sysfs must be mounted");
+ dbg("sysfs_get_mnt_path failed");
return NULL;
}

@@ -37,8 +37,7 @@ static struct sysfs_driver *open_sysfs_stub_driver(void)

stub_driver = sysfs_open_driver_path(stub_driver_path);
if (!stub_driver) {
- err(USBIP_CORE_MOD_NAME ".ko and " USBIP_HOST_DRV_NAME
- ".ko must be loaded");
+ dbg("sysfs_open_driver_path failed");
return NULL;
}

@@ -82,7 +81,7 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
break;

if (errno != ENOENT) {
- err("error stat'ing %s", attrpath);
+ dbg("stat failed: %s", attrpath);
return -1;
}

@@ -91,21 +90,21 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
}

if (retries == 0)
- err("usbip_status not ready after %d retries",
- SYSFS_OPEN_RETRIES);
+ dbg("usbip_status not ready after %d retries",
+ SYSFS_OPEN_RETRIES);
else if (retries < SYSFS_OPEN_RETRIES)
- info("warning: usbip_status ready after %d retries",
- SYSFS_OPEN_RETRIES - retries);
+ dbg("warning: usbip_status ready after %d retries",
+ SYSFS_OPEN_RETRIES - retries);

attr = sysfs_open_attribute(attrpath);
if (!attr) {
- err("open %s", attrpath);
+ dbg("sysfs_open_attribute failed: %s", attrpath);
return -1;
}

ret = sysfs_read_attribute(attr);
if (ret) {
- err("read %s", attrpath);
+ dbg("sysfs_read_attribute failed: %s", attrpath);
sysfs_close_attribute(attr);
return -1;
}
@@ -134,13 +133,13 @@ static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)

edev = (struct usbip_exported_device *) calloc(1, sizeof(*edev));
if (!edev) {
- err("alloc device");
+ dbg("calloc failed");
return NULL;
}

edev->sudev = sysfs_open_device_path(sdevpath);
if (!edev->sudev) {
- err("open %s", sdevpath);
+ dbg("sysfs_open_device_path failed: %s", sdevpath);
goto err;
}

@@ -155,7 +154,7 @@ static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
sizeof(struct usbip_usb_interface);
edev = (struct usbip_exported_device *) realloc(edev, size);
if (!edev) {
- err("alloc device");
+ dbg("realloc failed");
goto err;
}

@@ -204,8 +203,8 @@ static int refresh_exported_devices(void)

suinf_list = sysfs_get_driver_devices(stub_driver->sysfs_driver);
if (!suinf_list) {
- info("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
- "exportable!\n");
+ dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
+ "exportable!\n");
goto bye;
}

@@ -215,7 +214,7 @@ static int refresh_exported_devices(void)
/* get usb device of this usb interface */
sudev = sysfs_get_device_parent(suinf);
if (!sudev) {
- err("get parent dev of %s", suinf->name);
+ dbg("sysfs_get_device_parent failed: %s", suinf->name);
continue;
}

@@ -229,7 +228,7 @@ static int refresh_exported_devices(void)

edev = usbip_exported_device_new(sudev->path);
if (!edev) {
- err("usbip_exported_device new");
+ dbg("usbip_exported_device_new failed");
continue;
}

@@ -257,7 +256,7 @@ int usbip_stub_refresh_device_list(void)
stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
usbip_exported_device_delete);
if (!stub_driver->edev_list) {
- err("alloc dlist");
+ dbg("dlist_new_with_delete failed");
return -1;
}

@@ -275,7 +274,7 @@ int usbip_stub_driver_open(void)

stub_driver = (struct usbip_stub_driver *) calloc(1, sizeof(*stub_driver));
if (!stub_driver) {
- err("alloc stub_driver");
+ dbg("calloc failed");
return -1;
}

@@ -284,7 +283,7 @@ int usbip_stub_driver_open(void)
stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
usbip_exported_device_delete);
if (!stub_driver->edev_list) {
- err("alloc dlist");
+ dbg("dlist_new_with_delete failed");
goto err;
}

@@ -334,16 +333,16 @@ int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd)


if (edev->status != SDEV_ST_AVAILABLE) {
- info("device not available, %s", edev->udev.busid);
+ dbg("device not available: %s", edev->udev.busid);
switch( edev->status ) {
case SDEV_ST_ERROR:
- info(" status SDEV_ST_ERROR");
+ dbg("status SDEV_ST_ERROR");
break;
case SDEV_ST_USED:
- info(" status SDEV_ST_USED");
+ dbg("status SDEV_ST_USED");
break;
default:
- info(" status unknown: 0x%x", edev->status);
+ dbg("status unknown: 0x%x", edev->status);
}
return -1;
}
@@ -357,21 +356,21 @@ int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd)

attr = sysfs_open_attribute(attrpath);
if (!attr) {
- err("open %s", attrpath);
+ dbg("sysfs_open_attribute failed: %s", attrpath);
return -1;
}

snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);

dbg("write: %s", sockfd_buff);
-
ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
if (ret < 0) {
- err("write sockfd %s to %s", sockfd_buff, attrpath);
+ dbg("sysfs_write_attribute failed: sockfd %s to %s",
+ sockfd_buff, attrpath);
goto err_write_sockfd;
}

- info("connect %s", edev->udev.busid);
+ dbg("connect: %s", edev->udev.busid);

err_write_sockfd:
sysfs_close_attribute(attr);
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_common.c b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
index e0ec23f..154b4b1 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_common.c
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_common.c
@@ -120,19 +120,19 @@ int read_attr_value(struct sysfs_device *dev, const char *name, const char *form

attr = sysfs_open_attribute(attrpath);
if (!attr) {
- err("open attr %s", attrpath);
+ dbg("sysfs_open_attribute failed: %s", attrpath);
return 0;
}

ret = sysfs_read_attribute(attr);
if (ret < 0) {
- err("read attr");
+ dbg("sysfs_read_attribute failed");
goto err;
}

ret = sscanf(attr->value, format, &num);
if (ret < 1) {
- err("sscanf");
+ dbg("sscanf failed");
goto err;
}

@@ -154,19 +154,19 @@ int read_attr_speed(struct sysfs_device *dev)

attr = sysfs_open_attribute(attrpath);
if (!attr) {
- err("open attr");
+ dbg("sysfs_open_attribute failed: %s", attrpath);
return 0;
}

ret = sysfs_read_attribute(attr);
if (ret < 0) {
- err("read attr");
+ dbg("sysfs_read_attribute failed");
goto err;
}

ret = sscanf(attr->value, "%s\n", speed);
if (ret < 1) {
- err("sscanf");
+ dbg("sscanf failed");
goto err;
}
err:
@@ -222,7 +222,7 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,

sif = sysfs_open_device("usb", busid);
if (!sif) {
- err("open sif of %s", busid);
+ dbg("sysfs_open_device(\"usb\", \"%s\") failed", busid);
return -1;
}

diff --git a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
index e663fab..abbc285 100644
--- a/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/vhci_driver.c
@@ -16,7 +16,7 @@ static struct usbip_imported_device *imported_device_init(struct usbip_imported_

sudev = sysfs_open_device("usb", busid);
if (!sudev) {
- err("sysfs_open_device %s", busid);
+ dbg("sysfs_open_device failed: %s", busid);
goto err;
}
read_usb_device(sudev, &idev->udev);
@@ -71,7 +71,7 @@ static int parse_status(char *value)
&devid, &socket, lbusid);

if (ret < 5) {
- err("scanf %d", ret);
+ dbg("sscanf failed: %d", ret);
BUG();
}

@@ -94,14 +94,14 @@ static int parse_status(char *value)

idev->cdev_list = dlist_new(sizeof(struct usbip_class_device));
if (!idev->cdev_list) {
- err("init new device");
+ dbg("dlist_new failed");
return -1;
}

if (idev->status != VDEV_ST_NULL && idev->status != VDEV_ST_NOTASSIGNED) {
idev = imported_device_init(idev, lbusid);
if (!idev) {
- err("init new device");
+ dbg("imported_device_init failed");
return -1;
}
}
@@ -134,7 +134,7 @@ static int check_usbip_device(struct sysfs_class_device *cdev)
/* found usbip device */
usbip_cdev = calloc(1, sizeof(*usbip_cdev));
if (!cdev) {
- err("calloc usbip_cdev");
+ dbg("calloc failed");
return -1;
}
dlist_unshift(vhci_driver->cdev_list, usbip_cdev);
@@ -142,7 +142,7 @@ static int check_usbip_device(struct sysfs_class_device *cdev)
sizeof(usbip_cdev->class_path));
strncpy(usbip_cdev->dev_path, dev_path,
sizeof(usbip_cdev->dev_path));
- dbg(" found %s %s", class_path, dev_path);
+ dbg("found: %s %s", class_path, dev_path);
}
}

@@ -159,11 +159,11 @@ static int search_class_for_usbip_device(char *cname)

class = sysfs_open_class(cname);
if (!class) {
- err("open class");
+ dbg("sysfs_open_class failed");
return -1;
}

- dbg("class %s", class->name);
+ dbg("class: %s", class->name);

cdev_list = sysfs_get_class_devices(class);
if (!cdev_list)
@@ -171,7 +171,7 @@ static int search_class_for_usbip_device(char *cname)
goto out;

dlist_for_each_data(cdev_list, cdev, struct sysfs_class_device) {
- dbg(" cdev %s", cdev->name);
+ dbg("cdev: %s", cdev->name);
ret = check_usbip_device(cdev);
if (ret < 0)
goto out;
@@ -194,7 +194,7 @@ static int refresh_class_device_list(void)

ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
if (ret < 0) {
- err("sysfs must be mounted");
+ dbg("sysfs_get_mnt_path failed");
return -1;
}

@@ -204,7 +204,7 @@ static int refresh_class_device_list(void)
/* search under /sys/class */
cname_list = sysfs_open_directory_list(class_path);
if (!cname_list) {
- err("open class directory");
+ dbg("sysfs_open_directory failed");
return -1;
}

@@ -234,45 +234,42 @@ static int refresh_imported_device_list(void)

attr_status = sysfs_get_device_attr(vhci_driver->hc_device, "status");
if (!attr_status) {
- err("get attr %s of %s", "status", vhci_driver->hc_device->name);
+ dbg("sysfs_get_device_attr(\"status\") failed: %s",
+ vhci_driver->hc_device->name);
return -1;
}

- dbg("name %s, path %s, len %d, method %d\n", attr_status->name,
- attr_status->path, attr_status->len, attr_status->method);
-
- dbg("%s", attr_status->value);
+ dbg("name: %s path: %s len: %d method: %d value: %s",
+ attr_status->name, attr_status->path, attr_status->len,
+ attr_status->method, attr_status->value);

return parse_status(attr_status->value);
}

static int get_nports(void)
{
+ char *c;
int nports = 0;
struct sysfs_attribute *attr_status;

attr_status = sysfs_get_device_attr(vhci_driver->hc_device, "status");
if (!attr_status) {
- err("get attr %s of %s", "status", vhci_driver->hc_device->name);
+ dbg("sysfs_get_device_attr(\"status\") failed: %s",
+ vhci_driver->hc_device->name);
return -1;
}

- dbg("name %s, path %s, len %d, method %d\n", attr_status->name,
- attr_status->path, attr_status->len, attr_status->method);
-
- dbg("%s", attr_status->value);
+ dbg("name: %s path: %s len: %d method: %d value: %s",
+ attr_status->name, attr_status->path, attr_status->len,
+ attr_status->method, attr_status->value);

- {
- char *c;
-
- /* skip a header line */
- c = strchr(attr_status->value, '\n') + 1;
+ /* skip a header line */
+ c = strchr(attr_status->value, '\n') + 1;

- while (*c != '\0') {
- /* go to the next line */
- c = strchr(c, '\n') + 1;
- nports += 1;
- }
+ while (*c != '\0') {
+ /* go to the next line */
+ c = strchr(c, '\n') + 1;
+ nports += 1;
}

return nports;
@@ -294,15 +291,15 @@ static int get_hc_busid(char *sysfs_mntpath, char *hc_busid)

sdriver = sysfs_open_driver_path(sdriver_path);
if (!sdriver) {
- info("%s is not found", sdriver_path);
- info("please load " USBIP_CORE_MOD_NAME ".ko and "
- USBIP_VHCI_DRV_NAME ".ko!");
+ dbg("sysfs_open_driver_path failed: %s", sdriver_path);
+ dbg("make sure " USBIP_CORE_MOD_NAME ".ko and "
+ USBIP_VHCI_DRV_NAME ".ko are loaded!");
return -1;
}

hc_devs = sysfs_get_driver_devices(sdriver);
if (!hc_devs) {
- err("get hc list");
+ dbg("sysfs_get_driver failed");
goto err;
}

@@ -318,7 +315,7 @@ err:
if (found)
return 0;

- err("not found usbip hc");
+ dbg("%s not found", hc_busid);
return -1;
}

@@ -332,13 +329,13 @@ int usbip_vhci_driver_open(void)

vhci_driver = (struct usbip_vhci_driver *) calloc(1, sizeof(*vhci_driver));
if (!vhci_driver) {
- err("alloc vhci_driver");
+ dbg("calloc failed");
return -1;
}

ret = sysfs_get_mnt_path(vhci_driver->sysfs_mntpath, SYSFS_PATH_MAX);
if (ret < 0) {
- err("sysfs must be mounted");
+ dbg("sysfs_get_mnt_path failed");
goto err;
}

@@ -350,13 +347,13 @@ int usbip_vhci_driver_open(void)
vhci_driver->hc_device = sysfs_open_device(USBIP_VHCI_BUS_TYPE,
hc_busid);
if (!vhci_driver->hc_device) {
- err("get sysfs vhci_driver");
+ dbg("sysfs_open_device failed");
goto err;
}

vhci_driver->nports = get_nports();

- info("%d ports available\n", vhci_driver->nports);
+ dbg("available ports: %d", vhci_driver->nports);

vhci_driver->cdev_list = dlist_new(sizeof(struct usbip_class_device));
if (!vhci_driver->cdev_list)
@@ -437,7 +434,7 @@ err:
dlist_destroy(vhci_driver->idev[i].cdev_list);
}

- err("refresh device list");
+ dbg("failed to refresh device list");
return -1;
}

@@ -460,7 +457,8 @@ int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,

attr_attach = sysfs_get_device_attr(vhci_driver->hc_device, "attach");
if (!attr_attach) {
- err("get attach");
+ dbg("sysfs_get_device_attr(\"attach\") failed: %s",
+ vhci_driver->hc_device->name);
return -1;
}

@@ -470,11 +468,11 @@ int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,

ret = sysfs_write_attribute(attr_attach, buff, strlen(buff));
if (ret < 0) {
- err("write to attach failed");
+ dbg("sysfs_write_attribute failed");
return -1;
}

- info("port %d attached", port);
+ dbg("attached port: %d", port);

return 0;
}
@@ -501,21 +499,21 @@ int usbip_vhci_detach_device(uint8_t port)

attr_detach = sysfs_get_device_attr(vhci_driver->hc_device, "detach");
if (!attr_detach) {
- err("get detach");
+ dbg("sysfs_get_device_attr(\"detach\") failed: %s",
+ vhci_driver->hc_device->name);
return -1;
}

snprintf(buff, sizeof(buff), "%u", port);
- dbg("writing to detach");
dbg("writing: %s", buff);

ret = sysfs_write_attribute(attr_detach, buff, strlen(buff));
if (ret < 0) {
- err("write to detach failed");
+ dbg("sysfs_write_attribute failed");
return -1;
}

- info("port %d detached", port);
+ dbg("detached port: %d", port);

return 0;
}
--
1.7.5.2

2011-06-20 05:47:44

by matt mooney

[permalink] [raw]
Subject: [PATCH 09/22] staging: usbip: userspace: usbip: modify command failure

When a bad option is given, display a message stating such and output
usage. When a bad command is given, output command help.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip.c | 13 ++++++++-----
1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip.c b/drivers/staging/usbip/userspace/src/usbip.c
index cdfe4c2..583b179 100644
--- a/drivers/staging/usbip/userspace/src/usbip.c
+++ b/drivers/staging/usbip/userspace/src/usbip.c
@@ -125,13 +125,13 @@ static int usbip_version(int argc, char *argv[])
(void) argc;
(void) argv;

- printf("%s\n", usbip_version_string);
+ printf(PROGNAME " (%s)\n", usbip_version_string);
return 0;
}

static int run_command(const struct command *cmd, int argc, char *argv[])
{
- dbg("running command: `%s'\n", cmd->name);
+ dbg("running command: `%s'", cmd->name);
return cmd->fn(argc, argv);
}

@@ -163,8 +163,11 @@ int main(int argc, char *argv[])
usbip_use_syslog = 1;
openlog("", LOG_PID, LOG_USER);
break;
+ case '?':
+ printf("usbip: invalid option\n");
default:
- goto err_out;
+ usbip_usage();
+ goto out;
}
}

@@ -180,8 +183,8 @@ int main(int argc, char *argv[])
}
}

-err_out:
- usbip_usage();
+ /* invalid command */
+ usbip_help(0, NULL);
out:
return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
--
1.7.5.2

2011-06-20 05:47:42

by matt mooney

[permalink] [raw]
Subject: [PATCH 10/22] staging: usbip: userspace: utils: remove libsysfs circumvention

Removes all of the helper functions that used a lot of hard-coded
values intead of libsysfs. Most of these functions were unused
anyway.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/utils.c | 250 ---------------------------
drivers/staging/usbip/userspace/src/utils.h | 31 ++--
2 files changed, 18 insertions(+), 263 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/utils.c b/drivers/staging/usbip/userspace/src/utils.c
index 2a25cec..9b31c78 100644
--- a/drivers/staging/usbip/userspace/src/utils.c
+++ b/drivers/staging/usbip/userspace/src/utils.c
@@ -62,253 +62,3 @@ int modify_match_busid(char *busid, int add)

return 0;
}
-
-int read_integer(char *path)
-{
- char buff[100];
- int fd;
- int ret = 0;
-
- memset(buff, 0, sizeof(buff));
-
- fd = open(path, O_RDONLY);
- if (fd < 0)
- return -1;
-
- ret = read(fd, buff, sizeof(buff));
- if (ret < 0) {
- close(fd);
- return -1;
- }
-
- sscanf(buff, "%d", &ret);
-
- close(fd);
-
- return ret;
-}
-
-int read_string(char *path, char *string, size_t len)
-{
- int fd;
- int ret = 0;
- char *p;
-
- memset(string, 0, len);
-
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- string = NULL;
- return -1;
- }
-
- ret = read(fd, string, len-1);
- if (ret < 0) {
- string = NULL;
- close(fd);
- return -1;
- }
-
- p = strchr(string, '\n');
- *p = '\0';
-
- close(fd);
-
- return 0;
-}
-
-int write_integer(char *path, int value)
-{
- int fd;
- int ret;
- char buff[100];
-
- snprintf(buff, sizeof(buff), "%d", value);
-
- fd = open(path, O_WRONLY);
- if (fd < 0)
- return -1;
-
- ret = write(fd, buff, strlen(buff));
- if (ret < 0) {
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-int read_bConfigurationValue(char *busid)
-{
- char path[PATH_MAX];
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
-
- return read_integer(path);
-}
-
-int write_bConfigurationValue(char *busid, int config)
-{
- char path[PATH_MAX];
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
-
- return write_integer(path, config);
-}
-
-int read_bNumInterfaces(char *busid)
-{
- char path[PATH_MAX];
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
-
- return read_integer(path);
-}
-
-int read_bDeviceClass(char *busid)
-{
- char path[PATH_MAX];
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
-
- return read_integer(path);
-}
-
-int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
-{
- char path[PATH_MAX];
- char linkto[PATH_MAX];
- const char none[] = "none";
- int ret;
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
-
- /* readlink does not add NULL */
- memset(linkto, 0, sizeof(linkto));
- ret = readlink(path, linkto, sizeof(linkto)-1);
- if (ret < 0) {
- strncpy(driver, none, len);
- return -1;
- } else {
- strncpy(driver, basename(linkto), len);
- return 0;
- }
-}
-
-int getdevicename(char *busid, char *name, size_t len)
-{
- char path[PATH_MAX];
- char idProduct[10], idVendor[10];
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
- read_string(path, idVendor, sizeof(idVendor));
-
- snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
- read_string(path, idProduct, sizeof(idProduct));
-
- if (!idVendor[0] || !idProduct[0])
- return -1;
-
- snprintf(name, len, "%s:%s", idVendor, idProduct);
-
- return 0;
-}
-
-#define MAXLINE 100
-
-/* if this cannot read a whole line, return -1 */
-int readline(int sockfd, char *buff, int bufflen)
-{
- int ret;
- char c;
- int index = 0;
-
-
- while (index < bufflen) {
- ret = read(sockfd, &c, sizeof(c));
- if (ret < 0 && errno == EINTR)
- continue;
- if (ret <= 0) {
- return -1;
- }
-
- buff[index] = c;
-
- if ( index > 0 && buff[index-1] == '\r' && buff[index] == '\n') {
- /* end of line */
- buff[index-1] = '\0'; /* get rid of delimitor */
- return index;
- } else
- index++;
- }
-
- return -1;
-}
-
-#if 0
-int writeline(int sockfd, char *str, int strlen)
-{
- int ret;
- int index = 0;
- int len;
- char buff[MAXLINE];
-
- if (strlen + 3 > MAXLINE)
- return -1;
-
- strncpy(buff, str, strlen);
- buff[strlen+1] = '\r';
- buff[strlen+2] = '\n';
- buff[strlen+3] = '\0';
-
- len = strlen + 3;
-
- while (len > 0) {
- ret = write(sockfd, buff+index, len);
- if (ret <= 0) {
- return -1;
- }
-
- len -= ret;
- index += ret;
- }
-
- return index;
-}
-#endif
-
-int writeline(int sockfd, char *str, int strlen)
-{
- int ret;
- int index = 0;
- int len;
- char buff[MAXLINE];
-
- len = strnlen(str, strlen);
-
- if (strlen + 2 > MAXLINE)
- return -1;
-
- memcpy(buff, str, strlen);
- buff[strlen] = '\r';
- buff[strlen+1] = '\n'; /* strlen+1 <= MAXLINE-1 */
-
- len = strlen + 2;
-
- while (len > 0) {
- ret = write(sockfd, buff+index, len);
- if (ret < 0 && errno == EINTR)
- continue;
- if (ret <= 0) {
- return -1;
- }
-
- len -= ret;
- index += ret;
- }
-
- return index;
-}
-
diff --git a/drivers/staging/usbip/userspace/src/utils.h b/drivers/staging/usbip/userspace/src/utils.h
index b50e95a..fdcb14d 100644
--- a/drivers/staging/usbip/userspace/src/utils.h
+++ b/drivers/staging/usbip/userspace/src/utils.h
@@ -1,19 +1,24 @@
+/*
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 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/>.
+ */
+
#ifndef __UTILS_H
#define __UTILS_H

-#include <stdlib.h>
-
int modify_match_busid(char *busid, int add);
-int read_string(char *path, char *, size_t len);
-int read_integer(char *path);
-int getdevicename(char *busid, char *name, size_t len);
-int getdriver(char *busid, int conf, int infnum, char *driver, size_t len);
-int read_bNumInterfaces(char *busid);
-int read_bConfigurationValue(char *busid);
-int write_integer(char *path, int value);
-int write_bConfigurationValue(char *busid, int config);
-int read_bDeviceClass(char *busid);
-int readline(int sockfd, char *str, int strlen);
-int writeline(int sockfd, char *buff, int bufflen);

#endif /* __UTILS_H */
--
1.7.5.2

2011-06-20 05:46:51

by matt mooney

[permalink] [raw]
Subject: [PATCH 11/22] staging: usbip: userspace: utils.c: rewrite modify_match_busid

Rewrite the function to use libsysfs.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/utils.c | 68 ++++++++++++++++-----------
1 files changed, 40 insertions(+), 28 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/utils.c b/drivers/staging/usbip/userspace/src/utils.c
index 9b31c78..2d4966e 100644
--- a/drivers/staging/usbip/userspace/src/utils.c
+++ b/drivers/staging/usbip/userspace/src/utils.c
@@ -1,64 +1,76 @@
/*
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 2005-2007 Takahiro Hirofuchi
*
- * Copyright (C) 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 <sysfs/libsysfs.h>

-#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

-#include <fcntl.h>
-#include <libgen.h>
-#include <unistd.h>
-
#include "usbip_common.h"
#include "utils.h"

int modify_match_busid(char *busid, int add)
{
- int fd;
- int ret;
+ char bus_type[] = "usb";
+ char attr_name[] = "match_busid";
char buff[SYSFS_BUS_ID_SIZE + 4];
char sysfs_mntpath[SYSFS_PATH_MAX];
- char match_busid_path[SYSFS_PATH_MAX];
+ char match_busid_attr_path[SYSFS_PATH_MAX];
+ struct sysfs_attribute *match_busid_attr;
+ int rc, ret = 0;

- ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
- if (ret < 0) {
- err("sysfs must be mounted");
+ if (strnlen(busid, SYSFS_BUS_ID_SIZE) > SYSFS_BUS_ID_SIZE - 1) {
+ dbg("busid is too long");
return -1;
}

- snprintf(match_busid_path, sizeof(match_busid_path),
- "%s/%s/usb/%s/%s/match_busid", sysfs_mntpath, SYSFS_BUS_NAME,
- SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME);
-
- /* BUS_IS_SIZE includes NULL termination? */
- if (strnlen(busid, SYSFS_BUS_ID_SIZE) > SYSFS_BUS_ID_SIZE - 1) {
- dbg("busid is too long");
+ rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
+ if (rc < 0) {
+ err("sysfs must be mounted: %s", strerror(errno));
return -1;
}

- fd = open(match_busid_path, O_WRONLY);
- if (fd < 0)
+ snprintf(match_busid_attr_path, sizeof(match_busid_attr_path),
+ "%s/%s/%s/%s/%s/%s", sysfs_mntpath, SYSFS_BUS_NAME, bus_type,
+ SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
+
+ match_busid_attr = sysfs_open_attribute(match_busid_attr_path);
+ if (!match_busid_attr) {
+ dbg("problem getting match_busid attribute: %s",
+ strerror(errno));
return -1;
+ }

if (add)
snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "add %s", busid);
else
snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "del %s", busid);

- dbg("write \"%s\" to %s", buff, match_busid_path);
+ dbg("write \"%s\" to %s", buff, match_busid_attr->path);

- ret = write(fd, buff, sizeof(buff));
- if (ret < 0) {
- close(fd);
- return -1;
+ rc = sysfs_write_attribute(match_busid_attr, buff, sizeof(buff));
+ if (rc < 0) {
+ dbg("failed to write match_busid: %s", strerror(errno));
+ ret = -1;
}

- close(fd);
+ sysfs_close_attribute(match_busid_attr);

- return 0;
+ return ret;
}
--
1.7.5.2

2011-06-20 05:46:55

by matt mooney

[permalink] [raw]
Subject: [PATCH 12/22] staging: usbip: userspace: usbip_bind.c: major rewrite of the implementation

Rewrite functions in terms of libsysfs, which eliminates a lot of
helper functions simplifying the file layout. Now, the two processes
taking place here, an unbind of the old driver and a bind of
usbip-host, are single functions and have been renamed along with the
controlling function. A check to see if the device is already bound to
usbip-host is now included.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip_bind.c | 303 +++++++++++-----------
1 files changed, 158 insertions(+), 145 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_bind.c b/drivers/staging/usbip/userspace/src/usbip_bind.c
index 978b7aa..9ecaf6e 100644
--- a/drivers/staging/usbip/userspace/src/usbip_bind.c
+++ b/drivers/staging/usbip/userspace/src/usbip_bind.c
@@ -18,18 +18,23 @@

#include <sysfs/libsysfs.h>

-#include <limits.h>
+#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>

-#include <fcntl.h>
#include <getopt.h>
-#include <unistd.h>

#include "usbip_common.h"
#include "utils.h"
#include "usbip.h"

+enum unbind_status {
+ UNBIND_ST_OK,
+ UNBIND_ST_USBIP_HOST,
+ UNBIND_ST_FAILED
+};
+
static const char usbip_bind_usage_string[] =
"usbip bind <args>\n"
" -b, --busid=<busid> Bind " USBIP_HOST_DRV_NAME ".ko to device "
@@ -40,195 +45,202 @@ void usbip_bind_usage(void)
printf("usage: %s", usbip_bind_usage_string);
}

-static const char unbind_path_format[] = "/sys/bus/usb/devices/%s/driver/unbind";
-
-/* buggy driver may cause dead lock */
-static int unbind_interface_busid(char *busid)
+/* call at unbound state */
+static int bind_usbip(char *busid)
{
- char unbind_path[SYSFS_PATH_MAX];
- int fd;
- int ret;
-
- snprintf(unbind_path, sizeof(unbind_path), unbind_path_format, busid);
-
- fd = open(unbind_path, O_WRONLY);
- if (fd < 0) {
- dbg("opening unbind_path failed: %d", fd);
+ char bus_type[] = "usb";
+ char attr_name[] = "bind";
+ char sysfs_mntpath[SYSFS_PATH_MAX];
+ char bind_attr_path[SYSFS_PATH_MAX];
+ char intf_busid[SYSFS_BUS_ID_SIZE];
+ struct sysfs_device *busid_dev;
+ struct sysfs_attribute *bind_attr;
+ struct sysfs_attribute *bConfValue;
+ struct sysfs_attribute *bNumIntfs;
+ int i, failed = 0;
+ int rc, ret = -1;
+
+ rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
+ if (rc < 0) {
+ err("sysfs must be mounted: %s", strerror(errno));
return -1;
}

- ret = write(fd, busid, strnlen(busid, SYSFS_BUS_ID_SIZE));
- if (ret < 0) {
- dbg("write to unbind_path failed: %d", ret);
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-static int unbind_interface(char *busid, int configvalue, int interface)
-{
- char inf_busid[SYSFS_BUS_ID_SIZE];
- dbg("unbinding interface");
-
- snprintf(inf_busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d", busid, configvalue,
- interface);
+ snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
+ sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
+ USBIP_HOST_DRV_NAME, attr_name);

- return unbind_interface_busid(inf_busid);
-}
-
-static int unbind(char *busid)
-{
- int configvalue = 0;
- int ninterface = 0;
- int devclass = 0;
- int i;
- int failed = 0;
-
- configvalue = read_bConfigurationValue(busid);
- ninterface = read_bNumInterfaces(busid);
- devclass = read_bDeviceClass(busid);
-
- if (configvalue < 0 || ninterface < 0 || devclass < 0) {
- dbg("read config and ninf value, removed?");
+ bind_attr = sysfs_open_attribute(bind_attr_path);
+ if (!bind_attr) {
+ dbg("problem getting bind attribute: %s", strerror(errno));
return -1;
}

- if (devclass == 0x09) {
- dbg("skip unbinding of hub");
- return -1;
+ busid_dev = sysfs_open_device(bus_type, busid);
+ if (!busid_dev) {
+ dbg("sysfs_open_device %s failed: %s", busid, strerror(errno));
+ goto err_close_bind_attr;
}

- for (i = 0; i < ninterface; i++) {
- char driver[PATH_MAX];
- int ret;
-
- memset(&driver, 0, sizeof(driver));
-
- getdriver(busid, configvalue, i, driver, PATH_MAX-1);
+ bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
+ bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces");

- dbg(" %s:%d.%d -> %s ", busid, configvalue, i, driver);
-
- if (!strncmp("none", driver, PATH_MAX))
- continue; /* unbound interface */
+ if (!bConfValue || !bNumIntfs) {
+ dbg("problem getting device attributes: %s",
+ strerror(errno));
+ goto err_close_busid_dev;
+ }

-#if 0
- if (!strncmp("usbip", driver, PATH_MAX))
- continue; /* already bound to usbip */
-#endif
+ for (i = 0; i < atoi(bNumIntfs->value); i++) {
+ snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid,
+ bConfValue->value, i);

- /* unbinding */
- ret = unbind_interface(busid, configvalue, i);
- if (ret < 0) {
- dbg("unbind driver at %s:%d.%d failed",
- busid, configvalue, i);
+ rc = sysfs_write_attribute(bind_attr, intf_busid,
+ SYSFS_BUS_ID_SIZE);
+ if (rc < 0) {
+ dbg("bind driver at %s failed", intf_busid);
failed = 1;
}
}

- if (failed)
- return -1;
- else
- return 0;
-}
-
-static const char bind_path_format[] = "/sys/bus/usb/drivers/%s/bind";
+ if (!failed)
+ ret = 0;

-static int bind_interface_busid(char *busid, char *driver)
-{
- char bind_path[PATH_MAX];
- int fd;
- int ret;
+err_close_busid_dev:
+ sysfs_close_device(busid_dev);
+err_close_bind_attr:
+ sysfs_close_attribute(bind_attr);

- snprintf(bind_path, sizeof(bind_path), bind_path_format, driver);
+ return ret;
+}

- fd = open(bind_path, O_WRONLY);
- if (fd < 0)
+/* buggy driver may cause dead lock */
+static int unbind_other(char *busid)
+{
+ char bus_type[] = "usb";
+ char intf_busid[SYSFS_BUS_ID_SIZE];
+ struct sysfs_device *busid_dev;
+ struct sysfs_device *intf_dev;
+ struct sysfs_driver *intf_drv;
+ struct sysfs_attribute *unbind_attr;
+ struct sysfs_attribute *bConfValue;
+ struct sysfs_attribute *bDevClass;
+ struct sysfs_attribute *bNumIntfs;
+ int i, rc;
+ enum unbind_status status = UNBIND_ST_OK;
+
+ busid_dev = sysfs_open_device(bus_type, busid);
+ if (!busid_dev) {
+ dbg("sysfs_open_device %s failed: %s", busid, strerror(errno));
return -1;
+ }

- ret = write(fd, busid, strnlen(busid, SYSFS_BUS_ID_SIZE));
- if (ret < 0) {
- close(fd);
- return -1;
+ bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
+ bDevClass = sysfs_get_device_attr(busid_dev, "bDeviceClass");
+ bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces");
+ if (!bConfValue || !bDevClass || !bNumIntfs) {
+ dbg("problem getting device attributes: %s",
+ strerror(errno));
+ goto err_close_busid_dev;
}

- close(fd);
+ if (!strncmp(bDevClass->value, "09", bDevClass->len)) {
+ dbg("skip unbinding of hub");
+ goto err_close_busid_dev;
+ }

- return 0;
-}
+ for (i = 0; i < atoi(bNumIntfs->value); i++) {
+ snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid,
+ bConfValue->value, i);
+ intf_dev = sysfs_open_device(bus_type, intf_busid);
+ if (!intf_dev) {
+ dbg("could not open interface device: %s",
+ strerror(errno));
+ goto err_close_busid_dev;
+ }

-static int bind_interface(char *busid, int configvalue, int interface, char *driver)
-{
- char inf_busid[SYSFS_BUS_ID_SIZE];
+ dbg("%s -> %s", intf_dev->name, intf_dev->driver_name);

- snprintf(inf_busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d", busid, configvalue,
- interface);
+ if (!strncmp("unknown", intf_dev->driver_name, SYSFS_NAME_LEN))
+ /* unbound interface */
+ continue;

- return bind_interface_busid(inf_busid, driver);
-}
+ if (!strncmp(USBIP_HOST_DRV_NAME, intf_dev->driver_name,
+ SYSFS_NAME_LEN)) {
+ /* already bound to usbip-host */
+ status = UNBIND_ST_USBIP_HOST;
+ continue;
+ }

-/* call at unbound state */
-static int bind_to_usbip(char *busid)
-{
- int configvalue = 0;
- int ninterface = 0;
- int i;
- int failed = 0;
+ /* unbinding */
+ intf_drv = sysfs_open_driver(bus_type, intf_dev->driver_name);
+ if (!intf_drv) {
+ dbg("could not open interface driver on %s: %s",
+ intf_dev->name, strerror(errno));
+ goto err_close_intf_dev;
+ }

- configvalue = read_bConfigurationValue(busid);
- ninterface = read_bNumInterfaces(busid);
+ unbind_attr = sysfs_get_driver_attr(intf_drv, "unbind");
+ if (!unbind_attr) {
+ dbg("problem getting interface driver attribute: %s",
+ strerror(errno));
+ goto err_close_intf_drv;
+ }

- if (configvalue < 0 || ninterface < 0) {
- dbg("read config and ninf value, removed?");
- return -1;
+ rc = sysfs_write_attribute(unbind_attr, intf_dev->bus_id,
+ SYSFS_BUS_ID_SIZE);
+ if (rc < 0) {
+ /* NOTE: why keep unbinding other interfaces? */
+ dbg("unbind driver at %s failed", intf_dev->bus_id);
+ status = UNBIND_ST_FAILED;
+ }
+
+ sysfs_close_driver(intf_drv);
+ sysfs_close_device(intf_dev);
}

- for (i = 0; i < ninterface; i++) {
- int ret;
+ goto out;

- ret = bind_interface(busid, configvalue, i,
- USBIP_HOST_DRV_NAME);
- if (ret < 0) {
- dbg("bind usbip at %s:%d.%d, failed",
- busid, configvalue, i);
- failed = 1;
- /* need to contine binding at other interfaces */
- }
- }
+err_close_intf_drv:
+ sysfs_close_driver(intf_drv);
+err_close_intf_dev:
+ sysfs_close_device(intf_dev);
+err_close_busid_dev:
+ status = UNBIND_ST_FAILED;
+out:
+ sysfs_close_device(busid_dev);

- if (failed)
- return -1;
- else
- return 0;
+ return status;
}

-static int use_device_by_usbip(char *busid)
+static int bind_device(char *busid)
{
- int ret;
+ int rc;

- ret = unbind(busid);
- if (ret < 0) {
- dbg("unbind drivers of %s, failed", busid);
+ rc = unbind_other(busid);
+ if (rc == UNBIND_ST_FAILED) {
+ err("could not unbind driver from device on busid %s", busid);
+ return -1;
+ } else if (rc == UNBIND_ST_USBIP_HOST) {
+ err("device on busid %s is already bound to %s", busid,
+ USBIP_HOST_DRV_NAME);
return -1;
}

- ret = modify_match_busid(busid, 1);
- if (ret < 0) {
- dbg("add %s to match_busid, failed", busid);
+ rc = modify_match_busid(busid, 1);
+ if (rc < 0) {
+ err("unable to bind device on %s", busid);
return -1;
}

- ret = bind_to_usbip(busid);
- if (ret < 0) {
- dbg("bind usbip to %s, failed", busid);
+ rc = bind_usbip(busid);
+ if (rc < 0) {
+ err("could not bind device to %s", USBIP_HOST_DRV_NAME);
modify_match_busid(busid, 0);
return -1;
}

- dbg("bind %s complete!", busid);
+ printf("bind device on busid %s: complete\n", busid);

return 0;
}
@@ -237,8 +249,9 @@ int usbip_bind(int argc, char *argv[])
{
static const struct option opts[] = {
{ "busid", required_argument, NULL, 'b' },
- { NULL, 0, NULL, 0 }
+ { NULL, 0, NULL, 0 }
};
+
int opt;
int ret = -1;

@@ -250,7 +263,7 @@ int usbip_bind(int argc, char *argv[])

switch (opt) {
case 'b':
- ret = use_device_by_usbip(optarg);
+ ret = bind_device(optarg);
goto out;
default:
goto err_out;
--
1.7.5.2

2011-06-20 05:46:53

by matt mooney

[permalink] [raw]
Subject: [PATCH 13/22] staging: usbip: userspace: usbip_unbind.c: implement using libsysfs

Modify unbind to use libsysfs, and include a check to verify that the
device is actually using usbip-host before proceeding. The output
messages have been changed to be consistent with `usbip bind'.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip_unbind.c | 123 +++++++++++++++++---
1 files changed, 107 insertions(+), 16 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_unbind.c b/drivers/staging/usbip/userspace/src/usbip_unbind.c
index 9978d38..d5a9ab6 100644
--- a/drivers/staging/usbip/userspace/src/usbip_unbind.c
+++ b/drivers/staging/usbip/userspace/src/usbip_unbind.c
@@ -16,10 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

+#include <sysfs/libsysfs.h>
+
+#include <errno.h>
#include <stdio.h>
+#include <string.h>

#include <getopt.h>
-#include <unistd.h>

#include "usbip_common.h"
#include "utils.h"
@@ -35,41 +38,129 @@ void usbip_unbind_usage(void)
printf("usage: %s", usbip_unbind_usage_string);
}

-static int use_device_by_other(char *busid)
+static int unbind_device(char *busid)
{
- int rc;
- int config;
+ char bus_type[] = "usb";
+ struct sysfs_driver *usbip_host_drv;
+ struct sysfs_device *dev;
+ struct dlist *devlist;
+ int verified = 0;
+ int rc, ret = -1;
+
+ char attr_name[] = "bConfigurationValue";
+ char sysfs_mntpath[SYSFS_PATH_MAX];
+ char busid_attr_path[SYSFS_PATH_MAX];
+ struct sysfs_attribute *busid_attr;
+ char *val = NULL;
+ int len;
+
+ /* verify the busid device is using usbip-host */
+ usbip_host_drv = sysfs_open_driver(bus_type, USBIP_HOST_DRV_NAME);
+ if (!usbip_host_drv) {
+ err("could not open %s driver: %s", USBIP_HOST_DRV_NAME,
+ strerror(errno));
+ return -1;
+ }
+
+ devlist = sysfs_get_driver_devices(usbip_host_drv);
+ if (!devlist) {
+ err("%s is not in use by any devices", USBIP_HOST_DRV_NAME);
+ goto err_close_usbip_host_drv;
+ }
+
+ dlist_for_each_data(devlist, dev, struct sysfs_device) {
+ if (!strncmp(busid, dev->name, strlen(busid)) &&
+ !strncmp(dev->driver_name, USBIP_HOST_DRV_NAME,
+ strlen(USBIP_HOST_DRV_NAME))) {
+ verified = 1;
+ break;
+ }
+ }
+
+ if (!verified) {
+ err("device on busid %s is not using %s", busid,
+ USBIP_HOST_DRV_NAME);
+ goto err_close_usbip_host_drv;
+ }
+
+ /*
+ * NOTE: A read and write of an attribute value of the device busid
+ * refers to must be done to start probing. That way a rebind of the
+ * default driver for the device occurs.
+ *
+ * This seems very hackish and adds a lot of pointless code. I think it
+ * should be done in the kernel by the driver after del_match_busid is
+ * finished!
+ */
+
+ rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
+ if (rc < 0) {
+ err("sysfs must be mounted: %s", strerror(errno));
+ return -1;
+ }

- /* read and write the same config value to kick probing */
- config = read_bConfigurationValue(busid);
- if (config < 0) {
- dbg("read bConfigurationValue of %s, failed", busid);
+ snprintf(busid_attr_path, sizeof(busid_attr_path), "%s/%s/%s/%s/%s/%s",
+ sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DEVICES_NAME,
+ busid, attr_name);
+
+ /* read a device attribute */
+ busid_attr = sysfs_open_attribute(busid_attr_path);
+ if (!busid_attr) {
+ err("could not open %s/%s: %s", busid, attr_name,
+ strerror(errno));
return -1;
}

+ if (sysfs_read_attribute(busid_attr) < 0) {
+ err("problem reading attribute: %s", strerror(errno));
+ goto err_out;
+ }
+
+ len = busid_attr->len;
+ val = malloc(len);
+ *val = *busid_attr->value;
+ sysfs_close_attribute(busid_attr);
+
+ /* notify driver of unbind */
rc = modify_match_busid(busid, 0);
if (rc < 0) {
- dbg("del %s to match_busid, failed", busid);
+ err("unable to unbind device on %s", busid);
+ goto err_out;
+ }
+
+ /* write the device attribute */
+ busid_attr = sysfs_open_attribute(busid_attr_path);
+ if (!busid_attr) {
+ err("could not open %s/%s: %s", busid, attr_name,
+ strerror(errno));
return -1;
}

- rc = write_bConfigurationValue(busid, config);
+ rc = sysfs_write_attribute(busid_attr, val, len);
if (rc < 0) {
- dbg("read bConfigurationValue of %s, failed", busid);
- return -1;
+ err("problem writing attribute: %s", strerror(errno));
+ goto err_out;
}
+ sysfs_close_attribute(busid_attr);
+
+ ret = 0;
+ printf("unbind device on busid %s: complete\n", busid);

- info("bind %s to other drivers than usbip, complete!", busid);
+err_out:
+ free(val);
+err_close_usbip_host_drv:
+ sysfs_close_driver(usbip_host_drv);

- return 0;
+ return ret;
}

int usbip_unbind(int argc, char *argv[])
{
static const struct option opts[] = {
{ "busid", required_argument, NULL, 'b' },
- { NULL, 0, NULL, 0 }
+ { NULL, 0, NULL, 0 }
};
+
int opt;
int ret = -1;

@@ -81,7 +172,7 @@ int usbip_unbind(int argc, char *argv[])

switch (opt) {
case 'b':
- ret = use_device_by_other(optarg);
+ ret = unbind_device(optarg);
goto out;
default:
goto err_out;
--
1.7.5.2

2011-06-20 05:53:26

by matt mooney

[permalink] [raw]
Subject: [PATCH 14/22] staging: usbip: userspace: usbip list: edit output messages

Edit dbg and normal output messages for consistency and better
feedback.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip_list.c | 58 +++++++++++++---------
1 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_list.c b/drivers/staging/usbip/userspace/src/usbip_list.c
index 03f6210..afff469 100644
--- a/drivers/staging/usbip/userspace/src/usbip_list.c
+++ b/drivers/staging/usbip/userspace/src/usbip_list.c
@@ -49,7 +49,7 @@ static int get_exported_devices(int sockfd)
{
char product_name[100];
char class_name[100];
- struct op_devlist_reply rep;
+ struct op_devlist_reply reply;
uint16_t code = OP_REP_DEVLIST;
struct usbip_usb_device udev;
struct usbip_usb_interface uintf;
@@ -58,30 +58,30 @@ static int get_exported_devices(int sockfd)

rc = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
if (rc < 0) {
- dbg("usbip_send_op_common");
+ dbg("usbip_send_op_common failed");
return -1;
}

rc = usbip_recv_op_common(sockfd, &code);
if (rc < 0) {
- dbg("usbip_recv_op_common");
+ dbg("usbip_recv_op_common failed");
return -1;
}

- memset(&rep, 0, sizeof(rep));
- rc = usbip_recv(sockfd, &rep, sizeof(rep));
+ memset(&reply, 0, sizeof(reply));
+ rc = usbip_recv(sockfd, &reply, sizeof(reply));
if (rc < 0) {
- dbg("usbip_recv_op_devlist");
+ dbg("usbip_recv_op_devlist failed");
return -1;
}
- PACK_OP_DEVLIST_REPLY(0, &rep);
- dbg("exportable devices: %d", rep.ndev);
+ PACK_OP_DEVLIST_REPLY(0, &reply);
+ dbg("exportable devices: %d\n", reply.ndev);

- for (i = 0; i < rep.ndev; i++) {
+ for (i = 0; i < reply.ndev; i++) {
memset(&udev, 0, sizeof(udev));
rc = usbip_recv(sockfd, &udev, sizeof(udev));
if (rc < 0) {
- dbg("usbip_recv: usbip_usb_device[%d]", i);
+ dbg("usbip_recv failed: usbip_usb_device[%d]", i);
return -1;
}
pack_usb_device(0, &udev);
@@ -98,7 +98,7 @@ static int get_exported_devices(int sockfd)
for (j = 0; j < udev.bNumInterfaces; j++) {
rc = usbip_recv(sockfd, &uintf, sizeof(uintf));
if (rc < 0) {
- dbg("usbip_recv: usbip_usb_interface[%d]", j);
+ dbg("usbip_recv failed: usbip_usb_intf[%d]", j);
return -1;
}
pack_usb_interface(0, &uintf);
@@ -108,7 +108,6 @@ static int get_exported_devices(int sockfd)
uintf.bInterfaceSubClass,
uintf.bInterfaceProtocol);
printf("%8s: %2d - %s\n", "", j, class_name);
-
}
printf("\n");
}
@@ -123,16 +122,19 @@ static int list_exported_devices(char *host)

sockfd = usbip_net_tcp_connect(host, USBIP_PORT_STRING);
if (sockfd < 0) {
- err("unable to connect to %s port %s: %s\n", host,
+ err("could not connect to %s:%s: %s", host,
USBIP_PORT_STRING, gai_strerror(sockfd));
return -1;
}
- dbg("connected to %s port %s\n", host, USBIP_PORT_STRING);
- printf("- %s\n", host);
+ dbg("connected to %s:%s", host, USBIP_PORT_STRING);
+
+ printf("Exportable USB devices\n");
+ printf("======================\n");
+ printf(" - %s\n", host);

rc = get_exported_devices(sockfd);
if (rc < 0) {
- dbg("get_exported_devices failed");
+ err("failed to get device list from %s", host);
return -1;
}

@@ -193,13 +195,14 @@ static int list_devices(bool parsable)

ubus = sysfs_open_bus(bus_type);
if (!ubus) {
- err("sysfs_open_bus: %s", strerror(errno));
+ err("could not open %s bus: %s", bus_type, strerror(errno));
return -1;
}

devlist = sysfs_get_bus_devices(ubus);
if (!devlist) {
- err("sysfs_get_bus_devices: %s", strerror(errno));
+ err("could not get %s bus devices: %s", bus_type,
+ strerror(errno));
goto err_out;
}

@@ -215,8 +218,11 @@ static int list_devices(bool parsable)
idProduct = sysfs_get_device_attr(dev, "idProduct");
bConfValue = sysfs_get_device_attr(dev, "bConfigurationValue");
bNumIntfs = sysfs_get_device_attr(dev, "bNumInterfaces");
- if (!idVendor || !idProduct || !bConfValue || !bNumIntfs)
+ if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
+ err("problem getting device attributes: %s",
+ strerror(errno));
goto err_out;
+ }

print_device(dev->bus_id, idVendor->value, idProduct->value,
parsable);
@@ -225,8 +231,11 @@ static int list_devices(bool parsable)
snprintf(busid, sizeof(busid), "%s:%.1s.%d",
dev->bus_id, bConfValue->value, i);
intf = sysfs_open_device(bus_type, busid);
- if (!intf)
+ if (!intf) {
+ err("could not open device interface: %s",
+ strerror(errno));
goto err_out;
+ }
print_interface(busid, intf->driver_name, parsable);
sysfs_close_device(intf);
}
@@ -244,11 +253,12 @@ err_out:
int usbip_list(int argc, char *argv[])
{
static const struct option opts[] = {
- { "parsable", no_argument, NULL, 'p' },
- { "remote", required_argument, NULL, 'r' },
- { "local", no_argument, NULL, 'l' },
- { NULL, 0, NULL, 0 }
+ { "parsable", no_argument, NULL, 'p' },
+ { "remote", required_argument, NULL, 'r' },
+ { "local", no_argument, NULL, 'l' },
+ { NULL, 0, NULL, 0 }
};
+
bool parsable = false;
int opt;
int ret = -1;
--
1.7.5.2

2011-06-20 06:22:00

by matt mooney

[permalink] [raw]
Subject: [PATCH 15/22] staging: usbip: userspace: usbip list: move output header

Delay the printing of the output header until the list is received
from the remote host. This allows notification that the host does not
have any exportable devices.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip_list.c | 17 +++++++++++------
1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_list.c b/drivers/staging/usbip/userspace/src/usbip_list.c
index afff469..973bf8c 100644
--- a/drivers/staging/usbip/userspace/src/usbip_list.c
+++ b/drivers/staging/usbip/userspace/src/usbip_list.c
@@ -45,7 +45,7 @@ void usbip_list_usage(void)
printf("usage: %s", usbip_list_usage_string);
}

-static int get_exported_devices(int sockfd)
+static int get_exported_devices(char *host, int sockfd)
{
char product_name[100];
char class_name[100];
@@ -77,6 +77,15 @@ static int get_exported_devices(int sockfd)
PACK_OP_DEVLIST_REPLY(0, &reply);
dbg("exportable devices: %d\n", reply.ndev);

+ if (reply.ndev == 0) {
+ info("no exportable devices found on %s", host);
+ return 0;
+ }
+
+ printf("Exportable USB devices\n");
+ printf("======================\n");
+ printf(" - %s\n", host);
+
for (i = 0; i < reply.ndev; i++) {
memset(&udev, 0, sizeof(udev));
rc = usbip_recv(sockfd, &udev, sizeof(udev));
@@ -128,11 +137,7 @@ static int list_exported_devices(char *host)
}
dbg("connected to %s:%s", host, USBIP_PORT_STRING);

- printf("Exportable USB devices\n");
- printf("======================\n");
- printf(" - %s\n", host);
-
- rc = get_exported_devices(sockfd);
+ rc = get_exported_devices(host, sockfd);
if (rc < 0) {
err("failed to get device list from %s", host);
return -1;
--
1.7.5.2

2011-06-20 05:45:52

by matt mooney

[permalink] [raw]
Subject: [PATCH 16/22] staging: usbip: userspace: rename stub driver files

Rename stub_driver.? to usbip_host_driver.?

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/Makefile.am | 2 +-
drivers/staging/usbip/userspace/libsrc/Makefile.am | 2 +-
.../staging/usbip/userspace/libsrc/stub_driver.c | 395 --------------------
.../staging/usbip/userspace/libsrc/stub_driver.h | 37 --
.../usbip/userspace/libsrc/usbip_host_driver.c | 395 ++++++++++++++++++++
.../usbip/userspace/libsrc/usbip_host_driver.h | 37 ++
6 files changed, 434 insertions(+), 434 deletions(-)
delete mode 100644 drivers/staging/usbip/userspace/libsrc/stub_driver.c
delete mode 100644 drivers/staging/usbip/userspace/libsrc/stub_driver.h
create mode 100644 drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
create mode 100644 drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h

diff --git a/drivers/staging/usbip/userspace/Makefile.am b/drivers/staging/usbip/userspace/Makefile.am
index d557fe9..fbdeef3 100644
--- a/drivers/staging/usbip/userspace/Makefile.am
+++ b/drivers/staging/usbip/userspace/Makefile.am
@@ -1,6 +1,6 @@
SUBDIRS := libsrc src
includedir := @includedir@/usbip
include_HEADERS := $(addprefix libsrc/, \
- usbip_common.h vhci_driver.h stub_driver.h)
+ usbip_common.h vhci_driver.h usbip_host_driver.h)

dist_man_MANS := $(addprefix doc/, usbip.8 usbipd.8 usbip_bind_driver.8)
diff --git a/drivers/staging/usbip/userspace/libsrc/Makefile.am b/drivers/staging/usbip/userspace/libsrc/Makefile.am
index 6696aa7..9b663a4 100644
--- a/drivers/staging/usbip/userspace/libsrc/Makefile.am
+++ b/drivers/staging/usbip/userspace/libsrc/Makefile.am
@@ -3,5 +3,5 @@ libusbip_la_CFLAGS := @EXTRA_CFLAGS@
libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@

lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h stub_driver.c stub_driver.h \
+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
diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.c b/drivers/staging/usbip/userspace/libsrc/stub_driver.c
deleted file mode 100644
index 604aed1..0000000
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.c
+++ /dev/null
@@ -1,395 +0,0 @@
-/*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include <errno.h>
-#include <unistd.h>
-
-#include "usbip_common.h"
-#include "stub_driver.h"
-
-#undef PROGNAME
-#define PROGNAME "libusbip"
-
-struct usbip_stub_driver *stub_driver;
-
-static struct sysfs_driver *open_sysfs_stub_driver(void)
-{
- int ret;
-
- char sysfs_mntpath[SYSFS_PATH_MAX];
- char stub_driver_path[SYSFS_PATH_MAX];
- struct sysfs_driver *stub_driver;
-
-
- ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
- if (ret < 0) {
- dbg("sysfs_get_mnt_path failed");
- return NULL;
- }
-
- snprintf(stub_driver_path, SYSFS_PATH_MAX, "%s/%s/usb/%s/%s",
- sysfs_mntpath, SYSFS_BUS_NAME, SYSFS_DRIVERS_NAME,
- USBIP_HOST_DRV_NAME);
-
- stub_driver = sysfs_open_driver_path(stub_driver_path);
- if (!stub_driver) {
- dbg("sysfs_open_driver_path failed");
- return NULL;
- }
-
- return stub_driver;
-}
-
-
-#define SYSFS_OPEN_RETRIES 100
-
-/* only the first interface value is true! */
-static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
-{
- char attrpath[SYSFS_PATH_MAX];
- struct sysfs_attribute *attr;
- int value = 0;
- int ret;
- struct stat s;
- int retries = SYSFS_OPEN_RETRIES;
-
- /* This access is racy!
- *
- * Just after detach, our driver removes the sysfs
- * files and recreates them.
- *
- * We may try and fail to open the usbip_status of
- * an exported device in the (short) window where
- * it has been removed and not yet recreated.
- *
- * This is a bug in the interface. Nothing we can do
- * except work around it here by polling for the sysfs
- * usbip_status to reappear.
- */
-
- snprintf(attrpath, SYSFS_PATH_MAX, "%s/%s:%d.%d/usbip_status",
- udev->path, udev->busid,
- udev->bConfigurationValue,
- 0);
-
- while (retries > 0) {
- if (stat(attrpath, &s) == 0)
- break;
-
- if (errno != ENOENT) {
- dbg("stat failed: %s", attrpath);
- return -1;
- }
-
- usleep(10000); /* 10ms */
- retries--;
- }
-
- if (retries == 0)
- dbg("usbip_status not ready after %d retries",
- SYSFS_OPEN_RETRIES);
- else if (retries < SYSFS_OPEN_RETRIES)
- dbg("warning: usbip_status ready after %d retries",
- SYSFS_OPEN_RETRIES - retries);
-
- attr = sysfs_open_attribute(attrpath);
- if (!attr) {
- dbg("sysfs_open_attribute failed: %s", attrpath);
- return -1;
- }
-
- ret = sysfs_read_attribute(attr);
- if (ret) {
- dbg("sysfs_read_attribute failed: %s", attrpath);
- sysfs_close_attribute(attr);
- return -1;
- }
-
- value = atoi(attr->value);
-
- sysfs_close_attribute(attr);
-
- return value;
-}
-
-
-static void usbip_exported_device_delete(void *dev)
-{
- struct usbip_exported_device *edev =
- (struct usbip_exported_device *) dev;
-
- sysfs_close_device(edev->sudev);
- free(dev);
-}
-
-
-static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
-{
- struct usbip_exported_device *edev = NULL;
-
- edev = (struct usbip_exported_device *) calloc(1, sizeof(*edev));
- if (!edev) {
- dbg("calloc failed");
- return NULL;
- }
-
- edev->sudev = sysfs_open_device_path(sdevpath);
- if (!edev->sudev) {
- dbg("sysfs_open_device_path failed: %s", sdevpath);
- goto err;
- }
-
- read_usb_device(edev->sudev, &edev->udev);
-
- edev->status = read_attr_usbip_status(&edev->udev);
- if (edev->status < 0)
- goto err;
-
- /* reallocate buffer to include usb interface data */
- size_t size = sizeof(*edev) + edev->udev.bNumInterfaces *
- sizeof(struct usbip_usb_interface);
- edev = (struct usbip_exported_device *) realloc(edev, size);
- if (!edev) {
- dbg("realloc failed");
- goto err;
- }
-
- for (int i=0; i < edev->udev.bNumInterfaces; i++)
- read_usb_interface(&edev->udev, i, &edev->uinf[i]);
-
- return edev;
-
-err:
- if (edev && edev->sudev)
- sysfs_close_device(edev->sudev);
- if (edev)
- free(edev);
- return NULL;
-}
-
-
-static int check_new(struct dlist *dlist, struct sysfs_device *target)
-{
- struct sysfs_device *dev;
-
- dlist_for_each_data(dlist, dev, struct sysfs_device) {
- if (!strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
- /* found. not new */
- return 0;
- }
-
- return 1;
-}
-
-static void delete_nothing(void *dev __attribute__((unused)))
-{
- /* do not delete anything. but, its container will be deleted. */
-}
-
-static int refresh_exported_devices(void)
-{
- struct sysfs_device *suinf; /* sysfs_device of usb_interface */
- struct dlist *suinf_list;
-
- struct sysfs_device *sudev; /* sysfs_device of usb_device */
- struct dlist *sudev_list;
-
-
- sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device), delete_nothing);
-
- suinf_list = sysfs_get_driver_devices(stub_driver->sysfs_driver);
- if (!suinf_list) {
- dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
- "exportable!\n");
- goto bye;
- }
-
- /* collect unique USB devices (not interfaces) */
- dlist_for_each_data(suinf_list, suinf, struct sysfs_device) {
-
- /* get usb device of this usb interface */
- sudev = sysfs_get_device_parent(suinf);
- if (!sudev) {
- dbg("sysfs_get_device_parent failed: %s", suinf->name);
- continue;
- }
-
- if (check_new(sudev_list, sudev)) {
- dlist_unshift(sudev_list, sudev);
- }
- }
-
- dlist_for_each_data(sudev_list, sudev, struct sysfs_device) {
- struct usbip_exported_device *edev;
-
- edev = usbip_exported_device_new(sudev->path);
- if (!edev) {
- dbg("usbip_exported_device_new failed");
- continue;
- }
-
- dlist_unshift(stub_driver->edev_list, (void *) edev);
- stub_driver->ndevs++;
- }
-
-
- dlist_destroy(sudev_list);
-
-bye:
-
- return 0;
-}
-
-int usbip_stub_refresh_device_list(void)
-{
- int ret;
-
- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
-
- stub_driver->ndevs = 0;
-
- stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
- usbip_exported_device_delete);
- if (!stub_driver->edev_list) {
- dbg("dlist_new_with_delete failed");
- return -1;
- }
-
- ret = refresh_exported_devices();
- if (ret < 0)
- return ret;
-
- return 0;
-}
-
-int usbip_stub_driver_open(void)
-{
- int ret;
-
-
- stub_driver = (struct usbip_stub_driver *) calloc(1, sizeof(*stub_driver));
- if (!stub_driver) {
- dbg("calloc failed");
- return -1;
- }
-
- stub_driver->ndevs = 0;
-
- stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
- usbip_exported_device_delete);
- if (!stub_driver->edev_list) {
- dbg("dlist_new_with_delete failed");
- goto err;
- }
-
- stub_driver->sysfs_driver = open_sysfs_stub_driver();
- if (!stub_driver->sysfs_driver)
- goto err;
-
- ret = refresh_exported_devices();
- if (ret < 0)
- goto err;
-
- return 0;
-
-
-err:
- if (stub_driver->sysfs_driver)
- sysfs_close_driver(stub_driver->sysfs_driver);
- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
- free(stub_driver);
-
- stub_driver = NULL;
- return -1;
-}
-
-
-void usbip_stub_driver_close(void)
-{
- if (!stub_driver)
- return;
-
- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
- if (stub_driver->sysfs_driver)
- sysfs_close_driver(stub_driver->sysfs_driver);
- free(stub_driver);
-
- stub_driver = NULL;
-}
-
-int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd)
-{
- char attrpath[SYSFS_PATH_MAX];
- struct sysfs_attribute *attr;
- char sockfd_buff[30];
- int ret;
-
-
- if (edev->status != SDEV_ST_AVAILABLE) {
- dbg("device not available: %s", edev->udev.busid);
- switch( edev->status ) {
- case SDEV_ST_ERROR:
- dbg("status SDEV_ST_ERROR");
- break;
- case SDEV_ST_USED:
- dbg("status SDEV_ST_USED");
- break;
- default:
- dbg("status unknown: 0x%x", edev->status);
- }
- return -1;
- }
-
- /* only the first interface is true */
- snprintf(attrpath, sizeof(attrpath), "%s/%s:%d.%d/%s",
- edev->udev.path,
- edev->udev.busid,
- edev->udev.bConfigurationValue, 0,
- "usbip_sockfd");
-
- attr = sysfs_open_attribute(attrpath);
- if (!attr) {
- dbg("sysfs_open_attribute failed: %s", attrpath);
- return -1;
- }
-
- snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
-
- dbg("write: %s", sockfd_buff);
- ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
- if (ret < 0) {
- dbg("sysfs_write_attribute failed: sockfd %s to %s",
- sockfd_buff, attrpath);
- goto err_write_sockfd;
- }
-
- dbg("connect: %s", edev->udev.busid);
-
-err_write_sockfd:
- sysfs_close_attribute(attr);
-
- return ret;
-}
-
-struct usbip_exported_device *usbip_stub_get_device(int num)
-{
- struct usbip_exported_device *edev;
- struct dlist *dlist = stub_driver->edev_list;
- int count = 0;
-
- dlist_for_each_data(dlist, edev, struct usbip_exported_device) {
- if (num == count)
- return edev;
- else
- count++ ;
- }
-
- return NULL;
-}
diff --git a/drivers/staging/usbip/userspace/libsrc/stub_driver.h b/drivers/staging/usbip/userspace/libsrc/stub_driver.h
deleted file mode 100644
index 9eaf92c..0000000
--- a/drivers/staging/usbip/userspace/libsrc/stub_driver.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
- */
-
-#ifndef __USBIP_STUB_DRIVER_H
-#define __USBIP_STUB_DRIVER_H
-
-#include <stdint.h>
-#include "usbip_common.h"
-
-struct usbip_stub_driver {
- int ndevs;
- struct sysfs_driver *sysfs_driver;
-
- struct dlist *edev_list; /* list of exported device */
-};
-
-struct usbip_exported_device {
- struct sysfs_device *sudev;
-
- int32_t status;
- struct usbip_usb_device udev;
- struct usbip_usb_interface uinf[];
-};
-
-
-extern struct usbip_stub_driver *stub_driver;
-
-int usbip_stub_driver_open(void);
-void usbip_stub_driver_close(void);
-
-int usbip_stub_refresh_device_list(void);
-int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd);
-
-struct usbip_exported_device *usbip_stub_get_device(int num);
-
-#endif /* __USBIP_STUB_DRIVER_H */
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
new file mode 100644
index 0000000..604aed1
--- /dev/null
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
@@ -0,0 +1,395 @@
+/*
+ * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include "usbip_common.h"
+#include "stub_driver.h"
+
+#undef PROGNAME
+#define PROGNAME "libusbip"
+
+struct usbip_stub_driver *stub_driver;
+
+static struct sysfs_driver *open_sysfs_stub_driver(void)
+{
+ int ret;
+
+ char sysfs_mntpath[SYSFS_PATH_MAX];
+ char stub_driver_path[SYSFS_PATH_MAX];
+ struct sysfs_driver *stub_driver;
+
+
+ ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
+ if (ret < 0) {
+ dbg("sysfs_get_mnt_path failed");
+ return NULL;
+ }
+
+ snprintf(stub_driver_path, SYSFS_PATH_MAX, "%s/%s/usb/%s/%s",
+ sysfs_mntpath, SYSFS_BUS_NAME, SYSFS_DRIVERS_NAME,
+ USBIP_HOST_DRV_NAME);
+
+ stub_driver = sysfs_open_driver_path(stub_driver_path);
+ if (!stub_driver) {
+ dbg("sysfs_open_driver_path failed");
+ return NULL;
+ }
+
+ return stub_driver;
+}
+
+
+#define SYSFS_OPEN_RETRIES 100
+
+/* only the first interface value is true! */
+static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
+{
+ char attrpath[SYSFS_PATH_MAX];
+ struct sysfs_attribute *attr;
+ int value = 0;
+ int ret;
+ struct stat s;
+ int retries = SYSFS_OPEN_RETRIES;
+
+ /* This access is racy!
+ *
+ * Just after detach, our driver removes the sysfs
+ * files and recreates them.
+ *
+ * We may try and fail to open the usbip_status of
+ * an exported device in the (short) window where
+ * it has been removed and not yet recreated.
+ *
+ * This is a bug in the interface. Nothing we can do
+ * except work around it here by polling for the sysfs
+ * usbip_status to reappear.
+ */
+
+ snprintf(attrpath, SYSFS_PATH_MAX, "%s/%s:%d.%d/usbip_status",
+ udev->path, udev->busid,
+ udev->bConfigurationValue,
+ 0);
+
+ while (retries > 0) {
+ if (stat(attrpath, &s) == 0)
+ break;
+
+ if (errno != ENOENT) {
+ dbg("stat failed: %s", attrpath);
+ return -1;
+ }
+
+ usleep(10000); /* 10ms */
+ retries--;
+ }
+
+ if (retries == 0)
+ dbg("usbip_status not ready after %d retries",
+ SYSFS_OPEN_RETRIES);
+ else if (retries < SYSFS_OPEN_RETRIES)
+ dbg("warning: usbip_status ready after %d retries",
+ SYSFS_OPEN_RETRIES - retries);
+
+ attr = sysfs_open_attribute(attrpath);
+ if (!attr) {
+ dbg("sysfs_open_attribute failed: %s", attrpath);
+ return -1;
+ }
+
+ ret = sysfs_read_attribute(attr);
+ if (ret) {
+ dbg("sysfs_read_attribute failed: %s", attrpath);
+ sysfs_close_attribute(attr);
+ return -1;
+ }
+
+ value = atoi(attr->value);
+
+ sysfs_close_attribute(attr);
+
+ return value;
+}
+
+
+static void usbip_exported_device_delete(void *dev)
+{
+ struct usbip_exported_device *edev =
+ (struct usbip_exported_device *) dev;
+
+ sysfs_close_device(edev->sudev);
+ free(dev);
+}
+
+
+static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
+{
+ struct usbip_exported_device *edev = NULL;
+
+ edev = (struct usbip_exported_device *) calloc(1, sizeof(*edev));
+ if (!edev) {
+ dbg("calloc failed");
+ return NULL;
+ }
+
+ edev->sudev = sysfs_open_device_path(sdevpath);
+ if (!edev->sudev) {
+ dbg("sysfs_open_device_path failed: %s", sdevpath);
+ goto err;
+ }
+
+ read_usb_device(edev->sudev, &edev->udev);
+
+ edev->status = read_attr_usbip_status(&edev->udev);
+ if (edev->status < 0)
+ goto err;
+
+ /* reallocate buffer to include usb interface data */
+ size_t size = sizeof(*edev) + edev->udev.bNumInterfaces *
+ sizeof(struct usbip_usb_interface);
+ edev = (struct usbip_exported_device *) realloc(edev, size);
+ if (!edev) {
+ dbg("realloc failed");
+ goto err;
+ }
+
+ for (int i=0; i < edev->udev.bNumInterfaces; i++)
+ read_usb_interface(&edev->udev, i, &edev->uinf[i]);
+
+ return edev;
+
+err:
+ if (edev && edev->sudev)
+ sysfs_close_device(edev->sudev);
+ if (edev)
+ free(edev);
+ return NULL;
+}
+
+
+static int check_new(struct dlist *dlist, struct sysfs_device *target)
+{
+ struct sysfs_device *dev;
+
+ dlist_for_each_data(dlist, dev, struct sysfs_device) {
+ if (!strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
+ /* found. not new */
+ return 0;
+ }
+
+ return 1;
+}
+
+static void delete_nothing(void *dev __attribute__((unused)))
+{
+ /* do not delete anything. but, its container will be deleted. */
+}
+
+static int refresh_exported_devices(void)
+{
+ struct sysfs_device *suinf; /* sysfs_device of usb_interface */
+ struct dlist *suinf_list;
+
+ struct sysfs_device *sudev; /* sysfs_device of usb_device */
+ struct dlist *sudev_list;
+
+
+ sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device), delete_nothing);
+
+ suinf_list = sysfs_get_driver_devices(stub_driver->sysfs_driver);
+ if (!suinf_list) {
+ dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
+ "exportable!\n");
+ goto bye;
+ }
+
+ /* collect unique USB devices (not interfaces) */
+ dlist_for_each_data(suinf_list, suinf, struct sysfs_device) {
+
+ /* get usb device of this usb interface */
+ sudev = sysfs_get_device_parent(suinf);
+ if (!sudev) {
+ dbg("sysfs_get_device_parent failed: %s", suinf->name);
+ continue;
+ }
+
+ if (check_new(sudev_list, sudev)) {
+ dlist_unshift(sudev_list, sudev);
+ }
+ }
+
+ dlist_for_each_data(sudev_list, sudev, struct sysfs_device) {
+ struct usbip_exported_device *edev;
+
+ edev = usbip_exported_device_new(sudev->path);
+ if (!edev) {
+ dbg("usbip_exported_device_new failed");
+ continue;
+ }
+
+ dlist_unshift(stub_driver->edev_list, (void *) edev);
+ stub_driver->ndevs++;
+ }
+
+
+ dlist_destroy(sudev_list);
+
+bye:
+
+ return 0;
+}
+
+int usbip_stub_refresh_device_list(void)
+{
+ int ret;
+
+ if (stub_driver->edev_list)
+ dlist_destroy(stub_driver->edev_list);
+
+ stub_driver->ndevs = 0;
+
+ stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
+ usbip_exported_device_delete);
+ if (!stub_driver->edev_list) {
+ dbg("dlist_new_with_delete failed");
+ return -1;
+ }
+
+ ret = refresh_exported_devices();
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+int usbip_stub_driver_open(void)
+{
+ int ret;
+
+
+ stub_driver = (struct usbip_stub_driver *) calloc(1, sizeof(*stub_driver));
+ if (!stub_driver) {
+ dbg("calloc failed");
+ return -1;
+ }
+
+ stub_driver->ndevs = 0;
+
+ stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
+ usbip_exported_device_delete);
+ if (!stub_driver->edev_list) {
+ dbg("dlist_new_with_delete failed");
+ goto err;
+ }
+
+ stub_driver->sysfs_driver = open_sysfs_stub_driver();
+ if (!stub_driver->sysfs_driver)
+ goto err;
+
+ ret = refresh_exported_devices();
+ if (ret < 0)
+ goto err;
+
+ return 0;
+
+
+err:
+ if (stub_driver->sysfs_driver)
+ sysfs_close_driver(stub_driver->sysfs_driver);
+ if (stub_driver->edev_list)
+ dlist_destroy(stub_driver->edev_list);
+ free(stub_driver);
+
+ stub_driver = NULL;
+ return -1;
+}
+
+
+void usbip_stub_driver_close(void)
+{
+ if (!stub_driver)
+ return;
+
+ if (stub_driver->edev_list)
+ dlist_destroy(stub_driver->edev_list);
+ if (stub_driver->sysfs_driver)
+ sysfs_close_driver(stub_driver->sysfs_driver);
+ free(stub_driver);
+
+ stub_driver = NULL;
+}
+
+int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd)
+{
+ char attrpath[SYSFS_PATH_MAX];
+ struct sysfs_attribute *attr;
+ char sockfd_buff[30];
+ int ret;
+
+
+ if (edev->status != SDEV_ST_AVAILABLE) {
+ dbg("device not available: %s", edev->udev.busid);
+ switch( edev->status ) {
+ case SDEV_ST_ERROR:
+ dbg("status SDEV_ST_ERROR");
+ break;
+ case SDEV_ST_USED:
+ dbg("status SDEV_ST_USED");
+ break;
+ default:
+ dbg("status unknown: 0x%x", edev->status);
+ }
+ return -1;
+ }
+
+ /* only the first interface is true */
+ snprintf(attrpath, sizeof(attrpath), "%s/%s:%d.%d/%s",
+ edev->udev.path,
+ edev->udev.busid,
+ edev->udev.bConfigurationValue, 0,
+ "usbip_sockfd");
+
+ attr = sysfs_open_attribute(attrpath);
+ if (!attr) {
+ dbg("sysfs_open_attribute failed: %s", attrpath);
+ return -1;
+ }
+
+ snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+
+ dbg("write: %s", sockfd_buff);
+ ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
+ if (ret < 0) {
+ dbg("sysfs_write_attribute failed: sockfd %s to %s",
+ sockfd_buff, attrpath);
+ goto err_write_sockfd;
+ }
+
+ dbg("connect: %s", edev->udev.busid);
+
+err_write_sockfd:
+ sysfs_close_attribute(attr);
+
+ return ret;
+}
+
+struct usbip_exported_device *usbip_stub_get_device(int num)
+{
+ struct usbip_exported_device *edev;
+ struct dlist *dlist = stub_driver->edev_list;
+ int count = 0;
+
+ dlist_for_each_data(dlist, edev, struct usbip_exported_device) {
+ if (num == count)
+ return edev;
+ else
+ count++ ;
+ }
+
+ return NULL;
+}
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h
new file mode 100644
index 0000000..9eaf92c
--- /dev/null
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ */
+
+#ifndef __USBIP_STUB_DRIVER_H
+#define __USBIP_STUB_DRIVER_H
+
+#include <stdint.h>
+#include "usbip_common.h"
+
+struct usbip_stub_driver {
+ int ndevs;
+ struct sysfs_driver *sysfs_driver;
+
+ struct dlist *edev_list; /* list of exported device */
+};
+
+struct usbip_exported_device {
+ struct sysfs_device *sudev;
+
+ int32_t status;
+ struct usbip_usb_device udev;
+ struct usbip_usb_interface uinf[];
+};
+
+
+extern struct usbip_stub_driver *stub_driver;
+
+int usbip_stub_driver_open(void);
+void usbip_stub_driver_close(void);
+
+int usbip_stub_refresh_device_list(void);
+int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd);
+
+struct usbip_exported_device *usbip_stub_get_device(int num);
+
+#endif /* __USBIP_STUB_DRIVER_H */
--
1.7.5.2

2011-06-20 05:52:00

by matt mooney

[permalink] [raw]
Subject: [PATCH 17/22] staging: usbip: userspace: usbip_host: update function and variable names

Officially change stub_driver to usbip_host_driver. And, reorganize
usbip_host_driver.c while also cleaning up coding style.

Signed-off-by: matt mooney <[email protected]>
---
.../usbip/userspace/libsrc/usbip_host_driver.c | 316 ++++++++++----------
.../usbip/userspace/libsrc/usbip_host_driver.h | 45 ++-
2 files changed, 189 insertions(+), 172 deletions(-)

diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
index 604aed1..71a449c 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
@@ -1,5 +1,19 @@
/*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 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 <sys/types.h>
@@ -9,41 +23,12 @@
#include <unistd.h>

#include "usbip_common.h"
-#include "stub_driver.h"
+#include "usbip_host_driver.h"

#undef PROGNAME
#define PROGNAME "libusbip"

-struct usbip_stub_driver *stub_driver;
-
-static struct sysfs_driver *open_sysfs_stub_driver(void)
-{
- int ret;
-
- char sysfs_mntpath[SYSFS_PATH_MAX];
- char stub_driver_path[SYSFS_PATH_MAX];
- struct sysfs_driver *stub_driver;
-
-
- ret = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
- if (ret < 0) {
- dbg("sysfs_get_mnt_path failed");
- return NULL;
- }
-
- snprintf(stub_driver_path, SYSFS_PATH_MAX, "%s/%s/usb/%s/%s",
- sysfs_mntpath, SYSFS_BUS_NAME, SYSFS_DRIVERS_NAME,
- USBIP_HOST_DRV_NAME);
-
- stub_driver = sysfs_open_driver_path(stub_driver_path);
- if (!stub_driver) {
- dbg("sysfs_open_driver_path failed");
- return NULL;
- }
-
- return stub_driver;
-}
-
+struct usbip_host_driver *host_driver;

#define SYSFS_OPEN_RETRIES 100

@@ -53,7 +38,7 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
char attrpath[SYSFS_PATH_MAX];
struct sysfs_attribute *attr;
int value = 0;
- int ret;
+ int rc;
struct stat s;
int retries = SYSFS_OPEN_RETRIES;

@@ -72,9 +57,7 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
*/

snprintf(attrpath, SYSFS_PATH_MAX, "%s/%s:%d.%d/usbip_status",
- udev->path, udev->busid,
- udev->bConfigurationValue,
- 0);
+ udev->path, udev->busid, udev->bConfigurationValue, 0);

while (retries > 0) {
if (stat(attrpath, &s) == 0)
@@ -102,8 +85,8 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
return -1;
}

- ret = sysfs_read_attribute(attr);
- if (ret) {
+ rc = sysfs_read_attribute(attr);
+ if (rc) {
dbg("sysfs_read_attribute failed: %s", attrpath);
sysfs_close_attribute(attr);
return -1;
@@ -116,22 +99,13 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
return value;
}

-
-static void usbip_exported_device_delete(void *dev)
-{
- struct usbip_exported_device *edev =
- (struct usbip_exported_device *) dev;
-
- sysfs_close_device(edev->sudev);
- free(dev);
-}
-
-
static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
{
struct usbip_exported_device *edev = NULL;
+ size_t size;
+ int i;

- edev = (struct usbip_exported_device *) calloc(1, sizeof(*edev));
+ edev = calloc(1, sizeof(*edev));
if (!edev) {
dbg("calloc failed");
return NULL;
@@ -150,223 +124,255 @@ static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
goto err;

/* reallocate buffer to include usb interface data */
- size_t size = sizeof(*edev) + edev->udev.bNumInterfaces *
+ size = sizeof(*edev) + edev->udev.bNumInterfaces *
sizeof(struct usbip_usb_interface);
- edev = (struct usbip_exported_device *) realloc(edev, size);
+
+ edev = realloc(edev, size);
if (!edev) {
dbg("realloc failed");
goto err;
}

- for (int i=0; i < edev->udev.bNumInterfaces; i++)
+ for (i = 0; i < edev->udev.bNumInterfaces; i++)
read_usb_interface(&edev->udev, i, &edev->uinf[i]);

return edev;
-
err:
if (edev && edev->sudev)
sysfs_close_device(edev->sudev);
if (edev)
free(edev);
+
return NULL;
}

-
static int check_new(struct dlist *dlist, struct sysfs_device *target)
{
struct sysfs_device *dev;

dlist_for_each_data(dlist, dev, struct sysfs_device) {
if (!strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
- /* found. not new */
+ /* device found and is not new */
return 0;
}
-
return 1;
}

-static void delete_nothing(void *dev __attribute__((unused)))
+static void delete_nothing(void *unused_data)
{
- /* do not delete anything. but, its container will be deleted. */
+ /*
+ * NOTE: Do not delete anything, but the container will be deleted.
+ */
+ (void) unused_data;
}

static int refresh_exported_devices(void)
{
- struct sysfs_device *suinf; /* sysfs_device of usb_interface */
- struct dlist *suinf_list;
-
- struct sysfs_device *sudev; /* sysfs_device of usb_device */
+ /* sysfs_device of usb_interface */
+ struct sysfs_device *suintf;
+ struct dlist *suintf_list;
+ /* sysfs_device of usb_device */
+ struct sysfs_device *sudev;
struct dlist *sudev_list;
+ struct usbip_exported_device *edev;

+ sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device),
+ delete_nothing);

- sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device), delete_nothing);
-
- suinf_list = sysfs_get_driver_devices(stub_driver->sysfs_driver);
- if (!suinf_list) {
+ suintf_list = sysfs_get_driver_devices(host_driver->sysfs_driver);
+ if (!suintf_list) {
+ /*
+ * Not an error condition. There are simply no devices bound to
+ * the driver yet.
+ */
dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
- "exportable!\n");
- goto bye;
+ "exportable!");
+ return 0;
}

/* collect unique USB devices (not interfaces) */
- dlist_for_each_data(suinf_list, suinf, struct sysfs_device) {
-
+ dlist_for_each_data(suintf_list, suintf, struct sysfs_device) {
/* get usb device of this usb interface */
- sudev = sysfs_get_device_parent(suinf);
+ sudev = sysfs_get_device_parent(suintf);
if (!sudev) {
- dbg("sysfs_get_device_parent failed: %s", suinf->name);
+ dbg("sysfs_get_device_parent failed: %s", suintf->name);
continue;
}

if (check_new(sudev_list, sudev)) {
+ /* insert item at head of list */
dlist_unshift(sudev_list, sudev);
}
}

dlist_for_each_data(sudev_list, sudev, struct sysfs_device) {
- struct usbip_exported_device *edev;
-
edev = usbip_exported_device_new(sudev->path);
if (!edev) {
dbg("usbip_exported_device_new failed");
continue;
}

- dlist_unshift(stub_driver->edev_list, (void *) edev);
- stub_driver->ndevs++;
+ dlist_unshift(host_driver->edev_list, edev);
+ host_driver->ndevs++;
}

-
dlist_destroy(sudev_list);

-bye:
-
return 0;
}

-int usbip_stub_refresh_device_list(void)
+static struct sysfs_driver *open_sysfs_host_driver(void)
{
- int ret;
+ char bus_type[] = "usb";
+ char sysfs_mntpath[SYSFS_PATH_MAX];
+ char host_drv_path[SYSFS_PATH_MAX];
+ struct sysfs_driver *host_drv;
+ int rc;

- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
+ rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
+ if (rc < 0) {
+ dbg("sysfs_get_mnt_path failed");
+ return NULL;
+ }

- stub_driver->ndevs = 0;
+ snprintf(host_drv_path, SYSFS_PATH_MAX, "%s/%s/%s/%s/%s",
+ sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
+ USBIP_HOST_DRV_NAME);

- stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
- usbip_exported_device_delete);
- if (!stub_driver->edev_list) {
- dbg("dlist_new_with_delete failed");
- return -1;
+ host_drv = sysfs_open_driver_path(host_drv_path);
+ if (!host_drv) {
+ dbg("sysfs_open_driver_path failed");
+ return NULL;
}

- ret = refresh_exported_devices();
- if (ret < 0)
- return ret;
-
- return 0;
+ return host_drv;
}

-int usbip_stub_driver_open(void)
+static void usbip_exported_device_delete(void *dev)
{
- int ret;
+ struct usbip_exported_device *edev = dev;
+ sysfs_close_device(edev->sudev);
+ free(dev);
+}

+int usbip_host_driver_open(void)
+{
+ int rc;

- stub_driver = (struct usbip_stub_driver *) calloc(1, sizeof(*stub_driver));
- if (!stub_driver) {
+ host_driver = calloc(1, sizeof(*host_driver));
+ if (!host_driver) {
dbg("calloc failed");
return -1;
}

- stub_driver->ndevs = 0;
-
- stub_driver->edev_list = dlist_new_with_delete(sizeof(struct usbip_exported_device),
- usbip_exported_device_delete);
- if (!stub_driver->edev_list) {
+ host_driver->ndevs = 0;
+ host_driver->edev_list =
+ dlist_new_with_delete(sizeof(struct usbip_exported_device),
+ usbip_exported_device_delete);
+ if (!host_driver->edev_list) {
dbg("dlist_new_with_delete failed");
- goto err;
+ goto err_free_host_driver;
}

- stub_driver->sysfs_driver = open_sysfs_stub_driver();
- if (!stub_driver->sysfs_driver)
- goto err;
+ host_driver->sysfs_driver = open_sysfs_host_driver();
+ if (!host_driver->sysfs_driver)
+ goto err_destroy_edev_list;

- ret = refresh_exported_devices();
- if (ret < 0)
- goto err;
+ rc = refresh_exported_devices();
+ if (rc < 0)
+ goto err_close_sysfs_driver;

return 0;

+err_close_sysfs_driver:
+ sysfs_close_driver(host_driver->sysfs_driver);
+err_destroy_edev_list:
+ dlist_destroy(host_driver->edev_list);
+err_free_host_driver:
+ free(host_driver);
+ host_driver = NULL;

-err:
- if (stub_driver->sysfs_driver)
- sysfs_close_driver(stub_driver->sysfs_driver);
- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
- free(stub_driver);
-
- stub_driver = NULL;
return -1;
}

-
-void usbip_stub_driver_close(void)
+void usbip_host_driver_close(void)
{
- if (!stub_driver)
+ if (!host_driver)
return;

- if (stub_driver->edev_list)
- dlist_destroy(stub_driver->edev_list);
- if (stub_driver->sysfs_driver)
- sysfs_close_driver(stub_driver->sysfs_driver);
- free(stub_driver);
+ if (host_driver->edev_list)
+ dlist_destroy(host_driver->edev_list);
+ if (host_driver->sysfs_driver)
+ sysfs_close_driver(host_driver->sysfs_driver);

- stub_driver = NULL;
+ free(host_driver);
+ host_driver = NULL;
}

-int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd)
+int usbip_host_refresh_device_list(void)
{
- char attrpath[SYSFS_PATH_MAX];
+ int rc;
+
+ if (host_driver->edev_list)
+ dlist_destroy(host_driver->edev_list);
+
+ host_driver->ndevs = 0;
+ host_driver->edev_list =
+ dlist_new_with_delete(sizeof(struct usbip_exported_device),
+ usbip_exported_device_delete);
+ if (!host_driver->edev_list) {
+ dbg("dlist_new_with_delete failed");
+ return -1;
+ }
+
+ rc = refresh_exported_devices();
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
+{
+ char attr_name[] = "usbip_sockfd";
+ char attr_path[SYSFS_PATH_MAX];
struct sysfs_attribute *attr;
char sockfd_buff[30];
int ret;

-
if (edev->status != SDEV_ST_AVAILABLE) {
dbg("device not available: %s", edev->udev.busid);
- switch( edev->status ) {
- case SDEV_ST_ERROR:
- dbg("status SDEV_ST_ERROR");
- break;
- case SDEV_ST_USED:
- dbg("status SDEV_ST_USED");
- break;
- default:
- dbg("status unknown: 0x%x", edev->status);
+ switch (edev->status) {
+ case SDEV_ST_ERROR:
+ dbg("status SDEV_ST_ERROR");
+ break;
+ case SDEV_ST_USED:
+ dbg("status SDEV_ST_USED");
+ break;
+ default:
+ dbg("status unknown: 0x%x", edev->status);
}
return -1;
}

/* only the first interface is true */
- snprintf(attrpath, sizeof(attrpath), "%s/%s:%d.%d/%s",
- edev->udev.path,
- edev->udev.busid,
- edev->udev.bConfigurationValue, 0,
- "usbip_sockfd");
+ snprintf(attr_path, sizeof(attr_path), "%s/%s:%d.%d/%s",
+ edev->udev.path, edev->udev.busid,
+ edev->udev.bConfigurationValue, 0, attr_name);

- attr = sysfs_open_attribute(attrpath);
+ attr = sysfs_open_attribute(attr_path);
if (!attr) {
- dbg("sysfs_open_attribute failed: %s", attrpath);
+ dbg("sysfs_open_attribute failed: %s", attr_path);
return -1;
}

snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
-
dbg("write: %s", sockfd_buff);
+
ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
if (ret < 0) {
- dbg("sysfs_write_attribute failed: sockfd %s to %s",
- sockfd_buff, attrpath);
+ dbg("sysfs_write_attribute failed: sockfd %s to %s",
+ sockfd_buff, attr_path);
goto err_write_sockfd;
}

@@ -378,17 +384,17 @@ err_write_sockfd:
return ret;
}

-struct usbip_exported_device *usbip_stub_get_device(int num)
+struct usbip_exported_device *usbip_host_get_device(int num)
{
struct usbip_exported_device *edev;
- struct dlist *dlist = stub_driver->edev_list;
- int count = 0;
+ struct dlist *dlist = host_driver->edev_list;
+ int cnt = 0;

dlist_for_each_data(dlist, edev, struct usbip_exported_device) {
- if (num == count)
+ if (num == cnt)
return edev;
else
- count++ ;
+ cnt++;
}

return NULL;
diff --git a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h
index 9eaf92c..34fd14c 100644
--- a/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h
+++ b/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.h
@@ -1,37 +1,48 @@
/*
- * Copyright (C) 2005-2007 Takahiro Hirofuchi
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 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/>.
*/

-#ifndef __USBIP_STUB_DRIVER_H
-#define __USBIP_STUB_DRIVER_H
+#ifndef __USBIP_HOST_DRIVER_H
+#define __USBIP_HOST_DRIVER_H

#include <stdint.h>
#include "usbip_common.h"

-struct usbip_stub_driver {
+struct usbip_host_driver {
int ndevs;
struct sysfs_driver *sysfs_driver;
-
- struct dlist *edev_list; /* list of exported device */
+ /* list of exported device */
+ struct dlist *edev_list;
};

struct usbip_exported_device {
struct sysfs_device *sudev;
-
int32_t status;
- struct usbip_usb_device udev;
+ struct usbip_usb_device udev;
struct usbip_usb_interface uinf[];
};

+extern struct usbip_host_driver *host_driver;

-extern struct usbip_stub_driver *stub_driver;
-
-int usbip_stub_driver_open(void);
-void usbip_stub_driver_close(void);
-
-int usbip_stub_refresh_device_list(void);
-int usbip_stub_export_device(struct usbip_exported_device *edev, int sockfd);
+int usbip_host_driver_open(void);
+void usbip_host_driver_close(void);

-struct usbip_exported_device *usbip_stub_get_device(int num);
+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);

-#endif /* __USBIP_STUB_DRIVER_H */
+#endif /* __USBIP_HOST_DRIVER_H */
--
1.7.5.2

2011-06-20 05:52:35

by matt mooney

[permalink] [raw]
Subject: [PATCH 18/22] staging: usbip: userspace: usbipd: major cleanup of daemon

Reorganize, rename [for clarity and to remove stub_driver
references], modify output messages, and cleanup coding style;
nevertheless, the actual implementation is pretty much untouched.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbipd.c | 693 +++++++++++++-------------
1 files changed, 345 insertions(+), 348 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbipd.c b/drivers/staging/usbip/userspace/src/usbipd.c
index 332f9e6..aa92623 100644
--- a/drivers/staging/usbip/userspace/src/usbipd.c
+++ b/drivers/staging/usbip/userspace/src/usbipd.c
@@ -1,6 +1,19 @@
/*
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 2005-2007 Takahiro Hirofuchi
*
- * Copyright (C) 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/>.
*/

#ifdef HAVE_CONFIG_H
@@ -27,66 +40,157 @@
#include <glib.h>
#include <signal.h>

-#include "stub_driver.h"
+#include "usbip_host_driver.h"
#include "usbip_common.h"
#include "usbip_network.h"

-static const char version[] = PACKAGE_STRING;
+#undef PROGNAME
+#define PROGNAME "usbipd"
+#define MAXSOCKFD 20
+
+GMainLoop *main_loop;

-static int send_reply_devlist(int sockfd)
+static const char usbip_version_string[] = PACKAGE_STRING;
+
+static const char usbipd_help_string[] =
+ "usage: usbipd [options] \n"
+ " -D, --daemon \n"
+ " Run as a daemon process. \n"
+ " \n"
+ " -d, --debug \n"
+ " Print debugging information. \n"
+ " \n"
+ " -h, --help \n"
+ " Print this help. \n"
+ " \n"
+ " -v, --version \n"
+ " Show version. \n";
+
+static void usbipd_help(void)
{
- int ret;
+ printf("%s\n", usbipd_help_string);
+}
+
+static int recv_request_import(int sockfd)
+{
+ struct op_import_request req;
+ struct op_common reply;
struct usbip_exported_device *edev;
- struct op_devlist_reply reply;
+ struct usbip_usb_device pdu_udev;
+ int found = 0;
+ int error = 0;
+ int rc;

+ memset(&req, 0, sizeof(req));
+ memset(&reply, 0, sizeof(reply));

- reply.ndev = 0;
+ rc = usbip_recv(sockfd, &req, sizeof(req));
+ if (rc < 0) {
+ dbg("usbip_recv failed: import request");
+ return -1;
+ }
+ PACK_OP_IMPORT_REQUEST(0, &req);

- /* how many devices are exported ? */
- dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
- reply.ndev += 1;
+ dlist_for_each_data(host_driver->edev_list, edev,
+ struct usbip_exported_device) {
+ if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
+ info("found requested device: %s", req.busid);
+ found = 1;
+ break;
+ }
}

- dbg("%d devices are exported", reply.ndev);
+ if (found) {
+ /* should set TCP_NODELAY for usbip */
+ usbip_set_nodelay(sockfd);

- ret = usbip_send_op_common(sockfd, OP_REP_DEVLIST, ST_OK);
- if (ret < 0) {
- err("send op_common");
- return ret;
+ /* export device needs a TCP/IP socket descriptor */
+ rc = usbip_host_export_device(edev, sockfd);
+ if (rc < 0)
+ error = 1;
+ } else {
+ info("requested device not found: %s", req.busid);
+ error = 1;
}

- PACK_OP_DEVLIST_REPLY(1, &reply);

- ret = usbip_send(sockfd, (void *) &reply, sizeof(reply));
- if (ret < 0) {
- err("send op_devlist_reply");
- return ret;
+ rc = usbip_send_op_common(sockfd, OP_REP_IMPORT,
+ (!error ? ST_OK : ST_NA));
+ if (rc < 0) {
+ dbg("usbip_send_op_common failed: %#0x", OP_REP_IMPORT);
+ return -1;
+ }
+
+ if (error) {
+ dbg("import request busid %s: failed", req.busid);
+ return -1;
+ }
+
+ memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
+ pack_usb_device(1, &pdu_udev);
+
+ rc = usbip_send(sockfd, &pdu_udev, sizeof(pdu_udev));
+ if (rc < 0) {
+ dbg("usbip_send failed: devinfo");
+ return -1;
}

- dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
- struct usbip_usb_device pdu_udev;
+ dbg("import request busid %s: complete", req.busid);
+
+ return 0;
+}

+static int send_reply_devlist(int connfd)
+{
+ struct usbip_exported_device *edev;
+ struct usbip_usb_device pdu_udev;
+ struct usbip_usb_interface pdu_uinf;
+ struct op_devlist_reply reply;
+ int i;
+ int rc;
+
+ reply.ndev = 0;
+ /* number of exported devices */
+ dlist_for_each_data(host_driver->edev_list, edev,
+ struct usbip_exported_device) {
+ reply.ndev += 1;
+ }
+ info("exportable devices: %d", reply.ndev);
+
+ rc = usbip_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
+ if (rc < 0) {
+ dbg("usbip_send_op_common failed: %#0x", OP_REP_DEVLIST);
+ return -1;
+ }
+ PACK_OP_DEVLIST_REPLY(1, &reply);
+
+ rc = usbip_send(connfd, &reply, sizeof(reply));
+ if (rc < 0) {
+ dbg("usbip_send failed: %#0x", OP_REP_DEVLIST);
+ return -1;
+ }
+
+ dlist_for_each_data(host_driver->edev_list, edev,
+ struct usbip_exported_device) {
dump_usb_device(&edev->udev);
memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
pack_usb_device(1, &pdu_udev);

- ret = usbip_send(sockfd, (void *) &pdu_udev, sizeof(pdu_udev));
- if (ret < 0) {
- err("send pdu_udev");
- return ret;
+ rc = usbip_send(connfd, &pdu_udev, sizeof(pdu_udev));
+ if (rc < 0) {
+ dbg("usbip_send failed: pdu_udev");
+ return -1;
}

- for (int i=0; i < edev->udev.bNumInterfaces; i++) {
- struct usbip_usb_interface pdu_uinf;
-
+ for (i = 0; i < edev->udev.bNumInterfaces; i++) {
dump_usb_interface(&edev->uinf[i]);
memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
pack_usb_interface(1, &pdu_uinf);

- ret = usbip_send(sockfd, (void *) &pdu_uinf, sizeof(pdu_uinf));
- if (ret < 0) {
- err("send pdu_uinf");
- return ret;
+ rc = usbip_send(connfd, &pdu_uinf, sizeof(pdu_uinf));
+ if (rc < 0) {
+ dbg("usbip_send failed: pdu_uinf");
+ return -1;
}
}
}
@@ -94,283 +198,227 @@ static int send_reply_devlist(int sockfd)
return 0;
}

-
-static int recv_request_devlist(int sockfd)
+static int recv_request_devlist(int connfd)
{
- int ret;
struct op_devlist_request req;
+ int rc;

memset(&req, 0, sizeof(req));

- ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
- if (ret < 0) {
- err("recv devlist request");
+ rc = usbip_recv(connfd, &req, sizeof(req));
+ if (rc < 0) {
+ dbg("usbip_recv failed: devlist request");
return -1;
}

- ret = send_reply_devlist(sockfd);
- if (ret < 0) {
- err("send devlist reply");
+ rc = send_reply_devlist(connfd);
+ if (rc < 0) {
+ dbg("send_reply_devlist failed");
return -1;
}

return 0;
}

-
-static int recv_request_import(int sockfd)
+static int recv_pdu(int connfd)
{
+ uint16_t code = OP_UNSPEC;
int ret;
- struct op_import_request req;
- struct op_common reply;
- struct usbip_exported_device *edev;
- int found = 0;
- int error = 0;
-
- memset(&req, 0, sizeof(req));
- memset(&reply, 0, sizeof(reply));

- ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
+ ret = usbip_recv_op_common(connfd, &code);
if (ret < 0) {
- err("recv import request");
+ dbg("could not receive opcode: %#0x", code);
return -1;
}

- PACK_OP_IMPORT_REQUEST(0, &req);
-
- dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
- if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
- dbg("found requested device %s", req.busid);
- found = 1;
- break;
- }
+ ret = usbip_host_refresh_device_list();
+ if (ret < 0) {
+ dbg("could not refresh device list: %d", ret);
+ return -1;
}

- if (found) {
- /* should set TCP_NODELAY for usbip */
- usbip_set_nodelay(sockfd);
-
- /* export_device needs a TCP/IP socket descriptor */
- ret = usbip_stub_export_device(edev, sockfd);
- if (ret < 0)
- error = 1;
- } else {
- info("not found requested device %s", req.busid);
- error = 1;
+ info("received request: %#0x(%d)", code, connfd);
+ switch (code) {
+ case OP_REQ_DEVLIST:
+ ret = recv_request_devlist(connfd);
+ break;
+ case OP_REQ_IMPORT:
+ ret = recv_request_import(connfd);
+ break;
+ case OP_REQ_DEVINFO:
+ case OP_REQ_CRYPKEY:
+ default:
+ err("received an unknown opcode: %#0x", code);
+ ret = -1;
}

+ if (ret == 0)
+ info("request %#0x(%d): complete", code, connfd);
+ else
+ info("request %#0x(%d): failed", code, connfd);

- ret = usbip_send_op_common(sockfd, OP_REP_IMPORT, (!error ? ST_OK : ST_NA));
- if (ret < 0) {
- err("send import reply");
- return -1;
- }
-
- if (!error) {
- struct usbip_usb_device pdu_udev;
+ return ret;
+}

- memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
- pack_usb_device(1, &pdu_udev);
+#ifdef HAVE_LIBWRAP
+static int tcpd_auth(int connfd)
+{
+ struct request_info request;
+ int rc;

- ret = usbip_send(sockfd, (void *) &pdu_udev, sizeof(pdu_udev));
- if (ret < 0) {
- err("send devinfo");
- return -1;
- }
- }
+ request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
+ fromhost(&request);
+ rc = hosts_access(&request);
+ if (rc == 0)
+ return -1;

return 0;
}
+#endif

-
-
-static int recv_pdu(int sockfd)
+static int do_accept(int listenfd)
{
- int ret;
- uint16_t code = OP_UNSPEC;
+ int connfd;
+ struct sockaddr_storage ss;
+ socklen_t len = sizeof(ss);
+ char host[NI_MAXHOST], port[NI_MAXSERV];
+ int rc;

+ memset(&ss, 0, sizeof(ss));

- ret = usbip_recv_op_common(sockfd, &code);
- if (ret < 0) {
- err("recv op_common, %d", ret);
- return ret;
+ connfd = accept(listenfd, (struct sockaddr *) &ss, &len);
+ if (connfd < 0) {
+ err("failed to accept connection");
+ return -1;
}

+ rc = getnameinfo((struct sockaddr *) &ss, len, host, sizeof(host),
+ port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
+ if (rc)
+ err("getnameinfo: %s", gai_strerror(rc));

- ret = usbip_stub_refresh_device_list();
- if (ret < 0)
+#ifdef HAVE_LIBWRAP
+ rc = tcpd_auth(connfd);
+ if (rc < 0) {
+ info("denied access from %s", host);
+ close(connfd);
return -1;
+ }
+#endif
+ info("connection from %s:%s", host, port);

- switch(code) {
- case OP_REQ_DEVLIST:
- ret = recv_request_devlist(sockfd);
- break;
+ return connfd;
+}

- case OP_REQ_IMPORT:
- ret = recv_request_import(sockfd);
- break;
+gboolean process_request(GIOChannel *gio, GIOCondition condition,
+ gpointer unused_data)
+{
+ int listenfd;
+ int connfd;

- case OP_REQ_DEVINFO:
- case OP_REQ_CRYPKEY:
+ (void) unused_data;

- default:
- err("unknown op_code, %d", code);
- ret = -1;
+ if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
+ err("unknown condition");
+ BUG();
}

+ if (condition & G_IO_IN) {
+ listenfd = g_io_channel_unix_get_fd(gio);
+ connfd = do_accept(listenfd);
+ if (connfd < 0)
+ return TRUE;

- return ret;
-}
-
-
+ recv_pdu(connfd);
+ close(connfd);
+ }

+ return TRUE;
+}

static void log_addrinfo(struct addrinfo *ai)
{
- int ret;
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
+ int rc;

- ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
- sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
- if (ret)
- err("getnameinfo, %s", gai_strerror(ret));
+ rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
+ sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
+ if (rc)
+ err("getnameinfo: %s", gai_strerror(rc));

- info("listen at [%s]:%s", hbuf, sbuf);
-}
-
-static struct addrinfo *my_getaddrinfo(char *host, int ai_family)
-{
- int ret;
- struct addrinfo hints, *ai_head;
-
- memset(&hints, 0, sizeof(hints));
-
- hints.ai_family = ai_family;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE;
-
- ret = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
- if (ret) {
- err("%s: %s", USBIP_PORT_STRING, gai_strerror(ret));
- return NULL;
- }
-
- return ai_head;
+ info("listening on %s:%s", hbuf, sbuf);
}

-#define MAXSOCK 20
-static int listen_all_addrinfo(struct addrinfo *ai_head, int lsock[])
+static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
{
struct addrinfo *ai;
- int n = 0; /* number of sockets */
+ int ret, nsockfd = 0;

- for (ai = ai_head; ai && n < MAXSOCK; ai = ai->ai_next) {
- int ret;
-
- lsock[n] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (lsock[n] < 0)
+ for (ai = ai_head; ai && nsockfd < MAXSOCKFD; ai = ai->ai_next) {
+ sockfdlist[nsockfd] = socket(ai->ai_family, ai->ai_socktype,
+ ai->ai_protocol);
+ if (sockfdlist[nsockfd] < 0)
continue;

- usbip_set_reuseaddr(lsock[n]);
- usbip_set_nodelay(lsock[n]);
+ usbip_set_reuseaddr(sockfdlist[nsockfd]);
+ usbip_set_nodelay(sockfdlist[nsockfd]);

- if (lsock[n] >= FD_SETSIZE) {
- close(lsock[n]);
- lsock[n] = -1;
+ if (sockfdlist[nsockfd] >= FD_SETSIZE) {
+ close(sockfdlist[nsockfd]);
+ sockfdlist[nsockfd] = -1;
continue;
}

- ret = bind(lsock[n], ai->ai_addr, ai->ai_addrlen);
+ ret = bind(sockfdlist[nsockfd], ai->ai_addr, ai->ai_addrlen);
if (ret < 0) {
- close(lsock[n]);
- lsock[n] = -1;
+ close(sockfdlist[nsockfd]);
+ sockfdlist[nsockfd] = -1;
continue;
}

- ret = listen(lsock[n], SOMAXCONN);
+ ret = listen(sockfdlist[nsockfd], SOMAXCONN);
if (ret < 0) {
- close(lsock[n]);
- lsock[n] = -1;
+ close(sockfdlist[nsockfd]);
+ sockfdlist[nsockfd] = -1;
continue;
}

log_addrinfo(ai);
-
- /* next if succeed */
- n++;
+ nsockfd++;
}

- if (n == 0) {
- err("no socket to listen to");
+ if (nsockfd == 0)
return -1;
- }

- dbg("listen %d address%s", n, (n==1)?"":"es");
+ dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");

- return n;
+ return nsockfd;
}

-#ifdef HAVE_LIBWRAP
-static int tcpd_auth(int csock)
+static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
{
- int ret;
- struct request_info request;
-
- request_init(&request, RQ_DAEMON, "usbipd", RQ_FILE, csock, 0);
-
- fromhost(&request);
-
- ret = hosts_access(&request);
- if (!ret)
- return -1;
-
- return 0;
-}
-#endif
-
-static int my_accept(int lsock)
-{
- int csock;
- struct sockaddr_storage ss;
- socklen_t len = sizeof(ss);
- char host[NI_MAXHOST], port[NI_MAXSERV];
- int ret;
-
- memset(&ss, 0, sizeof(ss));
-
- csock = accept(lsock, (struct sockaddr *) &ss, &len);
- if (csock < 0) {
- err("accept");
- return -1;
- }
+ struct addrinfo hints, *ai_head;
+ int rc;

- ret = getnameinfo((struct sockaddr *) &ss, len,
- host, sizeof(host), port, sizeof(port),
- (NI_NUMERICHOST | NI_NUMERICSERV));
- if (ret)
- err("getnameinfo, %s", gai_strerror(ret));
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = ai_family;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;

-#ifdef HAVE_LIBWRAP
- ret = tcpd_auth(csock);
- if (ret < 0) {
- info("deny access from %s", host);
- close(csock);
- return -1;
+ rc = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
+ if (rc) {
+ err("failed to get a network address %s: %s", USBIP_PORT_STRING,
+ gai_strerror(rc));
+ return NULL;
}
-#endif
-
- info("connected from %s:%s", host, port);

- return csock;
+ return ai_head;
}

-
-GMainLoop *main_loop;
-
static void signal_handler(int i)
{
- dbg("signal catched, code %d", i);
+ dbg("received signal: code %d", i);

if (main_loop)
g_main_loop_quit(main_loop);
@@ -387,184 +435,133 @@ static void set_signal(void)
sigaction(SIGINT, &act, NULL);
}

-
-gboolean process_comming_request(GIOChannel *gio, GIOCondition condition,
- gpointer data __attribute__((unused)))
+static int do_standalone_mode(gboolean daemonize)
{
- int ret;
-
- if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
- g_error("unknown condition");
-
-
- if (condition & G_IO_IN) {
- int lsock;
- int csock;
-
- lsock = g_io_channel_unix_get_fd(gio);
-
- csock = my_accept(lsock);
- if (csock < 0)
- return TRUE;
-
- ret = recv_pdu(csock);
- if (ret < 0)
- err("process recieved pdu");
-
- close(csock);
- }
-
- return TRUE;
-}
-
-
-static void do_standalone_mode(gboolean daemonize)
-{
- int ret;
- int lsock[MAXSOCK];
struct addrinfo *ai_head;
- int n;
-
-
+ int sockfdlist[MAXSOCKFD];
+ int nsockfd;
+ int i;

- ret = usbip_names_init(USBIDS_FILE);
- if (ret)
- err("open usb.ids");
+ if (usbip_names_init(USBIDS_FILE))
+ err("failed to open %s", USBIDS_FILE);

- ret = usbip_stub_driver_open();
- if (ret < 0)
- g_error("driver open failed");
+ if (usbip_host_driver_open()) {
+ err("please load " USBIP_CORE_MOD_NAME ".ko and "
+ USBIP_HOST_DRV_NAME ".ko!");
+ return -1;
+ }

if (daemonize) {
- if (daemon(0,0) < 0)
- g_error("daemonizing failed: %s", g_strerror(errno));
+ if (daemon(0,0) < 0) {
+ err("daemonizing failed: %s", strerror(errno));
+ return -1;
+ }

usbip_use_syslog = 1;
}
-
set_signal();

- ai_head = my_getaddrinfo(NULL, PF_UNSPEC);
+ ai_head = do_getaddrinfo(NULL, PF_UNSPEC);
if (!ai_head)
- return;
+ return -1;

- n = listen_all_addrinfo(ai_head, lsock);
- if (n <= 0)
- g_error("no socket to listen to");
+ info("starting " PROGNAME " (%s)", usbip_version_string);
+
+ nsockfd = listen_all_addrinfo(ai_head, sockfdlist);
+ if (nsockfd <= 0) {
+ err("failed to open a listening socket");
+ return -1;
+ }

- for (int i = 0; i < n; i++) {
+ for (i = 0; i < nsockfd; i++) {
GIOChannel *gio;

- gio = g_io_channel_unix_new(lsock[i]);
+ gio = g_io_channel_unix_new(sockfdlist[i]);
g_io_add_watch(gio, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
- process_comming_request, NULL);
+ process_request, NULL);
}

-
- info("usbipd start (%s)", version);
-
-
main_loop = g_main_loop_new(FALSE, FALSE);
g_main_loop_run(main_loop);

- info("shutdown");
+ info("shutting down " PROGNAME);

freeaddrinfo(ai_head);
+ usbip_host_driver_close();
usbip_names_free();
- usbip_stub_driver_close();

- return;
-}
-
-
-static const char help_message[] = "\
-Usage: usbipd [options] \n\
- -D, --daemon \n\
- Run as a daemon process. \n\
- \n\
- -d, --debug \n\
- Print debugging information. \n\
- \n\
- -v, --version \n\
- Show version. \n\
- \n\
- -h, --help \n\
- Print this help. \n";
-
-static void show_help(void)
-{
- printf("%s", help_message);
+ return 0;
}

-static const struct option longopts[] = {
- {"daemon", no_argument, NULL, 'D'},
- {"debug", no_argument, NULL, 'd'},
- {"version", no_argument, NULL, 'v'},
- {"help", no_argument, NULL, 'h'},
- {NULL, 0, NULL, 0}
-};
-
int main(int argc, char *argv[])
{
- gboolean daemonize = FALSE;
+ static const struct option longopts[] = {
+ { "daemon", no_argument, NULL, 'D' },
+ { "debug", no_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'v' },
+ { NULL, 0, NULL, 0 }
+ };

enum {
cmd_standalone_mode = 1,
cmd_help,
cmd_version
- } cmd = cmd_standalone_mode;
+ } cmd;

+ gboolean daemonize = FALSE;
+ int opt, rc = -1;

usbip_use_stderr = 1;
usbip_use_syslog = 0;

if (geteuid() != 0)
- g_warning("running non-root?");
+ err("not running as root?");

+ cmd = cmd_standalone_mode;
for (;;) {
- int c;
- int index = 0;
+ opt = getopt_long(argc, argv, "Ddhv", longopts, NULL);

- c = getopt_long(argc, argv, "vhdD", longopts, &index);
-
- if (c == -1)
+ if (opt == -1)
break;

- switch (c) {
- case 'd':
- usbip_use_debug = 1;
- continue;
- case 'v':
- cmd = cmd_version;
- break;
- case 'h':
- cmd = cmd_help;
- break;
- case 'D':
- daemonize = TRUE;
- break;
- case '?':
- show_help();
- exit(EXIT_FAILURE);
- default:
- err("getopt");
- }
- }
-
- switch (cmd) {
- case cmd_standalone_mode:
- do_standalone_mode(daemonize);
+ switch (opt) {
+ case 'D':
+ daemonize = TRUE;
+ break;
+ case 'd':
+ usbip_use_debug = 1;
break;
- case cmd_version:
- printf("%s\n", version);
+ case 'h':
+ cmd = cmd_help;
break;
- case cmd_help:
- show_help();
+ case 'v':
+ cmd = cmd_version;
break;
+ case '?':
+ usbipd_help();
default:
- info("unknown cmd");
- show_help();
+ goto err_out;
+ }
}

- return 0;
+ switch (cmd) {
+ case cmd_standalone_mode:
+ rc = do_standalone_mode(daemonize);
+ break;
+ case cmd_version:
+ printf(PROGNAME " (%s)\n", usbip_version_string);
+ rc = 0;
+ break;
+ case cmd_help:
+ usbipd_help();
+ rc = 0;
+ break;
+ default:
+ usbipd_help();
+ goto err_out;
+ }
+
+err_out:
+ return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
--
1.7.5.2

2011-06-20 05:45:48

by matt mooney

[permalink] [raw]
Subject: [PATCH 19/22] staging: usbip: userspace: usbip_network.c: coding style cleanup

Change messges to debug, and fix a few coding style issues.

Signed-off-by: matt mooney <[email protected]>
---
.../staging/usbip/userspace/src/usbip_network.c | 73 +++++++++++--------
1 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_network.c b/drivers/staging/usbip/userspace/src/usbip_network.c
index 26e95bd..a3833ff 100644
--- a/drivers/staging/usbip/userspace/src/usbip_network.c
+++ b/drivers/staging/usbip/userspace/src/usbip_network.c
@@ -1,6 +1,19 @@
/*
+ * Copyright (C) 2011 matt mooney <[email protected]>
+ * 2005-2007 Takahiro Hirofuchi
*
- * Copyright (C) 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 <sys/socket.h>
@@ -56,17 +69,15 @@ void pack_usb_interface(int pack __attribute__((unused)),
/* uint8_t members need nothing */
}

-
static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
{
+ ssize_t nbytes;
ssize_t total = 0;

if (!bufflen)
return 0;

do {
- ssize_t nbytes;
-
if (sending)
nbytes = send(sockfd, buff, bufflen, 0);
else
@@ -75,13 +86,12 @@ static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
if (nbytes <= 0)
return -1;

- buff = (void *) ((intptr_t) buff + nbytes);
+ buff = (void *)((intptr_t) buff + nbytes);
bufflen -= nbytes;
total += nbytes;

} while (bufflen > 0);

-
return total;
}

@@ -97,8 +107,8 @@ ssize_t usbip_send(int sockfd, void *buff, size_t bufflen)

int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)
{
- int ret;
struct op_common op_common;
+ int rc;

memset(&op_common, 0, sizeof(op_common));

@@ -108,9 +118,9 @@ int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)

PACK_OP_COMMON(1, &op_common);

- ret = usbip_send(sockfd, (void *) &op_common, sizeof(op_common));
- if (ret < 0) {
- err("usbip_send has failed");
+ rc = usbip_send(sockfd, &op_common, sizeof(op_common));
+ if (rc < 0) {
+ dbg("usbip_send failed: %d", rc);
return -1;
}

@@ -119,36 +129,38 @@ int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)

int usbip_recv_op_common(int sockfd, uint16_t *code)
{
- int ret;
struct op_common op_common;
+ int rc;

memset(&op_common, 0, sizeof(op_common));

- ret = usbip_recv(sockfd, (void *) &op_common, sizeof(op_common));
- if (ret < 0) {
- err("usbip_recv has failed ret=%d", ret);
+ rc = usbip_recv(sockfd, &op_common, sizeof(op_common));
+ if (rc < 0) {
+ dbg("usbip_recv failed: %d", rc);
goto err;
}

PACK_OP_COMMON(0, &op_common);

if (op_common.version != USBIP_VERSION) {
- err("version mismatch, %d %d", op_common.version, USBIP_VERSION);
+ dbg("version mismatch: %d %d", op_common.version,
+ USBIP_VERSION);
goto err;
}

- switch(*code) {
- case OP_UNSPEC:
- break;
- default:
- if (op_common.code != *code) {
- info("unexpected pdu %d for %d", op_common.code, *code);
- goto err;
- }
+ switch (*code) {
+ case OP_UNSPEC:
+ break;
+ default:
+ if (op_common.code != *code) {
+ dbg("unexpected pdu %#0x for %#0x", op_common.code,
+ *code);
+ goto err;
+ }
}

if (op_common.status != ST_OK) {
- info("request failed at peer, %d", op_common.status);
+ dbg("request failed at peer: %d", op_common.status);
goto err;
}

@@ -159,7 +171,6 @@ err:
return -1;
}

-
int usbip_set_reuseaddr(int sockfd)
{
const int val = 1;
@@ -167,7 +178,7 @@ int usbip_set_reuseaddr(int sockfd)

ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if (ret < 0)
- err("setsockopt SO_REUSEADDR");
+ dbg("setsockopt: SO_REUSEADDR");

return ret;
}
@@ -179,7 +190,7 @@ int usbip_set_nodelay(int sockfd)

ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
if (ret < 0)
- err("setsockopt TCP_NODELAY");
+ dbg("setsockopt: TCP_NODELAY");

return ret;
}
@@ -191,7 +202,7 @@ int usbip_set_keepalive(int sockfd)

ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
if (ret < 0)
- err("setsockopt SO_KEEPALIVE");
+ dbg("setsockopt: SO_KEEPALIVE");

return ret;
}
@@ -199,7 +210,7 @@ int usbip_set_keepalive(int sockfd)
/*
* IPv6 Ready
*/
-int usbip_net_tcp_connect(char *hostname, char *port)
+int usbip_net_tcp_connect(char *hostname, char *service)
{
struct addrinfo hints, *res, *rp;
int sockfd;
@@ -210,9 +221,9 @@ int usbip_net_tcp_connect(char *hostname, char *port)
hints.ai_socktype = SOCK_STREAM;

/* get all possible addresses */
- ret = getaddrinfo(hostname, port, &hints, &res);
+ ret = getaddrinfo(hostname, service, &hints, &res);
if (ret < 0) {
- dbg("getaddrinfo: %s port %s: %s", hostname, port,
+ dbg("getaddrinfo: %s service %s: %s", hostname, service,
gai_strerror(ret));
return ret;
}
--
1.7.5.2

2011-06-20 05:45:54

by matt mooney

[permalink] [raw]
Subject: [PATCH 20/22] staging: usbip: userspace: add new prefix for usbip network code

Change and add new usbip_net_ prefix to every function in the network
code for easier identification.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/src/usbip_attach.c | 8 ++--
drivers/staging/usbip/userspace/src/usbip_list.c | 26 +++++----
.../staging/usbip/userspace/src/usbip_network.c | 56 ++++++++++---------
.../staging/usbip/userspace/src/usbip_network.h | 39 +++++++-------
drivers/staging/usbip/userspace/src/usbipd.c | 50 +++++++++---------
5 files changed, 91 insertions(+), 88 deletions(-)

diff --git a/drivers/staging/usbip/userspace/src/usbip_attach.c b/drivers/staging/usbip/userspace/src/usbip_attach.c
index 189238b..b7885a2 100644
--- a/drivers/staging/usbip/userspace/src/usbip_attach.c
+++ b/drivers/staging/usbip/userspace/src/usbip_attach.c
@@ -115,7 +115,7 @@ static int query_import_device(int sockfd, char *busid)
memset(&reply, 0, sizeof(reply));

/* send a request */
- rc = usbip_send_op_common(sockfd, OP_REQ_IMPORT, 0);
+ rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
if (rc < 0) {
err("send op_common");
return -1;
@@ -125,20 +125,20 @@ static int query_import_device(int sockfd, char *busid)

PACK_OP_IMPORT_REQUEST(0, &request);

- rc = usbip_send(sockfd, (void *) &request, sizeof(request));
+ rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
if (rc < 0) {
err("send op_import_request");
return -1;
}

/* recieve a reply */
- rc = usbip_recv_op_common(sockfd, &code);
+ rc = usbip_net_recv_op_common(sockfd, &code);
if (rc < 0) {
err("recv op_common");
return -1;
}

- rc = usbip_recv(sockfd, (void *) &reply, sizeof(reply));
+ rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
if (rc < 0) {
err("recv op_import_reply");
return -1;
diff --git a/drivers/staging/usbip/userspace/src/usbip_list.c b/drivers/staging/usbip/userspace/src/usbip_list.c
index 973bf8c..3edf5d1 100644
--- a/drivers/staging/usbip/userspace/src/usbip_list.c
+++ b/drivers/staging/usbip/userspace/src/usbip_list.c
@@ -56,22 +56,22 @@ static int get_exported_devices(char *host, int sockfd)
unsigned int i;
int j, rc;

- rc = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
+ rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
if (rc < 0) {
- dbg("usbip_send_op_common failed");
+ dbg("usbip_net_send_op_common failed");
return -1;
}

- rc = usbip_recv_op_common(sockfd, &code);
+ rc = usbip_net_recv_op_common(sockfd, &code);
if (rc < 0) {
- dbg("usbip_recv_op_common failed");
+ dbg("usbip_net_recv_op_common failed");
return -1;
}

memset(&reply, 0, sizeof(reply));
- rc = usbip_recv(sockfd, &reply, sizeof(reply));
+ rc = usbip_net_recv(sockfd, &reply, sizeof(reply));
if (rc < 0) {
- dbg("usbip_recv_op_devlist failed");
+ dbg("usbip_net_recv_op_devlist failed");
return -1;
}
PACK_OP_DEVLIST_REPLY(0, &reply);
@@ -88,12 +88,12 @@ static int get_exported_devices(char *host, int sockfd)

for (i = 0; i < reply.ndev; i++) {
memset(&udev, 0, sizeof(udev));
- rc = usbip_recv(sockfd, &udev, sizeof(udev));
+ rc = usbip_net_recv(sockfd, &udev, sizeof(udev));
if (rc < 0) {
- dbg("usbip_recv failed: usbip_usb_device[%d]", i);
+ dbg("usbip_net_recv failed: usbip_usb_device[%d]", i);
return -1;
}
- pack_usb_device(0, &udev);
+ usbip_net_pack_usb_device(0, &udev);

usbip_names_get_product(product_name, sizeof(product_name),
udev.idVendor, udev.idProduct);
@@ -105,12 +105,14 @@ static int get_exported_devices(char *host, int sockfd)
printf("%8s: %s\n", "", class_name);

for (j = 0; j < udev.bNumInterfaces; j++) {
- rc = usbip_recv(sockfd, &uintf, sizeof(uintf));
+ rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf));
if (rc < 0) {
- dbg("usbip_recv failed: usbip_usb_intf[%d]", j);
+ dbg("usbip_net_recv failed: usbip_usb_intf[%d]",
+ j);
+
return -1;
}
- pack_usb_interface(0, &uintf);
+ usbip_net_pack_usb_interface(0, &uintf);

usbip_names_get_class(class_name, sizeof(class_name),
uintf.bInterfaceClass,
diff --git a/drivers/staging/usbip/userspace/src/usbip_network.c b/drivers/staging/usbip/userspace/src/usbip_network.c
index a3833ff..1a84dd3 100644
--- a/drivers/staging/usbip/userspace/src/usbip_network.c
+++ b/drivers/staging/usbip/userspace/src/usbip_network.c
@@ -28,7 +28,7 @@
#include "usbip_common.h"
#include "usbip_network.h"

-void pack_uint32_t(int pack, uint32_t *num)
+void usbip_net_pack_uint32_t(int pack, uint32_t *num)
{
uint32_t i;

@@ -40,7 +40,7 @@ void pack_uint32_t(int pack, uint32_t *num)
*num = i;
}

-void pack_uint16_t(int pack, uint16_t *num)
+void usbip_net_pack_uint16_t(int pack, uint16_t *num)
{
uint16_t i;

@@ -52,24 +52,26 @@ void pack_uint16_t(int pack, uint16_t *num)
*num = i;
}

-void pack_usb_device(int pack, struct usbip_usb_device *udev)
+void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev)
{
- pack_uint32_t(pack, &udev->busnum);
- pack_uint32_t(pack, &udev->devnum);
- pack_uint32_t(pack, &udev->speed );
+ usbip_net_pack_uint32_t(pack, &udev->busnum);
+ usbip_net_pack_uint32_t(pack, &udev->devnum);
+ usbip_net_pack_uint32_t(pack, &udev->speed );

- pack_uint16_t(pack, &udev->idVendor );
- pack_uint16_t(pack, &udev->idProduct);
- pack_uint16_t(pack, &udev->bcdDevice);
+ usbip_net_pack_uint16_t(pack, &udev->idVendor);
+ usbip_net_pack_uint16_t(pack, &udev->idProduct);
+ usbip_net_pack_uint16_t(pack, &udev->bcdDevice);
}

-void pack_usb_interface(int pack __attribute__((unused)),
- struct usbip_usb_interface *udev __attribute__((unused)))
+void usbip_net_pack_usb_interface(int pack __attribute__((unused)),
+ struct usbip_usb_interface *udev
+ __attribute__((unused)))
{
/* uint8_t members need nothing */
}

-static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
+static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen,
+ int sending)
{
ssize_t nbytes;
ssize_t total = 0;
@@ -95,17 +97,17 @@ static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
return total;
}

-ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen)
+ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen)
{
- return usbip_xmit(sockfd, buff, bufflen, 0);
+ return usbip_net_xmit(sockfd, buff, bufflen, 0);
}

-ssize_t usbip_send(int sockfd, void *buff, size_t bufflen)
+ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen)
{
- return usbip_xmit(sockfd, buff, bufflen, 1);
+ return usbip_net_xmit(sockfd, buff, bufflen, 1);
}

-int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)
+int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
{
struct op_common op_common;
int rc;
@@ -118,25 +120,25 @@ int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)

PACK_OP_COMMON(1, &op_common);

- rc = usbip_send(sockfd, &op_common, sizeof(op_common));
+ rc = usbip_net_send(sockfd, &op_common, sizeof(op_common));
if (rc < 0) {
- dbg("usbip_send failed: %d", rc);
+ dbg("usbip_net_send failed: %d", rc);
return -1;
}

return 0;
}

-int usbip_recv_op_common(int sockfd, uint16_t *code)
+int usbip_net_recv_op_common(int sockfd, uint16_t *code)
{
struct op_common op_common;
int rc;

memset(&op_common, 0, sizeof(op_common));

- rc = usbip_recv(sockfd, &op_common, sizeof(op_common));
+ rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common));
if (rc < 0) {
- dbg("usbip_recv failed: %d", rc);
+ dbg("usbip_net_recv failed: %d", rc);
goto err;
}

@@ -171,7 +173,7 @@ err:
return -1;
}

-int usbip_set_reuseaddr(int sockfd)
+int usbip_net_set_reuseaddr(int sockfd)
{
const int val = 1;
int ret;
@@ -183,7 +185,7 @@ int usbip_set_reuseaddr(int sockfd)
return ret;
}

-int usbip_set_nodelay(int sockfd)
+int usbip_net_set_nodelay(int sockfd)
{
const int val = 1;
int ret;
@@ -195,7 +197,7 @@ int usbip_set_nodelay(int sockfd)
return ret;
}

-int usbip_set_keepalive(int sockfd)
+int usbip_net_set_keepalive(int sockfd)
{
const int val = 1;
int ret;
@@ -236,9 +238,9 @@ int usbip_net_tcp_connect(char *hostname, char *service)
continue;

/* should set TCP_NODELAY for usbip */
- usbip_set_nodelay(sockfd);
+ usbip_net_set_nodelay(sockfd);
/* TODO: write code for heartbeat */
- usbip_set_keepalive(sockfd);
+ usbip_net_set_keepalive(sockfd);

if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
diff --git a/drivers/staging/usbip/userspace/src/usbip_network.h b/drivers/staging/usbip/userspace/src/usbip_network.h
index 07274df..2d1e070 100644
--- a/drivers/staging/usbip/userspace/src/usbip_network.h
+++ b/drivers/staging/usbip/userspace/src/usbip_network.h
@@ -34,9 +34,9 @@ struct op_common {
} __attribute__((packed));

#define PACK_OP_COMMON(pack, op_common) do {\
- pack_uint16_t(pack, &(op_common)->version);\
- pack_uint16_t(pack, &(op_common)->code );\
- pack_uint32_t(pack, &(op_common)->status );\
+ usbip_net_pack_uint16_t(pack, &(op_common)->version);\
+ usbip_net_pack_uint16_t(pack, &(op_common)->code );\
+ usbip_net_pack_uint32_t(pack, &(op_common)->status );\
} while (0)

/* ---------------------------------------------------------------------- */
@@ -79,7 +79,7 @@ struct op_import_reply {
} while (0)

#define PACK_OP_IMPORT_REPLY(pack, reply) do {\
- pack_usb_device(pack, &(reply)->udev);\
+ usbip_net_pack_usb_device(pack, &(reply)->udev);\
} while (0)

/* ---------------------------------------------------------------------- */
@@ -98,7 +98,7 @@ struct op_export_reply {


#define PACK_OP_EXPORT_REQUEST(pack, request) do {\
- pack_usb_device(pack, &(request)->udev);\
+ usbip_net_pack_usb_device(pack, &(request)->udev);\
} while (0)

#define PACK_OP_EXPORT_REPLY(pack, reply) do {\
@@ -119,7 +119,7 @@ struct op_unexport_reply {
} __attribute__((packed));

#define PACK_OP_UNEXPORT_REQUEST(pack, request) do {\
- pack_usb_device(pack, &(request)->udev);\
+ usbip_net_pack_usb_device(pack, &(request)->udev);\
} while (0)

#define PACK_OP_UNEXPORT_REPLY(pack, reply) do {\
@@ -164,22 +164,21 @@ struct op_devlist_reply_extra {
} while (0)

#define PACK_OP_DEVLIST_REPLY(pack, reply) do {\
- pack_uint32_t(pack, &(reply)->ndev);\
+ usbip_net_pack_uint32_t(pack, &(reply)->ndev);\
} while (0)

-void pack_uint32_t(int pack, uint32_t *num);
-void pack_uint16_t(int pack, uint16_t *num);
-void pack_usb_device(int pack, struct usbip_usb_device *udev);
-void pack_usb_interface(int pack, struct usbip_usb_interface *uinf);
-
-ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen);
-ssize_t usbip_send(int sockfd, void *buff, size_t bufflen);
-int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status);
-int usbip_recv_op_common(int sockfd, uint16_t *code);
-int usbip_set_reuseaddr(int sockfd);
-int usbip_set_nodelay(int sockfd);
-int usbip_set_keepalive(int sockfd);
-
+void usbip_net_pack_uint32_t(int pack, uint32_t *num);
+void usbip_net_pack_uint16_t(int pack, uint16_t *num);
+void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev);
+void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf);
+
+ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen);
+ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen);
+int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status);
+int usbip_net_recv_op_common(int sockfd, uint16_t *code);
+int usbip_net_set_reuseaddr(int sockfd);
+int usbip_net_set_nodelay(int sockfd);
+int usbip_net_set_keepalive(int sockfd);
int usbip_net_tcp_connect(char *hostname, char *port);

#endif /* __USBIP_NETWORK_H */
diff --git a/drivers/staging/usbip/userspace/src/usbipd.c b/drivers/staging/usbip/userspace/src/usbipd.c
index aa92623..8668a80 100644
--- a/drivers/staging/usbip/userspace/src/usbipd.c
+++ b/drivers/staging/usbip/userspace/src/usbipd.c
@@ -84,9 +84,9 @@ static int recv_request_import(int sockfd)
memset(&req, 0, sizeof(req));
memset(&reply, 0, sizeof(reply));

- rc = usbip_recv(sockfd, &req, sizeof(req));
+ rc = usbip_net_recv(sockfd, &req, sizeof(req));
if (rc < 0) {
- dbg("usbip_recv failed: import request");
+ dbg("usbip_net_recv failed: import request");
return -1;
}
PACK_OP_IMPORT_REQUEST(0, &req);
@@ -102,7 +102,7 @@ static int recv_request_import(int sockfd)

if (found) {
/* should set TCP_NODELAY for usbip */
- usbip_set_nodelay(sockfd);
+ usbip_net_set_nodelay(sockfd);

/* export device needs a TCP/IP socket descriptor */
rc = usbip_host_export_device(edev, sockfd);
@@ -113,11 +113,10 @@ static int recv_request_import(int sockfd)
error = 1;
}

-
- rc = usbip_send_op_common(sockfd, OP_REP_IMPORT,
- (!error ? ST_OK : ST_NA));
+ rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
+ (!error ? ST_OK : ST_NA));
if (rc < 0) {
- dbg("usbip_send_op_common failed: %#0x", OP_REP_IMPORT);
+ dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
return -1;
}

@@ -127,11 +126,11 @@ static int recv_request_import(int sockfd)
}

memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
- pack_usb_device(1, &pdu_udev);
+ usbip_net_pack_usb_device(1, &pdu_udev);

- rc = usbip_send(sockfd, &pdu_udev, sizeof(pdu_udev));
+ rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
if (rc < 0) {
- dbg("usbip_send failed: devinfo");
+ dbg("usbip_net_send failed: devinfo");
return -1;
}

@@ -157,16 +156,16 @@ static int send_reply_devlist(int connfd)
}
info("exportable devices: %d", reply.ndev);

- rc = usbip_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
+ rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
if (rc < 0) {
- dbg("usbip_send_op_common failed: %#0x", OP_REP_DEVLIST);
+ dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
return -1;
}
PACK_OP_DEVLIST_REPLY(1, &reply);

- rc = usbip_send(connfd, &reply, sizeof(reply));
+ rc = usbip_net_send(connfd, &reply, sizeof(reply));
if (rc < 0) {
- dbg("usbip_send failed: %#0x", OP_REP_DEVLIST);
+ dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
return -1;
}

@@ -174,22 +173,23 @@ static int send_reply_devlist(int connfd)
struct usbip_exported_device) {
dump_usb_device(&edev->udev);
memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
- pack_usb_device(1, &pdu_udev);
+ usbip_net_pack_usb_device(1, &pdu_udev);

- rc = usbip_send(connfd, &pdu_udev, sizeof(pdu_udev));
+ rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
if (rc < 0) {
- dbg("usbip_send failed: pdu_udev");
+ dbg("usbip_net_send failed: pdu_udev");
return -1;
}

for (i = 0; i < edev->udev.bNumInterfaces; i++) {
dump_usb_interface(&edev->uinf[i]);
memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
- pack_usb_interface(1, &pdu_uinf);
+ usbip_net_pack_usb_interface(1, &pdu_uinf);

- rc = usbip_send(connfd, &pdu_uinf, sizeof(pdu_uinf));
+ rc = usbip_net_send(connfd, &pdu_uinf,
+ sizeof(pdu_uinf));
if (rc < 0) {
- dbg("usbip_send failed: pdu_uinf");
+ dbg("usbip_net_send failed: pdu_uinf");
return -1;
}
}
@@ -205,9 +205,9 @@ static int recv_request_devlist(int connfd)

memset(&req, 0, sizeof(req));

- rc = usbip_recv(connfd, &req, sizeof(req));
+ rc = usbip_net_recv(connfd, &req, sizeof(req));
if (rc < 0) {
- dbg("usbip_recv failed: devlist request");
+ dbg("usbip_net_recv failed: devlist request");
return -1;
}

@@ -225,7 +225,7 @@ static int recv_pdu(int connfd)
uint16_t code = OP_UNSPEC;
int ret;

- ret = usbip_recv_op_common(connfd, &code);
+ ret = usbip_net_recv_op_common(connfd, &code);
if (ret < 0) {
dbg("could not receive opcode: %#0x", code);
return -1;
@@ -361,8 +361,8 @@ static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
if (sockfdlist[nsockfd] < 0)
continue;

- usbip_set_reuseaddr(sockfdlist[nsockfd]);
- usbip_set_nodelay(sockfdlist[nsockfd]);
+ usbip_net_set_reuseaddr(sockfdlist[nsockfd]);
+ usbip_net_set_nodelay(sockfdlist[nsockfd]);

if (sockfdlist[nsockfd] >= FD_SETSIZE) {
close(sockfdlist[nsockfd]);
--
1.7.5.2

2011-06-20 05:52:37

by matt mooney

[permalink] [raw]
Subject: [PATCH 21/22] staging: usbip: userspace: fix header installation bug

A bug that I created due to using simply expanding variables in the
makefiles. Although only unexpanded paths are at issue here, I decided
to use recursively expanding variables on all of the parameterized
values.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/Makefile.am | 2 +-
drivers/staging/usbip/userspace/libsrc/Makefile.am | 6 +++---
drivers/staging/usbip/userspace/src/Makefile.am | 6 +++---
3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/usbip/userspace/Makefile.am b/drivers/staging/usbip/userspace/Makefile.am
index fbdeef3..9ab1949 100644
--- a/drivers/staging/usbip/userspace/Makefile.am
+++ b/drivers/staging/usbip/userspace/Makefile.am
@@ -1,5 +1,5 @@
SUBDIRS := libsrc src
-includedir := @includedir@/usbip
+includedir = @includedir@/usbip
include_HEADERS := $(addprefix libsrc/, \
usbip_common.h vhci_driver.h usbip_host_driver.h)

diff --git a/drivers/staging/usbip/userspace/libsrc/Makefile.am b/drivers/staging/usbip/userspace/libsrc/Makefile.am
index 9b663a4..4921189 100644
--- a/drivers/staging/usbip/userspace/libsrc/Makefile.am
+++ b/drivers/staging/usbip/userspace/libsrc/Makefile.am
@@ -1,6 +1,6 @@
-libusbip_la_CPPFLAGS := -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
-libusbip_la_CFLAGS := @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+libusbip_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
+libusbip_la_CFLAGS = @EXTRA_CFLAGS@
+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 \
diff --git a/drivers/staging/usbip/userspace/src/Makefile.am b/drivers/staging/usbip/userspace/src/Makefile.am
index 52741c8..3f09f6a 100644
--- a/drivers/staging/usbip/userspace/src/Makefile.am
+++ b/drivers/staging/usbip/userspace/src/Makefile.am
@@ -1,6 +1,6 @@
-AM_CPPFLAGS := -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
-AM_CFLAGS := @EXTRA_CFLAGS@ @PACKAGE_CFLAGS@
-LDADD := $(top_srcdir)/libsrc/libusbip.la @PACKAGE_LIBS@
+AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
+AM_CFLAGS = @EXTRA_CFLAGS@ @PACKAGE_CFLAGS@
+LDADD = $(top_builddir)/libsrc/libusbip.la @PACKAGE_LIBS@

sbin_PROGRAMS := usbip usbipd

--
1.7.5.2

2011-06-20 05:45:44

by matt mooney

[permalink] [raw]
Subject: [PATCH 22/22] staging: usbip: userspace: configure.ac: change package data

Change package name to usbip-utils, email address to linux-usb, and
bump minor version number.

Signed-off-by: matt mooney <[email protected]>
---
drivers/staging/usbip/userspace/configure.ac | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/staging/usbip/userspace/configure.ac b/drivers/staging/usbip/userspace/configure.ac
index 39e4a47..89fefd5 100644
--- a/drivers/staging/usbip/userspace/configure.ac
+++ b/drivers/staging/usbip/userspace/configure.ac
@@ -1,7 +1,7 @@
dnl Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
-AC_INIT([usbip], [1.0.0], [[email protected]])
+AC_INIT([usbip-utils], [1.1.0], [[email protected]])
AC_DEFINE([USBIP_VERSION], [0x00000100], [binary-coded decimal version number])

CURRENT=0
--
1.7.5.2