-
Notifications
You must be signed in to change notification settings - Fork 3
/
lsusb.c
4043 lines (3684 loc) · 121 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
863
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
/*****************************************************************************/
/*
* lsusb.c -- lspci like utility for the USB bus
*
* Copyright (C) 1999-2001, 2003
* Thomas Sailer ([email protected])
* Copyright (C) 2003-2005 David Brownell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*****************************************************************************/
#include "config.h"
#include <sys/types.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>
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif
#include <libusb.h>
#include <unistd.h>
#include "lsusb.h"
#include "names.h"
#include "usbmisc.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
/* 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
#define VERBLEVEL_DEFAULT 0 /* 0 gives lspci behaviour; 1, lsusb-0.9 */
#define CTRL_RETRIES 2
#define CTRL_TIMEOUT (5*1000) /* milliseconds */
#define HUB_STATUS_BYTELEN 3 /* max 3 bytes status = hub 23 ports */
static const char procbususb[] = "/proc/bus/usb";
static 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 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);
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_audiostreaming_endpoint(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 unsigned int convert_le_u32 (const unsigned char *buf)
{
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}
/* ---------------------------------------------------------------------- */
/* workaround libusb API goofs: "byte" should never be sign extended;
* using "char" is trouble. Likewise, sizes should never be negative.
*/
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);
if (ret < 0)
return -ret;
else
return ret;
}
#define usb_control_msg typesafe_control_msg
static int get_protocol_string(char *buf, size_t size, u_int8_t cls, u_int8_t subcls, u_int8_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_audioterminal_string(char *buf, size_t size, u_int16_t termt)
{
const char *cp;
if (size < 1)
return 0;
*buf = 0;
if (!(cp = names_audioterminal(termt)))
return 0;
return snprintf(buf, size, "%s", cp);
}
static int get_videoterminal_string(char *buf, size_t size, u_int16_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, "{xxxx"
"-xx"
"-xx"
"-xx"
"-xxxxxx}",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5],
buf[6], buf[7],
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(" x", 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(" x", buf[i]);
printf("\n");
}
/*
* General config descriptor dump
*/
static void dump_device(
libusb_device_handle *dev,
struct libusb_device_descriptor *descriptor
)
{
char vendor[128], product[128];
char cls[128], subcls[128], proto[128];
char *mfg, *prod, *serial;
get_vendor_string(vendor, sizeof(vendor), descriptor->idVendor);
get_product_string(product, sizeof(product),
descriptor->idVendor, descriptor->idProduct);
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);
mfg = get_dev_string(dev, descriptor->iManufacturer);
prod = get_dev_string(dev, descriptor->iProduct);
serial = get_dev_string(dev, descriptor->iSerialNumber);
printf("Device Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bcdUSB %2x.x\n"
" bDeviceClass %5u %s\n"
" bDeviceSubClass %5u %s\n"
" bDeviceProtocol %5u %s\n"
" bMaxPacketSize0 %5u\n"
" idVendor 0xx %s\n"
" idProduct 0xx %s\n"
" bcdDevice %2x.x\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);
free(mfg);
free(prod);
free(serial);
}
static void dump_wire_adapter(const unsigned char *buf)
{
printf(" Wire Adapter Class Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bcdWAVersion %2x.x\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.x\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 %5u\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)
{
char *cfg;
int i;
cfg = get_dev_string(dev, config->iConfiguration);
printf(" Configuration Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" wTotalLength %5u\n"
" bNumInterfaces %5u\n"
" bConfigurationValue %5u\n"
" iConfiguration %5u %s\n"
" bmAttributes 0xx\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 * 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(u_int8_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);
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 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(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 (0xx)\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 0xx EP %u %s\n"
" bmAttributes %5u\n"
" Transfer Type %s\n"
" Synch Type %s\n"
" Usage Type %s\n"
" wMaxPacketSize 0xx %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(u_int8_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(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 u\n", buf[2]);
/* Print bulk streams info or isoc "Mult" */
if ((endpoint->bmAttributes & 3) == 2 &&
(buf[3] & 0x1f))
printf(" MaxStreams u\n",
(unsigned) 1 << buf[3]);
if ((endpoint->bmAttributes & 3) == 1 &&
(buf[3] & 0x3))
printf(" Mult u\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
*/
struct bmcontrol {
const char *name;
unsigned int bit;
};
static const struct bmcontrol uac2_interface_header_bmcontrols[] = {
{ "Latency control", 0 },
{ NULL }
};
static const struct bmcontrol uac_fu_bmcontrols[] = {
{ "Mute", 0 },
{ "Volume", 1 },
{ "Bass", 2 },
{ "Mid", 3 },
{ "Treble", 4 },
{ "Graphic Equalizer", 5 },
{ "Automatic Gain", 6 },
{ "Delay", 7 },
{ "Bass Boost", 8 },
{ "Loudness", 9 },
{ "Input gain", 10 },
{ "Input gain pad", 11 },
{ "Phase inverter", 12 },
{ NULL }
};
static const struct bmcontrol uac2_input_term_bmcontrols[] = {
{ "Copy Protect", 0 },
{ "Connector", 1 },
{ "Overload", 2 },
{ "Cluster", 3 },
{ "Underflow", 4 },
{ "Overflow", 5 },
{ NULL }
};
static const struct bmcontrol uac2_output_term_bmcontrols[] = {
{ "Copy Protect", 0 },
{ "Connector", 1 },
{ "Overload", 2 },
{ "Underflow", 3 },
{ "Overflow", 4 },
{ NULL }
};
static const struct bmcontrol uac2_mixer_unit_bmcontrols[] = {
{ "Cluster", 0 },
{ "Underflow", 1 },
{ "Overflow", 2 },
{ NULL }
};
static const struct bmcontrol uac2_extension_unit_bmcontrols[] = {
{ "Enable", 0 },
{ "Cluster", 1 },
{ "Underflow", 2 },
{ "Overflow", 3 },
{ NULL }
};
static const struct bmcontrol uac2_clock_source_bmcontrols[] = {
{ "Clock Frequency", 0 },
{ "Clock Validity", 1 },
{ NULL }
};
static const struct bmcontrol uac2_clock_selector_bmcontrols[] = {
{ "Clock Selector", 0 },
{ NULL }
};
static const struct bmcontrol uac2_clock_multiplier_bmcontrols[] = {
{ "Clock Numerator", 0 },
{ "Clock Denominator", 1 },
{ NULL }
};
static const struct bmcontrol uac2_selector_bmcontrols[] = {
{ "Selector", 0 },
{ NULL }
};
static void dump_audio_bmcontrols(const char *prefix, int bmcontrols, const struct bmcontrol *list, int protocol)
{
while (list->name) {
switch (protocol) {
case USB_AUDIO_CLASS_1:
if (bmcontrols & (1 << list->bit))
printf("%s%s Control\n", prefix, list->name);
break;
case USB_AUDIO_CLASS_2: {
const char * const ctrl_type[] = { "read-only", "ILLEGAL (0b10)", "read/write" };
int ctrl = (bmcontrols >> (list->bit * 2)) & 0x3;
if (ctrl)
printf("%s%s Control (%s)\n", prefix, list->name, ctrl_type[ctrl-1]);
break;
}
} /* switch */
list ;
}
}
static const char * const chconfig_uac2[] = {
"Front Left (FL)", "Front Right (FR)", "Front Center (FC)", "Low Frequency Effects (LFE)",
"Back Left (BL)", "Back Right (BR)", "Front Left of Center (FLC)", "Front Right of Center (FRC)", "Back Center (BC)",
"Side Left (SL)", "Side Right (SR)",
"Top Center (TC)", "Top Front Left (TFL)", "Top Front Center (TFC)", "Top Front Right (TFR)", "Top Back Left (TBL)",
"Top Back Center (TBC)", "Top Back Right (TBR)", "Top Front Left of Center (TFLC)", "Top Front Right of Center (TFRC)",
"Left Low Frequency Effects (LLFE)", "Right Low Frequency Effects (RLFE)",
"Top Side Left (TSL)", "Top Side Right (TSR)", "Bottom Center (BC)",
"Back Left of Center (BLC)", "Back Right of Center (BRC)"
};
static void dump_audiocontrol_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol)
{
static const char * const chconfig[] = {
"Left Front (L)", "Right Front (R)", "Center Front (C)", "Low Freqency Enhancement (LFE)",
"Left Surround (LS)", "Right Surround (RS)", "Left of Center (LC)", "Right of Center (RC)",
"Surround (S)", "Side Left (SL)", "Side Right (SR)", "Top (T)"
};
static const char * const clock_source_attrs[] = {
"External", "Internal fixed", "Internal variable", "Internal programmable"
};
unsigned int i, chcfg, j, k, N, termt, subtype;
char *chnames = NULL, *term = NULL, termts[128];
if (buf[1] != USB_DT_CS_INTERFACE)
printf(" Warning: Invalid descriptor\n");
else if (buf[0] < 3)
printf(" Warning: Descriptor too short\n");
printf(" AudioControl Interface Descriptor:\n"
" bLength %5u\n"
" bDescriptorType %5u\n"
" bDescriptorSubtype %5u ",
buf[0], buf[1], buf[2]);
/*
* This is an utter mess - UAC2 defines some bDescriptorSubtype differently, so we have to do some ugly remapping here:
*
* bDescriptorSubtype UAC1 UAC2
* ------------------------------------------------------------------------
* 0x07 PROCESSING_UNIT EFFECT_UNIT
* 0x08 EXTENSION_UNIT PROCESSING_UNIT
* 0x09 - EXTENSION_UNIT
*