Index: uspace/lib/libc/Makefile
===================================================================
--- uspace/lib/libc/Makefile	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/lib/libc/Makefile	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -53,4 +53,5 @@
 	generic/clipboard.c \
 	generic/devmap.c \
+	generic/devman.c \
 	generic/event.c \
 	generic/errno.c \
Index: uspace/lib/libc/generic/devman.c
===================================================================
--- uspace/lib/libc/generic/devman.c	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
+++ uspace/lib/libc/generic/devman.c	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2007 Josef Cejka
+ * Copyright (c) 2009 Jiri Svoboda
+ * Copyright (c) 2010 Lenka Trochtova
+ * 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.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <ipc/ipc.h>
+#include <ipc/services.h>
+#include <ipc/devman.h>
+#include <devman.h>
+#include <async.h>
+#include <errno.h>
+#include <malloc.h>
+#include <bool.h>
+
+static int devman_phone_driver = -1;
+static int devman_phone_client = -1;
+
+int devman_get_phone(devman_interface_t iface, unsigned int flags)
+{
+	switch (iface) {
+	case DEVMAN_DRIVER:
+		if (devman_phone_driver >= 0)
+			return devman_phone_driver;
+		
+		if (flags & IPC_FLAG_BLOCKING)
+			devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
+			    SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
+		else
+			devman_phone_driver = ipc_connect_me_to(PHONE_NS,
+			    SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
+		
+		return devman_phone_driver;
+	case DEVMAN_CLIENT:
+		if (devman_phone_client >= 0)
+			return devman_phone_client;
+		
+		if (flags & IPC_FLAG_BLOCKING)
+			devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
+			    SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
+		else
+			devman_phone_client = ipc_connect_me_to(PHONE_NS,
+			    SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
+		
+		return devman_phone_client;
+	default:
+		return -1;
+	}
+}
+
+/** Register running driver with device manager. */
+int devman_driver_register(const char *name, async_client_conn_t conn)
+{
+	int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return phone;
+	
+	async_serialize_start();
+	
+	ipc_call_t answer;
+	aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
+	
+	printf("devman_driver_register (\"%s\", conn)", name);
+	ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
+	if (retval != EOK) {
+		async_wait_for(req, NULL);
+		async_serialize_end();
+		return -1;
+	}
+	
+	async_set_client_connection(conn);
+	
+	ipcarg_t callback_phonehash;
+	ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
+	async_wait_for(req, &retval);
+	
+	async_serialize_end();
+	
+	return retval;
+}
+
+void devman_hangup_phone(devman_interface_t iface)
+{
+	switch (iface) {
+	case DEVMAN_DRIVER:
+		if (devman_phone_driver >= 0) {
+			ipc_hangup(devman_phone_driver);
+			devman_phone_driver = -1;
+		}
+		break;
+	case DEVMAN_CLIENT:
+		if (devman_phone_client >= 0) {
+			ipc_hangup(devman_phone_client);
+			devman_phone_client = -1;
+		}
+		break;
+	default:
+		break;
+	}
+}
Index: uspace/lib/libc/include/devman.h
===================================================================
--- uspace/lib/libc/include/devman.h	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
+++ uspace/lib/libc/include/devman.h	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009 Jiri Svoboda
+ * Copyright (c) 2010 Lenka Trochtova * 
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_DEVMAN_H_
+#define LIBC_DEVMAN_H_
+
+#include <ipc/devman.h>
+#include <async.h>
+#include <bool.h>
+
+extern int devman_get_phone(devman_interface_t, unsigned int);
+extern void devman_hangup_phone(devman_interface_t iface);
+
+extern int devman_driver_register(const char *, async_client_conn_t);
+
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/libc/include/ipc/devman.h
===================================================================
--- uspace/lib/libc/include/ipc/devman.h	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/lib/libc/include/ipc/devman.h	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -36,4 +36,6 @@
 #include <ipc/ipc.h>
 
+#define DEVMAN_NAME_MAXLEN 256
+
 typedef enum {
 	DEVMAN_DRIVER = 1,
Index: uspace/srv/devman/devman.c
===================================================================
--- uspace/srv/devman/devman.c	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/devman/devman.c	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -348,4 +348,5 @@
 	
 	fibril_mutex_lock(&drivers_list->drivers_mutex);
+	
 	link_t *link = drivers_list->drivers.next;		
 	while (link != &drivers_list->drivers) {
@@ -358,4 +359,5 @@
 		link = link->next;
 	}
+	
 	fibril_mutex_unlock(&drivers_list->drivers_mutex);
 	
@@ -404,4 +406,26 @@
 	drv->state = DRIVER_STARTING;
 	return true;
+}
+
+driver_t * find_driver(driver_list_t *drv_list, const char *drv_name) 
+{	
+	driver_t *res = NULL;
+	
+	fibril_mutex_lock(&drv_list->drivers_mutex);	
+	
+	driver_t *drv = NULL;
+	link_t *link = drv_list->drivers.next; 	
+	while (link !=  &drv_list->drivers) {
+		drv = list_get_instance(link, driver_t, drivers);
+		if (0 == str_cmp(drv->name, drv_name)) {
+			res = drv;
+			break;
+		}		
+		link = link->next;
+	}	
+	
+	fibril_mutex_unlock(&drv_list->drivers_mutex);
+	
+	return res;
 }
 
Index: uspace/srv/devman/devman.h
===================================================================
--- uspace/srv/devman/devman.h	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/devman/devman.h	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -168,5 +168,9 @@
 }
 
-// Driver list
+
+
+
+
+// Drivers
 
 static inline void init_driver_list(driver_list_t *drv_list) 
@@ -178,6 +182,4 @@
 }
 
