adb: Use new libusbhost library for USB support on Linux host.

Change-Id: I341e3be0a3eed48b6c5b6559fdbd0e956e2d8fb9
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/adb/Android.mk b/adb/Android.mk
index 0c93c1e..6fea574 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -81,6 +81,9 @@
 ifeq ($(USE_SYSDEPS_WIN32),)
 	LOCAL_STATIC_LIBRARIES += libcutils
 endif
+ifeq ($(HOST_OS),linux)
+  LOCAL_STATIC_LIBRARIES += libusbhost
+endif
 
 include $(BUILD_HOST_EXECUTABLE)
 
diff --git a/adb/usb_linux.c b/adb/usb_linux.c
index bb86813..eb10118 100644
--- a/adb/usb_linux.c
+++ b/adb/usb_linux.c
@@ -19,14 +19,7 @@
 #include <unistd.h>
 #include <string.h>
 
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <ctype.h>
-
+#include <usbhost/usbhost.h>
 #include <linux/usbdevice_fs.h>
 #include <linux/version.h>
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
@@ -34,7 +27,6 @@
 #else
 #include <linux/usb_ch9.h>
 #endif
-#include <asm/byteorder.h>
 
 #include "sysdeps.h"
 
@@ -52,28 +44,19 @@
     usb_handle *prev;
     usb_handle *next;
 
-    char fname[64];
-    int desc;
-    unsigned char ep_in;
-    unsigned char ep_out;
+    struct usb_device *device;
+    struct usb_endpoint *ep_in;
+    struct usb_endpoint *ep_out;
 
-    unsigned zero_mask;
-    unsigned writeable;
-
-    struct usbdevfs_urb urb_in;
-    struct usbdevfs_urb urb_out;
-
-    int urb_in_busy;
-    int urb_out_busy;
-    int dead;
-
-    adb_cond_t notify;
+    adb_cond_t notify_in;
+    adb_cond_t notify_out;
     adb_mutex_t lock;
 
-    // for garbage collecting disconnected devices
-    int mark;
+    int read_result, write_result;
+    int zero_mask;
+    int dead;
 
-    // ID of thread currently in REAPURB
+    // Thread ID for our reaper thread
     pthread_t reaper_thread;
 };
 
@@ -87,10 +70,8 @@
     usb_handle *usb;
 
     adb_mutex_lock(&usb_lock);
-    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
-        if(!strcmp(usb->fname, dev_name)) {
-            // set mark flag to indicate this device is still alive
-            usb->mark = 1;
+    for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+        if (!strcmp(usb_device_get_name(usb->device), dev_name)) {
             adb_mutex_unlock(&usb_lock);
             return 1;
         }
@@ -99,185 +80,204 @@
     return 0;
 }
 
-static void kick_disconnected_devices()
+static void kick_disconnected_device(const char *devname)
 {
     usb_handle *usb;
 
     adb_mutex_lock(&usb_lock);
-    // kick any devices in the device list that were not found in the device scan
-    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
-        if (usb->mark == 0) {
+    /* kick the device if it is in our list */
+    for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+        if (!strcmp(devname, usb_device_get_name(usb->device)))
             usb_kick(usb);
-        } else {
-            usb->mark = 0;
-        }
     }
     adb_mutex_unlock(&usb_lock);
 
 }
 
