Index: uspace/lib/c/include/errno.h
===================================================================
--- uspace/lib/c/include/errno.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/c/include/errno.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -65,4 +65,7 @@
 #define EEMPTY (-302)
 
+/** Negative acknowledgment. */
+#define ENAK (-303)
+
 /** An API function is called while another blocking function is in progress. */
 #define EINPROGRESS  (-10036)
Index: uspace/lib/drv/include/usbhid_iface.h
===================================================================
--- uspace/lib/drv/include/usbhid_iface.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/drv/include/usbhid_iface.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -72,24 +72,21 @@
 /** USB HID device communication interface. */
 typedef struct {
-	/** Get number of items in the event.
+	/** Get size of the event in bytes.
 	 *
 	 * @param[in] fun DDF function answering the request.
 	 * @return Number of events or error code.
 	 */
-	int (*get_event_length)(ddf_fun_t *fun);
+	size_t (*get_event_length)(ddf_fun_t *fun);
 
 	/** Get single event from the HID device.
 	 *
 	 * @param[in] fun DDF function answering the request.
-	 * @param[out] usage_page Array of usage pages and usages.
-	 * @param[out] usage Array of data (1:1 with @p usage).
-	 * @param[in] size Size of @p usage and @p data arrays.
+	 * @param[out] buffer Buffer with raw data from the device.
 	 * @param[out] act_size Actual number of returned events.
 	 * @param[in] flags Flags (see USBHID_IFACE_FLAG_*).
 	 * @return Error code.
 	 */
-	int (*get_event)(ddf_fun_t *fun,
-	    uint16_t *usage_page, uint16_t *usage, size_t size, size_t *act_size,
-	    unsigned int flags);
+	int (*get_event)(ddf_fun_t *fun, int32_t *buffer, size_t size,
+	    size_t *act_size, unsigned int flags);
 } usbhid_iface_t;
 
Index: uspace/lib/usb/Makefile
===================================================================
--- uspace/lib/usb/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/Makefile	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -43,5 +43,7 @@
 	src/dump.c \
 	src/hidiface.c \
+	src/hidpath.c \
 	src/hidparser.c \
+	src/hiddescriptor.c \
 	src/hub.c \
 	src/pipepriv.c \
Index: uspace/lib/usb/include/usb/classes/hid.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hid.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/classes/hid.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -50,10 +50,4 @@
 	USB_HIDREQ_SET_PROTOCOL = 11
 } usb_hid_request_t;
-
-typedef enum {
-	USB_HID_REPORT_TYPE_INPUT = 1,
-	USB_HID_REPORT_TYPE_OUTPUT = 2,
-	USB_HID_REPORT_TYPE_FEATURE = 3
-} usb_hid_report_type_t;
 
 typedef enum {
Index: uspace/lib/usb/include/usb/classes/hid_report_items.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hid_report_items.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/classes/hid_report_items.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Matej Klonfar
  * All rights reserved.
  *
@@ -37,4 +37,30 @@
 
 #include <stdint.h>
+
+/**
+ * Item prefix
+ */
+#define USB_HID_ITEM_SIZE(data) 	((uint8_t)(data & 0x3))
+#define USB_HID_ITEM_TAG(data) 		((uint8_t)((data & 0xF0) >> 4))
+#define USB_HID_ITEM_TAG_CLASS(data)	((uint8_t)((data & 0xC) >> 2))
+#define USB_HID_ITEM_IS_LONG(data)	(data == 0xFE)
+
+
+/**
+ * Input/Output/Feature Item flags
+ */
+/** Constant (1) / Variable (0) */
+#define USB_HID_ITEM_FLAG_CONSTANT(flags) 	((flags & 0x1) == 0x1)
+/** Variable (1) / Array (0) */
+#define USB_HID_ITEM_FLAG_VARIABLE(flags) 	((flags & 0x2) == 0x2)
+/** Absolute / Relative*/
+#define USB_HID_ITEM_FLAG_RELATIVE(flags) 	((flags & 0x4) == 0x4)
+/** Wrap / No Wrap */
+#define USB_HID_ITEM_FLAG_WRAP(flags)		((flags & 0x8) == 0x8)
+#define USB_HID_ITEM_FLAG_LINEAR(flags)		((flags & 0x10) == 0x10)
+#define USB_HID_ITEM_FLAG_PREFERRED(flags)	((flags & 0x20) == 0x20)
+#define USB_HID_ITEM_FLAG_POSITION(flags)	((flags & 0x40) == 0x40)
+#define USB_HID_ITEM_FLAG_VOLATILE(flags)	((flags & 0x80) == 0x80)
+#define USB_HID_ITEM_FLAG_BUFFERED(flags)	((flags & 0x100) == 0x100)
 
 /* MAIN ITEMS */
Index: uspace/lib/usb/include/usb/classes/hiddescriptor.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hiddescriptor.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usb/include/usb/classes/hiddescriptor.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2011 Matej Klonfar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusb
+ * @{
+ */
+/** @file
+ * USB HID report descriptor and report data parser
+ */
+#ifndef LIBUSB_HIDDESCRIPTOR_H_
+#define LIBUSB_HIDDESCRIPTOR_H_
+
+#include <stdint.h>
+#include <adt/list.h>
+#include <usb/classes/hid_report_items.h>
+#include <usb/classes/hidpath.h>
+#include <usb/classes/hidtypes.h>
+
+
+/*
+ * Descriptor parser functions
+ */
+
+/** */
+int usb_hid_parse_report_descriptor(usb_hid_report_t *report, 
+                                    const uint8_t *data, size_t size);
+
+/** */
+void usb_hid_free_report(usb_hid_report_t *report);
+
+/** */
+void usb_hid_descriptor_print(usb_hid_report_t *report);
+
+
+int usb_hid_report_init(usb_hid_report_t *report);
+int usb_hid_report_append_fields(usb_hid_report_t *report, 
+                                 usb_hid_report_item_t *report_item);
+
+usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type);
+int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
+int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
+int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
+int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
+
+void usb_hid_descriptor_print_list(link_t *head);
+void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item);
+void usb_hid_free_report_list(link_t *head);
+usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
+uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usb/include/usb/classes/hidparser.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hidparser.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/classes/hidparser.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Matej Klonfar
  * All rights reserved.
  *
@@ -39,214 +39,7 @@
 #include <adt/list.h>
 #include <usb/classes/hid_report_items.h>
-
-/**
- * Item prefix
- */
-#define USB_HID_ITEM_SIZE(data) 	((uint8_t)(data & 0x3))
-#define USB_HID_ITEM_TAG(data) 		((uint8_t)((data & 0xF0) >> 4))
-#define USB_HID_ITEM_TAG_CLASS(data)	((uint8_t)((data & 0xC) >> 2))
-#define USB_HID_ITEM_IS_LONG(data)	(data == 0xFE)
-
-
-/**
- * Input/Output/Feature Item flags
- */
-/** Constant (1) / Variable (0) */
-#define USB_HID_ITEM_FLAG_CONSTANT(flags) 	((flags & 0x1) == 0x1)
-/** Variable (1) / Array (0) */
-#define USB_HID_ITEM_FLAG_VARIABLE(flags) 	((flags & 0x2) == 0x2)
-/** Absolute / Relative*/
-#define USB_HID_ITEM_FLAG_RELATIVE(flags) 	((flags & 0x4) == 0x4)
-/** Wrap / No Wrap */
-#define USB_HID_ITEM_FLAG_WRAP(flags)		((flags & 0x8) == 0x8)
-#define USB_HID_ITEM_FLAG_LINEAR(flags)		((flags & 0x10) == 0x10)
-#define USB_HID_ITEM_FLAG_PREFERRED(flags)	((flags & 0x20) == 0x20)
-#define USB_HID_ITEM_FLAG_POSITION(flags)	((flags & 0x40) == 0x40)
-#define USB_HID_ITEM_FLAG_VOLATILE(flags)	((flags & 0x80) == 0x80)
-#define USB_HID_ITEM_FLAG_BUFFERED(flags)	((flags & 0x100) == 0x100)
-
-
-/**
- * Description of path of usage pages and usages in report descriptor
- */
-#define USB_HID_PATH_COMPARE_STRICT				0
-#define USB_HID_PATH_COMPARE_END				1
-#define USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY	4
-
-/** */
-typedef struct {
-	/** */
-	int32_t usage_page;
-	/** */	
-	int32_t usage;
-	/** */
-	link_t link;
-} usb_hid_report_usage_path_t;
-
-/** */
-typedef struct {
-	/** */	
-	int depth;	
-	uint8_t report_id;
-	
-	/** */	
-	link_t link;
-
-} usb_hid_report_path_t;
-
-/**
- * Description of report items
- */
-typedef struct {
-	/** */	
-	int32_t id;
-	/** */	
-	int32_t usage_minimum;
-	/** */	
-	int32_t usage_maximum;
-	/** */	
-	int32_t logical_minimum;
-	/** */	
-	int32_t logical_maximum;
-	/** */	
-	int32_t size;
-	/** */	
-	int32_t count;
-	/** */	
-	size_t offset;
-	/** */	
-	int32_t delimiter;
-	/** */	
-	int32_t unit_exponent;
-	/** */	
-	int32_t unit;
-
-	/** */
-	int32_t string_index;
-	/** */	
-	int32_t string_minimum;
-	/** */	
-	int32_t string_maximum;
-	/** */	
-	int32_t designator_index;
-	/** */	
-	int32_t designator_minimum;
-	/** */	
-	int32_t designator_maximum;
-	/** */	
-	int32_t physical_minimum;
-	/** */	
-	int32_t physical_maximum;
-
-	/** */	
-	uint8_t item_flags;
-
-	/** */	
-	usb_hid_report_path_t *usage_path;
-	/** */	
-	link_t link;
-} usb_hid_report_item_t;
-
-
-/** HID report parser structure. */
-typedef struct {	
-	/** */	
-	link_t input;
-	/** */	
-	link_t output;
-	/** */	
-	link_t feature;
-	
-	int use_report_id;
-
-	/** */
- 	link_t stack;
-} usb_hid_report_parser_t;	
-
-
-/** HID parser callbacks for IN items. */
-typedef struct {
-	/** Callback for keyboard.
-	 *
-	 * @param key_codes Array of pressed key (including modifiers).
-	 * @param count Length of @p key_codes.
-	 * @param arg Custom argument.
-	 */
-	void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t report_id, void *arg);
-} usb_hid_report_in_callbacks_t;
-
-
-typedef enum {
-	USB_HID_MOD_LCTRL = 0x01,
-	USB_HID_MOD_LSHIFT = 0x02,
-	USB_HID_MOD_LALT = 0x04,
-	USB_HID_MOD_LGUI = 0x08,
-	USB_HID_MOD_RCTRL = 0x10,
-	USB_HID_MOD_RSHIFT = 0x20,
-	USB_HID_MOD_RALT = 0x40,
-	USB_HID_MOD_RGUI = 0x80,
-	USB_HID_MOD_COUNT = 8
-} usb_hid_modifiers_t;
-
-//typedef enum {
-//	USB_HID_LED_NUM_LOCK = 0x1,
-//	USB_HID_LED_CAPS_LOCK = 0x2,
-//	USB_HID_LED_SCROLL_LOCK = 0x4,
-//	USB_HID_LED_COMPOSE = 0x8,
-//	USB_HID_LED_KANA = 0x10,
-//	USB_HID_LED_COUNT = 5
-//} usb_hid_led_t;
-
-static const usb_hid_modifiers_t 
-    usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
-	USB_HID_MOD_LCTRL,
-	USB_HID_MOD_LSHIFT,
-	USB_HID_MOD_LALT,
-	USB_HID_MOD_LGUI,
-	USB_HID_MOD_RCTRL,
-	USB_HID_MOD_RSHIFT,
-	USB_HID_MOD_RALT,
-	USB_HID_MOD_RGUI
-};
-
-//static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
-//	USB_HID_LED_NUM_LOCK,
-//	USB_HID_LED_CAPS_LOCK,
-//	USB_HID_LED_SCROLL_LOCK,
-//	USB_HID_LED_COMPOSE,
-//	USB_HID_LED_KANA
-//};
-
-//#define USB_HID_BOOT_KEYBOARD_NUM_LOCK		0x01
-//#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK		0x02
-//#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK	0x04
-//#define USB_HID_BOOT_KEYBOARD_COMPOSE		0x08
-//#define USB_HID_BOOT_KEYBOARD_KANA			0x10
-
-/*
- * Descriptor parser functions
- */
-/** */
-int usb_hid_parser_init(usb_hid_report_parser_t *parser);
-
-/** */
-int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser, 
-    const uint8_t *data, size_t size);
-
-/** */
-void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
-
-/** */
-void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
-
-/*
- * Boot protocol functions
- */
-/** */
-int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
-	const usb_hid_report_in_callbacks_t *callbacks, void *arg);
-
-/** */
-int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
+#include <usb/classes/hidpath.h>
+#include <usb/classes/hidtypes.h>
+#include <usb/classes/hiddescriptor.h>
 
 
@@ -255,45 +48,10 @@
  */
 /** */
-int usb_hid_parse_report(const usb_hid_report_parser_t *parser,  
-    const uint8_t *data, size_t size,
-    usb_hid_report_path_t *path, int flags,
-    const usb_hid_report_in_callbacks_t *callbacks, void *arg);
+int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data, 
+                         size_t size, uint8_t *report_id);
 
 /** */
-size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
+size_t usb_hid_report_input_length(const usb_hid_report_t *report,
 	usb_hid_report_path_t *path, int flags);
-
-
-
-/* 
- * usage path functions 
- */
-/** */
-usb_hid_report_path_t *usb_hid_report_path(void);
-
-/** */
-void usb_hid_report_path_free(usb_hid_report_path_t *path);
-
-/** */
-int usb_hid_report_path_set_report_id(usb_hid_report_path_t *usage_path, uint8_t report_id);
-
-/** */
-int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, int32_t usage_page, int32_t usage);
-
-/** */
-void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
-
-/** */
-void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
-
-/** */
-void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data);
-
-/** */
-int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, usb_hid_report_path_t *path, int flags);
-
-/** */
-usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
-
 
 /*
@@ -301,5 +59,6 @@
  */
 /** Allocates output report buffer*/
-uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size);
+uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, 
+                               uint8_t report_id);
 
 /** Frees output report buffer*/
@@ -307,12 +66,23 @@
 
 /** Returns size of output for given usage path */
-size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
+size_t usb_hid_report_output_size(usb_hid_report_t *report,
                                   usb_hid_report_path_t *path, int flags);
 
-/** Updates the output report buffer by translated given data */
-int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
-                                    usb_hid_report_path_t *path, int flags,
-                                    uint8_t *buffer, size_t size,
-                                    int32_t *data, size_t data_size);
+/** Makes the output report buffer by translated given data */
+int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id, 
+                                    uint8_t *buffer, size_t size);
+
+/** */
+usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report, 
+                                                   usb_hid_report_field_t *field, 
+                                                   usb_hid_report_path_t *path, 
+                                                   int flags, 
+                                                   usb_hid_report_type_t type);
+
+/** */
+uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, 
+                                     uint8_t report_id, 
+                                     usb_hid_report_type_t type);
+
 #endif
 /**
Index: uspace/lib/usb/include/usb/classes/hidpath.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hidpath.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usb/include/usb/classes/hidpath.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2011 Matej Klonfar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusb
+ * @{
+ */
+/** @file
+ * USB HID report descriptor and report data parser
+ */
+#ifndef LIBUSB_HIDPATH_H_
+#define LIBUSB_HIDPATH_H_
+
+#include <usb/classes/hidparser.h>
+#include <stdint.h>
+#include <adt/list.h>
+
+/**
+ * Description of path of usage pages and usages in report descriptor
+ */
+#define USB_HID_PATH_COMPARE_STRICT				0
+#define USB_HID_PATH_COMPARE_END				1
+#define USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY	4
+#define USB_HID_PATH_COMPARE_COLLECTION_ONLY	2 /* porovnava jenom cestu z Kolekci */
+
+
+/** Collection usage path structure */
+typedef struct {
+	/** */
+	uint32_t usage_page;
+	/** */	
+	uint32_t usage;
+
+	uint8_t flags;
+	/** */
+	link_t link;
+} usb_hid_report_usage_path_t;
+
+/** */
+typedef struct {
+	/** */	
+	int depth;	
+	uint8_t report_id;
+	
+	/** */	
+	link_t link; /* list */
+
+	link_t head; /* head of list of usage paths */
+
+} usb_hid_report_path_t;
+
+/** */
+usb_hid_report_path_t *usb_hid_report_path(void);
+
+/** */
+void usb_hid_report_path_free(usb_hid_report_path_t *path);
+
+/** */
+int usb_hid_report_path_set_report_id(usb_hid_report_path_t *usage_path, 
+                                      uint8_t report_id);
+
+/** */
+int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, 
+                                    int32_t usage_page, int32_t usage);
+
+/** */
+void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
+
+/** */
+void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
+
+/** */
+void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, 
+                                  int32_t tag, int32_t data);
+
+/** */
+int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, 
+                                      usb_hid_report_path_t *path, int flags);
+
+/** */
+usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
+
+void usb_hid_print_usage_path(usb_hid_report_path_t *path);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usb/include/usb/classes/hidreport.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hidreport.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/classes/hidreport.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -57,5 +57,5 @@
  */
 int usb_hid_process_report_descriptor(usb_device_t *dev, 
-    usb_hid_report_parser_t *parser);
+    usb_hid_report_t *report);
 
 #endif /* LIBUSB_HIDREPORT_H_ */
