source: mainline/uspace/lib/usb/src/hidreport.c@ dc4c19e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dc4c19e was 15a9e63, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Bugfix in report descriptor retrieving.

  • Property mode set to 100644
File size: 5.9 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 libusb
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/classes/hidparser.h>
43#include <usb/dp.h>
44#include <usb/devdrv.h>
45#include <usb/pipes.h>
46#include <usb/classes/hid.h>
47#include <usb/descriptor.h>
48#include <usb/request.h>
49
50#include <usb/classes/hidreport.h>
51
52static int 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 = dev->descriptors.configuration,
64 .size = dev->descriptors.configuration_size,
65 .arg = NULL
66 };
67
68 /*
69 * First nested descriptor of the configuration descriptor.
70 */
71 uint8_t *d =
72 usb_dp_get_nested_descriptor(&parser, &parser_data,
73 dev->descriptors.configuration);
74
75 /*
76 * Find the interface descriptor corresponding to our interface number.
77 */
78 int i = 0;
79 while (d != NULL && i < dev->interface_no) {
80 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
81 dev->descriptors.configuration, d);
82 ++i;
83 }
84
85 if (d == NULL) {
86 usb_log_error("The %d. interface descriptor not found!\n",
87 dev->interface_no);
88 return ENOENT;
89 }
90
91 /*
92 * First nested descriptor of the interface descriptor.
93 */
94 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!\n");
107 return ENOENT;
108 }
109
110 if (*d != sizeof(usb_standard_hid_descriptor_t)) {
111 usb_log_error("HID descriptor hass wrong size (%u, expected %u"
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 = hid_desc->report_desc_info.length;
120 size_t actual_size = 0;
121
122 /*
123 * Start session for the control transfer.
124 */
125 int sess_rc = usb_pipe_start_session(&dev->ctrl_pipe);
126 if (sess_rc != EOK) {
127 usb_log_warning("Failed to start a session: %s.\n",
128 str_error(sess_rc));
129 return sess_rc;
130 }
131
132 /*
133 * Allocate space for the report descriptor.
134 */
135 *report_desc = (uint8_t *)malloc(length);
136 if (*report_desc == NULL) {
137 usb_log_error("Failed to allocate space for Report descriptor."
138 "\n");
139 return ENOMEM;
140 }
141
142 usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
143
144 /*
145 * Get the descriptor from the device.
146 */
147 int rc = usb_request_get_descriptor(&dev->ctrl_pipe,
148 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
149 USB_DESCTYPE_HID_REPORT, 0, dev->interface_no,
150 *report_desc, length, &actual_size);
151
152 if (rc != EOK) {
153 free(*report_desc);
154 *report_desc = NULL;
155 return rc;
156 }
157
158 if (actual_size != length) {
159 free(*report_desc);
160 *report_desc = NULL;
161 usb_log_error("Report descriptor has wrong size (%u, expected "
162 "%u)\n", actual_size, length);
163 return EINVAL;
164 }
165
166 /*
167 * End session for the control transfer.
168 */
169 sess_rc = usb_pipe_end_session(&dev->ctrl_pipe);
170 if (sess_rc != EOK) {
171 usb_log_warning("Failed to end a session: %s.\n",
172 str_error(sess_rc));
173 free(*report_desc);
174 *report_desc = NULL;
175 return sess_rc;
176 }
177
178 *size = length;
179
180 usb_log_debug("Done.\n");
181
182 return EOK;
183}
184
185/*----------------------------------------------------------------------------*/
186
187int usb_hid_process_report_descriptor(usb_device_t *dev,
188 usb_hid_report_parser_t *parser)
189{
190 if (dev == NULL || parser == NULL) {
191 usb_log_error("Failed to process Report descriptor: wrong "
192 "parameters given.\n");
193 return EINVAL;
194 }
195
196 uint8_t *report_desc = NULL;
197 size_t report_size;
198
199 int rc = usb_hid_get_report_descriptor(dev, &report_desc,
200 &report_size);
201
202 if (rc != EOK) {
203 usb_log_error("Problem with getting Report descriptor: %s.\n",
204 str_error(rc));
205 if (report_desc != NULL) {
206 free(report_desc);
207 }
208 return rc;
209 }
210
211 assert(report_desc != NULL);
212
213 rc = usb_hid_parse_report_descriptor(parser, report_desc, report_size);
214 if (rc != EOK) {
215 usb_log_error("Problem parsing Report descriptor: %s.\n",
216 str_error(rc));
217 free(report_desc);
218 return rc;
219 }
220
221 usb_hid_descriptor_print(parser);
222 free(report_desc);
223
224 return EOK;
225}
226
227/**
228 * @}
229 */
Note: See TracBrowser for help on using the repository browser.