Bring in USB4BSD userspace libraries and utilities.
[dragonfly.git] / usr.sbin / usbconfig / dump.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
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.
13  *
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.
25  */
26
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>
35
36 #include <libusb20.h>
37 #include <libusb20_desc.h>
38
39 #include "dump.h"
40
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);
45
46 const char *
47 dump_mode(uint8_t value)
48 {
49         if (value == LIBUSB20_MODE_HOST)
50                 return ("HOST");
51         return ("DEVICE");
52 }
53
54 const char *
55 dump_speed(uint8_t value)
56 {
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;
71         }
72         return ("UNKNOWN ()");
73 }
74
75 const char *
76 dump_power_mode(uint8_t value)
77 {
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");
92         }
93 }
94
95 static void
96 dump_field(struct libusb20_device *pdev, const char *plevel,
97     const char *field, uint32_t value)
98 {
99         uint8_t temp_string[256];
100
101         printf("%s%s = 0x%04x ", plevel, field, value);
102
103         if (strlen(plevel) == 8) {
104                 /* Endpoint Descriptor */
105
106                 if (strcmp(field, "bEndpointAddress") == 0) {
107                         if (value & 0x80)
108                                 printf(" <IN>\n");
109                         else
110                                 printf(" <OUT>\n");
111                         return;
112                 }
113
114                 if (strcmp(field, "bmAttributes") == 0) {
115                         switch (value & 0x03) {
116                         case 0:
117                                 printf(" <CONTROL>\n");
118                                 break;
119                         case 1:
120                                 switch (value & 0x0C) {
121                                 case 0x00:
122                                         printf(" <ISOCHRONOUS>\n");
123                                         break;
124                                 case 0x04:
125                                         printf(" <ASYNC-ISOCHRONOUS>\n");
126                                         break;
127                                 case 0x08:
128                                         printf(" <ADAPT-ISOCHRONOUS>\n");
129                                         break;
130                                 default:
131                                         printf(" <SYNC-ISOCHRONOUS>\n");
132                                         break;
133                                 }
134                                 break;
135                         case 2:
136                                 printf(" <BULK>\n");
137                                 break;
138                         default:
139                                 printf(" <INTERRUPT>\n");
140                                 break;
141                         }
142                         return;
143                 }
144         }
145
146         if ((field[0] == 'i') && (field[1] != 'd')) {
147                 /* Indirect String Descriptor */
148                 if (value == 0) {
149                         printf(" <no string>\n");
150                         return;
151                 }
152                 if (libusb20_dev_req_string_simple_sync(pdev, value,
153                     temp_string, sizeof(temp_string))) {
154                         printf(" <retrieving string failed>\n");
155                         return;
156                 }
157                 printf(" <%s>\n", temp_string);
158                 return;
159         }
160
161         /* No additional information */
162         printf("\n");
163 }
164
165 static void
166 dump_extra(struct libusb20_me_struct *str, const char *plevel)
167 {
168         const uint8_t *ptr;
169         uint8_t x;
170
171         ptr = NULL;
172
173         while ((ptr = libusb20_desc_foreach(str, ptr))) {
174                 printf("\n" "%sAdditional Descriptor\n\n", plevel);
175                 printf("%sbLength = 0x%02x\n", plevel, ptr[0]);
176                 printf("%sbDescriptorType = 0x%02x\n", plevel, ptr[1]);
177                 if (ptr[0] > 1)
178                         printf("%sbDescriptorSubType = 0x%02x\n",
179                             plevel, ptr[2]);
180                 printf("%s RAW dump: ", plevel);
181                 for (x = 0; x != ptr[0]; x++) {
182                         if ((x % 8) == 0) {
183                                 printf("\n%s 0x%02x | ", plevel, x);
184                         }
185                         printf("0x%02x%s", ptr[x],
186                             (x != (ptr[0] - 1)) ? ", " : (x % 8) ? "\n" : "");
187                 }
188                 printf("\n");
189         }
190         return;
191 }
192
193 static void
194 dump_endpoint(struct libusb20_device *pdev,
195     struct libusb20_endpoint *ep)
196 {
197         struct LIBUSB20_ENDPOINT_DESC_DECODED *edesc;
198
199         edesc = &ep->desc;
200         LIBUSB20_ENDPOINT_DESC(DUMP3, edesc);
201         dump_extra(&ep->extra, "  " "  " "  ");
202         return;
203 }
204
205 static void
206 dump_iface(struct libusb20_device *pdev,
207     struct libusb20_interface *iface)
208 {
209         struct LIBUSB20_INTERFACE_DESC_DECODED *idesc;
210         uint8_t z;
211
212         idesc = &iface->desc;
213         LIBUSB20_INTERFACE_DESC(DUMP2, idesc);
214         dump_extra(&iface->extra, "  " "  " "  ");
215
216         for (z = 0; z != iface->num_endpoints; z++) {
217                 printf("\n     Endpoint %u\n", z);
218                 dump_endpoint(pdev, iface->endpoints + z);
219         }
220         return;
221 }
222
223 void
224 dump_device_info(struct libusb20_device *pdev, uint8_t show_ifdrv)
225 {
226         char buf[128];
227         uint8_t n;
228
229         printf("%s, cfg=%u md=%s spd=%s pwr=%s\n",
230             libusb20_dev_get_desc(pdev),
231             libusb20_dev_get_config_index(pdev),
232             dump_mode(libusb20_dev_get_mode(pdev)),
233             dump_speed(libusb20_dev_get_speed(pdev)),
234             dump_power_mode(libusb20_dev_get_power_mode(pdev)));
235
236         if (!show_ifdrv)
237                 return;
238
239         for (n = 0; n != 255; n++) {
240                 if (libusb20_dev_get_iface_desc(pdev, n, buf, sizeof(buf)))
241                         break;
242                 if (buf[0] == 0)
243                         continue;
244                 printf("ugen%u.%u.%u: %s\n",
245                     libusb20_dev_get_bus_number(pdev),
246                     libusb20_dev_get_address(pdev), n, buf);
247         }
248 }
249
250 void
251 dump_be_quirk_names(struct libusb20_backend *pbe)
252 {
253         struct libusb20_quirk q;
254         uint16_t x;
255         int error;
256
257         memset(&q, 0, sizeof(q));
258
259         printf("\nDumping list of supported quirks:\n\n");
260
261         for (x = 0; x != 0xFFFF; x++) {
262
263                 error = libusb20_be_get_quirk_name(pbe, x, &q);
264                 if (error) {
265                         if (x == 0) {
266                                 printf("No quirk names - maybe the USB quirk "
267                                     "module has not been loaded.\n");
268                         }
269                         break;
270                 }
271                 if (strcmp(q.quirkname, "UQ_NONE"))
272                         printf("%s\n", q.quirkname);
273         }
274         printf("\n");
275         return;
276 }
277
278 void
279 dump_be_dev_quirks(struct libusb20_backend *pbe)
280 {
281         struct libusb20_quirk q;
282         uint16_t x;
283         int error;
284
285         memset(&q, 0, sizeof(q));
286
287         printf("\nDumping current device quirks:\n\n");
288
289         for (x = 0; x != 0xFFFF; x++) {
290
291                 error = libusb20_be_get_dev_quirk(pbe, x, &q);
292                 if (error) {
293                         if (x == 0) {
294                                 printf("No device quirks - maybe the USB quirk "
295                                     "module has not been loaded.\n");
296                         }
297                         break;
298                 }
299                 if (strcmp(q.quirkname, "UQ_NONE")) {
300                         printf("VID=0x%04x PID=0x%04x REVLO=0x%04x "
301                             "REVHI=0x%04x QUIRK=%s\n",
302                             q.vid, q.pid, q.bcdDeviceLow,
303                             q.bcdDeviceHigh, q.quirkname);
304                 }
305         }
306         printf("\n");
307         return;
308 }
309
310 void
311 dump_device_desc(struct libusb20_device *pdev)
312 {
313         struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
314
315         ddesc = libusb20_dev_get_device_desc(pdev);
316         LIBUSB20_DEVICE_DESC(DUMP0, ddesc);
317         return;
318 }
319
320 void
321 dump_config(struct libusb20_device *pdev, uint8_t all_cfg)
322 {
323         struct LIBUSB20_CONFIG_DESC_DECODED *cdesc;
324         struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
325         struct libusb20_config *pcfg = NULL;
326         uint8_t cfg_index;
327         uint8_t cfg_index_end;
328         uint8_t x;
329         uint8_t y;
330
331         ddesc = libusb20_dev_get_device_desc(pdev);
332
333         if (all_cfg) {
334                 cfg_index = 0;
335                 cfg_index_end = ddesc->bNumConfigurations;
336         } else {
337                 cfg_index = libusb20_dev_get_config_index(pdev);
338                 cfg_index_end = cfg_index + 1;
339         }
340
341         for (; cfg_index != cfg_index_end; cfg_index++) {
342
343                 pcfg = libusb20_dev_alloc_config(pdev, cfg_index);
344                 if (!pcfg) {
345                         continue;
346                 }
347                 printf("\n Configuration index %u\n\n", cfg_index);
348                 cdesc = &(pcfg->desc);
349                 LIBUSB20_CONFIG_DESC(DUMP1, cdesc);
350                 dump_extra(&(pcfg->extra), "  " "  ");
351
352                 for (x = 0; x != pcfg->num_interface; x++) {
353                         printf("\n    Interface %u\n", x);
354                         dump_iface(pdev, pcfg->interface + x);
355                         printf("\n");
356                         for (y = 0; y != (pcfg->interface + x)->num_altsetting; y++) {
357                                 printf("\n    Interface %u Alt %u\n", x, y + 1);
358                                 dump_iface(pdev,
359                                     (pcfg->interface + x)->altsetting + y);
360                                 printf("\n");
361                         }
362                 }
363                 printf("\n");
364                 free(pcfg);
365         }
366         return;
367 }
368
369 void
370 dump_string_by_index(struct libusb20_device *pdev, uint8_t str_index)
371 {
372         char *pbuf;
373         uint8_t n;
374         uint8_t len;
375
376         pbuf = malloc(256);
377         if (pbuf == NULL)
378                 err(1, "out of memory");
379
380         if (str_index == 0) {
381                 /* language table */
382                 if (libusb20_dev_req_string_sync(pdev,
383                     str_index, 0, pbuf, 256)) {
384                         printf("STRING_0x%02x = <read error>\n", str_index);
385                 } else {
386                         printf("STRING_0x%02x = ", str_index);
387                         len = (uint8_t)pbuf[0];
388                         for (n = 0; n != len; n++) {
389                                 printf("0x%02x%s", (uint8_t)pbuf[n], 
390                                     (n != (len-1)) ? ", " : "");
391                         }
392                         printf("\n");
393                 }
394         } else {
395                 /* ordinary string */
396                 if (libusb20_dev_req_string_simple_sync(pdev,
397                     str_index, pbuf, 256)) {
398                         printf("STRING_0x%02x = <read error>\n", str_index);
399                 } else {
400                         printf("STRING_0x%02x = <%s>\n", str_index, pbuf);
401                 }
402         }
403         free(pbuf);
404 }