blob: 236e74bc7890132cfec98bf1b8f6db32eccdb966 [file] [log] [blame]
Erik Gillingfd1e8552010-12-09 14:52:53 -08001/*
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
Erik Gillingbec29d42010-12-09 17:14:27 -080017#include <endian.h>
Erik Gillingfd1e8552010-12-09 14:52:53 -080018#include <errno.h>
19#include <stdio.h>
20#include <stdint.h>
21#include <string.h>
22
23#include <usbhost/usbhost.h>
24
Erik Gillingbec29d42010-12-09 17:14:27 -080025static int verbose = 0;
26static char str_buff[4096];
27
28static 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
43static 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
62static 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
75static 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
89static 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
102static 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
121static 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
Erik Gillingfd1e8552010-12-09 14:52:53 -0800148static int lsusb_device_added(const char *dev_name, void *client_data)
149{
150 struct usb_device *dev = usb_device_open(dev_name);
Erik Gillingfd1e8552010-12-09 14:52:53 -0800151
152 if (!dev) {
153 fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
154 return 0;
155 }
156
Erik Gillingbec29d42010-12-09 17:14:27 -0800157 if (verbose) {
158 struct usb_descriptor_iter iter;
159 struct usb_descriptor_header *desc;
Erik Gillingfd1e8552010-12-09 14:52:53 -0800160
Erik Gillingbec29d42010-12-09 17:14:27 -0800161 printf("%s:\n", dev_name);
Erik Gillingfd1e8552010-12-09 14:52:53 -0800162
Erik Gillingbec29d42010-12-09 17:14:27 -0800163 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 }
Erik Gillingfd1e8552010-12-09 14:52:53 -0800185
186 usb_device_close(dev);
187
188 return 0;
189}
190
191static int lsusb_device_removed(const char *dev_name, void *client_data)
192{
193 return 0;
194}
195
196
197static int lsusb_discovery_done(void *client_data)
198{
199 return 1;
200}
201
202
203
204int lsusb_main(int argc, char **argv)
205{
Erik Gillingbec29d42010-12-09 17:14:27 -0800206 struct usb_host_context *ctx;
207
208 if (argc == 2 && !strcmp(argv[1], "-v"))
209 verbose = 1;
210
211 ctx = usb_host_init();
Erik Gillingfd1e8552010-12-09 14:52:53 -0800212 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