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