8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <epan/conversation.h>
27 #include <epan/expert.h>
28 #include <epan/packet.h>
29 #include <epan/reassemble.h>
30 #include "packet-usb.h"
32 /* protocols and header fields */
33 static int proto_usb_audio
= -1;
34 static int hf_midi_cable_number
= -1;
35 static int hf_midi_code_index
= -1;
36 static int hf_midi_event
= -1;
38 static reassembly_table midi_data_reassembly_table
;
40 static gint ett_usb_audio
= -1;
41 static gint ett_usb_audio_desc
= -1;
43 static dissector_handle_t sysex_handle
;
45 #define AUDIO_SUBCLASS_UNDEFINED 0x00
46 #define AUDIO_SUBCLASS_AUDIOCONTROL 0x01
47 #define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02
48 #define AUDIO_SUBCLASS_MIDISTREAMING 0x03
51 static const value_string usb_audio_subclass_vals
[] = {
52 {AUDIO_SUBCLASS_UNDEFINED
, "SUBCLASS_UNDEFINED"},
53 {AUDIO_SUBCLASS_AUDIOCONTROL
, "AUDIOCONSTROL"},
54 {AUDIO_SUBCLASS_AUDIOSTREAMING
, "AUDIOSTREAMING"},
55 {AUDIO_SUBCLASS_MIDISTREAMING
, "MIDISTREAMING"},
60 static const value_string code_index_vals
[] = {
61 { 0x0, "Miscellaneous (Reserved)" },
62 { 0x1, "Cable events (Reserved)" },
63 { 0x2, "Two-byte System Common message" },
64 { 0x3, "Three-byte System Common message" },
65 { 0x4, "SysEx starts or continues" },
66 { 0x5, "SysEx ends with following single byte/Single-byte System Common Message" },
67 { 0x6, "SysEx ends with following two bytes" },
68 { 0x7, "SysEx ends with following three bytes" },
71 { 0xA, "Poly-KeyPress" },
72 { 0xB, "Control Change" },
73 { 0xC, "Program Change" },
74 { 0xD, "Channel Pressure" },
75 { 0xE, "PitchBend Change" },
76 { 0xF, "Single Byte" },
80 /* USB audio specification, section A.8 */
81 #define CS_INTERFACE 0x24
82 #define CS_ENDPOINT 0x25
84 static const value_string aud_descriptor_type_vals
[] = {
85 {CS_INTERFACE
, "audio class interface"},
86 {CS_ENDPOINT
, "audio class endpoint"},
89 static value_string_ext aud_descriptor_type_vals_ext
=
90 VALUE_STRING_EXT_INIT(aud_descriptor_type_vals
);
92 static int hf_sysex_msg_fragments
= -1;
93 static int hf_sysex_msg_fragment
= -1;
94 static int hf_sysex_msg_fragment_overlap
= -1;
95 static int hf_sysex_msg_fragment_overlap_conflicts
= -1;
96 static int hf_sysex_msg_fragment_multiple_tails
= -1;
97 static int hf_sysex_msg_fragment_too_long_fragment
= -1;
98 static int hf_sysex_msg_fragment_error
= -1;
99 static int hf_sysex_msg_fragment_count
= -1;
100 static int hf_sysex_msg_reassembled_in
= -1;
101 static int hf_sysex_msg_reassembled_length
= -1;
102 static int hf_sysex_msg_reassembled_data
= -1;
104 static gint ett_sysex_msg_fragment
= -1;
105 static gint ett_sysex_msg_fragments
= -1;
107 static expert_field ei_usb_audio_undecoded
= EI_INIT
;
109 static const fragment_items sysex_msg_frag_items
= {
110 /* Fragment subtrees */
111 &ett_sysex_msg_fragment
,
112 &ett_sysex_msg_fragments
,
113 /* Fragment fields */
114 &hf_sysex_msg_fragments
,
115 &hf_sysex_msg_fragment
,
116 &hf_sysex_msg_fragment_overlap
,
117 &hf_sysex_msg_fragment_overlap_conflicts
,
118 &hf_sysex_msg_fragment_multiple_tails
,
119 &hf_sysex_msg_fragment_too_long_fragment
,
120 &hf_sysex_msg_fragment_error
,
121 &hf_sysex_msg_fragment_count
,
122 /* Reassembled in field */
123 &hf_sysex_msg_reassembled_in
,
124 /* Reassembled length field */
125 &hf_sysex_msg_reassembled_length
,
126 &hf_sysex_msg_reassembled_data
,
131 static inline gboolean
132 is_sysex_code(guint8 code
)
134 return (code
== 0x04 || code
== 0x05 || code
== 0x06 || code
== 0x07);
138 is_last_sysex_packet_in_tvb(tvbuff_t
*tvb
, gint offset
)
140 gboolean last
= TRUE
;
141 gint length
= tvb_length(tvb
);
144 while (offset
< length
)
146 guint8 code
= tvb_get_guint8(tvb
, offset
);
149 if (is_sysex_code(code
))
162 dissect_usb_midi_event(tvbuff_t
*tvb
, packet_info
*pinfo
,
163 proto_tree
*usb_audio_tree
, proto_tree
*parent_tree
,
168 gboolean save_fragmented
;
169 proto_tree
*tree
= NULL
;
171 col_set_str(pinfo
->cinfo
, COL_INFO
, "USB-MIDI Event Packets");
173 code
= tvb_get_guint8(tvb
, offset
);
174 cable
= (code
& 0xF0) >> 4;
179 proto_item
*ti
= NULL
;
181 ti
= proto_tree_add_protocol_format(usb_audio_tree
, proto_usb_audio
, tvb
, offset
, 4, "USB Midi Event Packet");
182 tree
= proto_item_add_subtree(ti
, ett_usb_audio
);
183 proto_tree_add_item(tree
, hf_midi_cable_number
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
184 proto_tree_add_item(tree
, hf_midi_code_index
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
185 proto_tree_add_item(tree
, hf_midi_event
, tvb
, offset
+1, 3, ENC_BIG_ENDIAN
);
188 save_fragmented
= pinfo
->fragmented
;
190 /* Reassemble SysEx commands */
191 if (is_sysex_code(code
))
193 tvbuff_t
* new_tvb
= NULL
;
194 fragment_head
*frag_sysex_msg
= NULL
;
196 pinfo
->fragmented
= TRUE
;
200 frag_sysex_msg
= fragment_add_seq_next(&midi_data_reassembly_table
,
203 cable
, /* ID for fragments belonging together */
210 frag_sysex_msg
= fragment_add_seq_next(&midi_data_reassembly_table
,
213 cable
, /* ID for fragments belonging together */
219 if (is_last_sysex_packet_in_tvb(tvb
, offset
))
221 new_tvb
= process_reassembled_data(tvb
, offset
+1, pinfo
,
222 "Reassembled Message", frag_sysex_msg
, &sysex_msg_frag_items
,
223 NULL
, usb_audio_tree
);
225 if (code
!= 0x04) { /* Reassembled */
226 col_append_str(pinfo
->cinfo
, COL_INFO
,
227 " (SysEx Reassembled)");
228 } else { /* Not last packet of reassembled Short Message */
229 col_append_str(pinfo
->cinfo
, COL_INFO
,
230 " (SysEx fragment)");
235 call_dissector(sysex_handle
, new_tvb
, pinfo
, parent_tree
);
240 pinfo
->fragmented
= save_fragmented
;
245 dissect_usb_audio_descriptor(tvbuff_t
*tvb
, packet_info
*pinfo _U_
,
246 proto_tree
*tree
, void *data _U_
)
249 guint8 descriptor_len
;
250 guint8 descriptor_type
;
251 proto_item
*item
= NULL
;
252 proto_tree
*desc_tree
;
254 descriptor_len
= tvb_get_guint8(tvb
, offset
);
255 descriptor_type
= tvb_get_guint8(tvb
, offset
+1);
257 if (descriptor_type
== CS_INTERFACE
) {
258 item
= proto_tree_add_text(tree
, tvb
, offset
, descriptor_len
,
259 "AUDIO CONTROL INTERFACE DESCRIPTOR");
261 else if (descriptor_type
== CS_ENDPOINT
) {
262 item
= proto_tree_add_text(tree
, tvb
, offset
, descriptor_len
,
263 "AUDIO CONTROL ENDPOINT DESCRIPTOR");
268 desc_tree
= proto_item_add_subtree(item
, ett_usb_audio_desc
);
270 dissect_usb_descriptor_header(desc_tree
, tvb
, offset
,
271 &aud_descriptor_type_vals_ext
);
273 return descriptor_len
;
277 /* dissector for usb midi bulk data */
279 dissect_usb_audio_bulk(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*parent_tree
, void* data
)
281 usb_conv_info_t
*usb_conv_info
= (usb_conv_info_t
*)data
;
282 proto_tree
*tree
= NULL
;
284 guint length
= tvb_length(tvb
);
286 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "USBAUDIO");
290 proto_item
*ti
= NULL
;
292 ti
= proto_tree_add_protocol_format(parent_tree
, proto_usb_audio
, tvb
, 0, -1, "USB Audio");
293 tree
= proto_item_add_subtree(ti
, ett_usb_audio
);
296 switch (usb_conv_info
->interfaceSubclass
)
298 case AUDIO_SUBCLASS_MIDISTREAMING
:
300 col_set_str(pinfo
->cinfo
, COL_INFO
, "USB-MIDI Event Packets");
302 while (offset
< length
)
304 dissect_usb_midi_event(tvb
, pinfo
, tree
, parent_tree
, offset
);
310 proto_tree_add_expert(tree
, pinfo
, &ei_usb_audio_undecoded
, tvb
, offset
, length
- offset
);
313 return tvb_length(tvb
);
317 midi_data_reassemble_init(void)
319 reassembly_table_init(&midi_data_reassembly_table
,
320 &addresses_reassembly_table_functions
);
324 proto_register_usb_audio(void)
326 static hf_register_info hf
[] = {
327 { &hf_midi_cable_number
,
328 { "Cable Number", "usbaudio.midi.cable_number", FT_UINT8
, BASE_HEX
,
329 NULL
, 0xF0, NULL
, HFILL
}},
330 { &hf_midi_code_index
,
331 { "Code Index", "usbaudio.midi.code_index", FT_UINT8
, BASE_HEX
,
332 VALS(code_index_vals
), 0x0F, NULL
, HFILL
}},
334 { "MIDI Event", "usbaudio.midi.event", FT_UINT24
, BASE_HEX
,
335 NULL
, 0, NULL
, HFILL
}},
337 { &hf_sysex_msg_fragments
,
338 { "Message fragments", "usbaudio.sysex.fragments",
339 FT_NONE
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
340 { &hf_sysex_msg_fragment
,
341 { "Message fragment", "usbaudio.sysex.fragment",
342 FT_FRAMENUM
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
343 { &hf_sysex_msg_fragment_overlap
,
344 { "Message fragment overlap", "usbaudio.sysex.fragment.overlap",
345 FT_BOOLEAN
, 0, NULL
, 0x00, NULL
, HFILL
}},
346 { &hf_sysex_msg_fragment_overlap_conflicts
,
347 { "Message fragment overlapping with conflicting data",
348 "usbaudio.sysex.fragment.overlap.conflicts",
349 FT_BOOLEAN
, 0, NULL
, 0x00, NULL
, HFILL
}},
350 { &hf_sysex_msg_fragment_multiple_tails
,
351 { "Message has multiple tail fragments",
352 "usbaudio.sysex.fragment.multiple_tails",
353 FT_BOOLEAN
, 0, NULL
, 0x00, NULL
, HFILL
}},
354 { &hf_sysex_msg_fragment_too_long_fragment
,
355 { "Message fragment too long", "usbaudio.sysex.fragment.too_long_fragment",
356 FT_BOOLEAN
, 0, NULL
, 0x00, NULL
, HFILL
}},
357 { &hf_sysex_msg_fragment_error
,
358 { "Message defragmentation error", "usbaudio.sysex.fragment.error",
359 FT_FRAMENUM
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
360 { &hf_sysex_msg_fragment_count
,
361 { "Message fragment count", "usbaudio.sysex.fragment.count",
362 FT_UINT32
, BASE_DEC
, NULL
, 0x00, NULL
, HFILL
}},
363 { &hf_sysex_msg_reassembled_in
,
364 { "Reassembled in", "usbaudio.sysex.reassembled.in",
365 FT_FRAMENUM
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
366 { &hf_sysex_msg_reassembled_length
,
367 { "Reassembled length", "usbaudio.sysex.reassembled.length",
368 FT_UINT32
, BASE_DEC
, NULL
, 0x00, NULL
, HFILL
}},
369 { &hf_sysex_msg_reassembled_data
,
370 { "Reassembled data", "usbaudio.sysex.reassembled.data",
371 FT_BYTES
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
374 static gint
*usb_audio_subtrees
[] = {
377 &ett_sysex_msg_fragment
,
378 &ett_sysex_msg_fragments
381 static ei_register_info ei
[] = {
382 { &ei_usb_audio_undecoded
, { "usbaudio.undecoded", PI_UNDECODED
, PI_WARN
, "Not dissected yet (report to wireshark.org)", EXPFILL
}},
385 expert_module_t
* expert_usb_audio
;
387 proto_usb_audio
= proto_register_protocol("USB Audio", "USBAUDIO", "usbaudio");
388 proto_register_field_array(proto_usb_audio
, hf
, array_length(hf
));
389 proto_register_subtree_array(usb_audio_subtrees
, array_length(usb_audio_subtrees
));
390 expert_usb_audio
= expert_register_protocol(proto_usb_audio
);
391 expert_register_field_array(expert_usb_audio
, ei
, array_length(ei
));
392 register_init_routine(&midi_data_reassemble_init
);
394 new_register_dissector("usbaudio", dissect_usb_audio_bulk
, proto_usb_audio
);
398 proto_reg_handoff_usb_audio(void)
400 dissector_handle_t usb_audio_bulk_handle
, usb_audio_descr_handle
;
402 usb_audio_descr_handle
= new_create_dissector_handle(
403 dissect_usb_audio_descriptor
, proto_usb_audio
);
404 dissector_add_uint("usb.descriptor", IF_CLASS_AUDIO
, usb_audio_descr_handle
);
406 usb_audio_bulk_handle
= find_dissector("usbaudio");
407 dissector_add_uint("usb.bulk", IF_CLASS_AUDIO
, usb_audio_bulk_handle
);
409 sysex_handle
= find_dissector("sysex");
417 * indent-tabs-mode: nil
420 * ex: set shiftwidth=4 tabstop=8 expandtab:
421 * :indentSize=4:tabSize=8:noTabs=true: