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