source: mainline/uspace/lib/usbhid/src/hidreport.c@ e0a4686

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e0a4686 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak
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 libusbhid
30 * @{
31 */
32/**
33 * @file
34 * USB HID keyboard device structure and API.
35 */
36
37#include <assert.h>
38#include <errno.h>
39#include <str_error.h>
40
41#include <usb/debug.h>
42#include <usb/hid/hidparser.h>
43#include <usb/dev/dp.h>
44#include <usb/dev/driver.h>
45#include <usb/dev/pipes.h>
46#include <usb/hid/hid.h>
47#include <usb/descriptor.h>
48#include <usb/dev/request.h>
49
50#include <usb/hid/hidreport.h>
51
52static errno_t usb_hid_get_report_descriptor(usb_device_t *dev,
53 uint8_t **report_desc, size_t *size)
54{
55 assert(report_desc != NULL);
56 assert(size != NULL);
57
58 usb_dp_parser_t parser = {
59 .nesting = usb_dp_standard_descriptor_nesting
60 };
61
62 usb_dp_parser_data_t parser_data = {
63 .data = usb_device_descriptors(dev)->full_config,
64 .size = usb_device_descriptors(dev)->full_config_size,
65 .arg = NULL
66 };
67
68 /*
69 * First nested descriptor of the configuration descriptor.
70 */
71 const uint8_t *d =
72 usb_dp_get_nested_descriptor(&parser, &parser_data,
73 usb_device_descriptors(dev)->full_config);
74
75 /*
76 * Find the interface descriptor corresponding to our interface number.
77 */
78 int i = 0;
79 while (d != NULL && i < usb_device_get_iface_number(dev)) {
80 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
81 usb_device_descriptors(dev)->full_config, d);
82 ++i;
83 }
84
85 if (d == NULL) {
86 usb_log_error("The %d. interface descriptor not found!",
87 usb_device_get_iface_number(dev));
88 return ENOENT;
89 }
90
91 /*
92 * First nested descriptor of the interface descriptor.
93 */
94 const uint8_t *iface_desc = d;
95 d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
96
97 /*
98 * Search through siblings until the HID descriptor is found.
99 */
100 while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
101 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
102 iface_desc, d);
103 }
104
105 if (d == NULL) {
106 usb_log_fatal("No HID descriptor found!");
107 return ENOENT;
108 }
109
110 if (*d != sizeof(usb_standard_hid_descriptor_t)) {
111 usb_log_error("HID descriptor has wrong size (%u, expected %zu"
112 ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
113 return EINVAL;
114 }
115
116 usb_standard_hid_descriptor_t *hid_desc =
117 (usb_standard_hid_descriptor_t *)d;
118
119 uint16_t length = uint16_usb2host(hid_desc->report_desc_info.length);
120 size_t actual_size = 0;
121
122 /*
123 * Allocate space for the report descriptor.
124 */
125 *report_desc = (uint8_t *)malloc(length);
126 if (*report_desc == NULL) {
127 usb_log_error("Failed to allocate space for Report descriptor."
128 "\n");
129 return ENOMEM;
130 }
131
132 usb_log_debug("Getting Report descriptor, expected size: %u", length);
133
134 /*
135 * Get the descriptor from the device.
136 */
137 errno_t rc = usb_request_get_descriptor(usb_device_get_default_pipe(dev),
138 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
139 USB_DESCTYPE_HID_REPORT, 0, usb_device_get_iface_number(dev),
140 *report_desc, length, &actual_size);
141
142 if (rc != EOK) {
143 free(*report_desc);
144 *report_desc = NULL;
145 return rc;
146 }
147
148 if (actual_size != length) {
149 free(*report_desc);
150 *report_desc = NULL;
151 usb_log_error("Report descriptor has wrong size (%zu, expected "
152 "%u)\n", actual_size, length);
153 return EINVAL;
154 }
155
156 *size = length;
157
158 usb_log_debug("Done.");
159
160 return EOK;
161}
162
163
164
165errno_t usb_hid_process_report_descriptor(usb_device_t *dev,
166 usb_hid_report_t *report, uint8_t **report_desc, size_t *report_size)
167{
168 if (dev == NULL || report == NULL) {
169 usb_log_error("Failed to process Report descriptor: wrong "
170 "parameters given.\n");
171 return EINVAL;
172 }
173
174// uint8_t *report_desc = NULL;
175// size_t report_size;
176
177 errno_t rc = usb_hid_get_report_descriptor(dev, report_desc, report_size);
178
179 if (rc != EOK) {
180 usb_log_error("Problem with getting Report descriptor: %s.",
181 str_error(rc));
182 if (*report_desc != NULL) {
183 free(*report_desc);
184 *report_desc = NULL;
185 }
186 return rc;
187 }
188
189 assert(*report_desc != NULL);
190
191 rc = usb_hid_parse_report_descriptor(report, *report_desc, *report_size);
192 if (rc != EOK) {
193 usb_log_error("Problem parsing Report descriptor: %s.",
194 str_error(rc));
195 free(*report_desc);
196 *report_desc = NULL;
197 return rc;
198 }
199
200 usb_hid_descriptor_print(report);
201
202 return EOK;
203}
204
205/**
206 * @}
207 */
Note: See TracBrowser for help on using the repository browser.