Index: .bzrignore
===================================================================
--- .bzrignore	(revision 74f00b6d84025d10f2385366580496754f90b606)
+++ .bzrignore	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ boot/Makefile.common	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/Makefile	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/app/usbinfo/main.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/app/virtusbkbd/Makefile	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/app/virtusbkbd/stdreq.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/app/virtusbkbd/stdreq.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/app/virtusbkbd/virtusbkbd.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/Makefile	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/device.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/hids/bootkbd.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/ifaces.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/ifaces.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/main.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/stdreq.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/stdreq.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/app/vuhid/virthid.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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/vhc/Makefile
===================================================================
--- uspace/drv/vhc/Makefile	(revision 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/Makefile	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/conn.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/conndev.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/connhost.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/drv/vhc/devconn.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub/hub.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub/hub.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub/virthub.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub/virthub.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/hub/virthubops.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/drv/vhc/main.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/drv/vhc/transfer.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/drv/vhc/vhcd.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/c/include/errno.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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/usb/include/usb/usb.h
===================================================================
--- uspace/lib/usb/include/usb/usb.h	(revision 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usb/include/usb/usb.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usb/src/debug.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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/usbvirt/Makefile
===================================================================
--- uspace/lib/usbvirt/Makefile	(revision 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usbvirt/Makefile	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usbvirt/include/usbvirt/device.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/lib/usbvirt/src/ctrltransfer.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/lib/usbvirt/src/ipc.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usbvirt/src/private.h	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ uspace/lib/usbvirt/src/stdreq.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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 74f00b6d84025d10f2385366580496754f90b606)
+++ 	(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 cd4ae1e19104328a0283b82f5fa548a351b42bbd)
+++ uspace/lib/usbvirt/src/transfer.c	(revision cd4ae1e19104328a0283b82f5fa548a351b42bbd)
@@ -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;
+}
+
+/**
+ * @}
+ */
