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

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

usbinfo can print (raw) HID report descriptor

  • Property mode set to 100644
File size: 4.8 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 <errno.h>
41#include "usbinfo.h"
42
43#define BYTES_PER_LINE 20
44
45typedef struct {
46 usbinfo_device_t *dev;
47 usb_standard_interface_descriptor_t *last_iface;
48} descriptor_walk_context_t;
49
50static bool is_descriptor_kind(uint8_t *d, usb_descriptor_type_t t)
51{
52 if (d == NULL) {
53 return false;
54 }
55 uint8_t size = d[0];
56 if (size <= 1) {
57 return false;
58 }
59 uint8_t type = d[1];
60 return type == t;
61}
62
63/** Retrieves HID report from given USB device and dumps it.
64 *
65 * @param ctrl_pipe Default control pipe to the device.
66 * @param iface_no Interface number.
67 * @param report_size Size of the report descriptor.
68 */
69static void retrieve_and_dump_hid_report(usb_pipe_t *ctrl_pipe,
70 uint8_t iface_no, size_t report_size)
71{
72 assert(report_size > 0);
73
74 uint8_t *report = malloc(report_size);
75 if (report == NULL) {
76 usb_log_warning(
77 "Failed to allocate %zuB, skipping interface %d.\n",
78 report_size, (int) iface_no);
79 return;
80 }
81
82 size_t actual_report_size;
83 int rc = usb_request_get_descriptor(ctrl_pipe,
84 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
85 USB_DESCTYPE_HID_REPORT, 0, iface_no,
86 report, report_size, &actual_report_size);
87 if (rc != EOK) {
88 usb_log_error("Failed to retrieve HID report descriptor: %s.\n",
89 str_error(rc));
90 free(report);
91 return;
92 }
93
94 printf("%sHID report descriptor for interface %d", get_indent(0),
95 (int) iface_no);
96 for (size_t i = 0; i < report_size; i++) {
97 size_t line_idx = i % BYTES_PER_LINE;
98 if (line_idx == 0) {
99 printf("\n%s", get_indent(1));
100 }
101 printf("%02X", (int) report[i]);
102 if (line_idx + 1 < BYTES_PER_LINE) {
103 printf(" ");
104 }
105 }
106 printf("\n");
107
108 free(report);
109}
110
111/** Callback for walking descriptor tree.
112 * This callback remembers current interface and dumps HID report after
113 * encountering HID descriptor.
114 * It dumps only the first report and it expects it to be a normal
115 * report, not a physical one.
116 *
117 * @param raw_descriptor Descriptor as a byte array.
118 * @param depth Descriptor tree depth (currently ignored).
119 * @param arg Custom argument, passed as descriptor_walk_context_t.
120 */
121static void descriptor_walk_callback(uint8_t *raw_descriptor,
122 size_t depth, void *arg)
123{
124 descriptor_walk_context_t *context = (descriptor_walk_context_t *) arg;
125
126 if (is_descriptor_kind(raw_descriptor, USB_DESCTYPE_INTERFACE)) {
127 context->last_iface
128 = (usb_standard_interface_descriptor_t *) raw_descriptor;
129 return;
130 }
131
132 if (!is_descriptor_kind(raw_descriptor, USB_DESCTYPE_HID)) {
133 return;
134 }
135
136 if (context->last_iface == NULL) {
137 return;
138 }
139
140 usb_standard_hid_descriptor_t *hid_descr
141 = (usb_standard_hid_descriptor_t *) raw_descriptor;
142
143 if (hid_descr->report_desc_info.type != USB_DESCTYPE_HID_REPORT) {
144 return;
145 }
146
147 size_t report_size = hid_descr->report_desc_info.length;
148
149 if (report_size == 0) {
150 return;
151 }
152
153 retrieve_and_dump_hid_report(&context->dev->ctrl_pipe,
154 context->last_iface->interface_number, report_size);
155}
156
157
158void dump_hidreport(usbinfo_device_t *dev)
159{
160 descriptor_walk_context_t context = {
161 .dev = dev,
162 .last_iface = NULL
163 };
164
165 usb_dp_walk_simple(dev->full_configuration_descriptor,
166 dev->full_configuration_descriptor_size,
167 usb_dp_standard_descriptor_nesting,
168 descriptor_walk_callback, &context);
169}
170
171/** @}
172 */
Note: See TracBrowser for help on using the repository browser.