[ca038b4] | 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 | */
|
---|
[ba54451] | 28 | /** @addtogroup drvusbhid
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 |
|
---|
[ca038b4] | 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"
|
---|
[6986418] | 38 | #include "descdump.h"
|
---|
[ca038b4] | 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 | }
|
---|
[45019865] | 58 | /*if (iface->class_desc_info != NULL) {
|
---|
[ca038b4] | 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 | }
|
---|
[45019865] | 70 | }*/
|
---|
[ca038b4] | 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);
|
---|
[6986418] | 101 |
|
---|
[76cb03c] | 102 | //printf("Parsed configuration descriptor: \n");
|
---|
| 103 | //dump_standard_configuration_descriptor(0, &config->config_descriptor);
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 112 | }
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 122 | //int hid_i = -1;
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 144 |
|
---|
[76cb03c] | 145 | //printf("Parsed interface descriptor: \n");
|
---|
| 146 | //dump_standard_interface_descriptor(&actual_iface->iface_desc);
|
---|
[ca038b4] | 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;
|
---|
[45019865] | 158 |
|
---|
[76cb03c] | 159 | //printf("Remaining size: %d\n", size - (size_t)(pos - data));
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 185 |
|
---|
[76cb03c] | 186 | //printf("Parsed endpoint descriptor: \n");
|
---|
| 187 | //dump_standard_endpoint_descriptor(&actual_iface->endpoints[ep_i]);
|
---|
[ca038b4] | 188 | ++ep_i;
|
---|
| 189 |
|
---|
| 190 | break;
|
---|
| 191 | case USB_DESCTYPE_HID:
|
---|
[45019865] | 192 | if (desc_size < sizeof(usb_standard_hid_descriptor_t)) {
|
---|
[df175fa] | 193 | printf("Wrong size of descriptor: %d (should be %zu)\n",
|
---|
[45019865] | 194 | desc_size, sizeof(usb_standard_hid_descriptor_t));
|
---|
[ca038b4] | 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 |
|
---|
[45019865] | 204 | /*if (actual_iface->hid_desc.class_desc_count
|
---|
[ca038b4] | 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;
|
---|
[45019865] | 212 | }*/
|
---|
[6986418] | 213 |
|
---|
[76cb03c] | 214 | //printf("Parsed HID descriptor header: \n");
|
---|
| 215 | //dump_standard_hid_descriptor_header(&actual_iface->hid_desc);
|
---|
[ca038b4] | 216 |
|
---|
| 217 | // allocate space for all class-specific descriptor info
|
---|
[45019865] | 218 | /*actual_iface->class_desc_info =
|
---|
[ca038b4] | 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;
|
---|
[45019865] | 225 | }*/
|
---|
[ca038b4] | 226 |
|
---|
| 227 | // allocate space for all class-specific descriptors
|
---|
[6986418] | 228 | /*actual_iface->class_descs = (uint8_t **)calloc(
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 234 | }*/
|
---|
[ca038b4] | 235 |
|
---|
| 236 | // copy all class-specific descriptor info
|
---|
| 237 | // TODO: endianness
|
---|
[6986418] | 238 | /*
|
---|
[ca038b4] | 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);
|
---|
[6986418] | 244 |
|
---|
| 245 | printf("Parsed HID descriptor info:\n");
|
---|
| 246 | dump_standard_hid_class_descriptor_info(
|
---|
| 247 | actual_iface->class_desc_info);
|
---|
| 248 | */
|
---|
[ca038b4] | 249 |
|
---|
[6986418] | 250 | /*size_t tmp = (size_t)(pos - data);
|
---|
[1f383dde] | 251 | printf("Parser position: %d, remaining: %d\n",
|
---|
[6986418] | 252 | pos - data, size - tmp);*/
|
---|
| 253 |
|
---|
| 254 | //hid_i = 0;
|
---|
[ca038b4] | 255 |
|
---|
| 256 | break;
|
---|
[6986418] | 257 | /* case USB_DESCTYPE_HID_REPORT:
|
---|
[ca038b4] | 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 |
|
---|
[1f383dde] | 274 | printf("Saving class-specific descriptor #%d\n", hid_i);
|
---|
| 275 |
|
---|
[ca038b4] | 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;
|
---|
[6986418] | 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 |
|
---|
[ca038b4] | 290 | ++hid_i;
|
---|
| 291 |
|
---|
[6986418] | 292 | break; }*/
|
---|
[ca038b4] | 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");
|
---|
[45019865] | 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");
|
---|
[1f383dde] | 329 | // printf("%d class-specific descriptors\n",
|
---|
| 330 | // iface_d->hid_desc.class_desc_count);
|
---|
[45019865] | 331 | /*for (j = 0; j < iface_d->hid_desc.class_desc_count; ++j) {
|
---|
[ca038b4] | 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);
|
---|
[45019865] | 341 | }*/
|
---|
[ca038b4] | 342 | }
|
---|
| 343 | }
|
---|
[ba54451] | 344 |
|
---|
| 345 | /**
|
---|
| 346 | * @}
|
---|
| 347 | */
|
---|