Index: uspace/drv/vhc/Makefile
===================================================================
--- uspace/drv/vhc/Makefile	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/Makefile	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/conn.h	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/conndev.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/connhost.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
+++ uspace/drv/vhc/devconn.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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: uspace/drv/vhc/devices.c
===================================================================
--- uspace/drv/vhc/devices.c	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ 	(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: uspace/drv/vhc/devices.h
===================================================================
--- uspace/drv/vhc/devices.h	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ 	(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: uspace/drv/vhc/hc.c
===================================================================
--- uspace/drv/vhc/hc.c	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ 	(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: uspace/drv/vhc/hc.h
===================================================================
--- uspace/drv/vhc/hc.h	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ 	(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: uspace/drv/vhc/hcd.c
===================================================================
--- uspace/drv/vhc/hcd.c	(revision 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ 	(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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub.h	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub/hub.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub/hub.h	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub/virthub.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub/virthub.h	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/hub/virthubops.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
+++ uspace/drv/vhc/main.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
+++ uspace/drv/vhc/transfer.c	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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 564a2b33fa6a9365cde0a9228b5722048647e566)
+++ uspace/drv/vhc/vhcd.h	(revision e31b5c191ea0f11dc7765ce32f4e11cce68f6620)
@@ -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
