Index: uspace/drv/usbhub/usbhub.c
===================================================================
--- uspace/drv/usbhub/usbhub.c	(revision a2096484d3914f468df0fc24152c03d2e81826af)
+++ uspace/drv/usbhub/usbhub.c	(revision e5ccfd175b64e5de8ff32f502aa4355fc38b516d)
@@ -57,45 +57,41 @@
 	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);
 
-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_trigger_connecting_non_removable_devices(
-	usb_hub_info_t * hub,
-	usb_hub_descriptor_t * descriptor);
-
-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);
 
 /**
@@ -121,7 +117,5 @@
 	return 0;
 }
-
-/// \TODO set_port_feature use
-/// \TODO unmess code
+/// \TODO malloc checking
 
 //*********************************************
@@ -447,4 +441,469 @@
 }
 
+/**
+ * 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;
+}
+
+/**
+ * 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) 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_log_debug("some connection changed\n");
+			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 some 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);
+		}
+	}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
+ */
+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");
+	}else{
+		usb_log_debug("the non-removable device is not connected\n");
+	}
+	if (!hub_info->is_default_address_used)
+		usb_hub_init_add_device(hub_info, port,
+			usb_port_speed(&status));
+	return opResult;
+}
 
 /**
@@ -467,5 +926,5 @@
 	uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
 	int port;
-
+#if 0
 	opResult = usb_request_set_configuration(hub->control_pipe,
 		1);
@@ -475,5 +934,5 @@
 		return opResult;
 	}
-#if 0
+
 	for (port = 1; port <= descriptor->ports_count; ++port) {
 		bool is_non_removable =
@@ -590,476 +1049,15 @@
 				usb_log_debug("some connection changed\n");
 			}
+			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;
 }
 
-/**
- * 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;
-}
-
-/**
- * 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) 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) {
-	/// \todo no, this is not proper sollution
-	/// get affected ports
-	/// power them off
-	/// wait until there over-current is cleared
-	/// power them on
-
-	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_log_debug("some connection changed\n");
-			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);
-	if (status >> 16) {
-		usb_log_info("there was some 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);
-		}
-	}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);
-	}
-}
-
-/**
- * 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
- */
-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");
-	}
-	if (!hub_info->is_default_address_used)
-		usb_hub_init_add_device(hub_info, port,
-			usb_port_speed(&status));
-	return opResult;
-}
-
-
 
 /**
