1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/ioctl.h>
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/types.h>
24fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood#include <sys/time.h>
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <dirent.h>
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <ctype.h>
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/usbdevice_fs.h>
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/version.h>
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/usb/ch9.h>
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/usb_ch9.h>
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <asm/byteorder.h>
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "sysdeps.h"
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define   TRACE_TAG  TRACE_USB
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "adb.h"
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* usb scan debugging is waaaay too verbose */
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define DBGX(x...)
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
48408fa57864c01113deaa213e5c1848a9c594ae92JP AbgrallADB_MUTEX_DEFINE( usb_lock );
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstruct usb_handle
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_handle *prev;
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_handle *next;
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char fname[64];
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int desc;
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char ep_in;
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char ep_out;
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned zero_mask;
610927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    unsigned writeable;
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct usbdevfs_urb urb_in;
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct usbdevfs_urb urb_out;
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int urb_in_busy;
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int urb_out_busy;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int dead;
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_cond_t notify;
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_t lock;
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // for garbage collecting disconnected devices
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int mark;
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // ID of thread currently in REAPURB
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_t reaper_thread;
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project};
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic usb_handle handle_list = {
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    .prev = &handle_list,
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    .next = &handle_list,
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project};
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int known_device(const char *dev_name)
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_handle *usb;
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&usb_lock);
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!strcmp(usb->fname, dev_name)) {
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            // set mark flag to indicate this device is still alive
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            usb->mark = 1;
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            adb_mutex_unlock(&usb_lock);
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&usb_lock);
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void kick_disconnected_devices()
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_handle *usb;
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
106dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&usb_lock);
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // kick any devices in the device list that were not found in the device scan
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (usb->mark == 0) {
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            usb_kick(usb);
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            usb->mark = 0;
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&usb_lock);
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
119e109d266c12c5f537d429ca4b892f2719e02c2daScott Andersonstatic void register_device(const char *dev_name, const char *devpath,
120e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            unsigned char ep_in, unsigned char ep_out,
1210927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                            int ifc, int serial_index, unsigned zero_mask);
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic inline int badname(const char *name)
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(*name) {
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!isdigit(*name++)) return 1;
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1310927bf9690127bc45cf8837a1467759e9720399aMike Lockwoodstatic void find_usb_device(const char *base,
1320927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        void (*register_device_callback)
133e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                (const char *, const char *, unsigned char, unsigned char, int, int, unsigned))
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char busname[32], devname[32];
136dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char local_ep_in, local_ep_out;
137dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    DIR *busdir , *devdir ;
138dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct dirent *de;
139dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd ;
140dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
141dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    busdir = opendir(base);
1420927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    if(busdir == 0) return;
143dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
144dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while((de = readdir(busdir)) != 0) {
145dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(badname(de->d_name)) continue;
146dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
147dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
148dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        devdir = opendir(busname);
149dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(devdir == 0) continue;
150dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
151dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project//        DBGX("[ scanning %s ]\n", busname);
152dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        while((de = readdir(devdir))) {
153b596608f098937049fd98c5896cf72ebbf3584aeMike Lockwood            unsigned char devdesc[4096];
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            unsigned char* bufptr = devdesc;
15507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood            unsigned char* bufend;
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            struct usb_device_descriptor* device;
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            struct usb_config_descriptor* config;
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            struct usb_interface_descriptor* interface;
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            struct usb_endpoint_descriptor *ep1, *ep2;
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            unsigned zero_mask = 0;
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            unsigned vid, pid;
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            size_t desclength;
163dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
164dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(badname(de->d_name)) continue;
165dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
166dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
167dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(known_device(devname)) {
168dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                DBGX("skipping %s\n", devname);
169dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
170dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
171dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
172dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project//            DBGX("[ scanning %s ]\n", devname);
1730927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            if((fd = unix_open(devname, O_RDONLY)) < 0) {
174dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            desclength = adb_read(fd, devdesc, sizeof(devdesc));
17807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood            bufend = bufptr + desclength;
179dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
180dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                // should have device and configuration descriptors, and atleast two endpoints
181dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                D("desclength %d is too small\n", desclength);
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                adb_close(fd);
184dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
185dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
186dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
187dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            device = (struct usb_device_descriptor*)bufptr;
188dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            bufptr += USB_DT_DEVICE_SIZE;
189dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
190dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
191dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                adb_close(fd);
192dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
193dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
194dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1956f703a2e85a3098b3546a0ce254349b72b440628Marcus Comstedt            vid = device->idVendor;
1966f703a2e85a3098b3546a0ce254349b72b440628Marcus Comstedt            pid = device->idProduct;
197dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                // should have config descriptor next
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            config = (struct usb_config_descriptor *)bufptr;
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            bufptr += USB_DT_CONFIG_SIZE;
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                D("usb_config_descriptor not found\n");
204dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                adb_close(fd);
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
20807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                // loop through all the descriptors and look for the ADB interface
20907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood            while (bufptr < bufend) {
21007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                unsigned char length = bufptr[0];
21107e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                unsigned char type = bufptr[1];
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                if (type == USB_DT_INTERFACE) {
21407e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    interface = (struct usb_interface_descriptor *)bufptr;
21507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    bufptr += length;
216dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    if (length != USB_DT_INTERFACE_SIZE) {
21807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        D("interface descriptor has wrong size\n");
21907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        break;
220dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
22207e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    DBGX("bInterfaceClass: %d,  bInterfaceSubClass: %d,"
22307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                         "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
22407e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                         interface->bInterfaceClass, interface->bInterfaceSubClass,
22507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                         interface->bInterfaceProtocol, interface->bNumEndpoints);
22607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
22707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    if (interface->bNumEndpoints == 2 &&
22807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            is_adb_interface(vid, pid, interface->bInterfaceClass,
22907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            interface->bInterfaceSubClass, interface->bInterfaceProtocol))  {
23007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
231e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        struct stat st;
232e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        char pathbuf[128];
233e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        char link[256];
234e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        char *devpath = NULL;
235e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson
23607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        DBGX("looking for bulk endpoints\n");
23707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            // looks like ADB...
23807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        ep1 = (struct usb_endpoint_descriptor *)bufptr;
23907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        bufptr += USB_DT_ENDPOINT_SIZE;
24007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        ep2 = (struct usb_endpoint_descriptor *)bufptr;
24107e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        bufptr += USB_DT_ENDPOINT_SIZE;
24207e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
24307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        if (bufptr > devdesc + desclength ||
24407e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            ep1->bLength != USB_DT_ENDPOINT_SIZE ||
24507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            ep1->bDescriptorType != USB_DT_ENDPOINT ||
24607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            ep2->bLength != USB_DT_ENDPOINT_SIZE ||
24707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            ep2->bDescriptorType != USB_DT_ENDPOINT) {
24807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            D("endpoints not found\n");
24907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            break;
25007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        }
25107e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
25207e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            // both endpoints should be bulk
25307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
25407e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
25507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            D("bulk endpoints not found\n");
25607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            continue;
25707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        }
25807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            /* aproto 01 needs 0 termination */
25907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        if(interface->bInterfaceProtocol == 0x01) {
26007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            zero_mask = ep1->wMaxPacketSize - 1;
26107e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        }
26207e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
26307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            // we have a match.  now we just need to figure out which is in and which is out.
26407e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
26507e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            local_ep_in = ep1->bEndpointAddress;
26607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            local_ep_out = ep2->bEndpointAddress;
26707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        } else {
26807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            local_ep_in = ep2->bEndpointAddress;
26907e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                            local_ep_out = ep1->bEndpointAddress;
27007e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        }
27107e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood
272e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            // Determine the device path
273e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
274e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            char *slash;
275e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            ssize_t link_len;
276e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
277e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                     major(st.st_rdev), minor(st.st_rdev));
278e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            link_len = readlink(pathbuf, link, sizeof(link) - 1);
279e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            if (link_len > 0) {
280e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                link[link_len] = '\0';
281e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                slash = strrchr(link, '/');
282e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                if (slash) {
283e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                    snprintf(pathbuf, sizeof(pathbuf),
284e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                             "usb:%s", slash + 1);
285e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                    devpath = pathbuf;
286e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                }
287e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                            }
288e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        }
289e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson
290e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                        register_device_callback(devname, devpath,
291e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson                                local_ep_in, local_ep_out,
29207e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                                interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
29307e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                        break;
294dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
295dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                } else {
29607e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                    bufptr += length;
29707e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood                }
29807e8f7e8758d5ce44fe6cf3bacb670962750a92eMike Lockwood            } // end of while
299dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
300dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            adb_close(fd);
301dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } // end of devdir while
302dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        closedir(devdir);
303dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } //end of busdir while
304dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    closedir(busdir);
305dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
306dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
307dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid usb_cleanup()
308dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
309dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
310dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
311dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int usb_bulk_write(usb_handle *h, const void *data, int len)
312dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
313dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct usbdevfs_urb *urb = &h->urb_out;
314dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res;
315fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood    struct timeval tv;
316fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood    struct timespec ts;
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
318dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(urb, 0, sizeof(*urb));
319dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->type = USBDEVFS_URB_TYPE_BULK;
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->endpoint = h->ep_out;
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->status = -1;
322dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->buffer = (void*) data;
323dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->buffer_length = len;
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
325dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("++ write ++\n");
326dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
327dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&h->lock);
328dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(h->dead) {
329dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = -1;
330dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        goto fail;
331dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
332dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
333dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
334dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while((res < 0) && (errno == EINTR));
335dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
336dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(res < 0) {
337dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        goto fail;
338dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
339dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
340dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    res = -1;
341dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->urb_out_busy = 1;
342dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
343fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        /* time out after five seconds */
344fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        gettimeofday(&tv, NULL);
345fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        ts.tv_sec = tv.tv_sec + 5;
346fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        ts.tv_nsec = tv.tv_usec * 1000L;
347fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        res = pthread_cond_timedwait(&h->notify, &h->lock, &ts);
348fe582b5d32d08d075be87f4c9193e24029963361Mike Lockwood        if(res < 0 || h->dead) {
349dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
350dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
351dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(h->urb_out_busy == 0) {
352dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(urb->status == 0) {
353dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res = urb->actual_length;
354dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
355dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
356dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
357dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
358dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectfail:
359dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&h->lock);
360dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("-- write --\n");
361dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return res;
362dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
363dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
364dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int usb_bulk_read(usb_handle *h, void *data, int len)
365dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
366dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct usbdevfs_urb *urb = &h->urb_in;
367dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct usbdevfs_urb *out = NULL;
368dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res;
369dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
370dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(urb, 0, sizeof(*urb));
371dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->type = USBDEVFS_URB_TYPE_BULK;
372dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->endpoint = h->ep_in;
373dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->status = -1;
374dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->buffer = data;
375dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    urb->buffer_length = len;
376dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
377dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
378dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&h->lock);
379dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(h->dead) {
380dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = -1;
381dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        goto fail;
382dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
383dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
384dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
385dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while((res < 0) && (errno == EINTR));
386dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
387dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(res < 0) {
388dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        goto fail;
389dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
390dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
391dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->urb_in_busy = 1;
392dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
393dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("[ reap urb - wait ]\n");
394dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        h->reaper_thread = pthread_self();
395dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_mutex_unlock(&h->lock);
396dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = ioctl(h->desc, USBDEVFS_REAPURB, &out);
397408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        int saved_errno = errno;
398dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_mutex_lock(&h->lock);
399dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        h->reaper_thread = 0;
400dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(h->dead) {
401dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            res = -1;
402dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
403dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
404dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(res < 0) {
405408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall            if(saved_errno == EINTR) {
406dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
407dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
408dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            D("[ reap urb - error ]\n");
409dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
410dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
411dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("[ urb @%p status = %d, actual = %d ]\n",
412dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            out, out->status, out->actual_length);
413dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
414dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(out == &h->urb_in) {
415dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            D("[ reap urb - IN complete ]\n");
416dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            h->urb_in_busy = 0;
417dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(urb->status == 0) {
418dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res = urb->actual_length;
419dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            } else {
420dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res = -1;
421dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
422dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
423dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
424dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(out == &h->urb_out) {
425dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            D("[ reap urb - OUT compelete ]\n");
426dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            h->urb_out_busy = 0;
427dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            adb_cond_broadcast(&h->notify);
428dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
429dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
430dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectfail:
431dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&h->lock);
432dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return res;
433dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
434dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
435dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
436dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint usb_write(usb_handle *h, const void *_data, int len)
437dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
438dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char *data = (unsigned char*) _data;
439dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n;
440dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int need_zero = 0;
441dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
442dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(h->zero_mask) {
443dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /* if we need 0-markers and our transfer
444dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ** is an even multiple of the packet size,
445dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ** we make note of it
446dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            */
447dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!(len & h->zero_mask)) {
448dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            need_zero = 1;
449dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
450dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
451dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
452dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(len > 0) {
453dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int xfer = (len > 4096) ? 4096 : len;
454dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
455dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        n = usb_bulk_write(h, data, xfer);
456dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(n != xfer) {
457dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            D("ERROR: n = %d, errno = %d (%s)\n",
458dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                n, errno, strerror(errno));
459dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return -1;
460dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
461dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
462dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len -= xfer;
463dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        data += xfer;
464dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
465dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
466dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(need_zero){
467dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        n = usb_bulk_write(h, _data, 0);
468dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return n;
469dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
470dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
471dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
472dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
473dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
474dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint usb_read(usb_handle *h, void *_data, int len)
475dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
476dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char *data = (unsigned char*) _data;
477dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n;
478dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
479dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("++ usb_read ++\n");
480dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(len > 0) {
481dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int xfer = (len > 4096) ? 4096 : len;
482dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
483dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
484dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        n = usb_bulk_read(h, data, xfer);
485dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname);
486dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(n != xfer) {
487dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if((errno == ETIMEDOUT) && (h->desc != -1)) {
488dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                D("[ timeout ]\n");
489dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if(n > 0){
490dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    data += n;
491dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    len -= n;
492dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                }
493dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
494dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
495dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            D("ERROR: n = %d, errno = %d (%s)\n",
496dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                n, errno, strerror(errno));
497dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return -1;
498dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
499dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
500dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len -= xfer;
501dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        data += xfer;
502dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
503dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
504dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("-- usb_read --\n");
505dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
506dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
507dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
508dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid usb_kick(usb_handle *h)
509dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
510dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("[ kicking %p (fd = %d) ]\n", h, h->desc);
511dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&h->lock);
512dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(h->dead == 0) {
513dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        h->dead = 1;
514dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
5150927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        if (h->writeable) {
5160927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            /* HACK ALERT!
5170927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
5180927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ** This is a workaround for that problem.
5190927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            */
5200927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            if (h->reaper_thread) {
5210927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                pthread_kill(h->reaper_thread, SIGALRM);
5220927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            }
523dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
5240927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            /* cancel any pending transactions
5250927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ** these will quietly fail if the txns are not active,
5260927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ** but this ensures that a reader blocked on REAPURB
5270927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ** will get unblocked
5280927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            */
5290927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in);
5300927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out);
5310927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            h->urb_in.status = -ENODEV;
5320927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            h->urb_out.status = -ENODEV;
5330927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            h->urb_in_busy = 0;
5340927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            h->urb_out_busy = 0;
5350927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            adb_cond_broadcast(&h->notify);
5360927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        } else {
5370927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            unregister_usb_transport(h);
5380927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        }
539dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
540dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&h->lock);
541dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
542dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
543dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint usb_close(usb_handle *h)
544dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
545dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("[ usb close ... ]\n");
546dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&usb_lock);
547dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->next->prev = h->prev;
548dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->prev->next = h->next;
549dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->prev = 0;
550dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    h->next = 0;
551dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
552dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_close(h->desc);
553dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
554dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&usb_lock);
555dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
556dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(h);
557dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
558dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
559dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
560e109d266c12c5f537d429ca4b892f2719e02c2daScott Andersonstatic void register_device(const char *dev_name, const char *devpath,
561dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                            unsigned char ep_in, unsigned char ep_out,
5620927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                            int interface, int serial_index, unsigned zero_mask)
563dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
564dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_handle* usb = 0;
565dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n = 0;
5660927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    char serial[256];
567dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
568dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* Since Linux will not reassign the device ID (and dev_name)
569dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ** as long as the device is open, we can add to the list here
570dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ** once we open it and remove from the list when we're finally
571dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ** closed and everything will work out fine.
572dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        **
573dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ** If we have a usb_handle on the list 'o handles with a matching
574dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ** name, we have no further work to do.
575dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        */
576dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&usb_lock);
577dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
578dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!strcmp(usb->fname, dev_name)) {
579dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            adb_mutex_unlock(&usb_lock);
580dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return;
581dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
582dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
583dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&usb_lock);
584dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
585dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("[ usb located new device %s (%d/%d/%d) ]\n",
586dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        dev_name, ep_in, ep_out, interface);
587dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb = calloc(1, sizeof(usb_handle));
588dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(usb->fname, dev_name);
589dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->ep_in = ep_in;
590dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->ep_out = ep_out;
591dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->zero_mask = zero_mask;
5920927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    usb->writeable = 1;
593dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
594dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_cond_init(&usb->notify, 0);
595dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_init(&usb->lock, 0);
596dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* initialize mark to 1 so we don't get garbage collected after the device scan */
597dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->mark = 1;
598dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->reaper_thread = 0;
599dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
600dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->desc = unix_open(usb->fname, O_RDWR);
6010927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    if(usb->desc < 0) {
6020927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        /* if we fail, see if have read-only access */
6030927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        usb->desc = unix_open(usb->fname, O_RDONLY);
6040927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        if(usb->desc < 0) goto fail;
6050927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        usb->writeable = 0;
6060927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
6070927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    } else {
6080927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        D("[ usb open %s fd = %d]\n", usb->fname, usb->desc);
6090927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface);
6100927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        if(n != 0) goto fail;
6110927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    }
6120927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6130927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        /* read the device's serial number */
6140927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    serial[0] = 0;
6150927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    memset(serial, 0, sizeof(serial));
6160927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    if (serial_index) {
6170927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        struct usbdevfs_ctrltransfer  ctrl;
6180927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        __u16 buffer[128];
6190927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        __u16 languages[128];
6200927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        int i, result;
6210927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        int languageCount = 0;
6220927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6230927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        memset(languages, 0, sizeof(languages));
6240927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        memset(&ctrl, 0, sizeof(ctrl));
6250927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6260927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            // read list of supported languages
6270927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
6280927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
6290927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.wValue = (USB_DT_STRING << 8) | 0;
6300927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.wIndex = 0;
6310927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.wLength = sizeof(languages);
6320927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        ctrl.data = languages;
6338bbae239156cb51fd36300368588e9bf8d42613dOmari Stephens        ctrl.timeout = 1000;
6340927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6350927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
6360927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        if (result > 0)
6370927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            languageCount = (result - 2) / 2;
6380927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6390927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        for (i = 1; i <= languageCount; i++) {
6400927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            memset(buffer, 0, sizeof(buffer));
6410927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            memset(&ctrl, 0, sizeof(ctrl));
6420927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6430927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
6440927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
6450927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ctrl.wValue = (USB_DT_STRING << 8) | serial_index;
6466f703a2e85a3098b3546a0ce254349b72b440628Marcus Comstedt            ctrl.wIndex = __le16_to_cpu(languages[i]);
6470927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ctrl.wLength = sizeof(buffer);
6480927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            ctrl.data = buffer;
6498bbae239156cb51fd36300368588e9bf8d42613dOmari Stephens            ctrl.timeout = 1000;
6500927bf9690127bc45cf8837a1467759e9720399aMike Lockwood
6510927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
6520927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            if (result > 0) {
6530927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                int i;
6540927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                // skip first word, and copy the rest to the serial string, changing shorts to bytes.
6550927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                result /= 2;
6560927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                for (i = 1; i < result; i++)
6576f703a2e85a3098b3546a0ce254349b72b440628Marcus Comstedt                    serial[i - 1] = __le16_to_cpu(buffer[i]);
6580927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                serial[i - 1] = 0;
6590927bf9690127bc45cf8837a1467759e9720399aMike Lockwood                break;
6600927bf9690127bc45cf8837a1467759e9720399aMike Lockwood            }
6610927bf9690127bc45cf8837a1467759e9720399aMike Lockwood        }
6620927bf9690127bc45cf8837a1467759e9720399aMike Lockwood    }
663dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
664dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* add to the end of the active handles */
665dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_lock(&usb_lock);
666dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->next = &handle_list;
667dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->prev = handle_list.prev;
668dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->prev->next = usb;
669dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb->next->prev = usb;
670dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_mutex_unlock(&usb_lock);
671dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
672e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson    register_usb_transport(usb, serial, devpath, usb->writeable);
673dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return;
674dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
675dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectfail:
676dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("[ usb open %s error=%d, err_str = %s]\n",
677dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        usb->fname,  errno, strerror(errno));
678dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(usb->desc >= 0) {
679dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_close(usb->desc);
680dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
681dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(usb);
682dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
683dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
684dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid* device_poll_thread(void* unused)
685dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
686dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("Created device thread\n");
687dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
688dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /* XXX use inotify */
689dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        find_usb_device("/dev/bus/usb", register_device);
690dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        kick_disconnected_devices();
691dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        sleep(1);
692dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
693dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return NULL;
694dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
695dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
696dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void sigalrm_handler(int signo)
697dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
698dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // don't need to do anything here
699dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
700dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
701dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid usb_init()
702dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
703dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_thread_t tid;
704dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct sigaction    actions;
705dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
706dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(&actions, 0, sizeof(actions));
707dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sigemptyset(&actions.sa_mask);
708dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    actions.sa_flags = 0;
709dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    actions.sa_handler = sigalrm_handler;
710dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sigaction(SIGALRM,& actions, NULL);
711dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
712dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(adb_thread_create(&tid, device_poll_thread, NULL)){
713dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fatal_errno("cannot create input thread");
714dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
715dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
716