Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision 95ba2b84acffdac11b75bbad97e71f5b2be3c218)
+++ uspace/srv/devmap/devmap.c	(revision fc02cc41db04c79320221b96cb18bb2f916994a7)
@@ -42,4 +42,5 @@
 #include <errno.h>
 #include <bool.h>
+#include <fibril_sync.h>
 #include <stdlib.h>
 #include <string.h>
@@ -62,4 +63,6 @@
 	/** Device driver name */
 	char *name;
+	/** Fibril mutex for list of devices owned by this driver */
+	fibril_mutex_t devices_mutex;
 } devmap_driver_t;
 
@@ -92,4 +95,15 @@
 LIST_INITIALIZE(pending_req);
 
+/* Locking order:
+ *  drivers_list_mutex
+ *  devices_list_mutex
+ *  (devmap_driver_t *)->devices_mutex
+ *  create_handle_mutex
+ **/
+
+static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex);
+static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex);
+static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex);
+
 static dev_handle_t last_handle = 0;
 
@@ -100,5 +114,7 @@
 	 */
 	
+	fibril_mutex_lock(&create_handle_mutex);
 	last_handle++;
+	fibril_mutex_unlock(&create_handle_mutex);
 	
 	return last_handle;
@@ -134,4 +150,6 @@
 static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
 {
+	fibril_mutex_lock(&devices_list_mutex);
+	
 	link_t *item = (&devices_list)->next;
 	devmap_device_t *device = NULL;
@@ -144,8 +162,12 @@
 	}
 	
-	if (item == &devices_list)
+	if (item == &devices_list) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		return NULL;
+	}
 	
 	device = list_get_instance(item, devmap_device_t, devices);
+	
+	fibril_mutex_unlock(&devices_list_mutex);
 	
 	return device;
@@ -236,8 +258,11 @@
 	driver->name[name_size] = 0;
 	
+	/* Initialize mutex for list of devices owned by this driver */
+	fibril_mutex_initialize(&driver->devices_mutex);
+	
 	/*
 	 * Initialize list of asociated devices
 	 */
-	list_initialize(&(driver->devices));
+	list_initialize(&driver->devices);
 	
 	/*
@@ -262,4 +287,6 @@
 	list_initialize(&(driver->drivers));
 	
+	fibril_mutex_lock(&drivers_list_mutex);
+	
 	/* TODO:
 	 * check that no driver with name equal to driver->name is registered
@@ -270,4 +297,5 @@
 	 */
 	list_append(&(driver->drivers), &drivers_list);
+	fibril_mutex_unlock(&drivers_list_mutex);
 	
 	ipc_answer_0(iid, EOK);
@@ -286,4 +314,6 @@
 		return EEXISTS;
 	
+	fibril_mutex_lock(&drivers_list_mutex);
+	
 	if (driver->phone != 0)
 		ipc_hangup(driver->phone);
@@ -291,4 +321,8 @@
 	/* Remove it from list of drivers */
 	list_remove(&(driver->drivers));
+	
+	/* Unregister all its devices */
+	fibril_mutex_lock(&devices_list_mutex);
+	fibril_mutex_lock(&driver->devices_mutex);
 	
 	while (!list_empty(&(driver->devices))) {
@@ -298,4 +332,8 @@
 	}
 	
+	fibril_mutex_unlock(&driver->devices_mutex);
+	fibril_mutex_unlock(&devices_list_mutex);
+	fibril_mutex_unlock(&drivers_list_mutex);
+	
 	/* free name and driver */
 	if (driver->name != NULL)
@@ -311,6 +349,4 @@
 static void process_pending_lookup(void)
 {
-	async_serialize_start();
-	
 	link_t *cur;
 	
@@ -331,6 +367,4 @@
 		goto loop;
 	}
-	
-	async_serialize_end();
 }
 
@@ -386,7 +420,10 @@
 	list_initialize(&(device->driver_devices));
 	
+	fibril_mutex_lock(&devices_list_mutex);
+	
 	/* Check that device with such name is not already registered */
 	if (NULL != devmap_device_find_name(device->name)) {
 		printf(NAME ": Device '%s' already registered\n", device->name);
+		fibril_mutex_unlock(&devices_list_mutex);
 		free(device->name);
 		free(device);
@@ -403,5 +440,11 @@
 	list_append(&device->devices, &devices_list);
 	
+	/* Insert device into list of devices that belog to one driver */
+	fibril_mutex_lock(&device->driver->devices_mutex);
+	
 	list_append(&device->driver_devices, &device->driver->devices);
+	
+	fibril_mutex_unlock(&device->driver->devices_mutex);
+	fibril_mutex_unlock(&devices_list_mutex);
 	
 	ipc_answer_1(iid, EOK, device->handle);
@@ -556,9 +599,13 @@
 static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
 {
+	fibril_mutex_lock(&devices_list_mutex);
 	ipc_answer_1(iid, EOK, list_count(&devices_list));
+	fibril_mutex_unlock(&devices_list_mutex);
 }
 
 static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
 {
+	fibril_mutex_lock(&devices_list_mutex);
+	
 	ipc_callid_t callid;
 	size_t size;
@@ -604,4 +651,6 @@
 	free(desc);
 	
+	fibril_mutex_unlock(&devices_list_mutex);
+	
 	ipc_answer_1(iid, EOK, pos);
 }
@@ -627,4 +676,6 @@
 	list_initialize(&(device->driver_devices));
 	
+	fibril_mutex_lock(&devices_list_mutex);
+	
 	/* Get unique device handle */
 	device->handle = devmap_create_handle();
@@ -633,4 +684,6 @@
 	/* Insert device into list of all devices  */
 	list_append(&device->devices, &devices_list);
+	
+	fibril_mutex_unlock(&devices_list_mutex);
 	
 	return true;
@@ -656,10 +709,7 @@
 		ipc_callid_t callid = async_get_call(&call);
 		
-		async_serialize_start();
-		
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_PHONE_HUNGUP:
 			cont = false;
-			async_serialize_end();
 			continue;
 		case DEVMAP_DRIVER_UNREGISTER:
@@ -687,6 +737,4 @@
 				ipc_answer_0(callid, ENOENT);
 		}
-		
-		async_serialize_end();
 	}
 	
@@ -695,11 +743,6 @@
 		 * Unregister the device driver and all its devices.
 		 */
-		
-		async_serialize_start();
-		
 		devmap_driver_unregister(driver);
 		driver = NULL;
-		
-		async_serialize_end();
 	}
 }
@@ -718,10 +761,7 @@
 		ipc_callid_t callid = async_get_call(&call);
 		
-		async_serialize_start();
-		
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_PHONE_HUNGUP:
 			cont = false;
-			async_serialize_end();
 			continue;
 		case DEVMAP_DEVICE_GET_HANDLE:
@@ -741,6 +781,4 @@
 				ipc_answer_0(callid, ENOENT);
 		}
-		
-		async_serialize_end();
 	}
 }
