source: mainline/uspace/drv/bus/usb/usbmid/dump.c

Last change on this file was ae3a941, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usb: cstyle

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbmid
30 * @{
31 */
32/**
33 * @file
34 * Dumping and debugging functions.
35 */
36#include <errno.h>
37#include <str_error.h>
38#include <stdlib.h>
39#include <usb/dev/pipes.h>
40#include <usb/dev/dp.h>
41#include <usb/classes/classes.h>
42#include "usbmid.h"
43
44/** Dump found descriptor.
45 *
46 * @param data Descriptor data.
47 * @param depth Nesting depth.
48 */
49static void dump_tree_descriptor(const uint8_t *data, size_t depth)
50{
51 if (data == NULL) {
52 return;
53 }
54 const int type = data[1];
55 if (type == USB_DESCTYPE_INTERFACE) {
56 usb_standard_interface_descriptor_t *descriptor =
57 (usb_standard_interface_descriptor_t *) data;
58 usb_log_info("Found interface: %s (0x%02x/0x%02x/0x%02x).",
59 usb_str_class(descriptor->interface_class),
60 (int) descriptor->interface_class,
61 (int) descriptor->interface_subclass,
62 (int) descriptor->interface_protocol);
63 }
64}
65
66/** Dump tree of descriptors.
67 *
68 * @param parser Descriptor parser.
69 * @param data Descriptor parser data.
70 * @param root Pointer to current root.
71 * @param depth Nesting depth.
72 */
73static void dump_tree_internal(
74 usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
75 const uint8_t *root, size_t depth)
76{
77 if (root == NULL) {
78 return;
79 }
80 dump_tree_descriptor(root, depth);
81 const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
82 do {
83 dump_tree_internal(parser, data, child, depth + 1);
84 child = usb_dp_get_sibling_descriptor(parser, data, root, child);
85 } while (child != NULL);
86}
87
88/** Dump descriptor tree.
89 *
90 * @param parser Descriptor parser.
91 * @param data Descriptor parser data.
92 */
93static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
94{
95 const uint8_t *ptr = data->data;
96 dump_tree_internal(parser, data, ptr, 0);
97}
98
99/** Dump given descriptors.
100 *
101 * @param descriptors Descriptors buffer (typically full config descriptor).
102 * @param length Size of @p descriptors buffer in bytes.
103 */
104void usbmid_dump_descriptors(uint8_t *descriptors, size_t length)
105{
106 usb_dp_parser_data_t data = {
107 .data = descriptors,
108 .size = length,
109 .arg = NULL
110 };
111
112 usb_dp_parser_t parser = {
113 .nesting = usb_dp_standard_descriptor_nesting
114 };
115
116 dump_tree(&parser, &data);
117}
118
119/**
120 * @}
121 */
Note: See TracBrowser for help on using the repository browser.