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