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