Define all the standard request code values from the USB 2.0 spec, and
[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         offset += len-2;
739     }
740
741     if(item){
742         proto_item_set_len(item, offset-old_offset);
743     }
744
745     return offset;
746 }
747
748
749
750 /* 9.6.5 */
751 static int
752 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)
753 {
754     proto_item *item=NULL;
755     proto_tree *tree=NULL;
756     int old_offset=offset;
757
758     if(parent_tree){
759         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "INTERFACE DESCRIPTOR");
760         tree=proto_item_add_subtree(item, ett_descriptor_device);
761     }
762
763     /* bLength */
764     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
765     offset++;
766
767     /* bDescriptorType */
768     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
769     offset++;
770
771     /* bInterfaceNumber */
772     proto_tree_add_item(tree, hf_usb_bInterfaceNumber, tvb, offset, 1, TRUE);
773     offset++;
774
775     /* bAlternateSetting */
776     proto_tree_add_item(tree, hf_usb_bAlternateSetting, tvb, offset, 1, TRUE);
777     offset++;
778
779     /* bNumEndpoints */
780     proto_tree_add_item(tree, hf_usb_bNumEndpoints, tvb, offset, 1, TRUE);
781     offset++;
782
783     /* bInterfaceClass */
784     proto_tree_add_item(tree, hf_usb_bInterfaceClass, tvb, offset, 1, TRUE);
785     /* save the class so we can access it later in the endpoint descriptor */
786     usb_conv_info->interfaceClass=tvb_get_guint8(tvb, offset);
787     if(!pinfo->fd->flags.visited){
788         usb_trans_info->interface_info=se_alloc0(sizeof(usb_conv_info_t));
789         usb_trans_info->interface_info->interfaceClass=tvb_get_guint8(tvb, offset);
790         usb_trans_info->interface_info->transactions=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "usb transactions");
791     }
792     offset++;
793
794     /* bInterfaceSubClass */
795     proto_tree_add_item(tree, hf_usb_bInterfaceSubClass, tvb, offset, 1, TRUE);
796     offset++;
797
798     /* bInterfaceProtocol */
799     proto_tree_add_item(tree, hf_usb_bInterfaceProtocol, tvb, offset, 1, TRUE);
800     offset++;
801
802     /* iInterface */
803     proto_tree_add_item(tree, hf_usb_iInterface, tvb, offset, 1, TRUE);
804     offset++;
805
806     if(item){
807         proto_item_set_len(item, offset-old_offset);
808     }
809
810     return offset;
811 }
812
813 /* 9.6.6 */
814 static const true_false_string tfs_endpoint_direction = {
815     "IN Endpoint",
816     "OUT Endpoint"
817 };
818 static int
819 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_)
820 {
821     proto_item *item=NULL;
822     proto_tree *tree=NULL;
823     proto_item *endpoint_item=NULL;
824     proto_tree *endpoint_tree=NULL;
825     proto_item *ep_attrib_item=NULL;
826     proto_tree *ep_attrib_tree=NULL;
827     int old_offset=offset;
828     guint8 endpoint;
829
830     if(parent_tree){
831         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "ENDPOINT DESCRIPTOR");
832         tree=proto_item_add_subtree(item, ett_descriptor_device);
833     }
834
835     /* bLength */
836     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
837     offset++;
838
839     /* bDescriptorType */
840     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
841     offset++;
842
843     /* bEndpointAddress */
844     if(tree){
845         endpoint_item=proto_tree_add_item(tree, hf_usb_bEndpointAddress, tvb, offset, 1, TRUE);
846         endpoint_tree=proto_item_add_subtree(endpoint_item, ett_configuration_bEndpointAddress);
847     }
848     endpoint=tvb_get_guint8(tvb, offset)&0x0f;
849     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_direction, tvb, offset, 1, TRUE);
850     proto_item_append_text(endpoint_item, "  %s", (tvb_get_guint8(tvb, offset)&0x80)?"IN":"OUT");
851     proto_tree_add_item(endpoint_tree, hf_usb_bEndpointAddress_number, tvb, offset, 1, TRUE);
852     proto_item_append_text(endpoint_item, "  Endpoint:%d", endpoint);
853     offset++;
854
855     /* Together with class from the interface descriptor we know what kind
856      * of class the device at endpoint is.
857      * Make sure a conversation exists for this endpoint and attach a
858      * usb_conv_into_t structure to it.
859      *
860      * All endpoints for the same interface descriptor share the same
861      * usb_conv_info structure.
862      */
863     if((!pinfo->fd->flags.visited)&&usb_trans_info->interface_info){
864         conversation_t *conversation;
865
866         if(pinfo->destport==NO_ENDPOINT){
867             static address tmp_addr;
868             static usb_address_t usb_addr;
869
870             /* Create a new address structure that points to the same device
871              * but the new endpoint.
872              */
873             usb_addr.device=((usb_address_t *)(pinfo->src.data))->device;
874             usb_addr.endpoint=endpoint;
875             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
876             conversation=get_usb_conversation(pinfo, &tmp_addr, &pinfo->dst, endpoint, pinfo->destport);
877         } else {
878             static address tmp_addr;
879             static usb_address_t usb_addr;
880
881             /* Create a new address structure that points to the same device
882              * but the new endpoint.
883              */
884             usb_addr.device=((usb_address_t *)(pinfo->dst.data))->device;
885             usb_addr.endpoint=endpoint;
886             SET_ADDRESS(&tmp_addr, AT_USB, USB_ADDR_LEN, (char *)&usb_addr);
887             conversation=get_usb_conversation(pinfo, &pinfo->src, &tmp_addr, pinfo->srcport, endpoint);
888         }
889
890         conversation_add_proto_data(conversation, proto_usb, usb_trans_info->interface_info);
891     }
892
893     /* bmAttributes */
894     if (tree) {
895         ep_attrib_item=proto_tree_add_item(tree, hf_usb_bmAttributes, tvb, offset, 1, TRUE);
896         ep_attrib_tree=proto_item_add_subtree(ep_attrib_item, ett_endpoint_bmAttributes);
897     }
898     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeTransfer, tvb, offset, 1, TRUE);
899     /* isochronous only */
900     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeSynchonisation, tvb, offset, 1, TRUE);
901     /* isochronous only */
902     proto_tree_add_item(ep_attrib_tree, hf_usb_bEndpointAttributeBehaviour, tvb, offset, 1, TRUE);
903     offset++;
904
905     /* wMaxPacketSize */
906     proto_tree_add_item(tree, hf_usb_wMaxPacketSize, tvb, offset, 2, TRUE);
907     offset+=2;
908
909     /* bInterval */
910     proto_tree_add_item(tree, hf_usb_bInterval, tvb, offset, 1, TRUE);
911     offset++;
912
913     if(item){
914         proto_item_set_len(item, offset-old_offset);
915     }
916
917     return offset;
918 }
919
920 static int
921 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_)
922 {
923     proto_item *item=NULL;
924     proto_tree *tree=NULL;
925     int old_offset=offset;
926     guint8 bLength;
927
928     if(parent_tree){
929         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "UNKNOWN DESCRIPTOR");
930         tree=proto_item_add_subtree(item, ett_descriptor_device);
931     }
932
933     /* bLength */
934     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
935     bLength = tvb_get_guint8(tvb, offset);
936     offset++;
937     if (bLength < 3) {
938         item = proto_tree_add_text(parent_tree, tvb, offset - 1, 1,
939             "Invalid bLength: %u",  bLength);
940         expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR,
941             "Invalid bLength: %u",  bLength);
942         return offset;
943     }
944
945     /* bDescriptorType */
946     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
947     offset++;
948
949     offset += bLength - 2;
950
951     if(item){
952         proto_item_set_len(item, offset-old_offset);
953     }
954
955     return offset;
956 }
957
958 /* 9.6.3 */
959 static const true_false_string tfs_mustbeone = {
960     "Must be 1 for USB 1.1 and higher",
961     "FIXME: Is this a USB 1.0 device"
962 };
963 static const true_false_string tfs_selfpowered = {
964     "This device is SELF-POWERED",
965     "This device is powered from the USB bus"
966 };
967 static const true_false_string tfs_remotewakeup = {
968     "This device supports REMOTE WAKEUP",
969     "This device does NOT support remote wakeup"
970 };
971 static int
972 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)
973 {
974     proto_item *item=NULL;
975     proto_tree *tree=NULL;
976     int old_offset=offset;
977     guint16 len;
978     proto_item *flags_item=NULL;
979     proto_tree *flags_tree=NULL;
980     guint8 flags;
981     proto_item *power_item=NULL;
982     guint8 power;
983
984     if(parent_tree){
985         item=proto_tree_add_text(parent_tree, tvb, offset, 0, "CONFIGURATION DESCRIPTOR");
986         tree=proto_item_add_subtree(item, ett_descriptor_device);
987     }
988
989     /* bLength */
990     proto_tree_add_item(tree, hf_usb_bLength, tvb, offset, 1, TRUE);
991     offset++;
992
993     /* bDescriptorType */
994     proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
995     offset++;
996
997     /* wTotalLength */
998     proto_tree_add_item(tree, hf_usb_wTotalLength, tvb, offset, 2, TRUE);
999     len=tvb_get_letohs(tvb, offset);
1000     offset+=2;
1001
1002     /* bNumInterfaces */
1003     proto_tree_add_item(tree, hf_usb_bNumInterfaces, tvb, offset, 1, TRUE);
1004     offset++;
1005
1006     /* bConfigurationValue */
1007     proto_tree_add_item(tree, hf_usb_bConfigurationValue, tvb, offset, 1, TRUE);
1008     offset++;
1009
1010     /* iConfiguration */
1011     proto_tree_add_item(tree, hf_usb_iConfiguration, tvb, offset, 1, TRUE);
1012     offset++;
1013
1014     /* bmAttributes */
1015     if(tree){
1016         flags_item=proto_tree_add_item(tree, hf_usb_configuration_bmAttributes, tvb, offset, 1, TRUE);
1017         flags_tree=proto_item_add_subtree(flags_item, ett_configuration_bmAttributes);
1018     }
1019     flags=tvb_get_guint8(tvb, offset);
1020     proto_tree_add_item(flags_tree, hf_usb_configuration_legacy10buspowered, tvb, offset, 1, TRUE);
1021     proto_tree_add_item(flags_tree, hf_usb_configuration_selfpowered, tvb, offset, 1, TRUE);
1022     proto_item_append_text(flags_item, "  %sSELF-POWERED", (flags&0x40)?"":"NOT ");
1023     flags&=~0x40;
1024     proto_tree_add_item(flags_tree, hf_usb_configuration_remotewakeup, tvb, offset, 1, TRUE);
1025     proto_item_append_text(flags_item, "  %sREMOTE-WAKEUP", (flags&0x20)?"":"NO ");
1026     flags&=~0x20;
1027     offset++;
1028
1029     /* bMaxPower */
1030     power_item=proto_tree_add_item(tree, hf_usb_bMaxPower, tvb, offset, 1, TRUE);
1031     power=tvb_get_guint8(tvb, offset);
1032     proto_item_append_text(power_item, "  (%dmA)", power*2);
1033     offset++;
1034
1035     /* initialize interface_info to NULL */
1036     usb_trans_info->interface_info=NULL;
1037
1038     /* decode any additional interface and endpoint descriptors */
1039     while(len>(old_offset-offset)){
1040         guint8 next_type;
1041
1042         if(tvb_length_remaining(tvb, offset)<2){
1043             break;
1044         }
1045         next_type=tvb_get_guint8(tvb, offset+1);
1046         switch(next_type){
1047         case USB_DT_INTERFACE:
1048             offset=dissect_usb_interface_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1049             break;
1050         case USB_DT_ENDPOINT:
1051             offset=dissect_usb_endpoint_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1052             break;
1053         default:
1054             offset=dissect_usb_unknown_descriptor(pinfo, parent_tree, tvb, offset, usb_trans_info, usb_conv_info);
1055             break;
1056             /* was: return offset; */
1057         }
1058     }
1059
1060     if(item){
1061         proto_item_set_len(item, offset-old_offset);
1062     }
1063
1064     return offset;
1065 }
1066
1067
1068 static int
1069 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)
1070 {
1071     if(is_request){
1072         /* descriptor index */
1073         proto_tree_add_item(tree, hf_usb_descriptor_index, tvb, offset, 1, TRUE);
1074         usb_trans_info->u.get_descriptor.index=tvb_get_guint8(tvb, offset);
1075         offset++;
1076
1077         /* descriptor type */
1078         proto_tree_add_item(tree, hf_usb_bDescriptorType, tvb, offset, 1, TRUE);
1079         usb_trans_info->u.get_descriptor.type=tvb_get_guint8(tvb, offset);
1080         offset++;
1081         if (check_col(pinfo->cinfo, COL_INFO)) {
1082             col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1083                 val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %x"));
1084         }
1085
1086         /* language id */
1087         proto_tree_add_item(tree, hf_usb_language_id, tvb, offset, 2, TRUE);
1088         offset+=2;
1089
1090         /* length */
1091         proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1092         offset += 2;
1093     } else {
1094         if (check_col(pinfo->cinfo, COL_INFO)) {
1095             col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1096                 val_to_str(usb_trans_info->u.get_descriptor.type, descriptor_type_vals, "Unknown type %x"));
1097         }
1098         switch(usb_trans_info->u.get_descriptor.type){
1099         case USB_DT_DEVICE:
1100             offset=dissect_usb_device_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1101             break;
1102         case USB_DT_CONFIG:
1103             offset=dissect_usb_configuration_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1104             break;
1105         case USB_DT_STRING:
1106             offset=dissect_usb_string_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1107             break;
1108         case USB_DT_INTERFACE:
1109             offset=dissect_usb_interface_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1110             break;
1111         case USB_DT_ENDPOINT:
1112             offset=dissect_usb_endpoint_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1113             break;
1114         case USB_DT_DEVICE_QUALIFIER:
1115             offset=dissect_usb_device_qualifier_descriptor(pinfo, tree, tvb, offset, usb_trans_info, usb_conv_info);
1116             break;
1117         default:
1118             /* XXX dissect the descriptor coming back from the device */
1119             proto_tree_add_text(tree, tvb, offset, -1, "GET DESCRIPTOR data");
1120             offset += tvb_length_remaining(tvb, offset);
1121             break;
1122         }
1123     }
1124     return offset;
1125 }
1126
1127
1128
1129
1130 typedef int (*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);
1131
1132 typedef struct _usb_setup_dissector_table_t {
1133     guint8 request;
1134     usb_setup_dissector dissector;
1135 } usb_setup_dissector_table_t;
1136 #define USB_SETUP_GET_STATUS            0
1137 #define USB_SETUP_CLEAR_FEATURE         1
1138 #define USB_SETUP_SET_FEATURE           2
1139 #define USB_SETUP_SET_ADDRESS           5
1140 #define USB_SETUP_GET_DESCRIPTOR        6
1141 #define USB_SETUP_SET_DESCRIPTOR        7
1142 #define USB_SETUP_GET_CONFIGURATION     8
1143 #define USB_SETUP_SET_CONFIGURATION     9
1144 #define USB_SETUP_GET_INTERFACE         10
1145 #define USB_SETUP_SET_INTERFACE         11
1146 #define USB_SETUP_SYNCH_FRAME           12
1147 static const usb_setup_dissector_table_t setup_dissectors[] = {
1148     {USB_SETUP_GET_DESCRIPTOR,  dissect_usb_setup_get_descriptor},
1149     {0, NULL}
1150 };
1151 static const value_string setup_request_names_vals[] = {
1152     {USB_SETUP_GET_STATUS,              "GET STATUS"},
1153     {USB_SETUP_CLEAR_FEATURE,           "CLEAR FEATURE"},
1154     {USB_SETUP_SET_FEATURE,             "SET FEATURE"},
1155     {USB_SETUP_SET_ADDRESS,             "SET ADDRESS"},
1156     {USB_SETUP_GET_DESCRIPTOR,          "GET DESCRIPTOR"},
1157     {USB_SETUP_SET_DESCRIPTOR,          "SET DESCRIPTOR"},
1158     {USB_SETUP_GET_CONFIGURATION,       "GET CONFIGURATION"},
1159     {USB_SETUP_SET_CONFIGURATION,       "SET CONFIGURATION"},
1160     {USB_SETUP_GET_INTERFACE,           "GET INTERFACE"},
1161     {USB_SETUP_SET_INTERFACE,           "SET INTERFACE"},
1162     {USB_SETUP_SYNCH_FRAME,             "SYNCH FRAME"},
1163     {0, NULL}
1164 };
1165
1166
1167 #define USB_DIR_OUT                     0               /* to device */
1168 #define USB_DIR_IN                      0x80            /* to host */
1169
1170 static const true_false_string tfs_bmrequesttype_direction = {
1171         "Device-to-host",
1172         "Host-to-device"
1173 };
1174
1175 #define USB_TYPE_MASK                   (0x03 << 5)
1176 #define RQT_SETUP_TYPE_STANDARD 0
1177 #define RQT_SETUP_TYPE_CLASS    1
1178 #define RQT_SETUP_TYPE_VENDOR   2
1179
1180 static const value_string bmrequesttype_type_vals[] = {
1181     {RQT_SETUP_TYPE_STANDARD, "Standard"},
1182     {RQT_SETUP_TYPE_CLASS,    "Class"},
1183     {RQT_SETUP_TYPE_VENDOR,   "Vendor"},
1184     {0, NULL}
1185 };
1186 static const value_string bmrequesttype_recipient_vals[] = {
1187     {0, "Device"},
1188     {1, "Interface"},
1189     {2, "Endpoint"},
1190     {3, "Other"},
1191     {0, NULL}
1192 };
1193
1194 static int
1195 dissect_usb_bmrequesttype(proto_tree *parent_tree, tvbuff_t *tvb, int offset,
1196     int *type)
1197 {
1198         proto_item *item=NULL;
1199         proto_tree *tree=NULL;
1200         guint8 bmRequestType;
1201
1202         if(parent_tree){
1203                 item=proto_tree_add_item(parent_tree, hf_usb_bmRequestType, tvb, offset, 1, TRUE);
1204                 tree = proto_item_add_subtree(item, ett_usb_setup_bmrequesttype);
1205         }
1206
1207         bmRequestType = tvb_get_guint8(tvb, offset);
1208         *type = (bmRequestType & USB_TYPE_MASK) >>5;
1209         proto_tree_add_item(tree, hf_usb_bmRequestType_direction, tvb, offset, 1, TRUE);
1210         proto_tree_add_item(tree, hf_usb_bmRequestType_type, tvb, offset, 1, TRUE);
1211         proto_tree_add_item(tree, hf_usb_bmRequestType_recipient, tvb, offset, 1, TRUE);
1212
1213         offset++;
1214         return offset;
1215 }
1216
1217 static void
1218 dissect_linux_usb_pseudo_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1219 {
1220     guint8 transfer_type;
1221     const gchar* val_str;
1222
1223     proto_tree_add_uint64(tree, hf_usb_urb_id, tvb, 0, 0,
1224                           pinfo->pseudo_header->linux_usb.id);
1225
1226     /* show the event type of this URB as string and as a character */
1227     val_str = val_to_str(pinfo->pseudo_header->linux_usb.event_type,
1228         usb_urb_type_vals, "Unknown %d");
1229     proto_tree_add_string_format_value(tree, hf_usb_urb_type, tvb, 0, 0,
1230         &(pinfo->pseudo_header->linux_usb.event_type),
1231         "%s ('%c')", val_str,
1232         pinfo->pseudo_header->linux_usb.event_type);
1233
1234     transfer_type = pinfo->pseudo_header->linux_usb.transfer_type;
1235     proto_tree_add_uint(tree, hf_usb_transfer_type, tvb, 0, 0, transfer_type);
1236
1237     if (check_col(pinfo->cinfo, COL_INFO)) {
1238         col_append_str(pinfo->cinfo, COL_INFO,
1239             val_to_str(transfer_type, usb_transfer_type_vals, "Unknown type %x"));
1240     }
1241
1242     proto_tree_add_uint(tree, hf_usb_endpoint_number, tvb, 0, 0,
1243                         pinfo->pseudo_header->linux_usb.endpoint_number);
1244
1245     proto_tree_add_uint(tree, hf_usb_device_address, tvb, 0, 0,
1246                         pinfo->pseudo_header->linux_usb.device_address);
1247
1248     proto_tree_add_uint(tree, hf_usb_bus_id, tvb, 0, 0,
1249                         pinfo->pseudo_header->linux_usb.bus_id);
1250
1251     /* Right after the pseudo header we always have
1252      * sizeof(struct usb_device_setup_hdr)=8 bytes. The content of these
1253      * bytes have only meaning in case setup_flag == 0.
1254      */
1255     if (pinfo->pseudo_header->linux_usb.setup_flag == 0) {
1256         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
1257             0, 0,
1258             &(pinfo->pseudo_header->linux_usb.setup_flag),
1259             "present (%d)",
1260             pinfo->pseudo_header->linux_usb.setup_flag);
1261     } else {
1262         proto_tree_add_string_format_value(tree, hf_usb_setup_flag, tvb,
1263             0, 0,
1264             &(pinfo->pseudo_header->linux_usb.setup_flag),
1265             "not present ('%c')",
1266             pinfo->pseudo_header->linux_usb.setup_flag);
1267     }
1268
1269     if (pinfo->pseudo_header->linux_usb.data_flag == 0) {
1270         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
1271             0, 0,
1272             &(pinfo->pseudo_header->linux_usb.data_flag),
1273             "present (%d)",
1274             pinfo->pseudo_header->linux_usb.data_flag);
1275     } else {
1276         proto_tree_add_string_format_value(tree, hf_usb_data_flag, tvb,
1277             0, 0,
1278             &(pinfo->pseudo_header->linux_usb.data_flag),
1279             "not present ('%c')",
1280             pinfo->pseudo_header->linux_usb.data_flag);
1281     }
1282
1283     /* Timestamp was already processed by libpcap,
1284      * skip it for now:
1285      *   pinfo->pseudo_header->linux_usb.ts_sec
1286      *   pinfo->pseudo_header->linux_usb.ts_usec
1287      */
1288
1289     proto_tree_add_int(tree, hf_usb_urb_status, tvb, 0, 0,
1290                         pinfo->pseudo_header->linux_usb.status);
1291
1292     proto_tree_add_uint(tree, hf_usb_urb_len, tvb, 0, 0,
1293                         pinfo->pseudo_header->linux_usb.urb_len);
1294
1295     proto_tree_add_uint(tree, hf_usb_data_len, tvb, 0, 0,
1296                         pinfo->pseudo_header->linux_usb.data_len);
1297
1298 }
1299
1300 static void
1301 dissect_linux_usb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent)
1302 {
1303     int offset = 0;
1304     int type, endpoint;
1305     guint8 setup_flag;
1306     proto_tree *tree = NULL;
1307     guint32 src_device, dst_device, tmp_addr;
1308     static usb_address_t src_addr, dst_addr; /* has to be static due to SET_ADDRESS */
1309     guint32 src_endpoint, dst_endpoint;
1310     gboolean is_request=FALSE;
1311     usb_conv_info_t *usb_conv_info=NULL;
1312     usb_trans_info_t *usb_trans_info=NULL;
1313     conversation_t *conversation;
1314     usb_tap_data_t *tap_data=NULL;
1315
1316     if (check_col(pinfo->cinfo, COL_PROTOCOL))
1317         col_set_str(pinfo->cinfo, COL_PROTOCOL, "USB");
1318
1319     /* add usb hdr*/
1320     if (parent) {
1321       proto_item *ti = NULL;
1322       ti = proto_tree_add_protocol_format(parent, proto_usb, tvb, 0, sizeof(struct usb_device_setup_hdr), "USB URB");
1323
1324       tree = proto_item_add_subtree(ti, usb_hdr);
1325     }
1326
1327     dissect_linux_usb_pseudo_header(tvb, pinfo, tree);
1328
1329     type = pinfo->pseudo_header->linux_usb.transfer_type;
1330 #if 0
1331     /* The direction flag is broken so we must strip it off */
1332     endpoint=pinfo->pseudo_header->linux_usb.endpoint_number;
1333 #else
1334     endpoint=pinfo->pseudo_header->linux_usb.endpoint_number&(~URB_TRANSFER_IN);
1335 #endif
1336     tmp_addr=pinfo->pseudo_header->linux_usb.device_address;
1337     setup_flag = pinfo->pseudo_header->linux_usb.setup_flag;
1338
1339 #if 0
1340     /* this is how it is supposed to work but this flag seems to be broken -- ronnie */
1341     is_request = endpoint & URB_TRANSFER_IN;
1342 #else
1343     /* Determine whether this is a request or a response */
1344     switch(type){
1345     case URB_BULK:
1346     case URB_CONTROL:
1347     case URB_ISOCHRONOUS:
1348         switch(pinfo->pseudo_header->linux_usb.event_type){
1349         case URB_SUBMIT:
1350             is_request=TRUE;
1351             break;
1352         case URB_COMPLETE:
1353         case URB_ERROR:
1354             is_request=FALSE;
1355             break;
1356         default:
1357             DISSECTOR_ASSERT_NOT_REACHED();
1358         }
1359         break;
1360     case URB_INTERRUPT:
1361         switch(pinfo->pseudo_header->linux_usb.event_type){
1362         case URB_SUBMIT:
1363             is_request=FALSE;
1364             break;
1365         case URB_COMPLETE:
1366         case URB_ERROR:
1367             is_request=TRUE;
1368             break;
1369         default:
1370             DISSECTOR_ASSERT_NOT_REACHED();
1371         }
1372         break;
1373      default:
1374         DISSECTOR_ASSERT_NOT_REACHED();
1375     }
1376 #endif
1377
1378     /* Set up addresses and ports.
1379      * Note that URB_INTERRUPT goes in the reverse direction and thus
1380      * the request comes from the device and not the host.
1381      */
1382     if ( (is_request&&(type!=URB_INTERRUPT))
1383       || (!is_request&&(type==URB_INTERRUPT)) ){
1384         src_addr.device = src_device = 0xffffffff;
1385         src_addr.endpoint = src_endpoint = NO_ENDPOINT;
1386         dst_addr.device = dst_device = htolel(tmp_addr);
1387         dst_addr.endpoint = dst_endpoint = htolel(endpoint);
1388     } else {
1389         src_addr.device = src_device = htolel(tmp_addr);
1390         src_addr.endpoint = src_endpoint = htolel(endpoint);
1391         dst_addr.device = dst_device = 0xffffffff;
1392         dst_addr.endpoint = dst_endpoint = NO_ENDPOINT;
1393     }
1394
1395     SET_ADDRESS(&pinfo->net_src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1396     SET_ADDRESS(&pinfo->src, AT_USB, USB_ADDR_LEN, (char *)&src_addr);
1397     SET_ADDRESS(&pinfo->net_dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1398     SET_ADDRESS(&pinfo->dst, AT_USB, USB_ADDR_LEN, (char *)&dst_addr);
1399     pinfo->ptype=PT_USB;
1400     pinfo->srcport=src_endpoint;
1401     pinfo->destport=dst_endpoint;
1402
1403     conversation=get_usb_conversation(pinfo, &pinfo->src, &pinfo->dst, pinfo->srcport, pinfo->destport);
1404
1405     usb_conv_info=get_usb_conv_info(conversation);
1406     pinfo->usb_conv_info=usb_conv_info;
1407
1408
1409     /* request/response matching so we can keep track of transaction specific
1410      * data.
1411      */
1412     if(is_request){
1413         /* this is a request */
1414         usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1415         if(!usb_trans_info){
1416             usb_trans_info=se_alloc0(sizeof(usb_trans_info_t));
1417             usb_trans_info->request_in=pinfo->fd->num;
1418             usb_trans_info->req_time=pinfo->fd->abs_ts;
1419             se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1420         }
1421         usb_conv_info->usb_trans_info=usb_trans_info;
1422
1423         if(usb_trans_info && usb_trans_info->response_in){
1424             proto_item *ti;
1425
1426             ti=proto_tree_add_uint(tree, hf_usb_response_in, tvb, 0, 0, usb_trans_info->response_in);
1427             PROTO_ITEM_SET_GENERATED(ti);
1428         }
1429     } else {
1430         /* this is a response */
1431         if(pinfo->fd->flags.visited){
1432             usb_trans_info=se_tree_lookup32(usb_conv_info->transactions, pinfo->fd->num);
1433         } else {
1434             usb_trans_info=se_tree_lookup32_le(usb_conv_info->transactions, pinfo->fd->num);
1435             if(usb_trans_info){
1436                 usb_trans_info->response_in=pinfo->fd->num;
1437                 se_tree_insert32(usb_conv_info->transactions, pinfo->fd->num, usb_trans_info);
1438             }
1439         }
1440         usb_conv_info->usb_trans_info=usb_trans_info;
1441
1442         if(usb_trans_info && usb_trans_info->request_in){
1443             proto_item *ti;
1444             nstime_t t, deltat;
1445
1446             ti=proto_tree_add_uint(tree, hf_usb_request_in, tvb, 0, 0, usb_trans_info->request_in);
1447             PROTO_ITEM_SET_GENERATED(ti);
1448
1449             t = pinfo->fd->abs_ts;
1450             nstime_delta(&deltat, &t, &usb_trans_info->req_time);
1451             ti=proto_tree_add_time(tree, hf_usb_time, tvb, 0, 0, &deltat);
1452             PROTO_ITEM_SET_GENERATED(ti);
1453         }
1454     }
1455
1456     /* For DLT189 it seems
1457      * that all INTERRUPT or BULK packets as well as all CONTROL responses
1458      * are prepended with 8 mysterious bytes.
1459      */
1460     switch(type){
1461     case URB_CONTROL:
1462         if(pinfo->pseudo_header->linux_usb.event_type!=URB_SUBMIT){
1463             offset+=8;
1464         }
1465         break;
1466     case URB_BULK:
1467     case URB_ISOCHRONOUS:
1468     case URB_INTERRUPT:
1469         offset+=8;
1470         break;
1471     default:
1472         DISSECTOR_ASSERT_NOT_REACHED();
1473     }
1474
1475     tap_data=ep_alloc(sizeof(usb_tap_data_t));
1476     tap_data->urb_type=(guint8)pinfo->pseudo_header->linux_usb.event_type;
1477     tap_data->transfer_type=(guint8)type;
1478     tap_data->conv_info=usb_conv_info;
1479     tap_data->trans_info=usb_trans_info;
1480     tap_queue_packet(usb_tap, pinfo, tap_data);
1481
1482
1483     switch(type){
1484     case URB_BULK:
1485         {
1486         proto_item *item;
1487
1488         item=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, 0, 0, usb_conv_info->interfaceClass);
1489         PROTO_ITEM_SET_GENERATED(item);
1490         if(tvb_reported_length_remaining(tvb, offset)){
1491             tvbuff_t *next_tvb;
1492
1493             pinfo->usb_conv_info=usb_conv_info;
1494             next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1495             if(dissector_try_port(usb_bulk_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, parent)){
1496                 return;
1497             }
1498         }
1499         }
1500         break;
1501     case URB_CONTROL:
1502         {
1503         const usb_setup_dissector_table_t *tmp;
1504         usb_setup_dissector dissector;
1505         proto_item *ti = NULL;
1506         proto_tree *setup_tree = NULL;
1507         int type;
1508
1509         ti=proto_tree_add_uint(tree, hf_usb_bInterfaceClass, tvb, offset, 0, usb_conv_info->interfaceClass);
1510         PROTO_ITEM_SET_GENERATED(ti);
1511
1512         if(is_request){
1513             if (setup_flag == 0) {
1514                 tvbuff_t *next_tvb;
1515
1516                 /* this is a request */
1517                 ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, sizeof(struct usb_device_setup_hdr), "URB setup");
1518                 setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
1519                 usb_trans_info->requesttype=tvb_get_guint8(tvb, offset);
1520                 offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type);
1521
1522
1523                 /* read the request code and spawn off to a class specific
1524                  * dissector if found
1525                  */
1526                 usb_trans_info->request=tvb_get_guint8(tvb, offset);
1527
1528                 switch (type) {
1529
1530                 case RQT_SETUP_TYPE_STANDARD:
1531                     /*
1532                      * This is a standard request which is managed by this
1533                      * dissector
1534                      */
1535                     proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
1536                     offset += 1;
1537
1538                     if (check_col(pinfo->cinfo, COL_INFO)) {
1539                         col_clear(pinfo->cinfo, COL_INFO);
1540                         col_append_fstr(pinfo->cinfo, COL_INFO, "%s Request",
1541                              val_to_str(usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
1542                     }
1543
1544                     dissector=NULL;
1545                     for(tmp=setup_dissectors;tmp->dissector;tmp++){
1546                         if(tmp->request==usb_trans_info->request){
1547                             dissector=tmp->dissector;
1548                             break;
1549                         }
1550                     }
1551
1552                     if(dissector){
1553                         dissector(pinfo, setup_tree, tvb, offset, is_request, usb_trans_info, usb_conv_info);
1554                         offset+=6;
1555                     } else {
1556                         proto_tree_add_item(setup_tree, hf_usb_value, tvb, offset, 2, TRUE);
1557                         offset += 2;
1558                         proto_tree_add_item(setup_tree, hf_usb_index, tvb, offset, 2, TRUE);
1559                         offset += 2;
1560                         proto_tree_add_item(setup_tree, hf_usb_length, tvb, offset, 2, TRUE);
1561                         offset += 2;
1562                     }
1563                     break;
1564
1565                 case RQT_SETUP_TYPE_CLASS:
1566                     /* Try to find a class specific dissector */
1567                     next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1568                     if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
1569                         return;
1570                     /* XXX - dump as hex */
1571                     }
1572                     break;
1573                 }
1574             } else {
1575                 offset += 8;
1576             }
1577         } else {
1578             tvbuff_t *next_tvb;
1579
1580             /* this is a response */
1581             if(usb_trans_info){
1582                 /* Try to find a class specific dissector */
1583                 next_tvb=tvb_new_subset(tvb, offset, -1, -1);
1584                 if(dissector_try_port(usb_control_dissector_table, usb_conv_info->interfaceClass, next_tvb, pinfo, tree)){
1585                     return;
1586                 }
1587
1588                 type = (usb_trans_info->requesttype & USB_TYPE_MASK) >>5;
1589                 switch (type) {
1590
1591                 case RQT_SETUP_TYPE_STANDARD:
1592                     /*
1593                      * This is a standard reply which is managed by this
1594                      * dissector
1595                      */
1596                     if (check_col(pinfo->cinfo, COL_INFO)) {
1597                         col_clear(pinfo->cinfo, COL_INFO);
1598                         col_append_fstr(pinfo->cinfo, COL_INFO, "%s Response",
1599                             val_to_str(usb_conv_info->usb_trans_info->request, setup_request_names_vals, "Unknown type %x"));
1600                     }
1601
1602                     dissector=NULL;
1603                     for(tmp=setup_dissectors;tmp->dissector;tmp++){
1604                         if(tmp->request==usb_conv_info->usb_trans_info->request){
1605                             dissector=tmp->dissector;
1606                             break;
1607                         }
1608                     }
1609
1610                     if(dissector){
1611                         offset = dissector(pinfo, tree, tvb, offset, is_request, usb_conv_info->usb_trans_info, usb_conv_info);
1612                     } else {
1613                         proto_tree_add_text(tree, tvb, offset, -1, "CONTROL response data");
1614                         offset += tvb_length_remaining(tvb, offset);
1615                     }
1616                 }
1617             } else {
1618                 /* no matching request available */
1619                 proto_tree_add_text(tree, tvb, offset, -1, "CONTROL response data");
1620                 offset += tvb_length_remaining(tvb, offset);
1621             }
1622         }
1623         }
1624         break;
1625     default:
1626         /* dont know */
1627         if (setup_flag == 0) {
1628             proto_item *ti = NULL;
1629             proto_tree *setup_tree = NULL;
1630             guint8 requesttype, request;
1631             int type;
1632
1633             ti = proto_tree_add_protocol_format(tree, proto_usb, tvb, offset, sizeof(struct usb_device_setup_hdr), "URB setup");
1634             setup_tree = proto_item_add_subtree(ti, usb_setup_hdr);
1635
1636
1637             requesttype=tvb_get_guint8(tvb, offset);
1638             offset=dissect_usb_bmrequesttype(setup_tree, tvb, offset, &type);
1639
1640             request=tvb_get_guint8(tvb, offset);
1641             proto_tree_add_item(setup_tree, hf_usb_request, tvb, offset, 1, TRUE);
1642             offset += 1;
1643
1644             proto_tree_add_item(tree, hf_usb_value, tvb, offset, 2, TRUE);
1645             offset += 2;
1646             proto_tree_add_item(tree, hf_usb_index, tvb, offset, 2, TRUE);
1647             offset += 2;
1648             proto_tree_add_item(tree, hf_usb_length, tvb, offset, 2, TRUE);
1649             offset += 2;
1650         } else {
1651             offset += 8;
1652         }
1653         break;
1654     }
1655     if (tvb_reported_length_remaining(tvb, offset) != 0)
1656         proto_tree_add_item(tree, hf_usb_data, tvb, offset, -1, FALSE);
1657 }
1658
1659 void
1660 proto_register_usb(void)
1661 {
1662     static hf_register_info hf[] = {
1663
1664     /* USB packet pseudoheader members */
1665         { &hf_usb_urb_id,
1666         { "URB id", "usb.urb_id", FT_UINT64, BASE_HEX,
1667                 NULL, 0x0,
1668                 "URB id", HFILL }},
1669
1670         { &hf_usb_urb_type,
1671         { "URB type", "usb.urb_type", FT_STRING, BASE_NONE,
1672                 NULL, 0x0,
1673                 "URB type", HFILL }},
1674
1675         { &hf_usb_transfer_type,
1676         { "URB transfer type", "usb.transfer_type", FT_UINT8, BASE_DEC,
1677                 VALS(usb_transfer_type_vals), 0x0,
1678                 "URB transfer type", HFILL }},
1679
1680         { &hf_usb_endpoint_number,
1681         { "Endpoint", "usb.endpoint_number", FT_UINT8, BASE_HEX, NULL, 0x0,
1682                 "USB endpoint number", HFILL }},
1683
1684         { &hf_usb_device_address,
1685         { "Device", "usb.device_address", FT_UINT8, BASE_DEC, NULL, 0x0,
1686                 "USB device address", HFILL }},
1687
1688         { &hf_usb_bus_id,
1689         { "URB bus id", "usb.bus_id", FT_UINT16, BASE_DEC,
1690                 NULL, 0x0,
1691                 "URB bus id", HFILL }},
1692
1693         { &hf_usb_setup_flag,
1694         { "Device setup request", "usb.setup_flag", FT_STRING, BASE_NONE,
1695                  NULL, 0x0,
1696                  "USB device setup request is present (0) or not", HFILL }},
1697
1698         { &hf_usb_data_flag,
1699         { "Data", "usb.data_flag", FT_STRING, BASE_NONE,
1700                  NULL, 0x0,
1701                  "USB data is present (0) or not", HFILL }},
1702
1703         { &hf_usb_urb_status,
1704         { "URB status", "usb.urb_status", FT_INT32, BASE_DEC,
1705                 VALS(usb_urb_status_vals), 0x0,
1706                 "URB status", HFILL }},
1707
1708         { &hf_usb_urb_len,
1709         { "URB length [bytes]", "usb.urb_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1710                 "URB length in bytes", HFILL }},
1711
1712         { &hf_usb_data_len,
1713         { "Data length [bytes]", "usb.data_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1714                 "URB data length in bytes", HFILL }},
1715
1716     /* Generated values */
1717         { &hf_usb_src_endpoint_number,
1718         { "Src Endpoint", "usb.src.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
1719                 "Source USB endpoint number", HFILL }},
1720
1721         { &hf_usb_dst_endpoint_number,
1722         { "Dst Endpoint", "usb.dst.endpoint", FT_UINT8, BASE_HEX, NULL, 0x0,
1723                 "Destination USB endpoint number", HFILL }},
1724
1725     /* Fields from usb20.pdf, Table 9-2 'Format of Setup Data' */
1726         { &hf_usb_bmRequestType,
1727         { "bmRequestType", "usb.bmRequestType", FT_UINT8, BASE_HEX, NULL, 0x0,
1728                 "", HFILL }},
1729
1730         { &hf_usb_request,
1731         { "bRequest", "usb.setup.bRequest", FT_UINT8, BASE_DEC, VALS(setup_request_names_vals), 0x0,
1732                 "", HFILL }},
1733
1734         { &hf_usb_value,
1735         { "wValue", "usb.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
1736                 "", HFILL }},
1737
1738         { &hf_usb_index,
1739         { "wIndex", "usb.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
1740                 "", HFILL }},
1741
1742         { &hf_usb_length,
1743         { "wLength", "usb.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
1744                 "", HFILL }},
1745
1746     /* --------------------------------- */
1747         { &hf_usb_data,
1748         {"Application Data", "usb.data",
1749             FT_BYTES, BASE_HEX, NULL, 0x0,
1750             "Payload is application data", HFILL }},
1751
1752         { &hf_usb_bmRequestType_direction,
1753         { "Direction", "usb.bmRequestType.direction", FT_BOOLEAN, 8,
1754           TFS(&tfs_bmrequesttype_direction), USB_DIR_IN, "", HFILL }},
1755
1756         { &hf_usb_bmRequestType_type,
1757         { "Type", "usb.bmRequestType.type", FT_UINT8, BASE_HEX,
1758           VALS(bmrequesttype_type_vals), USB_TYPE_MASK, "", HFILL }},
1759
1760         { &hf_usb_bmRequestType_recipient,
1761         { "Recipient", "usb.bmRequestType.recipient", FT_UINT8, BASE_HEX,
1762           VALS(bmrequesttype_recipient_vals), 0x1f, "", HFILL }},
1763
1764         { &hf_usb_bDescriptorType,
1765         { "bDescriptorType", "usb.bDescriptorType", FT_UINT8, BASE_HEX,
1766           VALS(descriptor_type_vals), 0x0, "", HFILL }},
1767
1768         { &hf_usb_descriptor_index,
1769         { "Descriptor Index", "usb.DescriptorIndex", FT_UINT8, BASE_HEX,
1770           NULL, 0x0, "", HFILL }},
1771
1772         { &hf_usb_language_id,
1773         { "Language Id", "usb.LanguageId", FT_UINT16, BASE_HEX,
1774           VALS(usb_langid_vals), 0x0, "", HFILL }},
1775
1776         { &hf_usb_bLength,
1777         { "bLength", "usb.bLength", FT_UINT8, BASE_DEC,
1778           NULL, 0x0, "", HFILL }},
1779
1780         { &hf_usb_bcdUSB,
1781         { "bcdUSB", "usb.bcdUSB", FT_UINT16, BASE_HEX,
1782           NULL, 0x0, "", HFILL }},
1783
1784         { &hf_usb_bDeviceClass,
1785         { "bDeviceClass", "usb.bDeviceClass", FT_UINT8, BASE_DEC,
1786           NULL, 0x0, "", HFILL }},
1787
1788         { &hf_usb_bDeviceSubClass,
1789         { "bDeviceSubClass", "usb.bDeviceSubClass", FT_UINT8, BASE_DEC,
1790           NULL, 0x0, "", HFILL }},
1791
1792         { &hf_usb_bDeviceProtocol,
1793         { "bDeviceProtocol", "usb.bDeviceProtocol", FT_UINT8, BASE_DEC,
1794           NULL, 0x0, "", HFILL }},
1795
1796         { &hf_usb_bMaxPacketSize0,
1797         { "bMaxPacketSize0", "usb.bMaxPacketSize0", FT_UINT8, BASE_DEC,
1798           NULL, 0x0, "", HFILL }},
1799
1800         { &hf_usb_idVendor,
1801         { "idVendor", "usb.idVendor", FT_UINT16, BASE_HEX,
1802           NULL, 0x0, "", HFILL }},
1803
1804         { &hf_usb_idProduct,
1805         { "idProduct", "usb.idProduct", FT_UINT16, BASE_HEX,
1806           NULL, 0x0, "", HFILL }},
1807
1808         { &hf_usb_bcdDevice,
1809         { "bcdDevice", "usb.bcdDevice", FT_UINT16, BASE_HEX,
1810           NULL, 0x0, "", HFILL }},
1811
1812         { &hf_usb_iManufacturer,
1813         { "iManufacturer", "usb.iManufacturer", FT_UINT8, BASE_DEC,
1814           NULL, 0x0, "", HFILL }},
1815
1816         { &hf_usb_iProduct,
1817         { "iProduct", "usb.iProduct", FT_UINT8, BASE_DEC,
1818           NULL, 0x0, "", HFILL }},
1819
1820         { &hf_usb_iSerialNumber,
1821         { "iSerialNumber", "usb.iSerialNumber", FT_UINT8, BASE_DEC,
1822           NULL, 0x0, "", HFILL }},
1823
1824         { &hf_usb_bNumConfigurations,
1825         { "bNumConfigurations", "usb.bNumConfigurations", FT_UINT8, BASE_DEC,
1826           NULL, 0x0, "", HFILL }},
1827
1828         { &hf_usb_wLANGID,
1829         { "wLANGID", "usb.wLANGID", FT_UINT16, BASE_HEX,
1830           VALS(usb_langid_vals), 0x0, "", HFILL }},
1831
1832         { &hf_usb_bString,
1833         { "bString", "usb.bString", FT_STRING, BASE_NONE,
1834           NULL, 0x0, "", HFILL }},
1835
1836         { &hf_usb_bInterfaceNumber,
1837         { "bInterfaceNumber", "usb.bInterfaceNumber", FT_UINT8, BASE_DEC,
1838           NULL, 0x0, "", HFILL }},
1839
1840         { &hf_usb_bAlternateSetting,
1841         { "bAlternateSetting","usb.bAlternateSetting", FT_UINT8, BASE_DEC,
1842           NULL, 0x0, "", HFILL }},
1843
1844         { &hf_usb_bNumEndpoints,
1845         { "bNumEndpoints","usb.bNumEndpoints", FT_UINT8, BASE_DEC,
1846           NULL, 0x0, "", HFILL }},
1847
1848         { &hf_usb_bInterfaceClass,
1849         { "bInterfaceClass", "usb.bInterfaceClass", FT_UINT8, BASE_HEX,
1850           VALS(usb_interfaceclass_vals), 0x0, "", HFILL }},
1851
1852         { &hf_usb_bInterfaceSubClass,
1853         { "bInterfaceSubClass", "usb.bInterfaceSubClass", FT_UINT8, BASE_HEX,
1854           NULL, 0x0, "", HFILL }},
1855
1856         { &hf_usb_bInterfaceProtocol,
1857         { "bInterfaceProtocol", "usb.bInterfaceProtocol", FT_UINT8, BASE_HEX,
1858           NULL, 0x0, "", HFILL }},
1859
1860         { &hf_usb_iInterface,
1861         { "iInterface", "usb.iInterface", FT_UINT8, BASE_DEC,
1862           NULL, 0x0, "", HFILL }},
1863
1864         { &hf_usb_bEndpointAddress,
1865         { "bEndpointAddress", "usb.bEndpointAddress", FT_UINT8, BASE_HEX,
1866           NULL, 0x0, "", HFILL }},
1867
1868         { &hf_usb_configuration_bmAttributes,
1869         { "Configuration bmAttributes", "usb.configuration.bmAttributes", FT_UINT8, BASE_HEX,
1870           NULL, 0x0, "", HFILL }},
1871
1872         { &hf_usb_bmAttributes,
1873         { "bmAttributes", "usb.bmAttributes", FT_UINT8, BASE_HEX,
1874           NULL, 0x0, "", HFILL }},
1875
1876         { &hf_usb_bEndpointAttributeTransfer,
1877         { "Transfertype", "usb.bmAttributes.transfer", FT_UINT8, BASE_HEX,
1878           VALS(usb_bmAttributes_transfer_vals), 0x03, "", HFILL }},
1879
1880         { &hf_usb_bEndpointAttributeSynchonisation,
1881         { "Synchronisationtype", "usb.bmAttributes.sync", FT_UINT8, BASE_HEX,
1882           VALS(usb_bmAttributes_sync_vals), 0x0c, "", HFILL }},
1883
1884         { &hf_usb_bEndpointAttributeBehaviour,
1885         { "Behaviourtype", "usb.bmAttributes.behaviour", FT_UINT8, BASE_HEX,
1886           VALS(usb_bmAttributes_behaviour_vals), 0x30, "", HFILL }},
1887
1888         { &hf_usb_wMaxPacketSize,
1889         { "wMaxPacketSize", "usb.wMaxPacketSize", FT_UINT16, BASE_DEC,
1890           NULL, 0x0, "", HFILL }},
1891
1892         { &hf_usb_bInterval,
1893         { "bInterval", "usb.bInterval", FT_UINT8, BASE_DEC,
1894           NULL, 0x0, "", HFILL }},
1895
1896         { &hf_usb_wTotalLength,
1897         { "wTotalLength", "usb.wTotalLength", FT_UINT16, BASE_DEC,
1898           NULL, 0x0, "", HFILL }},
1899
1900         { &hf_usb_bNumInterfaces,
1901         { "bNumInterfaces", "usb.bNumInterfaces", FT_UINT8, BASE_DEC,
1902           NULL, 0x0, "", HFILL }},
1903
1904         { &hf_usb_bConfigurationValue,
1905         { "bConfigurationValue", "usb.bConfigurationValue", FT_UINT8, BASE_DEC,
1906           NULL, 0x0, "", HFILL }},
1907
1908         { &hf_usb_iConfiguration,
1909         { "iConfiguration", "usb.iConfiguration", FT_UINT8, BASE_DEC,
1910           NULL, 0x0, "", HFILL }},
1911
1912         { &hf_usb_bMaxPower,
1913         { "bMaxPower", "usb.bMaxPower", FT_UINT8, BASE_DEC,
1914           NULL, 0x0, "", HFILL }},
1915
1916         { &hf_usb_configuration_legacy10buspowered,
1917         { "Must be 1", "usb.configuration.legacy10buspowered", FT_BOOLEAN, 8,
1918           TFS(&tfs_mustbeone), 0x80, "Legacy USB 1.0 bus powered", HFILL }},
1919
1920         { &hf_usb_configuration_selfpowered,
1921         { "Self-Powered", "usb.configuration.selfpowered", FT_BOOLEAN, 8,
1922           TFS(&tfs_selfpowered), 0x40, "", HFILL }},
1923
1924         { &hf_usb_configuration_remotewakeup,
1925         { "Remote Wakeup", "usb.configuration.remotewakeup", FT_BOOLEAN, 8,
1926           TFS(&tfs_remotewakeup), 0x20, "", HFILL }},
1927
1928         { &hf_usb_bEndpointAddress_number,
1929         { "Endpoint Number", "usb.bEndpointAddress.number", FT_UINT8, BASE_HEX,
1930           NULL, 0x0f, "", HFILL }},
1931
1932         { &hf_usb_bEndpointAddress_direction,
1933         { "Direction", "usb.bEndpointAddress.direction", FT_BOOLEAN, 8,
1934           TFS(&tfs_endpoint_direction), 0x80, "", HFILL }},
1935
1936         { &hf_usb_request_in,
1937                 { "Request in", "usb.request_in", FT_FRAMENUM, BASE_NONE,
1938                 NULL, 0, "The request to this packet is in this packet", HFILL }},
1939
1940         { &hf_usb_time,
1941                 { "Time from request", "usb.time", FT_RELATIVE_TIME, BASE_NONE,
1942                 NULL, 0, "Time between Request and Response for USB cmds", HFILL }},
1943
1944         { &hf_usb_response_in,
1945                 { "Response in", "usb.response_in", FT_FRAMENUM, BASE_NONE,
1946                 NULL, 0, "The response to this packet is in this packet", HFILL }},
1947
1948     };
1949
1950     static gint *usb_subtrees[] = {
1951             &usb_hdr,
1952             &usb_setup_hdr,
1953             &ett_usb_setup_bmrequesttype,
1954             &ett_descriptor_device,
1955             &ett_configuration_bmAttributes,
1956             &ett_configuration_bEndpointAddress,
1957             &ett_endpoint_bmAttributes
1958     };
1959
1960
1961     proto_usb = proto_register_protocol("USB", "USB", "usb");
1962     proto_register_field_array(proto_usb, hf, array_length(hf));
1963     proto_register_subtree_array(usb_subtrees, array_length(usb_subtrees));
1964
1965     usb_bulk_dissector_table = register_dissector_table("usb.bulk",
1966         "USB bulk endpoint", FT_UINT8, BASE_DEC);
1967
1968     usb_control_dissector_table = register_dissector_table("usb.control",
1969         "USB control endpoint", FT_UINT8, BASE_DEC);
1970
1971     usb_tap=register_tap("usb");
1972 }
1973
1974 void
1975 proto_reg_handoff_usb(void)
1976 {
1977     dissector_handle_t linux_usb_handle;
1978
1979     linux_usb_handle = create_dissector_handle(dissect_linux_usb, proto_usb);
1980
1981     dissector_add("wtap_encap", WTAP_ENCAP_USB_LINUX, linux_usb_handle);
1982 }