Make sure any data structures we allocate are properly initialized. Fixes
[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  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30 #include <epan/packet.h>
31 #include <epan/etypes.h>
32 #include <epan/addr_resolv.h>
33 #include <epan/emem.h>
34 #include <epan/tap.h>
35 #include <epan/conversation.h>
36 #include <epan/expert.h>
37 #include <string.h>
38 #include "packet-usb.h"
39
40 /* protocols and header fields */
41 static int proto_usb = -1;
42
43 /* Linux USB pseudoheader fields */
44 static int hf_usb_urb_id = -1;
45 static int hf_usb_urb_type = -1;
46 static int hf_usb_transfer_type = -1;
47 static int hf_usb_endpoint_number = -1;
48 static int hf_usb_device_address = -1;
49 static int hf_usb_bus_id = -1;
50 static int hf_usb_setup_flag = -1;
51 static int hf_usb_data_flag = -1;
52 static int hf_usb_urb_status = -1;
53 static int hf_usb_urb_len = -1;
54 static int hf_usb_data_len = -1;
55
56 static int hf_usb_src_endpoint_number = -1;
57 static int hf_usb_dst_endpoint_number = -1;
58 static int hf_usb_request = -1;
59 static int hf_usb_value = -1;
60 static int hf_usb_index = -1;
61 static int hf_usb_length = -1;
62 static int hf_usb_data = -1;
63 static int hf_usb_bmRequestType = -1;
64 static int hf_usb_bmRequestType_direction = -1;
65 static int hf_usb_bmRequestType_type = -1;
66 static int hf_usb_bmRequestType_recipient = -1;
67 static int hf_usb_bDescriptorType = -1;
68 static int hf_usb_descriptor_index = -1;
69 static int hf_usb_language_id = -1;
70 static int hf_usb_bLength = -1;
71 static int hf_usb_bcdUSB = -1;
72 static int hf_usb_bDeviceClass = -1;
73 static int hf_usb_bDeviceSubClass = -1;
74 static int hf_usb_bDeviceProtocol = -1;
75 static int hf_usb_bMaxPacketSize0 = -1;
76 static int hf_usb_idVendor = -1;
77 static int hf_usb_idProduct = -1;
78 static int hf_usb_bcdDevice = -1;
79 static int hf_usb_iManufacturer = -1;
80 static int hf_usb_iProduct = -1;
81 static int hf_usb_iSerialNumber = -1;
82 static int hf_usb_bNumConfigurations = -1;
83 static int hf_usb_wLANGID = -1;
84 static int hf_usb_bString = -1;
85 static int hf_usb_bInterfaceNumber = -1;
86 static int hf_usb_bAlternateSetting = -1;
87 static int hf_usb_bNumEndpoints = -1;
88 static int hf_usb_bInterfaceClass = -1;
89 static int hf_usb_bInterfaceSubClass = -1;
90 static int hf_usb_bInterfaceProtocol = -1;
91 static int hf_usb_iInterface = -1;
92 static int hf_usb_bEndpointAddress = -1;
93 static int hf_usb_bmAttributes = -1;
94 static int hf_usb_bEndpointAttributeTransfer = -1;
95 static int hf_usb_bEndpointAttributeSynchonisation = -1;
96 static int hf_usb_bEndpointAttributeBehaviour = -1;
97 static int hf_usb_wMaxPacketSize = -1;
98 static int hf_usb_bInterval = -1;
99 static int hf_usb_wTotalLength = -1;
100 static int hf_usb_bNumInterfaces = -1;
101 static int hf_usb_bConfigurationValue = -1;
102 static int hf_usb_iConfiguration = -1;
103 static int hf_usb_bMaxPower = -1;
104 static int hf_usb_configuration_bmAttributes = -1;
105 static int hf_usb_configuration_legacy10buspowered = -1;
106 static int hf_usb_configuration_selfpowered = -1;
107 static int hf_usb_configuration_remotewakeup = -1;
108 static int hf_usb_bEndpointAddress_direction = -1;
109 static int hf_usb_bEndpointAddress_number = -1;
110 static int hf_usb_response_in = -1;
111 static int hf_usb_time = -1;
112 static int hf_usb_request_in = -1;
113
114 static gint usb_hdr = -1;
115 static gint usb_setup_hdr = -1;
116 static gint ett_usb_setup_bmrequesttype = -1;
117 static gint ett_descriptor_device = -1;
118 static gint ett_configuration_bmAttributes = -1;
119 static gint ett_configuration_bEndpointAddress = -1;
120 static gint ett_endpoint_bmAttributes = -1;
121
122
123 static int usb_tap = -1;
124
125 static dissector_table_t usb_bulk_dissector_table;
126 static dissector_table_t usb_control_dissector_table;
127
128 static const value_string usb_langid_vals[] = {
129     {0x0000,    "no language specified"},
130     {0x0409,    "English (United States)"},
131     {0, NULL}
132 };
133
134 static const value_string usb_interfaceclass_vals[] = {
135     {IF_CLASS_FROM_INTERFACE_DESC,      "Use class info in Interface Descriptor"},
136     {IF_CLASS_AUDIO,                    "AUDIO"},
137     {IF_CLASS_COMMUNICATIONS,           "COMMUNICATIONS"},
138     {IF_CLASS_HID,                      "HID"},
139     {IF_CLASS_PHYSICAL,                 "PHYSICAL"},
140     {IF_CLASS_IMAGE,                    "IMAGE"},
141     {IF_CLASS_PRINTER,                  "PRINTER"},
142     {IF_CLASS_MASSTORAGE,               "MASSTORAGE"},
143     {IF_CLASS_HUB,                      "HUB"},
144     {IF_CLASS_CDC_DATA,                 "CDC_DATA"},
145     {IF_CLASS_SMART_CARD,               "SMART_CARD"},
146     {IF_CLASS_CONTENT_SECURITY,         "CONTENT_SECURITY"},
147     {IF_CLASS_VIDEO,                    "VIDEO"},
148     {IF_CLASS_DIAGNOSTIC_DEVICE,        "DIAGNOSTIC_DEVICE"},
149     {IF_CLASS_WIRELESS_CONTROLLER,      "WIRELESS_CONTROLLER"},
150     {IF_CLASS_MISCELLANEOUS,            "MISCELLANEOUS"},
151     {IF_CLASS_APPLICATION_SPECIFIC,     "APPLICATION_SPECIFIC"},
152     {IF_CLASS_VENDOR_SPECIFIC,          "VENDOR_SPECIFIC"},
153     {0, NULL}
154 };
155
156
157 static const value_string usb_transfer_type_vals[] = {
158     {URB_CONTROL, "URB_CONTROL"},
159     {URB_ISOCHRONOUS,"URB_ISOCHRONOUS"},
160     {URB_INTERRUPT,"URB_INTERRUPT"},
161     {URB_BULK,"URB_BULK"},
162     {0, NULL}
163 };
164
165 static const value_string usb_urb_type_vals[] = {
166     {URB_SUBMIT, "URB_SUBMIT"},
167     {URB_COMPLETE,"URB_COMPLETE"},
168     {URB_ERROR,"URB_ERROR"},
169     {0, NULL}
170 };
171
172 #define USB_DT_DEVICE                   0x01
173 #define USB_DT_CONFIG                   0x02
174 #define USB_DT_STRING                   0x03
175 #define USB_DT_INTERFACE                0x04
176 #define USB_DT_ENDPOINT                 0x05
177 #define USB_DT_DEVICE_QUALIFIER         0x06
178 #define USB_DT_OTHER_SPEED_CONFIG       0x07
179 #define USB_DT_INTERFACE_POWER          0x08
180 /* these are from a minor usb 2.0 revision (ECN) */
181 #define USB_DT_OTG                      0x09
182 #define USB_DT_DEBUG                    0x0a
183 #define USB_DT_INTERFACE_ASSOCIATION    0x0b
184 /* these are from the Wireless USB spec */
185 #define USB_DT_SECURITY                 0x0c
186 #define USB_DT_KEY                      0x0d
187 #define USB_DT_ENCRYPTION_TYPE          0x0e
188 #define USB_DT_BOS                      0x0f
189 #define USB_DT_DEVICE_CAPABILITY        0x10
190 #define USB_DT_WIRELESS_ENDPOINT_COMP   0x11
191 #define USB_DT_HID                      0x21
192 #define USB_DT_RPIPE                    0x22
193 static const value_string descriptor_type_vals[] = {
194     {USB_DT_DEVICE,                     "DEVICE"},
195     {USB_DT_CONFIG,                     "CONFIGURATION"},
196     {USB_DT_STRING,                     "STRING"},
197     {USB_DT_INTERFACE,                  "INTERFACE"},
198     {USB_DT_ENDPOINT,                   "ENDPOINT"},
199     {USB_DT_DEVICE_QUALIFIER,           "DEVICE QUALIFIER"},
200     {USB_DT_OTHER_SPEED_CONFIG,         "OTHER_SPEED CONFIG"},
201     {USB_DT_INTERFACE_POWER,            "INTERFACE POWER"},
202     {USB_DT_OTG,                        "OTG"},
203     {USB_DT_DEBUG,                      "DEBUG"},
204     {USB_DT_INTERFACE_ASSOCIATION,      "INTERFACE ASSOCIATION"},
205     {USB_DT_SECURITY,                   "SECURITY"},
206     {USB_DT_KEY,                        "KEY"},
207     {USB_DT_ENCRYPTION_TYPE,            "ENCRYPTION TYPE"},
208     {USB_DT_BOS,                        "BOS"},
209     {USB_DT_DEVICE_CAPABILITY,          "DEVICE CAPABILITY"},
210     {USB_DT_WIRELESS_ENDPOINT_COMP,     "WIRELESS ENDPOINT COMP"},
211     {USB_DT_HID,                        "HID"},
212     {USB_DT_RPIPE,                      "RPIPE"},
213     {0,NULL}
214 };
215
216 static const value_string usb_bmAttributes_transfer_vals[] = {
217     {0x00,      "Control-Transfer"},
218     {0x01,      "Isochronous-Transfer"},
219     {0x02,      "Bulk-Transfer"},
220     {0x03,      "Interrupt-Transfer"},
221     {0,NULL}
222 };
223
224 static const value_string usb_bmAttributes_sync_vals[] = {
225     {0x00,      "No Sync"},
226     {0x04,      "Asynchronous"},
227     {0x08,      "Adaptive"},
228     {0x0c,      "Synchronous"},
229     {0,NULL}
230 };
231
232 static const value_string usb_bmAttributes_behaviour_vals[] = {
233     {0x00,      "Data-Endpoint"},
234     {0x10,      "Explicit Feedback-Endpoint"},
235     {0x20,      "Implicit Feedback-Data-Endpoint"},
236     {0x30,      "Reserved"},
237     {0,NULL}
238 };
239
240 /* from linux/include/asm-generic/errno.h */
241 #define EPERM            1      /* Operation not permitted */
242 #define ENOENT           2      /* No such file or directory */
243 #define ESRCH            3      /* No such process */
244 #define EINTR            4      /* Interrupted system call */
245 #define EIO              5      /* I/O error */
246 #define ENXIO            6      /* No such device or address */
247 #define E2BIG            7      /* Argument list too long */
248 #define ENOEXEC          8      /* Exec format error */
249 #define EBADF            9      /* Bad file number */
250 #define ECHILD          10      /* No child processes */
251 #define EAGAIN          11      /* Try again */
252 #define ENOMEM          12      /* Out of memory */
253 #define EACCES          13      /* Permission denied */
254 #define EFAULT          14      /* Bad address */
255 #define ENOTBLK         15      /* Block device required */
256 #define EBUSY           16      /* Device or resource busy */
257 #define EEXIST          17      /* File exists */
258 #define EXDEV           18      /* Cross-device link */
259 #define ENODEV          19      /* No such device */
260 #define ENOTDIR         20      /* Not a directory */
261 #define EISDIR          21      /* Is a directory */
262 #define EINVAL          22      /* Invalid argument */
263 #define ENFILE          23      /* File table overflow */
264 #define EMFILE          24      /* Too many open files */
265 #define ENOTTY          25      /* Not a typewriter */
266 #define ETXTBSY         26      /* Text file busy */
267 #define EFBIG           27      /* File too large */
268 #define ENOSPC          28      /* No space left on device */
269 #define ESPIPE          29      /* Illegal seek */
270 #define EROFS           30      /* Read-only file system */
271 #define EMLINK          31      /* Too many links */
272 #define EPIPE           32      /* Broken pipe */
273 #define EDOM            33      /* Math argument out of domain of func */
274 #define ERANGE          34      /* Math result not representable */
275
276
277 /* from linux/include/asm-generic/errno.h*/
278 #define EDEADLK         35      /* Resource deadlock would occur */
279 #define ENAMETOOLONG    36      /* File name too long */
280 #define ENOLCK          37      /* No record locks available */
281 #define ENOSYS          38      /* Function not implemented */
282 #define ENOTEMPTY       39      /* Directory not empty */
283 #define ELOOP           40      /* Too many symbolic links encountered */
284 #define EWOULDBLOCK     EAGAIN  /* Operation would block */
285 #define ENOMSG          42      /* No message of desired type */
286 #define EIDRM           43      /* Identifier removed */
287 #define ECHRNG          44      /* Channel number out of range */
288 #define EL2NSYNC        45      /* Level 2 not synchronized */
289 #define EL3HLT          46      /* Level 3 halted */
290 #define EL3RST          47      /* Level 3 reset */
291 #define ELNRNG          48      /* Link number out of range */
292 #define EUNATCH         49      /* Protocol driver not attached */
293 #define ENOCSI          50      /* No CSI structure available */
294 #define EL2HLT          51      /* Level 2 halted */
295 #define EBADE           52      /* Invalid exchange */
296 #define EBADR           53      /* Invalid request descriptor */
297 #define EXFULL          54      /* Exchange full */
298 #define ENOANO          55      /* No anode */
299 #define EBADRQC         56      /* Invalid request code */
300 #define EBADSLT         57      /* Invalid slot */
301
302 #define EDEADLOCK       EDEADLK
303
304 #define EBFONT          59      /* Bad font file format */
305 #define ENOSTR          60      /* Device not a stream */
306 #define ENODATA         61      /* No data available */
307 #define ETIME           62      /* Timer expired */
308 #define ENOSR           63      /* Out of streams resources */
309 #define ENONET          64      /* Machine is not on the network */
310 #define ENOPKG          65      /* Package not installed */
311 #define EREMOTE         66      /* Object is remote */
312 #define ENOLINK         67      /* Link has been severed */
313 #define EADV            68      /* Advertise error */
314 #define ESRMNT          69      /* Srmount error */
315 #define ECOMM           70      /* Communication error on send */
316 #define EPROTO          71      /* Protocol error */
317 #define EMULTIHOP       72      /* Multihop attempted */
318 #define EDOTDOT         73      /* RFS specific error */
319 #define EBADMSG         74      /* Not a data message */
320 #define EOVERFLOW       75      /* Value too large for defined data type */
321 #define ENOTUNIQ        76      /* Name not unique on network */
322 #define EBADFD          77      /* File descriptor in bad state */
323 #define EREMCHG         78      /* Remote address changed */
324 #define ELIBACC         79      /* Can not access a needed shared library */
325 #define ELIBBAD         80      /* Accessing a corrupted shared library */
326 #define ELIBSCN         81      /* .lib section in a.out corrupted */
327 #define ELIBMAX         82      /* Attempting to link in too many shared libraries */
328 #define ELIBEXEC        83      /* Cannot exec a shared library directly */
329 #define EILSEQ          84      /* Illegal byte sequence */
330 #define ERESTART        85      /* Interrupted system call should be restarted */
331 #define ESTRPIPE        86      /* Streams pipe error */
332 #define EUSERS          87      /* Too many users */
333 #define ENOTSOCK        88      /* Socket operation on non-socket */
334 #define EDESTADDRREQ    89      /* Destination address required */
335 #define EMSGSIZE        90      /* Message too long */
336 #define EPROTOTYPE      91      /* Protocol wrong type for socket */
337 #define ENOPROTOOPT     92      /* Protocol not available */
338 #define EPROTONOSUPPORT 93      /* Protocol not supported */
339 #define ESOCKTNOSUPPORT 94      /* Socket type not supported */
340 #define EOPNOTSUPP      95      /* Operation not supported on transport endpoint */
341 #define EPFNOSUPPORT    96      /* Protocol family not supported */
342 #define EAFNOSUPPORT    97      /* Address family not supported by protocol */
343 #define EADDRINUSE      98      /* Address already in use */
344 #define EADDRNOTAVAIL   99      /* Cannot assign requested address */
345 #define ENETDOWN        100     /* Network is down */
346 #define ENETUNREACH     101     /* Network is unreachable */
347 #define ENETRESET       102     /* Network dropped connection because of reset */
348 #define ECONNABORTED    103     /* Software caused connection abort */
349 #define ECONNRESET      104     /* Connection reset by peer */
350 #define ENOBUFS         105     /* No buffer space available */
351 #define EISCONN         106     /* Transport endpoint is already connected */
352 #define ENOTCONN        107     /* Transport endpoint is not connected */
353 #define ESHUTDOWN       108     /* Cannot send after transport endpoint shutdown */
354 #define ETOOMANYREFS    109     /* Too many references: cannot splice */
355 #define ETIMEDOUT       110     /* Connection timed out */
356 #define ECONNREFUSED    111     /* Connection refused */
357 #define EHOSTDOWN       112     /* Host is down */
358 #define EHOSTUNREACH    113     /* No route to host */
359 #define EALREADY        114     /* Operation already in progress */
360 #define EINPROGRESS     115     /* Operation now in progress */
361 #define ESTALE          116     /* Stale NFS file handle */
362 #define EUCLEAN         117     /* Structure needs cleaning */
363 #define ENOTNAM         118     /* Not a XENIX named type file */
364 #define ENAVAIL         119     /* No XENIX semaphores available */
365 #define EISNAM          120     /* Is a named type file */
366 #define EREMOTEIO       121     /* Remote I/O error */
367 #define EDQUOT          122     /* Quota exceeded */
368
369 #define ENOMEDIUM       123     /* No medium found */
370 #define EMEDIUMTYPE     124     /* Wrong medium type */
371 #define ECANCELED       125     /* Operation Canceled */
372 #define ENOKEY          126     /* Required key not available */
373 #define EKEYEXPIRED     127     /* Key has expired */
374 #define EKEYREVOKED     128     /* Key has been revoked */
375 #define EKEYREJECTED    129     /* Key was rejected by service */
376
377 /* for robust mutexes */
378 #define EOWNERDEAD      130     /* Owner died */
379 #define ENOTRECOVERABLE 131     /* State not recoverable */
380
381
382 static const value_string usb_urb_status_vals[] = {
383     { 0,                "Success"},
384     { -EPERM,           "Operation not permitted (-EPERM)" },
385     { -ENOENT,          "No such file or directory (-ENOENT)" },
386     { -ESRCH,           "No such process (-ESRCH)" },
387     { -EINTR,           "Interrupted system call (-EINTR)" },
388     { -EIO,             "I/O error (-EIO)" },
389     { -ENXIO,           "No such device or address (-ENXIO)" },
390     { -E2BIG,           "Argument list too long (-E2BIG)" },
391     { -ENOEXEC,         "Exec format error (-ENOEXEC)" },
392     { -EBADF,           "Bad file number (-EBADF)" },
393     { -ECHILD,          "No child processes (-ECHILD)" },
394     { -EAGAIN,          "Try again (-EAGAIN)" },
395     { -ENOMEM,          "Out of memory (-ENOMEM)" },
396     { -EACCES,          "Permission denied (-EACCES)" },
397     { -EFAULT,          "Bad address (-EFAULT)" },
398     { -ENOTBLK,         "Block device required (-ENOTBLK)" },
399     { -EBUSY,           "Device or resource busy (-EBUSY)" },
400     { -EEXIST,          "File exists (-EEXIST)" },
401     { -EXDEV,           "Cross-device link (-EXDEV)" },
402     { -ENODEV,          "No such device (-ENODEV)" },
403     { -ENOTDIR,         "Not a directory (-ENOTDIR)" },
404     { -EISDIR,          "Is a directory (-EISDIR)" },
405     { -EINVAL,          "Invalid argument (-EINVAL)" },
406     { -ENFILE,          "File table overflow (-ENFILE)" },
407     { -EMFILE,          "Too many open files (-EMFILE)" },
408     { -ENOTTY,          "Not a typewriter (-ENOTTY)" },
409     { -ETXTBSY,         "Text file busy (-ETXTBSY)" },
410     { -EFBIG,           "File too large (-EFBIG)" },
411     { -ENOSPC,          "No space left on device (-ENOSPC)" },
412     { -ESPIPE,          "Illegal seek (-ESPIPE)" },
413     { -EROFS,           "Read-only file system (-EROFS)" },
414     { -EMLINK,          "Too many links (-EMLINK)" },
415     { -EPIPE,           "Broken pipe (-EPIPE)" },
416     { -EDOM,            "Math argument out of domain of func (-EDOM)" },
417     { -ERANGE,          "Math result not representable (-ERANGE)" },
418     { -EDEADLK,         "Resource deadlock would occur (-EDEADLK)" },
419     { -ENAMETOOLONG,    "File name too long (-ENAMETOOLONG)" },
420     { -ENOLCK,          "No record locks available (-ENOLCK)" },
421     { -ENOSYS,          "Function not implemented (-ENOSYS)" },
422     { -ENOTEMPTY,       "Directory not empty (-ENOTEMPTY)" },
423     { -ELOOP,           "Too many symbolic links encountered (-ELOOP)" },
424     { -ENOMSG,          "No message of desired type (-ENOMSG)" },
425     { -EIDRM,           "Identifier removed (-EIDRM)" },
426     { -ECHRNG,          "Channel number out of range (-ECHRNG)" },
427     { -EL2NSYNC,        "Level 2 not synchronized (-EL2NSYNC)" },
428     { -EL3HLT,          "Level 3 halted (-EL3HLT)" },
429     { -EL3RST,          "Level 3 reset (-EL3RST)" },
430     { -ELNRNG,          "Link number out of range (-ELNRNG)" },
431     { -EUNATCH,         "Protocol driver not attached (-EUNATCH)" },
432     { -ENOCSI,          "No CSI structure available (-ENOCSI)" },
433     { -EL2HLT,          "Level 2 halted (-EL2HLT)" },
434     { -EBADE,           "Invalid exchange (-EBADE)" },
435     { -EBADR,           "Invalid request descriptor (-EBADR)" },
436     { -EXFULL,          "Exchange full (-EXFULL)" },
437     { -ENOANO,          "No anode (-ENOANO)" },
438     { -EBADRQC,         "Invalid request code (-EBADRQC)" },
439     { -EBADSLT,         "Invalid slot (-EBADSLT)" },
440     { -EBFONT,          "Bad font file format (-EBFONT)" },
441     { -ENOSTR,          "Device not a stream (-ENOSTR)" },
442     { -ENODATA,         "No data available (-ENODATA)" },
443     { -ETIME,           "Timer expired (-ETIME)" },
444     { -ENOSR,           "Out of streams resources (-ENOSR)" },
445     { -ENONET,          "Machine is not on the network (-ENONET)" },
446     { -ENOPKG,          "Package not installed (-ENOPKG)" },
447     { -EREMOTE,         "Object is remote (-EREMOTE)" },
448     { -ENOLINK,         "Link has been severed (-ENOLINK)" },
449     { -EADV,            "Advertise error (-EADV)" },
450     { -ESRMNT,          "Srmount error (-ESRMNT)" },
451     { -ECOMM,           "Communication error on send (-ECOMM)" },
452     { -EPROTO,          "Protocol error (-EPROTO)" },
453     { -EMULTIHOP,       "Multihop attempted (-EMULTIHOP)" },
454     { -EDOTDOT,         "RFS specific error (-EDOTDOT)" },
455     { -EBADMSG,         "Not a data message (-EBADMSG)" },
456     { -EOVERFLOW,       "Value too large for defined data type (-EOVERFLOW)" },
457     { -ENOTUNIQ,        "Name not unique on network (-ENOTUNIQ)" },
458     { -EBADFD,          "File descriptor in bad state (-EBADFD)" },
459     { -EREMCHG,         "Remote address changed (-EREMCHG)" },
460     { -ELIBACC,         "Can not access a needed shared library (-ELIBACC)" },
461     { -ELIBBAD,         "Accessing a corrupted shared library (-ELIBBAD)" },
462     { -ELIBSCN,         ".lib section in a.out corrupted (-ELIBSCN)" },
463     { -ELIBMAX,         "Attempting to link in too many shared libraries (-ELIBMAX)" },
464     { -ELIBEXEC,        "Cannot exec a shared library directly (-ELIBEXEC)" },
465     { -EILSEQ,          "Illegal byte sequence (-EILSEQ)" },
466     { -ERESTART,        "Interrupted system call should be restarted (-ERESTART)" },
467     { -ESTRPIPE,        "Streams pipe error (-ESTRPIPE)" },
468     { -EUSERS,          "Too many users (-EUSERS)" },
469     { -ENOTSOCK,        "Socket operation on non-socket (-ENOTSOCK)" },
470     { -EDESTADDRREQ,    "Destination address required (-EDESTADDRREQ)" },
471     { -EMSGSIZE,        "Message too long (-EMSGSIZE)" },
472     { -EPROTOTYPE,      "Protocol wrong type for socket (-EPROTOTYPE)" },
473     { -ENOPROTOOPT,     "Protocol not available (-ENOPROTOOPT)" },
474     { -EPROTONOSUPPORT, "Protocol not supported (-EPROTONOSUPPORT)" },
475     { -ESOCKTNOSUPPORT, "Socket type not supported (-ESOCKTNOSUPPORT)" },
476     { -EOPNOTSUPP,      "Operation not supported on transport endpoint (-EOPNOTSUPP)" },
477     { -EPFNOSUPPORT,    "Protocol family not supported (-EPFNOSUPPORT)" },
478     { -EAFNOSUPPORT,    "Address family not supported by protocol (-EAFNOSUPPORT)" },
479     { -EADDRINUSE,      "Address already in use (-EADDRINUSE)" },
480     { -EADDRNOTAVAIL,   "Cannot assign requested address (-EADDRNOTAVAIL)" },
481     { -ENETDOWN,        "Network is down (-ENETDOWN)" },
482     { -ENETUNREACH,     "Network is unreachable (-ENETUNREACH)" },
483     { -ENETRESET,       "Network dropped connection because of reset (-ENETRESET)" },
484     { -ECONNABORTED,    "Software caused connection abort (-ECONNABORTED)" },
485     { -ECONNRESET,      "Connection reset by peer (-ECONNRESET)" },
486     { -ENOBUFS,         "No buffer space available (-ENOBUFS)" },
487     { -EISCONN,         "Transport endpoint is already connected (-EISCONN)" },
488     { -ENOTCONN,        "Transport endpoint is not connected (-ENOTCONN)" },
489     { -ESHUTDOWN,       "Cannot send after transport endpoint shutdown (-ESHUTDOWN)" },
490     { -ETOOMANYREFS,    "Too many references: cannot splice (-ETOOMANYREFS)" },
491     { -ETIMEDOUT,       "Connection timed out (-ETIMEDOUT)" },
492     { -ECONNREFUSED,    "Connection refused (-ECONNREFUSED)" },
493     { -EHOSTDOWN,       "Host is down (-EHOSTDOWN)" },
494     { -EHOSTUNREACH,    "No route to host (-EHOSTUNREACH)" },
495     { -EALREADY,        "Operation already in progress (-EALREADY)" },
496     { -EINPROGRESS,     "Operation now in progress (-EINPROGRESS)" },
497     { -ESTALE,          "Stale NFS file handle (-ESTALE)" },
498     { -EUCLEAN,         "Structure needs cleaning (-EUCLEAN)" },
499     { -ENOTNAM,         "Not a XENIX named type file (-ENOTNAM)" },
500     { -ENAVAIL,         "No XENIX semaphores available (-ENAVAIL)" },
501     { -EISNAM,          "Is a named type file (-EISNAM)" },
502     { -EREMOTEIO,       "Remote I/O error (-EREMOTEIO)" },
503     { -EDQUOT,          "Quota exceeded (-EDQUOT)" },
504     { -ENOMEDIUM,       "No medium found (-ENOMEDIUM)" },
505     { -EMEDIUMTYPE,     "Wrong medium type (-EMEDIUMTYPE)" },
506     { -ECANCELED,       "Operation Canceled (-ECANCELED)" },
507     { -ENOKEY,          "Required key not available (-ENOKEY)" },
508     { -EKEYEXPIRED,     "Key has expired (-EKEYEXPIRED)" },
509     { -EKEYREVOKED,     "Key has been revoked (-EKEYREVOKED)" },
510     { -EKEYREJECTED,    "Key was rejected by service (-EKEYREJECTED)" },
511     { -EOWNERDEAD,      "Owner died (-EOWNERDEAD)" },
512     { -ENOTRECOVERABLE, "State not recoverable (-ENOTRECOVERABLE)" },
513     { 0, NULL }
514 };
515
516
517 static usb_conv_info_t *
518 get_usb_conv_info(conversation_t *conversation)
519 {
520     usb_conv_info_t *usb_conv_info;
521
522     /* do we have conversation specific data ? */
523     usb_conv_info = conversation_get_proto_data(conversation, proto_usb);
524     if(!usb_conv_info){
525         /* no not yet so create some */
526         usb_conv_info = se_alloc0(sizeof(usb_conv_info_t));
527         usb_conv_info->interfaceClass=IF_CLASS_UNKNOWN;
528         usb_conv_info->transactions=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "usb transactions");
529
530         conversation_add_proto_data(conversation, proto_usb, usb_conv_info);
531     }
532
533     return usb_conv_info;
534 }
535
536 static conversation_t *
537 get_usb_conversation(packet_info *pinfo, address *src_addr, address *dst_addr, guint32 src_endpoint, guint32 dst_endpoint)
538 {
539     conversation_t *conversation;
540
541     /*
542      * Do we have a conversation for this connection?
543      */
544     conversation = find_conversation(pinfo->fd->num,
545                                src_addr, dst_addr,
546                                pinfo->ptype,
547                                src_endpoint, dst_endpoint, 0);
548     if(conversation){
549         return conversation;
550     }
551
552     /* We don't yet have a conversation, so create one. */
553     conversation = conversation_new(pinfo->fd->num,
554                            src_addr, dst_addr,
555                            pinfo->ptype,
556                            src_endpoint, dst_endpoint, 0);
557     return conversation;
558 }
559
560
561
562 /* SETUP dissectors */
563
564
565 /*
566  * This dissector is used to dissect the setup part and the data
567  * for URB_CONTROL_INPUT / GET DESCRIPTOR
568  */
569
570
571 /* 9.6.2 */
572 static int
573 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_)
574 {
575     proto_item *item=NULL;
576     proto_tree *tree=NULL;
577     int old_offset=offset;
578
579     if(parent_tree){
580         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "DEVICE QUALIFIER DESCRIPTOR");
581         tree=proto_item_add_subtree(item, ett_descriptor_device);
582     }
583
584     /* bLength */
585     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
586     offset++;
587
588     /* bDescriptorType */
589     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
590     offset++;
591
592     /* bcdUSB */
593     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, TRUE);
594     offset+=2;
595
596     /* bDeviceClass */
597     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, TRUE);
598     offset++;
599
600     /* bDeviceSubClass */
601     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, TRUE);
602     offset++;
603
604     /* bDeviceProtocol */
605     proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, TRUE);
606     offset++;
607
608     /* bMaxPacketSize0 */
609     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, TRUE);
610     offset++;
611
612     /* bNumConfigurations */
613     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, TRUE);
614     offset++;
615
616     /* one reserved byte */
617     offset++;
618
619     if(item){
620         proto_item_set_len(item, offset-old_offset);
621     }
622
623     return offset;
624 }
625
626 /* 9.6.1 */
627 static int
628 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_)
629 {
630     proto_item *item=NULL;
631     proto_tree *tree=NULL;
632     int old_offset=offset;
633
634     if(parent_tree){
635         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "DEVICE DESCRIPTOR");
636         tree=proto_item_add_subtree(item, ett_descriptor_device);
637     }
638
639     /* bLength */
640     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
641     offset++;
642
643     /* bDescriptorType */
644     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
645     offset++;
646
647     /* bcdUSB */
648     proto_tree_add_item(tree, hf_usb_bcdUSB, tvb, offset, 2, TRUE);
649     offset+=2;
650
651     /* bDeviceClass */
652     proto_tree_add_item(tree, hf_usb_bDeviceClass, tvb, offset, 1, TRUE);
653     offset++;
654
655     /* bDeviceSubClass */
656     proto_tree_add_item(tree, hf_usb_bDeviceSubClass, tvb, offset, 1, TRUE);
657     offset++;
658
659     /* bDeviceProtocol */
660     proto_tree_add_item(tree, hf_usb_bDeviceProtocol, tvb, offset, 1, TRUE);
661     offset++;
662
663     /* bMaxPacketSize0 */
664     proto_tree_add_item(tree, hf_usb_bMaxPacketSize0, tvb, offset, 1, TRUE);
665     offset++;
666
667     /* idVendor */
668     proto_tree_add_item(tree, hf_usb_idVendor, tvb, offset, 2, TRUE);
669     offset+=2;
670
671     /* idProduct */
672     proto_tree_add_item(tree, hf_usb_idProduct, tvb, offset, 2, TRUE);
673     offset+=2;
674
675     /* bcdDevice */
676     proto_tree_add_item(tree, hf_usb_bcdDevice, tvb, offset, 2, TRUE);
677     offset+=2;
678
679     /* iManufacturer */
680     proto_tree_add_item(tree, hf_usb_iManufacturer, tvb, offset, 1, TRUE);
681     offset++;
682
683     /* iProduct */
684     proto_tree_add_item(tree, hf_usb_iProduct, tvb, offset, 1, TRUE);
685     offset++;
686
687     /* iSerialNumber */
688     proto_tree_add_item(tree, hf_usb_iSerialNumber, tvb, offset, 1, TRUE);
689     offset++;
690
691     /* bNumConfigurations */
692     proto_tree_add_item(tree, hf_usb_bNumConfigurations, tvb, offset, 1, TRUE);
693     offset++;
694
695     if(item){
696         proto_item_set_len(item, offset-old_offset);
697     }
698
699     return offset;
700 }
701
702 /* 9.6.7 */
703 static int
704 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_)
705 {
706     proto_item *item=NULL;
707     proto_tree *tree=NULL;
708     int old_offset=offset;
709     guint8 len;
710
711     if(parent_tree){
712         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "STRING DESCRIPTOR");
713         tree=proto_item_add_subtree(item, ett_descriptor_device);
714     }
715
716     /* bLength */
717     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
718     len=tvb_get_guint8(tvb, offset);
719     offset++;
720
721     /* bDescriptorType */
722     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
723     offset++;
724
725     if(!usb_trans_info->u.get_descriptor.index){
726         /* list of languanges */
727         while(len>(offset-old_offset)){
728             /* wLANGID */
729             proto_tree_add_item(tree, hf_usb_wLANGID, tvb, offset, 2, TRUE);
730             offset+=2;
731         }
732     } else {
733         char *str;
734
735         /* unicode string */
736         str=tvb_get_ephemeral_faked_unicode(tvb, offset, (len-2)/2, TRUE);
737         proto_tree_add_string(tree, hf_usb_bString, tvb, offset, len-2, str);
738     }
739
740     if(item){
741         proto_item_set_len(item, offset-old_offset);
742     }
743
744     return offset;
745 }
746
747
748
749 /* 9.6.5 */
750 static int
751 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)
752 {
753     proto_item *item=NULL;
754     proto_tree *tree=NULL;
755     int old_offset=offset;
756
757     if(parent_tree){
758         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "INTERFACE DESCRIPTOR");
759         tree=proto_item_add_subtree(item, ett_descriptor_device);
760     }
761
762     /* bLength */
763     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
764     offset++;
765
766     /* bDescriptorType */
767     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
768     offset++;
769
770     /* bInterfaceNumber */
771     proto_tree_add_item(tree, hf_usb_bInterfaceNumber, tvb, offset, 1, TRUE);
772     offset++;
773
774     /* bAlternateSetting */
775     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, TRUE);
776     offset++;
777
778     /* bNumEndpoints */
779     proto_tree_add_item(tree, hf_usb_bNumEndpoints, tvb, offset, 1, TRUE);
780     offset++;
781
782     /* bInterfaceClass */
783     proto_tree_add_item(tree, hf_usb_bInterfaceClass, tvb, offset, 1, TRUE);
784     /* save the class so we can access it later in the endpoint descriptor */
785     usb_conv_info->interfaceClass=tvb_get_guint8(tvb, offset);
786     if(!pinfo->fd->flags.visited){
787         usb_trans_info->interface_info=se_alloc0(sizeof(usb_conv_info_t));
788         usb_trans_info->interface_info->interfaceClass=tvb_get_guint8(tvb, offset);
789         usb_trans_info->interface_info->transactions=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "usb transactions");
790     }
791     offset++;
792
793     /* bInterfaceSubClass */
794     proto_tree_add_item(tree, hf_usb_bInterfaceSubClass, tvb, offset, 1, TRUE);
795     offset++;
796
797     /* bInterfaceProtocol */
798     proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, TRUE);
799     offset++;
800
801     /* iInterface */
802     proto_tree_add_item(tree, hf_usb_iInterface, tvb, offset, 1, TRUE);
803     offset++;
804
805     if(item){
806         proto_item_set_len(item, offset-old_offset);
807     }
808
809     return offset;
810 }
811
812 /* 9.6.6 */
813 static const true_false_string tfs_endpoint_direction = {
814     "IN Endpoint",
815     "OUT Endpoint"
816 };
817 static int
818 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_)
819 {
820     proto_item *item=NULL;
821     proto_tree *tree=NULL;
822     proto_item *endpoint_item=NULL;
823     proto_tree *endpoint_tree=NULL;
824     proto_item *ep_attrib_item=NULL;
825     proto_tree *ep_attrib_tree=NULL;
826     int old_offset=offset;
827     guint8 endpoint;
828
829     if(parent_tree){
830         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "ENDPOINT DESCRIPTOR");
831         tree=proto_item_add_subtree(item, ett_descriptor_device);
832     }
833
834     /* bLength */
835     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
836     offset++;
837
838     /* bDescriptorType */
839     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
840     offset++;
841
842     /* bEndpointAddress */
843     if(tree){
844         endpoint_item=proto_tree_add_item(tree, hf_usb_bEndpointAddress, tvb, offset, 1, TRUE);
845         endpoint_tree=proto_item_add_subtree(endpoint_item, ett_configuration_bEndpointAddress);
846     }
847     endpoint=tvb_get_guint8(tvb, offset)&0x0f;
848     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_direction, tvb, offset, 1, TRUE);
849     proto_item_append_text(endpoint_item, "  %s", (tvb_get_guint8(tvb, offset)&0x80)?"IN":"OUT");
850     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_number, tvb, offset, 1, TRUE);
851     proto_item_append_text(endpoint_item, "  Endpoint:%d", endpoint);
852     offset++;
853
854     /* Together with class from the interface descriptor we know what kind
855      * of class the device at endpoint is.
856      * Make sure a conversation exists for this endpoint and attach a
857      * usb_conv_into_t structure to it.
858      *
859      * All endpoints for the same interface descriptor share the same
860      * usb_conv_info structure.
861      */
862     if((!pinfo->fd->flags.visited)&&usb_trans_info->interface_info){
863         conversation_t *conversation;
864
865         if(pinfo->destport==NO_ENDPOINT){
866             static address tmp_addr;
867             static usb_address_t usb_addr;
868
869             /* Create a new address structure that points to the same device
870              * but the new endpoint.
871              */
872             usb_addr.device=((usb_address_t *)(pinfo->src.data))->device;
873             usb_addr.endpoint=endpoint;
874             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
875             conversation=get_usb_conversation(pinfo, &tmp_addr, &pinfo->dst, endpoint, pinfo->destport);
876         } else {
877             static address tmp_addr;
878             static usb_address_t usb_addr;
879
880             /* Create a new address structure that points to the same device
881              * but the new endpoint.
882              */
883             usb_addr.device=((usb_address_t *)(pinfo->dst.data))->device;
884             usb_addr.endpoint=endpoint;
885             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
886             conversation=get_usb_conversation(pinfo, &pinfo->src, &tmp_addr, pinfo->srcport, endpoint);
887         }
888
889         conversation_add_proto_data(conversation, proto_usb, usb_trans_info->interface_info);
890     }
891
892     /* bmAttributes */
893     if (tree) {
894         ep_attrib_item=proto_tree_add_item(tree, hf_usb_bmAttributes, tvb, offset, 1, TRUE);
895         ep_attrib_tree=proto_item_add_subtree(ep_attrib_item, ett_endpoint_bmAttributes);
896     }
897     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeTransfer, tvb, offset, 1, TRUE);
898     /* isochronous only */
899     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeSynchonisation, tvb, offset, 1, TRUE);
900     /* isochronous only */
901     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeBehaviour, tvb, offset, 1, TRUE);
902     offset++;
903
904     /* wMaxPacketSize */
905     proto_tree_add_item(tree, hf_usb_wMaxPacketSize, tvb, offset, 2, TRUE);
906     offset+=2;
907
908     /* bInterval */
909     proto_tree_add_item(tree, hf_usb_bInterval, tvb, offset, 1, TRUE);
910     offset++;
911
912     if(item){
913         proto_item_set_len(item, offset-old_offset);
914     }
915
916     return offset;
917 }
918
919 static int
920 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_)
921 {
922     proto_item *item=NULL;
923     proto_tree *tree=NULL;
924     int old_offset=offset;
925     guint8 bLength;
926
927     if(parent_tree){
928         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "UNKNOWN DESCRIPTOR");
929         tree=proto_item_add_subtree(item, ett_descriptor_device);
930     }
931
932     /* bLength */
933     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
934     bLength = tvb_get_guint8(tvb, offset);
935     offset++;
936     if (bLength < 3) {
937         item = proto_tree_add_text(parent_tree, tvb, offset - 1, 1,
938             "Invalid bLength: %u",  bLength);
939         expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
940             "Invalid bLength: %u",  bLength);
941         return offset;
942     }
943
944     /* bDescriptorType */
945     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
946     offset++;
947
948     offset += bLength - 2;
949
950     if(item){
951         proto_item_set_len(item, offset-old_offset);
952     }
953
954     return offset;
955 }
956
957 /* 9.6.3 */
958 static const true_false_string tfs_mustbeone = {
959     "Must be 1 for USB 1.1 and higher",
960     "FIXME: Is this a USB 1.0 device"
961 };
962 static const true_false_string tfs_selfpowered = {
963     "This device is SELF-POWERED",
964     "This device is powered from the USB bus"
965 };
966 static const true_false_string tfs_remotewakeup = {
967     "This device supports REMOTE WAKEUP",
968     "This device does NOT support remote wakeup"
969 };
970 static int
971 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)
972 {
973     proto_item *item=NULL;
974     proto_tree *tree=NULL;
975     int old_offset=offset;
976     guint16 len;
977     proto_item *flags_item=NULL;
978     proto_tree *flags_tree=NULL;
979     guint8 flags;
980     proto_item *power_item=NULL;
981     guint8 power;
982
983     if(parent_tree){
984         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "CONFIGURATION DESCRIPTOR");
985         tree=proto_item_add_subtree(item, ett_descriptor_device);
986     }
987
988     /* bLength */
989     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
990     offset++;
991
992     /* bDescriptorType */
993     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
994     offset++;
995
996     /* wTotalLength */
997     proto_tree_add_item(tree, hf_usb_wTotalLength, tvb, offset, 2, TRUE);
998     len=tvb_get_letohs(tvb, offset);
999     offset+=2;
1000
1001     /* bNumInterfaces */
1002     proto_tree_add_item(tree, hf_usb_bNumInterfaces, tvb, offset, 1, TRUE);
1003     offset++;
1004
1005     /* bConfigurationValue */
1006     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, TRUE);
1007     offset++;
1008
1009     /* iConfiguration */
1010     proto_tree_add_item(tree, hf_usb_iConfiguration, tvb, offset, 1, TRUE);
1011     offset++;
1012
1013     /* bmAttributes */
1014     if(tree){
1015         flags_item=proto_tree_add_item(tree, hf_usb_configuration_bmAttributes, tvb, offset, 1, TRUE);
1016         flags_tree=proto_item_add_subtree(flags_item, ett_configuration_bmAttributes);
1017     }
1018     flags=tvb_get_guint8(tvb, offset);
1019     proto_tree_add_item(flags_tree, hf_usb_configuration_legacy10buspowered, tvb, offset, 1, TRUE);
1020     proto_tree_add_item(flags_tree, hf_usb_configuration_selfpowered, tvb, offset, 1, TRUE);
1021     proto_item_append_text(flags_item, "  %sSELF-POWERED", (flags&0x40)?"":"NOT ");
1022     flags&=~0x40;
1023     proto_tree_add_item(flags_tree, hf_usb_configuration_remotewakeup, tvb, offset, 1, TRUE);
1024     proto_item_append_text(flags_item, "  %sREMOTE-WAKEUP", (flags&0x20)?"":"NO ");
1025     flags&=~0x20;
1026     offset++;
1027
1028     /* bMaxPower */
1029     power_item=proto_tree_add_item(tree, hf_usb_bMaxPower, tvb, offset, 1, TRUE);
1030     power=tvb_get_guint8(tvb, offset);
1031     proto_item_append_text(power_item, "  (%dmA)", power*2);
1032     offset++;
1033
1034     /* initialize interface_info to NULL */
1035     usb_trans_info->interface_info=NULL;
1036
1037     /* decode any additional interface and endpoint descriptors */
1038     while(len>(old_offset-offset)){
1039         guint8 next_type;
1040
1041         if(tvb_length_remaining(tvb, offset)<2){
1042             break;
1043         }
1044         next_type=tvb_get_guint8(tvb, offset+1);
1045         switch(next_type){
1046         case USB_DT_INTERFACE:
1047             offset=dissect_usb_interface_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1048             break;
1049         case USB_DT_ENDPOINT:
1050             offset=dissect_usb_endpoint_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1051             break;
1052         default:
1053             offset=dissect_usb_unknown_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1054             break;
1055             /* was: return offset; */
1056         }
1057     }
1058
1059     if(item){
1060         proto_item_set_len(item, offset-old_offset);
1061     }
1062
1063     return offset;
1064 }
1065
1066
1067 static void
1068 dissect_usb_setup_get_descriptor(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean is_request, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info)
1069 {
1070     if(is_request){
1071         /* descriptor index */
1072         proto_tree_add_item(tree, hf_usb_descriptor_index, tvb, offset, 1, TRUE);
1073         usb_trans_info->u.get_descriptor.index=tvb_get_guint8(tvb, offset);
1074         offset++;
1075
1076         /* descriptor type */
1077         proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1078         usb_trans_info->u.get_descriptor.type=tvb_get_guint8(tvb, offset);
1079         offset++;
1080         if (check_col(pinfo->cinfo, COL_INFO)) {
1081             col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1082                 val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %x"));
1083         }
1084
1085         /* language id */
1086         proto_tree_add_item(tree, hf_usb_language_id, tvb, offset, 2, TRUE);
1087         offset+=2;
1088
1089         /* length */
1090         proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1091         offset += 2;
1092     } else {
1093         if (check_col(pinfo->cinfo, COL_INFO)) {
1094             col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1095                 val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %x"));
1096         }
1097         switch(usb_trans_info->u.get_descriptor.type){
1098         case USB_DT_DEVICE:
1099             offset=dissect_usb_device_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1100             break;
1101         case USB_DT_CONFIG:
1102             offset=dissect_usb_configuration_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1103             break;
1104         case USB_DT_STRING:
1105             offset=dissect_usb_string_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1106             break;
1107         case USB_DT_INTERFACE:
1108             offset=dissect_usb_interface_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1109             break;
1110         case USB_DT_ENDPOINT:
1111             offset=dissect_usb_endpoint_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1112             break;
1113         case USB_DT_DEVICE_QUALIFIER:
1114             offset=dissect_usb_device_qualifier_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1115             break;
1116         default:
1117             /* XXX dissect the descriptor coming back from the device */
1118             proto_tree_add_text(tree, tvb, offset, tvb_length_remaining(tvb, offset), "get descriptor  data...");
1119         }
1120     }
1121 }
1122
1123
1124
1125
1126 typedef void (*usb_setup_dissector)(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean is_request, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info);
1127
1128 typedef struct _usb_setup_dissector_table_t {
1129     guint8 request;
1130     usb_setup_dissector dissector;
1131 } usb_setup_dissector_table_t;
1132 #define USB_SETUP_GET_DESCRIPTOR        6
1133 static const usb_setup_dissector_table_t setup_dissectors[] = {
1134     {USB_SETUP_GET_DESCRIPTOR,  dissect_usb_setup_get_descriptor},
1135     {0, NULL}
1136 };
1137 static const value_string setup_request_names_vals[] = {
1138     {USB_SETUP_GET_DESCRIPTOR,          "GET DESCRIPTOR"},
1139     {0, NULL}
1140 };
1141
1142
1143 #define USB_DIR_OUT                     0               /* to device */
1144 #define USB_DIR_IN                      0x80            /* to host */
1145
1146 static const true_false_string tfs_bmrequesttype_direction = {
1147         "Device-to-host",
1148         "Host-to-device"
1149 };
1150
1151 #define USB_TYPE_MASK                   (0x03 << 5)
1152 #define RQT_SETUP_TYPE_STANDARD 0
1153 #define RQT_SETUP_TYPE_CLASS    1
1154 #define RQT_SETUP_TYPE_VENDOR   2
1155
1156 static const value_string bmrequesttype_type_vals[] = {
1157     {RQT_SETUP_TYPE_STANDARD, "Standard"},
1158     {RQT_SETUP_TYPE_CLASS,    "Class"},
1159     {RQT_SETUP_TYPE_VENDOR,   "Vendor"},
1160     {0, NULL}
1161 };
1162 static const value_string bmrequesttype_recipient_vals[] = {
1163     {0, "Device"},
1164     {1, "Interface"},
1165     {2, "Endpoint"},
1166     {3, "Other"},
1167     {0, NULL}
1168 };
1169
1170 static int
1171 dissect_usb_bmrequesttype(proto_tree *parent_tree, tvbuff_t *tvb, int offset,
1172     int *type)
1173 {
1174         proto_item *item=NULL;
1175         proto_tree *tree=NULL;
1176         guint8 bmRequestType;
1177
1178         if(parent_tree){
1179                 item=proto_tree_add_item(parent_tree, hf_usb_bmRequestType, tvb, offset, 1, TRUE);
1180                 tree = proto_item_add_subtree(item, ett_usb_setup_bmrequesttype);
1181         }
1182
1183         bmRequestType = tvb_get_guint8(tvb, offset);
1184         *type = (bmRequestType & USB_TYPE_MASK) >>5;
1185         proto_tree_add_item(tree, hf_usb_bmRequestType_direction, tvb, offset, 1, TRUE);
1186         proto_tree_add_item(tree, hf_usb_bmRequestType_type, tvb, offset, 1, TRUE);
1187         proto_tree_add_item(tree, hf_usb_bmRequestType_recipient, tvb, offset, 1, TRUE);
1188
1189         offset++;
1190         return offset;
1191 }
1192
1193 static void
1194 dissect_linux_usb_pseudo_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1195 {
1196     guint8 transfer_type;
1197     const gchar* val_str;
1198
1199     proto_tree_add_uint64(tree, hf_usb_urb_id, tvb, 0, 0,
1200                           pinfo->pseudo_header->linux_usb.id);
1201
1202     /* show the event type of this URB as string and as a character */
1203     val_str = val_to_str(pinfo->pseudo_header->linux_usb.event_type,
1204         usb_urb_type_vals, "Unknown %d");
1205     proto_tree_add_string_format_value(tree, hf_usb_urb_type, tvb, 0, 0,
1206         &(pinfo->pseudo_header->linux_usb.event_type),
1207         "%s ('%c')", val_str,
1208         pinfo->pseudo_header->linux_usb.event_type);
1209
1210     transfer_type = pinfo->pseudo_header->linux_usb.transfer_type;
1211     proto_tree_add_uint(tree, hf_usb_transfer_type, tvb, 0, 0, transfer_type);
1212
1213     if (check_col(pinfo->cinfo, COL_INFO)) {
1214         col_append_str(pinfo->cinfo, COL_INFO,
1215             val_to_str(transfer_type, usb_transfer_type_vals, "Unknown type %x"));
1216     }
1217
1218     proto_tree_add_uint(tree, hf_usb_endpoint_number, tvb, 0, 0,
1219                         pinfo->pseudo_header->linux_usb.endpoint_number);
1220
1221     proto_tree_add_uint(tree, hf_usb_device_address, tvb, 0, 0,
1222                         pinfo->pseudo_header->linux_usb.device_address);
1223
1224     proto_tree_add_uint(tree, hf_usb_bus_id, tvb, 0, 0,
1225                         pinfo->pseudo_header->linux_usb.bus_id);
1226
1227     /* Right after the pseudo header we always have
1228      * sizeof(struct usb_device_setup_hdr)=8 bytes. The content of these
1229      * bytes have only meaning in case setup_flag == 0.
1230      */
1231     if (pinfo->pseudo_header->linux_usb.setup_flag == 0) {
1232         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
1233             0, sizeof(struct usb_device_setup_hdr),
1234             &(pinfo->pseudo_header->linux_usb.setup_flag),
1235             "present (%d)",
1236             pinfo->pseudo_header->linux_usb.setup_flag);
1237     } else {
1238         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
1239             0, sizeof(struct usb_device_setup_hdr),
1240             &(pinfo->pseudo_header->linux_usb.setup_flag),
1241             "not present ('%c')",
1242             pinfo->pseudo_header->linux_usb.setup_flag);
1243     }
1244
1245     if (pinfo->pseudo_header->linux_usb.data_flag == 0) {
1246         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
1247             sizeof(struct usb_device_setup_hdr), -1,
1248             &(pinfo->pseudo_header->linux_usb.data_flag),
1249             "present (%d)",
1250             pinfo->pseudo_header->linux_usb.data_flag);
1251     } else {
1252         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
1253             sizeof(struct usb_device_setup_hdr), -1,
1254             &(pinfo->pseudo_header->linux_usb.data_flag),
1255             "not present ('%c')",
1256             pinfo->pseudo_header->linux_usb.data_flag);
1257     }
1258
1259     /* Timestamp was already processed by libpcap,
1260      * skip it for now:
1261      *   pinfo->pseudo_header->linux_usb.ts_sec
1262      *   pinfo->pseudo_header->linux_usb.ts_usec
1263      */
1264
1265     proto_tree_add_int(tree, hf_usb_urb_status, tvb, 0, 0,
1266                         pinfo->pseudo_header->linux_usb.status);
1267
1268     proto_tree_add_uint(tree, hf_usb_urb_len, tvb, 0, 0,
1269                         pinfo->pseudo_header->linux_usb.urb_len);
1270
1271     proto_tree_add_uint(tree, hf_usb_data_len, tvb,
1272                         sizeof(struct usb_device_setup_hdr),
1273                         pinfo->pseudo_header->linux_usb.data_len,
1274                         pinfo->pseudo_header->linux_usb.data_len);
1275
1276 }
1277
1278 static void
1279 dissect_linux_usb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent)
1280 {
1281     int offset = 0;
1282     int type, endpoint;
1283     guint8 setup_flag;
1284     proto_tree *tree = NULL;
1285     guint32 src_device, dst_device, tmp_addr;
1286     static usb_address_t src_addr, dst_addr; /* has to be static due to SET_ADDRESS */
1287     guint32 src_endpoint, dst_endpoint;
1288     gboolean is_request=FALSE;
1289     usb_conv_info_t *usb_conv_info=NULL;
1290     usb_trans_info_t *usb_trans_info=NULL;
1291     conversation_t *conversation;
1292     usb_tap_data_t *tap_data=NULL;
1293
1294     if (check_col(pinfo->cinfo, COL_PROTOCOL))
1295         col_set_str(pinfo->cinfo, COL_PROTOCOL, "USB");
1296
1297     /* add usb hdr*/
1298     if (parent) {
1299       proto_item *ti = NULL;
1300       ti = proto_tree_add_protocol_format(parent, proto_usb, tvb, 0, sizeof(struct usb_device_setup_hdr), "USB URB");
1301
1302       tree = proto_item_add_subtree(ti, usb_hdr);
1303     }
1304
1305     dissect_linux_usb_pseudo_header(tvb, pinfo, tree);
1306
1307     type = pinfo->pseudo_header->linux_usb.transfer_type;
1308 #if 0
1309     /* The direction flag is broken so we must strip it off */
1310     endpoint=pinfo->pseudo_header->linux_usb.endpoint_number;
1311 #else
1312     endpoint=pinfo->pseudo_header->linux_usb.endpoint_number&(~URB_TRANSFER_IN);
1313 #endif
1314     tmp_addr=pinfo->pseudo_header->linux_usb.device_address;
1315     setup_flag = pinfo->pseudo_header->linux_usb.setup_flag;
1316
1317 #if 0
1318     /* this is how it is supposed to work but this flag seems to be broken -- ronnie */
1319     is_request = endpoint & URB_TRANSFER_IN;
1320 #else
1321     /* Determine whether this is a request or a response */
1322     switch(type){
1323     case URB_BULK:
1324     case URB_CONTROL:
1325     case URB_ISOCHRONOUS:
1326         switch(pinfo->pseudo_header->linux_usb.event_type){
1327         case URB_SUBMIT:
1328             is_request=TRUE;
1329             break;
1330         case URB_COMPLETE:
1331         case URB_ERROR:
1332             is_request=FALSE;
1333             break;
1334         default:
1335             DISSECTOR_ASSERT_NOT_REACHED();
1336         }
1337         break;
1338     case URB_INTERRUPT:
1339         switch(pinfo->pseudo_header->linux_usb.event_type){
1340         case URB_SUBMIT:
1341             is_request=FALSE;
1342             break;
1343         case URB_COMPLETE:
1344         case URB_ERROR:
1345             is_request=TRUE;
1346             break;
1347         default:
1348             DISSECTOR_ASSERT_NOT_REACHED();
1349         }
1350         break;
1351      default:
1352         DISSECTOR_ASSERT_NOT_REACHED();
1353     }
1354 #endif
1355
1356     /* Set up addresses and ports.
1357      * Note that URB_INTERRUPT goes in the reverse direction and thus
1358      * the request comes from the device and not the host.
1359      */
1360     if ( (is_request&&(type!=URB_INTERRUPT))
1361       || (!is_request&&(type==URB_INTERRUPT)) ){
1362         src_addr.device = src_device = 0xffffffff;
1363         src_addr.endpoint = src_endpoint = NO_ENDPOINT;
1364         dst_addr.device = dst_device = htolel(tmp_addr);
1365         dst_addr.endpoint = dst_endpoint = htolel(endpoint);
1366     } else {
1367         src_addr.device = src_device = htolel(tmp_addr);
1368         src_addr.endpoint = src_endpoint = htolel(endpoint);
1369         dst_addr.device = dst_device = 0xffffffff;
1370         dst_addr.endpoint = dst_endpoint = NO_ENDPOINT;
1371     }
1372
1373     SET_ADDRESS(&pinfo->net_src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1374     SET_ADDRESS(&pinfo->src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1375     SET_ADDRESS(&pinfo->net_dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1376     SET_ADDRESS(&pinfo->dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1377     pinfo->ptype=PT_USB;
1378     pinfo->srcport=src_endpoint;
1379     pinfo->destport=dst_endpoint;
1380
1381     conversation=get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, pinfo->srcport, pinfo->destport);
1382
1383     usb_conv_info=get_usb_conv_info(conversation);
1384     pinfo->usb_conv_info=usb_conv_info;
1385
1386
1387     /* request/response matching so we can keep track of transaction specific
1388      * data.
1389      */
1390     if(is_request){
1391         /* this is a request */
1392         usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1393         if(!usb_trans_info){
1394             usb_trans_info=se_alloc0(sizeof(usb_trans_info_t));
1395             usb_trans_info->request_in=pinfo->fd->num;
1396             usb_trans_info->req_time=pinfo->fd->abs_ts;
1397             se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1398         }
1399         usb_conv_info->usb_trans_info=usb_trans_info;
1400
1401         if(usb_trans_info && usb_trans_info->response_in){
1402             proto_item *ti;
1403
1404             ti=proto_tree_add_uint(tree, hf_usb_response_in, tvb, 0, 0, usb_trans_info->response_in);
1405             PROTO_ITEM_SET_GENERATED(ti);
1406         }
1407     } else {
1408         /* this is a response */
1409         if(pinfo->fd->flags.visited){
1410             usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1411         } else {
1412             usb_trans_info=se_tree_lookup32_le(usb_conv_info->transactions, pinfo->fd->num);
1413             if(usb_trans_info){
1414                 usb_trans_info->response_in=pinfo->fd->num;
1415                 se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1416             }
1417         }
1418         usb_conv_info->usb_trans_info=usb_trans_info;
1419
1420         if(usb_trans_info && usb_trans_info->request_in){
1421             proto_item *ti;
1422             nstime_t t, deltat;
1423
1424             ti=proto_tree_add_uint(tree, hf_usb_request_in, tvb, 0, 0, usb_trans_info->request_in);
1425             PROTO_ITEM_SET_GENERATED(ti);
1426
1427             t = pinfo->fd->abs_ts;
1428             nstime_delta(&deltat, &t, &usb_trans_info->req_time);
1429             ti=proto_tree_add_time(tree, hf_usb_time, tvb, 0, 0, &deltat);
1430             PROTO_ITEM_SET_GENERATED(ti);
1431         }
1432     }
1433
1434     /* For DLT189 it seems
1435      * that all INTERRUPT or BULK packets as well as all CONTROL responses
1436      * are prepended with 8 mysterious bytes.
1437      */
1438     switch(type){
1439     case URB_CONTROL:
1440         if(pinfo->pseudo_header->linux_usb.event_type!=URB_SUBMIT){
1441             offset+=8;
1442         }
1443         break;
1444     case URB_BULK:
1445     case URB_ISOCHRONOUS:
1446     case URB_INTERRUPT:
1447         offset+=8;
1448         break;
1449     default:
1450         DISSECTOR_ASSERT_NOT_REACHED();
1451     }
1452
1453     tap_data=ep_alloc(sizeof(usb_tap_data_t));
1454     tap_data->urb_type=(guint8)pinfo->pseudo_header->linux_usb.event_type;
1455     tap_data->transfer_type=(guint8)type;
1456     tap_data->conv_info=usb_conv_info;
1457     tap_data->trans_info=usb_trans_info;
1458     tap_queue_packet(usb_tap, pinfo, tap_data);
1459
1460
1461     switch(type){
1462     case URB_BULK:
1463         {
1464         proto_item *item;
1465
1466         item=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, 0, 0, usb_conv_info->interfaceClass);
1467         PROTO_ITEM_SET_GENERATED(item);
1468         if(tvb_length_remaining(tvb, offset)){
1469             tvbuff_t *next_tvb;
1470
1471             pinfo->usb_conv_info=usb_conv_info;
1472             next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1473             if(dissector_try_port(usb_bulk_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, parent)){
1474                 return;
1475             }
1476         }
1477         }
1478         break;
1479     case URB_CONTROL:
1480         {
1481         const usb_setup_dissector_table_t *tmp;
1482         usb_setup_dissector dissector;
1483         proto_item *ti = NULL;
1484         proto_tree *setup_tree = NULL;
1485         int type;
1486
1487         ti=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, offset, 0, usb_conv_info->interfaceClass);
1488         PROTO_ITEM_SET_GENERATED(ti);
1489
1490         if(is_request){
1491             if (setup_flag == 0) {
1492                 tvbuff_t *next_tvb;
1493
1494                 /* this is a request */
1495                 ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, sizeof(struct usb_device_setup_hdr), "URB setup");
1496                 setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
1497                 usb_trans_info->requesttype=tvb_get_guint8(tvb, offset);
1498                 offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type);
1499
1500
1501                 /* read the request code and spawn off to a class specific
1502                  * dissector if found
1503                  */
1504                 usb_trans_info->request=tvb_get_guint8(tvb, offset);
1505
1506                 switch (type) {
1507
1508                 case RQT_SETUP_TYPE_STANDARD:
1509                     /*
1510                      * This is a standard request which is managed by this
1511                      * dissector
1512                      */
1513                     proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
1514                     offset += 1;
1515
1516                     if (check_col(pinfo->cinfo, COL_INFO)) {
1517                         col_clear(pinfo->cinfo, COL_INFO);
1518                         col_append_fstr(pinfo->cinfo, COL_INFO, "%s Request",
1519                              val_to_str(usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
1520                     }
1521
1522                     dissector=NULL;
1523                     for(tmp=setup_dissectors;tmp->dissector;tmp++){
1524                         if(tmp->request==usb_trans_info->request){
1525                             dissector=tmp->dissector;
1526                             break;
1527                         }
1528                     }
1529
1530                     if(dissector){
1531                         dissector(pinfo, setup_tree, tvb, offset, is_request, usb_trans_info, usb_conv_info);
1532                         offset+=6;
1533                     } else {
1534                         proto_tree_add_item(setup_tree, hf_usb_value, tvb, offset, 2, TRUE);
1535                         offset += 2;
1536                         proto_tree_add_item(setup_tree, hf_usb_index, tvb, offset, 2, TRUE);
1537                         offset += 2;
1538                         proto_tree_add_item(setup_tree, hf_usb_length, tvb, offset, 2, TRUE);
1539                         offset += 2;
1540                     }
1541                     break;
1542
1543                 case RQT_SETUP_TYPE_CLASS:
1544                     /* Try to find a class specific dissector */
1545                     next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1546                     if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
1547                         return;
1548                     /* XXX - dump as hex */
1549                     }
1550                     break;
1551                 }
1552             } else {
1553                 offset += 8;
1554             }
1555         } else {
1556             tvbuff_t *next_tvb;
1557
1558             /* this is a response */
1559             if(usb_conv_info->usb_trans_info){
1560                 /* Try to find a class specific dissector */
1561                 next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1562                 if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
1563                     return;
1564                 }
1565
1566                 if (check_col(pinfo->cinfo, COL_INFO)) {
1567                     col_clear(pinfo->cinfo, COL_INFO);
1568                     col_append_fstr(pinfo->cinfo, COL_INFO, "%s Response",
1569                         val_to_str(usb_conv_info->usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
1570                 }
1571
1572                 dissector=NULL;
1573                 for(tmp=setup_dissectors;tmp->dissector;tmp++){
1574                     if(tmp->request==usb_conv_info->usb_trans_info->request){
1575                         dissector=tmp->dissector;
1576                         break;
1577                     }
1578                 }
1579
1580                 if(dissector){
1581                     dissector(pinfo, tree, tvb, offset, is_request, usb_conv_info->usb_trans_info, usb_conv_info);
1582                 }
1583             } else {
1584                 /* no matching request available */
1585                 ;
1586             }
1587         }
1588         }
1589         break;
1590     default:
1591         /* dont know */
1592         if (setup_flag == 0) {
1593             proto_item *ti = NULL;
1594             proto_tree *setup_tree = NULL;
1595             guint8 requesttype, request;
1596             int type;
1597
1598             ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, sizeof(struct usb_device_setup_hdr), "URB setup");
1599             setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
1600
1601
1602             requesttype=tvb_get_guint8(tvb, offset);
1603             offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type);
1604
1605             request=tvb_get_guint8(tvb, offset);
1606             proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
1607             offset += 1;
1608
1609             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
1610             offset += 2;
1611             proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
1612             offset += 2;
1613             proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1614             offset += 2;
1615         } else {
1616             offset += 8;
1617         }
1618         break;
1619     }
1620
1621     proto_tree_add_item(tree, hf_usb_data, tvb, offset, -1, FALSE);
1622 }
1623
1624 void
1625 proto_register_usb(void)
1626 {
1627     static hf_register_info hf[] = {
1628
1629     /* USB packet pseudoheader members */
1630         { &hf_usb_urb_id,
1631         { "URB id", "usb.urb_id", FT_UINT64, BASE_HEX,
1632                 NULL, 0x0,
1633                 "URB id", HFILL }},
1634
1635         { &hf_usb_urb_type,
1636         { "URB type", "usb.urb_type", FT_STRING, BASE_NONE,
1637                 NULL, 0x0,
1638                 "URB type", HFILL }},
1639
1640         { &hf_usb_transfer_type,
1641         { "URB transfer type", "usb.transfer_type", FT_UINT8, BASE_DEC,
1642                 VALS(usb_transfer_type_vals), 0x0,
1643                 "URB transfer type", HFILL }},
1644
1645         { &hf_usb_endpoint_number,
1646         { "Endpoint", "usb.endpoint_number", FT_UINT8, BASE_HEX, NULL, 0x0,
1647                 "USB endpoint number", HFILL }},
1648
1649         { &hf_usb_device_address,
1650         { "Device", "usb.device_address", FT_UINT8, BASE_DEC, NULL, 0x0,
1651                 "USB device address", HFILL }},
1652
1653         { &hf_usb_bus_id,
1654         { "URB bus id", "usb.bus_id", FT_UINT16, BASE_DEC,
1655                 NULL, 0x0,
1656                 "URB bus id", HFILL }},
1657
1658         { &hf_usb_setup_flag,
1659         { "Device setup request", "usb.setup_flag", FT_STRING, BASE_NONE,
1660                  NULL, 0x0,
1661                  "USB device setup request is present (0) or not", HFILL }},
1662
1663         { &hf_usb_data_flag,
1664         { "Data", "usb.data_flag", FT_STRING, BASE_NONE,
1665                  NULL, 0x0,
1666                  "USB data is present (0) or not", HFILL }},
1667
1668         { &hf_usb_urb_status,
1669         { "URB status", "usb.urb_status", FT_INT32, BASE_DEC,
1670                 VALS(usb_urb_status_vals), 0x0,
1671                 "URB status", HFILL }},
1672
1673         { &hf_usb_urb_len,
1674         { "URB length [bytes]", "usb.urb_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1675                 "URB length in bytes", HFILL }},
1676
1677         { &hf_usb_data_len,
1678         { "Data length [bytes]", "usb.data_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1679                 "URB data length in bytes", HFILL }},
1680
1681     /* Generated values */
1682         { &hf_usb_src_endpoint_number,
1683         { "Src Endpoint", "usb.src.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
1684                 "Source USB endpoint number", HFILL }},
1685
1686         { &hf_usb_dst_endpoint_number,
1687         { "Dst Endpoint", "usb.dst.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
1688                 "Destination USB endpoint number", HFILL }},
1689
1690     /* Fields from usb20.pdf, Table 9-2 'Format of Setup Data' */
1691         { &hf_usb_bmRequestType,
1692         { "bmRequestType", "usb.bmRequestType", FT_UINT8, BASE_HEX, NULL, 0x0,
1693                 "", HFILL }},
1694
1695         { &hf_usb_request,
1696         { "bRequest", "usb.setup.bRequest", FT_UINT8, BASE_HEX, VALS(setup_request_names_vals), 0x0,
1697                 "", HFILL }},
1698
1699         { &hf_usb_value,
1700         { "wValue", "usb.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
1701                 "", HFILL }},
1702
1703         { &hf_usb_index,
1704         { "wIndex", "usb.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
1705                 "", HFILL }},
1706
1707         { &hf_usb_length,
1708         { "wLength", "usb.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
1709                 "", HFILL }},
1710
1711     /* --------------------------------- */
1712         { &hf_usb_data,
1713         {"Application Data", "usb.data",
1714             FT_BYTES, BASE_HEX, NULL, 0x0,
1715             "Payload is application data", HFILL }},
1716
1717         { &hf_usb_bmRequestType_direction,
1718         { "Direction", "usb.bmRequestType.direction", FT_BOOLEAN, 8,
1719           TFS(&tfs_bmrequesttype_direction), USB_DIR_IN, "", HFILL }},
1720
1721         { &hf_usb_bmRequestType_type,
1722         { "Type", "usb.bmRequestType.type", FT_UINT8, BASE_HEX,
1723           VALS(bmrequesttype_type_vals), USB_TYPE_MASK, "", HFILL }},
1724
1725         { &hf_usb_bmRequestType_recipient,
1726         { "Recipient", "usb.bmRequestType.recipient", FT_UINT8, BASE_HEX,
1727           VALS(bmrequesttype_recipient_vals), 0x1f, "", HFILL }},
1728
1729         { &hf_usb_bDescriptorType,
1730         { "bDescriptorType", "usb.bDescriptorType", FT_UINT8, BASE_HEX,
1731           VALS(descriptor_type_vals), 0x0, "", HFILL }},
1732
1733         { &hf_usb_descriptor_index,
1734         { "Descriptor Index", "usb.DescriptorIndex", FT_UINT8, BASE_HEX,
1735           NULL, 0x0, "", HFILL }},
1736
1737         { &hf_usb_language_id,
1738         { "Language Id", "usb.LanguageId", FT_UINT16, BASE_HEX,
1739           VALS(usb_langid_vals), 0x0, "", HFILL }},
1740
1741         { &hf_usb_bLength,
1742         { "bLength", "usb.bLength", FT_UINT8, BASE_DEC,
1743           NULL, 0x0, "", HFILL }},
1744
1745         { &hf_usb_bcdUSB,
1746         { "bcdUSB", "usb.bcdUSB", FT_UINT16, BASE_HEX,
1747           NULL, 0x0, "", HFILL }},
1748
1749         { &hf_usb_bDeviceClass,
1750         { "bDeviceClass", "usb.bDeviceClass", FT_UINT8, BASE_DEC,
1751           NULL, 0x0, "", HFILL }},
1752
1753         { &hf_usb_bDeviceSubClass,
1754         { "bDeviceSubClass", "usb.bDeviceSubClass", FT_UINT8, BASE_DEC,
1755           NULL, 0x0, "", HFILL }},
1756
1757         { &hf_usb_bDeviceProtocol,
1758         { "bDeviceProtocol", "usb.bDeviceProtocol", FT_UINT8, BASE_DEC,
1759           NULL, 0x0, "", HFILL }},
1760
1761         { &hf_usb_bMaxPacketSize0,
1762         { "bMaxPacketSize0", "usb.bMaxPacketSize0", FT_UINT8, BASE_DEC,
1763           NULL, 0x0, "", HFILL }},
1764
1765         { &hf_usb_idVendor,
1766         { "idVendor", "usb.idVendor", FT_UINT16, BASE_HEX,
1767           NULL, 0x0, "", HFILL }},
1768
1769         { &hf_usb_idProduct,
1770         { "idProduct", "usb.idProduct", FT_UINT16, BASE_HEX,
1771           NULL, 0x0, "", HFILL }},
1772
1773         { &hf_usb_bcdDevice,
1774         { "bcdDevice", "usb.bcdDevice", FT_UINT16, BASE_HEX,
1775           NULL, 0x0, "", HFILL }},
1776
1777         { &hf_usb_iManufacturer,
1778         { "iManufacturer", "usb.iManufacturer", FT_UINT8, BASE_DEC,
1779           NULL, 0x0, "", HFILL }},
1780
1781         { &hf_usb_iProduct,
1782         { "iProduct", "usb.iProduct", FT_UINT8, BASE_DEC,
1783           NULL, 0x0, "", HFILL }},
1784
1785         { &hf_usb_iSerialNumber,
1786         { "iSerialNumber", "usb.iSerialNumber", FT_UINT8, BASE_DEC,
1787           NULL, 0x0, "", HFILL }},
1788
1789         { &hf_usb_bNumConfigurations,
1790         { "bNumConfigurations", "usb.bNumConfigurations", FT_UINT8, BASE_DEC,
1791           NULL, 0x0, "", HFILL }},
1792
1793         { &hf_usb_wLANGID,
1794         { "wLANGID", "usb.wLANGID", FT_UINT16, BASE_HEX,
1795           VALS(usb_langid_vals), 0x0, "", HFILL }},
1796
1797         { &hf_usb_bString,
1798         { "bString", "usb.bString", FT_STRING, BASE_NONE,
1799           NULL, 0x0, "", HFILL }},
1800
1801         { &hf_usb_bInterfaceNumber,
1802         { "bInterfaceNumber", "usb.bInterfaceNumber", FT_UINT8, BASE_DEC,
1803           NULL, 0x0, "", HFILL }},
1804
1805         { &hf_usb_bAlternateSetting,
1806         { "bAlternateSetting","usb.bAlternateSetting", FT_UINT8, BASE_DEC,
1807           NULL, 0x0, "", HFILL }},
1808
1809         { &hf_usb_bNumEndpoints,
1810         { "bNumEndpoints","usb.bNumEndpoints", FT_UINT8, BASE_DEC,
1811           NULL, 0x0, "", HFILL }},
1812
1813         { &hf_usb_bInterfaceClass,
1814         { "bInterfaceClass", "usb.bInterfaceClass", FT_UINT8, BASE_HEX,
1815           VALS(usb_interfaceclass_vals), 0x0, "", HFILL }},
1816
1817         { &hf_usb_bInterfaceSubClass,
1818         { "bInterfaceSubClass", "usb.bInterfaceSubClass", FT_UINT8, BASE_HEX,
1819           NULL, 0x0, "", HFILL }},
1820
1821         { &hf_usb_bInterfaceProtocol,
1822         { "bInterfaceProtocol", "usb.bInterfaceProtocol", FT_UINT8, BASE_HEX,
1823           NULL, 0x0, "", HFILL }},
1824
1825         { &hf_usb_iInterface,
1826         { "iInterface", "usb.iInterface", FT_UINT8, BASE_DEC,
1827           NULL, 0x0, "", HFILL }},
1828
1829         { &hf_usb_bEndpointAddress,
1830         { "bEndpointAddress", "usb.bEndpointAddress", FT_UINT8, BASE_HEX,
1831           NULL, 0x0, "", HFILL }},
1832
1833         { &hf_usb_configuration_bmAttributes,
1834         { "Configuration bmAttributes", "usb.configuration.bmAttributes", FT_UINT8, BASE_HEX,
1835           NULL, 0x0, "", HFILL }},
1836
1837         { &hf_usb_bmAttributes,
1838         { "bmAttributes", "usb.bmAttributes", FT_UINT8, BASE_HEX,
1839           NULL, 0x0, "", HFILL }},
1840
1841         { &hf_usb_bEndpointAttributeTransfer,
1842         { "Transfertype", "usb.bmAttributes.transfer", FT_UINT8, BASE_HEX,
1843           VALS(usb_bmAttributes_transfer_vals), 0x03, "", HFILL }},
1844
1845         { &hf_usb_bEndpointAttributeSynchonisation,
1846         { "Synchronisationtype", "usb.bmAttributes.sync", FT_UINT8, BASE_HEX,
1847           VALS(usb_bmAttributes_sync_vals), 0x0c, "", HFILL }},
1848
1849         { &hf_usb_bEndpointAttributeBehaviour,
1850         { "Behaviourtype", "usb.bmAttributes.behaviour", FT_UINT8, BASE_HEX,
1851           VALS(usb_bmAttributes_behaviour_vals), 0x30, "", HFILL }},
1852
1853         { &hf_usb_wMaxPacketSize,
1854         { "wMaxPacketSize", "usb.wMaxPacketSize", FT_UINT16, BASE_DEC,
1855           NULL, 0x0, "", HFILL }},
1856
1857         { &hf_usb_bInterval,
1858         { "bInterval", "usb.bInterval", FT_UINT8, BASE_DEC,
1859           NULL, 0x0, "", HFILL }},
1860
1861         { &hf_usb_wTotalLength,
1862         { "wTotalLength", "usb.wTotalLength", FT_UINT16, BASE_DEC,
1863           NULL, 0x0, "", HFILL }},
1864
1865         { &hf_usb_bNumInterfaces,
1866         { "bNumInterfaces", "usb.bNumInterfaces", FT_UINT8, BASE_DEC,
1867           NULL, 0x0, "", HFILL }},
1868
1869         { &hf_usb_bConfigurationValue,
1870         { "bConfigurationValue", "usb.bConfigurationValue", FT_UINT8, BASE_DEC,
1871           NULL, 0x0, "", HFILL }},
1872
1873         { &hf_usb_iConfiguration,
1874         { "iConfiguration", "usb.iConfiguration", FT_UINT8, BASE_DEC,
1875           NULL, 0x0, "", HFILL }},
1876
1877         { &hf_usb_bMaxPower,
1878         { "bMaxPower", "usb.bMaxPower", FT_UINT8, BASE_DEC,
1879           NULL, 0x0, "", HFILL }},
1880
1881         { &hf_usb_configuration_legacy10buspowered,
1882         { "Must be 1", "usb.configuration.legacy10buspowered", FT_BOOLEAN, 8,
1883           TFS(&tfs_mustbeone), 0x80, "Legacy USB 1.0 bus powered", HFILL }},
1884
1885         { &hf_usb_configuration_selfpowered,
1886         { "Self-Powered", "usb.configuration.selfpowered", FT_BOOLEAN, 8,
1887           TFS(&tfs_selfpowered), 0x40, "", HFILL }},
1888
1889         { &hf_usb_configuration_remotewakeup,
1890         { "Remote Wakeup", "usb.configuration.remotewakeup", FT_BOOLEAN, 8,
1891           TFS(&tfs_remotewakeup), 0x20, "", HFILL }},
1892
1893         { &hf_usb_bEndpointAddress_number,
1894         { "Endpoint Number", "usb.bEndpointAddress.number", FT_UINT8, BASE_HEX,
1895           NULL, 0x0f, "", HFILL }},
1896
1897         { &hf_usb_bEndpointAddress_direction,
1898         { "Direction", "usb.bEndpointAddress.direction", FT_BOOLEAN, 8,
1899           TFS(&tfs_endpoint_direction), 0x80, "", HFILL }},
1900
1901         { &hf_usb_request_in,
1902                 { "Request in", "usb.request_in", FT_FRAMENUM, BASE_NONE,
1903                 NULL, 0, "The request to this packet is in this packet", HFILL }},
1904
1905         { &hf_usb_time,
1906                 { "Time from request", "usb.time", FT_RELATIVE_TIME, BASE_NONE,
1907                 NULL, 0, "Time between Request and Response for USB cmds", HFILL }},
1908
1909         { &hf_usb_response_in,
1910                 { "Response in", "usb.response_in", FT_FRAMENUM, BASE_NONE,
1911                 NULL, 0, "The response to this packet is in this packet", HFILL }},
1912
1913     };
1914
1915     static gint *usb_subtrees[] = {
1916             &usb_hdr,
1917             &usb_setup_hdr,
1918             &ett_usb_setup_bmrequesttype,
1919             &ett_descriptor_device,
1920             &ett_configuration_bmAttributes,
1921             &ett_configuration_bEndpointAddress,
1922             &ett_endpoint_bmAttributes
1923     };
1924
1925
1926     proto_usb = proto_register_protocol("USB", "USB", "usb");
1927     proto_register_field_array(proto_usb, hf, array_length(hf));
1928     proto_register_subtree_array(usb_subtrees, array_length(usb_subtrees));
1929
1930     usb_bulk_dissector_table = register_dissector_table("usb.bulk",
1931         "USB bulk endpoint", FT_UINT8, BASE_DEC);
1932
1933     usb_control_dissector_table = register_dissector_table("usb.control",
1934         "USB control endpoint", FT_UINT8, BASE_DEC);
1935
1936     usb_tap=register_tap("usb");
1937 }
1938
1939 void
1940 proto_reg_handoff_usb(void)
1941 {
1942     dissector_handle_t linux_usb_handle;
1943
1944     linux_usb_handle = create_dissector_handle(dissect_linux_usb, proto_usb);
1945
1946     dissector_add("wtap_encap", WTAP_ENCAP_USB_LINUX, linux_usb_handle);
1947 }