Index: uspace/lib/usb/include/usb/classes/hidtypes.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hidtypes.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usb/include/usb/classes/hidtypes.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,208 @@
+/*
+ * Copyright (c) 2011 Matej Klonfar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusb
+ * @{
+ */
+/** @file
+ * USB HID report descriptor and report data parser
+ */
+#ifndef LIBUSB_HIDTYPES_H_
+#define LIBUSB_HIDTYPES_H_
+
+#include <stdint.h>
+#include <adt/list.h>
+
+#define USB_HID_MAX_USAGES	20
+
+#define USB_HID_UINT32_TO_INT32(x, size)	((((x) & (1 << ((size) - 1))) != 0) ? -(~(x - 1) & ((1 << size) - 1)) : (x)) //(-(~((x) - 1)))
+#define USB_HID_INT32_TO_UINT32(x, size)	(((x) < 0 ) ? ((1 << (size)) + (x)) : (x))
+
+
+typedef enum {
+	USB_HID_REPORT_TYPE_INPUT = 1,
+	USB_HID_REPORT_TYPE_OUTPUT = 2,
+	USB_HID_REPORT_TYPE_FEATURE = 3
+} usb_hid_report_type_t;
+
+
+typedef struct {
+	/** */
+	int report_count;
+	link_t reports;		/** list of usb_hid_report_description_t */
+
+	link_t collection_paths;
+	int collection_paths_count;
+
+	int use_report_ids;
+	uint8_t last_report_id;
+	
+} usb_hid_report_t;
+
+typedef struct {
+	uint8_t report_id;
+	usb_hid_report_type_t type;
+
+	size_t bit_length;
+	size_t item_length;
+	
+	link_t report_items;	/** list of report items (fields) */
+
+	link_t link;
+} usb_hid_report_description_t;
+
+typedef struct {
+
+	int offset;
+	size_t size;
+
+	uint16_t usage_page;
+	uint16_t usage;
+
+	uint8_t item_flags;
+	usb_hid_report_path_t *collection_path;
+
+	int32_t logical_minimum;
+	int32_t logical_maximum;
+	int32_t physical_minimum;
+	int32_t physical_maximum;
+	uint32_t usage_minimum;
+	uint32_t usage_maximum;
+	uint32_t unit;
+	uint32_t unit_exponent;
+	
+
+	int32_t value;
+
+	link_t link;
+} usb_hid_report_field_t;
+
+
+
+/**
+ * state table
+ */
+typedef struct {
+	/** report id */	
+	int32_t id;
+	
+	/** */
+	uint16_t extended_usage_page;
+	uint32_t usages[USB_HID_MAX_USAGES];
+	int usages_count;
+
+	/** */
+	uint32_t usage_page;
+
+	/** */	
+	uint32_t usage_minimum;
+	/** */	
+	uint32_t usage_maximum;
+	/** */	
+	int32_t logical_minimum;
+	/** */	
+	int32_t logical_maximum;
+	/** */	
+	int32_t size;
+	/** */	
+	int32_t count;
+	/** */	
+	size_t offset;
+	/** */	
+	int32_t unit_exponent;
+	/** */	
+	int32_t unit;
+
+	/** */
+	uint32_t string_index;
+	/** */	
+	uint32_t string_minimum;
+	/** */	
+	uint32_t string_maximum;
+	/** */	
+	uint32_t designator_index;
+	/** */	
+	uint32_t designator_minimum;
+	/** */	
+	uint32_t designator_maximum;
+	/** */	
+	int32_t physical_minimum;
+	/** */	
+	int32_t physical_maximum;
+
+	/** */	
+	uint8_t item_flags;
+
+	usb_hid_report_type_t type;
+
+	/** current collection path*/	
+	usb_hid_report_path_t *usage_path;
+	/** */	
+	link_t link;
+} usb_hid_report_item_t;
+
+/** HID parser callbacks for IN items. */
+typedef struct {
+	/** Callback for keyboard.
+	 *
+	 * @param key_codes Array of pressed key (including modifiers).
+	 * @param count Length of @p key_codes.
+	 * @param arg Custom argument.
+	 */
+	void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t report_id, void *arg);
+} usb_hid_report_in_callbacks_t;
+
+
+typedef enum {
+	USB_HID_MOD_LCTRL = 0x01,
+	USB_HID_MOD_LSHIFT = 0x02,
+	USB_HID_MOD_LALT = 0x04,
+	USB_HID_MOD_LGUI = 0x08,
+	USB_HID_MOD_RCTRL = 0x10,
+	USB_HID_MOD_RSHIFT = 0x20,
+	USB_HID_MOD_RALT = 0x40,
+	USB_HID_MOD_RGUI = 0x80,
+	USB_HID_MOD_COUNT = 8
+} usb_hid_modifiers_t;
+
+static const usb_hid_modifiers_t 
+    usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
+	USB_HID_MOD_LCTRL,
+	USB_HID_MOD_LSHIFT,
+	USB_HID_MOD_LALT,
+	USB_HID_MOD_LGUI,
+	USB_HID_MOD_RCTRL,
+	USB_HID_MOD_RSHIFT,
+	USB_HID_MOD_RALT,
+	USB_HID_MOD_RGUI
+};
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usb/include/usb/classes/hidut.h
===================================================================
--- uspace/lib/usb/include/usb/classes/hidut.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/classes/hidut.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -46,6 +46,8 @@
 	USB_HIDUT_PAGE_KEYBOARD = 7,
 	USB_HIDUT_PAGE_LED = 8,
-	USB_HIDUT_PAGE_BUTTON = 9
-	/* USB_HIDUT_PAGE_ = , */
+	USB_HIDUT_PAGE_BUTTON = 9,
+	USB_HIDUT_PAGE_ORDINAL = 0x0a,
+	USB_HIDUT_PAGE_TELEPHONY_DEVICE = 0x0b,
+	USB_HIDUT_PAGE_CONSUMER = 0x0c
 } usb_hidut_usage_page_t;
 
@@ -57,5 +59,8 @@
 	USB_HIDUT_USAGE_GENERIC_DESKTOP_GAMEPAD = 5,
 	USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD = 6,
-	USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYPAD = 7
+	USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYPAD = 7,
+	USB_HIDUT_USAGE_GENERIC_DESKTOP_X = 0x30,
+	USB_HIDUT_USAGE_GENERIC_DESKTOP_Y = 0x31,
+	USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL = 0x38
 	/* USB_HIDUT_USAGE_GENERIC_DESKTOP_ = , */
 	
Index: uspace/lib/usb/include/usb/usb.h
===================================================================
--- uspace/lib/usb/include/usb/usb.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/include/usb/usb.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -96,5 +96,6 @@
 	USB_REQUEST_RECIPIENT_DEVICE = 0,
 	USB_REQUEST_RECIPIENT_INTERFACE = 1,
-	USB_REQUEST_RECIPIENT_ENDPOINT = 2
+	USB_REQUEST_RECIPIENT_ENDPOINT = 2,
+	USB_REQUEST_RECIPIENT_OTHER = 3
 } usb_request_recipient_t;
 
Index: uspace/lib/usb/src/debug.c
===================================================================
--- uspace/lib/usb/src/debug.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/src/debug.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -158,5 +158,7 @@
 
 /** Fibril local storage for the dumped buffer. */
-static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
+static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
+/** Fibril local storage for buffer switching. */
+static fibril_local int buffer_dump_index = 0;
 
 /** Dump buffer into string.
@@ -167,5 +169,6 @@
  * can not do that) and you do not have to guard it against concurrent
  * calls to it.
- * The only limitation is that each call rewrites the buffer again.
+ * The only limitation is that each second call rewrites the buffer again
+ * (internally, two buffer are used in cyclic manner).
  * Thus, it is necessary to copy the buffer elsewhere (that includes printing
  * to screen or writing to file).
@@ -173,5 +176,5 @@
  * that is not a big limitation.
  *
- * @warning You cannot use this function twice in the same printf
+ * @warning You cannot use this function more than twice in the same printf
  * (see detailed explanation).
  *
@@ -185,8 +188,7 @@
 {
 	/*
-	 * Remove previous string (that might also reveal double usage of
-	 * this function).
+	 * Remove previous string.
 	 */
-	bzero(buffer_dump, BUFFER_DUMP_LEN);
+	bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);
 
 	if (buffer == NULL) {
@@ -202,5 +204,5 @@
 	/* How many bytes are available in the output buffer. */
 	size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
-	char *it = buffer_dump;
+	char *it = buffer_dump[buffer_dump_index];
 
 	size_t index = 0;
@@ -253,5 +255,9 @@
 	}
 
-	return buffer_dump;
+	/* Next time, use the other buffer. */
+	buffer_dump_index = 1 - buffer_dump_index;
+
+	/* Need to take the old one due to previous line. */
+	return buffer_dump[1 - buffer_dump_index];
 }
 
Index: uspace/lib/usb/src/hiddescriptor.c
===================================================================
--- uspace/lib/usb/src/hiddescriptor.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usb/src/hiddescriptor.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,760 @@
+/*
+ * Copyright (c) 2011 Matej Klonfar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusb
+ * @{
+ */
+/** @file
+ * HID report descriptor and report data parser implementation.
+ */
+#include <usb/classes/hidparser.h>
+#include <errno.h>
+#include <stdio.h>
+#include <malloc.h>
+#include <mem.h>
+#include <usb/debug.h>
+#include <assert.h>
+
+/** The new report item flag. Used to determine when the item is completly
+ * configured and should be added to the report structure
+ */
+#define USB_HID_NEW_REPORT_ITEM 1
+
+/** No special action after the report descriptor tag is processed should be
+ * done
+ */
+#define USB_HID_NO_ACTION	2
+
+#define USB_HID_RESET_OFFSET	3
+
+/** Unknown tag was founded in report descriptor data*/
+#define USB_HID_UNKNOWN_TAG		-99
+
+
+/**
+ * Initialize the report descriptor parser structure
+ *
+ * @param parser Report descriptor parser structure
+ * @return Error code
+ */
+int usb_hid_report_init(usb_hid_report_t *report)
+{
+	if(report == NULL) {
+		return EINVAL;
+	}
+
+	memset(report, 0, sizeof(usb_hid_report_t));
+	list_initialize(&report->reports);
+	list_initialize(&report->collection_paths);
+
+	report->use_report_ids = 0;
+    return EOK;   
+}
+
+int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
+{
+	usb_hid_report_field_t *field;
+	int i;
+
+
+	/* find or append current collection path to the list */
+	link_t *path_it = report->collection_paths.next;
+	usb_hid_report_path_t *path = NULL;
+	while(path_it != &report->collection_paths) {
+		path = list_get_instance(path_it, usb_hid_report_path_t, link);
+		
+		if(usb_hid_report_compare_usage_path(path, report_item->usage_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
+			break;
+		}			
+		path_it = path_it->next;
+	}
+	if(path_it == &report->collection_paths) {
+		path = usb_hid_report_path_clone(report_item->usage_path);			
+		list_append(&path->link, &report->collection_paths);					
+		report->collection_paths_count++;
+	}
+
+	for(i=0; i<report_item->usages_count; i++){
+		usb_log_debug("usages (%d) - %x\n", i, report_item->usages[i]);
+	}
+
+	
+	for(i=0; i<report_item->count; i++){
+
+		field = malloc(sizeof(usb_hid_report_field_t));
+		memset(field, 0, sizeof(usb_hid_report_field_t));
+		list_initialize(&field->link);
+
+		/* fill the attributes */		
+		field->collection_path = path;
+		field->logical_minimum = report_item->logical_minimum;
+		field->logical_maximum = report_item->logical_maximum;
+		field->physical_minimum = report_item->physical_minimum;
+		field->physical_maximum = report_item->physical_maximum;
+
+		field->usage_minimum = report_item->usage_minimum;
+		field->usage_maximum = report_item->usage_maximum;
+		if(report_item->extended_usage_page != 0){
+			field->usage_page = report_item->extended_usage_page;
+		}
+		else {
+			field->usage_page = report_item->usage_page;
+		}
+
+		if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
+			uint32_t usage;
+			if(report_item->type != USB_HID_REPORT_TYPE_OUTPUT) {
+				if(i < report_item->usages_count){
+					usage = report_item->usages[i];
+				}
+				else {
+					usage = report_item->usages[report_item->usages_count - 1];
+				}
+			}
+			else {
+				if((report_item->count - i - 1) < report_item->usages_count){
+					usage = report_item->usages[(report_item->count - i - 1)];
+				}
+				else {
+					usage = report_item->usages[report_item->usages_count - 1];
+				}
+			}
+
+						
+			if((usage & 0xFFFF0000) != 0){
+				field->usage_page = (usage >> 16);					
+				field->usage = (usage & 0xFFFF);
+			}
+			else {
+				field->usage = usage;
+			}
+
+			
+		}	
+
+		if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
+			if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
+				field->usage = report_item->usage_maximum - i;
+			}
+			else {
+				field->usage = report_item->usage_minimum + i;					
+			}
+
+		}
+		
+		field->size = report_item->size;
+		field->offset = report_item->offset + (i * report_item->size);
+		if(report_item->id != 0) {
+			field->offset += 8;
+			report->use_report_ids = 1;
+		}
+		field->item_flags = report_item->item_flags;
+
+		/* find the right report list*/
+		usb_hid_report_description_t *report_des;
+		report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
+		if(report_des == NULL){
+			report_des = malloc(sizeof(usb_hid_report_description_t));
+			memset(report_des, 0, sizeof(usb_hid_report_description_t));
+
+			report_des->type = report_item->type;
+			report_des->report_id = report_item->id;
+			list_initialize (&report_des->link);
+			list_initialize (&report_des->report_items);
+
+			list_append(&report_des->link, &report->reports);
+			report->report_count++;
+		}
+
+		/* append this field to the end of founded report list */
+		list_append (&field->link, &report_des->report_items);
+		
+		/* update the sizes */
+		report_des->bit_length += field->size;
+		report_des->item_length++;
+
+	}
+
+
+	return EOK;
+}
+
+usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
+{
+	link_t *report_it = report->reports.next;
+	usb_hid_report_description_t *report_des = NULL;
+	
+	while(report_it != &report->reports) {
+		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
+
+		if((report_des->report_id == report_id) && (report_des->type == type)){
+			return report_des;
+		}
+		
+		report_it = report_it->next;
+	}
+
+	return NULL;
+}
+
+/** Parse HID report descriptor.
+ *
+ * @param parser Opaque HID report parser structure.
+ * @param data Data describing the report.
+ * @return Error code.
+ */
+int usb_hid_parse_report_descriptor(usb_hid_report_t *report, 
+    const uint8_t *data, size_t size)
+{
+	size_t i=0;
+	uint8_t tag=0;
+	uint8_t item_size=0;
+	int class=0;
+	int ret;
+	usb_hid_report_item_t *report_item=0;
+	usb_hid_report_item_t *new_report_item;	
+	usb_hid_report_path_t *usage_path;
+
+	size_t offset_input=0;
+	size_t offset_output=0;
+	size_t offset_feature=0;
+
+	link_t stack;
+	list_initialize(&stack);	
+
+	/* parser structure initialization*/
+	if(usb_hid_report_init(report) != EOK) {
+		return EINVAL;
+	}
+	
+	/*report item initialization*/
+	if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
+		return ENOMEM;
+	}
+	memset(report_item, 0, sizeof(usb_hid_report_item_t));
+	list_initialize(&(report_item->link));	
+
+	/* usage path context initialization */
+	if(!(usage_path=usb_hid_report_path())){
+		return ENOMEM;
+	}
+	
+	while(i<size){	
+		if(!USB_HID_ITEM_IS_LONG(data[i])){
+
+			if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
+				return EINVAL;
+			}
+			
+			tag = USB_HID_ITEM_TAG(data[i]);
+			item_size = USB_HID_ITEM_SIZE(data[i]);
+			class = USB_HID_ITEM_TAG_CLASS(data[i]);
+			
+			ret = usb_hid_report_parse_tag(tag,class,data+i+1,
+			                               item_size,report_item, usage_path);
+			switch(ret){
+				case USB_HID_NEW_REPORT_ITEM:
+					// store report item to report and create the new one
+					// store current collection path
+					report_item->usage_path = usage_path;
+					
+					usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);	
+					if(report_item->id != 0){
+						report->use_report_ids = 1;
+					}
+					
+					switch(tag) {
+						case USB_HID_REPORT_TAG_INPUT:
+							report_item->type = USB_HID_REPORT_TYPE_INPUT;
+							report_item->offset = offset_input;
+							offset_input += report_item->count * report_item->size;
+							break;
+						case USB_HID_REPORT_TAG_OUTPUT:
+							report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
+							report_item->offset = offset_output;
+							offset_output += report_item->count * report_item->size;
+
+							break;
+						case USB_HID_REPORT_TAG_FEATURE:
+							report_item->type = USB_HID_REPORT_TYPE_FEATURE;
+							report_item->offset = offset_feature;
+							offset_feature += report_item->count * report_item->size;
+							break;
+						default:
+						    usb_log_debug("\tjump over - tag %X\n", tag);
+						    break;
+					}
+					
+					/* 
+					 * append new fields to the report
+					 * structure 					 
+					 */
+					usb_hid_report_append_fields(report, report_item);
+
+					/* reset local items */
+					usb_hid_report_reset_local_items (report_item);
+
+					break;
+
+				case USB_HID_RESET_OFFSET:
+					offset_input = 0;
+					offset_output = 0;
+					offset_feature = 0;
+					usb_hid_report_path_set_report_id (usage_path, report_item->id);
+					break;
+
+				case USB_HID_REPORT_TAG_PUSH:
+					// push current state to stack
+					new_report_item = usb_hid_report_item_clone(report_item);
+					usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
+					new_report_item->usage_path = tmp_path; 
+
+					list_prepend (&new_report_item->link, &stack);
+					break;
+				case USB_HID_REPORT_TAG_POP:
+					// restore current state from stack
+					if(list_empty (&stack)) {
+						return EINVAL;
+					}
+					free(report_item);
+						
+					report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
+					
+					usb_hid_report_usage_path_t *tmp_usage_path;
+					tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
+					
+					usb_hid_report_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage);
+
+					usb_hid_report_path_free(report_item->usage_path);
+					list_initialize(&report_item->usage_path->link);
+					list_remove (stack.next);
+					
+					break;
+					
+				default:
+					// nothing special to do					
+					break;
+			}
+
+			/* jump over the processed block */
+			i += 1 + USB_HID_ITEM_SIZE(data[i]);
+		}
+		else{
+			// TBD
+			i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
+		}
+		
+
+	}
+	
+	return EOK;
+}
+
+
+/**
+ * Parse one tag of the report descriptor
+ *
+ * @param Tag to parse
+ * @param Report descriptor buffer
+ * @param Size of data belongs to this tag
+ * @param Current report item structe
+ * @return Code of action to be done next
+ */
+int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+{	
+	int ret;
+	
+	switch(class){
+		case USB_HID_TAG_CLASS_MAIN:
+
+			if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
+				return USB_HID_NEW_REPORT_ITEM;
+			}
+			else {
+				/*TODO process the error */
+				return ret;
+			   }
+			break;
+
+		case USB_HID_TAG_CLASS_GLOBAL:	
+			return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
+			break;
+
+		case USB_HID_TAG_CLASS_LOCAL:			
+			return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
+			break;
+		default:
+			return USB_HID_NO_ACTION;
+	}
+}
+
+/**
+ * Parse main tags of report descriptor
+ *
+ * @param Tag identifier
+ * @param Data buffer
+ * @param Length of data buffer
+ * @param Current state table
+ * @return Error code
+ */
+
+int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+{		
+	switch(tag)
+	{
+		case USB_HID_REPORT_TAG_INPUT:
+		case USB_HID_REPORT_TAG_OUTPUT:
+		case USB_HID_REPORT_TAG_FEATURE:
+			report_item->item_flags = *data;			
+			return EOK;			
+			break;
+			
+		case USB_HID_REPORT_TAG_COLLECTION:
+			// TODO usage_path->flags = *data;
+			usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);						
+			usb_hid_report_reset_local_items (report_item);
+			return USB_HID_NO_ACTION;
+			break;
+			
+		case USB_HID_REPORT_TAG_END_COLLECTION:
+			usb_hid_report_remove_last_item(usage_path);
+			return USB_HID_NO_ACTION;
+			break;
+		default:
+			return USB_HID_NO_ACTION;
+	}
+
+	return EOK;
+}
+
+/**
+ * Parse global tags of report descriptor
+ *
+ * @param Tag identifier
+ * @param Data buffer
+ * @param Length of data buffer
+ * @param Current state table
+ * @return Error code
+ */
+int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+{
+	// TODO take care about the bit length of data
+	switch(tag)
+	{
+		case USB_HID_REPORT_TAG_USAGE_PAGE:
+			report_item->usage_page = usb_hid_report_tag_data_uint32(data, item_size);
+			break;
+		case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
+			report_item->logical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
+			break;
+		case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
+			report_item->logical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
+			break;
+		case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
+			report_item->physical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
+			break;			
+		case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
+			report_item->physical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
+
+			break;
+		case USB_HID_REPORT_TAG_UNIT_EXPONENT:
+			report_item->unit_exponent = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_UNIT:
+			report_item->unit = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_REPORT_SIZE:
+			report_item->size = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_REPORT_COUNT:
+			report_item->count = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_REPORT_ID:
+			report_item->id = usb_hid_report_tag_data_uint32(data,item_size);
+			return USB_HID_RESET_OFFSET;
+			break;
+		case USB_HID_REPORT_TAG_PUSH:
+		case USB_HID_REPORT_TAG_POP:
+			/* 
+			 * stack operations are done in top level parsing
+			 * function
+			 */
+			return tag;
+			break;
+			
+		default:
+			return USB_HID_NO_ACTION;
+	}
+
+	return EOK;
+}
+
+/**
+ * Parse local tags of report descriptor
+ *
+ * @param Tag identifier
+ * @param Data buffer
+ * @param Length of data buffer
+ * @param Current state table
+ * @return Error code
+ */
+int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
+                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+{
+	switch(tag)
+	{
+		case USB_HID_REPORT_TAG_USAGE:
+			report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_uint32(data,item_size);
+			report_item->usages_count++;
+			break;
+		case USB_HID_REPORT_TAG_USAGE_MINIMUM:
+			if (item_size == 3) {
+				// usage extended usages
+				report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16; 
+				report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
+			}
+			else {
+				report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size);
+			}
+			break;
+		case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
+			if (item_size == 3) {
+				// usage extended usages
+				report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16; 
+				report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
+			}
+			else {
+				report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size);
+			}
+			break;
+		case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
+			report_item->designator_index = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
+			report_item->designator_minimum = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
+			report_item->designator_maximum = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_STRING_INDEX:
+			report_item->string_index = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_STRING_MINIMUM:
+			report_item->string_minimum = usb_hid_report_tag_data_uint32(data,item_size);
+			break;
+		case USB_HID_REPORT_TAG_STRING_MAXIMUM:
+			report_item->string_maximum = usb_hid_report_tag_data_uint32(data,item_size);
+			break;			
+		case USB_HID_REPORT_TAG_DELIMITER:
+			//report_item->delimiter = usb_hid_report_tag_data_uint32(data,item_size);
+			//TODO: 
+			//	DELIMITER STUFF
+			break;
+		
+		default:
+			return USB_HID_NO_ACTION;
+	}
+	
+	return EOK;
+}
+
+/**
+ * Converts raw data to uint32 (thats the maximum length of short item data)
+ *
+ * @param Data buffer
+ * @param Size of buffer
+ * @return Converted int32 number
+ */
+uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
+{
+	unsigned int i;
+	uint32_t result;
+
+	result = 0;
+	for(i=0; i<size; i++) {
+		result = (result | (data[i]) << (i*8));
+	}
+
+	return result;
+}
+
+/**
+ * Prints content of given list of report items.
+ *
+ * @param List of report items (usb_hid_report_item_t)
+ * @return void
+ */
+void usb_hid_descriptor_print_list(link_t *head)
+{
+	usb_hid_report_field_t *report_item;
+	link_t *item;
+
+
+	if(head == NULL || list_empty(head)) {
+	    usb_log_debug("\tempty\n");
+	    return;
+	}
+        
+	for(item = head->next; item != head; item = item->next) {
+                
+		report_item = list_get_instance(item, usb_hid_report_field_t, link);
+
+		usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
+		usb_log_debug("\t\tSIZE: %zu\n", report_item->size);
+		usb_log_debug("\t\tLOGMIN: %d\n", report_item->logical_minimum);
+		usb_log_debug("\t\tLOGMAX: %d\n", report_item->logical_maximum);		
+		usb_log_debug("\t\tPHYMIN: %d\n", report_item->physical_minimum);		
+		usb_log_debug("\t\tPHYMAX: %d\n", report_item->physical_maximum);				
+		usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
+		usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
+
+		usb_log_debug("\t\tVALUE: %X\n", report_item->value);
+		usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
+		usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
+						
+//		usb_log_debug("\n");		
+
+	}
+
+
+}
+/**
+ * Prints content of given report descriptor in human readable format.
+ *
+ * @param parser Parsed descriptor to print
+ * @return void
+ */
+void usb_hid_descriptor_print(usb_hid_report_t *report)
+{
+	if(report == NULL) {
+		return;
+	}
+
+	link_t *report_it = report->reports.next;
+	usb_hid_report_description_t *report_des;
+
+	while(report_it != &report->reports) {
+		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
+		usb_log_debug("Report ID: %d\n", report_des->report_id);
+		usb_log_debug("\tType: %d\n", report_des->type);
+		usb_log_debug("\tLength: %zu\n", report_des->bit_length);
+		usb_log_debug("\tItems: %zu\n", report_des->item_length);
+
+		usb_hid_descriptor_print_list(&report_des->report_items);
+
+
+		link_t *path_it = report->collection_paths.next;
+		while(path_it != &report->collection_paths) {
+			usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
+			path_it = path_it->next;
+		}
+		
+		report_it = report_it->next;
+	}
+}
+
+/**
+ * Releases whole linked list of report items
+ *
+ * @param head Head of list of report descriptor items (usb_hid_report_item_t)
+ * @return void
+ */
+void usb_hid_free_report_list(link_t *head)
+{
+	return; 
+	
+	usb_hid_report_item_t *report_item;
+	link_t *next;
+	
+	if(head == NULL || list_empty(head)) {		
+	    return;
+	}
+	
+	next = head->next;
+	while(next != head) {
+	
+	    report_item = list_get_instance(next, usb_hid_report_item_t, link);
+
+		while(!list_empty(&report_item->usage_path->link)) {
+			usb_hid_report_remove_last_item(report_item->usage_path);
+		}
+
+		
+	    next = next->next;
+	    
+	    free(report_item);
+	}
+	
+	return;
+	
+}
+
+/** Frees the HID report descriptor parser structure 
+ *
+ * @param parser Opaque HID report parser structure
+ * @return void
+ */
+void usb_hid_free_report(usb_hid_report_t *report)
+{
+	if(report == NULL){
+		return;
+	}
+
+	// free collection paths
+	usb_hid_report_path_t *path;
+	while(!list_empty(&report->collection_paths)) {
+		path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
+		usb_hid_report_path_free(path);		
+	}
+	
+	// free report items
+	usb_hid_report_description_t *report_des;
+	usb_hid_report_field_t *field;
+	while(!list_empty(&report->reports)) {
+		report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
+		list_remove(&report_des->link);
+		
+		while(!list_empty(&report_des->report_items)) {
+			field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
+			list_remove(&field->link);
+
+			free(field);
+		}
+		
+		free(report_des);
+	}
+	
+	return;
+}
+
+/**
+ * @}
+ */
Index: uspace/lib/usb/src/hidparser.c
===================================================================
--- uspace/lib/usb/src/hidparser.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/src/hidparser.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Matej Klonfar
  * All rights reserved.
  *
