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