Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbhid/usbhid.c

    r5f6e25e r065064e6  
    5454
    5555/* Array of endpoints expected on the device, NULL terminated. */
    56 usb_endpoint_description_t *usb_hid_endpoints[USB_HID_POLL_EP_COUNT + 1] = {
     56usb_endpoint_description_t *usb_hid_endpoints[] = {
    5757        &usb_hid_kbd_poll_endpoint_description,
    5858        &usb_hid_mouse_poll_endpoint_description,
     
    6868{
    6969        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    70        
     70
    7171        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    7272            sizeof(usb_hid_subdriver_t));
     
    7474                return ENOMEM;
    7575        }
    76        
     76
    7777        assert(hid_dev->subdriver_count >= 0);
    78        
     78
    7979        // set the init callback
    8080        hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_kbd_init;
    81        
     81
    8282        // set the polling callback
    8383        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    8484            usb_kbd_polling_callback;
    85        
     85
    8686        // set the polling ended callback
    8787        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    88        
     88
    8989        // set the deinit callback
    9090        hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_kbd_deinit;
    91        
     91
    9292        // set subdriver count
    9393        ++hid_dev->subdriver_count;
    94        
     94
    9595        return EOK;
    9696}
     
    101101{
    102102        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    103        
     103
    104104        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    105105            sizeof(usb_hid_subdriver_t));
     
    107107                return ENOMEM;
    108108        }
    109        
     109
    110110        assert(hid_dev->subdriver_count >= 0);
    111        
     111
    112112        // set the init callback
    113113        hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_mouse_init;
    114        
     114
    115115        // set the polling callback
    116116        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    117117            usb_mouse_polling_callback;
    118        
     118
    119119        // set the polling ended callback
    120120        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    121        
     121
    122122        // set the deinit callback
    123123        hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_mouse_deinit;
    124        
     124
    125125        // set subdriver count
    126126        ++hid_dev->subdriver_count;
    127        
     127
    128128        return EOK;
    129129}
     
    134134{
    135135        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    136        
     136
    137137        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    138138            sizeof(usb_hid_subdriver_t));
     
    140140                return ENOMEM;
    141141        }
    142        
     142
    143143        assert(hid_dev->subdriver_count >= 0);
    144        
     144
    145145        // set the init callback
    146146        hid_dev->subdrivers[hid_dev->subdriver_count].init =
    147147            usb_generic_hid_init;
    148        
     148
    149149        // set the polling callback
    150150        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    151151            usb_generic_hid_polling_callback;
    152        
     152
    153153        // set the polling ended callback
    154154        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    155        
     155
    156156        // 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
    159160        // set subdriver count
    160161        ++hid_dev->subdriver_count;
    161        
     162
    162163        return EOK;
    163164}
     
    170171        assert(hid_dev != NULL);
    171172        assert(hid_dev->usb_dev != NULL);
    172        
     173
    173174        return (hid_dev->usb_dev->descriptors.device.vendor_id
    174175            == mapping->vendor_id
     
    184185        assert(hid_dev != NULL);
    185186        assert(mapping != NULL);
    186        
     187
    187188        usb_hid_report_path_t *usage_path = usb_hid_report_path();
    188189        if (usage_path == NULL) {
     
    202203                ++i;
    203204        }
    204        
     205
    205206        assert(hid_dev->report != NULL);
    206        
     207
    207208        usb_log_debug("Compare flags: %d\n", mapping->compare);
    208        
     209
    209210        bool matches = false;
    210211        uint8_t report_id = mapping->report_id;
     
    234235                    USB_HID_REPORT_TYPE_INPUT);
    235236        } while (!matches && report_id != 0);
    236        
     237
    237238        usb_hid_report_path_free(usage_path);
    238        
     239
    239240        return matches;
    240241}
     
    246247{
    247248        int i;
    248        
     249
    249250        if (count <= 0) {
    250251                hid_dev->subdriver_count = 0;
     
    252253                return EOK;
    253254        }
    254        
     255
    255256        // add one generic HID subdriver per device
    256        
     257
    257258        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
    258259            sizeof(usb_hid_subdriver_t));
     
    260261                return ENOMEM;
    261262        }
    262        
     263
    263264        for (i = 0; i < count; ++i) {
    264265                hid_dev->subdrivers[i].init = subdrivers[i]->init;
     
    267268                hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
    268269        }
    269        
     270
    270271        hid_dev->subdrivers[count].init = usb_generic_hid_init;
    271272        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;
    273274        hid_dev->subdrivers[count].poll_end = NULL;
    274        
     275
    275276        hid_dev->subdriver_count = count + 1;
    276        
     277
    277278        return EOK;
    278279}
     
    283284{
    284285        assert(hid_dev != NULL);
    285        
     286
    286287        const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
    287        
     288
    288289        int i = 0, count = 0;
    289290        const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
     
    291292        bool ids_matched;
    292293        bool matched;
    293        
     294
    294295        while (count < USB_HID_MAX_SUBDRIVERS &&
    295296            (mapping->usage_path != NULL
     
    339340                mapping = &usb_hid_subdrivers[++i];
    340341        }
    341        
     342
    342343        // we have all subdrivers determined, save them into the hid device
    343344        return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
     
    349350{
    350351        assert(hid_dev != NULL && dev != NULL);
    351        
     352
    352353        int rc = EOK;
    353        
     354
    354355        if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
    355356                usb_log_debug("Found keyboard endpoint.\n");
     
    369370                rc = ENOTSUP;
    370371        }
    371        
     372
    372373        return rc;
    373374}
     
    378379{
    379380        assert(hid_dev != NULL && hid_dev->report != NULL);
    380        
     381
    381382        uint8_t report_id = 0;
    382383        size_t size;
    383        
     384
    384385        size_t max_size = 0;
    385        
     386
    386387        do {
    387388                usb_log_debug("Getting size of the report.\n");
     
    394395                    report_id, USB_HID_REPORT_TYPE_INPUT);
    395396        } while (report_id != 0);
    396        
     397
    397398        usb_log_debug("Max size of input report: %zu\n", max_size);
    398        
     399
    399400        hid_dev->max_input_report_size = max_size;
    400401        assert(hid_dev->input_report == NULL);
    401        
     402
    402403        hid_dev->input_report = malloc(max_size);
    403404        if (hid_dev->input_report == NULL) {
     
    405406        }
    406407        memset(hid_dev->input_report, 0, max_size);
    407        
     408
    408409        return EOK;
    409410}
     
    411412/*----------------------------------------------------------------------------*/
    412413
    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 
    438414int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
    439415{
    440416        int rc, i;
    441        
     417
    442418        usb_log_debug("Initializing HID structure...\n");
    443        
     419
    444420        if (hid_dev == NULL) {
    445421                usb_log_error("Failed to init HID structure: no structure given"
     
    447423                return EINVAL;
    448424        }
    449        
     425
    450426        if (dev == NULL) {
    451427                usb_log_error("Failed to init HID structure: no USB device"
     
    453429                return EINVAL;
    454430        }
    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
    456440        /* The USB device should already be initialized, save it in structure */
    457441        hid_dev->usb_dev = dev;
    458        
     442        hid_dev->poll_pipe_index = -1;
     443
    459444        rc = usb_hid_check_pipes(hid_dev, dev);
    460445        if (rc != EOK) {
     
    465450        rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
    466451            hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
    467        
     452
    468453        bool fallback = false;
    469        
     454
    470455        if (rc == EOK) {
    471456                // try to find subdrivers that may want to handle this device
     
    484469                fallback = true;
    485470        }
    486        
     471
    487472        if (fallback) {
    488473                // fall back to boot protocol
     
    510495                }
    511496        }
    512        
     497
    513498        if (rc != EOK) {
    514499                usb_log_error("No subdriver for handling this device could be"
     
    542527                rc = (ok) ? EOK : -1;   // what error to report
    543528        }
    544        
    545        
     529
     530
    546531        if (rc == EOK) {
    547532                // save max input report size and allocate space for the report
     
    552537                }
    553538        }
    554        
    555        
     539
     540
    556541        return rc;
    557542}
     
    563548{
    564549        int i;
    565        
     550
    566551        if (dev == NULL || arg == NULL || buffer == NULL) {
    567552                usb_log_error("Missing arguments to polling callback.\n");
    568553                return false;
    569554        }
    570        
     555
    571556        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    572        
     557
    573558        assert(hid_dev->input_report != NULL);
    574559        usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
     
    582567                usb_hid_new_report(hid_dev);
    583568        }
    584        
     569
    585570        // parse the input report
    586        
     571
    587572        int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
    588573            &hid_dev->report_id);
    589        
     574
    590575        if (rc != EOK) {
    591576                usb_log_warning("Error in usb_hid_parse_report():"
    592577                    "%s\n", str_error(rc));
    593578        }       
    594        
     579
    595580        bool cont = false;
    596        
     581
    597582        // continue if at least one of the subdrivers want to continue
    598583        for (i = 0; i < hid_dev->subdriver_count; ++i) {
     
    603588                }
    604589        }
    605        
     590
    606591        return cont;
    607592}
     
    613598{
    614599        int i;
    615        
     600
    616601        if (dev == NULL || arg == NULL) {
    617602                return;
    618603        }
    619        
     604
    620605        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    621        
     606
    622607        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    623608                if (hid_dev->subdrivers[i].poll_end != NULL) {
     
    626611                }
    627612        }
    628        
    629         usb_hid_destroy(hid_dev);
     613
     614        hid_dev->running = false;
    630615}
    631616
     
    649634{
    650635        int i;
    651        
     636
    652637        if (hid_dev == NULL) {
    653638                return;
    654639        }
    655        
     640
    656641        usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
    657642            hid_dev->subdrivers, hid_dev->subdriver_count);
    658        
     643
    659644        assert(hid_dev->subdrivers != NULL
    660645            || hid_dev->subdriver_count == 0);
    661        
     646
    662647        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    663648                if (hid_dev->subdrivers[i].deinit != NULL) {
     
    666651                }
    667652        }
    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 */
    675659        if (hid_dev->report != NULL) {
    676660                usb_hid_free_report(hid_dev->report);
    677661        }
    678662
    679         if (hid_dev->report_desc != NULL) {
    680                 free(hid_dev->report_desc);
    681         }
    682663}
    683664
Note: See TracChangeset for help on using the changeset viewer.