0c19dbe20756c5285e844e06ac145e1dbd511f25
[metze/wireshark/wip.git] / epan / dissectors / packet-usb.c
1 /* packet-usb.c
2  *
3  * USB basic dissector
4  * By Paolo Abeni <paolo.abeni@email.it>
5  * Ronnie Sahlberg 2006
6  *
7  * http://www.usb.org/developers/docs/usb_20_122909-2.zip
8  *
9  * https://github.com/torvalds/linux/blob/master/Documentation/usb/usbmon.txt
10  *
11  * http://desowin.org/usbpcap/captureformat.html
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28
29 #include "config.h"
30
31 #include <epan/packet.h>
32 #include <epan/exceptions.h>
33 #include <epan/addr_resolv.h>
34 #include <epan/conversation_table.h>
35 #include <epan/expert.h>
36 #include <epan/prefs.h>
37 #include <epan/decode_as.h>
38 #include <epan/proto_data.h>
39
40 #include "packet-usb.h"
41 #include "packet-mausb.h"
42 #include "packet-usbip.h"
43
44 /* protocols and header fields */
45 static int proto_usb = -1;
46
47 /* USB pseudoheader fields, both FreeBSD and Linux */
48 static int hf_usb_totlen = -1;
49 static int hf_usb_busunit = -1;
50 static int hf_usb_address = -1;
51 static int hf_usb_mode = -1;
52 static int hf_usb_freebsd_urb_type = -1;
53 static int hf_usb_freebsd_transfer_type = -1;
54 static int hf_usb_xferflags = -1;
55 static int hf_usb_xferstatus = -1;
56 static int hf_usb_error = -1;
57 static int hf_usb_interval = -1;
58 static int hf_usb_nframes = -1;
59 static int hf_usb_packet_size = -1;
60 static int hf_usb_packet_count = -1;
61 static int hf_usb_speed = -1;
62 static int hf_usb_frame_length = -1;
63 static int hf_usb_frame_flags = -1;
64 static int hf_usb_urb_id = -1;
65 static int hf_usb_linux_urb_type = -1;
66 static int hf_usb_linux_transfer_type = -1;
67 static int hf_usb_endpoint_number = -1;
68 static int hf_usb_endpoint_direction = -1;
69 static int hf_usb_endpoint_number_value = -1;
70 static int hf_usb_device_address = -1;
71 static int hf_usb_bus_id = -1;
72 static int hf_usb_setup_flag = -1;
73 static int hf_usb_data_flag = -1;
74 static int hf_usb_urb_ts_sec = -1;
75 static int hf_usb_urb_ts_usec = -1;
76 static int hf_usb_urb_status = -1;
77 static int hf_usb_urb_len = -1;
78 static int hf_usb_urb_data_len = -1;
79 static int hf_usb_urb_unused_setup_header = -1;
80 static int hf_usb_urb_interval = -1;
81 static int hf_usb_urb_start_frame = -1;
82 static int hf_usb_urb_copy_of_transfer_flags = -1;
83
84 /* Win32 USBPcap pseudoheader fields */
85 static int hf_usb_win32_header_len = -1;
86 static int hf_usb_irp_id = -1;
87 static int hf_usb_usbd_status = -1;
88 static int hf_usb_function = -1;
89 static int hf_usb_info = -1;
90 static int hf_usb_usbpcap_info_reserved = -1;
91 static int hf_usb_usbpcap_info_direction = -1;
92 static int hf_usb_win32_device_address = -1;
93 /* hf_usb_bus_id, hf_usb_endpoint_number, hf_usb_endpoint_direction,
94  * hf_usb_endpoint_number_value, hf_usb_transfer_type are common with
95  * Linux pseudoheader */
96 static int hf_usb_win32_data_len = -1;
97 static int hf_usb_control_stage = -1;
98 static int hf_usb_win32_iso_start_frame = -1;
99 static int hf_usb_win32_iso_num_packets = -1;
100 static int hf_usb_win32_iso_error_count = -1;
101 static int hf_usb_win32_iso_offset = -1;
102 static int hf_usb_win32_iso_length = -1;
103 static int hf_usb_win32_iso_status = -1;
104
105 static int hf_usb_request = -1;
106 static int hf_usb_request_unknown_class = -1;
107 static int hf_usb_value = -1;
108 static int hf_usb_index = -1;
109 static int hf_usb_length = -1;
110 /* static int hf_usb_data_len = -1; */
111 static int hf_usb_capdata = -1;
112 static int hf_usb_device_wFeatureSelector = -1;
113 static int hf_usb_interface_wFeatureSelector = -1;
114 static int hf_usb_endpoint_wFeatureSelector = -1;
115 static int hf_usb_wInterface = -1;
116 static int hf_usb_wStatus = -1;
117 static int hf_usb_wFrameNumber = -1;
118
119 static int hf_usb_iso_error_count = -1;
120 static int hf_usb_iso_numdesc = -1;
121 static int hf_usb_iso_status = -1;
122 static int hf_usb_iso_off = -1;
123 static int hf_usb_iso_len = -1;
124 static int hf_usb_iso_actual_len = -1;
125 static int hf_usb_iso_pad = -1;
126 static int hf_usb_iso_data = -1;
127
128 static int hf_usb_bmRequestType = -1;
129 static int hf_usb_control_response_generic = -1;
130 static int hf_usb_bmRequestType_direction = -1;
131 static int hf_usb_bmRequestType_type = -1;
132 static int hf_usb_bmRequestType_recipient = -1;
133 static int hf_usb_bDescriptorType = -1;
134 static int hf_usb_get_descriptor_resp_generic = -1;
135 static int hf_usb_descriptor_index = -1;
136 static int hf_usb_language_id = -1;
137 static int hf_usb_bLength = -1;
138 static int hf_usb_bcdUSB = -1;
139 static int hf_usb_bDeviceClass = -1;
140 static int hf_usb_bDeviceSubClass = -1;
141 static int hf_usb_bDeviceProtocol = -1;
142 static int hf_usb_bMaxPacketSize0 = -1;
143 static int hf_usb_idVendor = -1;
144 static int hf_usb_idProduct = -1;
145 static int hf_usb_bcdDevice = -1;
146 static int hf_usb_iManufacturer = -1;
147 static int hf_usb_iProduct = -1;
148 static int hf_usb_iSerialNumber = -1;
149 static int hf_usb_bNumConfigurations = -1;
150 static int hf_usb_wLANGID = -1;
151 static int hf_usb_bString = -1;
152 static int hf_usb_bInterfaceNumber = -1;
153 static int hf_usb_bAlternateSetting = -1;
154 static int hf_usb_bNumEndpoints = -1;
155 static int hf_usb_bInterfaceClass = -1;
156 static int hf_usb_bInterfaceSubClass = -1;
157 static int hf_usb_bInterfaceSubClass_cdc = -1;
158 static int hf_usb_bInterfaceSubClass_hid = -1;
159 static int hf_usb_bInterfaceSubClass_app = -1;
160 static int hf_usb_bInterfaceProtocol = -1;
161 static int hf_usb_bInterfaceProtocol_cdc = -1;
162 static int hf_usb_bInterfaceProtocol_cdc_data = -1;
163 static int hf_usb_bInterfaceProtocol_hid_boot = -1;
164 static int hf_usb_bInterfaceProtocol_app_dfu = -1;
165 static int hf_usb_bInterfaceProtocol_app_irda = -1;
166 static int hf_usb_bInterfaceProtocol_app_usb_test_and_measurement = -1;
167 static int hf_usb_iInterface = -1;
168 static int hf_usb_bEndpointAddress = -1;
169 static int hf_usb_bmAttributes = -1;
170 static int hf_usb_bEndpointAttributeTransfer = -1;
171 static int hf_usb_bEndpointAttributeSynchonisation = -1;
172 static int hf_usb_bEndpointAttributeBehaviour = -1;
173 static int hf_usb_wMaxPacketSize = -1;
174 static int hf_usb_wMaxPacketSize_size = -1;
175 static int hf_usb_wMaxPacketSize_slots = -1;
176 static int hf_usb_bInterval = -1;
177 static int hf_usb_wTotalLength = -1;
178 static int hf_usb_bNumInterfaces = -1;
179 static int hf_usb_bConfigurationValue = -1;
180 static int hf_usb_iConfiguration = -1;
181 static int hf_usb_bMaxPower = -1;
182 static int hf_usb_configuration_bmAttributes = -1;
183 static int hf_usb_configuration_legacy10buspowered = -1;
184 static int hf_usb_configuration_selfpowered = -1;
185 static int hf_usb_configuration_remotewakeup = -1;
186 static int hf_usb_bEndpointAddress_direction = -1;
187 static int hf_usb_bEndpointAddress_number = -1;
188 static int hf_usb_response_in = -1;
189 static int hf_usb_time = -1;
190 static int hf_usb_request_in = -1;
191 static int hf_usb_bFirstInterface = -1;
192 static int hf_usb_bInterfaceCount = -1;
193 static int hf_usb_bFunctionClass = -1;
194 static int hf_usb_bFunctionSubClass = -1;
195 static int hf_usb_bFunctionProtocol = -1;
196 static int hf_usb_iFunction = -1;
197 static int hf_usb_data_fragment = -1;
198 static int hf_usb_src = -1;
199 static int hf_usb_dst = -1;
200 static int hf_usb_addr = -1;
201
202 static gint ett_usb_hdr = -1;
203 static gint ett_usb_setup_hdr = -1;
204 static gint ett_usb_isodesc = -1;
205 static gint ett_usb_win32_iso_packet = -1;
206 static gint ett_usb_endpoint = -1;
207 static gint ett_usb_setup_bmrequesttype = -1;
208 static gint ett_usb_usbpcap_info = -1;
209 static gint ett_descriptor_device = -1;
210 static gint ett_configuration_bmAttributes = -1;
211 static gint ett_configuration_bEndpointAddress = -1;
212 static gint ett_endpoint_bmAttributes = -1;
213 static gint ett_endpoint_wMaxPacketSize = -1;
214 static gint ett_usb_frame = -1;
215
216 static expert_field ei_usb_bLength_even = EI_INIT;
217 static expert_field ei_usb_bLength_too_short = EI_INIT;
218 static expert_field ei_usb_desc_length_invalid = EI_INIT;
219 static expert_field ei_usb_invalid_setup = EI_INIT;
220
221 static const int *usb_endpoint_fields[] = {
222     &hf_usb_endpoint_direction,
223     &hf_usb_endpoint_number_value,
224     NULL
225 };
226
227 static const int *usb_usbpcap_info_fields[] = {
228     &hf_usb_usbpcap_info_reserved,
229     &hf_usb_usbpcap_info_direction,
230     NULL
231 };
232
233 static int usb_tap = -1;
234 static gboolean try_heuristics = TRUE;
235
236 static dissector_handle_t linux_usb_handle;
237
238 static dissector_handle_t data_handle;
239
240 static dissector_table_t usb_bulk_dissector_table;
241 static dissector_table_t usb_control_dissector_table;
242 static dissector_table_t usb_interrupt_dissector_table;
243 static dissector_table_t usb_descriptor_dissector_table;
244
245 static heur_dissector_list_t heur_bulk_subdissector_list;
246 static heur_dissector_list_t heur_control_subdissector_list;
247 static heur_dissector_list_t heur_interrupt_subdissector_list;
248
249 static wmem_tree_t *device_to_protocol_table = NULL;
250 static wmem_tree_t *device_to_product_table  = NULL;
251
252 static dissector_table_t device_to_dissector;
253 static dissector_table_t protocol_to_dissector;
254 static dissector_table_t product_to_dissector;
255
256 typedef struct _device_product_data_t {
257     guint16  vendor;
258     guint16  product;
259     guint  bus_id;
260     guint  device_address;
261 } device_product_data_t;
262
263 typedef struct _device_protocol_data_t {
264     guint32  protocol;
265     guint  bus_id;
266     guint  device_address;
267 } device_protocol_data_t;
268
269 typedef struct _usb_alt_setting_t {
270     guint8 altSetting;
271     guint8 interfaceClass;
272     guint8 interfaceSubclass;
273     guint8 interfaceProtocol;
274 } usb_alt_setting_t;
275
276 /* http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
277 static const value_string usb_langid_vals[] = {
278     {0x0000, "no language specified"},
279     {0x0401, "Arabic (Saudi Arabia)"},
280     {0x0402, "Bulgarian"},
281     {0x0403, "Catalan"},
282     {0x0404, "Chinese (Taiwan)"},
283     {0x0405, "Czech"},
284     {0x0406, "Danish"},
285     {0x0407, "German (Standard)"},
286     {0x0408, "Greek"},
287     {0x0409, "English (United States)"},
288     {0x040a, "Spanish (Traditional Sort)"},
289     {0x040b, "Finnish"},
290     {0x040c, "French (Standard)"},
291     {0x040d, "Hebrew"},
292     {0x040e, "Hungarian"},
293     {0x040f, "Icelandic"},
294     {0x0410, "Italian (Standard)"},
295     {0x0411, "Japanese"},
296     {0x0412, "Korean"},
297     {0x0413, "Dutch (Netherlands)"},
298     {0x0414, "Norwegian (Bokmal)"},
299     {0x0415, "Polish"},
300     {0x0416, "Portuguese (Brazil)"},
301     {0x0418, "Romanian"},
302     {0x0419, "Russian"},
303     {0x041a, "Croatian"},
304     {0x041b, "Slovak"},
305     {0x041c, "Albanian"},
306     {0x041d, "Swedish"},
307     {0x041e, "Thai"},
308     {0x041f, "Turkish"},
309     {0x0420, "Urdu (Pakistan)"},
310     {0x0421, "Indonesian"},
311     {0x0422, "Ukrainian"},
312     {0x0423, "Belarussian"},
313     {0x0424, "Slovenian"},
314     {0x0425, "Estonian"},
315     {0x0426, "Latvian"},
316     {0x0427, "Lithuanian"},
317     {0x0429, "Farsi"},
318     {0x042a, "Vietnamese"},
319     {0x042b, "Armenian"},
320     {0x042c, "Azeri (Latin)"},
321     {0x042d, "Basque"},
322     {0x042f, "Macedonian"},
323     {0x0430, "Sutu"},
324     {0x0436, "Afrikaans"},
325     {0x0437, "Georgian"},
326     {0x0438, "Faeroese"},
327     {0x0439, "Hindi"},
328     {0x043e, "Malay (Malaysian)"},
329     {0x043f, "Kazakh"},
330     {0x0441, "Swahili (Kenya)"},
331     {0x0443, "Uzbek (Latin)"},
332     {0x0444, "Tatar (Tatarstan)"},
333     {0x0445, "Bengali"},
334     {0x0446, "Punjabi"},
335     {0x0447, "Gujarati"},
336     {0x0448, "Oriya"},
337     {0x0449, "Tamil"},
338     {0x044a, "Telugu"},
339     {0x044b, "Kannada"},
340     {0x044c, "Malayalam"},
341     {0x044d, "Assamese"},
342     {0x044e, "Marathi"},
343     {0x044f, "Sanskrit"},
344     {0x0455, "Burmese"},
345     {0x0457, "Konkani"},
346     {0x0458, "Manipuri"},
347     {0x0459, "Sindhi"},
348     {0x04ff, "HID (Usage Data Descriptor)"},
349     {0x0801, "Arabic (Iraq)"},
350     {0x0804, "Chinese (PRC)"},
351     {0x0807, "German (Switzerland)"},
352     {0x0809, "English (United Kingdom)"},
353     {0x080a, "Spanish (Mexican)"},
354     {0x080c, "French (Belgian)"},
355     {0x0810, "Italian (Switzerland)"},
356     {0x0812, "Korean (Johab)"},
357     {0x0813, "Dutch (Belgium)"},
358     {0x0814, "Norwegian (Nynorsk)"},
359     {0x0816, "Portuguese (Standard)"},
360     {0x081a, "Serbian (Latin)"},
361     {0x081d, "Swedish (Finland)"},
362     {0x0820, "Urdu (India)"},
363     {0x0827, "Lithuanian (Classic)"},
364     {0x082c, "Azeri (Cyrillic)"},
365     {0x083e, "Malay (Brunei Darussalam)"},
366     {0x0843, "Uzbek (Cyrillic)"},
367     {0x0860, "Kashmiri (India)"},
368     {0x0861, "Nepali (India)"},
369     {0x0c01, "Arabic (Egypt)"},
370     {0x0c04, "Chinese (Hong Kong SAR, PRC)"},
371     {0x0c07, "German (Austria)"},
372     {0x0c09, "English (Australian)"},
373     {0x0c0a, "Spanish (Modern Sort)"},
374     {0x0c0c, "French (Canadian)"},
375     {0x0c1a, "Serbian (Cyrillic)"},
376     {0x1001, "Arabic (Libya)"},
377     {0x1004, "Chinese (Singapore)"},
378     {0x1007, "German (Luxembourg)"},
379     {0x1009, "English (Canadian)"},
380     {0x100a, "Spanish (Guatemala)"},
381     {0x100c, "French (Switzerland)"},
382     {0x1401, "Arabic (Algeria)"},
383     {0x1404, "Chinese (Macau SAR)"},
384     {0x1407, "German (Liechtenstein)"},
385     {0x1409, "English (New Zealand)"},
386     {0x140a, "Spanish (Costa Rica)"},
387     {0x140c, "French (Luxembourg)"},
388     {0x1801, "Arabic (Morocco)"},
389     {0x1809, "English (Ireland)"},
390     {0x180a, "Spanish (Panama)"},
391     {0x180c, "French (Monaco)"},
392     {0x1c01, "Arabic (Tunisia)"},
393     {0x1c09, "English (South Africa)"},
394     {0x1c0a, "Spanish (Dominican Republic)"},
395     {0x2001, "Arabic (Oman)"},
396     {0x2009, "English (Jamaica)"},
397     {0x200a, "Spanish (Venezuela)"},
398     {0x2401, "Arabic (Yemen)"},
399     {0x2409, "English (Caribbean)"},
400     {0x240a, "Spanish (Colombia)"},
401     {0x2801, "Arabic (Syria)"},
402     {0x2809, "English (Belize)"},
403     {0x280a, "Spanish (Peru)"},
404     {0x2c01, "Arabic (Jordan)"},
405     {0x2c09, "English (Trinidad)"},
406     {0x2c0a, "Spanish (Argentina)"},
407     {0x3001, "Arabic (Lebanon)"},
408     {0x3009, "English (Zimbabwe)"},
409     {0x300a, "Spanish (Ecuador)"},
410     {0x3401, "Arabic (Kuwait)"},
411     {0x3409, "English (Philippines)"},
412     {0x340a, "Spanish (Chile)"},
413     {0x3801, "Arabic (U.A.E.)"},
414     {0x380a, "Spanish (Uruguay)"},
415     {0x3c01, "Arabic (Bahrain)"},
416     {0x3c0a, "Spanish (Paraguay)"},
417     {0x4001, "Arabic (Qatar)"},
418     {0x400a, "Spanish (Bolivia)"},
419     {0x440a, "Spanish (El Salvador)"},
420     {0x480a, "Spanish (Honduras)"},
421     {0x4c0a, "Spanish (Nicaragua)"},
422     {0x500a, "Spanish (Puerto Rico)"},
423     {0xf0ff, "HID (Vendor Defined 1)"},
424     {0xf4ff, "HID (Vendor Defined 2)"},
425     {0xf8ff, "HID (Vendor Defined 3)"},
426     {0xfcff, "HID (Vendor Defined 4)"},
427     {0, NULL}
428 };
429 value_string_ext usb_langid_vals_ext = VALUE_STRING_EXT_INIT(usb_langid_vals);
430
431 static const value_string usb_class_vals[] = {
432     {IF_CLASS_DEVICE,                   "Device"},
433     {IF_CLASS_AUDIO,                    "Audio"},
434     {IF_CLASS_COMMUNICATIONS,           "Communications and CDC Control"},
435     {IF_CLASS_HID,                      "HID"},
436     {IF_CLASS_PHYSICAL,                 "Physical"},
437     {IF_CLASS_IMAGE,                    "Imaging"},
438     {IF_CLASS_PRINTER,                  "Printer"},
439     {IF_CLASS_MASS_STORAGE,             "Mass Storage"},
440     {IF_CLASS_HUB,                      "Hub"},
441     {IF_CLASS_CDC_DATA,                 "CDC-Data"},
442     {IF_CLASS_SMART_CARD,               "Smart Card"},
443     {IF_CLASS_CONTENT_SECURITY,         "Content Security"},
444     {IF_CLASS_VIDEO,                    "Video"},
445     {IF_CLASS_PERSONAL_HEALTHCARE,      "Personal Healthcare"},
446     {IF_CLASS_AUDIO_VIDEO,              "Audio/Video Devices"},
447     {IF_CLASS_DIAGNOSTIC_DEVICE,        "Diagnostic Device"},
448     {IF_CLASS_WIRELESS_CONTROLLER,      "Wireless Controller"},
449     {IF_CLASS_MISCELLANEOUS,            "Miscellaneous"},
450     {IF_CLASS_APPLICATION_SPECIFIC,     "Application Specific"},
451     {IF_CLASS_VENDOR_SPECIFIC,          "Vendor Specific"},
452     {0, NULL}
453 };
454 value_string_ext usb_class_vals_ext = VALUE_STRING_EXT_INIT(usb_class_vals);
455
456 /* use usb class, subclass and protocol id together
457   http://www.usb.org/developers/defined_class
458   USB Class Definitions for Communications Devices, Revision 1.2 December 6, 2012
459 */
460 static const value_string usb_protocols[] = {
461     {0x000000,    "Use class code info from Interface Descriptors"},
462     {0x060101,    "Still Imaging"},
463     {0x090000,    "Full speed Hub"},
464     {0x090001,    "Hi-speed hub with single TT"},
465     {0x090002,    "Hi-speed hub with multiple TTs"},
466     {0x0D0000,    "Content Security"},
467     {0x100100,    "AVControl Interface"},
468     {0x100200,    "AVData Video Streaming Interface"},
469     {0x100300,    "AVData Audio Streaming Interface"},
470     {0xDC0101,    "USB2 Compliance Device"},
471     {0xE00101,    "Bluetooth Programming Interface"},
472     {0xE00102,    "UWB Radio Control Interface"},
473     {0xE00103,    "Remote NDIS"},
474     {0xE00104,    "Bluetooth AMP Controller"},
475     {0xE00201,    "Host Wire Adapter Control/Data interface"},
476     {0xE00202,    "Device Wire Adapter Control/Data interface"},
477     {0xE00203,    "Device Wire Adapter Isochronous interface"},
478     {0xEF0101,    "Active Sync device"},
479     {0xEF0102,    "Palm Sync"},
480     {0xEF0201,    "Interface Association Descriptor"},
481     {0xEF0202,    "Wire Adapter Multifunction Peripheral programming interface"},
482     {0xEF0301,    "Cable Based Association Framework"},
483     {0xFE0101,    "Device Firmware Upgrade"},
484     {0xFE0200,    "IRDA Bridge device"},
485     {0xFE0300,    "USB Test and Measurement Device"},
486     {0xFE0301,    "USB Test and Measurement Device conforming to the USBTMC USB488"},
487     {0, NULL}
488 };
489 static value_string_ext usb_protocols_ext = VALUE_STRING_EXT_INIT(usb_protocols);
490
491 /* FreeBSD header */
492
493 /* Transfer mode */
494 #define FREEBSD_MODE_HOST       0
495 #define FREEBSD_MODE_DEVICE     1
496 static const value_string usb_freebsd_transfer_mode_vals[] = {
497     {FREEBSD_MODE_HOST,   "Host"},
498     {FREEBSD_MODE_DEVICE, "Device"},
499     {0, NULL}
500 };
501
502 /* Type */
503 #define FREEBSD_URB_SUBMIT   0
504 #define FREEBSD_URB_COMPLETE 1
505 static const value_string usb_freebsd_urb_type_vals[] = {
506     {FREEBSD_URB_SUBMIT,   "URB_SUBMIT"},
507     {FREEBSD_URB_COMPLETE, "URB_COMPLETE"},
508     {0, NULL}
509 };
510
511 /* Transfer type */
512 #define FREEBSD_URB_CONTROL     0
513 #define FREEBSD_URB_ISOCHRONOUS 1
514 #define FREEBSD_URB_BULK        2
515 #define FREEBSD_URB_INTERRUPT   3
516
517 static const value_string usb_freebsd_transfer_type_vals[] = {
518     {FREEBSD_URB_CONTROL,     "URB_CONTROL"},
519     {FREEBSD_URB_ISOCHRONOUS, "URB_ISOCHRONOUS"},
520     {FREEBSD_URB_BULK,        "URB_BULK"},
521     {FREEBSD_URB_INTERRUPT,   "URB_INTERRUPT"},
522     {0, NULL}
523 };
524
525 /* Transfer flags */
526 #define FREEBSD_FLAG_FORCE_SHORT_XFER 0x00000001
527 #define FREEBSD_FLAG_SHORT_XFER_OK    0x00000002
528 #define FREEBSD_FLAG_SHORT_FRAMES_OK  0x00000004
529 #define FREEBSD_FLAG_PIPE_BOF         0x00000008
530 #define FREEBSD_FLAG_PROXY_BUFFER     0x00000010
531 #define FREEBSD_FLAG_EXT_BUFFER       0x00000020
532 #define FREEBSD_FLAG_MANUAL_STATUS    0x00000040
533 #define FREEBSD_FLAG_NO_PIPE_OK       0x00000080
534 #define FREEBSD_FLAG_STALL_PIPE       0x00000100
535
536 /* Transfer status */
537 #define FREEBSD_STATUS_OPEN              0x00000001
538 #define FREEBSD_STATUS_TRANSFERRING      0x00000002
539 #define FREEBSD_STATUS_DID_DMA_DELAY     0x00000004
540 #define FREEBSD_STATUS_DID_CLOSE         0x00000008
541 #define FREEBSD_STATUS_DRAINING          0x00000010
542 #define FREEBSD_STATUS_STARTED           0x00000020
543 #define FREEBSD_STATUS_BW_RECLAIMED      0x00000040
544 #define FREEBSD_STATUS_CONTROL_XFR       0x00000080
545 #define FREEBSD_STATUS_CONTROL_HDR       0x00000100
546 #define FREEBSD_STATUS_CONTROL_ACT       0x00000200
547 #define FREEBSD_STATUS_CONTROL_STALL     0x00000400
548 #define FREEBSD_STATUS_SHORT_FRAMES_OK   0x00000800
549 #define FREEBSD_STATUS_SHORT_XFER_OK     0x00001000
550 #define FREEBSD_STATUS_BDMA_ENABLE       0x00002000
551 #define FREEBSD_STATUS_BDMA_NO_POST_SYNC 0x00004000
552 #define FREEBSD_STATUS_BDMA_SETUP        0x00008000
553 #define FREEBSD_STATUS_ISOCHRONOUS_XFR   0x00010000
554 #define FREEBSD_STATUS_CURR_DMA_SET      0x00020000
555 #define FREEBSD_STATUS_CAN_CANCEL_IMMED  0x00040000
556 #define FREEBSD_STATUS_DOING_CALLBACK    0x00080000
557
558 /* USB errors */
559 #define FREEBSD_ERR_NORMAL_COMPLETION 0
560 #define FREEBSD_ERR_PENDING_REQUESTS  1
561 #define FREEBSD_ERR_NOT_STARTED       2
562 #define FREEBSD_ERR_INVAL             3
563 #define FREEBSD_ERR_NOMEM             4
564 #define FREEBSD_ERR_CANCELLED         5
565 #define FREEBSD_ERR_BAD_ADDRESS       6
566 #define FREEBSD_ERR_BAD_BUFSIZE       7
567 #define FREEBSD_ERR_BAD_FLAG          8
568 #define FREEBSD_ERR_NO_CALLBACK       9
569 #define FREEBSD_ERR_IN_USE            10
570 #define FREEBSD_ERR_NO_ADDR           11
571 #define FREEBSD_ERR_NO_PIPE           12
572 #define FREEBSD_ERR_ZERO_NFRAMES      13
573 #define FREEBSD_ERR_ZERO_MAXP         14
574 #define FREEBSD_ERR_SET_ADDR_FAILED   15
575 #define FREEBSD_ERR_NO_POWER          16
576 #define FREEBSD_ERR_TOO_DEEP          17
577 #define FREEBSD_ERR_IOERROR           18
578 #define FREEBSD_ERR_NOT_CONFIGURED    19
579 #define FREEBSD_ERR_TIMEOUT           20
580 #define FREEBSD_ERR_SHORT_XFER        21
581 #define FREEBSD_ERR_STALLED           22
582 #define FREEBSD_ERR_INTERRUPTED       23
583 #define FREEBSD_ERR_DMA_LOAD_FAILED   24
584 #define FREEBSD_ERR_BAD_CONTEXT       25
585 #define FREEBSD_ERR_NO_ROOT_HUB       26
586 #define FREEBSD_ERR_NO_INTR_THREAD    27
587 #define FREEBSD_ERR_NOT_LOCKED        28
588
589 static const value_string usb_freebsd_err_vals[] = {
590     {FREEBSD_ERR_NORMAL_COMPLETION, "Normal completion"},
591     {FREEBSD_ERR_PENDING_REQUESTS,  "Pending requests"},
592     {FREEBSD_ERR_NOT_STARTED,       "Not started"},
593     {FREEBSD_ERR_INVAL,             "Invalid"},
594     {FREEBSD_ERR_NOMEM,             "No memory"},
595     {FREEBSD_ERR_CANCELLED,         "Cancelled"},
596     {FREEBSD_ERR_BAD_ADDRESS,       "Bad address"},
597     {FREEBSD_ERR_BAD_BUFSIZE,       "Bad buffer size"},
598     {FREEBSD_ERR_BAD_FLAG,          "Bad flag"},
599     {FREEBSD_ERR_NO_CALLBACK,       "No callback"},
600     {FREEBSD_ERR_IN_USE,            "In use"},
601     {FREEBSD_ERR_NO_ADDR,           "No address"},
602     {FREEBSD_ERR_NO_PIPE,           "No pipe"},
603     {FREEBSD_ERR_ZERO_NFRAMES,      "Number of frames is zero"},
604     {FREEBSD_ERR_ZERO_MAXP,         "MAXP is zero"},
605     {FREEBSD_ERR_SET_ADDR_FAILED,   "Set address failed"},
606     {FREEBSD_ERR_NO_POWER,          "No power"},
607     {FREEBSD_ERR_TOO_DEEP,          "Too deep"},
608     {FREEBSD_ERR_IOERROR,           "I/O error"},
609     {FREEBSD_ERR_NOT_CONFIGURED,    "Not configured"},
610     {FREEBSD_ERR_TIMEOUT,           "Timeout"},
611     {FREEBSD_ERR_SHORT_XFER,        "Short transfer"},
612     {FREEBSD_ERR_STALLED,           "Stalled"},
613     {FREEBSD_ERR_INTERRUPTED,       "Interrupted"},
614     {FREEBSD_ERR_DMA_LOAD_FAILED,   "DMA load failed"},
615     {FREEBSD_ERR_BAD_CONTEXT,       "Bad context"},
616     {FREEBSD_ERR_NO_ROOT_HUB,       "No root hub"},
617     {FREEBSD_ERR_NO_INTR_THREAD,    "No interrupt thread"},
618     {FREEBSD_ERR_NOT_LOCKED,        "Not locked"},
619     {0, NULL}
620 };
621
622 /* USB speeds */
623 #define FREEBSD_SPEED_VARIABLE 0
624 #define FREEBSD_SPEED_LOW      1
625 #define FREEBSD_SPEED_FULL     2
626 #define FREEBSD_SPEED_HIGH     3
627 #define FREEBSD_SPEED_SUPER    4
628
629 static const value_string usb_freebsd_speed_vals[] = {
630     {FREEBSD_SPEED_VARIABLE, "Variable"},
631     {FREEBSD_SPEED_LOW,      "Low"},
632     {FREEBSD_SPEED_FULL,     "Full"},
633     {FREEBSD_SPEED_HIGH,     "High"},
634     {FREEBSD_SPEED_SUPER,    "Super"},
635     {0, NULL}
636 };
637
638 /* Frame flags */
639 #define FREEBSD_FRAMEFLAG_READ         0x00000001
640 #define FREEBSD_FRAMEFLAG_DATA_FOLLOWS 0x00000002
641
642 static const value_string usb_linux_urb_type_vals[] = {
643     {URB_SUBMIT,   "URB_SUBMIT"},
644     {URB_COMPLETE, "URB_COMPLETE"},
645     {URB_ERROR,    "URB_ERROR"},
646     {0, NULL}
647 };
648
649 static const value_string usb_linux_transfer_type_vals[] = {
650     {URB_CONTROL,                       "URB_CONTROL"},
651     {URB_ISOCHRONOUS,                   "URB_ISOCHRONOUS"},
652     {URB_INTERRUPT,                     "URB_INTERRUPT"},
653     {URB_BULK,                          "URB_BULK"},
654     {0, NULL}
655 };
656
657 static const value_string usb_transfer_type_and_direction_vals[] = {
658     {URB_CONTROL,                       "URB_CONTROL out"},
659     {URB_ISOCHRONOUS,                   "URB_ISOCHRONOUS out"},
660     {URB_INTERRUPT,                     "URB_INTERRUPT out"},
661     {URB_BULK,                          "URB_BULK out"},
662     {URB_CONTROL | URB_TRANSFER_IN,     "URB_CONTROL in"},
663     {URB_ISOCHRONOUS | URB_TRANSFER_IN, "URB_ISOCHRONOUS in"},
664     {URB_INTERRUPT | URB_TRANSFER_IN,   "URB_INTERRUPT in"},
665     {URB_BULK | URB_TRANSFER_IN,        "URB_BULK in"},
666     {0, NULL}
667 };
668
669 static const value_string usb_endpoint_direction_vals[] = {
670     {0, "OUT"},
671     {1, "IN"},
672     {0, NULL}
673 };
674
675 extern value_string_ext ext_usb_vendors_vals;
676 extern value_string_ext ext_usb_products_vals;
677 extern value_string_ext ext_usb_com_subclass_vals;
678
679 /*
680  * Standard descriptor types.
681  *
682  * all class specific descriptor types were removed from this list
683  * a descriptor type is not globally unique
684  * dissectors for the USB classes should provide their own value string
685  *  and pass it to dissect_usb_descriptor_header()
686  *
687  */
688 #define USB_DT_DEVICE                          1
689 #define USB_DT_CONFIG                          2
690 #define USB_DT_STRING                          3
691 #define USB_DT_INTERFACE                       4
692 #define USB_DT_ENDPOINT                        5
693 #define USB_DT_DEVICE_QUALIFIER                6
694 #define USB_DT_OTHER_SPEED_CONFIG              7
695 #define USB_DT_INTERFACE_POWER                 8
696 /* these are from a minor usb 2.0 revision (ECN) */
697 #define USB_DT_OTG                             9
698 #define USB_DT_DEBUG                          10
699 #define USB_DT_INTERFACE_ASSOCIATION          11
700
701 /* There are only Standard Descriptor Types, Class-specific types are
702    provided by "usb.descriptor" descriptors table*/
703 static const value_string std_descriptor_type_vals[] = {
704     {USB_DT_DEVICE,                         "DEVICE"},
705     {USB_DT_CONFIG,                         "CONFIGURATION"},
706     {USB_DT_STRING,                         "STRING"},
707     {USB_DT_INTERFACE,                      "INTERFACE"},
708     {USB_DT_ENDPOINT,                       "ENDPOINT"},
709     {USB_DT_DEVICE_QUALIFIER,               "DEVICE QUALIFIER"},
710     {USB_DT_OTHER_SPEED_CONFIG,             "OTHER SPEED CONFIG"},
711     {USB_DT_INTERFACE_POWER,                "INTERFACE POWER"},
712     {USB_DT_OTG,                            "OTG"},
713     {USB_DT_DEBUG,                          "DEBUG"},
714     {USB_DT_INTERFACE_ASSOCIATION,          "INTERFACE ASSOCIATION"},
715     { 0x0F,                                 "BOS"},
716     { 0x10,                                 "DEVICE CAPABILITY"},
717     { 0x30,                                 "SUPERSPEED USB ENDPOINT COMPANION"},
718     { 0x31,                                 "SUPERSPEED PLUS ISOCHRONOUS ENDPOINT COMPANION"},
719     {0,NULL}
720 };
721 static value_string_ext std_descriptor_type_vals_ext =
722                VALUE_STRING_EXT_INIT(std_descriptor_type_vals);
723
724 /*
725  * Feature selectors.
726  * Per USB 3.1 spec, Table 9-7
727  */
728 #define USB_FS_ENDPOINT_HALT            0
729 #define USB_FS_FUNCTION_SUSPEND         0 /* same as ENDPOINT_HALT */
730 #define USB_FS_DEVICE_REMOTE_WAKEUP     1
731 #define USB_FS_TEST_MODE                2
732 #define USB_FS_B_HNP_ENABLE             3
733 #define USB_FS_A_HNP_SUPPORT            4
734 #define USB_FS_A_ALT_HNP_SUPPORT        5
735 #define USB_FS_WUSB_DEVICE              6
736 #define USB_FS_U1_ENABLE                48
737 #define USB_FS_U2_ENABLE                49
738 #define USB_FS_LTM_ENABLE               50
739 #define USB_FS_B3_NTF_HOST_REL          51
740 #define USB_FS_B3_RSP_ENABLE            52
741 #define USB_FS_LDM_ENABLE               53
742
743 static const value_string usb_endpoint_feature_selector_vals[] = {
744     {USB_FS_ENDPOINT_HALT,              "ENDPOINT HALT"},
745     {0, NULL}
746 };
747
748 static const value_string usb_interface_feature_selector_vals[] = {
749     {USB_FS_FUNCTION_SUSPEND,           "FUNCTION SUSPEND"},
750     {0, NULL}
751 };
752
753 static const value_string usb_device_feature_selector_vals[] = {
754     {USB_FS_DEVICE_REMOTE_WAKEUP,       "DEVICE REMOTE WAKEUP"},
755     {USB_FS_TEST_MODE,                  "TEST MODE"},
756     {USB_FS_B_HNP_ENABLE,               "B HNP ENABLE"},
757     {USB_FS_A_HNP_SUPPORT,              "A HNP SUPPORT"},
758     {USB_FS_A_ALT_HNP_SUPPORT,          "A ALT HNP SUPPORT"},
759     {USB_FS_WUSB_DEVICE,                "WUSB DEVICE"},
760     {USB_FS_U1_ENABLE,                  "U1 ENABLE"},
761     {USB_FS_U2_ENABLE,                  "U2 ENABLE"},
762     {USB_FS_LTM_ENABLE,                 "LTM ENABLE"},
763     {USB_FS_B3_NTF_HOST_REL,            "B3 NTF HOST REL"},
764     {USB_FS_B3_RSP_ENABLE,              "B3 RSP ENABLE"},
765     {USB_FS_LDM_ENABLE,                 "LDM ENABLE"},
766     {0, NULL}
767 };
768
769
770 /* the transfer type in the endpoint descriptor, i.e. the type of the endpoint
771    (this is not the same as the URB transfer type) */
772 #define USB_EP_CONTROL     0x00
773 #define USB_EP_ISOCHRONOUS 0x01
774 #define USB_EP_BULK        0x02
775 #define USB_EP_INTERRUPT   0x03
776
777 static const value_string usb_bmAttributes_transfer_vals[] = {
778     {USB_EP_CONTROL,     "Control-Transfer"},
779     {USB_EP_ISOCHRONOUS, "Isochronous-Transfer"},
780     {USB_EP_BULK,        "Bulk-Transfer"},
781     {USB_EP_INTERRUPT,   "Interrupt-Transfer"},
782     {0, NULL}
783 };
784
785 static const value_string usb_bmAttributes_sync_vals[] = {
786     {0x00,      "No Sync"},
787     {0x01,      "Asynchronous"},
788     {0x02,      "Adaptive"},
789     {0x03,      "Synchronous"},
790     {0, NULL}
791 };
792
793 static const value_string usb_bmAttributes_behaviour_vals[] = {
794     {0x00,      "Data-Endpoint"},
795     {0x01,      "Explicit Feedback-Endpoint"},
796     {0x02,      "Implicit Feedback-Data-Endpoint"},
797     {0x03,      "Reserved"},
798     {0, NULL}
799 };
800
801 static const value_string usb_wMaxPacketSize_slots_vals[]  = {
802     {0x00,      "1"},
803     {0x01,      "2"},
804     {0x02,      "3"},
805     {0x03,      "Reserved"},
806     {0, NULL}
807 };
808
809 /* Note: sorted in (unsigned) ascending order */
810 static const value_string usb_urb_status_vals[] = {
811     /* from linux/include/asm-generic/errno.h*/
812     { -131, "State not recoverable (-ENOTRECOVERABLE)" },
813     { -130, "Owner died (-EOWNERDEAD)" },
814     { -129, "Key was rejected by service (-EKEYREJECTED)" },
815     { -128, "Key has been revoked (-EKEYREVOKED)" },
816     { -127, "Key has expired (-EKEYEXPIRED)" },
817     { -126, "Required key not available (-ENOKEY)" },
818     { -125, "Operation Canceled (-ECANCELED)" },
819     { -124, "Wrong medium type (-EMEDIUMTYPE)" },
820     { -123, "No medium found (-ENOMEDIUM)" },
821     { -122, "Quota exceeded (-EDQUOT)" },
822     { -121, "Remote I/O error (-EREMOTEIO)" },
823     { -120, "Is a named type file (-EISNAM)" },
824     { -119, "No XENIX semaphores available (-ENAVAIL)" },
825     { -118, "Not a XENIX named type file (-ENOTNAM)" },
826     { -117, "Structure needs cleaning (-EUCLEAN)" },
827     { -116, "Stale NFS file handle (-ESTALE)" },
828     { -115, "Operation now in progress (-EINPROGRESS)" },
829     { -114, "Operation already in progress (-EALREADY)" },
830     { -113, "No route to host (-EHOSTUNREACH)" },
831     { -112, "Host is down (-EHOSTDOWN)" },
832     { -111, "Connection refused (-ECONNREFUSED)" },
833     { -110, "Connection timed out (-ETIMEDOUT)" },
834     { -109, "Too many references: cannot splice (-ETOOMANYREFS)" },
835     { -108, "Cannot send after transport endpoint shutdown (-ESHUTDOWN)" },
836     { -107, "Transport endpoint is not connected (-ENOTCONN)" },
837     { -106, "Transport endpoint is already connected (-EISCONN)" },
838     { -105, "No buffer space available (-ENOBUFS)" },
839     { -104, "Connection reset by peer (-ECONNRESET)" },
840     { -103, "Software caused connection abort (-ECONNABORTED)" },
841     { -102, "Network dropped connection because of reset (-ENETRESET)" },
842     { -101, "Network is unreachable (-ENETUNREACH)" },
843     { -100, "Network is down (-ENETDOWN)" },
844     { -99,  "Cannot assign requested address (-EADDRNOTAVAIL)" },
845     { -98,  "Address already in use (-EADDRINUSE)" },
846     { -97,  "Address family not supported by protocol (-EAFNOSUPPORT)" },
847     { -96,  "Protocol family not supported (-EPFNOSUPPORT)" },
848     { -95,  "Operation not supported on transport endpoint (-EOPNOTSUPP)" },
849     { -94,  "Socket type not supported (-ESOCKTNOSUPPORT)" },
850     { -93,  "Protocol not supported (-EPROTONOSUPPORT)" },
851     { -92,  "Protocol not available (-ENOPROTOOPT)" },
852     { -91,  "Protocol wrong type for socket (-EPROTOTYPE)" },
853     { -90,  "Message too long (-EMSGSIZE)" },
854     { -89,  "Destination address required (-EDESTADDRREQ)" },
855     { -88,  "Socket operation on non-socket (-ENOTSOCK)" },
856     { -87,  "Too many users (-EUSERS)" },
857     { -86,  "Streams pipe error (-ESTRPIPE)" },
858     { -85,  "Interrupted system call should be restarted (-ERESTART)" },
859     { -84,  "Illegal byte sequence (-EILSEQ)" },
860     { -83,  "Cannot exec a shared library directly (-ELIBEXEC)" },
861     { -82,  "Attempting to link in too many shared libraries (-ELIBMAX)" },
862     { -81,  ".lib section in a.out corrupted (-ELIBSCN)" },
863     { -80,  "Accessing a corrupted shared library (-ELIBBAD)" },
864     { -79,  "Can not access a needed shared library (-ELIBACC)" },
865     { -78,  "Remote address changed (-EREMCHG)" },
866     { -77,  "File descriptor in bad state (-EBADFD)" },
867     { -76,  "Name not unique on network (-ENOTUNIQ)" },
868     { -75,  "Value too large for defined data type (-EOVERFLOW)" },
869     { -74,  "Not a data message (-EBADMSG)" },
870     { -73,  "RFS specific error (-EDOTDOT)" },
871     { -72,  "Multihop attempted (-EMULTIHOP)" },
872     { -71,  "Protocol error (-EPROTO)" },
873     { -70,  "Communication error on send (-ECOMM)" },
874     { -69,  "Srmount error (-ESRMNT)" },
875     { -68,  "Advertise error (-EADV)" },
876     { -67,  "Link has been severed (-ENOLINK)" },
877     { -66,  "Object is remote (-EREMOTE)" },
878     { -65,  "Package not installed (-ENOPKG)" },
879     { -64,  "Machine is not on the network (-ENONET)" },
880     { -63,  "Out of streams resources (-ENOSR)" },
881     { -62,  "Timer expired (-ETIME)" },
882     { -61,  "No data available (-ENODATA)" },
883     { -60,  "Device not a stream (-ENOSTR)" },
884     { -59,  "Bad font file format (-EBFONT)" },
885     { -58,  "(-58 \?\?\?)" },   /* dummy so that there are no "gaps" */
886     { -57,  "Invalid slot (-EBADSLT)" },
887     { -56,  "Invalid request code (-EBADRQC)" },
888     { -55,  "No anode (-ENOANO)" },
889     { -54,  "Exchange full (-EXFULL)" },
890     { -53,  "Invalid request descriptor (-EBADR)" },
891     { -52,  "Invalid exchange (-EBADE)" },
892     { -51,  "Level 2 halted (-EL2HLT)" },
893     { -50,  "No CSI structure available (-ENOCSI)" },
894     { -49,  "Protocol driver not attached (-EUNATCH)" },
895     { -48,  "Link number out of range (-ELNRNG)" },
896     { -47,  "Level 3 reset (-EL3RST)" },
897     { -46,  "Level 3 halted (-EL3HLT)" },
898     { -45,  "Level 2 not synchronized (-EL2NSYNC)" },
899     { -44,  "Channel number out of range (-ECHRNG)" },
900     { -43,  "Identifier removed (-EIDRM)" },
901     { -42,  "No message of desired type (-ENOMSG)" },
902     { -41,  "(-41 \?\?\?)" },   /* dummy so that there are no "gaps" */
903     { -40,  "Too many symbolic links encountered (-ELOOP)" },
904     { -39,  "Directory not empty (-ENOTEMPTY)" },
905     { -38,  "Function not implemented (-ENOSYS)" },
906     { -37,  "No record locks available (-ENOLCK)" },
907     { -36,  "File name too long (-ENAMETOOLONG)" },
908     { -35,  "Resource deadlock would occur (-EDEADLK)" },
909     /* from linux/include/asm-generic/errno.h */
910     { -34,  "Math result not representable (-ERANGE)" },
911     { -33,  "Math argument out of domain of func (-EDOM)" },
912     { -32,  "Broken pipe (-EPIPE)" },
913     { -31,  "Too many links (-EMLINK)" },
914     { -30,  "Read-only file system (-EROFS)" },
915     { -29,  "Illegal seek (-ESPIPE)" },
916     { -28,  "No space left on device (-ENOSPC)" },
917     { -27,  "File too large (-EFBIG)" },
918     { -26,  "Text file busy (-ETXTBSY)" },
919     { -25,  "Not a typewriter (-ENOTTY)" },
920     { -24,  "Too many open files (-EMFILE)" },
921     { -23,  "File table overflow (-ENFILE)" },
922     { -22,  "Invalid argument (-EINVAL)" },
923     { -21,  "Is a directory (-EISDIR)" },
924     { -20,  "Not a directory (-ENOTDIR)" },
925     { -19,  "No such device (-ENODEV)" },
926     { -18,  "Cross-device link (-EXDEV)" },
927     { -17,  "File exists (-EEXIST)" },
928     { -16,  "Device or resource busy (-EBUSY)" },
929     { -15,  "Block device required (-ENOTBLK)" },
930     { -14,  "Bad address (-EFAULT)" },
931     { -13,  "Permission denied (-EACCES)" },
932     { -12,  "Out of memory (-ENOMEM)" },
933     { -11,  "Try again (-EAGAIN)" },
934     { -10,  "No child processes (-ECHILD)" },
935     { -9,   "Bad file number (-EBADF)" },
936     { -8,   "Exec format error (-ENOEXEC)" },
937     { -7,   "Argument list too long (-E2BIG)" },
938     { -6,   "No such device or address (-ENXIO)" },
939     { -5,   "I/O error (-EIO)" },
940     { -4,   "Interrupted system call (-EINTR)" },
941     { -3,   "No such process (-ESRCH)" },
942     { -2,   "No such file or directory (-ENOENT)" },
943     { -1,   "Operation not permitted (-EPERM)" },
944     { 0,    "Success"},
945     { 0, NULL }
946 };
947 value_string_ext usb_urb_status_vals_ext = VALUE_STRING_EXT_INIT(usb_urb_status_vals);
948
949 #define USB_CONTROL_STAGE_SETUP  0x00
950 #define USB_CONTROL_STAGE_DATA   0x01
951 #define USB_CONTROL_STAGE_STATUS 0x02
952
953 static const value_string usb_control_stage_vals[] = {
954     {USB_CONTROL_STAGE_SETUP,  "Setup"},
955     {USB_CONTROL_STAGE_DATA,   "Data"},
956     {USB_CONTROL_STAGE_STATUS, "Status"},
957     {0, NULL}
958 };
959
960 static const value_string win32_urb_function_vals[] = {
961     {0x0000, "URB_FUNCTION_SELECT_CONFIGURATION"},
962     {0x0001, "URB_FUNCTION_SELECT_INTERFACE"},
963     {0x0002, "URB_FUNCTION_ABORT_PIPE"},
964     {0x0003, "URB_FUNCTION_TAKE_FRAME_LENGTH_CONTROL"},
965     {0x0004, "URB_FUNCTION_RELEASE_FRAME_LENGTH_CONTROL"},
966     {0x0005, "URB_FUNCTION_GET_FRAME_LENGTH"},
967     {0x0006, "URB_FUNCTION_SET_FRAME_LENGTH"},
968     {0x0007, "URB_FUNCTION_GET_CURRENT_FRAME_NUMBER"},
969     {0x0008, "URB_FUNCTION_CONTROL_TRANSFER"},
970     {0x0009, "URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER"},
971     {0x000A, "URB_FUNCTION_ISOCH_TRANSFER"},
972     {0x000B, "URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE"},
973     {0x000C, "URB_FUNCTION_SET_DESCRIPTOR_TO_DEVICE"},
974     {0x000D, "URB_FUNCTION_SET_FEATURE_TO_DEVICE"},
975     {0x000E, "URB_FUNCTION_SET_FEATURE_TO_INTERFACE"},
976     {0x000F, "URB_FUNCTION_SET_FEATURE_TO_ENDPOINT"},
977     {0x0010, "URB_FUNCTION_CLEAR_FEATURE_TO_DEVICE"},
978     {0x0011, "URB_FUNCTION_CLEAR_FEATURE_TO_INTERFACE"},
979     {0x0012, "URB_FUNCTION_CLEAR_FEATURE_TO_ENDPOINT"},
980     {0x0013, "URB_FUNCTION_GET_STATUS_FROM_DEVICE"},
981     {0x0014, "URB_FUNCTION_GET_STATUS_FROM_INTERFACE"},
982     {0x0015, "URB_FUNCTION_GET_STATUS_FROM_ENDPOINT"},
983     {0x0016, "URB_FUNCTION_RESERVED_0X0016"},
984     {0x0017, "URB_FUNCTION_VENDOR_DEVICE"},
985     {0x0018, "URB_FUNCTION_VENDOR_INTERFACE"},
986     {0x0019, "URB_FUNCTION_VENDOR_ENDPOINT"},
987     {0x001A, "URB_FUNCTION_CLASS_DEVICE"},
988     {0x001B, "URB_FUNCTION_CLASS_INTERFACE"},
989     {0x001C, "URB_FUNCTION_CLASS_ENDPOINT"},
990     {0x001D, "URB_FUNCTION_RESERVE_0X001D"},
991     {0x001E, "URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL"},
992     {0x001F, "URB_FUNCTION_CLASS_OTHER"},
993     {0x0020, "URB_FUNCTION_VENDOR_OTHER"},
994     {0x0021, "URB_FUNCTION_GET_STATUS_FROM_OTHER"},
995     {0x0022, "URB_FUNCTION_CLEAR_FEATURE_TO_OTHER"},
996     {0x0023, "URB_FUNCTION_SET_FEATURE_TO_OTHER"},
997     {0x0024, "URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT"},
998     {0x0025, "URB_FUNCTION_SET_DESCRIPTOR_TO_ENDPOINT"},
999     {0x0026, "URB_FUNCTION_GET_CONFIGURATION"},
1000     {0x0027, "URB_FUNCTION_GET_INTERFACE"},
1001     {0x0028, "URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE"},
1002     {0x0029, "URB_FUNCTION_SET_DESCRIPTOR_TO_INTERFACE"},
1003     {0x002A, "URB_FUNCTION_GET_MS_FEATURE_DESCRIPTOR"},
1004     {0x002B, "URB_FUNCTION_RESERVE_0X002B"},
1005     {0x002C, "URB_FUNCTION_RESERVE_0X002C"},
1006     {0x002D, "URB_FUNCTION_RESERVE_0X002D"},
1007     {0x002E, "URB_FUNCTION_RESERVE_0X002E"},
1008     {0x002F, "URB_FUNCTION_RESERVE_0X002F"},
1009     {0x0030, "URB_FUNCTION_SYNC_RESET_PIPE"},
1010     {0x0031, "URB_FUNCTION_SYNC_CLEAR_STALL"},
1011     {0x0032, "URB_FUNCTION_CONTROL_TRANSFER_EX"},
1012     {0x0033, "URB_FUNCTION_RESERVE_0X0033"},
1013     {0x0034, "URB_FUNCTION_RESERVE_0X0034"},
1014     {0, NULL}
1015 };
1016 value_string_ext win32_urb_function_vals_ext = VALUE_STRING_EXT_INIT(win32_urb_function_vals);
1017
1018 static const value_string win32_usbd_status_vals[] = {
1019     {0x00000000, "USBD_STATUS_SUCCESS"},
1020     {0x40000000, "USBD_STATUS_PENDING"},
1021
1022     {0x80000200, "USBD_STATUS_INVALID_URB_FUNCTION"},
1023     {0x80000300, "USBD_STATUS_INVALID_PARAMETER"},
1024     {0x80000400, "USBD_STATUS_ERROR_BUSY"},
1025     {0x80000600, "USBD_STATUS_INVALID_PIPE_HANDLE"},
1026     {0x80000700, "USBD_STATUS_NO_BANDWIDTH"},
1027     {0x80000800, "USBD_STATUS_INTERNAL_HC_ERROR"},
1028     {0x80000900, "USBD_STATUS_ERROR_SHORT_TRANSFER"},
1029
1030     {0xC0000001, "USBD_STATUS_CRC"},
1031     {0xC0000002, "USBD_STATUS_BTSTUFF"},
1032     {0xC0000003, "USBD_STATUS_DATA_TOGGLE_MISMATCH"},
1033     {0xC0000004, "USBD_STATUS_STALL_PID"},
1034     {0xC0000005, "USBD_STATUS_DEV_NOT_RESPONDING"},
1035     {0xC0000006, "USBD_STATUS_PID_CHECK_FAILURE"},
1036     {0xC0000007, "USBD_STATUS_UNEXPECTED_PID"},
1037     {0xC0000008, "USBD_STATUS_DATA_OVERRUN"},
1038     {0xC0000009, "USBD_STATUS_DATA_UNDERRUN"},
1039     {0xC000000A, "USBD_STATUS_RESERVED1"},
1040     {0xC000000B, "USBD_STATUS_RESERVED2"},
1041     {0xC000000C, "USBD_STATUS_BUFFER_OVERRUN"},
1042     {0xC000000D, "USBD_STATUS_BUFFER_UNDERRUN"},
1043     {0xC000000F, "USBD_STATUS_NOT_ACCESSED"},
1044     {0xC0000010, "USBD_STATUS_FIFO"},
1045     {0xC0000011, "USBD_STATUS_XACT_ERROR"},
1046     {0xC0000012, "USBD_STATUS_BABBLE_DETECTED"},
1047     {0xC0000013, "USBD_STATUS_DATA_BUFFER_ERROR"},
1048     {0xC0000030, "USBD_STATUS_ENDPOINT_HALTED"},
1049
1050     {0xC0000A00, "USBD_STATUS_BAD_START_FRAME"},
1051     {0xC0000B00, "USBD_STATUS_ISOCH_REQUEST_FAILED"},
1052     {0xC0000C00, "USBD_STATUS_FRAME_CONTROL_OWNED"},
1053     {0xC0000D00, "USBD_STATUS_FRAME_CONTROL_NOT_OWNED"},
1054     {0xC0000E00, "USBD_STATUS_NOT_SUPPORTED"},
1055     {0xC0000F00, "USBD_STATUS_INVALID_CONFIGURATION_DESCRIPTOR"},
1056     {0xC0001000, "USBD_STATUS_INSUFFICIENT_RESOURCES"},
1057     {0xC0002000, "USBD_STATUS_SET_CONFIG_FAILED"},
1058     {0xC0003000, "USBD_STATUS_BUFFER_TOO_SMALL"},
1059     {0xC0004000, "USBD_STATUS_INTERFACE_NOT_FOUND"},
1060     {0xC0005000, "USBD_STATUS_INVALID_PIPE_FLAGS"},
1061     {0xC0006000, "USBD_STATUS_TIMEOUT"},
1062     {0xC0007000, "USBD_STATUS_DEVICE_GONE"},
1063     {0xC0008000, "USBD_STATUS_STATUS_NOT_MAPPED"},
1064     {0xC0009000, "USBD_STATUS_HUB_INTERNAL_ERROR"},
1065     {0xC0010000, "USBD_STATUS_CANCELED"},
1066     {0xC0020000, "USBD_STATUS_ISO_NOT_ACCESSED_BY_HW"},
1067     {0xC0030000, "USBD_STATUS_ISO_TD_ERROR"},
1068     {0xC0040000, "USBD_STATUS_ISO_NA_LATE_USBPORT"},
1069     {0xC0050000, "USBD_STATUS_ISO_NOT_ACCESSED_LATE"},
1070     {0xC0100000, "USBD_STATUS_BAD_DESCRIPTOR"},
1071     {0xC0100001, "USBD_STATUS_BAD_DESCRIPTOR_BLEN"},
1072     {0xC0100002, "USBD_STATUS_BAD_DESCRIPTOR_TYPE"},
1073     {0xC0100003, "USBD_STATUS_BAD_INTERFACE_DESCRIPTOR"},
1074     {0xC0100004, "USBD_STATUS_BAD_ENDPOINT_DESCRIPTOR"},
1075     {0xC0100005, "USBD_STATUS_BAD_INTERFACE_ASSOC_DESCRIPTOR"},
1076     {0xC0100006, "USBD_STATUS_BAD_CONFIG_DESC_LENGTH"},
1077     {0xC0100007, "USBD_STATUS_BAD_NUMBER_OF_INTERFACES"},
1078     {0xC0100008, "USBD_STATUS_BAD_NUMBER_OF_ENDPOINTS"},
1079     {0xC0100009, "USBD_STATUS_BAD_ENDPOINT_ADDRESS"},
1080     {0, NULL}
1081 };
1082 static value_string_ext win32_usbd_status_vals_ext = VALUE_STRING_EXT_INIT(win32_usbd_status_vals);
1083
1084 static const value_string win32_usb_info_direction_vals[] = {
1085     {0, "FDO -> PDO"},
1086     {1, "PDO -> FDO"},
1087     {0, NULL}
1088 };
1089
1090 static const value_string usb_cdc_protocol_vals[] = {
1091     {0x00, "No class specific protocol required"},
1092     {0x01, "AT Commands: V.250 etc"},
1093     {0x02, "AT Commands defined by PCCA-101"},
1094     {0x03, "AT Commands defined by PCCA-101 & Annex O"},
1095     {0x04, "AT Commands defined by GSM 07.07"},
1096     {0x05, "AT Commands defined by 3GPP 27.007"},
1097     {0x06, "AT Commands defined by TIA for CDMA"},
1098     {0x07, "Ethernet Emulation Model"},
1099     {0xFE, "External Protocol: Commands defined by Command Set Functional Descriptor"},
1100     {0xFF, "Vendor-specific"},
1101     {0, NULL}
1102 };
1103 static value_string_ext usb_cdc_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_cdc_protocol_vals);
1104
1105 static const value_string usb_cdc_data_protocol_vals[] = {
1106     {0x00, "No class specific protocol required"},
1107     {0x01, "Network Transfer Block"},
1108     {0x02, "Network Transfer Block (IP + DSS)"},
1109     {0x30, "Physical interface protocol for ISDN BRI"},
1110     {0x31, "HDLC"},
1111     {0x32, "Transparent"},
1112     {0x50, "Management protocol for Q.921 data link protocol"},
1113     {0x51, "Data link protocol for Q.931"},
1114     {0x52, "TEI-multiplexor for Q.921 data link protocol"},
1115     {0x90, "Data compression procedures"},
1116     {0x91, "Euro-ISDN protocol control"},
1117     {0x92, "V.24 rate adaptation to ISDN"},
1118     {0x93, "CAPI Commands"},
1119     {0xFE, "The protocol(s) are described using a Protocol Unit Functional Descriptors on Communications Class Interface"},
1120     {0xFF, "Vendor-specific"},
1121     {0, NULL}
1122 };
1123 static value_string_ext usb_cdc_data_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_cdc_data_protocol_vals);
1124
1125 static const value_string usb_hid_subclass_vals[] = {
1126     {0, "No Subclass"},
1127     {1, "Boot Interface"},
1128     {0, NULL}
1129 };
1130 static value_string_ext usb_hid_subclass_vals_ext = VALUE_STRING_EXT_INIT(usb_hid_subclass_vals);
1131
1132 static const value_string usb_hid_boot_protocol_vals[] = {
1133     {0, "None"},
1134     {1, "Keyboard"},
1135     {2, "Mouse"},
1136     {0, NULL}
1137 };
1138 static value_string_ext usb_hid_boot_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_hid_boot_protocol_vals);
1139
1140 static const value_string usb_app_subclass_vals[] = {
1141     {0x01, "Device Firmware Upgrade"},
1142     {0x02, "IRDA Bridge"},
1143     {0x03, "USB Test and Measurement Device"},
1144     {0, NULL}
1145 };
1146 static value_string_ext usb_app_subclass_vals_ext = VALUE_STRING_EXT_INIT(usb_app_subclass_vals);
1147
1148
1149 static const value_string usb_app_dfu_protocol_vals[] = {
1150     {0x01, "Runtime protocol"},
1151     {0x02, "DFU mode protocol"},
1152     {0, NULL}
1153 };
1154 static value_string_ext usb_app_dfu_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_app_dfu_protocol_vals);
1155
1156 static const value_string usb_app_irda_protocol_vals[] = {
1157     {0x00, "IRDA Bridge device"},
1158     {0, NULL}
1159 };
1160 static value_string_ext usb_app_irda_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_app_irda_protocol_vals);
1161
1162 static const value_string usb_app_usb_test_and_measurement_protocol_vals[] = {
1163     {0x00, "USB Test and Measurement Device"},
1164     {0x01, "USB Test and Measurement Device conforming to the USBTMC USB488 Subclass Specification"},
1165     {0, NULL}
1166 };
1167 static value_string_ext usb_app_usb_test_and_measurement_protocol_vals_ext = VALUE_STRING_EXT_INIT(usb_app_usb_test_and_measurement_protocol_vals);
1168
1169 void proto_register_usb(void);
1170 void proto_reg_handoff_usb(void);
1171
1172 /* This keys provide information for DecodeBy and other dissector via
1173    per packet data: p_get_proto_data()/p_add_proto_data() */
1174 #define USB_BUS_ID           0
1175 #define USB_DEVICE_ADDRESS   1
1176 #define USB_VENDOR_ID        2
1177 #define USB_PRODUCT_ID       3
1178 #define USB_DEVICE_CLASS     4
1179 #define USB_DEVICE_SUBCLASS  5
1180 #define USB_DEVICE_PROTOCOL  6
1181
1182 static void
1183 usb_device_prompt(packet_info *pinfo, gchar* result)
1184 {
1185     g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Bus ID %u \nDevice Address %u\nas ",
1186             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_BUS_ID)),
1187             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_ADDRESS)));
1188 }
1189
1190 static gpointer
1191 usb_device_value(packet_info *pinfo)
1192 {
1193     guint32 value = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_BUS_ID)) << 16;
1194     value |= GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_ADDRESS));
1195     return GUINT_TO_POINTER(value);
1196 }
1197
1198 static void
1199 usb_product_prompt(packet_info *pinfo, gchar* result)
1200 {
1201     g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Vendor ID 0x%04x \nProduct ID 0x%04x\nas ",
1202             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_VENDOR_ID)),
1203             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_PRODUCT_ID)));
1204 }
1205
1206 static gpointer
1207 usb_product_value(packet_info *pinfo)
1208 {
1209     guint32 value = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_VENDOR_ID)) << 16;
1210     value |= GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_PRODUCT_ID));
1211     return GUINT_TO_POINTER(value);
1212 }
1213
1214 static void
1215 usb_protocol_prompt(packet_info *pinfo, gchar* result)
1216 {
1217     g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Class ID 0x%04x \nSubclass ID 0x%04x\nProtocol 0x%04x\nas ",
1218             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_CLASS)),
1219             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_SUBCLASS)),
1220             GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_PROTOCOL)));
1221 }
1222
1223 static gpointer
1224 usb_protocol_value(packet_info *pinfo)
1225 {
1226     guint32 value = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_CLASS)) << 16;
1227     value |= GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_SUBCLASS)) << 8;
1228     value |= GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_PROTOCOL));
1229     return GUINT_TO_POINTER(value);
1230 }
1231
1232 static build_valid_func   usb_product_da_build_value[1] = {usb_product_value};
1233 static decode_as_value_t  usb_product_da_values         = {usb_product_prompt, 1, usb_product_da_build_value};
1234 static decode_as_t        usb_product_da = {
1235         "usb", "USB Product", "usb.product",
1236         1, 0, &usb_product_da_values, NULL, NULL,
1237         decode_as_default_populate_list, decode_as_default_reset,
1238         decode_as_default_change, NULL};
1239
1240 static build_valid_func   usb_device_da_build_value[1] = {usb_device_value};
1241 static decode_as_value_t  usb_device_da_values         = {usb_device_prompt, 1, usb_device_da_build_value};
1242 static decode_as_t        usb_device_da = {
1243         "usb", "USB Device", "usb.device",
1244         1, 0, &usb_device_da_values, NULL, NULL,
1245         decode_as_default_populate_list, decode_as_default_reset,
1246         decode_as_default_change, NULL};
1247
1248 static build_valid_func   usb_protocol_da_build_value[1] = {usb_protocol_value};
1249 static decode_as_value_t  usb_protocol_da_values         = {usb_protocol_prompt, 1, usb_protocol_da_build_value};
1250 static decode_as_t        usb_protocol_da = {
1251         "usb", "USB Device Protocol", "usb.protocol",
1252         1, 0, &usb_protocol_da_values, NULL, NULL,
1253         decode_as_default_populate_list, decode_as_default_reset,
1254         decode_as_default_change, NULL};
1255
1256
1257 static usb_conv_info_t *
1258 get_usb_conv_info(conversation_t *conversation)
1259 {
1260     usb_conv_info_t *usb_conv_info;
1261
1262     /* do we have conversation specific data ? */
1263     usb_conv_info = (usb_conv_info_t *)conversation_get_proto_data(conversation, proto_usb);
1264     if (!usb_conv_info) {
1265         /* no not yet so create some */
1266         usb_conv_info = wmem_new0(wmem_file_scope(), usb_conv_info_t);
1267         usb_conv_info->interfaceClass    = IF_CLASS_UNKNOWN;
1268         usb_conv_info->interfaceSubclass = IF_SUBCLASS_UNKNOWN;
1269         usb_conv_info->interfaceProtocol = IF_PROTOCOL_UNKNOWN;
1270         usb_conv_info->deviceVendor      = DEV_VENDOR_UNKNOWN;
1271         usb_conv_info->deviceProduct     = DEV_PRODUCT_UNKNOWN;
1272         usb_conv_info->alt_settings      = wmem_array_new(wmem_file_scope(), sizeof(usb_alt_setting_t));
1273         usb_conv_info->transactions      = wmem_tree_new(wmem_file_scope());
1274
1275         conversation_add_proto_data(conversation, proto_usb, usb_conv_info);
1276     }
1277
1278     return usb_conv_info;
1279 }
1280
1281
1282 /* usb_conv_info_t contains some components that are valid only for one specific packet
1283    clear_usb_conv_tmp_data() clears these components, it should be called
1284    before we dissect a new packet */
1285 static void clear_usb_conv_tmp_data(usb_conv_info_t *usb_conv_info)
1286 {
1287     /* caller must have checked that usb_conv_info!= NULL */
1288
1289     usb_conv_info->direction = P2P_DIR_UNKNOWN;
1290     usb_conv_info->transfer_type = URB_UNKNOWN;
1291     usb_conv_info->is_request = FALSE;
1292     usb_conv_info->is_setup = FALSE;
1293     usb_conv_info->setup_requesttype = 0;
1294
1295     /* when we parse the configuration, interface and endpoint
1296        descriptors, we store the current interface class in endpoint 0's
1297        conversation
1298
1299        this must be cleared since endpoint 0 does not belong to any
1300        interface class
1301
1302        we used to clear these info in dissect_usb_configuration_descriptor()
1303        this doesn't work when the descriptor parsing throws an exception */
1304
1305     if (usb_conv_info->endpoint==0) {
1306         usb_conv_info->interfaceClass    = IF_CLASS_UNKNOWN;
1307         usb_conv_info->interfaceSubclass = IF_SUBCLASS_UNKNOWN;
1308         usb_conv_info->interfaceProtocol = IF_PROTOCOL_UNKNOWN;
1309     }
1310 }
1311
1312 static conversation_t *
1313 get_usb_conversation(packet_info *pinfo,
1314                      address *src_addr, address *dst_addr,
1315                      guint32 src_endpoint, guint32 dst_endpoint)
1316 {
1317     conversation_t *conversation;
1318
1319     /*
1320      * Do we have a conversation for this connection?
1321      */
1322     conversation = find_conversation(pinfo->num,
1323                                src_addr, dst_addr,
1324                                pinfo->ptype,
1325                                src_endpoint, dst_endpoint, 0);
1326     if (conversation) {
1327         return conversation;
1328     }
1329
1330     /* We don't yet have a conversation, so create one. */
1331     conversation = conversation_new(pinfo->num,
1332                            src_addr, dst_addr,
1333                            pinfo->ptype,
1334                            src_endpoint, dst_endpoint, 0);
1335     return conversation;
1336 }
1337
1338 /* Fetch or create usb_conv_info for a specified interface. */
1339 usb_conv_info_t *
1340 get_usb_iface_conv_info(packet_info *pinfo, guint8 interface_num)
1341 {
1342     conversation_t *conversation;
1343     guint32 if_port;
1344
1345     if_port = GUINT32_TO_LE(INTERFACE_PORT | interface_num);
1346
1347     if (pinfo->srcport == NO_ENDPOINT) {
1348         conversation = get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, pinfo->srcport, if_port);
1349     } else {
1350         conversation = get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, if_port, pinfo->destport);
1351     }
1352
1353     return get_usb_conv_info(conversation);
1354 }
1355
1356 static const char* usb_conv_get_filter_type(conv_item_t* conv, conv_filter_type_e filter)
1357 {
1358     if ((filter == CONV_FT_SRC_ADDRESS) && (conv->src_address.type == AT_USB))
1359         return "usb.src";
1360
1361     if ((filter == CONV_FT_DST_ADDRESS) && (conv->dst_address.type == AT_USB))
1362         return "usb.dst";
1363
1364     if ((filter == CONV_FT_ANY_ADDRESS) && (conv->src_address.type == AT_USB))
1365         return "usb.addr";
1366
1367     return CONV_FILTER_INVALID;
1368 }
1369
1370 static ct_dissector_info_t usb_ct_dissector_info = {&usb_conv_get_filter_type};
1371
1372 static int
1373 usb_conversation_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip _U_)
1374 {
1375     conv_hash_t *hash = (conv_hash_t*) pct;
1376     add_conversation_table_data(hash, &pinfo->src, &pinfo->dst, 0, 0, 1, pinfo->fd->pkt_len, &pinfo->rel_ts, &pinfo->abs_ts, &usb_ct_dissector_info, PT_NONE);
1377
1378     return 1;
1379 }
1380
1381 static const char* usb_host_get_filter_type(hostlist_talker_t* host, conv_filter_type_e filter)
1382 {
1383     if ((filter == CONV_FT_ANY_ADDRESS) && (host->myaddress.type == AT_USB))
1384         return "usb.addr";
1385
1386     return CONV_FILTER_INVALID;
1387 }
1388
1389 static hostlist_dissector_info_t usb_host_dissector_info = {&usb_host_get_filter_type};
1390
1391 static int
1392 usb_hostlist_packet(void *pit, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip _U_)
1393 {
1394     conv_hash_t *hash = (conv_hash_t*) pit;
1395
1396     /* Take two "add" passes per packet, adding for each direction, ensures that all
1397        packets are counted properly (even if address is sending to itself)
1398        XXX - this could probably be done more efficiently inside hostlist_table */
1399     add_hostlist_table_data(hash, &pinfo->src, 0, TRUE, 1, pinfo->fd->pkt_len, &usb_host_dissector_info, PT_NONE);
1400     add_hostlist_table_data(hash, &pinfo->dst, 0, FALSE, 1, pinfo->fd->pkt_len, &usb_host_dissector_info, PT_NONE);
1401
1402     return 1;
1403 }
1404
1405 /* SETUP dissectors */
1406
1407
1408 /*
1409  * These dissectors are used to dissect the setup part and the data
1410  * for URB_CONTROL_INPUT / CLEAR FEATURE
1411  */
1412
1413
1414 /* 9.4.1 */
1415 static int
1416 dissect_usb_setup_clear_feature_request(packet_info *pinfo _U_, proto_tree *tree,
1417                                         tvbuff_t *tvb, int offset,
1418                                         usb_conv_info_t  *usb_conv_info)
1419 {
1420     guint8 recip;
1421
1422     if (usb_conv_info) {
1423         recip = USB_RECIPIENT(usb_conv_info->usb_trans_info->setup.requesttype);
1424
1425         /* feature selector */
1426         switch (recip) {
1427         case RQT_SETUP_RECIPIENT_DEVICE:
1428             proto_tree_add_item(tree, hf_usb_device_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1429             break;
1430
1431         case RQT_SETUP_RECIPIENT_INTERFACE:
1432             proto_tree_add_item(tree, hf_usb_interface_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1433             break;
1434
1435         case RQT_SETUP_RECIPIENT_ENDPOINT:
1436             proto_tree_add_item(tree, hf_usb_endpoint_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1437             break;
1438
1439         case RQT_SETUP_RECIPIENT_OTHER:
1440         default:
1441             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1442             break;
1443         }
1444     } else {
1445         /* No conversation information, so recipient type is unknown */
1446         proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1447     }
1448     offset += 2;
1449
1450     /* zero/interface/endpoint */
1451     /* XXX - check based on request type */
1452     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1453     offset += 2;
1454
1455     /* length */
1456     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1457     offset += 2;
1458
1459     return offset;
1460 }
1461
1462 static int
1463 dissect_usb_setup_clear_feature_response(packet_info *pinfo _U_, proto_tree *tree _U_,
1464                                          tvbuff_t *tvb _U_, int offset,
1465                                          usb_conv_info_t  *usb_conv_info _U_)
1466 {
1467     return offset;
1468 }
1469
1470
1471 /*
1472  * These dissectors are used to dissect the setup part and the data
1473  * for URB_CONTROL_INPUT / GET CONFIGURATION
1474  */
1475
1476
1477 /* 9.4.2 */
1478 static int
1479 dissect_usb_setup_get_configuration_response(packet_info *pinfo _U_, proto_tree *tree _U_,
1480                                              tvbuff_t *tvb _U_, int offset,
1481                                              usb_conv_info_t  *usb_conv_info _U_)
1482 {
1483     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1484     offset += 1;
1485
1486     return offset;
1487 }
1488
1489
1490 /*
1491  * These dissectors are used to dissect the setup part and the data
1492  * for URB_CONTROL_INPUT / GET DESCRIPTOR
1493  */
1494
1495 proto_item * dissect_usb_descriptor_header(proto_tree *tree,
1496                                            tvbuff_t *tvb, int offset,
1497                                            value_string_ext *type_val_str)
1498 {
1499     guint8      desc_type;
1500     proto_item *length_item;
1501     proto_item *type_item;
1502
1503
1504     length_item = proto_tree_add_item(tree, hf_usb_bLength,
1505           tvb, offset, 1,  ENC_LITTLE_ENDIAN);
1506     offset++;
1507
1508     desc_type = tvb_get_guint8(tvb, offset);
1509     type_item = proto_tree_add_item(tree, hf_usb_bDescriptorType,
1510           tvb, offset, 1, ENC_LITTLE_ENDIAN);
1511     /* if the caller provided no class specific value string, we're
1512      * using the standard descriptor types */
1513     if (!type_val_str)
1514           type_val_str = &std_descriptor_type_vals_ext;
1515     proto_item_append_text(type_item, " (%s)",
1516              val_to_str_ext(desc_type, type_val_str, "unknown"));
1517
1518     return length_item;
1519 }
1520
1521 /* 9.6.2 */
1522 static int
1523 dissect_usb_device_qualifier_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
1524                                         tvbuff_t *tvb, int offset,
1525                                         usb_conv_info_t  *usb_conv_info)
1526 {
1527     proto_item *item;
1528     proto_tree *tree;
1529     proto_item *nitem;
1530     int         old_offset = offset;
1531     guint32     protocol;
1532     const gchar *description;
1533
1534     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "DEVICE QUALIFIER DESCRIPTOR");
1535
1536     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
1537     offset += 2;
1538
1539     /* bcdUSB */
1540     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1541     offset += 2;
1542
1543     protocol = tvb_get_ntoh24(tvb, offset);
1544     description = val_to_str_ext_const(protocol, &usb_protocols_ext, "");
1545
1546     /* bDeviceClass */
1547     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1548     offset += 1;
1549
1550     /* bDeviceSubClass */
1551     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1552     offset += 1;
1553
1554     /* bDeviceProtocol */
1555     nitem = proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1556     if (*description)
1557         proto_item_append_text(nitem, " (%s)", description);
1558     offset += 1;
1559
1560     if (!pinfo->fd->flags.visited) {
1561         guint                   k_bus_id;
1562         guint                   k_device_address;
1563         guint                   k_frame_number;
1564         wmem_tree_key_t         key[4];
1565         device_protocol_data_t  *device_protocol_data;
1566
1567         k_frame_number = pinfo->num;
1568         k_device_address = usb_conv_info->device_address;
1569         k_bus_id = usb_conv_info->bus_id;
1570
1571         key[0].length = 1;
1572         key[0].key    = &k_device_address;
1573         key[1].length = 1;
1574         key[1].key    = &k_bus_id;
1575         key[2].length = 1;
1576         key[2].key    = &k_frame_number;
1577         key[3].length = 0;
1578         key[3].key    = NULL;
1579
1580         device_protocol_data = wmem_new(wmem_file_scope(), device_protocol_data_t);
1581         device_protocol_data->protocol = protocol;
1582         device_protocol_data->bus_id = usb_conv_info->bus_id;
1583         device_protocol_data->device_address = usb_conv_info->device_address;
1584         wmem_tree_insert32_array(device_to_protocol_table, key, device_protocol_data);
1585     }
1586
1587     /* bMaxPacketSize0 */
1588     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1589     offset += 1;
1590
1591     /* bNumConfigurations */
1592     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1593     offset += 1;
1594
1595     /* one reserved byte */
1596     offset += 1;
1597
1598     proto_item_set_len(item, offset-old_offset);
1599
1600     return offset;
1601 }
1602
1603 /* 9.6.1 */
1604 static int
1605 dissect_usb_device_descriptor(packet_info *pinfo, proto_tree *parent_tree,
1606                               tvbuff_t *tvb, int offset,
1607                               usb_conv_info_t *usb_conv_info)
1608 {
1609     proto_item        *item;
1610     proto_tree        *tree;
1611     proto_item        *nitem;
1612     int                old_offset = offset;
1613     guint32            protocol;
1614     const gchar       *description;
1615     guint32            vendor_id;
1616     guint32            product;
1617     guint16            product_id;
1618
1619     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "DEVICE DESCRIPTOR");
1620
1621     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
1622     offset += 2;
1623
1624     /* bcdUSB */
1625     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1626     offset += 2;
1627
1628     protocol = tvb_get_ntoh24(tvb, offset);
1629     description = val_to_str_ext_const(protocol, &usb_protocols_ext, "");
1630
1631     /* bDeviceClass */
1632     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1633     offset += 1;
1634
1635     /* bDeviceSubClass */
1636     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1637     offset += 1;
1638
1639     /* bDeviceProtocol */
1640     nitem = proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1641     if (*description)
1642         proto_item_append_text(nitem, " (%s)", description);
1643     offset += 1;
1644
1645     /* bMaxPacketSize0 */
1646     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1647     offset += 1;
1648
1649     /* if request was only for the first 8 bytes */
1650     /* per 5.5.3 of USB2.0 Spec */
1651     if (8 == usb_conv_info->usb_trans_info->setup.wLength) {
1652         proto_item_set_len(item, offset-old_offset);
1653         return offset;
1654     }
1655
1656     /* idVendor */
1657     proto_tree_add_item_ret_uint(tree, hf_usb_idVendor, tvb, offset, 2, ENC_LITTLE_ENDIAN, &vendor_id);
1658     usb_conv_info->deviceVendor = (guint16)vendor_id;
1659     offset += 2;
1660
1661     /* idProduct */
1662     product_id = tvb_get_letohs(tvb, offset);
1663     usb_conv_info->deviceProduct = product_id;
1664     product = (guint16)vendor_id << 16 | product_id;
1665
1666     proto_tree_add_uint_format_value(tree, hf_usb_idProduct, tvb, offset, 2, product_id, "%s (0x%04x)",
1667                                      val_to_str_ext_const(product, &ext_usb_products_vals, "Unknown"),
1668                                      product_id);
1669     offset += 2;
1670
1671     if (!pinfo->fd->flags.visited) {
1672         guint                   k_bus_id;
1673         guint                   k_device_address;
1674         guint                   k_frame_number;
1675         wmem_tree_key_t         key[4];
1676         device_product_data_t   *device_product_data;
1677         device_protocol_data_t  *device_protocol_data;
1678
1679         k_frame_number = pinfo->num;
1680         k_device_address = usb_conv_info->device_address;
1681         k_bus_id = usb_conv_info->bus_id;
1682
1683         key[0].length = 1;
1684         key[0].key    = &k_device_address;
1685         key[1].length = 1;
1686         key[1].key    = &k_bus_id;
1687         key[2].length = 1;
1688         key[2].key    = &k_frame_number;
1689         key[3].length = 0;
1690         key[3].key    = NULL;
1691
1692         device_product_data = wmem_new(wmem_file_scope(), device_product_data_t);
1693         device_product_data->vendor = vendor_id;
1694         device_product_data->product = product_id;
1695         device_product_data->bus_id = usb_conv_info->bus_id;
1696         device_product_data->device_address = usb_conv_info->device_address;
1697         wmem_tree_insert32_array(device_to_product_table, key, device_product_data);
1698
1699         device_protocol_data = wmem_new(wmem_file_scope(), device_protocol_data_t);
1700         device_protocol_data->protocol = protocol;
1701         device_protocol_data->bus_id = usb_conv_info->bus_id;
1702         device_protocol_data->device_address = usb_conv_info->device_address;
1703
1704         wmem_tree_insert32_array(device_to_protocol_table, key, device_protocol_data);
1705     }
1706
1707     /* bcdDevice */
1708     proto_tree_add_item(tree, hf_usb_bcdDevice, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1709     offset += 2;
1710
1711     /* iManufacturer */
1712     proto_tree_add_item(tree, hf_usb_iManufacturer, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1713     offset += 1;
1714
1715     /* iProduct */
1716     proto_tree_add_item(tree, hf_usb_iProduct, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1717     offset += 1;
1718
1719     /* iSerialNumber */
1720     proto_tree_add_item(tree, hf_usb_iSerialNumber, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1721     offset += 1;
1722
1723     /* bNumConfigurations */
1724     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1725     offset += 1;
1726
1727     proto_item_set_len(item, offset-old_offset);
1728
1729     return offset;
1730 }
1731
1732 /* 9.6.7 */
1733 static int
1734 dissect_usb_string_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
1735                               tvbuff_t *tvb, int offset,
1736                               usb_conv_info_t  *usb_conv_info)
1737 {
1738     proto_item *item;
1739     proto_tree *tree;
1740     int         old_offset = offset;
1741     guint8      len;
1742     proto_item *len_item;
1743     usb_trans_info_t *usb_trans_info;
1744
1745     usb_trans_info = usb_conv_info->usb_trans_info;
1746
1747     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "STRING DESCRIPTOR");
1748
1749     len = tvb_get_guint8(tvb, offset);
1750     /* The USB spec says that the languages / the string are UTF16 and not
1751        0-terminated, i.e. the length field must contain an even number */
1752     if (len & 0x1) {
1753         /* bLength */
1754         len_item = proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1755         expert_add_info(pinfo, len_item, &ei_usb_bLength_even);
1756
1757         /* bDescriptorType */
1758         proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset+1, 1, ENC_LITTLE_ENDIAN);
1759     }
1760     else
1761        len_item = dissect_usb_descriptor_header(tree, tvb, offset, NULL);
1762     offset += 2;
1763
1764     /* Report an error, and give up, if the length is < 2 */
1765     if (len < 2) {
1766         expert_add_info(pinfo, len_item, &ei_usb_bLength_too_short);
1767         return offset;
1768     }
1769
1770     if (!usb_trans_info->u.get_descriptor.usb_index) {
1771         /* list of languanges */
1772         while (offset >= old_offset && len > (offset - old_offset)) {
1773             /* wLANGID */
1774             proto_tree_add_item(tree, hf_usb_wLANGID, tvb, offset, 2, ENC_LITTLE_ENDIAN);
1775             offset+=2;
1776         }
1777     } else {
1778         /* UTF-16 string */
1779         /* handle case of host requesting only substring */
1780         guint8 len_str = MIN(len-2, usb_trans_info->setup.wLength -2);
1781         proto_tree_add_item(tree, hf_usb_bString, tvb, offset, len_str, ENC_UTF_16 | ENC_LITTLE_ENDIAN);
1782         offset += len_str;
1783     }
1784
1785     proto_item_set_len(item, offset-old_offset);
1786
1787     return offset;
1788 }
1789
1790
1791
1792 /* 9.6.5 */
1793 static int
1794 dissect_usb_interface_descriptor(packet_info *pinfo, proto_tree *parent_tree,
1795                                  tvbuff_t *tvb, int offset,
1796                                  usb_conv_info_t  *usb_conv_info)
1797 {
1798     proto_item       *item;
1799     proto_tree       *tree;
1800     const char       *class_str  = NULL;
1801     int               old_offset = offset;
1802     guint8            len;
1803     guint8            interface_num;
1804     guint8            alt_setting;
1805     usb_trans_info_t *usb_trans_info;
1806
1807     usb_trans_info = usb_conv_info->usb_trans_info;
1808
1809     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "INTERFACE DESCRIPTOR");
1810
1811     len = tvb_get_guint8(tvb, offset);
1812     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
1813     offset += 2;
1814
1815     /* bInterfaceNumber */
1816     interface_num = tvb_get_guint8(tvb, offset);
1817     proto_tree_add_item(tree, hf_usb_bInterfaceNumber, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1818     usb_conv_info->interfaceNum = interface_num;
1819     offset += 1;
1820
1821     /* bAlternateSetting */
1822     alt_setting = tvb_get_guint8(tvb, offset);
1823     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1824     offset += 1;
1825
1826     /* bNumEndpoints */
1827     proto_tree_add_item(tree, hf_usb_bNumEndpoints, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1828     offset += 1;
1829
1830     /* bInterfaceClass */
1831     proto_tree_add_item(tree, hf_usb_bInterfaceClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1832     /* save the class so we can access it later in the endpoint descriptor */
1833     usb_conv_info->interfaceClass = tvb_get_guint8(tvb, offset);
1834
1835     class_str = val_to_str_ext(usb_conv_info->interfaceClass, &usb_class_vals_ext, "unknown (0x%X)");
1836     proto_item_append_text(item, " (%u.%u): class %s", interface_num, alt_setting, class_str);
1837
1838     if (!pinfo->fd->flags.visited) {
1839         usb_alt_setting_t alternate_setting;
1840
1841         /* Register conversation for this interface in case CONTROL messages are sent to it */
1842         usb_trans_info->interface_info = get_usb_iface_conv_info(pinfo, interface_num);
1843
1844         alternate_setting.altSetting = alt_setting;
1845         alternate_setting.interfaceClass = tvb_get_guint8(tvb, offset);
1846         alternate_setting.interfaceSubclass = tvb_get_guint8(tvb, offset+1);
1847         alternate_setting.interfaceProtocol = tvb_get_guint8(tvb, offset+2);
1848         wmem_array_append_one(usb_trans_info->interface_info->alt_settings, alternate_setting);
1849
1850         if (alt_setting == 0) {
1851             /* By default let's assume alternate setting 0 will be used */
1852
1853             /* in interface conversations, endpoint has no meaning */
1854             usb_trans_info->interface_info->endpoint = NO_ENDPOINT8;
1855
1856             usb_trans_info->interface_info->interfaceClass = alternate_setting.interfaceClass;
1857             usb_trans_info->interface_info->interfaceSubclass = alternate_setting.interfaceSubclass;
1858             usb_trans_info->interface_info->interfaceProtocol = alternate_setting.interfaceProtocol;
1859             usb_trans_info->interface_info->deviceVendor      = usb_conv_info->deviceVendor;
1860             usb_trans_info->interface_info->deviceProduct     = usb_conv_info->deviceProduct;
1861         }
1862     }
1863     offset += 1;
1864
1865     /* bInterfaceSubClass */
1866     switch (usb_conv_info->interfaceClass) {
1867     case IF_CLASS_COMMUNICATIONS:
1868         proto_tree_add_item(tree, hf_usb_bInterfaceSubClass_cdc, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1869         break;
1870     case IF_CLASS_HID:
1871         proto_tree_add_item(tree, hf_usb_bInterfaceSubClass_hid, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1872         break;
1873     case IF_CLASS_APPLICATION_SPECIFIC:
1874         proto_tree_add_item(tree, hf_usb_bInterfaceSubClass_app, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1875         break;
1876     default:
1877         proto_tree_add_item(tree, hf_usb_bInterfaceSubClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1878     }
1879
1880     /* save the subclass so we can access it later in class-specific descriptors */
1881     usb_conv_info->interfaceSubclass = tvb_get_guint8(tvb, offset);
1882     offset += 1;
1883
1884     /* bInterfaceProtocol */
1885     switch (usb_conv_info->interfaceClass) {
1886     case IF_CLASS_COMMUNICATIONS:
1887         proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_cdc, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1888         break;
1889     case IF_CLASS_CDC_DATA:
1890         proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_cdc_data, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1891         break;
1892     case IF_CLASS_APPLICATION_SPECIFIC:
1893         switch (usb_conv_info->interfaceSubclass) {
1894         case 0x01:
1895             proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_app_dfu, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1896             break;
1897         case 0x02:
1898             proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_app_irda, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1899             break;
1900         case 0x03:
1901             proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_app_usb_test_and_measurement, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1902             break;
1903         default:
1904             proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1905         }
1906         break;
1907     case IF_CLASS_HID:
1908         if (usb_conv_info->interfaceSubclass == 1) {
1909             proto_tree_add_item(tree, hf_usb_bInterfaceProtocol_hid_boot, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1910             break;
1911         }
1912
1913         proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1914
1915         break;
1916     default:
1917         proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1918     }
1919
1920     usb_conv_info->interfaceProtocol = tvb_get_guint8(tvb, offset);
1921     offset += 1;
1922
1923     /* iInterface */
1924     proto_tree_add_item(tree, hf_usb_iInterface, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1925     offset += 1;
1926
1927     proto_item_set_len(item, len);
1928
1929     if (offset < old_offset+len) {
1930         /* skip unknown records */
1931         offset = old_offset + len;
1932     }
1933
1934     return offset;
1935 }
1936
1937 /* 9.6.6 */
1938 const true_false_string tfs_endpoint_direction = {
1939     "IN Endpoint",
1940     "OUT Endpoint"
1941 };
1942
1943 void dissect_usb_endpoint_address(proto_tree *tree, tvbuff_t *tvb, int offset)
1944 {
1945     proto_item *endpoint_item;
1946     proto_tree *endpoint_tree;
1947     guint8      endpoint;
1948
1949     endpoint_item = proto_tree_add_item(tree, hf_usb_bEndpointAddress, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1950     endpoint_tree = proto_item_add_subtree(endpoint_item, ett_configuration_bEndpointAddress);
1951
1952     endpoint = tvb_get_guint8(tvb, offset)&0x0f;
1953     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_direction, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1954     proto_item_append_text(endpoint_item, "  %s", (tvb_get_guint8(tvb, offset)&0x80)?"IN":"OUT");
1955     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_number, tvb, offset, 1, ENC_LITTLE_ENDIAN);
1956     proto_item_append_text(endpoint_item, "  Endpoint:%d", endpoint);
1957 }
1958
1959 int
1960 dissect_usb_endpoint_descriptor(packet_info *pinfo, proto_tree *parent_tree,
1961                                 tvbuff_t *tvb, int offset,
1962                                 usb_conv_info_t  *usb_conv_info)
1963 {
1964     proto_item       *item;
1965     proto_tree       *tree;
1966     proto_item       *ep_attrib_item;
1967     proto_tree       *ep_attrib_tree;
1968     proto_item       *ep_pktsize_item;
1969     proto_tree       *ep_pktsize_tree;
1970     int               old_offset     = offset;
1971     guint8            endpoint;
1972     guint8            ep_type;
1973     guint8            len;
1974     usb_trans_info_t *usb_trans_info = NULL;
1975
1976     if (usb_conv_info)
1977         usb_trans_info = usb_conv_info->usb_trans_info;
1978
1979     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "ENDPOINT DESCRIPTOR");
1980
1981     len = tvb_get_guint8(tvb, offset);
1982     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
1983     offset += 2;
1984
1985     endpoint = tvb_get_guint8(tvb, offset)&0x0f;
1986     dissect_usb_endpoint_address(tree, tvb, offset);
1987     offset += 1;
1988
1989     /* Together with class from the interface descriptor we know what kind
1990      * of class the device at endpoint is.
1991      * Make sure a conversation exists for this endpoint and attach a
1992      * usb_conv_into_t structure to it.
1993      *
1994      * All endpoints for the same interface descriptor share the same
1995      * usb_conv_info structure.
1996      */
1997     if ((!pinfo->fd->flags.visited) && usb_trans_info && usb_trans_info->interface_info) {
1998         conversation_t *conversation = NULL;
1999
2000         if (pinfo->destport == NO_ENDPOINT) {
2001             address tmp_addr;
2002             usb_address_t *usb_addr = wmem_new0(wmem_packet_scope(), usb_address_t);
2003
2004             /* packet is sent from a USB device's endpoint 0 to the host
2005              * replace endpoint 0 with the endpoint of this descriptor
2006              * and find the corresponding conversation
2007              */
2008             usb_addr->bus_id = ((const usb_address_t *)(pinfo->src.data))->bus_id;
2009             usb_addr->device = ((const usb_address_t *)(pinfo->src.data))->device;
2010             usb_addr->endpoint = GUINT32_TO_LE(endpoint);
2011             set_address(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)usb_addr);
2012             conversation = get_usb_conversation(pinfo, &tmp_addr, &pinfo->dst, usb_addr->endpoint, pinfo->destport);
2013         }
2014
2015         if (conversation) {
2016             usb_trans_info->interface_info->endpoint = endpoint;
2017             conversation_add_proto_data(conversation, proto_usb, usb_trans_info->interface_info);
2018         }
2019     }
2020
2021     /* bmAttributes */
2022     ep_type = ENDPOINT_TYPE(tvb_get_guint8(tvb, offset));
2023
2024     ep_attrib_item = proto_tree_add_item(tree, hf_usb_bmAttributes, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2025     ep_attrib_tree = proto_item_add_subtree(ep_attrib_item, ett_endpoint_bmAttributes);
2026
2027     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeTransfer, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2028     if (ep_type==USB_EP_ISOCHRONOUS) {
2029         proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeSynchonisation, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2030         proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeBehaviour, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2031     }
2032     offset += 1;
2033
2034     /* wMaxPacketSize */
2035     ep_pktsize_item = proto_tree_add_item(tree, hf_usb_wMaxPacketSize, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2036     ep_pktsize_tree = proto_item_add_subtree(ep_pktsize_item, ett_endpoint_wMaxPacketSize);
2037     if ((ep_type == ENDPOINT_TYPE_INTERRUPT) || (ep_type == ENDPOINT_TYPE_ISOCHRONOUS)) {
2038         proto_tree_add_item(ep_pktsize_tree, hf_usb_wMaxPacketSize_slots, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2039     }
2040     proto_tree_add_item(ep_pktsize_tree, hf_usb_wMaxPacketSize_size, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2041     offset+=2;
2042
2043     /* bInterval */
2044     proto_tree_add_item(tree, hf_usb_bInterval, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2045     offset += 1;
2046
2047     proto_item_set_len(item, len);
2048
2049     if (offset < old_offset+len) {
2050         /* skip unknown records */
2051         offset = old_offset + len;
2052     }
2053
2054     return offset;
2055 }
2056
2057 /* ECN */
2058 static int
2059 dissect_usb_interface_assn_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
2060                                       tvbuff_t *tvb, int offset,
2061                                       usb_conv_info_t  *usb_conv_info _U_)
2062 {
2063     proto_item *item;
2064     proto_tree *tree;
2065     int         old_offset = offset;
2066
2067     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "INTERFACE ASSOCIATION DESCRIPTOR");
2068
2069     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
2070     offset += 2;
2071
2072     /* bFirstInterface */
2073     proto_tree_add_item(tree, hf_usb_bFirstInterface, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2074     offset += 1;
2075
2076     /* bInterfaceCount */
2077     proto_tree_add_item(tree, hf_usb_bInterfaceCount, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2078     offset += 1;
2079
2080     /* bFunctionClass */
2081     proto_tree_add_item(tree, hf_usb_bFunctionClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2082     offset += 1;
2083
2084     /* bFunctionSubclass */
2085     proto_tree_add_item(tree, hf_usb_bFunctionSubClass, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2086     offset += 1;
2087
2088     /* bFunctionProtocol */
2089     proto_tree_add_item(tree, hf_usb_bFunctionProtocol, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2090     offset += 1;
2091
2092     /* iFunction */
2093     proto_tree_add_item(tree, hf_usb_iFunction, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2094     offset += 1;
2095
2096     proto_item_set_len(item, offset-old_offset);
2097
2098     return offset;
2099 }
2100
2101 int
2102 dissect_usb_unknown_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
2103                                tvbuff_t *tvb, int offset,
2104                                usb_conv_info_t  *usb_conv_info _U_)
2105 {
2106     proto_item *item;
2107     proto_tree *tree;
2108     guint8      bLength;
2109
2110     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "UNKNOWN DESCRIPTOR");
2111
2112     bLength = tvb_get_guint8(tvb, offset);
2113     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
2114     offset += bLength;
2115
2116     proto_item_set_len(item, bLength);
2117
2118     return offset;
2119 }
2120
2121 /* 9.6.3 */
2122 static const true_false_string tfs_mustbeone = {
2123     "Must be 1 for USB 1.1 and higher",
2124     "FIXME: Is this a USB 1.0 device"
2125 };
2126 static const true_false_string tfs_selfpowered = {
2127     "This device is SELF-POWERED",
2128     "This device is powered from the USB bus"
2129 };
2130 static const true_false_string tfs_remotewakeup = {
2131     "This device supports REMOTE WAKEUP",
2132     "This device does NOT support remote wakeup"
2133 };
2134 static int
2135 dissect_usb_configuration_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
2136                                      tvbuff_t *tvb, int offset,
2137                                      usb_conv_info_t  *usb_conv_info)
2138 {
2139     proto_item *item;
2140     proto_tree *tree;
2141     int         old_offset = offset;
2142     guint16     len;
2143     proto_item *flags_item;
2144     proto_tree *flags_tree;
2145     guint8      flags;
2146     proto_item *power_item;
2147     guint8      power;
2148     gboolean    truncation_expected;
2149     usb_trans_info_t *usb_trans_info;
2150
2151     usb_trans_info = usb_conv_info->usb_trans_info;
2152
2153     usb_conv_info->interfaceClass    = IF_CLASS_UNKNOWN;
2154     usb_conv_info->interfaceSubclass = IF_SUBCLASS_UNKNOWN;
2155     usb_conv_info->interfaceProtocol = IF_PROTOCOL_UNKNOWN;
2156
2157     tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_descriptor_device, &item, "CONFIGURATION DESCRIPTOR");
2158
2159     dissect_usb_descriptor_header(tree, tvb, offset, NULL);
2160     offset += 2;
2161
2162     /* wTotalLength */
2163     proto_tree_add_item(tree, hf_usb_wTotalLength, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2164     len = tvb_get_letohs(tvb, offset);
2165     offset+=2;
2166
2167     /* bNumInterfaces */
2168     proto_tree_add_item(tree, hf_usb_bNumInterfaces, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2169     offset += 1;
2170
2171     /* bConfigurationValue */
2172     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2173     offset += 1;
2174
2175     /* iConfiguration */
2176     proto_tree_add_item(tree, hf_usb_iConfiguration, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2177     offset += 1;
2178
2179     /* bmAttributes */
2180     flags_item = proto_tree_add_item(tree, hf_usb_configuration_bmAttributes, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2181     flags_tree = proto_item_add_subtree(flags_item, ett_configuration_bmAttributes);
2182
2183     flags = tvb_get_guint8(tvb, offset);
2184     proto_tree_add_item(flags_tree, hf_usb_configuration_legacy10buspowered, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2185     proto_tree_add_item(flags_tree, hf_usb_configuration_selfpowered, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2186     proto_item_append_text(flags_item, "  %sSELF-POWERED", (flags&0x40)?"":"NOT ");
2187     proto_tree_add_item(flags_tree, hf_usb_configuration_remotewakeup, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2188     proto_item_append_text(flags_item, "  %sREMOTE-WAKEUP", (flags&0x20)?"":"NO ");
2189     offset += 1;
2190
2191     /* bMaxPower */
2192     power_item = proto_tree_add_item(tree, hf_usb_bMaxPower, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2193     power = tvb_get_guint8(tvb, offset);
2194     proto_item_append_text(power_item, "  (%dmA)", power*2);
2195     offset += 1;
2196
2197     /* initialize interface_info to NULL */
2198     usb_trans_info->interface_info = NULL;
2199
2200     truncation_expected = (usb_trans_info->setup.wLength < len);
2201
2202     /* decode any additional interface and endpoint descriptors */
2203     while(len>(offset-old_offset)) {
2204         guint8 next_type;
2205         guint8 next_len = 0;
2206         gint remaining_tvb, remaining_len;
2207         tvbuff_t *next_tvb = NULL;
2208
2209         /* Handle truncated descriptors appropriately */
2210         remaining_tvb = tvb_reported_length_remaining(tvb, offset);
2211         if (remaining_tvb > 0) {
2212             next_len  = tvb_get_guint8(tvb, offset);
2213             remaining_len = len - (offset - old_offset);
2214             if ((next_len < 3) || (next_len > remaining_len)) {
2215                 proto_tree_add_expert_format(parent_tree, pinfo, &ei_usb_desc_length_invalid,
2216                     tvb, offset, 1, "Invalid descriptor length: %u",  next_len);
2217                 item = NULL;
2218                 break;
2219             }
2220         }
2221
2222         if ((remaining_tvb == 0) || (next_len > remaining_tvb)) {
2223             if (truncation_expected)
2224                 break;
2225         }
2226
2227         next_type = tvb_get_guint8(tvb, offset+1);
2228         switch(next_type) {
2229         case USB_DT_INTERFACE:
2230             offset = dissect_usb_interface_descriptor(pinfo, parent_tree, tvb, offset, usb_conv_info);
2231             break;
2232         case USB_DT_ENDPOINT:
2233             offset = dissect_usb_endpoint_descriptor(pinfo, parent_tree, tvb, offset, usb_conv_info);
2234             break;
2235         case USB_DT_INTERFACE_ASSOCIATION:
2236             offset = dissect_usb_interface_assn_descriptor(pinfo, parent_tree, tvb, offset, usb_conv_info);
2237             break;
2238         default:
2239             next_tvb = tvb_new_subset_length(tvb, offset, next_len);
2240             if (dissector_try_uint_new(usb_descriptor_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, parent_tree, TRUE, usb_conv_info)) {
2241                 offset += next_len;
2242             } else {
2243                 offset = dissect_usb_unknown_descriptor(pinfo, parent_tree, tvb, offset, usb_conv_info);
2244             }
2245             break;
2246             /* was: return offset; */
2247         }
2248     }
2249
2250     proto_item_set_len(item, offset-old_offset);
2251
2252     return offset;
2253 }
2254
2255 /* 9.4.3 */
2256 static int
2257 dissect_usb_setup_get_descriptor_request(packet_info *pinfo, proto_tree *tree,
2258                                          tvbuff_t *tvb, int offset,
2259                                          usb_conv_info_t  *usb_conv_info)
2260 {
2261     usb_trans_info_t *usb_trans_info, trans_info;
2262
2263     if (usb_conv_info)
2264         usb_trans_info = usb_conv_info->usb_trans_info;
2265     else
2266         usb_trans_info = &trans_info;
2267
2268     /* descriptor index */
2269     proto_tree_add_item(tree, hf_usb_descriptor_index, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2270     usb_trans_info->u.get_descriptor.usb_index = tvb_get_guint8(tvb, offset);
2271     offset += 1;
2272
2273     /* descriptor type */
2274     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2275     usb_trans_info->u.get_descriptor.type = tvb_get_guint8(tvb, offset);
2276     offset += 1;
2277     col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
2278         val_to_str_ext(usb_trans_info->u.get_descriptor.type, &std_descriptor_type_vals_ext, "Unknown type %u"));
2279
2280     /* language id */
2281     proto_tree_add_item(tree, hf_usb_language_id, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2282     offset+=2;
2283
2284     /* length */
2285     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2286     offset += 2;
2287
2288     return offset;
2289 }
2290
2291 static int
2292 dissect_usb_setup_get_descriptor_response(packet_info *pinfo, proto_tree *tree,
2293                                           tvbuff_t *tvb, int offset,
2294                                           usb_conv_info_t  *usb_conv_info)
2295 {
2296     usb_trans_info_t *usb_trans_info;
2297
2298     usb_trans_info = usb_conv_info->usb_trans_info;
2299
2300     col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
2301         val_to_str_ext(usb_trans_info->u.get_descriptor.type, &std_descriptor_type_vals_ext, "Unknown type %u"));
2302
2303     switch(usb_trans_info->u.get_descriptor.type) {
2304         case USB_DT_INTERFACE:
2305         case USB_DT_ENDPOINT:
2306             /* an interface or an endpoint descriptor can only be accessed
2307                as part of a configuration descriptor */
2308             break;
2309         case USB_DT_DEVICE:
2310             offset = dissect_usb_device_descriptor(pinfo, tree, tvb, offset, usb_conv_info);
2311             break;
2312         case USB_DT_CONFIG:
2313             offset = dissect_usb_configuration_descriptor(pinfo, tree, tvb, offset, usb_conv_info);
2314             break;
2315         case USB_DT_STRING:
2316             offset = dissect_usb_string_descriptor(pinfo, tree, tvb, offset, usb_conv_info);
2317             break;
2318         case USB_DT_DEVICE_QUALIFIER:
2319             offset = dissect_usb_device_qualifier_descriptor(pinfo, tree, tvb, offset, usb_conv_info);
2320             break;
2321         default:
2322             /* XXX dissect the descriptor coming back from the device */
2323             {
2324                 guint len = tvb_reported_length_remaining(tvb, offset);
2325                 proto_tree_add_bytes_format(tree, hf_usb_get_descriptor_resp_generic, tvb, offset, len, NULL,
2326                                             "GET DESCRIPTOR Response data (unknown descriptor type %u): %s",
2327                                             usb_trans_info->u.get_descriptor.type,
2328                                             tvb_bytes_to_str(wmem_packet_scope(), tvb, offset, len));
2329                 offset = offset + len;
2330             }
2331             break;
2332     }
2333
2334     return offset;
2335 }
2336
2337
2338 /*
2339  * These dissectors are used to dissect the setup part and the data
2340  * for URB_CONTROL_INPUT / GET INTERFACE
2341  */
2342
2343
2344 /* 9.4.4 */
2345 static int
2346 dissect_usb_setup_get_interface_request(packet_info *pinfo _U_, proto_tree *tree,
2347                                         tvbuff_t *tvb, int offset,
2348                                         usb_conv_info_t  *usb_conv_info _U_)
2349 {
2350     /* zero */
2351     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2352     offset += 2;
2353
2354     /* interface */
2355     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2356     offset += 2;
2357
2358     /* length */
2359     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2360     offset += 2;
2361
2362     return offset;
2363 }
2364
2365 static int
2366 dissect_usb_setup_get_interface_response(packet_info *pinfo _U_, proto_tree *tree,
2367                                          tvbuff_t *tvb, int offset,
2368                                          usb_conv_info_t  *usb_conv_info _U_)
2369 {
2370     /* alternate setting */
2371     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2372     offset += 1;
2373
2374     return offset;
2375 }
2376
2377
2378 /*
2379  * These dissectors are used to dissect the setup part and the data
2380  * for URB_CONTROL_INPUT / GET STATUS
2381  */
2382
2383
2384 /* 9.4.5 */
2385 static int
2386 dissect_usb_setup_get_status_request(packet_info *pinfo _U_, proto_tree *tree,
2387                                      tvbuff_t *tvb, int offset,
2388                                      usb_conv_info_t  *usb_conv_info _U_)
2389 {
2390     /* zero */
2391     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2392     offset += 2;
2393
2394     /* zero/interface/endpoint */
2395     /* XXX - check based on request type */
2396     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2397     offset += 2;
2398
2399     /* length */
2400     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2401     offset += 2;
2402
2403     return offset;
2404 }
2405
2406 static int
2407 dissect_usb_setup_get_status_response(packet_info *pinfo _U_, proto_tree *tree,
2408                                       tvbuff_t *tvb, int offset,
2409                                       usb_conv_info_t  *usb_conv_info _U_)
2410 {
2411     /* status */
2412     /* XXX - show bits */
2413     proto_tree_add_item(tree, hf_usb_wStatus, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2414     offset += 2;
2415
2416     return offset;
2417 }
2418
2419
2420 /*
2421  * These dissectors are used to dissect the setup part and the data
2422  * for URB_CONTROL_INPUT / SET ADDRESS
2423  */
2424
2425
2426 /* 9.4.6 */
2427 static int
2428 dissect_usb_setup_set_address_request(packet_info *pinfo _U_, proto_tree *tree,
2429                                       tvbuff_t *tvb, int offset,
2430                                       usb_conv_info_t  *usb_conv_info _U_)
2431 {
2432     /* device address */
2433     proto_tree_add_item(tree, hf_usb_device_address, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2434     offset += 2;
2435
2436     /* zero */
2437     proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2438     offset += 2;
2439
2440     /* zero */
2441     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2442     offset += 2;
2443
2444     return offset;
2445 }
2446
2447 static int
2448 dissect_usb_setup_set_address_response(packet_info *pinfo _U_, proto_tree *tree _U_,
2449                                        tvbuff_t *tvb _U_, int offset,
2450                                        usb_conv_info_t  *usb_conv_info _U_)
2451 {
2452     return offset;
2453 }
2454
2455
2456 /*
2457  * These dissectors are used to dissect the setup part and the data
2458  * for URB_CONTROL_INPUT / SET CONFIGURATION
2459  */
2460
2461
2462 /* 9.4.7 */
2463 static int
2464 dissect_usb_setup_set_configuration_request(packet_info *pinfo _U_, proto_tree *tree,
2465                                             tvbuff_t *tvb, int offset,
2466                                             usb_conv_info_t  *usb_conv_info _U_)
2467 {
2468     /* configuration value */
2469     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2470     offset += 2;
2471
2472     /* zero */
2473     proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2474     offset += 2;
2475
2476     /* zero */
2477     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2478     offset += 2;
2479
2480     return offset;
2481 }
2482
2483 static int
2484 dissect_usb_setup_set_configuration_response(packet_info *pinfo _U_, proto_tree *tree _U_,
2485                                              tvbuff_t *tvb _U_, int offset,
2486                                              usb_conv_info_t  *usb_conv_info _U_)
2487 {
2488     return offset;
2489 }
2490
2491
2492 /*
2493  * These dissectors are used to dissect the setup part and the data
2494  * for URB_CONTROL_INPUT / SET FEATURE
2495  */
2496
2497
2498 /* 9.4.9 */
2499 static int
2500 dissect_usb_setup_set_feature_request(packet_info *pinfo _U_, proto_tree *tree,
2501                                       tvbuff_t *tvb, int offset,
2502                                       usb_conv_info_t  *usb_conv_info)
2503 {
2504     guint8 recip;
2505
2506     if (usb_conv_info) {
2507         recip = USB_RECIPIENT(usb_conv_info->usb_trans_info->setup.requesttype);
2508
2509         /* feature selector */
2510         switch (recip) {
2511         case RQT_SETUP_RECIPIENT_DEVICE:
2512             proto_tree_add_item(tree, hf_usb_device_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2513             break;
2514
2515         case RQT_SETUP_RECIPIENT_INTERFACE:
2516             proto_tree_add_item(tree, hf_usb_interface_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2517             break;
2518
2519         case RQT_SETUP_RECIPIENT_ENDPOINT:
2520             proto_tree_add_item(tree, hf_usb_endpoint_wFeatureSelector, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2521             break;
2522
2523         case RQT_SETUP_RECIPIENT_OTHER:
2524         default:
2525             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2526             break;
2527         }
2528     } else {
2529         /* No conversation information, so recipient type is unknown */
2530         proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2531     }
2532     offset += 2;
2533
2534     /* zero/interface/endpoint or test selector */
2535     /* XXX - check based on request type */
2536     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2537     offset += 2;
2538
2539     /* zero */
2540     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2541     offset += 2;
2542
2543     return offset;
2544 }
2545
2546 static int
2547 dissect_usb_setup_set_feature_response(packet_info *pinfo _U_, proto_tree *tree _U_,
2548                                        tvbuff_t *tvb _U_, int offset,
2549                                        usb_conv_info_t  *usb_conv_info _U_)
2550 {
2551     return offset;
2552 }
2553
2554
2555 /*
2556  * These dissectors are used to dissect the setup part and the data
2557  * for URB_CONTROL_INPUT / SET INTERFACE
2558  */
2559
2560
2561 /* 9.4.10 */
2562 static int
2563 dissect_usb_setup_set_interface_request(packet_info *pinfo, proto_tree *tree,
2564                                         tvbuff_t *tvb, int offset,
2565                                         usb_conv_info_t  *usb_conv_info _U_)
2566 {
2567     guint8 alt_setting, interface_num;
2568
2569     /* alternate setting */
2570     alt_setting = tvb_get_guint8(tvb, offset);
2571     proto_tree_add_uint(tree, hf_usb_bAlternateSetting, tvb, offset, 2, alt_setting);
2572     offset += 2;
2573
2574     /* interface */
2575     interface_num = tvb_get_guint8(tvb, offset);
2576     proto_tree_add_uint(tree, hf_usb_wInterface, tvb, offset, 2, interface_num);
2577     offset += 2;
2578
2579     /* zero */
2580     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2581     offset += 2;
2582
2583     if (!PINFO_FD_VISITED(pinfo)) {
2584         guint i, count;
2585         usb_conv_info_t *iface_conv_info = get_usb_iface_conv_info(pinfo, interface_num);
2586
2587         /* update the conversation info with the selected alternate setting */
2588         count = wmem_array_get_count(iface_conv_info->alt_settings);
2589         for (i = 0; i < count; i++) {
2590             usb_alt_setting_t *alternate_setting = (usb_alt_setting_t *)wmem_array_index(iface_conv_info->alt_settings, i);
2591
2592             if (alternate_setting->altSetting == alt_setting) {
2593                 iface_conv_info->interfaceClass = alternate_setting->interfaceClass;
2594                 iface_conv_info->interfaceSubclass = alternate_setting->interfaceSubclass;
2595                 iface_conv_info->interfaceProtocol = alternate_setting->interfaceProtocol;
2596                 break;
2597             }
2598         }
2599     }
2600
2601     return offset;
2602 }
2603
2604 static int
2605 dissect_usb_setup_set_interface_response(packet_info *pinfo _U_, proto_tree *tree _U_,
2606                                          tvbuff_t *tvb _U_, int offset,
2607                                          usb_conv_info_t  *usb_conv_info _U_)
2608 {
2609     return offset;
2610 }
2611
2612
2613 /*
2614  * These dissectors are used to dissect the setup part and the data
2615  * for URB_CONTROL_INPUT / SYNCH FRAME
2616  */
2617
2618
2619 /* 9.4.11 */
2620 static int
2621 dissect_usb_setup_synch_frame_request(packet_info *pinfo _U_, proto_tree *tree,
2622                                       tvbuff_t *tvb, int offset,
2623                                       usb_conv_info_t  *usb_conv_info _U_)
2624 {
2625     /* zero */
2626     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2627     offset += 2;
2628
2629     /* endpoint */
2630     /* XXX */
2631     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2632     offset += 2;
2633
2634     /* two */
2635     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2636     offset += 2;
2637
2638     return offset;
2639 }
2640
2641 static int
2642 dissect_usb_setup_synch_frame_response(packet_info *pinfo _U_, proto_tree *tree _U_,
2643                                        tvbuff_t *tvb _U_, int offset,
2644                                        usb_conv_info_t  *usb_conv_info _U_)
2645 {
2646     /* frame number */
2647     proto_tree_add_item(tree, hf_usb_wFrameNumber, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2648     offset += 2;
2649
2650     return offset;
2651 }
2652
2653 /* Dissector used for unknown USB setup request/responses */
2654 static int
2655 dissect_usb_setup_generic(packet_info *pinfo _U_, proto_tree *tree ,
2656                                        tvbuff_t *tvb, int offset,
2657                                        usb_conv_info_t  *usb_conv_info _U_)
2658 {
2659
2660     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2661     offset += 2;
2662     proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2663     offset += 2;
2664     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2665     offset += 2;
2666
2667     return offset;
2668 }
2669
2670
2671
2672 typedef int (*usb_setup_dissector)(packet_info *pinfo, proto_tree *tree,
2673                                    tvbuff_t *tvb, int offset,
2674                                    usb_conv_info_t  *usb_conv_info);
2675
2676 typedef struct _usb_setup_dissector_table_t {
2677     guint8 request;
2678     usb_setup_dissector dissector;
2679
2680 } usb_setup_dissector_table_t;
2681 static const usb_setup_dissector_table_t setup_request_dissectors[] = {
2682     {USB_SETUP_GET_STATUS,        dissect_usb_setup_get_status_request},
2683     {USB_SETUP_CLEAR_FEATURE,     dissect_usb_setup_clear_feature_request},
2684     {USB_SETUP_SET_FEATURE,       dissect_usb_setup_set_feature_request},
2685     {USB_SETUP_SET_ADDRESS,       dissect_usb_setup_set_address_request},
2686     {USB_SETUP_GET_DESCRIPTOR,    dissect_usb_setup_get_descriptor_request},
2687     {USB_SETUP_SET_CONFIGURATION, dissect_usb_setup_set_configuration_request},
2688     {USB_SETUP_GET_INTERFACE,     dissect_usb_setup_get_interface_request},
2689     {USB_SETUP_SET_INTERFACE,     dissect_usb_setup_set_interface_request},
2690     {USB_SETUP_SYNCH_FRAME,       dissect_usb_setup_synch_frame_request},
2691     {0, NULL}
2692 };
2693
2694 static const usb_setup_dissector_table_t setup_response_dissectors[] = {
2695     {USB_SETUP_GET_STATUS,        dissect_usb_setup_get_status_response},
2696     {USB_SETUP_CLEAR_FEATURE,     dissect_usb_setup_clear_feature_response},
2697     {USB_SETUP_SET_FEATURE,       dissect_usb_setup_set_feature_response},
2698     {USB_SETUP_SET_ADDRESS,       dissect_usb_setup_set_address_response},
2699     {USB_SETUP_GET_DESCRIPTOR,    dissect_usb_setup_get_descriptor_response},
2700     {USB_SETUP_GET_CONFIGURATION, dissect_usb_setup_get_configuration_response},
2701     {USB_SETUP_SET_CONFIGURATION, dissect_usb_setup_set_configuration_response},
2702     {USB_SETUP_GET_INTERFACE,     dissect_usb_setup_get_interface_response},
2703     {USB_SETUP_SET_INTERFACE,     dissect_usb_setup_set_interface_response},
2704     {USB_SETUP_SYNCH_FRAME,       dissect_usb_setup_synch_frame_response},
2705     {0, NULL}
2706 };
2707
2708 static const value_string setup_request_names_vals[] = {
2709     {USB_SETUP_GET_STATUS,              "GET STATUS"},
2710     {USB_SETUP_CLEAR_FEATURE,           "CLEAR FEATURE"},
2711     {USB_SETUP_SET_FEATURE,             "SET FEATURE"},
2712     {USB_SETUP_SET_ADDRESS,             "SET ADDRESS"},
2713     {USB_SETUP_GET_DESCRIPTOR,          "GET DESCRIPTOR"},
2714     {USB_SETUP_SET_DESCRIPTOR,          "SET DESCRIPTOR"},
2715     {USB_SETUP_GET_CONFIGURATION,       "GET CONFIGURATION"},
2716     {USB_SETUP_SET_CONFIGURATION,       "SET CONFIGURATION"},
2717     {USB_SETUP_GET_INTERFACE,           "GET INTERFACE"},
2718     {USB_SETUP_SET_INTERFACE,           "SET INTERFACE"},
2719     {USB_SETUP_SYNCH_FRAME,             "SYNCH FRAME"},
2720     {USB_SETUP_SET_SEL,                 "SET SEL"},
2721     {USB_SETUP_SET_ISOCH_DELAY,         "SET ISOCH DELAY"},
2722     {0, NULL}
2723 };
2724 static value_string_ext setup_request_names_vals_ext = VALUE_STRING_EXT_INIT(setup_request_names_vals);
2725
2726
2727 static const true_false_string tfs_bmrequesttype_direction = {
2728     "Device-to-host",
2729     "Host-to-device"
2730 };
2731
2732 static const value_string bmrequesttype_type_vals[] = {
2733     {RQT_SETUP_TYPE_STANDARD, "Standard"},
2734     {RQT_SETUP_TYPE_CLASS,    "Class"},
2735     {RQT_SETUP_TYPE_VENDOR,   "Vendor"},
2736     {0, NULL}
2737 };
2738
2739 static const value_string bmrequesttype_recipient_vals[] = {
2740     {RQT_SETUP_RECIPIENT_DEVICE,    "Device" },
2741     {RQT_SETUP_RECIPIENT_INTERFACE, "Interface" },
2742     {RQT_SETUP_RECIPIENT_ENDPOINT,  "Endpoint" },
2743     {RQT_SETUP_RECIPIENT_OTHER,     "Other" },
2744     {0, NULL }
2745 };
2746
2747 /* Dissector used for standard usb setup requests */
2748 static int
2749 dissect_usb_standard_setup_request(packet_info *pinfo, proto_tree *tree ,
2750                                    tvbuff_t *tvb, usb_conv_info_t  *usb_conv_info,
2751                                    usb_trans_info_t *usb_trans_info)
2752 {
2753     gint offset = 0;
2754     const usb_setup_dissector_table_t *tmp;
2755     usb_setup_dissector dissector;
2756
2757     proto_tree_add_item(tree, hf_usb_request, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2758     offset += 1;
2759
2760     col_add_fstr(pinfo->cinfo, COL_INFO, "%s Request",
2761             val_to_str_ext(usb_trans_info->setup.request, &setup_request_names_vals_ext, "Unknown type %x"));
2762
2763     dissector = NULL;
2764     for(tmp = setup_request_dissectors;tmp->dissector;tmp++) {
2765         if (tmp->request == usb_trans_info->setup.request) {
2766             dissector = tmp->dissector;
2767             break;
2768         }
2769     }
2770
2771     if (!dissector) {
2772             dissector = &dissect_usb_setup_generic;
2773     }
2774
2775     offset = dissector(pinfo, tree, tvb, offset, usb_conv_info);
2776
2777     return offset;
2778
2779 }
2780
2781 /* Dissector used for standard usb setup responses */
2782 static int
2783 dissect_usb_standard_setup_response(packet_info *pinfo, proto_tree *tree,
2784                                     tvbuff_t *tvb, int offset,
2785                                     usb_conv_info_t  *usb_conv_info)
2786 {
2787     const usb_setup_dissector_table_t *tmp;
2788     usb_setup_dissector dissector;
2789
2790
2791     col_add_fstr(pinfo->cinfo, COL_INFO, "%s Response",
2792         val_to_str_ext(usb_conv_info->usb_trans_info->setup.request,
2793             &setup_request_names_vals_ext, "Unknown type %x"));
2794
2795     dissector = NULL;
2796     for(tmp = setup_response_dissectors;tmp->dissector;tmp++) {
2797         if (tmp->request == usb_conv_info->usb_trans_info->setup.request) {
2798             dissector = tmp->dissector;
2799             break;
2800         }
2801     }
2802
2803     if (dissector) {
2804         offset = dissector(pinfo, tree, tvb, offset, usb_conv_info);
2805     } else {
2806         gint length_remaining = tvb_reported_length_remaining(tvb, offset);
2807         if (length_remaining > 0) {
2808             proto_tree_add_item(tree, hf_usb_control_response_generic,
2809                                 tvb, offset, length_remaining, ENC_NA);
2810             offset += length_remaining;
2811         }
2812     }
2813
2814     return offset;
2815 }
2816
2817
2818 static void
2819 usb_tap_queue_packet(packet_info *pinfo, guint8 urb_type,
2820                      usb_conv_info_t *usb_conv_info)
2821 {
2822     usb_tap_data_t *tap_data;
2823
2824     tap_data                = wmem_new(wmem_packet_scope(), usb_tap_data_t);
2825     tap_data->urb_type      = urb_type;
2826     tap_data->transfer_type = (guint8)(usb_conv_info->transfer_type);
2827     tap_data->conv_info     = usb_conv_info;
2828     tap_data->trans_info    = usb_conv_info->usb_trans_info;
2829
2830     tap_queue_packet(usb_tap, pinfo, tap_data);
2831 }
2832
2833
2834 static gboolean
2835 is_usb_standard_setup_request(usb_trans_info_t *usb_trans_info)
2836 {
2837     guint8 type, recip;
2838
2839     type = USB_TYPE(usb_trans_info->setup.requesttype);
2840     recip = USB_RECIPIENT(usb_trans_info->setup.requesttype);
2841
2842     if (type != RQT_SETUP_TYPE_STANDARD)
2843         return FALSE;
2844
2845     /* the USB standards defines the GET_DESCRIPTOR request only as a
2846        request to a device
2847        if it's not aimed at a device, it's a non-standard request that
2848        should be handled by a class-specific dissector */
2849     if (usb_trans_info->setup.request == USB_SETUP_GET_DESCRIPTOR &&
2850             recip != RQT_SETUP_RECIPIENT_DEVICE) {
2851         return FALSE;
2852     }
2853
2854     return TRUE;
2855 }
2856
2857
2858 static gint
2859 try_dissect_next_protocol(proto_tree *tree, tvbuff_t *next_tvb, packet_info *pinfo,
2860         usb_conv_info_t *usb_conv_info, guint8 urb_type, proto_tree *urb_tree)
2861 {
2862     int                      ret;
2863     wmem_tree_key_t          key[4];
2864     guint32                  k_frame_number;
2865     guint32                  k_device_address;
2866     guint32                  k_bus_id;
2867     usb_trans_info_t        *usb_trans_info;
2868     heur_dtbl_entry_t       *hdtbl_entry;
2869     heur_dissector_list_t    heur_subdissector_list = NULL;
2870     dissector_table_t        usb_dissector_table = NULL;
2871     proto_item              *sub_item;
2872     device_product_data_t   *device_product_data;
2873     device_protocol_data_t  *device_protocol_data;
2874     guint8                   ctrl_recip;
2875     /* if we select the next dissector based on a class,
2876        this is the (device or interface) class we're using */
2877     guint32                  usb_class;
2878
2879     if (!usb_conv_info) {
2880         /*
2881          * Not enough information to choose the next protocol.
2882          * XXX - is there something we can still do here?
2883          */
2884         if (tvb_reported_length(next_tvb) > 0)
2885             call_dissector(data_handle, next_tvb, pinfo, tree);
2886
2887         return tvb_captured_length(next_tvb);
2888     }
2889
2890     /* try dissect by "usb.device" */
2891     ret = dissector_try_uint_new(device_to_dissector,
2892             (guint32)(usb_conv_info->bus_id<<16 | usb_conv_info->device_address),
2893             next_tvb, pinfo, tree, TRUE, usb_conv_info);
2894     if (ret)
2895         return tvb_captured_length(next_tvb);
2896
2897     k_frame_number = pinfo->num;
2898     k_device_address = usb_conv_info->device_address;
2899     k_bus_id = usb_conv_info->bus_id;
2900
2901     key[0].length = 1;
2902     key[0].key    = &k_device_address;
2903     key[1].length = 1;
2904     key[1].key    = &k_bus_id;
2905     key[2].length = 1;
2906     key[2].key    = &k_frame_number;
2907     key[3].length = 0;
2908     key[3].key    = NULL;
2909
2910     /* try dissect by "usb.protocol" */
2911     device_protocol_data = (device_protocol_data_t *)wmem_tree_lookup32_array_le(device_to_protocol_table, key);
2912
2913     if (device_protocol_data &&
2914             device_protocol_data->bus_id == usb_conv_info->bus_id &&
2915             device_protocol_data->device_address == usb_conv_info->device_address) {
2916                 ret = dissector_try_uint_new(protocol_to_dissector,
2917                         (guint32)device_protocol_data->protocol,
2918                         next_tvb, pinfo, tree, TRUE, usb_conv_info);
2919                 if (ret)
2920                     return tvb_captured_length(next_tvb);
2921     }
2922
2923     device_product_data = (device_product_data_t *)wmem_tree_lookup32_array_le(device_to_product_table, key);
2924
2925     if (device_product_data && device_product_data->bus_id == usb_conv_info->bus_id &&
2926             device_product_data->device_address == usb_conv_info->device_address) {
2927                 ret = dissector_try_uint_new(product_to_dissector,
2928                         (guint32)(device_product_data->vendor<<16 | device_product_data->product),
2929                         next_tvb, pinfo, tree, TRUE, usb_conv_info);
2930                 if (ret)
2931                     return tvb_captured_length(next_tvb);
2932     }
2933
2934     switch(usb_conv_info->transfer_type) {
2935         case URB_BULK:
2936             heur_subdissector_list = heur_bulk_subdissector_list;
2937             usb_dissector_table = usb_bulk_dissector_table;
2938             break;
2939
2940         case URB_INTERRUPT:
2941             heur_subdissector_list = heur_interrupt_subdissector_list;
2942             usb_dissector_table = usb_interrupt_dissector_table;
2943             break;
2944
2945         case URB_CONTROL:
2946             usb_trans_info = usb_conv_info->usb_trans_info;
2947             if (!usb_trans_info)
2948                 break;
2949
2950             /* for standard control requests and responses, there's no
2951                need to query dissector tables */
2952             if (is_usb_standard_setup_request(usb_trans_info))
2953                 break;
2954
2955             ctrl_recip = USB_RECIPIENT(usb_trans_info->setup.requesttype);
2956
2957             if (ctrl_recip == RQT_SETUP_RECIPIENT_DEVICE) {
2958                 /* a non-standard control message addressed to a device
2959                    this is not supported (I don't think it's used
2960                    anywhere) */
2961                 break;
2962             }
2963             else if (ctrl_recip == RQT_SETUP_RECIPIENT_INTERFACE) {
2964                 guint8 interface_num = usb_trans_info->setup.wIndex & 0xff;
2965
2966                 heur_subdissector_list = heur_control_subdissector_list;
2967                 usb_dissector_table = usb_control_dissector_table;
2968
2969                 usb_conv_info = get_usb_iface_conv_info(pinfo, interface_num);
2970                 usb_conv_info->usb_trans_info = usb_trans_info;
2971             }
2972             else if (ctrl_recip == RQT_SETUP_RECIPIENT_ENDPOINT) {
2973                 address               endpoint_addr;
2974                 gint                  endpoint;
2975                 guint32               src_endpoint, dst_endpoint;
2976                 conversation_t       *conversation;
2977
2978                 heur_subdissector_list = heur_control_subdissector_list;
2979                 usb_dissector_table = usb_control_dissector_table;
2980
2981                 endpoint = usb_trans_info->setup.wIndex & 0x0f;
2982
2983                 if (usb_conv_info->is_request) {
2984                     usb_address_t *dst_addr = wmem_new0(wmem_packet_scope(), usb_address_t);
2985                     dst_addr->bus_id = usb_conv_info->bus_id;
2986                     dst_addr->device = usb_conv_info->device_address;
2987                     dst_addr->endpoint = dst_endpoint = GUINT32_TO_LE(endpoint);
2988                     set_address(&endpoint_addr, AT_USB, USB_ADDR_LEN, (char *)dst_addr);
2989
2990                     conversation = get_usb_conversation(pinfo, &pinfo->src, &endpoint_addr, pinfo->srcport, dst_endpoint);
2991                 }
2992                 else {
2993                     usb_address_t *src_addr = wmem_new0(wmem_packet_scope(), usb_address_t);
2994                     src_addr->bus_id = usb_conv_info->bus_id;
2995                     src_addr->device = usb_conv_info->device_address;
2996                     src_addr->endpoint = src_endpoint = GUINT32_TO_LE(endpoint);
2997                     set_address(&endpoint_addr, AT_USB, USB_ADDR_LEN, (char *)src_addr);
2998
2999                     conversation  = get_usb_conversation(pinfo, &endpoint_addr, &pinfo->dst, src_endpoint, pinfo->destport);
3000                 }
3001
3002                 usb_conv_info = get_usb_conv_info(conversation);
3003                 usb_conv_info->usb_trans_info = usb_trans_info;
3004             }
3005             else {
3006                 /* the recipient is "other" or "reserved"
3007                    there's no way for us to determine the interfaceClass
3008                    we set the usb_dissector_table anyhow as some
3009                    dissectors register for control messages to
3010                    IF_CLASS_UNKNOWN (this should be fixed) */
3011                 heur_subdissector_list = heur_control_subdissector_list;
3012                 usb_dissector_table = usb_control_dissector_table;
3013             }
3014
3015             usb_tap_queue_packet(pinfo, urb_type, usb_conv_info);
3016             sub_item = proto_tree_add_uint(urb_tree, hf_usb_bInterfaceClass, next_tvb, 0, 0, usb_conv_info->interfaceClass);
3017             PROTO_ITEM_SET_GENERATED(sub_item);
3018             break;
3019
3020         default:
3021             break;
3022     }
3023
3024     if (try_heuristics && heur_subdissector_list) {
3025         ret = dissector_try_heuristic(heur_subdissector_list,
3026                 next_tvb, pinfo, tree, &hdtbl_entry, usb_conv_info);
3027         if (ret)
3028             return tvb_captured_length(next_tvb);
3029     }
3030
3031     if (usb_dissector_table) {
3032         /* we prefer the interface class unless it says we should refer
3033            to the device class
3034            XXX - use the device class if the interface class is unknown */
3035         if (usb_conv_info->interfaceClass == IF_CLASS_DEVICE) {
3036             usb_class = (usb_conv_info->device_protocol>>16) & 0xFF;
3037         }
3038         else {
3039             usb_class = usb_conv_info->interfaceClass;
3040         }
3041
3042         ret = dissector_try_uint_new(usb_dissector_table, usb_class,
3043                 next_tvb, pinfo, tree, TRUE, usb_conv_info);
3044         if (ret)
3045             return tvb_captured_length(next_tvb);
3046     }
3047
3048     return 0;
3049 }
3050
3051
3052 static int
3053 dissect_usb_setup_response(packet_info *pinfo, proto_tree *tree,
3054                            tvbuff_t *tvb, int offset,
3055                            guint8 urb_type, usb_conv_info_t *usb_conv_info)
3056 {
3057     proto_tree *parent;
3058     tvbuff_t   *next_tvb = NULL;
3059     gint        length_remaining;
3060
3061     parent = proto_tree_get_parent_tree(tree);
3062
3063     if (usb_conv_info) {
3064         if (usb_conv_info->usb_trans_info && is_usb_standard_setup_request(usb_conv_info->usb_trans_info)) {
3065             offset = dissect_usb_standard_setup_response(pinfo, parent, tvb, offset, usb_conv_info);
3066         }
3067         else {
3068             next_tvb = tvb_new_subset_remaining(tvb, offset);
3069             offset += try_dissect_next_protocol(parent, next_tvb, pinfo, usb_conv_info, urb_type, tree);
3070
3071             length_remaining = tvb_reported_length_remaining(tvb, offset);
3072             if (length_remaining > 0) {
3073                 proto_tree_add_item(parent, hf_usb_control_response_generic,
3074                         tvb, offset, length_remaining, ENC_NA);
3075                 offset += length_remaining;
3076             }
3077         }
3078     }
3079     else {
3080         /* no matching request available */
3081         length_remaining = tvb_reported_length_remaining(tvb, offset);
3082         if (length_remaining > 0) {
3083             proto_tree_add_item(parent, hf_usb_control_response_generic, tvb,
3084                     offset, length_remaining, ENC_NA);
3085             offset += length_remaining;
3086         }
3087     }
3088
3089     return offset;
3090 }
3091
3092
3093 static int
3094 dissect_usb_bmrequesttype(proto_tree *parent_tree, tvbuff_t *tvb, int offset)
3095 {
3096     proto_item *item;
3097     proto_tree *tree;
3098
3099     item = proto_tree_add_item(parent_tree, hf_usb_bmRequestType, tvb, offset, 1, ENC_LITTLE_ENDIAN);
3100     tree = proto_item_add_subtree(item, ett_usb_setup_bmrequesttype);
3101
3102     proto_tree_add_item(tree, hf_usb_bmRequestType_direction, tvb, offset, 1, ENC_LITTLE_ENDIAN);
3103     proto_tree_add_item(tree, hf_usb_bmRequestType_type,      tvb, offset, 1, ENC_LITTLE_ENDIAN);
3104     proto_tree_add_item(tree, hf_usb_bmRequestType_recipient, tvb, offset, 1, ENC_LITTLE_ENDIAN);
3105
3106     return ++offset;
3107 }
3108
3109
3110 static int
3111 dissect_linux_usb_pseudo_header_ext(tvbuff_t *tvb, int offset,
3112                                     packet_info *pinfo _U_,
3113                                     proto_tree *tree)
3114 {
3115     proto_tree_add_item(tree, hf_usb_urb_interval, tvb, offset, 4, ENC_HOST_ENDIAN);
3116     offset += 4;
3117     proto_tree_add_item(tree, hf_usb_urb_start_frame, tvb, offset, 4, ENC_HOST_ENDIAN);
3118     offset += 4;
3119     proto_tree_add_item(tree, hf_usb_urb_copy_of_transfer_flags, tvb, offset, 4, ENC_HOST_ENDIAN);
3120     offset += 4;
3121     proto_tree_add_item(tree, hf_usb_iso_numdesc, tvb, offset, 4, ENC_HOST_ENDIAN);
3122     offset += 4;
3123
3124     return offset;
3125 }
3126
3127
3128 /* Dissector used for usb setup requests */
3129 static int
3130 dissect_usb_setup_request(packet_info *pinfo, proto_tree *tree,
3131                           tvbuff_t *tvb, int offset,
3132                           guint8 urb_type, usb_conv_info_t *usb_conv_info,
3133                           usb_header_t header_type)
3134 {
3135     gint              setup_offset;
3136     gint              req_type;
3137     gint              ret;
3138     proto_tree       *parent, *setup_tree;
3139     usb_trans_info_t *usb_trans_info, trans_info;
3140     tvbuff_t         *next_tvb, *data_tvb = NULL;
3141
3142     /* we should do the NULL check in all non-static functions */
3143     if (usb_conv_info)
3144         usb_trans_info = usb_conv_info->usb_trans_info;
3145     else
3146         usb_trans_info = &trans_info;
3147
3148     parent = proto_tree_get_parent_tree(tree);
3149
3150     setup_tree = proto_tree_add_subtree(parent, tvb, offset, 8, ett_usb_setup_hdr, NULL, "URB setup");
3151
3152     req_type = USB_TYPE(tvb_get_guint8(tvb, offset));
3153     usb_trans_info->setup.requesttype = tvb_get_guint8(tvb, offset);
3154     if (usb_conv_info) {
3155         usb_conv_info->setup_requesttype = tvb_get_guint8(tvb, offset);
3156         if (req_type != RQT_SETUP_TYPE_CLASS)
3157             usb_tap_queue_packet(pinfo, urb_type, usb_conv_info);
3158     }
3159
3160     offset = dissect_usb_bmrequesttype(setup_tree, tvb, offset);
3161
3162     /* as we're going through the data, we build a next_tvb that
3163        contains the the setup packet without the request type
3164        and request-specific data
3165        all subsequent dissection routines work on this tvb */
3166
3167     setup_offset = offset;
3168     usb_trans_info->setup.request = tvb_get_guint8(tvb, offset);
3169     offset++;
3170     usb_trans_info->setup.wValue  = tvb_get_letohs(tvb, offset);
3171     offset += 2;
3172     usb_trans_info->setup.wIndex  = tvb_get_letohs(tvb, offset);
3173     offset += 2;
3174     usb_trans_info->setup.wLength = tvb_get_letohs(tvb, offset);
3175     offset += 2;
3176
3177     if (header_type == USB_HEADER_LINUX_64_BYTES) {
3178         offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, tree);
3179     }
3180
3181
3182     if (tvb_captured_length_remaining(tvb, offset) > 0) {
3183         next_tvb = tvb_new_composite();
3184         tvb_composite_append(next_tvb, tvb_new_subset(tvb, setup_offset, 7, 7));
3185
3186         data_tvb = tvb_new_subset_remaining(tvb, offset);
3187         tvb_composite_append(next_tvb, data_tvb);
3188         offset += tvb_captured_length(data_tvb);
3189         tvb_composite_finalize(next_tvb);
3190         next_tvb = tvb_new_child_real_data(tvb,
3191                 (const guint8 *) tvb_memdup(pinfo->pool, next_tvb, 0, tvb_captured_length(next_tvb)),
3192                 tvb_captured_length(next_tvb),
3193                 tvb_captured_length(next_tvb));
3194         add_new_data_source(pinfo, next_tvb, "USB Control");
3195     } else {
3196         next_tvb = tvb_new_subset(tvb, setup_offset, 7, 7);
3197     }
3198
3199     /* at this point, offset contains the number of bytes that we
3200        dissected */
3201
3202     if (is_usb_standard_setup_request(usb_trans_info)) {
3203         /* there's no point in checking the return value as there's no
3204            fallback for standard setup requests */
3205         dissect_usb_standard_setup_request(pinfo, setup_tree,
3206                 next_tvb, usb_conv_info, usb_trans_info);
3207     }
3208     else {
3209         /* no standard request - pass it on to class-specific dissectors */
3210         ret = try_dissect_next_protocol(
3211                 parent, next_tvb, pinfo, usb_conv_info, urb_type, tree);
3212         if (ret <= 0) {
3213             /* no class-specific dissector could handle it,
3214                dissect it as generic setup request */
3215             proto_tree_add_item(setup_tree, hf_usb_request_unknown_class,
3216                     next_tvb, 0, 1, ENC_LITTLE_ENDIAN);
3217             dissect_usb_setup_generic(pinfo, setup_tree,
3218                     next_tvb, 1, usb_conv_info);
3219         } else if (data_tvb) {
3220             proto_tree_add_item(setup_tree, hf_usb_request_unknown_class,
3221                     tvb, 0, 1, ENC_LITTLE_ENDIAN);
3222             dissect_usb_setup_generic(pinfo, setup_tree,
3223                     tvb, setup_offset, usb_conv_info);
3224         }
3225     }
3226
3227     if (data_tvb)
3228         proto_tree_add_item(setup_tree, hf_usb_data_fragment, data_tvb, 0, -1, ENC_NA);
3229
3230     return offset;
3231 }
3232
3233
3234 /* dissect the linux-specific USB pseudo header and fill the conversation struct
3235    return the number of dissected bytes */
3236 static gint
3237 dissect_linux_usb_pseudo_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3238         usb_conv_info_t *usb_conv_info, guint64 *urb_id)
3239 {
3240     guint8  transfer_type;
3241     guint8  endpoint_byte;
3242     guint8  transfer_type_and_direction;
3243     guint8  urb_type;
3244     guint8  flag[2];
3245
3246     *urb_id = tvb_get_guint64(tvb, 0, ENC_HOST_ENDIAN);
3247     proto_tree_add_uint64(tree, hf_usb_urb_id, tvb, 0, 8, *urb_id);
3248
3249     /* show the urb type of this URB as string and as a character */
3250     urb_type = tvb_get_guint8(tvb, 8);
3251     usb_conv_info->is_request = (urb_type==URB_SUBMIT);
3252     proto_tree_add_uint_format_value(tree, hf_usb_linux_urb_type, tvb, 8, 1,
3253         urb_type, "%s ('%c')", val_to_str(urb_type, usb_linux_urb_type_vals, "Unknown %d"),
3254         g_ascii_isprint(urb_type) ? urb_type : '.');
3255     proto_tree_add_item(tree, hf_usb_linux_transfer_type, tvb, 9, 1, ENC_LITTLE_ENDIAN);
3256
3257     transfer_type = tvb_get_guint8(tvb, 9);
3258     usb_conv_info->transfer_type = transfer_type;
3259
3260     endpoint_byte = tvb_get_guint8(tvb, 10);   /* direction bit | endpoint */
3261     usb_conv_info->endpoint = endpoint_byte & 0x7F;
3262     if (endpoint_byte & URB_TRANSFER_IN)
3263         usb_conv_info->direction = P2P_DIR_RECV;
3264     else
3265         usb_conv_info->direction = P2P_DIR_SENT;
3266
3267     transfer_type_and_direction = (transfer_type & 0x7F) | (endpoint_byte & 0x80);
3268     col_append_str(pinfo->cinfo, COL_INFO,
3269                     val_to_str(transfer_type_and_direction, usb_transfer_type_and_direction_vals, "Unknown type %x"));
3270
3271     proto_tree_add_bitmask(tree, tvb, 10, hf_usb_endpoint_number, ett_usb_endpoint, usb_endpoint_fields, ENC_NA);
3272     proto_tree_add_item(tree, hf_usb_device_address, tvb, 11, 1, ENC_LITTLE_ENDIAN);
3273     usb_conv_info->device_address = (guint16)tvb_get_guint8(tvb, 11);
3274
3275     proto_tree_add_item(tree, hf_usb_bus_id, tvb, 12, 2, ENC_HOST_ENDIAN);
3276     tvb_memcpy(tvb, &usb_conv_info->bus_id, 12, 2);
3277
3278     /* Right after the pseudo header we always have
3279      * sizeof(struct usb_device_setup_hdr) bytes. The content of these
3280      * bytes only have meaning in case setup_flag == 0.
3281      */
3282     flag[0] = tvb_get_guint8(tvb, 14);
3283     flag[1] = '\0';
3284     if (flag[0] == 0) {
3285         usb_conv_info->is_setup = TRUE;
3286         proto_tree_add_string(tree, hf_usb_setup_flag, tvb, 14, 1, "relevant (0)");
3287         if (usb_conv_info->transfer_type!=URB_CONTROL)
3288             proto_tree_add_expert(tree, pinfo, &ei_usb_invalid_setup, tvb, 14, 1);
3289     } else {
3290         usb_conv_info->is_setup = FALSE;
3291         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
3292             14, 1, flag, "not relevant ('%c')", g_ascii_isprint(flag[0]) ? flag[0]: '.');
3293     }
3294
3295     flag[0] = tvb_get_guint8(tvb, 15);
3296     flag[1] = '\0';
3297     if (flag[0] == 0) {
3298         proto_tree_add_string(tree, hf_usb_data_flag, tvb, 15, 1, "present (0)");
3299     } else {
3300         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
3301             15, 1, flag, "not present ('%c')", g_ascii_isprint(flag[0]) ? flag[0] : '.');
3302     }
3303
3304     proto_tree_add_item(tree, hf_usb_urb_ts_sec, tvb, 16, 8, ENC_HOST_ENDIAN);
3305     proto_tree_add_item(tree, hf_usb_urb_ts_usec, tvb, 24, 4, ENC_HOST_ENDIAN);
3306     proto_tree_add_item(tree, hf_usb_urb_status, tvb, 28, 4, ENC_HOST_ENDIAN);
3307     proto_tree_add_item(tree, hf_usb_urb_len, tvb, 32, 4, ENC_HOST_ENDIAN);
3308     proto_tree_add_item(tree, hf_usb_urb_data_len, tvb, 36, 4, ENC_HOST_ENDIAN);
3309
3310     return 40;
3311 }
3312
3313 /* dissect the usbpcap_buffer_packet_header and fill the conversation struct
3314    this function does not handle the transfer-specific headers
3315    return the number of bytes processed */
3316 static gint
3317 dissect_usbpcap_buffer_packet_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3318         usb_conv_info_t *usb_conv_info, guint32 *win32_data_len, guint64 *irp_id)
3319 {
3320     guint8   transfer_type;
3321     guint8   endpoint_byte;
3322     guint8   transfer_type_and_direction;
3323     guint8   tmp_val8;
3324
3325     proto_tree_add_item(tree, hf_usb_win32_header_len, tvb, 0, 2, ENC_LITTLE_ENDIAN);
3326     *irp_id = tvb_get_guint64(tvb, 2, ENC_LITTLE_ENDIAN);
3327     proto_tree_add_uint64(tree, hf_usb_irp_id, tvb, 2, 8, *irp_id);
3328     proto_tree_add_item(tree, hf_usb_usbd_status, tvb, 10, 4, ENC_LITTLE_ENDIAN);
3329     proto_tree_add_item(tree, hf_usb_function, tvb, 14, 2, ENC_LITTLE_ENDIAN);
3330
3331     proto_tree_add_bitmask(tree, tvb, 16, hf_usb_info, ett_usb_usbpcap_info, usb_usbpcap_info_fields, ENC_LITTLE_ENDIAN);
3332     tmp_val8 = tvb_get_guint8(tvb, 16);
3333     /* TODO: Handle errors */
3334     if (tmp_val8 & 0x01) {
3335         usb_conv_info->is_request = FALSE;
3336     } else {
3337         usb_conv_info->is_request = TRUE;
3338     }
3339
3340     proto_tree_add_item(tree, hf_usb_bus_id, tvb, 17, 2, ENC_LITTLE_ENDIAN);
3341     usb_conv_info->bus_id = tvb_get_letohs(tvb, 17);
3342
3343     proto_tree_add_item(tree, hf_usb_win32_device_address, tvb, 19, 2, ENC_LITTLE_ENDIAN);
3344     usb_conv_info->device_address = tvb_get_letohs(tvb, 19);
3345
3346     endpoint_byte = tvb_get_guint8(tvb, 21);
3347     usb_conv_info->direction = endpoint_byte&URB_TRANSFER_IN ?  P2P_DIR_RECV : P2P_DIR_SENT;
3348     usb_conv_info->endpoint = endpoint_byte&0x7F;
3349     proto_tree_add_bitmask(tree, tvb, 21, hf_usb_endpoint_number, ett_usb_endpoint, usb_endpoint_fields, ENC_LITTLE_ENDIAN);
3350
3351     transfer_type = tvb_get_guint8(tvb, 22);
3352     usb_conv_info->transfer_type = transfer_type;
3353     proto_tree_add_item(tree, hf_usb_linux_transfer_type, tvb, 22, 1, ENC_LITTLE_ENDIAN);
3354
3355     transfer_type_and_direction = (transfer_type & 0x7F) | (endpoint_byte & 0x80);
3356     col_append_str(pinfo->cinfo, COL_INFO,
3357                    val_to_str(transfer_type_and_direction, usb_transfer_type_and_direction_vals, "Unknown type %x"));
3358
3359     *win32_data_len = tvb_get_letohl(tvb, 23);
3360     proto_tree_add_item(tree, hf_usb_win32_data_len, tvb, 23, 4, ENC_LITTLE_ENDIAN);
3361
3362     /* by default, we assume it's no setup packet
3363        the correct values will be set when we parse the control header */
3364     usb_conv_info->is_setup = FALSE;
3365     usb_conv_info->setup_requesttype = 0;
3366
3367     /* we don't handle the transfer-specific headers here */
3368     return 27;
3369 }
3370
3371 /* Set the usb_address_t fields based on the direction of the urb */
3372 static void
3373 usb_set_addr(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, guint16 bus_id, guint16 device_address,
3374              int endpoint, gboolean req)
3375 {
3376     proto_item     *sub_item;
3377     usb_address_t  *src_addr = wmem_new0(pinfo->pool, usb_address_t),
3378                    *dst_addr = wmem_new0(pinfo->pool, usb_address_t);
3379     guint8         *str_src_addr;
3380     guint8         *str_dst_addr;
3381
3382     if (req) {
3383         /* request */
3384         src_addr->device   = 0xffffffff;
3385         src_addr->endpoint = NO_ENDPOINT;
3386         dst_addr->device   = GUINT16_TO_LE(device_address);
3387         dst_addr->endpoint = GUINT32_TO_LE(endpoint);
3388     } else {
3389         /* response */
3390         src_addr->device   = GUINT16_TO_LE(device_address);
3391         src_addr->endpoint = GUINT32_TO_LE(endpoint);
3392         dst_addr->device   = 0xffffffff;
3393         dst_addr->endpoint = NO_ENDPOINT;
3394     }
3395     src_addr->bus_id = GUINT16_TO_LE(bus_id);
3396     dst_addr->bus_id = GUINT16_TO_LE(bus_id);
3397
3398     set_address(&pinfo->net_src, AT_USB, USB_ADDR_LEN, (char *)src_addr);
3399     copy_address_shallow(&pinfo->src, &pinfo->net_src);
3400     set_address(&pinfo->net_dst, AT_USB, USB_ADDR_LEN, (char *)dst_addr);
3401     copy_address_shallow(&pinfo->dst, &pinfo->net_dst);
3402
3403     pinfo->ptype = PT_USB;
3404     pinfo->srcport = src_addr->endpoint;
3405     pinfo->destport = dst_addr->endpoint;
3406     /* sent/received is from the perspective of the USB host */
3407     pinfo->p2p_dir = req ? P2P_DIR_SENT : P2P_DIR_RECV;
3408
3409     str_src_addr = address_to_str(wmem_packet_scope(), &pinfo->src);
3410     str_dst_addr = address_to_str(wmem_packet_scope(), &pinfo->dst);
3411
3412     sub_item = proto_tree_add_string(tree, hf_usb_src, tvb, 0, 0, str_src_addr);
3413     PROTO_ITEM_SET_GENERATED(sub_item);
3414
3415     sub_item = proto_tree_add_string(tree, hf_usb_addr, tvb, 0, 0, str_src_addr);
3416     PROTO_ITEM_SET_HIDDEN(sub_item);
3417
3418     sub_item = proto_tree_add_string(tree, hf_usb_dst, tvb, 0, 0, str_dst_addr);
3419     PROTO_ITEM_SET_GENERATED(sub_item);
3420
3421     sub_item = proto_tree_add_string(tree, hf_usb_addr, tvb, 0, 0, str_dst_addr);
3422     PROTO_ITEM_SET_HIDDEN(sub_item);
3423 }
3424
3425
3426 /* Gets the transfer info for a given packet
3427  * Generates transfer info if none exists yet
3428  * Also adds request/response info to the tree for the given packet */
3429 static usb_trans_info_t
3430 *usb_get_trans_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
3431                     usb_header_t header_type, usb_conv_info_t *usb_conv_info, guint64 usb_id)
3432 {
3433     usb_trans_info_t *usb_trans_info;
3434     proto_item       *ti;
3435     nstime_t          t, deltat;
3436     wmem_tree_key_t   key[3];
3437
3438     /* request/response matching so we can keep track of transaction specific
3439      * data.
3440      */
3441     key[0].length = 2;
3442     key[0].key = (guint32 *)&usb_id;
3443     key[1].length = 1;
3444     key[1].key = &pinfo->num;
3445     key[2].length = 0;
3446     key[2].key = NULL;
3447
3448     if (usb_conv_info->is_request) {
3449         /* this is a request */
3450         usb_trans_info = (usb_trans_info_t *)wmem_tree_lookup32_array(usb_conv_info->transactions, key);
3451         if (!usb_trans_info) {
3452             usb_trans_info              = wmem_new0(wmem_file_scope(), usb_trans_info_t);
3453             usb_trans_info->request_in  = pinfo->num;
3454             usb_trans_info->req_time    = pinfo->abs_ts;
3455             usb_trans_info->header_type = header_type;
3456             usb_trans_info->usb_id      = usb_id;
3457
3458             wmem_tree_insert32_array(usb_conv_info->transactions, key, usb_trans_info);
3459         }
3460
3461         if (usb_trans_info->response_in) {
3462             ti = proto_tree_add_uint(tree, hf_usb_response_in, tvb, 0, 0, usb_trans_info->response_in);
3463             PROTO_ITEM_SET_GENERATED(ti);
3464         }
3465
3466     } else {
3467         /* this is a response */
3468         if (pinfo->fd->flags.visited) {
3469             usb_trans_info = (usb_trans_info_t *)wmem_tree_lookup32_array(usb_conv_info->transactions, key);
3470
3471         } else {
3472             usb_trans_info = (usb_trans_info_t *)wmem_tree_lookup32_array_le(usb_conv_info->transactions, key);
3473             if (usb_trans_info) {
3474                 if (usb_trans_info->usb_id == usb_id) {
3475                     if (usb_trans_info->response_in == 0) {
3476                         /* USBPcap generates 2 frames for response; store the first one */
3477                         usb_trans_info->response_in = pinfo->num;
3478                     }
3479                     wmem_tree_insert32_array(usb_conv_info->transactions, key, usb_trans_info);
3480                 } else {
3481                     usb_trans_info = NULL;
3482                 }
3483             }
3484         }
3485
3486         if (usb_trans_info && usb_trans_info->request_in) {
3487
3488             ti = proto_tree_add_uint(tree, hf_usb_request_in, tvb, 0, 0, usb_trans_info->request_in);
3489             PROTO_ITEM_SET_GENERATED(ti);
3490
3491             t = pinfo->abs_ts;
3492             nstime_delta(&deltat, &t, &usb_trans_info->req_time);
3493             ti = proto_tree_add_time(tree, hf_usb_time, tvb, 0, 0, &deltat);
3494             PROTO_ITEM_SET_GENERATED(ti);
3495         }
3496     }
3497
3498     return usb_trans_info;
3499 }
3500
3501
3502 /* dissect a group of isochronous packets inside an usb packet in
3503    usbpcap format */
3504 static gint
3505 dissect_usbpcap_iso_packets(packet_info *pinfo _U_, proto_tree *urb_tree, guint8 urb_type,
3506         tvbuff_t *tvb, gint offset, guint32 win32_data_len, usb_conv_info_t *usb_conv_info)
3507 {
3508     guint32     i;
3509     guint32     num_packets;
3510     guint32     data_start_offset;
3511     proto_item *urb_tree_ti;
3512
3513     proto_tree_add_item(urb_tree, hf_usb_win32_iso_start_frame, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3514     offset += 4;
3515
3516     num_packets = tvb_get_letohl(tvb, offset);
3517     proto_tree_add_item(urb_tree, hf_usb_win32_iso_num_packets, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3518     offset += 4;
3519
3520     proto_tree_add_item(urb_tree, hf_usb_win32_iso_error_count, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3521     offset += 4;
3522
3523     data_start_offset = offset + 12 * num_packets;
3524     urb_tree_ti = proto_tree_get_parent(urb_tree);
3525     proto_item_set_len(urb_tree_ti, data_start_offset);
3526
3527     for (i = 0; i < num_packets; i++) {
3528         guint32 this_offset;
3529         guint32 next_offset;
3530         guint32 iso_len;
3531         proto_item *iso_packet_ti, *ti;
3532         proto_tree *iso_packet_tree;
3533
3534         iso_packet_ti = proto_tree_add_protocol_format(
3535                 proto_tree_get_root(urb_tree), proto_usb,
3536                 tvb, offset, 12, "USB isochronous packet");
3537         iso_packet_tree = proto_item_add_subtree(iso_packet_ti, ett_usb_win32_iso_packet);
3538
3539         this_offset = tvb_get_letohl(tvb, offset);
3540         if (num_packets - i == 1) {
3541             /* this is the last packet */
3542             next_offset = win32_data_len;
3543         } else {
3544             /* there is next packet */
3545             next_offset = tvb_get_letohl(tvb, offset + 12);
3546         }
3547
3548         if (next_offset > this_offset) {
3549             iso_len = next_offset - this_offset;
3550         } else {
3551             iso_len = 0;
3552         }
3553
3554         /* If this packet does not contain isochrounous data, do not try to display it */
3555         if (!((usb_conv_info->is_request && usb_conv_info->direction==P2P_DIR_SENT) ||
3556                     (!usb_conv_info->is_request && usb_conv_info->direction==P2P_DIR_RECV))) {
3557             iso_len = 0;
3558         }
3559
3560         proto_tree_add_item(iso_packet_tree, hf_usb_win32_iso_offset, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3561         offset += 4;
3562
3563         ti = proto_tree_add_item(iso_packet_tree, hf_usb_win32_iso_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3564         if (usb_conv_info->direction==P2P_DIR_SENT) {
3565             /* Isochronous OUT transfer */
3566             proto_item_append_text(ti, " (not used)");
3567         } else {
3568             /* Isochronous IN transfer.
3569              * Length field is being set by host controller.
3570              */
3571             if (usb_conv_info->is_request) {
3572                 /* Length was not yet set */
3573                 proto_item_append_text(ti, " (irrelevant)");
3574             } else {
3575                 /* Length was set and (should be) valid */
3576                 proto_item_append_text(ti, " (relevant)");
3577                 iso_len = tvb_get_letohl(tvb, offset);
3578             }
3579         }
3580         offset += 4;
3581
3582         ti = proto_tree_add_item(iso_packet_tree, hf_usb_win32_iso_status, tvb, offset, 4, ENC_LITTLE_ENDIAN);
3583         if (urb_type == URB_SUBMIT) {
3584             proto_item_append_text(ti, " (irrelevant)");
3585         } else {
3586             proto_item_append_text(ti, " (relevant)");
3587         }
3588         offset += 4;
3589
3590         if (iso_len && data_start_offset + this_offset + iso_len <= tvb_captured_length(tvb)) {
3591             proto_tree_add_item(iso_packet_tree, hf_usb_iso_data, tvb, (gint)(data_start_offset + this_offset), (gint)iso_len, ENC_NA);
3592             proto_tree_set_appendix(iso_packet_tree, tvb, (gint)(data_start_offset + this_offset), (gint)iso_len);
3593         }
3594     }
3595
3596     if ((usb_conv_info->is_request && usb_conv_info->direction==P2P_DIR_SENT) ||
3597             (!usb_conv_info->is_request && usb_conv_info->direction==P2P_DIR_RECV)) {
3598         /* We have dissected all the isochronous data */
3599         offset += win32_data_len;
3600     }
3601
3602     return offset;
3603 }
3604
3605
3606 static gint
3607 dissect_linux_usb_iso_transfer(packet_info *pinfo _U_, proto_tree *urb_tree,
3608         usb_header_t header_type, tvbuff_t *tvb, gint offset,
3609         usb_conv_info_t *usb_conv_info)
3610 {
3611     guint32     iso_numdesc = 0;
3612     proto_item *tii;
3613     guint32     val32;
3614     guint32     i;
3615     guint       data_base;
3616     guint32     iso_status;
3617     guint32     iso_off = 0;
3618     guint32     iso_len = 0;
3619
3620     tii = proto_tree_add_uint(urb_tree, hf_usb_bInterfaceClass, tvb, offset, 0, usb_conv_info->interfaceClass);
3621     PROTO_ITEM_SET_GENERATED(tii);
3622
3623     /* All fields which belong to Linux usbmon headers are in host-endian
3624      * byte order. The fields coming from the USB communication are in little
3625      * endian format (see usb_20.pdf, chapter 8.1 Byte/Bit ordering).
3626      *
3627      * When a capture file is transferred to a host with different endianness
3628      * than packet was captured then the necessary swapping happens in
3629      * wiretap/pcap-common.c, pcap_byteswap_linux_usb_pseudoheader().
3630      */
3631
3632     /* iso urbs on linux can't possibly contain a setup packet
3633        see mon_bin_event() in the linux kernel */
3634
3635     /* Process ISO related fields (usbmon_packet.iso). The fields are
3636      * in host endian byte order so use tvb_memcopy() and
3637      * proto_tree_add_uint() pair.
3638      */
3639
3640     tvb_memcpy(tvb, (guint8 *)&val32, offset, 4);
3641     proto_tree_add_uint(urb_tree, hf_usb_iso_error_count, tvb, offset, 4, val32);
3642     offset += 4;
3643
3644     tvb_memcpy(tvb, (guint8 *)&iso_numdesc, offset, 4);
3645     proto_tree_add_uint(urb_tree, hf_usb_iso_numdesc, tvb, offset, 4, iso_numdesc);
3646     offset += 4;
3647
3648     if (header_type == USB_HEADER_LINUX_64_BYTES) {
3649         offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, urb_tree);
3650     }
3651
3652     data_base = offset + iso_numdesc*16;
3653     for (i = 0; i<iso_numdesc; i++) {
3654         proto_item   *iso_desc_ti;
3655         proto_tree   *iso_desc_tree;
3656         guint32       iso_pad;
3657
3658         /* Fetch ISO descriptor fields stored in host endian byte order. */
3659         tvb_memcpy(tvb, (guint8 *)&iso_status, offset, 4);
3660         tvb_memcpy(tvb, (guint8 *)&iso_off, offset+4,  4);
3661         tvb_memcpy(tvb, (guint8 *)&iso_len, offset+8,  4);
3662
3663         iso_desc_ti = proto_tree_add_protocol_format(urb_tree, proto_usb, tvb, offset,
3664                 16, "USB isodesc %u [%s]", i, val_to_str_ext(iso_status, &usb_urb_status_vals_ext, "Error %d"));
3665         if (iso_len > 0)
3666             proto_item_append_text(iso_desc_ti, " (%u bytes)", iso_len);
3667         iso_desc_tree = proto_item_add_subtree(iso_desc_ti, ett_usb_isodesc);
3668
3669         proto_tree_add_int(iso_desc_tree, hf_usb_iso_status, tvb, offset, 4, iso_status);
3670         offset += 4;
3671
3672         proto_tree_add_uint(iso_desc_tree, hf_usb_iso_off, tvb, offset, 4, iso_off);
3673         offset += 4;
3674
3675         proto_tree_add_uint(iso_desc_tree, hf_usb_iso_len, tvb, offset, 4, iso_len);
3676         offset += 4;
3677
3678         /* Show the ISO data if we captured them and either the status
3679            is OK or the packet is sent from host to device.
3680            The Linux kernel sets the status field in outgoing isochronous
3681            URBs to -EXDEV and fills the data part with valid data.
3682          */
3683         if ((pinfo->p2p_dir==P2P_DIR_SENT || !iso_status) &&
3684                 iso_len && data_base + iso_off + iso_len <= tvb_captured_length(tvb)) {
3685             proto_tree_add_item(iso_desc_tree, hf_usb_iso_data, tvb, data_base + iso_off, iso_len, ENC_NA);
3686             proto_tree_set_appendix(iso_desc_tree, tvb, (gint)(data_base+iso_off), (gint)iso_len);
3687         }
3688
3689         tvb_memcpy(tvb, (guint8 *)&iso_pad, offset, 4);
3690         proto_tree_add_uint(iso_desc_tree, hf_usb_iso_pad, tvb, offset, 4, iso_pad);
3691         offset += 4;
3692     }
3693
3694     /* we jump to the end of the last iso data chunk
3695        this assumes that the iso data starts immediately after the
3696        iso descriptors
3697        we have to use the offsets from the last iso descriptor, we can't keep
3698        track of the offset ourselves as there may be gaps
3699        between data packets in the transfer buffer */
3700     return data_base+iso_off+iso_len;
3701 }
3702
3703 static gint
3704 dissect_usbip_iso_transfer(packet_info *pinfo _U_, proto_tree *urb_tree,
3705         tvbuff_t *tvb, gint offset, guint32 iso_numdesc, guint32 desc_offset,
3706         usb_conv_info_t *usb_conv_info)
3707 {
3708     proto_item *tii;
3709     guint32     i;
3710     guint       data_base;
3711     guint32     iso_off = 0;
3712     guint32     iso_len = 0;
3713
3714     tii = proto_tree_add_uint(urb_tree, hf_usb_bInterfaceClass, tvb, offset, 0, usb_conv_info->interfaceClass);
3715     PROTO_ITEM_SET_GENERATED(tii);
3716
3717     /* All fields which belong to usbip are in big-endian byte order.
3718      * unlike the linux kernel, the usb isoc descriptor is appended at
3719      * the end of the isoc data. We have to reassemble the pdus and jump
3720      * to the end (actual_length) and the remaining data is the isoc
3721      * descriptor.
3722      */
3723
3724     data_base = offset;
3725     for (i = 0; i<iso_numdesc; i++) {
3726         proto_item   *iso_desc_ti;
3727         proto_tree   *iso_desc_tree;
3728         guint32       iso_status;
3729
3730         iso_status = tvb_get_ntohl(tvb, desc_offset + 12);
3731         iso_desc_ti = proto_tree_add_protocol_format(urb_tree, proto_usb, tvb, desc_offset,
3732                 16, "USB isodesc %u [%s]", i, val_to_str_ext(iso_status, &usb_urb_status_vals_ext, "Error %d"));
3733         iso_desc_tree = proto_item_add_subtree(iso_desc_ti, ett_usb_isodesc);
3734
3735         proto_tree_add_item_ret_uint(iso_desc_tree, hf_usb_iso_off, tvb, desc_offset, 4, ENC_BIG_ENDIAN, &iso_off);
3736         desc_offset += 4;
3737
3738         proto_tree_add_item(iso_desc_tree, hf_usb_iso_len, tvb, desc_offset, 4, ENC_BIG_ENDIAN);
3739         desc_offset += 4;
3740
3741         proto_tree_add_item_ret_uint(iso_desc_tree, hf_usb_iso_actual_len, tvb, desc_offset, 4, ENC_BIG_ENDIAN, &iso_len);
3742         desc_offset += 4;
3743         if (iso_len > 0)
3744             proto_item_append_text(iso_desc_ti, " (%u bytes)", iso_len);
3745
3746         proto_tree_add_uint(iso_desc_tree, hf_usb_iso_status, tvb, desc_offset, 4, iso_status);
3747         desc_offset += 4;
3748
3749         /* Show the ISO data if we captured them and either the status
3750            is OK or the packet is sent from host to device.
3751            The Linux kernel sets the status field in outgoing isochronous
3752            URBs to -EXDEV and fills the data part with valid data.
3753          */
3754         if ((pinfo->p2p_dir==P2P_DIR_SENT || !iso_status) &&
3755                 iso_len && data_base + iso_off + iso_len <= tvb_reported_length(tvb)) {
3756             proto_tree_add_item(iso_desc_tree, hf_usb_iso_data, tvb, (guint) data_base + iso_off, iso_len, ENC_NA);
3757             proto_tree_set_appendix(iso_desc_tree, tvb, (guint) data_base + iso_off, (gint)iso_len);
3758         }
3759     }
3760     return desc_offset;
3761 }
3762
3763 static gint
3764 dissect_usb_payload(tvbuff_t *tvb, packet_info *pinfo,
3765                     proto_tree *parent, proto_tree *tree,
3766                     usb_conv_info_t *usb_conv_info, guint8 urb_type,
3767                     gint offset, guint16 device_address)
3768 {
3769     wmem_tree_key_t          key[4];
3770     guint32                  k_frame_number;
3771     guint32                  k_device_address;
3772     guint32                  k_bus_id;
3773     device_product_data_t   *device_product_data = NULL;
3774     device_protocol_data_t  *device_protocol_data = NULL;
3775     tvbuff_t                *next_tvb = NULL;
3776
3777     k_frame_number = pinfo->num;
3778     k_device_address = device_address;
3779     k_bus_id = usb_conv_info->bus_id;
3780
3781     key[0].length = 1;
3782     key[0].key    = &k_device_address;
3783     key[1].length = 1;
3784     key[1].key    = &k_bus_id;
3785     key[2].length = 1;
3786     key[2].key    = &k_frame_number;
3787     key[3].length = 0;
3788     key[3].key    = NULL;
3789
3790     device_product_data = (device_product_data_t *) wmem_tree_lookup32_array_le(device_to_product_table, key);
3791     if (device_product_data && device_product_data->bus_id == usb_conv_info->bus_id &&
3792             device_product_data->device_address == device_address) {
3793         p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_VENDOR_ID, GUINT_TO_POINTER((guint)device_product_data->vendor));
3794         p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_PRODUCT_ID, GUINT_TO_POINTER((guint)device_product_data->product));
3795     }
3796
3797     device_protocol_data = (device_protocol_data_t *) wmem_tree_lookup32_array_le(device_to_protocol_table, key);
3798     if (device_protocol_data && device_protocol_data->bus_id == usb_conv_info->bus_id &&
3799             device_protocol_data->device_address == device_address) {
3800         p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_CLASS, GUINT_TO_POINTER(device_protocol_data->protocol >> 16));
3801         p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_SUBCLASS, GUINT_TO_POINTER((device_protocol_data->protocol >> 8) & 0xFF));
3802         p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_PROTOCOL, GUINT_TO_POINTER(device_protocol_data->protocol & 0xFF));
3803         usb_conv_info->device_protocol = device_protocol_data->protocol;
3804     }
3805
3806     p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_BUS_ID, GUINT_TO_POINTER((guint)usb_conv_info->bus_id));
3807     p_add_proto_data(pinfo->pool, pinfo, proto_usb, USB_DEVICE_ADDRESS, GUINT_TO_POINTER((guint)device_address));
3808
3809     if (tvb_captured_length_remaining(tvb, offset) > 0) {
3810         next_tvb = tvb_new_subset_remaining(tvb, offset);
3811         offset += try_dissect_next_protocol(parent, next_tvb, pinfo, usb_conv_info, urb_type, tree);
3812     }
3813
3814     if (tvb_captured_length_remaining(tvb, offset) > 0) {
3815         /* There is still leftover capture data to add (padding?) */
3816         proto_tree_add_item(parent, hf_usb_capdata, tvb, offset, -1, ENC_NA);
3817     }
3818
3819     return offset;
3820 }
3821
3822 static int
3823 dissect_freebsd_usb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent, void *data _U_)
3824 {
3825     int offset = 0;
3826     proto_item *ti;
3827     proto_tree *tree = NULL, *frame_tree = NULL;
3828     guint32 nframes;
3829     guint32 i;
3830
3831     col_set_str(pinfo->cinfo, COL_PROTOCOL, "USB");
3832
3833     /* add usb hdr*/
3834     if (parent) {
3835       ti = proto_tree_add_protocol_format(parent, proto_usb, tvb, 0, 128,
3836                                           "USB URB");
3837       tree = proto_item_add_subtree(ti, ett_usb_hdr);
3838     }
3839
3840     proto_tree_add_item(tree, hf_usb_totlen, tvb, 0, 4, ENC_LITTLE_ENDIAN);
3841     proto_tree_add_item(tree, hf_usb_busunit, tvb, 4, 4, ENC_LITTLE_ENDIAN);
3842     proto_tree_add_item(tree, hf_usb_address, tvb, 8, 1, ENC_LITTLE_ENDIAN);
3843     proto_tree_add_item(tree, hf_usb_mode, tvb, 9, 1, ENC_LITTLE_ENDIAN);
3844     proto_tree_add_item(tree, hf_usb_freebsd_urb_type, tvb, 10, 1, ENC_LITTLE_ENDIAN);
3845     proto_tree_add_item(tree, hf_usb_freebsd_transfer_type, tvb, 11, 1, ENC_LITTLE_ENDIAN);
3846     proto_tree_add_item(tree, hf_usb_xferflags, tvb, 12, 4, ENC_LITTLE_ENDIAN);
3847     proto_tree_add_item(tree, hf_usb_xferstatus, tvb, 16, 4, ENC_LITTLE_ENDIAN);
3848     proto_tree_add_item(tree, hf_usb_error, tvb, 20, 4, ENC_LITTLE_ENDIAN);
3849     proto_tree_add_item(tree, hf_usb_interval, tvb, 24, 4, ENC_LITTLE_ENDIAN);
3850     proto_tree_add_item_ret_uint(tree, hf_usb_nframes, tvb, 28, 4, ENC_LITTLE_ENDIAN, &nframes);
3851     proto_tree_add_item(tree, hf_usb_packet_size, tvb, 32, 4, ENC_LITTLE_ENDIAN);
3852     proto_tree_add_item(tree, hf_usb_packet_count, tvb, 36, 4, ENC_LITTLE_ENDIAN);
3853     proto_tree_add_item(tree, hf_usb_endpoint_number, tvb, 40, 4, ENC_LITTLE_ENDIAN);
3854     proto_tree_add_item(tree, hf_usb_speed, tvb, 44, 1, ENC_LITTLE_ENDIAN);
3855
3856     offset += 128;
3857     for (i = 0; i < nframes; i++) {
3858         guint32 framelen;
3859         guint32 frameflags;
3860
3861         frame_tree = proto_tree_add_subtree_format(tree, tvb, offset, -1, ett_usb_frame, &ti, "Frame %u", i);
3862         proto_tree_add_item_ret_uint(frame_tree, hf_usb_frame_length, tvb, offset, 4, ENC_LITTLE_ENDIAN, &framelen);
3863         proto_tree_add_item_ret_uint(frame_tree, hf_usb_frame_flags, tvb, offset + 4, 4, ENC_LITTLE_ENDIAN, &frameflags);
3864         offset += 8;
3865         if (frameflags & FREEBSD_FRAMEFLAG_DATA_FOLLOWS)
3866             offset += (framelen + 3) & ~3;
3867         proto_item_set_end(ti, tvb, offset);
3868     }
3869
3870     return tvb_captured_length(tvb);
3871 }
3872
3873 void
3874 dissect_usb_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent,
3875                    usb_header_t header_type, void *extra_data)
3876 {
3877     gint                  offset = 0;
3878     int                   endpoint;
3879     guint8                urb_type;
3880     guint32               win32_data_len = 0;
3881     guint32               iso_numdesc = 0;
3882     guint32               desc_offset = 0;
3883     proto_item           *urb_tree_ti;
3884     proto_tree           *tree;
3885     proto_item           *item;
3886     usb_conv_info_t      *usb_conv_info;
3887     conversation_t       *conversation;
3888     guint16              device_address;
3889     guint16              bus_id;
3890     guint8                   usbpcap_control_stage = 0;
3891     guint64                  usb_id;
3892     struct mausb_header  *ma_header = NULL;
3893     struct usbip_header  *ip_header = NULL;
3894
3895     /* the goal is to get the conversation struct as early as possible
3896        and store all status values in this struct
3897        at first, we read the fields required to create/identify
3898        the right conversation struct */
3899     switch (header_type) {
3900
3901     case USB_HEADER_LINUX_48_BYTES:
3902     case USB_HEADER_LINUX_64_BYTES:
3903         urb_type = tvb_get_guint8(tvb, 8);
3904         endpoint = tvb_get_guint8(tvb, 10) & 0x7F;
3905         device_address = (guint16)tvb_get_guint8(tvb, 11);
3906         bus_id = tvb_get_letohs(tvb, 12);
3907         break;
3908
3909     case USB_HEADER_USBPCAP:
3910         urb_type = tvb_get_guint8(tvb, 16) & 0x01 ? URB_COMPLETE : URB_SUBMIT;
3911         device_address = tvb_get_letohs(tvb, 19);
3912         endpoint = tvb_get_guint8(tvb, 21) & 0x7F;
3913         bus_id = tvb_get_letohs(tvb, 17);
3914         break;
3915
3916     case USB_HEADER_MAUSB:
3917         ma_header = (struct mausb_header *) extra_data;
3918         urb_type = mausb_is_from_host(ma_header) ? URB_SUBMIT : URB_COMPLETE;
3919         device_address = mausb_ep_handle_dev_addr(ma_header->handle);
3920         endpoint = mausb_ep_handle_ep_num(ma_header->handle);
3921         bus_id = mausb_ep_handle_bus_num(ma_header->handle);
3922         break;
3923
3924     case USB_HEADER_USBIP:
3925         ip_header = (struct usbip_header *) extra_data;
3926         urb_type = tvb_get_ntohl(tvb, 0) == 1 ? URB_SUBMIT : URB_COMPLETE;
3927         device_address = ip_header->devid;
3928         bus_id = ip_header->busid;
3929         endpoint = ip_header->ep;
3930         break;
3931
3932     default:
3933         return; /* invalid USB pseudo header */
3934     }
3935
3936     col_set_str(pinfo->cinfo, COL_PROTOCOL, "USB");
3937     urb_tree_ti = proto_tree_add_protocol_format(parent, proto_usb, tvb, 0, -1, "USB URB");
3938     tree = proto_item_add_subtree(urb_tree_ti, ett_usb_hdr);
3939
3940     usb_set_addr(tree, tvb, pinfo, bus_id, device_address, endpoint,
3941                  (urb_type == URB_SUBMIT));
3942
3943     conversation = get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, pinfo->srcport, pinfo->destport);
3944     usb_conv_info = get_usb_conv_info(conversation);
3945     clear_usb_conv_tmp_data(usb_conv_info);
3946
3947
3948     switch (header_type) {
3949
3950     case USB_HEADER_LINUX_48_BYTES:
3951     case USB_HEADER_LINUX_64_BYTES:
3952         proto_item_set_len(urb_tree_ti, (header_type == USB_HEADER_LINUX_64_BYTES) ? 64 : 48);
3953         offset = dissect_linux_usb_pseudo_header(tvb, pinfo, tree, usb_conv_info, &usb_id);
3954         break;
3955
3956     case USB_HEADER_USBPCAP:
3957         offset = dissect_usbpcap_buffer_packet_header(tvb, pinfo, tree, usb_conv_info, &win32_data_len, &usb_id);
3958         /* the length that we're setting here might have to be corrected
3959            if there's a transfer-specific pseudo-header following */
3960         proto_item_set_len(urb_tree_ti, offset);