summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-usb-hub.c
blob: 74ef6a63e03d603bb7121673b65b2ff4c6cdbd5a (plain)
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
/* packet-usb-hub.c
 * Routines for USB HUB dissection
 * Copyright 2009, Marton Nemeth <nm127@freemail.hu>
 *
 * USB HUB Specification can be found in the Universal Serial Bus
 * Specification 2.0, Chapter 11 Hub Specification.
 * http://www.usb.org/developers/docs/usb_20_052709.zip
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include "packet-usb.h"

void proto_register_usb_hub(void);
void proto_reg_handoff_usb_hub(void);

static dissector_handle_t usb_hub_control_handle;

/* protocols and header fields */
static int proto_usb_hub;

/* USB 2.0, Chapter 11.24.2 Class-Specific Requests */
static int hf_usb_hub_request;
static int hf_usb_hub_value;
static int hf_usb_hub_index;
static int hf_usb_hub_length;

static int hf_usb_hub_hub_feature_selector;
static int hf_usb_hub_port_feature_selector;
static int hf_usb_hub_dev_addr;
static int hf_usb_hub_ep_num;
static int hf_usb_hub_descriptor_type;
static int hf_usb_hub_descriptor_index;
static int hf_usb_hub_zero;
static int hf_usb_hub_tt_flags;
static int hf_usb_hub_tt_port;
static int hf_usb_hub_tt_state_length;
static int hf_usb_hub_port;
static int hf_usb_hub_port_selector;
static int hf_usb_hub_port_status;
static int hf_usb_hub_port_change;
static int hf_usb_hub_port_status_connection;
static int hf_usb_hub_port_status_enable;
static int hf_usb_hub_port_status_suspend;
static int hf_usb_hub_port_status_overcurrent;
static int hf_usb_hub_port_status_reset;
static int hf_usb_hub_port_status_power;
static int hf_usb_hub_port_status_low_speed;
static int hf_usb_hub_port_status_high_speed;
static int hf_usb_hub_port_status_test;
static int hf_usb_hub_port_status_indicator;
static int hf_usb_hub_port_change_connection;
static int hf_usb_hub_port_change_enable;
static int hf_usb_hub_port_change_suspend;
static int hf_usb_hub_port_change_overcurrent;
static int hf_usb_hub_port_change_reset;
static int hf_usb_hub_descriptor_length;

static int ett_usb_hub_wValue;
static int ett_usb_hub_wIndex;
static int ett_usb_hub_wLength;
static int ett_usb_hub_port_status;
static int ett_usb_hub_port_change;

/* Table 11-16. Hub Class Request Codes */
#define USB_HUB_REQUEST_GET_STATUS            0
#define USB_HUB_REQUEST_CLEAR_FEATURE         1
#define USB_HUB_REQUEST_SET_FEATURE           3
#define USB_HUB_REQUEST_GET_DESCRIPTOR        6
#define USB_HUB_REQUEST_SET_DESCRIPTOR        7
#define USB_HUB_REQUEST_CLEAR_TT_BUFFER       8
#define USB_HUB_REQUEST_RESET_TT              9
#define USB_HUB_REQUEST_GET_TT_STATE         10
#define USB_HUB_REQUEST_STOP_TT              11

static const value_string setup_request_names_vals[] = {
	{ USB_HUB_REQUEST_GET_STATUS,	   "GET_STATUS" },
	{ USB_HUB_REQUEST_CLEAR_FEATURE,   "CLEAR_FEATURE" },
	{ USB_HUB_REQUEST_SET_FEATURE,	   "SET_FEATURE" },
	{ USB_HUB_REQUEST_GET_DESCRIPTOR,  "GET_DESCRIPTOR" },
	{ USB_HUB_REQUEST_SET_DESCRIPTOR,  "SET_DESCRIPTOR" },
	{ USB_HUB_REQUEST_CLEAR_TT_BUFFER, "CLEAR_TT_BUFFER" },
	{ USB_HUB_REQUEST_GET_TT_STATE,	   "GET_TT_STATE" },
	{ USB_HUB_REQUEST_STOP_TT,	   "STOP_TT" },
	{ 0, NULL }
};


