Index: uspace/lib/usb/src/hcdhubd.c
===================================================================
--- uspace/lib/usb/src/hcdhubd.c	(revision e4dbfda81ffba0222ededb046f47d6be4a1ebfd4)
+++ uspace/lib/usb/src/hcdhubd.c	(revision dac43bebb7b3d618aaeb7d50f1ebe54ea0b6918a)
@@ -31,31 +31,16 @@
  */
 /** @file
- * @brief HC driver and hub driver (implementation).
+ * @brief Common stuff for both HC driver and hub driver.
  */
 #include <usb/hcdhubd.h>
 #include <usb/devreq.h>
 #include <usbhc_iface.h>
+#include <usb/descriptor.h>
 #include <driver.h>
 #include <bool.h>
 #include <errno.h>
-
-#define USB_HUB_DEVICE_NAME "usbhub"
-
-/** List of handled host controllers. */
-static LIST_INITIALIZE(hc_list);
-
-/** Our HC driver. */
-static usb_hc_driver_t *hc_driver = NULL;
-
-static usbhc_iface_t usb_interface = {
-	.interrupt_out = NULL,
-	.interrupt_in = NULL
-};
-
-static device_ops_t usb_device_ops = {
-	.interfaces[USBHC_DEV_IFACE] = &usb_interface
-};
-
-static void set_hub_address(usb_hc_device_t *hc, usb_address_t address);
+#include <usb/classes/hub.h>
+
+#include "hcdhubd_private.h"
 
 /** Callback when new device is detected and must be handled by this driver.
@@ -64,10 +49,5 @@
  * @return Error code.
  */
-static int add_device(device_t *dev)
-{
-	/*
-	 * FIXME: use some magic to determine whether hub or another HC
-	 * was connected.
-	 */
+static int add_device(device_t *dev) {
 	bool is_hc = str_cmp(dev->name, USB_HUB_DEVICE_NAME) != 0;
 	printf("%s: add_device(name=\"%s\")\n", hc_driver->name, dev->name);
@@ -77,141 +57,12 @@
 		 * We are the HC itself.
 		 */
-		usb_hc_device_t *hc_dev = malloc(sizeof(usb_hc_device_t));
-		list_initialize(&hc_dev->link);
-		hc_dev->transfer_ops = NULL;
-
-		hc_dev->generic = dev;
-		dev->ops = &usb_device_ops;
-		hc_dev->generic->driver_data = hc_dev;
-
-		int rc = hc_driver->add_hc(hc_dev);
-		if (rc != EOK) {
-			free(hc_dev);
-			return rc;
-		}
-
+		return usb_add_hc_device(dev);
+	} else {
 		/*
-		 * FIXME: The following line causes devman to hang.
-		 * Will investigate later why.
-		 */
-		// add_device_to_class(dev, "usbhc");
-
-		list_append(&hc_dev->link, &hc_list);
-
-		return EOK;
-	} else {
-		usb_hc_device_t *hc = list_get_instance(hc_list.next, usb_hc_device_t, link);
-		set_hub_address(hc, 5);
-
-		/*
-		 * We are some (probably deeply nested) hub.
+		 * We are some (maybe deeply nested) hub.
 		 * Thus, assign our own operations and explore already
 		 * connected devices.
 		 */
-
-		return ENOTSUP;
-	}
-}
-
-/** Sample usage of usb_hc_async functions.
- * This function sets hub address using standard SET_ADDRESS request.
- *
- * @warning This function shall be removed once you are familiar with
- * the usb_hc_ API.
- *
- * @param hc Host controller the hub belongs to.
- * @param address New hub address.
- */
-static void set_hub_address(usb_hc_device_t *hc, usb_address_t address)
-{
-	printf("%s: setting hub address to %d\n", hc->generic->name, address);
-	usb_target_t target = {0, 0};
-	usb_handle_t handle;
-	int rc;
-
-	usb_device_request_setup_packet_t setup_packet = {
-		.request_type = 0,
-		.request = USB_DEVREQ_SET_ADDRESS,
-		.index = 0,
-		.length = 0,
-	};
-	setup_packet.value = address;
-
-	rc = usb_hc_async_control_write_setup(hc, target,
-	    &setup_packet, sizeof(setup_packet), &handle);
-	if (rc != EOK) {
-		return;
-	}
-
-	rc = usb_hc_async_wait_for(handle);
-	if (rc != EOK) {
-		return;
-	}
-
-	rc = usb_hc_async_control_write_status(hc, target, &handle);
-	if (rc != EOK) {
-		return;
-	}
-
-	rc = usb_hc_async_wait_for(handle);
-	if (rc != EOK) {
-		return;
-	}
-
-	printf("%s: hub address changed\n", hc->generic->name);
-}
-
-/** Check changes on all known hubs.
- */
-static void check_hub_changes(void)
-{
-	/*
-	 * Iterate through all HCs.
-	 */
-	link_t *link_hc;
-	for (link_hc = hc_list.next;
-	    link_hc != &hc_list;
-	    link_hc = link_hc->next) {
-		usb_hc_device_t *hc = list_get_instance(link_hc,
-		    usb_hc_device_t, link);
-		/*
-		 * Iterate through all their hubs.
-		 */
-		link_t *link_hub;
-		for (link_hub = hc->hubs.next;
-		    link_hub != &hc->hubs;
-		    link_hub = link_hub->next) {
-			usb_hcd_hub_info_t *hub = list_get_instance(link_hub,
-			    usb_hcd_hub_info_t, link);
-
-			/*
-			 * Check status change pipe of this hub.
-			 */
-			usb_target_t target = {
-				.address = hub->device->address,
-				.endpoint = 1
-			};
-
-			// FIXME: count properly
-			size_t byte_length = (hub->port_count / 8) + 1;
-
-			void *change_bitmap = malloc(byte_length);
-			size_t actual_size;
-			usb_handle_t handle;
-
-			/*
-			 * Send the request.
-			 * FIXME: check returned value for possible errors
-			 */
-			usb_hc_async_interrupt_in(hc, target,
-			    change_bitmap, byte_length, &actual_size,
-			    &handle);
-
-			usb_hc_async_wait_for(handle);
-
-			/*
-			 * TODO: handle the changes.
-			 */
-		}
+		return usb_add_hub_device(dev);
 	}
 }
@@ -234,15 +85,7 @@
  * @return Error code.
  */
-int usb_hcd_main(usb_hc_driver_t *hc)
-{
+int usb_hcd_main(usb_hc_driver_t *hc) {
 	hc_driver = hc;
 	hc_driver_generic.name = hc->name;
-
-	/*
-	 * Launch here fibril that will periodically check all
-	 * attached hubs for status change.
-	 * WARN: This call will effectively do nothing.
-	 */
-	check_hub_changes();
 
 	/*
