Index: uspace/drv/vhc/Makefile
===================================================================
--- uspace/drv/vhc/Makefile	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/Makefile	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -39,4 +39,7 @@
 
 SOURCES = \
+	hub/hub.c \
+	hub/virthub.c \
+	hub/virthubops.c \
 	conndev.c \
 	connhost.c \
@@ -45,6 +48,5 @@
 	hc.c \
 	hcd.c \
-	hub.c \
-	hubops.c
+	hub.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/drv/vhc/devices.c
===================================================================
--- uspace/drv/vhc/devices.c	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/devices.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -47,4 +47,5 @@
 #include "devices.h"
 #include "hub.h"
+#include "hub/virthub.h"
 #include "vhcd.h"
 
@@ -69,5 +70,5 @@
 	list_append(&dev->link, &devices);
 	
-	hub_add_device(dev);
+	virthub_connect_device(&virtual_hub_device, dev);
 	
 	return dev;
@@ -78,5 +79,5 @@
 void virtdev_destroy_device(virtdev_connection_t *dev)
 {
-	hub_remove_device(dev);
+	virthub_disconnect_device(&virtual_hub_device, dev);
 	list_remove(&dev->link);
 	free(dev);
@@ -94,5 +95,5 @@
 		    = list_get_instance(pos, virtdev_connection_t, link);
 		
-		if (!hub_can_device_signal(dev)) {
+		if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
 			continue;
 		}
@@ -145,5 +146,5 @@
 	 * (if the address matches).
 	 */
-	if (virthub_dev.address == transaction->target.address) {
+	if (virtual_hub_device.address == transaction->target.address) {
 		size_t tmp;
 		dprintf(1, "sending `%s' transaction to hub",
@@ -151,5 +152,6 @@
 		switch (transaction->type) {
 			case USBVIRT_TRANSACTION_SETUP:
-				virthub_dev.transaction_setup(&virthub_dev,
+				virtual_hub_device.transaction_setup(
+				    &virtual_hub_device,
 				    transaction->target.endpoint,
 				    transaction->buffer, transaction->len);
@@ -157,5 +159,6 @@
 				
 			case USBVIRT_TRANSACTION_IN:
-				virthub_dev.transaction_in(&virthub_dev,
+				virtual_hub_device.transaction_in(
+				    &virtual_hub_device,
 				    transaction->target.endpoint,
 				    transaction->buffer, transaction->len,
@@ -167,5 +170,6 @@
 				
 			case USBVIRT_TRANSACTION_OUT:
-				virthub_dev.transaction_out(&virthub_dev,
+				virtual_hub_device.transaction_out(
+				    &virtual_hub_device,
 				    transaction->target.endpoint,
 				    transaction->buffer, transaction->len);
Index: uspace/drv/vhc/hc.c
===================================================================
--- uspace/drv/vhc/hc.c	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/hc.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -78,4 +78,6 @@
 	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)
 {
@@ -115,8 +117,6 @@
 		}
 		
-		char ports[HUB_PORT_COUNT + 2];
-		hub_get_port_statuses(ports, HUB_PORT_COUNT + 1);
-		dprintf(4, "virtual hub: addr=%d ports=%s",
-		    virthub_dev.address, ports);
+		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;
@@ -126,5 +126,5 @@
 		
 
-		dprintf(0, "about to process " TRANSACTION_FORMAT " (vhub:%s)",
+		dprintf(0, "about to process " TRANSACTION_FORMAT " [%s]",
 		    TRANSACTION_PRINTF(*transaction), ports);
 
Index: uspace/drv/vhc/hcd.c
===================================================================
--- uspace/drv/vhc/hcd.c	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/hcd.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -79,5 +79,5 @@
 	 * Initialize our hub and announce its presence.
 	 */
-	hub_init(dev);
+	virtual_hub_device_init(dev);
 
 	printf("%s: virtual USB host controller ready.\n", NAME);
@@ -108,5 +108,11 @@
 int main(int argc, char * argv[])
 {	
-	printf("%s: virtual USB host controller driver.\n", NAME);
+	/*
+	 * Temporary workaround. Wait a little bit to be the last driver
+	 * in devman output.
+	 */
+	sleep(4);
+
+	printf(NAME ": virtual USB host controller driver.\n");
 
 	usb_dprintf_enable(NAME, 0);
@@ -114,14 +120,9 @@
 	fid_t fid = fibril_create(hc_manager_fibril, NULL);
 	if (fid == 0) {
-		printf("%s: failed to start HC manager fibril\n", NAME);
+		printf(NAME ": failed to start HC manager fibril\n");
 		return ENOMEM;
 	}
 	fibril_add_ready(fid);
 
-	/*
-	 * Temporary workaround. Wait a little bit to be the last driver
-	 * in devman output.
-	 */
-	sleep(4);
 
 	return driver_main(&vhc_driver);
Index: uspace/drv/vhc/hub.c
===================================================================
--- uspace/drv/vhc/hub.c	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/hub.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -42,113 +42,31 @@
 #include <usb/usbdrv.h>
 
+#include "hub.h"
+#include "hub/virthub.h"
 #include "vhcd.h"
-#include "hub.h"
-#include "hubintern.h"
-#include "conn.h"
 
+usbvirt_device_t virtual_hub_device;
 
-/** Standard device descriptor. */
-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_HUB,
-	.device_subclass = 0,
-	.device_protocol = 0,
-	.max_packet_size = 64,
-	.configuration_count = 1
-};
+static int hub_register_in_devman_fibril(void *arg);
 
-/** Standard interface descriptor. */
-usb_standard_interface_descriptor_t std_interface_descriptor = {
-	.length = sizeof(usb_standard_interface_descriptor_t),
-	.descriptor_type = USB_DESCTYPE_INTERFACE,
-	.interface_number = 0,
-	.alternate_setting = 0,
-	.endpoint_count = 1,
-	.interface_class = USB_CLASS_HUB,
-	.interface_subclass = 0,
-	.interface_protocol = 0,
-	.str_interface = 0
-};
+void virtual_hub_device_init(device_t *hc_dev)
+{
+	virthub_init(&virtual_hub_device);
 
-hub_descriptor_t hub_descriptor = {
-	.length = sizeof(hub_descriptor_t),
-	.type = USB_DESCTYPE_HUB,
-	.port_count = HUB_PORT_COUNT,
-	.characteristics = 0, 
-	.power_on_warm_up = 50, /* Huh? */
-	.max_current = 100, /* Huh again. */
-	.removable_device = { 0 },
-	.port_power = { 0xFF }
-};
+	/*
+	 * We need to register the root hub.
+	 * This must be done in separate fibril because the device
+	 * we are connecting to are ourselves and we cannot connect
+	 * before leaving the add_device() function.
+	 */
+	fid_t root_hub_registration
+	    = fibril_create(hub_register_in_devman_fibril, hc_dev);
+	if (root_hub_registration == 0) {
+		printf(NAME ": failed to register root hub\n");
+		return;
+	}
 
-/** Endpoint descriptor. */
-usb_standard_endpoint_descriptor_t endpoint_descriptor = {
-	.length = sizeof(usb_standard_endpoint_descriptor_t),
-	.descriptor_type = USB_DESCTYPE_ENDPOINT,
-	.endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
-	.attributes = USB_TRANSFER_INTERRUPT,
-	.max_packet_size = 8,
-	.poll_interval = 0xFF
-};
-
-/** Standard configuration descriptor. */
-usb_standard_configuration_descriptor_t std_configuration_descriptor = {
-	.length = sizeof(usb_standard_configuration_descriptor_t),
-	.descriptor_type = USB_DESCTYPE_CONFIGURATION,
-	.total_length = 
-		sizeof(usb_standard_configuration_descriptor_t)
-		+ sizeof(std_interface_descriptor)
-		+ sizeof(hub_descriptor)
-		+ sizeof(endpoint_descriptor)
-		,
-	.interface_count = 1,
-	.configuration_number = HUB_CONFIGURATION_ID,
-	.str_configuration = 0,
-	.attributes = 128, /* denotes bus-powered device */
-	.max_power = 50
-};
-
-/** All hub configuration descriptors. */
-static usbvirt_device_configuration_extras_t extra_descriptors[] = {
-	{
-		.data = (uint8_t *) &std_interface_descriptor,
-		.length = sizeof(std_interface_descriptor)
-	},
-	{
-		.data = (uint8_t *) &hub_descriptor,
-		.length = sizeof(hub_descriptor)
-	},
-	{
-		.data = (uint8_t *) &endpoint_descriptor,
-		.length = sizeof(endpoint_descriptor)
-	}
-};
-
-/** Hub configuration. */
-usbvirt_device_configuration_t configuration = {
-	.descriptor = &std_configuration_descriptor,
-	.extra = extra_descriptors,
-	.extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
-};
-
-/** Hub standard descriptors. */
-usbvirt_descriptors_t descriptors = {
-	.device = &std_device_descriptor,
-	.configuration = &configuration,
-	.configuration_count = 1,
-};
-
-/** Hub as a virtual device. */
-usbvirt_device_t virthub_dev = {
-	.ops = &hub_ops,
-	.descriptors = &descriptors,
-	.lib_debug_level = 0,
-	.lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL
-};
-
-/** Hub device. */
-hub_device_t hub_dev;
+	fibril_add_ready(root_hub_registration);
+}
 
 /** Register root hub in devman.
@@ -157,5 +75,5 @@
  * @return Error code.
  */
-static int hub_register_in_devman_fibril(void *arg)
+int hub_register_in_devman_fibril(void *arg)
 {
 	device_t *hc_dev = (device_t *) arg;
@@ -180,128 +98,5 @@
 	return EOK;
 }
-
-/** Initialize virtual hub. */
-void hub_init(device_t *hc_dev)
-{
-	size_t i;
 	
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_port_t *port = &hub_dev.ports[i];
-		
-		port->index = (int) i + 1;
-		port->device = NULL;
-		port->state = HUB_PORT_STATE_NOT_CONFIGURED;
-		port->status_change = 0;
-		fibril_mutex_initialize(&port->guard);
-	}
-	
-	usbvirt_connect_local(&virthub_dev);
-	
-	/*
-	 * We need to register the root hub.
-	 * This must be done in separate fibril because the device
-	 * we are connecting to are ourselves and we cannot connect
-	 * before leaving the add_device() function.
-	 */
-	fid_t root_hub_registration
-	    = fibril_create(hub_register_in_devman_fibril, hc_dev);
-	if (root_hub_registration == 0) {
-		printf(NAME ": failed to register root hub\n");
-		return;
-	}
-
-	fibril_add_ready(root_hub_registration);
-}
-
-/** Connect device to the hub.
- *
- * @param device Device to be connected.
- * @return Port where the device was connected to.
- */
-size_t hub_add_device(virtdev_connection_t *device)
-{
-	size_t i;
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_port_t *port = &hub_dev.ports[i];
-		fibril_mutex_lock(&port->guard);
-		
-		if (port->device != NULL) {
-			fibril_mutex_unlock(&port->guard);
-			continue;
-		}
-		
-		port->device = device;
-		
-		/*
-		 * TODO:
-		 * If the hub was configured, we can normally
-		 * announce the plug-in.
-		 * Otherwise, we will wait until hub is configured
-		 * and announce changes in single burst.
-		 */
-		//if (port->state == HUB_PORT_STATE_DISCONNECTED) {
-			port->state = HUB_PORT_STATE_DISABLED;
-			set_port_status_change_nl(port, HUB_STATUS_C_PORT_CONNECTION);
-		//}
-		
-		fibril_mutex_unlock(&port->guard);
-
-		return i;
-	}
-	
-	return (size_t)-1;
-}
-
-/** Disconnect device from the hub. */
-void hub_remove_device(virtdev_connection_t *device)
-{
-	size_t i;
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_port_t *port = &hub_dev.ports[i];
-		
-		if (port->device != device) {
-			continue;
-		}
-		
-		port->device = NULL;
-		port->state = HUB_PORT_STATE_DISCONNECTED;
-		
-		set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
-	}
-}
-
-/** Tell whether device port is open.
- *
- * @return Whether communication to and from the device can go through the hub.
- */
-bool hub_can_device_signal(virtdev_connection_t * device)
-{
-	size_t i;
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		if (hub_dev.ports[i].device == device) {
-			return hub_dev.ports[i].state == HUB_PORT_STATE_ENABLED;
-		}
-	}
-	
-	return false;
-}
-
-/** Format hub port status.
- *
- * @param result Buffer where to store status string.
- * @param len Number of characters that is possible to store in @p result
- * 	(excluding trailing zero).
- */
-void hub_get_port_statuses(char *result, size_t len)
-{
-	if (len > HUB_PORT_COUNT) {
-		len = HUB_PORT_COUNT;
-	}
-	size_t i;
-	for (i = 0; i < len; i++) {
-		result[i] = hub_port_state_as_char(hub_dev.ports[i].state);
-	}
-	result[len] = 0;
-}
 
 /**
Index: uspace/drv/vhc/hub.h
===================================================================
--- uspace/drv/vhc/hub.h	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ uspace/drv/vhc/hub.h	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -40,17 +40,10 @@
 
 #include "devices.h"
+#include "hub/hub.h"
+#include "hub/virthub.h"
 
-#define HUB_PORT_COUNT 2
+extern usbvirt_device_t virtual_hub_device;
 
-#define BITS2BYTES(bits) \
-    (bits ? ((((bits)-1)>>3)+1) : 0)
-
-extern usbvirt_device_t virthub_dev;
-
-void hub_init(device_t *);
-size_t hub_add_device(virtdev_connection_t *);
-void hub_remove_device(virtdev_connection_t *);
-bool hub_can_device_signal(virtdev_connection_t *);
-void hub_get_port_statuses(char *result, size_t len);
+void virtual_hub_device_init(device_t *);
 
 #endif
Index: uspace/drv/vhc/hub/hub.c
===================================================================
--- uspace/drv/vhc/hub/hub.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
+++ uspace/drv/vhc/hub/hub.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -0,0 +1,443 @@
+/*
+ * 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 usb
+ * @{
+ */
+/** @file
+ * @brief Virtual USB hub.
+ */
+#include <usb/classes/classes.h>
+#include <usbvirt/hub.h>
+#include <usbvirt/device.h>
+#include <errno.h>
+#include <str_error.h>
+#include <stdlib.h>
+#include <driver.h>
+#include <usb/usbdrv.h>
+
+#include "hub.h"
+
+
+/** Produce a byte from bit values.
+ */
+#define MAKE_BYTE(b0, b1, b2, b3, b4, b5, b6, b7) \
+	(( \
+		((b0) << 0) \
+		| ((b1) << 1) \
+		| ((b2) << 2) \
+		| ((b3) << 3) \
+		| ((b4) << 4) \
+		| ((b5) << 5) \
+		| ((b6) << 6) \
+		| ((b7) << 7) \
+	))
+
+/* Static functions. */
+static hub_port_t *get_hub_port(hub_t *, size_t);
+static void set_port_status_change(hub_port_t *, hub_status_change_t);
+static void clear_port_status_change(hub_port_t *, uint16_t);
+static int set_port_state_delayed_fibril(void *);
+static void set_port_state_delayed(hub_t *, size_t, suseconds_t,
+    hub_port_state_t, hub_port_state_t);
+
+/** Convert hub port state to a char. */
+char hub_port_state_to_char(hub_port_state_t state) {
+	switch (state) {
+		case HUB_PORT_STATE_NOT_CONFIGURED:
+			return '-';
+		case HUB_PORT_STATE_POWERED_OFF:
+			return 'O';
+		case HUB_PORT_STATE_DISCONNECTED:
+			return 'X';
+		case HUB_PORT_STATE_DISABLED:
+			return 'D';
+		case HUB_PORT_STATE_RESETTING:
+			return 'R';
+		case HUB_PORT_STATE_ENABLED:
+			return 'E';
+		case HUB_PORT_STATE_SUSPENDED:
+			return 'S';
+		case HUB_PORT_STATE_RESUMING:
+			return 'F';
+		default:
+			return '?';
+	}
+}
+
+/** Initialize single hub port.
+ *
+ * @param port Port to be initialized.
+ * @param index Port index (one based).
+ */
+static void hub_init_port(hub_port_t *port, size_t index)
+{
+	port->connected_device = NULL;
+	port->index = index;
+	port->state = HUB_PORT_STATE_NOT_CONFIGURED;
+	port->status_change = 0;
+}
+
+/** Initialize the hub.
+ *
+ * @param hub Hub to be initialized.
+ */
+void hub_init(hub_t *hub)
+{
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		hub_init_port(&hub->ports[i], i + 1);
+	}
+	hub->custom_data = NULL;
+	fibril_mutex_initialize(&hub->guard);
+}
+
+/** Connect a device to the hub.
+ *
+ * @param hub Hub to connect device to.
+ * @param device Device to be connected.
+ * @return Index of port the device was connected to.
+ * @retval -1 No free port available.
+ */
+size_t hub_connect_device(hub_t *hub, void *device)
+{
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		hub_port_t *port = &hub->ports[i];
+
+		if (port->connected_device != NULL) {
+			continue;
+		}
+
+		port->connected_device = device;
+
+		/*
+		 * TODO:
+		 * If the hub was configured, we can normally
+		 * announce the plug-in.
+		 * Otherwise, we will wait until hub is configured
+		 * and announce changes in single burst.
+		 */
+		//if (port->state == HUB_PORT_STATE_DISCONNECTED) {
+			port->state = HUB_PORT_STATE_DISABLED;
+			set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
+		//}
+
+		return i;
+	}
+
+	return (size_t) -1;
+}
+
+/** Find port device is connected to.
+ *
+ * @param hub Hub in question.
+ * @param device Device in question.
+ * @return Port index (zero based).
+ * @retval -1 Device is not connected.
+ */
+size_t hub_find_device(hub_t *hub, void *device)
+{
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		hub_port_t *port = &hub->ports[i];
+
+		if (port->connected_device == device) {
+			return i;
+		}
+	}
+
+	return 0;
+}
+
+/** Acquire exclusive access to the hub.
+ *
+ * @param hub Hub in question.
+ */
+void hub_acquire(hub_t *hub)
+{
+	fibril_mutex_lock(&hub->guard);
+}
+
+/** Give up exclusive access to the hub.
+ *
+ * @param hub Hub in question.
+ */
+void hub_release(hub_t *hub)
+{
+	fibril_mutex_unlock(&hub->guard);
+}
+
+/** Change port state.
+ *
+ * @param hub Hub the port belongs to.
+ * @param port_index Port index (zero based).
+ * @param state New port state.
+ */
+void hub_set_port_state(hub_t *hub, size_t port_index, hub_port_state_t state)
+{
+	hub_port_t *port = get_hub_port(hub, port_index);
+	if (port == NULL) {
+		return;
+	}
+
+	switch (state) {
+		case HUB_PORT_STATE_POWERED_OFF:
+			clear_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
+			clear_port_status_change(port, HUB_STATUS_C_PORT_ENABLE);
+			clear_port_status_change(port, HUB_STATUS_C_PORT_RESET);
+			break;
+		case HUB_PORT_STATE_RESUMING:
+			set_port_state_delayed(hub, port_index,
+			    10, state, HUB_PORT_STATE_ENABLED);
+			break;
+		case HUB_PORT_STATE_RESETTING:
+			set_port_state_delayed(hub, port_index,
+			    10, state, HUB_PORT_STATE_ENABLED);
+			break;
+		case HUB_PORT_STATE_ENABLED:
+			if (port->state == HUB_PORT_STATE_RESETTING) {
+				set_port_status_change(port, HUB_STATUS_C_PORT_RESET);
+			}
+			break;
+		default:
+			break;
+	}
+
+	port->state = state;
+}
+
+/** Change state of all ports.
+ *
+ * @param hub Hub in question.
+ * @param state New state for all ports.
+ */
+void hub_set_port_state_all(hub_t *hub, hub_port_state_t state)
+{
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		hub_set_port_state(hub, i, state);
+	}
+}
+
+/** Get port state.
+ *
+ * @param hub Hub the port belongs to.
+ * @param port_index Port index (zero based).
+ * @return Port state.
+ */
+hub_port_state_t hub_get_port_state(hub_t *hub, size_t port_index)
+{
+	hub_port_t *port = get_hub_port(hub, port_index);
+	if (port == NULL) {
+		return HUB_PORT_STATE_UNKNOWN;
+	}
+
+	return port->state;
+}
+
+/** Clear port status change bit.
+ *
+ * @param hub Hub the port belongs to.
+ * @param port_index Port index (zero based).
+ * @param change Change to be cleared.
+ */
+void hub_clear_port_status_change(hub_t *hub, size_t port_index,
+    hub_status_change_t change)
+{
+	hub_port_t *port = get_hub_port(hub, port_index);
+	if (port == NULL) {
+		return;
+	}
+
+	clear_port_status_change(port, change);
+}
+
+/** Get port status change bits.
+ *
+ * @param hub Hub the port belongs to.
+ * @param port_index Port index (zero based).
+ * @return Port status change bitmap in standard format.
+ */
+uint16_t hub_get_port_status_change(hub_t *hub, size_t port_index)
+{
+	hub_port_t *port = get_hub_port(hub, port_index);
+	if (port == NULL) {
+		return 0;
+	}
+
+	return port->status_change;
+}
+
+/** Get port status bits.
+ *
+ * @param hub Hub the port belongs to.
+ * @param port_index Port index (zero based).
+ * @return Port status bitmap in standard format.
+ */
+uint32_t hub_get_port_status(hub_t *hub, size_t port_index)
+{
+	hub_port_t *port = get_hub_port(hub, port_index);
+	if (port == NULL) {
+		return 0;
+	}
+
+	uint32_t status;
+	status = MAKE_BYTE(
+	    /* Current connect status. */
+	    port->connected_device == NULL ? 0 : 1,
+	    /* Port enabled/disabled. */
+	    port->state == HUB_PORT_STATE_ENABLED ? 1 : 0,
+	    /* Suspend. */
+	    (port->state == HUB_PORT_STATE_SUSPENDED)
+		|| (port->state == HUB_PORT_STATE_RESUMING) ? 1 : 0,
+	    /* Over-current. */
+	    0,
+	    /* Reset. */
+	    port->state == HUB_PORT_STATE_RESETTING ? 1 : 0,
+	    /* Reserved. */
+	    0, 0, 0)
+
+	    | (MAKE_BYTE(
+	    /* Port power. */
+	    port->state == HUB_PORT_STATE_POWERED_OFF ? 0 : 1,
+	    /* Full-speed device. */
+	    0,
+	    /* Reserved. */
+	    0, 0, 0, 0, 0, 0
+	    )) << 8;
+
+	status |= (port->status_change << 16);
+
+	return status;
+}
+
+uint8_t hub_get_status_change_bitmap(hub_t *hub)
+{
+	uint8_t change_map = 0;
+
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		hub_port_t *port = &hub->ports[i];
+
+		if (port->status_change != 0) {
+			change_map |= (1 << port->index);
+		}
+	}
+
+	return change_map;
+}
+
+
+/*
+ *
+ * STATIC (HELPER) FUNCTIONS
+ *
+ */
+
+static hub_port_t *get_hub_port(hub_t *hub, size_t port)
+{
+	if (port >= HUB_PORT_COUNT) {
+		return NULL;
+	}
+
+	return &hub->ports[port];
+}
+
+static void set_port_status_change(hub_port_t *port,
+    hub_status_change_t change)
+{
+	assert(port != NULL);
+	port->status_change |= change;
+}
+
+static void clear_port_status_change(hub_port_t *port,
+    uint16_t change)
+{
+	assert(port != NULL);
+	port->status_change &= (~change);
+}
+
+struct delay_port_state_change {
+	suseconds_t delay;
+	hub_port_state_t old_state;
+	hub_port_state_t new_state;
+	size_t port;
+	hub_t *hub;
+};
+
+static int set_port_state_delayed_fibril(void *arg)
+{
+	struct delay_port_state_change *change
+	    = (struct delay_port_state_change *) arg;
+
+	async_usleep(change->delay);
+
+	hub_acquire(change->hub);
+
+	hub_port_t *port = get_hub_port(change->hub, change->port);
+	assert(port != NULL);
+
+	if (port->state == change->old_state) {
+		hub_set_port_state(change->hub, change->port,
+		    change->new_state);
+	}
+
+	hub_release(change->hub);
+
+	free(change);
+
+	return EOK;
+}
+
+static void set_port_state_delayed(hub_t *hub, size_t port_index,
+    suseconds_t delay_time_ms,
+    hub_port_state_t old_state, hub_port_state_t new_state)
+{
+	struct delay_port_state_change *change
+	    = malloc(sizeof(struct delay_port_state_change));
+
+	change->hub = hub;
+	change->port = port_index;
+	change->delay = delay_time_ms * 1000;
+	change->old_state = old_state;
+	change->new_state = new_state;
+	fid_t fibril = fibril_create(set_port_state_delayed_fibril, change);
+	if (fibril == 0) {
+		printf("Failed to create fibril\n");
+		free(change);
+		return;
+	}
+	fibril_add_ready(fibril);
+}
+
+
+
+/**
+ * @}
+ */
Index: uspace/drv/vhc/hub/hub.h
===================================================================
--- uspace/drv/vhc/hub/hub.h	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
+++ uspace/drv/vhc/hub/hub.h	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -0,0 +1,104 @@
+/*
+ * 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 usb
+ * @{
+ */
+/** @file
+ * @brief
+ */
+#ifndef VHC_HUB_HUB_H_
+#define VHC_HUB_HUB_H_
+
+#include <fibril_synch.h>
+
+#define HUB_PORT_COUNT 2
+#define BITS2BYTES(bits) (bits ? ((((bits)-1)>>3)+1) : 0)
+
+/** Hub port internal state.
+ * Some states (e.g. port over current) are not covered as they are not
+ * simulated at all.
+ */
+typedef enum {
+	HUB_PORT_STATE_UNKNOWN,
+	HUB_PORT_STATE_NOT_CONFIGURED,
+	HUB_PORT_STATE_POWERED_OFF,
+	HUB_PORT_STATE_DISCONNECTED,
+	HUB_PORT_STATE_DISABLED,
+	HUB_PORT_STATE_RESETTING,
+	HUB_PORT_STATE_ENABLED,
+	HUB_PORT_STATE_SUSPENDED,
+	HUB_PORT_STATE_RESUMING,
+	/* HUB_PORT_STATE_, */
+} hub_port_state_t;
+
+char hub_port_state_to_char(hub_port_state_t);
+
+/** Hub status change mask bits. */
+typedef enum {
+	HUB_STATUS_C_PORT_CONNECTION = (1 << 0),
+	HUB_STATUS_C_PORT_ENABLE = (1 << 1),
+	HUB_STATUS_C_PORT_SUSPEND = (1 << 2),
+	HUB_STATUS_C_PORT_OVER_CURRENT = (1 << 3),
+	HUB_STATUS_C_PORT_RESET = (1 << 4),
+	/* HUB_STATUS_C_ = (1 << ), */
+} hub_status_change_t;
+
+/** Hub port information. */
+typedef struct {
+	void *connected_device;
+	size_t index;
+	hub_port_state_t state;
+	uint16_t status_change;
+} hub_port_t;
+
+/** Hub device type. */
+typedef struct {
+	hub_port_t ports[HUB_PORT_COUNT];
+	void *custom_data;
+	fibril_mutex_t guard;
+} hub_t;
+
+void hub_init(hub_t *);
+size_t hub_connect_device(hub_t *, void *);
+size_t hub_find_device(hub_t *, void *);
+void hub_acquire(hub_t *);
+void hub_release(hub_t *);
+void hub_set_port_state(hub_t *, size_t, hub_port_state_t);
+void hub_set_port_state_all(hub_t *, hub_port_state_t);
+hub_port_state_t hub_get_port_state(hub_t *, size_t);
+void hub_clear_port_status_change(hub_t *, size_t, hub_status_change_t);
+uint16_t hub_get_port_status_change(hub_t *, size_t);
+uint32_t hub_get_port_status(hub_t *, size_t);
+uint8_t hub_get_status_change_bitmap(hub_t *);
+
+
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/vhc/hub/virthub.c
===================================================================
--- uspace/drv/vhc/hub/virthub.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
+++ uspace/drv/vhc/hub/virthub.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -0,0 +1,234 @@
+/*
+ * 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 usb
+ * @{
+ */
+/** @file
+ * @brief
+ */
+#include <usb/classes/classes.h>
+#include <usbvirt/hub.h>
+#include <usbvirt/device.h>
+#include <assert.h>
+#include <errno.h>
+#include <str_error.h>
+#include <stdlib.h>
+#include <driver.h>
+#include <usb/usbdrv.h>
+
+#include "virthub.h"
+#include "hub.h"
+
+
+/** Standard device descriptor. */
+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_HUB,
+	.device_subclass = 0,
+	.device_protocol = 0,
+	.max_packet_size = 64,
+	.configuration_count = 1
+};
+
+/** Standard interface descriptor. */
+usb_standard_interface_descriptor_t std_interface_descriptor = {
+	.length = sizeof(usb_standard_interface_descriptor_t),
+	.descriptor_type = USB_DESCTYPE_INTERFACE,
+	.interface_number = 0,
+	.alternate_setting = 0,
+	.endpoint_count = 1,
+	.interface_class = USB_CLASS_HUB,
+	.interface_subclass = 0,
+	.interface_protocol = 0,
+	.str_interface = 0
+};
+
+hub_descriptor_t hub_descriptor = {
+	.length = sizeof(hub_descriptor_t),
+	.type = USB_DESCTYPE_HUB,
+	.port_count = HUB_PORT_COUNT,
+	.characteristics = 0, 
+	.power_on_warm_up = 50, /* Huh? */
+	.max_current = 100, /* Huh again. */
+	.removable_device = { 0 },
+	.port_power = { 0xFF }
+};
+
+/** Endpoint descriptor. */
+usb_standard_endpoint_descriptor_t endpoint_descriptor = {
+	.length = sizeof(usb_standard_endpoint_descriptor_t),
+	.descriptor_type = USB_DESCTYPE_ENDPOINT,
+	.endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
+	.attributes = USB_TRANSFER_INTERRUPT,
+	.max_packet_size = 8,
+	.poll_interval = 0xFF
+};
+
+/** Standard configuration descriptor. */
+usb_standard_configuration_descriptor_t std_configuration_descriptor = {
+	.length = sizeof(usb_standard_configuration_descriptor_t),
+	.descriptor_type = USB_DESCTYPE_CONFIGURATION,
+	.total_length = 
+		sizeof(usb_standard_configuration_descriptor_t)
+		+ sizeof(std_interface_descriptor)
+		+ sizeof(hub_descriptor)
+		+ sizeof(endpoint_descriptor)
+		,
+	.interface_count = 1,
+	.configuration_number = HUB_CONFIGURATION_ID,
+	.str_configuration = 0,
+	.attributes = 128, /* denotes bus-powered device */
+	.max_power = 50
+};
+
+/** All hub configuration descriptors. */
+static usbvirt_device_configuration_extras_t extra_descriptors[] = {
+	{
+		.data = (uint8_t *) &std_interface_descriptor,
+		.length = sizeof(std_interface_descriptor)
+	},
+	{
+		.data = (uint8_t *) &hub_descriptor,
+		.length = sizeof(hub_descriptor)
+	},
+	{
+		.data = (uint8_t *) &endpoint_descriptor,
+		.length = sizeof(endpoint_descriptor)
+	}
+};
+
+/** Hub configuration. */
+usbvirt_device_configuration_t configuration = {
+	.descriptor = &std_configuration_descriptor,
+	.extra = extra_descriptors,
+	.extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
+};
+
+/** Hub standard descriptors. */
+usbvirt_descriptors_t descriptors = {
+	.device = &std_device_descriptor,
+	.configuration = &configuration,
+	.configuration_count = 1,
+};
+
+int virthub_init(usbvirt_device_t *dev)
+{
+	if (dev == NULL) {
+		return EBADMEM;
+	}
+	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));
+	if (hub == NULL) {
+		return ENOMEM;
+	}
+
+	hub_init(hub);
+	dev->device_data = hub;
+
+	usbvirt_connect_local(dev);
+
+	return EOK;
+}
+
+int virthub_connect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
+{
+	assert(dev != NULL);
+	assert(conn != NULL);
+
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+	size_t port = hub_connect_device(hub, conn);
+	hub_release(hub);
+
+	return port;
+}
+
+int virthub_disconnect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
+{
+	assert(dev != NULL);
+	assert(conn != NULL);
+
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+	hub_release(hub);
+
+	return ENOTSUP;
+}
+
+bool virthub_is_device_enabled(usbvirt_device_t *dev, virtdev_connection_t *conn)
+{
+	assert(dev != NULL);
+	assert(conn != NULL);
+
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+
+	hub_port_state_t state = HUB_PORT_STATE_UNKNOWN;
+	size_t port = hub_find_device(hub, conn);
+	if (port != (size_t) -1) {
+		state = hub_get_port_state(hub, port);
+	}
+	hub_release(hub);
+
+	return state == HUB_PORT_STATE_ENABLED;
+}
+
+void virthub_get_status(usbvirt_device_t *dev, char *status, size_t len)
+{
+	assert(dev != NULL);
+	if (len == 0) {
+		return;
+	}
+
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	char port_status[HUB_PORT_COUNT + 1];
+
+	size_t i;
+	for (i = 0; i < HUB_PORT_COUNT; i++) {
+		port_status[i] = hub_port_state_to_char(
+		    hub_get_port_state(hub, i));
+	}
+	port_status[HUB_PORT_COUNT] = 0;
+
+	snprintf(status, len, "vhub:%s", port_status);
+}
+
+/**
+ * @}
+ */
Index: uspace/drv/vhc/hub/virthub.h
===================================================================
--- uspace/drv/vhc/hub/virthub.h	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
+++ uspace/drv/vhc/hub/virthub.h	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -0,0 +1,85 @@
+/*
+ * 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 usb
+ * @{
+ */
+/** @file
+ * @brief
+ */
+#ifndef VHC_HUB_VIRTHUB_H_
+#define VHC_HUB_VIRTHUB_H_
+
+#include <usbvirt/device.h>
+#include "../devices.h"
+#include "hub.h"
+
+/** Endpoint number for status change pipe. */
+#define HUB_STATUS_CHANGE_PIPE 1
+/** Configuration value for hub configuration. */
+#define HUB_CONFIGURATION_ID 1
+
+
+/** Hub descriptor.
+ */
+typedef struct {
+	/** Size of this descriptor in bytes. */
+	uint8_t length;
+	/** Descriptor type (USB_DESCTYPE_HUB). */
+	uint8_t type;
+	/** Number of downstream ports. */
+	uint8_t port_count;
+	/** Hub characteristics. */
+	uint16_t characteristics;
+	/** Time from power-on to stabilized current.
+	 * Expressed in 2ms unit.
+	 */
+	uint8_t power_on_warm_up;
+	/** Maximum current (in mA). */
+	uint8_t max_current;
+	/** Whether device at given port is removable. */
+	uint8_t removable_device[BITS2BYTES(HUB_PORT_COUNT+1)];
+	/** Port power control.
+	 * This is USB1.0 compatibility field, all bits must be 1.
+	 */
+	uint8_t port_power[BITS2BYTES(HUB_PORT_COUNT+1)];
+} __attribute__ ((packed)) hub_descriptor_t;
+
+extern usbvirt_device_ops_t hub_ops;
+extern hub_descriptor_t hub_descriptor;
+
+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 *);
+void virthub_get_status(usbvirt_device_t *, char *, size_t);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/vhc/hub/virthubops.c
===================================================================
--- uspace/drv/vhc/hub/virthubops.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
+++ uspace/drv/vhc/hub/virthubops.c	(revision 774afaae05c68de66709484e22ef17c90c2b3284)
@@ -0,0 +1,361 @@
+/*
+ * 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 usb
+ * @{
+ */
+/** @file
+ * @brief Virtual USB hub operations.
+ */
+#include <errno.h>
+#include <usb/classes/hub.h>
+#include "virthub.h"
+#include "hub.h"
+
+/** Callback when device changes states. */
+static void on_state_change(struct usbvirt_device *dev,
+    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state)
+{
+	hub_t *hub = (hub_t *)dev->device_data;
+
+	hub_acquire(hub);
+
+	switch (new_state) {
+		case USBVIRT_STATE_CONFIGURED:
+			hub_set_port_state_all(hub, HUB_PORT_STATE_POWERED_OFF);
+			break;
+		case USBVIRT_STATE_ADDRESS:
+			hub_set_port_state_all(hub, HUB_PORT_STATE_NOT_CONFIGURED);
+			break;
+		default:
+			break;
+	}
+
+	hub_release(hub);
+}
+
+/** 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)
+{
+	if (endpoint != HUB_STATUS_CHANGE_PIPE) {
+		return EINVAL;
+	}
+	
+	hub_t *hub = (hub_t *)dev->device_data;
+
+	hub_acquire(hub);
+
+	uint8_t change_map = hub_get_status_change_bitmap(hub);
+		
+	uint8_t *b = (uint8_t *) buffer;
+	if (size > 0) {
+		*b = change_map;
+		*actual_size = 1;
+	}
+	
+	hub_release(hub);
+
+	return EOK;
+}
+
+
+static int req_clear_hub_feature(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	return ENOTSUP;
+}
+
+static int req_clear_port_feature(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	int rc;
+	size_t port = request->index - 1;
+	usb_hub_class_feature_t feature = request->value;
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+
+	hub_port_state_t port_state = hub_get_port_state(hub, port);
+
+	switch (feature) {
+		case USB_HUB_FEATURE_PORT_ENABLE:
+			if ((port_state != HUB_PORT_STATE_NOT_CONFIGURED)
+			    && (port_state != HUB_PORT_STATE_POWERED_OFF)) {
+				hub_set_port_state(hub, port, HUB_PORT_STATE_DISABLED);
+			}
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_PORT_SUSPEND:
+			if (port_state != HUB_PORT_STATE_SUSPENDED) {
+				rc = EOK;
+				break;
+			}
+			hub_set_port_state(hub, port, HUB_PORT_STATE_RESUMING);
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_PORT_POWER:
+			if (port_state != HUB_PORT_STATE_NOT_CONFIGURED) {
+				hub_set_port_state(hub, port, HUB_PORT_STATE_POWERED_OFF);
+			}
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_C_PORT_CONNECTION:
+			hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_CONNECTION);
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_C_PORT_ENABLE:
+			hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_ENABLE);
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_C_PORT_SUSPEND:
+			hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_SUSPEND);
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
+			hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_OVER_CURRENT);
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_C_PORT_RESET:
+			hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_RESET);
+			rc = EOK;
+			break;
+
+		default:
+			rc = ENOTSUP;
+			break;
+	}
+
+	hub_release(hub);
+
+	return rc;
+}
+
+static int req_get_bus_state(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	return ENOTSUP;
+}
+
+static int req_get_descriptor(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	if (request->value_high == USB_DESCTYPE_HUB) {
+		int rc = dev->control_transfer_reply(dev, 0,
+		    &hub_descriptor, hub_descriptor.length);
+
+		return rc;
+	}
+	/* Let the framework handle all the rest. */
+	return EFORWARD;
+}
+
+static int req_get_hub_status(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	uint32_t hub_status = 0;
+
+	return dev->control_transfer_reply(dev, 0,
+	    &hub_status, sizeof(hub_status));
+}
+
+static int req_get_port_status(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+
+	uint32_t status = hub_get_port_status(hub, request->index - 1);
+
+	hub_release(hub);
+
+	return dev->control_transfer_reply(dev, 0, &status, 4);
+}
+
+static int req_set_hub_feature(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	return ENOTSUP;
+}
+
+static int req_set_port_feature(usbvirt_device_t *dev,
+    usb_device_request_setup_packet_t *request,
+    uint8_t *data)
+{
+	int rc;
+	size_t port = request->index - 1;
+	usb_hub_class_feature_t feature = request->value;
+	hub_t *hub = (hub_t *) dev->device_data;
+
+	hub_acquire(hub);
+
+	hub_port_state_t port_state = hub_get_port_state(hub, port);
+
+	switch (feature) {
+		case USB_HUB_FEATURE_PORT_RESET:
+			if (port_state != HUB_PORT_STATE_POWERED_OFF) {
+				hub_set_port_state(hub, port, HUB_PORT_STATE_RESETTING);
+			}
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_PORT_SUSPEND:
+			if (port_state == HUB_PORT_STATE_ENABLED) {
+				hub_set_port_state(hub, port, HUB_PORT_STATE_SUSPENDED);
+			}
+			rc = EOK;
+			break;
+
+		case USB_HUB_FEATURE_PORT_POWER:
+			if (port_state == HUB_PORT_STATE_POWERED_OFF) {
+				hub_set_port_state(hub, port, HUB_PORT_STATE_DISCONNECTED);
+			}
+			rc = EOK;
+			break;
+
+		default:
+			break;
+	}
+
+	hub_release(hub);
+
+	return rc;
+}
+
+
+
+#define CLASS_REQ_IN(recipient) \
+	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(USB_DIRECTION_IN, \
+	USBVIRT_REQUEST_TYPE_CLASS, recipient)
+#define CLASS_REQ_OUT(recipient) \
+	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(USB_DIRECTION_OUT, \
+	USBVIRT_REQUEST_TYPE_CLASS, recipient)
+
+#define REC_OTHER USBVIRT_REQUEST_RECIPIENT_OTHER
+#define REC_DEVICE USBVIRT_REQUEST_RECIPIENT_DEVICE
+#define DIR_IN USB_DIRECTION_IN
+#define DIR_OUT USB_DIRECTION_OUT
+
+#define CLASS_REQ(direction, recipient, req) \
+	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
+	    USBVIRT_REQUEST_TYPE_CLASS, recipient), \
+	.request = req
+
+#define STD_REQ(direction, recipient, req) \
+	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
+	    USBVIRT_REQUEST_TYPE_STANDARD, recipient), \
+	.request = req
+
+/** Hub operations on control endpoint zero. */
+static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
+	{
+		STD_REQ(DIR_IN, REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
+		.name = "GetDescriptor",
+		.callback = req_get_descriptor
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
+		.name = "GetDescriptor",
+		.callback = req_get_descriptor
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
+		.name = "GetPortStatus",
+		.callback = req_get_port_status
+	},
+	{
+		CLASS_REQ(DIR_OUT, REC_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
+		.name = "ClearHubFeature",
+		.callback = req_clear_hub_feature
+	},
+	{
+		CLASS_REQ(DIR_OUT, REC_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
+		.name = "ClearPortFeature",
+		.callback = req_clear_port_feature
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATE),
+		.name = "GetBusState",
+		.callback = req_get_bus_state
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
+		.name = "GetHubDescriptor",
+		.callback = req_get_descriptor
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_DEVICE, USB_HUB_REQUEST_GET_STATUS),
+		.name = "GetHubStatus",
+		.callback = req_get_hub_status
+	},
+	{
+		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
+		.name = "GetPortStatus",
+		.callback = req_get_port_status
+	},
+	{
+		CLASS_REQ(DIR_OUT, REC_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
+		.name = "SetHubFeature",
+		.callback = req_set_hub_feature
+	},
+	{
+		CLASS_REQ(DIR_OUT, REC_OTHER, USB_HUB_REQUEST_SET_FEATURE),
+		.name = "SetPortFeature",
+		.callback = req_set_port_feature
+	},
+	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
+};
+
+
+/** 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,
+};
+
+/**
+ * @}
+ */
Index: pace/drv/vhc/hubintern.h
===================================================================
--- uspace/drv/vhc/hubintern.h	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ 	(revision )
@@ -1,149 +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 usb
- * @{
- */
-/** @file
- * @brief
- */
-#ifndef VHCD_HUBINTERN_H_
-#define VHCD_HUBINTERN_H_
-
-#include "hub.h"
-#include <fibril_synch.h>
-
-/** Endpoint number for status change pipe. */
-#define HUB_STATUS_CHANGE_PIPE 1
-/** Configuration value for hub configuration. */
-#define HUB_CONFIGURATION_ID 1
-
-/** Hub descriptor.
- */
-typedef struct {
-	/** Size of this descriptor in bytes. */
-	uint8_t length;
-	/** Descriptor type (USB_DESCTYPE_HUB). */
-	uint8_t type;
-	/** Number of downstream ports. */
-	uint8_t port_count;
-	/** Hub characteristics. */
-	uint16_t characteristics;
-	/** Time from power-on to stabilized current.
-	 * Expressed in 2ms unit.
-	 */
-	uint8_t power_on_warm_up;
-	/** Maximum current (in mA). */
-	uint8_t max_current;
-	/** Whether device at given port is removable. */
-	uint8_t removable_device[BITS2BYTES(HUB_PORT_COUNT+1)];
-	/** Port power control.
-	 * This is USB1.0 compatibility field, all bits must be 1.
-	 */
-	uint8_t port_power[BITS2BYTES(HUB_PORT_COUNT+1)];
-} __attribute__ ((packed)) hub_descriptor_t;
-
-/** Hub port internal state.
- * Some states (e.g. port over current) are not covered as they are not
- * simulated at all.
- */
-typedef enum {
-	HUB_PORT_STATE_NOT_CONFIGURED,
-	HUB_PORT_STATE_POWERED_OFF,
-	HUB_PORT_STATE_DISCONNECTED,
-	HUB_PORT_STATE_DISABLED,
-	HUB_PORT_STATE_RESETTING,
-	HUB_PORT_STATE_ENABLED,
-	HUB_PORT_STATE_SUSPENDED,
-	HUB_PORT_STATE_RESUMING,
-	/* HUB_PORT_STATE_, */
-} hub_port_state_t;
-
-/** Convert hub port state to a char. */
-static inline char hub_port_state_as_char(hub_port_state_t state) {
-	switch (state) {
-		case HUB_PORT_STATE_NOT_CONFIGURED:
-			return '-';
-		case HUB_PORT_STATE_POWERED_OFF:
-			return 'O';
-		case HUB_PORT_STATE_DISCONNECTED:
-			return 'X';
-		case HUB_PORT_STATE_DISABLED:
-			return 'D';
-		case HUB_PORT_STATE_RESETTING:
-			return 'R';
-		case HUB_PORT_STATE_ENABLED:
-			return 'E';
-		case HUB_PORT_STATE_SUSPENDED:
-			return 'S';
-		case HUB_PORT_STATE_RESUMING:
-			return 'F';
-		default:
-			return '?';
-	}
-}
-
-/** Hub status change mask bits. */
-typedef enum {
-	HUB_STATUS_C_PORT_CONNECTION = (1 << 0),
-	HUB_STATUS_C_PORT_ENABLE = (1 << 1),
-	HUB_STATUS_C_PORT_SUSPEND = (1 << 2),
-	HUB_STATUS_C_PORT_OVER_CURRENT = (1 << 3),
-	HUB_STATUS_C_PORT_RESET = (1 << 4),
-	/* HUB_STATUS_C_ = (1 << ), */
-} hub_status_change_t;
-
-/** Hub port information. */
-typedef struct {
-	virtdev_connection_t *device;
-	int index;
-	hub_port_state_t state;
-	uint16_t status_change;
-	fibril_mutex_t guard;
-} hub_port_t;
-
-/** Hub device type. */
-typedef struct {
-	hub_port_t ports[HUB_PORT_COUNT];
-} hub_device_t;
-
-extern hub_device_t hub_dev;
-
-extern hub_descriptor_t hub_descriptor;
-
-extern usbvirt_device_ops_t hub_ops;
-
-void clear_port_status_change(hub_port_t *, uint16_t);
-void set_port_status_change(hub_port_t *, uint16_t);
-void set_port_status_change_nl(hub_port_t *, uint16_t);
-
-
-#endif
-/**
- * @}
- */
Index: pace/drv/vhc/hubops.c
===================================================================
--- uspace/drv/vhc/hubops.c	(revision 1840e0de00f48cce35b6ae321d0f7b24ecba4aa1)
+++ 	(revision )
@@ -1,597 +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 usb
- * @{
- */
-/** @file
- * @brief Virtual USB hub operations.
- */
-#include <usb/classes/classes.h>
-#include <usb/classes/hub.h>
-#include <usbvirt/hub.h>
-#include <usbvirt/device.h>
-#include <errno.h>
-
-#include "vhcd.h"
-#include "hub.h"
-#include "hubintern.h"
-
-/** Produce a byte from bit values.
- */
-#define MAKE_BYTE(b0, b1, b2, b3, b4, b5, b6, b7) \
-	(( \
-		((b0) << 0) \
-		| ((b1) << 1) \
-		| ((b2) << 2) \
-		| ((b3) << 3) \
-		| ((b4) << 4) \
-		| ((b5) << 5) \
-		| ((b6) << 6) \
-		| ((b7) << 7) \
-	))
-
-static int on_get_descriptor(struct usbvirt_device *dev,
-    usb_device_request_setup_packet_t *request, uint8_t *data);
-static int on_data_request(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *actual_size);
-static void set_port_state(hub_port_t *, hub_port_state_t);
-static void clear_port_status_change_nl(hub_port_t *, uint16_t);
-static void set_port_state_nl(hub_port_t *, hub_port_state_t);
-static int get_port_status(uint16_t portindex);
-
-/** Callback for GET_DESCRIPTOR request. */
-static int on_get_descriptor(struct usbvirt_device *dev,
-    usb_device_request_setup_packet_t *request, uint8_t *data)
-{
-	if (request->value_high == USB_DESCTYPE_HUB) {
-		int rc = dev->control_transfer_reply(dev, 0,
-		    &hub_descriptor, hub_descriptor.length);
-		
-		return rc;
-	}
-	/* Let the framework handle all the rest. */
-	return EFORWARD;
-}
-
-static void change_all_ports_state(hub_device_t *hub, hub_port_state_t state)
-{
-	size_t i;
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_port_t *port = &hub->ports[i];
-		set_port_state(port, state);
-	}
-}
-
-/** Callback when device changes states. */
-static void on_state_change(struct usbvirt_device *dev,
-    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state)
-{
-	switch (new_state) {
-		case USBVIRT_STATE_CONFIGURED:
-			change_all_ports_state(&hub_dev,
-			    HUB_PORT_STATE_POWERED_OFF);
-			break;
-		case USBVIRT_STATE_ADDRESS:
-			change_all_ports_state(&hub_dev,
-			    HUB_PORT_STATE_NOT_CONFIGURED);
-			break;
-		default:
-			break;
-	}
-}
-
-struct delay_port_state_change {
-	suseconds_t delay;
-	hub_port_state_t old_state;
-	hub_port_state_t new_state;
-	hub_port_t *port;
-};
-
-static int set_port_state_delayed_fibril(void *arg)
-{
-	struct delay_port_state_change *change
-	    = (struct delay_port_state_change *) arg;
-	
-	async_usleep(change->delay);
-	
-	fibril_mutex_lock(&change->port->guard);
-	if (change->port->state == change->old_state) {
-		set_port_state_nl(change->port, change->new_state);
-	}
-	fibril_mutex_unlock(&change->port->guard);
-	
-	free(change);
-	
-	return EOK;
-}
-
-static void set_port_state_delayed(hub_port_t *port,
-    suseconds_t delay_time,
-    hub_port_state_t old_state, hub_port_state_t new_state)
-{
-	struct delay_port_state_change *change
-	    = malloc(sizeof(struct delay_port_state_change));
-	change->port = port;
-	change->delay = delay_time;
-	change->old_state = old_state;
-	change->new_state = new_state;
-	fid_t fibril = fibril_create(set_port_state_delayed_fibril, change);
-	if (fibril == 0) {
-		printf("Failed to create fibril\n");
-		return;
-	}
-	fibril_add_ready(fibril);
-}
-
-/** Change port status and updates status change status fields.
- */
-void set_port_state(hub_port_t *port, hub_port_state_t state)
-{
-	fibril_mutex_lock(&port->guard);
-	set_port_state_nl(port, state);
-	fibril_mutex_unlock(&port->guard);
-}
-
-void set_port_state_nl(hub_port_t *port, hub_port_state_t state)
-{
-
-	dprintf(2, "setting port %d state to %d (%c) from %c (change=%u)",
-	    port->index,
-	    state, hub_port_state_as_char(state),
-	    hub_port_state_as_char(port->state),
-	    (unsigned int) port->status_change);
-	
-	if (state == HUB_PORT_STATE_POWERED_OFF) {
-		clear_port_status_change_nl(port, HUB_STATUS_C_PORT_CONNECTION);
-		clear_port_status_change_nl(port, HUB_STATUS_C_PORT_ENABLE);
-		clear_port_status_change_nl(port, HUB_STATUS_C_PORT_RESET);
-	}
-	if (state == HUB_PORT_STATE_RESUMING) {
-		set_port_state_delayed(port, 10*1000,
-		    HUB_PORT_STATE_RESUMING, HUB_PORT_STATE_ENABLED);
-	}
-	if (state == HUB_PORT_STATE_RESETTING) {
-		set_port_state_delayed(port, 10*1000,
-		    HUB_PORT_STATE_RESETTING, HUB_PORT_STATE_ENABLED);
-	}
-	if ((port->state == HUB_PORT_STATE_RESETTING)
-	    && (state == HUB_PORT_STATE_ENABLED)) {
-		set_port_status_change_nl(port, HUB_STATUS_C_PORT_RESET);
-	}
-	
-	port->state = state;
-}
-
-/** Get access to a port or return with EINVAL. */
-#define _GET_PORT(portvar, index) \
-	do { \
-		if (virthub_dev.state != USBVIRT_STATE_CONFIGURED) { \
-			return EINVAL; \
-		} \
-		if (((index) == 0) || ((index) > HUB_PORT_COUNT)) { \
-			return EINVAL; \
-		} \
-	} while (false); \
-	hub_port_t *portvar = &hub_dev.ports[index-1]
-
-
-static int clear_hub_feature(uint16_t feature)
-{
-	return ENOTSUP;
-}
-
-static int clear_port_feature(uint16_t feature, uint16_t portindex)
-{	
-	_GET_PORT(port, portindex);
-	
-	fibril_mutex_lock(&port->guard);
-	int rc = ENOTSUP;
-
-	switch (feature) {
-		case USB_HUB_FEATURE_PORT_ENABLE:
-			if ((port->state != HUB_PORT_STATE_NOT_CONFIGURED)
-			    && (port->state != HUB_PORT_STATE_POWERED_OFF)) {
-				set_port_state_nl(port, HUB_PORT_STATE_DISABLED);
-			}
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_PORT_SUSPEND:
-			if (port->state != HUB_PORT_STATE_SUSPENDED) {
-				rc = EOK;
-				break;
-			}
-			set_port_state_nl(port, HUB_PORT_STATE_RESUMING);
-			rc = EOK;
-			break;
-			
-		case USB_HUB_FEATURE_PORT_POWER:
-			if (port->state != HUB_PORT_STATE_NOT_CONFIGURED) {
-				set_port_state_nl(port, HUB_PORT_STATE_POWERED_OFF);
-			}
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_C_PORT_CONNECTION:
-			clear_port_status_change_nl(port, HUB_STATUS_C_PORT_CONNECTION);
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_C_PORT_ENABLE:
-			clear_port_status_change_nl(port, HUB_STATUS_C_PORT_ENABLE);
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_C_PORT_SUSPEND:
-			clear_port_status_change_nl(port, HUB_STATUS_C_PORT_SUSPEND);
-			rc = EOK;
-			break;
-			
-		case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
-			clear_port_status_change_nl(port, HUB_STATUS_C_PORT_OVER_CURRENT);
-			rc = EOK;
-			break;
-
-		case USB_HUB_FEATURE_C_PORT_RESET:
-			clear_port_status_change_nl(port, HUB_STATUS_C_PORT_RESET);
-			rc = EOK;
-			break;
-	}
-	
-	fibril_mutex_unlock(&port->guard);
-
-	return rc;
-}
-
-static int get_bus_state(uint16_t portindex)
-{
-	return ENOTSUP;
-}
-
-static int get_hub_descriptor(struct usbvirt_device *dev,
-    uint8_t descriptor_index,
-    uint8_t descriptor_type, uint16_t length)
-{
-	if (descriptor_type == USB_DESCTYPE_HUB) {
-		int rc = dev->control_transfer_reply(dev, 0,
-		    &hub_descriptor, hub_descriptor.length);
-
-		return rc;
-
-	}
-
-	return ENOTSUP;
-}
-
-static int get_hub_status(void)
-{
-	uint32_t hub_status = 0;
-	
-	return virthub_dev.control_transfer_reply(&virthub_dev, 0,
-	    &hub_status, 4);
-}
-
-int get_port_status(uint16_t portindex)
-{
-	_GET_PORT(port, portindex);
-	
-	fibril_mutex_lock(&port->guard);
-
-	uint32_t status;
-	status = MAKE_BYTE(
-	    /* Current connect status. */
-	    port->device == NULL ? 0 : 1,
-	    /* Port enabled/disabled. */
-	    port->state == HUB_PORT_STATE_ENABLED ? 1 : 0,
-	    /* Suspend. */
-	    (port->state == HUB_PORT_STATE_SUSPENDED)
-	        || (port->state == HUB_PORT_STATE_RESUMING) ? 1 : 0,
-	    /* Over-current. */
-	    0,
-	    /* Reset. */
-	    port->state == HUB_PORT_STATE_RESETTING ? 1 : 0,
-	    /* Reserved. */
-	    0, 0, 0)
-	    
-	    | (MAKE_BYTE(
-	    /* Port power. */
-	    port->state == HUB_PORT_STATE_POWERED_OFF ? 0 : 1,
-	    /* Full-speed device. */
-	    0,
-	    /* Reserved. */
-	    0, 0, 0, 0, 0, 0
-	    )) << 8;
-	    
-	status |= (port->status_change << 16);
-	
-	fibril_mutex_unlock(&port->guard);
-
-	dprintf(2, "GetPortStatus(port=%d, status=%u)\n", (int)portindex,
-	    (unsigned int) status);
-	return virthub_dev.control_transfer_reply(&virthub_dev, 0, &status, 4);
-}
-
-
-static int set_hub_feature(uint16_t feature)
-{
-	return ENOTSUP;
-}
-
-static int set_port_feature(uint16_t feature, uint16_t portindex)
-{
-	_GET_PORT(port, portindex);
-	
-	fibril_mutex_lock(&port->guard);
-
-	int rc = ENOTSUP;
-
-	switch (feature) {
-		case USB_HUB_FEATURE_PORT_RESET:
-			if (port->state != HUB_PORT_STATE_POWERED_OFF) {
-				set_port_state_nl(port, HUB_PORT_STATE_RESETTING);
-			}
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_PORT_SUSPEND:
-			if (port->state == HUB_PORT_STATE_ENABLED) {
-				set_port_state_nl(port, HUB_PORT_STATE_SUSPENDED);
-			}
-			rc = EOK;
-			break;
-		
-		case USB_HUB_FEATURE_PORT_POWER:
-			if (port->state == HUB_PORT_STATE_POWERED_OFF) {
-				set_port_state_nl(port, HUB_PORT_STATE_DISCONNECTED);
-			}
-			rc = EOK;
-			break;
-	}
-
-	fibril_mutex_unlock(&port->guard);
-	return rc;
-}
-
-#undef _GET_PORT
-
-
-void clear_port_status_change_nl(hub_port_t *port, uint16_t change)
-{
-	port->status_change &= (~change);
-	dprintf(2, "cleared port %d status change %d (%u)", port->index,
-	    (int)change, (unsigned int) port->status_change);
-}
-
-void set_port_status_change_nl(hub_port_t *port, uint16_t change)
-{
-	port->status_change |= change;
-	dprintf(2, "set port %d status change %d (%u)", port->index,
-	    (int)change, (unsigned int) port->status_change);
-
-}
-
-void clear_port_status_change(hub_port_t *port, uint16_t change)
-{
-	fibril_mutex_lock(&port->guard);
-	clear_port_status_change_nl(port, change);
-	fibril_mutex_unlock(&port->guard);
-}
-
-void set_port_status_change(hub_port_t *port, uint16_t change)
-{
-	fibril_mutex_lock(&port->guard);
-	set_port_status_change_nl(port, change);
-	fibril_mutex_unlock(&port->guard);
-}
-
-/** Callback for data request. */
-static int on_data_request(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *actual_size)
-{
-	if (endpoint != HUB_STATUS_CHANGE_PIPE) {
-		return EINVAL;
-	}
-	
-	uint8_t change_map = 0;
-	
-	size_t i;
-	for (i = 0; i < HUB_PORT_COUNT; i++) {
-		hub_port_t *port = &hub_dev.ports[i];
-		
-		fibril_mutex_lock(&port->guard);
-		if (port->status_change != 0) {
-			change_map |= (1 << (i + 1));
-		}
-		fibril_mutex_unlock(&port->guard);
-	}
-	
-	uint8_t *b = (uint8_t *) buffer;
-	if (size > 0) {
-		*b = change_map;
-		*actual_size = 1;
-	}
-	
-	return EOK;
-}
-
-
-
-static int req_clear_hub_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return clear_hub_feature(request->value);
-}
-
-static int req_clear_port_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return clear_port_feature(request->value, request->index);
-}
-
-static int req_get_bus_state(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return get_bus_state(request->index);
-}
-
-static int req_get_hub_descriptor(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return get_hub_descriptor(dev, request->value_low,
-	    request->value_high, request->length);
-}
-
-static int req_get_hub_status(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return get_hub_status();
-}
-
-static int req_get_port_status(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return get_port_status(request->index);
-}
-
-static int req_set_hub_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return set_hub_feature(request->value);
-}
-
-static int req_set_port_feature(usbvirt_device_t *dev,
-    usb_device_request_setup_packet_t *request,
-    uint8_t *data)
-{
-	return set_port_feature(request->value, request->index);
-}
-
-#define CLASS_REQ_IN(recipient) \
-	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(USB_DIRECTION_IN, \
-	USBVIRT_REQUEST_TYPE_CLASS, recipient)
-#define CLASS_REQ_OUT(recipient) \
-	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(USB_DIRECTION_OUT, \
-	USBVIRT_REQUEST_TYPE_CLASS, recipient)
-
-#define REC_OTHER USBVIRT_REQUEST_RECIPIENT_OTHER
-#define REC_DEVICE USBVIRT_REQUEST_RECIPIENT_DEVICE
-#define DIR_IN USB_DIRECTION_IN
-#define DIR_OUT USB_DIRECTION_OUT
-
-#define CLASS_REQ(direction, recipient, req) \
-	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_CLASS, recipient), \
-	.request = req
-
-#define STD_REQ(direction, recipient, req) \
-	.request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_STANDARD, recipient), \
-	.request = req
-
-/** Hub operations on control endpoint zero. */
-static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
-	{
-		STD_REQ(DIR_IN, REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
-		.name = "GetDescriptor",
-		.callback = on_get_descriptor
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
-		.name = "GetDescriptor",
-		.callback = on_get_descriptor
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
-		.name = "GetPortStatus",
-		.callback = req_get_port_status
-	},
-	{
-		CLASS_REQ(DIR_OUT, REC_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
-		.name = "ClearHubFeature",
-		.callback = req_clear_hub_feature
-	},
-	{
-		CLASS_REQ(DIR_OUT, REC_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
-		.name = "ClearPortFeature",
-		.callback = req_clear_port_feature
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATE),
-		.name = "GetBusState",
-		.callback = req_get_bus_state
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
-		.name = "GetHubDescriptor",
-		.callback = req_get_hub_descriptor
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_DEVICE, USB_HUB_REQUEST_GET_STATUS),
-		.name = "GetHubStatus",
-		.callback = req_get_hub_status
-	},
-	{
-		CLASS_REQ(DIR_IN, REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
-		.name = "GetPortStatus",
-		.callback = req_get_port_status
-	},
-	{
-		CLASS_REQ(DIR_OUT, REC_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
-		.name = "SetHubFeature",
-		.callback = req_set_hub_feature
-	},
-	{
-		CLASS_REQ(DIR_OUT, REC_OTHER, USB_HUB_REQUEST_SET_FEATURE),
-		.name = "SetPortFeature",
-		.callback = req_set_port_feature
-	},
-	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
-};
-
-
-/** Hub operations. */
-usbvirt_device_ops_t hub_ops = {
-	.control_transfer_handlers = endpoint_zero_handlers,
-	.on_data = NULL,
-	.on_data_request = on_data_request,
-	.on_state_change = on_state_change,
-};
-
-/**
- * @}
- */