@@ -39,38 +39,16 @@
 #include <mem.h>
 #include <usb/debug.h>
-
-/** */
-#define USB_HID_NEW_REPORT_ITEM 1
-
-/** */
-#define USB_HID_NO_ACTION		2
-
-/** */
-#define USB_HID_UNKNOWN_TAG		-99
-
-/*
- * Private descriptor parser functions
- */
-int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
-int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
-int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
-int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
-
-void usb_hid_descriptor_print_list(link_t *head);
-int usb_hid_report_reset_local_items();
-void usb_hid_free_report_list(link_t *head);
-usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
+#include <assert.h>
+
+
 /*
  * Data translation private functions
  */
-int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
+uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
 inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
-int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
-int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);
+int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
+uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
 int usb_pow(int a, int b);
+
 
 // TODO: tohle ma bejt asi jinde
@@ -90,595 +68,6 @@
 }
 
-/**
- * Initialize the report descriptor parser structure
- *
- * @param parser Report descriptor parser structure
- * @return Error code
- */
-int usb_hid_parser_init(usb_hid_report_parser_t *parser)
-{
-	if(parser == NULL) {
-		return EINVAL;
-	}
-
-	list_initialize(&(parser->input));
-    list_initialize(&(parser->output));
-    list_initialize(&(parser->feature));
-
-	list_initialize(&(parser->stack));
-
-	parser->use_report_id = 0;
-    return EOK;   
-}
-
-
-/** Parse HID report descriptor.
- *
- * @param parser Opaque HID report parser structure.
- * @param data Data describing the report.
- * @return Error code.
- */
-int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser, 
-    const uint8_t *data, size_t size)
-{
-	size_t i=0;
-	uint8_t tag=0;
-	uint8_t item_size=0;
-	int class=0;
-	int ret;
-	usb_hid_report_item_t *report_item=0;
-	usb_hid_report_item_t *new_report_item;	
-	usb_hid_report_path_t *usage_path;
-	usb_hid_report_path_t *tmp_usage_path;
-
-	size_t offset_input=0;
-	size_t offset_output=0;
-	size_t offset_feature=0;
-	
-
-	/* parser structure initialization*/
-	if(usb_hid_parser_init(parser) != EOK) {
-		return EINVAL;
-	}
-	
-
-	/*report item initialization*/
-	if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
-		return ENOMEM;
-	}
-	memset(report_item, 0, sizeof(usb_hid_report_item_t));
-	list_initialize(&(report_item->link));	
-
-	/* usage path context initialization */
-	if(!(usage_path=usb_hid_report_path())){
-		return ENOMEM;
-	}
-	
-	while(i<size){	
-		if(!USB_HID_ITEM_IS_LONG(data[i])){
-
-			if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
-				return EINVAL; // TODO ERROR CODE
-			}
-			
-			tag = USB_HID_ITEM_TAG(data[i]);
-			item_size = USB_HID_ITEM_SIZE(data[i]);
-			class = USB_HID_ITEM_TAG_CLASS(data[i]);
-
-			usb_log_debug2(
-				"i(%zu) data(%X) value(%X): TAG %d, class %u, size %u - ", i,
-			    data[i], usb_hid_report_tag_data_int32(data+i+1,item_size), 
-			    tag, class, item_size);
-			
-			ret = usb_hid_report_parse_tag(tag,class,data+i+1,
-			                               item_size,report_item, usage_path);
-			usb_log_debug2("ret: %u\n", ret);
-			switch(ret){
-				case USB_HID_NEW_REPORT_ITEM:
-					// store report item to report and create the new one
-					usb_log_debug("\nNEW REPORT ITEM: %X",ret);
-
-					// store current usage path
-					report_item->usage_path = usage_path;
-					
-					// clone path to the new one
-					tmp_usage_path = usb_hid_report_path_clone(usage_path);
-
-					// swap
-					usage_path = tmp_usage_path;
-					tmp_usage_path = NULL;
-
-					usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);	
-					if(report_item->id != 0){
-						parser->use_report_id = 1;
-					}
-					
-					switch(tag) {
-						case USB_HID_REPORT_TAG_INPUT:
-							report_item->offset = offset_input;
-							offset_input += report_item->count * report_item->size;
-							usb_log_debug(" - INPUT\n");
-							list_append(&(report_item->link), &(parser->input));
-							break;
-						case USB_HID_REPORT_TAG_OUTPUT:
-							report_item->offset = offset_output;
-							offset_output += report_item->count * report_item->size;
-							usb_log_debug(" - OUTPUT\n");
-								list_append(&(report_item->link), &(parser->output));
-
-							break;
-						case USB_HID_REPORT_TAG_FEATURE:
-							report_item->offset = offset_feature;
-							offset_feature += report_item->count * report_item->size;
-							usb_log_debug(" - FEATURE\n");
-								list_append(&(report_item->link), &(parser->feature));
-							break;
-						default:
-						    usb_log_debug("\tjump over - tag %X\n", tag);
-						    break;
-					}
-
-					/* clone current state table to the new item */
-					if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
-						return ENOMEM;
-					}					
-					memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
-					link_initialize(&(new_report_item->link));
-					
-					/* reset local items */
-					new_report_item->usage_minimum = 0;
-					new_report_item->usage_maximum = 0;
-					new_report_item->designator_index = 0;
-					new_report_item->designator_minimum = 0;
-					new_report_item->designator_maximum = 0;
-					new_report_item->string_index = 0;
-					new_report_item->string_minimum = 0;
-					new_report_item->string_maximum = 0;
-
-					/* reset usage from current usage path */
-					usb_hid_report_usage_path_t *path = list_get_instance(&usage_path->link, usb_hid_report_usage_path_t, link);
-					path->usage = 0;
-					
-					report_item = new_report_item;
-										
-					break;
-				case USB_HID_REPORT_TAG_PUSH:
-					// push current state to stack
-					new_report_item = usb_hid_report_item_clone(report_item);
-					list_prepend (&parser->stack, &new_report_item->link);
-					
-					break;
-				case USB_HID_REPORT_TAG_POP:
-					// restore current state from stack
-					if(list_empty (&parser->stack)) {
-						return EINVAL;
-					}
-					
-					report_item = list_get_instance(&parser->stack, usb_hid_report_item_t, link);
-					list_remove (parser->stack.next);
-					
-					break;
-					
-				default:
-					// nothing special to do					
-					break;
-			}
-
-			/* jump over the processed block */
-			i += 1 + USB_HID_ITEM_SIZE(data[i]);
-		}
-		else{
-			// TBD
-			i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
-		}
-		
-
-	}
-	
-	return EOK;
-}
-
-
-/**
- * Parse input report.
- *
- * @param data Data for report
- * @param size Size of report
- * @param callbacks Callbacks for report actions
- * @param arg Custom arguments
- *
- * @return Error code
- */
-int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
-	const usb_hid_report_in_callbacks_t *callbacks, void *arg)
-{
-	int i;
-	usb_hid_report_item_t item;
-
-	/* fill item due to the boot protocol report descriptor */
-	// modifier keys are in the first byte
-	uint8_t modifiers = data[0];
-
-	item.offset = 2; /* second byte is reserved */
-	item.size = 8;
-	item.count = 6;
-	item.usage_minimum = 0;
-	item.usage_maximum = 255;
-	item.logical_minimum = 0;
-	item.logical_maximum = 255;
-
-	if (size != 8) {
-		return -1; //ERANGE;
-	}
-
-	uint8_t keys[6];
-	for (i = 0; i < item.count; i++) {
-		keys[i] = data[i + item.offset];
-	}
-
-	callbacks->keyboard(keys, 6, modifiers, arg);
-	return EOK;
-}
-
-/**
- * Makes output report for keyboard boot protocol
- *
- * @param leds
- * @param output Output report data buffer
- * @param size Size of the output buffer
- * @return Error code
- */
-int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
-{
-	if(size != 1){
-		return -1;
-	}
-
-	/* used only first five bits, others are only padding*/
-	*data = leds;
-	return EOK;
-}
-
-/**
- * Parse one tag of the report descriptor
- *
- * @param Tag to parse
- * @param Report descriptor buffer
- * @param Size of data belongs to this tag
- * @param Current report item structe
- * @return Code of action to be done next
- */
-int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{	
-	int ret;
-	
-	switch(class){
-		case USB_HID_TAG_CLASS_MAIN:
-
-			if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
-				return USB_HID_NEW_REPORT_ITEM;
-			}
-			else {
-				/*TODO process the error */
-				return ret;
-			   }
-			break;
-
-		case USB_HID_TAG_CLASS_GLOBAL:	
-			return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
-			break;
-
-		case USB_HID_TAG_CLASS_LOCAL:			
-			return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
-			break;
-		default:
-			return USB_HID_NO_ACTION;
-	}
-}
-
-/**
- * Parse main tags of report descriptor
- *
- * @param Tag identifier
- * @param Data buffer
- * @param Length of data buffer
- * @param Current state table
- * @return Error code
- */
-
-int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{		
-	switch(tag)
-	{
-		case USB_HID_REPORT_TAG_INPUT:
-		case USB_HID_REPORT_TAG_OUTPUT:
-		case USB_HID_REPORT_TAG_FEATURE:
-			report_item->item_flags = *data;			
-			return EOK;			
-			break;
-			
-		case USB_HID_REPORT_TAG_COLLECTION:
-			usb_hid_report_path_append_item(usage_path, 0, 0);
-						
-			return USB_HID_NO_ACTION;
-			break;
-			
-		case USB_HID_REPORT_TAG_END_COLLECTION:
-			// TODO
-			// znici posledni uroven ve vsech usage paths
-			// otazka jestli nema nicit dve, respektive novou posledni vynulovat?
-			usb_hid_report_remove_last_item(usage_path);
-			return USB_HID_NO_ACTION;
-			break;
-		default:
-			return USB_HID_NO_ACTION;
-	}
-
-	return EOK;
-}
-
-/**
- * Parse global tags of report descriptor
- *
- * @param Tag identifier
- * @param Data buffer
- * @param Length of data buffer
- * @param Current state table
- * @return Error code
- */
-int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{
-	// TODO take care about the bit length of data
-	switch(tag)
-	{
-		case USB_HID_REPORT_TAG_USAGE_PAGE:
-			// zmeni to jenom v poslednim poli aktualni usage path
-			usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL,
-				usb_hid_report_tag_data_int32(data,item_size));
-			break;
-		case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
-			report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
-			report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
-			report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
-			break;			
-		case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
-			report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_UNIT_EXPONENT:
-			report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_UNIT:
-			report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_SIZE:
-			report_item->size = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_COUNT:
-			report_item->count = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_ID:
-			report_item->id = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_PUSH:
-		case USB_HID_REPORT_TAG_POP:
-			return tag;
-			break;
-			
-		default:
-			return USB_HID_NO_ACTION;
-	}
-	
-	return EOK;
-}
-
-/**
- * Parse local tags of report descriptor
- *
- * @param Tag identifier
- * @param Data buffer
- * @param Length of data buffer
- * @param Current state table
- * @return Error code
- */
-int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{
-	switch(tag)
-	{
-		case USB_HID_REPORT_TAG_USAGE:
-			usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL,
-				usb_hid_report_tag_data_int32(data,item_size));
-			break;
-		case USB_HID_REPORT_TAG_USAGE_MINIMUM:
-			report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
-			report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
-			report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
-			report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
-			report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_INDEX:
-			report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_MINIMUM:
-			report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_MAXIMUM:
-			report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
-			break;			
-		case USB_HID_REPORT_TAG_DELIMITER:
-			report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
-			break;
-		
-		default:
-			return USB_HID_NO_ACTION;
-	}
-	
-	return EOK;
-}
-
-/**
- * Converts raw data to int32 (thats the maximum length of short item data)
- *
- * @param Data buffer
- * @param Size of buffer
- * @return Converted int32 number
- */
-int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
-{
-	unsigned int i;
-	int32_t result;
-
-	result = 0;
-	for(i=0; i<size; i++) {
-		result = (result | (data[i]) << (i*8));
-	}
-
-	return result;
-}
-
-
-
-/**
- * Prints content of given list of report items.
- *
- * @param List of report items (usb_hid_report_item_t)
- * @return void
- */
-void usb_hid_descriptor_print_list(link_t *head)
-{
-	usb_hid_report_item_t *report_item;
-	usb_hid_report_usage_path_t *path_item;
-	link_t *path;
-	link_t *item;
-	
-	if(head == NULL || list_empty(head)) {
-	    usb_log_debug("\tempty\n");
-	    return;
-	}
-        
-	for(item = head->next; item != head; item = item->next) {
-                
-		report_item = list_get_instance(item, usb_hid_report_item_t, link);
-
-		usb_log_debug("\tOFFSET: %zX\n", report_item->offset);
-		usb_log_debug("\tCOUNT: %X\n", report_item->count);
-		usb_log_debug("\tSIZE: %X\n", report_item->size);
-		usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
-		usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags));
-		usb_log_debug("\tUSAGE PATH:\n");
-
-		path = report_item->usage_path->link.next;
-		while(path != &report_item->usage_path->link)	{
-			path_item = list_get_instance(path, usb_hid_report_usage_path_t, link);
-			usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage);
-			path = path->next;
-		}
-				
-		usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
-		usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);		
-		usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);		
-		usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);				
-		usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum);
-		usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum);
-		
-		usb_log_debug("\n");		
-
-	}
-
-
-}
-/**
- * Prints content of given report descriptor in human readable format.
- *
- * @param parser Parsed descriptor to print
- * @return void
- */
-void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
-{
-	if(parser == NULL) {
-		return;
-	}
-	
-	usb_log_debug("INPUT:\n");
-	usb_hid_descriptor_print_list(&parser->input);
-	
-	usb_log_debug("OUTPUT: \n");
-	usb_hid_descriptor_print_list(&parser->output);
-	
-	usb_log_debug("FEATURE:\n");	
-	usb_hid_descriptor_print_list(&parser->feature);
-
-}
-
-/**
- * Releases whole linked list of report items
- *
- * @param head Head of list of report descriptor items (usb_hid_report_item_t)
- * @return void
- */
-void usb_hid_free_report_list(link_t *head)
-{
-	return; 
-	
-	usb_hid_report_item_t *report_item;
-	link_t *next;
-	
-	if(head == NULL || list_empty(head)) {		
-	    return;
-	}
-	
-	next = head->next;
-	while(next != head) {
-	
-	    report_item = list_get_instance(next, usb_hid_report_item_t, link);
-
-		while(!list_empty(&report_item->usage_path->link)) {
-			usb_hid_report_remove_last_item(report_item->usage_path);
-		}
-
-		
-	    next = next->next;
-	    
-	    free(report_item);
-	}
-	
-	return;
-	
-}
-
-/** Frees the HID report descriptor parser structure 
- *
- * @param parser Opaque HID report parser structure
- * @return void
- */
-void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
-{
-	if(parser == NULL){
-		return;
-	}
-
-	parser->use_report_id = 0;
-
-	usb_hid_free_report_list(&parser->input);
-	usb_hid_free_report_list(&parser->output);
-	usb_hid_free_report_list(&parser->feature);
-
-	return;
-}
+
+
 
 /** Parse and act upon a HID report.
@@ -688,69 +77,51 @@
  * @param parser Opaque HID report parser structure.
  * @param data Data for the report.
- * @param callbacks Callbacks for report actions.
- * @param arg Custom argument (passed through to the callbacks).
  * @return Error code.
  */ 
