Index: uspace/drv/uhci-rhd/port.c
===================================================================
--- uspace/drv/uhci-rhd/port.c	(revision dced52af3bee58828a4b524068bd68da26c46c60)
+++ uspace/drv/uhci-rhd/port.c	(revision 275bf4564149c3cc7c77ddfb104ea8c6658f03b6)
@@ -46,15 +46,26 @@
 #include "port_status.h"
 
-static int uhci_port_new_device(uhci_port_t *port, uint16_t status);
+static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
 static int uhci_port_remove_device(uhci_port_t *port);
 static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
 static int uhci_port_check(void *port);
-static int new_device_enable_port(int portno, void *arg);
-
-int uhci_port_init(
-  uhci_port_t *port, port_status_t *address, unsigned number,
-  unsigned usec, ddf_dev_t *rh)
+static int uhci_port_reset_enable(int portno, void *arg);
+/*----------------------------------------------------------------------------*/
+/** Initializes UHCI root hub port instance.
+ *
+ * @param[in] port Memory structure to use.
+ * @param[in] addr Address of I/O register.
+ * @param[in] number Port number.
+ * @param[in] usec Polling interval.
+ * @param[in] rh Pointer to ddf instance fo the root hub driver.
+ * @return Error code.
+ *
+ * Starts the polling fibril.
+ */
+int uhci_port_init(uhci_port_t *port,
+    port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
 {
 	assert(port);
+
 	port->address = address;
 	port->number = number;
@@ -62,4 +73,5 @@
 	port->attached_device = 0;
 	port->rh = rh;
+
 	int rc = usb_hc_connection_initialize_from_device(
 	    &port->hc_connection, rh);
@@ -75,4 +87,5 @@
 		return ENOMEM;
 	}
+
 	fibril_add_ready(port->checker);
 	usb_log_debug("Port(%p - %d): Added fibril. %x\n",
@@ -81,76 +94,87 @@
 }
 /*----------------------------------------------------------------------------*/
+/** Finishes UHCI root hub port instance.
+ *
+ * @param[in] port Memory structure to use.
+ *
+ * Stops the polling fibril.
+ */
 void uhci_port_fini(uhci_port_t *port)
 {
-// TODO: destroy fibril
-// TODO: hangup phone
-//	fibril_teardown(port->checker);
+	/* TODO: Kill fibril here */
 	return;
 }
 /*----------------------------------------------------------------------------*/
+/** Periodically checks port status and reports new devices.
+ *
+ * @param[in] port Memory structure to use.
+ * @return Error code.
+ */
 int uhci_port_check(void *port)
 {
-	uhci_port_t *port_instance = port;
-	assert(port_instance);
-//	port_status_write(port_instance->address, 0);
-
+	uhci_port_t *instance = port;
+	assert(instance);
+
+	/* Iteration count, for debug purposes only */
 	unsigned count = 0;
 
 	while (1) {
-		async_usleep(port_instance->wait_period_usec);
+		async_usleep(instance->wait_period_usec);
 
 		/* read register value */
-		port_status_t port_status =
-			port_status_read(port_instance->address);
-
-		/* debug print */
-		static fibril_mutex_t dbg_mtx = FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
+		port_status_t port_status = port_status_read(instance->address);
+
+		/* debug print mutex */
+		static fibril_mutex_t dbg_mtx =
+		    FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
 		fibril_mutex_lock(&dbg_mtx);
 		usb_log_debug2("Port(%p - %d): Status: %#04x. === %u\n",
-		  port_instance->address, port_instance->number, port_status, count++);
+		  instance->address, instance->number, port_status, count++);
 //		print_port_status(port_status);
 		fibril_mutex_unlock(&dbg_mtx);
 
-		if ((port_status & STATUS_CONNECTED_CHANGED) != 0) {
-			usb_log_debug("Port(%p - %d): Connected change detected: %x.\n",
-			    port_instance->address, port_instance->number, port_status);
-
-
-			int rc = usb_hc_connection_open(
-			    &port_instance->hc_connection);
-			if (rc != EOK) {
-				usb_log_error("Port(%p - %d): Failed to connect to HC.",
-				    port_instance->address, port_instance->number);
-				continue;
-			}
-
-			/* remove any old device */
-			if (port_instance->attached_device) {
-				usb_log_debug("Port(%p - %d): Removing device.\n",
-				    port_instance->address, port_instance->number);
-				uhci_port_remove_device(port_instance);
-			}
-
-			if ((port_status & STATUS_CONNECTED) != 0) {
-				/* new device */
-				uhci_port_new_device(port_instance, port_status);
-			} else {
-				/* ack changes by writing one to WC bits */
-				port_status_write(port_instance->address, port_status);
-				usb_log_debug("Port(%p - %d): Change status ACK.\n",
-						port_instance->address, port_instance->number);
-			}
-
-			rc = usb_hc_connection_close(
-			    &port_instance->hc_connection);
-			if (rc != EOK) {
-				usb_log_error("Port(%p - %d): Failed to disconnect from HC.",
-				    port_instance->address, port_instance->number);
-			}
-		}
-	}
-	return EOK;
-}
-
+		if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
+			continue;
+
+		usb_log_debug("Port(%p - %d): Connected change detected: %x.\n",
+		    instance->address, instance->number, port_status);
+
+		int rc =
+		    usb_hc_connection_open(&instance->hc_connection);
+		if (rc != EOK) {
+			usb_log_error("Port(%p - %d): Failed to connect to HC.",
+			    instance->address, instance->number);
+			continue;
+		}
+
+		/* Remove any old device */
+		if (instance->attached_device) {
+			usb_log_debug2("Port(%p - %d): Removing device.\n",
+			    instance->address, instance->number);
+			uhci_port_remove_device(instance);
+		}
+
+		if ((port_status & STATUS_CONNECTED) != 0) {
+			/* New device */
+			const usb_speed_t speed =
+			    ((port_status & STATUS_LOW_SPEED) != 0) ?
+			    USB_SPEED_LOW : USB_SPEED_FULL;
+			uhci_port_new_device(instance, speed);
+		} else {
+			/* Write one to WC bits, to ack changes */
+			port_status_write(instance->address, port_status);
+			usb_log_debug("Port(%p - %d): Change status ACK.\n",
+			    instance->address, instance->number);
+		}
+
+		rc = usb_hc_connection_close(&instance->hc_connection);
+		if (rc != EOK) {
+			usb_log_error("Port(%p - %d): Failed to disconnect.",
+			    instance->address, instance->number);
+		}
+	}
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 /** Callback for enabling port during adding a new device.
  *
@@ -159,5 +183,5 @@
  * @return Error code.
  */