/* Table 11-17 Hub Class Feature Selectors */
#define USB_HUB_FEATURE_C_HUB_LOCAL_POWER      0
#define USB_HUB_FEATURE_C_HUB_OVER_CURRENT     1

#define USB_HUB_FEATURE_PORT_CONNECTION        0
#define USB_HUB_FEATURE_PORT_ENABLE            1
#define USB_HUB_FEATURE_PORT_SUSPEND           2
#define USB_HUB_FEATURE_PORT_OVER_CURRENT      3
#define USB_HUB_FEATURE_PORT_RESET             4
#define USB_HUB_FEATURE_PORT_POWER             8
#define USB_HUB_FEATURE_PORT_LOW_SPEED         9
#define USB_HUB_FEATURE_C_PORT_CONNECTION     16
#define USB_HUB_FEATURE_C_PORT_ENABLE         17
#define USB_HUB_FEATURE_C_PORT_SUSPEND        18
#define USB_HUB_FEATURE_C_PORT_OVER_CURRENT   19
#define USB_HUB_FEATURE_C_PORT_RESET          20
#define USB_HUB_FEATURE_PORT_TEST             21
#define USB_HUB_FEATURE_PORT_INDICATOR        22
/* Table 10-9 Hub Class Feature Selectors (USB3) */
#define USB_HUB_FEATURE_PORT_U1_TIMEOUT       23
#define USB_HUB_FEATURE_PORT_U2_TIMEOUT       24
#define USB_HUB_FEATURE_C_PORT_LINK_STATE     25
#define USB_HUB_FEATURE_C_PORT_CONFIG_ERROR   26
#define USB_HUB_FEATURE_PORT_REMOTE_WAKE_MASK 27
#define USB_HUB_FEATURE_BH_PORT_RESET         28
#define USB_HUB_FEATURE_C_BH_PORT_RESET       29
#define USB_HUB_FEATURE_FORCE_LINKPM_ACCEPT   30

static const value_string hub_class_feature_selectors_recipient_hub_vals[] = {
	{ USB_HUB_FEATURE_C_HUB_LOCAL_POWER,  "C_HUB_LOCAL_POWER" },
	{ USB_HUB_FEATURE_C_HUB_OVER_CURRENT, "C_HUB_OVER_CURRENT" },
	{ 0, NULL }
};

static const value_string hub_class_feature_selectors_recipient_port_vals[] = {
	{ USB_HUB_FEATURE_PORT_CONNECTION,      "PORT_CONNECTION" },
	{ USB_HUB_FEATURE_PORT_ENABLE,          "PORT_ENABLE" },
	{ USB_HUB_FEATURE_PORT_SUSPEND,         "PORT_SUSPEND" },
	{ USB_HUB_FEATURE_PORT_OVER_CURRENT,    "PORT_OVER_CURRENT" },
	{ USB_HUB_FEATURE_PORT_RESET,           "PORT_RESET" },
	{ USB_HUB_FEATURE_PORT_POWER,           "PORT_POWER" },
	{ USB_HUB_FEATURE_PORT_LOW_SPEED,       "PORT_LOW_SPEED" },
	{ USB_HUB_FEATURE_C_PORT_CONNECTION,    "C_PORT_CONNECTION" },
	{ USB_HUB_FEATURE_C_PORT_ENABLE,        "C_PORT_ENABLE" },
	{ USB_HUB_FEATURE_C_PORT_SUSPEND,       "C_PORT_SUSPEND" },
	{ USB_HUB_FEATURE_C_PORT_OVER_CURRENT,  "C_PORT_OVER_CURRENT" },
	{ USB_HUB_FEATURE_C_PORT_RESET,         "C_PORT_RESET" },
	{ USB_HUB_FEATURE_PORT_TEST,            "PORT_TEST" },
	{ USB_HUB_FEATURE_PORT_INDICATOR,       "PORT_INDICATOR" },
	{ USB_HUB_FEATURE_PORT_U1_TIMEOUT,      "PORT_U1_TIMEOUT" },
	{ USB_HUB_FEATURE_PORT_U2_TIMEOUT,      "PORT_U2_TIMEOUT" },
	{ USB_HUB_FEATURE_C_PORT_LINK_STATE,    "C_PORT_LINK_STATE" },
	{ USB_HUB_FEATURE_C_PORT_CONFIG_ERROR,  "C_PORT_CONFIG_ERROR" },
	{ USB_HUB_FEATURE_PORT_REMOTE_WAKE_MASK,"PORT_REMOTE_WAKE_MASK" },
	{ USB_HUB_FEATURE_BH_PORT_RESET,        "BH_PORT_RESET" },
	{ USB_HUB_FEATURE_C_BH_PORT_RESET,      "C_BH_PORT_RESET" },
	{ USB_HUB_FEATURE_FORCE_LINKPM_ACCEPT,  "FORCE_LINKPM_ACCEPT" },
	{ 0, NULL }
};

