source: mainline/uspace/drv/usbkbd/descparser.c@ 692f13e4

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

Configuration parsing

Added functions for parsing configuration descriptor and all underlying

descriptors (interface, endpoint, HID, class-specific).

Kbd driver uses new function usbkbd_parse_descriptors() to get the

configuration. As for now, only one configuration is considered.

  • Property mode set to 100644
File size: 12.2 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 hid_i = 0;
225
226 break;
227 case USB_DESCTYPE_HID_REPORT:
228 case USB_DESCTYPE_HID_PHYSICAL: {
229 // check if the type matches
230 uint8_t exp_type =
231 actual_iface->class_desc_info[hid_i].type;
232 if (exp_type != desc_type) {
233 fprintf(stderr, "Expected descriptor type %u, "
234 "but got %u.\n", exp_type, desc_type);
235 ret = EINVAL;
236 goto end;
237 }
238
239 // the size of this descriptor is stored in the
240 // class-specific descriptor info
241 uint16_t length =
242 actual_iface->class_desc_info[hid_i].length;
243
244 actual_iface->class_descs[hid_i] =
245 (uint8_t *)malloc(length);
246 if (actual_iface->class_descs[hid_i] == NULL) {
247 ret = ENOMEM;
248 goto end;
249 }
250
251 memcpy(actual_iface->class_descs[hid_i], pos, length);
252 pos += length;
253 ++hid_i;
254
255 break; }
256 default:
257 fprintf(stderr, "Got descriptor of unknown type: %u.\n",
258 desc_type);
259 ret = EINVAL;
260 goto end;
261 break;
262 }
263 }
264
265end:
266 if (ret != EOK) {
267 usbkbd_config_free(config);
268 }
269
270 return ret;
271}
272
273/*----------------------------------------------------------------------------*/
274
275#define BYTES_PER_LINE 12
276
277static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
278{
279 printf("%s\n", msg);
280
281 size_t i;
282 for (i = 0; i < length; i++) {
283 printf(" 0x%02X", buffer[i]);
284 if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
285 || (i + 1 == length)) {
286 printf("\n");
287 }
288 }
289}
290
291/*----------------------------------------------------------------------------*/
292
293#define INDENT " "
294
295static void dump_standard_configuration_descriptor(
296 int index, const usb_standard_configuration_descriptor_t *d)
297{
298 bool self_powered = d->attributes & 64;
299 bool remote_wakeup = d->attributes & 32;
300
301 printf("Standard configuration descriptor #%d\n", index);
302 printf(INDENT "bLength = %d\n", d->length);
303 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
304 printf(INDENT "wTotalLength = %d\n", d->total_length);
305 printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
306 printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
307 printf(INDENT "iConfiguration = %d\n", d->str_configuration);
308 printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
309 self_powered ? "self-powered" : "",
310 (self_powered & remote_wakeup) ? ", " : "",
311 remote_wakeup ? "remote-wakeup" : "");
312 printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
313 2 * d->max_power);
314 // printf(INDENT " = %d\n", d->);
315}
316
317static void dump_standard_interface_descriptor(
318 const usb_standard_interface_descriptor_t *d)
319{
320 printf("Standard interface descriptor\n");
321 printf(INDENT "bLength = %d\n", d->length);
322 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
323 printf(INDENT "bInterfaceNumber = %d\n", d->interface_number);
324 printf(INDENT "bAlternateSetting = %d\n", d->alternate_setting);
325 printf(INDENT "bNumEndpoints = %d\n", d->endpoint_count);
326 printf(INDENT "bInterfaceClass = %d\n", d->interface_class);
327 printf(INDENT "bInterfaceSubClass = %d\n", d->interface_subclass);
328 printf(INDENT "bInterfaceProtocol = %d\n", d->interface_protocol);
329 printf(INDENT "iInterface = %d", d->str_interface);
330}
331
332static void dump_standard_endpoint_descriptor(
333 const usb_standard_endpoint_descriptor_t *d)
334{
335 const char *transfer_type;
336 switch (d->attributes & 3) {
337 case USB_TRANSFER_CONTROL:
338 transfer_type = "control";
339 break;
340 case USB_TRANSFER_ISOCHRONOUS:
341 transfer_type = "isochronous";
342 break;
343 case USB_TRANSFER_BULK:
344 transfer_type = "bulk";
345 break;
346 case USB_TRANSFER_INTERRUPT:
347 transfer_type = "interrupt";
348 break;
349 }
350
351 printf("Standard endpoint descriptor\n");
352 printf(INDENT "bLength = %d\n", d->length);
353 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
354 printf(INDENT "bmAttributes = %d [%s]\n", d->attributes, transfer_type);
355 printf(INDENT "wMaxPacketSize = %d\n", d->max_packet_size);
356 printf(INDENT "bInterval = %d\n", d->poll_interval);
357}
358
359static void dump_standard_hid_descriptor_header(
360 const usb_standard_hid_descriptor_t *d)
361{
362 printf("Standard HID descriptor\n");
363 printf(INDENT "bLength = %d\n", d->length);
364 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
365 printf(INDENT "bcdHID = %d\n", d->spec_release);
366 printf(INDENT "bCountryCode = %d\n", d->country_code);
367 printf(INDENT "bNumDescriptors = %d\n", d->class_desc_count);
368}
369
370static void dump_standard_hid_class_descriptor_info(
371 const usb_standard_hid_class_descriptor_info_t *d)
372{
373 printf(INDENT "bDescriptorType = %d\n", d->type);
374 printf(INDENT "wDescriptorLength = %d\n", d->length);
375}
376
377static void dump_hid_class_descriptor(int index, uint8_t type,
378 const uint8_t *d, size_t size )
379{
380 printf("Class-specific descriptor #%d (type: %u)\n", index, type);
381 dump_buffer("", d, size);
382}
383
384void usbkbd_print_config(const usb_hid_configuration_t *config)
385{
386 dump_standard_configuration_descriptor(0, &config->config_descriptor);
387 int i = 0;
388 for (; i < config->config_descriptor.interface_count; ++i) {
389 usb_hid_iface_t *iface_d = &config->interfaces[i];
390 dump_standard_interface_descriptor(&iface_d->iface_desc);
391 printf("\n");
392 int j = 0;
393 for (; j < iface_d->iface_desc.endpoint_count; ++j) {
394 dump_standard_endpoint_descriptor(
395 &iface_d->endpoints[j]);
396 printf("\n");
397 }
398 dump_standard_hid_descriptor_header(&iface_d->hid_desc);
399 printf("\n");
400 for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
401 dump_standard_hid_class_descriptor_info(
402 &iface_d->class_desc_info[j]);
403 }
404
405 for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
406 dump_hid_class_descriptor(j,
407 iface_d->class_desc_info[j].type,
408 iface_d->class_descs[j],
409 iface_d->class_desc_info[j].length);
410 }
411 }
412}
Note: See TracBrowser for help on using the repository browser.