-// Drivers
-
 driver_t * create_driver();
 bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
@@ -191,4 +193,6 @@
 bool add_device(driver_t *drv, node_t *node);
 bool start_driver(driver_t *drv);
+
+driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
 
 
Index: uspace/srv/devman/main.c
===================================================================
--- uspace/srv/devman/main.c	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/devman/main.c	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -59,4 +59,88 @@
 static dev_tree_t device_tree;
 
+/**
+ * 
+ * 
+ * Driver's mutex must be locked.
+ */
+static void pass_devices_to_driver(driver_t *driver)
+{
+	
+
+	
+	
+}
+
+static void init_running_driver(driver_t *driver) 
+{
+	fibril_mutex_lock(&driver->driver_mutex);
+	
+	// pass devices which have been already assigned to the driver to the driver
+	pass_devices_to_driver(driver);	
+	
+	// change driver's state to running
+	driver->state = DRIVER_RUNNING;	
+	
+	fibril_mutex_unlock(&driver->driver_mutex);
+}
+
+/**
+ * Register running driver.
+ */
+static driver_t * devman_driver_register(void)
+{	
+	printf(NAME ": devman_driver_register \n");
+	
+	ipc_call_t icall;
+	ipc_callid_t iid = async_get_call(&icall);
+	driver_t *driver = NULL;
+	
+	if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
+		ipc_answer_0(iid, EREFUSED);
+		return NULL;
+	}
+	
+	char drv_name[DEVMAN_NAME_MAXLEN];
+	
+	// Get driver name
+	int rc = async_string_receive(drv_name, DEVMAN_NAME_MAXLEN, NULL);	
+	if (rc != EOK) {
+		ipc_answer_0(iid, rc);
+		return NULL;
+	}
+	printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
+	
+	// Find driver structure
+	driver = find_driver(&drivers_list, drv_name);
+	if (NULL == driver) {
+		printf(NAME ": no driver named %s was found.\n", drv_name);
+		ipc_answer_0(iid, ENOENT);
+		return NULL;
+	}
+	printf(NAME ": registering the running instance of the %s driver.\n", driver->name);
+	
+	// Create connection to the driver
+	printf(NAME ":  creating connection to the %s driver.\n", driver->name);	
+	ipc_call_t call;
+	ipc_callid_t callid = async_get_call(&call);		
+	if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
+		ipc_answer_0(callid, ENOTSUP);
+		ipc_answer_0(iid, ENOTSUP);
+		return NULL;
+	}
+	
+	fibril_mutex_lock(&driver->driver_mutex);
+	assert(DRIVER_STARTING == driver->state);
+	driver->phone = IPC_GET_ARG5(call);	
+	fibril_mutex_unlock(&driver->driver_mutex);	
+	
+	printf(NAME ":  the %s driver was successfully registered as running.\n", driver->name);
+	
+	ipc_answer_0(callid, EOK);	
+	
+	ipc_answer_0(iid, EOK);
+	
+	return driver;
+}
 
 /** Function for handling connections to device manager.
@@ -64,18 +148,24 @@
  */
 static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
