Index: uspace/drv/ohci/root_hub.c
===================================================================
--- uspace/drv/ohci/root_hub.c	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/ohci/root_hub.c	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -47,20 +47,19 @@
  *	standart device descriptor for ohci root hub
  */
-static const usb_standard_device_descriptor_t ohci_rh_device_descriptor =
-{
-		.configuration_count = 1,
-		.descriptor_type = USB_DESCTYPE_DEVICE,
-		.device_class = USB_CLASS_HUB,
-		.device_protocol = 0,
-		.device_subclass = 0,
-		.device_version = 0,
-		.length = sizeof(usb_standard_device_descriptor_t),
-		/// \TODO this value is guessed
-		.max_packet_size = 8,
-		.vendor_id = 0x16db,
-		.product_id = 0x0001,
-		/// \TODO these values migt be different
-		.str_serial_number = 0,
-		.usb_spec_version = 0x110,
+static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
+	.configuration_count = 1,
+	.descriptor_type = USB_DESCTYPE_DEVICE,
+	.device_class = USB_CLASS_HUB,
+	.device_protocol = 0,
+	.device_subclass = 0,
+	.device_version = 0,
+	.length = sizeof (usb_standard_device_descriptor_t),
+	/// \TODO this value is guessed
+	.max_packet_size = 8,
+	.vendor_id = 0x16db,
+	.product_id = 0x0001,
+	/// \TODO these values migt be different
+	.str_serial_number = 0,
+	.usb_spec_version = 0x110,
 };
 
@@ -69,12 +68,11 @@
  * for ohci root hubs
  */
-static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor =
-{
+static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
 	/// \TODO some values are default or guessed
-	.attributes = 1<<7,
+	.attributes = 1 << 7,
 	.configuration_number = 1,
 	.descriptor_type = USB_DESCTYPE_CONFIGURATION,
 	.interface_count = 1,
-	.length = sizeof(usb_standard_configuration_descriptor_t),
+	.length = sizeof (usb_standard_configuration_descriptor_t),
 	.max_power = 100,
 	.str_configuration = 0,
@@ -84,6 +82,5 @@
  * standart ohci root hub interface descriptor
  */
-static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor =
-{
+static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
 	.alternate_setting = 0,
 	.descriptor_type = USB_DESCTYPE_INTERFACE,
@@ -94,5 +91,5 @@
 	.interface_protocol = 0,
 	.interface_subclass = 0,
-	.length = sizeof(usb_standard_interface_descriptor_t),
+	.length = sizeof (usb_standard_interface_descriptor_t),
 	.str_interface = 0,
 };
@@ -101,10 +98,9 @@
  * standart ohci root hub endpoint descriptor
  */
-static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor =
-{
+static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
 	.attributes = USB_TRANSFER_INTERRUPT,
 	.descriptor_type = USB_DESCTYPE_ENDPOINT,
-	.endpoint_address = 1 + (1<<7),
-	.length = sizeof(usb_standard_endpoint_descriptor_t),
+	.endpoint_address = 1 + (1 << 7),
+	.length = sizeof (usb_standard_endpoint_descriptor_t),
 	.max_packet_size = 8,
 	.poll_interval = 255,
@@ -112,6 +108,6 @@
 
 static const uint32_t hub_clear_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) +
-	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
+	(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) |
+(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
 static const uint32_t hub_clear_feature_by_writing_one_mask =
@@ -121,28 +117,149 @@
 	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
-	
+
 static const uint32_t hub_set_feature_direct_mask =
 	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
 static const uint32_t port_set_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_PORT_ENABLE) +
-	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_PORT_RESET) +
-	(1 << USB_HUB_FEATURE_PORT_POWER);
+	(1 << USB_HUB_FEATURE_PORT_ENABLE) |
+(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_PORT_RESET) |
+(1 << USB_HUB_FEATURE_PORT_POWER);
 
 static const uint32_t port_clear_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_PORT_CONNECTION) +
