source: mainline/uspace/drv/usbhid/hiddev.c@ 27270db

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

Fixed initialization of kbd_dev.

  • Setting console phone to -1.
  • Property mode set to 100644
File size: 8.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 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
56static 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
144static 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 // get the first configuration descriptor
152 usb_standard_configuration_descriptor_t config_desc;
153
154 int rc;
155 rc = usb_request_get_bare_configuration_descriptor(&hid_dev->ctrl_pipe,
156 0, &config_desc);
157
158 if (rc != EOK) {
159 usb_log_error("Failed to get bare config descriptor: %s.\n",
160 str_error(rc));
161 return rc;
162 }
163
164 // prepare space for all underlying descriptors
165 uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
166 if (descriptors == NULL) {
167 usb_log_error("No memory!.\n");
168 return ENOMEM;
169 }
170
171 size_t transferred = 0;
172 // get full configuration descriptor
173 rc = usb_request_get_full_configuration_descriptor(&hid_dev->ctrl_pipe,
174 0, descriptors, config_desc.total_length, &transferred);
175
176 if (rc != EOK) {
177 usb_log_error("Failed to get full config descriptor: %s.\n",
178 str_error(rc));
179 free(descriptors);
180 return rc;
181 }
182
183 if (transferred != config_desc.total_length) {
184 usb_log_error("Configuration descriptor has wrong size (%u, "
185 "expected %u).\n", transferred, config_desc.total_length);
186 free(descriptors);
187 return ELIMIT;
188 }
189
190 /*
191 * Initialize the interrupt in endpoint.
192 */
193 usb_endpoint_mapping_t endpoint_mapping[1] = {
194 {
195 .pipe = &hid_dev->poll_pipe,
196 .description = poll_ep_desc,
197 .interface_no =
198 usb_device_get_assigned_interface(hid_dev->device)
199 }
200 };
201
202 rc = usb_endpoint_pipe_initialize_from_configuration(
203 endpoint_mapping, 1, descriptors, config_desc.total_length,
204 &hid_dev->wire);
205
206 if (rc != EOK) {
207 usb_log_error("Failed to initialize poll pipe: %s.\n",
208 str_error(rc));
209 free(descriptors);
210 return rc;
211 }
212
213 if (!endpoint_mapping[0].present) {
214 usb_log_warning("Not accepting device.\n");
215 free(descriptors);
216 return EREFUSED; // probably not very good return value
217 }
218
219 usb_log_debug("Accepted device. Saving interface, and getting Report"
220 " descriptor.\n");
221
222 /*
223 * Save assigned interface number.
224 */
225 if (endpoint_mapping[0].interface_no < 0) {
226 usb_log_error("Bad interface number.\n");
227 free(descriptors);
228 return EINVAL;
229 }
230
231 hid_dev->iface = endpoint_mapping[0].interface_no;
232
233 assert(endpoint_mapping[0].interface != NULL);
234
235 rc = usbhid_dev_get_report_descriptor(hid_dev, descriptors, transferred,
236 (uint8_t *)endpoint_mapping[0].interface);
237
238 free(descriptors);
239
240 if (rc != EOK) {
241 usb_log_warning("Problem with parsing Report descriptor: %s.\n",
242 str_error(rc));
243 return rc;
244 }
245
246 return EOK;
247}
248
249/*----------------------------------------------------------------------------*/
250/* API functions */
251/*----------------------------------------------------------------------------*/
252
253usbhid_dev_t *usbhid_dev_new(void)
254{
255 usbhid_dev_t *dev =
256 (usbhid_dev_t *)malloc(sizeof(usbhid_dev_t));
257
258 if (dev == NULL) {
259 usb_log_fatal("No memory!\n");
260 return NULL;
261 }
262
263 memset(dev, 0, sizeof(usbhid_dev_t));
264
265 dev->initialized = 0;
266
267 return dev;
268}
269
270/*----------------------------------------------------------------------------*/
271
272int usbhid_dev_init(usbhid_dev_t *hid_dev, ddf_dev_t *dev,
273 usb_endpoint_description_t *poll_ep_desc)
274{
275 usb_log_info("Initializing HID device structure.\n");
276
277 if (hid_dev == NULL) {
278 usb_log_error("Failed to init HID device structure: no "
279 "structure given.\n");
280 return EINVAL;
281 }
282
283 if (dev == NULL) {
284 usb_log_error("Failed to init HID device structure: no device"
285 " given.\n");
286 return EINVAL;
287 }
288
289 if (poll_ep_desc == NULL) {
290 usb_log_error("No poll endpoint description given.\n");
291 return EINVAL;
292 }
293
294 hid_dev->device = dev;
295
296 int rc;
297
298 /*
299 * Initialize the backing connection to the host controller.
300 */
301 rc = usb_device_connection_initialize_from_device(&hid_dev->wire, dev);
302 if (rc != EOK) {
303 usb_log_error("Problem initializing connection to device: %s."
304 "\n", str_error(rc));
305 return rc;
306 }
307
308 /*
309 * Initialize device pipes.
310 */
311 rc = usb_endpoint_pipe_initialize_default_control(&hid_dev->ctrl_pipe,
312 &hid_dev->wire);
313 if (rc != EOK) {
314 usb_log_error("Failed to initialize default control pipe: %s."
315 "\n", str_error(rc));
316 return rc;
317 }
318
319 /*
320 * Get descriptors, parse descriptors and save endpoints.
321 */
322 usb_endpoint_pipe_start_session(&hid_dev->ctrl_pipe);
323
324 rc = usbhid_dev_process_descriptors(hid_dev, poll_ep_desc);
325
326 usb_endpoint_pipe_end_session(&hid_dev->ctrl_pipe);
327 if (rc != EOK) {
328 usb_log_error("Failed to process descriptors: %s.\n",
329 str_error(rc));
330 return rc;
331 }
332
333 hid_dev->initialized = 1;
334 usb_log_info("HID device structure initialized.\n");
335
336 return EOK;
337}
338
339/**
340 * @}
341 */
Note: See TracBrowser for help on using the repository browser.