Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 207c17ff1f
Fetching contributors…

Cannot retrieve contributors at this time

file 226 lines (183 sloc) 6.901 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/*
* Copyright (C) 2010 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 <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <usbhost/usbhost.h>

static int verbose = 0;
static char str_buff[4096];

static const char *get_str(struct usb_device *dev, int id)
{
    char *str = usb_device_get_string(dev, id);

    if (id && str) {
        strlcpy(str_buff, str, sizeof(str_buff));
        free(str);
    } else {
        snprintf(str_buff, sizeof(str_buff), "%02x", id);
    }

    return str_buff;
}


static void lsusb_parse_device_descriptor(struct usb_device *dev,
                                          struct usb_device_descriptor *desc)
{
    printf(" Device Descriptor\n");
    printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB));
    printf("\tbDeviceClass: %02x\n", desc->bDeviceClass);
    printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass);
    printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol);
    printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0);
    printf("\tidVendor: %04x\n", letoh16(desc->idVendor));
    printf("\tidProduct: %04x\n", letoh16(desc->idProduct));
    printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice));
    printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer));
    printf("\tiProduct: %s\n", get_str(dev, desc->iProduct));
    printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber));
    printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations);
    printf("\n");
}

static void lsusb_parse_config_descriptor(struct usb_device *dev,
                                          struct usb_config_descriptor *desc)
{
    printf(" Config Descriptor\n");
    printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength));
    printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces);
    printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue);
    printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration));
    printf("\tbmAttributes: %02x\n", desc->bmAttributes);
    printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2);
    printf("\n");
}

static void lsusb_parse_interface_descriptor(struct usb_device *dev,
                                             struct usb_interface_descriptor *desc)
{
    printf(" Interface Descriptor\n");
    printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber);
    printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting);
    printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints);
    printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass);
    printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass);
    printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol);
    printf("\tiInterface: %s\n", get_str(dev, desc->iInterface));
    printf("\n");
}

static void lsusb_parse_endpoint_descriptor(struct usb_device *dev,
                                            struct usb_endpoint_descriptor *desc)
{
    printf(" Endpoint Descriptor\n");
    printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress);
    printf("\tbmAttributes: %02x\n", desc->bmAttributes);
    printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize));
    printf("\tbInterval: %02x\n", desc->bInterval);
    printf("\tbRefresh: %02x\n", desc->bRefresh);
    printf("\tbSynchAddress: %02x\n", desc->bSynchAddress);
    printf("\n");
}

static void lsusb_dump_descriptor(struct usb_device *dev,
                                  struct usb_descriptor_header *desc)
{
    int i;
    printf(" Descriptor type %02x\n", desc->bDescriptorType);

    for (i = 0; i < desc->bLength; i++ ) {
        if ((i % 16) == 0)
            printf("\t%02x:", i);
        printf(" %02x", ((uint8_t *)desc)[i]);
        if ((i % 16) == 15)
            printf("\n");
    }

    if ((i % 16) != 0)
        printf("\n");
    printf("\n");
}

static void lsusb_parse_descriptor(struct usb_device *dev,
                                   struct usb_descriptor_header *desc)
{
    switch (desc->bDescriptorType) {
    case USB_DT_DEVICE:
        lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc);
        break;

    case USB_DT_CONFIG:
        lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc);
        break;

    case USB_DT_INTERFACE:
        lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc);
        break;

    case USB_DT_ENDPOINT:
        lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc);
        break;

    default:
        lsusb_dump_descriptor(dev, desc);

        break;
    }
}

static int lsusb_device_added(const char *dev_name, void *client_data)
{
    struct usb_device *dev = usb_device_open(dev_name);

    if (!dev) {
        fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
        return 0;
    }

    if (verbose) {
        struct usb_descriptor_iter iter;
        struct usb_descriptor_header *desc;

        printf("%s:\n", dev_name);

        usb_descriptor_iter_init(dev, &iter);

        while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
            lsusb_parse_descriptor(dev, desc);

    } else {
        uint16_t vid, pid;
        char *mfg_name, *product_name, *serial;

        vid = usb_device_get_vendor_id(dev);
        pid = usb_device_get_product_id(dev);
        mfg_name = usb_device_get_manufacturer_name(dev);
        product_name = usb_device_get_product_name(dev);
        serial = usb_device_get_serial(dev);

        printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
               mfg_name, product_name, serial);

        free(mfg_name);
        free(product_name);
        free(serial);
    }

    usb_device_close(dev);

    return 0;
}

static int lsusb_device_removed(const char *dev_name, void *client_data)
{
    return 0;
}


static int lsusb_discovery_done(void *client_data)
{
    return 1;
}



int lsusb_main(int argc, char **argv)
{
    struct usb_host_context *ctx;

    if (argc == 2 && !strcmp(argv[1], "-v"))
        verbose = 1;

    ctx = usb_host_init();
    if (!ctx) {
        perror("usb_host_init:");
        return 1;
    }

    usb_host_run(ctx,
                 lsusb_device_added,
                 lsusb_device_removed,
                 lsusb_discovery_done,
                 NULL);

    usb_host_cleanup(ctx);

    return 0;
}
Something went wrong with that request. Please try again.