Index: uspace/app/tester/Makefile
===================================================================
--- uspace/app/tester/Makefile	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/app/tester/Makefile	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -49,4 +49,5 @@
 	loop/loop1.c \
 	mm/malloc1.c \
+	devs/devman1.c \
 	hw/misc/virtchar1.c \
 	hw/serial/serial1.c
Index: uspace/app/tester/devs/devman1.c
===================================================================
--- uspace/app/tester/devs/devman1.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
+++ uspace/app/tester/devs/devman1.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * 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 tester
+ * @brief Test devman service.
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include <inttypes.h>
+#include <errno.h>
+#include <str_error.h>
+#include <sys/types.h>
+#include <async.h>
+#include <devman.h>
+#include <str.h>
+#include <vfs/vfs.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include "../tester.h"
+
+#define DEVICE_PATH_NORMAL "/virt/null/a"
+#define DEVICE_CLASS "virt-null"
+#define DEVICE_CLASS_NAME "1"
+#define DEVICE_PATH_CLASSES DEVICE_CLASS "/" DEVICE_CLASS_NAME
+
+const char *test_devman1(void)
+{
+	devman_handle_t handle_primary;
+	devman_handle_t handle_class;
+	
+	int rc;
+	
+	TPRINTF("Asking for handle of `%s'...\n", DEVICE_PATH_NORMAL);
+	rc = devman_device_get_handle(DEVICE_PATH_NORMAL, &handle_primary, 0);
+	if (rc != EOK) {
+		TPRINTF(" ...failed: %s.\n", str_error(rc));
+		if (rc == ENOENT) {
+			TPRINTF("Have you compiled the test drivers?\n");
+		}
+		return "Failed getting device handle";
+	}
+
+	TPRINTF("Asking for handle of `%s' by class..\n", DEVICE_PATH_CLASSES);
+	rc = devman_device_get_handle_by_class(DEVICE_CLASS, DEVICE_CLASS_NAME,
+	    &handle_class, 0);
+	if (rc != EOK) {
+		TPRINTF(" ...failed: %s.\n", str_error(rc));
+		return "Failed getting device class handle";
+	}
+
+	TPRINTF("Received handles %" PRIun " and %" PRIun ".\n",
+	    handle_primary, handle_class);
+	if (handle_primary != handle_class) {
+		return "Retrieved different handles for the same device";
+	}
+
+	return NULL;
+}
+
+/** @}
+ */
Index: uspace/app/tester/devs/devman1.def
===================================================================
--- uspace/app/tester/devs/devman1.def	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
+++ uspace/app/tester/devs/devman1.def	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -0,0 +1,6 @@
+{
+	"devman1",
+	"devman test",
+	&test_devman1,
+	false
+},
Index: uspace/app/tester/tester.c
===================================================================
--- uspace/app/tester/tester.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/app/tester/tester.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -64,4 +64,5 @@
 #include "hw/serial/serial1.def"
 #include "hw/misc/virtchar1.def"
+#include "devs/devman1.def"
 	{NULL, NULL, NULL, false}
 };
Index: uspace/app/tester/tester.h
===================================================================
--- uspace/app/tester/tester.h	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/app/tester/tester.h	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -80,4 +80,5 @@
 extern const char *test_serial1(void);
 extern const char *test_virtchar1(void);
+extern const char *test_devman1(void);
 
 extern test_t tests[];
