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 drvusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Generic USB HID device structure and API.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <str_error.h>
|
---|
40 |
|
---|
41 | #include <ddf/driver.h>
|
---|
42 |
|
---|
43 | #include <usb/dp.h>
|
---|
44 | #include <usb/debug.h>
|
---|
45 | #include <usb/request.h>
|
---|
46 | #include <usb/descriptor.h>
|
---|
47 | #include <usb/classes/hid.h>
|
---|
48 | #include <usb/pipes.h>
|
---|
49 |
|
---|
50 | #include "hiddev.h"
|
---|
51 |
|
---|
52 | /*----------------------------------------------------------------------------*/
|
---|
53 | /* Non-API functions */
|
---|
54 | /*----------------------------------------------------------------------------*/
|
---|
55 |
|
---|
56 | static int usbhid_dev_get_report_descriptor(usbhid_dev_t *hid_dev,
|
---|
57 | uint8_t *config_desc, size_t config_desc_size, uint8_t *iface_desc)
|
---|
58 | {
|
---|
59 | assert(hid_dev != NULL);
|
---|
60 | assert(config_desc != NULL);
|
---|
61 | assert(config_desc_size != 0);
|
---|
62 | assert(iface_desc != NULL);
|
---|
63 |
|
---|
64 | usb_dp_parser_t parser = {
|
---|
65 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
66 | };
|
---|
67 |
|
---|
68 | usb_dp_parser_data_t parser_data = {
|
---|
69 | .data = config_desc,
|
---|
70 | .size = config_desc_size,
|
---|
71 | .arg = NULL
|
---|
72 | };
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * First nested descriptor of interface descriptor.
|
---|
76 | */
|
---|
77 | uint8_t *d =
|
---|
78 | usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Search through siblings until the HID descriptor is found.
|
---|
82 | */
|
---|
83 | while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
|
---|
84 | d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
|
---|
85 | iface_desc, d);
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (d == NULL) {
|
---|
89 | usb_log_fatal("No HID descriptor found!\n");
|
---|
90 | return ENOENT;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (*d != sizeof(usb_standard_hid_descriptor_t)) {
|
---|
94 | usb_log_fatal("HID descriptor hass wrong size (%u, expected %u"
|
---|
95 | ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
|
---|
96 | return EINVAL;
|
---|
97 | }
|
---|
98 |
|
---|
99 | usb_standard_hid_descriptor_t *hid_desc =
|
---|
100 | (usb_standard_hid_descriptor_t *)d;
|
---|
101 |
|
---|
102 | uint16_t length = hid_desc->report_desc_info.length;
|
---|
103 | size_t actual_size = 0;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Allocate space for the report descriptor.
|
---|
107 | */
|
---|
108 | hid_dev->report_desc = (uint8_t *)malloc(length);
|
---|
109 | if (hid_dev->report_desc == NULL) {
|
---|
110 | usb_log_fatal("Failed to allocate space for Report descriptor."
|
---|
111 | "\n");
|
---|
112 | return ENOMEM;
|
---|
113 | }
|
---|
114 |
|
---|
115 | usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Get the descriptor from the device.
|
---|
119 | */
|
---|
120 | int rc = usb_request_get_descriptor(&hid_dev->ctrl_pipe,
|
---|
121 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
122 | USB_DESCTYPE_HID_REPORT, 0,
|
---|
123 | hid_dev->iface, hid_dev->report_desc, length, &actual_size);
|
---|
124 |
|
---|
125 | if (rc != EOK) {
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (actual_size != length) {
|
---|
130 | free(hid_dev->report_desc);
|
---|
131 | hid_dev->report_desc = NULL;
|
---|
132 | usb_log_fatal("Report descriptor has wrong size (%u, expected "
|
---|
133 | "%u)\n", actual_size, length);
|
---|
134 | return EINVAL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | usb_log_debug("Done.\n");
|
---|
138 |
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*----------------------------------------------------------------------------*/
|
---|
143 |
|
---|
144 | static int usbhid_dev_process_descriptors(usbhid_dev_t *hid_dev,
|
---|
145 | usb_endpoint_description_t *poll_ep_desc)
|
---|
146 | {
|
---|
147 | assert(hid_dev != NULL);
|
---|
148 |
|
---|
149 | usb_log_info("Processing descriptors...\n");
|
---|
150 |
|
---|
151 | int rc;
|
---|
152 |
|
---|
153 | uint8_t *descriptors = NULL;
|
---|
154 | size_t descriptors_size;
|
---|
155 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
156 | &hid_dev->ctrl_pipe, 0, (void **) &descriptors, &descriptors_size);
|
---|
157 | if (rc != EOK) {
|
---|
158 | usb_log_error("Failed to retrieve config descriptor: %s.\n",
|
---|
159 | str_error(rc));
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Initialize the interrupt in endpoint.
|
---|
165 | */
|
---|
166 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
---|
167 | {
|
---|
168 | .pipe = &hid_dev->poll_pipe,
|
---|
169 | .description = poll_ep_desc,
|
---|
170 | .interface_no =
|
---|
171 | usb_device_get_assigned_interface(hid_dev->device)
|
---|
172 | }
|
---|
173 | };
|
---|
174 |
|
---|
175 | rc = usb_endpoint_pipe_initialize_from_configuration(
|
---|
176 | endpoint_mapping, 1, descriptors, descriptors_size,
|
---|
177 | &hid_dev->wire);
|
---|
178 |
|
---|
179 | if (rc != EOK) {
|
---|
180 | usb_log_error("Failed to initialize poll pipe: %s.\n",
|
---|
181 | str_error(rc));
|
---|
182 | free(descriptors);
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (!endpoint_mapping[0].present) {
|
---|
187 | usb_log_warning("Not accepting device.\n");
|
---|
188 | free(descriptors);
|
---|
189 | return EREFUSED; // probably not very good return value
|
---|
190 | }
|
---|
191 |
|
---|
192 | usb_log_debug("Accepted device. Saving interface, and getting Report"
|
---|
193 | " descriptor.\n");
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Save assigned interface number.
|
---|
197 | */
|
---|
198 | if (endpoint_mapping[0].interface_no < 0) {
|
---|
199 | usb_log_error("Bad interface number.\n");
|
---|
200 | free(descriptors);
|
---|
201 | return EINVAL;
|
---|
202 | }
|
---|
203 |
|
---|
204 | hid_dev->iface = endpoint_mapping[0].interface_no;
|
---|
205 |
|
---|
206 | assert(endpoint_mapping[0].interface != NULL);
|
---|
207 |
|
---|
208 | rc = usbhid_dev_get_report_descriptor(hid_dev,
|
---|
209 | descriptors, descriptors_size,
|
---|
210 | (uint8_t *)endpoint_mapping[0].interface);
|
---|
211 |
|
---|
212 | free(descriptors);
|
---|
213 |
|
---|
214 | if (rc != EOK) {
|
---|
215 | usb_log_warning("Problem with parsing Report descriptor: %s.\n",
|
---|
216 | str_error(rc));
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | return EOK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /*----------------------------------------------------------------------------*/
|
---|
224 | /* API functions */
|
---|
225 | /*----------------------------------------------------------------------------*/
|
---|
226 |
|
---|
227 | usbhid_dev_t *usbhid_dev_new(void)
|
---|
228 | {
|
---|
229 | usbhid_dev_t *dev =
|
---|
230 | (usbhid_dev_t *)malloc(sizeof(usbhid_dev_t));
|
---|
231 |
|
---|
232 | if (dev == NULL) {
|
---|
233 | usb_log_fatal("No memory!\n");
|
---|
234 | return NULL;
|
---|
235 | }
|
---|
236 |
|
---|
237 | memset(dev, 0, sizeof(usbhid_dev_t));
|
---|
238 |
|
---|
239 | dev->initialized = 0;
|
---|
240 |
|
---|
241 | return dev;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*----------------------------------------------------------------------------*/
|
---|
245 |
|
---|
246 | void usbhid_dev_free(usbhid_dev_t **hid_dev)
|
---|
247 | {
|
---|
248 | if (hid_dev == NULL || *hid_dev == NULL) {
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | // free the report descriptor
|
---|
253 | if ((*hid_dev)->report_desc != NULL) {
|
---|
254 | free((*hid_dev)->report_desc);
|
---|
255 | }
|
---|
256 | // destroy the parser
|
---|
257 | if ((*hid_dev)->parser != NULL) {
|
---|
258 | usb_hid_free_report_parser((*hid_dev)->parser);
|
---|
259 | }
|
---|
260 |
|
---|
261 | // TODO: cleanup pipes
|
---|
262 |
|
---|
263 | free(*hid_dev);
|
---|
264 | *hid_dev = NULL;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*----------------------------------------------------------------------------*/
|
---|
268 |
|
---|
269 | int usbhid_dev_init(usbhid_dev_t *hid_dev, ddf_dev_t *dev,
|
---|
270 | usb_endpoint_description_t *poll_ep_desc)
|
---|
271 | {
|
---|
272 | usb_log_info("Initializing HID device structure.\n");
|
---|
273 |
|
---|
274 | if (hid_dev == NULL) {
|
---|
275 | usb_log_error("Failed to init HID device structure: no "
|
---|
276 | "structure given.\n");
|
---|
277 | return EINVAL;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (dev == NULL) {
|
---|
281 | usb_log_error("Failed to init HID device structure: no device"
|
---|
282 | " given.\n");
|
---|
283 | return EINVAL;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (poll_ep_desc == NULL) {
|
---|
287 | usb_log_error("No poll endpoint description given.\n");
|
---|
288 | return EINVAL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | hid_dev->device = dev;
|
---|
292 |
|
---|
293 | int rc;
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Initialize the backing connection to the host controller.
|
---|
297 | */
|
---|
298 | rc = usb_device_connection_initialize_from_device(&hid_dev->wire, dev);
|
---|
299 | if (rc != EOK) {
|
---|
300 | usb_log_error("Problem initializing connection to device: %s."
|
---|
301 | "\n", str_error(rc));
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Initialize device pipes.
|
---|
307 | */
|
---|
308 | rc = usb_endpoint_pipe_initialize_default_control(&hid_dev->ctrl_pipe,
|
---|
309 | &hid_dev->wire);
|
---|
310 | if (rc != EOK) {
|
---|
311 | usb_log_error("Failed to initialize default control pipe: %s."
|
---|
312 | "\n", str_error(rc));
|
---|
313 | return rc;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Get descriptors, parse descriptors and save endpoints.
|
---|
318 | */
|
---|
319 | usb_endpoint_pipe_start_session(&hid_dev->ctrl_pipe);
|
---|
320 |
|
---|
321 | rc = usbhid_dev_process_descriptors(hid_dev, poll_ep_desc);
|
---|
322 |
|
---|
323 | usb_endpoint_pipe_end_session(&hid_dev->ctrl_pipe);
|
---|
324 | if (rc != EOK) {
|
---|
325 | usb_log_error("Failed to process descriptors: %s.\n",
|
---|
326 | str_error(rc));
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|
330 | hid_dev->initialized = 1;
|
---|
331 | usb_log_info("HID device structure initialized.\n");
|
---|
332 |
|
---|
333 | return EOK;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * @}
|
---|
338 | */
|
---|