-int usb_hid_parse_report(const usb_hid_report_parser_t *parser,  
-    const uint8_t *data, size_t size,
-    usb_hid_report_path_t *path, int flags,
-    const usb_hid_report_in_callbacks_t *callbacks, void *arg)
+int usb_hid_parse_report(const usb_hid_report_t *report,  
+    const uint8_t *data, size_t size, uint8_t *report_id)
 {
 	link_t *list_item;
-	usb_hid_report_item_t *item;
-	uint8_t *keys;
-	uint8_t item_value;
-	size_t key_count=0;
-	size_t i=0;
-	size_t j=0;
-	uint8_t report_id = 0;
-
-	if(parser == NULL) {
+	usb_hid_report_field_t *item;
+
+	usb_hid_report_description_t *report_des;
+	usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
+
+	if(report == NULL) {
 		return EINVAL;
 	}
-	
-	/* get the size of result array */
-	key_count = usb_hid_report_input_length(parser, path, flags);
-
-	if(!(keys = malloc(sizeof(uint8_t) * key_count))){
-		return ENOMEM;
-	}
-
-	if(parser->use_report_id != 0) {
-		report_id = data[0];
-		usb_hid_report_path_set_report_id(path, report_id);
-	}
+
+	if(report->use_report_ids != 0) {
+		*report_id = data[0];
+	}	
+	else {
+		*report_id = 0;
+	}
+
+
+	report_des = usb_hid_report_find_description(report, *report_id, type);
 
 	/* read data */
-	list_item = parser->input.next;	   
-	while(list_item != &(parser->input)) {
-
-		item = list_get_instance(list_item, usb_hid_report_item_t, link);
-
-		if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) && 
-		   (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) {
-			for(j=0; j<(size_t)(item->count); j++) {
-				if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) ||
-				   ((item->usage_minimum == 0) && (item->usage_maximum == 0))) {
-					// variable item
-					keys[i++] = usb_hid_translate_data(item, data,j);
-				}
-				else {
-					// bitmapa
-					if((item_value = usb_hid_translate_data(item, data, j)) != 0) {
-						keys[i++] = (item->count - 1 - j) + item->usage_minimum;
-					}
-					else {
-						keys[i++] = 0;
-					}
-				}
-			}
+	list_item = report_des->report_items.next;	   
+	while(list_item != &(report_des->report_items)) {
+
+		item = list_get_instance(list_item, usb_hid_report_field_t, link);
+
+		if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
+			
+			if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
+
+				// array
+				item->value = usb_hid_translate_data(item, data);
+			    item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
+			}
+			else {
+				// variable item
+				item->value = usb_hid_translate_data(item, data);				
+			}				
 		}
 		list_item = list_item->next;
 	}
-
-	callbacks->keyboard(keys, key_count, report_id, arg);
 	   
-	free(keys);	
 	return EOK;
 	
@@ -758,5 +129,5 @@
 
 /**
- * Translate data from the report as specified in report descriptor
+ * Translate data from the report as specified in report descriptor item
  *
  * @param item Report descriptor item with definition of translation
@@ -765,5 +136,5 @@
  * @return Translated data
  */
-int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
+int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
 {
 	int resolution;
@@ -771,17 +142,18 @@
 	int part_size;
 	
-	int32_t value;
+	int32_t value=0;
 	int32_t mask;
 	const uint8_t *foo;
 
-	// now only common numbers llowed
+	// now only shot tags are allowed
 	if(item->size > 32) {
 		return 0;
 	}
 
-	if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
+	if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
 		item->physical_minimum = item->logical_minimum;
-		item->physical_maximum = item->logical_maximum;		
-	}
+		item->physical_maximum = item->logical_maximum;			
+	}
+	
 
 	if(item->physical_maximum == item->physical_minimum){
@@ -794,37 +166,29 @@
 	}
 
-	offset = item->offset + (j * item->size);
-	if(item->id != 0) {
-		offset += 8;
-		usb_log_debug("MOVED OFFSET BY 1Byte, REPORT_ID(%d)\n", item->id);
-	}
-	
+	offset = item->offset;
 	// FIXME
-	if((offset/8) != ((offset+item->size)/8)) {
-		usb_log_debug2("offset %d\n", offset);
+	if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
 		
 		part_size = ((offset+item->size)%8);
-		usb_log_debug2("part size %d\n",part_size);
-
-		// the higher one
-		foo = data+(offset/8);
-		mask =  ((1 << (item->size-part_size))-1);
-		value = (*foo & mask) << part_size;
-
-		usb_log_debug2("hfoo %x\n", *foo);
-		usb_log_debug2("hmaska %x\n",  mask);
-		usb_log_debug2("hval %d\n", value);		
-
-		// the lower one
-		foo = data+((offset+item->size)/8);
-		mask =  ((1 << part_size)-1) << (8-part_size);
-		value += ((*foo & mask) >> (8-part_size));
-
-		usb_log_debug2("lfoo %x\n", *foo);
-		usb_log_debug2("lmaska %x\n",  mask);
-		usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));		
-		usb_log_debug2("val %d\n", value);
-		
-		
+
+		size_t i=0;
+		for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
+			if(i == (size_t)(offset/8)) {
+				// the higher one
+				foo = data + i;
+				mask =  ((1 << (item->size-part_size))-1);
+				value = (*foo & mask) << part_size;
+			}
+			else if(i == ((offset+item->size-1)/8)){
+				// the lower one
+				foo = data + i;
+				mask =  ((1 << part_size)-1) << (8-part_size);
+				value += ((*foo & mask) >> (8-part_size));
+			}
+			else {
+				value = value << 8;
+				value += *(data + 1);
+			}
+		}
 	}
 	else {		
@@ -832,13 +196,9 @@
 		mask =  ((1 << item->size)-1) << (8-((offset%8)+item->size));
 		value = (*foo & mask) >> (8-((offset%8)+item->size));
-
-		usb_log_debug2("offset %d\n", offset);
-	
-		usb_log_debug2("foo %x\n", *foo);
-		usb_log_debug2("maska %x\n",  mask);
-		usb_log_debug2("val %d\n", value);				
-	}
-
-	usb_log_debug2("---\n\n"); 
+	}
+
+	if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
+		value = USB_HID_UINT32_TO_INT32(value, item->size);
+	}
 
 	return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
@@ -847,325 +207,84 @@
 
 /**
- *
- *
- * @param parser
- * @param path
- * @param flags
- * @return
- */
-size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
+ * Returns number of items in input report which are accessible by given usage path
+ *
+ * @param parser Opaque report descriptor structure
+ * @param path Usage path specification
+ * @param flags Usage path comparison flags
+ * @return Number of items in input report
+ */
+size_t usb_hid_report_input_length(const usb_hid_report_t *report,
 	usb_hid_report_path_t *path, int flags)
 {	
+	
 	size_t ret = 0;
-	link_t *item;
-	usb_hid_report_item_t *report_item;
-
-	if(parser == NULL) {
+
+	if(report == NULL) {
 		return 0;
 	}
-	
-	item = parser->input.next;
-	while(&parser->input != item) {
-		report_item = list_get_instance(item, usb_hid_report_item_t, link);
-		if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
-		   (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
-			ret += report_item->count;
-		}
-
-		item = item->next;
-	} 
+
+	usb_hid_report_description_t *report_des;
+	report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
+	if(report_des == NULL) {
+		return 0;
+	}
+
+	link_t *field_it = report_des->report_items.next;
+	usb_hid_report_field_t *field;
+	while(field_it != &report_des->report_items) {
+
+		field = list_get_instance(field_it, usb_hid_report_field_t, link);
+		if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
+			
+			usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
+			if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
+				ret++;
+			}
+			usb_hid_report_remove_last_item (field->collection_path);
+		}
+		
+		field_it = field_it->next;
+	}
 
 	return ret;
-}
-
-
-/**
- * 
- * @param usage_path
- * @param usage_page
- * @param usage
- * @return
- */
-int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, 
-                                    int32_t usage_page, int32_t usage)
-{	
-	usb_hid_report_usage_path_t *item;
-
-	if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
-		return ENOMEM;
-	}
-	list_initialize(&item->link);
-
-	item->usage = usage;
-	item->usage_page = usage_page;
-	
-	usb_log_debug("Appending usage %d, usage page %d\n", usage, usage_page);
-	
-	list_append (&usage_path->link, &item->link);
-	usage_path->depth++;
-	return EOK;
-}
-
-/**
- *
- * @param usage_path
- * @return
- */
-void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
-{
-	usb_hid_report_usage_path_t *item;
-	
-	if(!list_empty(&usage_path->link)){
-		item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);		
-		list_remove(usage_path->link.prev);
-		usage_path->depth--;
-		free(item);
-	}
-}
-
-/**
- *
- * @param usage_path
- * @return
- */
-void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
-{
-	usb_hid_report_usage_path_t *item;
-	
-	if(!list_empty(&usage_path->link)){	
-		item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
-		memset(item, 0, sizeof(usb_hid_report_usage_path_t));
-	}
-}
-
-/**
- *
- * @param usage_path
- * @param tag
- * @param data
- * @return
- */
-void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
-{
-	usb_hid_report_usage_path_t *item;
-	
-	if(!list_empty(&usage_path->link)){	
-		item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
-
-		switch(tag) {
-			case USB_HID_TAG_CLASS_GLOBAL:
-				item->usage_page = data;
-				break;
-			case USB_HID_TAG_CLASS_LOCAL:
-				item->usage = data;
-				break;
-		}
-	}
-	
-}
-
-/**
- * 
- *
- * @param report_path
- * @param path
- * @param flags
- * @return
- */
-int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, 
-                                      usb_hid_report_path_t *path,
-                                      int flags)
-{
-	usb_hid_report_usage_path_t *report_item;
-	usb_hid_report_usage_path_t *path_item;
-
-	link_t *report_link;
-	link_t *path_link;
-
-	int only_page;
-
-	if(report_path->report_id != path->report_id) {
-		return 1;
-	}
-
-	if(path->depth == 0){
-		return EOK;
-	}
-
-
-	if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
-		flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
-	}
-	
-	switch(flags){
-		/* path must be completly identical */
-		case USB_HID_PATH_COMPARE_STRICT:
-				if(report_path->depth != path->depth){
-					return 1;
-				}
-
-				report_link = report_path->link.next;
-				path_link = path->link.next;
-			
-				while((report_link != &report_path->link) && (path_link != &path->link)) {
-					report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
-					path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);		
-
-					if((report_item->usage_page != path_item->usage_page) || 
-					   ((only_page == 0) && (report_item->usage != path_item->usage))) {
-						   return 1;
-					} else {
-						report_link = report_link->next;
-						path_link = path_link->next;			
-					}
-			
-				}
-
-				if((report_link == &report_path->link) && (path_link == &path->link)) {
-					return EOK;
-				}
-				else {
-					return 1;
-				}						
-			break;
-
-		/* compare with only the end of path*/
-		case USB_HID_PATH_COMPARE_END:
-				report_link = report_path->link.prev;
-				path_link = path->link.prev;
-
-				if(list_empty(&path->link)){
-					return EOK;
-				}
-			
-				while((report_link != &report_path->link) && (path_link != &path->link)) {
-					report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
-					path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);		
-
-					if((report_item->usage_page != path_item->usage_page) || 
-					   ((only_page == 0) && (report_item->usage != path_item->usage))) {
-						   return 1;
-					} else {
-						report_link = report_link->prev;
-						path_link = path_link->prev;			
-					}
-			
-				}
-
-				if(path_link == &path->link) {
-					return EOK;
-				}
-				else {
-					return 1;
-				}						
-			
-			break;
-
-		default:
-			return EINVAL;
-	}
-	
-	
-	
-	
-}
-
-/**
- *
- * @return
- */
-usb_hid_report_path_t *usb_hid_report_path(void)
-{
-	usb_hid_report_path_t *path;
-	path = malloc(sizeof(usb_hid_report_path_t));
-	if(!path){
-		return NULL;
-	}
-	else {
-		path->depth = 0;
-		path->report_id = 0;
-		list_initialize(&path->link);
-		return path;
-	}
-}
-
-/**
- *
- * @param path
- * @return void
- */
-void usb_hid_report_path_free(usb_hid_report_path_t *path)
-{
-	while(!list_empty(&path->link)){
-		usb_hid_report_remove_last_item(path);
-	}
-}
-
-
-/**
- * Clone content of given usage path to the new one
- *
- * @param usage_path
- * @return
- */
-usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
-{
-	usb_hid_report_usage_path_t *path_item;
-	link_t *path_link;
-	usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
-
-	if(new_usage_path == NULL){
-		return NULL;
-	}
-	
-	if(list_empty(&usage_path->link)){
-		return new_usage_path;
-	}
-
-	path_link = usage_path->link.next;
-	while(path_link != &usage_path->link) {
-		path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
-		usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage);
-
-		path_link = path_link->next;
-	}
-
-	return new_usage_path;
-}
-
+	}
 
 /*** OUTPUT API **/
 
-/** Allocates output report buffer
- *
- * @param parser
- * @param size
- * @return
- */
-uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)
-{
-	if(parser == NULL) {
+/** 
+ * Allocates output report buffer for output report
+ *
+ * @param parser Report parsed structure
+ * @param size Size of returned buffer
+ * @param report_id Report id of created output report
+ * @return Returns allocated output buffer for specified output
+ */
+uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
+{
+	if(report == NULL) {
 		*size = 0;
 		return NULL;
 	}
-	
-	// read the last output report item
-	usb_hid_report_item_t *last;
-	link_t *link;
-
-	link = parser->output.prev;
-	if(link != &parser->output) {
-		last = list_get_instance(link, usb_hid_report_item_t, link);
-		*size = (last->offset + (last->size * last->count)) / 8;
-
-		uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));
-		memset(buffer, 0, sizeof(uint8_t) * (*size));
-		usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
-
-		return buffer;
+
+	link_t *report_it = report->reports.next;
+	usb_hid_report_description_t *report_des = NULL;
+	while(report_it != &report->reports) {
+		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
+		if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
+			break;
+		}
+
+		report_it = report_it->next;
+	}
+
+	if(report_des == NULL){
+		*size = 0;
+		return NULL;
 	}
 	else {
-		*size = 0;		
-		return NULL;
+		*size = (report_des->bit_length + (8 - 1))/8;
+		uint8_t *ret = malloc((*size) * sizeof(uint8_t));
+		memset(ret, 0, (*size) * sizeof(uint8_t));
+		return ret;
 	}
 }