-static void register_device(const char *dev_name, unsigned char ep_in, unsigned char ep_out,
-                            int ifc, int serial_index, unsigned zero_mask);
-
-static inline int badname(const char *name)
+static void* reaper_thread(void* arg)
 {
-    while(*name) {
-        if(!isdigit(*name++)) return 1;
+    struct usb_handle* h = (struct usb_handle *)arg;
+    int ep_in = usb_endpoint_number(h->ep_in);
+    int ep_out = usb_endpoint_number(h->ep_out);
+    int reaped_ep, res;
+
+    while (1) {
+        D("[ reap urb - wait ]\n");
+        adb_mutex_unlock(&h->lock);
+        res = usb_endpoint_wait(h->device, &reaped_ep);
+        adb_mutex_lock(&h->lock);
+        if(h->dead) {
+            res = -1;
+            break;
+        }
+
+        D("[ reaped ep %d ret = %d ]\n", reaped_ep, res);
+
+        if (reaped_ep == ep_in) {
+            D("[ reap urb - IN complete ]\n");
+            h->read_result = res;
+            adb_cond_broadcast(&h->notify_in);
+        }
+        if (reaped_ep == ep_out) {
+            D("[ reap urb - OUT compelete ]\n");
+            h->write_result = res;
+            adb_cond_broadcast(&h->notify_out);
+        }
     }
-    return 0;
+
+    return NULL;
 }
 
-static void find_usb_device(const char *base,
-        void (*register_device_callback)
-                (const char *, unsigned char, unsigned char, int, int, unsigned))
+static void register_device(struct usb_device *device, int interface,
+        struct usb_endpoint *ep_in, struct usb_endpoint *ep_out)
 {
-    char busname[32], devname[32];
-    unsigned char local_ep_in, local_ep_out;
-    DIR *busdir , *devdir ;
-    struct dirent *de;
-    int fd ;
+    usb_handle* usb = 0;
+    int ret = 0;
+    int writeable;
+    char *serial;
+    pthread_attr_t   attr;
+    const char* dev_name = usb_device_get_name(device);
 
-    busdir = opendir(base);
-    if(busdir == 0) return;
+        /* Since Linux will not reassign the device ID (and dev_name)
+        ** as long as the device is open, we can add to the list here
+        ** once we open it and remove from the list when we're finally
+        ** closed and everything will work out fine.
+        **
+        ** If we have a usb_handle on the list 'o handles with a matching
+        ** name, we have no further work to do.
+        */
+    adb_mutex_lock(&usb_lock);
+    for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+        if (!strcmp(usb_device_get_name(usb->device), dev_name)) {
+            adb_mutex_unlock(&usb_lock);
+            return;
+        }
+    }
+    adb_mutex_unlock(&usb_lock);
 
-    while((de = readdir(busdir)) != 0) {
-        if(badname(de->d_name)) continue;
+    usb = calloc(1, sizeof(usb_handle));
+    adb_cond_init(&usb->notify_in, 0);
+    adb_cond_init(&usb->notify_out, 0);
+    adb_mutex_init(&usb->lock, 0);
 
-        snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
-        devdir = opendir(busname);
-        if(devdir == 0) continue;
+    usb->device = device;
+    usb->ep_in = ep_in;
+    usb->ep_out = ep_out;
+    usb->zero_mask = usb_endpoint_max_packet(usb->ep_out) - 1;
 
-//        DBGX("[ scanning %s ]\n", busname);
-        while((de = readdir(devdir))) {
-            unsigned char devdesc[256];
-            unsigned char* bufptr = devdesc;
-            unsigned char* bufend;
-            struct usb_device_descriptor* device;
-            struct usb_config_descriptor* config;
-            struct usb_interface_descriptor* interface;
-            struct usb_endpoint_descriptor *ep1, *ep2;
-            unsigned zero_mask = 0;
-            unsigned vid, pid;
-            size_t desclength;
+    D("[ usb open %s ]\n", dev_name);
+    writeable = usb_device_is_writeable(device);
+    if (writeable) {
+        ret = usb_device_claim_interface(device, interface);
+        if(ret != 0) goto fail;
+    }
 
-            if(badname(de->d_name)) continue;
-            snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
+        /* add to the end of the active handles */
+    adb_mutex_lock(&usb_lock);
+    usb->next = &handle_list;
+    usb->prev = handle_list.prev;
+    usb->prev->next = usb;
+    usb->next->prev = usb;
+    adb_mutex_unlock(&usb_lock);
 
-            if(known_device(devname)) {
-                DBGX("skipping %s\n", devname);
-                continue;
-            }
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+    pthread_create(&usb->reaper_thread, &attr, reaper_thread, usb);
 
-//            DBGX("[ scanning %s ]\n", devname);
-            if((fd = unix_open(devname, O_RDONLY)) < 0) {
-                continue;
-            }
+    serial = usb_device_get_serial(device);
+    register_usb_transport(usb, serial, writeable);
+    if (serial)
+        free(serial);
+    return;
 
-            desclength = adb_read(fd, devdesc, sizeof(devdesc));
-            bufend = bufptr + desclength;
+fail:
+    D("[ usb open %s error=%d, err_str = %s]\n",
+        dev_name,  errno, strerror(errno));
+    if (usb->ep_in)
+        usb_endpoint_close(usb->ep_in);
+    if (usb->ep_out)
+        usb_endpoint_close(usb->ep_out);
+    if(device) {
+        usb_device_close(device);
+    }
+    free(usb);
+}
 
-                // should have device and configuration descriptors, and atleast two endpoints
-            if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
-                D("desclength %d is too small\n", desclength);
-                adb_close(fd);
-                continue;
-            }
+static void check_usb_device(const char *devname) {
+    struct usb_device *device;
+    struct usb_descriptor_iter iter;
+    struct usb_descriptor_header* header;
+    struct usb_interface_descriptor* interface;
+    struct usb_endpoint_descriptor *ep1, *ep2;
+    struct usb_endpoint *ep_in = NULL, *ep_out = NULL;
+    uint16_t vid, pid;
 
-            device = (struct usb_device_descriptor*)bufptr;
-            bufptr += USB_DT_DEVICE_SIZE;
+    if(known_device(devname)) {
+        DBGX("skipping %s\n", devname);
+        return;
+    }
 
-            if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
-                adb_close(fd);
-                continue;
-            }
+    device = usb_device_open(devname);
+    if (!device) return;
 
-            vid = __le16_to_cpu(device->idVendor);
-            pid = __le16_to_cpu(device->idProduct);
-            pid = devdesc[10] | (devdesc[11] << 8);
-            DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
+    vid = usb_device_get_vendor_id(device);
+    pid = usb_device_get_product_id(device);
+    DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
 
-                // should have config descriptor next
-            config = (struct usb_config_descriptor *)bufptr;
-            bufptr += USB_DT_CONFIG_SIZE;
-            if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
-                D("usb_config_descriptor not found\n");
-                adb_close(fd);
-                continue;
-            }
+    // loop through all the descriptors and look for the ADB interface
+    usb_descriptor_iter_init(device, &iter);
 
