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 usbinfo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Dumping of HID-related properties.
|
---|
35 | */
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 | #include <usb/classes/classes.h>
|
---|
40 | #include <usb/dev/request.h>
|
---|
41 | #include <usb/hid/hidparser.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include "usbinfo.h"
|
---|
44 |
|
---|
45 | #define BYTES_PER_LINE 20
|
---|
46 |
|
---|
47 | typedef enum {
|
---|
48 | HID_DUMP_RAW,
|
---|
49 | HID_DUMP_USAGES
|
---|
50 | } hid_dump_type_t;
|
---|
51 |
|
---|
52 | typedef struct {
|
---|
53 | usb_device_t *usb_dev;
|
---|
54 | hid_dump_type_t dump_type;
|
---|
55 | usb_standard_interface_descriptor_t *last_iface;
|
---|
56 | } descriptor_walk_context_t;
|
---|
57 |
|
---|
58 | static bool is_descriptor_kind(const uint8_t *d, usb_descriptor_type_t t)
|
---|
59 | {
|
---|
60 | if (d == NULL) {
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | uint8_t size = d[0];
|
---|
64 | if (size <= 1) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | uint8_t type = d[1];
|
---|
68 | return type == t;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Dumps HID report in raw format.
|
---|
72 | *
|
---|
73 | * @param iface_no USB interface the report belongs to.
|
---|
74 | * @param report Report descriptor.
|
---|
75 | * @param size Size of the @p report in bytes.
|
---|
76 | */
|
---|
77 | static void dump_hid_report_raw(int iface_no, uint8_t *report, size_t size)
|
---|
78 | {
|
---|
79 | printf("%sHID report descriptor for interface %d", get_indent(0),
|
---|
80 | iface_no);
|
---|
81 | for (size_t i = 0; i < size; i++) {
|
---|
82 | size_t line_idx = i % BYTES_PER_LINE;
|
---|
83 | if (line_idx == 0) {
|
---|
84 | printf("\n%s", get_indent(1));
|
---|
85 | }
|
---|
86 | printf("%02X", (int) report[i]);
|
---|
87 | if (line_idx + 1 < BYTES_PER_LINE) {
|
---|
88 | printf(" ");
|
---|
89 | }
|
---|
90 | }
|
---|
91 | printf("\n");
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Dumps usages in HID report.
|
---|
95 | *
|
---|
96 | * @param iface_no USB interface the report belongs to.
|
---|
97 | * @param report Parsed report descriptor.
|
---|
98 | */
|
---|
99 | static void dump_hid_report_usages(int iface_no, usb_hid_report_t *report)
|
---|
100 | {
|
---|
101 | printf("%sParsed HID report descriptor for interface %d\n",
|
---|
102 | get_indent(0), iface_no);
|
---|
103 | list_foreach(report->reports, reports_link,
|
---|
104 | usb_hid_report_description_t, description) {
|
---|
105 | printf("%sReport %d (type %d)\n", get_indent(1),
|
---|
106 | (int) description->report_id,
|
---|
107 | (int) description->type);
|
---|
108 | list_foreach(description->report_items, ritems_link,
|
---|
109 | usb_hid_report_field_t, field) {
|
---|
110 | printf("%sUsage page = 0x%04x Usage = 0x%04x\n",
|
---|
111 | get_indent(2),
|
---|
112 | (int) field->usage_page, (int) field->usage);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Retrieves HID report from given USB device and dumps it.
|
---|
118 | *
|
---|
119 | * @param dump_type In which format to dump the report.
|
---|
120 | * @param ctrl_pipe Default control pipe to the device.
|
---|
121 | * @param iface_no Interface number.
|
---|
122 | * @param report_size Size of the report descriptor.
|
---|
123 | */
|
---|
124 | static void retrieve_and_dump_hid_report(hid_dump_type_t dump_type,
|
---|
125 | usb_pipe_t *ctrl_pipe, uint8_t iface_no, size_t report_size)
|
---|
126 | {
|
---|
127 | assert(report_size > 0);
|
---|
128 |
|
---|
129 | uint8_t *raw_report = malloc(report_size);
|
---|
130 | if (raw_report == NULL) {
|
---|
131 | usb_log_warning(
|
---|
132 | "Failed to allocate %zuB, skipping interface %d.\n",
|
---|
133 | report_size, (int) iface_no);
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | size_t actual_report_size;
|
---|
138 | int rc = usb_request_get_descriptor(ctrl_pipe,
|
---|
139 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
140 | USB_DESCTYPE_HID_REPORT, 0, iface_no,
|
---|
141 | raw_report, report_size, &actual_report_size);
|
---|
142 | if (rc != EOK) {
|
---|
143 | usb_log_error("Failed to retrieve HID report descriptor: %s.\n",
|
---|
144 | str_error(rc));
|
---|
145 | free(raw_report);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | usb_hid_report_t report;
|
---|
150 | rc = usb_hid_parse_report_descriptor(&report, raw_report, report_size);
|
---|
151 | if (rc != EOK) {
|
---|
152 | usb_log_error("Failed to part report descriptor: %s.\n",
|
---|
153 | str_error(rc));
|
---|
154 | }
|
---|
155 |
|
---|
156 | switch (dump_type) {
|
---|
157 | case HID_DUMP_RAW:
|
---|
158 | dump_hid_report_raw(iface_no, raw_report, report_size);
|
---|
159 | break;
|
---|
160 | case HID_DUMP_USAGES:
|
---|
161 | dump_hid_report_usages(iface_no, &report);
|
---|
162 | break;
|
---|
163 | default:
|
---|
164 | assert(false && "unreachable code apparently reached");
|
---|
165 | }
|
---|
166 |
|
---|
167 | free(raw_report);
|
---|
168 | usb_hid_report_deinit(&report);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Callback for walking descriptor tree.
|
---|
172 | * This callback remembers current interface and dumps HID report after
|
---|
173 | * encountering HID descriptor.
|
---|
174 | * It dumps only the first report and it expects it to be a normal
|
---|
175 | * report, not a physical one.
|
---|
176 | *
|
---|
177 | * @param raw_descriptor Descriptor as a byte array.
|
---|
178 | * @param depth Descriptor tree depth (currently ignored).
|
---|
179 | * @param arg Custom argument, passed as descriptor_walk_context_t.
|
---|
180 | */
|
---|
181 | static void descriptor_walk_callback(const uint8_t *raw_descriptor,
|
---|
182 | size_t depth, void *arg)
|
---|
183 | {
|
---|
184 | descriptor_walk_context_t *context = (descriptor_walk_context_t *) arg;
|
---|
185 |
|
---|
186 | if (is_descriptor_kind(raw_descriptor, USB_DESCTYPE_INTERFACE)) {
|
---|
187 | context->last_iface
|
---|
188 | = (usb_standard_interface_descriptor_t *) raw_descriptor;
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (!is_descriptor_kind(raw_descriptor, USB_DESCTYPE_HID)) {
|
---|
193 | return;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (context->last_iface == NULL) {
|
---|
197 | return;
|
---|
198 | }
|
---|
199 |
|
---|
200 | usb_standard_hid_descriptor_t *hid_descr
|
---|
201 | = (usb_standard_hid_descriptor_t *) raw_descriptor;
|
---|
202 |
|
---|
203 | if (hid_descr->report_desc_info.type != USB_DESCTYPE_HID_REPORT) {
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | size_t report_size = hid_descr->report_desc_info.length;
|
---|
208 |
|
---|
209 | if (report_size == 0) {
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | retrieve_and_dump_hid_report(context->dump_type,
|
---|
214 | usb_device_get_default_pipe(context->usb_dev),
|
---|
215 | context->last_iface->interface_number, report_size);
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | void dump_hidreport_raw(usb_device_t *usb_dev)
|
---|
220 | {
|
---|
221 | descriptor_walk_context_t context = {
|
---|
222 | .usb_dev = usb_dev,
|
---|
223 | .dump_type = HID_DUMP_RAW,
|
---|
224 | .last_iface = NULL
|
---|
225 | };
|
---|
226 |
|
---|
227 | usb_dp_walk_simple(
|
---|
228 | usb_device_descriptors(usb_dev)->full_config,
|
---|
229 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
230 | usb_dp_standard_descriptor_nesting,
|
---|
231 | descriptor_walk_callback, &context);
|
---|
232 | }
|
---|
233 |
|
---|
234 | void dump_hidreport_usages(usb_device_t *usb_dev)
|
---|
235 | {
|
---|
236 | descriptor_walk_context_t context = {
|
---|
237 | .usb_dev = usb_dev,
|
---|
238 | .dump_type = HID_DUMP_USAGES,
|
---|
239 | .last_iface = NULL
|
---|
240 | };
|
---|
241 |
|
---|
242 | usb_dp_walk_simple(
|
---|
243 | usb_device_descriptors(usb_dev)->full_config,
|
---|
244 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
245 | usb_dp_standard_descriptor_nesting,
|
---|
246 | descriptor_walk_callback, &context);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** @}
|
---|
250 | */
|
---|