Changeset 069b80d in mainline
- Timestamp:
- 2011-10-20T20:29:58Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2fc487f, 4267908, e2793a4, e3ca824
- Parents:
- d75628da
- Location:
- uspace/lib/usbhid
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbhid/include/usb/hid/hiddescriptor.h
rd75628da r069b80d 78 78 void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item); 79 79 80 void usb_hid_free_report_list(list_t *list);81 82 80 usb_hid_report_item_t *usb_hid_report_item_clone( 83 81 const usb_hid_report_item_t *item); -
uspace/lib/usbhid/src/hiddescriptor.c
rd75628da r069b80d 974 974 /*---------------------------------------------------------------------------*/ 975 975 976 /**977 * Releases whole linked list of report items978 *979 * @param list List of report descriptor items (usb_hid_report_item_t)980 * @return void981 */982 void usb_hid_free_report_list(list_t *list)983 {984 return; /* XXX What's this? */985 986 /* usb_hid_report_item_t *report_item;987 link_t *next;988 989 if(list == NULL || list_empty(list)) {990 return;991 }992 993 next = list->head.next;994 while (next != &list->head) {995 report_item = list_get_instance(next, usb_hid_report_item_t,996 rpath_items_link);997 998 while(!list_empty(&report_item->usage_path->link)) {999 usb_hid_report_remove_last_item(report_item->usage_path);1000 }1001 1002 1003 next = next->next;1004 1005 free(report_item);1006 }1007 1008 return;1009 */1010 }1011 /*---------------------------------------------------------------------------*/1012 976 1013 977 /** Frees the HID report descriptor parser structure -
uspace/lib/usbhid/src/hidparser.c
rd75628da r069b80d 52 52 int32_t value); 53 53 54 int usb_pow(int a, int b); 55 56 /*---------------------------------------------------------------------------*/ 57 58 // TODO: tohle ma bejt asi jinde 59 int usb_pow(int a, int b) 60 { 61 switch(b) { 54 /*---------------------------------------------------------------------------*/ 55 56 static int usb_pow(int a, int b) 57 { 58 switch (b) { 62 59 case 0: 63 60 return 1; … … 67 64 break; 68 65 default: 69 return a * usb_pow(a, b -1);66 return a * usb_pow(a, b - 1); 70 67 break; 71 68 } … … 81 78 */ 82 79 size_t usb_hid_report_size(usb_hid_report_t *report, uint8_t report_id, 83 80 usb_hid_report_type_t type) 84 81 { 85 82 usb_hid_report_description_t *report_des; 86 83 87 if (report == NULL) {84 if (report == NULL) { 88 85 return 0; 89 86 } 90 87 91 report_des = usb_hid_report_find_description 92 if (report_des == NULL){88 report_des = usb_hid_report_find_description(report, report_id, type); 89 if (report_des == NULL) { 93 90 return 0; 94 } 95 else { 91 } else { 96 92 return report_des->item_length; 97 93 } … … 106 102 */ 107 103 size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id, 108 104 usb_hid_report_type_t type) 109 105 { 110 106 usb_hid_report_description_t *report_des; 111 107 112 if (report == NULL) {108 if (report == NULL) { 113 109 return 0; 114 110 } 115 111 116 report_des = usb_hid_report_find_description 117 if (report_des == NULL){112 report_des = usb_hid_report_find_description(report, report_id, type); 113 if (report_des == NULL) { 118 114 return 0; 119 } 120 else { 115 } else { 121 116 return ((report_des->bit_length + 7) / 8) ; 122 117 } … … 133 128 */ 134 129 int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data, 135 130 size_t size, uint8_t *report_id) 136 131 { 137 132 usb_hid_report_field_t *item; … … 140 135 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT; 141 136 142 if (report == NULL) {137 if (report == NULL) { 143 138 return EINVAL; 144 139 } 145 140 146 if (report->use_report_ids != 0) {141 if (report->use_report_ids != 0) { 147 142 *report_id = data[0]; 148 } 149 else { 143 } else { 150 144 *report_id = 0; 151 145 } 152 146 153 154 147 report_des = usb_hid_report_find_description(report, *report_id, 155 156 157 if (report_des == NULL) {148 type); 149 150 if (report_des == NULL) { 158 151 return EINVAL; 159 152 } … … 162 155 list_foreach(report_des->report_items, list_item) { 163 156 item = list_get_instance(list_item, usb_hid_report_field_t, 164 165 166 if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {157 ritems_link); 158 159 if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) { 167 160 168 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0){ 169 170 // array 161 if (USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) { 162 /* array */ 171 163 item->value = 172 164 usb_hid_translate_data(item, data); 173 165 174 166 item->usage = USB_HID_EXTENDED_USAGE( 175 item->usages[ 176 item-> value - item->physical_minimum]);167 item->usages[item->value - 168 item->physical_minimum]); 177 169 178 170 item->usage_page = 179 171 USB_HID_EXTENDED_USAGE_PAGE( 180 item->usages[ 181 item-> value - item->physical_minimum]);182 183 usb_hid_report_set_last_item 172 item->usages[item->value - 173 item->physical_minimum]); 174 175 usb_hid_report_set_last_item( 184 176 item->collection_path, 185 177 USB_HID_TAG_CLASS_GLOBAL, 186 178 item->usage_page); 187 179 188 usb_hid_report_set_last_item 180 usb_hid_report_set_last_item( 189 181 item->collection_path, 190 182 USB_HID_TAG_CLASS_LOCAL, item->usage); 191 192 } 193 else { 194 // variable item 183 } else { 184 /* variable item */ 195 185 item->value = usb_hid_translate_data(item, 196 186 data); … … 200 190 201 191 return EOK; 202 203 192 } 204 193 … … 217 206 int part_size; 218 207 219 int32_t value =0;220 int32_t mask =0;221 const uint8_t *foo =0;222 223 / / now only shot tags are allowed224 if (item->size > 32) {208 int32_t value = 0; 209 int32_t mask = 0; 210 const uint8_t *foo = 0; 211 212 /* now only short tags are allowed */ 213 if (item->size > 32) { 225 214 return 0; 226 215 } 227 216 228 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)){217 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) { 229 218 item->physical_minimum = item->logical_minimum; 230 219 item->physical_maximum = item->logical_maximum; … … 232 221 233 222 234 if (item->physical_maximum == item->physical_minimum){223 if (item->physical_maximum == item->physical_minimum) { 235 224 resolution = 1; 236 } 237 else { 225 } else { 238 226 resolution = (item->logical_maximum - item->logical_minimum) / 239 227 ((item->physical_maximum - item->physical_minimum) * 240 (usb_pow(10, (item->unit_exponent))));228 (usb_pow(10, (item->unit_exponent)))); 241 229 } 242 230 243 231 offset = item->offset; 244 232 // FIXME 245 if ((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {233 if ((size_t) (offset / 8) != (size_t) ((offset+item->size - 1) / 8)) { 246 234 247 235 part_size = 0; 248 236 249 size_t i=0; 250 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){ 251 if(i == (size_t)(offset/8)) { 252 // the higher one 237 size_t i = 0; 238 for (i = (size_t) (offset / 8); 239 i <= (size_t) (offset + item->size - 1) / 8; i++) { 240 if (i == (size_t) (offset / 8)) { 241 /* the higher one */ 253 242 part_size = 8 - (offset % 8); 254 243 foo = data + i; 255 mask = ((1 << (item->size -part_size))-1);244 mask = ((1 << (item->size - part_size)) - 1); 256 245 value = (*foo & mask); 257 } 258 else if(i == ((offset+item->size-1)/8)){ 259 // the lower one 246 } else if (i == ((offset + item->size - 1) / 8)) { 247 /* the lower one */ 260 248 foo = data + i; 261 mask = ((1 << (item->size - part_size)) - 1) 262 <<(8 - (item->size - part_size));249 mask = ((1 << (item->size - part_size)) - 1) << 250 (8 - (item->size - part_size)); 263 251 264 252 value = (((*foo & mask) >> (8 - 265 (item->size - part_size))) << part_size )266 +value;267 } 268 else {269 value = (*(data + 1) << (part_size + 8)) +value;253 (item->size - part_size))) << part_size) + 254 value; 255 } else { 256 value = (*(data + 1) << (part_size + 8)) + 257 value; 270 258 part_size += 8; 271 259 } 272 260 } 273 } 274 else {275 foo = data+(offset/8);276 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));277 value = (*foo & mask) >> (8 -((offset%8)+item->size));278 } 279 280 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)){261 } else { 262 foo = data + (offset / 8); 263 mask = ((1 << item->size) - 1) << 264 (8 - ((offset % 8) + item->size)); 265 value = (*foo & mask) >> (8 - ((offset % 8) + item->size)); 266 } 267 268 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) { 281 269 value = USB_HID_UINT32_TO_INT32(value, item->size); 282 270 } 283 271 284 return (int)(((value - item->logical_minimum) / resolution) + 285 item->physical_minimum); 286 272 return (int) (((value - item->logical_minimum) / resolution) + 273 item->physical_minimum); 287 274 } 288 275 … … 299 286 */ 300 287 uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, 301 302 { 303 if (report == NULL) {288 uint8_t report_id) 289 { 290 if (report == NULL) { 304 291 *size = 0; 305 292 return NULL; … … 310 297 list_foreach(report->reports, report_it) { 311 298 report_des = list_get_instance(report_it, 312 299 usb_hid_report_description_t, reports_link); 313 300 314 if ((report_des->report_id == report_id) &&315 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){301 if ((report_des->report_id == report_id) && 302 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)) { 316 303 break; 317 304 } 318 305 } 319 306 320 if (report_des == NULL){307 if (report_des == NULL) { 321 308 *size = 0; 322 309 return NULL; 323 } 324 else { 325 *size = (report_des->bit_length + (8 - 1))/8; 310 } else { 311 *size = (report_des->bit_length + (8 - 1)) / 8; 326 312 uint8_t *ret = malloc((*size) * sizeof(uint8_t)); 327 313 memset(ret, 0, (*size) * sizeof(uint8_t)); … … 337 323 */ 338 324 void usb_hid_report_output_free(uint8_t *output) 339 340 { 341 if(output != NULL) { 342 free (output); 325 { 326 if (output != NULL) { 327 free(output); 343 328 } 344 329 } … … 354 339 */ 355 340 int usb_hid_report_output_translate(usb_hid_report_t *report, 356 357 { 358 int32_t value =0;341 uint8_t report_id, uint8_t *buffer, size_t size) 342 { 343 int32_t value = 0; 359 344 int offset; 360 345 int length; 361 346 int32_t tmp_value; 362 347 363 if (report == NULL) {348 if (report == NULL) { 364 349 return EINVAL; 365 350 } 366 351 367 if (report->use_report_ids != 0) {352 if (report->use_report_ids != 0) { 368 353 buffer[0] = report_id; 369 354 } 370 355 371 356 usb_hid_report_description_t *report_des; 372 report_des = usb_hid_report_find_description 373 374 375 if (report_des == NULL){357 report_des = usb_hid_report_find_description(report, report_id, 358 USB_HID_REPORT_TYPE_OUTPUT); 359 360 if (report_des == NULL) { 376 361 return EINVAL; 377 362 } … … 384 369 385 370 value = usb_hid_translate_data_reverse(report_item, 386 371 report_item->value); 387 372 388 373 offset = report_des->bit_length - report_item->offset - 1; … … 391 376 usb_log_debug("\ttranslated value: %x\n", value); 392 377 393 if((offset/8) == ((offset+length-1)/8)) { 394 // je to v jednom bytu 395 if(((size_t)(offset/8) >= size) || 396 ((size_t)(offset+length-1)/8) >= size) { 378 if ((offset / 8) == ((offset + length - 1) / 8)) { 379 if (((size_t) (offset / 8) >= size) || 380 ((size_t) (offset + length - 1) / 8) >= size) { 397 381 break; // TODO ErrorCode 398 382 } 399 size_t shift = 8 - offset %8 - length;383 size_t shift = 8 - offset % 8 - length; 400 384 value = value << shift; 401 value = value & (((1 << length) -1) << shift);385 value = value & (((1 << length) - 1) << shift); 402 386 403 387 uint8_t mask = 0; 404 388 mask = 0xff - (((1 << length) - 1) << shift); 405 buffer[offset /8] = (buffer[offset/8] & mask) | value;406 }407 else {389 buffer[offset / 8] = (buffer[offset / 8] & mask) | 390 value; 391 } else { 408 392 int i = 0; 409 393 uint8_t mask = 0; 410 for(i = (offset/8); i <= ((offset+length-1)/8); i++) { 411 if(i == (offset/8)) { 394 for (i = (offset / 8); 395 i <= ((offset + length - 1) / 8); i++) { 396 if (i == (offset / 8)) { 412 397 tmp_value = value; 413 398 tmp_value = tmp_value & 414 ((1 << (8-(offset%8)))-1);415 416 tmp_value = tmp_value << (offset %8);417 418 mask = ~(((1 << (8 -(offset%8)))-1) <<419 (offset%8));399 ((1 << (8 - (offset % 8))) - 1); 400 401 tmp_value = tmp_value << (offset % 8); 402 403 mask = ~(((1 << (8 - (offset % 8))) - 1) 404 << (offset % 8)); 420 405 421 406 buffer[i] = (buffer[i] & mask) | 422 tmp_value; 423 } 424 else if (i == ((offset + length -1)/8)) { 407 tmp_value; 408 } else if (i == ((offset + length - 1) / 8)) { 425 409 426 410 value = value >> (length - 427 411 ((offset + length) % 8)); 428 412 429 413 value = value & ((1 << (length - 430 414 ((offset + length) % 8))) - 1); 431 415 432 416 mask = (1 << (length - 433 417 ((offset + length) % 8))) - 1; 434 418 435 419 buffer[i] = (buffer[i] & mask) | value; 436 } 437 else { 438 buffer[i] = value & (0xFF << i); 420 } else { 421 buffer[i] = value & (0xff << i); 439 422 } 440 423 } 441 424 } 442 425 443 / / reset value426 /* reset value */ 444 427 report_item->value = 0; 445 428 } … … 456 439 */ 457 440 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, 458 459 { 460 int ret =0;441 int value) 442 { 443 int ret = 0; 461 444 int resolution; 462 445 463 if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {446 if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) { 464 447 ret = item->logical_minimum; 465 448 } 466 449 467 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)){450 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) { 468 451 item->physical_minimum = item->logical_minimum; 469 452 item->physical_maximum = item->logical_maximum; 470 453 } 471 454 472 / / variable item473 if (item->physical_maximum == item->physical_minimum){455 /* variable item */ 456 if (item->physical_maximum == item->physical_minimum) { 474 457 resolution = 1; 475 } 476 else { 458 } else { 477 459 resolution = (item->logical_maximum - item->logical_minimum) / 478 460 ((item->physical_maximum - item->physical_minimum) * 479 (usb_pow(10, (item->unit_exponent))));461 (usb_pow(10, (item->unit_exponent)))); 480 462 } 481 463 482 464 ret = ((value - item->physical_minimum) * resolution) + 483 484 485 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), \486 487 488 489 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)){465 item->logical_minimum; 466 467 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), " 468 "ret(%x)\n", value, resolution, item->physical_minimum, 469 item->logical_minimum, ret); 470 471 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) { 490 472 return USB_HID_INT32_TO_UINT32(ret, item->size); 491 473 } 492 return (int32_t)0 + ret; 474 475 return (int32_t) 0 + ret; 493 476 } 494 477 … … 501 484 */ 502 485 usb_hid_report_item_t *usb_hid_report_item_clone( 503 486 const usb_hid_report_item_t *item) 504 487 { 505 488 usb_hid_report_item_t *new_report_item; 506 489 507 if (!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {490 if (!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) { 508 491 return NULL; 509 492 } … … 529 512 */ 530 513 usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report, 531 532 514 usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags, 515 usb_hid_report_type_t type) 533 516 { 534 517 usb_hid_report_description_t *report_des = 535 518 usb_hid_report_find_description(report, path->report_id, type); 536 519 537 520 link_t *field_it; 538 521 539 if (report_des == NULL){522 if (report_des == NULL) { 540 523 return NULL; 541 524 } 542 525 543 if (field == NULL){526 if (field == NULL) { 544 527 field_it = report_des->report_items.head.next; 545 } 546 else { 528 } else { 547 529 field_it = field->ritems_link.next; 548 530 } 549 531 550 while (field_it != &report_des->report_items.head) {532 while (field_it != &report_des->report_items.head) { 551 533 field = list_get_instance(field_it, usb_hid_report_field_t, 552 ritems_link); 553 554 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 555 usb_hid_report_path_append_item ( 556 field->collection_path, field->usage_page, 557 field->usage); 558 559 if(usb_hid_report_compare_usage_path( 560 field->collection_path, path, flags) == EOK){ 561 534 ritems_link); 535 536 if (USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 537 usb_hid_report_path_append_item(field->collection_path, 538 field->usage_page, field->usage); 539 540 if (usb_hid_report_compare_usage_path( 541 field->collection_path, path, flags) == EOK) { 562 542 usb_hid_report_remove_last_item( 563 field->collection_path); 564 543 field->collection_path); 565 544 return field; 566 545 } 567 usb_hid_report_remove_last_item ( 568 field->collection_path); 546 usb_hid_report_remove_last_item(field->collection_path); 569 547 } 570 548 field_it = field_it->next; … … 586 564 * @retval report_id otherwise 587 565 */ 588 uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, 589 uint8_t report_id,usb_hid_report_type_t type)590 { 591 if (report == NULL){566 uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, uint8_t report_id, 567 usb_hid_report_type_t type) 568 { 569 if (report == NULL) { 592 570 return 0; 593 571 } … … 596 574 link_t *report_it; 597 575 598 if (report_id > 0) {576 if (report_id > 0) { 599 577 report_des = usb_hid_report_find_description(report, report_id, 600 601 if (report_des == NULL) {578 type); 579 if (report_des == NULL) { 602 580 return 0; 603 } 604 else { 581 } else { 605 582 report_it = report_des->reports_link.next; 606 583 } 607 } 608 else { 584 } else { 609 585 report_it = report->reports.head.next; 610 586 } 611 587 612 while (report_it != &report->reports.head) {588 while (report_it != &report->reports.head) { 613 589 report_des = list_get_instance(report_it, 614 615 616 if (report_des->type == type){590 usb_hid_report_description_t, reports_link); 591 592 if (report_des->type == type) { 617 593 return report_des->report_id; 618 594 } … … 635 611 void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item) 636 612 { 637 if (report_item == NULL){613 if (report_item == NULL) { 638 614 return; 639 615 } … … 651 627 report_item->string_minimum = 0; 652 628 report_item->string_maximum = 0; 653 654 return; 655 } 629 } 630 656 631 /** 657 632 * @}
Note:
See TracChangeset
for help on using the changeset viewer.