Index: .bzrignore
===================================================================
--- .bzrignore	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ .bzrignore	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -70,4 +70,5 @@
 ./uspace/app/virtusbkbd/vuk
 ./uspace/app/virtusbhub/vuh
+./uspace/app/vuhid/vuh
 ./uspace/app/virtusbhub/vhc_hub/*
 ./uspace/app/websrv/websrv
Index: boot/Makefile.common
===================================================================
--- boot/Makefile.common	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ boot/Makefile.common	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -144,5 +144,5 @@
 	$(USPACE_PATH)/app/usbinfo/usbinfo \
 	$(USPACE_PATH)/app/virtusbkbd/vuk \
-	$(USPACE_PATH)/app/virtusbhub/vuh \
+	$(USPACE_PATH)/app/vuhid/vuh \
 	$(USPACE_PATH)/app/websrv/websrv
 
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -52,5 +52,5 @@
 	app/usbinfo \
 	app/virtusbkbd \
-	app/virtusbhub \
+	app/vuhid \
 	app/netecho \
 	app/nettest1 \
Index: uspace/app/usbinfo/main.c
===================================================================
--- uspace/app/usbinfo/main.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/app/usbinfo/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -55,4 +55,9 @@
 	if (str_cmp(devpath, "qemu") == 0) {
 		devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
+	}
+
+	/* Hack for virtual keyboard. */
+	if (str_cmp(devpath, "virt") == 0) {
+		devpath = "/virt/usbhc/usb00_a1/usb00_a2";
 	}
 
Index: pace/app/virtusbhub/Makefile
===================================================================
--- uspace/app/virtusbhub/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,62 +1,0 @@
-#
-# 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.
-#
-
-USPACE_PREFIX = ../..
-# acronym for virtual USB hub
-# (it is really annoying to write long names)
-BINARY = vuh
-
-LIBS = $(LIBUSB_PREFIX)/libusb.a $(LIBUSBVIRT_PREFIX)/libusbvirt.a
-EXTRA_CFLAGS = -DSTANDALONE_HUB \
-	-DHUB_PORT_COUNT=10 \
-	-I$(LIBUSB_PREFIX)/include -I$(LIBUSBVIRT_PREFIX)/include -I$(LIBDRV_PREFIX)/include
-
-SOURCES = \
-	main.c \
-	$(STOLEN_VHC_SOURCES)
-
-STOLEN_VHC_SOURCES = \
-	vhc_hub/hub.c \
-	vhc_hub/virthub.c \
-	vhc_hub/virthubops.c 
-STOLEN_VHC_HEADERS = \
-	vhc_hub/hub.h \
-	vhc_hub/virthub.h
-
-PRE_DEPEND = $(STOLEN_VHC_SOURCES) $(STOLEN_VHC_HEADERS)
-
-EXTRA_CLEAN = $(STOLEN_VHC_SOURCES) $(STOLEN_VHC_HEADERS)
-
-HUB_IN_VHC = $(USPACE_PREFIX)/drv/vhc/hub
-
-include $(USPACE_PREFIX)/Makefile.common
-
-vhc_hub/%.h: $(HUB_IN_VHC)/%.h
-	ln -sfn ../$(HUB_IN_VHC)/$*.h $@
-vhc_hub/%.c: $(HUB_IN_VHC)/%.c
-	ln -sfn ../$(HUB_IN_VHC)/$*.c $@
Index: pace/app/virtusbhub/main.c
===================================================================
--- uspace/app/virtusbhub/main.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,102 +1,0 @@
-/*
- * 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 usbvirthub
- * @{
- */
-/**
- * @file
- * @brief Virtual USB hub.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <str_error.h>
-#include <bool.h>
-
-#include <usb/usb.h>
-#include <usb/descriptor.h>
-#include <usb/classes/hub.h>
-#include <usbvirt/device.h>
-#include <usbvirt/hub.h>
-
-#include "vhc_hub/virthub.h"
-
-#define NAME "vuh"
-
-static usbvirt_device_t hub_device;
-
-#define VERBOSE_SLEEP(sec, msg, ...) \
-	do { \
-		char _status[HUB_PORT_COUNT + 2]; \
-		printf(NAME ": doing nothing for %zu seconds...\n", \
-		    (size_t) (sec)); \
-		fibril_sleep((sec)); \
-		virthub_get_status(&hub_device, _status, HUB_PORT_COUNT + 1); \
-		printf(NAME ": " msg " [%s]\n" #__VA_ARGS__, _status); \
-	} while (0)
-
-static void fibril_sleep(size_t sec)
-{
-	while (sec-- > 0) {
-		async_usleep(1000*1000);
-	}
-}
-
-static int dev1 = 1;
-
-int main(int argc, char * argv[])
-{
-	int rc;
-
-	printf(NAME ": virtual USB hub.\n");
-
-	rc = virthub_init(&hub_device);
-	if (rc != EOK) {
-		printf(NAME ": Unable to start communication with VHCD (%s).\n",
-		    str_error(rc));
-		return rc;
-	}
-	
-	while (true) {
-		VERBOSE_SLEEP(8, "will pretend device plug-in...");
-		virthub_connect_device(&hub_device, &dev1);
-
-		VERBOSE_SLEEP(8, "will pretend device un-plug...");
-		virthub_disconnect_device(&hub_device, &dev1);
-	}
-
-	usbvirt_disconnect(&hub_device);
-	
-	return 0;
-}
-
-
-/** @}
- */
Index: uspace/app/virtusbkbd/Makefile
===================================================================
--- uspace/app/virtusbkbd/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/app/virtusbkbd/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -32,5 +32,5 @@
 BINARY = vuk
 
-LIBS = $(LIBUSB_PREFIX)/libusb.a $(LIBUSBVIRT_PREFIX)/libusbvirt.a
+LIBS = $(LIBUSBVIRT_PREFIX)/libusbvirt.a $(LIBUSB_PREFIX)/libusb.a
 EXTRA_CFLAGS = -I$(LIBUSB_PREFIX)/include -I$(LIBUSBVIRT_PREFIX)/include -I$(LIBDRV_PREFIX)/include
 
Index: uspace/app/virtusbkbd/stdreq.c
===================================================================
--- uspace/app/virtusbkbd/stdreq.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/app/virtusbkbd/stdreq.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -39,16 +39,18 @@
 #include "kbdconfig.h"
 
