source: mainline/uspace/drv/usbkbd/descparser.c@ 1f383dde

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

Debugging

  • disabled some output from other parts (virtual hcd)
  • added some debug output to descparser
  • Property mode set to 100644
File size: 12.6 KB
Line 
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#include <errno.h>
29#include <stdint.h>
30#include <usb/usb.h>
31#include <usb/classes/hid.h>
32#include <usb/descriptor.h>
33#include "descparser.h"
34
35static void usbkbd_config_free(usb_hid_configuration_t *config)
36{
37 if (config == NULL) {
38 return;
39 }
40
41 if (config->interfaces == NULL) {
42 return;
43 }
44
45 int i;
46 for (i = 0; i < config->config_descriptor.interface_count; ++i) {
47
48 usb_hid_iface_t *iface = &config->interfaces[i];
49
50 if (iface->endpoints != NULL) {
51 free(config->interfaces[i].endpoints);
52 }
53 if (iface->class_desc_info != NULL) {
54 free(iface->class_desc_info);
55 }
56 if (iface->class_descs != NULL) {
57 int j;
58 for (j = 0;
59 j < iface->hid_desc.class_desc_count;
60 ++j) {
61 if (iface->class_descs[j] != NULL) {
62 free(iface->class_descs[j]);
63 }
64 }
65 }
66 }
67
68 free(config->interfaces);
69}
70
71/*----------------------------------------------------------------------------*/
72
73
74
75/*----------------------------------------------------------------------------*/
76
77int usbkbd_parse_descriptors(const uint8_t *data, size_t size,
78 usb_hid_configuration_t *config)
79{
80 if (config == NULL) {
81 return EINVAL;
82 }
83
84 const uint8_t *pos = data;
85
86 // get the configuration descriptor (should be first) || *config == NULL
87 if (*pos != sizeof(usb_standard_configuration_descriptor_t)
88 || *(pos + 1) != USB_DESCTYPE_CONFIGURATION) {
89 fprintf(stderr, "Wrong format of configuration descriptor.\n");
90 return EINVAL;
91 }
92
93 memcpy(&config->config_descriptor, pos,
94 sizeof(usb_standard_configuration_descriptor_t));
95 pos += sizeof(usb_standard_configuration_descriptor_t);
96
97 int ret = EOK;
98
99 // first descriptor after configuration should be interface
100 if (*(pos + 1) != USB_DESCTYPE_INTERFACE) {
101 fprintf(stderr, "Expected interface descriptor, but got %u.\n",
102 *(pos + 1));
103 return EINVAL;
104 }
105
106 // prepare place for interface descriptors
107 config->interfaces = (usb_hid_iface_t *)calloc(
108 config->config_descriptor.interface_count, sizeof(usb_hid_iface_t));
109
110 int iface_i = 0;
111 // as long as these are < 0, there is no space allocated for
112 // the respective structures
113 int ep_i = -1;
114 int hid_i = -1;
115
116 usb_hid_iface_t *actual_iface = NULL;
117 //usb_standard_endpoint_descriptor_t *actual_ep = NULL;
118
119 // parse other descriptors
120 while ((size_t)(pos - data) < size) {
121 uint8_t desc_size = *pos;
122 uint8_t desc_type = *(pos + 1); // 2nd byte is descriptor size
123
124 switch (desc_type) {
125 case USB_DESCTYPE_INTERFACE:
126 if (desc_size !=
127 sizeof(usb_standard_interface_descriptor_t)) {
128 ret = EINVAL;
129 goto end;
130 }
131
132 actual_iface = &config->interfaces[iface_i++];
133
134 memcpy(&actual_iface->iface_desc, pos, desc_size);
135 pos += desc_size;
136
137 // allocate space for endpoint descriptors
138 uint8_t eps = actual_iface->iface_desc.endpoint_count;
139 actual_iface->endpoints =
140 (usb_standard_endpoint_descriptor_t *)malloc(
141 eps * sizeof(usb_standard_endpoint_descriptor_t));
142 if (actual_iface->endpoints == NULL) {
143 ret = ENOMEM;
144 goto end;
145 }
146 ep_i = 0;
147
148 break;
149 case USB_DESCTYPE_ENDPOINT:
150 if (desc_size !=
151 sizeof(usb_standard_endpoint_descriptor_t)) {
152 ret = EINVAL;
153 goto end;
154 }
155
156 if (ep_i < 0) {
157 fprintf(stderr, "Missing interface descriptor "
158 "before endpoint descriptor.\n");
159 ret = EINVAL;
160 goto end;
161 }
162 if (ep_i > actual_iface->iface_desc.endpoint_count) {
163 fprintf(stderr, "More endpoint descriptors than"
164 " expected.\n");
165 ret = EINVAL;
166 goto end;
167 }
168
169 // save the endpoint descriptor
170 memcpy(&actual_iface->endpoints[ep_i], pos, desc_size);
171 pos += desc_size;
172 ++ep_i;
173
174 break;
175 case USB_DESCTYPE_HID:
176 if (desc_size < sizeof(usb_standard_hid_descriptor_t)
177 + sizeof(usb_standard_hid_class_descriptor_info_t)) {
178 ret = EINVAL;
179 goto end;
180 }
181
182 // copy the header of the hid descriptor
183 memcpy(&actual_iface->hid_desc, pos,
184 sizeof(usb_standard_hid_descriptor_t));
185 pos += sizeof(usb_standard_hid_descriptor_t);
186
187 if (actual_iface->hid_desc.class_desc_count
188 * sizeof(usb_standard_hid_class_descriptor_info_t)
189 != desc_size
190 - sizeof(usb_standard_hid_descriptor_t)) {
191 fprintf(stderr, "Wrong size of HID descriptor."
192 "\n");
193 ret = EINVAL;
194 goto end;
195 }
196
197 // allocate space for all class-specific descriptor info
198 actual_iface->class_desc_info =
199 (usb_standard_hid_class_descriptor_info_t *)malloc(
200 actual_iface->hid_desc.class_desc_count
201 * sizeof(usb_standard_hid_class_descriptor_info_t));
202 if (actual_iface->class_desc_info == NULL) {
203 ret = ENOMEM;
204 goto end;
205 }
206
207 // allocate space for all class-specific descriptors
208 actual_iface->class_descs = (uint8_t **)calloc(
209 actual_iface->hid_desc.class_desc_count,
210 sizeof(uint8_t *));
211 if (actual_iface->class_descs == NULL) {
212 ret = ENOMEM;
213 goto end;
214 }
215
216 // copy all class-specific descriptor info
217 // TODO: endianness
218 memcpy(actual_iface->class_desc_info, pos,
219 actual_iface->hid_desc.class_desc_count
220 * sizeof(usb_standard_hid_class_descriptor_info_t));
221 pos += actual_iface->hid_desc.class_desc_count
222 * sizeof(usb_standard_hid_class_descriptor_info_t);
223
224 size_t tmp = (size_t)(pos - data);
225 printf("Parser position: %d, remaining: %d\n",
226 pos - data, size - tmp);
227
228 /*
229 * TODO: this is not good, only 7 bytes remaining,
230 * something is wrong!
231 */
232
233 hid_i = 0;
234
235 break;
236 case USB_DESCTYPE_HID_REPORT:
237 case USB_DESCTYPE_HID_PHYSICAL: {
238 // check if the type matches
239 uint8_t exp_type =
240 actual_iface->class_desc_info[hid_i].type;
241 if (exp_type != desc_type) {
242 fprintf(stderr, "Expected descriptor type %u, "
243 "but got %u.\n", exp_type, desc_type);
244 ret = EINVAL;
245 goto end;
246 }
247
248 // the size of this descriptor is stored in the
249 // class-specific descriptor info
250 uint16_t length =
251 actual_iface->class_desc_info[hid_i].length;
252
253 printf("Saving class-specific descriptor #%d\n", hid_i);
254
255 actual_iface->class_descs[hid_i] =
256 (uint8_t *)malloc(length);
257 if (actual_iface->class_descs[hid_i] == NULL) {
258 ret = ENOMEM;
259 goto end;
260 }
261
262 memcpy(actual_iface->class_descs[hid_i], pos, length);
263 pos += length;
264 ++hid_i;
265
266 break; }
267 default:
268 fprintf(stderr, "Got descriptor of unknown type: %u.\n",
269 desc_type);
270 ret = EINVAL;
271 goto end;
272 break;
273 }
274 }
275
276end:
277 if (ret != EOK) {
278 usbkbd_config_free(config);
279 }
280
281 return ret;
282}
283
284/*----------------------------------------------------------------------------*/
285
286#define BYTES_PER_LINE 12
287
288static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
289{
290 printf("%s\n", msg);
291
292 size_t i;
293 for (i = 0; i < length; i++) {
294 printf(" 0x%02X", buffer[i]);
295 if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
296 || (i + 1 == length)) {
297 printf("\n");
298 }
299 }
300}
301
302/*----------------------------------------------------------------------------*/
303
304#define INDENT " "
305
306static void dump_standard_configuration_descriptor(
307 int index, const usb_standard_configuration_descriptor_t *d)
308{
309 bool self_powered = d->attributes & 64;
310 bool remote_wakeup = d->attributes & 32;
311
312 printf("Standard configuration descriptor #%d\n", index);
313 printf(INDENT "bLength = %d\n", d->length);
314 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
315 printf(INDENT "wTotalLength = %d\n", d->total_length);
316 printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
317 printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
318 printf(INDENT "iConfiguration = %d\n", d->str_configuration);
319 printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
320 self_powered ? "self-powered" : "",
321 (self_powered & remote_wakeup) ? ", " : "",
322 remote_wakeup ? "remote-wakeup" : "");
323 printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
324 2 * d->max_power);
325 // printf(INDENT " = %d\n", d->);
326}
327
328static void dump_standard_interface_descriptor(
329 const usb_standard_interface_descriptor_t *d)
330{
331 printf("Standard interface descriptor\n");
332 printf(INDENT "bLength = %d\n", d->length);
333 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
334 printf(INDENT "bInterfaceNumber = %d\n", d->interface_number);
335 printf(INDENT "bAlternateSetting = %d\n", d->alternate_setting);
336 printf(INDENT "bNumEndpoints = %d\n", d->endpoint_count);
337 printf(INDENT "bInterfaceClass = %d\n", d->interface_class);
338 printf(INDENT "bInterfaceSubClass = %d\n", d->interface_subclass);
339 printf(INDENT "bInterfaceProtocol = %d\n", d->interface_protocol);
340 printf(INDENT "iInterface = %d", d->str_interface);
341}
342
343static void dump_standard_endpoint_descriptor(
344 const usb_standard_endpoint_descriptor_t *d)
345{
346 const char *transfer_type;
347 switch (d->attributes & 3) {
348 case USB_TRANSFER_CONTROL:
349 transfer_type = "control";
350 break;
351 case USB_TRANSFER_ISOCHRONOUS:
352 transfer_type = "isochronous";
353 break;
354 case USB_TRANSFER_BULK:
355 transfer_type = "bulk";
356 break;
357 case USB_TRANSFER_INTERRUPT:
358 transfer_type = "interrupt";
359 break;
360 }
361
362 printf("Standard endpoint descriptor\n");
363 printf(INDENT "bLength = %d\n", d->length);
364 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
365 printf(INDENT "bmAttributes = %d [%s]\n", d->attributes, transfer_type);
366 printf(INDENT "wMaxPacketSize = %d\n", d->max_packet_size);
367 printf(INDENT "bInterval = %d\n", d->poll_interval);
368}
369
370static void dump_standard_hid_descriptor_header(
371 const usb_standard_hid_descriptor_t *d)
372{
373 printf("Standard HID descriptor\n");
374 printf(INDENT "bLength = %d\n", d->length);
375 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
376 printf(INDENT "bcdHID = %d\n", d->spec_release);
377 printf(INDENT "bCountryCode = %d\n", d->country_code);
378 printf(INDENT "bNumDescriptors = %d\n", d->class_desc_count);
379}
380
381static void dump_standard_hid_class_descriptor_info(
382 const usb_standard_hid_class_descriptor_info_t *d)
383{
384 printf(INDENT "bDescriptorType = %d\n", d->type);
385 printf(INDENT "wDescriptorLength = %d\n", d->length);
386}
387
388static void dump_hid_class_descriptor(int index, uint8_t type,
389 const uint8_t *d, size_t size )
390{
391 printf("Class-specific descriptor #%d (type: %u)\n", index, type);
392 assert(d != NULL);
393 dump_buffer("", d, size);
394}
395
396void usbkbd_print_config(const usb_hid_configuration_t *config)
397{
398 dump_standard_configuration_descriptor(0, &config->config_descriptor);
399 int i = 0;
400 for (; i < config->config_descriptor.interface_count; ++i) {
401 usb_hid_iface_t *iface_d = &config->interfaces[i];
402 dump_standard_interface_descriptor(&iface_d->iface_desc);
403 printf("\n");
404 int j = 0;
405 for (; j < iface_d->iface_desc.endpoint_count; ++j) {
406 dump_standard_endpoint_descriptor(
407 &iface_d->endpoints[j]);
408 printf("\n");
409 }
410 dump_standard_hid_descriptor_header(&iface_d->hid_desc);
411 printf("\n");
412// printf("%d class-specific descriptors\n",
413// iface_d->hid_desc.class_desc_count);
414 for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
415 dump_standard_hid_class_descriptor_info(
416 &iface_d->class_desc_info[j]);
417 }
418
419 for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
420 dump_hid_class_descriptor(j,
421 iface_d->class_desc_info[j].type,
422 iface_d->class_descs[j],
423 iface_d->class_desc_info[j].length);
424 }
425 }
426}
Note: See TracBrowser for help on using the repository browser.