@@ -1175,5 +294,5 @@
  *
  * @param output Output report buffer
- * @return
+ * @return void
  */
 void usb_hid_report_output_free(uint8_t *output)
@@ -1187,30 +306,39 @@
 /** Returns size of output for given usage path 
  *
- * @param parser
- * @param path
- * @param flags
- * @return
- */
-size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
+ * @param parser Opaque report parser structure
+ * @param path Usage path specified which items will be thought for the output
+ * @param flags Flags of usage path structure comparison
+ * @return Number of items matching the given usage path
+ */
+size_t usb_hid_report_output_size(usb_hid_report_t *report,
                                   usb_hid_report_path_t *path, int flags)
 {
-	size_t ret = 0;
-	link_t *item;
-	usb_hid_report_item_t *report_item;
-
-	if(parser == NULL) {
+	size_t ret = 0;	
+	usb_hid_report_description_t *report_des;
+
+	if(report == NULL) {
 		return 0;
 	}
 
-	item = parser->output.next;
-	while(&parser->output != item) {
-		report_item = list_get_instance(item, usb_hid_report_item_t, link);
-		if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
-		   (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
-			ret += report_item->count;
-		}
-
-		item = item->next;
-	} 
+	report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
+	if(report_des == NULL){
+		return 0;
+	}
+	
+	link_t *field_it = report_des->report_items.next;
+	usb_hid_report_field_t *field;
+	while(field_it != &report_des->report_items) {
+
+		field = list_get_instance(field_it, usb_hid_report_field_t, link);
+		if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
+			usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
+			if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
+				ret++;
+			}
+			usb_hid_report_remove_last_item (field->collection_path);
+		}
+		
+		field_it = field_it->next;
+	}
 
 	return ret;
@@ -1218,65 +346,55 @@
 }
 
-/** Updates the output report buffer by translated given data 
- *
- * @param parser
- * @param path
- * @param flags
- * @param buffer
- * @param size
- * @param data
- * @param data_size
- * @return
- */
-int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
-                                    usb_hid_report_path_t *path, int flags,
-                                    uint8_t *buffer, size_t size,
-                                    int32_t *data, size_t data_size)
-{
-	usb_hid_report_item_t *report_item;
+/** Makes the output report buffer for data given in the report structure
+ *
+ * @param parser Opaque report parser structure
+ * @param path Usage path specifing which parts of output will be set
+ * @param flags Usage path structure comparison flags
+ * @param buffer Output buffer
+ * @param size Size of output buffer
+ * @return Error code
+ */
+int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
+                                    uint8_t *buffer, size_t size)
+{
 	link_t *item;	
-	size_t idx=0;
-	int i=0;
 	int32_t value=0;
 	int offset;
 	int length;
 	int32_t tmp_value;
-	size_t offset_prefix = 0;
-	
-	if(parser == NULL) {
+	
+	if(report == NULL) {
 		return EINVAL;
 	}
 
-	if(parser->use_report_id != 0) {
-		buffer[0] = path->report_id;
-		offset_prefix = 8;
+	if(report->use_report_ids != 0) {
+		buffer[0] = report_id;		
 	}
 
 	usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
-	usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);
-
-	item = parser->output.next;	
-	while(item != &parser->output) {
-		report_item = list_get_instance(item, usb_hid_report_item_t, link);
-
-		for(i=0; i<report_item->count; i++) {
-
-			if(idx >= data_size) {
-				break;
-			}
-
-			if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
-				((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
+	
+	usb_hid_report_description_t *report_des;
+	report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
+	if(report_des == NULL){
+		return EINVAL;
+	}
+
+	usb_hid_report_field_t *report_item;	
+	item = report_des->report_items.next;	
+	while(item != &report_des->report_items) {
+		report_item = list_get_instance(item, usb_hid_report_field_t, link);
+
+			if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
 					
-//				// variable item
-				value = usb_hid_translate_data_reverse(report_item, data[idx++]);
-				offset = report_item->offset + (i * report_item->size) + offset_prefix;
+				// array
+				value = usb_hid_translate_data_reverse(report_item, report_item->value);
+				offset = report_item->offset;
 				length = report_item->size;
 			}
 			else {
-				//bitmap
-				value += usb_hid_translate_data_reverse(report_item, data[idx++]);
-				offset = report_item->offset + offset_prefix;
-				length = report_item->size * report_item->count;
+				// variable item
+				value  = usb_hid_translate_data_reverse(report_item, report_item->value);
+				offset = report_item->offset;
+				length = report_item->size;
 			}
 
@@ -1297,28 +415,36 @@
 			}
 			else {
-				// je to ve dvou!! FIXME: melo by to umet delsi jak 2
-
-				// konec prvniho -- dolni x bitu
-				tmp_value = value;
-				tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);				
-				tmp_value = tmp_value << (offset%8);
-
+				int i = 0;
 				uint8_t mask = 0;
-				mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
-				buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;
-
-				// a ted druhej -- hornich length-x bitu
-				value = value >> (8 - (offset % 8));
-				value = value & ((1 << (length - (8 - (offset % 8)))) - 1);
+				for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
+					if(i == (offset/8)) {
+						tmp_value = value;
+						tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);				
+						tmp_value = tmp_value << (offset%8);
+	
+						mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
+						buffer[i] = (buffer[i] & mask) | tmp_value;			
+					}
+					else if (i == ((offset + length -1)/8)) {
+						
+						value = value >> (length - ((offset + length) % 8));
+						value = value & ((1 << (length - ((offset + length) % 8))) - 1);
 				
-				mask = ((1 << (length - (8 - (offset % 8)))) - 1);
-				buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;
-			}
-
-		}
-
+						mask = (1 << (length - ((offset + length) % 8))) - 1;
+						buffer[i] = (buffer[i] & mask) | value;
+					}
+					else {
+						buffer[i] = value & (0xFF << i);
+					}
+				}
+			}
+
+
+		// reset value
+		report_item->value = 0;
+		
 		item = item->next;
 	}
-
+	
 	usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
 
@@ -1327,10 +453,10 @@
 
 /**
- *
- * @param item
- * @param value
- * @return
- */
-int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
+ * Translate given data for putting them into the outoput report
+ * @param item Report item structure
+ * @param value Value to translate
+ * @return ranslated value
+ */
+uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
 {
 	int ret=0;
@@ -1341,12 +467,13 @@
 	}
 
+	if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
+		item->physical_minimum = item->logical_minimum;
+		item->physical_maximum = item->logical_maximum;			
+	}
+	
+
 	if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
 
 		// variable item
-		if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
-			item->physical_minimum = item->logical_minimum;
-			item->physical_maximum = item->logical_maximum;
-		}
-
 		if(item->physical_maximum == item->physical_minimum){
 		    resolution = 1;
@@ -1371,19 +498,9 @@
 	}
 
-
-	return ret;
-}
-
-
-int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
-{
-	if(path == NULL){
-		return EINVAL;
-	}
-
-	path->report_id = report_id;
-	return EOK;
-}
-
+	if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
+		return USB_HID_INT32_TO_UINT32(ret, item->size);
+	}
+	return (int32_t)ret;
+}
 
 usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
@@ -1400,4 +517,89 @@
 }
 
+
+usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report, 
+							usb_hid_report_field_t *field, 
+                            usb_hid_report_path_t *path, int flags, 
+                            usb_hid_report_type_t type)
+{
+	usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
+	link_t *field_it;
+	
+	if(report_des == NULL){
+		return NULL;
+	}
+
+	if(field == NULL){
+		// vezmu prvni co mathuje podle path!!
+		field_it = report_des->report_items.next;
+	}
+	else {
+		field_it = field->link.next;
+	}
+
+	while(field_it != &report_des->report_items) {
+		field = list_get_instance(field_it, usb_hid_report_field_t, link);
+
+		if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
+			usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
+			if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
+				usb_hid_report_remove_last_item (field->collection_path);
+				return field;
+			}
+			usb_hid_report_remove_last_item (field->collection_path);
+		}
+		field_it = field_it->next;
+	}
+
+	return NULL;
+}
+
+uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
+{
+	if(report == NULL){
+		return 0;
+	}
+
+	usb_hid_report_description_t *report_des;
+	link_t *report_it;
+	
+	if(report_id == 0) {
+		report_it = usb_hid_report_find_description (report, report_id, type)->link.next;		
+	}
+	else {
+		report_it = report->reports.next;
+	}
+
+	while(report_it != &report->reports) {
+		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
+		if(report_des->type == type){
+			return report_des->report_id;
+		}
+	}
+
+	return 0;
+}
+
+void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
+{
+	if(report_item == NULL)	{
+		return;
+	}
+	
+	report_item->usages_count = 0;
+	memset(report_item->usages, 0, USB_HID_MAX_USAGES);
+	
+	report_item->extended_usage_page = 0;
+	report_item->usage_minimum = 0;
+	report_item->usage_maximum = 0;
+	report_item->designator_index = 0;
+	report_item->designator_minimum = 0;
+	report_item->designator_maximum = 0;
+	report_item->string_index = 0;
+	report_item->string_minimum = 0;
+	report_item->string_maximum = 0;
+
+	return;
+}
 /**
  * @}
Index: uspace/lib/usb/src/hidpath.c
===================================================================
--- uspace/lib/usb/src/hidpath.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usb/src/hidpath.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,388 @@
+/*
+ * Copyright (c) 2011 Matej Klonfar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusb
+ * @{
+ */
+/** @file
+ * HID report descriptor and report data parser implementation.
+ */
+#include <usb/classes/hidparser.h>
+#include <errno.h>
+#include <stdio.h>
+#include <malloc.h>
+#include <mem.h>
+#include <usb/debug.h>
+#include <assert.h>
+
+
+/**
+ * Appends one item (couple of usage_path and usage) into the usage path
+ * structure
+ *
+ * @param usage_path Usage path structure
+ * @param usage_page Usage page constant
+ * @param usage Usage constant
+ * @return Error code
+ */
+int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, 
+                                    int32_t usage_page, int32_t usage)
+{	
+	usb_hid_report_usage_path_t *item;
+
+	if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
+		return ENOMEM;
+	}
+	list_initialize(&item->link);
+
+	item->usage = usage;
+	item->usage_page = usage_page;
+	item->flags = 0;
+	
+	list_append (&item->link, &usage_path->head);
+	usage_path->depth++;
+	return EOK;
+}
+
+/**
+ * Removes last item from the usage path structure
+ * @param usage_path 
+ * @return void
+ */
+void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
+{
+	usb_hid_report_usage_path_t *item;
+	
+	if(!list_empty(&usage_path->head)){
+		item = list_get_instance(usage_path->head.prev, 
+		                         usb_hid_report_usage_path_t, link);		
+		list_remove(usage_path->head.prev);
+		usage_path->depth--;
+		free(item);
+	}
+}
+
+/**
+ * Nulls last item of the usage path structure.
+ *
+ * @param usage_path
+ * @return void
+ */
+void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
+{
+	usb_hid_report_usage_path_t *item;
+	
+	if(!list_empty(&usage_path->head)){	
+		item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
+		memset(item, 0, sizeof(usb_hid_report_usage_path_t));
+	}
+}
+
+/**
+ * Modifies last item of usage path structure by given usage page or usage
+ *
+ * @param usage_path Opaque usage path structure
+ * @param tag Class of currently processed tag (Usage page tag falls into Global
+ * class but Usage tag into the Local)
+ * @param data Value of the processed tag
+ * @return void
+ */
+void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, 
+                                  int32_t tag, int32_t data)
+{
+	usb_hid_report_usage_path_t *item;
+	
+	if(!list_empty(&usage_path->head)){	
+		item = list_get_instance(usage_path->head.prev, 
+		                         usb_hid_report_usage_path_t, link);
+
+		switch(tag) {
+			case USB_HID_TAG_CLASS_GLOBAL:
+				item->usage_page = data;
+				break;
+			case USB_HID_TAG_CLASS_LOCAL:
+				item->usage = data;
+				break;
+		}
+	}
+	
+}
+
+
+void usb_hid_print_usage_path(usb_hid_report_path_t *path)
+{
+	usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
+	usb_log_debug("\tLENGTH: %d\n", path->depth);
+
+	link_t *item = path->head.next;
+	usb_hid_report_usage_path_t *path_item;
+	while(item != &path->head) {
+
+		path_item = list_get_instance(item, usb_hid_report_usage_path_t, link);
+		usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
+		usb_log_debug("\tUSAGE: %X\n", path_item->usage);
+		usb_log_debug("\tFLAGS: %d\n", path_item->flags);		
+		
+		item = item->next;
+	}
+}
+
+/**
+ * Compares two usage paths structures
+ *
+ * If USB_HID_PATH_COMPARE_COLLECTION_ONLY flag is given, the last item in report_path structure is forgotten
+ *
+ * @param report_path usage path structure to compare
+ * @param path usage patrh structure to compare
+ * @param flags Flags determining the mode of comparison
+ * @return EOK if both paths are identical, non zero number otherwise
+ */
+int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, 
+                                      usb_hid_report_path_t *path,
+                                      int flags)
+{
+	usb_hid_report_usage_path_t *report_item;
+	usb_hid_report_usage_path_t *path_item;
+
+	link_t *report_link;
+	link_t *path_link;
+
+	int only_page;
+
+	if(report_path->report_id != path->report_id) {
+		return 1;
+	}
+
+	if(path->depth == 0){
+		return EOK;
+	}
+
+
+	if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
+		flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
+	}
+	
+	switch(flags){
+		/* path must be completly identical */
+		case USB_HID_PATH_COMPARE_STRICT:
+				if(report_path->depth != path->depth){
+					return 1;
+				}
+
+				report_link = report_path->head.next;
+				path_link = path->head.next;
+			
+				while((report_link != &report_path->head) && 
+				      (path_link != &path->head)) {
+						  
+					report_item = list_get_instance(report_link, 
+					                                usb_hid_report_usage_path_t, 
+					                                link);
+						  
+					path_item = list_get_instance(path_link, 
+					                              usb_hid_report_usage_path_t, 
+					                              link);		
+
+					if((report_item->usage_page != path_item->usage_page) || 
+					   ((only_page == 0) && 
+					    (report_item->usage != path_item->usage))) {
+							
+						   return 1;
+					} else {
+						report_link = report_link->next;
+						path_link = path_link->next;			
+					}
+			
+				}
+
+				if(((report_link == &report_path->head) && (path_link == &path->head)) || 
+				   (((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) && 
+				    (path_link = &path->head) && 
+				    (report_link == report_path->head.prev))) {
+					return EOK;
+				}
+				else {
+					return 1;
+				}						
+			break;
+
+		/* compare with only the end of path*/
+		case USB_HID_PATH_COMPARE_END:
+
+				if((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) {
+					report_link = report_path->head.prev->prev;
+				}
+				else {
+					report_link = report_path->head.prev;
+				}
+				path_link = path->head.prev;
+
+				if(list_empty(&path->head)){
+					return EOK;
+				}
+			
+				while((report_link != &report_path->head) && 
+				      (path_link != &path->head)) {
+						  
+					report_item = list_get_instance(report_link, 
+					                                usb_hid_report_usage_path_t, 
+					                                link);
+					path_item = list_get_instance(path_link, 
+					                              usb_hid_report_usage_path_t, 
+					                              link);		
+
+					if((report_item->usage_page != path_item->usage_page) || 
+					   ((only_page == 0) && 
+					    (report_item->usage != path_item->usage))) {
+						   return 1;
+					} else {
+						report_link = report_link->prev;
+						path_link = path_link->prev;			
+					}
+			
+				}
+
+				if(path_link == &path->head) {
+					return EOK;
+				}
+				else {
+					return 1;
+				}						
+			
+			break;
+
+		default:
+			return EINVAL;
+	}
+}
+
+/**
+ * Allocates and initializes new usage path structure.
+ *
+ * @return Initialized usage path structure
+ */
+usb_hid_report_path_t *usb_hid_report_path(void)
+{
+	usb_hid_report_path_t *path;
+	path = malloc(sizeof(usb_hid_report_path_t));
+	if(path == NULL){
+		return NULL;
+	}
+	else {
+		path->depth = 0;
+		path->report_id = 0;
+		list_initialize(&path->link);
+		list_initialize(&path->head);
+		return path;
+	}
+}
+
+/**
+ * Releases given usage path structure.
+ *
+ * @param path usage path structure to release
+ * @return void
+ */
+void usb_hid_report_path_free(usb_hid_report_path_t *path)
+{
+	while(!list_empty(&path->head)){
+		usb_hid_report_remove_last_item(path);
+	}
+
+	list_remove(&path->link);
+	free(path);
+}
+
+
+/**
+ * Clone content of given usage path to the new one
+ *
+ * @param usage_path Usage path structure to clone
+ * @return New copy of given usage path structure
+ */
+usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
+{
+	link_t *path_link;
+	usb_hid_report_usage_path_t *path_item;
+	usb_hid_report_usage_path_t *new_path_item;
+	usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
+
+	if(new_usage_path == NULL){
+		return NULL;
+	}
+
+	new_usage_path->report_id = usage_path->report_id;
+	
+	if(list_empty(&usage_path->head)){
+		return new_usage_path;
+	}
+
+	path_link = usage_path->head.next;
+	while(path_link != &usage_path->head) {
+		path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, 
+		                              link);
+		new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
+		if(new_path_item == NULL) {
+			return NULL;
+		}
+		
+		list_initialize (&new_path_item->link);		
+		new_path_item->usage_page = path_item->usage_page;
+		new_path_item->usage = path_item->usage;		
+		new_path_item->flags = path_item->flags;		
+		
+		list_append(&new_path_item->link, &new_usage_path->head);
+		new_usage_path->depth++;
+
+		path_link = path_link->next;
+	}
+
+	return new_usage_path;
+}
+
+
+/**
+ * Sets report id in usage path structure
+ *
+ * @param path Usage path structure
+ * @param report_id Report id to set
+ * @return Error code
+ */
+int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
+{
+	if(path == NULL){
+		return EINVAL;
+	}
+
+	path->report_id = report_id;
+	return EOK;
+}
+
+/**
+ * @}
+ */
Index: uspace/lib/usb/src/hidreport.c
===================================================================
--- uspace/lib/usb/src/hidreport.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usb/src/hidreport.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -164,7 +164,7 @@
 
 int usb_hid_process_report_descriptor(usb_device_t *dev, 
-    usb_hid_report_parser_t *parser)
+    usb_hid_report_t *report)
 {
-	if (dev == NULL || parser == NULL) {
+	if (dev == NULL || report == NULL) {
 		usb_log_error("Failed to process Report descriptor: wrong "
 		    "parameters given.\n");
@@ -189,5 +189,5 @@
 	assert(report_desc != NULL);
 	
-	rc = usb_hid_parse_report_descriptor(parser, report_desc, report_size);
+	rc = usb_hid_parse_report_descriptor(report, report_desc, report_size);
 	if (rc != EOK) {
 		usb_log_error("Problem parsing Report descriptor: %s.\n",
@@ -197,5 +197,5 @@
 	}
 	
-	usb_hid_descriptor_print(parser);
+	usb_hid_descriptor_print(report);
 	free(report_desc);
 	
Index: uspace/lib/usbvirt/Makefile
===================================================================
--- uspace/lib/usbvirt/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usbvirt/Makefile	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,4 +1,4 @@
 #
-# Copyright (c) 2010 Vojtech Horky
+# Copyright (c) 2011 Vojtech Horky
 # All rights reserved.
 #
@@ -33,10 +33,8 @@
 
 SOURCES = \
-	src/callback.c \
-	src/ctrlpipe.c \
-	src/debug.c \
-	src/main.c \
+	src/ipc.c \
+	src/ctrltransfer.c \
 	src/stdreq.c \
-	src/transaction.c
+	src/transfer.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/usbvirt/include/usbvirt/device.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/device.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usbvirt/include/usbvirt/device.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Vojtech Horky
  * All rights reserved.
  *
@@ -38,120 +38,24 @@
 #include <usb/usb.h>
 #include <usb/request.h>
-#include <usb/descriptor.h>
 
-/** Request type of a control transfer. */
-typedef enum {
-	/** Standard USB request. */
-	USBVIRT_REQUEST_TYPE_STANDARD = 0,
-	/** Standard class USB request. */
-	USBVIRT_REQUEST_TYPE_CLASS = 1
-} usbvirt_request_type_t;
-
-/** Recipient of control request. */
-typedef enum {
-	/** Device is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_DEVICE = 0,
-	/** Interface is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_INTERFACE = 1,
-	/** Endpoint is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_ENDPOINT = 2,
-	/** Other part of the device is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_OTHER = 3
-} usbvirt_request_recipient_t;
-
-/** Possible states of virtual USB device.
- * Notice that these are not 1:1 mappings to those in USB specification.
- */
-typedef enum {
-	/** Default state, device listens at default address. */
-	USBVIRT_STATE_DEFAULT,
-	/** Device has non-default address assigned. */
-	USBVIRT_STATE_ADDRESS,
-	/** Device is configured. */
-	USBVIRT_STATE_CONFIGURED
-} usbvirt_device_state_t;
+#define USBVIRT_ENDPOINT_MAX 16
 
 typedef struct usbvirt_device usbvirt_device_t;
-struct usbvirt_control_transfer;
 
-typedef int (*usbvirt_on_device_request_t)(usbvirt_device_t *dev,
-	usb_device_request_setup_packet_t *request,
-	uint8_t *data);
+typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *, usb_endpoint_t,
+    usb_transfer_type_t, void *, size_t);
+typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *, usb_endpoint_t,
+    usb_transfer_type_t, void *, size_t, size_t *);
+typedef int (*usbvirt_on_control_t)(usbvirt_device_t *,
+    const usb_device_request_setup_packet_t *, uint8_t *, size_t *);
 
-/** Callback for control request over pipe zero.
- *
- * @param dev Virtual device answering the call.
- * @param request Request setup packet.
- * @param data Data when DATA stage is present.
- * @return Error code.
- */
-typedef int (*usbvirt_control_request_callback_t)(usbvirt_device_t *dev,
-	usb_device_request_setup_packet_t *request,
-	uint8_t *data);
-
-/** Handler for control transfer on endpoint zero. */
 typedef struct {
-	/** Request type bitmap.
-	 * Use USBVIRT_MAKE_CONTROL_REQUEST_TYPE for creating the bitmap.
-	 */
-	uint8_t request_type;
-	/** Request code. */
+	usb_direction_t req_direction;
+	usb_request_recipient_t req_recipient;
+	usb_request_type_t req_type;
 	uint8_t request;
-	/** Request name for debugging. */
 	const char *name;
-	/** Callback for the request.
-	 * NULL value here announces end of a list.
-	 */
-	usbvirt_control_request_callback_t callback;
-} usbvirt_control_transfer_handler_t;
-
-/** Create control request type bitmap.
- *
- * @param direction Transfer direction (use usb_direction_t).
- * @param type Request type (use usbvirt_request_type_t).
- * @param recipient Recipient of the request (use usbvirt_request_recipient_t).
- * @return Request type bitmap.
- */
-#define USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, type, recipient) \
-	((((direction) == USB_DIRECTION_IN) ? 1 : 0) << 7) \
-	| (((type) & 3) << 5) \
-	| (((recipient) & 31))
-
-/** Create last item in an array of control request handlers. */
-#define USBVIRT_CONTROL_TRANSFER_HANDLER_LAST { 0, 0, NULL, NULL }
-
-/** Device operations. */
-typedef struct {
-	/** Callbacks for transfers over control pipe zero. */
-	usbvirt_control_transfer_handler_t *control_transfer_handlers;
-
-	int (*on_control_transfer)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, struct usbvirt_control_transfer *transfer);
-	
-	/** Callback for all other incoming data. */
-	int (*on_data)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	
-	/** Callback for host request for data. */
-	int (*on_data_request)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size);
-	
-	/** Decides direction of control transfer. */
-	usb_direction_t (*decide_control_transfer_direction)(
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-	/** Callback when device changes its state.
-	 *
-	 * It is correct that this function is called when both states
-	 * are equal (e.g. this function is called during SET_CONFIGURATION
-	 * request done on already configured device).
-	 *
-	 * @warning The value of <code>dev->state</code> before calling
-	 * this function is not specified (i.e. can be @p old_state or
-	 * @p new_state).
-	 */
-	void (*on_state_change)(usbvirt_device_t *dev,
-	    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
-} usbvirt_device_ops_t;
+	usbvirt_on_control_t callback;
+} usbvirt_control_request_handler_t;
 
 /** Extra configuration data for GET_CONFIGURATION request. */
@@ -179,104 +83,52 @@
 	 */
 	usb_standard_device_descriptor_t *device;
-	
+
 	/** Configurations. */
 	usbvirt_device_configuration_t *configuration;
 	/** Number of configurations. */
 	size_t configuration_count;
-	/** Index of currently selected configuration. */
-	uint8_t current_configuration;
 } usbvirt_descriptors_t;
 
-/** Information about on-going control transfer.
+/** Possible states of virtual USB device.
+ * Notice that these are not 1:1 mappings to those in USB specification.
  */
-typedef struct usbvirt_control_transfer {
-	/** Transfer direction (read/write control transfer). */
-	usb_direction_t direction;
-	/** Request data. */
-	void *request;
-	/** Size of request data. */
-	size_t request_size;
-	/** Payload. */
-	void *data;
-	/** Size of payload. */
-	size_t data_size;
-} usbvirt_control_transfer_t;
+typedef enum {
+	/** Default state, device listens at default address. */
+	USBVIRT_STATE_DEFAULT,
+	/** Device has non-default address assigned. */
+	USBVIRT_STATE_ADDRESS,
+	/** Device is configured. */
+	USBVIRT_STATE_CONFIGURED
+} usbvirt_device_state_t;
 
-typedef enum {
-	USBVIRT_DEBUGTAG_BASE = 1,
-	USBVIRT_DEBUGTAG_TRANSACTION = 2,
-	USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO = 4,
-	USBVIRT_DEBUGTAG_ALL = 255
-} usbvirt_debug_tags_t;
+typedef struct {
+	usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
+	usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
+	usbvirt_control_request_handler_t *control;
+	void (*state_changed)(usbvirt_device_t *dev,
+	    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
+} usbvirt_device_ops_t;
 
-/** Virtual USB device. */
 struct usbvirt_device {
-	/** Callback device operations. */
+	const char *name;
+	void *device_data;
 	usbvirt_device_ops_t *ops;
-	
-	/** Custom device data. */
-	void *device_data;
+	usbvirt_descriptors_t *descriptors;
+	usb_address_t address;
+	usbvirt_device_state_t state;
+};
 
-	/** Reply onto control transfer.
-	 */
-	int (*control_transfer_reply)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	
-	/** Device name.
-	 * Used in debug prints and sent to virtual host controller.
-	 */
-	const char *name;
-	
-	/** Standard descriptors. */
-	usbvirt_descriptors_t *descriptors;
-	
-	/** Current device state. */
-	usbvirt_device_state_t state;
-	
-	/** Device address. */
-	usb_address_t address;
-	/** New device address.
-	 * This field is used during SET_ADDRESS request.
-	 * On all other occasions, it holds invalid address (e.g. -1).
-	 */
-	usb_address_t new_address;
-	
-	/** Process OUT transaction. */
-	int (*transaction_out)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	/** Process SETUP transaction. */
-	int (*transaction_setup)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	/** Process IN transaction. */
-	int (*transaction_in)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size, size_t *data_size);
-	
-	/** State information on control-transfer endpoints. */
-	usbvirt_control_transfer_t current_control_transfers[USB11_ENDPOINT_MAX];
-	
-	/* User debugging. */
-	
-	/** Debug print. */
-	void (*debug)(usbvirt_device_t *dev, int level, uint8_t tag,
-	    const char *format, ...);
-	
-	/** Current debug level. */
-	int debug_level;
-	
-	/** Bitmap of currently enabled tags. */
-	uint8_t debug_enabled_tags;
-	
-	/* Library debugging. */
-	
-	/** Debug print. */
-	void (*lib_debug)(usbvirt_device_t *dev, int level, uint8_t tag,
-	    const char *format, ...);
-	
-	/** Current debug level. */
-	int lib_debug_level;
-	
-	/** Bitmap of currently enabled tags. */
-	uint8_t lib_debug_enabled_tags;
-};
+int usbvirt_device_plug(usbvirt_device_t *, const char *);
+
+void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
+    uint8_t *, size_t *, void *, size_t);
+
+int usbvirt_control_write(usbvirt_device_t *, void *, size_t, void *, size_t);
+int usbvirt_control_read(usbvirt_device_t *, void *, size_t, void *, size_t, size_t *);
+int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
+    void *, size_t);
+int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
+    void *, size_t, size_t *);
+
 
 #endif