-int stdreq_on_get_descriptor(struct usbvirt_device *dev,
-    usb_device_request_setup_packet_t *request, uint8_t *data)
+int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size)
 {
-	if (request->value_high == USB_DESCTYPE_HID_REPORT) {
+	if (setup_packet->value_high == USB_DESCTYPE_HID_REPORT) {
 		/*
 		 * For simplicity, always return the same
 		 * report descriptor.
 		 */
-		int rc = dev->control_transfer_reply(dev, 0,
+		usbvirt_control_reply_helper(setup_packet,
+		    data, act_size,
 		    report_descriptor, report_descriptor_size);
-		
-		return rc;
+
+		return EOK;
 	}
 	
Index: uspace/app/virtusbkbd/stdreq.h
===================================================================
--- uspace/app/virtusbkbd/stdreq.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/app/virtusbkbd/stdreq.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -38,6 +38,7 @@
 #include <usbvirt/device.h>
 
-int stdreq_on_get_descriptor(usbvirt_device_t *,
-    usb_device_request_setup_packet_t *, uint8_t *);
+int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size);
 
 #endif
Index: uspace/app/virtusbkbd/virtusbkbd.c
===================================================================
--- uspace/app/virtusbkbd/virtusbkbd.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/app/virtusbkbd/virtusbkbd.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -48,6 +48,6 @@
 #include <usb/descriptor.h>
 #include <usb/classes/hid.h>
+#include <usb/debug.h>
 #include <usbvirt/device.h>
-#include <usbvirt/hub.h>
 
 #include "kbdconfig.h"
@@ -67,13 +67,4 @@
 
 kb_status_t status;
-
-static int on_incoming_data(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size)
-{
-	printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
-	
-	return EOK;
-}
-
 
 /** Compares current and last status of pressed keys.
@@ -100,6 +91,7 @@
 }
 
-static int on_request_for_data(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size)
+static int on_request_for_data(usbvirt_device_t *dev,
+    usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
+    void *buffer, size_t size, size_t *actual_size)
 {
 	static uint8_t last_data[2 + KB_MAX_KEYS_AT_ONCE];
@@ -122,6 +114,5 @@
 	if (keypress_check_with_last_request(data, last_data,
 	    2 + KB_MAX_KEYS_AT_ONCE)) {
-		*actual_size = 0;
-		return EOK;
+		return ENAK;
 	}
 
@@ -131,24 +122,16 @@
 }
 
-static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
-	{
-		.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
-		    USB_DIRECTION_IN,
-		    USBVIRT_REQUEST_TYPE_STANDARD,
-		    USBVIRT_REQUEST_RECIPIENT_DEVICE),
+static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
+	{
+		.req_direction = USB_DIRECTION_IN,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
 		.request = USB_DEVREQ_GET_DESCRIPTOR,
 		.name = "GetDescriptor",
-		.callback = stdreq_on_get_descriptor
+		.callback = req_get_descriptor
 	},
 	{
-		.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
-		    USB_DIRECTION_IN,
-		    USBVIRT_REQUEST_TYPE_CLASS,
-		    USBVIRT_REQUEST_RECIPIENT_DEVICE),
-		.request = USB_DEVREQ_GET_DESCRIPTOR,
-		.name = "GetDescriptor",
-		.callback = stdreq_on_get_descriptor
-	},
-	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
+		.callback = NULL
+	}
 };
 
@@ -157,7 +140,6 @@
  */
 static usbvirt_device_ops_t keyboard_ops = {
-	.control_transfer_handlers = endpoint_zero_handlers,
-	.on_data = on_incoming_data,
-	.on_data_request = on_request_for_data
+	.control = endpoint_zero_handlers,
+	.data_in[1] = on_request_for_data
 };
 
@@ -197,6 +179,4 @@
 	.ops = &keyboard_ops,
 	.descriptors = &descriptors,
-	.lib_debug_level = 3,
-	.lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL,
 	.name = "keyboard"
 };
@@ -262,5 +242,5 @@
 	
 	
-	int rc = usbvirt_connect(&keyboard_dev);
+	int rc = usbvirt_device_plug(&keyboard_dev, "/virt/usbhc/hc");
 	if (rc != EOK) {
 		printf("%s: Unable to start communication with VHCD (%s).\n",
@@ -278,5 +258,5 @@
 	printf("%s: Terminating...\n", NAME);
 	
-	usbvirt_disconnect(&keyboard_dev);
+	//usbvirt_disconnect(&keyboard_dev);
 	
 	return 0;
Index: uspace/app/vuhid/Makefile
===================================================================
--- uspace/app/vuhid/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,54 @@
+#
+# 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.
+#
+
+USPACE_PREFIX = ../..
+# acronym for virtual USB human input device
+# (it is really annoying to write long names)
+BINARY = vuh
+
+LIBS = \
+	$(LIBUSBVIRT_PREFIX)/libusbvirt.a \
+	$(LIBUSB_PREFIX)/libusb.a
+EXTRA_CFLAGS = \
+	-I$(LIBUSB_PREFIX)/include \
+	-I$(LIBUSBVIRT_PREFIX)/include \
+	-I$(LIBDRV_PREFIX)/include
+
+
+SOURCES_INTERFACES = \
+	hids/bootkbd.c
+
+SOURCES = \
+	main.c \
+	device.c \
+	ifaces.c \
+	stdreq.c \
+	$(SOURCES_INTERFACES)
+	
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/app/vuhid/device.c
===================================================================
--- uspace/app/vuhid/device.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/device.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,335 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/**
+ * @file
+ * Virtual USB HID device.
+ */
+#include "virthid.h"
+#include <errno.h>
+#include <str.h>
+#include <assert.h>
+#include <usb/classes/classes.h>
+
+static int on_data_from_device(usbvirt_device_t *dev,
+    usb_endpoint_t ep, usb_transfer_type_t tr_type,
+    void *data, size_t data_size, size_t *actual_size)
+{
+	vuhid_data_t *vuhid = dev->device_data;
+	vuhid_interface_t *iface = vuhid->in_endpoints_mapping[ep];
+	if (iface == NULL) {
+		return ESTALL;
+	}
+
+	if (iface->on_data_in == NULL) {
+		return EBADCHECKSUM;
+	}
+
+	return iface->on_data_in(iface, data, data_size, actual_size);
+}
+
+static int on_data_to_device(usbvirt_device_t *dev,
+    usb_endpoint_t ep, usb_transfer_type_t tr_type,
+    void *data, size_t data_size)
+{
+	vuhid_data_t *vuhid = dev->device_data;
+	vuhid_interface_t *iface = vuhid->out_endpoints_mapping[ep];
+	if (iface == NULL) {
+		return ESTALL;
+	}
+
+	if (iface->on_data_out == NULL) {
+		return EBADCHECKSUM;
+	}
+
+	return iface->on_data_out(iface, data, data_size);
+}
+
+static int interface_life_fibril(void *arg)
+{
+	vuhid_interface_t *iface = arg;
+
+	if (iface->live != NULL) {
+		iface->live(iface);
+	}
+
+	return EOK;
+}
+
+
+static vuhid_interface_t *find_interface_by_id(vuhid_interface_t **ifaces,
+    const char *id)
+{
+	vuhid_interface_t *iface = ifaces[0];
+	while (iface != NULL) {
+		if (str_cmp(iface->id, id) == 0) {
+			return iface;
+		}
+		iface++;
+	}
+
+	return iface;
+}
+
+int add_interface_by_id(vuhid_interface_t **interfaces, const char *id,
+    usbvirt_device_t *dev)
+{
+	vuhid_interface_t *iface = find_interface_by_id(interfaces, id);
+	if (iface == NULL) {
+		return ENOENT;
+	}
+
+	if ((iface->in_data_size == 0) && (iface->out_data_size == 0)) {
+		return EEMPTY;
+	}
+
+	vuhid_data_t *hid_data = dev->device_data;
+
+	/* Check that we have not run out of available endpoints. */
+	if ((iface->in_data_size > 0)
+	    && (hid_data->in_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
+		return ELIMIT;
+	}
+	if ((iface->out_data_size > 0)
+	    && (hid_data->out_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
+		return ELIMIT;
+	}
+
+	if (dev->descriptors->configuration->descriptor->interface_count >=
+	    VUHID_INTERFACE_MAX) {
+		return ELIMIT;
+	}
+
+	/*
+	 * How may descriptors we would add?
+	 */
+	/* Start with interface descriptor. */
+	size_t new_descriptor_count = 1;
+	/* Having in/out data size positive means we would need endpoint. */
+	if (iface->in_data_size > 0) {
+		new_descriptor_count++;
+	}
+	if (iface->out_data_size > 0) {
+		new_descriptor_count++;
+	}
+	/* Having report descriptor means adding the HID descriptor. */
+	if (iface->report_descriptor != NULL) {
+		new_descriptor_count++;
+	}
+
+	/* From now on, in case of errors, we goto to error_leave */
+	int rc;
+
+	/*
+	 * Prepare new descriptors.
+	 */
+	printf("preparing descriptors...\n");
+	size_t descr_count = 0;
+	size_t total_descr_size = 0;
+	usb_standard_interface_descriptor_t *descr_iface = NULL;
+	hid_descriptor_t *descr_hid = NULL;
+	usb_standard_endpoint_descriptor_t *descr_ep_in = NULL;
+	usb_standard_endpoint_descriptor_t *descr_ep_out = NULL;
+
+	size_t ep_count = 0;
+	if (iface->in_data_size > 0) {
+		ep_count++;
+	}
+	if (iface->out_data_size > 0) {
+		ep_count++;
+	}
+	assert(ep_count > 0);
+
+	/* Interface would be needed always. */
+	descr_iface = malloc(sizeof(usb_standard_interface_descriptor_t));
+	if (descr_iface == NULL) {
+		rc = ENOMEM;
+		goto error_leave;
+	}
+	descr_count++;
+	total_descr_size += sizeof(usb_standard_interface_descriptor_t);
+	descr_iface->length = sizeof(usb_standard_interface_descriptor_t);
+	descr_iface->descriptor_type = USB_DESCTYPE_INTERFACE;
+	descr_iface->interface_number
+	    = dev->descriptors->configuration->descriptor->interface_count;
+	descr_iface->alternate_setting = 0;
+	descr_iface->endpoint_count = ep_count;
+	descr_iface->interface_class = USB_CLASS_HID;
+	descr_iface->interface_subclass = iface->usb_subclass;
+	descr_iface->interface_protocol = iface->usb_protocol;
+	descr_iface->str_interface = 0;
+
+	/* Create the HID descriptor. */
+	if (iface->report_descriptor != NULL) {
+		descr_hid = malloc(sizeof(hid_descriptor_t));
+		if (descr_hid == NULL) {
+			rc = ENOMEM;
+			goto error_leave;
+		}
+		descr_count++;
+		total_descr_size += sizeof(hid_descriptor_t);
+		descr_hid->length = sizeof(hid_descriptor_t);
+		descr_hid->type = USB_DESCTYPE_HID;
+		descr_hid->hid_spec_release = 0x101;
+		descr_hid->country_code = 0;
+		descr_hid->descriptor_count = 1;
+		descr_hid->descriptor1_type = USB_DESCTYPE_HID_REPORT;
+		descr_hid->descriptor1_length = iface->report_descriptor_size;
+	}
+
+	/* Prepare the endpoint descriptors. */
+	if (iface->in_data_size > 0) {
+		descr_ep_in = malloc(sizeof(usb_standard_endpoint_descriptor_t));
+		if (descr_ep_in == NULL) {
+			rc = ENOMEM;
+			goto error_leave;
+		}
+		descr_count++;
+		total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
+		descr_ep_in->length = sizeof(usb_standard_endpoint_descriptor_t);
+		descr_ep_in->descriptor_type = USB_DESCTYPE_ENDPOINT;
+		descr_ep_in->endpoint_address
+		    = 0x80 | hid_data->in_endpoint_first_free;
+		descr_ep_in->attributes = 3;
+		descr_ep_in->max_packet_size = iface->in_data_size;
+		descr_ep_in->poll_interval = 10;
+	}
+	if (iface->out_data_size > 0) {
+		descr_ep_out = malloc(sizeof(usb_standard_endpoint_descriptor_t));
+		if (descr_ep_out == NULL) {
+			rc = ENOMEM;
+			goto error_leave;
+		}
+		descr_count++;
+		total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
+		descr_ep_out->length = sizeof(usb_standard_endpoint_descriptor_t);
+		descr_ep_out->descriptor_type = USB_DESCTYPE_ENDPOINT;
+		descr_ep_out->endpoint_address
+		    = 0x00 | hid_data->out_endpoint_first_free;
+		descr_ep_out->attributes = 3;
+		descr_ep_out->max_packet_size = iface->out_data_size;
+		descr_ep_out->poll_interval = 10;
+	}
+
+	/* Extend existing extra descriptors with these ones. */
+	usbvirt_device_configuration_extras_t *extra_descriptors
+	    = dev->descriptors->configuration->extra;
+	extra_descriptors = realloc(extra_descriptors,
+	    sizeof(usbvirt_device_configuration_extras_t)
+	    * (dev->descriptors->configuration->extra_count + descr_count));
+	if (extra_descriptors == NULL) {
+		rc = ENOMEM;
+		goto error_leave;
+	}
+
+	/* Launch the "life" fibril. */
+	fid_t life_fibril = fibril_create(interface_life_fibril, iface);
+	if (life_fibril == 0) {
+		rc = ENOMEM;
+		goto error_leave;
+	}
+
+	printf("adding extra descriptors...\n");
+	/* Allocation is okay, we can (actually have to now) overwrite the
+	 * original pointer.
+	 */
+	dev->descriptors->configuration->extra = extra_descriptors;
+	extra_descriptors += dev->descriptors->configuration->extra_count;
+
+	/* Initialize them. */
+#define _APPEND_DESCRIPTOR(descriptor) \
+	do { \
+		if ((descriptor) != NULL) { \
+			extra_descriptors->data = (uint8_t *) (descriptor); \
+			extra_descriptors->length = (descriptor)->length; \
+			extra_descriptors++; \
+		} \
+	} while (0)
+
+	_APPEND_DESCRIPTOR(descr_iface);
+	_APPEND_DESCRIPTOR(descr_hid);
+	_APPEND_DESCRIPTOR(descr_ep_in);
+	_APPEND_DESCRIPTOR(descr_ep_out);
+#undef _APPEND_DESCRIPTOR
+	dev->descriptors->configuration->extra_count += descr_count;
+
+	/*
+	 * Final changes.
+	 * Increase the necessary counters, make endpoint mapping.
+	 *
+	 */
+	if (iface->in_data_size > 0) {
+		hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free]
+		    = iface;
+		dev->ops->data_in[hid_data->in_endpoint_first_free]
+		    = on_data_from_device;
+		hid_data->in_endpoint_first_free++;
+	}
+	if (iface->out_data_size > 0) {
+		hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free]
+		    = iface;
+		dev->ops->data_out[hid_data->out_endpoint_first_free]
+		    = on_data_to_device;
+		hid_data->out_endpoint_first_free++;
+	}
+
+	hid_data->interface_mapping[
+	    dev->descriptors->configuration->descriptor->interface_count]
+	    = iface;
+
+	dev->descriptors->configuration->descriptor->interface_count++;
+	dev->descriptors->configuration->descriptor->total_length
+	    += total_descr_size;
+
+	fibril_add_ready(life_fibril);
+
+	return EOK;
+
+error_leave:
+	if (descr_iface != NULL) {
+		free(descr_iface);
+	}
+	if (descr_hid != NULL) {
+		free(descr_hid);
+	}
+	if (descr_ep_in != NULL) {
+		free(descr_ep_in);
+	}
+	if (descr_ep_out != NULL) {
+		free(descr_ep_out);
+	}
+
+	return rc;
+}
+
+
+/** @}
+ */
Index: uspace/app/vuhid/hids/bootkbd.c
===================================================================
--- uspace/app/vuhid/hids/bootkbd.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/hids/bootkbd.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,176 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/** @file
+ *
+ */
+#include "../virthid.h"
+#include <errno.h>
+#include <usb/debug.h>
+#include <usb/classes/hid.h>
+#include <usb/classes/hidut.h>
+
+#include "../../virtusbkbd/report.h"
+
+uint8_t report_descriptor[] = {
+	STD_USAGE_PAGE(USB_HIDUT_PAGE_GENERIC_DESKTOP),
+	USAGE1(USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD),
+	START_COLLECTION(COLLECTION_APPLICATION),
+		STD_USAGE_PAGE(USB_HIDUT_PAGE_KEYBOARD),
+		USAGE_MINIMUM1(224),
+		USAGE_MAXIMUM1(231),
+		LOGICAL_MINIMUM1(0),
+		LOGICAL_MAXIMUM1(1),
+		REPORT_SIZE1(1),
+		REPORT_COUNT1(8),
+		/* Modifiers */
+		INPUT(IOF_DATA | IOF_VARIABLE | IOF_ABSOLUTE),
+		REPORT_COUNT1(1),
+		REPORT_SIZE1(8),
+		/* Reserved */
+		INPUT(IOF_CONSTANT),
+		REPORT_COUNT1(5),
+		REPORT_SIZE1(1),
+		STD_USAGE_PAGE(USB_HIDUT_PAGE_LED),
+		USAGE_MINIMUM1(1),
+		USAGE_MAXIMUM1(5),
+		/* LED states */
+		OUTPUT(IOF_DATA | IOF_VARIABLE | IOF_ABSOLUTE),
+		REPORT_COUNT1(1),
+		REPORT_SIZE1(3),
+		/* LED states padding */
+		OUTPUT(IOF_CONSTANT),
+		REPORT_COUNT1(6),
+		REPORT_SIZE1(8),
+		LOGICAL_MINIMUM1(0),
+		LOGICAL_MAXIMUM1(101),
+		STD_USAGE_PAGE(USB_HIDUT_PAGE_KEYBOARD),
+		USAGE_MINIMUM1(0),
+		USAGE_MAXIMUM1(101),
+		/* Key array */
+		INPUT(IOF_DATA | IOF_ARRAY),
+	END_COLLECTION()
+};
+
+#define INPUT_SIZE 8
+
+static uint8_t in_data[] = {
+	     0, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	     0, 0, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, // Caps Lock
+	     0, 0, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, // Num Lock
+	     0, 0, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, // Caps Lock
+	1 << 2, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	1 << 2, 0, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
+	1 << 2, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	     0, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+static size_t in_data_count = sizeof(in_data)/INPUT_SIZE;
+// FIXME - locking
+static size_t in_data_position = 0;
+
+static int on_data_in(vuhid_interface_t *iface,
+    void *buffer, size_t buffer_size, size_t *act_buffer_size)
+{
+	static size_t last_pos = (size_t) -1;
+	size_t pos = in_data_position;
+	if (pos >= in_data_count) {
+		return EBADCHECKSUM;
+	}
+
+	if (last_pos == pos) {
+		return ENAK;
+	}
+
+	if (buffer_size > INPUT_SIZE) {
+		buffer_size = INPUT_SIZE;
+	}
+
+	if (act_buffer_size != NULL) {
+		*act_buffer_size = buffer_size;
+	}
+
+	memcpy(buffer, in_data + pos * INPUT_SIZE, buffer_size);
+	last_pos = pos;
+
+	return EOK;
+}
+
+static int on_data_out(vuhid_interface_t *iface,
+    void *buffer, size_t buffer_size)
+{
+	if (buffer_size == 0) {
+		return EEMPTY;
+	}
+	uint8_t leds = ((uint8_t *) buffer)[0];
+#define _GET_LED(index, signature) \
+	(((leds) & (1 << index)) ? (signature) : '-')
+	usb_log_info("%s: LEDs = %c%c%c%c%c\n",
+	    iface->name,
+	    _GET_LED(0, '0'), _GET_LED(1, 'A'), _GET_LED(2, 's'),
+	    _GET_LED(3, 'c'), _GET_LED(4, 'k'));
+#undef _GET_LED
+	return EOK;
+}
+
+
+static void live(vuhid_interface_t *iface)
+{
+	async_usleep(1000 * 1000 * 5);
+	usb_log_debug("Boot keyboard comes to life...\n");
+	while (in_data_position < in_data_count) {
+		async_usleep(1000 * 500);
+		in_data_position++;
+	}
+	usb_log_debug("Boot keyboard died.\n");
+}
+
+
+vuhid_interface_t vuhid_interface_bootkbd = {
+	.id = "boot",
+	.name = "boot keyboard",
+	.usb_subclass = USB_HID_SUBCLASS_BOOT,
+	.usb_protocol = USB_HID_PROTOCOL_KEYBOARD,
+
+	.report_descriptor = report_descriptor,
+	.report_descriptor_size = sizeof(report_descriptor),
+
+	.in_data_size = INPUT_SIZE,
+	.on_data_in = on_data_in,
+
+	.out_data_size = 1,
+	.on_data_out = on_data_out,
+
+	.live = live
+};
+
+/**
+ * @}
+ */
Index: uspace/app/vuhid/ifaces.c
===================================================================
--- uspace/app/vuhid/ifaces.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/ifaces.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,48 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/**
+ * @file
+ * List of known interfaces.
+ */
+#include <stdlib.h>
+#include "ifaces.h"
+
+extern vuhid_interface_t vuhid_interface_bootkbd;
+
+vuhid_interface_t *available_hid_interfaces[] = {
+	&vuhid_interface_bootkbd,
+	NULL
+};
+
+
+/** @}
+ */
Index: uspace/app/vuhid/ifaces.h
===================================================================
--- uspace/app/vuhid/ifaces.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/ifaces.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,45 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/** @file
+ *
+ */
+#ifndef VUHID_IFACES_H_
+#define VUHID_IFACES_H_
+
+#include "virthid.h"
+
+extern vuhid_interface_t *available_hid_interfaces[];
+
+#endif
+/**
+ * @}
+ */
Index: uspace/app/vuhid/main.c
===================================================================
--- uspace/app/vuhid/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,195 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/**
+ * @file
+ * Virtual USB HID device.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <str_error.h>
+
+#include <usb/usb.h>
+#include <usb/descriptor.h>
+#include <usb/debug.h>
+#include <usb/classes/classes.h>
+#include <usb/classes/hid.h>
+#include <usbvirt/device.h>
+
+#include "virthid.h"
+#include "ifaces.h"
+#include "stdreq.h"
+
+static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
+	{
+		.req_direction = USB_DIRECTION_IN,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
+		.request = USB_DEVREQ_GET_DESCRIPTOR,
+		.name = "Get_Descriptor",
+		.callback = req_get_descriptor
+	},
+	{
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
+		.req_type = USB_REQUEST_TYPE_CLASS,
+		.request = USB_HIDREQ_SET_PROTOCOL,
+		.name = "Set_Protocol",
+		.callback = req_set_protocol
+	},
+	{
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
+		.req_type = USB_REQUEST_TYPE_CLASS,
+		.request = USB_HIDREQ_SET_REPORT,
+		.name = "Set_Report",
+		.callback = req_set_report
+	},
+	{
+		.callback = NULL
+	}
+};
+
+/** Keyboard callbacks.
+ * We abuse the fact that static variables are zero-filled.
+ */
+static usbvirt_device_ops_t hid_ops = {
+	.control = endpoint_zero_handlers,
+};
+
+/** Standard configuration descriptor. */
+static usb_standard_configuration_descriptor_t std_configuration_descriptor = {
+	.length = sizeof(usb_standard_configuration_descriptor_t),
+	.descriptor_type = USB_DESCTYPE_CONFIGURATION,
+	/* Will be patched at runtime. */
+	.total_length = sizeof(usb_standard_configuration_descriptor_t),
+	.interface_count = 0,
+	.configuration_number = 1,
+	.str_configuration = 0,
+	.attributes = 128, /* denotes bus-powered device */
+	.max_power = 50
+};
+
+/** Standard device descriptor. */
+static usb_standard_device_descriptor_t std_device_descriptor = {
+	.length = sizeof(usb_standard_device_descriptor_t),
+	.descriptor_type = USB_DESCTYPE_DEVICE,
+	.usb_spec_version = 0x110,
+	.device_class = USB_CLASS_USE_INTERFACE,
+	.device_subclass = 0,
+	.device_protocol = 0,
+	.max_packet_size = 64,
+	.configuration_count = 1
+};
+
+/** HID configuration. */
+usbvirt_device_configuration_t configuration = {
+	.descriptor = &std_configuration_descriptor,
+	.extra = NULL,
+	.extra_count = 0
+};
+
+/** HID standard descriptors. */
+usbvirt_descriptors_t descriptors = {
+	.device = &std_device_descriptor,
+	.configuration = &configuration,
+	.configuration_count = 1,
+};
+
+static vuhid_data_t vuhid_data = {
+	.in_endpoints_mapping = { NULL },
+	.in_endpoint_first_free = 1,
+	.out_endpoints_mapping = { NULL },
+	.out_endpoint_first_free = 1
+};
+
+/** Keyboard device.
+ * Rest of the items will be initialized later.
+ */
+static usbvirt_device_t hid_dev = {
+	.ops = &hid_ops,
+	.descriptors = &descriptors,
+	.name = "HID",
+	.device_data = &vuhid_data
+};
+
+
+int main(int argc, char * argv[])
+{
+	int rc;
+
+	usb_log_enable(USB_LOG_LEVEL_DEBUG2, "vusbhid");
+
+	/* Determine which interfaces to initialize. */
+	int i;
+	for (i = 1; i < argc; i++) {
+		rc = add_interface_by_id(available_hid_interfaces, argv[i],
+		    &hid_dev);
+		if (rc != EOK) {
+			fprintf(stderr, "Failed to add device `%s': %s.\n",
+			    argv[i], str_error(rc));
+		} else {
+			printf("Added device `%s'.\n", argv[i]);
+		}
+	}
+
+	for (i = 0; i < (int) hid_dev.descriptors->configuration->extra_count; i++) {
+		usb_log_debug("Found extra descriptor: %s.\n",
+		    usb_debug_str_buffer(
+		        hid_dev.descriptors->configuration->extra[i].data,
+		        hid_dev.descriptors->configuration->extra[i].length,
+		        0));
+	}
+
+	rc = usbvirt_device_plug(&hid_dev, "/virt/usbhc/hc");
+	if (rc != EOK) {
+		printf("Unable to start communication with VHCD: %s.\n",
+		    str_error(rc));
+		return rc;
+	}
+	
+	printf("Connected to VHCD...\n");
+
+	while (true) {
+		async_usleep(10 * 1000 * 1000);
+	}
+	
+	printf("Terminating...\n");
+	
+	return 0;
+}
+
+
+/** @}
+ */
Index: uspace/app/vuhid/stdreq.c
===================================================================
--- uspace/app/vuhid/stdreq.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/stdreq.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,119 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/**
+ * @file
+ *
+ */
+#include <errno.h>
+#include <usb/debug.h>
+#include <usb/descriptor.h>
+#include "stdreq.h"
+#include "virthid.h"
+
+#define VUHID_DATA(vuhid, device) \
+	vuhid_data_t *vuhid = device->device_data
+
+int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size)
+{
+	VUHID_DATA(vuhid, device);
+
+	if (setup_packet->value_high == USB_DESCTYPE_HID_REPORT) {
+		vuhid_interface_t *iface
+		    = vuhid->interface_mapping[setup_packet->index];
+		if (iface == NULL) {
+			return EFORWARD;
+		}
+		if (iface->report_descriptor != NULL) {
+			usbvirt_control_reply_helper(setup_packet,
+			    data, act_size,
+			    iface->report_descriptor,
+			    iface->report_descriptor_size);
+			return EOK;
+		} else {
+			return ENOENT;
+		}
+	}
+	
+	/* Let the framework handle all the rest. */
+	return EFORWARD;
+}
+
+int req_set_protocol(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size)
+{
+	VUHID_DATA(vuhid, device);
+
+	size_t iface_index = setup_packet->index;
+	int protocol = setup_packet->value;
+
+	// FIXME - check ranges
+	vuhid_interface_t *iface = vuhid->interface_mapping[iface_index];
+	if (iface == NULL) {
+		return ENOENT;
+	}
+	iface->set_protocol = protocol;
+
+	return EOK;
+}
+
+int req_set_report(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size)
+{
+	VUHID_DATA(vuhid, device);
+
+	size_t iface_index = setup_packet->index;
+
+	// FIXME - check ranges
+	vuhid_interface_t *iface = vuhid->interface_mapping[iface_index];
+	if (iface == NULL) {
+		return ENOENT;
+	}
+
+	size_t data_length = setup_packet->length;
+	if (iface->on_data_out == NULL) {
+		return ENOTSUP;
+	}
+
+	/* SET_REPORT is translated to data out */
+	int rc = iface->on_data_out(iface, data, data_length);
+
+	return rc;
+}
+
+
+
+/** @}
+ */
Index: uspace/app/vuhid/stdreq.h
===================================================================
--- uspace/app/vuhid/stdreq.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/stdreq.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,55 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/** @file
+ * Device request handlers.
+ */
+#ifndef VUHID_STDREQ_H_
+#define VUHID_STDREQ_H_
+
+#include <usbvirt/device.h>
+
+int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size);
+
+int req_set_protocol(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size);
+
+int req_set_report(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/app/vuhid/virthid.h
===================================================================
--- uspace/app/vuhid/virthid.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/app/vuhid/virthid.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,90 @@
+/*
+ * 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 usbvirthid
+ * @{
+ */
+/** @file
+ *
+ */
+#ifndef VUHID_VIRTHID_H_
+#define VUHID_VIRTHID_H_
+
+#include <usb/usb.h>
+#include <usbvirt/device.h>
+
+#define VUHID_ENDPOINT_MAX USB11_ENDPOINT_MAX
+#define VUHID_INTERFACE_MAX 8
+
+typedef struct vuhid_interface vuhid_interface_t;
+
+struct vuhid_interface {
+	const char *name;
+	const char *id;
+	int usb_subclass;
+	int usb_protocol;
+
+	uint8_t *report_descriptor;
+	size_t report_descriptor_size;
+
+	size_t in_data_size;
+	size_t out_data_size;
+
+	int (*on_data_in)(vuhid_interface_t *, void *, size_t, size_t *);
+	int (*on_data_out)(vuhid_interface_t *, void *, size_t);
+	void (*live)(vuhid_interface_t *);
+
+	int set_protocol;
+
+	void *interface_data;
+};
+
+typedef struct {
+	vuhid_interface_t *in_endpoints_mapping[VUHID_ENDPOINT_MAX];
+	size_t in_endpoint_first_free;
+	vuhid_interface_t *out_endpoints_mapping[VUHID_ENDPOINT_MAX];
+	size_t out_endpoint_first_free;
+	vuhid_interface_t *interface_mapping[VUHID_INTERFACE_MAX];
+} vuhid_data_t;
+
+typedef struct {
+	uint8_t length;
+	uint8_t type;
+	uint16_t hid_spec_release;
+	uint8_t country_code;
+	uint8_t descriptor_count;
+	uint8_t descriptor1_type;
+	uint16_t descriptor1_length;
+} __attribute__ ((packed)) hid_descriptor_t;
+
+int add_interface_by_id(vuhid_interface_t **, const char *, usbvirt_device_t *);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/ohci/root_hub.c
===================================================================
--- uspace/drv/ohci/root_hub.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/ohci/root_hub.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -149,9 +149,7 @@
 
 
-static void usb_create_serialized_hub_descriptor(rh_t *instance,
-	uint8_t ** out_result,
-	size_t * out_size);
-
-static void rh_init_descriptors(rh_t *instance);
+static int create_serialized_hub_descriptor(rh_t *instance);
+
+static int rh_init_descriptors(rh_t *instance);
 
 static int process_get_port_status_request(rh_t *instance, uint16_t port,
@@ -164,6 +162,5 @@
 	usb_transfer_batch_t * request);
 
-static void create_interrupt_mask(rh_t *instance, void ** buffer,
-	size_t * buffer_size);
+static void create_interrupt_mask_in_instance(rh_t *instance);
 
 static int process_get_descriptor_request(rh_t *instance,
@@ -198,6 +195,5 @@
 static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
 
-static int process_interrupt(rh_t *instance, usb_transfer_batch_t * request,
-    void * change_buffer, size_t buffe_size);
+static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request);
 
 static bool is_zeros(void * buffer, size_t size);
@@ -213,9 +209,17 @@
 	instance->port_count =
 	    (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
-	rh_init_descriptors(instance);
+	int opResult = rh_init_descriptors(instance);
+	if(opResult != EOK){
+		return opResult;
+	}
 	// set port power mode to no-power-switching
 	instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
 	instance->unfinished_interrupt_transfer = NULL;
-	instance->interrupt_buffer = malloc((instance->port_count + 8)/8);
+	instance->interrupt_mask_size = (instance->port_count + 8)/8;
+	instance->interrupt_buffer = malloc(instance->interrupt_mask_size);
+	if(!instance->interrupt_buffer)
+		return ENOMEM;
+	
+
 	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
 	return EOK;
@@ -240,19 +244,14 @@
 	} else if (request->ep->transfer_type == USB_TRANSFER_INTERRUPT) {
 		usb_log_info("Root hub got INTERRUPT packet\n");
-		void * buffer;
-		size_t buffer_size;
-		create_interrupt_mask(instance, &buffer,
-			&buffer_size);
-		if(is_zeros(buffer,buffer_size)){
-			usb_log_debug("no changes..");
-			instance->unfinished_interrupt_transfer=
-			    request;
+		create_interrupt_mask_in_instance(instance);
+		if(is_zeros(instance->interrupt_buffer,
+		    instance->interrupt_mask_size)){
+			usb_log_debug("no changes..\n");
+			instance->unfinished_interrupt_transfer = request;
 			//will be finished later
 		}else{
-			usb_log_debug("processing changes..");
-			process_interrupt(instance, request,
-			    buffer, buffer_size);
-		}
-		free(buffer);
+			usb_log_debug("processing changes..\n");
+			process_interrupt_mask_in_instance(instance, request);
+		}
 		opResult = EOK;
 	} else {
@@ -265,18 +264,18 @@
 /*----------------------------------------------------------------------------*/
 
-
+/**
+ * process interrupt on a hub
+ *
+ * If there is no pending interrupt transfer, nothing happens.
+ * @param instance
+ */
 void rh_interrupt(rh_t *instance) {
-	//usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
-	//	"interrupts, am I?\n");
 	if(!instance->unfinished_interrupt_transfer){
 		return;
 	}
-	size_t size;
-	void * buffer;
-	create_interrupt_mask(instance, &buffer,
-			&size);
-	process_interrupt(instance,instance->unfinished_interrupt_transfer,
-	    buffer,size);
-	free(buffer);
+	usb_log_debug("finalizing interrupt transfer\n");
+	create_interrupt_mask_in_instance(instance);
+	process_interrupt_mask_in_instance(instance,
+	    instance->unfinished_interrupt_transfer);
 }
 /*----------------------------------------------------------------------------*/
@@ -289,17 +288,13 @@
  *
  * @param instance root hub instance
- * @param@out out_result pointer to resultant serialized descriptor
- * @param@out out_size size of serialized descriptor
- */
-static void usb_create_serialized_hub_descriptor(rh_t *instance,
-	uint8_t ** out_result,
-	size_t * out_size) {
-	//base size
-	size_t size = 7;
-	//variable size according to port count
-	size_t var_size = instance->port_count / 8 +
-		((instance->port_count % 8 > 0) ? 1 : 0);
-	size += 2 * var_size;
+ * @return error code
+ */
+static int create_serialized_hub_descriptor(rh_t *instance) {
+	size_t size = 7 +
+	    ((instance->port_count +7 )/ 8) * 2;
+	size_t var_size = (instance->port_count +7 )/ 8;
 	uint8_t * result = (uint8_t*) malloc(size);
+	if(!result) return ENOMEM;
+
 	bzero(result, size);
 	//size
@@ -330,6 +325,7 @@
 		result[7 + var_size + i] = 255;
 	}
-	(*out_result) = result;
-	(*out_size) = size;
+	instance->hub_descriptor = result;
+	instance->descriptor_size = size;
+	return EOK;
 }
 /*----------------------------------------------------------------------------*/
@@ -340,6 +336,7 @@
  * be initialized only once per hub.
  * @instance root hub instance
- */
-static void rh_init_descriptors(rh_t *instance) {
+ * @return error code
+ */
+static int rh_init_descriptors(rh_t *instance) {
 	memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
 		sizeof (ohci_rh_device_descriptor)
@@ -348,17 +345,20 @@
 	memcpy(&descriptor, &ohci_rh_conf_descriptor,
 		sizeof (ohci_rh_conf_descriptor));
-	uint8_t * hub_descriptor;
-	size_t hub_desc_size;
-	usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
-		&hub_desc_size);
-
+
+	int opResult = create_serialized_hub_descriptor(instance);
+	if(opResult != EOK){
+		return opResult;
+	}
 	descriptor.total_length =
 		sizeof (usb_standard_configuration_descriptor_t) +
 		sizeof (usb_standard_endpoint_descriptor_t) +
 		sizeof (usb_standard_interface_descriptor_t) +
-		hub_desc_size;
+		instance->descriptor_size;
 
 	uint8_t * full_config_descriptor =
 		(uint8_t*) malloc(descriptor.total_length);
+	if(!full_config_descriptor){
+		return ENOMEM;
+	}
 	memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
 	memcpy(full_config_descriptor + sizeof (descriptor),
@@ -370,8 +370,9 @@
 		sizeof (ohci_rh_iface_descriptor) +
 		sizeof (ohci_rh_ep_descriptor),
-		hub_descriptor, hub_desc_size);
-
+		instance->hub_descriptor, instance->descriptor_size);
+	
 	instance->descriptors.configuration = full_config_descriptor;
 	instance->descriptors.configuration_size = descriptor.total_length;
+	return EOK;
 }
 /*----------------------------------------------------------------------------*/
@@ -464,19 +465,13 @@
  * bit i indicates change on i`th port (i>0). For more info see
  * Hub and Port status bitmap specification in USB specification
- * (chapter 11.13.4)
- * @param instance root hub instance
- * @param@out buffer pointer to created interrupt mas
- * @param@out buffer_size size of created interrupt mask
- */
-static void create_interrupt_mask(rh_t *instance, void ** buffer,
-	size_t * buffer_size) {
-	int bit_count = instance->port_count + 1;
-	(*buffer_size) = (bit_count+7 / 8);
-
-	(*buffer) = instance->interrupt_buffer;//malloc(*buffer_size);
-	uint8_t * bitmap = (uint8_t*) (*buffer);
+ * (chapter 11.13.4).
+ * Uses instance`s interrupt buffer to store the interrupt information.
+ * @param instance root hub instance
+ */
+static void create_interrupt_mask_in_instance(rh_t * instance) {
+	uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer);
 	uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
 		| (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
-	bzero(bitmap, (*buffer_size));
+	bzero(bitmap, instance->interrupt_mask_size);
 	if (instance->registers->rh_status & mask) {
 		bitmap[0] = 1;
@@ -509,13 +504,10 @@
 	const uint16_t setup_request_value = setup_request->value_high;
 	//(setup_request->value_low << 8);
-	bool del = false;
 	switch (setup_request_value) {
 		case USB_DESCTYPE_HUB:
 		{
-			uint8_t * descriptor;
-			usb_create_serialized_hub_descriptor(
-				instance, &descriptor, &size);
-			result_descriptor = descriptor;
-			if (result_descriptor) del = true;
+			usb_log_debug("USB_DESCTYPE_HUB\n");
+			result_descriptor = instance->hub_descriptor;
+			size = instance->descriptor_size;
 			break;
 		}
@@ -568,6 +560,4 @@
 	request->transfered_size = size;
 	memcpy(request->data_buffer, result_descriptor, size);
-	if (del)
-		free(result_descriptor);
 	return EOK;
 }
@@ -895,14 +885,11 @@
  * @param instance hub instance
  * @param request batch request to be processed
- * @param change_buffer chages on hub
- * @param buffer_size size of change buffer
  *
  * @return
  */
-static int process_interrupt(rh_t *instance, usb_transfer_batch_t * request,
-    void * change_buffer, size_t buffe_size){
-	create_interrupt_mask(instance, &change_buffer,
-	    &(request->transfered_size));
-	memcpy(request->data_buffer, change_buffer,request->transfered_size);
+static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request){
+	memcpy(request->data_buffer, instance->interrupt_buffer,
+	    instance->interrupt_mask_size);
+	request->transfered_size = instance->interrupt_mask_size;
 	instance->unfinished_interrupt_transfer = NULL;
 	usb_transfer_batch_finish_error(request, EOK);
Index: uspace/drv/ohci/root_hub.h
===================================================================
--- uspace/drv/ohci/root_hub.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/ohci/root_hub.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -59,7 +59,16 @@
 	 *
 	 * This is allocated when initializing instance, so that memory
-	 * allocation is not needed when processing request.
+	 * allocation is not needed when processing request. Buffer is used for
+	 * interrupt bitmask.
 	 */
 	uint8_t * interrupt_buffer;
+	/** size of interrupt buffer */
+	size_t interrupt_mask_size;
+	/** instance`s descriptor*/
+	uint8_t * hub_descriptor;
+	/** size of hub descriptor */
+	size_t descriptor_size;
+
+
 } rh_t;
 
Index: uspace/drv/usbhid/Makefile
===================================================================
--- uspace/drv/usbhid/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -47,4 +47,5 @@
 	mouse/mousedev.c \
 	lgtch-ultrax/lgtch-ultrax.c \
+	lgtch-ultrax/keymap.c \
 	$(STOLEN_LAYOUT_SOURCES)
 
Index: uspace/drv/usbhid/generic/hiddev.c
===================================================================
--- uspace/drv/usbhid/generic/hiddev.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/generic/hiddev.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -37,4 +37,8 @@
 #include <usb/debug.h>
 #include <usb/classes/classes.h>
+#include <errno.h>
+#include <str_error.h>
+
+#include <usbhid_iface.h>
 
 #include "hiddev.h"
@@ -55,4 +59,99 @@
 /*----------------------------------------------------------------------------*/
 
+static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
+
+static int usb_generic_hid_get_event(ddf_fun_t *fun, int32_t *buffer, 
+    size_t size, size_t *act_size, unsigned int flags);
+
+/*----------------------------------------------------------------------------*/
+
+static usbhid_iface_t usb_generic_iface = {
+	.get_event = usb_generic_hid_get_event,
+	.get_event_length = usb_generic_hid_get_event_length
+};
+
+static ddf_dev_ops_t usb_generic_hid_ops = {
+	.interfaces[USBHID_DEV_IFACE] = &usb_generic_iface
+};
+
+/*----------------------------------------------------------------------------*/
+
+static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
+{
+	if (fun == NULL || fun->driver_data) {
+		return 0;
+	}
+
+	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
+	
+	return hid_dev->input_report_size;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static int usb_generic_hid_get_event(ddf_fun_t *fun, int32_t *buffer, 
+    size_t size, size_t *act_size, unsigned int flags)
+{
+	if (fun == NULL || fun->driver_data) {
+		return EINVAL;
+	}
+
+	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
+	
+	if (hid_dev->input_report_size > size) {
+		return EINVAL;	// TODO: other error code
+	}
+	
+	/*! @todo This should probably be atomic. */
+	memcpy(buffer, hid_dev->input_report, hid_dev->input_report_size);
+	*act_size = hid_dev->input_report_size;
+	
+	// clear the buffer so that it will not be received twice
+	memset(hid_dev->input_report, 0, hid_dev->input_report_size);
+	
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
+{	
+	/* Create the function exposed under /dev/devices. */
+	/** @todo Generate numbers for the devices? */
+	usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
+	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 
+	    HID_GENERIC_FUN_NAME);
+	if (fun == NULL) {
+		usb_log_error("Could not create DDF function node.\n");
+		return ENOMEM;
+	}
+
+	int rc = ddf_fun_bind(fun);
+	if (rc != EOK) {
+		usb_log_error("Could not bind DDF function: %s.\n",
+		    str_error(rc));
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	fun->ops = &usb_generic_hid_ops;
+	fun->driver_data = hid_dev;
+	
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+int usb_generic_hid_init(usb_hid_dev_t *hid_dev)
+{
+	if (hid_dev == NULL) {
+		return EINVAL;
+	}
+	
+	return usb_generic_hid_create_function(hid_dev);
+}
+
+/*----------------------------------------------------------------------------*/
+
 bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, 
     uint8_t *buffer, size_t buffer_size)
Index: uspace/drv/usbhid/generic/hiddev.h
===================================================================
--- uspace/drv/usbhid/generic/hiddev.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/generic/hiddev.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -46,4 +46,8 @@
 const char *HID_GENERIC_CLASS_NAME;
 
+/*----------------------------------------------------------------------------*/
+
+int usb_generic_hid_init(struct usb_hid_dev *hid_dev);
+
 bool usb_generic_hid_polling_callback(struct usb_hid_dev *hid_dev,
     uint8_t *buffer, size_t buffer_size);
Index: uspace/drv/usbhid/kbd/conv.c
===================================================================
--- uspace/drv/usbhid/kbd/conv.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/kbd/conv.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -99,5 +99,6 @@
 	[0x30] = KC_RBRACKET,
 	[0x31] = KC_BACKSLASH,
-	//[0x32] = KC_,	// TODO: HASH??? maybe some as 0x31 - backslash
+	//[0x32] = KC_,	// TODO: HASH??? maybe same as 0x31 - backslash
+	[0x32] = KC_BACKSLASH,
 	[0x33] = KC_SEMICOLON,
 	[0x34] = KC_QUOTE,  // same as APOSTROPHE? (')
Index: uspace/drv/usbhid/kbd/kbddev.c
===================================================================
--- uspace/drv/usbhid/kbd/kbddev.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/kbd/kbddev.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -177,10 +177,10 @@
 /*----------------------------------------------------------------------------*/
 
-static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
-    uint8_t report_id, void *arg);
-
-static const usb_hid_report_in_callbacks_t usb_kbd_parser_callbacks = {
-	.keyboard = usb_kbd_process_keycodes
-};
+//static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
+//    uint8_t report_id, void *arg);
+
+//static const usb_hid_report_in_callbacks_t usb_kbd_parser_callbacks = {
+//	.keyboard = usb_kbd_process_keycodes
+//};
 
 /*----------------------------------------------------------------------------*/
@@ -203,27 +203,27 @@
 /*----------------------------------------------------------------------------*/
 /** Mapping of USB modifier key codes to generic modifier key codes. */
-static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
-	KC_LCTRL,         /* USB_HID_MOD_LCTRL */
-	KC_LSHIFT,        /* USB_HID_MOD_LSHIFT */
-	KC_LALT,          /* USB_HID_MOD_LALT */
-	0,                /* USB_HID_MOD_LGUI */
-	KC_RCTRL,         /* USB_HID_MOD_RCTRL */
-	KC_RSHIFT,        /* USB_HID_MOD_RSHIFT */
-	KC_RALT,          /* USB_HID_MOD_RALT */
-	0,                /* USB_HID_MOD_RGUI */
-};
-
-typedef enum usbhid_lock_code {
-	USB_KBD_LOCK_NUM = 0x53,
-	USB_KBD_LOCK_CAPS = 0x39,
-	USB_KBD_LOCK_SCROLL = 0x47,
-	USB_KBD_LOCK_COUNT = 3
-} usbhid_lock_code;
-
-static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {
-	USB_KBD_LOCK_NUM,
-	USB_KBD_LOCK_CAPS,
-	USB_KBD_LOCK_SCROLL
-};
+//static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
+//	KC_LCTRL,         /* USB_HID_MOD_LCTRL */
+//	KC_LSHIFT,        /* USB_HID_MOD_LSHIFT */
+//	KC_LALT,          /* USB_HID_MOD_LALT */
+//	0,                /* USB_HID_MOD_LGUI */
+//	KC_RCTRL,         /* USB_HID_MOD_RCTRL */
+//	KC_RSHIFT,        /* USB_HID_MOD_RSHIFT */
+//	KC_RALT,          /* USB_HID_MOD_RALT */
+//	0,                /* USB_HID_MOD_RGUI */
+//};
+
+//typedef enum usbhid_lock_code {
+//	USB_KBD_LOCK_NUM = 0x53,
+//	USB_KBD_LOCK_CAPS = 0x39,
+//	USB_KBD_LOCK_SCROLL = 0x47,
+//	USB_KBD_LOCK_COUNT = 3
+//} usbhid_lock_code;
+
+//static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {
+//	USB_KBD_LOCK_NUM,
+//	USB_KBD_LOCK_CAPS,
+//	USB_KBD_LOCK_SCROLL
+//};
 
 /*----------------------------------------------------------------------------*/
@@ -299,32 +299,39 @@
 		return;
 	}
-	
-	unsigned i = 0;
-	
+		
 	/* Reset the LED data. */
 	memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
-	
-	if ((kbd_dev->mods & KM_NUM_LOCK) && (i < kbd_dev->led_output_size)) {
-		kbd_dev->led_data[i++] = USB_HID_LED_NUM_LOCK;
-	}
-	
-	if ((kbd_dev->mods & KM_CAPS_LOCK) && (i < kbd_dev->led_output_size)) {
-		kbd_dev->led_data[i++] = USB_HID_LED_CAPS_LOCK;
-	}
-	
-	if ((kbd_dev->mods & KM_SCROLL_LOCK) 
-	    && (i < kbd_dev->led_output_size)) {
-		kbd_dev->led_data[i++] = USB_HID_LED_SCROLL_LOCK;
-	}
-
-	// TODO: COMPOSE and KANA
-	
-	usb_log_debug("Creating output report.\n");
-	
-	int rc = usb_hid_report_output_translate(hid_dev->parser, 
-	    kbd_dev->led_path, 
-	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
-	    kbd_dev->output_buffer, 
-	    kbd_dev->output_size, kbd_dev->led_data, kbd_dev->led_output_size);
+	usb_log_debug("Creating output report:\n");
+
+	usb_hid_report_field_t *field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, kbd_dev->led_path, 
+	    USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY | USB_HID_PATH_COMPARE_END,
+	    USB_HID_REPORT_TYPE_OUTPUT);
+	
+	while (field != NULL) {
+
+		if ((field->usage == USB_HID_LED_NUM_LOCK) 
+		    && (kbd_dev->mods & KM_NUM_LOCK)){
+			field->value = 1;
+		}
+
+		if ((field->usage == USB_HID_LED_CAPS_LOCK) 
+		    && (kbd_dev->mods & KM_CAPS_LOCK)){
+			field->value = 1;
+		}
+
+		if ((field->usage == USB_HID_LED_SCROLL_LOCK) 
+		    && (kbd_dev->mods & KM_SCROLL_LOCK)){
+			field->value = 1;
+		}
+		
+		field = usb_hid_report_get_sibling(hid_dev->report, field,
+		    kbd_dev->led_path, USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY 
+		    | USB_HID_PATH_COMPARE_END, USB_HID_REPORT_TYPE_OUTPUT);
+	}
+	
+	// TODO: what about the Report ID?
+	int rc = usb_hid_report_output_translate(hid_dev->report, 0,
+	    kbd_dev->output_buffer, kbd_dev->output_size);
 	
 	if (rc != EOK) {
@@ -485,5 +492,5 @@
  */
 static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev, 
-    usb_kbd_t *kbd_dev, const uint8_t *key_codes, size_t count)
+    usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/)
 {
 	unsigned int key;
@@ -499,8 +506,8 @@
 	 */
 	i = 0;
-	while (i < count && key_codes[i] != ERROR_ROLLOVER) {
+	while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
 		++i;
 	}
-	if (i != count) {
+	if (i != kbd_dev->key_count) {
 		usb_log_debug("Phantom state occured.\n");
 		// phantom state, do nothing
@@ -508,21 +515,18 @@
 	}
 	
-	/* TODO: quite dummy right now, think of better implementation */
-	assert(count == kbd_dev->key_count);
-	
 	/*
 	 * 1) Key releases
 	 */
-	for (j = 0; j < count; ++j) {
+	for (j = 0; j < kbd_dev->key_count; ++j) {
 		// try to find the old key in the new key list
 		i = 0;
 		while (i < kbd_dev->key_count
-		    && key_codes[i] != kbd_dev->keys[j]) {
+		    && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
 			++i;
 		}
 		
-		if (i == count) {
+		if (i == kbd_dev->key_count) {
 			// not found, i.e. the key was released
-			key = usbhid_parse_scancode(kbd_dev->keys[j]);
+			key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
 			if (!usb_kbd_is_lock(key)) {
 				usb_kbd_repeat_stop(kbd_dev, key);
@@ -541,15 +545,15 @@
 		// try to find the new key in the old key list
 		j = 0;
-		while (j < count && kbd_dev->keys[j] != key_codes[i]) { 
+		while (j < kbd_dev->key_count 
+		    && kbd_dev->keys_old[j] != kbd_dev->keys[i]) { 
 			++j;
 		}
 		
-		if (j == count) {
+		if (j == kbd_dev->key_count) {
 			// not found, i.e. new key pressed
-			key = usbhid_parse_scancode(key_codes[i]);
+			key = usbhid_parse_scancode(kbd_dev->keys[i]);
 			usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
-			    key_codes[i]);
-			usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, 
-			    key);
+			    kbd_dev->keys[i]);
+			usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
 			if (!usb_kbd_is_lock(key)) {
 				usb_kbd_repeat_start(kbd_dev, key);
@@ -560,8 +564,24 @@
 	}
 	
-	memcpy(kbd_dev->keys, key_codes, count);
-
-	usb_log_debug("New stored keycodes: %s\n", 
-	    usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0));
+//	usb_log_debug("Old keys: ");
+//	for (i = 0; i < kbd_dev->key_count; ++i) {
+//		usb_log_debug("%d ", kbd_dev->keys_old[i]);
+//	}
+//	usb_log_debug("\n");
+	
+	
+//	usb_log_debug("New keys: ");
+//	for (i = 0; i < kbd_dev->key_count; ++i) {
+//		usb_log_debug("%d ", kbd_dev->keys[i]);
+//	}
+//	usb_log_debug("\n");
+	
+	memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
+	
+	usb_log_debug2("New stored keys: ");
+	for (i = 0; i < kbd_dev->key_count; ++i) {
+		usb_log_debug2("%d ", kbd_dev->keys_old[i]);
+	}
+	usb_log_debug2("\n");
 }
 
@@ -585,34 +605,34 @@
  * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
  */
-static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
-    uint8_t report_id, void *arg)
-{
-	if (arg == NULL) {
-		usb_log_warning("Missing argument in callback "
-		    "usbhid_process_keycodes().\n");
-		return;
-	}
-	
-	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
-	
-	if (hid_dev->data == NULL) {
-		usb_log_warning("Missing KBD device structure in callback.\n");
-		return;
-	}
-	
-	usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
-
-	usb_log_debug("Got keys from parser (report id: %u): %s\n", 
-	    report_id, usb_debug_str_buffer(key_codes, count, 0));
-	
-	if (count != kbd_dev->key_count) {
-		usb_log_warning("Number of received keycodes (%zu) differs from"
-		    " expected (%zu).\n", count, kbd_dev->key_count);
-		return;
-	}
-	
-	///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
-	usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
-}
+//static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
+//    uint8_t report_id, void *arg)
+//{
+//	if (arg == NULL) {
+//		usb_log_warning("Missing argument in callback "
+//		    "usbhid_process_keycodes().\n");
+//		return;
+//	}
+	
+//	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
+	
+//	if (hid_dev->data == NULL) {
+//		usb_log_warning("Missing KBD device structure in callback.\n");
+//		return;
+//	}
+	
+//	usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
+
+//	usb_log_debug("Got keys from parser (report id: %u): %s\n", 
+//	    report_id, usb_debug_str_buffer(key_codes, count, 0));
+	
+//	if (count != kbd_dev->key_count) {
+//		usb_log_warning("Number of received keycodes (%zu) differs from"
+//		    " expected (%zu).\n", count, kbd_dev->key_count);
+//		return;
+//	}
+	
+//	///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
+//	usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
+//}
 
 /*----------------------------------------------------------------------------*/
@@ -638,5 +658,9 @@
                                  uint8_t *buffer, size_t actual_size)
 {
-	assert(hid_dev->parser != NULL);
+	assert(hid_dev->report != NULL);
+	assert(hid_dev != NULL);
+	assert(hid_dev->data != NULL);
+	
+	usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
 
 	usb_log_debug("Calling usb_hid_parse_report() with "
@@ -648,16 +672,59 @@
 	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
 	//usb_hid_report_path_set_report_id(path, 0);
-	
-	int rc = usb_hid_parse_report(hid_dev->parser, buffer,
-	    actual_size, path, 
+
+	uint8_t report_id;
+	int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size, 
+	    &report_id);
+	
+	if (rc != EOK) {
+		usb_log_warning("Error in usb_hid_parse_report():"
+		    "%s\n", str_error(rc));
+	}
+	
+	usb_hid_report_path_set_report_id (path, report_id);
+	
+	// fill in the currently pressed keys
+	
+	usb_hid_report_field_t *field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, path, 
 	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
-	    &usb_kbd_parser_callbacks, hid_dev);
-
+	    USB_HID_REPORT_TYPE_INPUT);
+	unsigned i = 0;
+	
+	while (field != NULL) {
+		usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n", 
+		    field, field->value, field->usage);
+		
+		assert(i < kbd_dev->key_count);
+//		if (i == kbd_dev->key_count) {
+//			break;
+//		}
+		
+		// save the key usage
+		/* TODO: maybe it's not good to save value, nor usage
+		 *       as the value may be e.g. 1 for LEDs and usage may be
+		 *       value of the LED. On the other hand, in case of normal
+		 *       keys, the usage is more important and we must check
+		 *       that. One possible solution: distinguish between those
+		 *       two parts of the Report somehow.
+		 */
+		if (field->value != 0) {
+			kbd_dev->keys[i] = field->usage;
+		}
+		else {
+			kbd_dev->keys[i] = 0;
+		}
+		usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
+		
+		++i;
+		field = usb_hid_report_get_sibling(hid_dev->report, field, path, 
+		    USB_HID_PATH_COMPARE_END 
+		    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
+		    USB_HID_REPORT_TYPE_INPUT);
+	}
+	
 	usb_hid_report_path_free(path);
 	
-	if (rc != EOK) {
-		usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
-		    "%s\n", str_error(rc));
-	}
+	usb_kbd_check_key_changes(hid_dev, kbd_dev);
 }
 
@@ -696,4 +763,49 @@
 	
 	return kbd_dev;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static int usb_kbd_create_function(usb_hid_dev_t *hid_dev)
+{
+	assert(hid_dev != NULL);
+	assert(hid_dev->usb_dev != NULL);
+	
+	/* Create the function exposed under /dev/devices. */
+	usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
+	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 
+	    HID_KBD_FUN_NAME);
+	if (fun == NULL) {
+		usb_log_error("Could not create DDF function node.\n");
+		return ENOMEM;
+	}
+	
+	/*
+	 * Store the initialized HID device and HID ops
+	 * to the DDF function.
+	 */
+	fun->ops = &hid_dev->ops;
+	fun->driver_data = hid_dev;   // TODO: maybe change to hid_dev->data
+
+	int rc = ddf_fun_bind(fun);
+	if (rc != EOK) {
+		usb_log_error("Could not bind DDF function: %s.\n",
+		    str_error(rc));
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	usb_log_debug("Adding DDF function to class %s...\n", 
+	    HID_KBD_CLASS_NAME);
+	rc = ddf_fun_add_to_class(fun, HID_KBD_CLASS_NAME);
+	if (rc != EOK) {
+		usb_log_error(
+		    "Could not add DDF function to class %s: %s.\n",
+		    HID_KBD_CLASS_NAME, str_error(rc));
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	return EOK;
 }
 
@@ -747,5 +859,5 @@
 	
 	kbd_dev->key_count = usb_hid_report_input_length(
-	    hid_dev->parser, path, 
+	    hid_dev->report, path, 
 	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
 	usb_hid_report_path_free(path);
@@ -753,5 +865,5 @@
 	usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
 	
-	kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));
+	kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
 	
 	if (kbd_dev->keys == NULL) {
@@ -761,15 +873,24 @@
 	}
 	
+	kbd_dev->keys_old = 
+		(int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
+	
+	if (kbd_dev->keys_old == NULL) {
+		usb_log_fatal("No memory!\n");
+		free(kbd_dev->keys);
+		free(kbd_dev);
+		return ENOMEM;
+	}
+	
 	/*
 	 * Output report
 	 */
 	kbd_dev->output_size = 0;
-	kbd_dev->output_buffer = usb_hid_report_output(hid_dev->parser, 
-	    &kbd_dev->output_size);
-	if (kbd_dev->output_buffer == NULL && kbd_dev->output_size != 0) {
+	kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report, 
+	    &kbd_dev->output_size, 0);
+	if (kbd_dev->output_buffer == NULL) {
 		usb_log_warning("Error creating output report buffer.\n");
 		free(kbd_dev->keys);
-		free(kbd_dev);
-		return ENOMEM;
+		return ENOMEM;  /* TODO: other error code */
 	}
 	
@@ -780,5 +901,5 @@
 	    kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
 	
-	kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->parser, 
+	kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->report, 
 	    kbd_dev->led_path, 
 	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
@@ -854,4 +975,11 @@
 	usb_log_debug("HID/KBD device structure initialized.\n");
 	
+	usb_log_debug("Creating KBD function...\n");
+	int rc = usb_kbd_create_function(hid_dev);
+	if (rc != EOK) {
+		usb_kbd_free(&kbd_dev);
+		return rc;
+	}
+	
 	return EOK;
 }
@@ -908,6 +1036,20 @@
 	}
 	
-	// free the output buffer
-	usb_hid_report_output_free((*kbd_dev)->output_buffer);
+	// free all buffers
+	if ((*kbd_dev)->keys != NULL) {
+		free((*kbd_dev)->keys);
+	}
+	if ((*kbd_dev)->keys_old != NULL) {
+		free((*kbd_dev)->keys_old);
+	}
+	if ((*kbd_dev)->led_data != NULL) {
+		free((*kbd_dev)->led_data);
+	}
+	if ((*kbd_dev)->led_path != NULL) {
+		usb_hid_report_path_free((*kbd_dev)->led_path);
+	}
+	if ((*kbd_dev)->output_buffer != NULL) {
+		usb_hid_report_output_free((*kbd_dev)->output_buffer);
+	}
 
 	free(*kbd_dev);
@@ -929,4 +1071,5 @@
 		} else {
 			usb_kbd_free(&kbd_dev);
+			hid_dev->data = NULL;
 		}
 	}
