[9d05599] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Matej Klonfar
|
---|
| 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 |
|
---|
| 29 | /** @addtogroup libusb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * HID report descriptor and report data parser implementation.
|
---|
| 34 | */
|
---|
| 35 | #include <usb/classes/hidparser.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <malloc.h>
|
---|
| 39 | #include <mem.h>
|
---|
| 40 | #include <usb/debug.h>
|
---|
| 41 | #include <assert.h>
|
---|
| 42 |
|
---|
| 43 | /** The new report item flag. Used to determine when the item is completly
|
---|
| 44 | * configured and should be added to the report structure
|
---|
| 45 | */
|
---|
| 46 | #define USB_HID_NEW_REPORT_ITEM 1
|
---|
| 47 |
|
---|
| 48 | /** No special action after the report descriptor tag is processed should be
|
---|
| 49 | * done
|
---|
| 50 | */
|
---|
| 51 | #define USB_HID_NO_ACTION 2
|
---|
| 52 |
|
---|
| 53 | #define USB_HID_RESET_OFFSET 3
|
---|
| 54 |
|
---|
| 55 | /** Unknown tag was founded in report descriptor data*/
|
---|
| 56 | #define USB_HID_UNKNOWN_TAG -99
|
---|
| 57 |
|
---|
[b9d7965] | 58 | usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report, usb_hid_report_path_t *cmp_path)
|
---|
[1519b91] | 59 | {
|
---|
| 60 | /* find or append current collection path to the list */
|
---|
| 61 | link_t *path_it = report->collection_paths.next;
|
---|
| 62 | usb_hid_report_path_t *path = NULL;
|
---|
| 63 | while(path_it != &report->collection_paths) {
|
---|
| 64 | path = list_get_instance(path_it, usb_hid_report_path_t, link);
|
---|
| 65 |
|
---|
| 66 | if(usb_hid_report_compare_usage_path(path, cmp_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | path_it = path_it->next;
|
---|
| 70 | }
|
---|
| 71 | if(path_it == &report->collection_paths) {
|
---|
| 72 | path = usb_hid_report_path_clone(cmp_path);
|
---|
| 73 | list_append(&path->link, &report->collection_paths);
|
---|
| 74 | report->collection_paths_count++;
|
---|
[b9d7965] | 75 |
|
---|
| 76 | return path;
|
---|
| 77 | }
|
---|
| 78 | else {
|
---|
| 79 | return list_get_instance(path_it, usb_hid_report_path_t, link);
|
---|
[1519b91] | 80 | }
|
---|
| 81 | }
|
---|
[9d05599] | 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Initialize the report descriptor parser structure
|
---|
| 85 | *
|
---|
| 86 | * @param parser Report descriptor parser structure
|
---|
| 87 | * @return Error code
|
---|
| 88 | */
|
---|
| 89 | int usb_hid_report_init(usb_hid_report_t *report)
|
---|
| 90 | {
|
---|
| 91 | if(report == NULL) {
|
---|
| 92 | return EINVAL;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | memset(report, 0, sizeof(usb_hid_report_t));
|
---|
| 96 | list_initialize(&report->reports);
|
---|
| 97 | list_initialize(&report->collection_paths);
|
---|
| 98 |
|
---|
| 99 | report->use_report_ids = 0;
|
---|
| 100 | return EOK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
|
---|
| 104 | {
|
---|
| 105 | usb_hid_report_field_t *field;
|
---|
| 106 | int i;
|
---|
| 107 |
|
---|
| 108 | for(i=0; i<report_item->usages_count; i++){
|
---|
| 109 | usb_log_debug("usages (%d) - %x\n", i, report_item->usages[i]);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[1519b91] | 112 | usb_hid_report_path_t *path = report_item->usage_path;
|
---|
[9d05599] | 113 | for(i=0; i<report_item->count; i++){
|
---|
| 114 |
|
---|
| 115 | field = malloc(sizeof(usb_hid_report_field_t));
|
---|
| 116 | memset(field, 0, sizeof(usb_hid_report_field_t));
|
---|
| 117 | list_initialize(&field->link);
|
---|
| 118 |
|
---|
| 119 | /* fill the attributes */
|
---|
| 120 | field->logical_minimum = report_item->logical_minimum;
|
---|
| 121 | field->logical_maximum = report_item->logical_maximum;
|
---|
| 122 | field->physical_minimum = report_item->physical_minimum;
|
---|
| 123 | field->physical_maximum = report_item->physical_maximum;
|
---|
| 124 |
|
---|
| 125 | field->usage_minimum = report_item->usage_minimum;
|
---|
| 126 | field->usage_maximum = report_item->usage_maximum;
|
---|
| 127 | if(report_item->extended_usage_page != 0){
|
---|
| 128 | field->usage_page = report_item->extended_usage_page;
|
---|
| 129 | }
|
---|
| 130 | else {
|
---|
| 131 | field->usage_page = report_item->usage_page;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
|
---|
| 135 | uint32_t usage;
|
---|
| 136 | if(report_item->type != USB_HID_REPORT_TYPE_OUTPUT) {
|
---|
| 137 | if(i < report_item->usages_count){
|
---|
| 138 | usage = report_item->usages[i];
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | usage = report_item->usages[report_item->usages_count - 1];
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | else {
|
---|
| 145 | if((report_item->count - i - 1) < report_item->usages_count){
|
---|
| 146 | usage = report_item->usages[(report_item->count - i - 1)];
|
---|
| 147 | }
|
---|
| 148 | else {
|
---|
| 149 | usage = report_item->usages[report_item->usages_count - 1];
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 | if((usage & 0xFFFF0000) != 0){
|
---|
| 155 | field->usage_page = (usage >> 16);
|
---|
| 156 | field->usage = (usage & 0xFFFF);
|
---|
| 157 | }
|
---|
| 158 | else {
|
---|
| 159 | field->usage = usage;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
|
---|
| 166 | if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
|
---|
| 167 | field->usage = report_item->usage_maximum - i;
|
---|
| 168 | }
|
---|
| 169 | else {
|
---|
| 170 | field->usage = report_item->usage_minimum + i;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[1519b91] | 175 | usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL, field->usage_page);
|
---|
| 176 | usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL, field->usage);
|
---|
| 177 |
|
---|
[b9d7965] | 178 | field->collection_path = usb_hid_report_path_try_insert(report, path);
|
---|
[1519b91] | 179 |
|
---|
[9d05599] | 180 | field->size = report_item->size;
|
---|
| 181 | field->offset = report_item->offset + (i * report_item->size);
|
---|
| 182 | if(report_item->id != 0) {
|
---|
| 183 | field->offset += 8;
|
---|
| 184 | report->use_report_ids = 1;
|
---|
| 185 | }
|
---|
| 186 | field->item_flags = report_item->item_flags;
|
---|
| 187 |
|
---|
| 188 | /* find the right report list*/
|
---|
| 189 | usb_hid_report_description_t *report_des;
|
---|
| 190 | report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
|
---|
| 191 | if(report_des == NULL){
|
---|
| 192 | report_des = malloc(sizeof(usb_hid_report_description_t));
|
---|
| 193 | memset(report_des, 0, sizeof(usb_hid_report_description_t));
|
---|
| 194 |
|
---|
| 195 | report_des->type = report_item->type;
|
---|
| 196 | report_des->report_id = report_item->id;
|
---|
| 197 | list_initialize (&report_des->link);
|
---|
| 198 | list_initialize (&report_des->report_items);
|
---|
| 199 |
|
---|
| 200 | list_append(&report_des->link, &report->reports);
|
---|
| 201 | report->report_count++;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* append this field to the end of founded report list */
|
---|
| 205 | list_append (&field->link, &report_des->report_items);
|
---|
| 206 |
|
---|
| 207 | /* update the sizes */
|
---|
| 208 | report_des->bit_length += field->size;
|
---|
| 209 | report_des->item_length++;
|
---|
| 210 |
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | return EOK;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
---|
| 218 | {
|
---|
| 219 | link_t *report_it = report->reports.next;
|
---|
| 220 | usb_hid_report_description_t *report_des = NULL;
|
---|
| 221 |
|
---|
| 222 | while(report_it != &report->reports) {
|
---|
| 223 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
| 224 |
|
---|
| 225 | if((report_des->report_id == report_id) && (report_des->type == type)){
|
---|
| 226 | return report_des;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | report_it = report_it->next;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | return NULL;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Parse HID report descriptor.
|
---|
| 236 | *
|
---|
| 237 | * @param parser Opaque HID report parser structure.
|
---|
| 238 | * @param data Data describing the report.
|
---|
| 239 | * @return Error code.
|
---|
| 240 | */
|
---|
| 241 | int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
|
---|
| 242 | const uint8_t *data, size_t size)
|
---|
| 243 | {
|
---|
| 244 | size_t i=0;
|
---|
| 245 | uint8_t tag=0;
|
---|
| 246 | uint8_t item_size=0;
|
---|
| 247 | int class=0;
|
---|
| 248 | int ret;
|
---|
| 249 | usb_hid_report_item_t *report_item=0;
|
---|
| 250 | usb_hid_report_item_t *new_report_item;
|
---|
| 251 | usb_hid_report_path_t *usage_path;
|
---|
| 252 |
|
---|
| 253 | size_t offset_input=0;
|
---|
| 254 | size_t offset_output=0;
|
---|
| 255 | size_t offset_feature=0;
|
---|
| 256 |
|
---|
| 257 | link_t stack;
|
---|
| 258 | list_initialize(&stack);
|
---|
| 259 |
|
---|
| 260 | /* parser structure initialization*/
|
---|
| 261 | if(usb_hid_report_init(report) != EOK) {
|
---|
| 262 | return EINVAL;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /*report item initialization*/
|
---|
| 266 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
---|
| 267 | return ENOMEM;
|
---|
| 268 | }
|
---|
| 269 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
---|
| 270 | list_initialize(&(report_item->link));
|
---|
| 271 |
|
---|
| 272 | /* usage path context initialization */
|
---|
| 273 | if(!(usage_path=usb_hid_report_path())){
|
---|
| 274 | return ENOMEM;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | while(i<size){
|
---|
| 278 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
---|
| 279 |
|
---|
| 280 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
---|
| 281 | return EINVAL;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | tag = USB_HID_ITEM_TAG(data[i]);
|
---|
| 285 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
---|
| 286 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
---|
| 287 |
|
---|
| 288 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
---|
| 289 | item_size,report_item, usage_path);
|
---|
| 290 | switch(ret){
|
---|
| 291 | case USB_HID_NEW_REPORT_ITEM:
|
---|
| 292 | // store report item to report and create the new one
|
---|
| 293 | // store current collection path
|
---|
| 294 | report_item->usage_path = usage_path;
|
---|
| 295 |
|
---|
| 296 | usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
|
---|
| 297 | if(report_item->id != 0){
|
---|
| 298 | report->use_report_ids = 1;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | switch(tag) {
|
---|
| 302 | case USB_HID_REPORT_TAG_INPUT:
|
---|
| 303 | report_item->type = USB_HID_REPORT_TYPE_INPUT;
|
---|
| 304 | report_item->offset = offset_input;
|
---|
| 305 | offset_input += report_item->count * report_item->size;
|
---|
| 306 | break;
|
---|
| 307 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
| 308 | report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
|
---|
| 309 | report_item->offset = offset_output;
|
---|
| 310 | offset_output += report_item->count * report_item->size;
|
---|
| 311 |
|
---|
| 312 | break;
|
---|
| 313 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
| 314 | report_item->type = USB_HID_REPORT_TYPE_FEATURE;
|
---|
| 315 | report_item->offset = offset_feature;
|
---|
| 316 | offset_feature += report_item->count * report_item->size;
|
---|
| 317 | break;
|
---|
| 318 | default:
|
---|
| 319 | usb_log_debug("\tjump over - tag %X\n", tag);
|
---|
| 320 | break;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /*
|
---|
| 324 | * append new fields to the report
|
---|
| 325 | * structure
|
---|
| 326 | */
|
---|
| 327 | usb_hid_report_append_fields(report, report_item);
|
---|
| 328 |
|
---|
| 329 | /* reset local items */
|
---|
| 330 | usb_hid_report_reset_local_items (report_item);
|
---|
| 331 |
|
---|
| 332 | break;
|
---|
| 333 |
|
---|
| 334 | case USB_HID_RESET_OFFSET:
|
---|
| 335 | offset_input = 0;
|
---|
| 336 | offset_output = 0;
|
---|
| 337 | offset_feature = 0;
|
---|
| 338 | usb_hid_report_path_set_report_id (usage_path, report_item->id);
|
---|
| 339 | break;
|
---|
| 340 |
|
---|
| 341 | case USB_HID_REPORT_TAG_PUSH:
|
---|
| 342 | // push current state to stack
|
---|
| 343 | new_report_item = usb_hid_report_item_clone(report_item);
|
---|
| 344 | usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
|
---|
| 345 | new_report_item->usage_path = tmp_path;
|
---|
| 346 |
|
---|
| 347 | list_prepend (&new_report_item->link, &stack);
|
---|
| 348 | break;
|
---|
| 349 | case USB_HID_REPORT_TAG_POP:
|
---|
| 350 | // restore current state from stack
|
---|
| 351 | if(list_empty (&stack)) {
|
---|
| 352 | return EINVAL;
|
---|
| 353 | }
|
---|
| 354 | free(report_item);
|
---|
| 355 |
|
---|
| 356 | report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
|
---|
| 357 |
|
---|
| 358 | usb_hid_report_usage_path_t *tmp_usage_path;
|
---|
| 359 | tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
---|
| 360 |
|
---|
[1519b91] | 361 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
|
---|
| 362 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
|
---|
[9d05599] | 363 |
|
---|
| 364 | usb_hid_report_path_free(report_item->usage_path);
|
---|
| 365 | list_initialize(&report_item->usage_path->link);
|
---|
| 366 | list_remove (stack.next);
|
---|
| 367 |
|
---|
| 368 | break;
|
---|
| 369 |
|
---|
| 370 | default:
|
---|
| 371 | // nothing special to do
|
---|
| 372 | break;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /* jump over the processed block */
|
---|
| 376 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
---|
| 377 | }
|
---|
| 378 | else{
|
---|
| 379 | // TBD
|
---|
| 380 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return EOK;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
| 390 | /**
|
---|
| 391 | * Parse one tag of the report descriptor
|
---|
| 392 | *
|
---|
| 393 | * @param Tag to parse
|
---|
| 394 | * @param Report descriptor buffer
|
---|
| 395 | * @param Size of data belongs to this tag
|
---|
| 396 | * @param Current report item structe
|
---|
| 397 | * @return Code of action to be done next
|
---|
| 398 | */
|
---|
| 399 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
---|
| 400 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
| 401 | {
|
---|
| 402 | int ret;
|
---|
| 403 |
|
---|
| 404 | switch(class){
|
---|
| 405 | case USB_HID_TAG_CLASS_MAIN:
|
---|
| 406 |
|
---|
| 407 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
---|
| 408 | return USB_HID_NEW_REPORT_ITEM;
|
---|
| 409 | }
|
---|
| 410 | else {
|
---|
| 411 | /*TODO process the error */
|
---|
| 412 | return ret;
|
---|
| 413 | }
|
---|
| 414 | break;
|
---|
| 415 |
|
---|
| 416 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
| 417 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
---|
| 418 | break;
|
---|
| 419 |
|
---|
| 420 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
| 421 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
---|
| 422 | break;
|
---|
| 423 | default:
|
---|
| 424 | return USB_HID_NO_ACTION;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * Parse main tags of report descriptor
|
---|
| 430 | *
|
---|
| 431 | * @param Tag identifier
|
---|
| 432 | * @param Data buffer
|
---|
| 433 | * @param Length of data buffer
|
---|
| 434 | * @param Current state table
|
---|
| 435 | * @return Error code
|
---|
| 436 | */
|
---|
| 437 |
|
---|
| 438 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
| 439 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
| 440 | {
|
---|
| 441 | switch(tag)
|
---|
| 442 | {
|
---|
| 443 | case USB_HID_REPORT_TAG_INPUT:
|
---|
| 444 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
| 445 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
| 446 | report_item->item_flags = *data;
|
---|
| 447 | return EOK;
|
---|
| 448 | break;
|
---|
| 449 |
|
---|
| 450 | case USB_HID_REPORT_TAG_COLLECTION:
|
---|
| 451 | // TODO usage_path->flags = *data;
|
---|
| 452 | usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);
|
---|
| 453 | usb_hid_report_reset_local_items (report_item);
|
---|
| 454 | return USB_HID_NO_ACTION;
|
---|
| 455 | break;
|
---|
| 456 |
|
---|
| 457 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
---|
| 458 | usb_hid_report_remove_last_item(usage_path);
|
---|
| 459 | return USB_HID_NO_ACTION;
|
---|
| 460 | break;
|
---|
| 461 | default:
|
---|
| 462 | return USB_HID_NO_ACTION;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | return EOK;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | /**
|
---|
| 469 | * Parse global tags of report descriptor
|
---|
| 470 | *
|
---|
| 471 | * @param Tag identifier
|
---|
| 472 | * @param Data buffer
|
---|
| 473 | * @param Length of data buffer
|
---|
| 474 | * @param Current state table
|
---|
| 475 | * @return Error code
|
---|
| 476 | */
|
---|
| 477 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
| 478 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
| 479 | {
|
---|
| 480 | // TODO take care about the bit length of data
|
---|
| 481 | switch(tag)
|
---|
| 482 | {
|
---|
| 483 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
---|
| 484 | report_item->usage_page = usb_hid_report_tag_data_uint32(data, item_size);
|
---|
| 485 | break;
|
---|
| 486 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
---|
| 487 | report_item->logical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
| 488 | break;
|
---|
| 489 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
---|
| 490 | report_item->logical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
| 491 | break;
|
---|
| 492 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
---|
| 493 | report_item->physical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
| 494 | break;
|
---|
| 495 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
---|
| 496 | report_item->physical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
| 497 |
|
---|
| 498 | break;
|
---|
| 499 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
---|
| 500 | report_item->unit_exponent = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 501 | break;
|
---|
| 502 | case USB_HID_REPORT_TAG_UNIT:
|
---|
| 503 | report_item->unit = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 504 | break;
|
---|
| 505 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
---|
| 506 | report_item->size = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 507 | break;
|
---|
| 508 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
---|
| 509 | report_item->count = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 510 | break;
|
---|
| 511 | case USB_HID_REPORT_TAG_REPORT_ID:
|
---|
| 512 | report_item->id = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 513 | return USB_HID_RESET_OFFSET;
|
---|
| 514 | break;
|
---|
| 515 | case USB_HID_REPORT_TAG_PUSH:
|
---|
| 516 | case USB_HID_REPORT_TAG_POP:
|
---|
| 517 | /*
|
---|
| 518 | * stack operations are done in top level parsing
|
---|
| 519 | * function
|
---|
| 520 | */
|
---|
| 521 | return tag;
|
---|
| 522 | break;
|
---|
| 523 |
|
---|
| 524 | default:
|
---|
| 525 | return USB_HID_NO_ACTION;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | return EOK;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | /**
|
---|
| 532 | * Parse local tags of report descriptor
|
---|
| 533 | *
|
---|
| 534 | * @param Tag identifier
|
---|
| 535 | * @param Data buffer
|
---|
| 536 | * @param Length of data buffer
|
---|
| 537 | * @param Current state table
|
---|
| 538 | * @return Error code
|
---|
| 539 | */
|
---|
| 540 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
| 541 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
| 542 | {
|
---|
| 543 | switch(tag)
|
---|
| 544 | {
|
---|
| 545 | case USB_HID_REPORT_TAG_USAGE:
|
---|
| 546 | report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 547 | report_item->usages_count++;
|
---|
| 548 | break;
|
---|
| 549 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
---|
| 550 | if (item_size == 3) {
|
---|
| 551 | // usage extended usages
|
---|
| 552 | report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16;
|
---|
| 553 | report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
|
---|
| 554 | }
|
---|
| 555 | else {
|
---|
| 556 | report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 557 | }
|
---|
| 558 | break;
|
---|
| 559 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
---|
| 560 | if (item_size == 3) {
|
---|
| 561 | // usage extended usages
|
---|
| 562 | report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16;
|
---|
| 563 | report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
|
---|
| 564 | }
|
---|
| 565 | else {
|
---|
| 566 | report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 567 | }
|
---|
| 568 | break;
|
---|
| 569 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
---|
| 570 | report_item->designator_index = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 571 | break;
|
---|
| 572 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
---|
| 573 | report_item->designator_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 574 | break;
|
---|
| 575 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
---|
| 576 | report_item->designator_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 577 | break;
|
---|
| 578 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
---|
| 579 | report_item->string_index = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 580 | break;
|
---|
| 581 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
---|
| 582 | report_item->string_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 583 | break;
|
---|
| 584 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
---|
| 585 | report_item->string_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 586 | break;
|
---|
| 587 | case USB_HID_REPORT_TAG_DELIMITER:
|
---|
| 588 | //report_item->delimiter = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
| 589 | //TODO:
|
---|
| 590 | // DELIMITER STUFF
|
---|
| 591 | break;
|
---|
| 592 |
|
---|
| 593 | default:
|
---|
| 594 | return USB_HID_NO_ACTION;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | return EOK;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | /**
|
---|
| 601 | * Converts raw data to uint32 (thats the maximum length of short item data)
|
---|
| 602 | *
|
---|
| 603 | * @param Data buffer
|
---|
| 604 | * @param Size of buffer
|
---|
| 605 | * @return Converted int32 number
|
---|
| 606 | */
|
---|
| 607 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
|
---|
| 608 | {
|
---|
| 609 | unsigned int i;
|
---|
| 610 | uint32_t result;
|
---|
| 611 |
|
---|
| 612 | result = 0;
|
---|
| 613 | for(i=0; i<size; i++) {
|
---|
| 614 | result = (result | (data[i]) << (i*8));
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | return result;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | /**
|
---|
| 621 | * Prints content of given list of report items.
|
---|
| 622 | *
|
---|
| 623 | * @param List of report items (usb_hid_report_item_t)
|
---|
| 624 | * @return void
|
---|
| 625 | */
|
---|
| 626 | void usb_hid_descriptor_print_list(link_t *head)
|
---|
| 627 | {
|
---|
| 628 | usb_hid_report_field_t *report_item;
|
---|
| 629 | link_t *item;
|
---|
| 630 |
|
---|
| 631 |
|
---|
| 632 | if(head == NULL || list_empty(head)) {
|
---|
| 633 | usb_log_debug("\tempty\n");
|
---|
| 634 | return;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | for(item = head->next; item != head; item = item->next) {
|
---|
| 638 |
|
---|
| 639 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
---|
| 640 |
|
---|
| 641 | usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
|
---|
| 642 | usb_log_debug("\t\tSIZE: %X\n", report_item->size);
|
---|
| 643 | usb_log_debug("\t\tLOGMIN: %d\n", report_item->logical_minimum);
|
---|
| 644 | usb_log_debug("\t\tLOGMAX: %d\n", report_item->logical_maximum);
|
---|
| 645 | usb_log_debug("\t\tPHYMIN: %d\n", report_item->physical_minimum);
|
---|
| 646 | usb_log_debug("\t\tPHYMAX: %d\n", report_item->physical_maximum);
|
---|
| 647 | usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
|
---|
| 648 | usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
|
---|
| 649 |
|
---|
| 650 | usb_log_debug("\t\tVALUE: %X\n", report_item->value);
|
---|
| 651 | usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
|
---|
| 652 | usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
|
---|
[b9d7965] | 653 |
|
---|
| 654 | //usb_hid_print_usage_path(report_item->collection_path);
|
---|
| 655 |
|
---|
| 656 | usb_log_debug("\n");
|
---|
[9d05599] | 657 |
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
| 661 | }
|
---|
| 662 | /**
|
---|
| 663 | * Prints content of given report descriptor in human readable format.
|
---|
| 664 | *
|
---|
| 665 | * @param parser Parsed descriptor to print
|
---|
| 666 | * @return void
|
---|
| 667 | */
|
---|
| 668 | void usb_hid_descriptor_print(usb_hid_report_t *report)
|
---|
| 669 | {
|
---|
| 670 | if(report == NULL) {
|
---|
| 671 | return;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | link_t *report_it = report->reports.next;
|
---|
| 675 | usb_hid_report_description_t *report_des;
|
---|
| 676 |
|
---|
| 677 | while(report_it != &report->reports) {
|
---|
| 678 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
| 679 | usb_log_debug("Report ID: %d\n", report_des->report_id);
|
---|
| 680 | usb_log_debug("\tType: %d\n", report_des->type);
|
---|
| 681 | usb_log_debug("\tLength: %d\n", report_des->bit_length);
|
---|
| 682 | usb_log_debug("\tItems: %d\n", report_des->item_length);
|
---|
| 683 |
|
---|
| 684 | usb_hid_descriptor_print_list(&report_des->report_items);
|
---|
| 685 |
|
---|
| 686 |
|
---|
| 687 | link_t *path_it = report->collection_paths.next;
|
---|
| 688 | while(path_it != &report->collection_paths) {
|
---|
| 689 | usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
|
---|
| 690 | path_it = path_it->next;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | report_it = report_it->next;
|
---|
| 694 | }
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | /**
|
---|
| 698 | * Releases whole linked list of report items
|
---|
| 699 | *
|
---|
| 700 | * @param head Head of list of report descriptor items (usb_hid_report_item_t)
|
---|
| 701 | * @return void
|
---|
| 702 | */
|
---|
| 703 | void usb_hid_free_report_list(link_t *head)
|
---|
| 704 | {
|
---|
| 705 | return;
|
---|
| 706 |
|
---|
| 707 | usb_hid_report_item_t *report_item;
|
---|
| 708 | link_t *next;
|
---|
| 709 |
|
---|
| 710 | if(head == NULL || list_empty(head)) {
|
---|
| 711 | return;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | next = head->next;
|
---|
| 715 | while(next != head) {
|
---|
| 716 |
|
---|
| 717 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
---|
| 718 |
|
---|
| 719 | while(!list_empty(&report_item->usage_path->link)) {
|
---|
| 720 | usb_hid_report_remove_last_item(report_item->usage_path);
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 |
|
---|
| 724 | next = next->next;
|
---|
| 725 |
|
---|
| 726 | free(report_item);
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | return;
|
---|
| 730 |
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | /** Frees the HID report descriptor parser structure
|
---|
| 734 | *
|
---|
| 735 | * @param parser Opaque HID report parser structure
|
---|
| 736 | * @return void
|
---|
| 737 | */
|
---|
| 738 | void usb_hid_free_report(usb_hid_report_t *report)
|
---|
| 739 | {
|
---|
| 740 | if(report == NULL){
|
---|
| 741 | return;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | // free collection paths
|
---|
| 745 | usb_hid_report_path_t *path;
|
---|
| 746 | while(!list_empty(&report->collection_paths)) {
|
---|
| 747 | path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
|
---|
| 748 | usb_hid_report_path_free(path);
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | // free report items
|
---|
| 752 | usb_hid_report_description_t *report_des;
|
---|
| 753 | usb_hid_report_field_t *field;
|
---|
| 754 | while(!list_empty(&report->reports)) {
|
---|
| 755 | report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
|
---|
| 756 | list_remove(&report_des->link);
|
---|
| 757 |
|
---|
| 758 | while(!list_empty(&report_des->report_items)) {
|
---|
| 759 | field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
|
---|
| 760 | list_remove(&field->link);
|
---|
| 761 |
|
---|
| 762 | free(field);
|
---|
| 763 | }
|
---|
| 764 |
|
---|
| 765 | free(report_des);
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | return;
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | /**
|
---|
| 772 | * @}
|
---|
| 773 | */
|
---|