source: mainline/uspace/app/usbinfo/hid.c@ e242fba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e242fba was e2dfa86, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

usb_device: Change API to allow direct(RO) access to descriptors.

  • Property mode set to 100644
File size: 7.2 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/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
47typedef enum {
48 HID_DUMP_RAW,
49 HID_DUMP_USAGES
50} hid_dump_type_t;
51
52typedef 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
58static 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 */
77static 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 */
99static 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, report_it) {
104 usb_hid_report_description_t *description = list_get_instance(
105 report_it, usb_hid_report_description_t, reports_link);
106 printf("%sReport %d (type %d)\n", get_indent(1),
107 (int) description->report_id,
108 (int) description->type);
109 list_foreach(description->report_items, item_it) {
110 usb_hid_report_field_t *field = list_get_instance(
111 item_it, usb_hid_report_field_t, ritems_link);
112 printf("%sUsage page = 0x%04x Usage = 0x%04x\n",
113 get_indent(2),
114 (int) field->usage_page, (int) field->usage);
115 }
116 }
117}
118
119/** Retrieves HID report from given USB device and dumps it.
120 *
121 * @param dump_type In which format to dump the report.
122 * @param ctrl_pipe Default control pipe to the device.
123 * @param iface_no Interface number.
124 * @param report_size Size of the report descriptor.
125 */
126static void retrieve_and_dump_hid_report(hid_dump_type_t dump_type,
127 usb_pipe_t *ctrl_pipe, uint8_t iface_no, size_t report_size)
128{
129 assert(report_size > 0);
130
131 uint8_t *raw_report = malloc(report_size);
132 if (raw_report == NULL) {
133 usb_log_warning(
134 "Failed to allocate %zuB, skipping interface %d.\n",
135 report_size, (int) iface_no);
136 return;
137 }
138
139 size_t actual_report_size;
140 int rc = usb_request_get_descriptor(ctrl_pipe,
141 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
142 USB_DESCTYPE_HID_REPORT, 0, iface_no,
143 raw_report, report_size, &actual_report_size);
144 if (rc != EOK) {
145 usb_log_error("Failed to retrieve HID report descriptor: %s.\n",
146 str_error(rc));
147 free(raw_report);
148 return;
149 }
150
151 usb_hid_report_t report;
152 rc = usb_hid_parse_report_descriptor(&report, raw_report, report_size);
153 if (rc != EOK) {
154 usb_log_error("Failed to part report descriptor: %s.\n",
155 str_error(rc));
156 }
157
158 switch (dump_type) {
159 case HID_DUMP_RAW:
160 dump_hid_report_raw(iface_no, raw_report, report_size);
161 break;
162 case HID_DUMP_USAGES:
163 dump_hid_report_usages(iface_no, &report);
164 break;
165 default:
166 assert(false && "unreachable code apparently reached");
167 }
168
169 free(raw_report);
170 usb_hid_report_deinit(&report);
171}
172
173/** Callback for walking descriptor tree.
174 * This callback remembers current interface and dumps HID report after
175 * encountering HID descriptor.
176 * It dumps only the first report and it expects it to be a normal
177 * report, not a physical one.
178 *
179 * @param raw_descriptor Descriptor as a byte array.
180 * @param depth Descriptor tree depth (currently ignored).
181 * @param arg Custom argument, passed as descriptor_walk_context_t.
182 */
183static void descriptor_walk_callback(const uint8_t *raw_descriptor,
184 size_t depth, void *arg)
185{
186 descriptor_walk_context_t *context = (descriptor_walk_context_t *) arg;
187
188 if (is_descriptor_kind(raw_descriptor, USB_DESCTYPE_INTERFACE)) {
189 context->last_iface
190 = (usb_standard_interface_descriptor_t *) raw_descriptor;
191 return;
192 }
193
194 if (!is_descriptor_kind(raw_descriptor, USB_DESCTYPE_HID)) {
195 return;
196 }
197
198 if (context->last_iface == NULL) {
199 return;
200 }
201
202 usb_standard_hid_descriptor_t *hid_descr
203 = (usb_standard_hid_descriptor_t *) raw_descriptor;
204
205 if (hid_descr->report_desc_info.type != USB_DESCTYPE_HID_REPORT) {
206 return;
207 }
208
209 size_t report_size = hid_descr->report_desc_info.length;
210
211 if (report_size == 0) {
212 return;
213 }
214
215 retrieve_and_dump_hid_report(context->dump_type,
216 usb_device_get_default_pipe(context->usb_dev),
217 context->last_iface->interface_number, report_size);
218}
219
220
221void dump_hidreport_raw(usb_device_t *usb_dev)
222{
223 descriptor_walk_context_t context = {
224 .usb_dev = usb_dev,
225 .dump_type = HID_DUMP_RAW,
226 .last_iface = NULL
227 };
228
229 usb_dp_walk_simple(
230 usb_device_descriptors(usb_dev)->full_config,
231 usb_device_descriptors(usb_dev)->full_config_size,
232 usb_dp_standard_descriptor_nesting,
233 descriptor_walk_callback, &context);
234}
235
236void dump_hidreport_usages(usb_device_t *usb_dev)
237{
238 descriptor_walk_context_t context = {
239 .usb_dev = usb_dev,
240 .dump_type = HID_DUMP_USAGES,
241 .last_iface = NULL
242 };
243
244 usb_dp_walk_simple(
245 usb_device_descriptors(usb_dev)->full_config,
246 usb_device_descriptors(usb_dev)->full_config_size,
247 usb_dp_standard_descriptor_nesting,
248 descriptor_walk_callback, &context);
249}
250
251/** @}
252 */
Note: See TracBrowser for help on using the repository browser.