@@ -937,5 +1080,5 @@
 int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
 {
-	int rc = usb_hid_parse_report_descriptor(hid_dev->parser, 
+	int rc = usb_hid_parse_report_descriptor(hid_dev->report, 
 	    USB_KBD_BOOT_REPORT_DESCRIPTOR, 
 	    USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
Index: uspace/drv/usbhid/kbd/kbddev.h
===================================================================
--- uspace/drv/usbhid/kbd/kbddev.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/kbd/kbddev.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -65,6 +65,8 @@
  */
 typedef struct usb_kbd_t {
+	/** Previously pressed keys (not translated to key codes). */
+	int32_t *keys_old;
 	/** Currently pressed keys (not translated to key codes). */
-	uint8_t *keys;
+	int32_t *keys;
 	/** Count of stored keys (i.e. number of keys in the report). */
 	size_t key_count;
Index: uspace/drv/usbhid/lgtch-ultrax/keymap.c
===================================================================
--- uspace/drv/usbhid/lgtch-ultrax/keymap.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/drv/usbhid/lgtch-ultrax/keymap.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2011 Lubos Slovak
+ * 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 drvusbhid
+ * @{
+ */
+/** @file
+ * UUSB multimedia key to keycode mapping.
+ */
+
+#include <io/keycode.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <usb/debug.h>
+#include "keymap.h"
+
+/**
+ * Mapping between USB HID multimedia usages (from HID Usage Tables) and 
+ * corresponding HelenOS key codes.
+ *
+ * Currently only Usages used by Logitech UltraX keyboard are present. All other
+ * should result in 0.
+ */
+static int usb_hid_keymap_consumer[0x29c] = {
+	[0xf] = KC_F1, /* Just for testing purposes */
+	[0x5] = KC_F1, /* Just for testing purposes */
+	[0x8] = KC_F3, /* Just for testing purposes */
+	[0x6] = KC_F4, /* Just for testing purposes */
+	[0x7] = KC_F5, /* Just for testing purposes */
+	[0xc] = KC_F6, /* Just for testing purposes */
+	
+	[0xb5] = 0,       /* Scan Next Track */
+	[0xb6] = 0,       /* Scan Previous Track */
+	[0xb7] = 0,       /* Stop */
+	[0xb8] = 0,       /* Eject */
+	[0xcd] = KC_F2,   /* Play/Pause */
+	[0xe2] = KC_F3,   /* Mute */
+	[0xe9] = KC_F5,   /* Volume Increment */
+	[0xea] = KC_F4,   /* Volume Decrement */
+	[0x183] = 0,      /* AL Consumer Control Configuration */
+	[0x18a] = 0,      /* AL Email Reader */
+	[0x192] = 0,      /* AL Calculator */
+	[0x221] = 0,      /* AC Search */
+	[0x223] = 0,      /* AC Home */
+	[0x224] = 0,      /* AC Back */
+	[0x225] = 0,      /* AC Forward */
+	[0x226] = 0,      /* AC Stop */
+	[0x227] = KC_F1,  /* AC Refresh */
+	[0x22a] = KC_F6   /* AC Bookmarks */
+};
+
+/**
+ * Translates USB HID Usages from the Consumer Page into HelenOS keycodes.
+ *
+ * @param usage USB HID Consumer Page Usage number.
+ * 
+ * @retval HelenOS key code corresponding to the given USB Consumer Page Usage.
+ */
+unsigned int usb_lgtch_map_usage(int usage)
+{
+	unsigned int key;
+	int *map = usb_hid_keymap_consumer;
+	size_t map_length = sizeof(usb_hid_keymap_consumer) / sizeof(int);
+
+	if ((usage < 0) || ((size_t)usage >= map_length))
+		return -1;
+
+	/*! @todo What if the usage is not in the table? */
+	key = map[usage];
+	
+	return key;
+}
+
+/**
+ * @}
+ */
Index: uspace/drv/usbhid/lgtch-ultrax/keymap.h
===================================================================
--- uspace/drv/usbhid/lgtch-ultrax/keymap.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/drv/usbhid/lgtch-ultrax/keymap.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2011 Lubos Slovak
+ * 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 drvusbhid
+ * @{
+ */
+/** @file
+ * USB multimedia key to keycode mapping.
+ */
+
+#ifndef USB_HID_LGTCH_ULTRAX_KEYMAP_H_
+#define USB_HID_LGTCH_ULTRAX_KEYMAP_H_
+
+unsigned int usb_lgtch_map_usage(int usage);
+
+#endif /* USB_HID_LGTCH_ULTRAX_KEYMAP_H_ */
+
+/**
+ * @}
+ */
Index: uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.c
===================================================================
--- uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -38,30 +38,330 @@
 #include "lgtch-ultrax.h"
 #include "../usbhid.h"
+#include "keymap.h"
 
 #include <usb/classes/hidparser.h>
 #include <usb/debug.h>
+#include <usb/classes/hidut.h>
+
 #include <errno.h>
 #include <str_error.h>
 
+#include <ipc/kbd.h>
+#include <io/console.h>
+
 #define NAME "lgtch-ultrax"
 
-/*----------------------------------------------------------------------------*/
-
-static void usb_lgtch_process_keycodes(const uint8_t *key_codes, size_t count,
-    uint8_t report_id, void *arg);
-
-static const usb_hid_report_in_callbacks_t usb_lgtch_parser_callbacks = {
-	.keyboard = usb_lgtch_process_keycodes
+typedef enum usb_lgtch_flags {
+	USB_LGTCH_STATUS_UNINITIALIZED = 0,
+	USB_LGTCH_STATUS_INITIALIZED = 1,
+	USB_LGTCH_STATUS_TO_DESTROY = -1
+} usb_lgtch_flags;
+
+/*----------------------------------------------------------------------------*/
+/**
+ * Logitech UltraX device type.
+ */
+typedef struct usb_lgtch_ultrax_t {
+	/** Previously pressed keys (not translated to key codes). */
+	int32_t *keys_old;
+	/** Currently pressed keys (not translated to key codes). */
+	int32_t *keys;
+	/** Count of stored keys (i.e. number of keys in the report). */
+	size_t key_count;
+	
+	/** IPC phone to the console device (for sending key events). */
+	int console_phone;
+
+	/** Information for auto-repeat of keys. */
+//	usb_kbd_repeat_t repeat;
+	
+	/** Mutex for accessing the information about auto-repeat. */
+//	fibril_mutex_t *repeat_mtx;
+
+	/** State of the structure (for checking before use). 
+	 * 
+	 * 0 - not initialized
+	 * 1 - initialized
+	 * -1 - ready for destroying
+	 */
+	int initialized;
+} usb_lgtch_ultrax_t;
+
+
+/*----------------------------------------------------------------------------*/
+/** 
+ * Default handler for IPC methods not handled by DDF.
+ *
+ * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
+ * assumes the caller is the console and thus it stores IPC phone to it for 
+ * later use by the driver to notify about key events.
+ *
+ * @param fun Device function handling the call.
+ * @param icallid Call id.
+ * @param icall Call data.
+ */
+static void default_connection_handler(ddf_fun_t *fun,
+    ipc_callid_t icallid, ipc_call_t *icall)
+{
+	usb_log_debug(NAME " default_connection_handler()\n");
+	
+	sysarg_t method = IPC_GET_IMETHOD(*icall);
+	
+	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
+	
+	if (hid_dev == NULL || hid_dev->data == NULL) {
+		async_answer_0(icallid, EINVAL);
+		return;
+	}
+	
+	assert(hid_dev != NULL);
+	assert(hid_dev->data != NULL);
+	usb_lgtch_ultrax_t *lgtch_dev = (usb_lgtch_ultrax_t *)hid_dev->data;
+
+	if (method == IPC_M_CONNECT_TO_ME) {
+		int callback = IPC_GET_ARG5(*icall);
+
+		if (lgtch_dev->console_phone != -1) {
+			async_answer_0(icallid, ELIMIT);
+			return;
+		}
+
+		lgtch_dev->console_phone = callback;
+		usb_log_debug(NAME " Saved phone to console: %d\n", callback);
+		async_answer_0(icallid, EOK);
+		return;
+	}
+	
+	async_answer_0(icallid, EINVAL);
+}
+
+/*----------------------------------------------------------------------------*/
+
+static ddf_dev_ops_t lgtch_ultrax_ops = {
+	.default_handler = default_connection_handler
 };
 
 /*----------------------------------------------------------------------------*/
 
-static void usb_lgtch_process_keycodes(const uint8_t *key_codes, size_t count,
-    uint8_t report_id, void *arg)
-{
-	// TODO: checks
-	
-	usb_log_debug(NAME " Got keys from parser (report id: %u): %s\n", 
-	    report_id, usb_debug_str_buffer(key_codes, count, 0));
+//static void usb_lgtch_process_keycodes(const uint8_t *key_codes, size_t count,
+//    uint8_t report_id, void *arg);
+
+//static const usb_hid_report_in_callbacks_t usb_lgtch_parser_callbacks = {
+//	.keyboard = usb_lgtch_process_keycodes
+//};
+
+///*----------------------------------------------------------------------------*/
+
+//static void usb_lgtch_process_keycodes(const uint8_t *key_codes, size_t count,
+//    uint8_t report_id, void *arg)
+//{
+//	// TODO: checks
+	
+//	usb_log_debug(NAME " Got keys from parser (report id: %u): %s\n", 
+//	    report_id, usb_debug_str_buffer(key_codes, count, 0));
+//}
+
+/*----------------------------------------------------------------------------*/
+/**
+ * Processes key events.
+ *
+ * @note This function was copied from AT keyboard driver and modified to suit
+ *       USB keyboard.
+ *
+ * @note Lock keys are not sent to the console, as they are completely handled
+ *       in the driver. It may, however, be required later that the driver
+ *       sends also these keys to application (otherwise it cannot use those
+ *       keys at all).
+ * 
+ * @param hid_dev 
+ * @param lgtch_dev 
+ * @param type Type of the event (press / release). Recognized values: 
+ *             KEY_PRESS, KEY_RELEASE
+ * @param key Key code of the key according to HID Usage Tables.
+ */
+static void usb_lgtch_push_ev(usb_hid_dev_t *hid_dev, int type, 
+    unsigned int key)
+{
+	assert(hid_dev != NULL);
+	assert(hid_dev->data != NULL);
+	
+	usb_lgtch_ultrax_t *lgtch_dev = (usb_lgtch_ultrax_t *)hid_dev->data;
+	
+	console_event_t ev;
+	
+	ev.type = type;
+	ev.key = key;
+	ev.mods = 0;
+
+	ev.c = 0;
+
+	usb_log_debug2(NAME " Sending key %d to the console\n", ev.key);
+	if (lgtch_dev->console_phone < 0) {
+		usb_log_warning(
+		    "Connection to console not ready, key discarded.\n");
+		return;
+	}
+	
+	async_msg_4(lgtch_dev->console_phone, KBD_EVENT, ev.type, ev.key, 
+	    ev.mods, ev.c);
+}
+
+/*----------------------------------------------------------------------------*/
+
+static void usb_lgtch_free(usb_lgtch_ultrax_t **lgtch_dev)
+{
+	if (lgtch_dev == NULL || *lgtch_dev == NULL) {
+		return;
+	}
+	
+	// hangup phone to the console
+	async_hangup((*lgtch_dev)->console_phone);
+	
+//	if ((*lgtch_dev)->repeat_mtx != NULL) {
+//		/* TODO: replace by some check and wait */
+//		assert(!fibril_mutex_is_locked((*lgtch_dev)->repeat_mtx));
+//		free((*lgtch_dev)->repeat_mtx);
+//	}
+	
+	// free all buffers
+	if ((*lgtch_dev)->keys != NULL) {
+		free((*lgtch_dev)->keys);
+	}
+	if ((*lgtch_dev)->keys_old != NULL) {
+		free((*lgtch_dev)->keys_old);
+	}
+
+	free(*lgtch_dev);
+	*lgtch_dev = NULL;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static int usb_lgtch_create_function(usb_hid_dev_t *hid_dev)
+{
+	/* Create the function exposed under /dev/devices. */
+	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 
+	    NAME);
+	if (fun == NULL) {
+		usb_log_error("Could not create DDF function node.\n");
+		return ENOMEM;
+	}
+	
+	/*
+	 * Store the initialized HID device and HID ops
+	 * to the DDF function.
+	 */
+	fun->ops = &lgtch_ultrax_ops;
+	fun->driver_data = hid_dev;   // TODO: maybe change to hid_dev->data
+	
+	int rc = ddf_fun_bind(fun);
+	if (rc != EOK) {
+		usb_log_error("Could not bind DDF function: %s.\n",
+		    str_error(rc));
+		// TODO: Can / should I destroy the DDF function?
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	rc = ddf_fun_add_to_class(fun, "keyboard");
+	if (rc != EOK) {
+		usb_log_error(
+		    "Could not add DDF function to class 'keyboard': %s.\n",
+		    str_error(rc));
+		// TODO: Can / should I destroy the DDF function?
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+int usb_lgtch_init(struct usb_hid_dev *hid_dev)
+{
+	if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
+		return EINVAL; /*! @todo Other return code? */
+	}
+	
+	usb_log_debug(NAME " Initializing HID/lgtch_ultrax structure...\n");
+	
+	usb_lgtch_ultrax_t *lgtch_dev = (usb_lgtch_ultrax_t *)malloc(
+	    sizeof(usb_lgtch_ultrax_t));
+	if (lgtch_dev == NULL) {
+		return ENOMEM;
+	}
+	
+	lgtch_dev->console_phone = -1;
+	
+	usb_hid_report_path_t *path = usb_hid_report_path();
+	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
+	
+	usb_hid_report_path_set_report_id(path, 1);
+	
+	lgtch_dev->key_count = usb_hid_report_input_length(
+	    hid_dev->report, path, 
+	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
+	usb_hid_report_path_free(path);
+	
+	usb_log_debug(NAME " Size of the input report: %zu\n", 
+	    lgtch_dev->key_count);
+	
+	lgtch_dev->keys = (int32_t *)calloc(lgtch_dev->key_count, 
+	    sizeof(int32_t));
+	
+	if (lgtch_dev->keys == NULL) {
+		usb_log_fatal("No memory!\n");
+		free(lgtch_dev);
+		return ENOMEM;
+	}
+	
+	lgtch_dev->keys_old = 
+		(int32_t *)calloc(lgtch_dev->key_count, sizeof(int32_t));
+	
+	if (lgtch_dev->keys_old == NULL) {
+		usb_log_fatal("No memory!\n");
+		free(lgtch_dev->keys);
+		free(lgtch_dev);
+		return ENOMEM;
+	}
+	
+	/*! @todo Autorepeat */
+	
+	// save the KBD device structure into the HID device structure
+	hid_dev->data = lgtch_dev;
+	
+	lgtch_dev->initialized = USB_LGTCH_STATUS_INITIALIZED;
+	usb_log_debug(NAME " HID/lgtch_ultrax device structure initialized.\n");
+	
+	int rc = usb_lgtch_create_function(hid_dev);
+	if (rc != EOK) {
+		usb_lgtch_free(&lgtch_dev);
+		return rc;
+	}
+	
+	usb_log_debug(NAME " HID/lgtch_ultrax structure initialized.\n");
+	
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+void usb_lgtch_deinit(struct usb_hid_dev *hid_dev)
+{
+	if (hid_dev == NULL) {
+		return;
+	}
+	
+	if (hid_dev->data != NULL) {
+		usb_lgtch_ultrax_t *lgtch_dev = 
+		    (usb_lgtch_ultrax_t *)hid_dev->data;
+//		if (usb_kbd_is_initialized(kbd_dev)) {
+//			usb_kbd_mark_unusable(kbd_dev);
+//		} else {
+			usb_lgtch_free(&lgtch_dev);
+			hid_dev->data = NULL;
+//		}
+	}
 }
 
@@ -81,17 +381,42 @@
 	usb_hid_report_path_t *path = usb_hid_report_path();
 	usb_hid_report_path_append_item(path, 0xc, 0);
-	usb_hid_report_path_set_report_id(path, 1);
-	
-	int rc = usb_hid_parse_report(hid_dev->parser, buffer,
-	    buffer_size, path, 
-	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
-	    &usb_lgtch_parser_callbacks, hid_dev);
+
+	uint8_t report_id;
+	
+	int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size, 
+	    &report_id);
+	
+	if (rc != EOK) {
+		usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n", 
+		    str_error(rc));
+		return true;
+	}
+	
+	usb_hid_report_path_set_report_id(path, report_id);
+
+	usb_hid_report_field_t *field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END 
+	    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
+	    USB_HID_REPORT_TYPE_INPUT);
+	
+	unsigned int key;
+	
+	/*! @todo Is this iterating OK if done multiple times? 
+	 *  @todo The parsing is not OK
+	 */
+	while (field != NULL) {
+		usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)\n", field->value, 
+		    field->usage);
+		
+		key = usb_lgtch_map_usage(field->usage);
+		usb_lgtch_push_ev(hid_dev, KEY_PRESS, key);
+		
+		field = usb_hid_report_get_sibling(
+		    hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
+		    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
+		    USB_HID_REPORT_TYPE_INPUT);
+	}	
 
 	usb_hid_report_path_free(path);
-	
-	if (rc != EOK) {
-		usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
-		    "%s\n", str_error(rc));
-	}
 	
 	return true;
Index: uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.h
===================================================================
--- uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -40,13 +40,10 @@
 
 struct usb_hid_dev;
-//struct usb_hid_subdriver_mapping;
 
 /*----------------------------------------------------------------------------*/
 
-//extern struct usb_hid_subdriver_mapping usb_lgtch_mapping;
+int usb_lgtch_init(struct usb_hid_dev *hid_dev);
 
-/*----------------------------------------------------------------------------*/
-
-//int usb_lgtch_init(struct usb_hid_dev *hid_dev);
+void usb_lgtch_deinit(struct usb_hid_dev *hid_dev);
 
 bool usb_lgtch_polling_callback(struct usb_hid_dev *hid_dev, uint8_t *buffer,
Index: uspace/drv/usbhid/main.c
===================================================================
--- uspace/drv/usbhid/main.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -99,40 +99,14 @@
 	usb_log_debug("USB/HID device structure initialized.\n");
 	
-	/* Create the function exposed under /dev/devices. */
-	ddf_fun_t *hid_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, 
-	    usb_hid_get_function_name(hid_dev));
-	if (hid_fun == NULL) {
-		usb_log_error("Could not create DDF function node.\n");
-		usb_hid_free(&hid_dev);
-		return ENOMEM;
-	}
-	
 	/*
-	 * Store the initialized HID device and HID ops
-	 * to the DDF function.
-	 */
-	hid_fun->ops = &hid_dev->ops;
-	hid_fun->driver_data = hid_dev;   // TODO: maybe change to hid_dev->data
-
-	rc = ddf_fun_bind(hid_fun);
-	if (rc != EOK) {
-		usb_log_error("Could not bind DDF function: %s.\n",
-		    str_error(rc));
-		// TODO: Can / should I destroy the DDF function?
-		ddf_fun_destroy(hid_fun);
-		usb_hid_free(&hid_dev);
-		return rc;
-	}
-	
-	rc = ddf_fun_add_to_class(hid_fun, usb_hid_get_class_name(hid_dev));
-	if (rc != EOK) {
-		usb_log_error(
-		    "Could not add DDF function to class 'hid': %s.\n",
-		    str_error(rc));
-		// TODO: Can / should I destroy the DDF function?
-		ddf_fun_destroy(hid_fun);
-		usb_hid_free(&hid_dev);
-		return rc;
-	}
+	 * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
+	 *    do nej.
+	 * 2) do tych ops do .interfaces[DEV_IFACE_USBHID (asi)] priradi 
+	 *    vyplnenu strukturu usbhid_iface_t.
+	 * 3) klientska aplikacia - musi si rucne vytvorit telefon
+	 *    (devman_device_connect() - cesta k zariadeniu (/hw/pci0/...) az 
+	 *    k tej fcii.
+	 *    pouzit usb/classes/hid/iface.h - prvy int je telefon
+	 */
 	
 	/* Start automated polling function.
Index: uspace/drv/usbhid/mouse/mousedev.c
===================================================================
--- uspace/drv/usbhid/mouse/mousedev.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/mouse/mousedev.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -39,4 +39,5 @@
 #include <usb/classes/hid.h>
 #include <usb/classes/hidreq.h>
+#include <usb/classes/hidut.h>
 #include <errno.h>
 #include <str_error.h>
@@ -45,4 +46,6 @@
 #include "mousedev.h"
 #include "../usbhid.h"
+
+#define NAME "mouse"
 
 /*----------------------------------------------------------------------------*/
@@ -62,4 +65,5 @@
 /** Default idle rate for mouses. */
 static const uint8_t IDLE_RATE = 0;
+static const size_t USB_MOUSE_BUTTON_COUNT = 3;
 
 /*----------------------------------------------------------------------------*/
@@ -170,61 +174,168 @@
 /*----------------------------------------------------------------------------*/
 
-static bool usb_mouse_process_boot_report(usb_mouse_t *mouse_dev,
+static bool usb_mouse_process_boot_report(usb_hid_dev_t *hid_dev,
     uint8_t *buffer, size_t buffer_size)
 {
+	usb_mouse_t *mouse_dev = (usb_mouse_t *)hid_dev->data;
+	
 	usb_log_debug2("got buffer: %s.\n",
 	    usb_debug_str_buffer(buffer, buffer_size, 0));
-
-	uint8_t butt = buffer[0];
-	char str_buttons[4] = {
-		butt & 1 ? '#' : '.',
-		butt & 2 ? '#' : '.',
-		butt & 4 ? '#' : '.',
-		0
-	};
-
-	int shift_x = ((int) buffer[1]) - 127;
-	int shift_y = ((int) buffer[2]) - 127;
-	int wheel = ((int) buffer[3]) - 127;
-
-	if (buffer[1] == 0) {
-		shift_x = 0;
-	}
-	if (buffer[2] == 0) {
-		shift_y = 0;
-	}
-	if (buffer[3] == 0) {
-		wheel = 0;
-	}
-	
-	if (mouse_dev->console_phone >= 0) {
-		usb_log_debug("Console phone: %d\n", mouse_dev->console_phone);
-		if ((shift_x != 0) || (shift_y != 0)) {
-			/* FIXME: guessed for QEMU */
+	
+	if (mouse_dev->console_phone < 0) {
+		usb_log_error(NAME " No console phone.\n");
+		return false;	// ??
+	}
+
+	/*
+	 * parse the input report
+	 */
+	
+	usb_log_debug(NAME " Calling usb_hid_parse_report() with "
+	    "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
+	
+	uint8_t report_id;
+	
+	int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size, 
+	    &report_id);
+	
+	if (rc != EOK) {
+		usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n", 
+		    str_error(rc));
+		return true;
+	}
+	
+	/*
+	 * X
+	 */
+	int shift_x = 0;
+	
+	usb_hid_report_path_t *path = usb_hid_report_path();
+	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP, 
+	    USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
+
+	usb_hid_report_path_set_report_id(path, report_id);
+
+	usb_hid_report_field_t *field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END, 
+	    USB_HID_REPORT_TYPE_INPUT);
+
+	if (field != NULL) {
+		usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value, 
+		    field->usage);
+		shift_x = field->value;
+	}
+
+	usb_hid_report_path_free(path);
+	
+	/*
+	 * Y
+	 */
+	int shift_y = 0;
+	
+	path = usb_hid_report_path();
+	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP, 
+	    USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
+
+	usb_hid_report_path_set_report_id(path, report_id);
+
+	field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END, 
+	    USB_HID_REPORT_TYPE_INPUT);
+
+	if (field != NULL) {
+		usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value, 
+		    field->usage);
+		shift_y = field->value;
+	}
+
+	usb_hid_report_path_free(path);
+	
+	if ((shift_x != 0) || (shift_y != 0)) {
+		async_req_2_0(mouse_dev->console_phone,
+		    MEVENT_MOVE, shift_x, shift_y);
+	}
+	
+	/*
+	 * Buttons
+	 */
+	path = usb_hid_report_path();
+	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
+	usb_hid_report_path_set_report_id(path, report_id);
+	
+	field = usb_hid_report_get_sibling(
+	    hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
+	    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
+	    USB_HID_REPORT_TYPE_INPUT);
+
+	while (field != NULL) {
+		usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value, 
+		    field->usage);
+		
+		if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
+		    && field->value != 0) {
 			async_req_2_0(mouse_dev->console_phone,
-			    MEVENT_MOVE,
-			    - shift_x / 10,  - shift_y / 10);
-		} else {
-			usb_log_error("No move reported\n");
-		}
-		if (butt) {
-			/* FIXME: proper button clicking. */
-			async_req_2_0(mouse_dev->console_phone,
-			    MEVENT_BUTTON, 1, 1);
-			async_req_2_0(mouse_dev->console_phone,
-			    MEVENT_BUTTON, 1, 0);
-		}
-	} else {
-		usb_log_error("No console phone in mouse!!\n");
-	}
-
-	usb_log_debug("buttons=%s  dX=%+3d  dY=%+3d  wheel=%+3d\n",
-	    str_buttons, shift_x, shift_y, wheel);
-
-	/* Guess. */
-	//async_usleep(1000);
-	// no sleep right now
+			    MEVENT_BUTTON, field->usage, 1);
+			mouse_dev->buttons[field->usage - field->usage_minimum]
+			    = field->value;
+		} else if (
+		    mouse_dev->buttons[field->usage - field->usage_minimum] != 0
+		    && field->value == 0) {
+		       async_req_2_0(mouse_dev->console_phone,
+			   MEVENT_BUTTON, field->usage, 0);
+		       mouse_dev->buttons[field->usage - field->usage_minimum]
+			   = field->value;
+	       }
+		
+		field = usb_hid_report_get_sibling(
+		    hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
+		    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
+		    USB_HID_REPORT_TYPE_INPUT);
+	}
+	
+	usb_hid_report_path_free(path);
 
 	return true;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static int usb_mouse_create_function(usb_hid_dev_t *hid_dev)