-static int new_device_enable_port(int portno, void *arg)
+int uhci_port_reset_enable(int portno, void *arg)
 {
 	uhci_port_t *port = (uhci_port_t *) arg;
@@ -184,6 +208,5 @@
 		port_status_write(port->address, port_status);
 		async_usleep(10000);
-		port_status =
-			port_status_read(port->address);
+		port_status = port_status_read(port->address);
 		port_status &= ~STATUS_IN_RESET;
 		port_status_write(port->address, port_status);
@@ -194,10 +217,16 @@
 	/* Enable the port. */
 	uhci_port_set_enabled(port, true);
-
-	return EOK;
-}
-
-/*----------------------------------------------------------------------------*/
-static int uhci_port_new_device(uhci_port_t *port, uint16_t status)
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+/** Initializes and reports connected device.
+ *
+ * @param[in] port Memory structure to use.
+ * @param[in] speed Detected speed.
+ * @return Error code.
+ *
+ * Uses libUSB function to do the actual work.
+ */
+int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
 {
 	assert(port);
@@ -209,10 +238,9 @@
 	usb_address_t dev_addr;
 	int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
-	    ((status & STATUS_LOW_SPEED) != 0) ? USB_SPEED_LOW : USB_SPEED_FULL,
-	    new_device_enable_port, port->number, port,
+	    speed, uhci_port_reset_enable, port->number, port,
 	    &dev_addr, &port->attached_device, NULL, NULL, NULL);
 
 	if (rc != EOK) {
-		usb_log_error("Port(%p-%d): Failed(%d) adding new device: %s.\n",
+		usb_log_error("Port(%p-%d): Failed(%d) to add device: %s.\n",
 		    port->address, port->number, rc, str_error(rc));
 		uhci_port_set_enabled(port, false);
@@ -225,23 +253,32 @@
 	return EOK;
 }
-
-/*----------------------------------------------------------------------------*/
-static int uhci_port_remove_device(uhci_port_t *port)
+/*----------------------------------------------------------------------------*/
+/** Removes device.
+ *
+ * @param[in] port Memory structure to use.
+ * @return Error code.
+ *
+ * Does not work DDF does not support device removal.
+ */
+int uhci_port_remove_device(uhci_port_t *port)
 {
 	usb_log_error("Port(%p-%d): Don't know how to remove device %#x.\n",
-		port->address, port->number, (unsigned int)port->attached_device);
-//	uhci_port_set_enabled(port, false);
-	return EOK;
-}
-/*----------------------------------------------------------------------------*/
-static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
+	    port->address, port->number, (unsigned int)port->attached_device);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+/** Enables and disables port.
+ *
+ * @param[in] port Memory structure to use.
+ * @return Error code. (Always EOK)
+ */
+int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
 {
 	assert(port);
 
-	/* read register value */
-	port_status_t port_status
-		= port_status_read(port->address);
-
-	/* enable port: register write */
+	/* Read register value */
+	port_status_t port_status = port_status_read(port->address);
+
+	/* Set enabled bit */
 	if (enabled) {
 		port_status |= STATUS_ENABLED;
@@ -249,4 +286,6 @@
 		port_status &= ~STATUS_ENABLED;
 	}
+
+	/* Write new value. */
 	port_status_write(port->address, port_status);
 
