source: mainline/uspace/app/usbinfo/hid.c@ 184b600

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 184b600 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

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