-                // loop through all the descriptors and look for the ADB interface
-            while (bufptr < bufend) {
-                unsigned char length = bufptr[0];
-                unsigned char type = bufptr[1];
+    while ((header = usb_descriptor_iter_next(&iter)) != NULL) {
+        if (header->bDescriptorType == USB_DT_INTERFACE) {
+            interface = (struct usb_interface_descriptor *)header;
 
-                if (type == USB_DT_INTERFACE) {
-                    interface = (struct usb_interface_descriptor *)bufptr;
-                    bufptr += length;
+            DBGX("bInterfaceClass: %d,  bInterfaceSubClass: %d,"
+                 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
+                 interface->bInterfaceClass, interface->bInterfaceSubClass,
+                 interface->bInterfaceProtocol, interface->bNumEndpoints);
 
-                    if (length != USB_DT_INTERFACE_SIZE) {
-                        D("interface descriptor has wrong size\n");
-                        break;
-                    }
+            if (interface->bNumEndpoints == 2 &&
+                    is_adb_interface(vid, pid, interface->bInterfaceClass,
+                    interface->bInterfaceSubClass, interface->bInterfaceProtocol))  {
 
-                    DBGX("bInterfaceClass: %d,  bInterfaceSubClass: %d,"
-                         "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
-                         interface->bInterfaceClass, interface->bInterfaceSubClass,
-                         interface->bInterfaceProtocol, interface->bNumEndpoints);
+                DBGX("looking for bulk endpoints\n");
+                    // looks like ADB...
+                ep1 = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
+                ep2 = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
 
-                    if (interface->bNumEndpoints == 2 &&
-                            is_adb_interface(vid, pid, interface->bInterfaceClass,
-                            interface->bInterfaceSubClass, interface->bInterfaceProtocol))  {
-
-                        DBGX("looking for bulk endpoints\n");
-                            // looks like ADB...
-                        ep1 = (struct usb_endpoint_descriptor *)bufptr;
-                        bufptr += USB_DT_ENDPOINT_SIZE;
-                        ep2 = (struct usb_endpoint_descriptor *)bufptr;
-                        bufptr += USB_DT_ENDPOINT_SIZE;
-
-                        if (bufptr > devdesc + desclength ||
-                            ep1->bLength != USB_DT_ENDPOINT_SIZE ||
-                            ep1->bDescriptorType != USB_DT_ENDPOINT ||
-                            ep2->bLength != USB_DT_ENDPOINT_SIZE ||
-                            ep2->bDescriptorType != USB_DT_ENDPOINT) {
-                            D("endpoints not found\n");
-                            break;
-                        }
-
-                            // both endpoints should be bulk
-                        if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
-                            ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
-                            D("bulk endpoints not found\n");
-                            continue;
-                        }
-                            /* aproto 01 needs 0 termination */
-                        if(interface->bInterfaceProtocol == 0x01) {
-                            zero_mask = ep1->wMaxPacketSize - 1;
-                        }
-
-                            // we have a match.  now we just need to figure out which is in and which is out.
-                        if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
-                            local_ep_in = ep1->bEndpointAddress;
-                            local_ep_out = ep2->bEndpointAddress;
-                        } else {
-                            local_ep_in = ep2->bEndpointAddress;
-                            local_ep_out = ep1->bEndpointAddress;
-                        }
-
-                        register_device_callback(devname, local_ep_in, local_ep_out,
-                                interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
-                        break;
-                    }
-                } else {
-                    bufptr += length;
+                if (!ep1 || !ep2 ||
+                    ep1->bDescriptorType != USB_DT_ENDPOINT ||
+                    ep2->bDescriptorType != USB_DT_ENDPOINT) {
+                    D("endpoints not found\n");
+                    continue;
                 }
-            } // end of while
 
-            adb_close(fd);
-        } // end of devdir while
-        closedir(devdir);
-    } //end of busdir while
-    closedir(busdir);
+                    // both endpoints should be bulk
+                if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
+                    ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
+                    D("bulk endpoints not found\n");
+                    continue;
+                }
+
+                    // we have a match.  now we just need to figure out which is in and which is out.
+                if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+                    ep_in = usb_endpoint_open(device, ep1);
+                    ep_out = usb_endpoint_open(device, ep2);
+                } else {
+                    ep_in = usb_endpoint_open(device, ep2);
+                    ep_out = usb_endpoint_open(device, ep1);
+                }
+
+                register_device(device, interface->bInterfaceNumber, ep_in, ep_out);
+                // so we don't free it at the bottom
+                device = NULL;
+                break;
+            }
+        }
+    } // end of while
+
+    if (device)
+        usb_device_close(device);
 }
 
 void usb_cleanup()
