1 | /*
|
---|
2 | * Copyright (c) 2010 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 | /** @addtogroup drvusbhid
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include <errno.h>
|
---|
33 | #include <stdint.h>
|
---|
34 | #include <usb/usb.h>
|
---|
35 | #include <usb/classes/hid.h>
|
---|
36 | #include <usb/descriptor.h>
|
---|
37 | #include "descparser.h"
|
---|
38 | #include "descdump.h"
|
---|
39 |
|
---|
40 | static void usbkbd_config_free(usb_hid_configuration_t *config)
|
---|
41 | {
|
---|
42 | if (config == NULL) {
|
---|
43 | return;
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (config->interfaces == NULL) {
|
---|
47 | return;
|
---|
48 | }
|
---|
49 |
|
---|
50 | int i;
|
---|
51 | for (i = 0; i < config->config_descriptor.interface_count; ++i) {
|
---|
52 |
|
---|
53 | usb_hid_iface_t *iface = &config->interfaces[i];
|
---|
54 |
|
---|
55 | if (iface->endpoints != NULL) {
|
---|
56 | free(config->interfaces[i].endpoints);
|
---|
57 | }
|
---|
58 | /*if (iface->class_desc_info != NULL) {
|
---|
59 | free(iface->class_desc_info);
|
---|
60 | }
|
---|
61 | if (iface->class_descs != NULL) {
|
---|
62 | int j;
|
---|
63 | for (j = 0;
|
---|
64 | j < iface->hid_desc.class_desc_count;
|
---|
65 | ++j) {
|
---|
66 | if (iface->class_descs[j] != NULL) {
|
---|
67 | free(iface->class_descs[j]);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }*/
|
---|
71 | }
|
---|
72 |
|
---|
73 | free(config->interfaces);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*----------------------------------------------------------------------------*/
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | /*----------------------------------------------------------------------------*/
|
---|
81 |
|
---|
82 | int usbkbd_parse_descriptors(const uint8_t *data, size_t size,
|
---|
83 | usb_hid_configuration_t *config)
|
---|
84 | {
|
---|
85 | if (config == NULL) {
|
---|
86 | return EINVAL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | const uint8_t *pos = data;
|
---|
90 |
|
---|
91 | // get the configuration descriptor (should be first) || *config == NULL
|
---|
92 | if (*pos != sizeof(usb_standard_configuration_descriptor_t)
|
---|
93 | || *(pos + 1) != USB_DESCTYPE_CONFIGURATION) {
|
---|
94 | fprintf(stderr, "Wrong format of configuration descriptor.\n");
|
---|
95 | return EINVAL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | memcpy(&config->config_descriptor, pos,
|
---|
99 | sizeof(usb_standard_configuration_descriptor_t));
|
---|
100 | pos += sizeof(usb_standard_configuration_descriptor_t);
|
---|
101 |
|
---|
102 | //printf("Parsed configuration descriptor: \n");
|
---|
103 | //dump_standard_configuration_descriptor(0, &config->config_descriptor);
|
---|
104 |
|
---|
105 | int ret = EOK;
|
---|
106 |
|
---|
107 | // first descriptor after configuration should be interface
|
---|
108 | if (*(pos + 1) != USB_DESCTYPE_INTERFACE) {
|
---|
109 | fprintf(stderr, "Expected interface descriptor, but got %u.\n",
|
---|
110 | *(pos + 1));
|
---|
111 | return EINVAL;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // prepare place for interface descriptors
|
---|
115 | config->interfaces = (usb_hid_iface_t *)calloc(
|
---|
116 | config->config_descriptor.interface_count, sizeof(usb_hid_iface_t));
|
---|
117 |
|
---|
118 | int iface_i = 0;
|
---|
119 | // as long as these are < 0, there is no space allocated for
|
---|
120 | // the respective structures
|
---|
121 | int ep_i = -1;
|
---|
122 | //int hid_i = -1;
|
---|
123 |
|
---|
124 | usb_hid_iface_t *actual_iface = NULL;
|
---|
125 | //usb_standard_endpoint_descriptor_t *actual_ep = NULL;
|
---|
126 |
|
---|
127 | // parse other descriptors
|
---|
128 | while ((size_t)(pos - data) < size) {
|
---|
129 | uint8_t desc_size = *pos;
|
---|
130 | uint8_t desc_type = *(pos + 1); // 2nd byte is descriptor size
|
---|
131 |
|
---|
132 | switch (desc_type) {
|
---|
133 | case USB_DESCTYPE_INTERFACE:
|
---|
134 | if (desc_size !=
|
---|
135 | sizeof(usb_standard_interface_descriptor_t)) {
|
---|
136 | ret = EINVAL;
|
---|
137 | goto end;
|
---|
138 | }
|
---|
139 |
|
---|
140 | actual_iface = &config->interfaces[iface_i++];
|
---|
141 |
|
---|
142 | memcpy(&actual_iface->iface_desc, pos, desc_size);
|
---|
143 | pos += desc_size;
|
---|
144 |
|
---|
145 | //printf("Parsed interface descriptor: \n");
|
---|
146 | //dump_standard_interface_descriptor(&actual_iface->iface_desc);
|
---|
147 |
|
---|
148 | // allocate space for endpoint descriptors
|
---|
149 | uint8_t eps = actual_iface->iface_desc.endpoint_count;
|
---|
150 | actual_iface->endpoints =
|
---|
151 | (usb_standard_endpoint_descriptor_t *)malloc(
|
---|
152 | eps * sizeof(usb_standard_endpoint_descriptor_t));
|
---|
153 | if (actual_iface->endpoints == NULL) {
|
---|
154 | ret = ENOMEM;
|
---|
155 | goto end;
|
---|
156 | }
|
---|
157 | ep_i = 0;
|
---|
158 |
|
---|
159 | //printf("Remaining size: %d\n", size - (size_t)(pos - data));
|
---|
160 |
|
---|
161 | break;
|
---|
162 | case USB_DESCTYPE_ENDPOINT:
|
---|
163 | if (desc_size !=
|
---|
164 | sizeof(usb_standard_endpoint_descriptor_t)) {
|
---|
165 | ret = EINVAL;
|
---|
166 | goto end;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (ep_i < 0) {
|
---|
170 | fprintf(stderr, "Missing interface descriptor "
|
---|
171 | "before endpoint descriptor.\n");
|
---|
172 | ret = EINVAL;
|
---|
173 | goto end;
|
---|
174 | }
|
---|
175 | if (ep_i > actual_iface->iface_desc.endpoint_count) {
|
---|
176 | fprintf(stderr, "More endpoint descriptors than"
|
---|
177 | " expected.\n");
|
---|
178 | ret = EINVAL;
|
---|
179 | goto end;
|
---|
180 | }
|
---|
181 |
|
---|
182 | // save the endpoint descriptor
|
---|
183 | memcpy(&actual_iface->endpoints[ep_i], pos, desc_size);
|
---|
184 | pos += desc_size;
|
---|
185 |
|
---|
186 | //printf("Parsed endpoint descriptor: \n");
|
---|
187 | //dump_standard_endpoint_descriptor(&actual_iface->endpoints[ep_i]);
|
---|
188 | ++ep_i;
|
---|
189 |
|
---|
190 | break;
|
---|
191 | case USB_DESCTYPE_HID:
|
---|
192 | if (desc_size < sizeof(usb_standard_hid_descriptor_t)) {
|
---|
193 | printf("Wrong size of descriptor: %d (should be %zu)\n",
|
---|
194 | desc_size, sizeof(usb_standard_hid_descriptor_t));
|
---|
195 | ret = EINVAL;
|
---|
196 | goto end;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // copy the header of the hid descriptor
|
---|
200 | memcpy(&actual_iface->hid_desc, pos,
|
---|
201 | sizeof(usb_standard_hid_descriptor_t));
|
---|
202 | pos += sizeof(usb_standard_hid_descriptor_t);
|
---|
203 |
|
---|
204 | /*if (actual_iface->hid_desc.class_desc_count
|
---|
205 | * sizeof(usb_standard_hid_class_descriptor_info_t)
|
---|
206 | != desc_size
|
---|
207 | - sizeof(usb_standard_hid_descriptor_t)) {
|
---|
208 | fprintf(stderr, "Wrong size of HID descriptor."
|
---|
209 | "\n");
|
---|
210 | ret = EINVAL;
|
---|
211 | goto end;
|
---|
212 | }*/
|
---|
213 |
|
---|
214 | //printf("Parsed HID descriptor header: \n");
|
---|
215 | //dump_standard_hid_descriptor_header(&actual_iface->hid_desc);
|
---|
216 |
|
---|
217 | // allocate space for all class-specific descriptor info
|
---|
218 | /*actual_iface->class_desc_info =
|
---|
219 | (usb_standard_hid_class_descriptor_info_t *)malloc(
|
---|
220 | actual_iface->hid_desc.class_desc_count
|
---|
221 | * sizeof(usb_standard_hid_class_descriptor_info_t));
|
---|
222 | if (actual_iface->class_desc_info == NULL) {
|
---|
223 | ret = ENOMEM;
|
---|
224 | goto end;
|
---|
225 | }*/
|
---|
226 |
|
---|
227 | // allocate space for all class-specific descriptors
|
---|
228 | /*actual_iface->class_descs = (uint8_t **)calloc(
|
---|
229 | actual_iface->hid_desc.class_desc_count,
|
---|
230 | sizeof(uint8_t *));
|
---|
231 | if (actual_iface->class_descs == NULL) {
|
---|
232 | ret = ENOMEM;
|
---|
233 | goto end;
|
---|
234 | }*/
|
---|
235 |
|
---|
236 | // copy all class-specific descriptor info
|
---|
237 | // TODO: endianness
|
---|
238 | /*
|
---|
239 | memcpy(actual_iface->class_desc_info, pos,
|
---|
240 | actual_iface->hid_desc.class_desc_count
|
---|
241 | * sizeof(usb_standard_hid_class_descriptor_info_t));
|
---|
242 | pos += actual_iface->hid_desc.class_desc_count
|
---|
243 | * sizeof(usb_standard_hid_class_descriptor_info_t);
|
---|
244 |
|
---|
245 | printf("Parsed HID descriptor info:\n");
|
---|
246 | dump_standard_hid_class_descriptor_info(
|
---|
247 | actual_iface->class_desc_info);
|
---|
248 | */
|
---|
249 |
|
---|
250 | /*size_t tmp = (size_t)(pos - data);
|
---|
251 | printf("Parser position: %d, remaining: %d\n",
|
---|
252 | pos - data, size - tmp);*/
|
---|
253 |
|
---|
254 | //hid_i = 0;
|
---|
255 |
|
---|
256 | break;
|
---|
257 | /* case USB_DESCTYPE_HID_REPORT:
|
---|
258 | case USB_DESCTYPE_HID_PHYSICAL: {
|
---|
259 | // check if the type matches
|
---|
260 | uint8_t exp_type =
|
---|
261 | actual_iface->class_desc_info[hid_i].type;
|
---|
262 | if (exp_type != desc_type) {
|
---|
263 | fprintf(stderr, "Expected descriptor type %u, "
|
---|
264 | "but got %u.\n", exp_type, desc_type);
|
---|
265 | ret = EINVAL;
|
---|
266 | goto end;
|
---|
267 | }
|
---|
268 |
|
---|
269 | // the size of this descriptor is stored in the
|
---|
270 | // class-specific descriptor info
|
---|
271 | uint16_t length =
|
---|
272 | actual_iface->class_desc_info[hid_i].length;
|
---|
273 |
|
---|
274 | printf("Saving class-specific descriptor #%d\n", hid_i);
|
---|
275 |
|
---|
276 | actual_iface->class_descs[hid_i] =
|
---|
277 | (uint8_t *)malloc(length);
|
---|
278 | if (actual_iface->class_descs[hid_i] == NULL) {
|
---|
279 | ret = ENOMEM;
|
---|
280 | goto end;
|
---|
281 | }
|
---|
282 |
|
---|
283 | memcpy(actual_iface->class_descs[hid_i], pos, length);
|
---|
284 | pos += length;
|
---|
285 |
|
---|
286 | printf("Parsed class-specific descriptor:\n");
|
---|
287 | dump_hid_class_descriptor(hid_i, desc_type,
|
---|
288 | actual_iface->class_descs[hid_i], length);
|
---|
289 |
|
---|
290 | ++hid_i;
|
---|
291 |
|
---|
292 | break; }*/
|
---|
293 | default:
|
---|
294 | fprintf(stderr, "Got descriptor of unknown type: %u.\n",
|
---|
295 | desc_type);
|
---|
296 | ret = EINVAL;
|
---|
297 | goto end;
|
---|
298 | break;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | end:
|
---|
303 | if (ret != EOK) {
|
---|
304 | usbkbd_config_free(config);
|
---|
305 | }
|
---|
306 |
|
---|
307 | return ret;
|
---|
308 | }
|
---|
309 |
|
---|
310 | void usbkbd_print_config(const usb_hid_configuration_t *config)
|
---|
311 | {
|
---|
312 | dump_standard_configuration_descriptor(0, &config->config_descriptor);
|
---|
313 | int i = 0;
|
---|
314 | for (; i < config->config_descriptor.interface_count; ++i) {
|
---|
315 | usb_hid_iface_t *iface_d = &config->interfaces[i];
|
---|
316 | dump_standard_interface_descriptor(&iface_d->iface_desc);
|
---|
317 | printf("\n");
|
---|
318 | int j = 0;
|
---|
319 | for (; j < iface_d->iface_desc.endpoint_count; ++j) {
|
---|
320 | dump_standard_endpoint_descriptor(
|
---|
321 | &iface_d->endpoints[j]);
|
---|
322 | printf("\n");
|
---|
323 | }
|
---|
324 | dump_standard_hid_descriptor_header(&iface_d->hid_desc);
|
---|
325 | printf("\n");
|
---|
326 | dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
|
---|
327 | iface_d->report_desc, iface_d->hid_desc.report_desc_info.length);
|
---|
328 | printf("\n");
|
---|
329 | // printf("%d class-specific descriptors\n",
|
---|
330 | // iface_d->hid_desc.class_desc_count);
|
---|
331 | /*for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
|
---|
332 | dump_standard_hid_class_descriptor_info(
|
---|
333 | &iface_d->class_desc_info[j]);
|
---|
334 | }
|
---|
335 |
|
---|
336 | for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
|
---|
337 | dump_hid_class_descriptor(j,
|
---|
338 | iface_d->class_desc_info[j].type,
|
---|
339 | iface_d->class_descs[j],
|
---|
340 | iface_d->class_desc_info[j].length);
|
---|
341 | }*/
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * @}
|
---|
347 | */
|
---|