-	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) +
-	(1 << USB_HUB_FEATURE_PORT_POWER) +
-	(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) +
-	(1 << USB_HUB_FEATURE_C_PORT_ENABLE) +
-	(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) +
-	(1 << USB_HUB_FEATURE_C_PORT_RESET);
-//note that USB_HUB_FEATURE_PORT_POWER bit is translated into USB_HUB_FEATURE_PORT_LOW_SPEED
-
-
-
+	(1 << USB_HUB_FEATURE_PORT_CONNECTION) |
+(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) |
+(1 << USB_HUB_FEATURE_PORT_POWER) |
+(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) |
+(1 << USB_HUB_FEATURE_C_PORT_ENABLE) |
+(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
+(1 << USB_HUB_FEATURE_C_PORT_RESET);
+//note that USB_HUB_FEATURE_PORT_POWER bit is translated into
+//USB_HUB_FEATURE_PORT_LOW_SPEED
+
+static const uint32_t port_status_change_mask =
+(1<< USB_HUB_FEATURE_C_PORT_CONNECTION) |
+(1<< USB_HUB_FEATURE_C_PORT_ENABLE) |
+(1<< USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
+(1<< USB_HUB_FEATURE_C_PORT_RESET) |
+(1<< USB_HUB_FEATURE_C_PORT_SUSPEND);
+
+
+static void usb_create_serialized_hub_descriptor(rh_t *instance,
+	uint8_t ** out_result,
+	size_t * out_size);
+
+static void rh_init_descriptors(rh_t *instance);
+
+static int process_get_port_status_request(rh_t *instance, uint16_t port,
+	usb_transfer_batch_t * request);
+
+static int process_get_hub_status_request(rh_t *instance,
+	usb_transfer_batch_t * request);
+
+static int process_get_status_request(rh_t *instance,
+	usb_transfer_batch_t * request);
+
+static void create_interrupt_mask(rh_t *instance, void ** buffer,
+	size_t * buffer_size);
+
+static int process_get_descriptor_request(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_get_configuration_request(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
+
+static int process_hub_feature_clear_request(rh_t *instance,
+	uint16_t feature);
+
+static int process_port_feature_set_request(rh_t *instance,
+	uint16_t feature, uint16_t port);
+
+static int process_port_feature_clear_request(rh_t *instance,
+	uint16_t feature, uint16_t port);
+
+static int process_address_set_request(rh_t *instance,
+	uint16_t address);
+
+static int process_request_with_output(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_request_with_input(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_request_without_data(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
+
+
+
+
+
+/** Root hub initialization
+ * @return Error code.
+ */
+int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs) {
+	assert(instance);
+	//instance->address = -1;
+	instance->registers = regs;
+	instance->device = dev;
+	instance->port_count = instance->registers->rh_desc_a & 0xff;
+	rh_init_descriptors(instance);
+	// set port power mode to no-power-switching
+	instance->registers->rh_desc_a =
+		instance->registers->rh_desc_a | (1<<9);
+
+	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
+
+	//start generic usb hub driver
+
+	/* TODO: implement */
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+
+/**
+ * process root hub request
+ *
+ * @param instance root hub instance
+ * @param request structure containing both request and response information
+ * @return error code
+ */
+int rh_request(rh_t *instance, usb_transfer_batch_t *request) {
+	assert(instance);
+	assert(request);
+	int opResult;
+	if (request->transfer_type == USB_TRANSFER_CONTROL) {
+		usb_log_info("Root hub got CONTROL packet\n");
+		opResult = process_ctrl_request(instance, request);
+	} else if (request->transfer_type == USB_TRANSFER_INTERRUPT) {
+		usb_log_info("Root hub got INTERRUPT packet\n");
+		void * buffer;
+		create_interrupt_mask(instance, &buffer,
+			&(request->transfered_size));
+		memcpy(request->transport_buffer, buffer,
+			request->transfered_size);
+		opResult = EOK;
+	} else {
+		opResult = EINVAL;
+	}
+	usb_transfer_batch_finish(request, opResult);
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+
+void rh_interrupt(rh_t *instance) {
+	usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
+		"interrupts, am I?\n");
+	/* TODO: implement? */
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -157,14 +274,14 @@
  */
 static void usb_create_serialized_hub_descriptor(rh_t *instance,
-		uint8_t ** out_result,
-		size_t * out_size) {
+	uint8_t ** out_result,
+	size_t * out_size) {
 	//base size
 	size_t size = 7;
 	//variable size according to port count
 	size_t var_size = instance->port_count / 8 +
-			((instance->port_count % 8 > 0) ? 1 : 0);
+		((instance->port_count % 8 > 0) ? 1 : 0);
 	size += 2 * var_size;
 	uint8_t * result = (uint8_t*) malloc(size);
-	bzero(result,size);
+	bzero(result, size);
 	//size
 	result[0] = size;
@@ -174,9 +291,9 @@
 	uint32_t hub_desc_reg = instance->registers->rh_desc_a;
 	result[3] =
-			((hub_desc_reg >> 8) %2) +
-			(((hub_desc_reg >> 9) %2) << 1) +
-			(((hub_desc_reg >> 10) %2) << 2) +
-			(((hub_desc_reg >> 11) %2) << 3) +
-			(((hub_desc_reg >> 12) %2) << 4);
+		((hub_desc_reg >> 8) % 2) +
+		(((hub_desc_reg >> 9) % 2) << 1) +
+		(((hub_desc_reg >> 10) % 2) << 2) +
+		(((hub_desc_reg >> 11) % 2) << 3) +
+		(((hub_desc_reg >> 12) % 2) << 4);
 	result[4] = 0;
 	result[5] = /*descriptor->pwr_on_2_good_time*/ 50;
@@ -185,6 +302,8 @@
 	int port;
 	for (port = 1; port <= instance->port_count; ++port) {
-		result[7 + port/8] +=
-				((instance->registers->rh_desc_b >> port)%2) << (port%8);
+		uint8_t is_non_removable =
+			instance->registers->rh_desc_b >> port % 2;
+		result[7 + port / 8] +=
+			is_non_removable << (port % 8);
 	}
 	size_t i;
@@ -195,5 +314,5 @@
 	(*out_size) = size;
 }
-
+/*----------------------------------------------------------------------------*/
 
 /** initialize hub descriptors
@@ -203,60 +322,38 @@
  * @instance root hub instance
  */
-static void rh_init_descriptors(rh_t *instance){
+static void rh_init_descriptors(rh_t *instance) {
 	memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
-		sizeof(ohci_rh_device_descriptor)
-	);
+		sizeof (ohci_rh_device_descriptor)
+		);
 	usb_standard_configuration_descriptor_t descriptor;
-	memcpy(&descriptor,&ohci_rh_conf_descriptor,
-			sizeof(ohci_rh_conf_descriptor));
+	memcpy(&descriptor, &ohci_rh_conf_descriptor,
+		sizeof (ohci_rh_conf_descriptor));
 	uint8_t * hub_descriptor;
 	size_t hub_desc_size;
 	usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
-			&hub_desc_size);
+		&hub_desc_size);
 
 	descriptor.total_length =
-			sizeof(usb_standard_configuration_descriptor_t)+
-			sizeof(usb_standard_endpoint_descriptor_t)+
-			sizeof(usb_standard_interface_descriptor_t)+
-			hub_desc_size;
-	
+		sizeof (usb_standard_configuration_descriptor_t) +
+		sizeof (usb_standard_endpoint_descriptor_t) +
+		sizeof (usb_standard_interface_descriptor_t) +
+		hub_desc_size;
+
 	uint8_t * full_config_descriptor =
-			(uint8_t*) malloc(descriptor.total_length);
-	memcpy(full_config_descriptor, &descriptor, sizeof(descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor),
-			&ohci_rh_iface_descriptor, sizeof(ohci_rh_iface_descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor) +
-				sizeof(ohci_rh_iface_descriptor),
-			&ohci_rh_ep_descriptor, sizeof(ohci_rh_ep_descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor) +
-				sizeof(ohci_rh_iface_descriptor) +
-				sizeof(ohci_rh_ep_descriptor),
-			hub_descriptor, hub_desc_size);
-	
+		(uint8_t*) malloc(descriptor.total_length);
+	memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor),
+		&ohci_rh_iface_descriptor, sizeof (ohci_rh_iface_descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor) +
+		sizeof (ohci_rh_iface_descriptor),
+		&ohci_rh_ep_descriptor, sizeof (ohci_rh_ep_descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor) +
+		sizeof (ohci_rh_iface_descriptor) +
+		sizeof (ohci_rh_ep_descriptor),
+		hub_descriptor, hub_desc_size);
+
 	instance->descriptors.configuration = full_config_descriptor;
 	instance->descriptors.configuration_size = descriptor.total_length;
 }
-
-/** Root hub initialization
- * @return Error code.
- */
-int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs)
-{
-	assert(instance);
-	instance->address = -1;
-	instance->registers = regs;
-	instance->device = dev;
-	instance->port_count = instance->registers->rh_desc_a & 0xff;
-	rh_init_descriptors(instance);
-	/// \TODO set port power mode
-
-
-	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
-
-	//start generic usb hub driver
-	
-	/* TODO: implement */
-	return EOK;
-}
 /*----------------------------------------------------------------------------*/
 
@@ -272,12 +369,21 @@
  */
 static int process_get_port_status_request(rh_t *instance, uint16_t port,
-		usb_transfer_batch_t * request){
-	if(port<1 || port>instance->port_count)
-		return EINVAL;
-	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	usb_transfer_batch_t * request) {
+	if (port < 1 || port > instance->port_count)
+		return EINVAL;
+	uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
 	request->transfered_size = 4;
-	uint32_buffer[0] = instance->registers->rh_port_status[port -1];
-	return EOK;
-}
+	uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
+#if 0
+	int i;
+	for (i = 0; i < instance->port_count; ++i) {
+		usb_log_debug("port status %d,x%x\n",
+			instance->registers->rh_port_status[i],
+			instance->registers->rh_port_status[i]);
+	}
+#endif
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -291,15 +397,13 @@
  */
 static int process_get_hub_status_request(rh_t *instance,
-		usb_transfer_batch_t * request){
-	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	usb_transfer_batch_t * request) {
+	uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
+	request->transfered_size = 4;
 	//bits, 0,1,16,17
-	request->transfered_size = 4;
-	uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
+	uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
 	uint32_buffer[0] = mask & instance->registers->rh_status;
 	return EOK;
-
-}
-
-
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -313,24 +417,25 @@
  */
 static int process_get_status_request(rh_t *instance,
-		usb_transfer_batch_t * request)
-{
+	usb_transfer_batch_t * request) {
 	size_t buffer_size = request->buffer_size;
 	usb_device_request_setup_packet_t * request_packet =
-			(usb_device_request_setup_packet_t*)
-			request->setup_buffer;
+		(usb_device_request_setup_packet_t*)
+		request->setup_buffer;
 
 	usb_hub_bm_request_type_t request_type = request_packet->request_type;
-	if(buffer_size<4/*request_packet->length*/){///\TODO
+	if (buffer_size < 4/*request_packet->length*/) {///\TODO
 		usb_log_warning("requested more data than buffer size\n");
 		return EINVAL;
 	}
 
-	if(request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
+	if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
 		return process_get_hub_status_request(instance, request);
-	if(request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
-		return process_get_port_status_request(instance, request_packet->index,
-				request);
+	if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
+		return process_get_port_status_request(instance,
+		request_packet->index,
+		request);
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -339,5 +444,6 @@
  * Result contains bitmap where bit 0 indicates change on hub and
  * bit i indicates change on i`th port (i>0). For more info see
- * Hub and Port status bitmap specification in USB specification.
+ * Hub and Port status bitmap specification in USB specification
+ * (chapter 11.13.4)
  * @param instance root hub instance
  * @param@out buffer pointer to created interrupt mas
@@ -345,26 +451,26 @@
  */
 static void create_interrupt_mask(rh_t *instance, void ** buffer,
-		size_t * buffer_size){
+	size_t * buffer_size) {
 	int bit_count = instance->port_count + 1;
-	(*buffer_size) = (bit_count / 8) + (bit_count%8==0)?0:1;
+	(*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1);
+
 	(*buffer) = malloc(*buffer_size);
-	uint8_t * bitmap = (uint8_t*)(*buffer);
-	uint32_t mask = (1<<16) + (1<<17);
-	bzero(bitmap,(*buffer_size));
-	if(instance->registers->rh_status & mask){
+	uint8_t * bitmap = (uint8_t*) (*buffer);
+	uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
+		| (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
+	bzero(bitmap, (*buffer_size));
+	if (instance->registers->rh_status & mask) {
 		bitmap[0] = 1;
 	}
 	int port;
-	mask = 0;
-	int i;
-	for(i=16;i<=20;++i)
-		mask += 1<<i;
-	for(port = 1; port<=instance->port_count;++port){
-		if(mask & instance->registers->rh_port_status[port-1]){
-			bitmap[(port+1)/8] += 1<<(port%8);
-		}
-	}
-}
- 
+	mask = port_status_change_mask;
+	for (port = 1; port <= instance->port_count; ++port) {
+		if (mask & instance->registers->rh_port_status[port - 1]) {
+			bitmap[(port) / 8] += 1 << (port % 8);
+		}
+	}
+}
+/*----------------------------------------------------------------------------*/
+
 /**
  * create answer to a descriptor request
@@ -377,29 +483,31 @@
  */
 static int process_get_descriptor_request(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	size_t size;
 	const void * result_descriptor = NULL;
 	const uint16_t setup_request_value = setup_request->value_high;
-			//(setup_request->value_low << 8);
+	//(setup_request->value_low << 8);
 	bool del = false;
-	switch (setup_request_value)
-	{
-		case USB_DESCTYPE_HUB: {
+	switch (setup_request_value) {
+		case USB_DESCTYPE_HUB:
+		{
 			uint8_t * descriptor;
 			usb_create_serialized_hub_descriptor(
 				instance, &descriptor, &size);
 			result_descriptor = descriptor;
-			if(result_descriptor) del = true;
+			if (result_descriptor) del = true;
 			break;
 		}
-		case USB_DESCTYPE_DEVICE: {
+		case USB_DESCTYPE_DEVICE:
+		{
 			usb_log_debug("USB_DESCTYPE_DEVICE\n");
 			result_descriptor = &ohci_rh_device_descriptor;
-			size = sizeof(ohci_rh_device_descriptor);
+			size = sizeof (ohci_rh_device_descriptor);
 			break;
 		}
-		case USB_DESCTYPE_CONFIGURATION: {
+		case USB_DESCTYPE_CONFIGURATION:
+		{
 			usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
 			result_descriptor = instance->descriptors.configuration;
@@ -407,39 +515,43 @@
 			break;
 		}
-		case USB_DESCTYPE_INTERFACE: {
+		case USB_DESCTYPE_INTERFACE:
+		{
 			usb_log_debug("USB_DESCTYPE_INTERFACE\n");
 			result_descriptor = &ohci_rh_iface_descriptor;
-			size = sizeof(ohci_rh_iface_descriptor);
+			size = sizeof (ohci_rh_iface_descriptor);
 			break;
 		}
-		case USB_DESCTYPE_ENDPOINT: {
+		case USB_DESCTYPE_ENDPOINT:
+		{
 			usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
 			result_descriptor = &ohci_rh_ep_descriptor;
-			size = sizeof(ohci_rh_ep_descriptor);
+			size = sizeof (ohci_rh_ep_descriptor);
 			break;
 		}
-		default: {
-			usb_log_debug("USB_DESCTYPE_EINVAL %d \n",setup_request->value);
-			usb_log_debug("\ttype %d\n\trequest %d\n\tvalue %d\n\tindex %d\n\tlen %d\n ",
-					setup_request->request_type,
-					setup_request->request,
-					setup_request_value,
-					setup_request->index,
-					setup_request->length
-					);
+		default:
+		{
+			usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
+				setup_request->value);
+			usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
+				"%d\n\tindex %d\n\tlen %d\n ",
+				setup_request->request_type,
+				setup_request->request,
+				setup_request_value,
+				setup_request->index,
+				setup_request->length
+				);
 			return EINVAL;
 		}
 	}
-	if(request->buffer_size < size){
+	if (request->buffer_size < size) {
 		size = request->buffer_size;
 	}
 	request->transfered_size = size;
-	memcpy(request->transport_buffer,result_descriptor,size);
-	usb_log_debug("sent desctiptor: %s\n",
-			usb_debug_str_buffer((uint8_t*)request->transport_buffer,size,size));
+	memcpy(request->transport_buffer, result_descriptor, size);
 	if (del)
 		free(result_descriptor);
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -451,9 +563,9 @@
  * @return error code
  */
-static int process_get_configuration_request(rh_t *instance, 
-		usb_transfer_batch_t *request){
+static int process_get_configuration_request(rh_t *instance,
+	usb_transfer_batch_t *request) {
 	//set and get configuration requests do not have any meaning, only dummy
 	//values are returned
-	if(request->buffer_size != 1)
+	if (request->buffer_size != 1)
 		return EINVAL;
 	request->transport_buffer[0] = 1;
@@ -461,8 +573,9 @@
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
  * process feature-enabling request on hub
- * 
+ *
  * @param instance root hub instance
  * @param feature feature selector
@@ -470,12 +583,13 @@
  */
 static int process_hub_feature_set_request(rh_t *instance,
-		uint16_t feature){
-	if(! ((1<<feature) & hub_set_feature_valid_mask))
+	uint16_t feature) {
+	if (!((1 << feature) & hub_set_feature_valid_mask))
 		return EINVAL;
 	instance->registers->rh_status =
-			(instance->registers->rh_status | (1<<feature))
-			& (~ hub_clear_feature_by_writing_one_mask);
-	return EOK;
-}
+		(instance->registers->rh_status | (1 << feature))
+		& (~hub_clear_feature_by_writing_one_mask);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -487,26 +601,25 @@
  */
 static int process_hub_feature_clear_request(rh_t *instance,
-		uint16_t feature){
-	if(! ((1<<feature) & hub_clear_feature_valid_mask))
+	uint16_t feature) {
+	if (!((1 << feature) & hub_clear_feature_valid_mask))
 		return EINVAL;
 	//is the feature cleared directly?
-	if ((1<<feature) & hub_set_feature_direct_mask){
+	if ((1 << feature) & hub_set_feature_direct_mask) {
 		instance->registers->rh_status =
-			(instance->registers->rh_status & (~(1<<feature)))
-			& (~ hub_clear_feature_by_writing_one_mask);
-	}else{//the feature is cleared by writing '1'
+			(instance->registers->rh_status & (~(1 << feature)))
+			& (~hub_clear_feature_by_writing_one_mask);
+	} else {//the feature is cleared by writing '1'
 		instance->registers->rh_status =
-				(instance->registers->rh_status
-				& (~ hub_clear_feature_by_writing_one_mask))
-				| (1<<feature);
-	}
-	return EOK;
-}
-
-
+			(instance->registers->rh_status
+			& (~hub_clear_feature_by_writing_one_mask))
+			| (1 << feature);
+	}
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
  * process feature-enabling request on hub
- * 
+ *
  * @param instance root hub instance
  * @param feature feature selector
@@ -516,15 +629,16 @@
  */
 static int process_port_feature_set_request(rh_t *instance,
-		uint16_t feature, uint16_t port){
-	if(!((1<<feature) & port_set_feature_valid_mask))
-		return EINVAL;
-	if(port<1 || port>instance->port_count)
+	uint16_t feature, uint16_t port) {
+	if (!((1 << feature) & port_set_feature_valid_mask))
+		return EINVAL;
+	if (port < 1 || port > instance->port_count)
 		return EINVAL;
 	instance->registers->rh_port_status[port - 1] =
-			(instance->registers->rh_port_status[port - 1] | (1<<feature))
-			& (~port_clear_feature_valid_mask);
+		(instance->registers->rh_port_status[port - 1] | (1 << feature))
+		& (~port_clear_feature_valid_mask);
 	/// \TODO any error?
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -538,25 +652,25 @@
  */
 static int process_port_feature_clear_request(rh_t *instance,
-		uint16_t feature, uint16_t port){
-	if(!((1<<feature) & port_clear_feature_valid_mask))
-		return EINVAL;
-	if(port<1 || port>instance->port_count)
-		return EINVAL;
-	if(feature == USB_HUB_FEATURE_PORT_POWER)
+	uint16_t feature, uint16_t port) {
+	if (!((1 << feature) & port_clear_feature_valid_mask))
+		return EINVAL;
+	if (port < 1 || port > instance->port_count)
+		return EINVAL;
+	if (feature == USB_HUB_FEATURE_PORT_POWER)
 		feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
-	if(feature == USB_HUB_FEATURE_PORT_SUSPEND)
+	if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
 		feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
 	instance->registers->rh_port_status[port - 1] =
-			(instance->registers->rh_port_status[port - 1] 
-			& (~port_clear_feature_valid_mask))
-			| (1<<feature);
+		(instance->registers->rh_port_status[port - 1]
+		& (~port_clear_feature_valid_mask))
+		| (1 << feature);
 	/// \TODO any error?
 	return EOK;
 }
-
+/*----------------------------------------------------------------------------*/
 
 /**
  * register address to this device
- * 
+ *
  * @param instance root hub instance
  * @param address new address
@@ -564,8 +678,9 @@
  */
 static int process_address_set_request(rh_t *instance,
-		uint16_t address){
+	uint16_t address) {
 	instance->address = address;
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -579,16 +694,16 @@
  */
 static int process_request_with_output(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
-	if(setup_request->request == USB_DEVREQ_GET_STATUS){
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
+	if (setup_request->request == USB_DEVREQ_GET_STATUS) {
 		usb_log_debug("USB_DEVREQ_GET_STATUS\n");
 		return process_get_status_request(instance, request);
 	}
-	if(setup_request->request == USB_DEVREQ_GET_DESCRIPTOR){
+	if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
 		usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
 		return process_get_descriptor_request(instance, request);
 	}
-	if(setup_request->request == USB_DEVREQ_GET_CONFIGURATION){
+	if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
 		usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
 		return process_get_configuration_request(instance, request);
@@ -596,4 +711,5 @@
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -607,12 +723,12 @@
  */
 static int process_request_with_input(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	request->transfered_size = 0;
-	if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR){
+	if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
 		return ENOTSUP;
 	}
-	if(setup_request->request == USB_DEVREQ_SET_CONFIGURATION){
+	if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
 		//set and get configuration requests do not have any meaning,
 		//only dummy values are returned
@@ -621,4 +737,5 @@
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -632,46 +749,50 @@
  */
 static int process_request_without_data(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	request->transfered_size = 0;
-	if(setup_request->request == USB_DEVREQ_CLEAR_FEATURE){
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
+	if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
 			return process_hub_feature_clear_request(instance,
-					setup_request->value);
-		}
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
+				setup_request->value);
+		}
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
 			return process_port_feature_clear_request(instance,
-					setup_request->value,
-					setup_request->index);
+				setup_request->value,
+				setup_request->index);
 		}
 		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
-				setup_request->request_type);
-		return EINVAL;
-	}
-	if(setup_request->request == USB_DEVREQ_SET_FEATURE){
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
+			setup_request->request_type);
+		return EINVAL;
+	}
+	if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
 			return process_hub_feature_set_request(instance,
-					setup_request->value);
-		}
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
+				setup_request->value);
+		}
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
 			return process_port_feature_set_request(instance,
-					setup_request->value,
-					setup_request->index);
-		}
-		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",setup_request->request_type);
-		return EINVAL;
-	}
-	if(setup_request->request == USB_DEVREQ_SET_ADDRESS){
+				setup_request->value,
+				setup_request->index);
+		}
+		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
+			setup_request->request_type);
+		return EINVAL;
+	}
+	if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
 		usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
-		return process_address_set_request(instance, setup_request->value);
-	}
-	usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",setup_request->request_type);
+		return process_address_set_request(instance,
+			setup_request->value);
+	}
+	usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
+		setup_request->request_type);
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -693,83 +814,56 @@
  * @return error code
  */
-static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request){
+static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
+	if (!request->setup_buffer) {
+		usb_log_error("root hub received empty transaction?");
+		return EINVAL;
+	}
 	int opResult;
-	if (request->setup_buffer) {
-		if(sizeof(usb_device_request_setup_packet_t)>request->setup_size){
-			usb_log_error("setup packet too small\n");
-			return EINVAL;
-		}
-		usb_log_info("CTRL packet: %s.\n",
-			usb_debug_str_buffer((const uint8_t *)request->setup_buffer, 8, 8));
-		usb_device_request_setup_packet_t * setup_request =
-				(usb_device_request_setup_packet_t*)request->setup_buffer;
-		if(
-			setup_request->request == USB_DEVREQ_GET_STATUS
-			|| setup_request->request == USB_DEVREQ_GET_DESCRIPTOR
-			|| setup_request->request == USB_DEVREQ_GET_CONFIGURATION
-		){
+	if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
+		usb_log_error("setup packet too small\n");
+		return EINVAL;
+	}
+	usb_log_info("CTRL packet: %s.\n",
+		usb_debug_str_buffer(
+		(const uint8_t *) request->setup_buffer, 8, 8));
+	usb_device_request_setup_packet_t * setup_request =
+		(usb_device_request_setup_packet_t*)
+		request->setup_buffer;
+	switch (setup_request->request) {
+		case USB_DEVREQ_GET_STATUS:
+		case USB_DEVREQ_GET_DESCRIPTOR:
+		case USB_DEVREQ_GET_CONFIGURATION:
 			usb_log_debug("processing request with output\n");
-			opResult = process_request_with_output(instance,request);
-		}else if(
-			setup_request->request == USB_DEVREQ_CLEAR_FEATURE
-			|| setup_request->request == USB_DEVREQ_SET_FEATURE
-			|| setup_request->request == USB_DEVREQ_SET_ADDRESS
-		){
-			usb_log_debug("processing request without additional data\n");
-			opResult = process_request_without_data(instance,request);
-		}else if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR
-				|| setup_request->request == USB_DEVREQ_SET_CONFIGURATION
-		){
-			usb_log_debug("processing request with input\n");
-			opResult = process_request_with_input(instance,request);
-		}else{
-			usb_log_warning("received unsuported request: %d\n",
-					setup_request->request
-					);
+			opResult = process_request_with_output(
+				instance, request);
+			break;
+		case USB_DEVREQ_CLEAR_FEATURE:
+		case USB_DEVREQ_SET_FEATURE:
+		case USB_DEVREQ_SET_ADDRESS:
+			usb_log_debug("processing request without "
+				"additional data\n");
+			opResult = process_request_without_data(
+				instance, request);
+			break;
+		case USB_DEVREQ_SET_DESCRIPTOR:
+		case USB_DEVREQ_SET_CONFIGURATION:
+			usb_log_debug("processing request with "
+				"input\n");
+			opResult = process_request_with_input(
+				instance, request);
+			break;
+		default:
+			usb_log_warning("received unsuported request: "
+				"%d\n",
+				setup_request->request
+				);
 			opResult = ENOTSUP;
-		}
-	}else{
-		usb_log_error("root hub received empty transaction?");
-		opResult = EINVAL;
 	}
 	return opResult;
 }
 
-/**
- * process root hub request
- *
- * @param instance root hub instance
- * @param request structure containing both request and response information
- * @return error code
- */
-int rh_request(rh_t *instance, usb_transfer_batch_t *request)
-{
-	assert(instance);
-	assert(request);
-	int opResult;
-	if(request->transfer_type == USB_TRANSFER_CONTROL){
-		usb_log_info("Root hub got CONTROL packet\n");
-		opResult = process_ctrl_request(instance,request);
-	}else if(request->transfer_type == USB_TRANSFER_INTERRUPT){
-		usb_log_info("Root hub got INTERRUPT packet\n");
-		void * buffer;
-		create_interrupt_mask(instance, &buffer,
-			&(request->transfered_size));
-		memcpy(request->transport_buffer,buffer, request->transfered_size);
-		opResult = EOK;
-	}else{
-		opResult = EINVAL;
-	}
-	usb_transfer_batch_finish(request, opResult);
-	return EOK;
-}
-/*----------------------------------------------------------------------------*/
-
-
-void rh_interrupt(rh_t *instance)
-{
-	usb_log_info("Whoa whoa wait, I`m not supposed to receive any interrupts, am I?\n");
-	/* TODO: implement? */
-}
+
+
+
 /**
  * @}
Index: uspace/drv/usbhub/Makefile
===================================================================
--- uspace/drv/usbhub/Makefile	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/usbhub/Makefile	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -34,7 +34,7 @@
 SOURCES = \
 	main.c \
-	ports.c \
 	utils.c \
-	usbhub.c
+	usbhub.c \
+	usblist.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/drv/usbhub/port_status.h
===================================================================
--- uspace/drv/usbhub/port_status.h	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/usbhub/port_status.h	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -49,4 +49,14 @@
 
 /**
+ * structure holding hub status and changes flags.
+ * should not be accessed directly, use supplied getter/setter methods.
+ *
+ * For more information refer to table 11.16.2.5 in
+ * "Universal Serial Bus Specification Revision 1.1"
+ *
+ */
+typedef uint32_t usb_hub_status_t;
+
+/**
  * set values in request to be it a port status request
  * @param request
@@ -54,6 +64,6 @@
  */
 static inline void usb_hub_set_port_status_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS;
@@ -63,4 +73,18 @@
 }
 
+/**
+ * set values in request to be it a port status request
+ * @param request
+ * @param port
+ */
+static inline void usb_hub_set_hub_status_request(
+	usb_device_request_setup_packet_t * request
+	) {
+	request->index = 0;
+	request->request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS;
+	request->request = USB_HUB_REQUEST_GET_STATUS;
+	request->value = 0;
+	request->length = 4;
+}
 
 /**
@@ -70,12 +94,11 @@
  */
 static inline usb_device_request_setup_packet_t *
-usb_hub_create_port_status_request(uint16_t port){
+usb_hub_create_port_status_request(uint16_t port) {
 	usb_device_request_setup_packet_t * result =
 		usb_new(usb_device_request_setup_packet_t);
-	usb_hub_set_port_status_request(result,port);
+	usb_hub_set_port_status_request(result, port);
 	return result;
 }
 
-
 /**
  * set the device request to be a port feature enable request
@@ -85,7 +108,7 @@
  */
 static inline void usb_hub_set_enable_port_feature_request(
-usb_device_request_setup_packet_t * request, uint16_t port,
-		uint16_t feature_selector
-){
+	usb_device_request_setup_packet_t * request, uint16_t port,
+	uint16_t feature_selector
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -102,7 +125,7 @@
  */
 static inline void usb_hub_set_disable_port_feature_request(
-usb_device_request_setup_packet_t * request, uint16_t port,
-		uint16_t feature_selector
-){
+	usb_device_request_setup_packet_t * request, uint16_t port,
+	uint16_t feature_selector
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -118,6 +141,6 @@
  */
 static inline void usb_hub_set_enable_port_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -133,8 +156,8 @@
  */
 static inline usb_device_request_setup_packet_t *
-usb_hub_create_enable_port_request(uint16_t port){
+usb_hub_create_enable_port_request(uint16_t port) {
 	usb_device_request_setup_packet_t * result =
 		usb_new(usb_device_request_setup_packet_t);
-	usb_hub_set_enable_port_request(result,port);
+	usb_hub_set_enable_port_request(result, port);
 	return result;
 }
@@ -146,6 +169,6 @@
  */
 static inline void usb_hub_set_disable_port_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -161,8 +184,8 @@
  */
 static inline usb_device_request_setup_packet_t *
-usb_hub_create_disable_port_request(uint16_t port){
+usb_hub_create_disable_port_request(uint16_t port) {
 	usb_device_request_setup_packet_t * result =
 		usb_new(usb_device_request_setup_packet_t);
-	usb_hub_set_disable_port_request(result,port);
+	usb_hub_set_disable_port_request(result, port);
 	return result;
 }
@@ -174,6 +197,6 @@
  */
 static inline void usb_hub_set_reset_port_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -189,8 +212,8 @@
  */
 static inline usb_device_request_setup_packet_t *
-usb_hub_create_reset_port_request(uint16_t port){
+usb_hub_create_reset_port_request(uint16_t port) {
 	usb_device_request_setup_packet_t * result =
 		usb_new(usb_device_request_setup_packet_t);
-	usb_hub_set_reset_port_request(result,port);
+	usb_hub_set_reset_port_request(result, port);
 	return result;
 }
@@ -202,6 +225,6 @@
  */
 static inline void usb_hub_set_power_port_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -217,6 +240,6 @@
  */
 static inline void usb_hub_unset_power_port_request(
-usb_device_request_setup_packet_t * request, uint16_t port
-){
+	usb_device_request_setup_packet_t * request, uint16_t port
+	) {
 	request->index = port;
 	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
@@ -226,96 +249,184 @@
 }
 
-
-/** get i`th bit of port status */
-static inline bool usb_port_get_bit(usb_port_status_t * status, int idx)
-{
-	return (((*status)>>(idx))%2);
-}
-
-/** set i`th bit of port status */
+/**
+ * get i`th bit of port status
+ * 
+ * @param status
+ * @param idx
+ * @return
+ */
+static inline bool usb_port_get_bit(usb_port_status_t * status, int idx) {
+	return ((*status)&(1 << idx))!=0;
+}
+
+/**
+ * set i`th bit of port status
+ * 
+ * @param status
+ * @param idx
+ * @param value
+ */
 static inline void usb_port_set_bit(
-	usb_port_status_t * status, int idx, bool value)
-{
-	(*status) = value?
-		               ((*status)|(1<<(idx))):
-		               ((*status)&(~(1<<(idx))));
-}
-
-//device connnected on port
-static inline bool usb_port_dev_connected(usb_port_status_t * status){
-	return usb_port_get_bit(status,0);
-}
-
-static inline void usb_port_set_dev_connected(usb_port_status_t * status,bool connected){
-	usb_port_set_bit(status,0,connected);
+	usb_port_status_t * status, int idx, bool value) {
+	(*status) = value ?
+		((*status) | (1 << (idx))) :
+		((*status)&(~(1 << (idx))));
+}
+
+/**
+ * get i`th bit of hub status
+ * 
+ * @param status
+ * @param idx
+ * @return
+ */
+static inline bool usb_hub_get_bit(usb_hub_status_t * status, int idx) {
+	return (*status)&(1 << idx);
+}
+
+/**
+ * set i`th bit of hub status
+ * 
+ * @param status
+ * @param idx
+ * @param value
+ */
+static inline void usb_hub_set_bit(
+	usb_hub_status_t * status, int idx, bool value) {
+	(*status) = value ?
+		((*status) | (1 << (idx))) :
+		((*status)&(~(1 << (idx))));
+}
+
+/**
+ * connection status geter for port status
+ * 
+ * @param status
+ * @return true if there is something connected
+ */
+static inline bool usb_port_dev_connected(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 0);
+}
+
+static inline void usb_port_set_dev_connected(usb_port_status_t * status, bool connected) {
+	usb_port_set_bit(status, 0, connected);
 }
 
 //port enabled
-static inline bool usb_port_enabled(usb_port_status_t * status){
-	return usb_port_get_bit(status,1);
-}
-
-static inline void usb_port_set_enabled(usb_port_status_t * status,bool enabled){
-	usb_port_set_bit(status,1,enabled);
+
+/**
+ * port enabled getter for port status
+ * 
+ * @param status
+ * @return true if the port is enabled
+ */
+static inline bool usb_port_enabled(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 1);
+}
+
+static inline void usb_port_set_enabled(usb_port_status_t * status, bool enabled) {
+	usb_port_set_bit(status, 1, enabled);
 }
 
 //port suspended
-static inline bool usb_port_suspended(usb_port_status_t * status){
-	return usb_port_get_bit(status,2);
-}
-
-static inline void usb_port_set_suspended(usb_port_status_t * status,bool suspended){
-	usb_port_set_bit(status,2,suspended);
+/**
+ * port suspended getter for port status
+ *
+ * @param status
+ * @return true if port is suspended
+ */
+static inline bool usb_port_suspended(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 2);
+}
+
+static inline void usb_port_set_suspended(usb_port_status_t * status, bool suspended) {
+	usb_port_set_bit(status, 2, suspended);
 }
 
 //over currect
-static inline bool usb_port_over_current(usb_port_status_t * status){
-	return usb_port_get_bit(status,3);
-}
-
-static inline void usb_port_set_over_current(usb_port_status_t * status,bool value){
-	usb_port_set_bit(status,3,value);
+/**
+ * over current condition indicator getter for port status
+ *
+ * @param status
+ * @return true if there is opver-current condition on the hub
+ */
+static inline bool usb_port_over_current(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 3);
+}
+
+static inline void usb_port_set_over_current(usb_port_status_t * status, bool value) {
+	usb_port_set_bit(status, 3, value);
 }
 
 //port reset
-static inline bool usb_port_reset(usb_port_status_t * status){
-	return usb_port_get_bit(status,4);
-}
-
-static inline void usb_port_set_reset(usb_port_status_t * status,bool value){
-	usb_port_set_bit(status,4,value);
+/**
+ * port reset indicator getter for port status
+ * 
+ * @param status
+ * @return true if port is reset
+ */
+static inline bool usb_port_reset(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 4);
+}
+
+static inline void usb_port_set_reset(usb_port_status_t * status, bool value) {
+	usb_port_set_bit(status, 4, value);
 }
 
 //powered
-static inline bool usb_port_powered(usb_port_status_t * status){
-	return usb_port_get_bit(status,8);
-}
-
-static inline void usb_port_set_powered(usb_port_status_t * status,bool powered){
-	usb_port_set_bit(status,8,powered);
+/**
+ * power state getter for port status
+ *
+ * @param status
+ * @return true if port is powered
+ */
+static inline bool usb_port_powered(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 8);
+}
+
+static inline void usb_port_set_powered(usb_port_status_t * status, bool powered) {
+	usb_port_set_bit(status, 8, powered);
 }
 
 //low speed device attached
-static inline bool usb_port_low_speed(usb_port_status_t * status){
-	return usb_port_get_bit(status,9);
-}
-
-static inline void usb_port_set_low_speed(usb_port_status_t * status,bool low_speed){
-	usb_port_set_bit(status,9,low_speed);
-}
-
-//low speed device attached
-static inline bool usb_port_high_speed(usb_port_status_t * status){
-	return usb_port_get_bit(status,10);
-}
-
-static inline void usb_port_set_high_speed(usb_port_status_t * status,bool high_speed){
-	usb_port_set_bit(status,10,high_speed);
-}
-
-static inline usb_speed_t usb_port_speed(usb_port_status_t * status){
-	if(usb_port_low_speed(status))
+/**
+ * low speed device on the port indicator
+ * 
+ * @param status
+ * @return true if low speed device is attached
+ */
+static inline bool usb_port_low_speed(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 9);
+}
+
+static inline void usb_port_set_low_speed(usb_port_status_t * status, bool low_speed) {
+	usb_port_set_bit(status, 9, low_speed);
+}
+
+//high speed device attached
+/**
+ * high speed device on the port indicator
+ *
+ * @param status
+ * @return true if high speed device is on port
+ */
+static inline bool usb_port_high_speed(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 10);
+}
+
+static inline void usb_port_set_high_speed(usb_port_status_t * status, bool high_speed) {
+	usb_port_set_bit(status, 10, high_speed);
+}
+
+/**
+ * speed getter for port status
+ *
+ * @param status
+ * @return speed of usb device (for more see usb specification)
+ */
+static inline usb_speed_t usb_port_speed(usb_port_status_t * status) {
+	if (usb_port_low_speed(status))
 		return USB_SPEED_LOW;
-	if(usb_port_high_speed(status))
+	if (usb_port_high_speed(status))
 		return USB_SPEED_HIGH;
 	return USB_SPEED_FULL;
@@ -324,48 +435,141 @@
 
 //connect change
-static inline bool usb_port_connect_change(usb_port_status_t * status){
-	return usb_port_get_bit(status,16);
-}
-
-static inline void usb_port_set_connect_change(usb_port_status_t * status,bool change){
-	usb_port_set_bit(status,16,change);
+/**
+ * port connect change indicator
+ *
+ * @param status
+ * @return true if connection has changed
+ */
+static inline bool usb_port_connect_change(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 16);
+}
+
+static inline void usb_port_set_connect_change(usb_port_status_t * status, bool change) {
+	usb_port_set_bit(status, 16, change);
 }
 
 //port enable change
-static inline bool usb_port_enabled_change(usb_port_status_t * status){
-	return usb_port_get_bit(status,17);
-}
-
-static inline void usb_port_set_enabled_change(usb_port_status_t * status,bool change){
-	usb_port_set_bit(status,17,change);
+/**
+ * port enable change for port status
+ *
+ * @param status
+ * @return true if the port has been enabled/disabled
+ */
+static inline bool usb_port_enabled_change(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 17);
+}
+
+static inline void usb_port_set_enabled_change(usb_port_status_t * status, bool change) {
+	usb_port_set_bit(status, 17, change);
 }
 
 //suspend change
-static inline bool usb_port_suspend_change(usb_port_status_t * status){
-	return usb_port_get_bit(status,18);
-}
-
-static inline void usb_port_set_suspend_change(usb_port_status_t * status,bool change){
-	usb_port_set_bit(status,18,change);
+/**
+ * port suspend change for port status
+ * 
+ * @param status
+ * @return ture if suspend status has changed
+ */
+static inline bool usb_port_suspend_change(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 18);
+}
+
+static inline void usb_port_set_suspend_change(usb_port_status_t * status, bool change) {
+	usb_port_set_bit(status, 18, change);
 }
 
 //over current change
-static inline bool usb_port_overcurrent_change(usb_port_status_t * status){
-	return usb_port_get_bit(status,19);
-}
-
-static inline void usb_port_set_overcurrent_change(usb_port_status_t * status,bool change){
-	usb_port_set_bit(status,19,change);
+/**
+ * over current change indicator
+ * 
+ * @param status
+ * @return true if over-current condition on port has changed
+ */
+static inline bool usb_port_overcurrent_change(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 19);
+}
+
+static inline void usb_port_set_overcurrent_change(usb_port_status_t * status, bool change) {
+	usb_port_set_bit(status, 19, change);
 }
 
 //reset change
-static inline bool usb_port_reset_completed(usb_port_status_t * status){
-	return usb_port_get_bit(status,20);
-}
-
-static inline void usb_port_set_reset_completed(usb_port_status_t * status,bool completed){
-	usb_port_set_bit(status,20,completed);
-}
-
+/**
+ * port reset change indicator
+ * @param status
+ * @return true if port has been reset
+ */
+static inline bool usb_port_reset_completed(usb_port_status_t * status) {
+	return usb_port_get_bit(status, 20);
+}
+
+static inline void usb_port_set_reset_completed(usb_port_status_t * status, bool completed) {
+	usb_port_set_bit(status, 20, completed);
+}
+
+//local power status
+/**
+ * local power lost indicator for hub status
+ * 
+ * @param status
+ * @return true if hub is not powered
+ */
+static inline bool usb_hub_local_power_lost(usb_hub_status_t * status) {
+	return usb_hub_get_bit(status, 0);
+}
+
+static inline void usb_hub_set_local_power_lost(usb_port_status_t * status,
+	bool power_lost) {
+	usb_hub_set_bit(status, 0, power_lost);
+}
+
+//over current ocndition
+/**
+ * hub over-current indicator
+ *
+ * @param status
+ * @return true if over-current condition occurred on hub
+ */
+static inline bool usb_hub_over_current(usb_hub_status_t * status) {
+	return usb_hub_get_bit(status, 1);
+}
+
+static inline void usb_hub_set_over_current(usb_port_status_t * status,
+	bool over_current) {
+	usb_hub_set_bit(status, 1, over_current);
+}
+
+//local power change
+/**
+ * hub power change indicator
+ *
+ * @param status
+ * @return true if local power status has been changed - power has been
+ * dropped or re-established
+ */
+static inline bool usb_hub_local_power_change(usb_hub_status_t * status) {
+	return usb_hub_get_bit(status, 16);
+}
+
+static inline void usb_hub_set_local_power_change(usb_port_status_t * status,
+	bool change) {
+	usb_hub_set_bit(status, 16, change);
+}
+
+//local power status
+/**
+ * hub over-current condition change indicator
+ *
+ * @param status
+ * @return true if over-current condition has changed
+ */
+static inline bool usb_hub_over_current_change(usb_hub_status_t * status) {
+	return usb_hub_get_bit(status, 17);
+}
+
+static inline void usb_hub_set_over_current_change(usb_port_status_t * status,
+	bool change) {
+	usb_hub_set_bit(status, 17, change);
+}
 
 
Index: uspace/drv/usbhub/ports.c
===================================================================
--- uspace/drv/usbhub/ports.c	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ 	(revision )
@@ -1,331 +1,0 @@
-/*
- * Copyright (c) 2011 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup drvusbhub
- * @{
- */
-/** @file
- * Hub ports functions.
- */
-#include "port_status.h"
-#include <inttypes.h>
-#include <errno.h>
-#include <str_error.h>
-#include <usb/request.h>
-#include <usb/debug.h>
-
-/** Retrieve port status.
- *
- * @param[in] ctrl_pipe Control pipe to use.
- * @param[in] port Port number (starting at 1).
- * @param[out] status Where to store the port status.
- * @return Error code.
- */
-static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
-    usb_port_status_t *status)
-{
-	size_t recv_size;
-	usb_device_request_setup_packet_t request;
-	usb_port_status_t status_tmp;
-
-	usb_hub_set_port_status_request(&request, port);
-	int rc = usb_pipe_control_read(ctrl_pipe,
-	    &request, sizeof(usb_device_request_setup_packet_t),
-	    &status_tmp, sizeof(status_tmp), &recv_size);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	if (recv_size != sizeof (status_tmp)) {
-		return ELIMIT;
-	}
-
-	if (status != NULL) {
-		*status = status_tmp;
-	}
-
-	return EOK;
-}
-
-/** Information for fibril for device discovery. */
-struct add_device_phase1 {
-	usb_hub_info_t *hub;
-	size_t port;
-	usb_speed_t speed;
-};
-
-/** Callback for enabling a specific port.
- *
- * We wait on a CV until port is reseted.
- * That is announced via change on interrupt pipe.
- *
- * @param port_no Port number (starting at 1).
- * @param arg Custom argument, points to @c usb_hub_info_t.
- * @return Error code.
- */
-static int enable_port_callback(int port_no, void *arg)
-{
-	usb_hub_info_t *hub = (usb_hub_info_t *) arg;
-	int rc;
-	usb_device_request_setup_packet_t request;
-	usb_hub_port_t *my_port = hub->ports + port_no;
-
-	usb_hub_set_reset_port_request(&request, port_no);
-	rc = usb_pipe_control_write(hub->control_pipe,
-	    &request, sizeof(request), NULL, 0);
-	if (rc != EOK) {
-		usb_log_warning("Port reset failed: %s.\n", str_error(rc));
-		return rc;
-	}
-
-	/*
-	 * Wait until reset completes.
-	 */
-	fibril_mutex_lock(&my_port->reset_mutex);
-	while (!my_port->reset_completed) {
-		fibril_condvar_wait(&my_port->reset_cv, &my_port->reset_mutex);
-	}
-	fibril_mutex_unlock(&my_port->reset_mutex);
-
-	/* Clear the port reset change. */
-	rc = usb_hub_clear_port_feature(hub->control_pipe,
-	    port_no, USB_HUB_FEATURE_C_PORT_RESET);
-	if (rc != EOK) {
-		usb_log_error("Failed to clear port %d reset feature: %s.\n",
-		    port_no, str_error(rc));
-		return rc;
-	}
-
-	return EOK;
-}
-
-/** Fibril for adding a new device.
- *
- * Separate fibril is needed because the port reset completion is announced
- * via interrupt pipe and thus we cannot block here.
- *
- * @param arg Pointer to struct add_device_phase1.
- * @return 0 Always.
- */
-static int add_device_phase1_worker_fibril(void *arg)
-{
-	struct add_device_phase1 *data
-	    = (struct add_device_phase1 *) arg;
-
-	usb_address_t new_address;
-	devman_handle_t child_handle;
-
-	int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
-	    &data->hub->connection, data->speed,
-	    enable_port_callback, (int) data->port, data->hub,
-	    &new_address, &child_handle,
-	    NULL, NULL, NULL);
-
-	if (rc != EOK) {
-		usb_log_error("Failed registering device on port %zu: %s.\n",
-		    data->port, str_error(rc));
-		goto leave;
-	}
-
-	data->hub->ports[data->port].attached_device.handle = child_handle;
-	data->hub->ports[data->port].attached_device.address = new_address;
-
-	usb_log_info("Detected new device on `%s' (port %zu), " \
-	    "address %d (handle %" PRIun ").\n",
-	    data->hub->usb_device->ddf_dev->name, data->port,
-	    new_address, child_handle);
-
-leave:
-	free(arg);
-
-	return EOK;
-}
-
-/** Start device adding when connection change is detected.
- *
- * This fires a new fibril to complete the device addition.
- *
- * @param hub Hub where the change occured.
- * @param port Port index (starting at 1).
- * @param speed Speed of the device.
- * @return Error code.
- */
-static int add_device_phase1_new_fibril(usb_hub_info_t *hub, size_t port,
-    usb_speed_t speed)
-{
-	struct add_device_phase1 *data
-	    = malloc(sizeof(struct add_device_phase1));
-	if (data == NULL) {
-		return ENOMEM;
-	}
-	data->hub = hub;
-	data->port = port;
-	data->speed = speed;
-
-	usb_hub_port_t *the_port = hub->ports + port;
-
-	fibril_mutex_lock(&the_port->reset_mutex);
-	the_port->reset_completed = false;
-	fibril_mutex_unlock(&the_port->reset_mutex);
-
-	int rc = usb_hub_clear_port_feature(hub->control_pipe, port,
-	    USB_HUB_FEATURE_C_PORT_CONNECTION);
-	if (rc != EOK) {
-		free(data);
-		usb_log_warning("Failed to clear port change flag: %s.\n",
-		    str_error(rc));
-		return rc;
-	}
-
-	fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
-	if (fibril == 0) {
-		free(data);
-		return ENOMEM;
-	}
-	fibril_add_ready(fibril);
-
-	return EOK;
-}
-
-/** Process change on a single port.
- *
- * @param hub Hub to which the port belongs.
- * @param port Port index (starting at 1).
- */
-static void process_port_change(usb_hub_info_t *hub, size_t port)
-{
-	int rc;
-
-	usb_port_status_t port_status;
-
-	rc = get_port_status(&hub->usb_device->ctrl_pipe, port, &port_status);
-	if (rc != EOK) {
-		usb_log_error("Failed to get port %zu status: %s.\n",
-		    port, str_error(rc));
-		return;
-	}
-
-	/*
-	 * Check exact nature of the change.
-	 */
-	usb_log_debug("Port %zu change status: %x.\n", port,
-	    (unsigned int) port_status);
-
-	if (usb_port_connect_change(&port_status)) {
-		bool device_connected = usb_port_dev_connected(&port_status);
-		usb_log_debug("Connection change on port %zu: %s.\n", port,
-		    device_connected ? "device attached" : "device removed");
-
-		if (device_connected) {
-			rc = add_device_phase1_new_fibril(hub, port,
-			    usb_port_speed(&port_status));
-			if (rc != EOK) {
-				usb_log_error(
-				    "Cannot handle change on port %zu: %s.\n",
-				    str_error(rc));
-			}
-		} else {
-			usb_hub_removed_device(hub, port);
-		}
-	}
-
-	if (usb_port_overcurrent_change(&port_status)) {
-		if (usb_port_over_current(&port_status)) {
-			usb_log_warning("Overcurrent on port %zu.\n", port);
-			usb_hub_over_current(hub, port);
-		} else {
-			usb_log_debug("Overcurrent on port %zu autoresolved.\n",
-			    port);
-		}
-	}
-
-	if (usb_port_reset_completed(&port_status)) {
-		usb_log_debug("Port %zu reset complete.\n", port);
-		if (usb_port_enabled(&port_status)) {
-			/* Finalize device adding. */
-			usb_hub_port_t *the_port = hub->ports + port;
-			fibril_mutex_lock(&the_port->reset_mutex);
-			the_port->reset_completed = true;
-			fibril_condvar_broadcast(&the_port->reset_cv);
-			fibril_mutex_unlock(&the_port->reset_mutex);
-		} else {
-			usb_log_warning(
-			    "Port %zu reset complete but port not enabled.\n",
-			    port);
-		}
-	}
-
-	usb_port_set_connect_change(&port_status, false);
-	usb_port_set_reset(&port_status, false);
-	usb_port_set_reset_completed(&port_status, false);
-	usb_port_set_dev_connected(&port_status, false);
-	if (port_status >> 16) {
-		usb_log_warning("Unsupported change on port %zu: %x.\n",
-		    port, (unsigned int) port_status);
-	}
-}
-
-
-/** Callback for polling hub for port changes.
- *
- * @param dev Device where the change occured.
- * @param change_bitmap Bitmap of changed ports.
- * @param change_bitmap_size Size of the bitmap in bytes.
- * @param arg Custom argument, points to @c usb_hub_info_t.
- * @return Whether to continue polling.
- */
-bool hub_port_changes_callback(usb_device_t *dev,
-    uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
-{
-	usb_hub_info_t *hub = (usb_hub_info_t *) arg;
-
-	/* FIXME: check that we received enough bytes. */
-	if (change_bitmap_size == 0) {
-		goto leave;
-	}
-
-	size_t port;
-	for (port = 1; port < hub->port_count + 1; port++) {
-		bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
-		if (change) {
-			process_port_change(hub, port);
-		}
-	}
-
-
-leave:
-	/* FIXME: proper interval. */
-	async_usleep(1000 * 1000 * 10 );
-
-	return true;
-}
-
-
-/**
- * @}
- */
Index: uspace/drv/usbhub/ports.h
===================================================================
--- uspace/drv/usbhub/ports.h	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ 	(revision )
@@ -1,80 +1,0 @@
-/*
- * Copyright (c) 2011 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup drvusbhub
- * @{
- */
-/** @file
- * Hub ports related functions.
- */
-#ifndef DRV_USBHUB_PORTS_H
-#define DRV_USBHUB_PORTS_H
-
-#include <ipc/devman.h>
-#include <usb/usb.h>
-#include <ddf/driver.h>
-#include <fibril_synch.h>
-
-#include <usb/hub.h>
-
-#include <usb/pipes.h>
-#include <usb/devdrv.h>
-
-/** Information about single port on a hub. */
-typedef struct {
-	/** Mutex needed by CV for checking port reset. */
-	fibril_mutex_t reset_mutex;
-	/** CV for waiting to port reset completion. */
-	fibril_condvar_t reset_cv;
-	/** Whether port reset is completed.
-	 * Guarded by @c reset_mutex.
-	 */
-	bool reset_completed;
-
-	/** Information about attached device. */
-	usb_hc_attached_device_t attached_device;
-} usb_hub_port_t;
-
-/** Initialize hub port information.
- *
- * @param port Port to be initialized.
- */
-static inline void usb_hub_port_init(usb_hub_port_t *port) {
-	port->attached_device.address = -1;
-	port->attached_device.handle = 0;
-	fibril_mutex_initialize(&port->reset_mutex);
-	fibril_condvar_initialize(&port->reset_cv);
-}
-
-bool hub_port_changes_callback(usb_device_t *, uint8_t *, size_t, void *);
-
-
-#endif
-/**
- * @}
- */
Index: uspace/drv/usbhub/usbhub.c
===================================================================
--- uspace/drv/usbhub/usbhub.c	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/usbhub/usbhub.c	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -53,7 +53,69 @@
 #include "usb/classes/classes.h"
 
+
+static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
+	usb_speed_t speed);
+
+static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev);
+
+static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info);
+
+static int usb_hub_set_configuration(usb_hub_info_t * hub_info);
+
+static int usb_hub_release_default_address(usb_hub_info_t * hub);
+
+static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
+	usb_speed_t speed);
+
+static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
+	uint16_t port, usb_speed_t speed);
+
+static void usb_hub_removed_device(
+	usb_hub_info_t * hub, uint16_t port);
+
+static void usb_hub_port_over_current(usb_hub_info_t * hub,
+	uint16_t port, uint32_t status);
+
+static void usb_hub_process_interrupt(usb_hub_info_t * hub,
+	uint16_t port);
+
+static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
+	usb_hub_status_t status);
+
+static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
+	usb_hub_status_t status);
+
+static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
+
+//static int initialize_non_removable(usb_hub_info_t * hub_info,
+//	unsigned int port);
+
 static int usb_hub_trigger_connecting_non_removable_devices(
-		usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
-
+	usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
+
+
+/**
+ * control loop running in hub`s fibril
+ *
+ * Hub`s fibril periodically asks for changes on hub and if needded calls
+ * change handling routine.
+ * @warning currently hub driver asks for changes once a second
+ * @param hub_info_param hub representation pointer
+ * @return zero
+ */
+int usb_hub_control_loop(void * hub_info_param) {
+	usb_hub_info_t * hub_info = (usb_hub_info_t*) hub_info_param;
+	int errorCode = EOK;
+
+	while (errorCode == EOK) {
+		async_usleep(1000 * 1000 * 10); /// \TODO proper number once
+		errorCode = usb_hub_check_hub_changes(hub_info);
+	}
+	usb_log_error("something in ctrl loop went wrong, errno %d\n",
+		errorCode);
+
+	return 0;
+}
+/// \TODO malloc checking
 
 //*********************************************
@@ -63,4 +125,186 @@
 //*********************************************
 
+
+
+/**
+ * Initialize hub device driver fibril
+ *
+ * Creates hub representation and fibril that periodically checks hub`s status.
+ * Hub representation is passed to the fibril.
+ * @param usb_dev generic usb device information
+ * @return error code
+ */
+int usb_hub_add_device(usb_device_t * usb_dev) {
+	if (!usb_dev) return EINVAL;
+	usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
+	//create hc connection
+	usb_log_debug("Initializing USB wire abstraction.\n");
+	int opResult = usb_hc_connection_initialize_from_device(
+		&hub_info->connection,
+		hub_info->usb_device->ddf_dev);
+	if (opResult != EOK) {
+		usb_log_error("could not initialize connection to device, "
+			"errno %d\n",
+			opResult);
+		free(hub_info);
+		return opResult;
+	}
+
+	usb_pipe_start_session(hub_info->control_pipe);
+	//set hub configuration
+	opResult = usb_hub_set_configuration(hub_info);
+	if (opResult != EOK) {
+		usb_log_error("could not set hub configuration, errno %d\n",
+			opResult);
+		free(hub_info);
+		return opResult;
+	}
+	//get port count and create attached_devs
+	opResult = usb_hub_process_hub_specific_info(hub_info);
+	if (opResult != EOK) {
+		usb_log_error("could not set hub configuration, errno %d\n",
+			opResult);
+		free(hub_info);
+		return opResult;
+	}
+	usb_pipe_end_session(hub_info->control_pipe);
+
+
+	/// \TODO what is this?
+	usb_log_debug("Creating `hub' function.\n");
+	ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
+		fun_exposed, "hub");
+	assert(hub_fun != NULL);
+	hub_fun->ops = NULL;
+
+	int rc = ddf_fun_bind(hub_fun);
+	assert(rc == EOK);
+	rc = ddf_fun_add_to_class(hub_fun, "hub");
+	assert(rc == EOK);
+
+	//create fibril for the hub control loop
+	fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
+	if (fid == 0) {
+		usb_log_error("failed to start monitoring fibril for new"
+			" hub.\n");
+		return ENOMEM;
+	}
+	fibril_add_ready(fid);
+	usb_log_debug("Hub fibril created.\n");
+
+	usb_log_info("Controlling hub `%s' (%d ports).\n",
+		hub_info->usb_device->ddf_dev->name, hub_info->port_count);
+	return EOK;
+}
+
+
+//*********************************************
+//
+//  hub driver code, main loop and port handling
+//
+//*********************************************
+
+/**
+ * check changes on hub
+ *
+ * Handles changes on each port with a status change.
+ * @param hub_info hub representation
+ * @return error code
+ */
+int usb_hub_check_hub_changes(usb_hub_info_t * hub_info) {
+	int opResult;
+	opResult = usb_pipe_start_session(
+		hub_info->status_change_pipe);
+	//this might not be necessary - if all non-removables are ok, it is
+	//not needed here
+	opResult = usb_pipe_start_session(hub_info->control_pipe);
+	if (opResult != EOK) {
+		usb_log_error("could not initialize communication for hub; %d\n",
+			opResult);
+		return opResult;
+	}
+
+	size_t port_count = hub_info->port_count;
+	//first check non-removable devices
+	/*
+	{
+		unsigned int port;
+		for (port = 0; port < port_count; ++port) {
+			bool is_non_removable =
+				hub_info->not_initialized_non_removables[port/8]
+				& (1 << (port-1 % 8));
+			if (is_non_removable) {
+				opResult = initialize_non_removable(hub_info,
+					port+1);
+			}
+		}
+	}
+	*/
+
+	/// FIXME: count properly
+	size_t byte_length = ((port_count + 1) / 8) + 1;
+	void *change_bitmap = malloc(byte_length);
+	size_t actual_size;
+
+	/*
+	 * Send the request.
+	 */
+	opResult = usb_pipe_read(
+		hub_info->status_change_pipe,
+		change_bitmap, byte_length, &actual_size
+		);
+
+	if (opResult != EOK) {
+		free(change_bitmap);
+		usb_log_warning("something went wrong while getting the"
+			"status of hub\n");
+		usb_pipe_end_session(hub_info->status_change_pipe);
+		return opResult;
+	}
+	unsigned int port;
+
+	if (opResult != EOK) {
+		usb_log_error("could not start control pipe session %d\n",
+			opResult);
+		usb_pipe_end_session(hub_info->status_change_pipe);
+		return opResult;
+	}
+	opResult = usb_hc_connection_open(&hub_info->connection);
+	if (opResult != EOK) {
+		usb_log_error("could not start host controller session %d\n",
+			opResult);
+		usb_pipe_end_session(hub_info->control_pipe);
+		usb_pipe_end_session(hub_info->status_change_pipe);
+		return opResult;
+	}
+
+	///todo, opresult check, pre obe konekce
+	bool interrupt;
+	interrupt = ((uint8_t*)change_bitmap)[0] & 1;
+	if(interrupt){
+		usb_hub_process_global_interrupt(hub_info);
+	}
+	for (port = 1; port < port_count + 1; ++port) {
+		interrupt =
+			((uint8_t*) change_bitmap)[port / 8] & (1<<(port % 8));
+		if (interrupt) {
+			usb_hub_process_interrupt(
+				hub_info, port);
+		}
+	}
+	/// \todo check hub status
+	usb_hc_connection_close(&hub_info->connection);
+	usb_pipe_end_session(hub_info->control_pipe);
+	usb_pipe_end_session(hub_info->status_change_pipe);
+	free(change_bitmap);
+	return EOK;
+}
+
+//*********************************************
+//
+//  support functions
+//
+//*********************************************
+
 /**
  * create usb_hub_info_t structure
@@ -72,5 +316,5 @@
 static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
 	usb_hub_info_t * result = usb_new(usb_hub_info_t);
-	if(!result) return NULL;
+	if (!result) return NULL;
 	result->usb_device = usb_dev;
 	result->status_change_pipe = usb_dev->pipes[0].pipe;
@@ -79,4 +323,5 @@
 	return result;
 }
+
 
 /**
@@ -90,29 +335,35 @@
  * @return error code
  */
-static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
+static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
 	// get hub descriptor
 	usb_log_debug("creating serialized descriptor\n");
 	void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
 	usb_hub_descriptor_t * descriptor;
+	int opResult;
 
 	/* this was one fix of some bug, should not be needed anymore
 	 * these lines allow to reset hub once more, it can be used as
 	 * brute-force initialization for non-removable devices
-	int opResult = usb_request_set_configuration(&result->endpoints.control, 1);
-	if(opResult!=EOK){
-		usb_log_error("could not set default configuration, errno %d",opResult);
-		return opResult;
-	}
-	 */
+	 *
+	opResult = usb_request_set_configuration(hub_info->control_pipe,
+		1);
+	if (opResult != EOK) {
+		usb_log_error("could not set default configuration, errno %d",
+			opResult);
+		return opResult;
+	}*/
+
+
 	size_t received_size;
-	int opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
-			USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
-			USB_DESCTYPE_HUB,
-			0, 0, serialized_descriptor,
-			USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
-
-	if (opResult != EOK) {
-		usb_log_error("failed when receiving hub descriptor, badcode = %d\n",
-				opResult);
+	opResult = usb_request_get_descriptor(hub_info->control_pipe,
+		USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
+		USB_DESCTYPE_HUB,
+		0, 0, serialized_descriptor,
+		USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
+
+	if (opResult != EOK) {
+		usb_log_error("failed when receiving hub descriptor, "
+			"badcode = %d\n",
+			opResult);
 		free(serialized_descriptor);
 		return opResult;
@@ -120,14 +371,27 @@
 	usb_log_debug2("deserializing descriptor\n");
 	descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
-	if(descriptor==NULL){
+	if (descriptor == NULL) {
 		usb_log_warning("could not deserialize descriptor \n");
 		return opResult;
 	}
-	usb_log_debug("setting port count to %d\n",descriptor->ports_count);
+	usb_log_debug("setting port count to %d\n", descriptor->ports_count);
 	hub_info->port_count = descriptor->ports_count;
-	hub_info->ports = malloc(sizeof(usb_hub_port_t) * (hub_info->port_count+1));
-	size_t port;
-	for (port = 0; port < hub_info->port_count + 1; port++) {
-		usb_hub_port_init(&hub_info->ports[port]);
+	/// \TODO check attached_devices array: this is not semantically correct
+	hub_info->attached_devs = (usb_hc_attached_device_t*)
+		malloc((hub_info->port_count + 1) *
+			sizeof (usb_hc_attached_device_t)
+		);
+	int i;
+	for (i = 1; i <= hub_info->port_count; ++i) {
+		hub_info->attached_devs[i].handle = 0;
+		hub_info->attached_devs[i].address = 0;
+		usb_log_info("powering port %d\n",i);
+		opResult = usb_hub_set_port_feature(
+			hub_info->control_pipe,
+			i,
+			USB_HUB_FEATURE_PORT_POWER);
+		if(opResult!=EOK)
+			usb_log_warning("could not power port %d\n",i);
+
 	}
 	//handle non-removable devices
@@ -135,8 +399,17 @@
 	usb_log_debug2("freeing data\n");
 	free(serialized_descriptor);
-	free(descriptor->devices_removable);
-	free(descriptor);
+	hub_info->descriptor = descriptor;
+	hub_info->not_initialized_non_removables =
+		(uint8_t*) malloc((hub_info->port_count + 8) / 8);
+	memcpy(hub_info->not_initialized_non_removables,
+		descriptor->devices_removable,
+		(hub_info->port_count + 8) / 8
+		);
+
+	//free(descriptor->devices_removable);
+	//free(descriptor);
 	return EOK;
 }
+
 /**
  * Set configuration of hub
@@ -147,11 +420,11 @@
  * @return error code
  */
-static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
+static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
 	//device descriptor
 	usb_standard_device_descriptor_t *std_descriptor
-	    = &hub_info->usb_device->descriptors.device;
+		= &hub_info->usb_device->descriptors.device;
 	usb_log_debug("hub has %d configurations\n",
-	    std_descriptor->configuration_count);
-	if(std_descriptor->configuration_count<1){
+		std_descriptor->configuration_count);
+	if (std_descriptor->configuration_count < 1) {
 		usb_log_error("there are no configurations available\n");
 		return EINVAL;
@@ -159,19 +432,19 @@
 
 	usb_standard_configuration_descriptor_t *config_descriptor
-	    = (usb_standard_configuration_descriptor_t *)
-	    hub_info->usb_device->descriptors.configuration;
+		= (usb_standard_configuration_descriptor_t *)
+		hub_info->usb_device->descriptors.configuration;
 
 	/* Set configuration. */
 	int opResult = usb_request_set_configuration(
-	    &hub_info->usb_device->ctrl_pipe,
-	    config_descriptor->configuration_number);
+		&hub_info->usb_device->ctrl_pipe,
+		config_descriptor->configuration_number);
 
 	if (opResult != EOK) {
 		usb_log_error("Failed to set hub configuration: %s.\n",
-		    str_error(opResult));
+			str_error(opResult));
 		return opResult;
 	}
 	usb_log_debug("\tused configuration %d\n",
-			config_descriptor->configuration_number);
+		config_descriptor->configuration_number);
 
 	return EOK;
@@ -179,105 +452,473 @@
 
 /**
- * Initialize hub device driver fibril
- *
- * Creates hub representation and fibril that periodically checks hub`s status.
- * Hub representation is passed to the fibril.
- * @param usb_dev generic usb device information
+ * release default address used by given hub
+ *
+ * Also unsets hub->is_default_address_used. Convenience wrapper function.
+ * @note hub->connection MUST be open for communication
+ * @param hub hub representation
  * @return error code
  */
-int usb_hub_add_device(usb_device_t * usb_dev){
-	if(!usb_dev) return EINVAL;
-	usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
-	//create hc connection
-	usb_log_debug("Initializing USB wire abstraction.\n");
-	int opResult = usb_hc_connection_initialize_from_device(
-			&hub_info->connection,
-			hub_info->usb_device->ddf_dev);
-	if(opResult != EOK){
-		usb_log_error("could not initialize connection to device, errno %d\n",
+static int usb_hub_release_default_address(usb_hub_info_t * hub) {
+	int opResult = usb_hc_release_default_address(&hub->connection);
+	if (opResult != EOK) {
+		usb_log_error("could not release default address, errno %d\n",
+			opResult);
+		return opResult;
+	}
+	hub->is_default_address_used = false;
+	return EOK;
+}
+
+/**
+ * Reset the port with new device and reserve the default address.
+ * @param hub hub representation
+ * @param port port number, starting from 1
+ * @param speed transfer speed of attached device, one of low, full or high
+ * @return error code
+ */
+static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
+	usb_speed_t speed) {
+	//if this hub already uses default address, it cannot request it once more
+	if (hub->is_default_address_used) {
+		usb_log_info("default address used, another time\n");
+		return EREFUSED;
+	}
+	usb_log_debug("some connection changed\n");
+	assert(hub->control_pipe->hc_phone);
+	int opResult = usb_hub_clear_port_feature(hub->control_pipe,
+		port, USB_HUB_FEATURE_C_PORT_CONNECTION);
+	if (opResult != EOK) {
+		usb_log_warning("could not clear port-change-connection flag\n");
+	}
+	usb_device_request_setup_packet_t request;
+
+	//get default address
+	opResult = usb_hc_reserve_default_address(&hub->connection, speed);
+
+	if (opResult != EOK) {
+		usb_log_warning("cannot assign default address, it is probably "
+			"used %d\n",
+			opResult);
+		return opResult;
+	}
+	hub->is_default_address_used = true;
+	//reset port
+	usb_hub_set_reset_port_request(&request, port);
+	opResult = usb_pipe_control_write(
+		hub->control_pipe,
+		&request, sizeof (usb_device_request_setup_packet_t),
+		NULL, 0
+		);
+	if (opResult != EOK) {
+		usb_log_error("something went wrong when reseting a port %d\n",
+			opResult);
+		usb_hub_release_default_address(hub);
+	}
+	return opResult;
+}
+
+/**
+ * Finalize adding new device after port reset
+ *
+ * Set device`s address and start it`s driver.
+ * @param hub hub representation
+ * @param port port number, starting from 1
+ * @param speed transfer speed of attached device, one of low, full or high
+ */
+static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
+	uint16_t port, usb_speed_t speed) {
+
+	int opResult;
+	usb_log_debug("finalizing add device\n");
+	opResult = usb_hub_clear_port_feature(hub->control_pipe,
+		port, USB_HUB_FEATURE_C_PORT_RESET);
+
+	if (opResult != EOK) {
+		usb_log_error("failed to clear port reset feature\n");
+		usb_hub_release_default_address(hub);
+		return;
+	}
+	//create connection to device
+	usb_pipe_t new_device_pipe;
+	usb_device_connection_t new_device_connection;
+	usb_device_connection_initialize_on_default_address(
+		&new_device_connection,
+		&hub->connection
+		);
+	usb_pipe_initialize_default_control(
+		&new_device_pipe,
+		&new_device_connection);
+	usb_pipe_probe_default_control(&new_device_pipe);
+
+	/* Request address from host controller. */
+	usb_address_t new_device_address = usb_hc_request_address(
+		&hub->connection,
+		speed
+		);
+	if (new_device_address < 0) {
+		usb_log_error("failed to get free USB address\n");
+		opResult = new_device_address;
+		usb_hub_release_default_address(hub);
+		return;
+	}
+	usb_log_debug("setting new address %d\n", new_device_address);
+	//opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
+	//    new_device_address);
+	usb_pipe_start_session(&new_device_pipe);
+	opResult = usb_request_set_address(&new_device_pipe,
+		new_device_address);
+	usb_pipe_end_session(&new_device_pipe);
+	if (opResult != EOK) {
+		usb_log_error("could not set address for new device %d\n",
+			opResult);
+		usb_hub_release_default_address(hub);
+		return;
+	}
+
+	//opResult = usb_hub_release_default_address(hc);
+	opResult = usb_hub_release_default_address(hub);
+	if (opResult != EOK) {
+		return;
+	}
+
+	devman_handle_t child_handle;
+	//??
+	opResult = usb_device_register_child_in_devman(new_device_address,
+		hub->connection.hc_handle, hub->usb_device->ddf_dev,
+		&child_handle,
+		NULL, NULL, NULL);
+
+	if (opResult != EOK) {
+		usb_log_error("could not start driver for new device %d\n",
+			opResult);
+		return;
+	}
+	hub->attached_devs[port].handle = child_handle;
+	hub->attached_devs[port].address = new_device_address;
+
+	//opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
+	opResult = usb_hc_register_device(
+		&hub->connection,
+		&hub->attached_devs[port]);
+	if (opResult != EOK) {
+		usb_log_error("could not assign address of device in hcd %d\n",
+			opResult);
+		return;
+	}
+	usb_log_info("Detected new device on `%s' (port %d), " \
+	    "address %d (handle %llu).\n",
+		hub->usb_device->ddf_dev->name, (int) port,
+		new_device_address, child_handle);
+}
+
+/**
+ * routine called when a device on port has been removed
+ *
+ * If the device on port had default address, it releases default address.
+ * Otherwise does not do anything, because DDF does not allow to remove device
+ * from it`s device tree.
+ * @param hub hub representation
+ * @param port port number, starting from 1
+ */
+static void usb_hub_removed_device(
+	usb_hub_info_t * hub, uint16_t port) {
+
+	int opResult = usb_hub_clear_port_feature(hub->control_pipe,
+		port, USB_HUB_FEATURE_C_PORT_CONNECTION);
+	if (opResult != EOK) {
+		usb_log_warning("could not clear port-change-connection flag\n");
+	}
+	/** \TODO remove device from device manager - not yet implemented in
+	 * devide manager
+	 */
+
+	//close address
+	if (hub->attached_devs[port].address != 0) {
+		/*uncomment this code to use it when DDF allows device removal
+		opResult = usb_hc_unregister_device(
+			&hub->connection,
+			hub->attached_devs[port].address);
+		if(opResult != EOK) {
+			dprintf(USB_LOG_LEVEL_WARNING, "could not release "
+				"address of "
+			    "removed device: %d", opResult);
+		}
+		hub->attached_devs[port].address = 0;
+		hub->attached_devs[port].handle = 0;
+		 */
+	} else {
+		usb_log_warning("this is strange, disconnected device had "
+			"no address\n");
+		//device was disconnected before it`s port was reset -
+		//return default address
+		usb_hub_release_default_address(hub);
+	}
+}
+
+/**
+ * Process over current condition on port.
+ *
+ * Turn off the power on the port.
+ *
+ * @param hub hub representation
+ * @param port port number, starting from 1
+ */
+static void usb_hub_port_over_current(usb_hub_info_t * hub,
+	uint16_t port, uint32_t status) {
+	int opResult;
+	if(usb_port_over_current(&status)){
+		opResult = usb_hub_clear_port_feature(hub->control_pipe,
+			port, USB_HUB_FEATURE_PORT_POWER);
+		if (opResult != EOK) {
+			usb_log_error("cannot power off port %d;  %d\n",
+				port, opResult);
+		}
+	}else{
+		opResult = usb_hub_set_port_feature(hub->control_pipe,
+			port, USB_HUB_FEATURE_PORT_POWER);
+		if (opResult != EOK) {
+			usb_log_error("cannot power on port %d;  %d\n",
+				port, opResult);
+		}
+	}
+}
+
+/**
+ * Process interrupts on given hub port
+ *
+ * Accepts connection, over current and port reset change.
+ * @param hub hub representation
+ * @param port port number, starting from 1
+ */
+static void usb_hub_process_interrupt(usb_hub_info_t * hub,
+	uint16_t port) {
+	usb_log_debug("interrupt at port %d\n", port);
+	//determine type of change
+	usb_pipe_t *pipe = hub->control_pipe;
+
+	int opResult;
+
+	usb_port_status_t status;
+	size_t rcvd_size;
+	usb_device_request_setup_packet_t request;
+	//int opResult;
+	usb_hub_set_port_status_request(&request, port);
+	//endpoint 0
+
+	opResult = usb_pipe_control_read(
+		pipe,
+		&request, sizeof (usb_device_request_setup_packet_t),
+		&status, 4, &rcvd_size
+		);
+	if (opResult != EOK) {
+		usb_log_error("could not get port status\n");
+		return;
+	}
+	if (rcvd_size != sizeof (usb_port_status_t)) {
+		usb_log_error("received status has incorrect size\n");
+		return;
+	}
+	//something connected/disconnected
+	if (usb_port_connect_change(&status)) {
+		usb_log_debug("connection change on port\n");
+		if (usb_port_dev_connected(&status)) {
+			usb_hub_init_add_device(hub, port,
+				usb_port_speed(&status));
+		} else {
+			usb_hub_removed_device(hub, port);
+		}
+	}
+	//over current
+	if (usb_port_overcurrent_change(&status)) {
+		//check if it was not auto-resolved
+		usb_log_debug("overcurrent change on port\n");
+		usb_hub_port_over_current(hub, port, status);
+	}
+	//port reset
+	if (usb_port_reset_completed(&status)) {
+		usb_log_debug("port reset complete\n");
+		if (usb_port_enabled(&status)) {
+			usb_hub_finalize_add_device(hub, port,
+				usb_port_speed(&status));
+		} else {
+			usb_log_warning("port reset, but port still not "
+				"enabled\n");
+		}
+	}
+	usb_log_debug("status x%x : %d\n ", status, status);
+
+	usb_port_set_connect_change(&status, false);
+	usb_port_set_reset(&status, false);
+	usb_port_set_reset_completed(&status, false);
+	usb_port_set_dev_connected(&status, false);
+	usb_port_set_overcurrent_change(&status,false);
+	/// \TODO what about port power change?
+	if (status >> 16) {
+		usb_log_info("there was unsupported change on port %d: %X\n",
+			port, status);
+
+	}
+}
+
+/**
+ * process hub over current change
+ *
+ * This means either to power off the hub or power it on.
+ * @param hub_info hub instance
+ * @param status hub status bitmask
+ * @return error code
+ */
+static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
+	usb_hub_status_t status)
+{
+	int opResult;
+	if(usb_hub_over_current(&status)){
+		opResult = usb_hub_clear_feature(hub_info->control_pipe,
+			USB_HUB_FEATURE_PORT_POWER);
+		if (opResult != EOK) {
+			usb_log_error("cannot power off hub: %d\n",
 				opResult);
-		free(hub_info);
-		return opResult;
+		}
+	}else{
+		opResult = usb_hub_set_feature(hub_info->control_pipe,
+			USB_HUB_FEATURE_PORT_POWER);
+		if (opResult != EOK) {
+			usb_log_error("cannot power on hub: %d\n",
+				opResult);
+		}
+	}
+	return opResult;
+}
+
+/**
+ * process hub power change
+ *
+ * If the power has been lost, reestablish it.
+ * If it was reestablished, re-power all ports.
+ * @param hub_info hub instance
+ * @param status hub status bitmask
+ * @return error code
+ */
+static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
+	usb_hub_status_t status)
+{
+	int opResult;
+	if(usb_hub_local_power_lost(&status)){
+		//restart power on hub
+		opResult = usb_hub_set_feature(hub_info->control_pipe,
+			USB_HUB_FEATURE_PORT_POWER);
+		if (opResult != EOK) {
+			usb_log_error("cannot power on hub: %d\n",
+				opResult);
+		}
+	}else{//power reestablished on hub- restart ports
+		int port;
+		for(port=0;port<hub_info->port_count;++port){
+			opResult = usb_hub_set_port_feature(
+				hub_info->control_pipe,
+				port, USB_HUB_FEATURE_PORT_POWER);
+			if (opResult != EOK) {
+				usb_log_error("cannot power on port %d;  %d\n",
+					port, opResult);
+			}
+		}
+	}
+	return opResult;
+}
+
+/**
+ * process hub interrupts
+ *
+ * The change can be either in the over-current condition or
+ * local-power lost condition.
+ * @param hub_info hub instance
+ */
+static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info){
+	usb_log_debug("global interrupt on a hub\n");
+	usb_pipe_t *pipe = hub_info->control_pipe;
+	int opResult;
+
+	usb_port_status_t status;
+	size_t rcvd_size;
+	usb_device_request_setup_packet_t request;
+	//int opResult;
+	usb_hub_set_hub_status_request(&request);
+	//endpoint 0
+
+	opResult = usb_pipe_control_read(
+		pipe,
+		&request, sizeof (usb_device_request_setup_packet_t),
+		&status, 4, &rcvd_size
+		);
+	if (opResult != EOK) {
+		usb_log_error("could not get hub status\n");
+		return;
+	}
+	if (rcvd_size != sizeof (usb_port_status_t)) {
+		usb_log_error("received status has incorrect size\n");
+		return;
+	}
+	//port reset
+	if (usb_hub_over_current_change(&status)) {
+		usb_process_hub_over_current(hub_info,status);
+	}
+	if (usb_hub_local_power_change(&status)) {
+		usb_process_hub_power_change(hub_info,status);
+	}
+}
+
+//-----------attempts to solve non-removable------------------------
+//-----------attempts to solve non-removable------------------------
+//-----------attempts to solve non-removable------------------------
+//-----------attempts to solve non-removable------------------------
+
+/**
+ * this is an attempt to initialize non-removable devices in the hub
+ *
+ * @param hub_info hub instance
+ * @param port port number, counting from 1
+ * @return error code
+ */
+#if 0
+static int initialize_non_removable(usb_hub_info_t * hub_info,
+	unsigned int port) {
+	int opResult;
+	usb_log_debug("there is not pluged in non-removable device on "
+		"port %d\n", port
+		);
+	//usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
+	usb_port_status_t status;
+	size_t rcvd_size;
+	usb_device_request_setup_packet_t request;
+	//int opResult;
+	usb_hub_set_port_status_request(&request, port);
+	//endpoint 0
+
+	opResult = usb_pipe_control_read(
+		hub_info->control_pipe,
+		&request, sizeof (usb_device_request_setup_packet_t),
+		&status, 4, &rcvd_size
+		);
+	if (opResult != EOK) {
+		usb_log_error("could not get port status %d\n", opResult);
+		return opResult;
+	}
+	if (rcvd_size != sizeof (usb_port_status_t)) {
+		usb_log_error("received status has incorrect size\n");
+		return opResult;
+	}
+	usb_log_debug("port status %d, x%x\n", status, status);
+	if (usb_port_dev_connected(&status)) {
+		usb_log_debug("there is connected device on this port\n");
+		opResult = usb_hub_init_add_device(hub_info, port,
+			usb_port_speed(&status));
+	}else{
+		usb_log_debug("the non-removable device is not connected\n");
+		opResult = EINVAL;
 	}
 	
-	usb_pipe_start_session(hub_info->control_pipe);
-	//set hub configuration
-	opResult = usb_hub_set_configuration(hub_info);
-	if(opResult!=EOK){
-		usb_log_error("could not set hub configuration, errno %d\n",opResult);
-		free(hub_info);
-		return opResult;
-	}
-	//get port count and create attached_devs
-	opResult = usb_hub_process_hub_specific_info(hub_info);
-	if(opResult!=EOK){
-		usb_log_error("could not set hub configuration, errno %d\n",opResult);
-		free(hub_info);
-		return opResult;
-	}
-	usb_pipe_end_session(hub_info->control_pipe);
-
-
-	/// \TODO what is this?
-	usb_log_debug("Creating `hub' function.\n");
-	ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
-			fun_exposed, "hub");
-	assert(hub_fun != NULL);
-	hub_fun->ops = NULL;
-
-	int rc = ddf_fun_bind(hub_fun);
-	assert(rc == EOK);
-	rc = ddf_fun_add_to_class(hub_fun, "hub");
-	assert(rc == EOK);
-
-	/*
-	 * The processing will require opened control pipe and connection
-	 * to the host controller.
-	 * It is waste of resources but let's hope there will be less
-	 * hubs than the phone limit.
-	 * FIXME: with some proper locking over pipes and session
-	 * auto destruction, this could work better.
-	 */
-	rc = usb_pipe_start_session(&usb_dev->ctrl_pipe);
-	if (rc != EOK) {
-		usb_log_error("Failed to start session on control pipe: %s.\n",
-		    str_error(rc));
-		goto leave;
-	}
-	rc = usb_hc_connection_open(&hub_info->connection);
-	if (rc != EOK) {
-		usb_pipe_end_session(&usb_dev->ctrl_pipe);
-		usb_log_error("Failed to open connection to HC: %s.\n",
-		    str_error(rc));
-		goto leave;
-	}
-
-	rc = usb_device_auto_poll(hub_info->usb_device, 0,
-	    hub_port_changes_callback, ((hub_info->port_count+1) / 8) + 1,
-	    NULL, hub_info);
-	if (rc != EOK) {
-		usb_log_error("Failed to create polling fibril: %s.\n",
-		    str_error(rc));
-		free(hub_info);
-		return rc;
-	}
-
-	usb_log_info("Controlling hub `%s' (%d ports).\n",
-	    hub_info->usb_device->ddf_dev->name, hub_info->port_count);
-	return EOK;
-
-leave:
-	free(hub_info);
-
-	return rc;
-}
-
-
-//*********************************************
-//
-//  hub driver code, main loop and port handling
-//
-//*********************************************
-
+	return opResult;
+}
+#endif
 /**
  * triggers actions to connect non0removable devices
@@ -289,67 +930,113 @@
  * @return error code
  */
