Changes in / [a8ac368:fc883bb] in mainline


Ignore:
Location:
uspace/lib/usb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/include/usb/classes/hidparser.h

    ra8ac368 rfc883bb  
    3131 */
    3232/** @file
    33  * @brief USB HID parser.
     33 * USB HID report descriptor and report data parser
    3434 */
    3535#ifndef LIBUSB_HIDPARSER_H_
     
    7474#define USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY    4
    7575
     76/** */
    7677typedef struct {
     78        /** */
    7779        int32_t usage_page;
     80        /** */ 
    7881        int32_t usage;
    79 
     82        /** */
    8083        link_t link;
    8184} usb_hid_report_usage_path_t;
    8285
     86/** */
    8387typedef struct {
     88        /** */ 
    8489        int depth;     
     90       
     91        /** */ 
    8592        link_t link;
    8693} usb_hid_report_path_t;
     
    9097 */
    9198typedef struct {
     99        /** */ 
    92100        int32_t id;
     101        /** */ 
    93102        int32_t usage_minimum;
     103        /** */ 
    94104        int32_t usage_maximum;
     105        /** */ 
    95106        int32_t logical_minimum;
     107        /** */ 
    96108        int32_t logical_maximum;
     109        /** */ 
    97110        int32_t size;
     111        /** */ 
    98112        int32_t count;
     113        /** */ 
    99114        size_t offset;
     115        /** */ 
    100116        int32_t delimiter;
    101 
     117        /** */ 
    102118        int32_t unit_exponent;
     119        /** */ 
    103120        int32_t unit;
    104121
    105         /*
    106          * some not yet used fields
    107          */
     122        /** */
    108123        int32_t string_index;
     124        /** */ 
    109125        int32_t string_minimum;
     126        /** */ 
    110127        int32_t string_maximum;
     128        /** */ 
    111129        int32_t designator_index;
     130        /** */ 
    112131        int32_t designator_minimum;
     132        /** */ 
    113133        int32_t designator_maximum;
     134        /** */ 
    114135        int32_t physical_minimum;
     136        /** */ 
    115137        int32_t physical_maximum;
    116138
     139        /** */ 
    117140        uint8_t item_flags;
    118141
     142        /** */ 
    119143        usb_hid_report_path_t *usage_path;
     144        /** */ 
    120145        link_t link;
    121146} usb_hid_report_item_t;
     
    124149/** HID report parser structure. */
    125150typedef struct {       
     151        /** */ 
    126152        link_t input;
     153        /** */ 
    127154        link_t output;
     155        /** */ 
    128156        link_t feature;
    129157} usb_hid_report_parser_t;     
     
    190218
    191219/*
    192  * modifiers definitions
    193  */
    194 
     220 * Descriptor parser functions
     221 */
     222/** */
     223int usb_hid_parser_init(usb_hid_report_parser_t *parser);
     224
     225/** */
     226int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
     227    const uint8_t *data, size_t size);
     228
     229/** */
     230void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
     231
     232/** */
     233void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
     234
     235/*
     236 * Boot protocol functions
     237 */
     238/** */
    195239int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
    196240        const usb_hid_report_in_callbacks_t *callbacks, void *arg);
    197241
     242/** */
    198243int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
    199244
    200 int usb_hid_parser_init(usb_hid_report_parser_t *parser);
    201 int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
    202     const uint8_t *data, size_t size);
    203 
     245
     246/*
     247 * Input report parser functions
     248 */
     249/** */
    204250int usb_hid_parse_report(const usb_hid_report_parser_t *parser, 
    205251    const uint8_t *data, size_t size,
     
    207253    const usb_hid_report_in_callbacks_t *callbacks, void *arg);
    208254
    209 int usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
     255/** */
     256size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
    210257        usb_hid_report_path_t *path, int flags);
    211258
    212259
    213 void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
    214 
    215 void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
    216 
    217 /* usage path functions */
     260
     261/*
     262 * usage path functions
     263 */
     264/** */
    218265usb_hid_report_path_t *usb_hid_report_path(void);
     266
     267/** */
    219268void usb_hid_report_path_free(usb_hid_report_path_t *path);
     269
     270/** */
    220271int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, int32_t usage_page, int32_t usage);
     272
     273/** */
    221274void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
     275
     276/** */
    222277void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
     278
     279/** */
    223280void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data);
     281
     282/** */
    224283int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, usb_hid_report_path_t *path, int flags);
    225 int     usb_hid_report_path_clone(usb_hid_report_path_t *new_usage_path, usb_hid_report_path_t *usage_path);
    226 
    227 
    228 // output
    229 //      - funkce co vrati cesty poli v output reportu
    230 //      - funkce co pro danou cestu nastavi data
    231 //      - finalize
    232 
     284
     285/** */
     286usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
     287
     288
     289/*
     290 * Output report parser functions
     291 */
     292/** Allocates output report buffer*/
     293uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size);
     294
     295/** Frees output report buffer*/
     296void usb_hid_report_output_free(uint8_t *output);
     297
     298/** Returns size of output for given usage path */
     299size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
     300                                  usb_hid_report_path_t *path, int flags);
     301
     302/** Updates the output report buffer by translated given data */
     303int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
     304                                    usb_hid_report_path_t *path, int flags,
     305                                    uint8_t *buffer, size_t size,
     306                                    int32_t *data, size_t data_size);
    233307#endif
    234308/**
  • uspace/lib/usb/src/hidparser.c

    ra8ac368 rfc883bb  
    3131 */
    3232/** @file
    33  * @brief HID parser implementation.
     33 * HID report descriptor and report data parser implementation.
    3434 */
    3535#include <usb/classes/hidparser.h>
     
    4040#include <usb/debug.h>
    4141
     42/** */
    4243#define USB_HID_NEW_REPORT_ITEM 1
     44
     45/** */
    4346#define USB_HID_NO_ACTION               2
     47
     48/** */
    4449#define USB_HID_UNKNOWN_TAG             -99
    4550
    46 #define BAD_HACK_USAGE_PAGE             0x07
    47 
     51/*
     52 * Private descriptor parser functions
     53 */
    4854int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
    4955                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
     
    5864int usb_hid_report_reset_local_items();
    5965void usb_hid_free_report_list(link_t *head);
     66
     67/*
     68 * Data translation private functions
     69 */
    6070int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
    6171inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
    6272int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
     73int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);
    6374int usb_pow(int a, int b);
    6475
    65 
     76// TODO: tohle ma bejt asi jinde
    6677int usb_pow(int a, int b)
    6778{
     
    8091
    8192/**
    82  *
     93 * Initialize the report descriptor parser structure
     94 *
     95 * @param parser Report descriptor parser structure
     96 * @return Error code
    8397 */
    8498int usb_hid_parser_init(usb_hid_report_parser_t *parser)
    8599{
    86    if(parser == NULL) {
    87         return EINVAL;
    88    }
    89 
    90     list_initialize(&(parser->input));
     100        if(parser == NULL) {
     101                return EINVAL;
     102        }
     103
     104        list_initialize(&(parser->input));
    91105    list_initialize(&(parser->output));
    92106    list_initialize(&(parser->feature));
     
    164178                                        // store current usage path
    165179                                        report_item->usage_path = usage_path;
    166 
    167                                         // new current usage path
    168                                         tmp_usage_path = usb_hid_report_path();
    169180                                       
    170                                         // copy old path to the new one
    171                                         usb_hid_report_path_clone(tmp_usage_path, usage_path);
     181                                        // clone path to the new one
     182                                        tmp_usage_path = usb_hid_report_path_clone(usage_path);
    172183
    173184                                        // swap
     
    304315
    305316/**
     317 * Parse one tag of the report descriptor
    306318 *
    307319 * @param Tag to parse
     
    391403 * @return Error code
    392404 */
    393 
    394405int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    395406                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
     
    520531 * Prints content of given list of report items.
    521532 *
    522  * @param List of report items
     533 * @param List of report items (usb_hid_report_item_t)
    523534 * @return void
    524535 */
     
    552563                        path = path->next;
    553564                }
    554                
    555                
    556 //              usb_log_debug("\tUSAGE: %X\n", report_item->usage);
    557 //              usb_log_debug("\tUSAGE PAGE: %X\n", report_item->usage_page);
     565                               
    558566                usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
    559567                usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);         
     
    570578}
    571579/**
    572  * Prints content of given descriptor in human readable format.
    573  *
    574  * @param Parsed descriptor to print
     580 * Prints content of given report descriptor in human readable format.
     581 *
     582 * @param parser Parsed descriptor to print
    575583 * @return void
    576584 */
     
    595603 * Releases whole linked list of report items
    596604 *
    597  *
     605 * @param head Head of list of report descriptor items (usb_hid_report_item_t)
     606 * @return void
    598607 */
    599608void usb_hid_free_report_list(link_t *head)
     
    627636}
    628637
    629 /** Free the HID report parser structure
     638/** Frees the HID report descriptor parser structure
    630639 *
    631640 * @param parser Opaque HID report parser structure
    632  * @return Error code
     641 * @return void
    633642 */
    634643void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
     
    660669    const usb_hid_report_in_callbacks_t *callbacks, void *arg)
    661670{
    662         /*
    663          *
    664          * only key codes (usage page 0x07) will be processed
    665          * other usages will be ignored
    666          */
    667671        link_t *list_item;
    668672        usb_hid_report_item_t *item;
     
    676680                return EINVAL;
    677681        }
    678 
    679        
    680         // get the size of result keycodes array
     682       
     683        /* get the size of result array */
    681684        key_count = usb_hid_report_input_length(parser, path, flags);
    682685
     
    685688        }
    686689
    687         // read data           
     690        /* read data */
    688691        list_item = parser->input.next;   
    689692        while(list_item != &(parser->input)) {
     
    719722}
    720723
    721 
     724/**
     725 * Translate data from the report as specified in report descriptor
     726 *
     727 * @param item Report descriptor item with definition of translation
     728 * @param data Data to translate
     729 * @param j Index of processed field in report descriptor item
     730 * @return Translated data
     731 */
    722732int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
    723733{
     
    796806}
    797807
    798 int usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
     808/**
     809 *
     810 *
     811 * @param parser
     812 * @param path
     813 * @param flags
     814 * @return
     815 */
     816size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
    799817        usb_hid_report_path_t *path, int flags)
    800818{       
    801         int ret = 0;
     819        size_t ret = 0;
    802820        link_t *item;
    803821        usb_hid_report_item_t *report_item;
    804822
    805823        if(parser == NULL) {
    806                 return EINVAL;
    807         }
    808        
    809         item = (&parser->input)->next;
     824                return 0;
     825        }
     826       
     827        item = parser->input.next;
    810828        while(&parser->input != item) {
    811829                report_item = list_get_instance(item, usb_hid_report_item_t, link);
     
    824842/**
    825843 *
     844 * @param usage_path
     845 * @param usage_page
     846 * @param usage
     847 * @return
    826848 */
    827849int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
     
    845867/**
    846868 *
     869 * @param usage_path
     870 * @return
    847871 */
    848872void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
     
    860884/**
    861885 *
     886 * @param usage_path
     887 * @return
    862888 */
    863889void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
     
    873899/**
    874900 *
     901 * @param usage_path
     902 * @param tag
     903 * @param data
     904 * @return
    875905 */
    876906void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
     
    894924
    895925/**
    896  *
     926 *
     927 *
     928 * @param report_path
     929 * @param path
     930 * @param flags
     931 * @return
    897932 */
    898933int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
     
    949984                        break;
    950985
    951                 /* given path must be the end of the report one*/
     986                /* compare with only the end of path*/
    952987                case USB_HID_PATH_COMPARE_END:
    953988                                report_link = report_path->link.prev;
     
    9921027/**
    9931028 *
     1029 * @return
    9941030 */
    9951031usb_hid_report_path_t *usb_hid_report_path(void)
     
    10091045/**
    10101046 *
     1047 * @param path
     1048 * @return void
    10111049 */
    10121050void usb_hid_report_path_free(usb_hid_report_path_t *path)
     
    10191057
    10201058/**
    1021  *
    1022  */
    1023 int     usb_hid_report_path_clone(usb_hid_report_path_t *new_usage_path, usb_hid_report_path_t *usage_path)
     1059 * Clone content of given usage path to the new one
     1060 *
     1061 * @param usage_path
     1062 * @return
     1063 */
     1064usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
    10241065{
    10251066        usb_hid_report_usage_path_t *path_item;
    10261067        link_t *path_link;
    1027 
     1068        usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
     1069
     1070        if(new_usage_path == NULL){
     1071                return NULL;
     1072        }
    10281073       
    10291074        if(list_empty(&usage_path->link)){
    1030                 return EOK;
     1075                return new_usage_path;
    10311076        }
    10321077
     
    10391084        }
    10401085
     1086        return new_usage_path;
     1087}
     1088
     1089
     1090/*** OUTPUT API **/
     1091
     1092/** Allocates output report buffer
     1093 *
     1094 * @param parser
     1095 * @param size
     1096 * @return
     1097 */
     1098uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)
     1099{
     1100        if(parser == NULL) {
     1101                *size = 0;
     1102                return NULL;
     1103        }
     1104       
     1105        // read the last output report item
     1106        usb_hid_report_item_t *last;
     1107        link_t *link;
     1108
     1109        link = parser->output.prev;
     1110        if(link != &parser->output) {
     1111                last = list_get_instance(link, usb_hid_report_item_t, link);
     1112                *size = (last->offset + (last->size * last->count)) / 8;
     1113
     1114                uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));
     1115                memset(buffer, 0, sizeof(uint8_t) * (*size));
     1116                usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
     1117
     1118                return buffer;
     1119        }
     1120        else {
     1121                *size = 0;             
     1122                return NULL;
     1123        }
     1124}
     1125
     1126
     1127/** Frees output report buffer
     1128 *
     1129 * @param output Output report buffer
     1130 * @return
     1131 */
     1132void usb_hid_report_output_free(uint8_t *output)
     1133
     1134{
     1135        if(output != NULL) {
     1136                free (output);
     1137        }
     1138}
     1139
     1140/** Returns size of output for given usage path
     1141 *
     1142 * @param parser
     1143 * @param path
     1144 * @param flags
     1145 * @return
     1146 */
     1147size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
     1148                                  usb_hid_report_path_t *path, int flags)
     1149{
     1150        size_t ret = 0;
     1151        link_t *item;
     1152        usb_hid_report_item_t *report_item;
     1153
     1154        if(parser == NULL) {
     1155                return 0;
     1156        }
     1157       
     1158        item = parser->output.next;
     1159        while(&parser->output != item) {
     1160                report_item = list_get_instance(item, usb_hid_report_item_t, link);
     1161                if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
     1162                   (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
     1163                        ret += report_item->count;
     1164                }
     1165
     1166                item = item->next;
     1167        }
     1168
     1169        return ret;
     1170       
     1171}
     1172
     1173/** Updates the output report buffer by translated given data
     1174 *
     1175 * @param parser
     1176 * @param path
     1177 * @param flags
     1178 * @param buffer
     1179 * @param size
     1180 * @param data
     1181 * @param data_size
     1182 * @return
     1183 */
     1184int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
     1185                                    usb_hid_report_path_t *path, int flags,
     1186                                    uint8_t *buffer, size_t size,
     1187                                    int32_t *data, size_t data_size)
     1188{
     1189        usb_hid_report_item_t *report_item;
     1190        link_t *item;   
     1191        size_t idx=0;
     1192        int i=0;
     1193        int32_t value=0;
     1194        int offset;
     1195        int length;
     1196        int32_t tmp_value;
     1197       
     1198        if(parser == NULL) {
     1199                return EINVAL;
     1200        }
     1201
     1202        item = parser->output.next;     
     1203        while(item != &parser->output) {
     1204                report_item = list_get_instance(item, usb_hid_report_item_t, link);
     1205
     1206                for(i=0; i<report_item->count; i++) {
     1207
     1208                        if(idx >= data_size) {
     1209                                break;
     1210                        }
     1211
     1212                        // translate data
     1213                        if(USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags)) {
     1214                                value = report_item->logical_minimum;
     1215                        }
     1216                        else {
     1217                                //variable item
     1218                                value = usb_hid_translate_data_reverse(report_item, data[idx]);
     1219                                idx++;
     1220                        }
     1221
     1222                        if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
     1223                                ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
     1224                                       
     1225//                              // variable item
     1226                                offset = report_item->offset + (i * report_item->size);
     1227                                length = report_item->size;
     1228                        }
     1229                        else {
     1230                                //bitmap
     1231                                offset = report_item->offset;
     1232                                length = report_item->size * report_item->count;
     1233                        }
     1234
     1235                        if((offset/8) == ((offset+length-1)/8)) {
     1236                                // je to v jednom bytu
     1237                                if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
     1238                                        break; // TODO ErrorCode
     1239                                }
     1240
     1241                                size_t shift=0;
     1242                                if(((offset+length)%8) > 0) {
     1243                                        shift = (8- ((offset+length)%8));
     1244                                }
     1245
     1246                                value = value << shift;                                                 
     1247                                value = value & (((1 << length)-1) << shift);
     1248                                buffer[offset/8] = buffer[offset/8] | value;
     1249                        }
     1250                        else {
     1251                                // je to ve dvou!! FIXME: melo by to umet delsi jak 2
     1252
     1253                                // konec prvniho
     1254                                tmp_value = value;
     1255                                tmp_value = tmp_value >> (8 - (offset%8) - 1);
     1256                                tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);                             
     1257
     1258                                buffer[offset/8] = buffer[offset/8] | tmp_value;
     1259
     1260                                // a ted druhej
     1261                                value = value & ((1 << (length - (8 - (offset%8))))-1);
     1262                                value = value << (8 - (length - (8 - (offset%8))));
     1263                               
     1264                                buffer[(offset+length-1)/8] = buffer[(offset+length-1)/8] | value;
     1265                        }
     1266
     1267                }
     1268
     1269                item = item->next;
     1270        }
     1271
     1272
    10411273        return EOK;
    10421274}
    10431275
     1276/**
     1277 *
     1278 * @param item
     1279 * @param value
     1280 * @return
     1281 */
     1282int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
     1283{
     1284        int ret=0;
     1285        int resolution;
     1286
     1287        if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
     1288
     1289                // variable item
     1290                if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
     1291                        item->physical_minimum = item->logical_minimum;
     1292                        item->physical_maximum = item->logical_maximum;
     1293                }
     1294
     1295                if(item->physical_maximum == item->physical_minimum){
     1296                    resolution = 1;
     1297                }
     1298                else {
     1299                    resolution = (item->logical_maximum - item->logical_minimum) /
     1300                        ((item->physical_maximum - item->physical_minimum) *
     1301                        (usb_pow(10,(item->unit_exponent))));
     1302                }
     1303
     1304                ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
     1305        }
     1306        else {
     1307                // bitmapa
     1308                if(value == 0) {
     1309                        ret = 0;
     1310                }
     1311                else {
     1312                        size_t bitmap_idx = (value - item->usage_minimum);
     1313                        ret = 1 << bitmap_idx;
     1314                }
     1315        }
     1316
     1317
     1318        return ret;
     1319}
     1320
    10441321
    10451322/**
Note: See TracChangeset for help on using the changeset viewer.