+{
+	/* Create the function exposed under /dev/devices. */
+	usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
+	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 
+	    HID_MOUSE_FUN_NAME);
+	if (fun == NULL) {
+		usb_log_error("Could not create DDF function node.\n");
+		return ENOMEM;
+	}
+	
+	/*
+	 * Store the initialized HID device and HID ops
+	 * to the DDF function.
+	 */
+	fun->ops = &hid_dev->ops;
+	fun->driver_data = hid_dev;   // TODO: maybe change to hid_dev->data
+
+	int rc = ddf_fun_bind(fun);
+	if (rc != EOK) {
+		usb_log_error("Could not bind DDF function: %s.\n",
+		    str_error(rc));
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	usb_log_debug("Adding DDF function to class %s...\n", 
+	    HID_MOUSE_CLASS_NAME);
+	rc = ddf_fun_add_to_class(fun, HID_MOUSE_CLASS_NAME);
+	if (rc != EOK) {
+		usb_log_error(
+		    "Could not add DDF function to class %s: %s.\n",
+		    HID_MOUSE_CLASS_NAME, str_error(rc));
+		ddf_fun_destroy(fun);
+		return rc;
+	}
+	
+	return EOK;
 }
 
@@ -248,4 +359,13 @@
 	}
 	
+	mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT, 
+	    sizeof(int32_t));
+	
+	if (mouse_dev->buttons == NULL) {
+		usb_log_fatal("No memory!\n");
+		free(mouse_dev);
+		return ENOMEM;
+	}
+	
 	// save the Mouse device structure into the HID device structure
 	hid_dev->data = mouse_dev;