Index: uspace/drv/test1/test1.c
===================================================================
--- uspace/drv/test1/test1.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/drv/test1/test1.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -59,7 +59,8 @@
  */
 static int register_fun_verbose(ddf_dev_t *parent, const char *message,
-    const char *name, const char *match_id, int match_score)
+    const char *name, const char *match_id, int match_score,
+    int expected_rc)
 {
-	ddf_fun_t *fun;
+	ddf_fun_t *fun = NULL;
 	int rc;
 
@@ -69,13 +70,13 @@
 	if (fun == NULL) {
 		ddf_msg(LVL_ERROR, "Failed creating function %s\n", name);
-		return ENOMEM;
+		rc = ENOMEM;
+		goto leave;
 	}
 
-	rc = ddf_fun_add_match_id(fun, match_id, match_score);
+	rc = ddf_fun_add_match_id(fun, str_dup(match_id), match_score);
 	if (rc != EOK) {
 		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s\n",
 		    name);
-		ddf_fun_destroy(fun);
-		return rc;
+		goto leave;
 	}
 
@@ -84,10 +85,23 @@
 		ddf_msg(LVL_ERROR, "Failed binding function %s: %s\n", name,
 		    str_error(rc));
-		ddf_fun_destroy(fun);
-		return rc;
+		goto leave;
 	}
 
 	ddf_msg(LVL_NOTE, "Registered child device `%s'\n", name);
-	return EOK;
+	rc = EOK;
+
+leave:
+	if (rc != expected_rc) {
+		fprintf(stderr,
+		    NAME ": Unexpected error registering function `%s'.\n" 
+		    NAME ":     Expected \"%s\" but got \"%s\".\n",
+		    name, str_error(expected_rc), str_error(rc));
+	}
+
+	if ((rc != EOK) && (fun != NULL)) {
+		ddf_fun_destroy(fun);
+	}
+
+	return rc;
 }
 
@@ -135,9 +149,14 @@
 		ddf_fun_add_to_class(fun_a, "virt-null");
 	} else if (str_cmp(dev->name, "test1") == 0) {
-		(void) register_fun_verbose(dev, "cloning myself ;-)", "clone",
-		    "virtual&test1", 10);
+		(void) register_fun_verbose(dev,
+		    "cloning myself ;-)", "clone",
+		    "virtual&test1", 10, EOK);
+		(void) register_fun_verbose(dev,
+		    "cloning myself twice ;-)", "clone",
+		    "virtual&test1", 10, EEXISTS);
 	} else if (str_cmp(dev->name, "clone") == 0) {
-		(void) register_fun_verbose(dev, "run by the same task", "child",
-		    "virtual&test1&child", 10);
+		(void) register_fun_verbose(dev,
+		    "run by the same task", "child",
+		    "virtual&test1&child", 10, EOK);
 	}
 
Index: uspace/lib/c/generic/devman.c
===================================================================
--- uspace/lib/c/generic/devman.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/lib/c/generic/devman.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -147,6 +147,4 @@
 		ret = devman_send_match_id(phone, match_id);
 		if (ret != EOK) {
-			printf("Driver failed to send match id, error %d\n",
-			    ret);
 			return ret;
 		}
@@ -195,10 +193,15 @@
 	}
 	
-	devman_send_match_ids(phone, match_ids);
-	
-	async_wait_for(req, &retval);
-	
-	async_serialize_end();
-	
+	int match_ids_rc = devman_send_match_ids(phone, match_ids);
+	
+	async_wait_for(req, &retval);
+	
+	async_serialize_end();
+	
+	/* Prefer the answer to DEVMAN_ADD_FUNCTION in case of errors. */
+	if ((match_ids_rc != EOK) && (retval == EOK)) {
+		retval = match_ids_rc;
+	}
+
 	if (retval == EOK)
 		fun_handle = (int) IPC_GET_ARG1(answer);
@@ -326,4 +329,49 @@
 }
 