-static int usb_hub_trigger_connecting_non_removable_devices(usb_hub_info_t * hub,
-		usb_hub_descriptor_t * descriptor)
-{
+static int usb_hub_trigger_connecting_non_removable_devices(
+	usb_hub_info_t * hub,
+	usb_hub_descriptor_t * descriptor) {
 	usb_log_info("attaching non-removable devices(if any)\n");
-	usb_device_request_setup_packet_t request;
+	//usb_device_request_setup_packet_t request;
 	int opResult;
-	size_t rcvd_size;
-	usb_port_status_t status;
+	//size_t rcvd_size;
+	//usb_port_status_t status;
 	uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
 	int port;
-	for(port=1;port<=descriptor->ports_count;++port){
+#if 0
+	opResult = usb_request_set_configuration(hub->control_pipe,
+		1);
+	if (opResult != EOK) {
+		usb_log_error("could not set default configuration, errno %d",
+			opResult);
+		return opResult;
+	}
+
+	for (port = 1; port <= descriptor->ports_count; ++port) {
 		bool is_non_removable =
-				((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
-		if(is_non_removable){
-			usb_log_debug("non-removable device on port %d\n",port);
+			((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
+		if (is_non_removable) {
+			usb_log_debug("non-removable device on port %d\n", port);
 			usb_hub_set_port_status_request(&request, port);
 			opResult = usb_pipe_control_read(
+				hub->control_pipe,
+				&request,
+				sizeof (usb_device_request_setup_packet_t),
+				&status, 4, &rcvd_size
+				);
+			if (opResult != EOK) {
+				usb_log_error("could not get port status of "
+					"port %d errno:%d\n",
+					port, opResult);
+				return opResult;
+			}
+			//try to reset port
+			if (usb_port_dev_connected(&status) || true) {
+				usb_hub_set_enable_port_feature_request(
+					&request, port,
+					USB_HUB_FEATURE_PORT_RESET);
+				opResult = usb_pipe_control_read(
 					hub->control_pipe,
-					&request, sizeof(usb_device_request_setup_packet_t),
+					&request,
+					sizeof (usb_device_request_setup_packet_t),
 					&status, 4, &rcvd_size
 					);
-			if (opResult != EOK) {
-				usb_log_error("could not get port status of port %d errno:%d\n",
-						port, opResult);
-				return opResult;
-			}
-			//set the status change bit, so it will be noticed in driver loop
-			if(usb_port_dev_connected(&status)){
-				usb_hub_set_disable_port_feature_request(&request, port,
-						USB_HUB_FEATURE_PORT_CONNECTION);
-				opResult = usb_pipe_control_read(
-						hub->control_pipe,
-						&request, sizeof(usb_device_request_setup_packet_t),
-						&status, 4, &rcvd_size
-						);
 				if (opResult != EOK) {
 					usb_log_warning(
-							"could not clear port connection on port %d errno:%d\n",
-							port, opResult);
+						"could not reset port %d "
+						"errno:%d\n",
+						port, opResult);
+				}
+				usb_log_debug("port reset, should look like "
+					"%d,x%x\n",
+					(1 << USB_HUB_FEATURE_PORT_RESET),
+					(1 << USB_HUB_FEATURE_PORT_RESET)
+					);
+			}
+			//set the status change bit, so it will be noticed
+			//in driver loop
+			if (usb_port_dev_connected(&status) && false) {
+				usb_hub_set_disable_port_feature_request(
+					&request, port,
+					USB_HUB_FEATURE_PORT_CONNECTION);
+				opResult = usb_pipe_control_read(
+					hub->control_pipe,
+					&request,
+					sizeof (usb_device_request_setup_packet_t),
+					&status, 4, &rcvd_size
+					);
+				if (opResult != EOK) {
+					usb_log_warning(
+						"could not clear port "
+						"connection on port %d "
+						"errno:%d\n",
+						port, opResult);
 				}
 				usb_log_debug("cleared port connection\n");
-				usb_hub_set_enable_port_feature_request(&request, port,
-						USB_HUB_FEATURE_PORT_ENABLE);
+				usb_hub_set_enable_port_feature_request(&request,
+					port,
+					USB_HUB_FEATURE_PORT_ENABLE);
 				opResult = usb_pipe_control_read(
-						hub->control_pipe,
-						&request, sizeof(usb_device_request_setup_packet_t),
-						&status, 4, &rcvd_size
-						);
+					hub->control_pipe,
+					&request,
+					sizeof (usb_device_request_setup_packet_t),
+					&status, 4, &rcvd_size
+					);
 				if (opResult != EOK) {
 					usb_log_warning(
-							"could not set port enabled on port %d errno:%d\n",
-							port, opResult);
+						"could not set port enabled "
+						"on port %d errno:%d\n",
+						port, opResult);
 				}
-				usb_log_debug("port set to enabled - should lead to connection change\n");
+				usb_log_debug("port set to enabled - "
+					"should lead to connection change\n");
 			}
 		}
 	}
+#endif
+
 	/// \TODO this is just a debug code
-	for(port=1;port<=descriptor->ports_count;++port){
+	for (port = 1; port <= descriptor->ports_count; ++port) {
 		bool is_non_removable =
-				((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
-		if(is_non_removable){
-			usb_log_debug("port %d is non-removable\n",port);
+			((non_removable_dev_bitmap[(port-1) / 8]) >> ((port-1) % 8)) % 2;
+		if (is_non_removable) {
+			usb_log_debug("CHECKING port %d is non-removable\n",
+				port);
 			usb_port_status_t status;
 			size_t rcvd_size;
@@ -359,13 +1046,16 @@
 			//endpoint 0
 			opResult = usb_pipe_control_read(
-					hub->control_pipe,
-					&request, sizeof(usb_device_request_setup_packet_t),
-					&status, 4, &rcvd_size
-					);
+				hub->control_pipe,
+				&request,
+				sizeof (usb_device_request_setup_packet_t),
+				&status, 4, &rcvd_size
+				);
 			if (opResult != EOK) {
-				usb_log_error("could not get port status %d\n",opResult);
+				usb_log_error("could not get port status %d\n",
+					opResult);
 			}
 			if (rcvd_size != sizeof (usb_port_status_t)) {
-				usb_log_error("received status has incorrect size\n");
+				usb_log_error("received status has incorrect"
+					" size\n");
 			}
 			//something connected/disconnected
@@ -373,8 +1063,12 @@
 				usb_log_debug("some connection changed\n");
 			}
-			usb_log_debug("status: %s\n",usb_debug_str_buffer(
-					(uint8_t *)&status,4,4));
-		}
-	}
+			if(usb_port_dev_connected(&status)){
+				usb_log_debug("device connected on port\n");
+			}
+			usb_log_debug("status: %s\n", usb_debug_str_buffer(
+				(uint8_t *) & status, 4, 4));
+		}
+	}
+	async_usleep(1000*1000*10);
 	return EOK;
 }
@@ -382,83 +1076,4 @@
 
 /**
- * release default address used by given hub
- *
- * Also unsets hub->is_default_address_used. Convenience wrapper function.
- * @note hub->connection MUST be open for communication
- * @param hub hub representation
- * @return error code
- */
-static int usb_hub_release_default_address(usb_hub_info_t * hub){
-	int opResult = usb_hc_release_default_address(&hub->connection);
-	if(opResult!=EOK){
-		usb_log_error("could not release default address, errno %d\n",opResult);
-		return opResult;
-	}
-	hub->is_default_address_used = false;
-	return EOK;
-}
-
-/**
- * routine called when a device on port has been removed
- *
- * If the device on port had default address, it releases default address.
- * Otherwise does not do anything, because DDF does not allow to remove device
- * from it`s device tree.
- * @param hub hub representation
- * @param port port number, starting from 1
- */
-void usb_hub_removed_device(
-    usb_hub_info_t * hub,uint16_t port) {
-
-	int opResult = usb_hub_clear_port_feature(hub->control_pipe,
-				port, USB_HUB_FEATURE_C_PORT_CONNECTION);
-	if(opResult != EOK){
-		usb_log_warning("could not clear port-change-connection flag\n");
-	}
-	/** \TODO remove device from device manager - not yet implemented in
-	 * devide manager
-	 */
-	
-	//close address
-	if(hub->ports[port].attached_device.address >= 0){
-		/*uncomment this code to use it when DDF allows device removal
-		opResult = usb_hc_unregister_device(
-				&hub->connection, hub->attached_devs[port].address);
-		if(opResult != EOK) {
-			dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
-			    "removed device: %d", opResult);
-		}
-		hub->attached_devs[port].address = 0;
-		hub->attached_devs[port].handle = 0;
-		 */
-	}else{
-		usb_log_warning("this is strange, disconnected device had no address\n");
-		//device was disconnected before it`s port was reset - return default address
-		usb_hub_release_default_address(hub);
-	}
-}
-
-
-/**
- * Process over current condition on port.
- * 
- * Turn off the power on the port.
- *
- * @param hub hub representation
- * @param port port number, starting from 1
- */
-void usb_hub_over_current( usb_hub_info_t * hub,
-		uint16_t port){
-	int opResult;
-	opResult = usb_hub_clear_port_feature(hub->control_pipe,
-	    port, USB_HUB_FEATURE_PORT_POWER);
-	if(opResult!=EOK){
-		usb_log_error("cannot power off port %d;  %d\n",
-				port, opResult);
-	}
-}
-
-
-/**
  * @}
  */
Index: uspace/drv/usbhub/usbhub.h
===================================================================
--- uspace/drv/usbhub/usbhub.h	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/usbhub/usbhub.h	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -43,20 +43,17 @@
 
 #include <usb/hub.h>
+#include <usb/classes/hub.h>
 
 #include <usb/pipes.h>
 #include <usb/devdrv.h>
 
-#include "ports.h"
-
-
-
 /** Information about attached hub. */
 typedef struct {
 	/** Number of ports. */
-	size_t port_count;
+	int port_count;
 
-	/** Ports. */
-	usb_hub_port_t *ports;
-	
+	/** attached device handles, for each port one */
+	usb_hc_attached_device_t * attached_devs;
+
 	/** connection to hcd */
 	usb_hc_connection_t connection;
@@ -87,4 +84,11 @@
 	/** generic usb device data*/
 	usb_device_t * usb_device;
+
+	/** usb hub specific descriptor */
+	usb_hub_descriptor_t * descriptor;
+
+	/** not yet initialized non-removable devices */
+	uint8_t * not_initialized_non_removables;
+	
 } usb_hub_info_t;
 
@@ -94,4 +98,6 @@
  */
 int usb_hub_control_loop(void * hub_info_param);
+
+int usb_hub_add_device(usb_device_t * usb_dev);
 
 /**
@@ -103,8 +109,4 @@
 int usb_hub_check_hub_changes(usb_hub_info_t * hub_info_param);
 
-void usb_hub_removed_device(usb_hub_info_t *, uint16_t);
-void usb_hub_over_current(usb_hub_info_t *, uint16_t);
-
-int usb_hub_add_device(usb_device_t * usb_dev);
 
 #endif
Index: uspace/drv/usbhub/usbhub_private.h
===================================================================
--- uspace/drv/usbhub/usbhub_private.h	(revision 69df9373f337c12038bd59ce0f905d4c728e4b13)
+++ uspace/drv/usbhub/usbhub_private.h	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -38,4 +38,5 @@
 
 #include "usbhub.h"
+#include "usblist.h"
 
 #include <adt/list.h>
@@ -110,4 +111,71 @@
 
 /**
+ * Clear feature on hub port.
+ *
+ * @param hc Host controller telephone
+ * @param address Hub address
+ * @param port_index Port
+ * @param feature Feature selector
+ * @return Operation result
+ */
+static inline int usb_hub_set_port_feature(usb_pipe_t *pipe,
+    int port_index,
+    usb_hub_class_feature_t feature) {
+
+	usb_device_request_setup_packet_t clear_request = {
+		.request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
+		.request = USB_DEVREQ_SET_FEATURE,
+		.length = 0,
+		.index = port_index
+	};
+	clear_request.value = feature;
+	return usb_pipe_control_write(pipe, &clear_request,
+	    sizeof(clear_request), NULL, 0);
+}
+
+
+/**
+ * Clear feature on hub port.
+ *
+ * @param pipe pipe to hub control endpoint
+ * @param feature Feature selector
+ * @return Operation result
+ */
+static inline int usb_hub_clear_feature(usb_pipe_t *pipe,
+    usb_hub_class_feature_t feature) {
+
+	usb_device_request_setup_packet_t clear_request = {
+		.request_type = USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE,
+		.request = USB_DEVREQ_CLEAR_FEATURE,
+		.length = 0,
+		.index = 0
+	};
+	clear_request.value = feature;
+	return usb_pipe_control_write(pipe, &clear_request,
+	    sizeof(clear_request), NULL, 0);
+}
+
+/**
+ * Clear feature on hub port.
+ *
+ * @param pipe pipe to hub control endpoint
+ * @param feature Feature selector
+ * @return Operation result
+ */
+static inline int usb_hub_set_feature(usb_pipe_t *pipe,
+    usb_hub_class_feature_t feature) {
+
+	usb_device_request_setup_packet_t clear_request = {
+		.request_type = USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE,
+		.request = USB_DEVREQ_SET_FEATURE,
+		.length = 0,
+		.index = 0
+	};
+	clear_request.value = feature;
+	return usb_pipe_control_write(pipe, &clear_request,
+	    sizeof(clear_request), NULL, 0);
+}
+
+/**
  * create uint8_t array with serialized descriptor
  *
Index: uspace/drv/usbhub/usblist.c
===================================================================
--- uspace/drv/usbhub/usblist.c	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
+++ uspace/drv/usbhub/usblist.c	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2010 Matus Dekanek
+ * 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 drvusbhub
+ * @{
+ */
+/** @file
+ * @brief usblist implementation
+ */
+#include <sys/types.h>
+
+#include "usbhub_private.h"
+
+
+usb_general_list_t * usb_lst_create(void) {
+	usb_general_list_t* result = usb_new(usb_general_list_t);
+	usb_lst_init(result);
+	return result;
+}
+
+void usb_lst_init(usb_general_list_t * lst) {
+	lst->prev = lst;
+	lst->next = lst;
+	lst->data = NULL;
+}
+
+void usb_lst_prepend(usb_general_list_t* item, void* data) {
+	usb_general_list_t* appended = usb_new(usb_general_list_t);
+	appended->data = data;
+	appended->next = item;
+	appended->prev = item->prev;
+	item->prev->next = appended;
+	item->prev = appended;
+}
+
+void usb_lst_append(usb_general_list_t* item, void* data) {
+	usb_general_list_t* appended = usb_new(usb_general_list_t);
+	appended->data = data;
+	appended->next = item->next;
+	appended->prev = item;
+	item->next->prev = appended;
+	item->next = appended;
+}
+
+void usb_lst_remove(usb_general_list_t* item) {
+	item->next->prev = item->prev;
+	item->prev->next = item->next;
+}
+
+
+
+/**
+ * @}
+ */
+
+
Index: uspace/drv/usbhub/usblist.h
===================================================================
--- uspace/drv/usbhub/usblist.h	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
+++ uspace/drv/usbhub/usblist.h	(revision a4e18e1e33397c8ddac49bbb77b9cac96670e076)
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2010 Matus Dekanek
+ * 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 drvusbhub
+ * @{
+ */
+/** @file
+ * @brief HC driver and hub driver.
+ *
+ * My private list implementation; I did not like the original helenos list.
+ * This one does not depend on the structure of stored data and has
+ * much simpler and more straight-forward semantics.
+ */
+#ifndef USBLIST_H
+#define	USBLIST_H
+
+/**
+ * general list structure
+ */
+typedef struct usb_general_list{
+	void * data;
+	struct usb_general_list * prev, * next;
+} usb_general_list_t;
+
+/** create head of usb general list */
+usb_general_list_t * usb_lst_create(void);
+
+/** initialize head of usb general list */
+void usb_lst_init(usb_general_list_t * lst);
+
+
+/** is the list empty? */
+static inline bool usb_lst_empty(usb_general_list_t * lst){
+	return lst?(lst->next==lst):true;
+}
+
+/** append data behind item */
+void usb_lst_append(usb_general_list_t * lst, void * data);
+
+/** prepend data beore item */
+void usb_lst_prepend(usb_general_list_t * lst, void * data);
+
+/** remove list item from list */
+void usb_lst_remove(usb_general_list_t * item);
+
+/** get data o specified type from list item */
+#define usb_lst_get_data(item, type)  (type *) (item->data)
+
+/** get usb_hub_info_t data from list item */
+static inline usb_hub_info_t * usb_hub_lst_get_data(usb_general_list_t * item) {
+	return usb_lst_get_data(item,usb_hub_info_t);
+}
+
+#endif	/* USBLIST_H */
+/**
+ * @}
+ */