Index: uspace/lib/usbvirt/include/usbvirt/hub.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/hub.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,68 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Virtual USB device.
- */
-#ifndef LIBUSBVIRT_HUB_H_
-#define LIBUSBVIRT_HUB_H_
-
-#include "device.h"
-
-/** USB transaction type.
- * This types does not correspond directly to types in USB specification,
- * as actually DATA transactions are marked with these types to identify
- * their direction (and tag).
- */
-typedef enum {
-	USBVIRT_TRANSACTION_SETUP,
-	USBVIRT_TRANSACTION_IN,
-	USBVIRT_TRANSACTION_OUT
-} usbvirt_transaction_type_t;
-
-const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type);
-
-/** Telephony methods of virtual devices. */
-typedef enum {
-	IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD,
-	IPC_M_USBVIRT_TRANSACTION_SETUP,
-	IPC_M_USBVIRT_TRANSACTION_OUT,
-	IPC_M_USBVIRT_TRANSACTION_IN,
-} usbvirt_device_method_t;
-
-int usbvirt_connect(usbvirt_device_t *);
-int usbvirt_connect_local(usbvirt_device_t *);
-int usbvirt_disconnect(usbvirt_device_t *dev);
-
-#endif
-/**
- * @}
- */
Index: uspace/lib/usbvirt/include/usbvirt/ipc.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2010 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ * @brief Virtual USB device.
+ */
+#ifndef LIBUSBVIRT_IPC_H_
+#define LIBUSBVIRT_IPC_H_
+
+#include <ipc/common.h>
+#include <usb/usb.h>
+#include <bool.h>
+
+typedef enum {
+	IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD + 80,
+	IPC_M_USBVIRT_CONTROL_READ,
+	IPC_M_USBVIRT_CONTROL_WRITE,
+	IPC_M_USBVIRT_INTERRUPT_IN,
+	IPC_M_USBVIRT_INTERRUPT_OUT
+} usbvirt_ipc_t;
+
+int usbvirt_ipc_send_control_read(int, usb_endpoint_t, void *, size_t,
+    void *, size_t, size_t *);
+int usbvirt_ipc_send_control_write(int, usb_endpoint_t, void *, size_t,
+    void *, size_t);
+int usbvirt_ipc_send_data_in(int, usb_endpoint_t, usb_transfer_type_t,
+    void *, size_t, size_t *);
+int usbvirt_ipc_send_data_out(int, usb_endpoint_t, usb_transfer_type_t,
+    void *, size_t);
+
+bool usbvirt_is_usbvirt_method(sysarg_t);
+bool usbvirt_ipc_handle_call(usbvirt_device_t *, ipc_callid_t, ipc_call_t *);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usbvirt/src/callback.c
===================================================================
--- uspace/lib/usbvirt/src/callback.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,237 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Callback connection handling.
- */
-#include <devmap.h>
-#include <fcntl.h>
-#include <vfs/vfs.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-
-#include "private.h"
-
-#define NAMESPACE "usb"
-#define USB_MAX_PAYLOAD_SIZE 1020
-
-/** Wrapper for SETUP transaction over telephone. */
-static void handle_setup_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	if (expected_len == 0) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	size_t len = 0;
-	void * buffer = NULL;
-	int rc = async_data_write_accept(&buffer, false,
-	    1, USB_MAX_PAYLOAD_SIZE, 0, &len);
-		
-	if (rc != EOK) {
-		async_answer_0(iid, rc);
-		return;
-	}
-	
-	rc = device->transaction_setup(device, endpoint, buffer, len);
-	
-	async_answer_0(iid, rc);
-}
-
-/** Wrapper for OUT transaction over telephone. */
-static void handle_out_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	int rc = EOK;
-	
-	size_t len = 0;
-	void *buffer = NULL;
-	
-	if (expected_len > 0) {
-		rc = async_data_write_accept(&buffer, false,
-		    1, USB_MAX_PAYLOAD_SIZE, 0, &len);
-			
-		if (rc != EOK) {
-			async_answer_0(iid, rc);
-			return;
-		}
-	}
-	
-	rc = device->transaction_out(device, endpoint, buffer, len);
-	
-	if (buffer != NULL) {
-		free(buffer);
-	}
-	
-	async_answer_0(iid, rc);
-}
-
-
-/** Wrapper for IN transaction over telephone. */
-static void handle_in_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	int rc = EOK;
-	
-	void *buffer = expected_len > 0 ? malloc(expected_len) : NULL;
-	size_t len;
-	
-	rc = device->transaction_in(device, endpoint, buffer, expected_len, &len);
-	/*
-	 * If the request was processed, we will send data back.
-	 */
-	if ((rc == EOK) && (expected_len > 0)) {
-		size_t receive_len;
-		ipc_callid_t callid;
-		if (!async_data_read_receive(&callid, &receive_len)) {
-			async_answer_0(iid, EINVAL);
-			return;
-		}
-		if (len > receive_len) {
-			len = receive_len;
-		}
-		async_data_read_finalize(callid, buffer, len);
-	}
-	
-	async_answer_1(iid, rc, len);
-}
-
-/** Wrapper for getting device name. */
-static void handle_get_name(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	if (device->name == NULL) {
-		async_answer_0(iid, ENOENT);
-	}
-	
-	size_t size = str_size(device->name);
-	
-	ipc_callid_t callid;
-	size_t accepted_size;
-	if (!async_data_read_receive(&callid, &accepted_size)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	if (accepted_size > size) {
-		accepted_size = size;
-	}
-	async_data_read_finalize(callid, device->name, accepted_size);
-	
-	async_answer_1(iid, EOK, accepted_size);
-}
-
-/** Callback connection for a given device. */
-void device_callback_connection(usbvirt_device_t *device, ipc_callid_t iid, ipc_call_t *icall)
-{
-	async_answer_0(iid, EOK);
-	
-	while (true) {
-		ipc_callid_t callid; 
-		ipc_call_t call; 
-		
-		callid = async_get_call(&call);
-		switch (IPC_GET_IMETHOD(call)) {
-			case IPC_M_PHONE_HUNGUP:
-				async_answer_0(callid, EOK);
-				return;
-			
-			case IPC_M_USBVIRT_GET_NAME:
-				handle_get_name(device, callid, call);
-				break;
-			
-			case IPC_M_USBVIRT_TRANSACTION_SETUP:
-				handle_setup_transaction(device, callid, call);
-				break;
-			
-			case IPC_M_USBVIRT_TRANSACTION_OUT:
-				handle_out_transaction(device, callid, call);
-				break;
-				
-			case IPC_M_USBVIRT_TRANSACTION_IN:
-				handle_in_transaction(device, callid, call);
-				break;
-			
-			default:
-				async_answer_0(callid, EINVAL);
-				break;
-		}
-	}
-}
-
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ctrlpipe.c
===================================================================
--- uspace/lib/usbvirt/src/ctrlpipe.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,182 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Device control pipe.
- */
-#include <errno.h>
-
-#include "private.h"
-
-/** Compares handler type with request packet type.
- *
- * @param handler Handler.
- * @param request_packet Request packet.
- * @return Whether handler can serve this packet.
- */
-static bool is_suitable_handler(usbvirt_control_transfer_handler_t *handler,
-    usb_device_request_setup_packet_t *request_packet)
-{
-	return (
-	    (handler->request_type == request_packet->request_type)
-	    && (handler->request == request_packet->request));
-
-}
-
-/** Find suitable transfer handler for given request packet.
- *
- * @param handlers Array of available handlers.
- * @param request_packet Request SETUP packet.
- * @return Handler or NULL.
- */
-static usbvirt_control_transfer_handler_t *find_handler(
-    usbvirt_control_transfer_handler_t *handlers,
-    usb_device_request_setup_packet_t *request_packet)
-{
-	if (handlers == NULL) {
-		return NULL;
-	}
-
-	while (handlers->callback != NULL) {
-		if (is_suitable_handler(handlers, request_packet)) {
-			return handlers;
-		}
-		handlers++;
-	}
-
-	return NULL;
-}
-
-#define _GET_BIT(byte, bit) \
-	(((byte) & (1 << (bit))) ? '1' : '0')
-#define _GET_BITS(byte) \
-	_GET_BIT(byte, 7), _GET_BIT(byte, 6), _GET_BIT(byte, 5), \
-	_GET_BIT(byte, 4), _GET_BIT(byte, 3), _GET_BIT(byte, 2), \
-	_GET_BIT(byte, 1), _GET_BIT(byte, 0)
-
-static int find_and_run_handler(usbvirt_device_t *device,
-    usbvirt_control_transfer_handler_t *handlers,
-    usb_device_request_setup_packet_t *setup_packet,
-    uint8_t *data)
-{
-	int rc = EFORWARD;
-	usbvirt_control_transfer_handler_t *suitable_handler
-	    = find_handler(handlers, setup_packet);
-	if (suitable_handler != NULL) {
-		const char *callback_name = "user handler";
-		if (suitable_handler->name != NULL) {
-			callback_name = suitable_handler->name;
-		}
-		device->lib_debug(device, 1, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-		    "pipe #0 - calling %s " \
-		        "[%c.%c%c.%c%c%c%c%c, R%d, V%d, I%d, L%d]",
-		    callback_name,
-		    _GET_BITS(setup_packet->request_type),
-		    setup_packet->request, setup_packet->value,
-		    setup_packet->index, setup_packet->length);
-		rc = suitable_handler->callback(device, setup_packet, data);
-	}
-
-	return rc;
-}
-#undef _GET_BITS
-#undef _GET_BIT
-
-
-/** Handle communication over control pipe zero.
- */
-int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer)
-{
-	device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-	    "op on control pipe zero (request_size=%u)", transfer->request_size);
-	
-	if (transfer->request_size < sizeof(usb_device_request_setup_packet_t)) {
-		return ENOMEM;
-	}
-	
-	usb_device_request_setup_packet_t *request
-	    = (usb_device_request_setup_packet_t *) transfer->request;
-
-	/*
-	 * First, see whether user provided its own callback.
-	 */
-	int rc = EFORWARD;
-	if (device->ops) {
-		rc = find_and_run_handler(device,
-		    device->ops->control_transfer_handlers,
-		    request, transfer->data);
-	}
-
-	/*
-	 * If there was no user callback or the callback returned EFORWARD,
-	 * we need to run a local handler.
-	 */
-	if (rc == EFORWARD) {
-		rc = find_and_run_handler(device,
-		    control_pipe_zero_local_handlers,
-		    request, transfer->data);
-	}
-	
-	/*
-	 * Check for SET_ADDRESS finalization.
-	 */
-	if (device->new_address != -1) {
-		/*
-		 * TODO: handle when this request is invalid (e.g.
-		 * setting address when in configured state).
-		 */
-		usbvirt_device_state_t new_state;
-		if (device->new_address == 0) {
-			new_state = USBVIRT_STATE_DEFAULT;
-		} else {
-			new_state = USBVIRT_STATE_ADDRESS;
-		}
-		device->address = device->new_address;
-		
-		device->new_address = -1;
-		
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    new_state);
-		}
-		device->state = new_state;
-
-		device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-		    "device address changed to %d (state %s)",
-		    device->address, str_device_state(device->state));
-	}
-	
-	return rc;
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ctrltransfer.c
===================================================================
--- uspace/lib/usbvirt/src/ctrltransfer.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usbvirt/src/ctrltransfer.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,53 @@
+#include "private.h"
+#include <usb/request.h>
+#include <usb/debug.h>
+#include <assert.h>
+#include <errno.h>
+
+int process_control_transfer(usbvirt_device_t *dev,
+    usbvirt_control_request_handler_t *control_handlers,
+    usb_device_request_setup_packet_t *setup,
+    uint8_t *data, size_t *data_sent_size)
+{
+	assert(dev);
+	assert(setup);
+
+	if (control_handlers == NULL) {
+		return EFORWARD;
+	}
+
+	usb_direction_t direction = setup->request_type & 128 ?
+	    USB_DIRECTION_IN : USB_DIRECTION_OUT;
+	usb_request_recipient_t req_recipient = setup->request_type & 31;
+	usb_request_type_t req_type = (setup->request_type >> 5) & 3;
+
+	usbvirt_control_request_handler_t *handler = control_handlers;
+	while (handler->callback != NULL) {
+		if (handler->req_direction != direction) {
+			goto next;
+		}
+		if (handler->req_recipient != req_recipient) {
+			goto next;
+		}
+		if (handler->req_type != req_type) {
+			goto next;
+		}
+		if (handler->request != setup->request) {
+			goto next;
+		}
+
+		usb_log_debug("Control transfer: %s(%s)\n", handler->name,
+		    usb_debug_str_buffer((uint8_t*) setup, sizeof(*setup), 0));
+		int rc = handler->callback(dev, setup, data, data_sent_size);
+		if (rc == EFORWARD) {
+			goto next;
+		}
+
+		return rc;
+
+next:
+		handler++;
+	}
+
+	return EFORWARD;
+}
Index: uspace/lib/usbvirt/src/debug.c
===================================================================
--- uspace/lib/usbvirt/src/debug.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,103 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Debugging support.
- */
-#include <stdio.h>
-#include <bool.h>
-
-#include "private.h"
-
-
-static void debug_print(int level, uint8_t tag,
-    int current_level, uint8_t enabled_tags,
-    const char *format, va_list args)
-{
-	if (level > current_level) {
-		return;
-	}
-	
-	if ((tag & enabled_tags) == 0) {
-		return;
-	}
-	
-	bool print_prefix = true;
-	
-	if ((format[0] == '%') && (format[1] == 'M')) {
-		format += 2;
-		print_prefix = false;
-	}
-	
-	if (print_prefix) {
-		printf("[vusb]: ");
-		while (--level > 0) {
-			printf(" ");
-		}
-	}
-	
-	vprintf(format, args);
-	
-	if (print_prefix) {
-		printf("\n");
-	}
-}
-
-
-void user_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...)
-{
-	va_list args;
-	va_start(args, format);
-	
-	debug_print(level, tag,
-	    device->debug_level, device->debug_enabled_tags,
-	    format, args);
-	
-	va_end(args);
-}
-
-void lib_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...)
-{
-	va_list args;
-	va_start(args, format);
-	
-	debug_print(level, tag,
-	    device->lib_debug_level, device->lib_debug_enabled_tags,
-	    format, args);
-	
-	va_end(args);
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ipc.c
===================================================================
--- uspace/lib/usbvirt/src/ipc.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usbvirt/src/ipc.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,455 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ *
+ */
+#include <errno.h>
+#include <str.h>
+#include <stdio.h>
+#include <assert.h>
+#include <async.h>
+#include <devman.h>
+#include <usbvirt/device.h>
+#include <usbvirt/ipc.h>
+
+#include <usb/debug.h>
+
+static usbvirt_device_t *DEV = NULL;
+
+static void ipc_get_name(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	if (dev->name == NULL) {
+		async_answer_0(iid, ENOENT);
+	}
+
+	size_t size = str_size(dev->name);
+
+	ipc_callid_t callid;
+	size_t accepted_size;
+	if (!async_data_read_receive(&callid, &accepted_size)) {
+		async_answer_0(iid, EINVAL);
+		return;
+	}
+
+	if (accepted_size > size) {
+		accepted_size = size;
+	}
+	async_data_read_finalize(callid, dev->name, accepted_size);
+
+	async_answer_1(iid, EOK, accepted_size);
+}
+
+static void ipc_control_read(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	//usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+
+	int rc;
+
+	void *setup_packet = NULL;
+	size_t setup_packet_len = 0;
+	size_t data_len = 0;
+
+	rc = async_data_write_accept(&setup_packet, false,
+	    1, 1024, 0, &setup_packet_len);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	ipc_callid_t data_callid;
+	if (!async_data_read_receive(&data_callid, &data_len)) {
+		async_answer_0(iid, EPARTY);
+		free(setup_packet);
+		return;
+	}
+
+	void *buffer = malloc(data_len);
+	if (buffer == NULL) {
+		async_answer_0(iid, ENOMEM);
+		free(setup_packet);
+		return;
+	}
+
+	size_t actual_len;
+	rc = usbvirt_control_read(dev, setup_packet, setup_packet_len,
+	    buffer, data_len, &actual_len);
+
+	if (rc != EOK) {
+		async_answer_0(data_callid, rc);
+		async_answer_0(iid, rc);
+		free(setup_packet);
+		free(buffer);
+		return;
+	}
+
+	async_data_read_finalize(data_callid, buffer, actual_len);
+	async_answer_0(iid, EOK);
+
+	free(setup_packet);
+	free(buffer);
+}
+
+static void ipc_control_write(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	size_t data_buffer_len = IPC_GET_ARG2(*icall);
+	int rc;
+
+	void *setup_packet = NULL;
+	void *data_buffer = NULL;
+	size_t setup_packet_len = 0;
+
+	rc = async_data_write_accept(&setup_packet, false,
+	    1, 1024, 0, &setup_packet_len);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	if (data_buffer_len > 0) {
+		rc = async_data_write_accept(&data_buffer, false,
+		    1, 1024, 0, &data_buffer_len);
+		if (rc != EOK) {
+			async_answer_0(iid, rc);
+			free(setup_packet);
+			return;
+		}
+	}
+
+	rc = usbvirt_control_write(dev, setup_packet, setup_packet_len,
+	    data_buffer, data_buffer_len);
+
+	async_answer_0(iid, rc);
+}
+
+static void ipc_interrupt_in(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+	usb_transfer_type_t transfer_type = IPC_GET_ARG2(*icall);
+
+	int rc;
+
+	size_t data_len = 0;
+	ipc_callid_t data_callid;
+	if (!async_data_read_receive(&data_callid, &data_len)) {
+		async_answer_0(iid, EPARTY);
+		return;
+	}
+
+	void *buffer = malloc(data_len);
+	if (buffer == NULL) {
+		async_answer_0(iid, ENOMEM);
+		return;
+	}
+
+	size_t actual_len;
+	rc = usbvirt_data_in(dev, transfer_type, endpoint,
+	    buffer, data_len, &actual_len);
+
+	if (rc != EOK) {
+		async_answer_0(data_callid, rc);
+		async_answer_0(iid, rc);
+		free(buffer);
+		return;
+	}
+
+	async_data_read_finalize(data_callid, buffer, actual_len);
+	async_answer_0(iid, EOK);
+
+	free(buffer);
+}
+
+static void ipc_interrupt_out(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+	usb_transfer_type_t transfer_type = IPC_GET_ARG2(*icall);
+
+	void *data_buffer = NULL;
+	size_t data_buffer_size = 0;
+
+	int rc = async_data_write_accept(&data_buffer, false,
+	    1, 1024, 0, &data_buffer_size);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	rc = usbvirt_data_out(dev, transfer_type, endpoint,
+	    data_buffer, data_buffer_size);
+
+	async_answer_0(iid, rc);
+
+	free(data_buffer);
+}
+
+
+bool usbvirt_ipc_handle_call(usbvirt_device_t *dev,
+    ipc_callid_t callid, ipc_call_t *call)
+{
+	switch (IPC_GET_IMETHOD(*call)) {
+		case IPC_M_USBVIRT_GET_NAME:
+			ipc_get_name(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_CONTROL_READ:
+			ipc_control_read(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_CONTROL_WRITE:
+			ipc_control_write(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_INTERRUPT_IN:
+			ipc_interrupt_in(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_INTERRUPT_OUT:
+			ipc_interrupt_out(dev, callid, call);
+			break;
+
+		default:
+			return false;
+	}
+
+	return true;
+}
+
+static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	assert(DEV != NULL);
+
+	async_answer_0(iid, EOK);
+
+	while (true) {
+		ipc_callid_t callid;
+		ipc_call_t call;
+
+		callid = async_get_call(&call);
+		bool processed = usbvirt_ipc_handle_call(DEV, callid, &call);
+		if (!processed) {
+			switch (IPC_GET_IMETHOD(call)) {
+				case IPC_M_PHONE_HUNGUP:
+					async_answer_0(callid, EOK);
+					return;
+				default:
+					async_answer_0(callid, EINVAL);
+					break;
+			}
+		}
+	}
+}
+
+int usbvirt_device_plug(usbvirt_device_t *dev, const char *vhc_path)
+{
+	int rc;
+	devman_handle_t handle;
+
+	rc = devman_device_get_handle(vhc_path, &handle, 0);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	int hcd_phone = devman_device_connect(handle, 0);
+
+	if (hcd_phone < 0) {
+		return hcd_phone;
+	}
+
+	DEV = dev;
+
+	rc = async_connect_to_me(hcd_phone, 0, 0, 0, callback_connection);
+	if (rc != EOK) {
+		DEV = NULL;
+		return rc;
+	}
+
+
+
+	return EOK;
+}
+
+
+
+int usbvirt_ipc_send_control_read(int phone, usb_endpoint_t ep,
+    void *setup_buffer, size_t setup_buffer_size,
+    void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
+{
+	aid_t opening_request = async_send_1(phone,
+	    IPC_M_USBVIRT_CONTROL_READ, ep, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    setup_buffer, setup_buffer_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	ipc_call_t data_request_call;
+	aid_t data_request = async_data_read(phone,
+	    data_buffer, data_buffer_size,
+	    &data_request_call);
+
+	if (data_request == 0) {
+		async_wait_for(opening_request, NULL);
+		return ENOMEM;
+	}
+
+	sysarg_t data_request_rc;
+	sysarg_t opening_request_rc;
+	async_wait_for(data_request, &data_request_rc);
+	async_wait_for(opening_request, &opening_request_rc);
+
+	if (data_request_rc != EOK) {
+		/* Prefer the return code of the opening request. */
+		if (opening_request_rc != EOK) {
+			return (int) opening_request_rc;
+		} else {
+			return (int) data_request_rc;
+		}
+	}
+	if (opening_request_rc != EOK) {
+		return (int) opening_request_rc;
+	}
+
+	*data_transfered_size = IPC_GET_ARG2(data_request_call);
+
+	return EOK;
+}
+
+int usbvirt_ipc_send_control_write(int phone, usb_endpoint_t ep,
+    void *setup_buffer, size_t setup_buffer_size,
+    void *data_buffer, size_t data_buffer_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_CONTROL_WRITE, ep, data_buffer_size,  NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    setup_buffer, setup_buffer_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	if (data_buffer_size > 0) {
+		rc = async_data_write_start(phone,
+		    data_buffer, data_buffer_size);
+
+		if (rc != EOK) {
+			async_wait_for(opening_request, NULL);
+			return rc;
+		}
+	}
+
+	sysarg_t opening_request_rc;
+	async_wait_for(opening_request, &opening_request_rc);
+
+	return (int) opening_request_rc;
+}
+
+int usbvirt_ipc_send_data_in(int phone, usb_endpoint_t ep,
+    usb_transfer_type_t tr_type, void *data, size_t data_size, size_t *act_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_INTERRUPT_IN, ep, tr_type, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	ipc_call_t data_request_call;
+	aid_t data_request = async_data_read(phone,
+	    data, data_size,  &data_request_call);
+
+	if (data_request == 0) {
+		async_wait_for(opening_request, NULL);
+		return ENOMEM;
+	}
+
+	sysarg_t data_request_rc;
+	sysarg_t opening_request_rc;
+	async_wait_for(data_request, &data_request_rc);
+	async_wait_for(opening_request, &opening_request_rc);
+
+	if (data_request_rc != EOK) {
+		/* Prefer the return code of the opening request. */
+		if (opening_request_rc != EOK) {
+			return (int) opening_request_rc;
+		} else {
+			return (int) data_request_rc;
+		}
+	}
+	if (opening_request_rc != EOK) {
+		return (int) opening_request_rc;
+	}
+
+	if (act_size != NULL) {
+		*act_size = IPC_GET_ARG2(data_request_call);
+	}
+
+	return EOK;
+}
+
+int usbvirt_ipc_send_data_out(int phone, usb_endpoint_t ep,
+    usb_transfer_type_t tr_type, void *data, size_t data_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_INTERRUPT_OUT, ep, tr_type, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    data, data_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	sysarg_t opening_request_rc;
+	async_wait_for(opening_request, &opening_request_rc);
+
+	return (int) opening_request_rc;
+}
+
+
+/**
+ * @}
+ */
Index: uspace/lib/usbvirt/src/main.c
===================================================================
--- uspace/lib/usbvirt/src/main.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,284 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Device registration with virtual USB framework.
- */
-#include <devman.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-#include <assert.h>
-
-#include "private.h"
-
-#define NAMESPACE "usb"
-
-/** Virtual device wrapper. */
-typedef struct {
-	/** Actual device. */
-	usbvirt_device_t *device;
-	/** Phone to host controller. */
-	int vhcd_phone;
-	/** Device id. */
-	sysarg_t id;
-	/** Linked-list member. */
-	link_t link;
-} virtual_device_t;
-
-/*** List of known device. */
-static LIST_INITIALIZE(device_list);
-
-/** Find virtual device wrapper based on the contents. */
-static virtual_device_t *find_device(usbvirt_device_t *device)
-{
-	if (list_empty(&device_list)) {
-		return NULL;
-	}
-	
-	link_t *pos;
-	for (pos = device_list.next; pos != &device_list; pos = pos->next) {
-		virtual_device_t *dev
-		    = list_get_instance(pos, virtual_device_t, link);
-		if (dev->device == device) {
-			return dev;
-		}
-	}
-	
-	return NULL;
-}
-
-/** Find virtual device wrapper by its id. */
-static virtual_device_t *find_device_by_id(sysarg_t id)
-{
-	if (list_empty(&device_list)) {
-		return NULL;
-	}
-	
-	link_t *pos;
-	for (pos = device_list.next; pos != &device_list; pos = pos->next) {
-		virtual_device_t *dev
-		    = list_get_instance(pos, virtual_device_t, link);
-		if (dev->id == id) {
-			return dev;
-		}
-	}
-	
-	return NULL;
-}
-
-/** Reply to a control transfer. */
-static int control_transfer_reply(usbvirt_device_t *device,
-	    usb_endpoint_t endpoint, void *buffer, size_t size)
-{
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->data != NULL) {
-		free(transfer->data);
-	}
-	transfer->data = malloc(size);
-	memcpy(transfer->data, buffer, size);
-	transfer->data_size = size;
-	
-	return EOK;
-}
-
-/** Initialize virtual device. */
-static void device_init(usbvirt_device_t *dev)
-{
-	dev->transaction_out = transaction_out;
-	dev->transaction_setup = transaction_setup;
-	dev->transaction_in = transaction_in;
-	
-	dev->control_transfer_reply = control_transfer_reply;
-	
-	dev->debug = user_debug;
-	dev->lib_debug = lib_debug;
-	
-	dev->state = USBVIRT_STATE_DEFAULT;
-	dev->address = 0;
-	dev->new_address = -1;
-	
-	size_t i;
-	for (i = 0; i < USB11_ENDPOINT_MAX; i++) {
-		usbvirt_control_transfer_t *transfer = &dev->current_control_transfers[i];
-		transfer->direction = 0;
-		transfer->request = NULL;
-		transfer->request_size = 0;
-		transfer->data = NULL;
-		transfer->data_size = 0;
-	}
-}
-
-/** Add a virtual device.
- * The returned device (if not NULL) shall be destroy via destroy_device().
- */
-static virtual_device_t *add_device(usbvirt_device_t *dev)
-{
-	assert(find_device(dev) == NULL);
-	virtual_device_t *new_device
-	    = (virtual_device_t *) malloc(sizeof(virtual_device_t));
-	
-	new_device->device = dev;
-	link_initialize(&new_device->link);
-	
-	list_append(&new_device->link, &device_list);
-	
-	return new_device;
-}
-
-/** Destroy virtual device. */
-static void destroy_device(virtual_device_t *dev)
-{
-	if (dev->vhcd_phone > 0) {
-		async_hangup(dev->vhcd_phone);
-	}
-	
-	list_remove(&dev->link);
-	
-	free(dev);
-}
-
-/** Callback connection handler. */
-static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
-{
-	// FIXME - determine which device just called back
-	virtual_device_t *dev = find_device_by_id(0);
-	if (dev == NULL) {
-		async_answer_0(iid, EINVAL);
-		printf("Ooops\n");
-		return;
-	}
-
-	device_callback_connection(dev->device, iid, icall);
-}
-
-/** Create necessary phones for communication with virtual HCD.
- * This function wraps following calls:
- * -# open <code>/dev/devices/\\virt\\usbhc</code> for reading
- * -# access phone of file opened in previous step
- * -# create callback through just opened phone
- * -# create handler for calling on data from host to function
- * -# return the (outgoing) phone
- *
- * @warning This function is wrapper for several actions and therefore
- * it is not possible - in case of error - to determine at which point
- * error occurred.
- *
- * @param dev Device to connect.
- * @return EOK on success or error code from errno.h.
- */
-int usbvirt_connect(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device != NULL) {
-		return EEXISTS;
-	}
-	
-	const char *vhc_path = "/virt/usbhc/hc";
-	int rc;
-	devman_handle_t handle;
-
-	rc = devman_device_get_handle(vhc_path, &handle, 0);
-	if (rc != EOK) {
-		printf("devman_device_get_handle() failed\n");
-		return rc;
-	}
-	
-	int hcd_phone = devman_device_connect(handle, 0);
-	
-	if (hcd_phone < 0) {
-		printf("devman_device_connect() failed\n");
-		return hcd_phone;
-	}
-	
-	rc = async_connect_to_me(hcd_phone, 0, 0, 0, callback_connection);
-	if (rc != EOK) {
-		printf("ipc_connect_to_me() failed\n");
-		return rc;
-	}
-	
-	device_init(dev);
-	
-	virtual_device = add_device(dev);
-	virtual_device->vhcd_phone = hcd_phone;
-	virtual_device->id = 0;
-	
-	return EOK;
-}
-
-/** Prepares device as local.
- * This is useful if you want to have a virtual device in the same task
- * as HCD.
- *
- * @param dev Device to connect.
- * @return Error code.
- * @retval EOK Device connected.
- * @retval EEXISTS This device is already connected.
- */
-int usbvirt_connect_local(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device != NULL) {
-		return EEXISTS;
-	}
-	
-	device_init(dev);
-	
-	virtual_device = add_device(dev);
-	virtual_device->vhcd_phone = -1;
-	virtual_device->id = 0;
-	
-	return EOK;
-}
-
-/** Disconnects device from HCD.
- *
- * @param dev Device to be disconnected.
- * @return Error code.
- * @retval EOK Device connected.
- * @retval ENOENT This device is not connected.
- */
-int usbvirt_disconnect(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device == NULL) {
-		return ENOENT;
-	}
-	
-	destroy_device(virtual_device);
-	
-	return EOK;
-}
-
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/private.h
===================================================================
--- uspace/lib/usbvirt/src/private.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usbvirt/src/private.h	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,94 +1,8 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
+#include <usbvirt/device.h>
 
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Virtual USB private header.
- */
-#ifndef LIBUSBVIRT_PRIVATE_H_
-#define LIBUSBVIRT_PRIVATE_H_
+int process_control_transfer(usbvirt_device_t *,
+    usbvirt_control_request_handler_t *,
+    usb_device_request_setup_packet_t *,
+    uint8_t *, size_t *);
 
-#include <usbvirt/device.h>
-#include <usbvirt/hub.h>
-#include <assert.h>
-
-
-#define DEVICE_HAS_OP(dev, op) \
-	( \
-		(  ((dev)->ops) != NULL  ) \
-		&& \
-		(  ((dev)->ops->op) != NULL  ) \
-	)
-
-int usbvirt_data_to_host(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-int handle_incoming_data(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer);
-
-int handle_std_request(usbvirt_device_t *device, usb_device_request_setup_packet_t *request, uint8_t *data);
-
-void device_callback_connection(usbvirt_device_t *device, ipc_callid_t iid, ipc_call_t *icall);
-
-int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size);
-int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size);
-int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *data_size);
-
-
-void user_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...);
-void lib_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...);
-    
-static inline const char *str_device_state(usbvirt_device_state_t state)
-{
-	switch (state) {
-		case USBVIRT_STATE_DEFAULT:
-			return "default";
-		case USBVIRT_STATE_ADDRESS:
-			return "address";
-		case USBVIRT_STATE_CONFIGURED:
-			return "configured";
-		default:
-			return "unknown";
-	}
-}
-
-extern usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[];
-
-#endif
-/**
- * @}
- */
+extern usbvirt_control_request_handler_t library_handlers[];
Index: uspace/lib/usbvirt/src/stdreq.c
===================================================================
--- uspace/lib/usbvirt/src/stdreq.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/usbvirt/src/stdreq.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -1,70 +1,44 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
+#include "private.h"
+#include <usb/request.h>
+#include <assert.h>
+#include <errno.h>
 
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Preprocessing of standard device requests.
- */
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-#include <usb/request.h>
+void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size,
+    void *actual_data, size_t actual_data_size)
+{
+	size_t expected_size = setup_packet->length;
+	if (expected_size < actual_data_size) {
+		actual_data_size = expected_size;
+	}
 
-#include "private.h"
+	memcpy(data, actual_data, actual_data_size);
 
-/*
- * All sub handlers must return EFORWARD to inform the caller that
- * they were not able to process the request (yes, it is abuse of
- * this error code but such error code shall not collide with anything
- * else in this context).
- */
- 
+	if (act_size != NULL) {
+		*act_size = actual_data_size;
+	}
+}
+
 /** GET_DESCRIPTOR handler. */