@@ -286,17 +286,8 @@
 
 static int usb_bulk_write(usb_handle *h, const void *data, int len)
 {
-    struct usbdevfs_urb *urb = &h->urb_out;
+    struct usb_endpoint *ep = h->ep_out;
     int res;
-    struct timeval tv;
-    struct timespec ts;
-
-    memset(urb, 0, sizeof(*urb));
-    urb->type = USBDEVFS_URB_TYPE_BULK;
-    urb->endpoint = h->ep_out;
-    urb->status = -1;
-    urb->buffer = (void*) data;
-    urb->buffer_length = len;
 
     D("++ write ++\n");
 
@@ -305,32 +296,15 @@
         res = -1;
         goto fail;
     }
-    do {
-        res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
-    } while((res < 0) && (errno == EINTR));
-
+    res = usb_endpoint_queue(ep, (void *)data, len);
     if(res < 0) {
         goto fail;
     }
 
-    res = -1;
-    h->urb_out_busy = 1;
-    for(;;) {
-        /* time out after five seconds */
-        gettimeofday(&tv, NULL);
-        ts.tv_sec = tv.tv_sec + 5;
-        ts.tv_nsec = tv.tv_usec * 1000L;
-        res = pthread_cond_timedwait(&h->notify, &h->lock, &ts);
-        if(res < 0 || h->dead) {
-            break;
-        }
-        if(h->urb_out_busy == 0) {
-            if(urb->status == 0) {
-                res = urb->actual_length;
-            }
-            break;
-        }
-    }
+    res = pthread_cond_wait(&h->notify_out, &h->lock);
+    if (!res)
+        res = h->write_result;
+
 fail:
     adb_mutex_unlock(&h->lock);
     D("-- write --\n");
