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

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

Added boot protocol static descriptor.

  • The fallback does not work very well, because the usb_kbd_init() function receives the device even if it does not support boot protocol.
  • 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 }
83
84 if (d == NULL) {
85 usb_log_error("The %d. interface descriptor not found!\n",
86 dev->interface_no);
87 return ENOENT;
88 }
89
90 /*
91 * First nested descriptor of the interface descriptor.
92 */
93 uint8_t *iface_desc = d;
94 d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
95
96 /*
97 * Search through siblings until the HID descriptor is found.
98 */
99 while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
100 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
101 iface_desc, d);
102 }
103
104 if (d == NULL) {
105 usb_log_fatal("No HID descriptor found!\n");
106 return ENOENT;
107 }
108
109 if (*d != sizeof(usb_standard_hid_descriptor_t)) {
110 usb_log_error("HID descriptor hass wrong size (%u, expected %u"
111 ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
112 return EINVAL;
113 }
114
115 usb_standard_hid_descriptor_t *hid_desc =
116 (usb_standard_hid_descriptor_t *)d;
117
118 uint16_t length = hid_desc->report_desc_info.length;
119 size_t actual_size = 0;
120
121 /*
122 * Start session for the control transfer.
123 */
124 int sess_rc = usb_pipe_start_session(&dev->ctrl_pipe);
125 if (sess_rc != EOK) {
126 usb_log_warning("Failed to start a session: %s.\n",
127 str_error(sess_rc));
128 return sess_rc;
129 }
130
131 /*
132 * Allocate space for the report descriptor.
133 */
134 *report_desc = (uint8_t *)malloc(length);
135 if (*report_desc == NULL) {
136 usb_log_error("Failed to allocate space for Report descriptor."
137 "\n");
138 return ENOMEM;
139 }
140
141 usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
142
143 /*
144 * Get the descriptor from the device.
145 */
146 int rc = usb_request_get_descriptor(&dev->ctrl_pipe,
147 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
148 USB_DESCTYPE_HID_REPORT, 0, dev->interface_no,
149 *report_desc, length, &actual_size);
150
151 if (rc != EOK) {
152 free(*report_desc);
153 *report_desc = NULL;
154 return rc;
155 }
156
157 if (actual_size != length) {
158 free(*report_desc);
159 *report_desc = NULL;
160 usb_log_error("Report descriptor has wrong size (%u, expected "
161 "%u)\n", actual_size, length);
162 return EINVAL;
163 }
164
165 /*
166 * End session for the control transfer.
167 */
168 sess_rc = usb_pipe_end_session(&dev->ctrl_pipe);
169 if (sess_rc != EOK) {
170 usb_log_warning("Failed to end a session: %s.\n",
171 str_error(sess_rc));
172 free(*report_desc);
173 *report_desc = NULL;
174 return sess_rc;
175 }
176
177 *size = length;
178
179 usb_log_debug("Done.\n");
180
181 return EOK;
182}
183
184/*----------------------------------------------------------------------------*/
185
186int usb_hid_process_report_descriptor(usb_device_t *dev,
187 usb_hid_report_parser_t *parser)
188{
189 if (dev == NULL || parser == NULL) {
190 usb_log_error("Failed to process Report descriptor: wrong "
191 "parameters given.\n");
192 return EINVAL;
193 }
194
195 uint8_t *report_desc = NULL;
196 size_t report_size;
197
198 int rc = usb_hid_get_report_descriptor(dev, &report_desc,
199 &report_size);
200
201 if (rc != EOK) {
202 usb_log_error("Problem with getting Report descriptor: %s.\n",
203 str_error(rc));
204 if (report_desc != NULL) {
205 free(report_desc);
206 }
207 return rc;
208 }
209
210 assert(report_desc != NULL);
211
212 rc = usb_hid_parse_report_descriptor(parser, report_desc, report_size);
213 if (rc != EOK) {
214 usb_log_error("Problem parsing Report descriptor: %s.\n",
215 str_error(rc));
216 free(report_desc);
217 return rc;
218 }
219
220 usb_hid_descriptor_print(parser);
221 free(report_desc);
222
223 return EOK;
224}
225
226/**
227 * @}
228 */
Note: See TracBrowser for help on using the repository browser.