Index: uspace/srv/devman/main.c
===================================================================
--- uspace/srv/devman/main.c	(revision 0dd16778197e5f50b6e77a71d9b8aeb33b2ad863)
+++ uspace/srv/devman/main.c	(revision f9b2cb4cf365498e1fe141b1aa66950fbc3dcb17)
@@ -49,5 +49,4 @@
 #include <io/log.h>
 #include <ipc/devman.h>
-#include <ipc/driver.h>
 #include <loc.h>
 
@@ -66,23 +65,23 @@
 dev_tree_t device_tree;
 
-static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
-    bool drv_to_parent)
+static void devman_connection_device(ipc_callid_t iid, ipc_call_t *icall,
+    void *arg)
 {
 	devman_handle_t handle = IPC_GET_ARG2(*icall);
-	devman_handle_t fwd_h;
-	fun_node_t *fun = NULL;
 	dev_node_t *dev = NULL;
 	
-	fun = find_fun_node(&device_tree, handle);
-	if (fun == NULL)
+	fun_node_t *fun = find_fun_node(&device_tree, handle);
+	if (fun == NULL) {
 		dev = find_dev_node(&device_tree, handle);
-	else {
+	} else {
 		fibril_rwlock_read_lock(&device_tree.rwlock);
+		
 		dev = fun->dev;
 		if (dev != NULL)
 			dev_add_ref(dev);
+		
 		fibril_rwlock_read_unlock(&device_tree.rwlock);
 	}
-
+	
 	/*
 	 * For a valid function to connect to we need a device. The root
@@ -97,6 +96,6 @@
 		goto cleanup;
 	}
-
-	if (fun == NULL && !drv_to_parent) {
+	
+	if (fun == NULL) {
 		log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
 		    "connect to handle %" PRIun ", refers to a device.",
@@ -106,19 +105,8 @@
 	}
 	
-	driver_t *driver = NULL;
-	
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 	
-	if (drv_to_parent) {
-		/* Connect to parent function of a device (or device function). */
-		if (dev->pfun->dev != NULL)
-			driver = dev->pfun->dev->drv;
-		
-		fwd_h = dev->pfun->handle;
-	} else {
-		/* Connect to the specified function */
-		driver = dev->drv;
-		fwd_h = handle;
-	}
+	/* Connect to the specified function */
+	driver_t *driver = dev->drv;
 	
 	fibril_rwlock_read_unlock(&device_tree.rwlock);
@@ -130,10 +118,4 @@
 		goto cleanup;
 	}
-	
-	int method;
-	if (drv_to_parent)
-		method = DRIVER_DRIVER;
-	else
-		method = DRIVER_CLIENT;
 	
 	if (!driver->sess) {
@@ -143,5 +125,5 @@
 		goto cleanup;
 	}
