s/NO_MAN/NOMAN/ in various Makefiles.
[dragonfly.git] / usr.sbin / usbconfig / dump.c
bloba207d2283d9f77e0132c10bd2afb703cbbbdc086
1 /* $FreeBSD: head/usr.sbin/usbconfig/dump.c 274435 2014-11-12 14:52:44Z hselasky $ */
2 /*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <err.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <ctype.h>
36 #include <libusb20.h>
37 #include <libusb20_desc.h>
39 #include "dump.h"
41 #define DUMP0(n,type,field,...) dump_field(pdev, " ", #field, n->field);
42 #define DUMP1(n,type,field,...) dump_field(pdev, " ", #field, n->field);
43 #define DUMP2(n,type,field,...) dump_field(pdev, " ", #field, n->field);
44 #define DUMP3(n,type,field,...) dump_field(pdev, " ", #field, n->field);
46 const char *
47 dump_mode(uint8_t value)
49 if (value == LIBUSB20_MODE_HOST)
50 return ("HOST");
51 return ("DEVICE");
54 const char *
55 dump_speed(uint8_t value)
57 ; /* style fix */
58 switch (value) {
59 case LIBUSB20_SPEED_LOW:
60 return ("LOW (1.5Mbps)");
61 case LIBUSB20_SPEED_FULL:
62 return ("FULL (12Mbps)");
63 case LIBUSB20_SPEED_HIGH:
64 return ("HIGH (480Mbps)");
65 case LIBUSB20_SPEED_VARIABLE:
66 return ("VARIABLE (52-480Mbps)");
67 case LIBUSB20_SPEED_SUPER:
68 return ("SUPER (5.0Gbps)");
69 default:
70 break;
72 return ("UNKNOWN ()");
75 const char *
76 dump_power_mode(uint8_t value)
78 ; /* style fix */
79 switch (value) {
80 case LIBUSB20_POWER_OFF:
81 return ("OFF");
82 case LIBUSB20_POWER_ON:
83 return ("ON");
84 case LIBUSB20_POWER_SAVE:
85 return ("SAVE");
86 case LIBUSB20_POWER_SUSPEND:
87 return ("SUSPEND");
88 case LIBUSB20_POWER_RESUME:
89 return ("RESUME");
90 default:
91 return ("UNKNOWN");
95 static void
96 dump_field(struct libusb20_device *pdev, const char *plevel,
97 const char *field, uint32_t value)
99 uint8_t temp_string[256];
101 printf("%s%s = 0x%04x ", plevel, field, value);
103 if (strlen(plevel) == 8) {
104 /* Endpoint Descriptor */
106 if (strcmp(field, "bEndpointAddress") == 0) {
107 if (value & 0x80)
108 printf(" <IN>\n");
109 else
110 printf(" <OUT>\n");
111 return;
113 if (strcmp(field, "bmAttributes") == 0) {
114 switch (value & 0x03) {
115 case 0:
116 printf(" <CONTROL>\n");
117 break;
118 case 1:
119 switch (value & 0x0C) {
120 case 0x00:
121 printf(" <ISOCHRONOUS>\n");
122 break;
123 case 0x04:
124 printf(" <ASYNC-ISOCHRONOUS>\n");
125 break;
126 case 0x08:
127 printf(" <ADAPT-ISOCHRONOUS>\n");
128 break;
129 default:
130 printf(" <SYNC-ISOCHRONOUS>\n");
131 break;
133 break;
134 case 2:
135 printf(" <BULK>\n");
136 break;
137 default:
138 printf(" <INTERRUPT>\n");
139 break;
141 return;
144 if ((field[0] == 'i') && (field[1] != 'd')) {
145 /* Indirect String Descriptor */
146 if (value == 0) {
147 printf(" <no string>\n");
148 return;
150 if (libusb20_dev_req_string_simple_sync(pdev, value,
151 temp_string, sizeof(temp_string))) {
152 printf(" <retrieving string failed>\n");
153 return;
155 printf(" <%s>\n", temp_string);
156 return;
158 if (strlen(plevel) == 2 || strlen(plevel) == 6) {
160 /* Device and Interface Descriptor class codes */
162 if (strcmp(field, "bInterfaceClass") == 0 ||
163 strcmp(field, "bDeviceClass") == 0) {
165 switch (value) {
166 case 0x00:
167 printf(" <Probed by interface class>\n");
168 break;
169 case 0x01:
170 printf(" <Audio device>\n");
171 break;
172 case 0x02:
173 printf(" <Communication device>\n");
174 break;
175 case 0x03:
176 printf(" <HID device>\n");
177 break;
178 case 0x05:
179 printf(" <Physical device>\n");
180 break;
181 case 0x06:
182 printf(" <Still imaging>\n");
183 break;
184 case 0x07:
185 printf(" <Printer device>\n");
186 break;
187 case 0x08:
188 printf(" <Mass storage>\n");
189 break;
190 case 0x09:
191 printf(" <HUB>\n");
192 break;
193 case 0x0A:
194 printf(" <CDC-data>\n");
195 break;
196 case 0x0B:
197 printf(" <Smart card>\n");
198 break;
199 case 0x0D:
200 printf(" <Content security>\n");
201 break;
202 case 0x0E:
203 printf(" <Video device>\n");
204 break;
205 case 0x0F:
206 printf(" <Personal healthcare>\n");
207 break;
208 case 0x10:
209 printf(" <Audio and video device>\n");
210 break;
211 case 0x11:
212 printf(" <Billboard device>\n");
213 break;
214 case 0xDC:
215 printf(" <Diagnostic device>\n");
216 break;
217 case 0xE0:
218 printf(" <Wireless controller>\n");
219 break;
220 case 0xEF:
221 printf(" <Miscellaneous device>\n");
222 break;
223 case 0xFE:
224 printf(" <Application specific>\n");
225 break;
226 case 0xFF:
227 printf(" <Vendor specific>\n");
228 break;
229 default:
230 printf(" <Unknown>\n");
231 break;
233 return;
236 /* No additional information */
237 printf("\n");
240 static void
241 dump_extra(struct libusb20_me_struct *str, const char *plevel)
243 const uint8_t *ptr;
244 uint8_t x;
246 ptr = NULL;
248 while ((ptr = libusb20_desc_foreach(str, ptr))) {
249 printf("\n" "%sAdditional Descriptor\n\n", plevel);
250 printf("%sbLength = 0x%02x\n", plevel, ptr[0]);
251 printf("%sbDescriptorType = 0x%02x\n", plevel, ptr[1]);
252 if (ptr[0] > 1)
253 printf("%sbDescriptorSubType = 0x%02x\n",
254 plevel, ptr[2]);
255 printf("%s RAW dump: ", plevel);
256 for (x = 0; x != ptr[0]; x++) {
257 if ((x % 8) == 0) {
258 printf("\n%s 0x%02x | ", plevel, x);
260 printf("0x%02x%s", ptr[x],
261 (x != (ptr[0] - 1)) ? ", " : (x % 8) ? "\n" : "");
263 printf("\n");
265 return;
268 static void
269 dump_endpoint(struct libusb20_device *pdev,
270 struct libusb20_endpoint *ep)
272 struct LIBUSB20_ENDPOINT_DESC_DECODED *edesc;
274 edesc = &ep->desc;
275 LIBUSB20_ENDPOINT_DESC(DUMP3, edesc);
276 dump_extra(&ep->extra, " " " " " ");
277 return;
280 static void
281 dump_iface(struct libusb20_device *pdev,
282 struct libusb20_interface *iface)
284 struct LIBUSB20_INTERFACE_DESC_DECODED *idesc;
285 uint8_t z;
287 idesc = &iface->desc;
288 LIBUSB20_INTERFACE_DESC(DUMP2, idesc);
289 dump_extra(&iface->extra, " " " " " ");
291 for (z = 0; z != iface->num_endpoints; z++) {
292 printf("\n Endpoint %u\n", z);
293 dump_endpoint(pdev, iface->endpoints + z);
295 return;
298 void
299 dump_device_info(struct libusb20_device *pdev, uint8_t show_ifdrv)
301 char buf[128];
302 uint8_t n;
303 unsigned int usage;
305 usage = libusb20_dev_get_power_usage(pdev);
307 printf("%s, cfg=%u md=%s spd=%s pwr=%s (%umA)\n",
308 libusb20_dev_get_desc(pdev),
309 libusb20_dev_get_config_index(pdev),
310 dump_mode(libusb20_dev_get_mode(pdev)),
311 dump_speed(libusb20_dev_get_speed(pdev)),
312 dump_power_mode(libusb20_dev_get_power_mode(pdev)),
313 usage);
315 if (!show_ifdrv)
316 return;
318 for (n = 0; n != 255; n++) {
319 if (libusb20_dev_get_iface_desc(pdev, n, buf, sizeof(buf)))
320 break;
321 if (buf[0] == 0)
322 continue;
323 printf("ugen%u.%u.%u: %s\n",
324 libusb20_dev_get_bus_number(pdev),
325 libusb20_dev_get_address(pdev), n, buf);
329 void
330 dump_be_quirk_names(struct libusb20_backend *pbe)
332 struct libusb20_quirk q;
333 uint16_t x;
334 int error;
336 memset(&q, 0, sizeof(q));
338 printf("\nDumping list of supported quirks:\n\n");
340 for (x = 0; x != 0xFFFF; x++) {
342 error = libusb20_be_get_quirk_name(pbe, x, &q);
343 if (error) {
344 if (x == 0) {
345 printf("No quirk names - maybe the USB quirk "
346 "module has not been loaded.\n");
348 break;
350 if (strcmp(q.quirkname, "UQ_NONE"))
351 printf("%s\n", q.quirkname);
353 printf("\n");
354 return;
357 void
358 dump_be_dev_quirks(struct libusb20_backend *pbe)
360 struct libusb20_quirk q;
361 uint16_t x;
362 int error;
364 memset(&q, 0, sizeof(q));
366 printf("\nDumping current device quirks:\n\n");
368 for (x = 0; x != 0xFFFF; x++) {
370 error = libusb20_be_get_dev_quirk(pbe, x, &q);
371 if (error) {
372 if (x == 0) {
373 printf("No device quirks - maybe the USB quirk "
374 "module has not been loaded.\n");
376 break;
378 if (strcmp(q.quirkname, "UQ_NONE")) {
379 printf("VID=0x%04x PID=0x%04x REVLO=0x%04x "
380 "REVHI=0x%04x QUIRK=%s\n",
381 q.vid, q.pid, q.bcdDeviceLow,
382 q.bcdDeviceHigh, q.quirkname);
385 printf("\n");
386 return;
389 void
390 dump_device_desc(struct libusb20_device *pdev)
392 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
394 ddesc = libusb20_dev_get_device_desc(pdev);
395 LIBUSB20_DEVICE_DESC(DUMP0, ddesc);
396 return;
399 void
400 dump_config(struct libusb20_device *pdev, uint8_t all_cfg)
402 struct LIBUSB20_CONFIG_DESC_DECODED *cdesc;
403 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
404 struct libusb20_config *pcfg = NULL;
405 uint8_t cfg_index;
406 uint8_t cfg_index_end;
407 uint8_t x;
408 uint8_t y;
410 ddesc = libusb20_dev_get_device_desc(pdev);
412 if (all_cfg) {
413 cfg_index = 0;
414 cfg_index_end = ddesc->bNumConfigurations;
415 } else {
416 cfg_index = libusb20_dev_get_config_index(pdev);
417 cfg_index_end = cfg_index + 1;
420 for (; cfg_index != cfg_index_end; cfg_index++) {
422 pcfg = libusb20_dev_alloc_config(pdev, cfg_index);
423 if (!pcfg) {
424 continue;
426 printf("\n Configuration index %u\n\n", cfg_index);
427 cdesc = &(pcfg->desc);
428 LIBUSB20_CONFIG_DESC(DUMP1, cdesc);
429 dump_extra(&(pcfg->extra), " " " ");
431 for (x = 0; x != pcfg->num_interface; x++) {
432 printf("\n Interface %u\n", x);
433 dump_iface(pdev, pcfg->interface + x);
434 printf("\n");
435 for (y = 0; y != (pcfg->interface + x)->num_altsetting; y++) {
436 printf("\n Interface %u Alt %u\n", x, y + 1);
437 dump_iface(pdev,
438 (pcfg->interface + x)->altsetting + y);
439 printf("\n");
442 printf("\n");
443 free(pcfg);
445 return;
448 void
449 dump_string_by_index(struct libusb20_device *pdev, uint8_t str_index)
451 char *pbuf;
452 uint8_t n;
453 uint8_t len;
455 pbuf = malloc(256);
456 if (pbuf == NULL)
457 err(1, "out of memory");
459 if (str_index == 0) {
460 /* language table */
461 if (libusb20_dev_req_string_sync(pdev,
462 str_index, 0, pbuf, 256)) {
463 printf("STRING_0x%02x = <read error>\n", str_index);
464 } else {
465 printf("STRING_0x%02x = ", str_index);
466 len = (uint8_t)pbuf[0];
467 for (n = 0; n != len; n++) {
468 printf("0x%02x%s", (uint8_t)pbuf[n],
469 (n != (len-1)) ? ", " : "");
471 printf("\n");
473 } else {
474 /* ordinary string */
475 if (libusb20_dev_req_string_simple_sync(pdev,
476 str_index, pbuf, 256)) {
477 printf("STRING_0x%02x = <read error>\n", str_index);
478 } else {
479 printf("STRING_0x%02x = <%s>\n", str_index, pbuf);
482 free(pbuf);