-{
+{	
+	/* Accept the connection */
+	ipc_answer_0(iid, EOK);
+	
+	driver_t *driver = devman_driver_register();
+	if (driver == NULL)
+		return;
+		
+	init_running_driver(driver);
+	
 	ipc_callid_t callid;
 	ipc_call_t call;
-	
-	/* Accept the connection */
-	ipc_answer_0(iid, EOK);
-	
-	while (1) {
+	bool cont = true;
+	while (cont) {
 		callid = async_get_call(&call);
 		
 		switch (IPC_GET_METHOD(call)) {
-		case DEVMAN_DRIVER_REGISTER:
-			// TODO register running driver instance - remember driver's phone and lookup devices to which it has been assigned
-			break;
+		case IPC_M_PHONE_HUNGUP:
+			cont = false;
+			continue;
 		case DEVMAN_ADD_CHILD_DEVICE:
 			// TODO add new device node to the device tree
Index: uspace/srv/devman/util.h
===================================================================
--- uspace/srv/devman/util.h	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/devman/util.h	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -57,5 +57,5 @@
 }
 
-static inline void free_not_null(void *ptr)
+static inline void free_not_null(const void *ptr)
 {
 	if (NULL != ptr) {
Index: uspace/srv/drivers/root/Makefile
===================================================================
--- uspace/srv/drivers/root/Makefile	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/drivers/root/Makefile	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -28,11 +28,9 @@
 USPACE_PREFIX = ../../..
 LIBS = $(LIBC_PREFIX)/libc.a
-EXTRA_CFLAGS = -Iinclude
 
-OUTPUT = root 
+OUTPUT = root
 
 SOURCES = \
-	root.c \
+	root.c
 
 include ../../Makefile.common
-
Index: uspace/srv/drivers/root/root.c
===================================================================
--- uspace/srv/drivers/root/root.c	(revision 924c75e15c9b616be210fa9c2c5f37204c6c2feb)
+++ uspace/srv/drivers/root/root.c	(revision 729fa2d6e7839f9070d458529df329b34e01afb8)
@@ -48,4 +48,5 @@
 #include <ctype.h>
 
+#include <devman.h>
 #include <ipc/devman.h>
 
@@ -149,116 +150,58 @@
 
 
-// TODO put this to library (like in device mapper)
-static int devman_phone_driver = -1;
-static int devman_phone_client = -1;
-
-int devman_get_phone(devman_interface_t iface, unsigned int flags)
-{
-	switch (iface) {
-	case DEVMAN_DRIVER:
-		if (devman_phone_driver >= 0)
-			return devman_phone_driver;
-		
-		if (flags & IPC_FLAG_BLOCKING)
-			devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
-			    SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
-		else
-			devman_phone_driver = ipc_connect_me_to(PHONE_NS,
-			    SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
-		
-		return devman_phone_driver;
-	case DEVMAN_CLIENT:
-		if (devman_phone_client >= 0)
-			return devman_phone_client;
-		
-		if (flags & IPC_FLAG_BLOCKING)
-			devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
-			    SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
-		else
-			devman_phone_client = ipc_connect_me_to(PHONE_NS,
-			    SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
-		
-		return devman_phone_client;
-	default:
+
+int driver_main(driver_t *drv) 
+{
+	// remember driver structure - driver_ops will be called by generic handler for incoming connections
+	driver = drv;
+	
+	// register driver by device manager with generic handler for incoming connections
+	printf("%s: sending registration request to devman.\n", driver->name);
+	devman_driver_register(driver->name, driver_connection);		
+
+	async_manager();
+
+	// Never reached
+	return 0;
+	
+}
+
+////////////////////////////////////////
+// ROOT driver 
+////////////////////////////////////////
+
+#define NAME "root"
+
+
+
+bool root_add_device(device_t *dev) 
+{
+	// TODO add root device and register its children
+	return true;
+}
+
+static driver_ops_t root_ops = {
+	.add_device = &root_add_device
+};
+
+static driver_t root_driver = {
+	.name = NAME,
+	.driver_ops = &root_ops
+};
+
+bool root_init() 
+{
+	// TODO  driver initialization	
+	return true;
+}
+
+int main(int argc, char *argv[])
+{
+	printf(NAME ": HelenOS root device driver\n");
+	if (!root_init()) {
+		printf(NAME ": Error while initializing driver.\n");
 		return -1;
 	}
-}
-
-/** Register new driver with device manager. */
-int devman_driver_register(const char *name, async_client_conn_t conn)
-{
-	int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
-	
-	if (phone < 0)
-		return phone;
-	
-	async_serialize_start();
-	
-	ipc_call_t answer;
-	aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
-	
-	ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
-	if (retval != EOK) {
-		async_wait_for(req, NULL);
-		async_serialize_end();
-		return -1;
-	}
-	
-	async_set_client_connection(conn);
-	
-	ipcarg_t callback_phonehash;
-	ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
-	async_wait_for(req, &retval);
-	
-	async_serialize_end();
-	
-	return retval;
-}
-
-int driver_main(driver_t *drv) 
-{
-	// remember driver structure - driver_ops will be called by generic handler for incoming connections
-	driver = drv;
-	
-	// register driver by device manager with generic handler for incoming connections
-	devman_driver_register(driver->name, driver_connection);		
-
-	async_manager();
-
-	// Never reached
-	return 0;
-	
-}
-
-////////////////////////////////////////
-// ROOT driver 
-////////////////////////////////////////
-
-#define NAME "root"
-
-static driver_t root_driver;
-
-bool root_add_device(device_t *dev) 
-{
-	// TODO
-	return true;
-}
-
-bool root_init(driver_t *drv, const char *name) 
-{
-	// TODO  initialize driver structure
-	
-	return true;
-}
-
-int main(int argc, char *argv[])
-{
-	printf(NAME ": HelenOS root device driver\n");
-	if (!root_init(&root_driver, argv[0])) {
-		printf(NAME ": Error while initializing driver\n");
-		return -1;
-	}
 	
 	return driver_main(&root_driver);
-
-}
+}