@@ -255,6 +375,12 @@
 	
 	// TODO: how to know if the device supports the request???
-	usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 
-	    hid_dev->usb_dev->interface_no, IDLE_RATE);
+//	usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 
+//	    hid_dev->usb_dev->interface_no, IDLE_RATE);
+	
+	int rc = usb_mouse_create_function(hid_dev);
+	if (rc != EOK) {
+		usb_mouse_free(&mouse_dev);
+		return rc;
+	}
 	
 	return EOK;
@@ -280,7 +406,6 @@
 		return false;
 	}
-	usb_mouse_t *mouse_dev = (usb_mouse_t *)hid_dev->data;
-	
-	return usb_mouse_process_boot_report(mouse_dev, buffer, buffer_size);
+	
+	return usb_mouse_process_boot_report(hid_dev, buffer, buffer_size);
 }
 
@@ -296,5 +421,5 @@
 int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
 {
-	int rc = usb_hid_parse_report_descriptor(hid_dev->parser, 
+	int rc = usb_hid_parse_report_descriptor(hid_dev->report, 
 	    USB_MOUSE_BOOT_REPORT_DESCRIPTOR, 
 	    USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
Index: uspace/drv/usbhid/mouse/mousedev.h
===================================================================
--- uspace/drv/usbhid/mouse/mousedev.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/mouse/mousedev.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -49,4 +49,6 @@
 	/** IPC phone to console (consumer). */
 	int console_phone;
+	
+	int32_t *buttons;
 } usb_mouse_t;
 
Index: uspace/drv/usbhid/subdrivers.c
===================================================================
--- uspace/drv/usbhid/subdrivers.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/subdrivers.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -55,6 +55,6 @@
 		USB_HID_PATH_COMPARE_END 
 		| USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
-		0,
-		0,
+		-1,
+		-1,
 		{
 			.init = usb_kbd_init,
@@ -73,11 +73,11 @@
 		0xc30e,
 		{
-			.init = NULL,
-			.deinit = NULL,
+			.init = usb_lgtch_init,
+			.deinit = usb_lgtch_deinit,
 			.poll = usb_lgtch_polling_callback,
 			.poll_end = NULL
 		}
 	},
-	{NULL, -1, 0, 0, 0, {NULL, NULL, NULL, NULL}}
+	{NULL, -1, 0, -1, -1, {NULL, NULL, NULL, NULL}}
 };
 
Index: uspace/drv/usbhid/subdrivers.h
===================================================================
--- uspace/drv/usbhid/subdrivers.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/subdrivers.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -56,6 +56,6 @@
 	int report_id;
 	int compare;
-	uint16_t vendor_id;
-	uint16_t product_id;
+	int vendor_id;
+	int product_id;
 	usb_hid_subdriver_t subdriver;
 } usb_hid_subdriver_mapping_t;
Index: uspace/drv/usbhid/usbhid.c
===================================================================
--- uspace/drv/usbhid/usbhid.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/usbhid.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -136,5 +136,5 @@
 	
 	// set the init callback
-	hid_dev->subdrivers[0].init = NULL;
+	hid_dev->subdrivers[0].init = usb_generic_hid_init;
 	
 	// set the polling callback
@@ -158,5 +158,11 @@
     const usb_hid_subdriver_mapping_t *mapping)
 {
-	return false;
+	assert(hid_dev != NULL);
+	assert(hid_dev->usb_dev != NULL);
+	
+	return (hid_dev->usb_dev->descriptors.device.vendor_id 
+	    == mapping->vendor_id
+	    && hid_dev->usb_dev->descriptors.device.product_id 
+	    == mapping->product_id);
 }
 
@@ -192,8 +198,8 @@
 	}
 	
-	assert(hid_dev->parser != NULL);
+	assert(hid_dev->report != NULL);
 	
 	usb_log_debug("Compare flags: %d\n", mapping->compare);
