Changeset 8d3f198 in mainline for uspace/lib/usbhid/src


Ignore:
Timestamp:
2011-05-26T10:06:24Z (15 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c0940a85
Parents:
fa8d346 (diff), 5f9b81af (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from maklf (getting report IDs in report + report size in bytes)

Location:
uspace/lib/usbhid/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhid/src/hidparser.c

    rfa8d346 r8d3f198  
    3131 */
    3232/** @file
    33  * HID report descriptor and report data parser implementation.
     33 * USB HID report data parser implementation.
    3434 */
    3535#include <usb/hid/hidparser.h>
     
    4141#include <assert.h>
    4242
    43 
     43/*---------------------------------------------------------------------------*/
    4444/*
    4545 * Data translation private functions
    4646 */
    4747uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
    48 //inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
     48
    4949int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
    50 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
     50
     51uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
     52        int32_t value);
     53
    5154int usb_pow(int a, int b);
    5255
     56/*---------------------------------------------------------------------------*/
    5357
    5458// TODO: tohle ma bejt asi jinde
     
    5660{
    5761        switch(b) {
    58                 case 0:
    59                         return 1;
    60                         break;
    61                 case 1:
    62                         return a;
    63                         break;
    64                 default:
    65                         return a * usb_pow(a, b-1);
    66                         break;
    67         }
    68 }
    69 
     62        case 0:
     63                return 1;
     64                break;
     65        case 1:
     66                return a;
     67                break;
     68        default:
     69                return a * usb_pow(a, b-1);
     70                break;
     71        }
     72}
     73/*---------------------------------------------------------------------------*/
    7074
    7175/** Returns size of report of specified report id and type in items
     
    9498}
    9599
     100/** Returns size of report of specified report id and type in bytes
     101 *
     102 * @param parser Opaque report parser structure
     103 * @param report_id
     104 * @param type
     105 * @return Number of items in specified report
     106 */
     107size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id,
     108                           usb_hid_report_type_t type)
     109{
     110        usb_hid_report_description_t *report_des;
     111
     112        if(report == NULL) {
     113                return 0;
     114        }
     115
     116        report_des = usb_hid_report_find_description (report, report_id, type);
     117        if(report_des == NULL){
     118                return 0;
     119        }
     120        else {
     121                return (report_des->bit_length + 7) / 8;
     122        }
     123}
     124/*---------------------------------------------------------------------------*/
    96125
    97126/** Parse and act upon a HID report.
     
    103132 * @return Error code.
    104133 */
    105 int usb_hid_parse_report(const usb_hid_report_t *report,
    106     const uint8_t *data, size_t size, uint8_t *report_id)
     134int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
     135        size_t size, uint8_t *report_id)
    107136{
    108137        link_t *list_item;
     
    139168                                item->value = usb_hid_translate_data(item, data);
    140169               
    141                                 item->usage = USB_HID_EXTENDED_USAGE(item->usages[item->value - item->physical_minimum]);
    142                                 item->usage_page = USB_HID_EXTENDED_USAGE_PAGE(item->usages[item->value - item->physical_minimum]);                             
     170                                item->usage = USB_HID_EXTENDED_USAGE(
     171                                        item->usages[item->value - item->physical_minimum]);
     172                                item->usage_page = USB_HID_EXTENDED_USAGE_PAGE(
     173                                        item->usages[item->value - item->physical_minimum]);                           
    143174
    144175                                usb_hid_report_set_last_item (item->collection_path,
    145                                                               USB_HID_TAG_CLASS_GLOBAL,
    146                                                               item->usage_page);
     176                                        USB_HID_TAG_CLASS_GLOBAL, item->usage_page);
    147177                                usb_hid_report_set_last_item (item->collection_path,
    148                                                               USB_HID_TAG_CLASS_LOCAL,
    149                                                               item->usage);
     178                                    USB_HID_TAG_CLASS_LOCAL, item->usage);
    150179                               
    151180                        }
     
    162191}
    163192
     193/*---------------------------------------------------------------------------*/
    164194/**
    165195 * Translate data from the report as specified in report descriptor item
     
    167197 * @param item Report descriptor item with definition of translation
    168198 * @param data Data to translate
    169  * @param j Index of processed field in report descriptor item
    170199 * @return Translated data
    171200 */
     
    240269}
    241270
    242 /*** OUTPUT API **/
     271/*---------------------------------------------------------------------------*/
     272/* OUTPUT API */
    243273
    244274/**
     
    250280 * @return Returns allocated output buffer for specified output
    251281 */
    252 uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
     282uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
     283        uint8_t report_id)
    253284{
    254285        if(report == NULL) {
     
    260291        usb_hid_report_description_t *report_des = NULL;
    261292        while(report_it != &report->reports) {
    262                 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
    263                 if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
     293                report_des = list_get_instance(report_it,
     294                        usb_hid_report_description_t, link);
     295               
     296                if((report_des->report_id == report_id) &&
     297                        (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
    264298                        break;
    265299                }
     
    303337 * @return Error code
    304338 */
    305 int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
    306                                     uint8_t *buffer, size_t size)
     339int usb_hid_report_output_translate(usb_hid_report_t *report,
     340        uint8_t report_id, uint8_t *buffer, size_t size)
    307341{
    308342        link_t *item;   
     
    320354        }
    321355
    322         usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
    323        
    324356        usb_hid_report_description_t *report_des;
    325         report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
     357        report_des = usb_hid_report_find_description (report, report_id,
     358                USB_HID_REPORT_TYPE_OUTPUT);
     359       
    326360        if(report_des == NULL){
    327361                return EINVAL;
     
    333367                report_item = list_get_instance(item, usb_hid_report_field_t, link);
    334368
    335                 usb_log_debug("OUTPUT ITEM usage(%x), value(%x)\n", report_item->usage, report_item->value);
    336                
    337369                if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
    338370                                       
    339371                        // array
    340                         value = usb_hid_translate_data_reverse(report_item, report_item->value);
     372                        value = usb_hid_translate_data_reverse(report_item,
     373                                report_item->value);
     374
    341375                        offset = report_item->offset;
    342376                        length = report_item->size;
     
    344378                else {
    345379                        // variable item
    346                         value  = usb_hid_translate_data_reverse(report_item, report_item->value);
     380                        value  = usb_hid_translate_data_reverse(report_item,
     381                                report_item->value);
     382
    347383                        offset = report_item->offset;
    348384                        length = report_item->size;
     
    353389                if((offset/8) == ((offset+length-1)/8)) {
    354390                        // je to v jednom bytu
    355                         if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
     391                        if(((size_t)(offset/8) >= size) ||
     392                                ((size_t)(offset+length-1)/8) >= size) {
    356393                                break; // TODO ErrorCode
    357394                        }
     
    379416                                       
    380417                                        value = value >> (length - ((offset + length) % 8));
    381                                         value = value & ((1 << (length - ((offset + length) % 8))) - 1);
     418                                        value = value &
     419                                                ((1 << (length - ((offset + length) % 8))) - 1);
    382420                               
    383421                                        mask = (1 << (length - ((offset + length) % 8))) - 1;
     
    396434        }
    397435       
    398         usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
    399 
    400436        return EOK;
    401437}
    402438
     439/*---------------------------------------------------------------------------*/
    403440/**
    404441 * Translate given data for putting them into the outoput report
     
    407444 * @return ranslated value
    408445 */
    409 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
     446uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
     447        int value)
    410448{
    411449        int ret=0;
     
    431469        }
    432470
    433         ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
    434         usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), ret(%x)\n", value, resolution, item->physical_minimum, item->logical_minimum, ret);
     471        ret = ((value - item->physical_minimum) * resolution) +
     472                item->logical_minimum;
     473
     474        usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), \
     475                ret(%x)\n", value, resolution, item->physical_minimum,
     476                item->logical_minimum, ret);
    435477       
    436478        if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
     
    440482}
    441483
    442 usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
     484/*---------------------------------------------------------------------------*/
     485/**
     486 * Clones given state table
     487 *
     488 * @param item State table to clone
     489 * @return Pointer to the cloned item
     490 */
     491usb_hid_report_item_t *usb_hid_report_item_clone(
     492        const usb_hid_report_item_t *item)
    443493{
    444494        usb_hid_report_item_t *new_report_item;
     
    453503}
    454504
    455 
     505/*---------------------------------------------------------------------------*/
     506/**
     507 * Function for sequence walking through the report. Returns next field in the
     508 * report or the first one when no field is given.
     509 *
     510 * @param report Searched report structure
     511 * @param field Current field. If NULL is given, the first one in the report
     512 * is returned. Otherwise the next one i nthe list is returned.
     513 * @param path Usage path specifying which fields wa are interested in.
     514 * @param flags Flags defining mode of usage paths comparison
     515 * @param type Type of report we search.
     516 * @retval NULL if no field is founded
     517 * @retval Pointer to the founded report structure when founded
     518 */
    456519usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
    457                                                         usb_hid_report_field_t *field,
    458                             usb_hid_report_path_t *path, int flags,
    459                             usb_hid_report_type_t type)
    460 {
    461         usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
     520        usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags,
     521        usb_hid_report_type_t type)
     522{
     523        usb_hid_report_description_t *report_des = usb_hid_report_find_description(
     524                report, path->report_id, type);
     525
    462526        link_t *field_it;
    463527       
     
    467531
    468532        if(field == NULL){
    469                 // vezmu prvni co mathuje podle path!!
    470533                field_it = report_des->report_items.next;
    471534        }
     
    478541
    479542                if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
    480                         usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
    481                         if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
    482                                 usb_hid_report_remove_last_item (field->collection_path);
     543                        usb_hid_report_path_append_item (field->collection_path,
     544                                field->usage_page, field->usage);
     545
     546                        if(usb_hid_report_compare_usage_path(field->collection_path, path,
     547                                flags) == EOK){
     548
     549                                usb_hid_report_remove_last_item(field->collection_path);
    483550                                return field;
    484551                        }
     
    491558}
    492559
    493 uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
     560/*---------------------------------------------------------------------------*/
     561/**
     562 * Returns next report_id of report of specified type. If zero is given than
     563 * first report_id of specified type is returned (0 is not legal value for
     564 * repotr_id)
     565 *
     566 * @param report_id Current report_id, 0 if there is no current report_id
     567 * @param type Type of searched report
     568 * @param report Report structure inwhich we search
     569 * @retval 0 if report structure is null or there is no specified report
     570 * @retval report_id otherwise
     571 */
     572uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report,
     573        uint8_t report_id, usb_hid_report_type_t type)
    494574{
    495575        if(report == NULL){
     
    500580        link_t *report_it;
    501581       
    502         if(report_id == 0) {
    503                 report_it = usb_hid_report_find_description (report, report_id, type)->link.next;               
     582        if(report_id > 0) {
     583                report_it = usb_hid_report_find_description(report, report_id,
     584                        type)->link.next;               
    504585        }
    505586        else {
     
    508589
    509590        while(report_it != &report->reports) {
    510                 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
     591                report_des = list_get_instance(report_it,
     592                        usb_hid_report_description_t, link);
     593
    511594                if(report_des->type == type){
    512595                        return report_des->report_id;
     
    517600}
    518601
     602/*---------------------------------------------------------------------------*/
     603/**
     604 * Reset all local items in given state table
     605 *
     606 * @param report_item State table containing current state of report
     607 * descriptor parsing
     608 *
     609 * @return void
     610 */
    519611void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
    520612{
  • uspace/lib/usbhid/src/hidpath.c

    rfa8d346 r8d3f198  
    4141#include <assert.h>
    4242
    43 
    44 #define USB_HID_SAME_USAGE(usage1, usage2)      ((usage1 == usage2) || (usage1 == 0) || (usage2 == 0))
    45 #define USB_HID_SAME_USAGE_PAGE(page1, page2)   ((page1 == page2) || (page1 == 0) || (page2 == 0))
    46 
     43/*---------------------------------------------------------------------------*/
     44/**
     45 * Compares two usages if they are same or not or one of the usages is not
     46 * set.
     47 *
     48 * @param usage1
     49 * @param usage2
     50 * @return boolean
     51 */
     52#define USB_HID_SAME_USAGE(usage1, usage2)              \
     53        ((usage1 == usage2) || (usage1 == 0) || (usage2 == 0))
     54
     55/**
     56 * Compares two usage pages if they are same or not or one of them is not set.
     57 *
     58 * @param page1
     59 * @param page2
     60 * @return boolean
     61 */
     62#define USB_HID_SAME_USAGE_PAGE(page1, page2)   \
     63        ((page1 == page2) || (page1 == 0) || (page2 == 0))
     64
     65/*---------------------------------------------------------------------------*/
    4766/**
    4867 * Appends one item (couple of usage_path and usage) into the usage path
     
    7392}
    7493
     94/*---------------------------------------------------------------------------*/
    7595/**
    7696 * Removes last item from the usage path structure
     
    91111}
    92112
     113/*---------------------------------------------------------------------------*/
    93114/**
    94115 * Nulls last item of the usage path structure.
     
    102123       
    103124        if(!list_empty(&usage_path->head)){     
    104                 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
     125                item = list_get_instance(usage_path->head.prev,
     126                        usb_hid_report_usage_path_t, link);
     127
    105128                memset(item, 0, sizeof(usb_hid_report_usage_path_t));
    106129        }
    107130}
    108131
     132/*---------------------------------------------------------------------------*/
    109133/**
    110134 * Modifies last item of usage path structure by given usage page or usage
     
    137161}
    138162
    139 
     163/*---------------------------------------------------------------------------*/
     164/**
     165 *
     166 *
     167 *
     168 *
     169 */
    140170void usb_hid_print_usage_path(usb_hid_report_path_t *path)
    141171{
     
    147177        while(item != &path->head) {
    148178
    149                 path_item = list_get_instance(item, usb_hid_report_usage_path_t, link);
     179                path_item = list_get_instance(item, usb_hid_report_usage_path_t,
     180                        link);
     181
    150182                usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
    151183                usb_log_debug("\tUSAGE: %X\n", path_item->usage);
    152184                usb_log_debug("\tFLAGS: %d\n", path_item->flags);               
    153185               
    154                 item = item->next;
    155         }
    156 }
    157 
     186        item = item->next;
     187        }
     188}
     189
     190/*---------------------------------------------------------------------------*/
    158191/**
    159192 * Compares two usage paths structures
     
    198231                        }
    199232
    200                         // projit skrz cestu a kdyz nekde sedi tak vratim EOK
    201                         // dojduli az za konec tak nnesedi
    202233                        report_link = report_path->head.next;
    203234                        path_link = path->head.next;
    204                         path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
     235                        path_item = list_get_instance(path_link,
     236                                usb_hid_report_usage_path_t, link);
    205237
    206238                        while(report_link != &report_path->head) {
    207                                 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
    208                                 if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page, path_item->usage_page)){
     239                                report_item = list_get_instance(report_link,
     240                                        usb_hid_report_usage_path_t, link);
     241                               
     242                                if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
     243                                        path_item->usage_page)){
     244                                       
    209245                                        if(only_page == 0){
    210                                                 if(USB_HID_SAME_USAGE(report_item->usage, path_item->usage)) {
     246                                                if(USB_HID_SAME_USAGE(report_item->usage,
     247                                                        path_item->usage)) {
     248                                                       
    211249                                                        return EOK;
    212250                                                }
     
    238276                                                 
    239277                                        report_item = list_get_instance(report_link,
    240                                                                         usb_hid_report_usage_path_t,
    241                                                                         link);
     278                        usb_hid_report_usage_path_t, link);
    242279                                                 
    243280                                        path_item = list_get_instance(path_link,
    244                                                                       usb_hid_report_usage_path_t,
    245                                                                       link);           
    246 
    247                                         if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page, path_item->usage_page) ||
    248                                            ((only_page == 0) &&
    249                                             !USB_HID_SAME_USAGE(report_item->usage, path_item->usage))) {
     281                        usb_hid_report_usage_path_t, link);             
     282
     283                                        if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
     284                                                path_item->usage_page) || ((only_page == 0) &&
     285                                            !USB_HID_SAME_USAGE(report_item->usage,
     286                                                path_item->usage))) {
    250287                                                       
    251288                                                   return 1;
     
    257294                                }
    258295
    259                                 if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) && (path_link == &path->head)) ||
    260                                    ((report_link == &report_path->head) && (path_link == &path->head))) {
     296                                if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) &&
     297                                        (path_link == &path->head)) ||
     298                                   ((report_link == &report_path->head) &&
     299                                        (path_link == &path->head))) {
     300                                       
    261301                                        return EOK;
    262302                                }
     
    280320                                                 
    281321                                        report_item = list_get_instance(report_link,
    282                                                                         usb_hid_report_usage_path_t,
    283                                                                         link);
     322                                                usb_hid_report_usage_path_t, link);
     323
    284324                                        path_item = list_get_instance(path_link,
    285                                                                       usb_hid_report_usage_path_t,
    286                                                                       link);           
     325                                                usb_hid_report_usage_path_t, link);             
    287326                                                 
    288                                         if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page, path_item->usage_page) ||
    289                                            ((only_page == 0) &&
    290                                             !USB_HID_SAME_USAGE(report_item->usage, path_item->usage))) {
     327                                        if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
     328                                                path_item->usage_page) || ((only_page == 0) &&
     329                                            !USB_HID_SAME_USAGE(report_item->usage,
     330                                                path_item->usage))) {
     331                                               
    291332                                                        return 1;
    292333                                        } else {
     
    311352}
    312353
     354/*---------------------------------------------------------------------------*/
    313355/**
    314356 * Allocates and initializes new usage path structure.
     
    332374}
    333375
     376/*---------------------------------------------------------------------------*/
    334377/**
    335378 * Releases given usage path structure.
     
    348391}
    349392
    350 
     393/*---------------------------------------------------------------------------*/
    351394/**
    352395 * Clone content of given usage path to the new one
     
    355398 * @return New copy of given usage path structure
    356399 */
    357 usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
     400usb_hid_report_path_t *usb_hid_report_path_clone(
     401        usb_hid_report_path_t *usage_path)
    358402{
    359403        link_t *path_link;
     
    374418        path_link = usage_path->head.next;
    375419        while(path_link != &usage_path->head) {
    376                 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, 
     420                path_item = list_get_instance(path_link, usb_hid_report_usage_path_t,
    377421                                              link);
    378422                new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
     
    395439}
    396440
    397 
     441/*---------------------------------------------------------------------------*/
    398442/**
    399443 * Sets report id in usage path structure
     
    403447 * @return Error code
    404448 */
    405 int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
     449int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
     450        uint8_t report_id)
    406451{
    407452        if(path == NULL){
Note: See TracChangeset for help on using the changeset viewer.