Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/hid/usbhid/usbhid.c

    rb7fd2a0 rbe01eb3  
    134134        usb_hid_report_path_t *usage_path = usb_hid_report_path();
    135135        if (usage_path == NULL) {
    136                 usb_log_debug("Failed to create usage path.\n");
     136                usb_log_debug("Failed to create usage path.");
    137137                return false;
    138138        }
     
    143143                    mapping->usage_path[i].usage_page,
    144144                    mapping->usage_path[i].usage) != EOK) {
    145                         usb_log_debug("Failed to append to usage path.\n");
     145                        usb_log_debug("Failed to append to usage path.");
    146146                        usb_hid_report_path_free(usage_path);
    147147                        return false;
     
    149149        }
    150150
    151         usb_log_debug("Compare flags: %d\n", mapping->compare);
     151        usb_log_debug("Compare flags: %d", mapping->compare);
    152152
    153153        bool matches = false;
     
    155155
    156156        do {
    157                 usb_log_debug("Trying report id %u\n", report_id);
     157                usb_log_debug("Trying report id %u", report_id);
    158158                if (report_id != 0) {
    159159                        usb_hid_report_path_set_report_id(usage_path,
     
    166166                        USB_HID_REPORT_TYPE_INPUT);
    167167
    168                 usb_log_debug("Field: %p\n", field);
     168                usb_log_debug("Field: %p", field);
    169169
    170170                if (field != NULL) {
     
    243243                            mapping->product_id);
    244244                        if (usb_hid_ids_match(hid_dev, mapping)) {
    245                                 usb_log_debug("IDs matched.\n");
     245                                usb_log_debug("IDs matched.");
    246246                                matched = true;
    247247                        }
     
    250250                /* Check usage match. */
    251251                if (mapping->usage_path != NULL) {
    252                         usb_log_debug("Comparing device against usage path.\n");
     252                        usb_log_debug("Comparing device against usage path.");
    253253                        if (usb_hid_path_matches(hid_dev, mapping)) {
    254254                                /* Does not matter if IDs were matched. */
     
    258258
    259259                if (matched) {
    260                         usb_log_debug("Subdriver matched.\n");
     260                        usb_log_debug("Subdriver matched.");
    261261                        subdrivers[count++] = &mapping->subdriver;
    262262                }
     
    285285                    usb_device_get_mapped_ep_desc(dev, endpoints[i].desc);
    286286                if (epm && epm->present) {
    287                         usb_log_debug("Found: %s.\n", endpoints[i].description);
     287                        usb_log_debug("Found: %s.", endpoints[i].description);
    288288                        hid_dev->poll_pipe_mapping = epm;
    289289                        return EOK;
     
    301301
    302302        do {
    303                 usb_log_debug("Getting size of the report.\n");
     303                usb_log_debug("Getting size of the report.");
    304304                const size_t size =
    305305                    usb_hid_report_byte_size(&hid_dev->report, report_id,
    306306                        USB_HID_REPORT_TYPE_INPUT);
    307                 usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
     307                usb_log_debug("Report ID: %u, size: %zu", report_id, size);
    308308                max_size = (size > max_size) ? size : max_size;
    309                 usb_log_debug("Getting next report ID\n");
     309                usb_log_debug("Getting next report ID");
    310310                report_id = usb_hid_get_next_report_id(&hid_dev->report,
    311311                    report_id, USB_HID_REPORT_TYPE_INPUT);
    312312        } while (report_id != 0);
    313313
    314         usb_log_debug("Max size of input report: %zu\n", max_size);
     314        usb_log_debug("Max size of input report: %zu", max_size);
    315315
    316316        assert(hid_dev->input_report == NULL);
     
    323323
    324324        return EOK;
     325}
     326
     327static bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
     328    size_t buffer_size, void *arg)
     329{
     330        if (dev == NULL || arg == NULL || buffer == NULL) {
     331                usb_log_error("Missing arguments to polling callback.");
     332                return false;
     333        }
     334        usb_hid_dev_t *hid_dev = arg;
     335
     336        assert(hid_dev->input_report != NULL);
     337
     338        usb_log_debug("New data [%zu/%zu]: %s", buffer_size,
     339            hid_dev->max_input_report_size,
     340            usb_debug_str_buffer(buffer, buffer_size, 0));
     341
     342        if (hid_dev->max_input_report_size >= buffer_size) {
     343                /*! @todo This should probably be atomic. */
     344                memcpy(hid_dev->input_report, buffer, buffer_size);
     345                hid_dev->input_report_size = buffer_size;
     346                usb_hid_new_report(hid_dev);
     347        }
     348
     349        /* Parse the input report */
     350        const errno_t rc = usb_hid_parse_report(
     351            &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
     352        if (rc != EOK) {
     353                usb_log_warning("Failure in usb_hid_parse_report():"
     354                    "%s\n", str_error(rc));
     355        }
     356
     357        bool cont = false;
     358        /* Continue if at least one of the subdrivers want to continue */
     359        for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
     360                if (hid_dev->subdrivers[i].poll != NULL) {
     361                        cont = cont || hid_dev->subdrivers[i].poll(
     362                            hid_dev, hid_dev->subdrivers[i].data);
     363                }
     364        }
     365
     366        return cont;
     367}
     368
     369static bool usb_hid_polling_error_callback(usb_device_t *dev, errno_t err_code, void *arg)
     370{
     371        assert(dev);
     372        assert(arg);
     373        usb_hid_dev_t *hid_dev = arg;
     374
     375        usb_log_error("Device %s polling error: %s", usb_device_get_name(dev),
     376            str_error(err_code));
     377
     378        /* Continue polling until the device is about to be removed. */
     379        return hid_dev->running;
     380}
     381
     382static void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
     383{
     384        assert(dev);
     385        assert(arg);
     386
     387        usb_hid_dev_t *hid_dev = arg;
     388
     389        for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
     390                if (hid_dev->subdrivers[i].poll_end != NULL) {
     391                        hid_dev->subdrivers[i].poll_end(
     392                            hid_dev, hid_dev->subdrivers[i].data, reason);
     393                }
     394        }
     395
     396        hid_dev->running = false;
    325397}
    326398
     
    332404 * During initialization, the keyboard is switched into boot protocol, the idle
    333405 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
    334  * when a key is pressed or released. Finally, the LED lights are turned on 
     406 * when a key is pressed or released. Finally, the LED lights are turned on
    335407 * according to the default setup of lock keys.
    336408 *
    337  * @note By default, the keyboards is initialized with Num Lock turned on and 
     409 * @note By default, the keyboards is initialized with Num Lock turned on and
    338410 *       other locks turned off.
    339411 *
     
    347419        assert(dev);
    348420
    349         usb_log_debug("Initializing HID structure...\n");
     421        usb_log_debug("Initializing HID structure...");
    350422
    351423        usb_hid_report_init(&hid_dev->report);
     
    369441                usb_hid_find_subdrivers(hid_dev);
    370442        } else {
    371                 usb_log_error("Failed to parse report descriptor: fallback.\n");
     443                usb_log_error("Failed to parse report descriptor: fallback.");
    372444                hid_dev->subdrivers = NULL;
    373445                hid_dev->subdriver_count = 0;
    374446        }
    375447
    376         usb_log_debug("Subdriver count(before trying boot protocol): %d\n",
     448        usb_log_debug("Subdriver count(before trying boot protocol): %d",
    377449            hid_dev->subdriver_count);
    378450
     
    385457                switch (hid_dev->poll_pipe_mapping->interface->interface_protocol) {
    386458                case USB_HID_PROTOCOL_KEYBOARD:
    387                         usb_log_info("Falling back to kbd boot protocol.\n");
     459                        usb_log_info("Falling back to kbd boot protocol.");
    388460                        rc = usb_kbd_set_boot_protocol(hid_dev);
    389461                        if (rc == EOK) {
     
    392464                        break;
    393465                case USB_HID_PROTOCOL_MOUSE:
    394                         usb_log_info("Falling back to mouse boot protocol.\n");
     466                        usb_log_info("Falling back to mouse boot protocol.");
    395467                        rc = usb_mouse_set_boot_protocol(hid_dev);
    396468                        if (rc == EOK) {
     
    399471                        break;
    400472                default:
    401                         usb_log_info("Falling back to generic HID driver.\n");
     473                        usb_log_info("Falling back to generic HID driver.");
    402474                        usb_hid_set_generic_hid_subdriver(hid_dev);
    403475                }
    404476        }
    405477
    406         usb_log_debug("Subdriver count(after trying boot protocol): %d\n",
     478        usb_log_debug("Subdriver count(after trying boot protocol): %d",
    407479            hid_dev->subdriver_count);
    408480
     
    419491        for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
    420492                if (hid_dev->subdrivers[i].init != NULL) {
    421                         usb_log_debug("Initializing subdriver %d.\n",i);
     493                        usb_log_debug("Initializing subdriver %d.",i);
    422494                        const errno_t pret = hid_dev->subdrivers[i].init(hid_dev,
    423495                            &hid_dev->subdrivers[i].data);
     
    442514                rc = usb_hid_init_report(hid_dev);
    443515                if (rc != EOK) {
    444                         usb_log_error("Failed to initialize input report buffer"
    445                             ".\n");
    446                 }
     516                        usb_log_error("Failed to initialize input report buffer: %s", str_error(rc));
     517                        // FIXME: What happens now?
     518                }
     519
     520                usb_polling_t *polling = &hid_dev->polling;
     521                if ((rc = usb_polling_init(polling))) {
     522                        usb_log_error("Failed to initialize polling: %s", str_error(rc));
     523                        // FIXME: What happens now?
     524                }
     525
     526                polling->device = hid_dev->usb_dev;
     527                polling->ep_mapping = hid_dev->poll_pipe_mapping;
     528                polling->request_size = hid_dev->poll_pipe_mapping->pipe.desc.max_transfer_size;
     529                polling->buffer = malloc(polling->request_size);
     530                polling->on_data = usb_hid_polling_callback;
     531                polling->on_polling_end = usb_hid_polling_ended_callback;
     532                polling->on_error = usb_hid_polling_error_callback;
     533                polling->arg = hid_dev;
    447534        }
    448535
    449536        return rc;
    450 }
    451 
    452 bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
    453     size_t buffer_size, void *arg)
    454 {
    455         if (dev == NULL || arg == NULL || buffer == NULL) {
    456                 usb_log_error("Missing arguments to polling callback.\n");
    457                 return false;
    458         }
    459         usb_hid_dev_t *hid_dev = arg;
    460 
    461         assert(hid_dev->input_report != NULL);
    462 
    463         usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
    464             hid_dev->max_input_report_size,
    465             usb_debug_str_buffer(buffer, buffer_size, 0));
    466 
    467         if (hid_dev->max_input_report_size >= buffer_size) {
    468                 /*! @todo This should probably be atomic. */
    469                 memcpy(hid_dev->input_report, buffer, buffer_size);
    470                 hid_dev->input_report_size = buffer_size;
    471                 usb_hid_new_report(hid_dev);
    472         }
    473 
    474         /* Parse the input report */
    475         const errno_t rc = usb_hid_parse_report(
    476             &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
    477         if (rc != EOK) {
    478                 usb_log_warning("Failure in usb_hid_parse_report():"
    479                     "%s\n", str_error(rc));
    480         }
    481 
    482         bool cont = false;
    483         /* Continue if at least one of the subdrivers want to continue */
    484         for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
    485                 if (hid_dev->subdrivers[i].poll != NULL) {
    486                         cont = cont || hid_dev->subdrivers[i].poll(
    487                             hid_dev, hid_dev->subdrivers[i].data);
    488                 }
    489         }
    490 
    491         return cont;
    492 }
    493 
    494 void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
    495 {
    496         assert(dev);
    497         assert(arg);
    498 
    499         usb_hid_dev_t *hid_dev = arg;
    500 
    501         for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
    502                 if (hid_dev->subdrivers[i].poll_end != NULL) {
    503                         hid_dev->subdrivers[i].poll_end(
    504                             hid_dev, hid_dev->subdrivers[i].data, reason);
    505                 }
    506         }
    507 
    508         hid_dev->running = false;
    509537}
    510538
     
    524552        assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
    525553
    526 
    527         usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
     554        free(hid_dev->polling.buffer);
     555        usb_polling_fini(&hid_dev->polling);
     556
     557        usb_log_debug("Subdrivers: %p, subdriver count: %d",
    528558            hid_dev->subdrivers, hid_dev->subdriver_count);
    529559
     
    541571        /* Destroy the parser */
    542572        usb_hid_report_deinit(&hid_dev->report);
    543 
    544573}
    545574
Note: See TracChangeset for help on using the changeset viewer.