-	size_t size = usb_hid_report_input_length(hid_dev->parser, usage_path, 
+	size_t size = usb_hid_report_input_length(hid_dev->report, usage_path, 
 	    mapping->compare);
 	usb_log_debug("Size of the input report: %zuB\n", size);
@@ -251,13 +257,13 @@
 	while (count < USB_HID_MAX_SUBDRIVERS &&
 	    (mapping->usage_path != NULL
-	    || mapping->vendor_id != 0 || mapping->product_id != 0)) {
+	    || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
 		// check the vendor & product ID
-		if (mapping->vendor_id != 0 && mapping->product_id == 0) {
-			usb_log_warning("Missing Product ID for Vendor ID %u\n",
+		if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
+			usb_log_warning("Missing Product ID for Vendor ID %d\n",
 			    mapping->vendor_id);
 			return EINVAL;
 		}
-		if (mapping->product_id != 0 && mapping->vendor_id == 0) {
-			usb_log_warning("Missing Vendor ID for Product ID %u\n",
+		if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
+			usb_log_warning("Missing Vendor ID for Product ID %d\n",
 			    mapping->product_id);
 			return EINVAL;
@@ -267,6 +273,6 @@
 		matched = false;
 		
-		if (mapping->vendor_id != 0) {
-			assert(mapping->product_id != 0);
+		if (mapping->vendor_id >= 0) {
+			assert(mapping->product_id >= 0);
 			usb_log_debug("Comparing device against vendor ID %u"
 			    " and product ID %u.\n", mapping->vendor_id,
@@ -341,7 +347,7 @@
 	}
 	
-	hid_dev->parser = (usb_hid_report_parser_t *)(malloc(sizeof(
-	    usb_hid_report_parser_t)));
-	if (hid_dev->parser == NULL) {
+	hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
+	    usb_hid_report_t)));
+	if (hid_dev->report == NULL) {
 		usb_log_fatal("No memory!\n");
 		free(hid_dev);
@@ -382,16 +388,8 @@
 		return rc;
 	}
-	
-	/* Initialize the report parser. */
-	rc = usb_hid_parser_init(hid_dev->parser);
-	if (rc != EOK) {
-		usb_log_error("Failed to initialize report parser.\n");
-		//usb_hid_free(&hid_dev);
-		return rc;
-	}
-	
+		
 	/* Get the report descriptor and parse it. */
 	rc = usb_hid_process_report_descriptor(hid_dev->usb_dev, 
-	    hid_dev->parser);
+	    hid_dev->report);
 	
 	bool fallback = false;
@@ -492,4 +490,30 @@
 	usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
 	
+	int allocated = (hid_dev->input_report != NULL);
+	
+	if (!allocated
+	    || hid_dev->input_report_size < buffer_size) {
+		uint8_t *input_old = hid_dev->input_report;
+		uint8_t *input_new = (uint8_t *)malloc(buffer_size);
+		
+		if (input_new == NULL) {
+			usb_log_error("Failed to allocate space for input "
+			    "buffer. This event may not be reported\n");
+			memset(hid_dev->input_report, 0, 
+			    hid_dev->input_report_size);
+		} else {
+			memcpy(input_new, input_old, 
+			    hid_dev->input_report_size);
+			hid_dev->input_report = input_new;
+			if (allocated) {
+				free(input_old);
+			}
+		}
+	}
+	
+	/*! @todo This should probably be atomic. */
+	memcpy(hid_dev->input_report, buffer, buffer_size);
+	hid_dev->input_report_size = buffer_size;
+	
 	bool cont = false;
 	
@@ -530,36 +554,36 @@
 /*----------------------------------------------------------------------------*/
 
-const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
-{
-	switch (hid_dev->poll_pipe_index) {
-	case USB_HID_KBD_POLL_EP_NO:
-		return HID_KBD_FUN_NAME;
-		break;
-	case USB_HID_MOUSE_POLL_EP_NO:
-		return HID_MOUSE_FUN_NAME;
-		break;
-	default:
-		return HID_GENERIC_FUN_NAME;
-	}
-}
-
-/*----------------------------------------------------------------------------*/
-
-const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
-{
-	// this means that only boot protocol keyboards will be connected
-	// to the console; there is probably no better way to do this
-	
-	switch (hid_dev->poll_pipe_index) {
-	case USB_HID_KBD_POLL_EP_NO:
-		return HID_KBD_CLASS_NAME;
-		break;
-	case USB_HID_MOUSE_POLL_EP_NO:
-		return HID_MOUSE_CLASS_NAME;
-		break;
-	default:
-		return HID_GENERIC_CLASS_NAME;
-	}
-}
+//const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
+//{
+//	switch (hid_dev->poll_pipe_index) {
+//	case USB_HID_KBD_POLL_EP_NO:
+//		return HID_KBD_FUN_NAME;
+//		break;
+//	case USB_HID_MOUSE_POLL_EP_NO:
+//		return HID_MOUSE_FUN_NAME;
+//		break;
+//	default:
+//		return HID_GENERIC_FUN_NAME;
+//	}
+//}
+
+/*----------------------------------------------------------------------------*/
+
+//const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
+//{
+//	// this means that only boot protocol keyboards will be connected
+//	// to the console; there is probably no better way to do this
+	
+//	switch (hid_dev->poll_pipe_index) {
+//	case USB_HID_KBD_POLL_EP_NO:
+//		return HID_KBD_CLASS_NAME;
+//		break;
+//	case USB_HID_MOUSE_POLL_EP_NO:
+//		return HID_MOUSE_CLASS_NAME;
+//		break;
+//	default:
+//		return HID_GENERIC_CLASS_NAME;
+//	}
+//}
 
 /*----------------------------------------------------------------------------*/
@@ -591,6 +615,6 @@
 
 	// destroy the parser
-	if ((*hid_dev)->parser != NULL) {
-		usb_hid_free_report_parser((*hid_dev)->parser);
+	if ((*hid_dev)->report != NULL) {
+		usb_hid_free_report((*hid_dev)->report);
 	}
 
Index: uspace/drv/usbhid/usbhid.h
===================================================================
--- uspace/drv/usbhid/usbhid.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhid/usbhid.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -91,5 +91,9 @@
 	
 	/** HID Report parser. */
-	usb_hid_report_parser_t *parser;
+	usb_hid_report_t *report;
+	
+	uint8_t *input_report;
+	
+	size_t input_report_size;
 	
 	/** Arbitrary data (e.g. a special structure for handling keyboard). */
@@ -120,7 +124,7 @@
      void *arg);
 
-const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev);
+//const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev);
 
-const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev);
+//const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev);
 
 void usb_hid_free(usb_hid_dev_t **hid_dev);
Index: uspace/drv/usbhub/main.c
===================================================================
--- uspace/drv/usbhub/main.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhub/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -55,9 +55,16 @@
 };
 
-
+/**
+ * usb hub driver operations
+ *
+ * The most important one is add_device, which is set to usb_hub_add_device.
+ */
 static usb_driver_ops_t usb_hub_driver_ops = {
 	.add_device = usb_hub_add_device
 };
 
+/**
+ * hub endpoints, excluding control endpoint
+ */
 static usb_endpoint_description_t *usb_hub_endpoints[] = {
 	&hub_status_change_endpoint_description,
@@ -65,4 +72,7 @@
 };
 
+/**
+ * static usb hub driver information
+ */
 static usb_driver_t usb_hub_driver = {
 	.name = NAME,
Index: uspace/drv/usbhub/ports.c
===================================================================
--- uspace/drv/usbhub/ports.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhub/ports.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -167,5 +167,4 @@
 
 	//close address
-	//if (hub->attached_devs[port].address != 0) {
 	if(hub->ports[port].attached_device.address >= 0){
 		/*uncomment this code to use it when DDF allows device removal
@@ -182,5 +181,4 @@
 		 */
 	} else {
-		// TODO: is this really reason to print a warning?
 		usb_log_warning("Device removed before being registered.\n");
 
Index: uspace/drv/usbhub/usbhub.c
===================================================================
--- uspace/drv/usbhub/usbhub.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbhub/usbhub.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -73,6 +73,4 @@
 
 
-/// \TODO malloc checking
-
 //*********************************************
 //
@@ -248,4 +246,7 @@
 	hub_info->ports = malloc(
 	    sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
+	if(!hub_info->ports){
+		return ENOMEM;
+	}
 	size_t port;
 	for (port = 0; port < hub_info->port_count + 1; ++port) {
@@ -255,13 +256,4 @@
 		usb_log_debug("is_power_switched\n");
 		
-		for (port = 1; port <= hub_info->port_count; ++port) {
-			usb_log_debug("powering port %d\n",port);
-			opResult = usb_hub_set_port_feature(hub_info->control_pipe,
-			    port, USB_HUB_FEATURE_PORT_POWER);
-			if (opResult != EOK) {
-				usb_log_error("cannot power on port %zu: %s.\n",
-				    port, str_error(opResult));
-			}
-		}
 		if(!has_individual_port_powering){
 			usb_log_debug("!has_individual_port_powering\n");
@@ -273,6 +265,17 @@
 			}
 		}
+
+		for (port = 1; port <= hub_info->port_count; ++port) {
+			usb_log_debug("Powering port %zu.\n",port);
+			opResult = usb_hub_set_port_feature(hub_info->control_pipe,
+			    port, USB_HUB_FEATURE_PORT_POWER);
+			if (opResult != EOK) {
+				usb_log_error("cannot power on port %zu: %s.\n",
+				    port, str_error(opResult));
+			}
+		}
+		
 	}else{
-		usb_log_debug("!is_power_switched\n");
+		usb_log_debug("!is_power_switched, not going to be powered\n");
 	}
 	usb_log_debug2("freeing data\n");
@@ -408,5 +411,5 @@
 static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
     usb_hub_status_t status) {
-	int opResult;
+	int opResult = EOK;
 	if (!usb_hub_is_status(status,USB_HUB_FEATURE_HUB_LOCAL_POWER)) {
 		//restart power on hub
@@ -428,11 +431,14 @@
 			}
 		}
-		opResult = usb_hub_clear_feature(hub_info->control_pipe,
-		    USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
+	}
+	if(opResult!=EOK){
+		return opResult;//no feature clearing
+	}
+	opResult = usb_hub_clear_feature(hub_info->control_pipe,
+	    USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
 		if (opResult != EOK) {
-			usb_log_error("cannnot clear hub power change flag: "
-			    "%d\n",
-			    opResult);
-		}
+		usb_log_error("cannnot clear hub power change flag: "
+		    "%d\n",
+		    opResult);
 	}
 	return opResult;
Index: uspace/drv/usbkbd/kbddev.c
===================================================================
--- uspace/drv/usbkbd/kbddev.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbkbd/kbddev.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -128,7 +128,10 @@
         0x15, 0x00,  //   Logical Minimum (0),
         0x25, 0x01,  //   Logical Maximum (1),
+	//0x85, 0x00,  //   Report ID,
+	//0xA4,	     //   Push
         0x81, 0x02,  //   Input (Data, Variable, Absolute),   ; Modifier byte
-	0x95, 0x01,  //   Report Count (1),
-        0x75, 0x08,  //   Report Size (8),
+	//0xB4,	     //   Pop
+        0x75, 0x08,  //   Report Size (1),
+        0x95, 0x01,  //   Report Count (8),       
         0x81, 0x01,  //   Input (Constant),                   ; Reserved byte
         0x95, 0x05,  //   Report Count (5),
@@ -268,5 +271,5 @@
 		return;
 	}
-	
+
 	unsigned i = 0;
 	
@@ -290,10 +293,10 @@
 	
 	usb_log_debug("Creating output report.\n");
-	
-	int rc = usb_hid_report_output_translate(kbd_dev->parser, 
-	    kbd_dev->led_path, 
-	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
-	    kbd_dev->output_buffer, 
-	    kbd_dev->output_size, kbd_dev->led_data, kbd_dev->led_output_size);
+
+	//usb_hid_report_output_set_data(kbd_dev->parser, kbd_dev->led_path, 
+	//                               USB_HID_PATH_COMPARE_END , kbd_dev->led_data, 
+	//                               kbd_dev->led_output_size);
+	int rc = usb_hid_report_output_translate(kbd_dev->parser, 0,
+	    kbd_dev->output_buffer, kbd_dev->output_size);
 	
 	if (rc != EOK) {
@@ -563,6 +566,6 @@
 	assert(kbd_dev != NULL);
 
-	usb_log_debug("Got keys from parser (report id: %u): %s\n", 
-	    report_id, usb_debug_str_buffer(key_codes, count, 0));
+	usb_log_debug("Got keys from parser (report id: %d): %s\n", report_id, 
+	    usb_debug_str_buffer(key_codes, count, 0));
 	
 	if (count != kbd_dev->key_count) {
@@ -614,10 +617,8 @@
 	usb_hid_report_path_t *path = usb_hid_report_path();
 	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
-	usb_hid_report_path_set_report_id(path, 0);
-	
-	int rc = usb_hid_parse_report(kbd_dev->parser, buffer,
-	    actual_size, path, 
-	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 
-	    callbacks, kbd_dev);
+
+	uint8_t report_id;
+	int rc = usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, &report_id);
+	usb_hid_descriptor_print (kbd_dev->parser);
 
 	usb_hid_report_path_free (path);
@@ -663,6 +664,6 @@
 	memset(kbd_dev, 0, sizeof(usb_kbd_t));
 	
-	kbd_dev->parser = (usb_hid_report_parser_t *)(malloc(sizeof(
-	    usb_hid_report_parser_t)));
+	kbd_dev->parser = (usb_hid_report_t *)(malloc(sizeof(
+	    usb_hid_report_t)));
 	if (kbd_dev->parser == NULL) {
 		usb_log_fatal("No memory!\n");
@@ -732,9 +733,9 @@
 	
 	/* Initialize the report parser. */
-	rc = usb_hid_parser_init(kbd_dev->parser);
-	if (rc != EOK) {
-		usb_log_error("Failed to initialize report parser.\n");
-		return rc;
-	}
+	//rc = usb_hid_parser_init(kbd_dev->parser);
+	//if (rc != EOK) {
+	//	usb_log_error("Failed to initialize report parser.\n");
+	//	return rc;
+	//}
 	
 	/* Get the report descriptor and parse it. */
@@ -771,6 +772,5 @@
 	
 	kbd_dev->key_count = usb_hid_report_input_length(
-	    kbd_dev->parser, path, 
-	    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
+	    kbd_dev->parser, path, USB_HID_PATH_COMPARE_END);
 	usb_hid_report_path_free (path);
 	
@@ -789,6 +789,6 @@
 	kbd_dev->output_size = 0;
 	kbd_dev->output_buffer = usb_hid_report_output(kbd_dev->parser, 
-	    &kbd_dev->output_size);
-	if (kbd_dev->output_buffer == NULL && kbd_dev->output_size != 0) {
+	    &kbd_dev->output_size, 0x00);
+	if (kbd_dev->output_buffer == NULL) {
 		usb_log_warning("Error creating output report buffer.\n");
 		free(kbd_dev->keys);
@@ -801,4 +801,5 @@
 	usb_hid_report_path_append_item(
 	    kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
+	usb_hid_report_path_set_report_id(kbd_dev->led_path, 0x00);
 	
 	kbd_dev->led_output_size = usb_hid_report_output_size(kbd_dev->parser, 
@@ -849,5 +850,5 @@
 	 * Set Idle rate
 	 */
-	usb_kbd_set_led(kbd_dev);
+	usb_kbd_set_led(kbd_dev);	
 	
 	usbhid_req_set_idle(&kbd_dev->usb_dev->ctrl_pipe, 
@@ -934,5 +935,5 @@
 	// destroy the parser
 	if ((*kbd_dev)->parser != NULL) {
-		usb_hid_free_report_parser((*kbd_dev)->parser);
+		usb_hid_free_report((*kbd_dev)->parser);
 	}
 	
Index: uspace/drv/usbkbd/kbddev.h
===================================================================
--- uspace/drv/usbkbd/kbddev.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/usbkbd/kbddev.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -106,5 +106,5 @@
 
 	/** HID Report parser. */
-	usb_hid_report_parser_t *parser;
+	usb_hid_report_t *parser;
 	
 	/** State of the structure (for checking before use). 
Index: uspace/drv/vhc/Makefile
===================================================================
--- uspace/drv/vhc/Makefile	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/Makefile	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -39,13 +39,13 @@
 
 SOURCES = \
+	hub/virthub.c \
 	hub/hub.c \
-	hub/virthub.c \
 	hub/virthubops.c \
 	conndev.c \
 	connhost.c \
-	devices.c \
-	hc.c \
-	hcd.c \
-	hub.c
+	devconn.c \
+	hub.c \
+	main.c \
+	transfer.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/drv/vhc/conn.h
===================================================================
--- uspace/drv/vhc/conn.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/conn.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -40,14 +40,8 @@
 #include <usb_iface.h>
 #include "vhcd.h"
-#include "devices.h"
-
-void connection_handler_host(sysarg_t);
 
 extern usbhc_iface_t vhc_iface;
 extern usb_iface_t vhc_usb_iface;
 extern usb_iface_t rh_usb_iface;
-
-void address_init(void);
-
 
 void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
Index: uspace/drv/vhc/conndev.c
===================================================================
--- uspace/drv/vhc/conndev.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/conndev.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Vojtech Horky
  * All rights reserved.
  *
@@ -31,46 +31,13 @@
  */
 /** @file
- * @brief Connection handling of calls from virtual device (implementation).
+ * Connection handling of calls from virtual device (implementation).
  */
 
 #include <assert.h>
 #include <errno.h>
-#include <usbvirt/hub.h>
+#include <ddf/driver.h>
+#include "conn.h"
 
-#include "conn.h"
-#include "hc.h"
-#include "hub.h"
-
-#define DEVICE_NAME_MAXLENGTH 32
-
-static int get_device_name(int phone, char *buffer, size_t len)
-{
-	ipc_call_t answer_data;
-	sysarg_t answer_rc;
-	aid_t req;
-	int rc;
-	
-	req = async_send_0(phone,
-	    IPC_M_USBVIRT_GET_NAME,
-	    &answer_data);
-	
-	rc = async_data_read_start(phone, buffer, len);
-	if (rc != EOK) {
-		async_wait_for(req, NULL);
-		return EINVAL;
-	}
-	
-	async_wait_for(req, &answer_rc);
-	rc = (int)answer_rc;
-	
-	if (IPC_GET_ARG1(answer_data) < len) {
-		len = IPC_GET_ARG1(answer_data);
-	} else {
-		len--;
-	}
-	buffer[len] = 0;
-	
-	return rc;
-}
+static fibril_local uintptr_t plugged_device_handle = 0;
 
 /** Default handler for IPC methods not handled by DDF.
@@ -83,22 +50,21 @@
     ipc_callid_t icallid, ipc_call_t *icall)
 {
+	vhc_data_t *vhc = fun->dev->driver_data;
 	sysarg_t method = IPC_GET_IMETHOD(*icall);
 
 	if (method == IPC_M_CONNECT_TO_ME) {
 		int callback = IPC_GET_ARG5(*icall);
-		virtdev_connection_t *dev
-		    = virtdev_add_device(callback, (sysarg_t)fibril_get_id());
-		if (!dev) {
-			async_answer_0(icallid, EEXISTS);
+		int rc = vhc_virtdev_plug(vhc, callback,
+		    &plugged_device_handle);
+		if (rc != EOK) {
+			async_answer_0(icallid, rc);
 			async_hangup(callback);
 			return;
 		}
+
 		async_answer_0(icallid, EOK);
 
-		char devname[DEVICE_NAME_MAXLENGTH + 1];
-		int rc = get_device_name(callback, devname, DEVICE_NAME_MAXLENGTH);
-
 		usb_log_info("New virtual device `%s' (id = %" PRIxn ").\n",
-		    rc == EOK ? devname : "<unknown>", dev->id);
+		    rc == EOK ? "XXX" : "<unknown>", plugged_device_handle);
 
 		return;
@@ -108,21 +74,19 @@
 }
 
-/** Callback for DDF when client disconnects.
+/** Callback when client disconnects.
  *
- * @param fun Device function the client was connected to.
+ * Used to unplug virtual USB device.
+ *
+ * @param fun
  */
 void on_client_close(ddf_fun_t *fun)
 {
-	/*
-	 * Maybe a virtual device is being unplugged.
-	 */
-	virtdev_connection_t *dev = virtdev_find((sysarg_t)fibril_get_id());
-	if (dev == NULL) {
-		return;
+	vhc_data_t *vhc = fun->dev->driver_data;
+
+	if (plugged_device_handle != 0) {
+		usb_log_info("Virtual device disconnected (id = %" PRIxn ").\n",
+		    plugged_device_handle);
+		vhc_virtdev_unplug(vhc, plugged_device_handle);
 	}
-
-	usb_log_info("Virtual device disconnected (id = %" PRIxn ").\n",
-	    dev->id);
-	virtdev_destroy_device(dev);
 }
 
Index: uspace/drv/vhc/connhost.c
===================================================================
--- uspace/drv/vhc/connhost.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/connhost.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Vojtech Horky
  * All rights reserved.
  *
@@ -31,5 +31,5 @@
  */
 /** @file
- * @brief Connection handling of calls from host (implementation).
+ * Host controller interface implementation.
  */
 #include <assert.h>
@@ -38,339 +38,405 @@
 #include <usb/addrkeep.h>
 #include <usb/ddfiface.h>
-
+#include <usb/debug.h>
+#include <usbhc_iface.h>
 #include "vhcd.h"
-#include "conn.h"
-#include "hc.h"
-
-
-typedef struct {
-	usb_direction_t direction;
-	usbhc_iface_transfer_out_callback_t out_callback;
-	usbhc_iface_transfer_in_callback_t in_callback;
-	ddf_fun_t *fun;
-	size_t reported_size;
-	void *arg;
-} transfer_info_t;
-
-typedef struct {
-	usb_direction_t direction;
-	usb_target_t target;
-	usbhc_iface_transfer_out_callback_t out_callback;
-	usbhc_iface_transfer_in_callback_t in_callback;
-	ddf_fun_t *fun;
-	void *arg;
-	void *data_buffer;
-	size_t data_buffer_size;
-} control_transfer_info_t;
-
-static void universal_callback(void *buffer, size_t size,
-    int outcome, void *arg)
-{
-	transfer_info_t *transfer = (transfer_info_t *) arg;
-
-	if (transfer->reported_size != (size_t) -1) {
-		size = transfer->reported_size;
-	}
-
-	switch (transfer->direction) {
-		case USB_DIRECTION_IN:
-			transfer->in_callback(transfer->fun,
-			    outcome, size,
-			    transfer->arg);
-			break;
-		case USB_DIRECTION_OUT:
-			transfer->out_callback(transfer->fun,
-			    outcome,
-			    transfer->arg);
-			break;
-		default:
-			assert(false && "unreachable");
-			break;
-	}
-
-	free(transfer);
-}
-
-static transfer_info_t *create_transfer_info(ddf_fun_t *fun,
-    usb_direction_t direction, void *arg)
-{
-	transfer_info_t *transfer = malloc(sizeof(transfer_info_t));
-
-	transfer->direction = direction;
-	transfer->in_callback = NULL;
-	transfer->out_callback = NULL;
-	transfer->arg = arg;
-	transfer->fun = fun;
-	transfer->reported_size = (size_t) -1;
-
-	return transfer;
-}
-
-static void control_abort_prematurely(control_transfer_info_t *transfer,
-    size_t size, int outcome)
-{
-	switch (transfer->direction) {
-		case USB_DIRECTION_IN:
-			transfer->in_callback(transfer->fun,
-			    outcome, size,
-			    transfer->arg);
-			break;
-		case USB_DIRECTION_OUT:
-			transfer->out_callback(transfer->fun,
-			    outcome,
-			    transfer->arg);
-			break;
-		default:
-			assert(false && "unreachable");
-			break;
-	}
-}
-
-static void control_callback_two(void *buffer, size_t size,
-    int outcome, void *arg)
-{
-	control_transfer_info_t *ctrl_transfer = (control_transfer_info_t *) arg;
-
-	if (outcome != EOK) {
-		control_abort_prematurely(ctrl_transfer, outcome, size);
-		free(ctrl_transfer);
-		return;
-	}
-
-	transfer_info_t *transfer  = create_transfer_info(ctrl_transfer->fun,
-	    ctrl_transfer->direction, ctrl_transfer->arg);
-	transfer->out_callback = ctrl_transfer->out_callback;
-	transfer->in_callback = ctrl_transfer->in_callback;
-	transfer->reported_size = size;
-
-	switch (ctrl_transfer->direction) {
-		case USB_DIRECTION_IN:
-			hc_add_transaction_to_device(false, ctrl_transfer->target,
-			    USB_TRANSFER_CONTROL,
-			    NULL, 0,
-			    universal_callback, transfer);
-			break;
-		case USB_DIRECTION_OUT:
-			hc_add_transaction_from_device(ctrl_transfer->target,
-			    USB_TRANSFER_CONTROL,
-			    NULL, 0,
-			    universal_callback, transfer);
-			break;
-		default:
-			assert(false && "unreachable");
-			break;
-	}
-
-	free(ctrl_transfer);
-}
-
-static void control_callback_one(void *buffer, size_t size,
-    int outcome, void *arg)
-{
-	control_transfer_info_t *transfer = (control_transfer_info_t *) arg;
-
-	if (outcome != EOK) {
-		control_abort_prematurely(transfer, outcome, size);
-		free(transfer);
-		return;
-	}
-
-	switch (transfer->direction) {
-		case USB_DIRECTION_IN:
-			hc_add_transaction_from_device(transfer->target,
-			    USB_TRANSFER_CONTROL,
-			    transfer->data_buffer, transfer->data_buffer_size,
-			    control_callback_two, transfer);
-			break;
-		case USB_DIRECTION_OUT:
-			hc_add_transaction_to_device(false, transfer->target,
-			    USB_TRANSFER_CONTROL,
-			    transfer->data_buffer, transfer->data_buffer_size,
-			    control_callback_two, transfer);
-			break;
-		default:
-			assert(false && "unreachable");
-			break;
-	}
-}
-
-static control_transfer_info_t *create_control_transfer_info(ddf_fun_t *fun,
-    usb_direction_t direction, usb_target_t target,
-    void *data_buffer, size_t data_buffer_size,
-    void *arg)
-{
-	control_transfer_info_t *transfer
-	    = malloc(sizeof(control_transfer_info_t));
-
-	transfer->direction = direction;
-	transfer->target = target;
-	transfer->in_callback = NULL;
-	transfer->out_callback = NULL;
-	transfer->arg = arg;
-	transfer->fun = fun;
-	transfer->data_buffer = data_buffer;
-	transfer->data_buffer_size = data_buffer_size;
-
-	return transfer;
-}
-
-static int enqueue_transfer_out(ddf_fun_t *fun,
-    usb_target_t target, usb_transfer_type_t transfer_type,
-    void *buffer, size_t size,
-    usbhc_iface_transfer_out_callback_t callback, void *arg)
-{
-	usb_log_debug2("Transfer OUT [%d.%d (%s); %zu].\n",
-	    target.address, target.endpoint,
-	    usb_str_transfer_type(transfer_type),
-	    size);
-
-	transfer_info_t *transfer
-	    = create_transfer_info(fun, USB_DIRECTION_OUT, arg);
-	transfer->out_callback = callback;
-
-	hc_add_transaction_to_device(false, target, transfer_type, buffer, size,
-	    universal_callback, transfer);
-
-	return EOK;
-}
-
-static int enqueue_transfer_in(ddf_fun_t *fun,
-    usb_target_t target, usb_transfer_type_t transfer_type,
-    void *buffer, size_t size,
-    usbhc_iface_transfer_in_callback_t callback, void *arg)
-{
-	usb_log_debug2("Transfer IN [%d.%d (%s); %zu].\n",
-	    target.address, target.endpoint,
-	    usb_str_transfer_type(transfer_type),
-	    size);
-
-	transfer_info_t *transfer
-	    = create_transfer_info(fun, USB_DIRECTION_IN, arg);
-	transfer->in_callback = callback;
-
-	hc_add_transaction_from_device(target, transfer_type, buffer, size,
-	    universal_callback, transfer);
-
-	return EOK;
-}
-
-
+
+#define GET_VHC_DATA(fun) \
+	((vhc_data_t *)fun->dev->driver_data)
+#define VHC_DATA(vhc, fun) \
+	vhc_data_t *vhc = GET_VHC_DATA(fun); assert(vhc->magic == 0xdeadbeef)
+
+#define UNSUPPORTED(methodname) \
+	usb_log_warning("Unsupported interface method `%s()' in %s:%d.\n", \
+	    methodname, __FILE__, __LINE__)
+
+/** Found free USB address.
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] speed Speed of the device that will get this address.
+ * @param[out] address Non-null pointer where to store the free address.
+ * @return Error code.
+ */
+static int request_address(ddf_fun_t *fun, usb_speed_t speed,
+    usb_address_t *address)
+{
+	VHC_DATA(vhc, fun);
+
+	usb_address_t addr = device_keeper_get_free_address(&vhc->dev_keeper,
+	    USB_SPEED_HIGH);
+	if (addr < 0) {
+		return addr;
+	}
+
+	if (address != NULL) {
+		*address = addr;
+	}
+
+	return EOK;
+}
+
+/** Bind USB address with device devman handle.
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] address USB address of the device.
+ * @param[in] handle Devman handle of the device.
+ * @return Error code.
+ */
+static int bind_address(ddf_fun_t *fun,
+    usb_address_t address, devman_handle_t handle)
+{
+	VHC_DATA(vhc, fun);
+	usb_log_debug("Binding handle %" PRIun " to address %d.\n",
+	    handle, address);
+	usb_device_keeper_bind(&vhc->dev_keeper, address, handle);
+
+	return EOK;
+}
+
+/** Release previously requested address.
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] address USB address to be released.
+ * @return Error code.
+ */
+static int release_address(ddf_fun_t *fun, usb_address_t address)
+{
+	VHC_DATA(vhc, fun);
+	usb_log_debug("Releasing address %d...\n", address);
+	usb_device_keeper_release(&vhc->dev_keeper, address);
+
+	return ENOTSUP;
+}
+
+/** Register endpoint for bandwidth reservation.
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] address USB address of the device.
+ * @param[in] speed Endpoint speed (invalid means to use device one).
+ * @param[in] endpoint Endpoint number.
+ * @param[in] transfer_type USB transfer type.
+ * @param[in] direction Endpoint data direction.
+ * @param[in] max_packet_size Max packet size of the endpoint.
+ * @param[in] interval Polling interval.
+ * @return Error code.
+ */
+static int register_endpoint(ddf_fun_t *fun,
+    usb_address_t address, usb_speed_t speed, usb_endpoint_t endpoint,
+    usb_transfer_type_t transfer_type, usb_direction_t direction,
+    size_t max_packet_size, unsigned int interval)
+{
+	VHC_DATA(vhc, fun);
+
+	endpoint_t *ep = malloc(sizeof(endpoint_t));
+	if (ep == NULL) {
+		return ENOMEM;
+	}
+
+	int rc = endpoint_init(ep, address, endpoint, direction, transfer_type,
+	    USB_SPEED_FULL, 1);
+	if (rc != EOK) {
+		free(ep);
+		return rc;
+	}
+
+	rc = usb_endpoint_manager_register_ep(&vhc->ep_manager, ep, 1);
+	if (rc != EOK) {
+		endpoint_destroy(ep);
+		return rc;
+	}
+
+	return EOK;
+}
+
+/** Unregister endpoint (free some bandwidth reservation).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] address USB address of the device.
+ * @param[in] endpoint Endpoint number.
+ * @param[in] direction Endpoint data direction.
+ * @return Error code.
+ */
+static int unregister_endpoint(ddf_fun_t *fun, usb_address_t address,
+    usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	VHC_DATA(vhc, fun);
+
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&vhc->ep_manager,
+	    address, endpoint, direction, NULL);
+	if (ep == NULL) {
+		return ENOENT;
+	}
+
+	int rc = usb_endpoint_manager_unregister_ep(&vhc->ep_manager,
+	    address, endpoint, direction);
+
+	return rc;
+}
+
+/** Schedule interrupt out transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
+ *	by the caller).
+ * @param[in] size Size of the @p data buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
 static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
     void *data, size_t size,
     usbhc_iface_transfer_out_callback_t callback, void *arg)
 {
-	return enqueue_transfer_out(fun, target, USB_TRANSFER_INTERRUPT,
-	    data, size,
-	    callback, arg);
-}
-
+	VHC_DATA(vhc, fun);
+
+	vhc_transfer_t *transfer = vhc_transfer_create(target.address,
+	    target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_INTERRUPT,
+	    fun, arg);
+	if (transfer == NULL) {
+		return ENOMEM;
+	}
+
+	transfer->data_buffer = data;
+	transfer->data_buffer_size = size;
+	transfer->callback_out = callback;
+
+	int rc = vhc_virtdev_add_transfer(vhc, transfer);
+	if (rc != EOK) {
+		free(transfer);
+		return rc;
+	}
+
+	return EOK;
+}
+
+/** Schedule interrupt in transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] data Buffer where to store the data (in USB endianess,
+ *	allocated and deallocated by the caller).
+ * @param[in] size Size of the @p data buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
 static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
     void *data, size_t size,
     usbhc_iface_transfer_in_callback_t callback, void *arg)
 {
-	return enqueue_transfer_in(fun, target, USB_TRANSFER_INTERRUPT,
-	    data, size,
-	    callback, arg);
-}
-
+	VHC_DATA(vhc, fun);
+
+	vhc_transfer_t *transfer = vhc_transfer_create(target.address,
+	    target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_INTERRUPT,
+	    fun, arg);
+	if (transfer == NULL) {
+		return ENOMEM;
+	}
+
+	transfer->data_buffer = data;
+	transfer->data_buffer_size = size;
+	transfer->callback_in = callback;
+
+	int rc = vhc_virtdev_add_transfer(vhc, transfer);
+	if (rc != EOK) {
+		free(transfer);
+		return rc;
+	}
+
+	return EOK;
+}
+
+/** Schedule bulk out transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
+ *	by the caller).
+ * @param[in] size Size of the @p data buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
+static int bulk_out(ddf_fun_t *fun, usb_target_t target,
+    void *data, size_t size,
+    usbhc_iface_transfer_out_callback_t callback, void *arg)
+{
+	UNSUPPORTED("bulk_out");
+
+	return ENOTSUP;
+}
+
+/** Schedule bulk in transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] data Buffer where to store the data (in USB endianess,
+ *	allocated and deallocated by the caller).
+ * @param[in] size Size of the @p data buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
+static int bulk_in(ddf_fun_t *fun, usb_target_t target,
+    void *data, size_t size,
+    usbhc_iface_transfer_in_callback_t callback, void *arg)
+{
+	UNSUPPORTED("bulk_in");
+
+	return ENOTSUP;
+}
+
+/** Schedule control write transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
+ *	and deallocated by the caller).
+ * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
+ * @param[in] data_buffer Data buffer (in USB endianess, allocated and
+ *	deallocated by the caller).
+ * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
 static int control_write(ddf_fun_t *fun, usb_target_t target,
     void *setup_packet, size_t setup_packet_size,
-    void *data, size_t data_size,
+    void *data_buffer, size_t data_buffer_size,
     usbhc_iface_transfer_out_callback_t callback, void *arg)
 {
-	control_transfer_info_t *transfer
-	    = create_control_transfer_info(fun, USB_DIRECTION_OUT, target,
-	    data, data_size, arg);
-	transfer->out_callback = callback;
-
-	hc_add_transaction_to_device(true, target, USB_TRANSFER_CONTROL,
-	    setup_packet, setup_packet_size,
-	    control_callback_one, transfer);
-
-	return EOK;
-}
-
+	VHC_DATA(vhc, fun);
+
+	vhc_transfer_t *transfer = vhc_transfer_create(target.address,
+	    target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_CONTROL,
+	    fun, arg);
+	if (transfer == NULL) {
+		return ENOMEM;
+	}
+
+	transfer->setup_buffer = setup_packet;
+	transfer->setup_buffer_size = setup_packet_size;
+	transfer->data_buffer = data_buffer;
+	transfer->data_buffer_size = data_buffer_size;
+	transfer->callback_out = callback;
+
+	int rc = vhc_virtdev_add_transfer(vhc, transfer);
+	if (rc != EOK) {
+		free(transfer);
+		return rc;
+	}
+
+	return EOK;
+}
+
+/** Schedule control read transfer.
+ *
+ * The callback is supposed to be called once the transfer (on the wire) is
+ * complete regardless of the outcome.
+ * However, the callback could be called only when this function returns
+ * with success status (i.e. returns EOK).
+ *
+ * @param[in] fun Device function the action was invoked on.
+ * @param[in] target Target pipe (address and endpoint number) specification.
+ * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
+ *	and deallocated by the caller).
+ * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
+ * @param[in] data_buffer Buffer where to store the data (in USB endianess,
+ *	allocated and deallocated by the caller).
+ * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
+ * @param[in] callback Callback to be issued once the transfer is complete.
+ * @param[in] arg Pass-through argument to the callback.
+ * @return Error code.
+ */
 static int control_read(ddf_fun_t *fun, usb_target_t target,
     void *setup_packet, size_t setup_packet_size,
-    void *data, size_t data_size,
+    void *data_buffer, size_t data_buffer_size,
     usbhc_iface_transfer_in_callback_t callback, void *arg)
 {
-	control_transfer_info_t *transfer
-	    = create_control_transfer_info(fun, USB_DIRECTION_IN, target,
-	    data, data_size, arg);
-	transfer->in_callback = callback;
-
-	hc_add_transaction_to_device(true, target, USB_TRANSFER_CONTROL,
-	    setup_packet, setup_packet_size,
-	    control_callback_one, transfer);
-
-	return EOK;
-}
-
-static usb_address_keeping_t addresses;
+	VHC_DATA(vhc, fun);
+
+	vhc_transfer_t *transfer = vhc_transfer_create(target.address,
+	    target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_CONTROL,
+	    fun, arg);
+	if (transfer == NULL) {
+		return ENOMEM;
+	}
+
+	transfer->setup_buffer = setup_packet;
+	transfer->setup_buffer_size = setup_packet_size;
+	transfer->data_buffer = data_buffer;
+	transfer->data_buffer_size = data_buffer_size;
+	transfer->callback_in = callback;
+
+	int rc = vhc_virtdev_add_transfer(vhc, transfer);
+	if (rc != EOK) {
+		free(transfer);
+		return rc;
+	}
+
+	return EOK;
+}
 
 static int tell_address(ddf_fun_t *fun, devman_handle_t handle,
     usb_address_t *address)
 {
-	usb_log_debug("tell_address(fun \"%s\", handle %zu)\n",
-	    fun->name, (size_t) fun->handle);
-	usb_address_t addr = usb_address_keeping_find(&addresses, handle);
+	UNSUPPORTED("tell_address");
+
+	return ENOTSUP;
+}
+
+static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
+    devman_handle_t *handle)
+{
+	VHC_DATA(vhc, root_hub_fun);
+
+	*handle = vhc->hc_fun->handle;
+
+	return EOK;
+}
+
+static int tell_address_rh(ddf_fun_t *root_hub_fun, devman_handle_t handle,
+    usb_address_t *address)
+{
+	VHC_DATA(vhc, root_hub_fun);
+
+	if (handle == 0) {
+		handle = root_hub_fun->handle;
+	}
+
+	usb_log_debug("tell_address_rh(handle=%" PRIun ")\n", handle);
+	usb_address_t addr = usb_device_keeper_find(&vhc->dev_keeper, handle);
 	if (addr < 0) {
 		return addr;
-	}
-
-	*address = addr;
-	return EOK;
-}
-
-static int request_address(ddf_fun_t *fun, usb_speed_t ignored,
-    usb_address_t *address)
-{
-	usb_address_t addr = usb_address_keeping_request(&addresses);
-	if (addr < 0) {
-		return (int)addr;
-	}
-
-	*address = addr;
-	return EOK;
-}
-
-static int release_address(ddf_fun_t *fun, usb_address_t address)
-{
-	return usb_address_keeping_release(&addresses, address);
-}
-
-static int bind_address(ddf_fun_t *fun, usb_address_t address,
-    devman_handle_t handle)
-{
-	usb_address_keeping_devman_bind(&addresses, address, handle);
-	return EOK;
-}
-
-static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
-    devman_handle_t *handle)
-{
-	ddf_fun_t *hc_fun = root_hub_fun->driver_data;
-	assert(hc_fun != NULL);
-
-	*handle = hc_fun->handle;
-
-	usb_log_debug("usb_iface_get_hc_handle_rh_impl returns %zu\n", *handle);
-
-	return EOK;
-}
-
-static int tell_address_rh(ddf_fun_t *root_hub_fun, devman_handle_t handle,
-    usb_address_t *address)
-{
-	ddf_fun_t *hc_fun = root_hub_fun->driver_data;
-	assert(hc_fun != NULL);
-
-	return tell_address(hc_fun, root_hub_fun->handle, address);
-}
-
-void address_init(void)
-{
-	usb_address_keeping_init(&addresses, 50);
+	} else {
+		*address = addr;
+		return EOK;
+	}
 }
 
@@ -380,6 +446,12 @@
 	.release_address = release_address,
 
+	.register_endpoint = register_endpoint,
+	.unregister_endpoint = unregister_endpoint,
+
 	.interrupt_out = interrupt_out,
 	.interrupt_in = interrupt_in,
+
+	.bulk_in = bulk_in,
+	.bulk_out = bulk_out,
 
 	.control_write = control_write,
Index: uspace/drv/vhc/devconn.c
===================================================================
--- uspace/drv/vhc/devconn.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/drv/vhc/devconn.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,86 @@
+#include <errno.h>
+#include "vhcd.h"
+#include "hub/virthub.h"
+
+
+static vhc_virtdev_t *vhc_virtdev_create()
+{
+	vhc_virtdev_t *dev = malloc(sizeof(vhc_virtdev_t));
+	if (dev == NULL) {
+		return NULL;
+	}
+	dev->address = 0;
+	dev->dev_phone = -1;
+	dev->dev_local = NULL;
+	dev->plugged = true;
+	link_initialize(&dev->link);
+	fibril_mutex_initialize(&dev->guard);
+	list_initialize(&dev->transfer_queue);
+
+	return dev;
+}
+
+static int vhc_virtdev_plug_generic(vhc_data_t *vhc,
+    int phone, usbvirt_device_t *virtdev,
+    uintptr_t *handle, bool connect)
+{
+	vhc_virtdev_t *dev = vhc_virtdev_create();
+	if (dev == NULL) {
+		return ENOMEM;
+	}
+
+	dev->dev_phone = phone;
+	dev->dev_local = virtdev;
+
+	fibril_mutex_lock(&vhc->guard);
+	list_append(&dev->link, &vhc->devices);
+	fibril_mutex_unlock(&vhc->guard);
+
+	fid_t fibril = fibril_create(vhc_transfer_queue_processor, dev);
+	if (fibril == 0) {
+		free(dev);
+		return ENOMEM;
+	}
+	fibril_add_ready(fibril);
+
+	if (handle != NULL) {
+		*handle = (uintptr_t) dev;
+	}
+
+	if (connect) {
+		// FIXME: check status
+		(void) virthub_connect_device(vhc->hub, dev);
+	}
+
+	return EOK;
+}
+
+int vhc_virtdev_plug(vhc_data_t *vhc, int phone, uintptr_t *handle)
+{
+	return vhc_virtdev_plug_generic(vhc, phone, NULL, handle, true);
+}
+
+int vhc_virtdev_plug_local(vhc_data_t *vhc, usbvirt_device_t *dev, uintptr_t *handle)
+{
+	return vhc_virtdev_plug_generic(vhc, -1, dev, handle, true);
+}
+
+int vhc_virtdev_plug_hub(vhc_data_t *vhc, usbvirt_device_t *dev, uintptr_t *handle)
+{
+	return vhc_virtdev_plug_generic(vhc, -1, dev, handle, false);
+}
+
+void vhc_virtdev_unplug(vhc_data_t *vhc, uintptr_t handle)
+{
+	vhc_virtdev_t *dev = (vhc_virtdev_t *) handle;
+
+	// FIXME: check status
+	(void) virthub_disconnect_device(vhc->hub, dev);
+
+	fibril_mutex_lock(&vhc->guard);
+	fibril_mutex_lock(&dev->guard);
+	dev->plugged = false;
+	list_remove(&dev->link);
+	fibril_mutex_unlock(&dev->guard);
+	fibril_mutex_unlock(&vhc->guard);
+}
Index: pace/drv/vhc/devices.c
===================================================================
--- uspace/drv/vhc/devices.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,235 +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 drvusbvhc
- * @{
- */
-/** @file
- * @brief Virtual device management (implementation).
- */
-
-#include <adt/list.h>
-#include <bool.h>
-#include <async.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-#include <str_error.h>
-
-#include <usbvirt/hub.h>
-
-#include "devices.h"
-#include "hub.h"
-#include "hub/virthub.h"
-#include "vhcd.h"
-
-#define list_foreach(pos, head) \
-	for (pos = (head)->next; pos != (head); \
-        	pos = pos->next)
-
-static LIST_INITIALIZE(devices);
-
-/** Create virtual device.
- *
- * @param phone Callback phone.
- * @param id Device id.
- * @return New device.
- * @retval NULL Out of memory.
- */
-virtdev_connection_t *virtdev_add_device(int phone, sysarg_t id)
-{
-	virtdev_connection_t *dev = (virtdev_connection_t *)
-	    malloc(sizeof(virtdev_connection_t));
-	if (dev == NULL) {
-		return NULL;
-	}
-
-	dev->phone = phone;
-	dev->id = id;
-	list_append(&dev->link, &devices);
-	
-	virthub_connect_device(&virtual_hub_device, dev);
-	
-	return dev;
-}
-
-/** Find virtual device by id.
- *
- * @param id Device id.
- * @return Device with given id.
- * @retval NULL No such device.
- */
-virtdev_connection_t *virtdev_find(sysarg_t id)
-{
-	link_t *pos;
-	list_foreach(pos, &devices) {
-		virtdev_connection_t *dev
-		    = list_get_instance(pos, virtdev_connection_t, link);
-		if (dev->id == id) {
-			return dev;
-		}
-	}
-
-	return NULL;
-}
-
-/** Destroy virtual device.
- */
-void virtdev_destroy_device(virtdev_connection_t *dev)
-{
-	virthub_disconnect_device(&virtual_hub_device, dev);
-	list_remove(&dev->link);
-	free(dev);
-}
-
-/** Send data to all connected devices.
- *
- * @param transaction Transaction to be sent over the bus.
- */
-int virtdev_send_to_all(transaction_t *transaction)
-{
-	/* For easier debugging. */
-	switch (transaction->type) {
-		case USBVIRT_TRANSACTION_SETUP:
-		case USBVIRT_TRANSACTION_OUT:
-			transaction->actual_len = transaction->len;
-			break;
-		case USBVIRT_TRANSACTION_IN:
-			transaction->actual_len = 0;
-			break;
-		default:
-			assert(false && "unreachable branch in switch()");
-	}
-	int outcome = EBADCHECKSUM;
-
-	link_t *pos;
-	list_foreach(pos, &devices) {
-		virtdev_connection_t *dev
-		    = list_get_instance(pos, virtdev_connection_t, link);
-		
-		if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
-			continue;
-		}
-		
-		ipc_call_t answer_data;
-		sysarg_t answer_rc;
-		aid_t req;
-		int rc = EOK;
-		int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
-		
-		switch (transaction->type) {
-			case USBVIRT_TRANSACTION_SETUP:
-				method = IPC_M_USBVIRT_TRANSACTION_SETUP;
-				break;
-			case USBVIRT_TRANSACTION_IN:
-				method = IPC_M_USBVIRT_TRANSACTION_IN;
-				break;
-			case USBVIRT_TRANSACTION_OUT:
-				method = IPC_M_USBVIRT_TRANSACTION_OUT;
-				break;
-		}
-		
-		req = async_send_3(dev->phone,
-		    method,
-		    transaction->target.address,
-		    transaction->target.endpoint,
-		    transaction->len,
-		    &answer_data);
-		
-		if (transaction->len > 0) {
-			if (transaction->type == USBVIRT_TRANSACTION_IN) {
-				rc = async_data_read_start(dev->phone,
-				    transaction->buffer, transaction->len);
-			} else {
-				rc = async_data_write_start(dev->phone,
-				    transaction->buffer, transaction->len);
-			}
-		}
-		
-		if (rc != EOK) {
-			async_wait_for(req, NULL);
-		} else {
-			async_wait_for(req, &answer_rc);
-			transaction->actual_len = IPC_GET_ARG1(answer_data);
-			rc = (int)answer_rc;
-		}
-
-		/*
-		 * If at least one device was able to accept this
-		 * transaction and process it, we can announce success.
-		 */
-		if (rc == EOK) {
-			outcome = EOK;
-		}
-	}
-	
-	/*
-	 * Send the data to the virtual hub as well
-	 * (if the address matches).
-	 */
-	if (virtual_hub_device.address == transaction->target.address) {
-		size_t tmp;
-		usb_log_debug2("Sending `%s' transaction to hub.\n",
-		    usbvirt_str_transaction_type(transaction->type));
-		switch (transaction->type) {
-			case USBVIRT_TRANSACTION_SETUP:
-				virtual_hub_device.transaction_setup(
-				    &virtual_hub_device,
-				    transaction->target.endpoint,
-				    transaction->buffer, transaction->len);
-				break;
-				
-			case USBVIRT_TRANSACTION_IN:
-				virtual_hub_device.transaction_in(
-				    &virtual_hub_device,
-				    transaction->target.endpoint,
-				    transaction->buffer, transaction->len,
-				    &tmp);
-				transaction->actual_len = tmp;
-				break;
-				
-			case USBVIRT_TRANSACTION_OUT:
-				virtual_hub_device.transaction_out(
-				    &virtual_hub_device,
-				    transaction->target.endpoint,
-				    transaction->buffer, transaction->len);
-				break;
-		}
-		outcome = EOK;
-	}
-	
-	/*
-	 * TODO: maybe screw some transactions to get more
-	 * real-life image.
-	 */
-	return outcome;
-}
-
-/**
- * @}
- */
Index: pace/drv/vhc/devices.h
===================================================================
--- uspace/drv/vhc/devices.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,61 +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 drvusbvhc
- * @{
- */ 
-/** @file
- * @brief Virtual device management.
- */
-#ifndef VHCD_DEVICES_H_
-#define VHCD_DEVICES_H_
-
-#include <adt/list.h>
-#include <usb/usb.h>
-
-#include "hc.h"
-
-/** Connected virtual device. */
-typedef struct {
-	/** Phone used when sending data to device. */
-	int phone;
-	/** Unique identification. */
-	sysarg_t id;
-	/** Linked-list handle. */
-	link_t link;
-} virtdev_connection_t;
-
-virtdev_connection_t *virtdev_add_device(int, sysarg_t);
-virtdev_connection_t *virtdev_find(sysarg_t);
-void virtdev_destroy_device(virtdev_connection_t *);
-int virtdev_send_to_all(transaction_t *);
-
-#endif
-/**
- * @}
- */
Index: pace/drv/vhc/hc.c
===================================================================
--- uspace/drv/vhc/hc.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,209 +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 drvusbvhc
- * @{
- */
-/** @file
- * @brief Virtual HC (implementation).
- */
-
-#include <adt/list.h>
-#include <bool.h>
-#include <async.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-#include <str_error.h>
-
-#include <usbvirt/hub.h>
-
-#include "vhcd.h"
-#include "hc.h"
-#include "devices.h"
-#include "hub.h"
-
-#define USLEEP_BASE (0 * 5 * 1000)
-
-#define USLEEP_VAR 50
-
-#define SHORTENING_VAR 15
-
-#define PROB_OUTCOME_BABBLE 5
-#define PROB_OUTCOME_CRCERROR 7
-
-#define PROB_TEST(var, new_value, prob, number) \
-	do { \
-		if (((number) % (prob)) == 0) { \
-			var = (new_value); \
-		} \
-	} while (0)
-
-static link_t transaction_list;
-
-#define TRANSACTION_FORMAT "T[%d.%d %s/%s (%d)]"
-#define TRANSACTION_PRINTF(t) \
-	(t).target.address, (t).target.endpoint, \
-	usb_str_transfer_type((t).transfer_type), \
-	usbvirt_str_transaction_type((t).type), \
-	(int)(t).len
-
-#define transaction_get_instance(lnk) \
-	list_get_instance(lnk, transaction_t, link)
-
-#define HUB_STATUS_MAX_LEN (HUB_PORT_COUNT + 64)
-
-static inline unsigned int pseudo_random(unsigned int *seed)
-{
-	*seed = ((*seed) * 873511) % 22348977 + 7;
-	return ((*seed) >> 8);
-}
-
-/** Call transaction callback.
- * Calling this callback informs the backend that transaction was processed.
- */
-static void process_transaction_with_outcome(transaction_t * transaction,
-    int outcome)
-{
-	usb_log_debug2("Transaction " TRANSACTION_FORMAT " done: %s.\n",
-	    TRANSACTION_PRINTF(*transaction),
-	    str_error(outcome));
-	
-	transaction->callback(transaction->buffer, transaction->actual_len,
-	    outcome, transaction->callback_arg);
-}
-
-/** Host controller manager main function.
- */
-static int hc_manager_fibril(void *arg)
-{
-	list_initialize(&transaction_list);
-	
-	static unsigned int seed = 4573;
-	
-	usb_log_info("Transaction processor ready.\n");
-	
-	while (true) {
-		async_usleep(USLEEP_BASE + (pseudo_random(&seed) % USLEEP_VAR));
-		
-		if (list_empty(&transaction_list)) {
-			continue;
-		}
-		
-		char ports[HUB_STATUS_MAX_LEN + 1];
-		virthub_get_status(&virtual_hub_device, ports, HUB_STATUS_MAX_LEN);
-		
-		link_t *first_transaction_link = transaction_list.next;
-		transaction_t *transaction
-		    = transaction_get_instance(first_transaction_link);
-		list_remove(first_transaction_link);
-		
-		usb_log_debug("Processing " TRANSACTION_FORMAT " [%s].\n",
-		    TRANSACTION_PRINTF(*transaction), ports);
-
-		int outcome;
-		outcome = virtdev_send_to_all(transaction);
-		
-		process_transaction_with_outcome(transaction, outcome);
-
-		free(transaction);
-	}
-
-	assert(false && "unreachable");
-	return EOK;
-}
-
-void hc_manager(void)
-{
-	fid_t fid = fibril_create(hc_manager_fibril, NULL);
-	if (fid == 0) {
-		usb_log_fatal("Failed to start HC manager fibril.\n");
-		return;
-	}
-	fibril_add_ready(fid);
-}
-
-/** Create new transaction
- */
-static transaction_t *transaction_create(usbvirt_transaction_type_t type,
-    usb_target_t target, usb_transfer_type_t transfer_type,
-    void * buffer, size_t len,
-    hc_transaction_done_callback_t callback, void * arg)
-{
-	transaction_t * transaction = malloc(sizeof(transaction_t));
-	
-	list_initialize(&transaction->link);
-	transaction->type = type;
-	transaction->transfer_type = transfer_type;
-	transaction->target = target;
-	transaction->buffer = buffer;
-	transaction->len = len;
-	transaction->actual_len = len;
-	transaction->callback = callback;
-	transaction->callback_arg = arg;
-
-	return transaction;
-}
-
-static void hc_add_transaction(transaction_t *transaction)
-{
-	usb_log_debug("Adding transaction " TRANSACTION_FORMAT ".\n",
-	    TRANSACTION_PRINTF(*transaction));
-	list_append(&transaction->link, &transaction_list);
-}
-
-/** Add transaction directioned towards the device.
- */
-void hc_add_transaction_to_device(bool setup, usb_target_t target,
-    usb_transfer_type_t transfer_type,
-    void * buffer, size_t len,
-    hc_transaction_done_callback_t callback, void * arg)
-{
-	transaction_t *transaction = transaction_create(
-	    setup ? USBVIRT_TRANSACTION_SETUP : USBVIRT_TRANSACTION_OUT,
-	    target, transfer_type,
-	    buffer, len, callback, arg);
-	hc_add_transaction(transaction);
-}
-
-/** Add transaction directioned from the device.
- */
-void hc_add_transaction_from_device(usb_target_t target,
-    usb_transfer_type_t transfer_type,
-    void * buffer, size_t len,
-    hc_transaction_done_callback_t callback, void * arg)
-{
-	transaction_t *transaction = transaction_create(USBVIRT_TRANSACTION_IN,
-	    target, transfer_type,
-	    buffer, len, callback, arg);
-	hc_add_transaction(transaction);
-}
-
-/**
- * @}
- */
Index: pace/drv/vhc/hc.h
===================================================================
--- uspace/drv/vhc/hc.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,91 +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 drvusbvhc
- * @{
- */
-/** @file
- * @brief Virtual HC.
- */
-#ifndef VHCD_HC_H_
-#define VHCD_HC_H_
-
-#include <usb/usb.h>
-#include <usbvirt/hub.h>
-
-/** Callback after transaction is sent to USB.
- *
- * @param buffer Transaction data buffer.
- * @param size Transaction data size.
- * @param outcome Transaction outcome.
- * @param arg Custom argument.
- */
-typedef void (*hc_transaction_done_callback_t)(void *buffer, size_t size,
-    int outcome, void *arg);
-
-/** Pending transaction details. */
-typedef struct {
-	/** Linked-list link. */
-	link_t link;
-	/** Transaction type. */
-	usbvirt_transaction_type_t type;
-	/** Transfer type. */
-	usb_transfer_type_t transfer_type;
-	/** Device address. */
-	usb_target_t target;
-	/** Direction of the transaction. */
-	usb_direction_t direction;
-	/** Transaction data buffer. */
-	void * buffer;
-	/** Transaction data length. */
-	size_t len;
-	/** Data length actually transfered. */
-	size_t actual_len;
-	/** Callback after transaction is done. */
-	hc_transaction_done_callback_t callback;
-	/** Argument to the callback. */
-	void * callback_arg;
-} transaction_t;
-
-void hc_manager(void);
-
-void hc_add_transaction_to_device(bool setup,
-    usb_target_t target, usb_transfer_type_t transfer_type,
-    void * buffer, size_t len,
-    hc_transaction_done_callback_t callback, void * arg);
-
-void hc_add_transaction_from_device(
-    usb_target_t target, usb_transfer_type_t transfer_type,
-    void * buffer, size_t len,
-    hc_transaction_done_callback_t callback, void * arg);
-
-
-#endif
-/**
- * @}
- */
Index: pace/drv/vhc/hcd.c
===================================================================
--- uspace/drv/vhc/hcd.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ 	(revision )
@@ -1,147 +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 drvusbvhc
- * @{
- */ 
-/** @file
- * @brief Virtual host controller driver.
- */
-
-#include <devmap.h>
-#include <async.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <sysinfo.h>
-#include <stdio.h>
-#include <errno.h>
-#include <str_error.h>
-#include <ddf/driver.h>
-
-#include <usb/usb.h>
-#include <usb/ddfiface.h>
-#include <usb_iface.h>
-#include "vhcd.h"
-#include "hc.h"
-#include "devices.h"
-#include "hub.h"
-#include "conn.h"
-
-static ddf_dev_ops_t vhc_ops = {
-	.interfaces[USBHC_DEV_IFACE] = &vhc_iface,
-	.interfaces[USB_DEV_IFACE] = &vhc_usb_iface,
-	.close = on_client_close,
-	.default_handler = default_connection_handler
-};
-
-static int vhc_add_device(ddf_dev_t *dev)
-{
-	static int vhc_count = 0;
-	int rc;
-
-	/*
-	 * Currently, we know how to simulate only single HC.
-	 */
-	if (vhc_count > 0) {
-		return ELIMIT;
-	}
-
-	/*
-	 * Create exposed function representing the host controller
-	 * itself.
-	 */
-	ddf_fun_t *hc = ddf_fun_create(dev, fun_exposed, "hc");
-	if (hc == NULL) {
-		usb_log_fatal("Failed to create device function.\n");
-		return ENOMEM;
-	}
-
-	hc->ops = &vhc_ops;
-
-	rc = ddf_fun_bind(hc);
-	if (rc != EOK) {
-		usb_log_fatal("Failed to bind HC function: %s.\n",
-		    str_error(rc));
-		return rc;
-	}
-
-	ddf_fun_add_to_class(hc, "usbhc");
-
-	/*
-	 * Initialize our hub and announce its presence.
-	 */
-	virtual_hub_device_init(hc);
-
-	usb_log_info("Virtual USB host controller ready (dev %zu, hc %zu).\n",
-	    (size_t) dev->handle, (size_t) hc->handle);
-
-	return EOK;
-}
-
-static driver_ops_t vhc_driver_ops = {
-	.add_device = vhc_add_device,
-};
-
-static driver_t vhc_driver = {
-	.name = NAME,
-	.driver_ops = &vhc_driver_ops
-};
-
-
-int main(int argc, char * argv[])
-{	
-	/*
-	 * Temporary workaround. Wait a little bit to be the last driver
-	 * in devman output.
-	 */
-	//sleep(5);
-
-	usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
-
-	printf(NAME ": virtual USB host controller driver.\n");
-
-	/*
-	 * Initialize address management.
-	 */
-	address_init();
-
-	/*
-	 * Run the transfer scheduler.
-	 */
-	hc_manager();
-
-	/*
-	 * We are also a driver within devman framework.
-	 */
-	return ddf_driver_main(&vhc_driver);
-}
-
-
-/**
- * @}
- */
Index: uspace/drv/vhc/hub.c
===================================================================
--- uspace/drv/vhc/hub.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -34,5 +34,4 @@
  */
 #include <usb/classes/classes.h>
-#include <usbvirt/hub.h>
 #include <usbvirt/device.h>
 #include <errno.h>
@@ -45,9 +44,14 @@
 
 #include "hub.h"
-#include "hub/virthub.h"
+//#include "hub/virthub.h"
 #include "vhcd.h"
 #include "conn.h"
 
-usbvirt_device_t virtual_hub_device;
+usbvirt_device_t virtual_hub_device = {
+	.name = "root hub",
+	.ops = &hub_ops,
+	.address = 0
+};
+
 static ddf_dev_ops_t rh_ops = {
 	.interfaces[USB_DEV_IFACE] = &rh_usb_iface,
Index: uspace/drv/vhc/hub.h
===================================================================
--- uspace/drv/vhc/hub.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -39,5 +39,4 @@
 #include <ddf/driver.h>
 
-#include "devices.h"
 #include "hub/hub.h"
 #include "hub/virthub.h"
Index: uspace/drv/vhc/hub/hub.c
===================================================================
--- uspace/drv/vhc/hub/hub.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub/hub.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -34,5 +34,4 @@
  */
 #include <usb/classes/classes.h>
-#include <usbvirt/hub.h>
 #include <usbvirt/device.h>
 #include <errno.h>
@@ -41,4 +40,5 @@
 #include <stdlib.h>
 #include <ddf/driver.h>
+#include <usb/debug.h>
 
 #include "hub.h"
@@ -96,5 +96,5 @@
  * @param index Port index (one based).
  */
-static void hub_init_port(hub_port_t *port, size_t index)
+static void hub_init_port(hub_port_t *port, hub_t *hub, size_t index)
 {
 	port->connected_device = NULL;
@@ -102,4 +102,5 @@
 	port->state = HUB_PORT_STATE_NOT_CONFIGURED;
 	port->status_change = 0;
+	port->hub = hub;
 }
 
@@ -112,7 +113,8 @@
 	size_t i;
 	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_init_port(&hub->ports[i], i + 1);
+		hub_init_port(&hub->ports[i], hub, i + 1);
 	}
 	hub->custom_data = NULL;
+	hub->signal_changes = true;
 	fibril_mutex_initialize(&hub->guard);
 }
@@ -229,4 +231,6 @@
 	}
 
+	usb_log_debug("Setting port %zu to state %d.\n", port_index, state);
+
 	switch (state) {
 		case HUB_PORT_STATE_POWERED_OFF:
@@ -236,8 +240,10 @@
 			break;
 		case HUB_PORT_STATE_RESUMING:
+			port->state = state;
 			set_port_state_delayed(hub, port_index,
 			    10, state, HUB_PORT_STATE_ENABLED);
 			break;
 		case HUB_PORT_STATE_RESETTING:
+			port->state = state;
 			set_port_state_delayed(hub, port_index,
 			    10, state, HUB_PORT_STATE_ENABLED);
@@ -415,5 +421,10 @@
 {
 	assert(port != NULL);
+	uint16_t old_value = port->status_change;
 	port->status_change |= change;
+	usb_log_debug("Changing status change on %zu: %04x => %04x\n",
+	    port->index,
+	    (unsigned int) old_value, (unsigned int) port->status_change);
+	port->hub->signal_changes = true;
 }
 
@@ -428,4 +439,5 @@
 	assert(port != NULL);
 	port->status_change &= (~change);
+	port->hub->signal_changes = true;
 }
 
Index: uspace/drv/vhc/hub/hub.h
===================================================================
--- uspace/drv/vhc/hub/hub.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub/hub.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -72,4 +72,6 @@
 } hub_status_change_t;
 
+typedef struct hub hub_t;
+
 /** Hub port information. */
 typedef struct {
@@ -82,8 +84,10 @@
 	/** Status change bitmap. */
 	uint16_t status_change;
+	/** Containing hub. */
+	hub_t *hub;
 } hub_port_t;
 
 /** Hub device type. */
-typedef struct {
+struct hub {
 	/** Hub ports. */
 	hub_port_t ports[HUB_PORT_COUNT];
@@ -92,5 +96,7 @@
 	/** Access guard to the whole hub. */
 	fibril_mutex_t guard;
-} hub_t;
+	/** Last value of status change bitmap. */
+	bool signal_changes;
+};
 
 void hub_init(hub_t *);
Index: uspace/drv/vhc/hub/virthub.c
===================================================================
--- uspace/drv/vhc/hub/virthub.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub/virthub.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -34,5 +34,4 @@
  */
 #include <usb/classes/classes.h>
-#include <usbvirt/hub.h>
 #include <usbvirt/device.h>
 #include <assert.h>
@@ -153,6 +152,4 @@
 	dev->ops = &hub_ops;
 	dev->descriptors = &descriptors;
-	dev->lib_debug_level = 0;
-	dev->lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL;
 
 	hub_t *hub = malloc(sizeof(hub_t));
@@ -164,13 +161,5 @@
 	dev->device_data = hub;
 
-	int rc;
-#ifdef STANDALONE_HUB
-	dev->name = "hub";
-	rc = usbvirt_connect(dev);
-#else
-	rc = usbvirt_connect_local(dev);
-#endif
-
-	return rc;
+	return EOK;
 }
 
@@ -181,5 +170,5 @@
  * @return Port device was connected to.
  */
-int virthub_connect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
+int virthub_connect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
 {
 	assert(dev != NULL);
@@ -201,5 +190,5 @@
  * @return Error code.
  */
-int virthub_disconnect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
+int virthub_disconnect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
 {
 	assert(dev != NULL);
@@ -212,5 +201,5 @@
 	hub_release(hub);
 
-	return ENOTSUP;
+	return EOK;
 }
 
@@ -221,5 +210,5 @@
  * @return Whether port is signalling to the device.
  */
-bool virthub_is_device_enabled(usbvirt_device_t *dev, virtdev_connection_t *conn)
+bool virthub_is_device_enabled(usbvirt_device_t *dev, vhc_virtdev_t *conn)
 {
 	assert(dev != NULL);
Index: uspace/drv/vhc/hub/virthub.h
===================================================================
--- uspace/drv/vhc/hub/virthub.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub/virthub.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -42,5 +42,5 @@
 #define virtdev_connection_t int
 #else
-#include "../devices.h"
+#include "../vhcd.h"
 #endif
 
@@ -80,7 +80,7 @@
 
 int virthub_init(usbvirt_device_t *);
-int virthub_connect_device(usbvirt_device_t *, virtdev_connection_t *);
-int virthub_disconnect_device(usbvirt_device_t *, virtdev_connection_t *);
-bool virthub_is_device_enabled(usbvirt_device_t *, virtdev_connection_t *);
+int virthub_connect_device(usbvirt_device_t *, vhc_virtdev_t *);
+int virthub_disconnect_device(usbvirt_device_t *, vhc_virtdev_t *);
+bool virthub_is_device_enabled(usbvirt_device_t *, vhc_virtdev_t *);
 void virthub_get_status(usbvirt_device_t *, char *, size_t);
 
Index: uspace/drv/vhc/hub/virthubops.c
===================================================================
--- uspace/drv/vhc/hub/virthubops.c	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/hub/virthubops.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -35,9 +35,10 @@
 #include <errno.h>
 #include <usb/classes/hub.h>
+#include <usbvirt/device.h>
 #include "virthub.h"
 #include "hub.h"
 
 /** Callback when device changes states. */
-static void on_state_change(struct usbvirt_device *dev,
+static void on_state_change(usbvirt_device_t *dev,
     usbvirt_device_state_t old_state, usbvirt_device_state_t new_state)
 {
@@ -61,24 +62,38 @@
 
 /** Callback for data request. */
-static int req_on_data(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *actual_size)
+static int req_on_status_change_pipe(usbvirt_device_t *dev,
+    usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
+    void *buffer, size_t buffer_size, size_t *actual_size)
 {
 	if (endpoint != HUB_STATUS_CHANGE_PIPE) {
-		return EINVAL;
+		return ESTALL;
+	}
+	if (tr_type != USB_TRANSFER_INTERRUPT) {
+		return ESTALL;
 	}
 	
-	hub_t *hub = (hub_t *)dev->device_data;
+	hub_t *hub = dev->device_data;
 
 	hub_acquire(hub);
 
+	if (!hub->signal_changes) {
+		hub_release(hub);
+
+		return ENAK;
+	}
+
+
 	uint8_t change_map = hub_get_status_change_bitmap(hub);
-		
+
 	uint8_t *b = (uint8_t *) buffer;
-	if (size > 0) {
+	if (buffer_size > 0) {
 		*b = change_map;
 		*actual_size = 1;
+	} else {
+		*actual_size = 0;
 	}
 	
+	hub->signal_changes = false;
+
 	hub_release(hub);
 
@@ -94,6 +109,6 @@
  */
 static int req_clear_hub_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	return ENOTSUP;
@@ -108,6 +123,6 @@
  */
 static int req_clear_port_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	int rc;
@@ -188,6 +203,6 @@
  */
 static int req_get_bus_state(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	return ENOTSUP;
@@ -202,12 +217,12 @@
  */
 static int req_get_descriptor(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	if (request->value_high == USB_DESCTYPE_HUB) {
-		int rc = dev->control_transfer_reply(dev, 0,
+		usbvirt_control_reply_helper(request, data, act_size,
 		    &hub_descriptor, hub_descriptor.length);
 
-		return rc;
+		return EOK;
 	}
 	/* Let the framework handle all the rest. */
@@ -223,11 +238,13 @@
  */
 static int req_get_hub_status(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	uint32_t hub_status = 0;
 
-	return dev->control_transfer_reply(dev, 0,
+	usbvirt_control_reply_helper(request, data, act_size,
 	    &hub_status, sizeof(hub_status));
+
+	return EOK;
 }
 
@@ -240,6 +257,6 @@
  */
 static int req_get_port_status(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	hub_t *hub = (hub_t *) dev->device_data;
@@ -251,5 +268,8 @@
 	hub_release(hub);
 
-	return dev->control_transfer_reply(dev, 0, &status, 4);
+	usbvirt_control_reply_helper(request, data, act_size,
+	    &status, sizeof(status));
+
+	return EOK;
 }
 
@@ -262,6 +282,6 @@
  */
 static int req_set_hub_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	return ENOTSUP;
@@ -276,6 +296,6 @@
  */
 static int req_set_port_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
+    const usb_device_request_setup_packet_t *request, uint8_t *data,
+    size_t *act_size)
 {
 	int rc;
@@ -330,7 +350,7 @@
 
 /** Recipient: other. */
-#define REC_OTHER USBVIRT_REQUEST_RECIPIENT_OTHER
+#define REC_OTHER USB_REQUEST_RECIPIENT_OTHER
 /** Recipient: device. */
-#define REC_DEVICE USBVIRT_REQUEST_RECIPIENT_DEVICE
+#define REC_DEVICE USB_REQUEST_RECIPIENT_DEVICE
 /** Direction: in. */
 #define DIR_IN USB_DIRECTION_IN
@@ -338,4 +358,5 @@
 #define DIR_OUT USB_DIRECTION_OUT
 
+
 /** Create a class request.
  *
@@ -345,6 +366,7 @@
  */
 #define CLASS_REQ(direction, recipient, req) \
-	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_CLASS, recipient), \
+	.req_direction = direction, \
+	.req_recipient = recipient, \
+	.req_type = USB_REQUEST_TYPE_CLASS, \
 	.request = req
 
@@ -356,10 +378,11 @@
  */
 #define STD_REQ(direction, recipient, req) \
-	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_STANDARD, recipient), \
+	.req_direction = direction, \
+	.req_recipient = recipient, \
+	.req_type = USB_REQUEST_TYPE_STANDARD, \
 	.request = req
 
 /** Hub operations on control endpoint zero. */
-static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
+static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
 	{
 		STD_REQ(DIR_IN, REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
@@ -417,5 +440,7 @@
 		.callback = req_set_port_feature
 	},
-	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
+	{
+		.callback = NULL
+	}
 };
 
@@ -423,8 +448,7 @@
 /** Hub operations. */
 usbvirt_device_ops_t hub_ops = {
-	.control_transfer_handlers = endpoint_zero_handlers,
-	.on_data = NULL,
-	.on_data_request = req_on_data,
-	.on_state_change = on_state_change,
+	.control = endpoint_zero_handlers,
+	.data_in[HUB_STATUS_CHANGE_PIPE] = req_on_status_change_pipe,
+	.state_changed = on_state_change,
 };
 
Index: uspace/drv/vhc/main.c
===================================================================
--- uspace/drv/vhc/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/drv/vhc/main.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,147 @@
+/*
+ * 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 drvusbvhc
+ * @{
+ */ 
+/** @file
+ * Virtual host controller.
+ */
+
+#include <devmap.h>
+#include <async.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sysinfo.h>
+#include <stdio.h>
+#include <errno.h>
+#include <str_error.h>
+#include <ddf/driver.h>
+
+#include <usb/usb.h>
+#include <usb/ddfiface.h>
+#include <usb_iface.h>
+#include "vhcd.h"
+#include "hub.h"
+#include "conn.h"
+
+static ddf_dev_ops_t vhc_ops = {
+	.interfaces[USBHC_DEV_IFACE] = &vhc_iface,
+	.interfaces[USB_DEV_IFACE] = &vhc_usb_iface,
+	.close = on_client_close,
+	.default_handler = default_connection_handler
+};
+
+static int vhc_add_device(ddf_dev_t *dev)
+{
+	static int vhc_count = 0;
+	int rc;
+
+	if (vhc_count > 0) {
+		return ELIMIT;
+	}
+
+	vhc_data_t *data = malloc(sizeof(vhc_data_t));
+	if (data == NULL) {
+		usb_log_fatal("Failed to allocate memory.\n");
+		return ENOMEM;
+	}
+	data->magic = 0xDEADBEEF;
+	rc = usb_endpoint_manager_init(&data->ep_manager, (size_t) -1);
+	if (rc != EOK) {
+		usb_log_fatal("Failed to initialize endpoint manager.\n");
+		free(data);
+		return rc;
+	}
+	usb_device_keeper_init(&data->dev_keeper);
+
+	ddf_fun_t *hc = ddf_fun_create(dev, fun_exposed, "hc");
+	if (hc == NULL) {
+		usb_log_fatal("Failed to create device function.\n");
+		free(data);
+		return ENOMEM;
+	}
+
+	hc->ops = &vhc_ops;
+	list_initialize(&data->devices);
+	fibril_mutex_initialize(&data->guard);
+	data->hub = &virtual_hub_device;
+	data->hc_fun = hc;
+
+	dev->driver_data = data;
+
+	rc = ddf_fun_bind(hc);
+	if (rc != EOK) {
+		usb_log_fatal("Failed to bind HC function: %s.\n",
+		    str_error(rc));
+		free(data);
+		return rc;
+	}
+
+	ddf_fun_add_to_class(hc, "usbhc");
+
+	virtual_hub_device_init(hc);
+
+	usb_log_info("Virtual USB host controller ready (dev %zu, hc %zu).\n",
+	    (size_t) dev->handle, (size_t) hc->handle);
+
+
+
+	rc = vhc_virtdev_plug_hub(data, data->hub, NULL);
+	if (rc != EOK) {
+		usb_log_fatal("Failed to plug root hub: %s.\n", str_error(rc));
+		free(data);
+		return rc;
+	}
+
+	return EOK;
+}
+
+static driver_ops_t vhc_driver_ops = {
+	.add_device = vhc_add_device,
+};
+
+static driver_t vhc_driver = {
+	.name = NAME,
+	.driver_ops = &vhc_driver_ops
+};
+
+
+int main(int argc, char * argv[])
+{	
+	usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
+
+	printf(NAME ": virtual USB host controller driver.\n");
+
+	return ddf_driver_main(&vhc_driver);
+}
+
+
+/**
+ * @}
+ */
Index: uspace/drv/vhc/transfer.c
===================================================================
--- uspace/drv/vhc/transfer.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/drv/vhc/transfer.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -0,0 +1,241 @@
+#include <errno.h>
+#include <str_error.h>
+#include <usbvirt/device.h>
+#include <usbvirt/ipc.h>
+#include "vhcd.h"
+
+#define list_foreach(pos, head) \
+	for (pos = (head)->next; pos != (head); \
+        	pos = pos->next)
+
+vhc_transfer_t *vhc_transfer_create(usb_address_t address, usb_endpoint_t ep,
+    usb_direction_t dir, usb_transfer_type_t tr_type,
+    ddf_fun_t *fun, void *callback_arg)
+{
+	vhc_transfer_t *result = malloc(sizeof(vhc_transfer_t));
+	if (result == NULL) {
+		return NULL;
+	}
+	link_initialize(&result->link);
+	result->address = address;
+	result->endpoint = ep;
+	result->direction = dir;
+	result->transfer_type = tr_type;
+	result->setup_buffer = NULL;
+	result->setup_buffer_size = 0;
+	result->data_buffer = NULL;
+	result->data_buffer_size = 0;
+	result->ddf_fun = fun;
+	result->callback_arg = callback_arg;
+	result->callback_in = NULL;
+	result->callback_out = NULL;
+
+	usb_log_debug2("Created transfer %p (%d.%d %s %s)\n", result,
+	    address, ep, usb_str_transfer_type_short(tr_type),
+	    dir == USB_DIRECTION_IN ? "in" : "out");
+
+	return result;
+}
+
+static bool is_set_address_transfer(vhc_transfer_t *transfer)
+{
+	if (transfer->endpoint != 0) {
+		return false;
+	}
+	if (transfer->transfer_type != USB_TRANSFER_CONTROL) {
+		return false;
+	}
+	if (transfer->direction != USB_DIRECTION_OUT) {
+		return false;
+	}
+	if (transfer->setup_buffer_size != sizeof(usb_device_request_setup_packet_t)) {
+		return false;
+	}
+	usb_device_request_setup_packet_t *setup = transfer->setup_buffer;
+	if (setup->request_type != 0) {
+		return false;
+	}
+	if (setup->request != USB_DEVREQ_SET_ADDRESS) {
+		return false;
+	}
+
+	return true;
+}
+
+int vhc_virtdev_add_transfer(vhc_data_t *vhc, vhc_transfer_t *transfer)
+{
+	fibril_mutex_lock(&vhc->guard);
+
+	link_t *pos;
+	bool target_found = false;
+	list_foreach(pos, &vhc->devices) {
+		vhc_virtdev_t *dev = list_get_instance(pos, vhc_virtdev_t, link);
+		fibril_mutex_lock(&dev->guard);
+		if (dev->address == transfer->address) {
+			if (target_found) {
+				usb_log_warning("Transfer would be accepted by more devices!\n");
+				goto next;
+			}
+			target_found = true;
+			list_append(&transfer->link, &dev->transfer_queue);
+		}
+next:
+		fibril_mutex_unlock(&dev->guard);
+	}
+
+	fibril_mutex_unlock(&vhc->guard);
+
+	if (target_found) {
+		return EOK;
+	} else {
+		return ENOENT;
+	}
+}
+
+static int process_transfer_local(vhc_transfer_t *transfer,
+    usbvirt_device_t *dev, size_t *actual_data_size)
+{
+	int rc;
+
+	if (transfer->transfer_type == USB_TRANSFER_CONTROL) {
+		if (transfer->direction == USB_DIRECTION_IN) {
+			rc = usbvirt_control_read(dev,
+			    transfer->setup_buffer, transfer->setup_buffer_size,
+			    transfer->data_buffer, transfer->data_buffer_size,
+			    actual_data_size);
+		} else {
+			assert(transfer->direction == USB_DIRECTION_OUT);
+			rc = usbvirt_control_write(dev,
+			    transfer->setup_buffer, transfer->setup_buffer_size,
+			    transfer->data_buffer, transfer->data_buffer_size);
+		}
+	} else {
+		if (transfer->direction == USB_DIRECTION_IN) {
+			rc = usbvirt_data_in(dev, transfer->transfer_type,
+			    transfer->endpoint,
+			    transfer->data_buffer, transfer->data_buffer_size,
+			    actual_data_size);
+		} else {
+			assert(transfer->direction == USB_DIRECTION_OUT);
+			rc = usbvirt_data_out(dev, transfer->transfer_type,
+			    transfer->endpoint,
+			    transfer->data_buffer, transfer->data_buffer_size);
+		}
+	}
+
+	return rc;
+}
+
+static int process_transfer_remote(vhc_transfer_t *transfer,
+    int phone, size_t *actual_data_size)
+{
+	int rc;
+
+	if (transfer->transfer_type == USB_TRANSFER_CONTROL) {
+		if (transfer->direction == USB_DIRECTION_IN) {
+			rc = usbvirt_ipc_send_control_read(phone,
+			    transfer->endpoint,
+			    transfer->setup_buffer, transfer->setup_buffer_size,
+			    transfer->data_buffer, transfer->data_buffer_size,
+			    actual_data_size);
+		} else {
+			assert(transfer->direction == USB_DIRECTION_OUT);
+			rc = usbvirt_ipc_send_control_write(phone,
+			    transfer->endpoint,
+			    transfer->setup_buffer, transfer->setup_buffer_size,
+			    transfer->data_buffer, transfer->data_buffer_size);
+		}
+	} else {
+		if (transfer->direction == USB_DIRECTION_IN) {
+			rc = usbvirt_ipc_send_data_in(phone, transfer->endpoint,
+			    transfer->transfer_type,
+			    transfer->data_buffer, transfer->data_buffer_size,
+			    actual_data_size);
+		} else {
+			assert(transfer->direction == USB_DIRECTION_OUT);
+			rc = usbvirt_ipc_send_data_out(phone, transfer->endpoint,
+			    transfer->transfer_type,
+			    transfer->data_buffer, transfer->data_buffer_size);
+		}
+	}
+
+	return rc;
+}
+
+
+int vhc_transfer_queue_processor(void *arg)
+{
+	vhc_virtdev_t *dev = arg;
+	fibril_mutex_lock(&dev->guard);
+	while (dev->plugged) {
+		if (list_empty(&dev->transfer_queue)) {
+			fibril_mutex_unlock(&dev->guard);
+			async_usleep(10 * 1000);
+			fibril_mutex_lock(&dev->guard);
+			continue;
+		}
+
+		vhc_transfer_t *transfer = list_get_instance(dev->transfer_queue.next,
+		    vhc_transfer_t, link);
+		list_remove(&transfer->link);
+		fibril_mutex_unlock(&dev->guard);
+
+		int rc = EOK;
+		size_t data_transfer_size = 0;
+		if (dev->dev_phone > 0) {
+			rc = process_transfer_remote(transfer, dev->dev_phone,
+			    &data_transfer_size);
+		} else if (dev->dev_local != NULL) {
+			rc = process_transfer_local(transfer, dev->dev_local,
+			    &data_transfer_size);
+		} else {
+			usb_log_warning("Device has no remote phone nor local node.\n");
+			rc = ESTALL;
+		}
+
+		usb_log_debug2("Transfer %p processed: %s.\n",
+		    transfer, str_error(rc));
+
+		fibril_mutex_lock(&dev->guard);
+		if (rc == EOK) {
+			if (is_set_address_transfer(transfer)) {
+				usb_device_request_setup_packet_t *setup
+				    = transfer->setup_buffer;
+				dev->address = setup->value;
+				usb_log_debug2("Address changed to %d\n",
+				    dev->address);
+			}
+		}
+		if (rc == ENAK) {
+			// FIXME: this will work only because we do
+			// not NAK control transfers but this is generally
+			// a VERY bad idea indeed
+			list_append(&transfer->link, &dev->transfer_queue);
+		}
+		fibril_mutex_unlock(&dev->guard);
+
+		if (rc != ENAK) {
+			usb_log_debug2("Transfer %p ended: %s.\n",
+			    transfer, str_error(rc));
+			if (transfer->direction == USB_DIRECTION_IN) {
+				transfer->callback_in(transfer->ddf_fun, rc,
+				    data_transfer_size, transfer->callback_arg);
+			} else {
+				assert(transfer->direction == USB_DIRECTION_OUT);
+				transfer->callback_out(transfer->ddf_fun, rc,
+				    transfer->callback_arg);
+			}
+			free(transfer);
+		}
+
+		async_usleep(1000 * 100);
+		fibril_mutex_lock(&dev->guard);
+	}
+
+	fibril_mutex_unlock(&dev->guard);
+
+	// TODO - destroy pending transfers
+
+	return EOK;
+}
+
Index: uspace/drv/vhc/vhcd.h
===================================================================
--- uspace/drv/vhc/vhcd.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/drv/vhc/vhcd.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -37,15 +37,57 @@
 
 #include <usb/debug.h>
+#include <usbvirt/device.h>
+#include <usb/host/usb_endpoint_manager.h>
+#include <usb/host/device_keeper.h>
+#include <usbhc_iface.h>
 
 #define NAME "vhc"
-#define NAME_DEV "hcd-virt-dev"
-#define NAMESPACE "usb"
 
-#define DEVMAP_PATH_HC NAMESPACE "/" NAME
-#define DEVMAP_PATH_DEV NAMESPACE "/" NAME_DEV
+typedef struct {
+	link_t link;
+	int dev_phone;
+	usbvirt_device_t *dev_local;
+	bool plugged;
+	usb_address_t address;
+	fibril_mutex_t guard;
+	link_t transfer_queue;
+} vhc_virtdev_t;
 
-//#define dprintf(level, format, ...)
-//	usb_dprintf(NAME, (level), format "\n", ##__VA_ARGS__)
-//void dprintf_inval_call(int, ipc_call_t, sysarg_t);
+typedef struct {
+	uint32_t magic;
+	link_t devices;
+	fibril_mutex_t guard;
+	usb_endpoint_manager_t ep_manager;
+	usb_device_keeper_t dev_keeper;
+	usbvirt_device_t *hub;
+	ddf_fun_t *hc_fun;
+} vhc_data_t;
+
+typedef struct {
+	link_t link;
+	usb_address_t address;
+	usb_endpoint_t endpoint;
+	usb_direction_t direction;
+	usb_transfer_type_t transfer_type;
+	void *setup_buffer;
+	size_t setup_buffer_size;
+	void *data_buffer;
+	size_t data_buffer_size;
+	ddf_fun_t *ddf_fun;
+	void *callback_arg;
+	usbhc_iface_transfer_in_callback_t callback_in;
+	usbhc_iface_transfer_out_callback_t callback_out;
+} vhc_transfer_t;
+
+vhc_transfer_t *vhc_transfer_create(usb_address_t, usb_endpoint_t,
+    usb_direction_t, usb_transfer_type_t, ddf_fun_t *, void *);
+int vhc_virtdev_plug(vhc_data_t *, int, uintptr_t *);
+int vhc_virtdev_plug_local(vhc_data_t *, usbvirt_device_t *, uintptr_t *);
+int vhc_virtdev_plug_hub(vhc_data_t *, usbvirt_device_t *, uintptr_t *);
+void vhc_virtdev_unplug(vhc_data_t *, uintptr_t);
+int vhc_virtdev_add_transfer(vhc_data_t *, vhc_transfer_t *);
+
+int vhc_transfer_queue_processor(void *arg);
+
 
 #endif
Index: uspace/lib/c/include/errno.h
===================================================================
--- uspace/lib/c/include/errno.h	(revision a58dd620d81c410a5d10fc02dc04659e7e2ebd39)
+++ uspace/lib/c/include/errno.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usb/include/usb/classes/hiddescriptor.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usb/include/usb/classes/hidpath.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usb/include/usb/classes/hidtypes.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usb/src/hiddescriptor.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usb/src/hidpath.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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: pace/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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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: pace/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: pace/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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usbvirt/src/ctrltransfer.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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: pace/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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usbvirt/src/ipc.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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: pace/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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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: pace/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 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
+++ uspace/lib/usbvirt/src/transfer.c	(revision 3d4aa05551843bc5de66e78e0dd0dbc2c4a21801)
@@ -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;
+}
+
+/**
+ * @}
+ */