-
+	
 	if (fun != NULL) {
 		log_msg(LOG_DEFAULT, LVL_DEBUG,
@@ -155,5 +137,5 @@
 	
 	async_exch_t *exch = async_exchange_begin(driver->sess);
-	async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
+	async_forward_fast(iid, exch, INTERFACE_DDF_CLIENT, handle, 0, IPC_FF_NONE);
 	async_exchange_end(exch);
 	
@@ -166,20 +148,95 @@
 }
 
-/** Function for handling connections from a client forwarded by the location
- * service to the device manager. */
-static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
-{
+static void devman_connection_parent(ipc_callid_t iid, ipc_call_t *icall,
+    void *arg)
+{
+	devman_handle_t handle = IPC_GET_ARG2(*icall);
+	dev_node_t *dev = NULL;
+	
+	fun_node_t *fun = find_fun_node(&device_tree, handle);
+	if (fun == NULL) {
+		dev = find_dev_node(&device_tree, handle);
+	} else {
+		fibril_rwlock_read_lock(&device_tree.rwlock);
+		
+		dev = fun->dev;
+		if (dev != NULL)
+			dev_add_ref(dev);
+		
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+	}
+	
+	/*
+	 * For a valid function to connect to we need a device. The root
+	 * function, for example, has no device and cannot be connected to.
+	 * This means @c dev needs to be valid regardless whether we are
+	 * connecting to a device or to a function.
+	 */
+	if (dev == NULL) {
+		log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
+		    "function with handle %" PRIun " was found.", handle);
+		async_answer_0(iid, ENOENT);
+		goto cleanup;
+	}
+	
+	driver_t *driver = NULL;
+	
+	fibril_rwlock_read_lock(&device_tree.rwlock);
+	
+	/* Connect to parent function of a device (or device function). */
+	if (dev->pfun->dev != NULL)
+		driver = dev->pfun->dev->drv;
+	
+	devman_handle_t fun_handle = dev->pfun->handle;
+	
+	fibril_rwlock_read_unlock(&device_tree.rwlock);
+	
+	if (driver == NULL) {
+		log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
+		    "the device %" PRIun " is not in usable state.", handle);
+		async_answer_0(iid, ENOENT);
+		goto cleanup;
+	}
+	
+	if (!driver->sess) {
+		log_msg(LOG_DEFAULT, LVL_ERROR,
+		    "Could not forward to driver `%s'.", driver->name);
+		async_answer_0(iid, EINVAL);
+		goto cleanup;
+	}
+	
+	if (fun != NULL) {
+		log_msg(LOG_DEFAULT, LVL_DEBUG,
+		    "Forwarding request for `%s' function to driver `%s'.",
+		    fun->pathname, driver->name);
+	} else {
+		log_msg(LOG_DEFAULT, LVL_DEBUG,
+		    "Forwarding request for `%s' device to driver `%s'.",
+		    dev->pfun->pathname, driver->name);
+	}
+	
+	async_exch_t *exch = async_exchange_begin(driver->sess);
+	async_forward_fast(iid, exch, INTERFACE_DDF_DRIVER, fun_handle, 0, IPC_FF_NONE);
+	async_exchange_end(exch);
+	
+cleanup:
+	if (dev != NULL)
+		dev_del_ref(dev);
+	
+	if (fun != NULL)
+		fun_del_ref(fun);
+}
+
+static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, void *arg)
+{
+	iface_t iface = IPC_GET_ARG1(*icall);
 	service_id_t service_id = IPC_GET_ARG2(*icall);
-	fun_node_t *fun;
-	dev_node_t *dev;
-	devman_handle_t handle;
-	driver_t *driver;
-
-	fun = find_loc_tree_function(&device_tree, service_id);
+	
+	fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
 	
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 	
-	if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
-		log_msg(LOG_DEFAULT, LVL_WARN, "devman_connection_loc(): function "
+	if ((fun == NULL) || (fun->dev == NULL) || (fun->dev->drv == NULL)) {
+		log_msg(LOG_DEFAULT, LVL_WARN, "devman_forward(): function "
 		    "not found.\n");
 		fibril_rwlock_read_unlock(&device_tree.rwlock);
@@ -188,49 +245,19 @@
 	}
 	
-	dev = fun->dev;
-	driver = dev->drv;
-	handle = fun->handle;
+	dev_node_t *dev = fun->dev;
+	driver_t *driver = dev->drv;
+	devman_handle_t handle = fun->handle;
 	
 	fibril_rwlock_read_unlock(&device_tree.rwlock);
 	
 	async_exch_t *exch = async_exchange_begin(driver->sess);
-	async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
-	    IPC_FF_NONE);
+	async_forward_fast(iid, exch, iface, handle, 0, IPC_FF_NONE);
 	async_exchange_end(exch);
 	
 	log_msg(LOG_DEFAULT, LVL_DEBUG,
-	    "Forwarding loc service request for `%s' function to driver `%s'.",
+	    "Forwarding service request for `%s' function to driver `%s'.",
 	    fun->pathname, driver->name);
-
+	
 	fun_del_ref(fun);
-}
-
-/** Function for handling connections to device manager. */
-static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
-{
-	/* Select port. */
-	switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
-	case DEVMAN_DRIVER:
-		devman_connection_driver(iid, icall);
-		break;
-	case DEVMAN_CLIENT:
-		devman_connection_client(iid, icall);
-		break;
-	case DEVMAN_CONNECT_TO_DEVICE:
-		/* Connect client to selected device. */
-		devman_forward(iid, icall, false);
-		break;
-	case INTERFACE_DDF_CLIENT:
-		/* Someone connected through loc node. */
-		devman_connection_loc(iid, icall);
-		break;
-	case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
-		/* Connect client to selected device. */
-		devman_forward(iid, icall, true);
-		break;
-	default:
-		/* No such interface */
-		async_answer_0(iid, ENOENT);
-	}
 }
 
@@ -298,5 +325,27 @@
 	async_set_client_data_constructor(devman_client_data_create);
 	async_set_client_data_destructor(devman_client_data_destroy);
-	async_set_fallback_port_handler(devman_connection, NULL);
+	
+	port_id_t port;
+	rc = async_create_port(INTERFACE_DDF_DRIVER,
+	    devman_connection_driver, NULL, &port);
+	if (rc != EOK)
+		return rc;
+	
+	rc = async_create_port(INTERFACE_DDF_CLIENT,
+	    devman_connection_client, NULL, &port);
+	if (rc != EOK)
+		return rc;
+	
+	rc = async_create_port(INTERFACE_DEVMAN_DEVICE,
+	    devman_connection_device, NULL, &port);
+	if (rc != EOK)
+		return rc;
+	
+	rc = async_create_port(INTERFACE_DEVMAN_PARENT,
+	    devman_connection_parent, NULL, &port);
+	if (rc != EOK)
+		return rc;
+	
+	async_set_fallback_port_handler(devman_forward, NULL);
 	
 	if (!devman_init()) {