+int devman_device_get_handle_by_class(const char *classname,
+    const char *devname, devman_handle_t *handle, unsigned int flags)
+{
+	int phone = devman_get_phone(DEVMAN_CLIENT, flags);
+
+	if (phone < 0)
+		return phone;
+
+	async_serialize_start();
+
+	ipc_call_t answer;
+	aid_t req = async_send_1(phone, DEVMAN_DEVICE_GET_HANDLE_BY_CLASS,
+	    flags, &answer);
+
+	sysarg_t retval = async_data_write_start(phone, classname,
+	    str_size(classname));
+	if (retval != EOK) {
+		async_wait_for(req, NULL);
+		async_serialize_end();
+		return retval;
+	}
+	retval = async_data_write_start(phone, devname,
+	    str_size(devname));
+	if (retval != EOK) {
+		async_wait_for(req, NULL);
+		async_serialize_end();
+		return retval;
+	}
+
+	async_wait_for(req, &retval);
+
+	async_serialize_end();
+
+	if (retval != EOK) {
+		if (handle != NULL)
+			*handle = (devman_handle_t) -1;
+		return retval;
+	}
+
+	if (handle != NULL)
+		*handle = (devman_handle_t) IPC_GET_ARG1(answer);
+
+	return retval;
+}
+
 
 /** @}
Index: uspace/lib/c/include/devman.h
===================================================================
--- uspace/lib/c/include/devman.h	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/lib/c/include/devman.h	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -53,4 +53,6 @@
 extern int devman_device_get_handle(const char *, devman_handle_t *,
     unsigned int);
+extern int devman_device_get_handle_by_class(const char *, const char *,
+    devman_handle_t *, unsigned int);
 
 extern int devman_add_device_to_class(devman_handle_t, const char *);
Index: uspace/lib/c/include/ipc/devman.h
===================================================================
--- uspace/lib/c/include/ipc/devman.h	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/lib/c/include/ipc/devman.h	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -148,5 +148,6 @@
 
 typedef enum {
-	DEVMAN_DEVICE_GET_HANDLE = IPC_FIRST_USER_METHOD
+	DEVMAN_DEVICE_GET_HANDLE = IPC_FIRST_USER_METHOD,
+	DEVMAN_DEVICE_GET_HANDLE_BY_CLASS
 } client_to_devman_t;
 
Index: uspace/srv/devman/devman.c
===================================================================
--- uspace/srv/devman/devman.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/srv/devman/devman.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -1173,4 +1173,63 @@
 }
 
+/** Find function with a specified name belonging to given device.
+ *
+ * Device tree rwlock should be held at least for reading.
+ *
+ * @param dev Device the function belongs to.
+ * @param name Function name (not path).
+ * @return Function node.
+ * @retval NULL No function with given name.
+ */
+fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
+{
+	assert(dev != NULL);
+	assert(name != NULL);
+
+	fun_node_t *fun;
+	link_t *link;
+
+	for (link = dev->functions.next;
+	    link != &dev->functions;
+	    link = link->next) {
+		fun = list_get_instance(link, fun_node_t, dev_functions);
+
+		if (str_cmp(name, fun->name) == 0)
+			return fun;
+	}
+
+	return NULL;
+}
+
+/** Find function node by its class name and index. */
+fun_node_t *find_fun_node_by_class(class_list_t *class_list,
+    const char *class_name, const char *dev_name)
+{
+	assert(class_list != NULL);
+	assert(class_name != NULL);
+	assert(dev_name != NULL);
+
+	fibril_rwlock_read_lock(&class_list->rwlock);
+
+	dev_class_t *cl = find_dev_class_no_lock(class_list, class_name);
+	if (cl == NULL) {
+		fibril_rwlock_read_unlock(&class_list->rwlock);
+		return NULL;
+	}
+
+	dev_class_info_t *dev = find_dev_in_class(cl, dev_name);
+	if (dev == NULL) {
+		fibril_rwlock_read_unlock(&class_list->rwlock);
+		return NULL;
+	}
+
+	fun_node_t *fun = dev->fun;
+
+	fibril_rwlock_read_unlock(&class_list->rwlock);
+
+	return fun;
+}
+
+
 /** Find child function node with a specified name.
  *
@@ -1183,19 +1242,5 @@
 fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
 {
-	fun_node_t *fun;
-	link_t *link;
-	
-	link = pfun->child->functions.next;
-	
-	while (link != &pfun->child->functions) {
-		fun = list_get_instance(link, fun_node_t, dev_functions);
-		
-		if (str_cmp(name, fun->name) == 0)
-			return fun;
-		
-		link = link->next;
-	}
-	
-	return NULL;
+	return find_fun_node_in_device(pfun->child, name);
 }
 
@@ -1359,4 +1404,24 @@
 }
 
+dev_class_info_t *find_dev_in_class(dev_class_t *dev_class, const char *dev_name)
+{
+	assert(dev_class != NULL);
+	assert(dev_name != NULL);
+
+	link_t *link;
+	for (link = dev_class->devices.next;
+	    link != &dev_class->devices;
+	    link = link->next) {
+		dev_class_info_t *dev = list_get_instance(link,
+		    dev_class_info_t, link);
+
+		if (str_cmp(dev->dev_name, dev_name) == 0) {
+			return dev;
+		}
+	}
+
+	return NULL;
+}
+
 void init_class_list(class_list_t *class_list)
 {
Index: uspace/srv/devman/devman.h
===================================================================
--- uspace/srv/devman/devman.h	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/srv/devman/devman.h	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -338,4 +338,6 @@
 extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
 extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
+extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
+extern fun_node_t *find_fun_node_by_class(class_list_t *, const char *, const char *);
 
 /* Device tree */
