Changeset 3d4aa055 in mainline for uspace/lib


Ignore:
Timestamp:
2011-05-06T13:08:10Z (14 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
075c1eb, 3da17644
Parents:
a58dd620 (diff), 310c4df (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:

Development branch changes

Location:
uspace/lib
Files:
8 added
4 deleted
16 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/include/errno.h

    ra58dd620 r3d4aa055  
    6565#define EEMPTY (-302)
    6666
     67/** Negative acknowledgment. */
     68#define ENAK (-303)
     69
    6770/** An API function is called while another blocking function is in progress. */
    6871#define EINPROGRESS  (-10036)
  • uspace/lib/drv/include/usbhid_iface.h

    ra58dd620 r3d4aa055  
    7272/** USB HID device communication interface. */
    7373typedef struct {
    74         /** Get number of items in the event.
     74        /** Get size of the event in bytes.
    7575         *
    7676         * @param[in] fun DDF function answering the request.
    7777         * @return Number of events or error code.
    7878         */
    79         int (*get_event_length)(ddf_fun_t *fun);
     79        size_t (*get_event_length)(ddf_fun_t *fun);
    8080
    8181        /** Get single event from the HID device.
    8282         *
    8383         * @param[in] fun DDF function answering the request.
    84          * @param[out] usage_page Array of usage pages and usages.
    85          * @param[out] usage Array of data (1:1 with @p usage).
    86          * @param[in] size Size of @p usage and @p data arrays.
     84         * @param[out] buffer Buffer with raw data from the device.
    8785         * @param[out] act_size Actual number of returned events.
    8886         * @param[in] flags Flags (see USBHID_IFACE_FLAG_*).
    8987         * @return Error code.
    9088         */
    91         int (*get_event)(ddf_fun_t *fun,
    92             uint16_t *usage_page, uint16_t *usage, size_t size, size_t *act_size,
    93             unsigned int flags);
     89        int (*get_event)(ddf_fun_t *fun, int32_t *buffer, size_t size,
     90            size_t *act_size, unsigned int flags);
    9491} usbhid_iface_t;
    9592
  • uspace/lib/usb/Makefile

    ra58dd620 r3d4aa055  
    4343        src/dump.c \
    4444        src/hidiface.c \
     45        src/hidpath.c \
    4546        src/hidparser.c \
     47        src/hiddescriptor.c \
    4648        src/hub.c \
    4749        src/pipepriv.c \
  • uspace/lib/usb/include/usb/classes/hid.h

    ra58dd620 r3d4aa055  
    5050        USB_HIDREQ_SET_PROTOCOL = 11
    5151} usb_hid_request_t;
    52 
    53 typedef enum {
    54         USB_HID_REPORT_TYPE_INPUT = 1,
    55         USB_HID_REPORT_TYPE_OUTPUT = 2,
    56         USB_HID_REPORT_TYPE_FEATURE = 3
    57 } usb_hid_report_type_t;
    5852
    5953typedef enum {
  • uspace/lib/usb/include/usb/classes/hid_report_items.h

    ra58dd620 r3d4aa055  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2011 Matej Klonfar
    33 * All rights reserved.
    44 *
     
    3737
    3838#include <stdint.h>
     39
     40/**
     41 * Item prefix
     42 */
     43#define USB_HID_ITEM_SIZE(data)         ((uint8_t)(data & 0x3))
     44#define USB_HID_ITEM_TAG(data)          ((uint8_t)((data & 0xF0) >> 4))
     45#define USB_HID_ITEM_TAG_CLASS(data)    ((uint8_t)((data & 0xC) >> 2))
     46#define USB_HID_ITEM_IS_LONG(data)      (data == 0xFE)
     47
     48
     49/**
     50 * Input/Output/Feature Item flags
     51 */
     52/** Constant (1) / Variable (0) */
     53#define USB_HID_ITEM_FLAG_CONSTANT(flags)       ((flags & 0x1) == 0x1)
     54/** Variable (1) / Array (0) */
     55#define USB_HID_ITEM_FLAG_VARIABLE(flags)       ((flags & 0x2) == 0x2)
     56/** Absolute / Relative*/
     57#define USB_HID_ITEM_FLAG_RELATIVE(flags)       ((flags & 0x4) == 0x4)
     58/** Wrap / No Wrap */
     59#define USB_HID_ITEM_FLAG_WRAP(flags)           ((flags & 0x8) == 0x8)
     60#define USB_HID_ITEM_FLAG_LINEAR(flags)         ((flags & 0x10) == 0x10)
     61#define USB_HID_ITEM_FLAG_PREFERRED(flags)      ((flags & 0x20) == 0x20)
     62#define USB_HID_ITEM_FLAG_POSITION(flags)       ((flags & 0x40) == 0x40)
     63#define USB_HID_ITEM_FLAG_VOLATILE(flags)       ((flags & 0x80) == 0x80)
     64#define USB_HID_ITEM_FLAG_BUFFERED(flags)       ((flags & 0x100) == 0x100)
    3965
    4066/* MAIN ITEMS */
  • uspace/lib/usb/include/usb/classes/hidparser.h

    ra58dd620 r3d4aa055  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2011 Matej Klonfar
    33 * All rights reserved.
    44 *
     
    3939#include <adt/list.h>
    4040#include <usb/classes/hid_report_items.h>
    41 
    42 /**
    43  * Item prefix
    44  */
    45 #define USB_HID_ITEM_SIZE(data)         ((uint8_t)(data & 0x3))
    46 #define USB_HID_ITEM_TAG(data)          ((uint8_t)((data & 0xF0) >> 4))
    47 #define USB_HID_ITEM_TAG_CLASS(data)    ((uint8_t)((data & 0xC) >> 2))
    48 #define USB_HID_ITEM_IS_LONG(data)      (data == 0xFE)
    49 
    50 
    51 /**
    52  * Input/Output/Feature Item flags
    53  */
    54 /** Constant (1) / Variable (0) */
    55 #define USB_HID_ITEM_FLAG_CONSTANT(flags)       ((flags & 0x1) == 0x1)
    56 /** Variable (1) / Array (0) */
    57 #define USB_HID_ITEM_FLAG_VARIABLE(flags)       ((flags & 0x2) == 0x2)
    58 /** Absolute / Relative*/
    59 #define USB_HID_ITEM_FLAG_RELATIVE(flags)       ((flags & 0x4) == 0x4)
    60 /** Wrap / No Wrap */
    61 #define USB_HID_ITEM_FLAG_WRAP(flags)           ((flags & 0x8) == 0x8)
    62 #define USB_HID_ITEM_FLAG_LINEAR(flags)         ((flags & 0x10) == 0x10)
    63 #define USB_HID_ITEM_FLAG_PREFERRED(flags)      ((flags & 0x20) == 0x20)
    64 #define USB_HID_ITEM_FLAG_POSITION(flags)       ((flags & 0x40) == 0x40)
    65 #define USB_HID_ITEM_FLAG_VOLATILE(flags)       ((flags & 0x80) == 0x80)
    66 #define USB_HID_ITEM_FLAG_BUFFERED(flags)       ((flags & 0x100) == 0x100)
    67 
    68 
    69 /**
    70  * Description of path of usage pages and usages in report descriptor
    71  */
    72 #define USB_HID_PATH_COMPARE_STRICT                             0
    73 #define USB_HID_PATH_COMPARE_END                                1
    74 #define USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY    4
    75 
    76 /** */
    77 typedef struct {
    78         /** */
    79         int32_t usage_page;
    80         /** */ 
    81         int32_t usage;
    82         /** */
    83         link_t link;
    84 } usb_hid_report_usage_path_t;
    85 
    86 /** */
    87 typedef struct {
    88         /** */ 
    89         int depth;     
    90         uint8_t report_id;
    91        
    92         /** */ 
    93         link_t link;
    94 
    95 } usb_hid_report_path_t;
    96 
    97 /**
    98  * Description of report items
    99  */
    100 typedef struct {
    101         /** */ 
    102         int32_t id;
    103         /** */ 
    104         int32_t usage_minimum;
    105         /** */ 
    106         int32_t usage_maximum;
    107         /** */ 
    108         int32_t logical_minimum;
    109         /** */ 
    110         int32_t logical_maximum;
    111         /** */ 
    112         int32_t size;
    113         /** */ 
    114         int32_t count;
    115         /** */ 
    116         size_t offset;
    117         /** */ 
    118         int32_t delimiter;
    119         /** */ 
    120         int32_t unit_exponent;
    121         /** */ 
    122         int32_t unit;
    123 
    124         /** */
    125         int32_t string_index;
    126         /** */ 
    127         int32_t string_minimum;
    128         /** */ 
    129         int32_t string_maximum;
    130         /** */ 
    131         int32_t designator_index;
    132         /** */ 
    133         int32_t designator_minimum;
    134         /** */ 
    135         int32_t designator_maximum;
    136         /** */ 
    137         int32_t physical_minimum;
    138         /** */ 
    139         int32_t physical_maximum;
    140 
    141         /** */ 
    142         uint8_t item_flags;
    143 
    144         /** */ 
    145         usb_hid_report_path_t *usage_path;
    146         /** */ 
    147         link_t link;
    148 } usb_hid_report_item_t;
    149 
    150 
    151 /** HID report parser structure. */
    152 typedef struct {       
    153         /** */ 
    154         link_t input;
    155         /** */ 
    156         link_t output;
    157         /** */ 
    158         link_t feature;
    159        
    160         int use_report_id;
    161 
    162         /** */
    163         link_t stack;
    164 } usb_hid_report_parser_t;     
    165 
    166 
    167 /** HID parser callbacks for IN items. */
    168 typedef struct {
    169         /** Callback for keyboard.
    170          *
    171          * @param key_codes Array of pressed key (including modifiers).
    172          * @param count Length of @p key_codes.
    173          * @param arg Custom argument.
    174          */
    175         void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t report_id, void *arg);
    176 } usb_hid_report_in_callbacks_t;
    177 
    178 
    179 typedef enum {
    180         USB_HID_MOD_LCTRL = 0x01,
    181         USB_HID_MOD_LSHIFT = 0x02,
    182         USB_HID_MOD_LALT = 0x04,
    183         USB_HID_MOD_LGUI = 0x08,
    184         USB_HID_MOD_RCTRL = 0x10,
    185         USB_HID_MOD_RSHIFT = 0x20,
    186         USB_HID_MOD_RALT = 0x40,
    187         USB_HID_MOD_RGUI = 0x80,
    188         USB_HID_MOD_COUNT = 8
    189 } usb_hid_modifiers_t;
    190 
    191 //typedef enum {
    192 //      USB_HID_LED_NUM_LOCK = 0x1,
    193 //      USB_HID_LED_CAPS_LOCK = 0x2,
    194 //      USB_HID_LED_SCROLL_LOCK = 0x4,
    195 //      USB_HID_LED_COMPOSE = 0x8,
    196 //      USB_HID_LED_KANA = 0x10,
    197 //      USB_HID_LED_COUNT = 5
    198 //} usb_hid_led_t;
    199 
    200 static const usb_hid_modifiers_t
    201     usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
    202         USB_HID_MOD_LCTRL,
    203         USB_HID_MOD_LSHIFT,
    204         USB_HID_MOD_LALT,
    205         USB_HID_MOD_LGUI,
    206         USB_HID_MOD_RCTRL,
    207         USB_HID_MOD_RSHIFT,
    208         USB_HID_MOD_RALT,
    209         USB_HID_MOD_RGUI
    210 };
    211 
    212 //static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
    213 //      USB_HID_LED_NUM_LOCK,
    214 //      USB_HID_LED_CAPS_LOCK,
    215 //      USB_HID_LED_SCROLL_LOCK,
    216 //      USB_HID_LED_COMPOSE,
    217 //      USB_HID_LED_KANA
    218 //};
    219 
    220 //#define USB_HID_BOOT_KEYBOARD_NUM_LOCK                0x01
    221 //#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK               0x02
    222 //#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK     0x04
    223 //#define USB_HID_BOOT_KEYBOARD_COMPOSE         0x08
    224 //#define USB_HID_BOOT_KEYBOARD_KANA                    0x10
    225 
    226 /*
    227  * Descriptor parser functions
    228  */
    229 /** */
    230 int usb_hid_parser_init(usb_hid_report_parser_t *parser);
    231 
    232 /** */
    233 int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
    234     const uint8_t *data, size_t size);
    235 
    236 /** */
    237 void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
    238 
    239 /** */
    240 void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
    241 
    242 /*
    243  * Boot protocol functions
    244  */
    245 /** */
    246 int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
    247         const usb_hid_report_in_callbacks_t *callbacks, void *arg);
    248 
    249 /** */
    250 int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
     41#include <usb/classes/hidpath.h>
     42#include <usb/classes/hidtypes.h>
     43#include <usb/classes/hiddescriptor.h>
    25144
    25245
     
    25548 */
    25649/** */
    257 int usb_hid_parse_report(const usb_hid_report_parser_t *parser, 
    258     const uint8_t *data, size_t size,
    259     usb_hid_report_path_t *path, int flags,
    260     const usb_hid_report_in_callbacks_t *callbacks, void *arg);
     50int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
     51                         size_t size, uint8_t *report_id);
    26152
    26253/** */
    263 size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
     54size_t usb_hid_report_input_length(const usb_hid_report_t *report,
    26455        usb_hid_report_path_t *path, int flags);
    265 
    266 
    267 
    268 /*
    269  * usage path functions
    270  */
    271 /** */
    272 usb_hid_report_path_t *usb_hid_report_path(void);
    273 
    274 /** */
    275 void usb_hid_report_path_free(usb_hid_report_path_t *path);
    276 
    277 /** */
    278 int usb_hid_report_path_set_report_id(usb_hid_report_path_t *usage_path, uint8_t report_id);
    279 
    280 /** */
    281 int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, int32_t usage_page, int32_t usage);
    282 
    283 /** */
    284 void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
    285 
    286 /** */
    287 void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
    288 
    289 /** */
    290 void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data);
    291 
    292 /** */
    293 int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, usb_hid_report_path_t *path, int flags);
    294 
    295 /** */
    296 usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
    297 
    29856
    29957/*
     
    30159 */
    30260/** Allocates output report buffer*/
    303 uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size);
     61uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
     62                               uint8_t report_id);
    30463
    30564/** Frees output report buffer*/
     
    30766
    30867/** Returns size of output for given usage path */
    309 size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
     68size_t usb_hid_report_output_size(usb_hid_report_t *report,
    31069                                  usb_hid_report_path_t *path, int flags);
    31170
    312 /** Updates the output report buffer by translated given data */
    313 int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
    314                                     usb_hid_report_path_t *path, int flags,
    315                                     uint8_t *buffer, size_t size,
    316                                     int32_t *data, size_t data_size);
     71/** Makes the output report buffer by translated given data */
     72int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
     73                                    uint8_t *buffer, size_t size);
     74
     75/** */
     76usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
     77                                                   usb_hid_report_field_t *field,
     78                                                   usb_hid_report_path_t *path,
     79                                                   int flags,
     80                                                   usb_hid_report_type_t type);
     81
     82/** */
     83uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report,
     84                                     uint8_t report_id,
     85                                     usb_hid_report_type_t type);
     86
    31787#endif
    31888/**
  • uspace/lib/usb/include/usb/classes/hidreport.h

    ra58dd620 r3d4aa055  
    5757 */
    5858int usb_hid_process_report_descriptor(usb_device_t *dev,
    59     usb_hid_report_parser_t *parser);
     59    usb_hid_report_t *report);
    6060
    6161#endif /* LIBUSB_HIDREPORT_H_ */
  • uspace/lib/usb/include/usb/classes/hidut.h

    ra58dd620 r3d4aa055  
    4646        USB_HIDUT_PAGE_KEYBOARD = 7,
    4747        USB_HIDUT_PAGE_LED = 8,
    48         USB_HIDUT_PAGE_BUTTON = 9
    49         /* USB_HIDUT_PAGE_ = , */
     48        USB_HIDUT_PAGE_BUTTON = 9,
     49        USB_HIDUT_PAGE_ORDINAL = 0x0a,
     50        USB_HIDUT_PAGE_TELEPHONY_DEVICE = 0x0b,
     51        USB_HIDUT_PAGE_CONSUMER = 0x0c
    5052} usb_hidut_usage_page_t;
    5153
     
    5759        USB_HIDUT_USAGE_GENERIC_DESKTOP_GAMEPAD = 5,
    5860        USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD = 6,
    59         USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYPAD = 7
     61        USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYPAD = 7,
     62        USB_HIDUT_USAGE_GENERIC_DESKTOP_X = 0x30,
     63        USB_HIDUT_USAGE_GENERIC_DESKTOP_Y = 0x31,
     64        USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL = 0x38
    6065        /* USB_HIDUT_USAGE_GENERIC_DESKTOP_ = , */
    6166       
  • uspace/lib/usb/include/usb/usb.h

    ra58dd620 r3d4aa055  
    9696        USB_REQUEST_RECIPIENT_DEVICE = 0,
    9797        USB_REQUEST_RECIPIENT_INTERFACE = 1,
    98         USB_REQUEST_RECIPIENT_ENDPOINT = 2
     98        USB_REQUEST_RECIPIENT_ENDPOINT = 2,
     99        USB_REQUEST_RECIPIENT_OTHER = 3
    99100} usb_request_recipient_t;
    100101
  • uspace/lib/usb/src/debug.c

    ra58dd620 r3d4aa055  
    158158
    159159/** Fibril local storage for the dumped buffer. */
    160 static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
     160static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
     161/** Fibril local storage for buffer switching. */
     162static fibril_local int buffer_dump_index = 0;
    161163
    162164/** Dump buffer into string.
     
    167169 * can not do that) and you do not have to guard it against concurrent
    168170 * calls to it.
    169  * The only limitation is that each call rewrites the buffer again.
     171 * The only limitation is that each second call rewrites the buffer again
     172 * (internally, two buffer are used in cyclic manner).
    170173 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
    171174 * to screen or writing to file).
     
    173176 * that is not a big limitation.
    174177 *
    175  * @warning You cannot use this function twice in the same printf
     178 * @warning You cannot use this function more than twice in the same printf
    176179 * (see detailed explanation).
    177180 *
     
    185188{
    186189        /*
    187          * Remove previous string (that might also reveal double usage of
    188          * this function).
     190         * Remove previous string.
    189191         */
    190         bzero(buffer_dump, BUFFER_DUMP_LEN);
     192        bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);
    191193
    192194        if (buffer == NULL) {
     
    202204        /* How many bytes are available in the output buffer. */
    203205        size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
    204         char *it = buffer_dump;
     206        char *it = buffer_dump[buffer_dump_index];
    205207
    206208        size_t index = 0;
     
    253255        }
    254256
    255         return buffer_dump;
     257        /* Next time, use the other buffer. */
     258        buffer_dump_index = 1 - buffer_dump_index;
     259
     260        /* Need to take the old one due to previous line. */
     261        return buffer_dump[1 - buffer_dump_index];
    256262}
    257263
  • uspace/lib/usb/src/hidparser.c

    ra58dd620 r3d4aa055  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2011 Matej Klonfar
    33 * All rights reserved.
    44 *
     
    3939#include <mem.h>
    4040#include <usb/debug.h>
    41 
    42 /** */
    43 #define USB_HID_NEW_REPORT_ITEM 1
    44 
    45 /** */
    46 #define USB_HID_NO_ACTION               2
    47 
    48 /** */
    49 #define USB_HID_UNKNOWN_TAG             -99
    50 
    51 /*
    52  * Private descriptor parser functions
    53  */
    54 int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
    55                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
    56 int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    57                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
    58 int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    59                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
    60 int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    61                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
    62 
    63 void usb_hid_descriptor_print_list(link_t *head);
    64 int usb_hid_report_reset_local_items();
    65 void usb_hid_free_report_list(link_t *head);
    66 usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
     41#include <assert.h>
     42
     43
    6744/*
    6845 * Data translation private functions
    6946 */
    70 int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
     47uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
    7148inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
    72 int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
    73 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);
     49int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
     50uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
    7451int usb_pow(int a, int b);
     52
    7553
    7654// TODO: tohle ma bejt asi jinde
     
    9068}
    9169
    92 /**
    93  * Initialize the report descriptor parser structure
    94  *
    95  * @param parser Report descriptor parser structure
    96  * @return Error code
    97  */
    98 int usb_hid_parser_init(usb_hid_report_parser_t *parser)
    99 {
    100         if(parser == NULL) {
    101                 return EINVAL;
    102         }
    103 
    104         list_initialize(&(parser->input));
    105     list_initialize(&(parser->output));
    106     list_initialize(&(parser->feature));
    107 
    108         list_initialize(&(parser->stack));
    109 
    110         parser->use_report_id = 0;
    111     return EOK;   
    112 }
    113 
    114 
    115 /** Parse HID report descriptor.
    116  *
    117  * @param parser Opaque HID report parser structure.
    118  * @param data Data describing the report.
    119  * @return Error code.
    120  */
    121 int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
    122     const uint8_t *data, size_t size)
    123 {
    124         size_t i=0;
    125         uint8_t tag=0;
    126         uint8_t item_size=0;
    127         int class=0;
    128         int ret;
    129         usb_hid_report_item_t *report_item=0;
    130         usb_hid_report_item_t *new_report_item;
    131         usb_hid_report_path_t *usage_path;
    132         usb_hid_report_path_t *tmp_usage_path;
    133 
    134         size_t offset_input=0;
    135         size_t offset_output=0;
    136         size_t offset_feature=0;
    137        
    138 
    139         /* parser structure initialization*/
    140         if(usb_hid_parser_init(parser) != EOK) {
    141                 return EINVAL;
    142         }
    143        
    144 
    145         /*report item initialization*/
    146         if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
    147                 return ENOMEM;
    148         }
    149         memset(report_item, 0, sizeof(usb_hid_report_item_t));
    150         list_initialize(&(report_item->link)); 
    151 
    152         /* usage path context initialization */
    153         if(!(usage_path=usb_hid_report_path())){
    154                 return ENOMEM;
    155         }
    156        
    157         while(i<size){ 
    158                 if(!USB_HID_ITEM_IS_LONG(data[i])){
    159 
    160                         if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
    161                                 return EINVAL; // TODO ERROR CODE
    162                         }
    163                        
    164                         tag = USB_HID_ITEM_TAG(data[i]);
    165                         item_size = USB_HID_ITEM_SIZE(data[i]);
    166                         class = USB_HID_ITEM_TAG_CLASS(data[i]);
    167 
    168                         usb_log_debug2(
    169                                 "i(%zu) data(%X) value(%X): TAG %d, class %u, size %u - ", i,
    170                             data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
    171                             tag, class, item_size);
    172                        
    173                         ret = usb_hid_report_parse_tag(tag,class,data+i+1,
    174                                                        item_size,report_item, usage_path);
    175                         usb_log_debug2("ret: %u\n", ret);
    176                         switch(ret){
    177                                 case USB_HID_NEW_REPORT_ITEM:
    178                                         // store report item to report and create the new one
    179                                         usb_log_debug("\nNEW REPORT ITEM: %X",ret);
    180 
    181                                         // store current usage path
    182                                         report_item->usage_path = usage_path;
    183                                        
    184                                         // clone path to the new one
    185                                         tmp_usage_path = usb_hid_report_path_clone(usage_path);
    186 
    187                                         // swap
    188                                         usage_path = tmp_usage_path;
    189                                         tmp_usage_path = NULL;
    190 
    191                                         usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);   
    192                                         if(report_item->id != 0){
    193                                                 parser->use_report_id = 1;
    194                                         }
    195                                        
    196                                         switch(tag) {
    197                                                 case USB_HID_REPORT_TAG_INPUT:
    198                                                         report_item->offset = offset_input;
    199                                                         offset_input += report_item->count * report_item->size;
    200                                                         usb_log_debug(" - INPUT\n");
    201                                                         list_append(&(report_item->link), &(parser->input));
    202                                                         break;
    203                                                 case USB_HID_REPORT_TAG_OUTPUT:
    204                                                         report_item->offset = offset_output;
    205                                                         offset_output += report_item->count * report_item->size;
    206                                                         usb_log_debug(" - OUTPUT\n");
    207                                                                 list_append(&(report_item->link), &(parser->output));
    208 
    209                                                         break;
    210                                                 case USB_HID_REPORT_TAG_FEATURE:
    211                                                         report_item->offset = offset_feature;
    212                                                         offset_feature += report_item->count * report_item->size;
    213                                                         usb_log_debug(" - FEATURE\n");
    214                                                                 list_append(&(report_item->link), &(parser->feature));
    215                                                         break;
    216                                                 default:
    217                                                     usb_log_debug("\tjump over - tag %X\n", tag);
    218                                                     break;
    219                                         }
    220 
    221                                         /* clone current state table to the new item */
    222                                         if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
    223                                                 return ENOMEM;
    224                                         }                                       
    225                                         memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
    226                                         link_initialize(&(new_report_item->link));
    227                                        
    228                                         /* reset local items */
    229                                         new_report_item->usage_minimum = 0;
    230                                         new_report_item->usage_maximum = 0;
    231                                         new_report_item->designator_index = 0;
    232                                         new_report_item->designator_minimum = 0;
    233                                         new_report_item->designator_maximum = 0;
    234                                         new_report_item->string_index = 0;
    235                                         new_report_item->string_minimum = 0;
    236                                         new_report_item->string_maximum = 0;
    237 
    238                                         /* reset usage from current usage path */
    239                                         usb_hid_report_usage_path_t *path = list_get_instance(&usage_path->link, usb_hid_report_usage_path_t, link);
    240                                         path->usage = 0;
    241                                        
    242                                         report_item = new_report_item;
    243                                                                                
    244                                         break;
    245                                 case USB_HID_REPORT_TAG_PUSH:
    246                                         // push current state to stack
    247                                         new_report_item = usb_hid_report_item_clone(report_item);
    248                                         list_prepend (&parser->stack, &new_report_item->link);
    249                                        
    250                                         break;
    251                                 case USB_HID_REPORT_TAG_POP:
    252                                         // restore current state from stack
    253                                         if(list_empty (&parser->stack)) {
    254                                                 return EINVAL;
    255                                         }
    256                                        
    257                                         report_item = list_get_instance(&parser->stack, usb_hid_report_item_t, link);
    258                                         list_remove (parser->stack.next);
    259                                        
    260                                         break;
    261                                        
    262                                 default:
    263                                         // nothing special to do                                       
    264                                         break;
    265                         }
    266 
    267                         /* jump over the processed block */
    268                         i += 1 + USB_HID_ITEM_SIZE(data[i]);
    269                 }
    270                 else{
    271                         // TBD
    272                         i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
    273                 }
    274                
    275 
    276         }
    277        
    278         return EOK;
    279 }
    280 
    281 
    282 /**
    283  * Parse input report.
    284  *
    285  * @param data Data for report
    286  * @param size Size of report
    287  * @param callbacks Callbacks for report actions
    288  * @param arg Custom arguments
    289  *
    290  * @return Error code
    291  */
    292 int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
    293         const usb_hid_report_in_callbacks_t *callbacks, void *arg)
    294 {
    295         int i;
    296         usb_hid_report_item_t item;
    297 
    298         /* fill item due to the boot protocol report descriptor */
    299         // modifier keys are in the first byte
    300         uint8_t modifiers = data[0];
    301 
    302         item.offset = 2; /* second byte is reserved */
    303         item.size = 8;
    304         item.count = 6;
    305         item.usage_minimum = 0;
    306         item.usage_maximum = 255;
    307         item.logical_minimum = 0;
    308         item.logical_maximum = 255;
    309 
    310         if (size != 8) {
    311                 return -1; //ERANGE;
    312         }
    313 
    314         uint8_t keys[6];
    315         for (i = 0; i < item.count; i++) {
    316                 keys[i] = data[i + item.offset];
    317         }
    318 
    319         callbacks->keyboard(keys, 6, modifiers, arg);
    320         return EOK;
    321 }
    322 
    323 /**
    324  * Makes output report for keyboard boot protocol
    325  *
    326  * @param leds
    327  * @param output Output report data buffer
    328  * @param size Size of the output buffer
    329  * @return Error code
    330  */
    331 int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
    332 {
    333         if(size != 1){
    334                 return -1;
    335         }
    336 
    337         /* used only first five bits, others are only padding*/
    338         *data = leds;
    339         return EOK;
    340 }
    341 
    342 /**
    343  * Parse one tag of the report descriptor
    344  *
    345  * @param Tag to parse
    346  * @param Report descriptor buffer
    347  * @param Size of data belongs to this tag
    348  * @param Current report item structe
    349  * @return Code of action to be done next
    350  */
    351 int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
    352                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
    353 {       
    354         int ret;
    355        
    356         switch(class){
    357                 case USB_HID_TAG_CLASS_MAIN:
    358 
    359                         if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
    360                                 return USB_HID_NEW_REPORT_ITEM;
    361                         }
    362                         else {
    363                                 /*TODO process the error */
    364                                 return ret;
    365                            }
    366                         break;
    367 
    368                 case USB_HID_TAG_CLASS_GLOBAL: 
    369                         return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
    370                         break;
    371 
    372                 case USB_HID_TAG_CLASS_LOCAL:                   
    373                         return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
    374                         break;
    375                 default:
    376                         return USB_HID_NO_ACTION;
    377         }
    378 }
    379 
    380 /**
    381  * Parse main tags of report descriptor
    382  *
    383  * @param Tag identifier
    384  * @param Data buffer
    385  * @param Length of data buffer
    386  * @param Current state table
    387  * @return Error code
    388  */
    389 
    390 int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    391                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
    392 {               
    393         switch(tag)
    394         {
    395                 case USB_HID_REPORT_TAG_INPUT:
    396                 case USB_HID_REPORT_TAG_OUTPUT:
    397                 case USB_HID_REPORT_TAG_FEATURE:
    398                         report_item->item_flags = *data;                       
    399                         return EOK;                     
    400                         break;
    401                        
    402                 case USB_HID_REPORT_TAG_COLLECTION:
    403                         usb_hid_report_path_append_item(usage_path, 0, 0);
    404                                                
    405                         return USB_HID_NO_ACTION;
    406                         break;
    407                        
    408                 case USB_HID_REPORT_TAG_END_COLLECTION:
    409                         // TODO
    410                         // znici posledni uroven ve vsech usage paths
    411                         // otazka jestli nema nicit dve, respektive novou posledni vynulovat?
    412                         usb_hid_report_remove_last_item(usage_path);
    413                         return USB_HID_NO_ACTION;
    414                         break;
    415                 default:
    416                         return USB_HID_NO_ACTION;
    417         }
    418 
    419         return EOK;
    420 }
    421 
    422 /**
    423  * Parse global tags of report descriptor
    424  *
    425  * @param Tag identifier
    426  * @param Data buffer
    427  * @param Length of data buffer
    428  * @param Current state table
    429  * @return Error code
    430  */
    431 int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    432                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
    433 {
    434         // TODO take care about the bit length of data
    435         switch(tag)
    436         {
    437                 case USB_HID_REPORT_TAG_USAGE_PAGE:
    438                         // zmeni to jenom v poslednim poli aktualni usage path
    439                         usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL,
    440                                 usb_hid_report_tag_data_int32(data,item_size));
    441                         break;
    442                 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
    443                         report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
    444                         break;
    445                 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
    446                         report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
    447                         break;
    448                 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
    449                         report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
    450                         break;                 
    451                 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
    452                         report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
    453                         break;
    454                 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
    455                         report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
    456                         break;
    457                 case USB_HID_REPORT_TAG_UNIT:
    458                         report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
    459                         break;
    460                 case USB_HID_REPORT_TAG_REPORT_SIZE:
    461                         report_item->size = usb_hid_report_tag_data_int32(data,item_size);
    462                         break;
    463                 case USB_HID_REPORT_TAG_REPORT_COUNT:
    464                         report_item->count = usb_hid_report_tag_data_int32(data,item_size);
    465                         break;
    466                 case USB_HID_REPORT_TAG_REPORT_ID:
    467                         report_item->id = usb_hid_report_tag_data_int32(data,item_size);
    468                         break;
    469                 case USB_HID_REPORT_TAG_PUSH:
    470                 case USB_HID_REPORT_TAG_POP:
    471                         return tag;
    472                         break;
    473                        
    474                 default:
    475                         return USB_HID_NO_ACTION;
    476         }
    477        
    478         return EOK;
    479 }
    480 
    481 /**
    482  * Parse local tags of report descriptor
    483  *
    484  * @param Tag identifier
    485  * @param Data buffer
    486  * @param Length of data buffer
    487  * @param Current state table
    488  * @return Error code
    489  */
    490 int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
    491                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
    492 {
    493         switch(tag)
    494         {
    495                 case USB_HID_REPORT_TAG_USAGE:
    496                         usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL,
    497                                 usb_hid_report_tag_data_int32(data,item_size));
    498                         break;
    499                 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
    500                         report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
    501                         break;
    502                 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
    503                         report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
    504                         break;
    505                 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
    506                         report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
    507                         break;
    508                 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
    509                         report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
    510                         break;
    511                 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
    512                         report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
    513                         break;
    514                 case USB_HID_REPORT_TAG_STRING_INDEX:
    515                         report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
    516                         break;
    517                 case USB_HID_REPORT_TAG_STRING_MINIMUM:
    518                         report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
    519                         break;
    520                 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
    521                         report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
    522                         break;                 
    523                 case USB_HID_REPORT_TAG_DELIMITER:
    524                         report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
    525                         break;
    526                
    527                 default:
    528                         return USB_HID_NO_ACTION;
    529         }
    530        
    531         return EOK;
    532 }
    533 
    534 /**
    535  * Converts raw data to int32 (thats the maximum length of short item data)
    536  *
    537  * @param Data buffer
    538  * @param Size of buffer
    539  * @return Converted int32 number
    540  */
    541 int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
    542 {
    543         unsigned int i;
    544         int32_t result;
    545 
    546         result = 0;
    547         for(i=0; i<size; i++) {
    548                 result = (result | (data[i]) << (i*8));
    549         }
    550 
    551         return result;
    552 }
    553 
    554 
    555 
    556 /**
    557  * Prints content of given list of report items.
    558  *
    559  * @param List of report items (usb_hid_report_item_t)
    560  * @return void
    561  */
    562 void usb_hid_descriptor_print_list(link_t *head)
    563 {
    564         usb_hid_report_item_t *report_item;
    565         usb_hid_report_usage_path_t *path_item;
    566         link_t *path;
    567         link_t *item;
    568        
    569         if(head == NULL || list_empty(head)) {
    570             usb_log_debug("\tempty\n");
    571             return;
    572         }
    573        
    574         for(item = head->next; item != head; item = item->next) {
    575                
    576                 report_item = list_get_instance(item, usb_hid_report_item_t, link);
    577 
    578                 usb_log_debug("\tOFFSET: %zX\n", report_item->offset);
    579                 usb_log_debug("\tCOUNT: %X\n", report_item->count);
    580                 usb_log_debug("\tSIZE: %X\n", report_item->size);
    581                 usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
    582                 usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags));
    583                 usb_log_debug("\tUSAGE PATH:\n");
    584 
    585                 path = report_item->usage_path->link.next;
    586                 while(path != &report_item->usage_path->link)   {
    587                         path_item = list_get_instance(path, usb_hid_report_usage_path_t, link);
    588                         usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage);
    589                         path = path->next;
    590                 }
    591                                
    592                 usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
    593                 usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);         
    594                 usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);         
    595                 usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);                         
    596                 usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum);
    597                 usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum);
    598                
    599                 usb_log_debug("\n");           
    600 
    601         }
    602 
    603 
    604 }
    605 /**
    606  * Prints content of given report descriptor in human readable format.
    607  *
    608  * @param parser Parsed descriptor to print
    609  * @return void
    610  */
    611 void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
    612 {
    613         if(parser == NULL) {
    614                 return;
    615         }
    616        
    617         usb_log_debug("INPUT:\n");
    618         usb_hid_descriptor_print_list(&parser->input);
    619        
    620         usb_log_debug("OUTPUT: \n");
    621         usb_hid_descriptor_print_list(&parser->output);
    622        
    623         usb_log_debug("FEATURE:\n");   
    624         usb_hid_descriptor_print_list(&parser->feature);
    625 
    626 }
    627 
    628 /**
    629  * Releases whole linked list of report items
    630  *
    631  * @param head Head of list of report descriptor items (usb_hid_report_item_t)
    632  * @return void
    633  */
    634 void usb_hid_free_report_list(link_t *head)
    635 {
    636         return;
    637        
    638         usb_hid_report_item_t *report_item;
    639         link_t *next;
    640        
    641         if(head == NULL || list_empty(head)) {         
    642             return;
    643         }
    644        
    645         next = head->next;
    646         while(next != head) {
    647        
    648             report_item = list_get_instance(next, usb_hid_report_item_t, link);
    649 
    650                 while(!list_empty(&report_item->usage_path->link)) {
    651                         usb_hid_report_remove_last_item(report_item->usage_path);
    652                 }
    653 
    654                
    655             next = next->next;
    656            
    657             free(report_item);
    658         }
    659        
    660         return;
    661        
    662 }
    663 
    664 /** Frees the HID report descriptor parser structure
    665  *
    666  * @param parser Opaque HID report parser structure
    667  * @return void
    668  */
    669 void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
    670 {
    671         if(parser == NULL){
    672                 return;
    673         }
    674 
    675         parser->use_report_id = 0;
    676 
    677         usb_hid_free_report_list(&parser->input);
    678         usb_hid_free_report_list(&parser->output);
    679         usb_hid_free_report_list(&parser->feature);
    680 
    681         return;
    682 }
     70
     71
    68372
    68473/** Parse and act upon a HID report.
     
    68877 * @param parser Opaque HID report parser structure.
    68978 * @param data Data for the report.
    690  * @param callbacks Callbacks for report actions.
    691  * @param arg Custom argument (passed through to the callbacks).
    69279 * @return Error code.
    69380 */
    694 int usb_hid_parse_report(const usb_hid_report_parser_t *parser, 
    695     const uint8_t *data, size_t size,
    696     usb_hid_report_path_t *path, int flags,
    697     const usb_hid_report_in_callbacks_t *callbacks, void *arg)
     81int usb_hid_parse_report(const usb_hid_report_t *report, 
     82    const uint8_t *data, size_t size, uint8_t *report_id)
    69883{
    69984        link_t *list_item;
    700         usb_hid_report_item_t *item;
    701         uint8_t *keys;
    702         uint8_t item_value;
    703         size_t key_count=0;
    704         size_t i=0;
    705         size_t j=0;
    706         uint8_t report_id = 0;
    707 
    708         if(parser == NULL) {
     85        usb_hid_report_field_t *item;
     86
     87        usb_hid_report_description_t *report_des;
     88        usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
     89
     90        if(report == NULL) {
    70991                return EINVAL;
    71092        }
    711        
    712         /* get the size of result array */
    713         key_count = usb_hid_report_input_length(parser, path, flags);
    714 
    715         if(!(keys = malloc(sizeof(uint8_t) * key_count))){
    716                 return ENOMEM;
    717         }
    718 
    719         if(parser->use_report_id != 0) {
    720                 report_id = data[0];
    721                 usb_hid_report_path_set_report_id(path, report_id);
    722         }
     93
     94        if(report->use_report_ids != 0) {
     95                *report_id = data[0];
     96        }       
     97        else {
     98                *report_id = 0;
     99        }
     100
     101
     102        report_des = usb_hid_report_find_description(report, *report_id, type);
    723103
    724104        /* read data */
    725         list_item = parser->input.next;   
    726         while(list_item != &(parser->input)) {
    727 
    728                 item = list_get_instance(list_item, usb_hid_report_item_t, link);
    729 
    730                 if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) &&
    731                    (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) {
    732                         for(j=0; j<(size_t)(item->count); j++) {
    733                                 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) ||
    734                                    ((item->usage_minimum == 0) && (item->usage_maximum == 0))) {
    735                                         // variable item
    736                                         keys[i++] = usb_hid_translate_data(item, data,j);
    737                                 }
    738                                 else {
    739                                         // bitmapa
    740                                         if((item_value = usb_hid_translate_data(item, data, j)) != 0) {
    741                                                 keys[i++] = (item->count - 1 - j) + item->usage_minimum;
    742                                         }
    743                                         else {
    744                                                 keys[i++] = 0;
    745                                         }
    746                                 }
    747                         }
     105        list_item = report_des->report_items.next;         
     106        while(list_item != &(report_des->report_items)) {
     107
     108                item = list_get_instance(list_item, usb_hid_report_field_t, link);
     109
     110                if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
     111                       
     112                        if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
     113
     114                                // array
     115                                item->value = usb_hid_translate_data(item, data);
     116                            item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
     117                        }
     118                        else {
     119                                // variable item
     120                                item->value = usb_hid_translate_data(item, data);                               
     121                        }                               
    748122                }
    749123                list_item = list_item->next;
    750124        }
    751 
    752         callbacks->keyboard(keys, key_count, report_id, arg);
    753125           
    754         free(keys);     
    755126        return EOK;
    756127       
     
    758129
    759130/**
    760  * Translate data from the report as specified in report descriptor
     131 * Translate data from the report as specified in report descriptor item
    761132 *
    762133 * @param item Report descriptor item with definition of translation
     
    765136 * @return Translated data
    766137 */
    767 int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
     138int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
    768139{
    769140        int resolution;
     
    771142        int part_size;
    772143       
    773         int32_t value;
     144        int32_t value=0;
    774145        int32_t mask;
    775146        const uint8_t *foo;
    776147
    777         // now only common numbers llowed
     148        // now only shot tags are allowed
    778149        if(item->size > 32) {
    779150                return 0;
    780151        }
    781152
    782         if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
     153        if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
    783154                item->physical_minimum = item->logical_minimum;
    784                 item->physical_maximum = item->logical_maximum;         
    785         }
     155                item->physical_maximum = item->logical_maximum;                 
     156        }
     157       
    786158
    787159        if(item->physical_maximum == item->physical_minimum){
     
    794166        }
    795167
    796         offset = item->offset + (j * item->size);
    797         if(item->id != 0) {
    798                 offset += 8;
    799                 usb_log_debug("MOVED OFFSET BY 1Byte, REPORT_ID(%d)\n", item->id);
    800         }
    801        
     168        offset = item->offset;
    802169        // FIXME
    803         if((offset/8) != ((offset+item->size)/8)) {
    804                 usb_log_debug2("offset %d\n", offset);
     170        if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
    805171               
    806172                part_size = ((offset+item->size)%8);
    807                 usb_log_debug2("part size %d\n",part_size);
    808 
    809                 // the higher one
    810                 foo = data+(offset/8);
    811                 mask =  ((1 << (item->size-part_size))-1);
    812                 value = (*foo & mask) << part_size;
    813 
    814                 usb_log_debug2("hfoo %x\n", *foo);
    815                 usb_log_debug2("hmaska %x\n",  mask);
    816                 usb_log_debug2("hval %d\n", value);             
    817 
    818                 // the lower one
    819                 foo = data+((offset+item->size)/8);
    820                 mask =  ((1 << part_size)-1) << (8-part_size);
    821                 value += ((*foo & mask) >> (8-part_size));
    822 
    823                 usb_log_debug2("lfoo %x\n", *foo);
    824                 usb_log_debug2("lmaska %x\n",  mask);
    825                 usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));             
    826                 usb_log_debug2("val %d\n", value);
    827                
    828                
     173
     174                size_t i=0;
     175                for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
     176                        if(i == (size_t)(offset/8)) {
     177                                // the higher one
     178                                foo = data + i;
     179                                mask =  ((1 << (item->size-part_size))-1);
     180                                value = (*foo & mask) << part_size;
     181                        }
     182                        else if(i == ((offset+item->size-1)/8)){
     183                                // the lower one
     184                                foo = data + i;
     185                                mask =  ((1 << part_size)-1) << (8-part_size);
     186                                value += ((*foo & mask) >> (8-part_size));
     187                        }
     188                        else {
     189                                value = value << 8;
     190                                value += *(data + 1);
     191                        }
     192                }
    829193        }
    830194        else {         
     
    832196                mask =  ((1 << item->size)-1) << (8-((offset%8)+item->size));
    833197                value = (*foo & mask) >> (8-((offset%8)+item->size));
    834 
    835                 usb_log_debug2("offset %d\n", offset);
    836        
    837                 usb_log_debug2("foo %x\n", *foo);
    838                 usb_log_debug2("maska %x\n",  mask);
    839                 usb_log_debug2("val %d\n", value);                             
    840         }
    841 
    842         usb_log_debug2("---\n\n");
     198        }
     199
     200        if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
     201                value = USB_HID_UINT32_TO_INT32(value, item->size);
     202        }
    843203
    844204        return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
     
    847207
    848208/**
    849  *
    850  *
    851  * @param parser
    852  * @param path
    853  * @param flags
    854  * @return
    855  */
    856 size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
     209 * Returns number of items in input report which are accessible by given usage path
     210 *
     211 * @param parser Opaque report descriptor structure
     212 * @param path Usage path specification
     213 * @param flags Usage path comparison flags
     214 * @return Number of items in input report
     215 */
     216size_t usb_hid_report_input_length(const usb_hid_report_t *report,
    857217        usb_hid_report_path_t *path, int flags)
    858218{       
     219       
    859220        size_t ret = 0;
    860         link_t *item;
    861         usb_hid_report_item_t *report_item;
    862 
    863         if(parser == NULL) {
     221
     222        if(report == NULL) {
    864223                return 0;
    865224        }
    866        
    867         item = parser->input.next;
    868         while(&parser->input != item) {
    869                 report_item = list_get_instance(item, usb_hid_report_item_t, link);
    870                 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
    871                    (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
    872                         ret += report_item->count;
    873                 }
    874 
    875                 item = item->next;
    876         }
     225
     226        usb_hid_report_description_t *report_des;
     227        report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
     228        if(report_des == NULL) {
     229                return 0;
     230        }
     231
     232        link_t *field_it = report_des->report_items.next;
     233        usb_hid_report_field_t *field;
     234        while(field_it != &report_des->report_items) {
     235
     236                field = list_get_instance(field_it, usb_hid_report_field_t, link);
     237                if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
     238                       
     239                        usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
     240                        if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
     241                                ret++;
     242                        }
     243                        usb_hid_report_remove_last_item (field->collection_path);
     244                }
     245               
     246                field_it = field_it->next;
     247        }
    877248
    878249        return ret;
    879 }
    880 
    881 
    882 /**
    883  *
    884  * @param usage_path
    885  * @param usage_page
    886  * @param usage
    887  * @return
    888  */
    889 int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
    890                                     int32_t usage_page, int32_t usage)
    891 {       
    892         usb_hid_report_usage_path_t *item;
    893 
    894         if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
    895                 return ENOMEM;
    896         }
    897         list_initialize(&item->link);
    898 
    899         item->usage = usage;
    900         item->usage_page = usage_page;
    901        
    902         usb_log_debug("Appending usage %d, usage page %d\n", usage, usage_page);
    903        
    904         list_append (&usage_path->link, &item->link);
    905         usage_path->depth++;
    906         return EOK;
    907 }
    908 
    909 /**
    910  *
    911  * @param usage_path
    912  * @return
    913  */
    914 void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
    915 {
    916         usb_hid_report_usage_path_t *item;
    917        
    918         if(!list_empty(&usage_path->link)){
    919                 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);             
    920                 list_remove(usage_path->link.prev);
    921                 usage_path->depth--;
    922                 free(item);
    923         }
    924 }
    925 
    926 /**
    927  *
    928  * @param usage_path
    929  * @return
    930  */
    931 void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
    932 {
    933         usb_hid_report_usage_path_t *item;
    934        
    935         if(!list_empty(&usage_path->link)){     
    936                 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
    937                 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
    938         }
    939 }
    940 
    941 /**
    942  *
    943  * @param usage_path
    944  * @param tag
    945  * @param data
    946  * @return
    947  */
    948 void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
    949 {
    950         usb_hid_report_usage_path_t *item;
    951        
    952         if(!list_empty(&usage_path->link)){     
    953                 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
    954 
    955                 switch(tag) {
    956                         case USB_HID_TAG_CLASS_GLOBAL:
    957                                 item->usage_page = data;
    958                                 break;
    959                         case USB_HID_TAG_CLASS_LOCAL:
    960                                 item->usage = data;
    961                                 break;
    962                 }
    963         }
    964        
    965 }
    966 
    967 /**
    968  *
    969  *
    970  * @param report_path
    971  * @param path
    972  * @param flags
    973  * @return
    974  */
    975 int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
    976                                       usb_hid_report_path_t *path,
    977                                       int flags)
    978 {
    979         usb_hid_report_usage_path_t *report_item;
    980         usb_hid_report_usage_path_t *path_item;
    981 
    982         link_t *report_link;
    983         link_t *path_link;
    984 
    985         int only_page;
    986 
    987         if(report_path->report_id != path->report_id) {
    988                 return 1;
    989         }
    990 
    991         if(path->depth == 0){
    992                 return EOK;
    993         }
    994 
    995 
    996         if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
    997                 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
    998         }
    999        
    1000         switch(flags){
    1001                 /* path must be completly identical */
    1002                 case USB_HID_PATH_COMPARE_STRICT:
    1003                                 if(report_path->depth != path->depth){
    1004                                         return 1;
    1005                                 }
    1006 
    1007                                 report_link = report_path->link.next;
    1008                                 path_link = path->link.next;
    1009                        
    1010                                 while((report_link != &report_path->link) && (path_link != &path->link)) {
    1011                                         report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
    1012                                         path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);           
    1013 
    1014                                         if((report_item->usage_page != path_item->usage_page) ||
    1015                                            ((only_page == 0) && (report_item->usage != path_item->usage))) {
    1016                                                    return 1;
    1017                                         } else {
    1018                                                 report_link = report_link->next;
    1019                                                 path_link = path_link->next;                   
    1020                                         }
    1021                        
    1022                                 }
    1023 
    1024                                 if((report_link == &report_path->link) && (path_link == &path->link)) {
    1025                                         return EOK;
    1026                                 }
    1027                                 else {
    1028                                         return 1;
    1029                                 }                                               
    1030                         break;
    1031 
    1032                 /* compare with only the end of path*/
    1033                 case USB_HID_PATH_COMPARE_END:
    1034                                 report_link = report_path->link.prev;
    1035                                 path_link = path->link.prev;
    1036 
    1037                                 if(list_empty(&path->link)){
    1038                                         return EOK;
    1039                                 }
    1040                        
    1041                                 while((report_link != &report_path->link) && (path_link != &path->link)) {
    1042                                         report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
    1043                                         path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);           
    1044 
    1045                                         if((report_item->usage_page != path_item->usage_page) ||
    1046                                            ((only_page == 0) && (report_item->usage != path_item->usage))) {
    1047                                                    return 1;
    1048                                         } else {
    1049                                                 report_link = report_link->prev;
    1050                                                 path_link = path_link->prev;                   
    1051                                         }
    1052                        
    1053                                 }
    1054 
    1055                                 if(path_link == &path->link) {
    1056                                         return EOK;
    1057                                 }
    1058                                 else {
    1059                                         return 1;
    1060                                 }                                               
    1061                        
    1062                         break;
    1063 
    1064                 default:
    1065                         return EINVAL;
    1066         }
    1067        
    1068        
    1069        
    1070        
    1071 }
    1072 
    1073 /**
    1074  *
    1075  * @return
    1076  */
    1077 usb_hid_report_path_t *usb_hid_report_path(void)
    1078 {
    1079         usb_hid_report_path_t *path;
    1080         path = malloc(sizeof(usb_hid_report_path_t));
    1081         if(!path){
    1082                 return NULL;
    1083         }
    1084         else {
    1085                 path->depth = 0;
    1086                 path->report_id = 0;
    1087                 list_initialize(&path->link);
    1088                 return path;
    1089         }
    1090 }
    1091 
    1092 /**
    1093  *
    1094  * @param path
    1095  * @return void
    1096  */
    1097 void usb_hid_report_path_free(usb_hid_report_path_t *path)
    1098 {
    1099         while(!list_empty(&path->link)){
    1100                 usb_hid_report_remove_last_item(path);
    1101         }
    1102 }
    1103 
    1104 
    1105 /**
    1106  * Clone content of given usage path to the new one
    1107  *
    1108  * @param usage_path
    1109  * @return
    1110  */
    1111 usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
    1112 {
    1113         usb_hid_report_usage_path_t *path_item;
    1114         link_t *path_link;
    1115         usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
    1116 
    1117         if(new_usage_path == NULL){
    1118                 return NULL;
    1119         }
    1120        
    1121         if(list_empty(&usage_path->link)){
    1122                 return new_usage_path;
    1123         }
    1124 
    1125         path_link = usage_path->link.next;
    1126         while(path_link != &usage_path->link) {
    1127                 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
    1128                 usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage);
    1129 
    1130                 path_link = path_link->next;
    1131         }
    1132 
    1133         return new_usage_path;
    1134 }
    1135 
     250        }
    1136251
    1137252/*** OUTPUT API **/
    1138253
    1139 /** Allocates output report buffer
    1140  *
    1141  * @param parser
    1142  * @param size
    1143  * @return
    1144  */
    1145 uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)
    1146 {
    1147         if(parser == NULL) {
     254/**
     255 * Allocates output report buffer for output report
     256 *
     257 * @param parser Report parsed structure
     258 * @param size Size of returned buffer
     259 * @param report_id Report id of created output report
     260 * @return Returns allocated output buffer for specified output
     261 */
     262uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
     263{
     264        if(report == NULL) {
    1148265                *size = 0;
    1149266                return NULL;
    1150267        }
    1151        
    1152         // read the last output report item
    1153         usb_hid_report_item_t *last;
    1154         link_t *link;
    1155 
    1156         link = parser->output.prev;
    1157         if(link != &parser->output) {
    1158                 last = list_get_instance(link, usb_hid_report_item_t, link);
    1159                 *size = (last->offset + (last->size * last->count)) / 8;
    1160 
    1161                 uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));
    1162                 memset(buffer, 0, sizeof(uint8_t) * (*size));
    1163                 usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
    1164 
    1165                 return buffer;
     268
     269        link_t *report_it = report->reports.next;
     270        usb_hid_report_description_t *report_des = NULL;
     271        while(report_it != &report->reports) {
     272                report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
     273                if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
     274                        break;
     275                }
     276
     277                report_it = report_it->next;
     278        }
     279
     280        if(report_des == NULL){
     281                *size = 0;
     282                return NULL;
    1166283        }
    1167284        else {
    1168                 *size = 0;             
    1169                 return NULL;
     285                *size = (report_des->bit_length + (8 - 1))/8;
     286                uint8_t *ret = malloc((*size) * sizeof(uint8_t));
     287                memset(ret, 0, (*size) * sizeof(uint8_t));
     288                return ret;
    1170289        }
    1171290}
     
    1175294 *
    1176295 * @param output Output report buffer
    1177  * @return
     296 * @return void
    1178297 */
    1179298void usb_hid_report_output_free(uint8_t *output)
     
    1187306/** Returns size of output for given usage path
    1188307 *
    1189  * @param parser
    1190  * @param path
    1191  * @param flags
    1192  * @return
    1193  */
    1194 size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
     308 * @param parser Opaque report parser structure
     309 * @param path Usage path specified which items will be thought for the output
     310 * @param flags Flags of usage path structure comparison
     311 * @return Number of items matching the given usage path
     312 */
     313size_t usb_hid_report_output_size(usb_hid_report_t *report,
    1195314                                  usb_hid_report_path_t *path, int flags)
    1196315{
    1197         size_t ret = 0;
    1198         link_t *item;
    1199         usb_hid_report_item_t *report_item;
    1200 
    1201         if(parser == NULL) {
     316        size_t ret = 0;
     317        usb_hid_report_description_t *report_des;
     318
     319        if(report == NULL) {
    1202320                return 0;
    1203321        }
    1204322
    1205         item = parser->output.next;
    1206         while(&parser->output != item) {
    1207                 report_item = list_get_instance(item, usb_hid_report_item_t, link);
    1208                 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
    1209                    (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
    1210                         ret += report_item->count;
    1211                 }
    1212 
    1213                 item = item->next;
    1214         }
     323        report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
     324        if(report_des == NULL){
     325                return 0;
     326        }
     327       
     328        link_t *field_it = report_des->report_items.next;
     329        usb_hid_report_field_t *field;
     330        while(field_it != &report_des->report_items) {
     331
     332                field = list_get_instance(field_it, usb_hid_report_field_t, link);
     333                if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
     334                        usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
     335                        if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
     336                                ret++;
     337                        }
     338                        usb_hid_report_remove_last_item (field->collection_path);
     339                }
     340               
     341                field_it = field_it->next;
     342        }
    1215343
    1216344        return ret;
     
    1218346}
    1219347
    1220 /** Updates the output report buffer by translated given data
    1221  *
    1222  * @param parser
    1223  * @param path
    1224  * @param flags
    1225  * @param buffer
    1226  * @param size
    1227  * @param data
    1228  * @param data_size
    1229  * @return
    1230  */
    1231 int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
    1232                                     usb_hid_report_path_t *path, int flags,
    1233                                     uint8_t *buffer, size_t size,
    1234                                     int32_t *data, size_t data_size)
    1235 {
    1236         usb_hid_report_item_t *report_item;
     348/** Makes the output report buffer for data given in the report structure
     349 *
     350 * @param parser Opaque report parser structure
     351 * @param path Usage path specifing which parts of output will be set
     352 * @param flags Usage path structure comparison flags
     353 * @param buffer Output buffer
     354 * @param size Size of output buffer
     355 * @return Error code
     356 */
     357int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
     358                                    uint8_t *buffer, size_t size)
     359{
    1237360        link_t *item;   
    1238         size_t idx=0;
    1239         int i=0;
    1240361        int32_t value=0;
    1241362        int offset;
    1242363        int length;
    1243364        int32_t tmp_value;
    1244         size_t offset_prefix = 0;
    1245        
    1246         if(parser == NULL) {
     365       
     366        if(report == NULL) {
    1247367                return EINVAL;
    1248368        }
    1249369
    1250         if(parser->use_report_id != 0) {
    1251                 buffer[0] = path->report_id;
    1252                 offset_prefix = 8;
     370        if(report->use_report_ids != 0) {
     371                buffer[0] = report_id;         
    1253372        }
    1254373
    1255374        usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
    1256         usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);
    1257 
    1258         item = parser->output.next;     
    1259         while(item != &parser->output) {
    1260                 report_item = list_get_instance(item, usb_hid_report_item_t, link);
    1261 
    1262                 for(i=0; i<report_item->count; i++) {
    1263 
    1264                         if(idx >= data_size) {
    1265                                 break;
    1266                         }
    1267 
    1268                         if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
    1269                                 ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
     375       
     376        usb_hid_report_description_t *report_des;
     377        report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
     378        if(report_des == NULL){
     379                return EINVAL;
     380        }
     381
     382        usb_hid_report_field_t *report_item;   
     383        item = report_des->report_items.next;   
     384        while(item != &report_des->report_items) {
     385                report_item = list_get_instance(item, usb_hid_report_field_t, link);
     386
     387                        if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
    1270388                                       
    1271 //                              // variable item
    1272                                 value = usb_hid_translate_data_reverse(report_item, data[idx++]);
    1273                                 offset = report_item->offset + (i * report_item->size) + offset_prefix;
     389                                // array
     390                                value = usb_hid_translate_data_reverse(report_item, report_item->value);
     391                                offset = report_item->offset;
    1274392                                length = report_item->size;
    1275393                        }
    1276394                        else {
    1277                                 //bitmap
    1278                                 value += usb_hid_translate_data_reverse(report_item, data[idx++]);
    1279                                 offset = report_item->offset + offset_prefix;
    1280                                 length = report_item->size * report_item->count;
     395                                // variable item
     396                                value  = usb_hid_translate_data_reverse(report_item, report_item->value);
     397                                offset = report_item->offset;
     398                                length = report_item->size;
    1281399                        }
    1282400
     
    1297415                        }
    1298416                        else {
    1299                                 // je to ve dvou!! FIXME: melo by to umet delsi jak 2
    1300 
    1301                                 // konec prvniho -- dolni x bitu
    1302                                 tmp_value = value;
    1303                                 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);                             
    1304                                 tmp_value = tmp_value << (offset%8);
    1305 
     417                                int i = 0;
    1306418                                uint8_t mask = 0;
    1307                                 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
    1308                                 buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;
    1309 
    1310                                 // a ted druhej -- hornich length-x bitu
    1311                                 value = value >> (8 - (offset % 8));
    1312                                 value = value & ((1 << (length - (8 - (offset % 8)))) - 1);
     419                                for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
     420                                        if(i == (offset/8)) {
     421                                                tmp_value = value;
     422                                                tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);                             
     423                                                tmp_value = tmp_value << (offset%8);
     424       
     425                                                mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
     426                                                buffer[i] = (buffer[i] & mask) | tmp_value;                     
     427                                        }
     428                                        else if (i == ((offset + length -1)/8)) {
     429                                               
     430                                                value = value >> (length - ((offset + length) % 8));
     431                                                value = value & ((1 << (length - ((offset + length) % 8))) - 1);
    1313432                               
    1314                                 mask = ((1 << (length - (8 - (offset % 8)))) - 1);
    1315                                 buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;
    1316                         }
    1317 
    1318                 }
    1319 
     433                                                mask = (1 << (length - ((offset + length) % 8))) - 1;
     434                                                buffer[i] = (buffer[i] & mask) | value;
     435                                        }
     436                                        else {
     437                                                buffer[i] = value & (0xFF << i);
     438                                        }
     439                                }
     440                        }
     441
     442
     443                // reset value
     444                report_item->value = 0;
     445               
    1320446                item = item->next;
    1321447        }
    1322 
     448       
    1323449        usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
    1324450
     
    1327453
    1328454/**
    1329  *
    1330  * @param item
    1331  * @param value
    1332  * @return
    1333  */
    1334 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
     455 * Translate given data for putting them into the outoput report
     456 * @param item Report item structure
     457 * @param value Value to translate
     458 * @return ranslated value
     459 */
     460uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
    1335461{
    1336462        int ret=0;
     
    1341467        }
    1342468
     469        if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
     470                item->physical_minimum = item->logical_minimum;
     471                item->physical_maximum = item->logical_maximum;                 
     472        }
     473       
     474
    1343475        if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
    1344476
    1345477                // variable item
    1346                 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
    1347                         item->physical_minimum = item->logical_minimum;
    1348                         item->physical_maximum = item->logical_maximum;
    1349                 }
    1350 
    1351478                if(item->physical_maximum == item->physical_minimum){
    1352479                    resolution = 1;
     
    1371498        }
    1372499
    1373 
    1374         return ret;
    1375 }
    1376 
    1377 
    1378 int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
    1379 {
    1380         if(path == NULL){
    1381                 return EINVAL;
    1382         }
    1383 
    1384         path->report_id = report_id;
    1385         return EOK;
    1386 }
    1387 
     500        if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
     501                return USB_HID_INT32_TO_UINT32(ret, item->size);
     502        }
     503        return (int32_t)ret;
     504}
    1388505
    1389506usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
     
    1400517}
    1401518
     519
     520usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
     521                                                        usb_hid_report_field_t *field,
     522                            usb_hid_report_path_t *path, int flags,
     523                            usb_hid_report_type_t type)
     524{
     525        usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
     526        link_t *field_it;
     527       
     528        if(report_des == NULL){
     529                return NULL;
     530        }
     531
     532        if(field == NULL){
     533                // vezmu prvni co mathuje podle path!!
     534                field_it = report_des->report_items.next;
     535        }
     536        else {
     537                field_it = field->link.next;
     538        }
     539
     540        while(field_it != &report_des->report_items) {
     541                field = list_get_instance(field_it, usb_hid_report_field_t, link);
     542
     543                if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
     544                        usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
     545                        if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
     546                                usb_hid_report_remove_last_item (field->collection_path);
     547                                return field;
     548                        }
     549                        usb_hid_report_remove_last_item (field->collection_path);
     550                }
     551                field_it = field_it->next;
     552        }
     553
     554        return NULL;
     555}
     556
     557uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
     558{
     559        if(report == NULL){
     560                return 0;
     561        }
     562
     563        usb_hid_report_description_t *report_des;
     564        link_t *report_it;
     565       
     566        if(report_id == 0) {
     567                report_it = usb_hid_report_find_description (report, report_id, type)->link.next;               
     568        }
     569        else {
     570                report_it = report->reports.next;
     571        }
     572
     573        while(report_it != &report->reports) {
     574                report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
     575                if(report_des->type == type){
     576                        return report_des->report_id;
     577                }
     578        }
     579
     580        return 0;
     581}
     582
     583void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
     584{
     585        if(report_item == NULL) {
     586                return;
     587        }
     588       
     589        report_item->usages_count = 0;
     590        memset(report_item->usages, 0, USB_HID_MAX_USAGES);
     591       
     592        report_item->extended_usage_page = 0;
     593        report_item->usage_minimum = 0;
     594        report_item->usage_maximum = 0;
     595        report_item->designator_index = 0;
     596        report_item->designator_minimum = 0;
     597        report_item->designator_maximum = 0;
     598        report_item->string_index = 0;
     599        report_item->string_minimum = 0;
     600        report_item->string_maximum = 0;
     601
     602        return;
     603}
    1402604/**
    1403605 * @}
  • uspace/lib/usb/src/hidreport.c

    ra58dd620 r3d4aa055  
    164164
    165165int usb_hid_process_report_descriptor(usb_device_t *dev,
    166     usb_hid_report_parser_t *parser)
     166    usb_hid_report_t *report)
    167167{
    168         if (dev == NULL || parser == NULL) {
     168        if (dev == NULL || report == NULL) {
    169169                usb_log_error("Failed to process Report descriptor: wrong "
    170170                    "parameters given.\n");
     
    189189        assert(report_desc != NULL);
    190190       
    191         rc = usb_hid_parse_report_descriptor(parser, report_desc, report_size);
     191        rc = usb_hid_parse_report_descriptor(report, report_desc, report_size);
    192192        if (rc != EOK) {
    193193                usb_log_error("Problem parsing Report descriptor: %s.\n",
     
    197197        }
    198198       
    199         usb_hid_descriptor_print(parser);
     199        usb_hid_descriptor_print(report);
    200200        free(report_desc);
    201201       
  • uspace/lib/usbvirt/Makefile

    ra58dd620 r3d4aa055  
    11#
    2 # Copyright (c) 2010 Vojtech Horky
     2# Copyright (c) 2011 Vojtech Horky
    33# All rights reserved.
    44#
     
    3333
    3434SOURCES = \
    35         src/callback.c \
    36         src/ctrlpipe.c \
    37         src/debug.c \
    38         src/main.c \
     35        src/ipc.c \
     36        src/ctrltransfer.c \
    3937        src/stdreq.c \
    40         src/transaction.c
     38        src/transfer.c
    4139
    4240include $(USPACE_PREFIX)/Makefile.common
  • uspace/lib/usbvirt/include/usbvirt/device.h

    ra58dd620 r3d4aa055  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2011 Vojtech Horky
    33 * All rights reserved.
    44 *
     
    3838#include <usb/usb.h>
    3939#include <usb/request.h>
    40 #include <usb/descriptor.h>
    4140
    42 /** Request type of a control transfer. */
    43 typedef enum {
    44         /** Standard USB request. */
    45         USBVIRT_REQUEST_TYPE_STANDARD = 0,
    46         /** Standard class USB request. */
    47         USBVIRT_REQUEST_TYPE_CLASS = 1
    48 } usbvirt_request_type_t;
    49 
    50 /** Recipient of control request. */
    51 typedef enum {
    52         /** Device is the recipient of the control request. */
    53         USBVIRT_REQUEST_RECIPIENT_DEVICE = 0,
    54         /** Interface is the recipient of the control request. */
    55         USBVIRT_REQUEST_RECIPIENT_INTERFACE = 1,
    56         /** Endpoint is the recipient of the control request. */
    57         USBVIRT_REQUEST_RECIPIENT_ENDPOINT = 2,
    58         /** Other part of the device is the recipient of the control request. */
    59         USBVIRT_REQUEST_RECIPIENT_OTHER = 3
    60 } usbvirt_request_recipient_t;
    61 
    62 /** Possible states of virtual USB device.
    63  * Notice that these are not 1:1 mappings to those in USB specification.
    64  */
    65 typedef enum {
    66         /** Default state, device listens at default address. */
    67         USBVIRT_STATE_DEFAULT,
    68         /** Device has non-default address assigned. */
    69         USBVIRT_STATE_ADDRESS,
    70         /** Device is configured. */
    71         USBVIRT_STATE_CONFIGURED
    72 } usbvirt_device_state_t;
     41#define USBVIRT_ENDPOINT_MAX 16
    7342
    7443typedef struct usbvirt_device usbvirt_device_t;
    75 struct usbvirt_control_transfer;
    7644
    77 typedef int (*usbvirt_on_device_request_t)(usbvirt_device_t *dev,
    78         usb_device_request_setup_packet_t *request,
    79         uint8_t *data);
     45typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *, usb_endpoint_t,
     46    usb_transfer_type_t, void *, size_t);
     47typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *, usb_endpoint_t,
     48    usb_transfer_type_t, void *, size_t, size_t *);
     49typedef int (*usbvirt_on_control_t)(usbvirt_device_t *,
     50    const usb_device_request_setup_packet_t *, uint8_t *, size_t *);
    8051
    81 /** Callback for control request over pipe zero.
    82  *
    83  * @param dev Virtual device answering the call.
    84  * @param request Request setup packet.
    85  * @param data Data when DATA stage is present.
    86  * @return Error code.
    87  */
    88 typedef int (*usbvirt_control_request_callback_t)(usbvirt_device_t *dev,
    89         usb_device_request_setup_packet_t *request,
    90         uint8_t *data);
    91 
    92 /** Handler for control transfer on endpoint zero. */
    9352typedef struct {
    94         /** Request type bitmap.
    95          * Use USBVIRT_MAKE_CONTROL_REQUEST_TYPE for creating the bitmap.
    96          */
    97         uint8_t request_type;
    98         /** Request code. */
     53        usb_direction_t req_direction;
     54        usb_request_recipient_t req_recipient;
     55        usb_request_type_t req_type;
    9956        uint8_t request;
    100         /** Request name for debugging. */
    10157        const char *name;
    102         /** Callback for the request.
    103          * NULL value here announces end of a list.
    104          */
    105         usbvirt_control_request_callback_t callback;
    106 } usbvirt_control_transfer_handler_t;
    107 
    108 /** Create control request type bitmap.
    109  *
    110  * @param direction Transfer direction (use usb_direction_t).
    111  * @param type Request type (use usbvirt_request_type_t).
    112  * @param recipient Recipient of the request (use usbvirt_request_recipient_t).
    113  * @return Request type bitmap.
    114  */
    115 #define USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, type, recipient) \
    116         ((((direction) == USB_DIRECTION_IN) ? 1 : 0) << 7) \
    117         | (((type) & 3) << 5) \
    118         | (((recipient) & 31))
    119 
    120 /** Create last item in an array of control request handlers. */
    121 #define USBVIRT_CONTROL_TRANSFER_HANDLER_LAST { 0, 0, NULL, NULL }
    122 
    123 /** Device operations. */
    124 typedef struct {
    125         /** Callbacks for transfers over control pipe zero. */
    126         usbvirt_control_transfer_handler_t *control_transfer_handlers;
    127 
    128         int (*on_control_transfer)(usbvirt_device_t *dev,
    129             usb_endpoint_t endpoint, struct usbvirt_control_transfer *transfer);
    130        
    131         /** Callback for all other incoming data. */
    132         int (*on_data)(usbvirt_device_t *dev,
    133             usb_endpoint_t endpoint, void *buffer, size_t size);
    134        
    135         /** Callback for host request for data. */
    136         int (*on_data_request)(usbvirt_device_t *dev,
    137             usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size);
    138        
    139         /** Decides direction of control transfer. */
    140         usb_direction_t (*decide_control_transfer_direction)(
    141             usb_endpoint_t endpoint, void *buffer, size_t size);
    142 
    143         /** Callback when device changes its state.
    144          *
    145          * It is correct that this function is called when both states
    146          * are equal (e.g. this function is called during SET_CONFIGURATION
    147          * request done on already configured device).
    148          *
    149          * @warning The value of <code>dev->state</code> before calling
    150          * this function is not specified (i.e. can be @p old_state or
    151          * @p new_state).
    152          */
    153         void (*on_state_change)(usbvirt_device_t *dev,
    154             usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
    155 } usbvirt_device_ops_t;
     58        usbvirt_on_control_t callback;
     59} usbvirt_control_request_handler_t;
    15660
    15761/** Extra configuration data for GET_CONFIGURATION request. */
     
    17983         */
    18084        usb_standard_device_descriptor_t *device;
    181        
     85
    18286        /** Configurations. */
    18387        usbvirt_device_configuration_t *configuration;
    18488        /** Number of configurations. */
    18589        size_t configuration_count;
    186         /** Index of currently selected configuration. */
    187         uint8_t current_configuration;
    18890} usbvirt_descriptors_t;
    18991
    190 /** Information about on-going control transfer.
     92/** Possible states of virtual USB device.
     93 * Notice that these are not 1:1 mappings to those in USB specification.
    19194 */
    192 typedef struct usbvirt_control_transfer {
    193         /** Transfer direction (read/write control transfer). */
    194         usb_direction_t direction;
    195         /** Request data. */
    196         void *request;
    197         /** Size of request data. */
    198         size_t request_size;
    199         /** Payload. */
    200         void *data;
    201         /** Size of payload. */
    202         size_t data_size;
    203 } usbvirt_control_transfer_t;
     95typedef enum {
     96        /** Default state, device listens at default address. */
     97        USBVIRT_STATE_DEFAULT,
     98        /** Device has non-default address assigned. */
     99        USBVIRT_STATE_ADDRESS,
     100        /** Device is configured. */
     101        USBVIRT_STATE_CONFIGURED
     102} usbvirt_device_state_t;
    204103
    205 typedef enum {
    206         USBVIRT_DEBUGTAG_BASE = 1,
    207         USBVIRT_DEBUGTAG_TRANSACTION = 2,
    208         USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO = 4,
    209         USBVIRT_DEBUGTAG_ALL = 255
    210 } usbvirt_debug_tags_t;
     104typedef struct {
     105        usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
     106        usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
     107        usbvirt_control_request_handler_t *control;
     108        void (*state_changed)(usbvirt_device_t *dev,
     109            usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
     110} usbvirt_device_ops_t;
    211111
    212 /** Virtual USB device. */
    213112struct usbvirt_device {
    214         /** Callback device operations. */
     113        const char *name;
     114        void *device_data;
    215115        usbvirt_device_ops_t *ops;
    216        
    217         /** Custom device data. */
    218         void *device_data;
     116        usbvirt_descriptors_t *descriptors;
     117        usb_address_t address;
     118        usbvirt_device_state_t state;
     119};
    219120
    220         /** Reply onto control transfer.
    221          */
    222         int (*control_transfer_reply)(usbvirt_device_t *dev,
    223             usb_endpoint_t endpoint, void *buffer, size_t size);
    224        
    225         /** Device name.
    226          * Used in debug prints and sent to virtual host controller.
    227          */
    228         const char *name;
    229        
    230         /** Standard descriptors. */
    231         usbvirt_descriptors_t *descriptors;
    232        
    233         /** Current device state. */
    234         usbvirt_device_state_t state;
    235        
    236         /** Device address. */
    237         usb_address_t address;
    238         /** New device address.
    239          * This field is used during SET_ADDRESS request.
    240          * On all other occasions, it holds invalid address (e.g. -1).
    241          */
    242         usb_address_t new_address;
    243        
    244         /** Process OUT transaction. */
    245         int (*transaction_out)(usbvirt_device_t *dev,
    246             usb_endpoint_t endpoint, void *buffer, size_t size);
    247         /** Process SETUP transaction. */
    248         int (*transaction_setup)(usbvirt_device_t *dev,
    249             usb_endpoint_t endpoint, void *buffer, size_t size);
    250         /** Process IN transaction. */
    251         int (*transaction_in)(usbvirt_device_t *dev,
    252             usb_endpoint_t endpoint, void *buffer, size_t size, size_t *data_size);
    253        
    254         /** State information on control-transfer endpoints. */
    255         usbvirt_control_transfer_t current_control_transfers[USB11_ENDPOINT_MAX];
    256        
    257         /* User debugging. */
    258        
    259         /** Debug print. */
    260         void (*debug)(usbvirt_device_t *dev, int level, uint8_t tag,
    261             const char *format, ...);
    262        
    263         /** Current debug level. */
    264         int debug_level;
    265        
    266         /** Bitmap of currently enabled tags. */
    267         uint8_t debug_enabled_tags;
    268        
    269         /* Library debugging. */
    270        
    271         /** Debug print. */
    272         void (*lib_debug)(usbvirt_device_t *dev, int level, uint8_t tag,
    273             const char *format, ...);
    274        
    275         /** Current debug level. */
    276         int lib_debug_level;
    277        
    278         /** Bitmap of currently enabled tags. */
    279         uint8_t lib_debug_enabled_tags;
    280 };
     121int usbvirt_device_plug(usbvirt_device_t *, const char *);
     122
     123void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
     124    uint8_t *, size_t *, void *, size_t);
     125
     126int usbvirt_control_write(usbvirt_device_t *, void *, size_t, void *, size_t);
     127int usbvirt_control_read(usbvirt_device_t *, void *, size_t, void *, size_t, size_t *);
     128int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
     129    void *, size_t);
     130int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
     131    void *, size_t, size_t *);
     132
    281133
    282134#endif
  • uspace/lib/usbvirt/include/usbvirt/ipc.h

    ra58dd620 r3d4aa055  
    3333 * @brief Virtual USB device.
    3434 */
    35 #ifndef LIBUSBVIRT_HUB_H_
    36 #define LIBUSBVIRT_HUB_H_
     35#ifndef LIBUSBVIRT_IPC_H_
     36#define LIBUSBVIRT_IPC_H_
    3737
    38 #include "device.h"
     38#include <ipc/common.h>
     39#include <usb/usb.h>
     40#include <bool.h>
    3941
    40 /** USB transaction type.
    41  * This types does not correspond directly to types in USB specification,
    42  * as actually DATA transactions are marked with these types to identify
    43  * their direction (and tag).
    44  */
    4542typedef enum {
    46         USBVIRT_TRANSACTION_SETUP,
    47         USBVIRT_TRANSACTION_IN,
    48         USBVIRT_TRANSACTION_OUT
    49 } usbvirt_transaction_type_t;
     43        IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD + 80,
     44        IPC_M_USBVIRT_CONTROL_READ,
     45        IPC_M_USBVIRT_CONTROL_WRITE,
     46        IPC_M_USBVIRT_INTERRUPT_IN,
     47        IPC_M_USBVIRT_INTERRUPT_OUT
     48} usbvirt_ipc_t;
    5049
    51 const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type);
     50int usbvirt_ipc_send_control_read(int, usb_endpoint_t, void *, size_t,
     51    void *, size_t, size_t *);
     52int usbvirt_ipc_send_control_write(int, usb_endpoint_t, void *, size_t,
     53    void *, size_t);
     54int usbvirt_ipc_send_data_in(int, usb_endpoint_t, usb_transfer_type_t,
     55    void *, size_t, size_t *);
     56int usbvirt_ipc_send_data_out(int, usb_endpoint_t, usb_transfer_type_t,
     57    void *, size_t);
    5258
    53 /** Telephony methods of virtual devices. */
    54 typedef enum {
    55         IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD,
    56         IPC_M_USBVIRT_TRANSACTION_SETUP,
    57         IPC_M_USBVIRT_TRANSACTION_OUT,
    58         IPC_M_USBVIRT_TRANSACTION_IN,
    59 } usbvirt_device_method_t;
    60 
    61 int usbvirt_connect(usbvirt_device_t *);
    62 int usbvirt_connect_local(usbvirt_device_t *);
    63 int usbvirt_disconnect(usbvirt_device_t *dev);
     59bool usbvirt_is_usbvirt_method(sysarg_t);
     60bool usbvirt_ipc_handle_call(usbvirt_device_t *, ipc_callid_t, ipc_call_t *);
    6461
    6562#endif
  • uspace/lib/usbvirt/src/private.h

    ra58dd620 r3d4aa055  
    1 /*
    2  * Copyright (c) 2010 Vojtech Horky
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  *
    9  * - Redistributions of source code must retain the above copyright
    10  *   notice, this list of conditions and the following disclaimer.
    11  * - Redistributions in binary form must reproduce the above copyright
    12  *   notice, this list of conditions and the following disclaimer in the
    13  *   documentation and/or other materials provided with the distribution.
    14  * - The name of the author may not be used to endorse or promote products
    15  *   derived from this software without specific prior written permission.
    16  *
    17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    27  */
     1#include <usbvirt/device.h>
    282
    29 /** @addtogroup libusbvirt
    30  * @{
    31  */
    32 /** @file
    33  * @brief Virtual USB private header.
    34  */
    35 #ifndef LIBUSBVIRT_PRIVATE_H_
    36 #define LIBUSBVIRT_PRIVATE_H_
     3int process_control_transfer(usbvirt_device_t *,
     4    usbvirt_control_request_handler_t *,
     5    usb_device_request_setup_packet_t *,
     6    uint8_t *, size_t *);
    377
    38 #include <usbvirt/device.h>
    39 #include <usbvirt/hub.h>
    40 #include <assert.h>
    41 
    42 
    43 #define DEVICE_HAS_OP(dev, op) \
    44         ( \
    45                 (  ((dev)->ops) != NULL  ) \
    46                 && \
    47                 (  ((dev)->ops->op) != NULL  ) \
    48         )
    49 
    50 int usbvirt_data_to_host(struct usbvirt_device *dev,
    51     usb_endpoint_t endpoint, void *buffer, size_t size);
    52 
    53 int handle_incoming_data(struct usbvirt_device *dev,
    54     usb_endpoint_t endpoint, void *buffer, size_t size);
    55 
    56 int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer);
    57 
    58 int handle_std_request(usbvirt_device_t *device, usb_device_request_setup_packet_t *request, uint8_t *data);
    59 
    60 void device_callback_connection(usbvirt_device_t *device, ipc_callid_t iid, ipc_call_t *icall);
    61 
    62 int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
    63     void *buffer, size_t size);
    64 int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
    65     void *buffer, size_t size);
    66 int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
    67     void *buffer, size_t size, size_t *data_size);
    68 
    69 
    70 void user_debug(usbvirt_device_t *device, int level, uint8_t tag,
    71     const char *format, ...);
    72 void lib_debug(usbvirt_device_t *device, int level, uint8_t tag,
    73     const char *format, ...);
    74    
    75 static inline const char *str_device_state(usbvirt_device_state_t state)
    76 {
    77         switch (state) {
    78                 case USBVIRT_STATE_DEFAULT:
    79                         return "default";
    80                 case USBVIRT_STATE_ADDRESS:
    81                         return "address";
    82                 case USBVIRT_STATE_CONFIGURED:
    83                         return "configured";
    84                 default:
    85                         return "unknown";
    86         }
    87 }
    88 
    89 extern usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[];
    90 
    91 #endif
    92 /**
    93  * @}
    94  */
     8extern usbvirt_control_request_handler_t library_handlers[];
  • uspace/lib/usbvirt/src/stdreq.c

    ra58dd620 r3d4aa055  
    1 /*
    2  * Copyright (c) 2010 Vojtech Horky
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  *
    9  * - Redistributions of source code must retain the above copyright
    10  *   notice, this list of conditions and the following disclaimer.
    11  * - Redistributions in binary form must reproduce the above copyright
    12  *   notice, this list of conditions and the following disclaimer in the
    13  *   documentation and/or other materials provided with the distribution.
    14  * - The name of the author may not be used to endorse or promote products
    15  *   derived from this software without specific prior written permission.
    16  *
    17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    27  */
     1#include "private.h"
     2#include <usb/request.h>
     3#include <assert.h>
     4#include <errno.h>
    285
    29 /** @addtogroup libusbvirt
    30  * @{
    31  */
    32 /** @file
    33  * @brief Preprocessing of standard device requests.
    34  */
    35 #include <errno.h>
    36 #include <stdlib.h>
    37 #include <mem.h>
    38 #include <usb/request.h>
     6void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
     7    uint8_t *data, size_t *act_size,
     8    void *actual_data, size_t actual_data_size)
     9{
     10        size_t expected_size = setup_packet->length;
     11        if (expected_size < actual_data_size) {
     12                actual_data_size = expected_size;
     13        }
    3914
    40 #include "private.h"
     15        memcpy(data, actual_data, actual_data_size);
    4116
    42 /*
    43  * All sub handlers must return EFORWARD to inform the caller that
    44  * they were not able to process the request (yes, it is abuse of
    45  * this error code but such error code shall not collide with anything
    46  * else in this context).
    47  */
    48  
     17        if (act_size != NULL) {
     18                *act_size = actual_data_size;
     19        }
     20}
     21
    4922/** GET_DESCRIPTOR handler. */
    50 static int handle_get_descriptor(usbvirt_device_t *device,
    51     usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
     23static int req_get_descriptor(usbvirt_device_t *device,
     24    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
    5225{
    5326        uint8_t type = setup_packet->value_high;
    5427        uint8_t index = setup_packet->value_low;
    5528
    56         /* 
     29        /*
    5730         * Standard device descriptor.
    5831         */
    5932        if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
    6033                if (device->descriptors && device->descriptors->device) {
    61                         return device->control_transfer_reply(device, 0,
     34                        usbvirt_control_reply_helper(setup_packet, data, act_size,
    6235                            device->descriptors->device,
    6336                            device->descriptors->device->length);
     37                        return EOK;
    6438                } else {
    6539                        return EFORWARD;
    6640                }
    6741        }
    68        
     42
    6943        /*
    7044         * Configuration descriptor together with interface, endpoint and
     
    8559                        return ENOMEM;
    8660                }
    87                
     61
    8862                uint8_t *ptr = all_data;
    8963                memcpy(ptr, config->descriptor, config->descriptor->length);
     
    9670                        ptr += extra->length;
    9771                }
    98                
    99                 int rc = device->control_transfer_reply(device, 0,
     72
     73                usbvirt_control_reply_helper(setup_packet, data, act_size,
    10074                    all_data, config->descriptor->total_length);
    101                
     75
    10276                free(all_data);
    103                
    104                 return rc;
     77
     78                return EOK;
    10579        }
    106        
     80
    10781        return EFORWARD;
    10882}
    10983
    110 /** SET_ADDRESS handler. */
    111 static int handle_set_address(usbvirt_device_t *device,
    112     usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
     84static int req_set_address(usbvirt_device_t *device,
     85    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
    11386{
    11487        uint16_t new_address = setup_packet->value;
     
    11992                return EINVAL;
    12093        }
    121        
     94
    12295        if (new_address > 127) {
    12396                return EINVAL;
    12497        }
    125        
    126         device->new_address = new_address;
    127        
     98
     99        device->address = new_address;
     100
    128101        return EOK;
    129102}
    130103
    131 /** SET_CONFIGURATION handler. */
    132 static int handle_set_configuration(usbvirt_device_t *device,
    133     usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
     104static int req_set_configuration(usbvirt_device_t *device,
     105    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
    134106{
    135107        uint16_t configuration_value = setup_packet->value;
     
    140112                return EINVAL;
    141113        }
    142        
     114
    143115        /*
    144116         * Configuration value is 1 byte information.
     
    147119                return EINVAL;
    148120        }
    149        
     121
    150122        /*
    151123         * Do nothing when in default state. According to specification,
     
    155127                return EOK;
    156128        }
    157        
     129
     130        usbvirt_device_state_t new_state;
    158131        if (configuration_value == 0) {
    159                 if (DEVICE_HAS_OP(device, on_state_change)) {
    160                         device->ops->on_state_change(device, device->state,
    161                             USBVIRT_STATE_ADDRESS);
    162                 }
    163                 device->state = USBVIRT_STATE_ADDRESS;
     132                new_state = USBVIRT_STATE_ADDRESS;
    164133        } else {
    165                 /*
    166                 * TODO: browse provided configurations and verify that
    167                 * user selected existing configuration.
    168                 */
    169                 if (DEVICE_HAS_OP(device, on_state_change)) {
    170                         device->ops->on_state_change(device, device->state,
    171                             USBVIRT_STATE_CONFIGURED);
    172                 }
    173                 device->state = USBVIRT_STATE_CONFIGURED;
    174                 if (device->descriptors) {
    175                         device->descriptors->current_configuration
    176                             = configuration_value;
    177                 }
     134                // FIXME: check that this configuration exists
     135                new_state = USBVIRT_STATE_CONFIGURED;
    178136        }
    179                
     137
     138        if (device->ops && device->ops->state_changed) {
     139                device->ops->state_changed(device, device->state, new_state);
     140        }
     141        device->state = new_state;
     142
    180143        return EOK;
    181144}
    182145
    183 
    184 #define MAKE_BM_REQUEST(direction, recipient) \
    185         USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
    186             USBVIRT_REQUEST_TYPE_STANDARD, recipient)
    187 #define MAKE_BM_REQUEST_DEV(direction) \
    188         MAKE_BM_REQUEST(direction, USBVIRT_REQUEST_RECIPIENT_DEVICE)
    189 
    190 usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[] = {
     146usbvirt_control_request_handler_t library_handlers[] = {
    191147        {
    192                 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_IN),
    193                 .request = USB_DEVREQ_GET_DESCRIPTOR,
    194                 .name = "GetDescriptor()",
    195                 .callback = handle_get_descriptor
     148                .req_direction = USB_DIRECTION_OUT,
     149                .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
     150                .req_type = USB_REQUEST_TYPE_STANDARD,
     151                .request = USB_DEVREQ_SET_ADDRESS,
     152                .name = "SetAddress",
     153                .callback = req_set_address
    196154        },
    197155        {
    198                 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
    199                 .request = USB_DEVREQ_SET_ADDRESS,
    200                 .name = "SetAddress()",
    201                 .callback = handle_set_address
     156                .req_direction = USB_DIRECTION_IN,
     157                .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
     158                .req_type = USB_REQUEST_TYPE_STANDARD,
     159                .request = USB_DEVREQ_GET_DESCRIPTOR,
     160                .name = "GetDescriptor",
     161                .callback = req_get_descriptor
    202162        },
    203163        {
    204                 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
     164                .req_direction = USB_DIRECTION_OUT,
     165                .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
     166                .req_type = USB_REQUEST_TYPE_STANDARD,
    205167                .request = USB_DEVREQ_SET_CONFIGURATION,
    206                 .name = "SetConfiguration()",
    207                 .callback = handle_set_configuration
     168                .name = "SetConfiguration",
     169                .callback = req_set_configuration
    208170        },
    209         USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
     171
     172        { .callback = NULL }
    210173};
    211174
    212 /**
    213  * @}
    214  */
Note: See TracChangeset for help on using the changeset viewer.