Changeset 3f8f09f in mainline


Ignore:
Timestamp:
2011-10-15T15:34:03Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
93d2684
Parents:
b803845
Message:

usbhid: Refactoring.

My keyboard still works…

Location:
uspace/drv/bus/usb/usbhid
Files:
4 edited

Legend:

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

    rb803845 r3f8f09f  
    4242#include "generic/hiddev.h"
    4343
    44 static usb_hid_subdriver_usage_t path_kbd[] = {
    45         {USB_HIDUT_PAGE_GENERIC_DESKTOP, 
    46          USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD}, 
     44static const usb_hid_subdriver_usage_t path_kbd[] = {
     45        {USB_HIDUT_PAGE_GENERIC_DESKTOP,
     46         USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD},
    4747        {0, 0}
    4848};
    4949
    50 static usb_hid_subdriver_usage_t path_mouse[] = {
     50static const usb_hid_subdriver_usage_t path_mouse[] = {
    5151        {USB_HIDUT_PAGE_GENERIC_DESKTOP, USB_HIDUT_USAGE_GENERIC_DESKTOP_MOUSE},
    5252        {0, 0}
    5353};
    5454
    55 static usb_hid_subdriver_usage_t multim_key_path[] = {
     55static const usb_hid_subdriver_usage_t multim_key_path[] = {
    5656        {USB_HIDUT_PAGE_CONSUMER, USB_HIDUT_USAGE_CONSUMER_CONSUMER_CONTROL},
    5757        {0, 0}
     
    7171                        .poll_end = NULL
    7272                },
    73                
    7473        },
    7574        {
  • uspace/drv/bus/usb/usbhid/subdrivers.h

    rb803845 r3f8f09f  
    7878
    7979        /** Subdriver for controlling this device. */
    80         usb_hid_subdriver_t subdriver;
     80        const usb_hid_subdriver_t subdriver;
    8181} usb_hid_subdriver_mapping_t;
    8282
  • uspace/drv/bus/usb/usbhid/usbhid.c

    rb803845 r3f8f09f  
    6767static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
    6868{
    69         assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    70 
    71         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    72             sizeof(usb_hid_subdriver_t));
     69        assert(hid_dev != NULL);
     70        assert(hid_dev->subdriver_count == 0);
     71
     72        hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
    7373        if (hid_dev->subdrivers == NULL) {
    7474                return ENOMEM;
    7575        }
    76 
    77         assert(hid_dev->subdriver_count >= 0);
    78 
    79         // set the init callback
    80         hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_kbd_init;
    81 
    82         // set the polling callback
    83         hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    84             usb_kbd_polling_callback;
    85 
    86         // set the polling ended callback
    87         hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    88 
    89         // set the deinit callback
    90         hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_kbd_deinit;
    91 
    92         // set subdriver count
    93         ++hid_dev->subdriver_count;
     76        hid_dev->subdriver_count = 1;
     77        // TODO 0 should be keyboard, but find a better way
     78        hid_dev->subdrivers[0] = usb_hid_subdrivers[0].subdriver;
    9479
    9580        return EOK;
     
    10085static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
    10186{
    102         assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    103 
    104         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    105             sizeof(usb_hid_subdriver_t));
     87        assert(hid_dev != NULL);
     88        assert(hid_dev->subdriver_count == 0);
     89
     90        hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
    10691        if (hid_dev->subdrivers == NULL) {
    10792                return ENOMEM;
    10893        }
    109 
    110         assert(hid_dev->subdriver_count >= 0);
    111 
    112         // set the init callback
    113         hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_mouse_init;
    114 
    115         // set the polling callback
    116         hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    117             usb_mouse_polling_callback;
    118 
    119         // set the polling ended callback
    120         hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    121 
    122         // set the deinit callback
    123         hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_mouse_deinit;
    124 
    125         // set subdriver count
    126         ++hid_dev->subdriver_count;
     94        hid_dev->subdriver_count = 1;
     95        // TODO 2 should be mouse, but find a better way
     96        hid_dev->subdrivers[2] = usb_hid_subdrivers[0].subdriver;
    12797
    12898        return EOK;
     
    135105        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    136106
    137         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    138             sizeof(usb_hid_subdriver_t));
     107        hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
    139108        if (hid_dev->subdrivers == NULL) {
    140109                return ENOMEM;
    141110        }
    142 
    143         assert(hid_dev->subdriver_count >= 0);
    144 
    145         // set the init callback
    146         hid_dev->subdrivers[hid_dev->subdriver_count].init =
    147             usb_generic_hid_init;
    148 
    149         // set the polling callback
    150         hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    151             usb_generic_hid_polling_callback;
    152 
    153         // set the polling ended callback
    154         hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    155 
    156         // set the deinit callback
    157         hid_dev->subdrivers[hid_dev->subdriver_count].deinit =
    158             usb_generic_hid_deinit;
    159 
    160         // set subdriver count
    161         ++hid_dev->subdriver_count;
     111        hid_dev->subdriver_count = 1;
     112
     113        /* Set generic hid subdriver routines */
     114        hid_dev->subdrivers[0].init = usb_generic_hid_init;
     115        hid_dev->subdrivers[0].poll = usb_generic_hid_polling_callback;
     116        hid_dev->subdrivers[0].poll_end = NULL;
     117        hid_dev->subdrivers[0].deinit = usb_generic_hid_deinit;
    162118
    163119        return EOK;
     
    166122/*----------------------------------------------------------------------------*/
    167123
    168 static bool usb_hid_ids_match(usb_hid_dev_t *hid_dev,
     124static bool usb_hid_ids_match(const usb_hid_dev_t *hid_dev,
    169125    const usb_hid_subdriver_mapping_t *mapping)
    170126{
     
    172128        assert(hid_dev->usb_dev != NULL);
    173129
    174         return (hid_dev->usb_dev->descriptors.device.vendor_id 
     130        return (hid_dev->usb_dev->descriptors.device.vendor_id
    175131            == mapping->vendor_id
    176132            && hid_dev->usb_dev->descriptors.device.product_id
     
    180136/*----------------------------------------------------------------------------*/
    181137
    182 static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev, 
     138static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
    183139    const usb_hid_subdriver_mapping_t *mapping)
    184140{
     
    192148        }
    193149        int i = 0;
    194         while (mapping->usage_path[i].usage != 0 
     150        while (mapping->usage_path[i].usage != 0
    195151            || mapping->usage_path[i].usage_page != 0) {
    196                 if (usb_hid_report_path_append_item(usage_path, 
    197                     mapping->usage_path[i].usage_page, 
     152                if (usb_hid_report_path_append_item(usage_path,
     153                    mapping->usage_path[i].usage_page,
    198154                    mapping->usage_path[i].usage) != EOK) {
    199155                        usb_log_debug("Failed to append to usage path.\n");
     
    239195/*----------------------------------------------------------------------------*/
    240196
    241 static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev, 
     197static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
    242198    const usb_hid_subdriver_t **subdrivers, int count)
    243199{
     
    250206        }
    251207
    252         // add one generic HID subdriver per device
    253 
    254         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
    255             sizeof(usb_hid_subdriver_t));
     208        /* +1 for generic hid subdriver */
     209        hid_dev->subdrivers = calloc((count + 1), sizeof(usb_hid_subdriver_t));
    256210        if (hid_dev->subdrivers == NULL) {
    257211                return ENOMEM;
     
    265219        }
    266220
     221        /* Add one generic HID subdriver per device */
    267222        hid_dev->subdrivers[count].init = usb_generic_hid_init;
    268223        hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
     
    303258                        return EINVAL;
    304259                }
    305                
     260
    306261                ids_matched = false;
    307262                matched = false;
    308                
     263
    309264                if (mapping->vendor_id >= 0) {
    310265                        assert(mapping->product_id >= 0);
     
    317272                        }
    318273                }
    319                
     274
    320275                if (mapping->usage_path != NULL) {
    321276                        usb_log_debug("Comparing device against usage path.\n");
     
    328283                        matched = ids_matched;
    329284                }
    330                
     285
    331286                if (matched) {
    332287                        usb_log_debug("Subdriver matched.\n");
    333288                        subdrivers[count++] = &mapping->subdriver;
    334289                }
    335                
     290
    336291                mapping = &usb_hid_subdrivers[++i];
    337292        }
    338293
    339         // we have all subdrivers determined, save them into the hid device
     294        /* We have all subdrivers determined, save them into the hid device */
     295        // TODO Dowe really need this complicated stuff if there is
     296        // max_subdrivers limitation?
    340297        return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
    341298}
     
    343300/*----------------------------------------------------------------------------*/
    344301
    345 static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
    346 {
    347         assert(hid_dev != NULL && dev != NULL);
    348 
    349         int rc = EOK;
     302static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, const usb_device_t *dev)
     303{
     304        assert(hid_dev);
     305        assert(dev);
    350306
    351307        if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
     
    364320                usb_log_error("None of supported endpoints found - probably"
    365321                    " not a supported device.\n");
    366                 rc = ENOTSUP;
    367         }
    368 
    369         return rc;
     322                return ENOTSUP;
     323        }
     324
     325        return EOK;
    370326}
    371327
     
    393349        usb_log_debug("Max size of input report: %zu\n", max_size);
    394350
    395         hid_dev->max_input_report_size = max_size;
    396351        assert(hid_dev->input_report == NULL);
    397352
    398         hid_dev->input_report = malloc(max_size);
     353        hid_dev->input_report = calloc(1, max_size);
    399354        if (hid_dev->input_report == NULL) {
    400355                return ENOMEM;
    401356        }
    402         memset(hid_dev->input_report, 0, max_size);
     357        hid_dev->max_input_report_size = max_size;
    403358
    404359        return EOK;
     
    477432                        break;
    478433                default:
    479                         assert(hid_dev->poll_pipe_index 
     434                        assert(hid_dev->poll_pipe_index
    480435                            == USB_HID_GENERIC_POLL_EP_NO);
    481                        
     436
    482437                        usb_log_info("Falling back to generic HID driver.\n");
    483438                        rc = usb_hid_set_generic_hid_subdriver(hid_dev);
     
    488443                usb_log_error("No subdriver for handling this device could be"
    489444                    " initialized: %s.\n", str_error(rc));
    490                 usb_log_debug("Subdriver count: %d\n", 
     445                usb_log_debug("Subdriver count: %d\n",
    491446                    hid_dev->subdriver_count);
    492                
    493447        } else {
    494448                bool ok = false;
    495                
    496                 usb_log_debug("Subdriver count: %d\n", 
     449
     450                usb_log_debug("Subdriver count: %d\n",
    497451                    hid_dev->subdriver_count);
    498                
     452
    499453                for (i = 0; i < hid_dev->subdriver_count; ++i) {
    500454                        if (hid_dev->subdrivers[i].init != NULL) {
     
    527481        }
    528482
    529 
    530483        return rc;
    531484}
     
    533486/*----------------------------------------------------------------------------*/
    534487
    535 bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer, 
     488bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
    536489    size_t buffer_size, void *arg)
    537490{
    538         int i;
    539 
    540491        if (dev == NULL || arg == NULL || buffer == NULL) {
    541492                usb_log_error("Missing arguments to polling callback.\n");
    542493                return false;
    543494        }
    544 
    545         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
     495        usb_hid_dev_t *hid_dev = arg;
    546496
    547497        assert(hid_dev->input_report != NULL);
     498
    548499        usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
    549500            hid_dev->max_input_report_size,
     
    557508        }
    558509
    559         // parse the input report
    560 
    561         int rc = usb_hid_parse_report(&hid_dev->report, buffer, buffer_size,
    562             &hid_dev->report_id);
    563 
     510        /* Parse the input report */
     511        const int rc = usb_hid_parse_report(
     512            &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
    564513        if (rc != EOK) {
    565514                usb_log_warning("Error in usb_hid_parse_report():"
     
    568517
    569518        bool cont = false;
    570 
    571         // continue if at least one of the subdrivers want to continue
    572         for (i = 0; i < hid_dev->subdriver_count; ++i) {
    573                 if (hid_dev->subdrivers[i].poll != NULL
    574                     && hid_dev->subdrivers[i].poll(hid_dev,
    575                         hid_dev->subdrivers[i].data)) {
    576                         cont = true;
     519        /* Continue if at least one of the subdrivers want to continue */
     520        for (int i = 0; i < hid_dev->subdriver_count; ++i) {
     521                if (hid_dev->subdrivers[i].poll != NULL) {
     522                        cont = cont || hid_dev->subdrivers[i].poll(
     523                            hid_dev, hid_dev->subdrivers[i].data);
    577524                }
    578525        }
     
    583530/*----------------------------------------------------------------------------*/
    584531
    585 void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
    586      void *arg)
    587 {
    588         int i;
    589 
    590         if (dev == NULL || arg == NULL) {
    591                 return;
    592         }
    593 
    594         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    595 
    596         for (i = 0; i < hid_dev->subdriver_count; ++i) {
     532void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
     533{
     534        assert(dev);
     535        assert(arg);
     536
     537        usb_hid_dev_t *hid_dev = arg;
     538
     539        for (int i = 0; i < hid_dev->subdriver_count; ++i) {
    597540                if (hid_dev->subdrivers[i].poll_end != NULL) {
    598                         hid_dev->subdrivers[i].poll_end(hid_dev,
    599                             hid_dev->subdrivers[i].data, reason);
     541                        hid_dev->subdrivers[i].poll_end(
     542                            hid_dev, hid_dev->subdrivers[i].data, reason);
    600543                }
    601544        }
     
    613556/*----------------------------------------------------------------------------*/
    614557
    615 int usb_hid_report_number(usb_hid_dev_t *hid_dev)
     558int usb_hid_report_number(const usb_hid_dev_t *hid_dev)
    616559{
    617560        return hid_dev->report_nr;
     
    622565void usb_hid_deinit(usb_hid_dev_t *hid_dev)
    623566{
    624         int i;
    625 
    626         if (hid_dev == NULL) {
    627                 return;
    628         }
     567        assert(hid_dev);
     568        assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
     569
    629570
    630571        usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
    631572            hid_dev->subdrivers, hid_dev->subdriver_count);
    632573
    633         assert(hid_dev->subdrivers != NULL
    634             || hid_dev->subdriver_count == 0);
    635 
    636         for (i = 0; i < hid_dev->subdriver_count; ++i) {
     574        for (int i = 0; i < hid_dev->subdriver_count; ++i) {
    637575                if (hid_dev->subdrivers[i].deinit != NULL) {
    638576                        hid_dev->subdrivers[i].deinit(hid_dev,
  • uspace/drv/bus/usb/usbhid/usbhid.h

    rb803845 r3f8f09f  
    146146
    147147int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev);
     148
    148149void usb_hid_deinit(usb_hid_dev_t *hid_dev);
    149150
     
    155156void usb_hid_new_report(usb_hid_dev_t *hid_dev);
    156157
    157 int usb_hid_report_number(usb_hid_dev_t *hid_dev);
     158int usb_hid_report_number(const usb_hid_dev_t *hid_dev);
    158159
    159160#endif /* USB_HID_USBHID_H_ */
Note: See TracChangeset for help on using the changeset viewer.