@@ -359,4 +361,5 @@
 extern dev_class_t *get_dev_class(class_list_t *, char *);
 extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
+extern dev_class_info_t *find_dev_in_class(dev_class_t *, const char *);
 extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
 
Index: uspace/srv/devman/main.c
===================================================================
--- uspace/srv/devman/main.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/srv/devman/main.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -248,4 +248,14 @@
 	}
 	
+	/* Check that function with same name is not there already. */
+	if (find_fun_node_in_device(pdev, fun_name) != NULL) {
+		fibril_rwlock_write_unlock(&tree->rwlock);
+		async_answer_0(callid, EEXISTS);
+		printf(NAME ": Warning, driver tried to register `%s' twice.\n",
+		    fun_name);
+		free(fun_name);
+		return;
+	}
+
 	fun_node_t *fun = create_fun_node();
 	if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
@@ -443,4 +453,38 @@
 }
 
+/** Find handle for the device instance identified by device class name. */
+static void devman_function_get_handle_by_class(ipc_callid_t iid,
+    ipc_call_t *icall)
+{
+	char *classname;
+	char *devname;
+
+	int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+	rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0);
+	if (rc != EOK) {
+		free(classname);
+		async_answer_0(iid, rc);
+		return;
+	}
+
+
+	fun_node_t *fun = find_fun_node_by_class(&class_list,
+	    classname, devname);
+
+	free(classname);
+	free(devname);
+
+	if (fun == NULL) {
+		async_answer_0(iid, ENOENT);
+		return;
+	}
+
+	async_answer_1(iid, EOK, fun->handle);
+}
+
 
 /** Function for handling connections from a client to the device manager. */
@@ -461,4 +505,7 @@
 		case DEVMAN_DEVICE_GET_HANDLE:
 			devman_function_get_handle(callid, &call);
+			break;
+		case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
+			devman_function_get_handle_by_class(callid, &call);
 			break;
 		default:
Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision cfc30274cc1a9f886f2e2f3f7692467e244a4c80)
+++ uspace/srv/devmap/devmap.c	(revision bdbb6f6e2d2ba190ccc8ccec3897b458fa47eada)
@@ -551,5 +551,5 @@
 	if (devmap_device_find_name(namespace->name, device->name) != NULL) {
 		printf("%s: Device '%s/%s' already registered\n", NAME,
-		    device->namespace->name, device->name);
+		    namespace->name, device->name);
 		devmap_namespace_destroy(namespace);
 		fibril_mutex_unlock(&devices_list_mutex);
