From Marton Nemeth via bug #5490: Cleanup USB endpoint direction display.
[obnox/wireshark/wip.git] / epan / dissectors / packet-usb.c
1 /* packet-usb.c
2  *
3  * $Id$
4  *
5  * USB basic dissector
6  * By Paolo Abeni <paolo.abeni@email.it>
7  * Ronnie Sahlberg 2006
8  * Chris Maynard 2010 <chris[dot]maynard[at]gtech[dot]com>
9  *
10  * http://www.usb.org/developers/docs/usb_20_122909-2.zip
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <isprint.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/etypes.h>
36 #include <epan/addr_resolv.h>
37 #include <epan/emem.h>
38 #include <epan/tap.h>
39 #include <epan/conversation.h>
40 #include <epan/expert.h>
41 #include "packet-usb.h"
42 #include "packet-usb-hid.h"
43
44 /* protocols and header fields */
45 static int proto_usb = -1;
46
47 /* Linux USB pseudoheader fields */
48 static int hf_usb_urb_id = -1;
49 static int hf_usb_urb_type = -1;
50 static int hf_usb_transfer_type = -1;
51 static int hf_usb_endpoint_number = -1;
52 static int hf_usb_endpoint_direction = -1;
53 static int hf_usb_endpoint_number_value = -1;
54 static int hf_usb_device_address = -1;
55 static int hf_usb_bus_id = -1;
56 static int hf_usb_setup_flag = -1;
57 static int hf_usb_data_flag = -1;
58 static int hf_usb_urb_ts_sec = -1;
59 static int hf_usb_urb_ts_usec = -1;
60 static int hf_usb_urb_status = -1;
61 static int hf_usb_urb_len = -1;
62 static int hf_usb_data_len = -1;
63
64 static int hf_usb_src_endpoint_number = -1;
65 static int hf_usb_dst_endpoint_number = -1;
66 static int hf_usb_request = -1;
67 static int hf_usb_request_unknown_class = -1;
68 static int hf_usb_value = -1;
69 static int hf_usb_index = -1;
70 static int hf_usb_length = -1;
71 static int hf_usb_data = -1;
72 static int hf_usb_capdata = -1;
73 static int hf_usb_wFeatureSelector = -1;
74 static int hf_usb_wInterface = -1;
75 static int hf_usb_wStatus = -1;
76 static int hf_usb_wFrameNumber = -1;
77
78 static int hf_usb_iso_error_count = -1;
79 static int hf_usb_iso_numdesc = -1;
80 static int hf_usb_iso_status = -1;
81 static int hf_usb_iso_off = -1;
82 static int hf_usb_iso_len = -1;
83 static int hf_usb_iso_pad = -1;
84 static int hf_usb_iso_data = -1;
85
86 static int hf_usb_bmRequestType = -1;
87 static int hf_usb_bmRequestType_direction = -1;
88 static int hf_usb_bmRequestType_type = -1;
89 static int hf_usb_bmRequestType_recipient = -1;
90 static int hf_usb_bDescriptorType = -1;
91 static int hf_usb_descriptor_index = -1;
92 static int hf_usb_language_id = -1;
93 static int hf_usb_bLength = -1;
94 static int hf_usb_bcdUSB = -1;
95 static int hf_usb_bDeviceClass = -1;
96 static int hf_usb_bDeviceSubClass = -1;
97 static int hf_usb_bDeviceProtocol = -1;
98 static int hf_usb_bMaxPacketSize0 = -1;
99 static int hf_usb_idVendor = -1;
100 static int hf_usb_idProduct = -1;
101 static int hf_usb_bcdDevice = -1;
102 static int hf_usb_iManufacturer = -1;
103 static int hf_usb_iProduct = -1;
104 static int hf_usb_iSerialNumber = -1;
105 static int hf_usb_bNumConfigurations = -1;
106 static int hf_usb_wLANGID = -1;
107 static int hf_usb_bString = -1;
108 static int hf_usb_bInterfaceNumber = -1;
109 static int hf_usb_bAlternateSetting = -1;
110 static int hf_usb_bNumEndpoints = -1;
111 static int hf_usb_bInterfaceClass = -1;
112 static int hf_usb_bInterfaceSubClass = -1;
113 static int hf_usb_bInterfaceProtocol = -1;
114 static int hf_usb_iInterface = -1;
115 static int hf_usb_bEndpointAddress = -1;
116 static int hf_usb_bmAttributes = -1;
117 static int hf_usb_bEndpointAttributeTransfer = -1;
118 static int hf_usb_bEndpointAttributeSynchonisation = -1;
119 static int hf_usb_bEndpointAttributeBehaviour = -1;
120 static int hf_usb_wMaxPacketSize = -1;
121 static int hf_usb_bInterval = -1;
122 static int hf_usb_wTotalLength = -1;
123 static int hf_usb_bNumInterfaces = -1;
124 static int hf_usb_bConfigurationValue = -1;
125 static int hf_usb_iConfiguration = -1;
126 static int hf_usb_bMaxPower = -1;
127 static int hf_usb_configuration_bmAttributes = -1;
128 static int hf_usb_configuration_legacy10buspowered = -1;
129 static int hf_usb_configuration_selfpowered = -1;
130 static int hf_usb_configuration_remotewakeup = -1;
131 static int hf_usb_bEndpointAddress_direction = -1;
132 static int hf_usb_bEndpointAddress_number = -1;
133 static int hf_usb_response_in = -1;
134 static int hf_usb_time = -1;
135 static int hf_usb_request_in = -1;
136
137 static gint usb_hdr = -1;
138 static gint usb_setup_hdr = -1;
139 static gint usb_isodesc = -1;
140 static gint ett_usb_endpoint = -1;
141 static gint ett_usb_setup_bmrequesttype = -1;
142 static gint ett_descriptor_device = -1;
143 static gint ett_configuration_bmAttributes = -1;
144 static gint ett_configuration_bEndpointAddress = -1;
145 static gint ett_endpoint_bmAttributes = -1;
146
147 static const int *usb_endpoint_fields[] = {
148     &hf_usb_endpoint_direction,
149     &hf_usb_endpoint_number_value,
150     NULL
151 };
152
153 static int usb_tap = -1;
154
155 static dissector_table_t usb_bulk_dissector_table;
156 static dissector_table_t usb_control_dissector_table;
157
158 /* http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
159 static const value_string usb_langid_vals[] = {
160     {0x0000, "no language specified"},
161     {0x0401, "Arabic (Saudi Arabia)"},
162     {0x0402, "Bulgarian"},
163     {0x0403, "Catalan"},
164     {0x0404, "Chinese (Taiwan)"},
165     {0x0405, "Czech"},
166     {0x0406, "Danish"},
167     {0x0407, "German (Standard)"},
168     {0x0408, "Greek"},
169     {0x0409, "English (United States)"},
170     {0x040a, "Spanish (Traditional Sort)"},
171     {0x040b, "Finnish"},
172     {0x040c, "French (Standard)"},
173     {0x040d, "Hebrew"},
174     {0x040e, "Hungarian"},
175     {0x040f, "Icelandic"},
176     {0x0410, "Italian (Standard)"},
177     {0x0411, "Japanese"},
178     {0x0412, "Korean"},
179     {0x0413, "Dutch (Netherlands)"},
180     {0x0414, "Norwegian (Bokmal)"},
181     {0x0415, "Polish"},
182     {0x0416, "Portuguese (Brazil)"},
183     {0x0418, "Romanian"},
184     {0x0419, "Russian"},
185     {0x041a, "Croatian"},
186     {0x041b, "Slovak"},
187     {0x041c, "Albanian"},
188     {0x041d, "Swedish"},
189     {0x041e, "Thai"},
190     {0x041f, "Turkish"},
191     {0x0420, "Urdu (Pakistan)"},
192     {0x0421, "Indonesian"},
193     {0x0422, "Ukrainian"},
194     {0x0423, "Belarussian"},
195     {0x0424, "Slovenian"},
196     {0x0425, "Estonian"},
197     {0x0426, "Latvian"},
198     {0x0427, "Lithuanian"},
199     {0x0429, "Farsi"},
200     {0x042a, "Vietnamese"},
201     {0x042b, "Armenian"},
202     {0x042c, "Azeri (Latin)"},
203     {0x042d, "Basque"},
204     {0x042f, "Macedonian"},
205     {0x0430, "Sutu"},
206     {0x0436, "Afrikaans"},
207     {0x0437, "Georgian"},
208     {0x0438, "Faeroese"},
209     {0x0439, "Hindi"},
210     {0x043e, "Malay (Malaysian)"},
211     {0x043f, "Kazakh"},
212     {0x0441, "Swahili (Kenya)"},
213     {0x0443, "Uzbek (Latin)"},
214     {0x0444, "Tatar (Tatarstan)"},
215     {0x0445, "Bengali"},
216     {0x0446, "Punjabi"},
217     {0x0447, "Gujarati"},
218     {0x0448, "Oriya"},
219     {0x0449, "Tamil"},
220     {0x044a, "Telugu"},
221     {0x044b, "Kannada"},
222     {0x044c, "Malayalam"},
223     {0x044d, "Assamese"},
224     {0x044e, "Marathi"},
225     {0x044f, "Sanskrit"},
226     {0x0455, "Burmese"},
227     {0x0457, "Konkani"},
228     {0x0458, "Manipuri"},
229     {0x0459, "Sindhi"},
230     {0x04ff, "HID (Usage Data Descriptor)"},
231     {0x0801, "Arabic (Iraq)"},
232     {0x0804, "Chinese (PRC)"},
233     {0x0807, "German (Switzerland)"},
234     {0x0809, "English (United Kingdom)"},
235     {0x080a, "Spanish (Mexican)"},
236     {0x080c, "French (Belgian)"},
237     {0x0810, "Italian (Switzerland)"},
238     {0x0812, "Korean (Johab)"},
239     {0x0813, "Dutch (Belgium)"},
240     {0x0814, "Norwegian (Nynorsk)"},
241     {0x0816, "Portuguese (Standard)"},
242     {0x081a, "Serbian (Latin)"},
243     {0x081d, "Swedish (Finland)"},
244     {0x0820, "Urdu (India)"},
245     {0x0827, "Lithuanian (Classic)"},
246     {0x082c, "Azeri (Cyrillic)"},
247     {0x083e, "Malay (Brunei Darussalam)"},
248     {0x0843, "Uzbek (Cyrillic)"},
249     {0x0860, "Kashmiri (India)"},
250     {0x0861, "Nepali (India)"},
251     {0x0c01, "Arabic (Egypt)"},
252     {0x0c04, "Chinese (Hong Kong SAR, PRC)"},
253     {0x0c07, "German (Austria)"},
254     {0x0c09, "English (Australian)"},
255     {0x0c0a, "Spanish (Modern Sort)"},
256     {0x0c0c, "French (Canadian)"},
257     {0x0c1a, "Serbian (Cyrillic)"},
258     {0x1001, "Arabic (Libya)"},
259     {0x1004, "Chinese (Singapore)"},
260     {0x1007, "German (Luxembourg)"},
261     {0x1009, "English (Canadian)"},
262     {0x100a, "Spanish (Guatemala)"},
263     {0x100c, "French (Switzerland)"},
264     {0x1401, "Arabic (Algeria)"},
265     {0x1404, "Chinese (Macau SAR)"},
266     {0x1407, "German (Liechtenstein)"},
267     {0x1409, "English (New Zealand)"},
268     {0x140a, "Spanish (Costa Rica)"},
269     {0x140c, "French (Luxembourg)"},
270     {0x1801, "Arabic (Morocco)"},
271     {0x1809, "English (Ireland)"},
272     {0x180a, "Spanish (Panama)"},
273     {0x180c, "French (Monaco)"},
274     {0x1c01, "Arabic (Tunisia)"},
275     {0x1c09, "English (South Africa)"},
276     {0x1c0a, "Spanish (Dominican Republic)"},
277     {0x2001, "Arabic (Oman)"},
278     {0x2009, "English (Jamaica)"},
279     {0x200a, "Spanish (Venezuela)"},
280     {0x2401, "Arabic (Yemen)"},
281     {0x2409, "English (Caribbean)"},
282     {0x240a, "Spanish (Colombia)"},
283     {0x2801, "Arabic (Syria)"},
284     {0x2809, "English (Belize)"},
285     {0x280a, "Spanish (Peru)"},
286     {0x2c01, "Arabic (Jordan)"},
287     {0x2c09, "English (Trinidad)"},
288     {0x2c0a, "Spanish (Argentina)"},
289     {0x3001, "Arabic (Lebanon)"},
290     {0x3009, "English (Zimbabwe)"},
291     {0x300a, "Spanish (Ecuador)"},
292     {0x3401, "Arabic (Kuwait)"},
293     {0x3409, "English (Philippines)"},
294     {0x340a, "Spanish (Chile)"},
295     {0x3801, "Arabic (U.A.E.)"},
296     {0x380a, "Spanish (Uruguay)"},
297     {0x3c01, "Arabic (Bahrain)"},
298     {0x3c0a, "Spanish (Paraguay)"},
299     {0x4001, "Arabic (Qatar)"},
300     {0x400a, "Spanish (Bolivia)"},
301     {0x440a, "Spanish (El Salvador)"},
302     {0x480a, "Spanish (Honduras)"},
303     {0x4c0a, "Spanish (Nicaragua)"},
304     {0x500a, "Spanish (Puerto Rico)"},
305     {0xf0ff, "HID (Vendor Defined 1)"},
306     {0xf4ff, "HID (Vendor Defined 2)"},
307     {0xf8ff, "HID (Vendor Defined 3)"},
308     {0xfcff, "HID (Vendor Defined 4)"},
309     {0, NULL}
310 };
311
312 static value_string_ext usb_langid_vals_ext = VALUE_STRING_EXT_INIT(usb_langid_vals);
313
314 static const value_string usb_interfaceclass_vals[] = {
315     {IF_CLASS_FROM_INTERFACE_DESC,      "Use class info in Interface Descriptor"},
316     {IF_CLASS_AUDIO,                    "AUDIO"},
317     {IF_CLASS_COMMUNICATIONS,           "COMMUNICATIONS"},
318     {IF_CLASS_HID,                      "HID"},
319     {IF_CLASS_PHYSICAL,                 "PHYSICAL"},
320     {IF_CLASS_IMAGE,                    "IMAGE"},
321     {IF_CLASS_PRINTER,                  "PRINTER"},
322     {IF_CLASS_MASSTORAGE,               "MASSTORAGE"},
323     {IF_CLASS_HUB,                      "HUB"},
324     {IF_CLASS_CDC_DATA,                 "CDC_DATA"},
325     {IF_CLASS_SMART_CARD,               "SMART_CARD"},
326     {IF_CLASS_CONTENT_SECURITY,         "CONTENT_SECURITY"},
327     {IF_CLASS_VIDEO,                    "VIDEO"},
328     {IF_CLASS_DIAGNOSTIC_DEVICE,        "DIAGNOSTIC_DEVICE"},
329     {IF_CLASS_WIRELESS_CONTROLLER,      "WIRELESS_CONTROLLER"},
330     {IF_CLASS_MISCELLANEOUS,            "MISCELLANEOUS"},
331     {IF_CLASS_APPLICATION_SPECIFIC,     "APPLICATION_SPECIFIC"},
332     {IF_CLASS_VENDOR_SPECIFIC,          "VENDOR_SPECIFIC"},
333     {0, NULL}
334 };
335
336
337 static const value_string usb_transfer_type_vals[] = {
338     {URB_CONTROL, "URB_CONTROL"},
339     {URB_ISOCHRONOUS,"URB_ISOCHRONOUS"},
340     {URB_INTERRUPT,"URB_INTERRUPT"},
341     {URB_BULK,"URB_BULK"},
342     {0, NULL}
343 };
344
345 static const value_string usb_transfer_type_and_direction_vals[] = {
346     {URB_CONTROL, "URB_CONTROL out"},
347     {URB_ISOCHRONOUS,"URB_ISOCHRONOUS out"},
348     {URB_INTERRUPT,"URB_INTERRUPT out"},
349     {URB_BULK,"URB_BULK out"},
350     {URB_CONTROL | URB_TRANSFER_IN, "URB_CONTROL in"},
351     {URB_ISOCHRONOUS | URB_TRANSFER_IN,"URB_ISOCHRONOUS in"},
352     {URB_INTERRUPT | URB_TRANSFER_IN,"URB_INTERRUPT in"},
353     {URB_BULK | URB_TRANSFER_IN,"URB_BULK in"},
354     {0, NULL}
355 };
356
357 static const value_string usb_endpoint_direction_vals[] = {
358     {0, "OUT"},
359     {1, "IN"},
360     {0, NULL}
361 };
362
363 static const value_string usb_urb_type_vals[] = {
364     {URB_SUBMIT,  "URB_SUBMIT"},
365     {URB_COMPLETE,"URB_COMPLETE"},
366     {URB_ERROR,"URB_ERROR"},
367     {0, NULL}
368 };
369
370 /*
371  * Descriptor types.
372  */
373 #define USB_DT_DEVICE                   1
374 #define USB_DT_CONFIG                   2
375 #define USB_DT_STRING                   3
376 #define USB_DT_INTERFACE                4
377 #define USB_DT_ENDPOINT                 5
378 #define USB_DT_DEVICE_QUALIFIER         6
379 #define USB_DT_OTHER_SPEED_CONFIG       7
380 #define USB_DT_INTERFACE_POWER          8
381 /* these are from a minor usb 2.0 revision (ECN) */
382 #define USB_DT_OTG                      9
383 #define USB_DT_DEBUG                    10
384 #define USB_DT_INTERFACE_ASSOCIATION    11
385 /* these are from the Wireless USB spec */
386 #define USB_DT_SECURITY                 12
387 #define USB_DT_KEY                      13
388 #define USB_DT_ENCRYPTION_TYPE          14
389 #define USB_DT_BOS                      15
390 #define USB_DT_DEVICE_CAPABILITY        16
391 #define USB_DT_WIRELESS_ENDPOINT_COMP   17
392 #define USB_DT_HID                      33
393 #define USB_DT_RPIPE                    34
394
395 static const value_string descriptor_type_vals[] = {
396     {USB_DT_DEVICE,                     "DEVICE"},
397     {USB_DT_CONFIG,                     "CONFIGURATION"},
398     {USB_DT_STRING,                     "STRING"},
399     {USB_DT_INTERFACE,                  "INTERFACE"},
400     {USB_DT_ENDPOINT,                   "ENDPOINT"},
401     {USB_DT_DEVICE_QUALIFIER,           "DEVICE QUALIFIER"},
402     {USB_DT_OTHER_SPEED_CONFIG,         "OTHER_SPEED CONFIG"},
403     {USB_DT_INTERFACE_POWER,            "INTERFACE POWER"},
404     {USB_DT_OTG,                        "OTG"},
405     {USB_DT_DEBUG,                      "DEBUG"},
406     {USB_DT_INTERFACE_ASSOCIATION,      "INTERFACE ASSOCIATION"},
407     {USB_DT_SECURITY,                   "SECURITY"},
408     {USB_DT_KEY,                        "KEY"},
409     {USB_DT_ENCRYPTION_TYPE,            "ENCRYPTION TYPE"},
410     {USB_DT_BOS,                        "BOS"},
411     {USB_DT_DEVICE_CAPABILITY,          "DEVICE CAPABILITY"},
412     {USB_DT_WIRELESS_ENDPOINT_COMP,     "WIRELESS ENDPOINT COMP"},
413     {USB_DT_HID,                        "HID"},
414     {USB_DT_RPIPE,                      "RPIPE"},
415     {0,NULL}
416 };
417
418 /*
419  * Feature selectors.
420  */
421 #define USB_FS_DEVICE_REMOTE_WAKEUP     1
422 #define USB_FS_ENDPOINT_HALT            0
423 #define USB_FS_TEST_MODE                2
424
425 static const value_string usb_feature_selector_vals[] = {
426     {USB_FS_DEVICE_REMOTE_WAKEUP,       "DEVICE REMOTE WAKEUP"},
427     {USB_FS_ENDPOINT_HALT,              "ENDPOINT HALT"},
428     {USB_FS_TEST_MODE,                  "TEST MODE"},
429     {0,NULL}
430 };
431
432 static const value_string usb_bmAttributes_transfer_vals[] = {
433     {0x00,      "Control-Transfer"},
434     {0x01,      "Isochronous-Transfer"},
435     {0x02,      "Bulk-Transfer"},
436     {0x03,      "Interrupt-Transfer"},
437     {0,NULL}
438 };
439
440 static const value_string usb_bmAttributes_sync_vals[] = {
441     {0x00,      "No Sync"},
442     {0x04,      "Asynchronous"},
443     {0x08,      "Adaptive"},
444     {0x0c,      "Synchronous"},
445     {0,NULL}
446 };
447
448 static const value_string usb_bmAttributes_behaviour_vals[] = {
449     {0x00,      "Data-Endpoint"},
450     {0x10,      "Explicit Feedback-Endpoint"},
451     {0x20,      "Implicit Feedback-Data-Endpoint"},
452     {0x30,      "Reserved"},
453     {0,NULL}
454 };
455
456 /* from linux/include/asm-generic/errno.h */
457 #define EPERM            1      /* Operation not permitted */
458 #define ENOENT           2      /* No such file or directory */
459 #define ESRCH            3      /* No such process */
460 #define EINTR            4      /* Interrupted system call */
461 #define EIO              5      /* I/O error */
462 #define ENXIO            6      /* No such device or address */
463 #define E2BIG            7      /* Argument list too long */
464 #define ENOEXEC          8      /* Exec format error */
465 #define EBADF            9      /* Bad file number */
466 #define ECHILD          10      /* No child processes */
467 #define EAGAIN          11      /* Try again */
468 #define ENOMEM          12      /* Out of memory */
469 #define EACCES          13      /* Permission denied */
470 #define EFAULT          14      /* Bad address */
471 #define ENOTBLK         15      /* Block device required */
472 #define EBUSY           16      /* Device or resource busy */
473 #define EEXIST          17      /* File exists */
474 #define EXDEV           18      /* Cross-device link */
475 #define ENODEV          19      /* No such device */
476 #define ENOTDIR         20      /* Not a directory */
477 #define EISDIR          21      /* Is a directory */
478 #define EINVAL          22      /* Invalid argument */
479 #define ENFILE          23      /* File table overflow */
480 #define EMFILE          24      /* Too many open files */
481 #define ENOTTY          25      /* Not a typewriter */
482 #define ETXTBSY         26      /* Text file busy */
483 #define EFBIG           27      /* File too large */
484 #define ENOSPC          28      /* No space left on device */
485 #define ESPIPE          29      /* Illegal seek */
486 #define EROFS           30      /* Read-only file system */
487 #define EMLINK          31      /* Too many links */
488 #define EPIPE           32      /* Broken pipe */
489 #define EDOM            33      /* Math argument out of domain of func */
490 #define ERANGE          34      /* Math result not representable */
491
492
493 /* from linux/include/asm-generic/errno.h*/
494 #define EDEADLK         35      /* Resource deadlock would occur */
495 #define ENAMETOOLONG    36      /* File name too long */
496 #define ENOLCK          37      /* No record locks available */
497 #define ENOSYS          38      /* Function not implemented */
498 #define ENOTEMPTY       39      /* Directory not empty */
499 #define ELOOP           40      /* Too many symbolic links encountered */
500 #define EWOULDBLOCK     EAGAIN  /* Operation would block */
501 #define ENOMSG          42      /* No message of desired type */
502 #define EIDRM           43      /* Identifier removed */
503 #define ECHRNG          44      /* Channel number out of range */
504 #define EL2NSYNC        45      /* Level 2 not synchronized */
505 #define EL3HLT          46      /* Level 3 halted */
506 #define EL3RST          47      /* Level 3 reset */
507 #define ELNRNG          48      /* Link number out of range */
508 #define EUNATCH         49      /* Protocol driver not attached */
509 #define ENOCSI          50      /* No CSI structure available */
510 #define EL2HLT          51      /* Level 2 halted */
511 #define EBADE           52      /* Invalid exchange */
512 #define EBADR           53      /* Invalid request descriptor */
513 #define EXFULL          54      /* Exchange full */
514 #define ENOANO          55      /* No anode */
515 #define EBADRQC         56      /* Invalid request code */
516 #define EBADSLT         57      /* Invalid slot */
517
518 #define EDEADLOCK       EDEADLK
519
520 #define EBFONT          59      /* Bad font file format */
521 #define ENOSTR          60      /* Device not a stream */
522 #define ENODATA         61      /* No data available */
523 #define ETIME           62      /* Timer expired */
524 #define ENOSR           63      /* Out of streams resources */
525 #define ENONET          64      /* Machine is not on the network */
526 #define ENOPKG          65      /* Package not installed */
527 #define EREMOTE         66      /* Object is remote */
528 #define ENOLINK         67      /* Link has been severed */
529 #define EADV            68      /* Advertise error */
530 #define ESRMNT          69      /* Srmount error */
531 #define ECOMM           70      /* Communication error on send */
532 #define EPROTO          71      /* Protocol error */
533 #define EMULTIHOP       72      /* Multihop attempted */
534 #define EDOTDOT         73      /* RFS specific error */
535 #define EBADMSG         74      /* Not a data message */
536 #define EOVERFLOW       75      /* Value too large for defined data type */
537 #define ENOTUNIQ        76      /* Name not unique on network */
538 #define EBADFD          77      /* File descriptor in bad state */
539 #define EREMCHG         78      /* Remote address changed */
540 #define ELIBACC         79      /* Can not access a needed shared library */
541 #define ELIBBAD         80      /* Accessing a corrupted shared library */
542 #define ELIBSCN         81      /* .lib section in a.out corrupted */
543 #define ELIBMAX         82      /* Attempting to link in too many shared libraries */
544 #define ELIBEXEC        83      /* Cannot exec a shared library directly */
545 #define EILSEQ          84      /* Illegal byte sequence */
546 #define ERESTART        85      /* Interrupted system call should be restarted */
547 #define ESTRPIPE        86      /* Streams pipe error */
548 #define EUSERS          87      /* Too many users */
549 #define ENOTSOCK        88      /* Socket operation on non-socket */
550 #define EDESTADDRREQ    89      /* Destination address required */
551 #define EMSGSIZE        90      /* Message too long */
552 #define EPROTOTYPE      91      /* Protocol wrong type for socket */
553 #define ENOPROTOOPT     92      /* Protocol not available */
554 #define EPROTONOSUPPORT 93      /* Protocol not supported */
555 #define ESOCKTNOSUPPORT 94      /* Socket type not supported */
556 #define EOPNOTSUPP      95      /* Operation not supported on transport endpoint */
557 #define EPFNOSUPPORT    96      /* Protocol family not supported */
558 #define EAFNOSUPPORT    97      /* Address family not supported by protocol */
559 #define EADDRINUSE      98      /* Address already in use */
560 #define EADDRNOTAVAIL   99      /* Cannot assign requested address */
561 #define ENETDOWN        100     /* Network is down */
562 #define ENETUNREACH     101     /* Network is unreachable */
563 #define ENETRESET       102     /* Network dropped connection because of reset */
564 #define ECONNABORTED    103     /* Software caused connection abort */
565 #define ECONNRESET      104     /* Connection reset by peer */
566 #define ENOBUFS         105     /* No buffer space available */
567 #define EISCONN         106     /* Transport endpoint is already connected */
568 #define ENOTCONN        107     /* Transport endpoint is not connected */
569 #define ESHUTDOWN       108     /* Cannot send after transport endpoint shutdown */
570 #define ETOOMANYREFS    109     /* Too many references: cannot splice */
571 #define ETIMEDOUT       110     /* Connection timed out */
572 #define ECONNREFUSED    111     /* Connection refused */
573 #define EHOSTDOWN       112     /* Host is down */
574 #define EHOSTUNREACH    113     /* No route to host */
575 #define EALREADY        114     /* Operation already in progress */
576 #define EINPROGRESS     115     /* Operation now in progress */
577 #define ESTALE          116     /* Stale NFS file handle */
578 #define EUCLEAN         117     /* Structure needs cleaning */
579 #define ENOTNAM         118     /* Not a XENIX named type file */
580 #define ENAVAIL         119     /* No XENIX semaphores available */
581 #define EISNAM          120     /* Is a named type file */
582 #define EREMOTEIO       121     /* Remote I/O error */
583 #define EDQUOT          122     /* Quota exceeded */
584
585 #define ENOMEDIUM       123     /* No medium found */
586 #define EMEDIUMTYPE     124     /* Wrong medium type */
587 #define ECANCELED       125     /* Operation Canceled */
588 #define ENOKEY          126     /* Required key not available */
589 #define EKEYEXPIRED     127     /* Key has expired */
590 #define EKEYREVOKED     128     /* Key has been revoked */
591 #define EKEYREJECTED    129     /* Key was rejected by service */
592
593 /* for robust mutexes */
594 #define EOWNERDEAD      130     /* Owner died */
595 #define ENOTRECOVERABLE 131     /* State not recoverable */
596
597
598 static const value_string usb_urb_status_vals[] = {
599     { 0,                "Success"},
600     { -EPERM,           "Operation not permitted (-EPERM)" },
601     { -ENOENT,          "No such file or directory (-ENOENT)" },
602     { -ESRCH,           "No such process (-ESRCH)" },
603     { -EINTR,           "Interrupted system call (-EINTR)" },
604     { -EIO,             "I/O error (-EIO)" },
605     { -ENXIO,           "No such device or address (-ENXIO)" },
606     { -E2BIG,           "Argument list too long (-E2BIG)" },
607     { -ENOEXEC,         "Exec format error (-ENOEXEC)" },
608     { -EBADF,           "Bad file number (-EBADF)" },
609     { -ECHILD,          "No child processes (-ECHILD)" },
610     { -EAGAIN,          "Try again (-EAGAIN)" },
611     { -ENOMEM,          "Out of memory (-ENOMEM)" },
612     { -EACCES,          "Permission denied (-EACCES)" },
613     { -EFAULT,          "Bad address (-EFAULT)" },
614     { -ENOTBLK,         "Block device required (-ENOTBLK)" },
615     { -EBUSY,           "Device or resource busy (-EBUSY)" },
616     { -EEXIST,          "File exists (-EEXIST)" },
617     { -EXDEV,           "Cross-device link (-EXDEV)" },
618     { -ENODEV,          "No such device (-ENODEV)" },
619     { -ENOTDIR,         "Not a directory (-ENOTDIR)" },
620     { -EISDIR,          "Is a directory (-EISDIR)" },
621     { -EINVAL,          "Invalid argument (-EINVAL)" },
622     { -ENFILE,          "File table overflow (-ENFILE)" },
623     { -EMFILE,          "Too many open files (-EMFILE)" },
624     { -ENOTTY,          "Not a typewriter (-ENOTTY)" },
625     { -ETXTBSY,         "Text file busy (-ETXTBSY)" },
626     { -EFBIG,           "File too large (-EFBIG)" },
627     { -ENOSPC,          "No space left on device (-ENOSPC)" },
628     { -ESPIPE,          "Illegal seek (-ESPIPE)" },
629     { -EROFS,           "Read-only file system (-EROFS)" },
630     { -EMLINK,          "Too many links (-EMLINK)" },
631     { -EPIPE,           "Broken pipe (-EPIPE)" },
632     { -EDOM,            "Math argument out of domain of func (-EDOM)" },
633     { -ERANGE,          "Math result not representable (-ERANGE)" },
634     { -EDEADLK,         "Resource deadlock would occur (-EDEADLK)" },
635     { -ENAMETOOLONG,    "File name too long (-ENAMETOOLONG)" },
636     { -ENOLCK,          "No record locks available (-ENOLCK)" },
637     { -ENOSYS,          "Function not implemented (-ENOSYS)" },
638     { -ENOTEMPTY,       "Directory not empty (-ENOTEMPTY)" },
639     { -ELOOP,           "Too many symbolic links encountered (-ELOOP)" },
640     { -ENOMSG,          "No message of desired type (-ENOMSG)" },
641     { -EIDRM,           "Identifier removed (-EIDRM)" },
642     { -ECHRNG,          "Channel number out of range (-ECHRNG)" },
643     { -EL2NSYNC,        "Level 2 not synchronized (-EL2NSYNC)" },
644     { -EL3HLT,          "Level 3 halted (-EL3HLT)" },
645     { -EL3RST,          "Level 3 reset (-EL3RST)" },
646     { -ELNRNG,          "Link number out of range (-ELNRNG)" },
647     { -EUNATCH,         "Protocol driver not attached (-EUNATCH)" },
648     { -ENOCSI,          "No CSI structure available (-ENOCSI)" },
649     { -EL2HLT,          "Level 2 halted (-EL2HLT)" },
650     { -EBADE,           "Invalid exchange (-EBADE)" },
651     { -EBADR,           "Invalid request descriptor (-EBADR)" },
652     { -EXFULL,          "Exchange full (-EXFULL)" },
653     { -ENOANO,          "No anode (-ENOANO)" },
654     { -EBADRQC,         "Invalid request code (-EBADRQC)" },
655     { -EBADSLT,         "Invalid slot (-EBADSLT)" },
656     { -EBFONT,          "Bad font file format (-EBFONT)" },
657     { -ENOSTR,          "Device not a stream (-ENOSTR)" },
658     { -ENODATA,         "No data available (-ENODATA)" },
659     { -ETIME,           "Timer expired (-ETIME)" },
660     { -ENOSR,           "Out of streams resources (-ENOSR)" },
661     { -ENONET,          "Machine is not on the network (-ENONET)" },
662     { -ENOPKG,          "Package not installed (-ENOPKG)" },
663     { -EREMOTE,         "Object is remote (-EREMOTE)" },
664     { -ENOLINK,         "Link has been severed (-ENOLINK)" },
665     { -EADV,            "Advertise error (-EADV)" },
666     { -ESRMNT,          "Srmount error (-ESRMNT)" },
667     { -ECOMM,           "Communication error on send (-ECOMM)" },
668     { -EPROTO,          "Protocol error (-EPROTO)" },
669     { -EMULTIHOP,       "Multihop attempted (-EMULTIHOP)" },
670     { -EDOTDOT,         "RFS specific error (-EDOTDOT)" },
671     { -EBADMSG,         "Not a data message (-EBADMSG)" },
672     { -EOVERFLOW,       "Value too large for defined data type (-EOVERFLOW)" },
673     { -ENOTUNIQ,        "Name not unique on network (-ENOTUNIQ)" },
674     { -EBADFD,          "File descriptor in bad state (-EBADFD)" },
675     { -EREMCHG,         "Remote address changed (-EREMCHG)" },
676     { -ELIBACC,         "Can not access a needed shared library (-ELIBACC)" },
677     { -ELIBBAD,         "Accessing a corrupted shared library (-ELIBBAD)" },
678     { -ELIBSCN,         ".lib section in a.out corrupted (-ELIBSCN)" },
679     { -ELIBMAX,         "Attempting to link in too many shared libraries (-ELIBMAX)" },
680     { -ELIBEXEC,        "Cannot exec a shared library directly (-ELIBEXEC)" },
681     { -EILSEQ,          "Illegal byte sequence (-EILSEQ)" },
682     { -ERESTART,        "Interrupted system call should be restarted (-ERESTART)" },
683     { -ESTRPIPE,        "Streams pipe error (-ESTRPIPE)" },
684     { -EUSERS,          "Too many users (-EUSERS)" },
685     { -ENOTSOCK,        "Socket operation on non-socket (-ENOTSOCK)" },
686     { -EDESTADDRREQ,    "Destination address required (-EDESTADDRREQ)" },
687     { -EMSGSIZE,        "Message too long (-EMSGSIZE)" },
688     { -EPROTOTYPE,      "Protocol wrong type for socket (-EPROTOTYPE)" },
689     { -ENOPROTOOPT,     "Protocol not available (-ENOPROTOOPT)" },
690     { -EPROTONOSUPPORT, "Protocol not supported (-EPROTONOSUPPORT)" },
691     { -ESOCKTNOSUPPORT, "Socket type not supported (-ESOCKTNOSUPPORT)" },
692     { -EOPNOTSUPP,      "Operation not supported on transport endpoint (-EOPNOTSUPP)" },
693     { -EPFNOSUPPORT,    "Protocol family not supported (-EPFNOSUPPORT)" },
694     { -EAFNOSUPPORT,    "Address family not supported by protocol (-EAFNOSUPPORT)" },
695     { -EADDRINUSE,      "Address already in use (-EADDRINUSE)" },
696     { -EADDRNOTAVAIL,   "Cannot assign requested address (-EADDRNOTAVAIL)" },
697     { -ENETDOWN,        "Network is down (-ENETDOWN)" },
698     { -ENETUNREACH,     "Network is unreachable (-ENETUNREACH)" },
699     { -ENETRESET,       "Network dropped connection because of reset (-ENETRESET)" },
700     { -ECONNABORTED,    "Software caused connection abort (-ECONNABORTED)" },
701     { -ECONNRESET,      "Connection reset by peer (-ECONNRESET)" },
702     { -ENOBUFS,         "No buffer space available (-ENOBUFS)" },
703     { -EISCONN,         "Transport endpoint is already connected (-EISCONN)" },
704     { -ENOTCONN,        "Transport endpoint is not connected (-ENOTCONN)" },
705     { -ESHUTDOWN,       "Cannot send after transport endpoint shutdown (-ESHUTDOWN)" },
706     { -ETOOMANYREFS,    "Too many references: cannot splice (-ETOOMANYREFS)" },
707     { -ETIMEDOUT,       "Connection timed out (-ETIMEDOUT)" },
708     { -ECONNREFUSED,    "Connection refused (-ECONNREFUSED)" },
709     { -EHOSTDOWN,       "Host is down (-EHOSTDOWN)" },
710     { -EHOSTUNREACH,    "No route to host (-EHOSTUNREACH)" },
711     { -EALREADY,        "Operation already in progress (-EALREADY)" },
712     { -EINPROGRESS,     "Operation now in progress (-EINPROGRESS)" },
713     { -ESTALE,          "Stale NFS file handle (-ESTALE)" },
714     { -EUCLEAN,         "Structure needs cleaning (-EUCLEAN)" },
715     { -ENOTNAM,         "Not a XENIX named type file (-ENOTNAM)" },
716     { -ENAVAIL,         "No XENIX semaphores available (-ENAVAIL)" },
717     { -EISNAM,          "Is a named type file (-EISNAM)" },
718     { -EREMOTEIO,       "Remote I/O error (-EREMOTEIO)" },
719     { -EDQUOT,          "Quota exceeded (-EDQUOT)" },
720     { -ENOMEDIUM,       "No medium found (-ENOMEDIUM)" },
721     { -EMEDIUMTYPE,     "Wrong medium type (-EMEDIUMTYPE)" },
722     { -ECANCELED,       "Operation Canceled (-ECANCELED)" },
723     { -ENOKEY,          "Required key not available (-ENOKEY)" },
724     { -EKEYEXPIRED,     "Key has expired (-EKEYEXPIRED)" },
725     { -EKEYREVOKED,     "Key has been revoked (-EKEYREVOKED)" },
726     { -EKEYREJECTED,    "Key was rejected by service (-EKEYREJECTED)" },
727     { -EOWNERDEAD,      "Owner died (-EOWNERDEAD)" },
728     { -ENOTRECOVERABLE, "State not recoverable (-ENOTRECOVERABLE)" },
729     { 0, NULL }
730 };
731
732
733 static usb_conv_info_t *
734 get_usb_conv_info(conversation_t *conversation)
735 {
736     usb_conv_info_t *usb_conv_info;
737
738     /* do we have conversation specific data ? */
739     usb_conv_info = conversation_get_proto_data(conversation, proto_usb);
740     if(!usb_conv_info){
741         /* no not yet so create some */
742         usb_conv_info = se_alloc0(sizeof(usb_conv_info_t));
743         usb_conv_info->interfaceClass=IF_CLASS_UNKNOWN;
744         usb_conv_info->transactions=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "usb transactions");
745
746         conversation_add_proto_data(conversation, proto_usb, usb_conv_info);
747     }
748
749     return usb_conv_info;
750 }
751
752 static conversation_t *
753 get_usb_conversation(packet_info *pinfo, address *src_addr, address *dst_addr, guint32 src_endpoint, guint32 dst_endpoint)
754 {
755     conversation_t *conversation;
756
757     /*
758      * Do we have a conversation for this connection?
759      */
760     conversation = find_conversation(pinfo->fd->num,
761                                src_addr, dst_addr,
762                                pinfo->ptype,
763                                src_endpoint, dst_endpoint, 0);
764     if(conversation){
765         return conversation;
766     }
767
768     /* We don't yet have a conversation, so create one. */
769     conversation = conversation_new(pinfo->fd->num,
770                            src_addr, dst_addr,
771                            pinfo->ptype,
772                            src_endpoint, dst_endpoint, 0);
773     return conversation;
774 }
775
776
777
778 /* SETUP dissectors */
779
780
781 /*
782  * These dissectors are used to dissect the setup part and the data
783  * for URB_CONTROL_INPUT / CLEAR FEATURE
784  */
785
786
787 /* 9.4.1 */
788 static int
789 dissect_usb_setup_clear_feature_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
790 {
791     /* feature selector */
792     proto_tree_add_item(tree, hf_usb_wFeatureSelector, tvb, offset, 2, TRUE);
793     offset += 2;
794
795     /* zero/interface/endpoint */
796     /* XXX - check based on request type */
797     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
798     offset += 2;
799
800     /* length */
801     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
802     offset += 2;
803
804     return offset;
805 }
806
807 static int
808 dissect_usb_setup_clear_feature_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
809 {
810     return offset;
811 }
812
813
814 /*
815  * These dissectors are used to dissect the setup part and the data
816  * for URB_CONTROL_INPUT / GET CONFIGURATION
817  */
818
819
820 /* 9.4.2 */
821 static int
822 dissect_usb_setup_get_configuration_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
823 {
824     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, TRUE);
825     offset++;
826
827     return offset;
828 }
829
830
831 /*
832  * These dissectors are used to dissect the setup part and the data
833  * for URB_CONTROL_INPUT / GET DESCRIPTOR
834  */
835
836
837 /* 9.6.2 */
838 static int
839 dissect_usb_device_qualifier_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
840 {
841     proto_item *item=NULL;
842     proto_tree *tree=NULL;
843     int old_offset=offset;
844
845     if(parent_tree){
846         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "DEVICE QUALIFIER DESCRIPTOR");
847         tree=proto_item_add_subtree(item, ett_descriptor_device);
848     }
849
850     /* bLength */
851     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
852     offset++;
853
854     /* bDescriptorType */
855     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
856     offset++;
857
858     /* bcdUSB */
859     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, TRUE);
860     offset+=2;
861
862     /* bDeviceClass */
863     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, TRUE);
864     offset++;
865
866     /* bDeviceSubClass */
867     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, TRUE);
868     offset++;
869
870     /* bDeviceProtocol */
871     proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, TRUE);
872     offset++;
873
874     /* bMaxPacketSize0 */
875     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, TRUE);
876     offset++;
877
878     /* bNumConfigurations */
879     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, TRUE);
880     offset++;
881
882     /* one reserved byte */
883     offset++;
884
885     if(item){
886         proto_item_set_len(item, offset-old_offset);
887     }
888
889     return offset;
890 }
891
892 /* 9.6.1 */
893 static int
894 dissect_usb_device_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
895 {
896     proto_item *item=NULL;
897     proto_tree *tree=NULL;
898     int old_offset=offset;
899
900     if(parent_tree){
901         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "DEVICE DESCRIPTOR");
902         tree=proto_item_add_subtree(item, ett_descriptor_device);
903     }
904
905     /* bLength */
906     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
907     offset++;
908
909     /* bDescriptorType */
910     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
911     offset++;
912
913     /* bcdUSB */
914     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, TRUE);
915     offset+=2;
916
917     /* bDeviceClass */
918     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, TRUE);
919     offset++;
920
921     /* bDeviceSubClass */
922     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, TRUE);
923     offset++;
924
925     /* bDeviceProtocol */
926     proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, TRUE);
927     offset++;
928
929     /* bMaxPacketSize0 */
930     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, TRUE);
931     offset++;
932
933     /* idVendor */
934     proto_tree_add_item(tree, hf_usb_idVendor, tvb, offset, 2, TRUE);
935     offset+=2;
936
937     /* idProduct */
938     proto_tree_add_item(tree, hf_usb_idProduct, tvb, offset, 2, TRUE);
939     offset+=2;
940
941     /* bcdDevice */
942     proto_tree_add_item(tree, hf_usb_bcdDevice, tvb, offset, 2, TRUE);
943     offset+=2;
944
945     /* iManufacturer */
946     proto_tree_add_item(tree, hf_usb_iManufacturer, tvb, offset, 1, TRUE);
947     offset++;
948
949     /* iProduct */
950     proto_tree_add_item(tree, hf_usb_iProduct, tvb, offset, 1, TRUE);
951     offset++;
952
953     /* iSerialNumber */
954     proto_tree_add_item(tree, hf_usb_iSerialNumber, tvb, offset, 1, TRUE);
955     offset++;
956
957     /* bNumConfigurations */
958     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, TRUE);
959     offset++;
960
961     if(item){
962         proto_item_set_len(item, offset-old_offset);
963     }
964
965     return offset;
966 }
967
968 /* 9.6.7 */
969 static int
970 dissect_usb_string_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info _U_)
971 {
972     proto_item *item=NULL;
973     proto_tree *tree=NULL;
974     int old_offset=offset;
975     guint8 len;
976
977     if(parent_tree){
978         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "STRING DESCRIPTOR");
979         tree=proto_item_add_subtree(item, ett_descriptor_device);
980     }
981
982     /* bLength */
983     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
984     len=tvb_get_guint8(tvb, offset);
985     offset++;
986
987     /* bDescriptorType */
988     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
989     offset++;
990
991     if(!usb_trans_info->u.get_descriptor.index){
992         /* list of languanges */
993         while(len>(offset-old_offset)){
994             /* wLANGID */
995             proto_tree_add_item(tree, hf_usb_wLANGID, tvb, offset, 2, TRUE);
996             offset+=2;
997         }
998     } else {
999         char *str;
1000
1001         /* unicode string */
1002         str=tvb_get_ephemeral_faked_unicode(tvb, offset, (len-2)/2, TRUE);
1003         proto_tree_add_string(tree, hf_usb_bString, tvb, offset, len-2, str);
1004         offset += len-2;
1005     }
1006
1007     if(item){
1008         proto_item_set_len(item, offset-old_offset);
1009     }
1010
1011     return offset;
1012 }
1013
1014
1015
1016 /* 9.6.5 */
1017 static int
1018 dissect_usb_interface_descriptor(packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info)
1019 {
1020     proto_item *item=NULL;
1021     proto_tree *tree=NULL;
1022     int old_offset=offset;
1023     guint8 len;
1024
1025     if(parent_tree){
1026         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "INTERFACE DESCRIPTOR");
1027         tree=proto_item_add_subtree(item, ett_descriptor_device);
1028     }
1029
1030     /* bLength */
1031     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
1032     len = tvb_get_guint8(tvb, offset);
1033     offset++;
1034
1035     /* bDescriptorType */
1036     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1037     offset++;
1038
1039     /* bInterfaceNumber */
1040     proto_tree_add_item(tree, hf_usb_bInterfaceNumber, tvb, offset, 1, TRUE);
1041     offset++;
1042
1043     /* bAlternateSetting */
1044     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, TRUE);
1045     offset++;
1046
1047     /* bNumEndpoints */
1048     proto_tree_add_item(tree, hf_usb_bNumEndpoints, tvb, offset, 1, TRUE);
1049     offset++;
1050
1051     /* bInterfaceClass */
1052     proto_tree_add_item(tree, hf_usb_bInterfaceClass, tvb, offset, 1, TRUE);
1053     /* save the class so we can access it later in the endpoint descriptor */
1054     usb_conv_info->interfaceClass=tvb_get_guint8(tvb, offset);
1055     if(!pinfo->fd->flags.visited){
1056         usb_trans_info->interface_info=se_alloc0(sizeof(usb_conv_info_t));
1057         usb_trans_info->interface_info->interfaceClass=tvb_get_guint8(tvb, offset);
1058         usb_trans_info->interface_info->transactions=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "usb transactions");
1059     }
1060     offset++;
1061
1062     /* bInterfaceSubClass */
1063     proto_tree_add_item(tree, hf_usb_bInterfaceSubClass, tvb, offset, 1, TRUE);
1064     offset++;
1065
1066     /* bInterfaceProtocol */
1067     proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, TRUE);
1068     offset++;
1069
1070     /* iInterface */
1071     proto_tree_add_item(tree, hf_usb_iInterface, tvb, offset, 1, TRUE);
1072     offset++;
1073
1074     if(item){
1075         proto_item_set_len(item, len);
1076     }
1077     if (offset != old_offset + len) {
1078         /* unknown records */
1079     }
1080     offset = old_offset + len;
1081
1082     return offset;
1083 }
1084
1085 /* 9.6.6 */
1086 static const true_false_string tfs_endpoint_direction = {
1087     "IN Endpoint",
1088     "OUT Endpoint"
1089 };
1090 static int
1091 dissect_usb_endpoint_descriptor(packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1092 {
1093     proto_item *item=NULL;
1094     proto_tree *tree=NULL;
1095     proto_item *endpoint_item=NULL;
1096     proto_tree *endpoint_tree=NULL;
1097     proto_item *ep_attrib_item=NULL;
1098     proto_tree *ep_attrib_tree=NULL;
1099     int old_offset=offset;
1100     guint8 endpoint;
1101     guint8 len;
1102
1103     if(parent_tree){
1104         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "ENDPOINT DESCRIPTOR");
1105         tree=proto_item_add_subtree(item, ett_descriptor_device);
1106     }
1107
1108     /* bLength */
1109     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
1110     len = tvb_get_guint8(tvb, offset);
1111     offset++;
1112
1113     /* bDescriptorType */
1114     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1115     offset++;
1116
1117     /* bEndpointAddress */
1118     if(tree){
1119         endpoint_item=proto_tree_add_item(tree, hf_usb_bEndpointAddress, tvb, offset, 1, TRUE);
1120         endpoint_tree=proto_item_add_subtree(endpoint_item, ett_configuration_bEndpointAddress);
1121     }
1122     endpoint=tvb_get_guint8(tvb, offset)&0x0f;
1123     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_direction, tvb, offset, 1, TRUE);
1124     proto_item_append_text(endpoint_item, "  %s", (tvb_get_guint8(tvb, offset)&0x80)?"IN":"OUT");
1125     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_number, tvb, offset, 1, TRUE);
1126     proto_item_append_text(endpoint_item, "  Endpoint:%d", endpoint);
1127     offset++;
1128
1129     /* Together with class from the interface descriptor we know what kind
1130      * of class the device at endpoint is.
1131      * Make sure a conversation exists for this endpoint and attach a
1132      * usb_conv_into_t structure to it.
1133      *
1134      * All endpoints for the same interface descriptor share the same
1135      * usb_conv_info structure.
1136      */
1137     if((!pinfo->fd->flags.visited)&&usb_trans_info->interface_info){
1138         conversation_t *conversation;
1139
1140         if(pinfo->destport==NO_ENDPOINT){
1141             static address tmp_addr;
1142             static usb_address_t usb_addr;
1143
1144             /* Create a new address structure that points to the same device
1145              * but the new endpoint.
1146              */
1147             usb_addr.device=((usb_address_t *)(pinfo->src.data))->device;
1148             usb_addr.endpoint=endpoint;
1149             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
1150             conversation=get_usb_conversation(pinfo, &tmp_addr, &pinfo->dst, endpoint, pinfo->destport);
1151         } else {
1152             static address tmp_addr;
1153             static usb_address_t usb_addr;
1154
1155             /* Create a new address structure that points to the same device
1156              * but the new endpoint.
1157              */
1158             usb_addr.device=((usb_address_t *)(pinfo->dst.data))->device;
1159             usb_addr.endpoint=endpoint;
1160             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
1161             conversation=get_usb_conversation(pinfo, &pinfo->src, &tmp_addr, pinfo->srcport, endpoint);
1162         }
1163
1164         conversation_add_proto_data(conversation, proto_usb, usb_trans_info->interface_info);
1165     }
1166
1167     /* bmAttributes */
1168     if (tree) {
1169         ep_attrib_item=proto_tree_add_item(tree, hf_usb_bmAttributes, tvb, offset, 1, TRUE);
1170         ep_attrib_tree=proto_item_add_subtree(ep_attrib_item, ett_endpoint_bmAttributes);
1171     }
1172     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeTransfer, tvb, offset, 1, TRUE);
1173     /* isochronous only */
1174     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeSynchonisation, tvb, offset, 1, TRUE);
1175     /* isochronous only */
1176     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeBehaviour, tvb, offset, 1, TRUE);
1177     offset++;
1178
1179     /* wMaxPacketSize */
1180     proto_tree_add_item(tree, hf_usb_wMaxPacketSize, tvb, offset, 2, TRUE);
1181     offset+=2;
1182
1183     /* bInterval */
1184     proto_tree_add_item(tree, hf_usb_bInterval, tvb, offset, 1, TRUE);
1185     offset++;
1186
1187     if(item){
1188         proto_item_set_len(item, len);
1189     }
1190     if (offset != old_offset + len) {
1191         /* unknown records */
1192     }
1193     offset = old_offset + len;
1194
1195     return offset;
1196 }
1197
1198 static int
1199 dissect_usb_unknown_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1200 {
1201     proto_item *item=NULL;
1202     proto_tree *tree=NULL;
1203     int old_offset=offset;
1204     guint8 bLength;
1205
1206     if(parent_tree){
1207         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "UNKNOWN DESCRIPTOR");
1208         tree=proto_item_add_subtree(item, ett_descriptor_device);
1209     }
1210
1211     /* bLength */
1212     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
1213     bLength = tvb_get_guint8(tvb, offset);
1214     offset++;
1215     if (bLength < 3) {
1216         if(item){
1217             proto_item_set_len(item, offset-old_offset);
1218         }
1219
1220         item = proto_tree_add_text(parent_tree, tvb, offset - 1, 1,
1221             "Invalid bLength: %u",  bLength);
1222         expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
1223             "Invalid bLength: %u",  bLength);
1224
1225         return offset;
1226     }
1227
1228     /* bDescriptorType */
1229     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1230     offset++;
1231
1232     offset += bLength - 2;
1233
1234     if(item){
1235         proto_item_set_len(item, offset-old_offset);
1236     }
1237
1238     return offset;
1239 }
1240
1241 /* 9.6.3 */
1242 static const true_false_string tfs_mustbeone = {
1243     "Must be 1 for USB 1.1 and higher",
1244     "FIXME: Is this a USB 1.0 device"
1245 };
1246 static const true_false_string tfs_selfpowered = {
1247     "This device is SELF-POWERED",
1248     "This device is powered from the USB bus"
1249 };
1250 static const true_false_string tfs_remotewakeup = {
1251     "This device supports REMOTE WAKEUP",
1252     "This device does NOT support remote wakeup"
1253 };
1254 static int
1255 dissect_usb_configuration_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info)
1256 {
1257     proto_item *item=NULL;
1258     proto_tree *tree=NULL;
1259     int old_offset=offset;
1260     guint16 len;
1261     proto_item *flags_item=NULL;
1262     proto_tree *flags_tree=NULL;
1263     guint8 flags;
1264     proto_item *power_item=NULL;
1265     guint8 power;
1266
1267     if(parent_tree){
1268         item=proto_tree_add_text(parent_tree, tvb, offset, -1, "CONFIGURATION DESCRIPTOR");
1269         tree=proto_item_add_subtree(item, ett_descriptor_device);
1270     }
1271
1272     /* bLength */
1273     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
1274     offset++;
1275
1276     /* bDescriptorType */
1277     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1278     offset++;
1279
1280     /* wTotalLength */
1281     proto_tree_add_item(tree, hf_usb_wTotalLength, tvb, offset, 2, TRUE);
1282     len=tvb_get_letohs(tvb, offset);
1283     offset+=2;
1284
1285     /* bNumInterfaces */
1286     proto_tree_add_item(tree, hf_usb_bNumInterfaces, tvb, offset, 1, TRUE);
1287     offset++;
1288
1289     /* bConfigurationValue */
1290     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, TRUE);
1291     offset++;
1292
1293     /* iConfiguration */
1294     proto_tree_add_item(tree, hf_usb_iConfiguration, tvb, offset, 1, TRUE);
1295     offset++;
1296
1297     /* bmAttributes */
1298     if(tree){
1299         flags_item=proto_tree_add_item(tree, hf_usb_configuration_bmAttributes, tvb, offset, 1, TRUE);
1300         flags_tree=proto_item_add_subtree(flags_item, ett_configuration_bmAttributes);
1301     }
1302     flags=tvb_get_guint8(tvb, offset);
1303     proto_tree_add_item(flags_tree, hf_usb_configuration_legacy10buspowered, tvb, offset, 1, TRUE);
1304     proto_tree_add_item(flags_tree, hf_usb_configuration_selfpowered, tvb, offset, 1, TRUE);
1305     proto_item_append_text(flags_item, "  %sSELF-POWERED", (flags&0x40)?"":"NOT ");
1306     flags&=~0x40;
1307     proto_tree_add_item(flags_tree, hf_usb_configuration_remotewakeup, tvb, offset, 1, TRUE);
1308     proto_item_append_text(flags_item, "  %sREMOTE-WAKEUP", (flags&0x20)?"":"NO ");
1309     flags&=~0x20;
1310     offset++;
1311
1312     /* bMaxPower */
1313     power_item=proto_tree_add_item(tree, hf_usb_bMaxPower, tvb, offset, 1, TRUE);
1314     power=tvb_get_guint8(tvb, offset);
1315     proto_item_append_text(power_item, "  (%dmA)", power*2);
1316     offset++;
1317
1318     /* initialize interface_info to NULL */
1319     usb_trans_info->interface_info=NULL;
1320
1321     /* decode any additional interface and endpoint descriptors */
1322     while(len>(old_offset-offset)){
1323         guint8 next_type;
1324
1325         if(tvb_length_remaining(tvb, offset)<2){
1326             break;
1327         }
1328         next_type=tvb_get_guint8(tvb, offset+1);
1329         switch(next_type){
1330         case USB_DT_INTERFACE:
1331             offset=dissect_usb_interface_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1332             break;
1333         case USB_DT_ENDPOINT:
1334             offset=dissect_usb_endpoint_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1335             break;
1336         default:
1337             offset=dissect_usb_unknown_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1338             break;
1339             /* was: return offset; */
1340         }
1341     }
1342
1343     if(item){
1344         proto_item_set_len(item, offset-old_offset);
1345     }
1346
1347     return offset;
1348 }
1349
1350 /* 9.4.3 */
1351 static int
1352 dissect_usb_setup_get_descriptor_request(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info _U_)
1353 {
1354     /* descriptor index */
1355     proto_tree_add_item(tree, hf_usb_descriptor_index, tvb, offset, 1, TRUE);
1356     usb_trans_info->u.get_descriptor.index=tvb_get_guint8(tvb, offset);
1357     offset++;
1358
1359     /* descriptor type */
1360     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1361     usb_trans_info->u.get_descriptor.type=tvb_get_guint8(tvb, offset);
1362     offset++;
1363     if (check_col(pinfo->cinfo, COL_INFO)) {
1364         col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1365             val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %u"));
1366     }
1367
1368     /* language id */
1369     proto_tree_add_item(tree, hf_usb_language_id, tvb, offset, 2, TRUE);
1370     offset+=2;
1371
1372     /* length */
1373     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1374     offset += 2;
1375
1376     return offset;
1377 }
1378
1379 static int
1380 dissect_usb_setup_get_descriptor_response(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info)
1381 {
1382     proto_item *item=NULL;
1383     guint32 data_len;
1384
1385     if (check_col(pinfo->cinfo, COL_INFO)) {
1386         col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1387             val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %u"));
1388     }
1389     switch(usb_trans_info->u.get_descriptor.type){
1390     case USB_DT_DEVICE:
1391         offset=dissect_usb_device_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1392         break;
1393     case USB_DT_CONFIG:
1394         offset=dissect_usb_configuration_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1395         break;
1396     case USB_DT_STRING:
1397         offset=dissect_usb_string_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1398         break;
1399     case USB_DT_INTERFACE:
1400         offset=dissect_usb_interface_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1401         break;
1402     case USB_DT_ENDPOINT:
1403         offset=dissect_usb_endpoint_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1404         break;
1405     case USB_DT_DEVICE_QUALIFIER:
1406         offset=dissect_usb_device_qualifier_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1407         break;
1408     case USB_DT_RPIPE:
1409         if (usb_conv_info->interfaceClass == IF_CLASS_HID) {
1410                 offset=dissect_usb_hid_get_report_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1411                 break;
1412         }
1413         /* else fall through as default/unknown */
1414     default:
1415         /* XXX dissect the descriptor coming back from the device */
1416         item=proto_tree_add_text(tree, tvb, offset, -1, "GET DESCRIPTOR data (unknown descriptor type)");
1417         tree=proto_item_add_subtree(item, ett_descriptor_device);
1418         tvb_memcpy(tvb, (guint8 *)&data_len, offset, 4);
1419         proto_tree_add_uint(tree, hf_usb_data, tvb, offset, 4, data_len);
1420         offset += data_len;
1421         break;
1422     }
1423
1424     return offset;
1425 }
1426
1427
1428 /*
1429  * These dissectors are used to dissect the setup part and the data
1430  * for URB_CONTROL_INPUT / GET INTERFACE
1431  */
1432
1433
1434 /* 9.4.4 */
1435 static int
1436 dissect_usb_setup_get_interface_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1437 {
1438     /* zero */
1439     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
1440     offset += 2;
1441
1442     /* interface */
1443     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
1444     offset += 2;
1445
1446     /* length */
1447     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1448     offset += 2;
1449
1450     return offset;
1451 }
1452
1453 static int
1454 dissect_usb_setup_get_interface_response(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1455 {
1456     /* alternate setting */
1457     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, TRUE);
1458     offset++;
1459
1460     return offset;
1461 }
1462
1463
1464 /*
1465  * These dissectors are used to dissect the setup part and the data
1466  * for URB_CONTROL_INPUT / GET STATUS
1467  */
1468
1469
1470 /* 9.4.5 */
1471 static int
1472 dissect_usb_setup_get_status_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1473 {
1474     /* zero */
1475     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
1476     offset += 2;
1477
1478     /* zero/interface/endpoint */
1479     /* XXX - check based on request type */
1480     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
1481     offset += 2;
1482
1483     /* length */
1484     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1485     offset += 2;
1486
1487     return offset;
1488 }
1489
1490 static int
1491 dissect_usb_setup_get_status_response(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1492 {
1493     /* status */
1494     /* XXX - show bits */
1495     proto_tree_add_item(tree, hf_usb_wStatus, tvb, offset, 2, TRUE);
1496     offset += 2;
1497
1498     return offset;
1499 }
1500
1501
1502 /*
1503  * These dissectors are used to dissect the setup part and the data
1504  * for URB_CONTROL_INPUT / SET ADDRESS
1505  */
1506
1507
1508 /* 9.4.6 */
1509 static int
1510 dissect_usb_setup_set_address_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1511 {
1512     /* device address */
1513     proto_tree_add_item(tree, hf_usb_device_address, tvb, offset, 2, TRUE);
1514     offset += 2;
1515
1516     /* zero */
1517     proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
1518     offset += 2;
1519
1520     /* zero */
1521     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1522     offset += 2;
1523
1524     return offset;
1525 }
1526
1527 static int
1528 dissect_usb_setup_set_address_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1529 {
1530     return offset;
1531 }
1532
1533
1534 /*
1535  * These dissectors are used to dissect the setup part and the data
1536  * for URB_CONTROL_INPUT / SET CONFIGURATION
1537  */
1538
1539
1540 /* 9.4.7 */
1541 static int
1542 dissect_usb_setup_set_configuration_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1543 {
1544     /* configuration value */
1545     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, TRUE);
1546     offset += 2;
1547
1548     /* zero */
1549     proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
1550     offset += 2;
1551
1552     /* zero */
1553     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1554     offset += 2;
1555
1556     return offset;
1557 }
1558
1559 static int
1560 dissect_usb_setup_set_configuration_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1561 {
1562     return offset;
1563 }
1564
1565
1566 /*
1567  * These dissectors are used to dissect the setup part and the data
1568  * for URB_CONTROL_INPUT / SET FEATURE
1569  */
1570
1571
1572 /* 9.4.9 */
1573 static int
1574 dissect_usb_setup_set_feature_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1575 {
1576     /* feature selector */
1577     proto_tree_add_item(tree, hf_usb_wFeatureSelector, tvb, offset, 2, TRUE);
1578     offset += 2;
1579
1580     /* zero/interface/endpoint or test selector */
1581     /* XXX - check based on request type */
1582     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
1583     offset += 2;
1584
1585     /* zero */
1586     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1587     offset += 2;
1588
1589     return offset;
1590 }
1591
1592 static int
1593 dissect_usb_setup_set_feature_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1594 {
1595     return offset;
1596 }
1597
1598
1599 /*
1600  * These dissectors are used to dissect the setup part and the data
1601  * for URB_CONTROL_INPUT / SET INTERFACE
1602  */
1603
1604
1605 /* 9.4.10 */
1606 static int
1607 dissect_usb_setup_set_interface_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1608 {
1609     /* alternate setting */
1610     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, TRUE);
1611     offset += 2;
1612
1613     /* interface */
1614     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
1615     offset += 2;
1616
1617     /* zero */
1618     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1619     offset += 2;
1620
1621     return offset;
1622 }
1623
1624 static int
1625 dissect_usb_setup_set_interface_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1626 {
1627     return offset;
1628 }
1629
1630
1631 /*
1632  * These dissectors are used to dissect the setup part and the data
1633  * for URB_CONTROL_INPUT / SYNCH FRAME
1634  */
1635
1636
1637 /* 9.4.11 */
1638 static int
1639 dissect_usb_setup_synch_frame_request(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1640 {
1641     /* zero */
1642     proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
1643     offset += 2;
1644
1645     /* endpoint */
1646     /* XXX */
1647     proto_tree_add_item(tree, hf_usb_wInterface, tvb, offset, 2, TRUE);
1648     offset += 2;
1649
1650     /* two */
1651     proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1652     offset += 2;
1653
1654     return offset;
1655 }
1656
1657 static int
1658 dissect_usb_setup_synch_frame_response(packet_info *pinfo _U_, proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
1659 {
1660     /* frame number */
1661     proto_tree_add_item(tree, hf_usb_wFrameNumber, tvb, offset, 2, TRUE);
1662     offset += 2;
1663
1664     return offset;
1665 }
1666
1667
1668 typedef int (*usb_setup_dissector)(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info);
1669
1670 typedef struct _usb_setup_dissector_table_t {
1671     guint8 request;
1672     usb_setup_dissector dissector;
1673 } usb_setup_dissector_table_t;
1674 #define USB_SETUP_GET_STATUS            0
1675 #define USB_SETUP_CLEAR_FEATURE         1
1676 #define USB_SETUP_SET_FEATURE           2
1677 #define USB_SETUP_SET_ADDRESS           5
1678 #define USB_SETUP_GET_DESCRIPTOR        6
1679 #define USB_SETUP_SET_DESCRIPTOR        7
1680 #define USB_SETUP_GET_CONFIGURATION     8
1681 #define USB_SETUP_SET_CONFIGURATION     9
1682 #define USB_SETUP_GET_INTERFACE         10
1683 #define USB_SETUP_SET_INTERFACE         11
1684 #define USB_SETUP_SYNCH_FRAME           12
1685
1686 static const usb_setup_dissector_table_t setup_request_dissectors[] = {
1687     {USB_SETUP_GET_STATUS,      dissect_usb_setup_get_status_request},
1688     {USB_SETUP_CLEAR_FEATURE,   dissect_usb_setup_clear_feature_request},
1689     {USB_SETUP_SET_FEATURE,     dissect_usb_setup_set_feature_request},
1690     {USB_SETUP_SET_ADDRESS,     dissect_usb_setup_set_address_request},
1691     {USB_SETUP_GET_DESCRIPTOR,  dissect_usb_setup_get_descriptor_request},
1692     {USB_SETUP_SET_CONFIGURATION, dissect_usb_setup_set_configuration_request},
1693     {USB_SETUP_GET_INTERFACE,   dissect_usb_setup_get_interface_request},
1694     {USB_SETUP_SET_INTERFACE,   dissect_usb_setup_set_interface_request},
1695     {USB_SETUP_SYNCH_FRAME,     dissect_usb_setup_synch_frame_request},
1696     {0, NULL}
1697 };
1698
1699 static const usb_setup_dissector_table_t setup_response_dissectors[] = {
1700     {USB_SETUP_GET_STATUS,      dissect_usb_setup_get_status_response},
1701     {USB_SETUP_CLEAR_FEATURE,   dissect_usb_setup_clear_feature_response},
1702     {USB_SETUP_SET_FEATURE,     dissect_usb_setup_set_feature_response},
1703     {USB_SETUP_SET_ADDRESS,     dissect_usb_setup_set_address_response},
1704     {USB_SETUP_GET_DESCRIPTOR,  dissect_usb_setup_get_descriptor_response},
1705     {USB_SETUP_GET_CONFIGURATION, dissect_usb_setup_get_configuration_response},
1706     {USB_SETUP_SET_CONFIGURATION, dissect_usb_setup_set_configuration_response},
1707     {USB_SETUP_GET_INTERFACE,   dissect_usb_setup_get_interface_response},
1708     {USB_SETUP_SET_INTERFACE,   dissect_usb_setup_set_interface_response},
1709     {USB_SETUP_SYNCH_FRAME,     dissect_usb_setup_synch_frame_response},
1710     {0, NULL}
1711 };
1712
1713 /* bRequest values but only when bmRequestType.type == 0 (Device) */
1714 static const value_string setup_request_names_vals[] = {
1715     {USB_SETUP_GET_STATUS,              "GET STATUS"},
1716     {USB_SETUP_CLEAR_FEATURE,           "CLEAR FEATURE"},
1717     {USB_SETUP_SET_FEATURE,             "SET FEATURE"},
1718     {USB_SETUP_SET_ADDRESS,             "SET ADDRESS"},
1719     {USB_SETUP_GET_DESCRIPTOR,          "GET DESCRIPTOR"},
1720     {USB_SETUP_SET_DESCRIPTOR,          "SET DESCRIPTOR"},
1721     {USB_SETUP_GET_CONFIGURATION,       "GET CONFIGURATION"},
1722     {USB_SETUP_SET_CONFIGURATION,       "SET CONFIGURATION"},
1723     {USB_SETUP_GET_INTERFACE,           "GET INTERFACE"},
1724     {USB_SETUP_SET_INTERFACE,           "SET INTERFACE"},
1725     {USB_SETUP_SYNCH_FRAME,             "SYNCH FRAME"},
1726     {0, NULL}
1727 };
1728
1729
1730 static const true_false_string tfs_bmrequesttype_direction = {
1731     "Device-to-host",
1732     "Host-to-device"
1733 };
1734
1735 static const value_string bmrequesttype_type_vals[] = {
1736     {RQT_SETUP_TYPE_STANDARD, "Standard"},
1737     {RQT_SETUP_TYPE_CLASS,    "Class"},
1738     {RQT_SETUP_TYPE_VENDOR,   "Vendor"},
1739     {0, NULL}
1740 };
1741
1742 static const value_string bmrequesttype_recipient_vals[] = {
1743     {RQT_SETUP_RECIPIENT_DEVICE,    "Device" },
1744     {RQT_SETUP_RECIPIENT_INTERFACE, "Interface" },
1745     {RQT_SETUP_RECIPIENT_INTERFACE, "Endpoint" },
1746     {RQT_SETUP_RECIPIENT_INTERFACE, "Other" },
1747     {0, NULL }
1748 };
1749
1750 static int
1751 dissect_usb_bmrequesttype(proto_tree *parent_tree, tvbuff_t *tvb, int offset,
1752     int *type)
1753 {
1754     proto_item *item=NULL;
1755     proto_tree *tree=NULL;
1756
1757     if(parent_tree){
1758         item=proto_tree_add_item(parent_tree, hf_usb_bmRequestType, tvb, offset, 1, TRUE);
1759         tree = proto_item_add_subtree(item, ett_usb_setup_bmrequesttype);
1760     }
1761
1762     *type = USB_TYPE(tvb_get_guint8(tvb, offset));
1763     proto_tree_add_item(tree, hf_usb_bmRequestType_direction, tvb, offset, 1, TRUE);
1764     proto_tree_add_item(tree, hf_usb_bmRequestType_type, tvb, offset, 1, TRUE);
1765     proto_tree_add_item(tree, hf_usb_bmRequestType_recipient, tvb, offset, 1, TRUE);
1766
1767     return ++offset;
1768 }
1769
1770 /* Adds the Linux USB pseudo header fields to the tree.
1771  * NOTE: The multi-byte fields in this header, and the pseudo-header
1772  *       extension, are in host-endian format so we can't
1773  *       use proto_tree_add_item() nor the tvb_get_xyz() routines and is
1774  *       the reason for the tvb_memcpy() and proto_tree_add_uint[64]()
1775  *       pairs below. */
1776 static void
1777 dissect_linux_usb_pseudo_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1778 {
1779     guint8 transfer_type;
1780     guint8 endpoint_number;
1781     guint8 transfer_type_and_direction;
1782     const gchar* val_str;
1783     guint8 type, flag;
1784     guint16 val16;
1785     guint32 val32;
1786     guint64 val64;
1787
1788     tvb_memcpy(tvb, (guint8 *)&val64, 0, 8);
1789     proto_tree_add_uint64(tree, hf_usb_urb_id, tvb, 0, 8, val64);
1790
1791     /* show the event type of this URB as string and as a character */
1792     type = tvb_get_guint8(tvb, 8);
1793     val_str = val_to_str(type, usb_urb_type_vals, "Unknown %d");
1794     proto_tree_add_string_format_value(tree, hf_usb_urb_type, tvb, 8, 1,
1795         &type, "%s ('%c')", val_str, isprint(type) ? type : '.');
1796
1797     proto_tree_add_item(tree, hf_usb_transfer_type, tvb, 9, 1, FALSE);
1798
1799     if (check_col(pinfo->cinfo, COL_INFO)) {
1800         transfer_type = tvb_get_guint8(tvb, 9);
1801         endpoint_number = tvb_get_guint8(tvb, 10);
1802         transfer_type_and_direction = (transfer_type & 0x7F) | (endpoint_number & 0x80);
1803         col_append_str(pinfo->cinfo, COL_INFO,
1804                        val_to_str(transfer_type_and_direction, usb_transfer_type_and_direction_vals, "Unknown type %x"));
1805     }
1806
1807     proto_tree_add_bitmask(tree, tvb, 10, hf_usb_endpoint_number, ett_usb_endpoint, usb_endpoint_fields, FALSE);
1808     proto_tree_add_item(tree, hf_usb_device_address, tvb, 11, 1, FALSE);
1809
1810     tvb_memcpy(tvb, (guint8 *)&val16, 12, 2);
1811     proto_tree_add_uint(tree, hf_usb_bus_id, tvb, 12, 2, val16);
1812
1813     /* Right after the pseudo header we always have
1814      * sizeof(struct usb_device_setup_hdr) bytes. The content of these
1815      * bytes only have meaning in case setup_flag == 0.
1816      */
1817     flag = tvb_get_guint8(tvb, 14);
1818     if (flag == 0) {
1819         proto_tree_add_string(tree, hf_usb_setup_flag, tvb, 14, 1, "relevant (0)");
1820     } else {
1821         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
1822             14, 1, &flag, "not relevant ('%c')", isprint(flag) ? flag: '.');
1823     }
1824
1825     flag = tvb_get_guint8(tvb, 15);
1826     if (flag == 0) {
1827         proto_tree_add_string(tree, hf_usb_data_flag, tvb, 15, 1, "present (0)");
1828     } else {
1829         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
1830             15, 1, &flag, "not present ('%c')", isprint(flag) ? flag : '.');
1831     }
1832
1833     tvb_memcpy(tvb, (guint8 *)&val64, 16, 8);
1834     proto_tree_add_uint64(tree, hf_usb_urb_ts_sec, tvb, 16, 8, val64);
1835
1836     tvb_memcpy(tvb, (guint8 *)&val32, 24, 4);
1837     proto_tree_add_uint(tree, hf_usb_urb_ts_usec, tvb, 24, 4, val32);
1838
1839     tvb_memcpy(tvb, (guint8 *)&val32, 28, 4);
1840     proto_tree_add_int(tree, hf_usb_urb_status, tvb, 28, 4, val32);
1841
1842     tvb_memcpy(tvb, (guint8 *)&val32, 32, 4);
1843     proto_tree_add_uint(tree, hf_usb_urb_len, tvb, 32, 4, val32);
1844
1845     tvb_memcpy(tvb, (guint8 *)&val32, 36, 4);
1846     proto_tree_add_uint(tree, hf_usb_data_len, tvb, 36, 4, val32);
1847 }
1848
1849 /*
1850  * XXX - put these into the protocol tree as appropriate.
1851  */
1852 static int
1853 dissect_linux_usb_pseudo_header_ext(tvbuff_t *tvb, int offset,
1854                                     packet_info *pinfo _U_,
1855                                     proto_tree *tree _U_)
1856 {
1857     guint32 ndesc;
1858
1859     offset += 4;        /* interval */
1860     offset += 4;        /* start_frame */
1861     offset += 4;        /* copy of URB's transfer flags */
1862
1863     tvb_memcpy(tvb, (guint8 *)&ndesc, offset, 4);
1864     offset += 4;
1865
1866     /*
1867      * Isochronous descriptors.  Each one is 16 bytes long.
1868      */
1869     offset += ndesc*16;
1870
1871     return offset;
1872 }
1873
1874 static void
1875 dissect_linux_usb_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent,
1876                          gboolean header_len_64_bytes)
1877 {
1878     unsigned int offset = 0;
1879     int type, endpoint;
1880     guint8 setup_flag;
1881     proto_tree *tree = NULL;
1882     guint32 src_device, dst_device, tmp_addr;
1883     static usb_address_t src_addr, dst_addr; /* has to be static due to SET_ADDRESS */
1884     guint32 src_endpoint, dst_endpoint;
1885     gboolean is_request=FALSE;
1886     usb_conv_info_t *usb_conv_info=NULL;
1887     usb_trans_info_t *usb_trans_info=NULL;
1888     conversation_t *conversation;
1889     usb_tap_data_t *tap_data=NULL;
1890
1891     col_set_str(pinfo->cinfo, COL_PROTOCOL, "USB");
1892
1893     /* add usb hdr*/
1894     if (parent) {
1895       proto_item *ti = NULL;
1896       ti = proto_tree_add_protocol_format(parent, proto_usb, tvb, 0,
1897           header_len_64_bytes ? 64 : 48, "USB URB");
1898       tree = proto_item_add_subtree(ti, usb_hdr);
1899     }
1900
1901     dissect_linux_usb_pseudo_header(tvb, pinfo, tree);
1902     is_request = (tvb_get_guint8(tvb, 8) == URB_SUBMIT) ? TRUE : FALSE;
1903     type = tvb_get_guint8(tvb, 9);
1904     endpoint = tvb_get_guint8(tvb, 10) & (~URB_TRANSFER_IN);
1905     tmp_addr = tvb_get_guint8(tvb, 11);
1906     setup_flag = tvb_get_guint8(tvb, 14);
1907     offset += 40; /* skip first part of the pseudo-header */
1908
1909     /* Set up addresses and ports. */
1910     if (is_request) {
1911         src_addr.device = src_device = 0xffffffff;
1912         src_addr.endpoint = src_endpoint = NO_ENDPOINT;
1913         dst_addr.device = dst_device = htolel(tmp_addr);
1914         dst_addr.endpoint = dst_endpoint = htolel(endpoint);
1915     } else {
1916         src_addr.device = src_device = htolel(tmp_addr);
1917         src_addr.endpoint = src_endpoint = htolel(endpoint);
1918         dst_addr.device = dst_device = 0xffffffff;
1919         dst_addr.endpoint = dst_endpoint = NO_ENDPOINT;
1920     }
1921
1922     SET_ADDRESS(&pinfo->net_src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1923     SET_ADDRESS(&pinfo->src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1924     SET_ADDRESS(&pinfo->net_dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1925     SET_ADDRESS(&pinfo->dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1926     pinfo->ptype=PT_USB;
1927     pinfo->srcport=src_endpoint;
1928     pinfo->destport=dst_endpoint;
1929
1930     conversation=get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, pinfo->srcport, pinfo->destport);
1931
1932     usb_conv_info=get_usb_conv_info(conversation);
1933     pinfo->usb_conv_info=usb_conv_info;
1934
1935
1936     /* request/response matching so we can keep track of transaction specific
1937      * data.
1938      */
1939     if(is_request){
1940         /* this is a request */
1941         usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1942         if(!usb_trans_info){
1943             usb_trans_info=se_alloc0(sizeof(usb_trans_info_t));
1944             usb_trans_info->request_in=pinfo->fd->num;
1945             usb_trans_info->req_time=pinfo->fd->abs_ts;
1946             se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1947         }
1948         usb_conv_info->usb_trans_info=usb_trans_info;
1949
1950         if(usb_trans_info && usb_trans_info->response_in){
1951             proto_item *ti;
1952
1953             ti=proto_tree_add_uint(tree, hf_usb_response_in, tvb, 0, 0, usb_trans_info->response_in);
1954             PROTO_ITEM_SET_GENERATED(ti);
1955         }
1956     } else {
1957         /* this is a response */
1958         if(pinfo->fd->flags.visited){
1959             usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1960         } else {
1961             usb_trans_info=se_tree_lookup32_le(usb_conv_info->transactions, pinfo->fd->num);
1962             if(usb_trans_info){
1963                 usb_trans_info->response_in=pinfo->fd->num;
1964                 se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1965             }
1966         }
1967         usb_conv_info->usb_trans_info=usb_trans_info;
1968
1969         if(usb_trans_info && usb_trans_info->request_in){
1970             proto_item *ti;
1971             nstime_t t, deltat;
1972
1973             ti=proto_tree_add_uint(tree, hf_usb_request_in, tvb, 0, 0, usb_trans_info->request_in);
1974             PROTO_ITEM_SET_GENERATED(ti);
1975
1976             t = pinfo->fd->abs_ts;
1977             nstime_delta(&deltat, &t, &usb_trans_info->req_time);
1978             ti=proto_tree_add_time(tree, hf_usb_time, tvb, 0, 0, &deltat);
1979             PROTO_ITEM_SET_GENERATED(ti);
1980         }
1981     }
1982
1983     tap_data=ep_alloc(sizeof(usb_tap_data_t));
1984     tap_data->urb_type=tvb_get_guint8(tvb, 8);
1985     tap_data->transfer_type=(guint8)type;
1986     tap_data->conv_info=usb_conv_info;
1987     tap_data->trans_info=usb_trans_info;
1988     tap_queue_packet(usb_tap, pinfo, tap_data);
1989
1990     switch(type){
1991     case URB_BULK:
1992         {
1993         proto_item *item;
1994
1995         item=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, 0, 0, usb_conv_info->interfaceClass);
1996         PROTO_ITEM_SET_GENERATED(item);
1997
1998         /* Skip setup/isochronous header - it's not applicable */
1999         offset += 8;
2000
2001         /*
2002          * If this has a 64-byte header, process the extra 16 bytes of
2003          * pseudo-header information.
2004          */
2005         if (header_len_64_bytes)
2006             offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, tree);
2007
2008         if(tvb_reported_length_remaining(tvb, offset)){
2009             tvbuff_t *next_tvb;
2010
2011             pinfo->usb_conv_info=usb_conv_info;
2012             next_tvb=tvb_new_subset_remaining(tvb, offset);
2013             if(dissector_try_port(usb_bulk_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, parent)){
2014                 return;
2015             }
2016         }
2017         }
2018         break;
2019     case URB_CONTROL:
2020         {
2021         const usb_setup_dissector_table_t *tmp;
2022         usb_setup_dissector dissector;
2023         proto_item *ti = NULL;
2024         proto_tree *setup_tree = NULL;
2025         int type_2;
2026
2027         ti=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, offset, 0, usb_conv_info->interfaceClass);
2028         PROTO_ITEM_SET_GENERATED(ti);
2029
2030         if(is_request){
2031             if (setup_flag == 0) {
2032                 tvbuff_t *next_tvb;
2033
2034                 /* this is a request */
2035
2036                 /* Dissect the setup header - it's applicable */
2037
2038                 ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, 8, "URB setup");
2039                 setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
2040                 usb_trans_info->requesttype=tvb_get_guint8(tvb, offset);
2041                 offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type_2);
2042
2043
2044                 /* read the request code and spawn off to a class specific
2045                  * dissector if found
2046                  */
2047                 usb_trans_info->request=tvb_get_guint8(tvb, offset);
2048
2049                 switch (type_2) {
2050
2051                 case RQT_SETUP_TYPE_STANDARD:
2052                     /*
2053                      * This is a standard request which is managed by this
2054                      * dissector
2055                      */
2056                     proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
2057                     offset++;
2058
2059                     if (check_col(pinfo->cinfo, COL_INFO)) {
2060                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s Request",
2061                              val_to_str(usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
2062                     }
2063
2064                     dissector=NULL;
2065                     for(tmp=setup_request_dissectors;tmp->dissector;tmp++){
2066                         if(tmp->request==usb_trans_info->request){
2067                             dissector=tmp->dissector;
2068                             break;
2069                         }
2070                     }
2071
2072                     if(dissector){
2073                         offset=dissector(pinfo, setup_tree, tvb, offset, usb_trans_info, usb_conv_info);
2074                     } else {
2075                         proto_tree_add_item(setup_tree, hf_usb_value, tvb, offset, 2, TRUE);
2076                         offset += 2;
2077                         proto_tree_add_item(setup_tree, hf_usb_index, tvb, offset, 2, TRUE);
2078                         offset += 2;
2079                         proto_tree_add_item(setup_tree, hf_usb_length, tvb, offset, 2, TRUE);
2080                         offset += 2;
2081                     }
2082                     break;
2083
2084                 case RQT_SETUP_TYPE_CLASS:
2085                     /* Try to find a class specific dissector */
2086                     next_tvb=tvb_new_subset_remaining(tvb, offset);
2087                     if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
2088                         return;
2089                     }
2090                     /* Else no class dissector, just display generic fields */
2091                     proto_tree_add_item(setup_tree, hf_usb_request_unknown_class, tvb, offset, 1, TRUE);
2092                     offset++;
2093                     proto_tree_add_item(setup_tree, hf_usb_value, tvb, offset, 2, TRUE);
2094                     offset += 2;
2095                     proto_tree_add_item(setup_tree, hf_usb_index, tvb, offset, 2, TRUE);
2096                     offset += 2;
2097                     proto_tree_add_item(setup_tree, hf_usb_length, tvb, offset, 2, TRUE);
2098                     offset += 2;
2099                     break;
2100
2101                 default:
2102                     proto_tree_add_item(setup_tree, hf_usb_request_unknown_class, tvb, offset, 1, TRUE);
2103                     offset++;
2104                     proto_tree_add_item(setup_tree, hf_usb_value, tvb, offset, 2, TRUE);
2105                     offset += 2;
2106                     proto_tree_add_item(setup_tree, hf_usb_index, tvb, offset, 2, TRUE);
2107                     offset += 2;
2108                     proto_tree_add_item(setup_tree, hf_usb_length, tvb, offset, 2, TRUE);
2109                     offset += 2;
2110                 }
2111             } else {
2112                 /* Skip setup/isochronous header - it's not applicable */
2113                 offset += 8;
2114             }
2115
2116             /*
2117              * If this has a 64-byte header, process the extra 16 bytes of
2118              * pseudo-header information.
2119              */
2120             if (header_len_64_bytes)
2121                 offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, tree);
2122         } else {
2123             tvbuff_t *next_tvb;
2124
2125             /* this is a response */
2126
2127             /* Skip setup header - it's never applicable for responses */
2128             offset += 8;
2129
2130             /*
2131              * If this has a 64-byte header, process the extra 16 bytes of
2132              * pseudo-header information.
2133              */
2134             if (header_len_64_bytes)
2135                 offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, tree);
2136
2137             if(usb_trans_info){
2138                 /* Try to find a class specific dissector */
2139                 next_tvb=tvb_new_subset_remaining(tvb, offset);
2140                 if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
2141                     return;
2142                 }
2143
2144                 type_2 = USB_TYPE(usb_trans_info->requesttype);
2145                 switch (type_2) {
2146
2147                 case RQT_SETUP_TYPE_STANDARD:
2148                     /*
2149                      * This is a standard response which is managed by this
2150                      * dissector
2151                      */
2152                     if (check_col(pinfo->cinfo, COL_INFO)) {
2153                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s Response",
2154                             val_to_str(usb_conv_info->usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
2155                     }
2156
2157                     dissector=NULL;
2158                     for(tmp=setup_response_dissectors;tmp->dissector;tmp++){
2159                         if(tmp->request==usb_conv_info->usb_trans_info->request){
2160                             dissector=tmp->dissector;
2161                             break;
2162                         }
2163                     }
2164
2165                     if(dissector){
2166                         offset = dissector(pinfo, tree, tvb, offset, usb_conv_info->usb_trans_info, usb_conv_info);
2167                     } else {
2168                         if (tvb_reported_length_remaining(tvb, offset) != 0) {
2169                             proto_tree_add_text(tree, tvb, offset, -1, "CONTROL response data");
2170                             offset += tvb_length_remaining(tvb, offset);
2171                         }
2172                     }
2173                     break;
2174                 default:
2175                     if (tvb_reported_length_remaining(tvb, offset) != 0) {
2176                         proto_tree_add_text(tree, tvb, offset, -1, "CONTROL response data");
2177                         offset += tvb_length_remaining(tvb, offset);
2178                     }
2179                     break;
2180                 }
2181             } else {
2182                 /* no matching request available */
2183                 if (tvb_reported_length_remaining(tvb, offset) != 0) {
2184                     proto_tree_add_text(tree, tvb, offset, -1, "CONTROL response data");
2185                     offset += tvb_length_remaining(tvb, offset);
2186                 }
2187             }
2188         }
2189         }
2190         break;
2191     case URB_ISOCHRONOUS:
2192         {
2193         /* All fields which belong to Linux usbmon headers are in host-endian
2194          * byte order. The fields coming from the USB communication are in little
2195          * endian format (see usb_20.pdf, chapter 8.1 Byte/Bit ordering).
2196          *
2197          * When a capture file is transfered to a host with different endianness
2198          * than packet was captured then the necessary swapping happens in
2199          * wiretap/pcap-common.c, pcap_process_linux_usb_pseudoheader().
2200          */
2201         guint32 iso_numdesc = 0;
2202
2203         if (setup_flag == 0) {
2204             proto_item *ti = NULL;
2205             proto_tree *setup_tree = NULL;
2206             int type_2;
2207
2208             /* Dissect the setup header - it's applicable */
2209
2210             ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, 8, "URB setup");
2211             setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
2212
2213             offset = dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type_2);
2214             proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
2215             offset++;
2216             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
2217             offset += 2;
2218             proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
2219             offset += 2;
2220             proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
2221             offset += 2;
2222         } else {
2223
2224             /* Process ISO related fields (usbmon_packet.iso). The fields are
2225              * in host endian byte order so use tvb_memcopy() and
2226              * proto_tree_add_uint() pair.
2227              */
2228             guint32 val32;
2229
2230             tvb_memcpy(tvb, (guint8 *)&val32, offset, 4);
2231             proto_tree_add_uint(tree, hf_usb_iso_error_count, tvb, offset, 4, val32);
2232             offset += 4;
2233
2234             tvb_memcpy(tvb, (guint8 *)&iso_numdesc, offset, 4);
2235             proto_tree_add_uint(tree, hf_usb_iso_numdesc, tvb, offset, 4, iso_numdesc);
2236             offset += 4;
2237         }
2238
2239         /*
2240          * If this has a 64-byte header, process the extra 16 bytes of
2241          * pseudo-header information.
2242          */
2243         if (header_len_64_bytes) {
2244             guint32 ndesc;
2245
2246             offset += 4;        /* interval */
2247             offset += 4;        /* start_frame */
2248             offset += 4;        /* copy of URB's transfer flags */
2249
2250             tvb_memcpy(tvb, (guint8 *)&ndesc, offset, 4);
2251             offset += 4;
2252
2253         }
2254
2255         if (setup_flag != 0) {
2256             proto_tree *urb_tree = NULL;
2257             guint32 i;
2258             unsigned int data_base;
2259             guint32 iso_status;
2260             guint32 iso_off;
2261             guint32 iso_len;
2262             guint32 iso_pad;
2263
2264             data_base = offset + iso_numdesc * 16;
2265             urb_tree = tree;
2266             for (i = 0; i != iso_numdesc; i++) {
2267                 if (parent) {
2268                     proto_item *ti = NULL;
2269                     ti = proto_tree_add_protocol_format(urb_tree, proto_usb, tvb, offset,
2270                          16, "USB isodesc %u", i);
2271                     tree = proto_item_add_subtree(ti, usb_isodesc);
2272                 }
2273
2274                 /* Add ISO descriptor fields which are stored in host
2275                  * endian byte order so use tvb_memcopy() and
2276                  * proto_tree_add_uint()/proto_tree_add_int() pair.
2277                  */
2278                 tvb_memcpy(tvb, (guint8 *)&iso_status, offset, 4);
2279                 proto_tree_add_int(tree, hf_usb_iso_status, tvb, offset, 4, iso_status);
2280                 offset += 4;
2281
2282                 tvb_memcpy(tvb, (guint8 *)&iso_off, offset, 4);
2283                 proto_tree_add_uint(tree, hf_usb_iso_off, tvb, offset, 4, iso_off);
2284                 offset += 4;
2285
2286                 tvb_memcpy(tvb, (guint8 *)&iso_len, offset, 4);
2287                 proto_tree_add_uint(tree, hf_usb_iso_len, tvb, offset, 4, iso_len);
2288                 offset += 4;
2289
2290                 /* When the ISO status is OK and there is ISO data and this ISO data is
2291                  * fully captured then show this data.
2292                  */
2293                 if (!iso_status && iso_len && data_base + iso_off + iso_len <= tvb_length(tvb))
2294                     proto_tree_add_item(tree, hf_usb_iso_data, tvb, data_base + iso_off, iso_len, TRUE);
2295
2296                 tvb_memcpy(tvb, (guint8 *)&iso_pad, offset, 4);
2297                 proto_tree_add_uint(tree, hf_usb_iso_pad, tvb, offset, 4, iso_pad);
2298                 offset += 4;
2299             }
2300             tree = urb_tree;
2301         }
2302
2303         }
2304         break;
2305
2306     default:
2307         /* dont know */
2308         if (setup_flag == 0) {
2309             proto_item *ti = NULL;
2310             proto_tree *setup_tree = NULL;
2311             int type_2;
2312
2313             /* Dissect the setup header - it's applicable */
2314
2315             ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, 8, "URB setup");
2316             setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
2317
2318             offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type_2);
2319             proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
2320             offset++;
2321             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
2322             offset += 2;
2323             proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
2324             offset += 2;
2325             proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
2326             offset += 2;
2327         } else {
2328             /* Skip setup/isochronous header - it's not applicable */
2329             offset += 8;
2330         }
2331
2332         /*
2333          * If this has a 64-byte header, process the extra 16 bytes of
2334          * pseudo-header information.
2335          */
2336         if (header_len_64_bytes)
2337             offset = dissect_linux_usb_pseudo_header_ext(tvb, offset, pinfo, tree);
2338
2339         break;
2340     }
2341
2342     if (tvb_reported_length_remaining(tvb, offset) != 0) {
2343         /* There is leftover capture data to add (padding?) */
2344         proto_tree_add_item(tree, hf_usb_capdata, tvb, offset, -1, FALSE);
2345     }
2346 }
2347
2348 static void
2349 dissect_linux_usb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent)
2350 {
2351     dissect_linux_usb_common(tvb, pinfo, parent, FALSE);
2352 }
2353
2354 static void
2355 dissect_linux_usb_mmapped(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent)
2356 {
2357     dissect_linux_usb_common(tvb, pinfo, parent, TRUE);
2358 }
2359
2360 void
2361 proto_register_usb(void)
2362 {
2363     static hf_register_info hf[] = {
2364
2365     /* USB packet pseudoheader members */
2366         { &hf_usb_urb_id,
2367         { "URB id", "usb.urb_id", FT_UINT64, BASE_HEX,
2368                 NULL, 0x0,
2369                 NULL, HFILL }},
2370
2371         { &hf_usb_urb_type,
2372         { "URB type", "usb.urb_type", FT_STRING, BASE_NONE,
2373                 NULL, 0x0,
2374                 NULL, HFILL }},
2375
2376         { &hf_usb_transfer_type,
2377         { "URB transfer type", "usb.transfer_type", FT_UINT8, BASE_HEX,
2378                 VALS(usb_transfer_type_vals), 0x0,
2379                 NULL, HFILL }},
2380
2381         { &hf_usb_endpoint_number,
2382         { "Endpoint", "usb.endpoint_number", FT_UINT8, BASE_HEX, NULL, 0x0,
2383                 "USB endpoint number", HFILL }},
2384
2385         { &hf_usb_endpoint_direction,
2386         { "Direction", "usb.endpoint_number.direction", FT_UINT8, BASE_DEC,
2387                 VALS(usb_endpoint_direction_vals), 0x80,
2388                 "USB endpoint direction", HFILL }},
2389
2390         { &hf_usb_endpoint_number_value,
2391         { "Endpoint value", "usb.endpoint_number.endpoint", FT_UINT8, BASE_DEC,
2392                 NULL, 0x7F,
2393                 "USB endpoint value", HFILL }},
2394
2395         { &hf_usb_device_address,
2396         { "Device", "usb.device_address", FT_UINT8, BASE_DEC, NULL, 0x0,
2397                 "USB device address", HFILL }},
2398
2399         { &hf_usb_bus_id,
2400         { "URB bus id", "usb.bus_id", FT_UINT16, BASE_DEC,
2401                 NULL, 0x0,
2402                 NULL, HFILL }},
2403
2404         { &hf_usb_setup_flag,
2405         { "Device setup request", "usb.setup_flag", FT_STRING, BASE_NONE,
2406                  NULL, 0x0,
2407                  "USB device setup request is relevant (0) or not", HFILL }},
2408
2409         { &hf_usb_data_flag,
2410         { "Data", "usb.data_flag", FT_STRING, BASE_NONE,
2411                  NULL, 0x0,
2412                  "USB data is present (0) or not", HFILL }},
2413
2414         { &hf_usb_urb_ts_sec,
2415         { "URB sec", "usb.urb_ts_sec", FT_UINT64, BASE_DEC,
2416                 NULL, 0x0,
2417                 NULL, HFILL }},
2418
2419         { &hf_usb_urb_ts_usec,
2420         { "URB usec", "usb.urb_ts_usec", FT_UINT32, BASE_DEC,
2421                 NULL, 0x0,
2422                 NULL, HFILL }},
2423
2424         { &hf_usb_urb_status,
2425         { "URB status", "usb.urb_status", FT_INT32, BASE_DEC,
2426                 VALS(usb_urb_status_vals), 0x0,
2427                 NULL, HFILL }},
2428
2429         { &hf_usb_urb_len,
2430         { "URB length [bytes]", "usb.urb_len", FT_UINT32, BASE_DEC, NULL, 0x0,
2431                 "URB length in bytes", HFILL }},
2432
2433         { &hf_usb_data_len,
2434         { "Data length [bytes]", "usb.data_len", FT_UINT32, BASE_DEC, NULL, 0x0,
2435                 "URB data length in bytes", HFILL }},
2436
2437     /* Generated values */
2438         { &hf_usb_src_endpoint_number,
2439         { "Src Endpoint", "usb.src.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
2440                 "Source USB endpoint number", HFILL }},
2441
2442         { &hf_usb_dst_endpoint_number,
2443         { "Dst Endpoint", "usb.dst.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
2444                 "Destination USB endpoint number", HFILL }},
2445
2446     /* Fields from usb20.pdf, Table 9-2 'Format of Setup Data' */
2447         { &hf_usb_bmRequestType,
2448         { "bmRequestType", "usb.bmRequestType", FT_UINT8, BASE_HEX, NULL, 0x0,
2449                 NULL, HFILL }},
2450
2451         { &hf_usb_request,
2452         { "bRequest", "usb.setup.bRequest", FT_UINT8, BASE_DEC, VALS(setup_request_names_vals), 0x0,
2453                 NULL, HFILL }},
2454
2455         /* Same as hf_usb_request but no descriptive text */
2456         { &hf_usb_request_unknown_class,
2457         { "bRequest", "usb.setup.bRequest", FT_UINT8, BASE_DEC, NULL, 0x0,
2458                 NULL, HFILL }},
2459
2460         { &hf_usb_value,
2461         { "wValue", "usb.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
2462                 NULL, HFILL }},
2463
2464         { &hf_usb_index,
2465         { "wIndex", "usb.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
2466                 NULL, HFILL }},
2467
2468         { &hf_usb_length,
2469         { "wLength", "usb.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
2470                 NULL, HFILL }},
2471
2472         { &hf_usb_wFeatureSelector,
2473         { "wFeatureSelector", "usb.setup.wFeatureSelector", FT_UINT16, BASE_DEC,
2474                 VALS(usb_feature_selector_vals), 0x0, NULL, HFILL }},
2475
2476         { &hf_usb_wInterface,
2477         { "wInterface", "usb.setup.wInterface", FT_UINT16, BASE_DEC,
2478                 NULL, 0x0, NULL, HFILL }},
2479
2480         { &hf_usb_wStatus,
2481         { "wStatus", "usb.setup.wStatus", FT_UINT16, BASE_HEX,
2482                 NULL, 0x0, NULL, HFILL }},
2483
2484         { &hf_usb_wFrameNumber,
2485         { "wFrameNumber", "usb.setup.wFrameNumber", FT_UINT16, BASE_DEC,
2486                 NULL, 0x0, NULL, HFILL }},
2487
2488     /* --------------------------------- */
2489         { &hf_usb_iso_error_count,                /* host endian byte order */
2490         { "ISO error count", "usb.iso.error_count", FT_UINT16, BASE_DEC, NULL, 0x0,
2491                 NULL, HFILL }},
2492
2493         { &hf_usb_iso_numdesc,                    /* host endian byte order */
2494         { "Number of ISO descriptors", "usb.iso.numdesc", FT_UINT16, BASE_DEC, NULL, 0x0,
2495                 NULL, HFILL }},
2496
2497     /* fields of struct mon_bin_isodesc from linux/drivers/usb/mon/mon_bin.c */
2498         { &hf_usb_iso_status,                     /* host endian byte order */
2499         { "Status", "usb.iso.iso_status", FT_INT32, BASE_DEC,
2500                 VALS(usb_urb_status_vals), 0x0,
2501                 "ISO descriptor status", HFILL }},
2502
2503         { &hf_usb_iso_off,                        /* host endian byte order */
2504         { "Offset [bytes]", "usb.iso.iso_off", FT_UINT32, BASE_DEC, NULL, 0x0,
2505                 "ISO data offset in bytes starting from the end of the last ISO descriptor", HFILL }},
2506
2507         { &hf_usb_iso_len,                        /* host endian byte order */
2508         { "Length [bytes]", "usb.iso.iso_len", FT_UINT32, BASE_DEC, NULL, 0x0,
2509                 "ISO data length in bytes", HFILL }},
2510
2511         { &hf_usb_iso_pad,                        /* host endian byte order */
2512         { "Padding", "usb.iso._pad", FT_UINT32, BASE_HEX, NULL, 0x0,
2513                 "Padding field of ISO descriptor structure", HFILL }},
2514
2515         { &hf_usb_iso_data,
2516         {"ISO Data", "usb.iso.data",
2517             FT_BYTES, BASE_NONE, NULL, 0x0,
2518             NULL, HFILL }},
2519     /* --------------------------------- */
2520         { &hf_usb_data,
2521         {"Application Data", "usb.data",
2522             FT_BYTES, BASE_NONE, NULL, 0x0,
2523             "Payload is application data", HFILL }},
2524
2525         { &hf_usb_capdata,
2526         {"Leftover Capture Data", "usb.capdata",
2527             FT_BYTES, BASE_NONE, NULL, 0x0,
2528             "Padding added by the USB capture system", HFILL }},
2529
2530         { &hf_usb_bmRequestType_direction,
2531         { "Direction", "usb.bmRequestType.direction", FT_BOOLEAN, 8,
2532           TFS(&tfs_bmrequesttype_direction), USB_DIR_IN, NULL, HFILL }},
2533
2534         { &hf_usb_bmRequestType_type,
2535         { "Type", "usb.bmRequestType.type", FT_UINT8, BASE_HEX,
2536           VALS(bmrequesttype_type_vals), USB_TYPE_MASK, NULL, HFILL }},
2537
2538         { &hf_usb_bmRequestType_recipient,
2539         { "Recipient", "usb.bmRequestType.recipient", FT_UINT8, BASE_HEX,
2540           VALS(bmrequesttype_recipient_vals), 0x1f, NULL, HFILL }},
2541
2542         { &hf_usb_bDescriptorType,
2543         { "bDescriptorType", "usb.bDescriptorType", FT_UINT8, BASE_DEC,
2544           VALS(descriptor_type_vals), 0x0, NULL, HFILL }},
2545
2546         { &hf_usb_descriptor_index,
2547         { "Descriptor Index", "usb.DescriptorIndex", FT_UINT8, BASE_HEX,
2548           NULL, 0x0, NULL, HFILL }},
2549
2550         { &hf_usb_language_id,
2551         { "Language Id", "usb.LanguageId", FT_UINT16, BASE_HEX|BASE_EXT_STRING,
2552           &usb_langid_vals_ext, 0x0, NULL, HFILL }},
2553
2554         { &hf_usb_bLength,
2555         { "bLength", "usb.bLength", FT_UINT8, BASE_DEC,
2556           NULL, 0x0, NULL, HFILL }},
2557
2558         { &hf_usb_bcdUSB,
2559         { "bcdUSB", "usb.bcdUSB", FT_UINT16, BASE_HEX,
2560           NULL, 0x0, NULL, HFILL }},
2561
2562         { &hf_usb_bDeviceClass,
2563         { "bDeviceClass", "usb.bDeviceClass", FT_UINT8, BASE_DEC,
2564           NULL, 0x0, NULL, HFILL }},
2565
2566         { &hf_usb_bDeviceSubClass,
2567         { "bDeviceSubClass", "usb.bDeviceSubClass", FT_UINT8, BASE_DEC,
2568           NULL, 0x0, NULL, HFILL }},
2569
2570         { &hf_usb_bDeviceProtocol,
2571         { "bDeviceProtocol", "usb.bDeviceProtocol", FT_UINT8, BASE_DEC,
2572           NULL, 0x0, NULL, HFILL }},
2573
2574         { &hf_usb_bMaxPacketSize0,
2575         { "bMaxPacketSize0", "usb.bMaxPacketSize0", FT_UINT8, BASE_DEC,
2576           NULL, 0x0, NULL, HFILL }},
2577
2578         { &hf_usb_idVendor,
2579         { "idVendor", "usb.idVendor", FT_UINT16, BASE_HEX,
2580           NULL, 0x0, NULL, HFILL }},
2581
2582         { &hf_usb_idProduct,
2583         { "idProduct", "usb.idProduct", FT_UINT16, BASE_HEX,
2584           NULL, 0x0, NULL, HFILL }},
2585
2586         { &hf_usb_bcdDevice,
2587         { "bcdDevice", "usb.bcdDevice", FT_UINT16, BASE_HEX,
2588           NULL, 0x0, NULL, HFILL }},
2589
2590         { &hf_usb_iManufacturer,
2591         { "iManufacturer", "usb.iManufacturer", FT_UINT8, BASE_DEC,
2592           NULL, 0x0, NULL, HFILL }},
2593
2594         { &hf_usb_iProduct,
2595         { "iProduct", "usb.iProduct", FT_UINT8, BASE_DEC,
2596           NULL, 0x0, NULL, HFILL }},
2597
2598         { &hf_usb_iSerialNumber,
2599         { "iSerialNumber", "usb.iSerialNumber", FT_UINT8, BASE_DEC,
2600           NULL, 0x0, NULL, HFILL }},
2601
2602         { &hf_usb_bNumConfigurations,
2603         { "bNumConfigurations", "usb.bNumConfigurations", FT_UINT8, BASE_DEC,
2604           NULL, 0x0, NULL, HFILL }},
2605
2606         { &hf_usb_wLANGID,
2607         { "wLANGID", "usb.wLANGID", FT_UINT16, BASE_HEX|BASE_EXT_STRING,
2608           &usb_langid_vals_ext, 0x0, NULL, HFILL }},
2609
2610         { &hf_usb_bString,
2611         { "bString", "usb.bString", FT_STRING, BASE_NONE,
2612           NULL, 0x0, NULL, HFILL }},
2613
2614         { &hf_usb_bInterfaceNumber,
2615         { "bInterfaceNumber", "usb.bInterfaceNumber", FT_UINT8, BASE_DEC,
2616           NULL, 0x0, NULL, HFILL }},
2617
2618         { &hf_usb_bAlternateSetting,
2619         { "bAlternateSetting","usb.bAlternateSetting", FT_UINT8, BASE_DEC,
2620           NULL, 0x0, NULL, HFILL }},
2621
2622         { &hf_usb_bNumEndpoints,
2623         { "bNumEndpoints","usb.bNumEndpoints", FT_UINT8, BASE_DEC,
2624           NULL, 0x0, NULL, HFILL }},
2625
2626         { &hf_usb_bInterfaceClass,
2627         { "bInterfaceClass", "usb.bInterfaceClass", FT_UINT8, BASE_HEX,
2628           VALS(usb_interfaceclass_vals), 0x0, NULL, HFILL }},
2629
2630         { &hf_usb_bInterfaceSubClass,
2631         { "bInterfaceSubClass", "usb.bInterfaceSubClass", FT_UINT8, BASE_HEX,
2632           NULL, 0x0, NULL, HFILL }},
2633
2634         { &hf_usb_bInterfaceProtocol,
2635         { "bInterfaceProtocol", "usb.bInterfaceProtocol", FT_UINT8, BASE_HEX,
2636           NULL, 0x0, NULL, HFILL }},
2637
2638         { &hf_usb_iInterface,
2639         { "iInterface", "usb.iInterface", FT_UINT8, BASE_DEC,
2640           NULL, 0x0, NULL, HFILL }},
2641
2642         { &hf_usb_bEndpointAddress,
2643         { "bEndpointAddress", "usb.bEndpointAddress", FT_UINT8, BASE_HEX,
2644           NULL, 0x0, NULL, HFILL }},
2645
2646         { &hf_usb_configuration_bmAttributes,
2647         { "Configuration bmAttributes", "usb.configuration.bmAttributes", FT_UINT8, BASE_HEX,
2648           NULL, 0x0, NULL, HFILL }},
2649
2650         { &hf_usb_bmAttributes,
2651         { "bmAttributes", "usb.bmAttributes", FT_UINT8, BASE_HEX,
2652           NULL, 0x0, NULL, HFILL }},
2653
2654         { &hf_usb_bEndpointAttributeTransfer,
2655         { "Transfertype", "usb.bmAttributes.transfer", FT_UINT8, BASE_HEX,
2656           VALS(usb_bmAttributes_transfer_vals), 0x03, NULL, HFILL }},
2657
2658         { &hf_usb_bEndpointAttributeSynchonisation,
2659         { "Synchronisationtype", "usb.bmAttributes.sync", FT_UINT8, BASE_HEX,
2660           VALS(usb_bmAttributes_sync_vals), 0x0c, NULL, HFILL }},
2661
2662         { &hf_usb_bEndpointAttributeBehaviour,
2663         { "Behaviourtype", "usb.bmAttributes.behaviour", FT_UINT8, BASE_HEX,
2664           VALS(usb_bmAttributes_behaviour_vals), 0x30, NULL, HFILL }},
2665
2666         { &hf_usb_wMaxPacketSize,
2667         { "wMaxPacketSize", "usb.wMaxPacketSize", FT_UINT16, BASE_DEC,
2668           NULL, 0x0, NULL, HFILL }},
2669
2670         { &hf_usb_bInterval,
2671         { "bInterval", "usb.bInterval", FT_UINT8, BASE_DEC,
2672           NULL, 0x0, NULL, HFILL }},
2673
2674         { &hf_usb_wTotalLength,
2675         { "wTotalLength", "usb.wTotalLength", FT_UINT16, BASE_DEC,
2676           NULL, 0x0, NULL, HFILL }},
2677
2678         { &hf_usb_bNumInterfaces,
2679         { "bNumInterfaces", "usb.bNumInterfaces", FT_UINT8, BASE_DEC,
2680           NULL, 0x0, NULL, HFILL }},
2681
2682         { &hf_usb_bConfigurationValue,
2683         { "bConfigurationValue", "usb.bConfigurationValue", FT_UINT8, BASE_DEC,
2684           NULL, 0x0, NULL, HFILL }},
2685
2686         { &hf_usb_iConfiguration,
2687         { "iConfiguration", "usb.iConfiguration", FT_UINT8, BASE_DEC,
2688           NULL, 0x0, NULL, HFILL }},
2689
2690         { &hf_usb_bMaxPower,
2691         { "bMaxPower", "usb.bMaxPower", FT_UINT8, BASE_DEC,
2692           NULL, 0x0, NULL, HFILL }},
2693
2694         { &hf_usb_configuration_legacy10buspowered,
2695         { "Must be 1", "usb.configuration.legacy10buspowered", FT_BOOLEAN, 8,
2696           TFS(&tfs_mustbeone), 0x80, "Legacy USB 1.0 bus powered", HFILL }},
2697
2698         { &hf_usb_configuration_selfpowered,
2699         { "Self-Powered", "usb.configuration.selfpowered", FT_BOOLEAN, 8,
2700           TFS(&tfs_selfpowered), 0x40, NULL, HFILL }},
2701
2702         { &hf_usb_configuration_remotewakeup,
2703         { "Remote Wakeup", "usb.configuration.remotewakeup", FT_BOOLEAN, 8,
2704           TFS(&tfs_remotewakeup), 0x20, NULL, HFILL }},
2705
2706         { &hf_usb_bEndpointAddress_number,
2707         { "Endpoint Number", "usb.bEndpointAddress.number", FT_UINT8, BASE_HEX,
2708           NULL, 0x0f, NULL, HFILL }},
2709
2710         { &hf_usb_bEndpointAddress_direction,
2711         { "Direction", "usb.bEndpointAddress.direction", FT_BOOLEAN, 8,
2712           TFS(&tfs_endpoint_direction), 0x80, NULL, HFILL }},
2713
2714         { &hf_usb_request_in,
2715         { "Request in", "usb.request_in", FT_FRAMENUM, BASE_NONE,
2716           NULL, 0, "The request to this packet is in this packet", HFILL }},
2717
2718         { &hf_usb_time,
2719         { "Time from request", "usb.time", FT_RELATIVE_TIME, BASE_NONE,
2720           NULL, 0, "Time between Request and Response for USB cmds", HFILL }},
2721
2722         { &hf_usb_response_in,
2723         { "Response in", "usb.response_in", FT_FRAMENUM, BASE_NONE,
2724           NULL, 0, "The response to this packet is in this packet", HFILL }},
2725     };
2726
2727     static gint *usb_subtrees[] = {
2728         &usb_hdr,
2729         &usb_setup_hdr,
2730         &usb_isodesc,
2731         &ett_usb_endpoint,
2732         &ett_usb_setup_bmrequesttype,
2733         &ett_descriptor_device,
2734         &ett_configuration_bmAttributes,
2735         &ett_configuration_bEndpointAddress,
2736         &ett_endpoint_bmAttributes
2737     };
2738
2739
2740     proto_usb = proto_register_protocol("USB", "USB", "usb");
2741     proto_register_field_array(proto_usb, hf, array_length(hf));
2742     proto_register_subtree_array(usb_subtrees, array_length(usb_subtrees));
2743
2744     usb_bulk_dissector_table = register_dissector_table("usb.bulk",
2745         "USB bulk endpoint", FT_UINT8, BASE_DEC);
2746
2747     usb_control_dissector_table = register_dissector_table("usb.control",
2748         "USB control endpoint", FT_UINT8, BASE_DEC);
2749
2750     usb_tap=register_tap("usb");
2751 }
2752
2753 void
2754 proto_reg_handoff_usb(void)
2755 {
2756     dissector_handle_t linux_usb_handle, linux_usb_mmapped_handle;
2757
2758     linux_usb_handle = create_dissector_handle(dissect_linux_usb, proto_usb);
2759     linux_usb_mmapped_handle = create_dissector_handle(dissect_linux_usb_mmapped,
2760                                                        proto_usb);
2761
2762     dissector_add("wtap_encap", WTAP_ENCAP_USB_LINUX, linux_usb_handle);
2763     dissector_add("wtap_encap", WTAP_ENCAP_USB_LINUX_MMAPPED, linux_usb_mmapped_handle);
2764 }