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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e9c02b7 was e9c02b7, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

More human-readable HID report printing

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