static const true_false_string hub_port_status_indicator_meaning = {
	"Software-controlled color",
	"Default colors"
};

/* Dissector for ClearHubFeature, Chapter 11.24.2.1 Clear Hub Feature */
static void
dissect_usb_hub_clear_hub_feature(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;
	const char* feature_name;

	feature_name = val_to_str(usb_trans_info->setup.wValue,
								hub_class_feature_selectors_recipient_hub_vals,
								"UNKNOWN (0x%x)");
	col_append_fstr(pinfo->cinfo, COL_INFO, " [Hub: %s]", feature_name);

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_hub_feature_selector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for ClearPortFeature, Chapter 11.24.2.2 Clear Port Feature */
static void
dissect_usb_hub_clear_port_feature(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;
	const char* feature_name;

	feature_name = val_to_str(usb_trans_info->setup.wValue,
								hub_class_feature_selectors_recipient_port_vals,
								"UNKNOWN (0x%x)");
	col_append_fstr(pinfo->cinfo, COL_INFO, " [Port %u: %s]", usb_trans_info->setup.wIndex, feature_name);

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_port_feature_selector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_port, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;
		proto_tree_add_item(subtree, hf_usb_hub_port_selector, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for ClearTTBuffer, Chapter 11.24.2.3 Clear TT Buffer */
static void
dissect_usb_hub_clear_tt_buffer(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_ep_num, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;
		proto_tree_add_item(subtree, hf_usb_hub_dev_addr, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;

		proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_tt_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for GetHubDescriptor, Chapter 11.24.2.5 Get Hub Descriptor */
static void
dissect_usb_hub_get_hub_descriptor(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_index, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for GetHubStatus, Chapter 11.24.2.6 Get Hub Status */
static void
dissect_usb_hub_get_hub_status(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	col_append_str(pinfo->cinfo, COL_INFO, "    [Hub]");

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/* length shall always contain 4 */
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for GetPortStatus, Chapter 11.24.2.7 Get Port Status */
static void
dissect_usb_hub_get_port_status(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	col_append_fstr(pinfo->cinfo, COL_INFO, "    [Port %u]", usb_trans_info->setup.wIndex);

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/* length shall always contain 4 */
		/*offset += 2;*/
	} else {
		static int * const status_fields[] = {
			&hf_usb_hub_port_status_connection,
			&hf_usb_hub_port_status_enable,
			&hf_usb_hub_port_status_suspend,
			&hf_usb_hub_port_status_overcurrent,
			&hf_usb_hub_port_status_reset,
			&hf_usb_hub_port_status_power,
			&hf_usb_hub_port_status_low_speed,
			&hf_usb_hub_port_status_high_speed,
			&hf_usb_hub_port_status_test,
			&hf_usb_hub_port_status_indicator,
			NULL
		};

		static int * const change_fields[] = {
			&hf_usb_hub_port_change_connection,
			&hf_usb_hub_port_change_enable,
			&hf_usb_hub_port_change_suspend,
			&hf_usb_hub_port_change_overcurrent,
			&hf_usb_hub_port_change_reset,
			NULL
		};

		proto_tree_add_bitmask(tree, tvb, offset, hf_usb_hub_port_status,
		                       ett_usb_hub_port_status, status_fields, ENC_LITTLE_ENDIAN);
		offset += 2;
		proto_tree_add_bitmask(tree, tvb, offset, hf_usb_hub_port_change,
		                       ett_usb_hub_port_change, change_fields, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	}
}

/* Dissector for GetTTState, Chapter 11.24.2.8 Get_TT_State */
static void
dissect_usb_hub_get_tt_state(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_tt_flags, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_tt_port, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_tt_state_length, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for ResetTT, Chapter 11.24.2.9 Reset_TT */
static void
dissect_usb_hub_reset_tt(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_tt_port, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for SetHubDescriptor, Chapter 11.24.2.10 Set Hub Descriptor */
static void
dissect_usb_hub_set_hub_descriptor(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_index, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_descriptor_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for StopTT, Chapter 11.24.2.11 Stop TT */
static void
dissect_usb_hub_stop_tt(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info _U_, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_tt_port, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for SetHubFeature, Chapter 11.24.2.12 Set Hub Feature */
static void
dissect_usb_hub_set_hub_feature(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;
	const char* feature_name;
	feature_name = val_to_str(usb_trans_info->setup.wValue,
								hub_class_feature_selectors_recipient_hub_vals,
								"UNKNOWN (0x%x)");
	col_append_fstr(pinfo->cinfo, COL_INFO, "   [Hub: %s]", feature_name);

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_hub_feature_selector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wIndex);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}

/* Dissector for SetPortFeature, Chapter 11.24.2.13 Set Port Feature */
static void
dissect_usb_hub_set_port_feature(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info, urb_info_t *urb _U_)
{
	proto_item *item = NULL;
	proto_tree *subtree = NULL;
	const char* feature_name;

	feature_name = val_to_str(usb_trans_info->setup.wValue,
								hub_class_feature_selectors_recipient_port_vals,
								"UNKNOWN (0x%x)");
	col_append_fstr(pinfo->cinfo, COL_INFO, "   [Port %u: %s]", usb_trans_info->setup.wIndex,
					feature_name);

	if (is_request) {
		item = proto_tree_add_item(tree, hf_usb_hub_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_port_feature_selector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		offset += 2;

		item = proto_tree_add_item(tree, hf_usb_hub_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wValue);
		proto_tree_add_item(subtree, hf_usb_hub_port, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;
		proto_tree_add_item(subtree, hf_usb_hub_port_selector, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset++;

		item = proto_tree_add_item(tree, hf_usb_hub_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		subtree = proto_item_add_subtree(item, ett_usb_hub_wLength);
		proto_tree_add_item(subtree, hf_usb_hub_zero, tvb, offset, 2, ENC_LITTLE_ENDIAN);
		/*offset += 2;*/
	} else {
	}
}


typedef void (*usb_setup_dissector)(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, bool is_request, usb_trans_info_t *usb_trans_info, urb_info_t *urb);

typedef struct _usb_setup_dissector_table_t {
	uint8_t request_type;
	uint8_t request;
	usb_setup_dissector dissector;
} usb_setup_dissector_table_t;


/* USB 2.0, Table 11-15 Hub Class Requests */
static const usb_setup_dissector_table_t setup_dissectors[] = {
	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_DEVICE,
	  USB_HUB_REQUEST_CLEAR_FEATURE,
	  dissect_usb_hub_clear_hub_feature
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_CLEAR_FEATURE,
	  dissect_usb_hub_clear_port_feature
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_CLEAR_TT_BUFFER,
	  dissect_usb_hub_clear_tt_buffer
	},

	{ USB_DIR_IN | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_DEVICE,
	  USB_HUB_REQUEST_GET_DESCRIPTOR,
	  dissect_usb_hub_get_hub_descriptor
	},

	{ USB_DIR_IN | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_DEVICE,
	  USB_HUB_REQUEST_GET_STATUS,
	  dissect_usb_hub_get_hub_status
	},

	{ USB_DIR_IN | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_GET_STATUS,
	  dissect_usb_hub_get_port_status
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_RESET_TT,
	  dissect_usb_hub_reset_tt
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_DEVICE,
	  USB_HUB_REQUEST_SET_DESCRIPTOR,
	  dissect_usb_hub_set_hub_descriptor
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_DEVICE,
	  USB_HUB_REQUEST_SET_FEATURE,
	  dissect_usb_hub_set_hub_feature
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_SET_FEATURE,
	  dissect_usb_hub_set_port_feature
	},

	{ USB_DIR_IN | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_GET_TT_STATE,
	  dissect_usb_hub_get_tt_state
	},

	{ USB_DIR_OUT | (RQT_SETUP_TYPE_CLASS << 5) | RQT_SETUP_RECIPIENT_OTHER,
	  USB_HUB_REQUEST_STOP_TT,
	  dissect_usb_hub_stop_tt
	},

	{ 0, 0, NULL }
};

/* Dissector for USB HUB class-specific control request as defined in
 * USB 2.0, Chapter 11.24.2 Class-specific Requests
 * Returns tvb_captured_length(tvb) if a class specific dissector was found
 * and 0 otherwise.
 */
static int
dissect_usb_hub_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
	bool is_request;
	urb_info_t *urb;
	usb_trans_info_t *usb_trans_info;
	int offset = 0;
	usb_setup_dissector dissector;
	const usb_setup_dissector_table_t *tmp;

	/* Reject the packet if data or usb_trans_info are NULL */
	if (data == NULL || ((urb_info_t *)data)->usb_trans_info == NULL)
		return 0;
	urb = (urb_info_t *)data;
	usb_trans_info = urb->usb_trans_info;

	is_request = (pinfo->srcport==NO_ENDPOINT);

	/* See if we can find a class specific dissector for this request */
	dissector = NULL;

	/* Check valid values for bmRequestType and bRequest */
	for (tmp = setup_dissectors; tmp->dissector; tmp++) {
		if (tmp->request_type == usb_trans_info->setup.requesttype &&
		    tmp->request == usb_trans_info->setup.request) {
			dissector = tmp->dissector;
			break;
		}
	}
	/* No, we could not find any class specific dissector for this request
	 * return 0 and let USB try any of the standard requests.
	 */
	if (!dissector) {
		return 0;
	}

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBHUB");

	col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s",
	val_to_str(usb_trans_info->setup.request, setup_request_names_vals, "Unknown type %x"),
		is_request ? "Request " : "Response");

	if (is_request) {
		proto_tree_add_item(tree, hf_usb_hub_request, tvb, offset, 1, ENC_LITTLE_ENDIAN);
		offset += 1;
	}

	dissector(pinfo, tree, tvb, offset, is_request, usb_trans_info, urb);
	return tvb_captured_length(tvb);
}

void
proto_register_usb_hub(void)
{
	static hf_register_info hf[] = {
		/* USB HUB specific requests */
		{ &hf_usb_hub_request,
		{ "bRequest", "usbhub.setup.bRequest", FT_UINT8, BASE_HEX, VALS(setup_request_names_vals), 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_value,
		{ "wValue", "usbhub.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_index,
		{ "wIndex", "usbhub.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_length,
		{ "wLength", "usbhub.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_hub_feature_selector,
		{ "HubFeatureSelector", "usbhub.setup.HubFeatureSelector", FT_UINT16, BASE_DEC,
		  VALS(hub_class_feature_selectors_recipient_hub_vals), 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port_feature_selector,
		{ "PortFeatureSelector", "usbhub.setup.PortFeatureSelector", FT_UINT16, BASE_DEC,
		  VALS(hub_class_feature_selectors_recipient_port_vals), 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_dev_addr,
		{ "Dev_Addr", "usbhub.setup.Dev_Addr", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_ep_num,
		{ "EP_Num", "usbhub.setup.EP_Num", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_descriptor_type,
		{ "DescriptorType", "usbhub.setup.DescriptorType", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_descriptor_index,
		{ "DescriptorIndex", "usbhub.setup.DescriptorIndex", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_descriptor_length,
		{ "DescriptorLength", "usbhub.setup.DescriptorLength", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_zero,
		{ "(zero)", "usbhub.setup.zero", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_tt_flags,
		{ "TT_Flags", "usbhub.setup.TT_Flags", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_tt_port,
		{ "TT_Port", "usbhub.setup.TT_Port", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_tt_state_length,
		{ "TT State Length", "usbhub.setup.TT_StateLength", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port_selector,
		{ "PortSelector", "usbhub.setup.PortSelector", FT_UINT8, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port,
		{ "Port", "usbhub.setup.Port", FT_UINT16, BASE_DEC, NULL, 0x0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status,
		{ "Port Status", "usbhub.status.port", FT_UINT16, BASE_HEX, NULL, 0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change,
		{ "Port Change", "usbhub.change.port", FT_UINT16, BASE_HEX, NULL, 0,
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_connection,
		{ "PORT_CONNECTION", "usbhub.status.port.connection", FT_BOOLEAN, 16, NULL, (1<<0),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_enable,
		{ "PORT_ENABLE", "usbhub.status.port.enable", FT_BOOLEAN, 16, NULL, (1<<1),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_suspend,
		{ "PORT_SUSPEND", "usbhub.status.port.suspend", FT_BOOLEAN, 16, NULL, (1<<2),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_overcurrent,
		{ "PORT_OVER_CURRENT", "usbhub.status.port.overcurrent", FT_BOOLEAN, 16, NULL, (1<<3),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_reset,
		{ "PORT_RESET", "usbhub.status.port.reset", FT_BOOLEAN, 16, NULL, (1<<4),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_power,
		{ "PORT_POWER", "usbhub.status.port.power", FT_BOOLEAN, 16, NULL, (1<<8),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_low_speed,
		{ "PORT_LOW_SPEED", "usbhub.status.port.low_speed", FT_BOOLEAN, 16, NULL, (1<<9),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_high_speed,
		{ "PORT_HIGH_SPEED", "usbhub.status.port.high_speed", FT_BOOLEAN, 16, NULL, (1<<10),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_test,
		{ "PORT_TEST", "usbhub.status.port.test", FT_BOOLEAN, 16, NULL, (1<<11),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_status_indicator,
		{ "PORT_INDICATOR", "usbhub.status.port.indicator", FT_BOOLEAN, 16,
		  TFS(&hub_port_status_indicator_meaning), (1<<12),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change_connection,
		{ "C_PORT_CONNECTION", "usbhub.change.port.connection", FT_BOOLEAN, 16, NULL, (1<<0),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change_enable,
		{ "C_PORT_ENABLE", "usbhub.change.port.enable", FT_BOOLEAN, 16, NULL, (1<<1),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change_suspend,
		{ "C_PORT_SUSPEND", "usbhub.status.port.suspend", FT_BOOLEAN, 16, NULL, (1<<2),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change_overcurrent,
		{ "C_PORT_OVER_CURRENT", "usbhub.status.port.overcurrent", FT_BOOLEAN, 16, NULL, (1<<3),
		  NULL, HFILL }},

		{ &hf_usb_hub_port_change_reset,
		{ "C_PORT_RESET", "usbhub.status.port.reset", FT_BOOLEAN, 16, NULL, (1<<4),
		  NULL, HFILL }}
	};

	static int *usb_hub_subtrees[] = {
		&ett_usb_hub_wValue,
		&ett_usb_hub_wIndex,
		&ett_usb_hub_wLength,
		&ett_usb_hub_port_status,
		&ett_usb_hub_port_change
	};

	proto_usb_hub = proto_register_protocol("USB HUB", "USBHUB", "usbhub");
	proto_register_field_array(proto_usb_hub, hf, array_length(hf));
	proto_register_subtree_array(usb_hub_subtrees, array_length(usb_hub_subtrees));
	usb_hub_control_handle = register_dissector("usbhub", dissect_usb_hub_control, proto_usb_hub);
}

void
proto_reg_handoff_usb_hub(void)
{
	dissector_add_uint("usb.control", IF_CLASS_HUB, usb_hub_control_handle);
	dissector_add_uint("usb.control", IF_CLASS_UNKNOWN, usb_hub_control_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */