usb_linux.c

来自「Android 一些工具」· C语言 代码 · 共 654 行 · 第 1/2 页

C
654
字号
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/ioctl.h>#include <sys/types.h>#include <dirent.h>#include <fcntl.h>#include <errno.h>#include <ctype.h>#include <linux/usbdevice_fs.h>#include <linux/version.h>#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)#include <linux/usb/ch9.h>#else#include <linux/usb_ch9.h>#endif#include <asm/byteorder.h>#include "sysdeps.h"#define   TRACE_TAG  TRACE_USB#include "adb.h"/* usb scan debugging is waaaay too verbose */#define DBGX(x...)static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER;struct usb_handle{    usb_handle *prev;    usb_handle *next;    char fname[64];    int desc;    unsigned char ep_in;    unsigned char ep_out;    unsigned zero_mask;    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_mutex_t lock;    // for garbage collecting disconnected devices    int mark;    // ID of thread currently in REAPURB    pthread_t reaper_thread;};static usb_handle handle_list = {    .prev = &handle_list,    .next = &handle_list,};static int known_device(const char *dev_name){    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;            adb_mutex_unlock(&usb_lock);            return 1;        }    }    adb_mutex_unlock(&usb_lock);    return 0;}static void kick_disconnected_devices(){    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) {            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, const char *serial, unsigned zero_mask);static inline int badname(const char *name){    while(*name) {        if(!isdigit(*name++)) return 1;    }    return 0;}static int find_usb_device(const char *base,                           void (*register_device_callback) (const char *, unsigned char, unsigned char, int, const char *, unsigned)){    char busname[32], devname[32];    unsigned char local_ep_in, local_ep_out;    DIR *busdir , *devdir ;    struct dirent *de;    int fd ;    int found_device = 0;    char serial[256];    busdir = opendir(base);    if(busdir == 0) return 0;    while((de = readdir(busdir)) != 0) {        if(badname(de->d_name)) continue;        snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);        devdir = opendir(busname);        if(devdir == 0) continue;//        DBGX("[ scanning %s ]\n", busname);        while((de = readdir(devdir))) {            unsigned char devdesc[256];            unsigned char* bufptr = devdesc;            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;            int i, interfaces;            size_t desclength;            if(badname(de->d_name)) continue;            snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);            if(known_device(devname)) {                DBGX("skipping %s\n", devname);                continue;            }//            DBGX("[ scanning %s ]\n", devname);            if((fd = unix_open(devname, O_RDWR)) < 0) {                continue;            }            desclength = adb_read(fd, devdesc, sizeof(devdesc));                // 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;            }            device = (struct usb_device_descriptor*)bufptr;            bufptr += USB_DT_DEVICE_SIZE;            if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {                adb_close(fd);                continue;            }            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);                // 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 interfaces and look for the ADB interface            interfaces = config->bNumInterfaces;            for (i = 0; i < interfaces; i++) {                if (bufptr + USB_DT_ENDPOINT_SIZE > devdesc + desclength)                    break;                interface = (struct usb_interface_descriptor *)bufptr;                bufptr += USB_DT_INTERFACE_SIZE;                if (interface->bLength != USB_DT_INTERFACE_SIZE ||                    interface->bDescriptorType != USB_DT_INTERFACE) {                    D("usb_interface_descriptor not found\n");                    break;                }                DBGX("bInterfaceClass: %d,  bInterfaceSubClass: %d,"                     "bInterfaceProtocol: %d, bNumEndpoints: %d\n",                     interface->bInterfaceClass, interface->bInterfaceSubClass,                     interface->bInterfaceProtocol, interface->bNumEndpoints);                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;                    }                        // read the device's serial number                    serial[0] = 0;                    memset(serial, 0, sizeof(serial));                    if (device->iSerialNumber) {                        struct usbdevfs_ctrltransfer  ctrl;                        __u16 buffer[128];                        int result;                        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) | device->iSerialNumber;                        ctrl.wIndex = 0;                        ctrl.wLength = sizeof(buffer);                        ctrl.data = buffer;                        result = ioctl(fd, 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;                        }                    }                    register_device_callback(devname, local_ep_in, local_ep_out, i, serial, zero_mask);                    found_device = 1;                    break;                } else {                        // skip to next interface                    bufptr += (interface->bNumEndpoints * USB_DT_ENDPOINT_SIZE);                }            } // end of for            adb_close(fd);        } // end of devdir while        closedir(devdir);    } //end of busdir while    closedir(busdir);    return found_device;}void usb_cleanup(){}static int usb_bulk_write(usb_handle *h, const void *data, int len){    struct usbdevfs_urb *urb = &h->urb_out;    int res;    memset(urb, 0, sizeof(*urb));    urb->type = USBDEVFS_URB_TYPE_BULK;    urb->endpoint = h->ep_out;    urb->status = -1;    urb->buffer = (void*) data;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?