forked from gregkh/usbutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsusb.c
3861 lines (3519 loc) · 115 KB
/
lsusb.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* lspci like utility for the USB bus
*
* Copyright (C) 1999-2001, 2003 Thomas Sailer (t.sailer@alumni.ethz.ch)
* Copyright (C) 2003-2005 David Brownell
*/
#include "config.h"
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <libusb.h>
#include <unistd.h>
#include "lsusb.h"
#include "names.h"
#include "sysfs.h"
#include "usbmisc.h"
#include "desc-defs.h"
#include "desc-dump.h"
#include <getopt.h>
#define le16_to_cpu(x) libusb_cpu_to_le16(libusb_cpu_to_le16(x))
/* from USB 2.0 spec and updates */
#define USB_DT_DEVICE_QUALIFIER 0x06
#define USB_DT_OTHER_SPEED_CONFIG 0x07
#define USB_DT_OTG 0x09
#define USB_DT_DEBUG 0x0a
#define USB_DT_INTERFACE_ASSOCIATION 0x0b
#define USB_DT_SECURITY 0x0c
#define USB_DT_KEY 0x0d
#define USB_DT_ENCRYPTION_TYPE 0x0e
#define USB_DT_BOS 0x0f
#define USB_DT_DEVICE_CAPABILITY 0x10
#define USB_DT_WIRELESS_ENDPOINT_COMP 0x11
#define USB_DT_WIRE_ADAPTER 0x21
#define USB_DT_RPIPE 0x22
#define USB_DT_RC_INTERFACE 0x23
#define USB_DT_SS_ENDPOINT_COMP 0x30
/* Device Capability Type Codes (Wireless USB spec and USB 3.0 bus spec) */
#define USB_DC_WIRELESS_USB 0x01
#define USB_DC_20_EXTENSION 0x02
#define USB_DC_SUPERSPEED 0x03
#define USB_DC_CONTAINER_ID 0x04
#define USB_DC_PLATFORM 0x05
#define USB_DC_SUPERSPEEDPLUS 0x0a
#define USB_DC_BILLBOARD 0x0d
#define USB_DC_BILLBOARD_ALT_MODE 0x0f
#define USB_DC_CONFIGURATION_SUMMARY 0x10
/* Conventional codes for class-specific descriptors. The convention is
* defined in the USB "Common Class" Spec (3.11). Individual class specs
* are authoritative for their usage, not the "common class" writeup.
*/
#define USB_DT_CS_DEVICE (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_DEVICE)
#define USB_DT_CS_CONFIG (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_CONFIG)
#define USB_DT_CS_STRING (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_STRING)
#define USB_DT_CS_INTERFACE (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_INTERFACE)
#define USB_DT_CS_ENDPOINT (LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_ENDPOINT)
#ifndef USB_CLASS_CCID
#define USB_CLASS_CCID 0x0b
#endif
#ifndef USB_CLASS_VIDEO
#define USB_CLASS_VIDEO 0x0e
#endif
#ifndef USB_CLASS_APPLICATION
#define USB_CLASS_APPLICATION 0xfe
#endif
#ifndef USB_AUDIO_CLASS_1
#define USB_AUDIO_CLASS_1 0x00
#endif
#ifndef USB_AUDIO_CLASS_2
#define USB_AUDIO_CLASS_2 0x20
#endif
/* USB DCD for Audio Devices Release 3.0: Section A.6, pp139 */
#ifndef USB_AUDIO_CLASS_3
#define USB_AUDIO_CLASS_3 0x30
#endif
#ifndef USB_VIDEO_PROTOCOL_15
#define USB_VIDEO_PROTOCOL_15 0x01
#endif
#define VERBLEVEL_DEFAULT 0 /* 0 gives lspci behaviour; 1, lsusb-0.9 */
#define CTRL_TIMEOUT (5*1000) /* milliseconds */
#define HUB_STATUS_BYTELEN 3 /* max 3 bytes status = hub + 23 ports */
#define BILLBOARD_MAX_NUM_ALT_MODE (0x34)
/* from WebUSB specification : https://wicg.github.io/webusb/ */
#define WEBUSB_GUID "{3408b638-09a9-47a0-8bfd-a0768815b665}"
#define WEBUSB_GET_URL 0x02
#define USB_DT_WEBUSB_URL 0x03
/* New speeds are only in newer versions of libusb */
#ifndef LIBUSB_SPEED_SUPER_PLUS_X2
#define LIBUSB_SPEED_SUPER_PLUS_X2 6
#endif
unsigned int verblevel = VERBLEVEL_DEFAULT;
static int do_report_desc = 1;
static const char * const encryption_type[] = {
"UNSECURE",
"WIRED",
"CCM_1",
"RSA_1",
"RESERVED"
};
static const char * const vconn_power[] = {
"1W",
"1.5W",
"2W",
"3W",
"4W",
"5W",
"6W",
"reserved"
};
static const char * const alt_mode_state[] = {
"Unspecified Error",
"Alternate Mode configuration not attempted",
"Alternate Mode configuration attempted but unsuccessful",
"Alternate Mode configuration successful"
};
static void dump_interface(libusb_device_handle *dev, const struct libusb_interface *interface);
static void dump_endpoint(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const struct libusb_endpoint_descriptor *endpoint);
static void dump_audiocontrol_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol);
static void dump_audiostreaming_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol);
static void dump_midistreaming_interface(libusb_device_handle *dev, const unsigned char *buf);
static void dump_videocontrol_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol);
static void dump_videostreaming_interface(const unsigned char *buf);
static void dump_dfu_interface(const unsigned char *buf);
static char *dump_comm_descriptor(libusb_device_handle *dev, const unsigned char *buf, char *indent);
static void dump_hid_device(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const unsigned char *buf);
static void dump_printer_device(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const unsigned char *buf);
static void dump_audiostreaming_endpoint(libusb_device_handle *dev, const unsigned char *buf, int protocol);
static void dump_midistreaming_endpoint(const unsigned char *buf);
static void dump_hub(const char *prefix, const unsigned char *p, int tt_type);
static void dump_ccid_device(const unsigned char *buf);
static void dump_billboard_device_capability_desc(libusb_device_handle *dev, unsigned char *buf);
/* ---------------------------------------------------------------------- */
static unsigned int convert_le_u32 (const unsigned char *buf)
{
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}
static unsigned int convert_le_u16 (const unsigned char *buf)
{
return buf[0] | (buf[1] << 8);
}
/* ---------------------------------------------------------------------- */
/* workaround libusb API goofs: "byte" should never be sign extended;
* using "char" is trouble.
*/
static inline int typesafe_control_msg(libusb_device_handle *dev,
unsigned char requesttype, unsigned char request,
int value, int idx,
unsigned char *bytes, unsigned size, int timeout)
{
int ret = libusb_control_transfer(dev, requesttype, request, value,
idx, bytes, size, timeout);
return ret;
}
#define usb_control_msg typesafe_control_msg
static int get_protocol_string(char *buf, size_t size, uint8_t cls, uint8_t subcls, uint8_t proto)
{
const char *cp;
if (size < 1)
return 0;
*buf = 0;
if (!(cp = names_protocol(cls, subcls, proto)))
return 0;
return snprintf(buf, size, "%s", cp);
}
static int get_videoterminal_string(char *buf, size_t size, uint16_t termt)
{
const char *cp;
if (size < 1)
return 0;
*buf = 0;
if (!(cp = names_videoterminal(termt)))
return 0;
return snprintf(buf, size, "%s", cp);
}
static const char *get_guid(const unsigned char *buf)
{
static char guid[39];
/* NOTE: see RFC 4122 for more information about GUID/UUID
* structure. The first fields fields are historically big
* endian numbers, dating from Apollo mc68000 workstations.
*/
sprintf(guid, "{%02x%02x%02x%02x"
"-%02x%02x"
"-%02x%02x"
"-%02x%02x"
"-%02x%02x%02x%02x%02x%02x}",
buf[3], buf[2], buf[1], buf[0],
buf[5], buf[4],
buf[7], buf[6],
buf[8], buf[9],
buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
return guid;
}
/* ---------------------------------------------------------------------- */
static void dump_bytes(const unsigned char *buf, unsigned int len)
{
unsigned int i;
for (i = 0; i < len; i++)
printf(" %02x", buf[i]);
printf("\n");
}
static void dump_junk(const unsigned char *buf, const char *indent, unsigned int len)
{
unsigned int i;
if (buf[0] <= len)
return;
printf("%sjunk at descriptor end:", indent);
for (i = len; i < buf[0]; i++)
printf(" %02x", buf[i]);
printf("\n");
}
/*
* General config descriptor dump
*/
static void dump_device(
libusb_device *dev,
struct libusb_device_descriptor *descriptor
)
{
char vendor[128], product[128];
char cls[128], subcls[128], proto[128];
char mfg[128] = {0}, prod[128] = {0}, serial[128] = {0};
char sysfs_name[PATH_MAX];
const char *negotiated_speed;
get_vendor_product_with_fallback(vendor, sizeof(vendor),
product, sizeof(product), dev);
get_class_string(cls, sizeof(cls), descriptor->bDeviceClass);
get_subclass_string(subcls, sizeof(subcls),
descriptor->bDeviceClass, descriptor->bDeviceSubClass);
get_protocol_string(proto, sizeof(proto), descriptor->bDeviceClass,
descriptor->bDeviceSubClass, descriptor->bDeviceProtocol);
if (get_sysfs_name(sysfs_name, sizeof(sysfs_name), dev) >= 0) {
read_sysfs_prop(mfg, sizeof(mfg), sysfs_name, "manufacturer");
read_sysfs_prop(prod, sizeof(prod), sysfs_name, "product");
read_sysfs_prop(serial, sizeof(serial), sysfs_name, "serial");
}
switch (libusb_get_device_speed(dev)) {
case LIBUSB_SPEED_LOW:
negotiated_speed = "Low Speed (1Mbps)";
break;
case LIBUSB_SPEED_FULL:
negotiated_speed = "Full Speed (12Mbps)";
break;
case LIBUSB_SPEED_HIGH:
negotiated_speed = "High Speed (480Mbps)";
break;
case LIBUSB_SPEED_SUPER:
negotiated_speed = "SuperSpeed (5Gbps)";
break;
case LIBUSB_SPEED_SUPER_PLUS:
negotiated_speed = "SuperSpeed+ (10Gbps)";
break;
case LIBUSB_SPEED_SUPER_PLUS_X2:
negotiated_speed = "SuperSpeed++ (20Gbps)";
break;
case LIBUSB_SPEED_UNKNOWN:
default:
negotiated_speed = "Unknown";
break;
}
printf("Negotiated speed: %s\n", negotiated_speed);
printf("Device Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bcdUSB %2x.%02x\n"
" bDeviceClass %5u %s\n"
" bDeviceSubClass %5u %s\n"
" bDeviceProtocol %5u %s\n"
" bMaxPacketSize0 %5u\n"
" idVendor 0x%04x %s\n"
" idProduct 0x%04x %s\n"
" bcdDevice %2x.%02x\n"
" iManufacturer %5u %s\n"
" iProduct %5u %s\n"
" iSerial %5u %s\n"
" bNumConfigurations %5u\n",
descriptor->bLength, descriptor->bDescriptorType,
descriptor->bcdUSB >> 8, descriptor->bcdUSB & 0xff,
descriptor->bDeviceClass, cls,
descriptor->bDeviceSubClass, subcls,
descriptor->bDeviceProtocol, proto,
descriptor->bMaxPacketSize0,
descriptor->idVendor, vendor, descriptor->idProduct, product,
descriptor->bcdDevice >> 8, descriptor->bcdDevice & 0xff,
descriptor->iManufacturer, mfg,
descriptor->iProduct, prod,
descriptor->iSerialNumber, serial,
descriptor->bNumConfigurations);
}
static void dump_wire_adapter(const unsigned char *buf)
{
printf(" Wire Adapter Class Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bcdWAVersion %2x.%02x\n"
" bNumPorts %5u\n"
" bmAttributes %5u\n"
" wNumRPRipes %5u\n"
" wRPipeMaxBlock %5u\n"
" bRPipeBlockSize %5u\n"
" bPwrOn2PwrGood %5u\n"
" bNumMMCIEs %5u\n"
" DeviceRemovable %5u\n",
buf[0], buf[1], buf[3], buf[2], buf[4], buf[5],
(buf[6] | buf[7] << 8),
(buf[8] | buf[9] << 8),
buf[10], buf[11], buf[12], buf[13]);
}
static void dump_rc_interface(const unsigned char *buf)
{
printf(" Radio Control Interface Class Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bcdRCIVersion %2x.%02x\n",
buf[0], buf[1], buf[3], buf[2]);
}
static void dump_security(const unsigned char *buf)
{
printf(" Security Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" wTotalLength 0x%04x\n"
" bNumEncryptionTypes %5u\n",
buf[0], buf[1], (buf[3] << 8 | buf[2]), buf[4]);
}
static void dump_encryption_type(const unsigned char *buf)
{
int b_encryption_type = buf[2] & 0x4;
printf(" Encryption Type Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bEncryptionType %5u %s\n"
" bEncryptionValue %5u\n"
" bAuthKeyIndex %5u\n",
buf[0], buf[1], buf[2],
encryption_type[b_encryption_type], buf[3], buf[4]);
}
static void dump_association(libusb_device_handle *dev, const unsigned char *buf)
{
char cls[128], subcls[128], proto[128];
char *func;
get_class_string(cls, sizeof(cls), buf[4]);
get_subclass_string(subcls, sizeof(subcls), buf[4], buf[5]);
get_protocol_string(proto, sizeof(proto), buf[4], buf[5], buf[6]);
func = get_dev_string(dev, buf[7]);
printf(" Interface Association:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bFirstInterface %5u\n"
" bInterfaceCount %5u\n"
" bFunctionClass %5u %s\n"
" bFunctionSubClass %5u %s\n"
" bFunctionProtocol %5u %s\n"
" iFunction %5u %s\n",
buf[0], buf[1],
buf[2], buf[3],
buf[4], cls,
buf[5], subcls,
buf[6], proto,
buf[7], func);
free(func);
}
static void dump_config(libusb_device_handle *dev, struct libusb_config_descriptor *config, unsigned speed)
{
char *cfg;
int i;
cfg = get_dev_string(dev, config->iConfiguration);
printf(" Configuration Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" wTotalLength 0x%04x\n"
" bNumInterfaces %5u\n"
" bConfigurationValue %5u\n"
" iConfiguration %5u %s\n"
" bmAttributes 0x%02x\n",
config->bLength, config->bDescriptorType,
le16_to_cpu(config->wTotalLength),
config->bNumInterfaces, config->bConfigurationValue,
config->iConfiguration,
cfg, config->bmAttributes);
free(cfg);
if (!(config->bmAttributes & 0x80))
printf(" (Missing must-be-set bit!)\n");
if (config->bmAttributes & 0x40)
printf(" Self Powered\n");
else
printf(" (Bus Powered)\n");
if (config->bmAttributes & 0x20)
printf(" Remote Wakeup\n");
if (config->bmAttributes & 0x10)
printf(" Battery Powered\n");
printf(" MaxPower %5umA\n", config->MaxPower * (speed >= 0x0300 ? 8 : 2));
/* avoid re-ordering or hiding descriptors for display */
if (config->extra_length) {
int size = config->extra_length;
const unsigned char *buf = config->extra;
while (size >= 2) {
if (buf[0] < 2) {
dump_junk(buf, " ", size);
break;
}
switch (buf[1]) {
case USB_DT_OTG:
/* handled separately */
break;
case USB_DT_INTERFACE_ASSOCIATION:
dump_association(dev, buf);
break;
case USB_DT_SECURITY:
dump_security(buf);
break;
case USB_DT_ENCRYPTION_TYPE:
dump_encryption_type(buf);
break;
default:
/* often a misplaced class descriptor */
printf(" ** UNRECOGNIZED: ");
dump_bytes(buf, buf[0]);
break;
}
size -= buf[0];
buf += buf[0];
}
}
for (i = 0 ; i < config->bNumInterfaces ; i++)
dump_interface(dev, &config->interface[i]);
}
static void dump_altsetting(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface)
{
char cls[128], subcls[128], proto[128];
char *ifstr;
const unsigned char *buf;
unsigned size, i;
get_class_string(cls, sizeof(cls), interface->bInterfaceClass);
get_subclass_string(subcls, sizeof(subcls), interface->bInterfaceClass, interface->bInterfaceSubClass);
get_protocol_string(proto, sizeof(proto), interface->bInterfaceClass, interface->bInterfaceSubClass, interface->bInterfaceProtocol);
ifstr = get_dev_string(dev, interface->iInterface);
printf(" Interface Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bInterfaceNumber %5u\n"
" bAlternateSetting %5u\n"
" bNumEndpoints %5u\n"
" bInterfaceClass %5u %s\n"
" bInterfaceSubClass %5u %s\n"
" bInterfaceProtocol %5u %s\n"
" iInterface %5u %s\n",
interface->bLength, interface->bDescriptorType, interface->bInterfaceNumber,
interface->bAlternateSetting, interface->bNumEndpoints, interface->bInterfaceClass, cls,
interface->bInterfaceSubClass, subcls, interface->bInterfaceProtocol, proto,
interface->iInterface, ifstr);
free(ifstr);
/* avoid re-ordering or hiding descriptors for display */
if (interface->extra_length) {
size = interface->extra_length;
buf = interface->extra;
while (size >= 2 * sizeof(uint8_t)) {
if (buf[0] < 2) {
dump_junk(buf, " ", size);
break;
}
switch (buf[1]) {
/* This is the polite way to provide class specific
* descriptors: explicitly tagged, using common class
* spec conventions.
*/
case USB_DT_CS_DEVICE:
case USB_DT_CS_INTERFACE:
switch (interface->bInterfaceClass) {
case LIBUSB_CLASS_AUDIO:
switch (interface->bInterfaceSubClass) {
case 1:
dump_audiocontrol_interface(dev, buf, interface->bInterfaceProtocol);
break;
case 2:
dump_audiostreaming_interface(dev, buf, interface->bInterfaceProtocol);
break;
case 3:
dump_midistreaming_interface(dev, buf);
break;
default:
goto dump;
}
break;
case LIBUSB_CLASS_COMM:
dump_comm_descriptor(dev, buf,
" ");
break;
case USB_CLASS_VIDEO:
switch (interface->bInterfaceSubClass) {
case 1:
dump_videocontrol_interface(dev, buf, interface->bInterfaceProtocol);
break;
case 2:
dump_videostreaming_interface(buf);
break;
default:
goto dump;
}
break;
case USB_CLASS_APPLICATION:
switch (interface->bInterfaceSubClass) {
case 1:
dump_dfu_interface(buf);
break;
default:
goto dump;
}
break;
case LIBUSB_CLASS_HID:
dump_hid_device(dev, interface, buf);
break;
case LIBUSB_CLASS_PRINTER:
dump_printer_device(dev, interface, buf);
break;
case USB_CLASS_CCID:
dump_ccid_device(buf);
break;
default:
goto dump;
}
break;
/* This is the ugly way: implicitly tagged,
* each class could redefine the type IDs.
*/
default:
switch (interface->bInterfaceClass) {
case LIBUSB_CLASS_HID:
dump_hid_device(dev, interface, buf);
break;
case USB_CLASS_CCID:
dump_ccid_device(buf);
break;
case 0xe0: /* wireless */
switch (interface->bInterfaceSubClass) {
case 1:
switch (interface->bInterfaceProtocol) {
case 2:
dump_rc_interface(buf);
break;
default:
goto dump;
}
break;
case 2:
dump_wire_adapter(buf);
break;
default:
goto dump;
}
break;
case LIBUSB_CLASS_AUDIO:
switch (buf[1]) {
/* MISPLACED DESCRIPTOR */
case USB_DT_CS_ENDPOINT:
switch (interface->bInterfaceSubClass) {
case 2:
dump_audiostreaming_endpoint(dev, buf, interface->bInterfaceProtocol);
break;
default:
goto dump;
}
break;
default:
goto dump;
}
break;
default:
/* ... not everything is class-specific */
switch (buf[1]) {
case USB_DT_OTG:
/* handled separately */
break;
case USB_DT_INTERFACE_ASSOCIATION:
dump_association(dev, buf);
break;
default:
dump:
/* often a misplaced class descriptor */
printf(" ** UNRECOGNIZED: ");
dump_bytes(buf, buf[0]);
break;
}
}
}
size -= buf[0];
buf += buf[0];
}
}
for (i = 0 ; i < interface->bNumEndpoints ; i++)
dump_endpoint(dev, interface, &interface->endpoint[i]);
}
static void dump_interface(libusb_device_handle *dev, const struct libusb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
dump_altsetting(dev, &interface->altsetting[i]);
}
static void dump_pipe_desc(const unsigned char *buf)
{
static const char *pipe_name[] = {
"Reserved",
"Command pipe",
"Status pipe",
"Data-in pipe",
"Data-out pipe",
[5 ... 0xDF] = "Reserved",
[0xE0 ... 0xEF] = "Vendor specific",
[0xF0 ... 0xFF] = "Reserved",
};
if (buf[0] == 4 && buf[1] == 0x24) {
printf(" %s (0x%02x)\n", pipe_name[buf[2]], buf[2]);
} else {
printf(" INTERFACE CLASS: ");
dump_bytes(buf, buf[0]);
}
}
static void dump_endpoint(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const struct libusb_endpoint_descriptor *endpoint)
{
static const char * const typeattr[] = {
"Control",
"Isochronous",
"Bulk",
"Interrupt"
};
static const char * const syncattr[] = {
"None",
"Asynchronous",
"Adaptive",
"Synchronous"
};
static const char * const usage[] = {
"Data",
"Feedback",
"Implicit feedback Data",
"(reserved)"
};
static const char * const hb[] = { "1x", "2x", "3x", "(?\?)" };
const unsigned char *buf;
unsigned size;
unsigned wmax = le16_to_cpu(endpoint->wMaxPacketSize);
printf(" Endpoint Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bEndpointAddress 0x%02x EP %u %s\n"
" bmAttributes %5u\n"
" Transfer Type %s\n"
" Synch Type %s\n"
" Usage Type %s\n"
" wMaxPacketSize 0x%04x %s %d bytes\n"
" bInterval %5u\n",
endpoint->bLength,
endpoint->bDescriptorType,
endpoint->bEndpointAddress,
endpoint->bEndpointAddress & 0x0f,
(endpoint->bEndpointAddress & 0x80) ? "IN" : "OUT",
endpoint->bmAttributes,
typeattr[endpoint->bmAttributes & 3],
syncattr[(endpoint->bmAttributes >> 2) & 3],
usage[(endpoint->bmAttributes >> 4) & 3],
wmax, hb[(wmax >> 11) & 3], wmax & 0x7ff,
endpoint->bInterval);
/* only for audio endpoints */
if (endpoint->bLength == 9)
printf(" bRefresh %5u\n"
" bSynchAddress %5u\n",
endpoint->bRefresh, endpoint->bSynchAddress);
/* avoid re-ordering or hiding descriptors for display */
if (endpoint->extra_length) {
size = endpoint->extra_length;
buf = endpoint->extra;
while (size >= 2 * sizeof(uint8_t)) {
if (buf[0] < 2) {
dump_junk(buf, " ", size);
break;
}
switch (buf[1]) {
case USB_DT_CS_ENDPOINT:
if (interface->bInterfaceClass == 1 && interface->bInterfaceSubClass == 2)
dump_audiostreaming_endpoint(dev, buf, interface->bInterfaceProtocol);
else if (interface->bInterfaceClass == 1 && interface->bInterfaceSubClass == 3)
dump_midistreaming_endpoint(buf);
break;
case USB_DT_CS_INTERFACE:
/* MISPLACED DESCRIPTOR ... less indent */
switch (interface->bInterfaceClass) {
case LIBUSB_CLASS_COMM:
case LIBUSB_CLASS_DATA: /* comm data */
dump_comm_descriptor(dev, buf,
" ");
break;
case LIBUSB_CLASS_MASS_STORAGE:
dump_pipe_desc(buf);
break;
default:
printf(" INTERFACE CLASS: ");
dump_bytes(buf, buf[0]);
}
break;
case USB_DT_CS_DEVICE:
/* MISPLACED DESCRIPTOR ... less indent */
switch (interface->bInterfaceClass) {
case USB_CLASS_CCID:
dump_ccid_device(buf);
break;
default:
printf(" DEVICE CLASS: ");
dump_bytes(buf, buf[0]);
}
break;
case USB_DT_OTG:
/* handled separately */
break;
case USB_DT_INTERFACE_ASSOCIATION:
dump_association(dev, buf);
break;
case USB_DT_SS_ENDPOINT_COMP:
printf(" bMaxBurst %15u\n", buf[2]);
/* Print bulk streams info or isoc "Mult" */
if ((endpoint->bmAttributes & 3) == 2 &&
(buf[3] & 0x1f))
printf(" MaxStreams %14u\n",
(unsigned) 1 << buf[3]);
if ((endpoint->bmAttributes & 3) == 1 &&
(buf[3] & 0x3))
printf(" Mult %20u\n",
buf[3] & 0x3);
break;
default:
/* often a misplaced class descriptor */
printf(" ** UNRECOGNIZED: ");
dump_bytes(buf, buf[0]);
break;
}
size -= buf[0];
buf += buf[0];
}
}
}
static void dump_unit(unsigned int data, unsigned int len)
{
char *systems[5] = { "None", "SI Linear", "SI Rotation",
"English Linear", "English Rotation" };
char *units[5][8] = {
{ "None", "None", "None", "None", "None",
"None", "None", "None" },
{ "None", "Centimeter", "Gram", "Seconds", "Kelvin",
"Ampere", "Candela", "None" },
{ "None", "Radians", "Gram", "Seconds", "Kelvin",
"Ampere", "Candela", "None" },
{ "None", "Inch", "Slug", "Seconds", "Fahrenheit",
"Ampere", "Candela", "None" },
{ "None", "Degrees", "Slug", "Seconds", "Fahrenheit",
"Ampere", "Candela", "None" },
};
unsigned int i;
unsigned int sys;
int earlier_unit = 0;
/* First nibble tells us which system we're in. */
sys = data & 0xf;
data >>= 4;
if (sys > 4) {
if (sys == 0xf)
printf("System: Vendor defined, Unit: (unknown)\n");
else
printf("System: Reserved, Unit: (unknown)\n");
return;
} else {
printf("System: %s, Unit: ", systems[sys]);
}
for (i = 1 ; i < len * 2 ; i++) {
char nibble = data & 0xf;
data >>= 4;
if (nibble != 0) {
if (earlier_unit++ > 0)
printf("*");
printf("%s", units[sys][i]);
if (nibble != 1) {
/* This is a _signed_ nibble(!) */
int val = nibble & 0x7;
if (nibble & 0x08)
val = -((0x7 & ~val) + 1);
printf("^%d", val);
}
}
}
if (earlier_unit == 0)
printf("(None)");
printf("\n");
}
/* ---------------------------------------------------------------------- */
/*
* Audio Class descriptor dump
*/
static void dump_audio_subtype(libusb_device_handle *dev,
const char *name,
const struct desc * const desc[3],
const unsigned char *buf,
int protocol,
unsigned int indent)
{
static const char * const strings[] = { "UAC1", "UAC2", "UAC3" };
unsigned int idx = 0;
switch (protocol) {
case USB_AUDIO_CLASS_2: idx = 1; break;
case USB_AUDIO_CLASS_3: idx = 2; break;
}
printf("(%s)\n", name);
if (desc[idx] == NULL) {
printf("%*sWarning: %s descriptors are illegal for %s\n",
indent * 2, "", name, strings[idx]);
return;
}
/* Skip the first three bytes; those common fields have already
* been dumped. */
desc_dump(dev, desc[idx], buf + 3, buf[0] - 3, indent);
}
/* USB Audio Class subtypes */
enum uac_interface_subtype {
UAC_INTERFACE_SUBTYPE_AC_DESCRIPTOR_UNDEFINED = 0x00,
UAC_INTERFACE_SUBTYPE_HEADER = 0x01,
UAC_INTERFACE_SUBTYPE_INPUT_TERMINAL = 0x02,
UAC_INTERFACE_SUBTYPE_OUTPUT_TERMINAL = 0x03,
UAC_INTERFACE_SUBTYPE_EXTENDED_TERMINAL = 0x04,
UAC_INTERFACE_SUBTYPE_MIXER_UNIT = 0x05,
UAC_INTERFACE_SUBTYPE_SELECTOR_UNIT = 0x06,
UAC_INTERFACE_SUBTYPE_FEATURE_UNIT = 0x07,
UAC_INTERFACE_SUBTYPE_EFFECT_UNIT = 0x08,
UAC_INTERFACE_SUBTYPE_PROCESSING_UNIT = 0x09,
UAC_INTERFACE_SUBTYPE_EXTENSION_UNIT = 0x0a,
UAC_INTERFACE_SUBTYPE_CLOCK_SOURCE = 0x0b,
UAC_INTERFACE_SUBTYPE_CLOCK_SELECTOR = 0x0c,
UAC_INTERFACE_SUBTYPE_CLOCK_MULTIPLIER = 0x0d,
UAC_INTERFACE_SUBTYPE_SAMPLE_RATE_CONVERTER = 0x0e,
UAC_INTERFACE_SUBTYPE_CONNECTORS = 0x0f,
UAC_INTERFACE_SUBTYPE_POWER_DOMAIN = 0x10,
};
/*
* UAC1, UAC2, and UAC3 define bDescriptorSubtype differently for the
* AudioControl interface, so we need to do some ugly remapping:
*
* val | UAC1 | UAC2 | UAC3
* -----|-----------------|-----------------------|---------------------
* 0x00 | AC UNDEFINED | AC UNDEFINED | AC UNDEFINED
* 0x01 | HEADER | HEADER | HEADER
* 0x02 | INPUT_TERMINAL | INPUT_TERMINAL | INPUT_TERMINAL
* 0x03 | OUTPUT_TERMINAL | OUTPUT_TERMINAL | OUTPUT_TERMINAL
* 0x04 | MIXER_UNIT | MIXER_UNIT | EXTENDED_TERMINAL
* 0x05 | SELECTOR_UNIT | SELECTOR_UNIT | MIXER_UNIT
* 0x06 | FEATURE_UNIT | FEATURE_UNIT | SELECTOR_UNIT
* 0x07 | PROCESSING_UNIT | EFFECT_UNIT | FEATURE_UNIT
* 0x08 | EXTENSION_UNIT | PROCESSING_UNIT | EFFECT_UNIT
* 0x09 | - | EXTENSION_UNIT | PROCESSING_UNIT
* 0x0a | - | CLOCK_SOURCE | EXTENSION_UNIT
* 0x0b | - | CLOCK_SELECTOR | CLOCK_SOURCE
* 0x0c | - | CLOCK_MULTIPLIER | CLOCK_SELECTOR
* 0x0d | - | SAMPLE_RATE_CONVERTER | CLOCK_MULTIPLIER
* 0x0e | - | - | SAMPLE_RATE_CONVERTER
* 0x0f | - | - | CONNECTORS
* 0x10 | - | - | POWER_DOMAIN
*/
static enum uac_interface_subtype get_uac_interface_subtype(unsigned char c, int protocol)
{
switch (protocol) {
case USB_AUDIO_CLASS_1:
switch(c) {
case 0x04: return UAC_INTERFACE_SUBTYPE_MIXER_UNIT;
case 0x05: return UAC_INTERFACE_SUBTYPE_SELECTOR_UNIT;
case 0x06: return UAC_INTERFACE_SUBTYPE_FEATURE_UNIT;
case 0x07: return UAC_INTERFACE_SUBTYPE_PROCESSING_UNIT;
case 0x08: return UAC_INTERFACE_SUBTYPE_EXTENSION_UNIT;
}
break;
case USB_AUDIO_CLASS_2:
switch(c) {
case 0x04: return UAC_INTERFACE_SUBTYPE_MIXER_UNIT;
case 0x05: return UAC_INTERFACE_SUBTYPE_SELECTOR_UNIT;
case 0x06: return UAC_INTERFACE_SUBTYPE_FEATURE_UNIT;
case 0x07: return UAC_INTERFACE_SUBTYPE_EFFECT_UNIT;
case 0x08: return UAC_INTERFACE_SUBTYPE_PROCESSING_UNIT;
case 0x09: return UAC_INTERFACE_SUBTYPE_EXTENSION_UNIT;
case 0x0a: return UAC_INTERFACE_SUBTYPE_CLOCK_SOURCE;
case 0x0b: return UAC_INTERFACE_SUBTYPE_CLOCK_SELECTOR;
case 0x0c: return UAC_INTERFACE_SUBTYPE_CLOCK_MULTIPLIER;
case 0x0d: return UAC_INTERFACE_SUBTYPE_SAMPLE_RATE_CONVERTER;
}
break;
case USB_AUDIO_CLASS_3:
/* No mapping required. */
break;
default:
/* Unknown protocol */
break;
}