@@ -339,75 +313,27 @@
 
 static int usb_bulk_read(usb_handle *h, void *data, int len)
 {
-    struct usbdevfs_urb *urb = &h->urb_in;
-    struct usbdevfs_urb *out = NULL;
+    struct usb_endpoint *ep = h->ep_in;
     int res;
 
-    memset(urb, 0, sizeof(*urb));
-    urb->type = USBDEVFS_URB_TYPE_BULK;
-    urb->endpoint = h->ep_in;
-    urb->status = -1;
-    urb->buffer = data;
-    urb->buffer_length = len;
-
-
     adb_mutex_lock(&h->lock);
     if(h->dead) {
         res = -1;
         goto fail;
     }
-    do {
-        res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
-    } while((res < 0) && (errno == EINTR));
-
-    if(res < 0) {
+    res = usb_endpoint_queue(ep, data, len);
+    if (res < 0) {
         goto fail;
     }
+    res = pthread_cond_wait(&h->notify_in, &h->lock);
+    if (!res)
+        res = h->read_result;
 
-    h->urb_in_busy = 1;
-    for(;;) {
-        D("[ reap urb - wait ]\n");
-        h->reaper_thread = pthread_self();
-        adb_mutex_unlock(&h->lock);
-        res = ioctl(h->desc, USBDEVFS_REAPURB, &out);
-        adb_mutex_lock(&h->lock);
-        h->reaper_thread = 0;
-        if(h->dead) {
-            res = -1;
-            break;
-        }
-        if(res < 0) {
-            if(errno == EINTR) {
-                continue;
-            }
-            D("[ reap urb - error ]\n");
-            break;
-        }
-        D("[ urb @%p status = %d, actual = %d ]\n",
-            out, out->status, out->actual_length);
-
-        if(out == &h->urb_in) {
-            D("[ reap urb - IN complete ]\n");
-            h->urb_in_busy = 0;
-            if(urb->status == 0) {
-                res = urb->actual_length;
-            } else {
-                res = -1;
-            }
-            break;
-        }
-        if(out == &h->urb_out) {
-            D("[ reap urb - OUT compelete ]\n");
-            h->urb_out_busy = 0;
-            adb_cond_broadcast(&h->notify);
-        }
-    }
 fail:
     adb_mutex_unlock(&h->lock);
     return res;
 }
 
-
 int usb_write(usb_handle *h, const void *_data, int len)
 {
     unsigned char *data = (unsigned char*) _data;
@@ -438,7 +364,7 @@
         data += xfer;
     }
 
-    if(need_zero){
+    if(need_zero) {
         n = usb_bulk_write(h, _data, 0);
         return n;
     }
@@ -455,11 +381,9 @@
     while(len > 0) {
         int xfer = (len > 4096) ? 4096 : len;
 
-        D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
         n = usb_bulk_read(h, data, xfer);
-        D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname);
         if(n != xfer) {
-            if((errno == ETIMEDOUT) && (h->desc != -1)) {
+            if(errno == ETIMEDOUT && h->device) {
                 D("[ timeout ]\n");
                 if(n > 0){
                     data += n;
@@ -482,12 +406,12 @@
 
 void usb_kick(usb_handle *h)
 {
-    D("[ kicking %p (fd = %d) ]\n", h, h->desc);
+    D("[ kicking %p (fd = %s) ]\n", h, usb_device_get_name(h->device));
     adb_mutex_lock(&h->lock);
     if(h->dead == 0) {
         h->dead = 1;
 
-        if (h->writeable) {
+        if (usb_device_is_writeable(h->device)) {
             /* HACK ALERT!
             ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
             ** This is a workaround for that problem.
@@ -501,13 +425,10 @@
             ** but this ensures that a reader blocked on REAPURB
             ** will get unblocked
             */
-            ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in);
-            ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out);
-            h->urb_in.status = -ENODEV;
-            h->urb_out.status = -ENODEV;
-            h->urb_in_busy = 0;
-            h->urb_out_busy = 0;
-            adb_cond_broadcast(&h->notify);
+            usb_endpoint_cancel(h->ep_in);
+            usb_endpoint_cancel(h->ep_out);
+            adb_cond_broadcast(&h->notify_in);
+            adb_cond_broadcast(&h->notify_out);
         } else {
             unregister_usb_transport(h);
         }
@@ -524,148 +445,14 @@
     h->prev = 0;
     h->next = 0;
 
-    adb_close(h->desc);
-    D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
+    usb_device_close(h->device);
+    D("[ usb closed %p ]\n", h);
     adb_mutex_unlock(&usb_lock);
 
     free(h);
     return 0;
 }
 
-static void register_device(const char *dev_name,
-                            unsigned char ep_in, unsigned char ep_out,
-                            int interface, int serial_index, unsigned zero_mask)
-{
-    usb_handle* usb = 0;
-    int n = 0;
-    char serial[256];
-
-        /* Since Linux will not reassign the device ID (and dev_name)
-        ** as long as the device is open, we can add to the list here
-        ** once we open it and remove from the list when we're finally
-        ** closed and everything will work out fine.
-        **
-        ** If we have a usb_handle on the list 'o handles with a matching
-        ** name, we have no further work to do.
-        */
-    adb_mutex_lock(&usb_lock);
-    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
-        if(!strcmp(usb->fname, dev_name)) {
-            adb_mutex_unlock(&usb_lock);
-            return;
-        }
-    }
-    adb_mutex_unlock(&usb_lock);
-
-    D("[ usb located new device %s (%d/%d/%d) ]\n",
-        dev_name, ep_in, ep_out, interface);
-    usb = calloc(1, sizeof(usb_handle));
-    strcpy(usb->fname, dev_name);
-    usb->ep_in = ep_in;
-    usb->ep_out = ep_out;
-    usb->zero_mask = zero_mask;
-    usb->writeable = 1;
-
-    adb_cond_init(&usb->notify, 0);
-    adb_mutex_init(&usb->lock, 0);
-    /* initialize mark to 1 so we don't get garbage collected after the device scan */
-    usb->mark = 1;
-    usb->reaper_thread = 0;
-
-    usb->desc = unix_open(usb->fname, O_RDWR);
-    if(usb->desc < 0) {
-        /* if we fail, see if have read-only access */
-        usb->desc = unix_open(usb->fname, O_RDONLY);
-        if(usb->desc < 0) goto fail;
-        usb->writeable = 0;
-        D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
-    } else {
-        D("[ usb open %s fd = %d]\n", usb->fname, usb->desc);
-        n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface);
-        if(n != 0) goto fail;
-    }
-
-        /* read the device's serial number */
-    serial[0] = 0;
-    memset(serial, 0, sizeof(serial));
-    if (serial_index) {
-        struct usbdevfs_ctrltransfer  ctrl;
-        __u16 buffer[128];
-        __u16 languages[128];
-        int i, result;
-        int languageCount = 0;
-
-        memset(languages, 0, sizeof(languages));
-        memset(&ctrl, 0, sizeof(ctrl));
-
-            // read list of supported languages
-        ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
-        ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
-        ctrl.wValue = (USB_DT_STRING << 8) | 0;
-        ctrl.wIndex = 0;
-        ctrl.wLength = sizeof(languages);
-        ctrl.data = languages;
-
-        result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
-        if (result > 0)
-            languageCount = (result - 2) / 2;
-
-        for (i = 1; i <= languageCount; i++) {
-            memset(buffer, 0, sizeof(buffer));
-            memset(&ctrl, 0, sizeof(ctrl));
-
-            ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
-            ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
-            ctrl.wValue = (USB_DT_STRING << 8) | serial_index;
-            ctrl.wIndex = languages[i];
-            ctrl.wLength = sizeof(buffer);
-            ctrl.data = buffer;
-
-            result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
-            if (result > 0) {
-                int i;
-                // skip first word, and copy the rest to the serial string, changing shorts to bytes.
-                result /= 2;
-                for (i = 1; i < result; i++)
-                    serial[i - 1] = buffer[i];
-                serial[i - 1] = 0;
-                break;
-            }
-        }
-    }
-
-        /* add to the end of the active handles */
-    adb_mutex_lock(&usb_lock);
-    usb->next = &handle_list;
-    usb->prev = handle_list.prev;
-    usb->prev->next = usb;
-    usb->next->prev = usb;
-    adb_mutex_unlock(&usb_lock);
-
-    register_usb_transport(usb, serial, usb->writeable);
-    return;
-
-fail:
-    D("[ usb open %s error=%d, err_str = %s]\n",
-        usb->fname,  errno, strerror(errno));
-    if(usb->desc >= 0) {
-        adb_close(usb->desc);
-    }
-    free(usb);
-}
-
-void* device_poll_thread(void* unused)
-{
-    D("Created device thread\n");
-    for(;;) {
-            /* XXX use inotify */
-        find_usb_device("/dev/bus/usb", register_device);
-        kick_disconnected_devices();
-        sleep(1);
-    }
-    return NULL;
-}
-
 static void sigalrm_handler(int signo)
 {
     // don't need to do anything here
@@ -673,17 +460,15 @@
 
 void usb_init()
 {
-    adb_thread_t tid;
     struct sigaction    actions;
 
+    if (usb_host_init(check_usb_device, kick_disconnected_device))
+        fatal_errno("usb_host_init failed\n");
+
     memset(&actions, 0, sizeof(actions));
     sigemptyset(&actions.sa_mask);
     actions.sa_flags = 0;
     actions.sa_handler = sigalrm_handler;
     sigaction(SIGALRM,& actions, NULL);
-
-    if(adb_thread_create(&tid, device_poll_thread, NULL)){
-        fatal_errno("cannot create input thread");
-    }
 }