Home | History | Annotate | Download | only in toolbox
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <endian.h>
     18 #include <errno.h>
     19 #include <stdio.h>
     20 #include <stdint.h>
     21 #include <string.h>
     22 
     23 #include <usbhost/usbhost.h>
     24 
     25 static int verbose = 0;
     26 static char str_buff[4096];
     27 
     28 static const char *get_str(struct usb_device *dev, int id)
     29 {
     30     char *str = usb_device_get_string(dev, id);
     31 
     32     if (id && str) {
     33         strlcpy(str_buff, str, sizeof(str_buff));
     34         free(str);
     35     } else {
     36         snprintf(str_buff, sizeof(str_buff), "%02x", id);
     37     }
     38 
     39     return str_buff;
     40 }
     41 
     42 
     43 static void lsusb_parse_device_descriptor(struct usb_device *dev,
     44                                           struct usb_device_descriptor *desc)
     45 {
     46     printf("  Device Descriptor\n");
     47     printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB));
     48     printf("\tbDeviceClass: %02x\n", desc->bDeviceClass);
     49     printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass);
     50     printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol);
     51     printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0);
     52     printf("\tidVendor: %04x\n", letoh16(desc->idVendor));
     53     printf("\tidProduct: %04x\n", letoh16(desc->idProduct));
     54     printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice));
     55     printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer));
     56     printf("\tiProduct: %s\n", get_str(dev, desc->iProduct));
     57     printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber));
     58     printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations);
     59     printf("\n");
     60 }
     61 
     62 static void lsusb_parse_config_descriptor(struct usb_device *dev,
     63                                           struct usb_config_descriptor *desc)
     64 {
     65     printf("  Config Descriptor\n");
     66     printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength));
     67     printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces);
     68     printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue);
     69     printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration));
     70     printf("\tbmAttributes: %02x\n", desc->bmAttributes);
     71     printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2);
     72     printf("\n");
     73 }
     74 
     75 static void lsusb_parse_interface_descriptor(struct usb_device *dev,
     76                                              struct usb_interface_descriptor *desc)
     77 {
     78     printf("  Interface Descriptor\n");
     79     printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber);
     80     printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting);
     81     printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints);
     82     printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass);
     83     printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass);
     84     printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol);
     85     printf("\tiInterface: %s\n", get_str(dev, desc->iInterface));
     86     printf("\n");
     87 }
     88 
     89 static void lsusb_parse_endpoint_descriptor(struct usb_device *dev,
     90                                             struct usb_endpoint_descriptor *desc)
     91 {
     92     printf("  Endpoint Descriptor\n");
     93     printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress);
     94     printf("\tbmAttributes: %02x\n", desc->bmAttributes);
     95     printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize));
     96     printf("\tbInterval: %02x\n", desc->bInterval);
     97     printf("\tbRefresh: %02x\n", desc->bRefresh);
     98     printf("\tbSynchAddress: %02x\n", desc->bSynchAddress);
     99     printf("\n");
    100 }
    101 
    102 static void lsusb_dump_descriptor(struct usb_device *dev,
    103                                   struct usb_descriptor_header *desc)
    104 {
    105     int i;
    106     printf("  Descriptor type %02x\n", desc->bDescriptorType);
    107 
    108     for (i = 0; i < desc->bLength; i++ ) {
    109         if ((i % 16) == 0)
    110             printf("\t%02x:", i);
    111         printf(" %02x", ((uint8_t *)desc)[i]);
    112         if ((i % 16) == 15)
    113             printf("\n");
    114     }
    115 
    116     if ((i % 16) != 0)
    117         printf("\n");
    118     printf("\n");
    119 }
    120 
    121 static void lsusb_parse_descriptor(struct usb_device *dev,
    122                                    struct usb_descriptor_header *desc)
    123 {
    124     switch (desc->bDescriptorType) {
    125     case USB_DT_DEVICE:
    126         lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc);
    127         break;
    128 
    129     case USB_DT_CONFIG:
    130         lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc);
    131         break;
    132 
    133     case USB_DT_INTERFACE:
    134         lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc);
    135         break;
    136 
    137     case USB_DT_ENDPOINT:
    138         lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc);
    139         break;
    140 
    141     default:
    142         lsusb_dump_descriptor(dev, desc);
    143 
    144         break;
    145     }
    146 }
    147 
    148 static int lsusb_device_added(const char *dev_name, void *client_data)
    149 {
    150     struct usb_device *dev = usb_device_open(dev_name);
    151 
    152     if (!dev) {
    153         fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
    154         return 0;
    155     }
    156 
    157     if (verbose) {
    158         struct usb_descriptor_iter iter;
    159         struct usb_descriptor_header *desc;
    160 
    161         printf("%s:\n", dev_name);
    162 
    163         usb_descriptor_iter_init(dev, &iter);
    164 
    165         while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
    166             lsusb_parse_descriptor(dev, desc);
    167 
    168     } else {
    169         uint16_t vid, pid;
    170         char *mfg_name, *product_name, *serial;
    171 
    172         vid = usb_device_get_vendor_id(dev);
    173         pid = usb_device_get_product_id(dev);
    174         mfg_name = usb_device_get_manufacturer_name(dev);
    175         product_name = usb_device_get_product_name(dev);
    176         serial = usb_device_get_serial(dev);
    177 
    178         printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
    179                mfg_name, product_name, serial);
    180 
    181         free(mfg_name);
    182         free(product_name);
    183         free(serial);
    184     }
    185 
    186     usb_device_close(dev);
    187 
    188     return 0;
    189 }
    190 
    191 static int lsusb_device_removed(const char *dev_name, void *client_data)
    192 {
    193     return 0;
    194 }
    195 
    196 
    197 static int lsusb_discovery_done(void *client_data)
    198 {
    199     return 1;
    200 }
    201 
    202 
    203 
    204 int lsusb_main(int argc, char **argv)
    205 {
    206     struct usb_host_context *ctx;
    207 
    208     if (argc == 2 && !strcmp(argv[1], "-v"))
    209         verbose = 1;
    210 
    211     ctx = usb_host_init();
    212     if (!ctx) {
    213         perror("usb_host_init:");
    214         return 1;
    215     }
    216 
    217     usb_host_run(ctx,
    218                  lsusb_device_added,
    219                  lsusb_device_removed,
    220                  lsusb_discovery_done,
    221                  NULL);
    222 
    223     usb_host_cleanup(ctx);
    224 
    225     return 0;
    226 }
    227 
    228