Changes in uspace/drv/bus/usb/usbhid/usbhid.c [5f6e25e:065064e6] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/usbhid/usbhid.c
r5f6e25e r065064e6 54 54 55 55 /* Array of endpoints expected on the device, NULL terminated. */ 56 usb_endpoint_description_t *usb_hid_endpoints[ USB_HID_POLL_EP_COUNT + 1] = {56 usb_endpoint_description_t *usb_hid_endpoints[] = { 57 57 &usb_hid_kbd_poll_endpoint_description, 58 58 &usb_hid_mouse_poll_endpoint_description, … … 68 68 { 69 69 assert(hid_dev != NULL && hid_dev->subdriver_count == 0); 70 70 71 71 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc( 72 72 sizeof(usb_hid_subdriver_t)); … … 74 74 return ENOMEM; 75 75 } 76 76 77 77 assert(hid_dev->subdriver_count >= 0); 78 78 79 79 // set the init callback 80 80 hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_kbd_init; 81 81 82 82 // set the polling callback 83 83 hid_dev->subdrivers[hid_dev->subdriver_count].poll = 84 84 usb_kbd_polling_callback; 85 85 86 86 // set the polling ended callback 87 87 hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL; 88 88 89 89 // set the deinit callback 90 90 hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_kbd_deinit; 91 91 92 92 // set subdriver count 93 93 ++hid_dev->subdriver_count; 94 94 95 95 return EOK; 96 96 } … … 101 101 { 102 102 assert(hid_dev != NULL && hid_dev->subdriver_count == 0); 103 103 104 104 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc( 105 105 sizeof(usb_hid_subdriver_t)); … … 107 107 return ENOMEM; 108 108 } 109 109 110 110 assert(hid_dev->subdriver_count >= 0); 111 111 112 112 // set the init callback 113 113 hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_mouse_init; 114 114 115 115 // set the polling callback 116 116 hid_dev->subdrivers[hid_dev->subdriver_count].poll = 117 117 usb_mouse_polling_callback; 118 118 119 119 // set the polling ended callback 120 120 hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL; 121 121 122 122 // set the deinit callback 123 123 hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_mouse_deinit; 124 124 125 125 // set subdriver count 126 126 ++hid_dev->subdriver_count; 127 127 128 128 return EOK; 129 129 } … … 134 134 { 135 135 assert(hid_dev != NULL && hid_dev->subdriver_count == 0); 136 136 137 137 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc( 138 138 sizeof(usb_hid_subdriver_t)); … … 140 140 return ENOMEM; 141 141 } 142 142 143 143 assert(hid_dev->subdriver_count >= 0); 144 144 145 145 // set the init callback 146 146 hid_dev->subdrivers[hid_dev->subdriver_count].init = 147 147 usb_generic_hid_init; 148 148 149 149 // set the polling callback 150 150 hid_dev->subdrivers[hid_dev->subdriver_count].poll = 151 151 usb_generic_hid_polling_callback; 152 152 153 153 // set the polling ended callback 154 154 hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL; 155 155 156 156 // set the deinit callback 157 hid_dev->subdrivers[hid_dev->subdriver_count].deinit = NULL; 158 157 hid_dev->subdrivers[hid_dev->subdriver_count].deinit = 158 usb_generic_hid_deinit; 159 159 160 // set subdriver count 160 161 ++hid_dev->subdriver_count; 161 162 162 163 return EOK; 163 164 } … … 170 171 assert(hid_dev != NULL); 171 172 assert(hid_dev->usb_dev != NULL); 172 173 173 174 return (hid_dev->usb_dev->descriptors.device.vendor_id 174 175 == mapping->vendor_id … … 184 185 assert(hid_dev != NULL); 185 186 assert(mapping != NULL); 186 187 187 188 usb_hid_report_path_t *usage_path = usb_hid_report_path(); 188 189 if (usage_path == NULL) { … … 202 203 ++i; 203 204 } 204 205 205 206 assert(hid_dev->report != NULL); 206 207 207 208 usb_log_debug("Compare flags: %d\n", mapping->compare); 208 209 209 210 bool matches = false; 210 211 uint8_t report_id = mapping->report_id; … … 234 235 USB_HID_REPORT_TYPE_INPUT); 235 236 } while (!matches && report_id != 0); 236 237 237 238 usb_hid_report_path_free(usage_path); 238 239 239 240 return matches; 240 241 } … … 246 247 { 247 248 int i; 248 249 249 250 if (count <= 0) { 250 251 hid_dev->subdriver_count = 0; … … 252 253 return EOK; 253 254 } 254 255 255 256 // add one generic HID subdriver per device 256 257 257 258 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) * 258 259 sizeof(usb_hid_subdriver_t)); … … 260 261 return ENOMEM; 261 262 } 262 263 263 264 for (i = 0; i < count; ++i) { 264 265 hid_dev->subdrivers[i].init = subdrivers[i]->init; … … 267 268 hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end; 268 269 } 269 270 270 271 hid_dev->subdrivers[count].init = usb_generic_hid_init; 271 272 hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback; 272 hid_dev->subdrivers[count].deinit = NULL;273 hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit; 273 274 hid_dev->subdrivers[count].poll_end = NULL; 274 275 275 276 hid_dev->subdriver_count = count + 1; 276 277 277 278 return EOK; 278 279 } … … 283 284 { 284 285 assert(hid_dev != NULL); 285 286 286 287 const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS]; 287 288 288 289 int i = 0, count = 0; 289 290 const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i]; … … 291 292 bool ids_matched; 292 293 bool matched; 293 294 294 295 while (count < USB_HID_MAX_SUBDRIVERS && 295 296 (mapping->usage_path != NULL … … 339 340 mapping = &usb_hid_subdrivers[++i]; 340 341 } 341 342 342 343 // we have all subdrivers determined, save them into the hid device 343 344 return usb_hid_save_subdrivers(hid_dev, subdrivers, count); … … 349 350 { 350 351 assert(hid_dev != NULL && dev != NULL); 351 352 352 353 int rc = EOK; 353 354 354 355 if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) { 355 356 usb_log_debug("Found keyboard endpoint.\n"); … … 369 370 rc = ENOTSUP; 370 371 } 371 372 372 373 return rc; 373 374 } … … 378 379 { 379 380 assert(hid_dev != NULL && hid_dev->report != NULL); 380 381 381 382 uint8_t report_id = 0; 382 383 size_t size; 383 384 384 385 size_t max_size = 0; 385 386 386 387 do { 387 388 usb_log_debug("Getting size of the report.\n"); … … 394 395 report_id, USB_HID_REPORT_TYPE_INPUT); 395 396 } while (report_id != 0); 396 397 397 398 usb_log_debug("Max size of input report: %zu\n", max_size); 398 399 399 400 hid_dev->max_input_report_size = max_size; 400 401 assert(hid_dev->input_report == NULL); 401 402 402 403 hid_dev->input_report = malloc(max_size); 403 404 if (hid_dev->input_report == NULL) { … … 405 406 } 406 407 memset(hid_dev->input_report, 0, max_size); 407 408 408 409 return EOK; 409 410 } … … 411 412 /*----------------------------------------------------------------------------*/ 412 413 413 usb_hid_dev_t *usb_hid_new(void)414 {415 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,416 sizeof(usb_hid_dev_t));417 418 if (hid_dev == NULL) {419 usb_log_fatal("No memory!\n");420 return NULL;421 }422 423 hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(424 usb_hid_report_t)));425 if (hid_dev->report == NULL) {426 usb_log_fatal("No memory!\n");427 free(hid_dev);428 return NULL;429 }430 431 hid_dev->poll_pipe_index = -1;432 433 return hid_dev;434 }435 436 /*----------------------------------------------------------------------------*/437 438 414 int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev) 439 415 { 440 416 int rc, i; 441 417 442 418 usb_log_debug("Initializing HID structure...\n"); 443 419 444 420 if (hid_dev == NULL) { 445 421 usb_log_error("Failed to init HID structure: no structure given" … … 447 423 return EINVAL; 448 424 } 449 425 450 426 if (dev == NULL) { 451 427 usb_log_error("Failed to init HID structure: no USB device" … … 453 429 return EINVAL; 454 430 } 455 431 432 hid_dev->report = (usb_hid_report_t *)(malloc(sizeof( 433 usb_hid_report_t))); 434 if (hid_dev->report == NULL) { 435 usb_log_error("No memory!\n"); 436 return ENOMEM; 437 } 438 usb_hid_report_init(hid_dev->report); 439 456 440 /* The USB device should already be initialized, save it in structure */ 457 441 hid_dev->usb_dev = dev; 458 442 hid_dev->poll_pipe_index = -1; 443 459 444 rc = usb_hid_check_pipes(hid_dev, dev); 460 445 if (rc != EOK) { … … 465 450 rc = usb_hid_process_report_descriptor(hid_dev->usb_dev, 466 451 hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size); 467 452 468 453 bool fallback = false; 469 454 470 455 if (rc == EOK) { 471 456 // try to find subdrivers that may want to handle this device … … 484 469 fallback = true; 485 470 } 486 471 487 472 if (fallback) { 488 473 // fall back to boot protocol … … 510 495 } 511 496 } 512 497 513 498 if (rc != EOK) { 514 499 usb_log_error("No subdriver for handling this device could be" … … 542 527 rc = (ok) ? EOK : -1; // what error to report 543 528 } 544 545 529 530 546 531 if (rc == EOK) { 547 532 // save max input report size and allocate space for the report … … 552 537 } 553 538 } 554 555 539 540 556 541 return rc; 557 542 } … … 563 548 { 564 549 int i; 565 550 566 551 if (dev == NULL || arg == NULL || buffer == NULL) { 567 552 usb_log_error("Missing arguments to polling callback.\n"); 568 553 return false; 569 554 } 570 555 571 556 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg; 572 557 573 558 assert(hid_dev->input_report != NULL); 574 559 usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size, … … 582 567 usb_hid_new_report(hid_dev); 583 568 } 584 569 585 570 // parse the input report 586 571 587 572 int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size, 588 573 &hid_dev->report_id); 589 574 590 575 if (rc != EOK) { 591 576 usb_log_warning("Error in usb_hid_parse_report():" 592 577 "%s\n", str_error(rc)); 593 578 } 594 579 595 580 bool cont = false; 596 581 597 582 // continue if at least one of the subdrivers want to continue 598 583 for (i = 0; i < hid_dev->subdriver_count; ++i) { … … 603 588 } 604 589 } 605 590 606 591 return cont; 607 592 } … … 613 598 { 614 599 int i; 615 600 616 601 if (dev == NULL || arg == NULL) { 617 602 return; 618 603 } 619 604 620 605 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg; 621 606 622 607 for (i = 0; i < hid_dev->subdriver_count; ++i) { 623 608 if (hid_dev->subdrivers[i].poll_end != NULL) { … … 626 611 } 627 612 } 628 629 usb_hid_destroy(hid_dev);613 614 hid_dev->running = false; 630 615 } 631 616 … … 649 634 { 650 635 int i; 651 636 652 637 if (hid_dev == NULL) { 653 638 return; 654 639 } 655 640 656 641 usb_log_debug("Subdrivers: %p, subdriver count: %d\n", 657 642 hid_dev->subdrivers, hid_dev->subdriver_count); 658 643 659 644 assert(hid_dev->subdrivers != NULL 660 645 || hid_dev->subdriver_count == 0); 661 646 662 647 for (i = 0; i < hid_dev->subdriver_count; ++i) { 663 648 if (hid_dev->subdrivers[i].deinit != NULL) { … … 666 651 } 667 652 } 668 669 // free the subdrivers info 670 if (hid_dev->subdrivers != NULL) { 671 free(hid_dev->subdrivers); 672 } 673 674 // destroy the parser 653 654 /* Free allocated structures */ 655 free(hid_dev->subdrivers); 656 free(hid_dev->report_desc); 657 658 /* Destroy the parser */ 675 659 if (hid_dev->report != NULL) { 676 660 usb_hid_free_report(hid_dev->report); 677 661 } 678 662 679 if (hid_dev->report_desc != NULL) {680 free(hid_dev->report_desc);681 }682 663 } 683 664
Note:
See TracChangeset
for help on using the changeset viewer.