-static int handle_get_descriptor(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint8_t type = setup_packet->value_high;
 	uint8_t index = setup_packet->value_low;
 
-	/* 
+	/*
 	 * Standard device descriptor.
 	 */
 	if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
 		if (device->descriptors && device->descriptors->device) {
-			return device->control_transfer_reply(device, 0,
+			usbvirt_control_reply_helper(setup_packet, data, act_size,
 			    device->descriptors->device,
 			    device->descriptors->device->length);
+			return EOK;
 		} else {
 			return EFORWARD;
 		}
 	}
-	
+
 	/*
 	 * Configuration descriptor together with interface, endpoint and
@@ -85,5 +59,5 @@
 			return ENOMEM;
 		}
-		
+
 		uint8_t *ptr = all_data;
 		memcpy(ptr, config->descriptor, config->descriptor->length);
@@ -96,19 +70,18 @@
 			ptr += extra->length;
 		}
-		
-		int rc = device->control_transfer_reply(device, 0,
+
+		usbvirt_control_reply_helper(setup_packet, data, act_size,
 		    all_data, config->descriptor->total_length);
-		
+
 		free(all_data);
-		
-		return rc;
+
+		return EOK;
 	}
-	
+
 	return EFORWARD;
 }
 
-/** SET_ADDRESS handler. */
-static int handle_set_address(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_set_address(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint16_t new_address = setup_packet->value;
@@ -119,17 +92,16 @@
 		return EINVAL;
 	}
-	
+
 	if (new_address > 127) {
 		return EINVAL;
 	}
-	
-	device->new_address = new_address;
-	
+
+	device->address = new_address;
+
 	return EOK;
 }
 
-/** SET_CONFIGURATION handler. */
-static int handle_set_configuration(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_set_configuration(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint16_t configuration_value = setup_packet->value;
@@ -140,5 +112,5 @@
 		return EINVAL;
 	}
-	
+
 	/*
 	 * Configuration value is 1 byte information.
@@ -147,5 +119,5 @@
 		return EINVAL;
 	}
-	
+
 	/*
 	 * Do nothing when in default state. According to specification,
@@ -155,60 +127,48 @@
 		return EOK;
 	}
-	
+
+	usbvirt_device_state_t new_state;
 	if (configuration_value == 0) {
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    USBVIRT_STATE_ADDRESS);
-		}
-		device->state = USBVIRT_STATE_ADDRESS;
+		new_state = USBVIRT_STATE_ADDRESS;
 	} else {
-		/*
-		* TODO: browse provided configurations and verify that
-		* user selected existing configuration.
-		*/
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    USBVIRT_STATE_CONFIGURED);
-		}
-		device->state = USBVIRT_STATE_CONFIGURED;
-		if (device->descriptors) {
-			device->descriptors->current_configuration
-			    = configuration_value;
-		}
+		// FIXME: check that this configuration exists
+		new_state = USBVIRT_STATE_CONFIGURED;
 	}
-		
+
+	if (device->ops && device->ops->state_changed) {
+		device->ops->state_changed(device, device->state, new_state);
+	}
+	device->state = new_state;
+
 	return EOK;
 }
 
-
-#define MAKE_BM_REQUEST(direction, recipient) \
-	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_STANDARD, recipient)
-#define MAKE_BM_REQUEST_DEV(direction) \
-	MAKE_BM_REQUEST(direction, USBVIRT_REQUEST_RECIPIENT_DEVICE)
-
-usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[] = {
+usbvirt_control_request_handler_t library_handlers[] = {
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_IN),
-		.request = USB_DEVREQ_GET_DESCRIPTOR,
-		.name = "GetDescriptor()",
-		.callback = handle_get_descriptor
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.request = USB_DEVREQ_SET_ADDRESS,
+		.name = "SetAddress",
+		.callback = req_set_address
 	},
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
-		.request = USB_DEVREQ_SET_ADDRESS,
-		.name = "SetAddress()",
-		.callback = handle_set_address
+		.req_direction = USB_DIRECTION_IN,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.request = USB_DEVREQ_GET_DESCRIPTOR,
+		.name = "GetDescriptor",
+		.callback = req_get_descriptor
 	},
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
 		.request = USB_DEVREQ_SET_CONFIGURATION,
-		.name = "SetConfiguration()",
-		.callback = handle_set_configuration
+		.name = "SetConfiguration",
+		.callback = req_set_configuration
 	},
-	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
+
+	{ .callback = NULL }
 };
 
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/transaction.c
===================================================================
--- uspace/lib/usbvirt/src/transaction.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,268 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Transaction processing.
- */
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-
-#include "private.h"
-
-static usb_direction_t setup_transaction_direction(usbvirt_device_t *,
-    usb_endpoint_t, void *, size_t);
-static void process_control_transfer(usbvirt_device_t *,
-    usb_endpoint_t, usbvirt_control_transfer_t *);
-
-/** Convert virtual USB transaction type to string.
- */
-const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type)
-{
-	switch (type) {
-		case USBVIRT_TRANSACTION_SETUP:
-			return "setup";
-		case USBVIRT_TRANSACTION_IN:
-			return "in";
-		case USBVIRT_TRANSACTION_OUT:
-			return "out";
-		default:
-			return "unknown";
-	}
-}
-
-/** SETUP transaction handling.
- * The setup transaction only prepares control transfer on given endpoint.
- */
-int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "setup transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	
-	if (transfer->request != NULL) {
-		free(transfer->request);
-	}
-	if (transfer->data != NULL) {
-		free(transfer->data);
-	}
-	
-	transfer->direction = setup_transaction_direction(device, endpoint,
-	    buffer, size);
-	transfer->request = malloc(size);
-	memcpy(transfer->request, buffer, size);
-	transfer->request_size = size;
-	transfer->data = NULL;
-	transfer->data_size = 0;
-	
-	if (transfer->direction == USB_DIRECTION_IN) {
-		process_control_transfer(device, endpoint, transfer);
-	}
-	
-	return EOK;
-}
-
-/** OUT transaction handling.
- * The OUT transaction can trigger processing of a control transfer.
- */
-int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "out transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	/*
-	 * First check whether it is a transaction over control pipe.
-	 */
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->request != NULL) {
-		if (transfer->direction == USB_DIRECTION_OUT) {
-			/*
-			 * For out transactions, append the data to the buffer.
-			 */
-			uint8_t *new_buffer = (uint8_t *) malloc(transfer->data_size + size);
-			if (transfer->data) {
-				memcpy(new_buffer, transfer->data, transfer->data_size);
-			}
-			memcpy(new_buffer + transfer->data_size, buffer, size);
-			
-			if (transfer->data) {
-				free(transfer->data);
-			}
-			transfer->data = new_buffer;
-			transfer->data_size += size;
-		} else {
-			/*
-			 * For in transactions, this means end of the
-			 * transaction.
-			 */
-			free(transfer->request);
-			if (transfer->data) {
-				free(transfer->data);
-			}
-			transfer->request = NULL;
-			transfer->request_size = 0;
-			transfer->data = NULL;
-			transfer->data_size = 0;
-		}
-		
-		return EOK;
-	}
-	
-	/*
-	 * Otherwise, announce that some data has come.
-	 */
-	if (device->ops && device->ops->on_data) {
-		return device->ops->on_data(device, endpoint, buffer, size);
-	} else {
-		return ENOTSUP;
-	}
-}
-
-/** IN transaction handling.
- * The IN transaction can trigger processing of a control transfer.
- */
-int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *data_size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "in transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	/*
-	 * First check whether it is a transaction over control pipe.
-	 */
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->request != NULL) {
-		if (transfer->direction == USB_DIRECTION_OUT) {
-			/*
-			 * This means end of input data.
-			 */
-			process_control_transfer(device, endpoint, transfer);
-		} else {
-			/*
-			 * For in transactions, this means sending next part
-			 * of the buffer.
-			 */
-			// FIXME: handle when the HC wants the data back
-			// in more chunks
-			size_t actual_size = 0;
-			if (transfer->data) {
-				actual_size = transfer->data_size;
-			}
-			if (actual_size > size) {
-				actual_size = size;
-			}
-			device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-			    "in transaction: will copy %zu bytes", actual_size);
-			if (actual_size > 0) {
-				memcpy(buffer, transfer->data, actual_size);
-				if (data_size) {
-					*data_size = actual_size;
-				}
-			}
-		}
-		
-		return EOK;
-	}
-	
-	if (size == 0) {
-		return EINVAL;
-	}
-	
-	int rc = 1;
-	
-	if (device->ops && device->ops->on_data_request) {
-		rc = device->ops->on_data_request(device, endpoint, buffer, size, data_size);
-	}
-	
-	return rc;
-}
-
-/** Determine direction of control transfer.
- * First, try the user provided callback, otherwise guess, believing that
- * it uses the same format as control pipe 0.
- */
-static usb_direction_t setup_transaction_direction(usbvirt_device_t *device,
-    usb_endpoint_t endpoint,
-    void *data, size_t size)
-{
-	int direction = -1;
-	if (device->ops && device->ops->decide_control_transfer_direction) {
-		direction = device->ops->decide_control_transfer_direction(endpoint,
-		    data, size);
-	}
-	
-	/*
-	 * If the user-supplied routine have not handled the direction
-	 * (or simply was not provided) we will guess, hoping that it 
-	 * uses same format as standard request on control pipe zero.
-	 */
-	if (direction < 0) {
-		if (size > 0) {
-			uint8_t *ptr = (uint8_t *) data;
-			if ((ptr[0] & 128) == 128) {
-				direction = USB_DIRECTION_IN;
-			} else {
-				direction = USB_DIRECTION_OUT;
-			}
-		} else {
-			/* This shall not happen anyway. */
-			direction = USB_DIRECTION_OUT;
-		}
-	}
-	
-	return (usb_direction_t) direction;
-}
-
-/** Process control transfer.
- */
-static void process_control_transfer(usbvirt_device_t *device,
-    usb_endpoint_t endpoint,
-    usbvirt_control_transfer_t *transfer)
-{
-	int rc = EFORWARD;
-	
-	if (device->ops && device->ops->on_control_transfer) {
-		rc = device->ops->on_control_transfer(device, endpoint, transfer);
-	}
-	
-	if (rc == EFORWARD) {
-		if (endpoint == 0) {
-			rc = control_pipe(device, transfer);
-		}
-	}
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/transfer.c
===================================================================
--- uspace/lib/usbvirt/src/transfer.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
+++ uspace/lib/usbvirt/src/transfer.c	(revision 192ba25584eb8fb43acd0a2121ed2a576162e63e)
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ *
+ */
+#include <usbvirt/device.h>
+#include <usb/debug.h>
+#include <errno.h>
+#include <assert.h>
+#include "private.h"
+
+static int usbvirt_control_transfer(usbvirt_device_t *dev,
+    void *setup, size_t setup_size,
+    void *data, size_t data_size, size_t *data_size_sent)
+{
+	assert(dev);
+	assert(dev->ops);
+
+	if (setup_size != sizeof(usb_device_request_setup_packet_t)) {
+		return ESTALL;
+	}
+	usb_device_request_setup_packet_t *setup_packet = setup;
+	if (data_size != setup_packet->length) {
+		return ESTALL;
+	}
+
+	int rc;
+
+	/* Run user handler first. */
+	rc = process_control_transfer(dev, dev->ops->control,
+	    setup_packet, data, data_size_sent);
+	if (rc != EFORWARD) {
+		return rc;
+	}
+
+	/* Run the library handlers afterwards. */
+	rc = process_control_transfer(dev, library_handlers,
+	    setup_packet, data, data_size_sent);
+
+	if (rc == EFORWARD) {
+		usb_log_warning("Control transfer {%s (%s)} not handled.\n",
+		    usb_debug_str_buffer(setup, setup_size, 10),
+		    setup_packet->request_type & 0x80
+		    ? "IN" : usb_debug_str_buffer(data, data_size, 10));
+		rc = EBADCHECKSUM;
+	}
+
+	return rc;
+}
+
+int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
+    void *data, size_t data_size)
+{
+	return usbvirt_control_transfer(dev, setup, setup_size,
+	    data, data_size, NULL);
+}
+
+int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
+    void *data, size_t data_size, size_t *data_size_sent)
+{
+	return usbvirt_control_transfer(dev, setup, setup_size,
+	    data, data_size, data_size_sent);
+}
+
+int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
+    usb_endpoint_t endpoint, void *data, size_t data_size)
+{
+	if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
+		return ERANGE;
+	}
+	if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
+		return ENOTSUP;
+	}
+
+	int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
+	    data, data_size);
+
+	return rc;
+}
+
+int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
+    usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
+{
+	if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
+		return ERANGE;
+	}
+	if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
+		return ENOTSUP;
+	}
+
+	size_t data_size_sent_tmp;
+	int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
+	    data, data_size, &data_size_sent_tmp);
+
+	if (rc != EOK) {
+		return rc;
+	}
+
+	if (data_size_sent != NULL) {
+		*data_size_sent = data_size_sent_tmp;
+	}
+
+	return EOK;
+}
+
+/**
+ * @}
+ */
