Index: uspace/app/bdsh/cmds/modules/cat/cat.c
===================================================================
--- uspace/app/bdsh/cmds/modules/cat/cat.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/bdsh/cmds/modules/cat/cat.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -164,5 +164,4 @@
 {
 	int fd, bytes = 0, count = 0, reads = 0;
-	off64_t total = 0;
 	char *buff = NULL;
 	int i;
@@ -174,7 +173,4 @@
 		return 1;
 	}
-
-	total = lseek(fd, 0, SEEK_END);
-	lseek(fd, 0, SEEK_SET);
 
 	if (NULL == (buff = (char *) malloc(blen + 1))) {
Index: uspace/app/bdsh/cmds/modules/ls/ls.c
===================================================================
--- uspace/app/bdsh/cmds/modules/ls/ls.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/bdsh/cmds/modules/ls/ls.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -38,7 +38,9 @@
 #include <dirent.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <str.h>
+#include <sort.h>
 
 #include "errors.h"
@@ -46,64 +48,160 @@
 #include "util.h"
 #include "entry.h"
-#include "ls.h"
 #include "cmds.h"
 
+/* Various values that can be returned by ls_scope() */
+#define LS_BOGUS 0
+#define LS_FILE  1
+#define LS_DIR   2
+
+/** Structure to represent a directory entry.
+ *
+ * Useful to keep together important information
+ * for sorting directory entries.
+ */
+struct dir_elem_t {
+	char *name;
+	struct stat s;
+};
+
 static const char *cmdname = "ls";
 
-static void ls_scan_dir(const char *d, DIR *dirp)
-{
+static struct option const long_options[] = {
+	{ "help", no_argument, 0, 'h' },
+	{ "unsort", no_argument, 0, 'u' },
+	{ 0, 0, 0, 0 }
+};
+
+/** Print an entry.
+ *
+ * ls_print currently does nothing more than print the entry.
+ * In the future, we will likely pass the absolute path, and
+ * some sort of ls_options structure that controls how each
+ * entry is printed and what is printed about it.
+ *
+ * Now we just print basic DOS style lists.
+ *
+ * @param de		Directory element.
+ */
+static void ls_print(struct dir_elem_t *de)
+{
+	if (de->s.is_file)
+		printf("%-40s\t%llu\n", de->name, (long long) de->s.size);
+	else if (de->s.is_directory)
+		printf("%-40s\t<dir>\n", de->name);
+	else
+		printf("%-40s\n", de->name);
+}
+
+
+/** Compare 2 directory elements.
+ *
+ * It compares 2 elements of a directory : a file is considered
+ * as bigger than a directory, and if they have the same type,
+ * they are compared alphabetically.
+ *
+ * @param a		Pointer to the structure of the first element.
+ * @param b		Pointer to the structure of the second element.
+ * @param arg		Pointer for an other and optionnal argument.
+ *
+ * @return		-1 if a < b, 1 otherwise.
+ */
+static int ls_cmp(void *a, void *b, void *arg)
+{
+	struct dir_elem_t *da = a;
+	struct dir_elem_t *db = b;
+	
+	if ((da->s.is_directory && db->s.is_file) ||
+	    ((da->s.is_directory == db->s.is_directory) &&
+	    str_cmp(da->name, db->name) < 0))
+		return -1;
+	else
+		return 1;
+}
+
+/** Scan a directory.
+ *
+ * Scan the content of a directory and print it.
+ *
+ * @param d		Name of the directory.
+ * @param dirp	Directory stream.
+ * @param sort	1 if the output must be sorted,
+ *				0 otherwise.
+ */
+static void ls_scan_dir(const char *d, DIR *dirp, int sort)
+{
+	int alloc_blocks = 20;
+	int i;
+	int nbdirs = 0;
+	int rc;
+	int len;
+	char *buff;
+	struct dir_elem_t *tmp;
+	struct dir_elem_t *tosort;
 	struct dirent *dp;
-	char *buff;
-
-	if (! dirp)
+	
+	if (!dirp)
 		return;
 
-	buff = (char *)malloc(PATH_MAX);
-	if (NULL == buff) {
+	buff = (char *) malloc(PATH_MAX);
+	if (!buff) {
 		cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
 		return;
 	}
-
+	
+	tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort));
+	if (!tosort) {
+		cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
+		free(buff);
+		return;
+	}
+	
 	while ((dp = readdir(dirp))) {
-		memset(buff, 0, sizeof(buff));
-		/* Don't worry if inserting a double slash, this will be fixed by
-		 * absolutize() later with subsequent calls to open() or readdir() */
-		snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name);
-		ls_print(dp->d_name, buff);
-	}
-
+		if (nbdirs + 1 > alloc_blocks) {
+			alloc_blocks += alloc_blocks;
+			
+			tmp = (struct dir_elem_t *) realloc(tosort,
+			    alloc_blocks * sizeof(struct dir_elem_t));
+			if (!tmp) {
+				cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
+				goto out;
+			}
+			tosort = tmp;
+		}
+		
+		/* fill the name field */
+		tosort[nbdirs].name = (char *) malloc(str_length(dp->d_name) + 1);
+		if (!tosort[nbdirs].name) {
+			cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
+			goto out;
+		}
+		
+		str_cpy(tosort[nbdirs].name, str_length(dp->d_name) + 1, dp->d_name);
+		len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name);
+		buff[len] = '\0';
+
+		rc = stat(buff, &tosort[nbdirs++].s);
+		if (rc != 0) {
+			printf("ls: skipping bogus node %s\n", buff);
+			printf("rc=%d\n", rc);
+			goto out;
+		}
+	}
+	
+	if (sort) {
+		if (!qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t),
+		    ls_cmp, NULL)) {
+			printf("Sorting error.\n");
+		}
+	}
+	
+	for (i = 0; i < nbdirs; i++)
+		ls_print(&tosort[i]);
+	
+out:
+	for(i = 0; i < nbdirs; i++)
+		free(tosort[i].name);
+	free(tosort);
 	free(buff);
-
-	return;
-}
-
-/* ls_print currently does nothing more than print the entry.
- * in the future, we will likely pass the absolute path, and
- * some sort of ls_options structure that controls how each
- * entry is printed and what is printed about it.
- *
- * Now we just print basic DOS style lists */
-
-static void ls_print(const char *name, const char *pathname)
-{
-	struct stat s;
-	int rc;
-
-	rc = stat(pathname, &s);
-	if (rc != 0) {
-		/* Odd chance it was deleted from the time readdir() found it */
-		printf("ls: skipping bogus node %s\n", pathname);
-		printf("rc=%d\n", rc);
-		return;
-	}
-	
-	if (s.is_file)
-		printf("%-40s\t%llu\n", name, (long long) s.size);
-	else if (s.is_directory)
-		printf("%-40s\t<dir>\n", name);
-	else
-		printf("%-40s\n", name);
-
-	return;
 }
 
@@ -114,6 +212,11 @@
 	} else {
 		help_cmd_ls(HELP_SHORT);
-		printf("  `%s' [path], if no path is given the current "
-				"working directory is used.\n", cmdname);
+		printf(
+		"Usage:  %s [options] [path]\n"
+		"If not path is given, the current working directory is used.\n"
+		"Options:\n"
+		"  -h, --help       A short option summary\n"
+		"  -u, --unsort     Do not sort directory entries\n",
+		cmdname);
 	}
 
@@ -124,43 +227,58 @@
 {
 	unsigned int argc;
-	struct stat s;
-	char *buff;
+	struct dir_elem_t de;
 	DIR *dirp;
+	int c, opt_ind;
+	int sort = 1;
 
 	argc = cli_count_args(argv);
-
-	buff = (char *) malloc(PATH_MAX);
-	if (NULL == buff) {
+	
+	for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
+		c = getopt_long(argc, argv, "hu", long_options, &opt_ind);
+		switch (c) {
+		case 'h':
+			help_cmd_ls(HELP_LONG);
+			return CMD_SUCCESS;
+		case 'u':
+			sort = 0;
+			break;
+		}
+	}
+	
+	argc -= optind;
+	
+	de.name = (char *) malloc(PATH_MAX);
+	if (!de.name) {
 		cli_error(CL_ENOMEM, "%s: ", cmdname);
 		return CMD_FAILURE;
 	}
-	memset(buff, 0, sizeof(buff));
-
-	if (argc == 1)
-		getcwd(buff, PATH_MAX);
+	memset(de.name, 0, sizeof(PATH_MAX));
+	
+	if (argc == 0)
+		getcwd(de.name, PATH_MAX);
 	else
-		str_cpy(buff, PATH_MAX, argv[1]);
-
-	if (stat(buff, &s)) {
-		cli_error(CL_ENOENT, buff);
-		free(buff);
+		str_cpy(de.name, PATH_MAX, argv[optind]);
+	
+	if (stat(de.name, &de.s)) {
+		cli_error(CL_ENOENT, de.name);
+		free(de.name);
 		return CMD_FAILURE;
 	}
 
-	if (s.is_file) {
-		ls_print(buff, buff);
+	if (de.s.is_file) {
+		ls_print(&de);
 	} else {
-		dirp = opendir(buff);
+		dirp = opendir(de.name);
 		if (!dirp) {
 			/* May have been deleted between scoping it and opening it */
-			cli_error(CL_EFAIL, "Could not stat %s", buff);
-			free(buff);
+			cli_error(CL_EFAIL, "Could not stat %s", de.name);
+			free(de.name);
 			return CMD_FAILURE;
 		}
-		ls_scan_dir(buff, dirp);
+		ls_scan_dir(de.name, dirp, sort);
 		closedir(dirp);
 	}
 
-	free(buff);
+	free(de.name);
 
 	return CMD_SUCCESS;
Index: uspace/app/bdsh/cmds/modules/ls/ls.h
===================================================================
--- uspace/app/bdsh/cmds/modules/ls/ls.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ 	(revision )
@@ -1,13 +1,0 @@
-#ifndef LS_H
-#define LS_H
-
-/* Various values that can be returned by ls_scope() */
-#define LS_BOGUS 0
-#define LS_FILE  1
-#define LS_DIR   2
-
-static void ls_scan_dir(const char *, DIR *);
-static void ls_print(const char *, const char *);
-
-#endif /* LS_H */
-
Index: uspace/app/init/init.c
===================================================================
--- uspace/app/init/init.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/init/init.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -314,4 +314,6 @@
 	getterm("term/vc6", "/app/klog", false);
 
+#ifdef CONFIG_START_DEVMAN
+
 #ifdef CONFIG_DEVMAN_EARLY_LAUNCH
 	spawn("/srv/devman");
@@ -320,4 +322,6 @@
 #endif
 
+#endif
+
 	return 0;
 }
Index: uspace/app/sbi/src/run_expr.c
===================================================================
--- uspace/app/sbi/src/run_expr.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/sbi/src/run_expr.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -2529,4 +2529,6 @@
 	if (rc1 == EOK)
 		rc2 = os_str_get_char(string->value, elem_index, &cval);
+	else
+		rc2 = EOK;
 
 	if (rc1 != EOK || rc2 != EOK) {
Index: uspace/app/tester/Makefile
===================================================================
--- uspace/app/tester/Makefile	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/tester/Makefile	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -54,4 +54,5 @@
 	mm/malloc1.c \
 	mm/mapping1.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 f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/app/tester/devs/devman1.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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 f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/app/tester/devs/devman1.def	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,6 @@
+{
+	"devman1",
+	"devman test",
+	&test_devman1,
+	false
+},
Index: uspace/app/tester/fault/fault2.c
===================================================================
--- uspace/app/tester/fault/fault2.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/tester/fault/fault2.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -29,4 +29,5 @@
 
 #include "../tester.h"
+#include <stdio.h>
 
 typedef int __attribute__((may_alias)) aliasing_int;
@@ -38,4 +39,5 @@
 	
 	var1 = *((aliasing_int *) (((char *) (&var)) + 1));
+	printf("Read %d\n", var1);
 	
 	return "Survived unaligned read";
Index: uspace/app/tester/tester.c
===================================================================
--- uspace/app/tester/tester.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/tester/tester.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -66,4 +66,5 @@
 #include "adt/usbaddrkeep.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 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/tester/tester.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -82,4 +82,5 @@
 extern const char *test_usbaddrkeep(void);
 extern const char *test_virtchar1(void);
+extern const char *test_devman1(void);
 
 extern test_t tests[];
Index: uspace/app/trace/trace.c
===================================================================
--- uspace/app/trace/trace.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/app/trace/trace.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -872,5 +872,5 @@
 static display_mask_t parse_display_mask(const char *text)
 {
-	display_mask_t dm;
+	display_mask_t dm = 0;
 	const char *c = text;
 	
Index: uspace/drv/isa/isa.c
===================================================================
--- uspace/drv/isa/isa.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/isa/isa.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -53,4 +53,5 @@
 
 #include <ddf/driver.h>
+#include <ddf/log.h>
 #include <ops/hw_res.h>
 
@@ -134,5 +135,5 @@
 	fd = open(conf_path, O_RDONLY);
 	if (fd < 0) {
-		printf(NAME ": unable to open %s\n", conf_path);
+		ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
 		goto cleanup;
 	}
@@ -141,8 +142,8 @@
 
 	len = lseek(fd, 0, SEEK_END);
-	lseek(fd, 0, SEEK_SET);	
+	lseek(fd, 0, SEEK_SET);
 	if (len == 0) {
-		printf(NAME ": fun_conf_read error: configuration file '%s' "
-		    "is empty.\n", conf_path);
+		ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
+		    conf_path);
 		goto cleanup;
 	}
@@ -150,11 +151,10 @@
 	buf = malloc(len + 1);
 	if (buf == NULL) {
-		printf(NAME ": fun_conf_read error: memory allocation failed.\n");
+		ddf_msg(LVL_ERROR, "Memory allocation failed.");
 		goto cleanup;
 	}
 
 	if (0 >= read(fd, buf, len)) {
-		printf(NAME ": fun_conf_read error: unable to read file '%s'.\n",
-		    conf_path);
+		ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
 		goto cleanup;
 	}
@@ -252,5 +252,5 @@
 		fun->hw_resources.count++;
 
-		printf(NAME ": added irq 0x%x to function %s\n", irq,
+		ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
 		    fun->fnode->name);
 	}
@@ -270,6 +270,6 @@
 		fun->hw_resources.count++;
 
-		printf(NAME ": added io range (addr=0x%x, size=0x%x) to "
-		    "function %s\n", (unsigned int) addr, (unsigned int) len,
+		ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
+		    "function %s", (unsigned int) addr, (unsigned int) len,
 		    fun->fnode->name);
 	}
@@ -331,6 +331,6 @@
 	score = (int)strtol(val, &end, 10);
 	if (val == end) {
-		printf(NAME " : error - could not read match score for "
-		    "function %s.\n", fun->fnode->name);
+		ddf_msg(LVL_ERROR, "Cannot read match score for function "
+		    "%s.", fun->fnode->name);
 		return;
 	}
@@ -339,15 +339,17 @@
 	get_match_id(&id, val);
 	if (id == NULL) {
-		printf(NAME " : error - could not read match id for "
-		    "function %s.\n", fun->fnode->name);
+		ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
+		    fun->fnode->name);
 		return;
 	}
 
-	printf(NAME ": adding match id '%s' with score %d to function %s\n", id,
-	    score, fun->fnode->name);
+	ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
+	    "function %s", id, score, fun->fnode->name);
 
 	rc = ddf_fun_add_match_id(fun->fnode, id, score);
-	if (rc != EOK)
-		printf(NAME ": error adding match ID: %s\n", str_error(rc));
+	if (rc != EOK) {
+		ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
+		    str_error(rc));
+	}
 }
 
@@ -375,8 +377,8 @@
 	if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
 	    !prop_parse(fun, line, "irq", &fun_parse_irq) &&
-	    !prop_parse(fun, line, "match", &fun_parse_match_id))
-	{
-	    printf(NAME " error undefined device property at line '%s'\n",
-		line);
+	    !prop_parse(fun, line, "match", &fun_parse_match_id)) {
+
+		ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
+		    line);
 	}
 }
@@ -439,5 +441,5 @@
 	fun->fnode->ops = &isa_fun_ops;
 
-	printf(NAME ": Binding function %s.\n", fun->fnode->name);
+	ddf_msg(LVL_DEBUG, "Binding function %s.", fun->fnode->name);
 
 	/* XXX Handle error */
@@ -467,18 +469,18 @@
 static int isa_add_device(ddf_dev_t *dev)
 {
-	printf(NAME ": isa_add_device, device handle = %d\n",
+	ddf_msg(LVL_DEBUG, "isa_add_device, device handle = %d",
 	    (int) dev->handle);
 
 	/* Make the bus device more visible. Does not do anything. */
-	printf(NAME ": adding a 'ctl' function\n");
+	ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
 
 	ddf_fun_t *ctl = ddf_fun_create(dev, fun_exposed, "ctl");
 	if (ctl == NULL) {
-		printf(NAME ": Error creating control function.\n");
+		ddf_msg(LVL_ERROR, "Failed creating control function.");
 		return EXDEV;
 	}
 
 	if (ddf_fun_bind(ctl) != EOK) {
-		printf(NAME ": Error binding control function.\n");
+		ddf_msg(LVL_ERROR, "Failed binding control function.");
 		return EXDEV;
 	}
@@ -486,5 +488,5 @@
 	/* Add functions as specified in the configuration file. */
 	isa_functions_add(dev);
-	printf(NAME ": finished the enumeration of legacy functions\n");
+	ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
 
 	return EOK;
@@ -493,4 +495,5 @@
 static void isa_init() 
 {
+	ddf_log_init(NAME, LVL_ERROR);
 	isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
 }
Index: uspace/drv/ns8250/ns8250.c
===================================================================
--- uspace/drv/ns8250/ns8250.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ns8250/ns8250.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -55,4 +55,5 @@
 #include <ddf/driver.h>
 #include <ddf/interrupt.h>
+#include <ddf/log.h>
 #include <ops/char_dev.h>
 
@@ -275,11 +276,11 @@
 static bool ns8250_pio_enable(ns8250_t *ns)
 {
-	printf(NAME ": ns8250_pio_enable %s\n", ns->dev->name);
+	ddf_msg(LVL_DEBUG, "ns8250_pio_enable %s", ns->dev->name);
 	
 	/* Gain control over port's registers. */
 	if (pio_enable((void *)(uintptr_t) ns->io_addr, REG_COUNT,
 	    (void **) &ns->port)) {
-		printf(NAME ": error - cannot gain the port %#" PRIx32 " for device "
-		    "%s.\n", ns->io_addr, ns->dev->name);
+		ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32
+		    " for device %s.", ns->io_addr, ns->dev->name);
 		return false;
 	}
@@ -295,5 +296,5 @@
 static bool ns8250_dev_probe(ns8250_t *ns)
 {
-	printf(NAME ": ns8250_dev_probe %s\n", ns->dev->name);
+	ddf_msg(LVL_DEBUG, "ns8250_dev_probe %s", ns->dev->name);
 	
 	ioport8_t *port_addr = ns->port;
@@ -313,6 +314,8 @@
 	pio_write_8(port_addr + 4, olddata);
 	
-	if (!res)
-		printf(NAME ": device %s is not present.\n", ns->dev->name);
+	if (!res) {
+		ddf_msg(LVL_DEBUG, "Device %s is not present.",
+		    ns->dev->name);
+	}
 	
 	return res;
@@ -326,5 +329,5 @@
 static int ns8250_dev_initialize(ns8250_t *ns)
 {
-	printf(NAME ": ns8250_dev_initialize %s\n", ns->dev->name);
+	ddf_msg(LVL_DEBUG, "ns8250_dev_initialize %s", ns->dev->name);
 	
 	int ret = EOK;
@@ -337,6 +340,6 @@
 	    IPC_FLAG_BLOCKING);
 	if (ns->dev->parent_phone < 0) {
-		printf(NAME ": failed to connect to the parent driver of the "
-		    "device %s.\n", ns->dev->name);
+		ddf_msg(LVL_ERROR, "Failed to connect to parent driver of "
+		    "device %s.", ns->dev->name);
 		ret = ns->dev->parent_phone;
 		goto failed;
@@ -346,6 +349,6 @@
 	ret = hw_res_get_resource_list(ns->dev->parent_phone, &hw_resources);
 	if (ret != EOK) {
-		printf(NAME ": failed to get hw resources for the device "
-		    "%s.\n", ns->dev->name);
+		ddf_msg(LVL_ERROR, "Failed to get HW resources for device "
+		    "%s.", ns->dev->name);
 		goto failed;
 	}
@@ -362,5 +365,5 @@
 			ns->irq = res->res.interrupt.irq;
 			irq = true;
-			printf(NAME ": the %s device was asigned irq = 0x%x.\n",
+			ddf_msg(LVL_NOTE, "Device %s was asigned irq = 0x%x.",
 			    ns->dev->name, ns->irq);
 			break;
@@ -369,13 +372,13 @@
 			ns->io_addr = res->res.io_range.address;
 			if (res->res.io_range.size < REG_COUNT) {
-				printf(NAME ": i/o range assigned to the device "
-				    "%s is too small.\n", ns->dev->name);
+				ddf_msg(LVL_ERROR, "I/O range assigned to "
+				    "device %s is too small.", ns->dev->name);
 				ret = ELIMIT;
 				goto failed;
 			}
 			ioport = true;
-			printf(NAME ": the %s device was asigned i/o address = "
-			    "0x%x.\n", ns->dev->name, ns->io_addr);
-			break;
+			ddf_msg(LVL_NOTE, "Device %s was asigned I/O address = "
+			    "0x%x.", ns->dev->name, ns->io_addr);
+    			break;
 			
 		default:
@@ -385,5 +388,5 @@
 	
 	if (!irq || !ioport) {
-		printf(NAME ": missing hw resource(s) for the device %s.\n",
+		ddf_msg(LVL_ERROR, "Missing HW resource(s) for device %s.",
 		    ns->dev->name);
 		ret = ENOENT;
@@ -470,6 +473,6 @@
 	
 	if (baud_rate < 50 || MAX_BAUD_RATE % baud_rate != 0) {
-		printf(NAME ": error - somebody tried to set invalid baud rate "
-		    "%d\n", baud_rate);
+		ddf_msg(LVL_ERROR, "Invalid baud rate %d requested.",
+		    baud_rate);
 		return EINVAL;
 	}
@@ -654,9 +657,9 @@
 			if (ns->client_connected) {
 				if (!buf_push_back(&ns->input_buffer, val)) {
-					printf(NAME ": buffer overflow on "
-					    "%s.\n", ns->dev->name);
+					ddf_msg(LVL_WARN, "Buffer overflow on "
+					    "%s.", ns->dev->name);
 				} else {
-					printf(NAME ": the character %c saved "
-					    "to the buffer of %s.\n",
+					ddf_msg(LVL_DEBUG2, "Character %c saved "
+					    "to the buffer of %s.",
 					    val, ns->dev->name);
 				}
@@ -714,5 +717,5 @@
 	int rc;
 	
-	printf(NAME ": ns8250_add_device %s (handle = %d)\n",
+	ddf_msg(LVL_DEBUG, "ns8250_add_device %s (handle = %d)",
 	    dev->name, (int) dev->handle);
 	
@@ -749,5 +752,5 @@
 	/* Register interrupt handler. */
 	if (ns8250_register_interrupt_handler(ns) != EOK) {
-		printf(NAME ": failed to register interrupt handler.\n");
+		ddf_msg(LVL_ERROR, "Failed to register interrupt handler.");
 		rc = EADDRNOTAVAIL;
 		goto fail;
@@ -757,6 +760,6 @@
 	rc = ns8250_interrupt_enable(ns);
 	if (rc != EOK) {
-		printf(NAME ": failed to enable the interrupt. Error code = "
-		    "%d.\n", rc);
+		ddf_msg(LVL_ERROR, "Failed to enable the interrupt. Error code = "
+		    "%d.", rc);
 		goto fail;
 	}
@@ -764,5 +767,5 @@
 	fun = ddf_fun_create(dev, fun_exposed, "a");
 	if (fun == NULL) {
-		printf(NAME ": error creating function.\n");
+		ddf_msg(LVL_ERROR, "Failed creating function.");
 		goto fail;
 	}
@@ -772,5 +775,5 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function.\n");
+		ddf_msg(LVL_ERROR, "Failed binding function.");
 		goto fail;
 	}
@@ -780,5 +783,5 @@
 	ddf_fun_add_to_class(fun, "serial");
 	
-	printf(NAME ": the %s device has been successfully initialized.\n",
+	ddf_msg(LVL_NOTE, "Device %s successfully initialized.",
 	    dev->name);
 	
@@ -862,6 +865,6 @@
 	fibril_mutex_unlock(&data->mutex);
 	
-	printf(NAME ": ns8250_get_props: baud rate %d, parity 0x%x, word "
-	    "length %d, stop bits %d\n", *baud_rate, *parity, *word_length,
+	ddf_msg(LVL_DEBUG, "ns8250_get_props: baud rate %d, parity 0x%x, word "
+	    "length %d, stop bits %d", *baud_rate, *parity, *word_length,
 	    *stop_bits);
 }
@@ -879,6 +882,6 @@
     unsigned int parity, unsigned int word_length, unsigned int stop_bits)
 {
-	printf(NAME ": ns8250_set_props: baud rate %d, parity 0x%x, word "
-	    "length %d, stop bits %d\n", baud_rate, parity, word_length,
+	ddf_msg(LVL_DEBUG, "ns8250_set_props: baud rate %d, parity 0x%x, word "
+	    "length %d, stop bits %d", baud_rate, parity, word_length,
 	    stop_bits);
 	
@@ -940,4 +943,6 @@
 static void ns8250_init(void)
 {
+	ddf_log_init(NAME, LVL_ERROR);
+	
 	ns8250_dev_ops.open = &ns8250_open;
 	ns8250_dev_ops.close = &ns8250_close;
Index: uspace/drv/ohci/batch.c
===================================================================
--- uspace/drv/ohci/batch.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/batch.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -73,7 +73,7 @@
 	CHECK_NULL_DISPOSE_RETURN(instance,
 	    "Failed to allocate batch instance.\n");
-	usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size,
-	    buffer, NULL, buffer_size, NULL, setup_size, func_in,
-	    func_out, arg, fun, NULL);
+	usb_transfer_batch_init(instance, target, transfer_type, speed,
+	    max_packet_size, buffer, NULL, buffer_size, NULL, setup_size,
+	    func_in, func_out, arg, fun, NULL, NULL);
 
         if (buffer_size > 0) {
Index: uspace/drv/ohci/hc.c
===================================================================
--- uspace/drv/ohci/hc.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/hc.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -45,4 +45,6 @@
 
 static int interrupt_emulator(hc_t *instance);
+static void hc_gain_control(hc_t *instance);
+static void hc_init_hw(hc_t *instance);
 /*----------------------------------------------------------------------------*/
 int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
@@ -58,5 +60,5 @@
 
 	char *match_str = NULL;
-	int ret = asprintf(&match_str, "usb&mid");
+	int ret = asprintf(&match_str, "usb&class=hub");
 	ret = (match_str == NULL) ? ret : EOK;
 	if (ret < 0) {
@@ -77,12 +79,21 @@
 	assert(instance);
 	int ret = EOK;
+#define CHECK_RET_RETURN(ret, message...) \
+if (ret != EOK) { \
+	usb_log_error(message); \
+	return ret; \
+} else (void)0
 
 	ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
-	if (ret != EOK) {
-		usb_log_error("Failed to gain access to device registers.\n");
-		return ret;
-	}
+	CHECK_RET_RETURN(ret,
+	    "Failed(%d) to gain access to device registers: %s.\n",
+	    ret, str_error(ret));
+
 	instance->ddf_instance = fun;
 	usb_device_keeper_init(&instance->manager);
+	ret = usb_endpoint_manager_init(&instance->ep_manager,
+	    BANDWIDTH_AVAILABLE_USB11);
+	CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
+	    ret, str_error(ret));
 
 	if (!interrupts) {
@@ -92,5 +103,9 @@
 	}
 
+	hc_gain_control(instance);
+
 	rh_init(&instance->rh, dev, instance->registers);
+
+	hc_init_hw(instance);
 
 	/* TODO: implement */
@@ -117,4 +132,6 @@
 		rh_interrupt(&instance->rh);
 
+	usb_log_info("OHCI interrupt: %x.\n", status);
+
 	/* TODO: Check for further interrupt causes */
 	/* TODO: implement */
@@ -126,5 +143,5 @@
 	usb_log_info("Started interrupt emulator.\n");
 	while (1) {
-		uint32_t status = instance->registers->interrupt_status;
+		const uint32_t status = instance->registers->interrupt_status;
 		instance->registers->interrupt_status = status;
 		hc_interrupt(instance, status);
@@ -133,4 +150,65 @@
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
+void hc_gain_control(hc_t *instance)
+{
+	assert(instance);
+	/* Interrupt routing enabled => smm driver is active */
+	if (instance->registers->control & C_IR) {
+		usb_log_info("Found SMM driver requesting ownership change.\n");
+		instance->registers->command_status |= CS_OCR;
+		while (instance->registers->control & C_IR) {
+			async_usleep(1000);
+		}
+		usb_log_info("Ownership taken from SMM driver.\n");
+		return;
+	}
+
+	const unsigned hc_status =
+	    (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
+	/* Interrupt routing disabled && status != USB_RESET => BIOS active */
+	if (hc_status != C_HCFS_RESET) {
+		usb_log_info("Found BIOS driver.\n");
+		if (hc_status == C_HCFS_OPERATIONAL) {
+			usb_log_info("HC operational(BIOS).\n");
+			return;
+		}
+		/* HC is suspended assert resume for 20ms */
+		instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
+		async_usleep(20000);
+		return;
+	}
+
+	/* HC is in reset (hw startup) => no other driver
+	 * maintain reset for at least the time specified in USB spec (50 ms)*/
+	async_usleep(50000);
+
+	/* turn off legacy emulation */
+	volatile uint32_t *ohci_emulation_reg =
+	    (uint32_t*)((char*)instance->registers + 0x100);
+	usb_log_info("OHCI legacy register status %p: %x.\n",
+		ohci_emulation_reg, *ohci_emulation_reg);
+	*ohci_emulation_reg = 0;
+
+}
+/*----------------------------------------------------------------------------*/
+void hc_init_hw(hc_t *instance)
+{
+	assert(instance);
+	const uint32_t fm_interval = instance->registers->fm_interval;
+	instance->registers->command_status = CS_HCR;
+	async_usleep(10);
+	instance->registers->fm_interval = fm_interval;
+	assert((instance->registers->command_status & CS_HCR) == 0);
+	/* hc is now in suspend state */
+	/* TODO: init HCCA block */
+	/* TODO: init queues */
+	/* TODO: enable queues */
+	/* TODO: enable interrupts */
+	/* TODO: set periodic start to 90% */
+
+	instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
+	usb_log_info("OHCI HC up and running.\n");
+}
 /**
  * @}
Index: uspace/drv/ohci/hc.h
===================================================================
--- uspace/drv/ohci/hc.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/hc.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -42,4 +42,5 @@
 #include <usb/usb.h>
 #include <usb/host/device_keeper.h>
+#include <usb/host/usb_endpoint_manager.h>
 #include <usbhc_iface.h>
 
@@ -47,4 +48,5 @@
 #include "ohci_regs.h"
 #include "root_hub.h"
+#include "hw_struct/hcca.h"
 
 typedef struct hc {
@@ -54,4 +56,5 @@
 	ddf_fun_t *ddf_instance;
 	usb_device_keeper_t manager;
+	usb_endpoint_manager_t ep_manager;
 	fid_t interrupt_emulator;
 } hc_t;
Index: uspace/drv/ohci/hw_struct/completion_codes.h
===================================================================
--- uspace/drv/ohci/hw_struct/completion_codes.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/ohci/hw_struct/completion_codes.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbohci
+ * @{
+ */
+/** @file
+ * @brief OHCI driver
+ */
+#ifndef DRV_OHCI_HW_STRUCT_COMPLETION_CODES_H
+#define DRV_OHCI_HW_STRUCT_COMPLETION_CODES_H
+
+#define CC_NOERROR (0x0)
+#define CC_CRC (0x1)
+#define CC_BITSTUFF (0x2)
+#define CC_TOGGLE (0x3)
+#define CC_STALL (0x4)
+#define CC_NORESPONSE (0x5)
+#define CC_PIDFAIL (0x6)
+#define CC_PIDUNEXPECTED (0x7)
+#define CC_DATAOVERRRUN (0x8)
+#define CC_DATAUNDERRRUN (0x9)
+#define CC_BUFFEROVERRRUN (0xc)
+#define CC_BUFFERUNDERRUN (0xd)
+#define CC_NOACCESS1 (0xe)
+#define CC_NOACCESS2 (0xf)
+
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/ohci/hw_struct/endpoint_descriptor.h
===================================================================
--- uspace/drv/ohci/hw_struct/endpoint_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/ohci/hw_struct/endpoint_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbohci
+ * @{
+ */
+/** @file
+ * @brief OHCI driver
+ */
+#ifndef DRV_OHCI_HW_STRUCT_ENDPOINT_DESCRIPTOR_H
+#define DRV_OHCI_HW_STRUCT_ENDPOINT_DESCRIPTOR_H
+
+#include <stdint.h>
+
+#include "completion_codes.h"
+
+typedef struct ed {
+	volatile uint32_t status;
+#define ED_STATUS_FA_MASK (0x7f)   /* USB device address   */
+#define ED_STATUS_FA_SHIFT (0)
+#define ED_STATUS_EN_MASK (0xf)    /* USB endpoint address */
+#define ED_STATUS_EN_SHIFT (6)
+#define ED_STATUS_D_MASK (0x3)     /* direction */
+#define ED_STATUS_D_SHIFT (10)
+#define ED_STATUS_D_IN (0x1)
+#define ED_STATUS_D_OUT (0x2)
+
+#define ED_STATUS_S_FLAG (1 << 13) /* speed flag */
+#define ED_STATUS_K_FLAG (1 << 14) /* skip flag (no not execute this ED) */
+#define ED_STATUS_F_FLAG (1 << 15) /* format: 1 = isochronous*/
+#define ED_STATUS_MPS_MASK (0x3ff) /* max_packet_size*/
+#define ED_STATUS_MPS_SHIFT (16)
+
+	volatile uint32_t td_tail;
+#define ED_TDTAIL_PTR_MASK (0xfffffff0)
+#define ED_TDTAIL_PTR_SHIFT (0)
+
+	volatile uint32_t td_head;
+#define ED_TDHEAD_PTR_MASK (0xfffffff0)
+#define ED_TDHEAD_PTR_SHIFT (0)
+#define ED_TDHEAD_ZERO_MASK (0x3)
+#define ED_TDHEAD_ZERO_SHIFT (2)
+#define ED_TDHEAD_TOGGLE_CARRY (0x2)
+
+	volatile uint32_t next;
+#define ED_NEXT_PTR_MASK (0xfffffff0)
+#define ED_NEXT_PTR_SHIFT (0)
+} __attribute__((packed)) ed_t;
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/ohci/hw_struct/hcca.h
===================================================================
--- uspace/drv/ohci/hw_struct/hcca.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/ohci/hw_struct/hcca.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbohci
+ * @{
+ */
+/** @file
+ * @brief OHCI driver
+ */
+#ifndef DRV_OHCI_HW_STRUCT_HCCA_H
+#define DRV_OHCI_HW_STRUCT_HCCA_H
+
+#include <stdint.h>
+
+typedef struct hcca {
+	uint32_t int_ep[32];
+	uint16_t frame_number;
+	uint16_t pad1;
+	uint32_t done_head;
+	uint32_t reserved[29];
+} __attribute__((packed)) hcca_t;
+
+#endif
+/**
+ * @}
+ */
+
Index: uspace/drv/ohci/hw_struct/iso_transfer_descriptor.h
===================================================================
--- uspace/drv/ohci/hw_struct/iso_transfer_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/ohci/hw_struct/iso_transfer_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbohci
+ * @{
+ */
+/** @file
+ * @brief OHCI driver
+ */
+#ifndef DRV_OHCI_HW_STRUCT_ISO_TRANSFER_DESCRIPTOR_H
+#define DRV_OHCI_HW_STRUCT_ISO_TRANSFER_DESCRIPTOR_H
+
+#include <stdint.h>
+
+#include "completion_codes.h"
+
+typedef struct itd {
+	volatile uint32_t status;
+#define ITD_STATUS_SF_MASK (0xffff) /* starting frame */
+#define ITD_STATUS_SF_SHIFT (0)
+#define ITD_STATUS_DI_MASK (0x7) /* delay int, wait DI frames before int */
+#define ITD_STATUS_DI_SHIFT (21)
+#define ITD_STATUS_DI_NO_INTERRUPT (0x7)
+#define ITD_STATUS_FC_MASK (0x7) /* frame count */
+#define ITD_STATUS_FC_SHIFT (24)
+#define ITD_STATUS_CC_MASK (0xf) /* condition code */
+#define ITD_STATUS_CC_SHIFT (28)
+
+	volatile uint32_t page;   /* page number of the first byte in buffer */
+#define ITD_PAGE_BP0_MASK (0xfffff000)
+#define ITD_PAGE_BP0_SHIFT (0)
+
+	volatile uint32_t next;
+#define ITD_NEXT_PTR_MASK (0xfffffff0)
+#define ITD_NEXT_PTR_SHIFT (0)
+
+	volatile uint32_t be; /* buffer end, address of the last byte */
+
+	volatile uint16_t offset[8];
+#define ITD_OFFSET_SIZE_MASK (0x3ff)
+#define ITD_OFFSET_SIZE_SHIFT (0)
+#define ITD_OFFSET_CC_MASK (0xf)
+#define ITD_OFFSET_CC_SHIFT (12)
+
+} __attribute__((packed)) itd_t;
+#endif
+/**
+ * @}
+ */
+
+
Index: uspace/drv/ohci/hw_struct/transfer_descriptor.h
===================================================================
--- uspace/drv/ohci/hw_struct/transfer_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/ohci/hw_struct/transfer_descriptor.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbohci
+ * @{
+ */
+/** @file
+ * @brief OHCI driver
+ */
+#ifndef DRV_OHCI_HW_STRUCT_TRANSFER_DESCRIPTOR_H
+#define DRV_OHCI_HW_STRUCT_TRANSFER_DESCRIPTOR_H
+
+#include <stdint.h>
+
+#include "completion_codes.h"
+
+typedef struct td {
+	volatile uint32_t status;
+#define TD_STATUS_ROUND_FLAG (1 << 18)
+#define TD_STATUS_DP_MASK (0x3) /* direction/PID */
+#define TD_STATUS_DP_SHIFT (19)
+#define TD_STATUS_DP_SETUP (0x0)
+#define TD_STATUS_DP_IN (0x1)
+#define TD_STATUS_DP_OUT (0x2)
+#define TD_STATUS_DI_MASK (0x7) /* delay interrupt, wait DI frames before int */
+#define TD_STATUS_DI_SHIFT (21)
+#define TD_STATUS_DI_NO_INTERRUPT (0x7)
+#define TD_STATUS_T_MASK (0x3)  /* data toggle 1x = use ED toggle carry */
+#define TD_STATUS_T_SHIFT (24)
+#define TD_STATUS_EC_MASK (0x3) /* error count */
+#define TD_STATUS_EC_SHIFT (26)
+#define TD_STATUS_CC_MASK (0xf) /* condition code */
+#define TD_STATUS_CC_SHIFT (28)
+
+	volatile uint32_t cbp; /* current buffer ptr, data to be transfered */
+	volatile uint32_t next;
+#define TD_NEXT_PTR_MASK (0xfffffff0)
+#define TD_NEXT_PTR_SHIFT (0)
+
+	volatile uint32_t be; /* buffer end, address of the last byte */
+} __attribute__((packed)) td_t;
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/ohci/iface.c
===================================================================
--- uspace/drv/ohci/iface.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/iface.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -151,7 +151,17 @@
     size_t max_packet_size, unsigned int interval)
 {
-	UNSUPPORTED("register_endpoint");
-
-	return ENOTSUP;
+	assert(fun);
+	hc_t *hc = fun_to_hc(fun);
+	assert(hc);
+	if (address == hc->rh.address)
+		return EOK;
+	const usb_speed_t speed =
+		usb_device_keeper_get_speed(&hc->manager, address);
+	const size_t size = max_packet_size;
+	usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
+	    address, endpoint, usb_str_transfer_type(transfer_type),
+	    usb_str_speed(speed), direction, size, max_packet_size, interval);
+	// TODO use real endpoint here!
+	return usb_endpoint_manager_register_ep(&hc->ep_manager,NULL, 0);
 }
 /*----------------------------------------------------------------------------*/
@@ -168,7 +178,11 @@
     usb_endpoint_t endpoint, usb_direction_t direction)
 {
-	UNSUPPORTED("unregister_endpoint");
-
-	return ENOTSUP;
+	assert(fun);
+	hc_t *hc = fun_to_hc(fun);
+	assert(hc);
+	usb_log_debug("Unregister endpoint %d:%d %d.\n",
+	    address, endpoint, direction);
+	return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
+	    endpoint, direction);
 }
 /*----------------------------------------------------------------------------*/
Index: uspace/drv/ohci/ohci.c
===================================================================
--- uspace/drv/ohci/ohci.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/ohci.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -31,5 +31,5 @@
  */
 /** @file
- * @brief UHCI driver
+ * @brief OHCI driver
  */
 #include <errno.h>
@@ -117,10 +117,10 @@
 /** Initialize hc and rh ddf structures and their respective drivers.
  *
- * @param[in] instance UHCI structure to use.
+ * @param[in] instance OHCI structure to use.
  * @param[in] device DDF instance of the device to use.
  *
  * This function does all the preparatory work for hc and rh drivers:
  *  - gets device hw resources
- *  - disables UHCI legacy support
+ *  - disables OHCI legacy support
  *  - asks for interrupt
  *  - registers interrupt handler
@@ -185,5 +185,5 @@
 	ret = ddf_fun_bind(instance->hc_fun);
 	CHECK_RET_DEST_FUN_RETURN(ret,
-	    "Failed(%d) to bind UHCI device function: %s.\n",
+	    "Failed(%d) to bind OHCI device function: %s.\n",
 	    ret, str_error(ret));
 #undef CHECK_RET_HC_RETURN
@@ -216,5 +216,5 @@
 	ret = ddf_fun_bind(instance->rh_fun);
 	CHECK_RET_FINI_RETURN(ret,
-	    "Failed(%d) to register UHCI root hub.\n", ret);
+	    "Failed(%d) to register OHCI root hub.\n", ret);
 
 	return EOK;
Index: uspace/drv/ohci/ohci_regs.h
===================================================================
--- uspace/drv/ohci/ohci_regs.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/ohci_regs.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -39,7 +39,32 @@
 typedef struct ohci_regs
 {
-	volatile uint32_t revision;
+	const volatile uint32_t revision;
 	volatile uint32_t control;
+#define C_CSBR_MASK (0x3)
+#define C_CSBR_SHIFT (0)
+#define C_PLE (1 << 2)
+#define C_IE (1 << 3)
+#define C_CLE (1 << 4)
+#define C_BLE (1 << 5)
+
+#define C_HCFS_MASK (0x3)
+#define C_HCFS_SHIFT (6)
+#define C_HCFS_RESET (0x0)
+#define C_HCFS_OPERATIONAL (0x1)
+#define C_HCFS_RESUME (0x2)
+#define C_HCFS_SUSPEND (0x3)
+
+#define C_IR (1 << 8)
+#define C_RWC (1 << 9)
+#define C_RWE (1 << 10)
+
 	volatile uint32_t command_status;
+#define CS_HCR (1 << 0)
+#define CS_CLF (1 << 1)
+#define CS_BLF (1 << 2)
+#define CS_OCR (1 << 3)
+#define CS_SOC_MASK (0x3)
+#define CS_SOC_SHIFT (16)
+
 	volatile uint32_t interrupt_status;
 #define IS_SO (1 << 0)
@@ -51,4 +76,5 @@
 #define IS_RHSC (1 << 6)
 #define IS_OC (1 << 30)
+
 	volatile uint32_t interupt_enable;
 #define IE_SO   (1 << 0)
Index: uspace/drv/ohci/root_hub.c
===================================================================
--- uspace/drv/ohci/root_hub.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/root_hub.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -40,4 +40,5 @@
 #include "root_hub.h"
 #include "usb/classes/classes.h"
+#include "usb/devdrv.h"
 #include <usb/request.h>
 #include <usb/classes/hub.h>
@@ -61,5 +62,5 @@
 		/// \TODO these values migt be different
 		.str_serial_number = 0,
-		.usb_spec_version = 0,
+		.usb_spec_version = 0x110,
 };
 
@@ -110,67 +111,42 @@
 };
 
-/** Root hub initialization
- * @return Error code.
- */
-int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs)
-{
-	assert(instance);
-	instance->address = -1;
-	instance->registers = regs;
-	instance->device = dev;
-
-
-	usb_log_info("OHCI root hub with %d ports.\n", regs->rh_desc_a & 0xff);
-
-	//start generic usb hub driver
+static const uint32_t hub_clear_feature_valid_mask =
+	(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) +
+	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
+
+static const uint32_t hub_clear_feature_by_writing_one_mask =
+	1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER;
+
+static const uint32_t hub_set_feature_valid_mask =
+	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
+
 	
-	/* TODO: implement */
-	return EOK;
-}
-/*----------------------------------------------------------------------------*/
-
-/**
- * create answer to port status_request
- *
- * Copy content of corresponding port status register to answer buffer.
- *
- * @param instance root hub instance
- * @param port port number, counted from 1
- * @param request structure containing both request and response information
- * @return error code
- */
-static int process_get_port_status_request(rh_t *instance, uint16_t port,
-		usb_transfer_batch_t * request){
-	if(port<1 || port>instance->port_count)
-		return EINVAL;
-	uint32_t * uint32_buffer = (uint32_t*)request->buffer;
-	request->transfered_size = 4;
-	uint32_buffer[0] = instance->registers->rh_port_status[port -1];
-	return EOK;
-}
-
-/**
- * create answer to port status_request
- *
- * Copy content of hub status register to answer buffer.
- *
- * @param instance root hub instance
- * @param request structure containing both request and response information
- * @return error code
- */
-static int process_get_hub_status_request(rh_t *instance,
-		usb_transfer_batch_t * request){
-	uint32_t * uint32_buffer = (uint32_t*)request->buffer;
-	//bits, 0,1,16,17
-	request->transfered_size = 4;
-	uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
-	uint32_buffer[0] = mask & instance->registers->rh_status;
-	return EOK;
-
-}
+static const uint32_t hub_set_feature_direct_mask =
+	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
+
+static const uint32_t port_set_feature_valid_mask =
+	(1 << USB_HUB_FEATURE_PORT_ENABLE) +
+	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
+	(1 << USB_HUB_FEATURE_PORT_RESET) +
+	(1 << USB_HUB_FEATURE_PORT_POWER);
+
+static const uint32_t port_clear_feature_valid_mask =
+	(1 << USB_HUB_FEATURE_PORT_CONNECTION) +
+	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
+	(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) +
+	(1 << USB_HUB_FEATURE_PORT_POWER) +
+	(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) +
+	(1 << USB_HUB_FEATURE_C_PORT_ENABLE) +
+	(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) +
+	(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) +
+	(1 << USB_HUB_FEATURE_C_PORT_RESET);
+//note that USB_HUB_FEATURE_PORT_POWER bit is translated into USB_HUB_FEATURE_PORT_LOW_SPEED
+
+
+
 
 /**
  * Create hub descriptor used in hub-driver <-> hub communication
- * 
+ *
  * This means creating byt array from data in root hub registers. For more
  * info see usb hub specification.
@@ -197,5 +173,5 @@
 	result[2] = instance->port_count;
 	uint32_t hub_desc_reg = instance->registers->rh_desc_a;
-	result[3] = 
+	result[3] =
 			((hub_desc_reg >> 8) %2) +
 			(((hub_desc_reg >> 9) %2) << 1) +
@@ -219,4 +195,110 @@
 	(*out_size) = size;
 }
+
+
+/** initialize hub descriptors
+ *
+ * Initialized are device and full configuration descriptor. These need to
+ * be initialized only once per hub.
+ * @instance root hub instance
+ */
+static void rh_init_descriptors(rh_t *instance){
+	memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
+		sizeof(ohci_rh_device_descriptor)
+	);
+	usb_standard_configuration_descriptor_t descriptor;
+	memcpy(&descriptor,&ohci_rh_conf_descriptor,
+			sizeof(ohci_rh_conf_descriptor));
+	uint8_t * hub_descriptor;
+	size_t hub_desc_size;
+	usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
+			&hub_desc_size);
+
+	descriptor.total_length =
+			sizeof(usb_standard_configuration_descriptor_t)+
+			sizeof(usb_standard_endpoint_descriptor_t)+
+			sizeof(usb_standard_interface_descriptor_t)+
+			hub_desc_size;
+	
+	uint8_t * full_config_descriptor =
+			(uint8_t*) malloc(descriptor.total_length);
+	memcpy(full_config_descriptor, &descriptor, sizeof(descriptor));
+	memcpy(full_config_descriptor + sizeof(descriptor),
+			&ohci_rh_iface_descriptor, sizeof(ohci_rh_iface_descriptor));
+	memcpy(full_config_descriptor + sizeof(descriptor) +
+				sizeof(ohci_rh_iface_descriptor),
+			&ohci_rh_ep_descriptor, sizeof(ohci_rh_ep_descriptor));
+	memcpy(full_config_descriptor + sizeof(descriptor) +
+				sizeof(ohci_rh_iface_descriptor) +
+				sizeof(ohci_rh_ep_descriptor),
+			hub_descriptor, hub_desc_size);
+	
+	instance->descriptors.configuration = full_config_descriptor;
+	instance->descriptors.configuration_size = descriptor.total_length;
+}
+
+/** Root hub initialization
+ * @return Error code.
+ */
+int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs)
+{
+	assert(instance);
+	instance->address = -1;
+	instance->registers = regs;
+	instance->device = dev;
+	instance->port_count = instance->registers->rh_desc_a & 0xff;
+	rh_init_descriptors(instance);
+	/// \TODO set port power mode
+
+
+	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
+
+	//start generic usb hub driver
+	
+	/* TODO: implement */
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+
+/**
+ * create answer to port status_request
+ *
+ * Copy content of corresponding port status register to answer buffer.
+ *
+ * @param instance root hub instance
+ * @param port port number, counted from 1
+ * @param request structure containing both request and response information
+ * @return error code
+ */
+static int process_get_port_status_request(rh_t *instance, uint16_t port,
+		usb_transfer_batch_t * request){
+	if(port<1 || port>instance->port_count)
+		return EINVAL;
+	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	request->transfered_size = 4;
+	uint32_buffer[0] = instance->registers->rh_port_status[port -1];
+	return EOK;
+}
+
+/**
+ * create answer to port status_request
+ *
+ * Copy content of hub status register to answer buffer.
+ *
+ * @param instance root hub instance
+ * @param request structure containing both request and response information
+ * @return error code
+ */
+static int process_get_hub_status_request(rh_t *instance,
+		usb_transfer_batch_t * request){
+	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	//bits, 0,1,16,17
+	request->transfered_size = 4;
+	uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
+	uint32_buffer[0] = mask & instance->registers->rh_status;
+	return EOK;
+
+}
+
 
 
@@ -284,28 +366,5 @@
 	}
 }
-
-/**
- * create standart configuration descriptor for the root hub instance
- * @param instance root hub instance
- * @return newly allocated descriptor
- */
-static usb_standard_configuration_descriptor_t *
-usb_ohci_rh_create_standart_configuration_descriptor(rh_t *instance){
-	usb_standard_configuration_descriptor_t * descriptor =
-			malloc(sizeof(usb_standard_configuration_descriptor_t));
-	memcpy(descriptor, &ohci_rh_conf_descriptor,
-		sizeof(usb_standard_configuration_descriptor_t));
-	/// \TODO should this include device descriptor?
-	const size_t hub_descriptor_size = 7 +
-			2* (instance->port_count / 8 +
-			((instance->port_count % 8 > 0) ? 1 : 0));
-	descriptor->total_length =
-			sizeof(usb_standard_configuration_descriptor_t)+
-			sizeof(usb_standard_endpoint_descriptor_t)+
-			sizeof(usb_standard_interface_descriptor_t)+
-			hub_descriptor_size;
-	return descriptor;
-}
-
+ 
 /**
  * create answer to a descriptor request
@@ -344,10 +403,6 @@
 		case USB_DESCTYPE_CONFIGURATION: {
 			usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
-			usb_standard_configuration_descriptor_t * descriptor =
-					usb_ohci_rh_create_standart_configuration_descriptor(
-						instance);
-			result_descriptor = descriptor;
-			size = sizeof(usb_standard_configuration_descriptor_t);
-			del = true;
+			result_descriptor = instance->descriptors.configuration;
+			size = instance->descriptors.configuration_size;
 			break;
 		}
@@ -380,5 +435,7 @@
 	}
 	request->transfered_size = size;
-	memcpy(request->buffer,result_descriptor,size);
+	memcpy(request->transport_buffer,result_descriptor,size);
+	usb_log_debug("sent desctiptor: %s\n",
+			usb_debug_str_buffer((uint8_t*)request->transport_buffer,size,size));
 	if (del)
 		free(result_descriptor);
@@ -400,5 +457,5 @@
 	if(request->buffer_size != 1)
 		return EINVAL;
-	request->buffer[0] = 1;
+	request->transport_buffer[0] = 1;
 	request->transfered_size = 1;
 	return EOK;
@@ -406,26 +463,49 @@
 
 /**
- * process feature-enabling/disabling request on hub
+ * process feature-enabling request on hub
  * 
  * @param instance root hub instance
  * @param feature feature selector
- * @param enable enable or disable specified feature
  * @return error code
  */
 static int process_hub_feature_set_request(rh_t *instance,
-		uint16_t feature, bool enable){
-	if(feature > USB_HUB_FEATURE_C_HUB_OVER_CURRENT)
+		uint16_t feature){
+	if(! ((1<<feature) & hub_set_feature_valid_mask))
 		return EINVAL;
 	instance->registers->rh_status =
-			enable ?
 			(instance->registers->rh_status | (1<<feature))
-			:
-			(instance->registers->rh_status & (~(1<<feature)));
-	/// \TODO any error?
-	return EOK;
-}
-
-/**
- * process feature-enabling/disabling request on hub
+			& (~ hub_clear_feature_by_writing_one_mask);
+	return EOK;
+}
+
+/**
+ * process feature-disabling request on hub
+ *
+ * @param instance root hub instance
+ * @param feature feature selector
+ * @return error code
+ */
+static int process_hub_feature_clear_request(rh_t *instance,
+		uint16_t feature){
+	if(! ((1<<feature) & hub_clear_feature_valid_mask))
+		return EINVAL;
+	//is the feature cleared directly?
+	if ((1<<feature) & hub_set_feature_direct_mask){
+		instance->registers->rh_status =
+			(instance->registers->rh_status & (~(1<<feature)))
+			& (~ hub_clear_feature_by_writing_one_mask);
+	}else{//the feature is cleared by writing '1'
+		instance->registers->rh_status =
+				(instance->registers->rh_status
+				& (~ hub_clear_feature_by_writing_one_mask))
+				| (1<<feature);
+	}
+	return EOK;
+}
+
+
+
+/**
+ * process feature-enabling request on hub
  * 
  * @param instance root hub instance
@@ -436,17 +516,43 @@
  */
 static int process_port_feature_set_request(rh_t *instance,
-		uint16_t feature, uint16_t port, bool enable){
-	if(feature > USB_HUB_FEATURE_C_PORT_RESET)
+		uint16_t feature, uint16_t port){
+	if(!((1<<feature) & port_set_feature_valid_mask))
 		return EINVAL;
 	if(port<1 || port>instance->port_count)
 		return EINVAL;
 	instance->registers->rh_port_status[port - 1] =
-			enable ?
 			(instance->registers->rh_port_status[port - 1] | (1<<feature))
-			:
-			(instance->registers->rh_port_status[port - 1] & (~(1<<feature)));
+			& (~port_clear_feature_valid_mask);
 	/// \TODO any error?
 	return EOK;
 }
+
+/**
+ * process feature-disabling request on hub
+ *
+ * @param instance root hub instance
+ * @param feature feature selector
+ * @param port port number, counted from 1
+ * @param enable enable or disable the specified feature
+ * @return error code
+ */
+static int process_port_feature_clear_request(rh_t *instance,
+		uint16_t feature, uint16_t port){
+	if(!((1<<feature) & port_clear_feature_valid_mask))
+		return EINVAL;
+	if(port<1 || port>instance->port_count)
+		return EINVAL;
+	if(feature == USB_HUB_FEATURE_PORT_POWER)
+		feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
+	if(feature == USB_HUB_FEATURE_PORT_SUSPEND)
+		feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
+	instance->registers->rh_port_status[port - 1] =
+			(instance->registers->rh_port_status[port - 1] 
+			& (~port_clear_feature_valid_mask))
+			| (1<<feature);
+	/// \TODO any error?
+	return EOK;
+}
+
 
 /**
@@ -530,16 +636,31 @@
 			(usb_device_request_setup_packet_t*)request->setup_buffer;
 	request->transfered_size = 0;
-	if(setup_request->request == USB_DEVREQ_CLEAR_FEATURE
-				|| setup_request->request == USB_DEVREQ_SET_FEATURE){
+	if(setup_request->request == USB_DEVREQ_CLEAR_FEATURE){
 		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
-			return process_hub_feature_set_request(instance, setup_request->value,
-					setup_request->request == USB_DEVREQ_SET_FEATURE);
+			return process_hub_feature_clear_request(instance,
+					setup_request->value);
 		}
 		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
-			return process_port_feature_set_request(instance, setup_request->value,
-					setup_request->index,
-					setup_request->request == USB_DEVREQ_SET_FEATURE);
+			return process_port_feature_clear_request(instance,
+					setup_request->value,
+					setup_request->index);
+		}
+		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
+				setup_request->request_type);
+		return EINVAL;
+	}
+	if(setup_request->request == USB_DEVREQ_SET_FEATURE){
+		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
+			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
+			return process_hub_feature_set_request(instance,
+					setup_request->value);
+		}
+		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
+			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
+			return process_port_feature_set_request(instance,
+					setup_request->value,
+					setup_request->index);
 		}
 		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",setup_request->request_type);
Index: uspace/drv/ohci/root_hub.h
===================================================================
--- uspace/drv/ohci/root_hub.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/ohci/root_hub.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -37,4 +37,5 @@
 
 #include <usb/usb.h>
+#include <usb/devdrv.h>
 
 #include "ohci_regs.h"
@@ -53,4 +54,6 @@
 	/** hub port count */
 	int port_count;
+	/** hubs descriptors */
+	usb_device_descriptors_t descriptors;
 } rh_t;
 
Index: uspace/drv/pciintel/pci.c
===================================================================
--- uspace/drv/pciintel/pci.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/pciintel/pci.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -48,4 +48,5 @@
 
 #include <ddf/driver.h>
+#include <ddf/log.h>
 #include <devman.h>
 #include <ipc/devman.h>
@@ -106,9 +107,11 @@
 	}
 
-	size_t i;
-	for (i = 0; i < dev_data->hw_resources.count; i++) {
-		if (dev_data->hw_resources.resources[i].type == INTERRUPT) {
-			int irq = dev_data->hw_resources.resources[i].res.interrupt.irq;
-			int rc = async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
+	size_t i = 0;
+	hw_resource_list_t *res = &dev_data->hw_resources;
+	for (; i < res->count; i++) {
+		if (res->resources[i].type == INTERRUPT) {
+			const int irq = res->resources[i].res.interrupt.irq;
+			const int rc =
+			    async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq);
 			if (rc != EOK) {
 				async_hangup(irc_phone);
@@ -325,5 +328,5 @@
 
 	if (match_id_str == NULL) {
-		printf(NAME ": out of memory creating match ID.\n");
+		ddf_msg(LVL_ERROR, "Out of memory creating match ID.");
 		return;
 	}
@@ -331,5 +334,5 @@
 	rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
 	if (rc != EOK) {
-		printf(NAME ": error adding match ID: %s\n",
+		ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
 		    str_error(rc));
 	}
@@ -428,7 +431,7 @@
 	
 	if (range_addr != 0) {
-		printf(NAME ": function %s : ", fun->fnode->name);
-		printf("address = %" PRIx64, range_addr);
-		printf(", size = %x\n", (unsigned int) range_size);
+		ddf_msg(LVL_DEBUG, "Function %s : address = %" PRIx64
+		    ", size = %x", fun->fnode->name, range_addr,
+		    (unsigned int) range_size);
 	}
 	
@@ -455,5 +458,5 @@
 	hw_res_list->count++;
 	
-	printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
+	ddf_msg(LVL_NOTE, "Function %s uses irq %x.", fun->fnode->name, irq);
 }
 
@@ -511,5 +514,5 @@
 			char *fun_name = pci_fun_create_name(fun);
 			if (fun_name == NULL) {
-				printf(NAME ": out of memory.\n");
+				ddf_msg(LVL_ERROR, "Out of memory.");
 				return;
 			}
@@ -517,5 +520,5 @@
 			fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
 			if (fnode == NULL) {
-				printf(NAME ": error creating function.\n");
+				ddf_msg(LVL_ERROR, "Failed creating function.");
 				return;
 			}
@@ -531,5 +534,5 @@
 			fnode->driver_data = fun;
 			
-			printf(NAME ": adding new function %s.\n",
+			ddf_msg(LVL_DEBUG, "Adding new function %s.",
 			    fnode->name);
 			
@@ -548,6 +551,7 @@
 				child_bus = pci_conf_read_8(fun,
 				    PCI_BRIDGE_SEC_BUS_NUM);
-				printf(NAME ": device is pci-to-pci bridge, "
-				    "secondary bus number = %d.\n", bus_num);
+				ddf_msg(LVL_DEBUG, "Device is pci-to-pci "
+				    "bridge, secondary bus number = %d.",
+				    bus_num);
 				if (child_bus > bus_num)
 					pci_bus_scan(bus, child_bus);
@@ -571,10 +575,10 @@
 	int rc;
 	
-	printf(NAME ": pci_add_device\n");
+	ddf_msg(LVL_DEBUG, "pci_add_device");
 	dnode->parent_phone = -1;
 	
 	bus = pci_bus_new();
 	if (bus == NULL) {
-		printf(NAME ": pci_add_device allocation failed.\n");
+		ddf_msg(LVL_ERROR, "pci_add_device allocation failed.");
 		rc = ENOMEM;
 		goto fail;
@@ -586,6 +590,6 @@
 	    IPC_FLAG_BLOCKING);
 	if (dnode->parent_phone < 0) {
-		printf(NAME ": pci_add_device failed to connect to the "
-		    "parent's driver.\n");
+		ddf_msg(LVL_ERROR, "pci_add_device failed to connect to the "
+		    "parent's driver.");
 		rc = dnode->parent_phone;
 		goto fail;
@@ -596,11 +600,11 @@
 	rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
 	if (rc != EOK) {
-		printf(NAME ": pci_add_device failed to get hw resources for "
-		    "the device.\n");
+		ddf_msg(LVL_ERROR, "pci_add_device failed to get hw resources "
+		    "for the device.");
 		goto fail;
 	}
 	got_res = true;
 	
-	printf(NAME ": conf_addr = %" PRIx64 ".\n",
+	ddf_msg(LVL_DEBUG, "conf_addr = %" PRIx64 ".",
 	    hw_resources.resources[0].res.io_range.address);
 	
@@ -614,5 +618,5 @@
 	if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
 	    &bus->conf_addr_port)) {
-		printf(NAME ": failed to enable configuration ports.\n");
+		ddf_msg(LVL_ERROR, "Failed to enable configuration ports.");
 		rc = EADDRNOTAVAIL;
 		goto fail;
@@ -621,9 +625,9 @@
 	
 	/* Make the bus device more visible. It has no use yet. */
-	printf(NAME ": adding a 'ctl' function\n");
+	ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
 	
 	ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
 	if (ctl == NULL) {
-		printf(NAME ": error creating control function.\n");
+		ddf_msg(LVL_ERROR, "Failed creating control function.");
 		rc = ENOMEM;
 		goto fail;
@@ -632,10 +636,10 @@
 	rc = ddf_fun_bind(ctl);
 	if (rc != EOK) {
-		printf(NAME ": error binding control function.\n");
+		ddf_msg(LVL_ERROR, "Failed binding control function.");
 		goto fail;
 	}
 	
 	/* Enumerate functions. */
-	printf(NAME ": scanning the bus\n");
+	ddf_msg(LVL_DEBUG, "Scanning the bus");
 	pci_bus_scan(bus, 0);
 	
@@ -659,4 +663,5 @@
 static void pciintel_init(void)
 {
+	ddf_log_init(NAME, LVL_ERROR);
 	pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
 	pci_fun_ops.interfaces[PCI_DEV_IFACE] = &pci_dev_ops;
@@ -738,5 +743,5 @@
 int main(int argc, char *argv[])
 {
-	printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
+	printf(NAME ": HelenOS PCI bus driver (Intel method 1).\n");
 	pciintel_init();
 	return ddf_driver_main(&pci_driver);
Index: uspace/drv/root/root.c
===================================================================
--- uspace/drv/root/root.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/root/root.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -52,4 +52,5 @@
 
 #include <ddf/driver.h>
+#include <ddf/log.h>
 #include <devman.h>
 #include <ipc/devman.h>
@@ -89,11 +90,11 @@
 	int rc;
 
-	printf(NAME ": adding new function for virtual devices.\n");
-	printf(NAME ":   function node is `%s' (%d %s)\n", name,
+	ddf_msg(LVL_DEBUG, "Adding new function for virtual devices. "
+	    "Function node is `%s' (%d %s)", name,
 	    VIRTUAL_FUN_MATCH_SCORE, VIRTUAL_FUN_MATCH_ID);
 
 	fun = ddf_fun_create(dev, fun_inner, name);
 	if (fun == NULL) {
-		printf(NAME ": error creating function %s\n", name);
+		ddf_msg(LVL_ERROR, "Failed creating function %s", name);
 		return ENOMEM;
 	}
@@ -102,5 +103,6 @@
 	    VIRTUAL_FUN_MATCH_SCORE);
 	if (rc != EOK) {
-		printf(NAME ": error adding match IDs to function %s\n", name);
+		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
+		    name);
 		ddf_fun_destroy(fun);
 		return rc;
@@ -109,5 +111,5 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function %s: %s\n", name,
+		ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
 		    str_error(rc));
 		ddf_fun_destroy(fun);
@@ -136,5 +138,5 @@
 	platform = sysinfo_get_data("platform", &platform_size);
 	if (platform == NULL) {
-		printf(NAME ": Failed to obtain platform name.\n");
+		ddf_msg(LVL_ERROR, "Failed to obtain platform name.");
 		return ENOENT;
 	}
@@ -143,5 +145,5 @@
 	platform = realloc(platform, platform_size + 1);
 	if (platform == NULL) {
-		printf(NAME ": Memory allocation failed.\n");
+		ddf_msg(LVL_ERROR, "Memory allocation failed.");
 		return ENOMEM;
 	}
@@ -151,16 +153,16 @@
 	/* Construct match ID. */
 	if (asprintf(&match_id, PLATFORM_FUN_MATCH_ID_FMT, platform) == -1) {
-		printf(NAME ": Memory allocation failed.\n");
+		ddf_msg(LVL_ERROR, "Memory allocation failed.");
 		return ENOMEM;
 	}
 
 	/* Add function. */
-	printf(NAME ": adding platform function\n");
-	printf(NAME ":   function node is `%s' (%d %s)\n", PLATFORM_FUN_NAME,
-	    PLATFORM_FUN_MATCH_SCORE, match_id);
+	ddf_msg(LVL_DEBUG, "Adding platform function. Function node is `%s' "
+	    " (%d %s)", PLATFORM_FUN_NAME, PLATFORM_FUN_MATCH_SCORE,
+	    match_id);
 
 	fun = ddf_fun_create(dev, fun_inner, name);
 	if (fun == NULL) {
-		printf(NAME ": error creating function %s\n", name);
+		ddf_msg(LVL_ERROR, "Error creating function %s", name);
 		return ENOMEM;
 	}
@@ -168,5 +170,6 @@
 	rc = ddf_fun_add_match_id(fun, match_id, PLATFORM_FUN_MATCH_SCORE);
 	if (rc != EOK) {
-		printf(NAME ": error adding match IDs to function %s\n", name);
+		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
+		    name);
 		ddf_fun_destroy(fun);
 		return rc;
@@ -175,5 +178,5 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function %s: %s\n", name,
+		ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
 		    str_error(rc));
 		ddf_fun_destroy(fun);
@@ -191,5 +194,5 @@
 static int root_add_device(ddf_dev_t *dev)
 {
-	printf(NAME ": root_add_device, device handle=%" PRIun "\n",
+	ddf_msg(LVL_DEBUG, "root_add_device, device handle=%" PRIun,
 	    dev->handle);
 
@@ -204,5 +207,5 @@
 	int res = add_platform_fun(dev);
 	if (EOK != res)
-		printf(NAME ": failed to add child device for platform.\n");
+		ddf_msg(LVL_ERROR, "Failed adding child device for platform.");
 
 	return res;
@@ -212,4 +215,6 @@
 {
 	printf(NAME ": HelenOS root device driver\n");
+
+	ddf_log_init(NAME, LVL_ERROR);
 	return ddf_driver_main(&root_driver);
 }
Index: uspace/drv/rootpc/rootpc.c
===================================================================
--- uspace/drv/rootpc/rootpc.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/rootpc/rootpc.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -47,4 +47,5 @@
 
 #include <ddf/driver.h>
+#include <ddf/log.h>
 #include <devman.h>
 #include <ipc/devman.h>
@@ -119,5 +120,5 @@
     rootpc_fun_t *fun)
 {
-	printf(NAME ": adding new function '%s'.\n", name);
+	ddf_msg(LVL_DEBUG, "Adding new function '%s'.", name);
 	
 	ddf_fun_t *fnode = NULL;
@@ -145,5 +146,5 @@
 	/* Register function. */
 	if (ddf_fun_bind(fnode) != EOK) {
-		printf(NAME ": error binding function %s.\n", name);
+		ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
 		goto failure;
 	}
@@ -158,5 +159,5 @@
 		ddf_fun_destroy(fnode);
 	
-	printf(NAME ": failed to add function '%s'.\n", name);
+	ddf_msg(LVL_ERROR, "Failed adding function '%s'.", name);
 	
 	return false;
@@ -176,10 +177,10 @@
 static int rootpc_add_device(ddf_dev_t *dev)
 {
-	printf(NAME ": rootpc_add_device, device handle = %d\n",
+	ddf_msg(LVL_DEBUG, "rootpc_add_device, device handle = %d",
 	    (int)dev->handle);
 	
 	/* Register functions. */
 	if (!rootpc_add_functions(dev)) {
-		printf(NAME ": failed to add functions for PC platform.\n");
+		ddf_msg(LVL_ERROR, "Failed to add functions for PC platform.");
 	}
 	
@@ -189,4 +190,5 @@
 static void root_pc_init(void)
 {
+	ddf_log_init(NAME, LVL_ERROR);
 	rootpc_fun_ops.interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops;
 }
Index: uspace/drv/rootvirt/rootvirt.c
===================================================================
--- uspace/drv/rootvirt/rootvirt.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/rootvirt/rootvirt.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -40,4 +40,5 @@
 #include <str_error.h>
 #include <ddf/driver.h>
+#include <ddf/log.h>
 
 #define NAME "rootvirt"
@@ -83,10 +84,10 @@
 	int rc;
 
-	printf(NAME ": registering function `%s' (match \"%s\")\n",
+	ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
 	    vfun->name, vfun->match_id);
 
 	fun = ddf_fun_create(vdev, fun_inner, vfun->name);
 	if (fun == NULL) {
-		printf(NAME ": error creating function %s\n", vfun->name);
+		ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
 		return ENOMEM;
 	}
@@ -94,5 +95,5 @@
 	rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
 	if (rc != EOK) {
-		printf(NAME ": error adding match IDs to function %s\n",
+		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
 		    vfun->name);
 		ddf_fun_destroy(fun);
@@ -102,11 +103,11 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function %s: %s\n", vfun->name,
-		    str_error(rc));
+		ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
+		    vfun->name, str_error(rc));
 		ddf_fun_destroy(fun);
 		return rc;
 	}
 
-	printf(NAME ": registered child device `%s'\n", vfun->name);
+	ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
 	return EOK;
 }
@@ -124,5 +125,5 @@
 	}
 
-	printf(NAME ": add_device(handle=%d)\n", (int)dev->handle);
+	ddf_msg(LVL_DEBUG, "add_device(handle=%d)", (int)dev->handle);
 
 	/*
@@ -142,4 +143,6 @@
 {
 	printf(NAME ": HelenOS virtual devices root driver\n");
+
+	ddf_log_init(NAME, LVL_ERROR);
 	return ddf_driver_main(&rootvirt_driver);
 }
Index: uspace/drv/test1/test1.c
===================================================================
--- uspace/drv/test1/test1.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/test1/test1.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -35,4 +35,5 @@
 #include <str_error.h>
 #include <ddf/driver.h>
+#include <ddf/log.h>
 
 #include "test1.h"
@@ -58,34 +59,49 @@
  */
 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;
 
-	printf(NAME ": registering function `%s': %s.\n", name, message);
+	ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
 
 	fun = ddf_fun_create(parent, fun_inner, name);
 	if (fun == NULL) {
-		printf(NAME ": error creating function %s\n", name);
-		return ENOMEM;
+		ddf_msg(LVL_ERROR, "Failed creating function %s", name);
+		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) {
-		printf(NAME ": error adding match IDs to function %s\n", name);
-		ddf_fun_destroy(fun);
-		return rc;
+		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
+		    name);
+		goto leave;
 	}
 
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function %s: %s\n", name,
+		ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
 		    str_error(rc));
-		ddf_fun_destroy(fun);
-		return rc;
+		goto leave;
 	}
 
-	printf(NAME ": registered child device `%s'\n", name);
-	return EOK;
+	ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
+	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;
 }
 
@@ -112,10 +128,10 @@
 	int rc;
 
-	printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
+	ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
 	    dev->name, (int) dev->handle);
 
 	fun_a = ddf_fun_create(dev, fun_exposed, "a");
 	if (fun_a == NULL) {
-		printf(NAME ": error creating function 'a'.\n");
+		ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
 		return ENOMEM;
 	}
@@ -123,5 +139,5 @@
 	rc = ddf_fun_bind(fun_a);
 	if (rc != EOK) {
-		printf(NAME ": error binding function 'a'.\n");
+		ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
 		return rc;
 	}
@@ -133,12 +149,17 @@
 		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);
 	}
 
-	printf(NAME ": device `%s' accepted.\n", dev->name);
+	ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
 
 	return EOK;
@@ -148,4 +169,5 @@
 {
 	printf(NAME ": HelenOS test1 virtual device driver\n");
+	ddf_log_init(NAME, LVL_ERROR);
 	return ddf_driver_main(&test1_driver);
 }
Index: uspace/drv/test2/test2.c
===================================================================
--- uspace/drv/test2/test2.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/test2/test2.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -36,4 +36,5 @@
 #include <str_error.h>
 #include <ddf/driver.h>
+#include <ddf/log.h>
 
 #define NAME "test2"
@@ -64,9 +65,9 @@
 	int rc;
 
-	printf(NAME ": registering function `%s': %s.\n", name, message);
+	ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
 
 	fun = ddf_fun_create(parent, fun_inner, name);
 	if (fun == NULL) {
-		printf(NAME ": error creating function %s\n", name);
+		ddf_msg(LVL_ERROR, "Failed creating function %s", name);
 		return ENOMEM;
 	}
@@ -74,5 +75,6 @@
 	rc = ddf_fun_add_match_id(fun, match_id, match_score);
 	if (rc != EOK) {
-		printf(NAME ": error adding match IDs to function %s\n", name);
+		ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
+		    name);
 		ddf_fun_destroy(fun);
 		return rc;
@@ -81,5 +83,5 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		printf(NAME ": error binding function %s: %s\n", name,
+		ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
 		    str_error(rc));
 		ddf_fun_destroy(fun);
@@ -87,5 +89,5 @@
 	}
 
-	printf(NAME ": registered child device `%s'\n", name);
+	ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
 	return EOK;
 }
@@ -111,5 +113,5 @@
 	fun_a = ddf_fun_create(dev, fun_exposed, "a");
 	if (fun_a == NULL) {
-		printf(NAME ": error creating function 'a'.\n");
+		ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
 		return ENOMEM;
 	}
@@ -117,5 +119,5 @@
 	rc = ddf_fun_bind(fun_a);
 	if (rc != EOK) {
-		printf(NAME ": error binding function 'a'.\n");
+		ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
 		return rc;
 	}
@@ -128,5 +130,5 @@
 static int test2_add_device(ddf_dev_t *dev)
 {
-	printf(NAME ": test2_add_device(name=\"%s\", handle=%d)\n",
+	ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
 	    dev->name, (int) dev->handle);
 
@@ -134,5 +136,5 @@
 		fid_t postpone = fibril_create(postponed_birth, dev);
 		if (postpone == 0) {
-			printf(NAME ": fibril_create() error\n");
+			ddf_msg(LVL_ERROR, "fibril_create() failed.");
 			return ENOMEM;
 		}
@@ -149,4 +151,5 @@
 {
 	printf(NAME ": HelenOS test2 virtual device driver\n");
+	ddf_log_init(NAME, LVL_ERROR);
 	return ddf_driver_main(&test2_driver);
 }
Index: uspace/drv/uhci-hcd/batch.c
===================================================================
--- uspace/drv/uhci-hcd/batch.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/batch.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -49,5 +49,4 @@
 	td_t *tds;
 	size_t transfers;
-	usb_device_keeper_t *manager;
 } uhci_batch_t;
 
@@ -73,5 +72,5 @@
  * @param[in] func_out function to call on outbound transaction completion
  * @param[in] arg additional parameter to func_in or func_out
- * @param[in] manager Pointer to toggle management structure.
+ * @param[in] ep Pointer to endpoint toggle management structure.
  * @return Valid pointer if all substructures were successfully created,
  * NULL otherwise.
@@ -86,6 +85,5 @@
     char* setup_buffer, size_t setup_size,
     usbhc_iface_transfer_in_callback_t func_in,
-    usbhc_iface_transfer_out_callback_t func_out, void *arg,
-    usb_device_keeper_t *manager
+    usbhc_iface_transfer_out_callback_t func_out, void *arg, endpoint_t *ep
     )
 {
@@ -105,7 +103,8 @@
 	CHECK_NULL_DISPOSE_RETURN(instance,
 	    "Failed to allocate batch instance.\n");
-	usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size,
+	usb_transfer_batch_init(instance, target,
+	    transfer_type, speed, max_packet_size,
 	    buffer, NULL, buffer_size, NULL, setup_size, func_in,
-	    func_out, arg, fun, NULL);
+	    func_out, arg, fun, ep, NULL);
 
 
@@ -114,5 +113,4 @@
 	    "Failed to allocate batch instance.\n");
 	bzero(data, sizeof(uhci_batch_t));
-	data->manager = manager;
 	instance->private_data = data;
 
@@ -180,8 +178,7 @@
 			    instance, i, data->tds[i].status);
 			td_print_status(&data->tds[i]);
-
-			usb_device_keeper_set_toggle(data->manager,
-			    instance->target, instance->direction,
-			    td_toggle(&data->tds[i]));
+			if (instance->ep != NULL)
+				endpoint_toggle_set(instance->ep,
+				    td_toggle(&data->tds[i]));
 			if (i > 0)
 				goto substract_ret;
@@ -310,6 +307,5 @@
 
 	const bool low_speed = instance->speed == USB_SPEED_LOW;
-	int toggle = usb_device_keeper_get_toggle(
-	    data->manager, instance->target, instance->direction);
+	int toggle = endpoint_toggle_get(instance->ep);
 	assert(toggle == 0 || toggle == 1);
 
@@ -342,6 +338,5 @@
 	}
 	td_set_ioc(&data->tds[transfer - 1]);
-	usb_device_keeper_set_toggle(data->manager, instance->target,
-	    instance->direction, toggle);
+	endpoint_toggle_set(instance->ep, toggle);
 }
 /*----------------------------------------------------------------------------*/
Index: uspace/drv/uhci-hcd/batch.h
===================================================================
--- uspace/drv/uhci-hcd/batch.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/batch.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -35,9 +35,8 @@
 #define DRV_UHCI_BATCH_H
 
-#include <adt/list.h>
-
 #include <usbhc_iface.h>
 #include <usb/usb.h>
 #include <usb/host/device_keeper.h>
+#include <usb/host/endpoint.h>
 #include <usb/host/batch.h>
 
@@ -57,5 +56,5 @@
     usbhc_iface_transfer_out_callback_t func_out,
 		void *arg,
-		usb_device_keeper_t *manager
+		endpoint_t *ep
 		);
 
Index: uspace/drv/uhci-hcd/hc.c
===================================================================
--- uspace/drv/uhci-hcd/hc.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/hc.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -66,7 +66,8 @@
 static int hc_interrupt_emulator(void *arg);
 static int hc_debug_checker(void *arg);
-
+#if 0
 static bool usb_is_allowed(
     bool low_speed, usb_transfer_type_t transfer, size_t size);
+#endif
 /*----------------------------------------------------------------------------*/
 /** Initialize UHCI hcd driver structure
@@ -238,4 +239,9 @@
 	usb_device_keeper_init(&instance->manager);
 	usb_log_debug("Initialized device manager.\n");
+
+	ret =
+	    usb_endpoint_manager_init(&instance->ep_manager,
+	        BANDWIDTH_AVAILABLE_USB11);
+	assert(ret == EOK);
 
 	return EOK;
@@ -322,14 +328,4 @@
 	assert(instance);
 	assert(batch);
-	const int low_speed = (batch->speed == USB_SPEED_LOW);
-	if (!usb_is_allowed(
-	    low_speed, batch->transfer_type, batch->max_packet_size)) {
-		usb_log_warning(
-		    "Invalid USB transfer specified %s SPEED %d %zu.\n",
-		    low_speed ? "LOW" : "FULL" , batch->transfer_type,
-		    batch->max_packet_size);
-		return ENOTSUP;
-	}
-	/* TODO: check available bandwidth here */
 
 	transfer_list_t *list =
@@ -338,5 +334,5 @@
 	if (batch->transfer_type == USB_TRANSFER_CONTROL) {
 		usb_device_keeper_use_control(
-		    &instance->manager, batch->target.address);
+		    &instance->manager, batch->target);
 	}
 	transfer_list_add_batch(list, batch);
@@ -358,4 +354,5 @@
 {
 	assert(instance);
+//	status |= 1; //Uncomment to work around qemu hang
 	/* TODO: Resume interrupts are not supported */
 	/* Lower 2 bits are transaction error and transaction complete */
@@ -376,7 +373,25 @@
 			usb_transfer_batch_t *batch =
 			    list_get_instance(item, usb_transfer_batch_t, link);
-			if (batch->transfer_type == USB_TRANSFER_CONTROL) {
+			switch (batch->transfer_type)
+			{
+			case USB_TRANSFER_CONTROL:
 				usb_device_keeper_release_control(
-				    &instance->manager, batch->target.address);
+				    &instance->manager, batch->target);
+				break;
+			case USB_TRANSFER_INTERRUPT:
+			case USB_TRANSFER_ISOCHRONOUS: {
+/*
+				int ret = bandwidth_free(&instance->bandwidth,
+				    batch->target.address,
+				    batch->target.endpoint,
+				    batch->direction);
+				if (ret != EOK)
+					usb_log_warning("Failed(%d) to free "
+					    "reserved bw: %s.\n", ret,
+					    str_error(ret));
+*/
+				}
+			default:
+				break;
 			}
 			batch->next_step(batch);
@@ -499,4 +514,5 @@
  * @return True if transaction is allowed by USB specs, false otherwise
  */
+#if 0
 bool usb_is_allowed(
     bool low_speed, usb_transfer_type_t transfer, size_t size)
@@ -516,4 +532,5 @@
 	return false;
 }
+#endif
 /**
  * @}
Index: uspace/drv/uhci-hcd/hc.h
===================================================================
--- uspace/drv/uhci-hcd/hc.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/hc.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -43,4 +43,5 @@
 #include <usbhc_iface.h>
 #include <usb/host/device_keeper.h>
+#include <usb/host/usb_endpoint_manager.h>
 
 #include "batch.h"
@@ -84,4 +85,5 @@
 typedef struct hc {
 	usb_device_keeper_t manager;
+	usb_endpoint_manager_t ep_manager;
 
 	regs_t *registers;
Index: uspace/drv/uhci-hcd/hw_struct/queue_head.h
===================================================================
--- uspace/drv/uhci-hcd/hw_struct/queue_head.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/hw_struct/queue_head.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -39,5 +39,4 @@
 
 #include "link_pointer.h"
-#include "utils/malloc32.h"
 
 typedef struct queue_head {
Index: uspace/drv/uhci-hcd/iface.c
===================================================================
--- uspace/drv/uhci-hcd/iface.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/iface.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -36,4 +36,5 @@
 
 #include <usb/debug.h>
+#include <usb/host/endpoint.h>
 
 #include "iface.h"
@@ -54,4 +55,23 @@
 	usb_device_keeper_reserve_default_address(&hc->manager, speed);
 	return EOK;
+#if 0
+	endpoint_t *ep = malloc(sizeof(endpoint_t));
+	if (ep == NULL)
+		return ENOMEM;
+	const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
+	endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
+	int ret;
+try_retgister:
+	ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
+	    USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
+	if (ret == EEXISTS) {
+		async_usleep(1000);
+		goto try_retgister;
+	}
+	if (ret != EOK) {
+		endpoint_destroy(ep);
+	}
+	return ret;
+#endif
 }
 /*----------------------------------------------------------------------------*/
@@ -67,4 +87,6 @@
 	assert(hc);
 	usb_log_debug("Default address release.\n");
+//	return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
+//	    USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
 	usb_device_keeper_release_default_address(&hc->manager);
 	return EOK;
@@ -128,4 +150,54 @@
 }
 /*----------------------------------------------------------------------------*/
+static int register_endpoint(
+    ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
+    usb_transfer_type_t transfer_type, usb_direction_t direction,
+    size_t max_packet_size, unsigned int interval)
+{
+	hc_t *hc = fun_to_hc(fun);
+	assert(hc);
+	const usb_speed_t speed =
+	    usb_device_keeper_get_speed(&hc->manager, address);
+	const size_t size =
+	    (transfer_type == USB_TRANSFER_INTERRUPT
+	    || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
+	    max_packet_size : 0;
+	int ret;
+
+	endpoint_t *ep = malloc(sizeof(endpoint_t));
+	if (ep == NULL)
+		return ENOMEM;
+	ret = endpoint_init(ep, address, endpoint, direction,
+	    transfer_type, speed, max_packet_size);
+	if (ret != EOK) {
+		free(ep);
+		return ret;
+	}
+
+	usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
+	    address, endpoint, usb_str_transfer_type(transfer_type),
+	    usb_str_speed(speed), direction, size, max_packet_size, interval);
+
+	ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
+	if (ret != EOK) {
+		endpoint_destroy(ep);
+	} else {
+		usb_device_keeper_add_ep(&hc->manager, address, ep);
+	}
+	return ret;
+}
+/*----------------------------------------------------------------------------*/
+static int unregister_endpoint(
+    ddf_fun_t *fun, usb_address_t address,
+    usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	hc_t *hc = fun_to_hc(fun);
+	assert(hc);
+	usb_log_debug("Unregister endpoint %d:%d %d.\n",
+	    address, endpoint, direction);
+	return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
+	    endpoint, direction);
+}
+/*----------------------------------------------------------------------------*/
 /** Interrupt out transaction interface function
  *
@@ -146,13 +218,33 @@
 	hc_t *hc = fun_to_hc(fun);
 	assert(hc);
-	usb_speed_t speed =
-	    usb_device_keeper_get_speed(&hc->manager, target.address);
 
 	usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
 	    target.address, target.endpoint, size, max_packet_size);
 
+	size_t res_bw;
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw);
+	if (ep == NULL) {
+		usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n",
+			target.address, target.endpoint);
+		return ENOENT;
+	}
+	const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
+	    size, ep->max_packet_size);
+	if (res_bw < bw)
+	{
+		usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
+		    "but only %zu is reserved.\n",
+		    target.address, target.endpoint, bw, res_bw);
+		return ENOENT;
+	}
+	assert(ep->speed ==
+	    usb_device_keeper_get_speed(&hc->manager, target.address));
+	assert(ep->max_packet_size == max_packet_size);
+	assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
+
 	usb_transfer_batch_t *batch =
-	    batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
-	        speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
+	    batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
+	        ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -183,12 +275,34 @@
 	hc_t *hc = fun_to_hc(fun);
 	assert(hc);
-	usb_speed_t speed =
-	    usb_device_keeper_get_speed(&hc->manager, target.address);
+
 	usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
 	    target.address, target.endpoint, size, max_packet_size);
 
+	size_t res_bw;
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_IN, &res_bw);
+	if (ep == NULL) {
+		usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n",
+		    target.address, target.endpoint);
+		return ENOENT;
+	}
+	const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
+	    size, ep->max_packet_size);
+	if (res_bw < bw)
+	{
+		usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
+		    "but only %zu bw is reserved.\n",
+		    target.address, target.endpoint, bw, res_bw);
+		return ENOENT;
+	}
+
+	assert(ep->speed ==
+	    usb_device_keeper_get_speed(&hc->manager, target.address));
+	assert(ep->max_packet_size == max_packet_size);
+	assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
+
 	usb_transfer_batch_t *batch =
-	    batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
-	        speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
+	    batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
+	        ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -219,13 +333,23 @@
 	hc_t *hc = fun_to_hc(fun);
 	assert(hc);
-	usb_speed_t speed =
-	    usb_device_keeper_get_speed(&hc->manager, target.address);
 
 	usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
 	    target.address, target.endpoint, size, max_packet_size);
 
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_OUT, NULL);
+	if (ep == NULL) {
+		usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n",
+			target.address, target.endpoint);
+		return ENOENT;
+	}
+	assert(ep->speed ==
+	    usb_device_keeper_get_speed(&hc->manager, target.address));
+	assert(ep->max_packet_size == max_packet_size);
+	assert(ep->transfer_type == USB_TRANSFER_BULK);
+
 	usb_transfer_batch_t *batch =
-	    batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
-	        data, size, NULL, 0, NULL, callback, arg, &hc->manager);
+	    batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
+	        ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -256,12 +380,22 @@
 	hc_t *hc = fun_to_hc(fun);
 	assert(hc);
-	usb_speed_t speed =
-	    usb_device_keeper_get_speed(&hc->manager, target.address);
 	usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
 	    target.address, target.endpoint, size, max_packet_size);
 
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_IN, NULL);
+	if (ep == NULL) {
+		usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n",
+			target.address, target.endpoint);
+		return ENOENT;
+	}
+	assert(ep->speed ==
+	    usb_device_keeper_get_speed(&hc->manager, target.address));
+	assert(ep->max_packet_size == max_packet_size);
+	assert(ep->transfer_type == USB_TRANSFER_BULK);
+
 	usb_transfer_batch_t *batch =
-	    batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
-	        data, size, NULL, 0, callback, NULL, arg, &hc->manager);
+	    batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
+	        ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -299,4 +433,10 @@
 	usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
 	    speed, target.address, target.endpoint, size, max_packet_size);
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
+	if (ep == NULL) {
+		usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
+			target.address, target.endpoint);
+	}
 
 	if (setup_size != 8)
@@ -305,6 +445,5 @@
 	usb_transfer_batch_t *batch =
 	    batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
-	        data, size, setup_data, setup_size, NULL, callback, arg,
-	        &hc->manager);
+	        data, size, setup_data, setup_size, NULL, callback, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -344,8 +483,13 @@
 	usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
 	    speed, target.address, target.endpoint, size, max_packet_size);
+	endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
+	    target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
+	if (ep == NULL) {
+		usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
+			target.address, target.endpoint);
+	}
 	usb_transfer_batch_t *batch =
 	    batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
-	        data, size, setup_data, setup_size, callback, NULL, arg,
-		&hc->manager);
+	        data, size, setup_data, setup_size, callback, NULL, arg, ep);
 	if (!batch)
 		return ENOMEM;
@@ -365,4 +509,7 @@
 	.release_address = release_address,
 
+	.register_endpoint = register_endpoint,
+	.unregister_endpoint = unregister_endpoint,
+
 	.interrupt_out = interrupt_out,
 	.interrupt_in = interrupt_in,
Index: uspace/drv/uhci-hcd/transfer_list.h
===================================================================
--- uspace/drv/uhci-hcd/transfer_list.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/transfer_list.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -39,4 +39,5 @@
 #include "batch.h"
 #include "hw_struct/queue_head.h"
+#include "utils/malloc32.h"
 
 typedef struct transfer_list
Index: uspace/drv/uhci-hcd/utils/malloc32.h
===================================================================
--- uspace/drv/uhci-hcd/utils/malloc32.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-hcd/utils/malloc32.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -36,4 +36,5 @@
 
 #include <assert.h>
+#include <errno.h>
 #include <malloc.h>
 #include <mem.h>
Index: uspace/drv/uhci-rhd/main.c
===================================================================
--- uspace/drv/uhci-rhd/main.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-rhd/main.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -44,71 +44,9 @@
 
 #define NAME "uhci-rhd"
+
 static int hc_get_my_registers(ddf_dev_t *dev,
     uintptr_t *io_reg_address, size_t *io_reg_size);
-#if 0
 /*----------------------------------------------------------------------------*/
-static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
-{
-	assert(fun);
-	assert(fun->driver_data);
-	assert(handle);
-
-	*handle = ((uhci_root_hub_t*)fun->driver_data)->hc_handle;
-
-	return EOK;
-}
-/*----------------------------------------------------------------------------*/
-static usb_iface_t uhci_rh_usb_iface = {
-	.get_hc_handle = usb_iface_get_hc_handle,
-	.get_address = usb_iface_get_address_hub_impl
-};
-/*----------------------------------------------------------------------------*/
-static ddf_dev_ops_t uhci_rh_ops = {
-	.interfaces[USB_DEV_IFACE] = &uhci_rh_usb_iface,
-};
-#endif
-/*----------------------------------------------------------------------------*/
-/** Initialize a new ddf driver instance of UHCI root hub.
- *
- * @param[in] device DDF instance of the device to initialize.
- * @return Error code.
- */
-static int uhci_rh_add_device(ddf_dev_t *device)
-{
-	if (!device)
-		return ENOTSUP;
-
-	usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle);
-
-	//device->ops = &uhci_rh_ops;
-	uintptr_t io_regs = 0;
-	size_t io_size = 0;
-
-	int ret = hc_get_my_registers(device, &io_regs, &io_size);
-	if (ret != EOK) {
-		usb_log_error("Failed to get registers from parent HC: %s.\n",
-		    str_error(ret));
-	}
-	usb_log_debug("I/O regs at %#X (size %zu).\n", io_regs, io_size);
-
-	uhci_root_hub_t *rh = malloc(sizeof(uhci_root_hub_t));
-	if (!rh) {
-		usb_log_error("Failed to allocate driver instance.\n");
-		return ENOMEM;
-	}
-
-	ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
-	if (ret != EOK) {
-		usb_log_error("Failed to initialize driver instance: %s.\n",
-		    str_error(ret));
-		free(rh);
-		return ret;
-	}
-
-	device->driver_data = rh;
-	usb_log_info("Controlling root hub `%s' (%llu).\n",
-	    device->name, device->handle);
-	return EOK;
-}
+static int uhci_rh_add_device(ddf_dev_t *device);
 /*----------------------------------------------------------------------------*/
 static driver_ops_t uhci_rh_driver_ops = {
@@ -132,8 +70,52 @@
 {
 	printf(NAME ": HelenOS UHCI root hub driver.\n");
+	usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
+	return ddf_driver_main(&uhci_rh_driver);
+}
+/*----------------------------------------------------------------------------*/
+/** Initialize a new ddf driver instance of UHCI root hub.
+ *
+ * @param[in] device DDF instance of the device to initialize.
+ * @return Error code.
+ */
+static int uhci_rh_add_device(ddf_dev_t *device)
+{
+	if (!device)
+		return EINVAL;
 
-	usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
+	usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle);
 
-	return ddf_driver_main(&uhci_rh_driver);
+	uintptr_t io_regs = 0;
+	size_t io_size = 0;
+	uhci_root_hub_t *rh = NULL;
+	int ret = EOK;
+
+#define CHECK_RET_FREE_RH_RETURN(ret, message...) \
+if (ret != EOK) { \
+	usb_log_error(message); \
+	if (rh) \
+		free(rh); \
+	return ret; \
+} else (void)0
+
+	ret = hc_get_my_registers(device, &io_regs, &io_size);
+	CHECK_RET_FREE_RH_RETURN(ret,
+	    "Failed(%d) to get registers from HC: %s.\n", ret, str_error(ret));
+	usb_log_debug("I/O regs at %#x (size %zu).\n", io_regs, io_size);
+
+	rh = malloc(sizeof(uhci_root_hub_t));
+	ret = (rh == NULL) ? ENOMEM : EOK;
+	CHECK_RET_FREE_RH_RETURN(ret,
+	    "Failed to allocate rh driver instance.\n");
+
+	ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
+	CHECK_RET_FREE_RH_RETURN(ret,
+	    "Failed(%d) to initialize rh driver instance: %s.\n",
+	    ret, str_error(ret));
+
+	device->driver_data = rh;
+	usb_log_info("Controlling root hub '%s' (%llu).\n",
+	    device->name, device->handle);
+	return EOK;
 }
 /*----------------------------------------------------------------------------*/
@@ -156,10 +138,9 @@
 	}
 
-	int rc;
-
 	hw_resource_list_t hw_resources;
-	rc = hw_res_get_resource_list(parent_phone, &hw_resources);
-	if (rc != EOK) {
-		goto leave;
+	int ret = hw_res_get_resource_list(parent_phone, &hw_resources);
+	if (ret != EOK) {
+		async_hangup(parent_phone);
+		return ret;
 	}
 
@@ -168,24 +149,18 @@
 	bool io_found = false;
 
-	size_t i;
-	for (i = 0; i < hw_resources.count; i++) {
+	size_t i = 0;
+	for (; i < hw_resources.count; i++) {
 		hw_resource_t *res = &hw_resources.resources[i];
-		switch (res->type)
-		{
-		case IO_RANGE:
-			io_address = (uintptr_t) res->res.io_range.address;
+		if (res->type == IO_RANGE) {
+			io_address = res->res.io_range.address;
 			io_size = res->res.io_range.size;
 			io_found = true;
-
-		default:
-			break;
 		}
 	}
+	async_hangup(parent_phone);
 
 	if (!io_found) {
-		rc = ENOENT;
-		goto leave;
+		return ENOENT;
 	}
-
 	if (io_reg_address != NULL) {
 		*io_reg_address = io_address;
@@ -194,9 +169,5 @@
 		*io_reg_size = io_size;
 	}
-	rc = EOK;
-
-leave:
-	async_hangup(parent_phone);
-	return rc;
+	return EOK;
 }
 /**
Index: uspace/drv/uhci-rhd/port.c
===================================================================
--- uspace/drv/uhci-rhd/port.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-rhd/port.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -43,9 +43,9 @@
 #include "port.h"
 
+static int uhci_port_check(void *port);
+static int uhci_port_reset_enable(int portno, void *arg);
 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 uhci_port_reset_enable(int portno, void *arg);
 static void uhci_port_print_status(
     uhci_port_t *port, const port_status_t value);
@@ -74,5 +74,4 @@
 	pio_write_16(port->address, value);
 }
-
 /*----------------------------------------------------------------------------*/
 /** Initialize UHCI root hub port instance.
@@ -259,13 +258,13 @@
 
 	usb_address_t dev_addr;
-	int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
+	int ret = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
 	    speed, uhci_port_reset_enable, port->number, port,
 	    &dev_addr, &port->attached_device, NULL, NULL, NULL);
 
-	if (rc != EOK) {
+	if (ret != EOK) {
 		usb_log_error("%s: Failed(%d) to add device: %s.\n",
-		    port->id_string, rc, str_error(rc));
+		    port->id_string, ret, str_error(ret));
 		uhci_port_set_enabled(port, false);
-		return rc;
+		return ret;
 	}
 
@@ -287,7 +286,7 @@
 int uhci_port_remove_device(uhci_port_t *port)
 {
-	usb_log_error("%s: Don't know how to remove device %d.\n",
-	    port->id_string, (unsigned int)port->attached_device);
-	return EOK;
+	usb_log_error("%s: Don't know how to remove device %llu.\n",
+	    port->id_string, port->attached_device);
+	return ENOTSUP;
 }
 /*----------------------------------------------------------------------------*/
@@ -341,5 +340,5 @@
 	    (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "",
 	    (value & STATUS_CONNECTED) ? " CONNECTED," : "",
-	    (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERROR: NO ALWAYS ONE"
+	    (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR: NO ALWAYS ONE"
 	);
 }
Index: uspace/drv/uhci-rhd/root_hub.c
===================================================================
--- uspace/drv/uhci-rhd/root_hub.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-rhd/root_hub.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -51,10 +51,9 @@
 	assert(instance);
 	assert(rh);
-	int ret;
 
 	/* Allow access to root hub port registers */
 	assert(sizeof(port_status_t) * UHCI_ROOT_HUB_PORT_COUNT <= size);
 	port_status_t *regs;
-	ret = pio_enable(addr, size, (void**)&regs);
+	int ret = pio_enable(addr, size, (void**)&regs);
 	if (ret < 0) {
 		usb_log_error(
@@ -84,7 +83,6 @@
  *
  * @param[in] instance Root hub structure to use.
- * @return Error code.
  */
-int uhci_root_hub_fini(uhci_root_hub_t* instance)
+void uhci_root_hub_fini(uhci_root_hub_t* instance)
 {
 	assert(instance);
@@ -93,5 +91,4 @@
 		uhci_port_fini(&instance->ports[i]);
 	}
-	return EOK;
 }
 /*----------------------------------------------------------------------------*/
Index: uspace/drv/uhci-rhd/root_hub.h
===================================================================
--- uspace/drv/uhci-rhd/root_hub.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/uhci-rhd/root_hub.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -50,5 +50,5 @@
   uhci_root_hub_t *instance, void *addr, size_t size, ddf_dev_t *rh);
 
-int uhci_root_hub_fini(uhci_root_hub_t *instance);
+void uhci_root_hub_fini(uhci_root_hub_t *instance);
 #endif
 /**
Index: uspace/drv/usbhub/Makefile
===================================================================
--- uspace/drv/usbhub/Makefile	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbhub/Makefile	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -34,7 +34,7 @@
 SOURCES = \
 	main.c \
+	ports.c \
 	utils.c \
-	usbhub.c \
-	usblist.c
+	usbhub.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/drv/usbhub/port_status.h
===================================================================
--- uspace/drv/usbhub/port_status.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbhub/port_status.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -95,4 +95,20 @@
 }
 
+/**
+ * set the device request to be a port feature clear request
+ * @param request
+ * @param port
+ * @param feature_selector
+ */
+static inline void usb_hub_set_disable_port_feature_request(
+usb_device_request_setup_packet_t * request, uint16_t port,
+		uint16_t feature_selector
+){
+	request->index = port;
+	request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
+	request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
+	request->value = feature_selector;
+	request->length = 0;
+}
 
 /**
Index: uspace/drv/usbhub/ports.c
===================================================================
--- uspace/drv/usbhub/ports.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/usbhub/ports.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,331 @@
+/*
+ * 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 drvusbhub
+ * @{
+ */
+/** @file
+ * Hub ports functions.
+ */
+#include "port_status.h"
+#include <inttypes.h>
+#include <errno.h>
+#include <str_error.h>
+#include <usb/request.h>
+#include <usb/debug.h>
+
+/** Retrieve port status.
+ *
+ * @param[in] ctrl_pipe Control pipe to use.
+ * @param[in] port Port number (starting at 1).
+ * @param[out] status Where to store the port status.
+ * @return Error code.
+ */
+static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
+    usb_port_status_t *status)
+{
+	size_t recv_size;
+	usb_device_request_setup_packet_t request;
+	usb_port_status_t status_tmp;
+
+	usb_hub_set_port_status_request(&request, port);
+	int rc = usb_pipe_control_read(ctrl_pipe,
+	    &request, sizeof(usb_device_request_setup_packet_t),
+	    &status_tmp, sizeof(status_tmp), &recv_size);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	if (recv_size != sizeof (status_tmp)) {
+		return ELIMIT;
+	}
+
+	if (status != NULL) {
+		*status = status_tmp;
+	}
+
+	return EOK;
+}
+
+/** Information for fibril for device discovery. */
+struct add_device_phase1 {
+	usb_hub_info_t *hub;
+	size_t port;
+	usb_speed_t speed;
+};
+
+/** Callback for enabling a specific port.
+ *
+ * We wait on a CV until port is reseted.
+ * That is announced via change on interrupt pipe.
+ *
+ * @param port_no Port number (starting at 1).
+ * @param arg Custom argument, points to @c usb_hub_info_t.
+ * @return Error code.
+ */
+static int enable_port_callback(int port_no, void *arg)
+{
+	usb_hub_info_t *hub = (usb_hub_info_t *) arg;
+	int rc;
+	usb_device_request_setup_packet_t request;
+	usb_hub_port_t *my_port = hub->ports + port_no;
+
+	usb_hub_set_reset_port_request(&request, port_no);
+	rc = usb_pipe_control_write(hub->control_pipe,
+	    &request, sizeof(request), NULL, 0);
+	if (rc != EOK) {
+		usb_log_warning("Port reset failed: %s.\n", str_error(rc));
+		return rc;
+	}
+
+	/*
+	 * Wait until reset completes.
+	 */
+	fibril_mutex_lock(&my_port->reset_mutex);
+	while (!my_port->reset_completed) {
+		fibril_condvar_wait(&my_port->reset_cv, &my_port->reset_mutex);
+	}
+	fibril_mutex_unlock(&my_port->reset_mutex);
+
+	/* Clear the port reset change. */
+	rc = usb_hub_clear_port_feature(hub->control_pipe,
+	    port_no, USB_HUB_FEATURE_C_PORT_RESET);
+	if (rc != EOK) {
+		usb_log_error("Failed to clear port %d reset feature: %s.\n",
+		    port_no, str_error(rc));
+		return rc;
+	}
+
+	return EOK;
+}
+
+/** Fibril for adding a new device.
+ *
+ * Separate fibril is needed because the port reset completion is announced
+ * via interrupt pipe and thus we cannot block here.
+ *
+ * @param arg Pointer to struct add_device_phase1.
+ * @return 0 Always.
+ */
+static int add_device_phase1_worker_fibril(void *arg)
+{
+	struct add_device_phase1 *data
+	    = (struct add_device_phase1 *) arg;
+
+	usb_address_t new_address;
+	devman_handle_t child_handle;
+
+	int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
+	    &data->hub->connection, data->speed,
+	    enable_port_callback, (int) data->port, data->hub,
+	    &new_address, &child_handle,
+	    NULL, NULL, NULL);
+
+	if (rc != EOK) {
+		usb_log_error("Failed registering device on port %zu: %s.\n",
+		    data->port, str_error(rc));
+		goto leave;
+	}
+
+	data->hub->ports[data->port].attached_device.handle = child_handle;
+	data->hub->ports[data->port].attached_device.address = new_address;
+
+	usb_log_info("Detected new device on `%s' (port %zu), " \
+	    "address %d (handle %" PRIun ").\n",
+	    data->hub->usb_device->ddf_dev->name, data->port,
+	    new_address, child_handle);
+
+leave:
+	free(arg);
+
+	return EOK;
+}
+
+/** Start device adding when connection change is detected.
+ *
+ * This fires a new fibril to complete the device addition.
+ *
+ * @param hub Hub where the change occured.
+ * @param port Port index (starting at 1).
+ * @param speed Speed of the device.
+ * @return Error code.
+ */
+static int add_device_phase1_new_fibril(usb_hub_info_t *hub, size_t port,
+    usb_speed_t speed)
+{
+	struct add_device_phase1 *data
+	    = malloc(sizeof(struct add_device_phase1));
+	if (data == NULL) {
+		return ENOMEM;
+	}
+	data->hub = hub;
+	data->port = port;
+	data->speed = speed;
+
+	usb_hub_port_t *the_port = hub->ports + port;
+
+	fibril_mutex_lock(&the_port->reset_mutex);
+	the_port->reset_completed = false;
+	fibril_mutex_unlock(&the_port->reset_mutex);
+
+	int rc = usb_hub_clear_port_feature(hub->control_pipe, port,
+	    USB_HUB_FEATURE_C_PORT_CONNECTION);
+	if (rc != EOK) {
+		free(data);
+		usb_log_warning("Failed to clear port change flag: %s.\n",
+		    str_error(rc));
+		return rc;
+	}
+
+	fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
+	if (fibril == 0) {
+		free(data);
+		return ENOMEM;
+	}
+	fibril_add_ready(fibril);
+
+	return EOK;
+}
+
+/** Process change on a single port.
+ *
+ * @param hub Hub to which the port belongs.
+ * @param port Port index (starting at 1).
+ */
+static void process_port_change(usb_hub_info_t *hub, size_t port)
+{
+	int rc;
+
+	usb_port_status_t port_status;
+
+	rc = get_port_status(&hub->usb_device->ctrl_pipe, port, &port_status);
+	if (rc != EOK) {
+		usb_log_error("Failed to get port %zu status: %s.\n",
+		    port, str_error(rc));
+		return;
+	}
+
+	/*
+	 * Check exact nature of the change.
+	 */
+	usb_log_debug("Port %zu change status: %x.\n", port,
+	    (unsigned int) port_status);
+
+	if (usb_port_connect_change(&port_status)) {
+		bool device_connected = usb_port_dev_connected(&port_status);
+		usb_log_debug("Connection change on port %zu: %s.\n", port,
+		    device_connected ? "device attached" : "device removed");
+
+		if (device_connected) {
+			rc = add_device_phase1_new_fibril(hub, port,
+			    usb_port_speed(&port_status));
+			if (rc != EOK) {
+				usb_log_error(
+				    "Cannot handle change on port %zu: %s.\n",
+				    str_error(rc));
+			}
+		} else {
+			usb_hub_removed_device(hub, port);
+		}
+	}
+
+	if (usb_port_overcurrent_change(&port_status)) {
+		if (usb_port_over_current(&port_status)) {
+			usb_log_warning("Overcurrent on port %zu.\n", port);
+			usb_hub_over_current(hub, port);
+		} else {
+			usb_log_debug("Overcurrent on port %zu autoresolved.\n",
+			    port);
+		}
+	}
+
+	if (usb_port_reset_completed(&port_status)) {
+		usb_log_debug("Port %zu reset complete.\n", port);
+		if (usb_port_enabled(&port_status)) {
+			/* Finalize device adding. */
+			usb_hub_port_t *the_port = hub->ports + port;
+			fibril_mutex_lock(&the_port->reset_mutex);
+			the_port->reset_completed = true;
+			fibril_condvar_broadcast(&the_port->reset_cv);
+			fibril_mutex_unlock(&the_port->reset_mutex);
+		} else {
+			usb_log_warning(
+			    "Port %zu reset complete but port not enabled.\n",
+			    port);
+		}
+	}
+
+	usb_port_set_connect_change(&port_status, false);
+	usb_port_set_reset(&port_status, false);
+	usb_port_set_reset_completed(&port_status, false);
+	usb_port_set_dev_connected(&port_status, false);
+	if (port_status >> 16) {
+		usb_log_warning("Unsupported change on port %zu: %x.\n",
+		    port, (unsigned int) port_status);
+	}
+}
+
+
+/** Callback for polling hub for port changes.
+ *
+ * @param dev Device where the change occured.
+ * @param change_bitmap Bitmap of changed ports.
+ * @param change_bitmap_size Size of the bitmap in bytes.
+ * @param arg Custom argument, points to @c usb_hub_info_t.
+ * @return Whether to continue polling.
+ */
+bool hub_port_changes_callback(usb_device_t *dev,
+    uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
+{
+	usb_hub_info_t *hub = (usb_hub_info_t *) arg;
+
+	/* FIXME: check that we received enough bytes. */
+	if (change_bitmap_size == 0) {
+		goto leave;
+	}
+
+	size_t port;
+	for (port = 1; port < hub->port_count + 1; port++) {
+		bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
+		if (change) {
+			process_port_change(hub, port);
+		}
+	}
+
+
+leave:
+	/* FIXME: proper interval. */
+	async_usleep(1000 * 1000 * 10 );
+
+	return true;
+}
+
+
+/**
+ * @}
+ */
Index: uspace/drv/usbhub/ports.h
===================================================================
--- uspace/drv/usbhub/ports.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/drv/usbhub/ports.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,80 @@
+/*
+ * 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 drvusbhub
+ * @{
+ */
+/** @file
+ * Hub ports related functions.
+ */
+#ifndef DRV_USBHUB_PORTS_H
+#define DRV_USBHUB_PORTS_H
+
+#include <ipc/devman.h>
+#include <usb/usb.h>
+#include <ddf/driver.h>
+#include <fibril_synch.h>
+
+#include <usb/hub.h>
+
+#include <usb/pipes.h>
+#include <usb/devdrv.h>
+
+/** Information about single port on a hub. */
+typedef struct {
+	/** Mutex needed by CV for checking port reset. */
+	fibril_mutex_t reset_mutex;
+	/** CV for waiting to port reset completion. */
+	fibril_condvar_t reset_cv;
+	/** Whether port reset is completed.
+	 * Guarded by @c reset_mutex.
+	 */
+	bool reset_completed;
+
+	/** Information about attached device. */
+	usb_hc_attached_device_t attached_device;
+} usb_hub_port_t;
+
+/** Initialize hub port information.
+ *
+ * @param port Port to be initialized.
+ */
+static inline void usb_hub_port_init(usb_hub_port_t *port) {
+	port->attached_device.address = -1;
+	port->attached_device.handle = 0;
+	fibril_mutex_initialize(&port->reset_mutex);
+	fibril_condvar_initialize(&port->reset_cv);
+}
+
+bool hub_port_changes_callback(usb_device_t *, uint8_t *, size_t, void *);
+
+
+#endif
+/**
+ * @}
+ */
Index: uspace/drv/usbhub/usbhub.c
===================================================================
--- uspace/drv/usbhub/usbhub.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbhub/usbhub.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -53,32 +53,6 @@
 #include "usb/classes/classes.h"
 
-
-static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
-		usb_speed_t speed);
-
 static int usb_hub_trigger_connecting_non_removable_devices(
 		usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
-
-/**
- * control loop running in hub`s fibril
- *
- * Hub`s fibril periodically asks for changes on hub and if needded calls
- * change handling routine.
- * @warning currently hub driver asks for changes once a second
- * @param hub_info_param hub representation pointer
- * @return zero
- */
-int usb_hub_control_loop(void * hub_info_param){
-	usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
-	int errorCode = EOK;
-
-	while(errorCode == EOK){
-		errorCode = usb_hub_check_hub_changes(hub_info);
-		async_usleep(1000 * 1000 );/// \TODO proper number once
-	}
-	usb_log_error("something in ctrl loop went wrong, errno %d\n",errorCode);
-
-	return 0;
-}
 
 
@@ -152,10 +126,8 @@
 	usb_log_debug("setting port count to %d\n",descriptor->ports_count);
 	hub_info->port_count = descriptor->ports_count;
-	hub_info->attached_devs = (usb_hc_attached_device_t*)
-	    malloc((hub_info->port_count+1) * sizeof(usb_hc_attached_device_t));
-	int i;
-	for(i=0;i<hub_info->port_count+1;++i){
-		hub_info->attached_devs[i].handle=0;
-		hub_info->attached_devs[i].address=0;
+	hub_info->ports = malloc(sizeof(usb_hub_port_t) * (hub_info->port_count+1));
+	size_t port;
+	for (port = 0; port < hub_info->port_count + 1; port++) {
+		usb_hub_port_init(&hub_info->ports[port]);
 	}
 	//handle non-removable devices
@@ -259,16 +231,44 @@
 	assert(rc == EOK);
 
-	//create fibril for the hub control loop
-	fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
-	if (fid == 0) {
-		usb_log_error("failed to start monitoring fibril for new hub.\n");
-		return ENOMEM;
-	}
-	fibril_add_ready(fid);
-	usb_log_debug("Hub fibril created.\n");
+	/*
+	 * The processing will require opened control pipe and connection
+	 * to the host controller.
+	 * It is waste of resources but let's hope there will be less
+	 * hubs than the phone limit.
+	 * FIXME: with some proper locking over pipes and session
+	 * auto destruction, this could work better.
+	 */
+	rc = usb_pipe_start_session(&usb_dev->ctrl_pipe);
+	if (rc != EOK) {
+		usb_log_error("Failed to start session on control pipe: %s.\n",
+		    str_error(rc));
+		goto leave;
+	}
+	rc = usb_hc_connection_open(&hub_info->connection);
+	if (rc != EOK) {
+		usb_pipe_end_session(&usb_dev->ctrl_pipe);
+		usb_log_error("Failed to open connection to HC: %s.\n",
+		    str_error(rc));
+		goto leave;
+	}
+
+	rc = usb_device_auto_poll(hub_info->usb_device, 0,
+	    hub_port_changes_callback, ((hub_info->port_count+1) / 8) + 1,
+	    NULL, hub_info);
+	if (rc != EOK) {
+		usb_log_error("Failed to create polling fibril: %s.\n",
+		    str_error(rc));
+		free(hub_info);
+		return rc;
+	}
 
 	usb_log_info("Controlling hub `%s' (%d ports).\n",
 	    hub_info->usb_device->ddf_dev->name, hub_info->port_count);
 	return EOK;
+
+leave:
+	free(hub_info);
+
+	return rc;
 }
 
@@ -317,6 +317,6 @@
 			//set the status change bit, so it will be noticed in driver loop
 			if(usb_port_dev_connected(&status)){
-				usb_hub_set_enable_port_feature_request(&request, port,
-						USB_HUB_FEATURE_C_PORT_CONNECTION);
+				usb_hub_set_disable_port_feature_request(&request, port,
+						USB_HUB_FEATURE_PORT_CONNECTION);
 				opResult = usb_pipe_control_read(
 						hub->control_pipe,
@@ -326,8 +326,53 @@
 				if (opResult != EOK) {
 					usb_log_warning(
-							"could not set port change on port %d errno:%d\n",
+							"could not clear port connection on port %d errno:%d\n",
 							port, opResult);
 				}
+				usb_log_debug("cleared port connection\n");
+				usb_hub_set_enable_port_feature_request(&request, port,
+						USB_HUB_FEATURE_PORT_ENABLE);
+				opResult = usb_pipe_control_read(
+						hub->control_pipe,
+						&request, sizeof(usb_device_request_setup_packet_t),
+						&status, 4, &rcvd_size
+						);
+				if (opResult != EOK) {
+					usb_log_warning(
+							"could not set port enabled on port %d errno:%d\n",
+							port, opResult);
+				}
+				usb_log_debug("port set to enabled - should lead to connection change\n");
 			}
+		}
+	}
+	/// \TODO this is just a debug code
+	for(port=1;port<=descriptor->ports_count;++port){
+		bool is_non_removable =
+				((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
+		if(is_non_removable){
+			usb_log_debug("port %d is non-removable\n",port);
+			usb_port_status_t status;
+			size_t rcvd_size;
+			usb_device_request_setup_packet_t request;
+			//int opResult;
+			usb_hub_set_port_status_request(&request, port);
+			//endpoint 0
+			opResult = usb_pipe_control_read(
+					hub->control_pipe,
+					&request, sizeof(usb_device_request_setup_packet_t),
+					&status, 4, &rcvd_size
+					);
+			if (opResult != EOK) {
+				usb_log_error("could not get port status %d\n",opResult);
+			}
+			if (rcvd_size != sizeof (usb_port_status_t)) {
+				usb_log_error("received status has incorrect size\n");
+			}
+			//something connected/disconnected
+			if (usb_port_connect_change(&status)) {
+				usb_log_debug("some connection changed\n");
+			}
+			usb_log_debug("status: %s\n",usb_debug_str_buffer(
+					(uint8_t *)&status,4,4));
 		}
 	}
@@ -352,135 +397,4 @@
 	hub->is_default_address_used = false;
 	return EOK;
-}
-
-/**
- * Reset the port with new device and reserve the default address.
- * @param hub hub representation
- * @param port port number, starting from 1
- * @param speed transfer speed of attached device, one of low, full or high
- */
-static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
-		usb_speed_t speed) {
-	//if this hub already uses default address, it cannot request it once more
-	if(hub->is_default_address_used) return;
-	usb_log_debug("some connection changed\n");
-	assert(hub->control_pipe->hc_phone);
-	int opResult = usb_hub_clear_port_feature(hub->control_pipe,
-				port, USB_HUB_FEATURE_C_PORT_CONNECTION);
-	if(opResult != EOK){
-		usb_log_warning("could not clear port-change-connection flag\n");
-	}
-	usb_device_request_setup_packet_t request;
-	
-	//get default address
-	opResult = usb_hc_reserve_default_address(&hub->connection, speed);
-	
-	if (opResult != EOK) {
-		usb_log_warning("cannot assign default address, it is probably used %d\n",
-				opResult);
-		return;
-	}
-	hub->is_default_address_used = true;
-	//reset port
-	usb_hub_set_reset_port_request(&request, port);
-	opResult = usb_pipe_control_write(
-			hub->control_pipe,
-			&request,sizeof(usb_device_request_setup_packet_t),
-			NULL, 0
-			);
-	if (opResult != EOK) {
-		usb_log_error("something went wrong when reseting a port %d\n",opResult);
-		usb_hub_release_default_address(hub);
-	}
-	return;
-}
-
-/**
- * Finalize adding new device after port reset
- *
- * Set device`s address and start it`s driver.
- * @param hub hub representation
- * @param port port number, starting from 1
- * @param speed transfer speed of attached device, one of low, full or high
- */
-static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
-		uint16_t port, usb_speed_t speed) {
-
-	int opResult;
-	usb_log_debug("finalizing add device\n");
-	opResult = usb_hub_clear_port_feature(hub->control_pipe,
-	    port, USB_HUB_FEATURE_C_PORT_RESET);
-
-	if (opResult != EOK) {
-		usb_log_error("failed to clear port reset feature\n");
-		usb_hub_release_default_address(hub);
-		return;
-	}
-	//create connection to device
-	usb_pipe_t new_device_pipe;
-	usb_device_connection_t new_device_connection;
-	usb_device_connection_initialize_on_default_address(
-			&new_device_connection,
-			&hub->connection
-			);
-	usb_pipe_initialize_default_control(
-			&new_device_pipe,
-			&new_device_connection);
-	usb_pipe_probe_default_control(&new_device_pipe);
-
-	/* Request address from host controller. */
-	usb_address_t new_device_address = usb_hc_request_address(
-			&hub->connection,
-			speed
-			);
-	if (new_device_address < 0) {
-		usb_log_error("failed to get free USB address\n");
-		opResult = new_device_address;
-		usb_hub_release_default_address(hub);
-		return;
-	}
-	usb_log_debug("setting new address %d\n",new_device_address);
-	//opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
-	//    new_device_address);
-	usb_pipe_start_session(&new_device_pipe);
-	opResult = usb_request_set_address(&new_device_pipe,new_device_address);
-	usb_pipe_end_session(&new_device_pipe);
-	if (opResult != EOK) {
-		usb_log_error("could not set address for new device %d\n",opResult);
-		usb_hub_release_default_address(hub);
-		return;
-	}
-
-	//opResult = usb_hub_release_default_address(hc);
-	opResult = usb_hub_release_default_address(hub);
-	if(opResult!=EOK){
-		return;
-	}
-
-	devman_handle_t child_handle;
-	//??
-    opResult = usb_device_register_child_in_devman(new_device_address,
-            hub->connection.hc_handle, hub->usb_device->ddf_dev, &child_handle,
-            NULL, NULL, NULL);
-
-	if (opResult != EOK) {
-		usb_log_error("could not start driver for new device %d\n",opResult);
-		return;
-	}
-	hub->attached_devs[port].handle = child_handle;
-	hub->attached_devs[port].address = new_device_address;
-
-	//opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
-	opResult = usb_hc_register_device(
-			&hub->connection,
-			&hub->attached_devs[port]);
-	if (opResult != EOK) {
-		usb_log_error("could not assign address of device in hcd %d\n",opResult);
-		return;
-	}
-	usb_log_info("Detected new device on `%s' (port %d), " \
-	    "address %d (handle %llu).\n",
-	    hub->usb_device->ddf_dev->name, (int) port,
-	    new_device_address, child_handle);
 }
 
@@ -494,5 +408,5 @@
  * @param port port number, starting from 1
  */
-static void usb_hub_removed_device(
+void usb_hub_removed_device(
     usb_hub_info_t * hub,uint16_t port) {
 
@@ -507,5 +421,5 @@
 	
 	//close address
-	if(hub->attached_devs[port].address!=0){
+	if(hub->ports[port].attached_device.address >= 0){
 		/*uncomment this code to use it when DDF allows device removal
 		opResult = usb_hc_unregister_device(
@@ -534,5 +448,5 @@
  * @param port port number, starting from 1
  */
-static void usb_hub_over_current( usb_hub_info_t * hub,
+void usb_hub_over_current( usb_hub_info_t * hub,
 		uint16_t port){
 	int opResult;
@@ -545,150 +459,4 @@
 }
 
-/**
- * Process interrupts on given hub port
- *
- * Accepts connection, over current and port reset change.
- * @param hub hub representation
- * @param port port number, starting from 1
- */
-static void usb_hub_process_interrupt(usb_hub_info_t * hub, 
-        uint16_t port) {
-	usb_log_debug("interrupt at port %d\n", port);
-	//determine type of change
-	usb_pipe_t *pipe = hub->control_pipe;
-	
-	int opResult;
-
-	usb_port_status_t status;
-	size_t rcvd_size;
-	usb_device_request_setup_packet_t request;
-	//int opResult;
-	usb_hub_set_port_status_request(&request, port);
-	//endpoint 0
-
-	opResult = usb_pipe_control_read(
-			pipe,
-			&request, sizeof(usb_device_request_setup_packet_t),
-			&status, 4, &rcvd_size
-			);
-	if (opResult != EOK) {
-		usb_log_error("could not get port status\n");
-		return;
-	}
-	if (rcvd_size != sizeof (usb_port_status_t)) {
-		usb_log_error("received status has incorrect size\n");
-		return;
-	}
-	//something connected/disconnected
-	if (usb_port_connect_change(&status)) {
-		if (usb_port_dev_connected(&status)) {
-			usb_log_debug("some connection changed\n");
-			usb_hub_init_add_device(hub, port, usb_port_speed(&status));
-		} else {
-			usb_hub_removed_device(hub, port);
-		}
-	}
-	//over current
-	if (usb_port_overcurrent_change(&status)) {
-		//check if it was not auto-resolved
-		if(usb_port_over_current(&status)){
-			usb_hub_over_current(hub,port);
-		}else{
-			usb_log_debug("over current condition was auto-resolved on port %d\n",
-					port);
-		}
-	}
-	//port reset
-	if (usb_port_reset_completed(&status)) {
-		usb_log_debug("port reset complete\n");
-		if (usb_port_enabled(&status)) {
-			usb_hub_finalize_add_device(hub, port, usb_port_speed(&status));
-		} else {
-			usb_log_warning("port reset, but port still not enabled\n");
-		}
-	}
-
-	usb_port_set_connect_change(&status, false);
-	usb_port_set_reset(&status, false);
-	usb_port_set_reset_completed(&status, false);
-	usb_port_set_dev_connected(&status, false);
-	if (status>>16) {
-		usb_log_info("there was some unsupported change on port %d: %X\n",
-				port,status);
-
-	}
-}
-
-/**
- * check changes on hub
- *
- * Handles changes on each port with a status change.
- * @param hub_info hub representation
- * @return error code
- */
-int usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
-	int opResult;
-	opResult = usb_pipe_start_session(
-			hub_info->status_change_pipe);
-	if(opResult != EOK){
-		usb_log_error("could not initialize communication for hub; %d\n",
-				opResult);
-		return opResult;
-	}
-
-	size_t port_count = hub_info->port_count;
-
-	/// FIXME: count properly
-	size_t byte_length = ((port_count+1) / 8) + 1;
-		void *change_bitmap = malloc(byte_length);
-	size_t actual_size;
-
-	/*
-	 * Send the request.
-	 */
-	opResult = usb_pipe_read(
-			hub_info->status_change_pipe,
-			change_bitmap, byte_length, &actual_size
-			);
-
-	if (opResult != EOK) {
-		free(change_bitmap);
-		usb_log_warning("something went wrong while getting status of hub\n");
-		usb_pipe_end_session(hub_info->status_change_pipe);
-		return opResult;
-	}
-	unsigned int port;
-	opResult = usb_pipe_start_session(hub_info->control_pipe);
-	if(opResult!=EOK){
-		usb_log_error("could not start control pipe session %d\n", opResult);
-		usb_pipe_end_session(hub_info->status_change_pipe);
-		return opResult;
-	}
-	opResult = usb_hc_connection_open(&hub_info->connection);
-	if(opResult!=EOK){
-		usb_log_error("could not start host controller session %d\n",
-				opResult);
-		usb_pipe_end_session(hub_info->control_pipe);
-		usb_pipe_end_session(hub_info->status_change_pipe);
-		return opResult;
-	}
-
-	///todo, opresult check, pre obe konekce
-	for (port = 1; port < port_count+1; ++port) {
-		bool interrupt =
-				(((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
-		if (interrupt) {
-			usb_hub_process_interrupt(
-			        hub_info, port);
-		}
-	}
-	usb_hc_connection_close(&hub_info->connection);
-	usb_pipe_end_session(hub_info->control_pipe);
-	usb_pipe_end_session(hub_info->status_change_pipe);
-	free(change_bitmap);
-	return EOK;
-}
-
-
 
 /**
Index: uspace/drv/usbhub/usbhub.h
===================================================================
--- uspace/drv/usbhub/usbhub.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbhub/usbhub.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -47,12 +47,15 @@
 #include <usb/devdrv.h>
 
+#include "ports.h"
+
+
 
 /** Information about attached hub. */
 typedef struct {
 	/** Number of ports. */
-	int port_count;
+	size_t port_count;
 
-	/** attached device handles, for each port one */
-	usb_hc_attached_device_t * attached_devs;
+	/** Ports. */
+	usb_hub_port_t *ports;
 	
 	/** connection to hcd */
@@ -100,4 +103,6 @@
 int usb_hub_check_hub_changes(usb_hub_info_t * hub_info_param);
 
+void usb_hub_removed_device(usb_hub_info_t *, uint16_t);
+void usb_hub_over_current(usb_hub_info_t *, uint16_t);
 
 int usb_hub_add_device(usb_device_t * usb_dev);
Index: uspace/drv/usbhub/usbhub_private.h
===================================================================
--- uspace/drv/usbhub/usbhub_private.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbhub/usbhub_private.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -38,5 +38,4 @@
 
 #include "usbhub.h"
-#include "usblist.h"
 
 #include <adt/list.h>
Index: uspace/drv/usbhub/usblist.c
===================================================================
--- uspace/drv/usbhub/usblist.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ 	(revision )
@@ -1,80 +1,0 @@
-/*
- * Copyright (c) 2010 Matus Dekanek
- * 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 drvusbhub
- * @{
- */
-/** @file
- * @brief usblist implementation
- */
-#include <sys/types.h>
-
-#include "usbhub_private.h"
-
-
-usb_general_list_t * usb_lst_create(void) {
-	usb_general_list_t* result = usb_new(usb_general_list_t);
-	usb_lst_init(result);
-	return result;
-}
-
-void usb_lst_init(usb_general_list_t * lst) {
-	lst->prev = lst;
-	lst->next = lst;
-	lst->data = NULL;
-}
-
-void usb_lst_prepend(usb_general_list_t* item, void* data) {
-	usb_general_list_t* appended = usb_new(usb_general_list_t);
-	appended->data = data;
-	appended->next = item;
-	appended->prev = item->prev;
-	item->prev->next = appended;
-	item->prev = appended;
-}
-
-void usb_lst_append(usb_general_list_t* item, void* data) {
-	usb_general_list_t* appended = usb_new(usb_general_list_t);
-	appended->data = data;
-	appended->next = item->next;
-	appended->prev = item;
-	item->next->prev = appended;
-	item->next = appended;
-}
-
-void usb_lst_remove(usb_general_list_t* item) {
-	item->next->prev = item->prev;
-	item->prev->next = item->next;
-}
-
-
-
-/**
- * @}
- */
-
-
Index: uspace/drv/usbhub/usblist.h
===================================================================
--- uspace/drv/usbhub/usblist.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ 	(revision )
@@ -1,82 +1,0 @@
-/*
- * Copyright (c) 2010 Matus Dekanek
- * 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 drvusbhub
- * @{
- */
-/** @file
- * @brief HC driver and hub driver.
- *
- * My private list implementation; I did not like the original helenos list.
- * This one does not depend on the structure of stored data and has
- * much simpler and more straight-forward semantics.
- */
-#ifndef USBLIST_H
-#define	USBLIST_H
-
-/**
- * general list structure
- */
-typedef struct usb_general_list{
-	void * data;
-	struct usb_general_list * prev, * next;
-} usb_general_list_t;
-
-/** create head of usb general list */
-usb_general_list_t * usb_lst_create(void);
-
-/** initialize head of usb general list */
-void usb_lst_init(usb_general_list_t * lst);
-
-
-/** is the list empty? */
-static inline bool usb_lst_empty(usb_general_list_t * lst){
-	return lst?(lst->next==lst):true;
-}
-
-/** append data behind item */
-void usb_lst_append(usb_general_list_t * lst, void * data);
-
-/** prepend data beore item */
-void usb_lst_prepend(usb_general_list_t * lst, void * data);
-
-/** remove list item from list */
-void usb_lst_remove(usb_general_list_t * item);
-
-/** get data o specified type from list item */
-#define usb_lst_get_data(item, type)  (type *) (item->data)
-
-/** get usb_hub_info_t data from list item */
-static inline usb_hub_info_t * usb_hub_lst_get_data(usb_general_list_t * item) {
-	return usb_lst_get_data(item,usb_hub_info_t);
-}
-
-#endif	/* USBLIST_H */
-/**
- * @}
- */
Index: uspace/drv/usbmid/explore.c
===================================================================
--- uspace/drv/usbmid/explore.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbmid/explore.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -48,21 +48,33 @@
 };
 
-/** Find starting indexes of all interface descriptors in a configuration.
- *
- * @param config_descriptor Full configuration descriptor.
- * @param config_descriptor_size Size of @p config_descriptor in bytes.
- * @param interface_positions Array where to store indexes of interfaces.
- * @param interface_count Size of @p interface_positions array.
- * @return Number of found interfaces.
- * @retval (size_t)-1 Error occured.
- */
-static size_t find_interface_descriptors(uint8_t *config_descriptor,
-    size_t config_descriptor_size,
-    size_t *interface_positions, size_t interface_count)
+/** Tell whether given interface is already in the list.
+ *
+ * @param list List of usbmid_interface_t members to be searched.
+ * @param interface_no Interface number caller is looking for.
+ * @return Interface @p interface_no is already present in the list.
+ */
+static bool interface_in_list(link_t *list, int interface_no)
 {
-	if (interface_count == 0) {
-		return (size_t) -1;
-	}
-
+	link_t *l;
+	for (l = list->next; l != list; l = l->next) {
+		usbmid_interface_t *iface
+		    = list_get_instance(l, usbmid_interface_t, link);
+		if (iface->interface_no == interface_no) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
+/** Create list of interfaces from configuration descriptor.
+ *
+ * @param config_descriptor Configuration descriptor.
+ * @param config_descriptor_size Size of configuration descriptor in bytes.
+ * @param list List where to add the interfaces.
+ */
+static void create_interfaces(uint8_t *config_descriptor,
+    size_t config_descriptor_size, link_t *list)
+{
 	usb_dp_parser_data_t data = {
 		.data = config_descriptor,
@@ -75,30 +87,43 @@
 	};
 
-	uint8_t *interface = usb_dp_get_nested_descriptor(&parser, &data,
+	uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data,
 	    data.data);
-	if (interface == NULL) {
-		return (size_t) -1;
-	}
-	if (interface[1] != USB_DESCTYPE_INTERFACE) {
-		return (size_t) -1;
-	}
-
-	size_t found_interfaces = 0;
-	interface_positions[found_interfaces] = interface - config_descriptor;
-	found_interfaces++;
-
-	while (interface != NULL) {
-		interface = usb_dp_get_sibling_descriptor(&parser, &data,
-		    data.data, interface);
-		if ((interface != NULL)
-		    && (found_interfaces < interface_count)
-		    && (interface[1] == USB_DESCTYPE_INTERFACE)) {
-			interface_positions[found_interfaces]
-			    = interface - config_descriptor;
-			found_interfaces++;
-		}
-	}
-
-	return found_interfaces;
+	if (interface_ptr == NULL) {
+		return;
+	}
+
+	do {
+		if (interface_ptr[1] != USB_DESCTYPE_INTERFACE) {
+			goto next_descriptor;
+		}
+
+		usb_standard_interface_descriptor_t *interface
+		    = (usb_standard_interface_descriptor_t *) interface_ptr;
+
+		/* Skip alternate interfaces. */
+		if (!interface_in_list(list, interface->interface_number)) {
+			usbmid_interface_t *iface
+			    = malloc(sizeof(usbmid_interface_t));
+			if (iface == NULL) {
+				break;
+			}
+			link_initialize(&iface->link);
+			iface->fun = NULL;
+			iface->interface_no = interface->interface_number;
+			iface->interface = interface;
+
+			list_append(&iface->link, list);
+		}
+
+		/* TODO: add the alternatives and create match ids from them
+		 * as well.
+		 */
+
+next_descriptor:
+		interface_ptr = usb_dp_get_sibling_descriptor(&parser, &data,
+		    data.data, interface_ptr);
+
+	} while (interface_ptr != NULL);
+
 }
 
@@ -130,23 +155,4 @@
 	    (usb_standard_configuration_descriptor_t *) config_descriptor_raw;
 
-	size_t *interface_descriptors
-	    = malloc(sizeof(size_t) * config_descriptor->interface_count);
-	if (interface_descriptors == NULL) {
-		usb_log_error("Out of memory (wanted %zuB).\n",
-		    sizeof(size_t) * config_descriptor->interface_count);
-		free(config_descriptor_raw);
-		return false;
-	}
-	size_t interface_descriptors_count
-	    = find_interface_descriptors(
-	    config_descriptor_raw, config_descriptor_size,
-	    interface_descriptors, config_descriptor->interface_count);
-
-	if (interface_descriptors_count == (size_t) -1) {
-		usb_log_error("Problem parsing configuration descriptor.\n");
-		free(interface_descriptors);
-		return false;
-	}
-
 	/* Select the first configuration */
 	rc = usb_request_set_configuration(&dev->ctrl_pipe,
@@ -155,8 +161,6 @@
 		usb_log_error("Failed to set device configuration: %s.\n",
 		    str_error(rc));
-		free(interface_descriptors);
-		return false;
-	}
-
+		return false;
+	}
 
 	/* Create control function */
@@ -164,5 +168,4 @@
 	if (ctl_fun == NULL) {
 		usb_log_error("Failed to create control function.\n");
-		free(interface_descriptors);
 		return false;
 	}
@@ -174,21 +177,25 @@
 		usb_log_error("Failed to bind control function: %s.\n",
 		    str_error(rc));
-		free(interface_descriptors);
-		return false;
-	}
-
-	/* Spawn interface children */
-	size_t i;
-	for (i = 0; i < interface_descriptors_count; i++) {
-		usb_standard_interface_descriptor_t *interface
-		    = (usb_standard_interface_descriptor_t *)
-		    (config_descriptor_raw + interface_descriptors[i]);
-		usb_log_debug2("Interface descriptor at index %zu (type %d).\n",
-		    interface_descriptors[i], (int) interface->descriptor_type);
+		return false;
+	}
+
+	/* Create interface children. */
+	link_t interface_list;
+	list_initialize(&interface_list);
+	create_interfaces(config_descriptor_raw, config_descriptor_size,
+	    &interface_list);
+
+	link_t *link;
+	for (link = interface_list.next; link != &interface_list;
+	    link = link->next) {
+		usbmid_interface_t *iface = list_get_instance(link,
+		    usbmid_interface_t, link);
+
 		usb_log_info("Creating child for interface %d (%s).\n",
-		    (int) interface->interface_number,
-		    usb_str_class(interface->interface_class));
-		rc = usbmid_spawn_interface_child(dev, &dev->descriptors.device,
-		    interface);
+		    (int) iface->interface_no,
+		    usb_str_class(iface->interface->interface_class));
+
+		rc = usbmid_spawn_interface_child(dev, iface,
+		    &dev->descriptors.device, iface->interface);
 		if (rc != EOK) {
 			usb_log_error("Failed to create interface child: %s.\n",
Index: uspace/drv/usbmid/usbmid.c
===================================================================
--- uspace/drv/usbmid/usbmid.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbmid/usbmid.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -79,30 +79,9 @@
 };
 
-/** Create new interface for USB MID device.
- *
- * @param fun Backing generic DDF device function (representing interface).
- * @param iface_no Interface number.
- * @return New interface.
- * @retval NULL Error occured.
- */
-usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
-{
-	usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
-	if (iface == NULL) {
-		usb_log_error("Out of memory (wanted %zuB).\n",
-		    sizeof(usbmid_interface_t));
-		return NULL;
-	}
-
-	iface->fun = fun;
-	iface->interface_no = iface_no;
-
-	return iface;
-}
-
 
 /** Spawn new child device from one interface.
  *
  * @param parent Parent MID device.
+ * @param iface Interface information.
  * @param device_descriptor Device descriptor.
  * @param interface_descriptor Interface descriptor.
@@ -110,4 +89,5 @@
  */
 int usbmid_spawn_interface_child(usb_device_t *parent,
+    usbmid_interface_t *iface,
     const usb_standard_device_descriptor_t *device_descriptor,
     const usb_standard_interface_descriptor_t *interface_descriptor)
@@ -115,5 +95,4 @@
 	ddf_fun_t *child = NULL;
 	char *child_name = NULL;
-	usbmid_interface_t *child_as_interface = NULL;
 	int rc;
 
@@ -137,14 +116,7 @@
 	}
 
+	iface->fun = child;
 
-
-	child_as_interface = usbmid_interface_create(child,
-	    (int) interface_descriptor->interface_number);
-	if (child_as_interface == NULL) {
-		rc = ENOMEM;
-		goto error_leave;
-	}
-
-	child->driver_data = child_as_interface;
+	child->driver_data = iface;
 	child->ops = &child_device_ops;
 
@@ -172,7 +144,4 @@
 		free(child_name);
 	}
-	if (child_as_interface != NULL) {
-		free(child_as_interface);
-	}
 
 	return rc;
Index: uspace/drv/usbmid/usbmid.h
===================================================================
--- uspace/drv/usbmid/usbmid.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/drv/usbmid/usbmid.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -37,4 +37,5 @@
 #define USBMID_H_
 
+#include <adt/list.h>
 #include <ddf/driver.h>
 #include <usb/usb.h>
@@ -49,12 +50,14 @@
 	/** Function container. */
 	ddf_fun_t *fun;
-
+	/** Interface descriptor. */
+	usb_standard_interface_descriptor_t *interface;
 	/** Interface number. */
 	int interface_no;
+	/** List link. */
+	link_t link;
 } usbmid_interface_t;
 
-usbmid_interface_t *usbmid_interface_create(ddf_fun_t *, int);
 bool usbmid_explore_device(usb_device_t *);
-int usbmid_spawn_interface_child(usb_device_t *,
+int usbmid_spawn_interface_child(usb_device_t *, usbmid_interface_t *,
     const usb_standard_device_descriptor_t *,
     const usb_standard_interface_descriptor_t *);
Index: uspace/lib/c/Makefile
===================================================================
--- uspace/lib/c/Makefile	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/Makefile	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -77,4 +77,5 @@
 	generic/io/io.c \
 	generic/io/printf.c \
+	generic/io/log.c \
 	generic/io/klog.c \
 	generic/io/snprintf.c \
Index: uspace/lib/c/arch/abs32le/_link.ld.in
===================================================================
--- uspace/lib/c/arch/abs32le/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/abs32le/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -11,6 +11,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/amd64/_link.ld.in
===================================================================
--- uspace/lib/c/arch/amd64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/amd64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -16,6 +16,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/arm32/_link.ld.in
===================================================================
--- uspace/lib/c/arch/arm32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/arm32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -15,6 +15,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/ia32/_link.ld.in
===================================================================
--- uspace/lib/c/arch/ia32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/ia32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -16,6 +16,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/ia64/_link.ld.in
===================================================================
--- uspace/lib/c/arch/ia64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/ia64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -15,6 +15,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
@@ -23,5 +23,5 @@
 	.got : {
 		_gp = .;
-		*(.got*);
+		*(.got .got.*);
 	} :data
 	
Index: uspace/lib/c/arch/mips32/_link.ld.in
===================================================================
--- uspace/lib/c/arch/mips32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/mips32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -15,6 +15,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/ppc32/_link.ld.in
===================================================================
--- uspace/lib/c/arch/ppc32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/ppc32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -15,6 +15,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/arch/sparc64/_link.ld.in
===================================================================
--- uspace/lib/c/arch/sparc64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/arch/sparc64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -15,6 +15,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/lib/c/generic/adt/hash_table.c
===================================================================
--- uspace/lib/c/generic/adt/hash_table.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/generic/adt/hash_table.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -54,5 +54,5 @@
  *
  */
-int hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
+bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
     hash_table_operations_t *op)
 {
Index: uspace/lib/c/generic/devman.c
===================================================================
--- uspace/lib/c/generic/devman.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/generic/devman.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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/generic/io/log.c
===================================================================
--- uspace/lib/c/generic/io/log.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/c/generic/io/log.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * Copyright (c) 2011 Jiri Svoboda
+ * 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
+ * @{
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <fibril_synch.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <io/log.h>
+
+/** Serialization mutex for logging functions. */
+static FIBRIL_MUTEX_INITIALIZE(log_serializer);
+
+/** Current log level. */
+static log_level_t log_level;
+
+static FILE *log_stream;
+
+static const char *log_prog_name;
+
+/** Prefixes for individual logging levels. */
+static const char *log_level_names[] = {
+	[LVL_FATAL] = "Fatal error",
+	[LVL_ERROR] = "Error",
+	[LVL_WARN] = "Warning",
+	[LVL_NOTE] = "Note",
+	[LVL_DEBUG] = "Debug",
+	[LVL_DEBUG2] = "Debug2"
+};
+
+/** Initialize the logging system.
+ *
+ * @param prog_name	Program name, will be printed as part of message
+ * @param level		Minimum message level to print
+ */
+int log_init(const char *prog_name, log_level_t level)
+{
+	assert(level < LVL_LIMIT);
+	log_level = level;
+
+	log_stream = stdout;
+	log_prog_name = str_dup(prog_name);
+	if (log_prog_name == NULL)
+		return ENOMEM;
+
+	return EOK;
+}
+
+/** Write an entry to the log.
+ *
+ * @param level		Message verbosity level. Message is only printed
+ *			if verbosity is less than or equal to current
+ *			reporting level.
+ * @param fmt		Format string (no traling newline).
+ */
+void log_msg(log_level_t level, const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	log_msgv(level, fmt, args);
+	va_end(args);
+}
+
+/** Write an entry to the log (va_list variant).
+ *
+ * @param level		Message verbosity level. Message is only printed
+ *			if verbosity is less than or equal to current
+ *			reporting level.
+ * @param fmt		Format string (no trailing newline)
+ */
+void log_msgv(log_level_t level, const char *fmt, va_list args)
+{
+	assert(level < LVL_LIMIT);
+
+	/* Higher number means higher verbosity. */
+	if (level <= log_level) {
+		fibril_mutex_lock(&log_serializer);
+
+		fprintf(log_stream, "%s: %s: ", log_prog_name,
+		    log_level_names[level]);
+		vfprintf(log_stream, fmt, args);
+		fputc('\n', log_stream);
+		fflush(log_stream);
+
+		fibril_mutex_unlock(&log_serializer);
+	}
+}
+
+/** @}
+ */
Index: uspace/lib/c/generic/net/packet.c
===================================================================
--- uspace/lib/c/generic/net/packet.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/generic/net/packet.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -190,5 +190,5 @@
 		}
 	}
-	gpm_destroy(&pm_globals.packet_map);
+	gpm_destroy(&pm_globals.packet_map, free);
 	/* leave locked */
 }
Index: uspace/lib/c/generic/net/socket_client.c
===================================================================
--- uspace/lib/c/generic/net/socket_client.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/generic/net/socket_client.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -749,5 +749,5 @@
 	dyn_fifo_destroy(&socket->received);
 	dyn_fifo_destroy(&socket->accepted);
-	sockets_exclude(socket_get_sockets(), socket->socket_id);
+	sockets_exclude(socket_get_sockets(), socket->socket_id, free);
 }
 
Index: uspace/lib/c/generic/vfs/vfs.c
===================================================================
--- uspace/lib/c/generic/vfs/vfs.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/generic/vfs/vfs.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -756,8 +756,9 @@
 {
 	struct stat stat;
-	int rc;
-
-	rc = fstat(fildes, &stat);
-
+	
+	int rc = fstat(fildes, &stat);
+	if (rc != 0)
+		return rc;
+	
 	if (!stat.device)
 		return -1;
Index: uspace/lib/c/include/adt/generic_char_map.h
===================================================================
--- uspace/lib/c/include/adt/generic_char_map.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/adt/generic_char_map.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -47,4 +47,8 @@
 #define GENERIC_CHAR_MAP_MAGIC_VALUE	0x12345622
 
+/** Generic destructor function pointer. */
+#define DTOR_T(identifier) \
+	void (*identifier)(const void *)
+
 /** Character string to generic type map declaration.
  *  @param[in] name	Name of the map.
@@ -64,6 +68,6 @@
 	int name##_add(name##_t *, const uint8_t *, const size_t, type *); \
 	int name##_count(name##_t *); \
-	void name##_destroy(name##_t *); \
-	void name##_exclude(name##_t *, const uint8_t *, const size_t); \
+	void name##_destroy(name##_t *, DTOR_T()); \
+	void name##_exclude(name##_t *, const uint8_t *, const size_t, DTOR_T()); \
 	type *name##_find(name##_t *, const uint8_t *, const size_t); \
 	int name##_initialize(name##_t *); \
@@ -84,5 +88,4 @@
 	     type *value) \
 	{ \
-		int rc; \
 		int index; \
 		if (!name##_is_valid(map)) \
@@ -91,10 +94,5 @@
 		if (index < 0) \
 			return index; \
-		rc = char_map_add(&map->names, name, length, index); \
-		if (rc != EOK) { \
-			name##_items_exclude_index(&map->values, index); \
-			return rc; \
-		} \
-		return EOK; \
+		return char_map_add(&map->names, name, length, index); \
 	} \
 	\
@@ -105,14 +103,14 @@
 	} \
 	\
-	void name##_destroy(name##_t *map) \
+	void name##_destroy(name##_t *map, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map)) { \
 			char_map_destroy(&map->names); \
-			name##_items_destroy(&map->values); \
+			name##_items_destroy(&map->values, dtor); \
 		} \
 	} \
 	\
 	void name##_exclude(name##_t *map, const uint8_t *name, \
-	    const size_t length) \
+	    const size_t length, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map)) { \
@@ -121,5 +119,5 @@
 			if (index != CHAR_MAP_NULL) \
 				name##_items_exclude_index(&map->values, \
-				     index); \
+				     index, dtor); \
 		} \
 	} \
Index: uspace/lib/c/include/adt/generic_field.h
===================================================================
--- uspace/lib/c/include/adt/generic_field.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/adt/generic_field.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -46,4 +46,8 @@
 #define GENERIC_FIELD_MAGIC_VALUE		0x55667788
 
+/** Generic destructor function pointer. */
+#define DTOR_T(identifier) \
+	void (*identifier)(const void *)
+
 /** Generic type field declaration.
  *
@@ -63,6 +67,6 @@
 	int name##_add(name##_t *, type *); \
 	int name##_count(name##_t *); \
-	void name##_destroy(name##_t *); \
-	void name##_exclude_index(name##_t *, int); \
+	void name##_destroy(name##_t *, DTOR_T()); \
+	void name##_exclude_index(name##_t *, int, DTOR_T()); \
 	type **name##_get_field(name##_t *); \
 	type *name##_get_index(name##_t *, int); \
@@ -103,12 +107,14 @@
 	} \
 	\
-	void name##_destroy(name##_t *field) \
+	void name##_destroy(name##_t *field, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(field)) { \
 			int index; \
 			field->magic = 0; \
-			for (index = 0; index < field->next; index++) { \
-				if (field->items[index]) \
-					free(field->items[index]); \
+			if (dtor) { \
+				for (index = 0; index < field->next; index++) { \
+					if (field->items[index]) \
+						dtor(field->items[index]); \
+				} \
 			} \
 			free(field->items); \
@@ -116,9 +122,10 @@
 	} \
 	 \
-	void name##_exclude_index(name##_t *field, int index) \
+	void name##_exclude_index(name##_t *field, int index, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(field) && (index >= 0) && \
 		    (index < field->next) && (field->items[index])) { \
-			free(field->items[index]); \
+			if (dtor) \
+				dtor(field->items[index]); \
 			field->items[index] = NULL; \
 		} \
Index: uspace/lib/c/include/adt/hash_table.h
===================================================================
--- uspace/lib/c/include/adt/hash_table.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/adt/hash_table.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -38,4 +38,5 @@
 #include <adt/list.h>
 #include <unistd.h>
+#include <bool.h>
 
 typedef unsigned long hash_count_t;
@@ -83,5 +84,5 @@
     list_get_instance((item), type, member)
 
-extern int hash_table_create(hash_table_t *, hash_count_t, hash_count_t,
+extern bool hash_table_create(hash_table_t *, hash_count_t, hash_count_t,
     hash_table_operations_t *);
 extern void hash_table_insert(hash_table_t *, unsigned long [], link_t *);
Index: uspace/lib/c/include/adt/int_map.h
===================================================================
--- uspace/lib/c/include/adt/int_map.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/adt/int_map.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -49,4 +49,8 @@
 #define INT_MAP_ITEM_MAGIC_VALUE	0x55667788
 
+/** Generic destructor function pointer. */
+#define DTOR_T(identifier) \
+	void (*identifier)(const void *)
+
 /** Integer to generic type map declaration.
  *
@@ -72,9 +76,9 @@
 	\
 	int name##_add(name##_t *, int, type *); \
-	void name##_clear(name##_t *); \
+	void name##_clear(name##_t *, DTOR_T()); \
 	int name##_count(name##_t *); \
-	void name##_destroy(name##_t *); \
-	void name##_exclude(name##_t *, int); \
-	void name##_exclude_index(name##_t *, int); \
+	void name##_destroy(name##_t *, DTOR_T()); \
+	void name##_exclude(name##_t *, int, DTOR_T()); \
+	void name##_exclude_index(name##_t *, int, DTOR_T()); \
 	type *name##_find(name##_t *, int); \
 	int name##_update(name##_t *, int, int); \
@@ -82,5 +86,5 @@
 	int name##_initialize(name##_t *); \
 	int name##_is_valid(name##_t *); \
-	void name##_item_destroy(name##_item_t *); \
+	void name##_item_destroy(name##_item_t *, DTOR_T()); \
 	int name##_item_is_valid(name##_item_t *);
 
@@ -115,5 +119,5 @@
 	} \
 	\
-	void name##_clear(name##_t *map) \
+	void name##_clear(name##_t *map, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map)) { \
@@ -122,5 +126,5 @@
 				if (name##_item_is_valid(&map->items[index])) { \
 					name##_item_destroy( \
-					    &map->items[index]); \
+					    &map->items[index], dtor); \
 				} \
 			} \
@@ -135,5 +139,5 @@
 	} \
 	\
-	void name##_destroy(name##_t *map) \
+	void name##_destroy(name##_t *map, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map)) { \
@@ -143,5 +147,5 @@
 				if (name##_item_is_valid(&map->items[index])) { \
 					name##_item_destroy( \
-					    &map->items[index]); \
+					    &map->items[index], dtor); \
 				} \
 			} \
@@ -150,5 +154,5 @@
 	} \
 	\
-	void name##_exclude(name##_t *map, int key) \
+	void name##_exclude(name##_t *map, int key, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map)) { \
@@ -158,16 +162,16 @@
 				    (map->items[index].key == key)) { \
 					name##_item_destroy( \
-					    &map->items[index]); \
-				} \
-			} \
-		} \
-	} \
-	\
-	void name##_exclude_index(name##_t *map, int index) \
+					    &map->items[index], dtor); \
+				} \
+			} \
+		} \
+	} \
+	\
+	void name##_exclude_index(name##_t *map, int index, DTOR_T(dtor)) \
 	{ \
 		if (name##_is_valid(map) && (index >= 0) && \
 		    (index < map->next) && \
 		    name##_item_is_valid(&map->items[index])) { \
-			name##_item_destroy(&map->items[index]); \
+			name##_item_destroy(&map->items[index], dtor); \
 		} \
 	} \
@@ -236,10 +240,11 @@
 	} \
 	\
-	void name##_item_destroy(name##_item_t *item) \
+	void name##_item_destroy(name##_item_t *item, DTOR_T(dtor)) \
 	{ \
 		if (name##_item_is_valid(item)) { \
 			item->magic = 0; \
 			if (item->value) { \
-				free(item->value); \
+				if (dtor) \
+					dtor(item->value); \
 				item->value = NULL; \
 			} \
Index: uspace/lib/c/include/devman.h
===================================================================
--- uspace/lib/c/include/devman.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/devman.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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/io/log.h
===================================================================
--- uspace/lib/c/include/io/log.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/c/include/io/log.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * Copyright (c) 2011 Jiri Svoboda
+ * 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
+ * @{
+ */
+
+#ifndef LIBC_IO_LOG_H_
+#define LIBC_IO_LOG_H_
+
+#include <stdarg.h>
+
+typedef enum {
+	LVL_FATAL,
+	LVL_ERROR,
+	LVL_WARN,
+	LVL_NOTE,
+	LVL_DEBUG,
+	LVL_DEBUG2,
+
+	/** For checking range of values */
+	LVL_LIMIT
+} log_level_t;
+
+extern int log_init(const char *, log_level_t);
+extern void log_msg(log_level_t, const char *, ...);
+extern void log_msgv(log_level_t, const char *, va_list);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/ipc/devman.h
===================================================================
--- uspace/lib/c/include/ipc/devman.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/c/include/ipc/devman.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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/lib/drv/Makefile
===================================================================
--- uspace/lib/drv/Makefile	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/drv/Makefile	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -36,4 +36,5 @@
 	generic/dev_iface.c \
 	generic/remote_char_dev.c \
+	generic/log.c \
 	generic/remote_hw_res.c \
 	generic/remote_usb.c \
Index: uspace/lib/drv/generic/driver.c
===================================================================
--- uspace/lib/drv/generic/driver.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/drv/generic/driver.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -273,12 +273,6 @@
 	
 	res = driver->driver_ops->add_device(dev);
-	if (res == EOK) {
-		printf("%s: new device with handle=%" PRIun " was added.\n",
-		    driver->name, dev_handle);
-	} else {
-		printf("%s: failed to add a new device with handle = %" PRIun ".\n",
-		    driver->name, dev_handle);
+	if (res != EOK)
 		delete_device(dev);
-	}
 	
 	async_answer_0(iid, res);
Index: uspace/lib/drv/generic/log.c
===================================================================
--- uspace/lib/drv/generic/log.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/drv/generic/log.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2011 Jiri Svoboda
+ * 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 libdrv
+ * @{
+ */
+
+#include <io/log.h>
+#include <stdarg.h>
+
+#include <ddf/log.h>
+
+/** Initialize the logging system.
+ *
+ * @param drv_name	Driver name, will be printed as part of message
+ * @param level		Minimum message level to print
+ */
+int ddf_log_init(const char *drv_name, log_level_t level)
+{
+	return log_init(drv_name, level);
+}
+
+/** Log a driver message.
+ *
+ * @param level		Message verbosity level. Message is only printed
+ *			if verbosity is less than or equal to current
+ *			reporting level.
+ * @param fmt		Format string (no trailing newline)
+ */
+void ddf_msg(log_level_t level, const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	log_msgv(level, fmt, args);
+	va_end(args);
+}
+
+/** @}
+ */
Index: uspace/lib/drv/include/ddf/log.h
===================================================================
--- uspace/lib/drv/include/ddf/log.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/drv/include/ddf/log.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2011 Jiri Svoboda
+ * 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 libdrv
+ * @{
+ */
+
+#ifndef DDF_LOG_H_
+#define DDF_LOG_H_
+
+#include <io/log.h>
+
+extern int ddf_log_init(const char *, log_level_t);
+extern void ddf_msg(log_level_t, const char *, ...);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/net/tl/socket_core.c
===================================================================
--- uspace/lib/net/tl/socket_core.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/net/tl/socket_core.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -107,5 +107,5 @@
 		socket_release(socket);
 
-	socket_cores_exclude(local_sockets, socket->socket_id);
+	socket_cores_exclude(local_sockets, socket->socket_id, free);
 }
 
@@ -230,5 +230,5 @@
 
 fail:
-	socket_port_map_destroy(&socket_port->map);
+	socket_port_map_destroy(&socket_port->map, free);
 	free(socket_port);
 	return rc;
@@ -649,12 +649,12 @@
 			if (socket_port->count <= 0) {
 				// destroy the map
-				socket_port_map_destroy(&socket_port->map);
+				socket_port_map_destroy(&socket_port->map, free);
 				// release the port
 				socket_ports_exclude(global_sockets,
-				    socket->port);
+				    socket->port, free);
 			} else {
 				// remove
 				socket_port_map_exclude(&socket_port->map,
-				    socket->key, socket->key_length);
+				    socket->key, socket->key_length, free);
 			}
 		}
Index: uspace/lib/net/tl/tl_common.c
===================================================================
--- uspace/lib/net/tl/tl_common.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/net/tl/tl_common.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -182,5 +182,5 @@
 			else
 				packet_dimensions_exclude(packet_dimensions,
-				    DEVICE_INVALID_ID);
+				    DEVICE_INVALID_ID, free);
 		}
 	}
Index: uspace/lib/usb/Makefile
===================================================================
--- uspace/lib/usb/Makefile	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/Makefile	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -53,5 +53,7 @@
 	src/hidreport.c \
 	src/host/device_keeper.c \
-	src/host/batch.c
+	src/host/batch.c \
+	src/host/endpoint.c \
+	src/host/usb_endpoint_manager.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/usb/include/usb/devdrv.h
===================================================================
--- uspace/lib/usb/include/usb/devdrv.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/include/usb/devdrv.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -47,4 +47,27 @@
 } usb_device_descriptors_t;
 
+/** Wrapper for data related to alternate interface setting.
+ * The pointers will typically point inside configuration descriptor and
+ * thus you shall not deallocate them.
+ */
+typedef struct {
+	/** Interface descriptor. */
+	usb_standard_interface_descriptor_t *interface;
+	/** Pointer to start of descriptor tree bound with this interface. */
+	uint8_t *nested_descriptors;
+	/** Size of data pointed by nested_descriptors in bytes. */
+	size_t nested_descriptors_size;
+} usb_alternate_interface_descriptors_t;
+
+/** Alternate interface settings. */
+typedef struct {
+	/** Array of alternate interfaces descriptions. */
+	usb_alternate_interface_descriptors_t *alternatives;
+	/** Size of @c alternatives array. */
+	size_t alternative_count;
+	/** Index of currently selected one. */
+	size_t current;
+} usb_alternate_interfaces_t;
+
 /** USB device structure. */
 typedef struct {
@@ -56,4 +79,6 @@
 	 */
 	usb_endpoint_mapping_t *pipes;
+	/** Number of other endpoint pipes. */
+	size_t pipes_count;
 	/** Current interface.
 	 * Usually, drivers operate on single interface only.
@@ -61,4 +86,10 @@
 	 */
 	int interface_no;
+
+	/** Alternative interfaces.
+	 * Set to NULL when the driver controls whole device
+	 * (i.e. more (or any) interfaces).
+	 */
+	usb_alternate_interfaces_t *alternate_interfaces;
 
 	/** Some useful descriptors. */
@@ -92,9 +123,32 @@
 	 */
 	const char *name;
-	/** Expected endpoints description, excluding default control endpoint.
+	/** Expected endpoints description.
+	 * This description shall exclude default control endpoint (pipe zero)
+	 * and must be NULL terminated.
+	 * When only control endpoint is expected, you may set NULL directly
+	 * without creating one item array containing NULL.
 	 *
-	 * It MUST be of size expected_enpoints_count(excluding default ctrl) + 1
-	 * where the last record MUST BE NULL, otherwise catastrophic things may
-	 * happen.
+	 * When the driver expect single interrupt in endpoint,
+	 * the initialization may look like this:
+\code
+static usb_endpoint_description_t poll_endpoint_description = {
+	.transfer_type = USB_TRANSFER_INTERRUPT,
+	.direction = USB_DIRECTION_IN,
+	.interface_class = USB_CLASS_HUB,
+	.interface_subclass = 0,
+	.interface_protocol = 0,
+	.flags = 0
+};
+
+static usb_endpoint_description_t *hub_endpoints[] = {
+	&poll_endpoint_description,
+	NULL
+};
+
+static usb_driver_t hub_driver = {
+	.endpoints = hub_endpoints,
+	...
+};
+\endcode
 	 */
 	usb_endpoint_description_t **endpoints;
@@ -105,8 +159,10 @@
 int usb_driver_main(usb_driver_t *);
 
+int usb_device_select_interface(usb_device_t *, uint8_t,
+    usb_endpoint_description_t **);
+
 typedef bool (*usb_polling_callback_t)(usb_device_t *,
     uint8_t *, size_t, void *);
 typedef void (*usb_polling_terminted_callback_t)(usb_device_t *, bool, void *);
-
 
 int usb_device_auto_poll(usb_device_t *, size_t,
Index: uspace/lib/usb/include/usb/host/batch.h
===================================================================
--- uspace/lib/usb/include/usb/host/batch.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/include/usb/host/batch.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -39,4 +39,5 @@
 #include <usbhc_iface.h>
 #include <usb/usb.h>
+#include <usb/host/endpoint.h>
 
 typedef struct usb_transfer_batch usb_transfer_batch_t;
@@ -60,4 +61,5 @@
 	ddf_fun_t *fun;
 	void *arg;
+	endpoint_t *ep;
 	void *private_data;
 };
@@ -78,4 +80,5 @@
     void *arg,
     ddf_fun_t *fun,
+		endpoint_t *ep,
     void *private_data
 );
Index: uspace/lib/usb/include/usb/host/device_keeper.h
===================================================================
--- uspace/lib/usb/include/usb/host/device_keeper.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/include/usb/host/device_keeper.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -40,7 +40,10 @@
 #ifndef LIBUSB_HOST_DEVICE_KEEPER_H
 #define LIBUSB_HOST_DEVICE_KEEPER_H
+
+#include <adt/list.h>
 #include <devman.h>
 #include <fibril_synch.h>
 #include <usb/usb.h>
+#include <usb/host/endpoint.h>
 
 /** Number of USB address for array dimensions. */
@@ -51,6 +54,6 @@
 	usb_speed_t speed;
 	bool occupied;
-	bool control_used;
-	uint16_t toggle_status[2];
+	link_t endpoints;
+	uint16_t control_used;
 	devman_handle_t handle;
 };
@@ -68,18 +71,14 @@
 void usb_device_keeper_init(usb_device_keeper_t *instance);
 
-void usb_device_keeper_reserve_default_address(usb_device_keeper_t *instance,
-    usb_speed_t speed);
+void usb_device_keeper_add_ep(
+    usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep);
+
+void usb_device_keeper_reserve_default_address(
+    usb_device_keeper_t *instance, usb_speed_t speed);
 
 void usb_device_keeper_release_default_address(usb_device_keeper_t *instance);
 
 void usb_device_keeper_reset_if_need(usb_device_keeper_t *instance,
-    usb_target_t target,
-    const uint8_t *setup_data);
-
-int usb_device_keeper_get_toggle(usb_device_keeper_t *instance,
-    usb_target_t target, usb_direction_t direction);
-
-int usb_device_keeper_set_toggle(usb_device_keeper_t *instance,
-    usb_target_t target, usb_direction_t direction, bool toggle);
+    usb_target_t target, const uint8_t *setup_data);
 
 usb_address_t device_keeper_get_free_address(usb_device_keeper_t *instance,
@@ -99,8 +98,8 @@
 
 void usb_device_keeper_use_control(usb_device_keeper_t *instance,
-    usb_address_t address);
+    usb_target_t target);
 
 void usb_device_keeper_release_control(usb_device_keeper_t *instance,
-    usb_address_t address);
+    usb_target_t target);
 
 #endif
Index: uspace/lib/usb/include/usb/host/endpoint.h
===================================================================
--- uspace/lib/usb/include/usb/host/endpoint.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/usb/include/usb/host/endpoint.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 libusb
+ * @{
+ */
+/** @file
+ *
+ */
+#ifndef LIBUSB_HOST_ENDPOINT_H
+#define LIBUSB_HOST_ENDPOINT_H
+
+#include <assert.h>
+#include <bool.h>
+#include <adt/list.h>
+#include <usb/usb.h>
+
+typedef struct endpoint {
+	usb_address_t address;
+	usb_endpoint_t endpoint;
+	usb_direction_t direction;
+	usb_transfer_type_t transfer_type;
+	usb_speed_t speed;
+	size_t max_packet_size;
+	bool active;
+	unsigned toggle:1;
+	link_t same_device_eps;
+} endpoint_t;
+
+int endpoint_init(endpoint_t *instance, usb_address_t address,
+    usb_endpoint_t endpoint, usb_direction_t direction,
+    usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size);
+
+void endpoint_destroy(endpoint_t *instance);
+
+int endpoint_toggle_get(endpoint_t *instance);
+
+void endpoint_toggle_set(endpoint_t *instance, int toggle);
+
+void endpoint_toggle_reset(link_t *ep);
+
+void endpoint_toggle_reset_filtered(link_t *ep, usb_endpoint_t epn);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usb/include/usb/host/usb_endpoint_manager.h
===================================================================
--- uspace/lib/usb/include/usb/host/usb_endpoint_manager.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/usb/include/usb/host/usb_endpoint_manager.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 libusb
+ * @{
+ */
+/** @file
+ * Device keeper structure and functions.
+ *
+ * Typical USB host controller needs to keep track of various settings for
+ * each device that is connected to it.
+ * State of toggle bit, device speed etc. etc.
+ * This structure shall simplify the management.
+ */
+#ifndef LIBUSB_HOST_USB_ENDPOINT_MANAGER_H
+#define LIBUSB_HOST_YSB_ENDPOINT_MANAGER_H
+
+#include <adt/hash_table.h>
+#include <fibril_synch.h>
+#include <usb/usb.h>
+#include <usb/host/endpoint.h>
+
+#define BANDWIDTH_TOTAL_USB11 12000000
+#define BANDWIDTH_AVAILABLE_USB11 ((BANDWIDTH_TOTAL_USB11 / 10) * 9)
+
+typedef struct usb_endpoint_manager {
+	hash_table_t ep_table;
+	fibril_mutex_t guard;
+	fibril_condvar_t change;
+	size_t free_bw;
+} usb_endpoint_manager_t;
+
+size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
+    size_t size, size_t max_packet_size);
+
+int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
+    size_t available_bandwidth);
+
+void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance);
+
+int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
+    endpoint_t *ep, size_t data_size);
+
+int usb_endpoint_manager_register_ep_wait(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_endpoint_t ep, usb_direction_t direction,
+    void *data, void (*data_remove_callback)(void* data, void* arg), void *arg,
+    size_t bw);
+
+int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_endpoint_t ep, usb_direction_t direction);
+
+endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_endpoint_t ep, usb_direction_t direction,
+    size_t *bw);
+
+#endif
+/**
+ * @}
+ */
+
Index: uspace/lib/usb/include/usb/pipes.h
===================================================================
--- uspace/lib/usb/include/usb/pipes.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/include/usb/pipes.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -107,4 +107,6 @@
 	/** Interface number the endpoint must belong to (-1 for any). */
 	int interface_no;
+	/** Alternate interface setting to choose. */
+	int interface_setting;
 	/** Found descriptor fitting the description. */
 	usb_standard_endpoint_descriptor_t *descriptor;
Index: uspace/lib/usb/src/devdrv.c
===================================================================
--- uspace/lib/usb/src/devdrv.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/devdrv.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -36,4 +36,5 @@
 #include <usb/request.h>
 #include <usb/debug.h>
+#include <usb/dp.h>
 #include <errno.h>
 #include <str_error.h>
@@ -86,12 +87,12 @@
  * @return Number of pipes (excluding default control pipe).
  */
-static size_t count_other_pipes(usb_driver_t *drv)
+static size_t count_other_pipes(usb_endpoint_description_t **endpoints)
 {
 	size_t count = 0;
-	if (drv->endpoints == NULL) {
+	if (endpoints == NULL) {
 		return 0;
 	}
 
-	while (drv->endpoints[count] != NULL) {
+	while (endpoints[count] != NULL) {
 		count++;
 	}
@@ -106,10 +107,14 @@
  * @return Error code.
  */
-static int initialize_other_pipes(usb_driver_t *drv, usb_device_t *dev)
+static int initialize_other_pipes(usb_endpoint_description_t **endpoints,
+    usb_device_t *dev)
 {
 	int rc;
-	dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
-
-	size_t pipe_count = count_other_pipes(drv);
+
+	size_t pipe_count = count_other_pipes(endpoints);
+	if (pipe_count == 0) {
+		return EOK;
+	}
+
 	dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
 	if (dev->pipes == NULL) {
@@ -133,6 +138,7 @@
 		}
 
-		dev->pipes[i].description = drv->endpoints[i];
+		dev->pipes[i].description = endpoints[i];
 		dev->pipes[i].interface_no = dev->interface_no;
+		dev->pipes[i].interface_setting = 0;
 	}
 
@@ -178,4 +184,6 @@
 	usb_hc_connection_close(&hc_conn);
 
+	dev->pipes_count = pipe_count;
+
 	return EOK;
 
@@ -227,4 +235,7 @@
 	}
 
+	/* Get our interface. */
+	dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
+
 	/*
 	 * For further actions, we need open session on default control pipe.
@@ -251,5 +262,5 @@
 	    &dev->descriptors.configuration_size);
 	if (rc != EOK) {
-		usb_log_error("Failed retrieving configuration descriptor: %s.\n",
+		usb_log_error("Failed retrieving configuration descriptor: %s. %s\n",
 		    dev->ddf_dev->name, str_error(rc));
 		return rc;
@@ -257,5 +268,5 @@
 
 	if (driver->endpoints != NULL) {
-		rc = initialize_other_pipes(driver, dev);
+		rc = initialize_other_pipes(driver->endpoints, dev);
 	}
 
@@ -271,4 +282,128 @@
 
 	return rc;
+}
+
+/** Count number of alternate settings of a interface.
+ *
+ * @param config_descr Full configuration descriptor.
+ * @param config_descr_size Size of @p config_descr in bytes.
+ * @param interface_no Interface number.
+ * @return Number of alternate interfaces for @p interface_no interface.
+ */
+static size_t count_alternate_interfaces(uint8_t *config_descr,
+    size_t config_descr_size, int interface_no)
+{
+	assert(config_descr != NULL);
+	usb_dp_parser_t dp_parser = {
+		.nesting = usb_dp_standard_descriptor_nesting
+	};
+	usb_dp_parser_data_t dp_data = {
+		.data = config_descr,
+		.size = config_descr_size,
+		.arg = NULL
+	};
+
+	size_t alternate_count = 0;
+
+	uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
+	    &dp_data, config_descr);
+	while (iface_ptr != NULL) {
+		usb_standard_interface_descriptor_t *iface
+		    = (usb_standard_interface_descriptor_t *) iface_ptr;
+		if (iface->descriptor_type == USB_DESCTYPE_INTERFACE) {
+			if (iface->interface_number == interface_no) {
+				alternate_count++;
+			}
+		}
+		iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
+		    config_descr, iface_ptr);
+	}
+
+	return alternate_count;
+}
+
+/** Initialize structures related to alternate interfaces.
+ *
+ * @param dev Device where alternate settings shall be initialized.
+ * @return Error code.
+ */
+static int initialize_alternate_interfaces(usb_device_t *dev)
+{
+	if (dev->interface_no < 0) {
+		dev->alternate_interfaces = NULL;
+		return EOK;
+	}
+
+	usb_alternate_interfaces_t *alternates
+	    = malloc(sizeof(usb_alternate_interfaces_t));
+
+	if (alternates == NULL) {
+		return ENOMEM;
+	}
+
+	alternates->alternative_count
+	    = count_alternate_interfaces(dev->descriptors.configuration,
+	    dev->descriptors.configuration_size, dev->interface_no);
+
+	if (alternates->alternative_count == 0) {
+		free(alternates);
+		return ENOENT;
+	}
+
+	alternates->alternatives = malloc(alternates->alternative_count
+	    * sizeof(usb_alternate_interface_descriptors_t));
+	if (alternates->alternatives == NULL) {
+		free(alternates);
+		return ENOMEM;
+	}
+
+	alternates->current = 0;
+
+	usb_dp_parser_t dp_parser = {
+		.nesting = usb_dp_standard_descriptor_nesting
+	};
+	usb_dp_parser_data_t dp_data = {
+		.data = dev->descriptors.configuration,
+		.size = dev->descriptors.configuration_size,
+		.arg = NULL
+	};
+
+	usb_alternate_interface_descriptors_t *cur_alt_iface
+	    = &alternates->alternatives[0];
+
+	uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
+	    &dp_data, dp_data.data);
+	while (iface_ptr != NULL) {
+		usb_standard_interface_descriptor_t *iface
+		    = (usb_standard_interface_descriptor_t *) iface_ptr;
+		if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
+		    || (iface->interface_number != dev->interface_no)) {
+			iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
+			    &dp_data,
+			    dp_data.data, iface_ptr);
+			continue;
+		}
+
+		cur_alt_iface->interface = iface;
+		cur_alt_iface->nested_descriptors = iface_ptr + sizeof(*iface);
+
+		/* Find next interface to count size of nested descriptors. */
+		iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
+		    dp_data.data, iface_ptr);
+		if (iface_ptr == NULL) {
+			uint8_t *next = dp_data.data + dp_data.size;
+			cur_alt_iface->nested_descriptors_size
+			    = next - cur_alt_iface->nested_descriptors;
+		} else {
+			cur_alt_iface->nested_descriptors_size
+			    = iface_ptr - cur_alt_iface->nested_descriptors;
+		}
+
+		cur_alt_iface++;
+	}
+
+	dev->alternate_interfaces = alternates;
+
+	return EOK;
 }
 
@@ -301,4 +436,7 @@
 	dev->descriptors.configuration = NULL;
 
+	dev->pipes_count = 0;
+	dev->pipes = NULL;
+
 	rc = initialize_pipes(dev);
 	if (rc != EOK) {
@@ -307,5 +445,97 @@
 	}
 
+	(void) initialize_alternate_interfaces(dev);
+
 	return driver->ops->add_device(dev);
+}
+
+/** Destroy existing pipes of a USB device.
+ *
+ * @param dev Device where to destroy the pipes.
+ * @return Error code.
+ */
+static int destroy_current_pipes(usb_device_t *dev)
+{
+	size_t i;
+	int rc;
+
+	/* TODO: this shall be done under some device mutex. */
+
+	/* First check that no session is opened. */
+	for (i = 0; i < dev->pipes_count; i++) {
+		if (usb_pipe_is_session_started(dev->pipes[i].pipe)) {
+			return EBUSY;
+		}
+	}
+
+	/* Prepare connection to HC. */
+	usb_hc_connection_t hc_conn;
+	rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev);
+	if (rc != EOK) {
+		return rc;
+	}
+	rc = usb_hc_connection_open(&hc_conn);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	/* Destroy the pipes. */
+	for (i = 0; i < dev->pipes_count; i++) {
+		usb_pipe_unregister(dev->pipes[i].pipe, &hc_conn);
+		free(dev->pipes[i].pipe);
+	}
+
+	usb_hc_connection_close(&hc_conn);
+
+	free(dev->pipes);
+	dev->pipes = NULL;
+	dev->pipes_count = 0;
+
+	return EOK;
+}
+
+/** Change interface setting of a device.
+ * This function selects new alternate setting of an interface by issuing
+ * proper USB command to the device and also creates new USB pipes
+ * under @c dev->pipes.
+ *
+ * @warning This function is intended for drivers working at interface level.
+ * For drivers controlling the whole device, you need to change interface
+ * manually using usb_request_set_interface() and creating new pipes
+ * with usb_pipe_initialize_from_configuration().
+ *
+ * @param dev USB device.
+ * @param alternate_setting Alternate setting to choose.
+ * @param endpoints New endpoint descriptions.
+ * @return Error code.
+ */
+int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
+    usb_endpoint_description_t **endpoints)
+{
+	if (dev->interface_no < 0) {
+		return EINVAL;
+	}
+
+	int rc;
+
+	/* TODO: more transactional behavior. */
+
+	/* Destroy existing pipes. */
+	rc = destroy_current_pipes(dev);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	/* Change the interface itself. */
+	rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
+	    alternate_setting);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	/* Create new pipes. */
+	rc = initialize_other_pipes(endpoints, dev);
+
+	return rc;
 }
 
Index: uspace/lib/usb/src/host/batch.c
===================================================================
--- uspace/lib/usb/src/host/batch.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/host/batch.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -54,4 +54,5 @@
     void *arg,
     ddf_fun_t *fun,
+		endpoint_t *ep,
     void *private_data
     )
@@ -77,5 +78,5 @@
 	instance->next_step = NULL;
 	instance->error = EOK;
-
+	instance->ep = ep;
 }
 /*----------------------------------------------------------------------------*/
Index: uspace/lib/usb/src/host/device_keeper.c
===================================================================
--- uspace/lib/usb/src/host/device_keeper.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/host/device_keeper.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -54,9 +54,18 @@
 	for (; i < USB_ADDRESS_COUNT; ++i) {
 		instance->devices[i].occupied = false;
-		instance->devices[i].control_used = false;
+		instance->devices[i].control_used = 0;
 		instance->devices[i].handle = 0;
-		instance->devices[i].toggle_status[0] = 0;
-		instance->devices[i].toggle_status[1] = 0;
-	}
+		list_initialize(&instance->devices[i].endpoints);
+	}
+}
+/*----------------------------------------------------------------------------*/
+void usb_device_keeper_add_ep(
+    usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep)
+{
+	assert(instance);
+	fibril_mutex_lock(&instance->guard);
+	assert(instance->devices[address].occupied);
+	list_append(&ep->same_device_eps, &instance->devices[address].endpoints);
+	fibril_mutex_unlock(&instance->guard);
 }
 /*----------------------------------------------------------------------------*/
@@ -66,6 +75,6 @@
  * @param[in] speed Speed of the device requesting default address.
  */
-void usb_device_keeper_reserve_default_address(usb_device_keeper_t *instance,
-    usb_speed_t speed)
+void usb_device_keeper_reserve_default_address(
+    usb_device_keeper_t *instance, usb_speed_t speed)
 {
 	assert(instance);
@@ -101,6 +110,6 @@
  * Really ugly one.
  */
-void usb_device_keeper_reset_if_need(usb_device_keeper_t *instance,
-    usb_target_t target, const uint8_t *data)
+void usb_device_keeper_reset_if_need(
+    usb_device_keeper_t *instance, usb_target_t target, const uint8_t *data)
 {
 	assert(instance);
@@ -119,9 +128,14 @@
 		/* recipient is endpoint, value is zero (ENDPOINT_STALL) */
 		if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
+			link_t *current =
+			    instance->devices[target.address].endpoints.next;
+			while (current !=
+			   &instance->devices[target.address].endpoints)
+			{
 			/* endpoint number is < 16, thus first byte is enough */
-			instance->devices[target.address].toggle_status[0] &=
-			    ~(1 << data[4]);
-			instance->devices[target.address].toggle_status[1] &=
-			    ~(1 << data[4]);
+				endpoint_toggle_reset_filtered(
+				    current, data[4]);
+				current = current->next;
+			}
 		}
 	break;
@@ -131,73 +145,16 @@
 		/* target must be device */
 		if ((data[0] & 0xf) == 0) {
-			instance->devices[target.address].toggle_status[0] = 0;
-			instance->devices[target.address].toggle_status[1] = 0;
+			link_t *current =
+			    instance->devices[target.address].endpoints.next;
+			while (current !=
+			   &instance->devices[target.address].endpoints)
+			{
+				endpoint_toggle_reset(current);
+				current = current->next;
+			}
 		}
 	break;
 	}
 	fibril_mutex_unlock(&instance->guard);
-}
-/*----------------------------------------------------------------------------*/
-/** Get current value of endpoint toggle.
- *
- * @param[in] instance Device keeper structure to use.
- * @param[in] target Device and endpoint used.
- * @return Error code
- */
-int usb_device_keeper_get_toggle(usb_device_keeper_t *instance,
-    usb_target_t target, usb_direction_t direction)
-{
-	assert(instance);
-	/* only control pipes are bi-directional and those do not need toggle */
-	if (direction == USB_DIRECTION_BOTH)
-		return ENOENT;
-	int ret;
-	fibril_mutex_lock(&instance->guard);
-	if (target.endpoint > 15 || target.endpoint < 0
-	    || target.address >= USB_ADDRESS_COUNT || target.address < 0
-	    || !instance->devices[target.address].occupied) {
-		usb_log_error("Invalid data when asking for toggle value.\n");
-		ret = EINVAL;
-	} else {
-		ret = (instance->devices[target.address].toggle_status[direction]
-		        >> target.endpoint) & 1;
-	}
-	fibril_mutex_unlock(&instance->guard);
-	return ret;
-}
-/*----------------------------------------------------------------------------*/
-/** Set current value of endpoint toggle.
- *
- * @param[in] instance Device keeper structure to use.
- * @param[in] target Device and endpoint used.
- * @param[in] toggle Toggle value.
- * @return Error code.
- */
-int usb_device_keeper_set_toggle(usb_device_keeper_t *instance,
-    usb_target_t target, usb_direction_t direction, bool toggle)
-{
-	assert(instance);
-	/* only control pipes are bi-directional and those do not need toggle */
-	if (direction == USB_DIRECTION_BOTH)
-		return ENOENT;
-	int ret;
-	fibril_mutex_lock(&instance->guard);
-	if (target.endpoint > 15 || target.endpoint < 0
-	    || target.address >= USB_ADDRESS_COUNT || target.address < 0
-	    || !instance->devices[target.address].occupied) {
-		usb_log_error("Invalid data when setting toggle value.\n");
-		ret = EINVAL;
-	} else {
-		if (toggle) {
-			instance->devices[target.address].toggle_status[direction]
-			    |= (1 << target.endpoint);
-		} else {
-			instance->devices[target.address].toggle_status[direction]
-			    &= ~(1 << target.endpoint);
-		}
-		ret = EOK;
-	}
-	fibril_mutex_unlock(&instance->guard);
-	return ret;
 }
 /*----------------------------------------------------------------------------*/
@@ -208,6 +165,6 @@
  * @return Free address, or error code.
  */
-usb_address_t device_keeper_get_free_address(usb_device_keeper_t *instance,
-    usb_speed_t speed)
+usb_address_t device_keeper_get_free_address(
+    usb_device_keeper_t *instance, usb_speed_t speed)
 {
 	assert(instance);
@@ -229,6 +186,4 @@
 	instance->devices[new_address].occupied = true;
 	instance->devices[new_address].speed = speed;
-	instance->devices[new_address].toggle_status[0] = 0;
-	instance->devices[new_address].toggle_status[1] = 0;
 	instance->last_address = new_address;
 	fibril_mutex_unlock(&instance->guard);
@@ -259,6 +214,6 @@
  * @param[in] address Device address
  */
-void usb_device_keeper_release(usb_device_keeper_t *instance,
-    usb_address_t address)
+void usb_device_keeper_release(
+    usb_device_keeper_t *instance, usb_address_t address)
 {
 	assert(instance);
@@ -278,6 +233,6 @@
  * @return USB Address, or error code.
  */
-usb_address_t usb_device_keeper_find(usb_device_keeper_t *instance,
-    devman_handle_t handle)
+usb_address_t usb_device_keeper_find(
+    usb_device_keeper_t *instance, devman_handle_t handle)
 {
 	assert(instance);
@@ -301,6 +256,6 @@
  * @return USB speed.
  */
-usb_speed_t usb_device_keeper_get_speed(usb_device_keeper_t *instance,
-    usb_address_t address)
+usb_speed_t usb_device_keeper_get_speed(
+    usb_device_keeper_t *instance, usb_address_t address)
 {
 	assert(instance);
@@ -310,22 +265,25 @@
 }
 /*----------------------------------------------------------------------------*/
-void usb_device_keeper_use_control(usb_device_keeper_t *instance,
-    usb_address_t address)
-{
-	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-	while (instance->devices[address].control_used) {
+void usb_device_keeper_use_control(
+    usb_device_keeper_t *instance, usb_target_t target)
+{
+	assert(instance);
+	const uint16_t ep = 1 << target.endpoint;
+	fibril_mutex_lock(&instance->guard);
+	while (instance->devices[target.address].control_used & ep) {
 		fibril_condvar_wait(&instance->change, &instance->guard);
 	}
-	instance->devices[address].control_used = true;
-	fibril_mutex_unlock(&instance->guard);
-}
-/*----------------------------------------------------------------------------*/
-void usb_device_keeper_release_control(usb_device_keeper_t *instance,
-    usb_address_t address)
-{
-	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-	instance->devices[address].control_used = false;
+	instance->devices[target.address].control_used |= ep;
+	fibril_mutex_unlock(&instance->guard);
+}
+/*----------------------------------------------------------------------------*/
+void usb_device_keeper_release_control(
+    usb_device_keeper_t *instance, usb_target_t target)
+{
+	assert(instance);
+	const uint16_t ep = 1 << target.endpoint;
+	fibril_mutex_lock(&instance->guard);
+	assert((instance->devices[target.address].control_used & ep) != 0);
+	instance->devices[target.address].control_used &= ~ep;
 	fibril_mutex_unlock(&instance->guard);
 	fibril_condvar_signal(&instance->change);
Index: uspace/lib/usb/src/host/endpoint.c
===================================================================
--- uspace/lib/usb/src/host/endpoint.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/usb/src/host/endpoint.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 drvusbuhcihc
+ * @{
+ */
+/** @file
+ * @brief UHCI host controller driver structure
+ */
+
+#include <errno.h>
+#include <usb/host/endpoint.h>
+
+int endpoint_init(endpoint_t *instance, usb_address_t address,
+    usb_endpoint_t endpoint, usb_direction_t direction,
+    usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size)
+{
+	assert(instance);
+	instance->address = address;
+	instance->endpoint = endpoint;
+	instance->direction = direction;
+	instance->transfer_type = type;
+	instance->speed = speed;
+	instance->max_packet_size = max_packet_size;
+	instance->toggle = 0;
+	link_initialize(&instance->same_device_eps);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+void endpoint_destroy(endpoint_t *instance)
+{
+	assert(instance);
+	list_remove(&instance->same_device_eps);
+	free(instance);
+}
+/*----------------------------------------------------------------------------*/
+int endpoint_toggle_get(endpoint_t *instance)
+{
+	assert(instance);
+	return (int)instance->toggle;
+}
+/*----------------------------------------------------------------------------*/
+void endpoint_toggle_set(endpoint_t *instance, int toggle)
+{
+	assert(instance);
+	assert(toggle == 0 || toggle == 1);
+	instance->toggle = toggle;
+}
+/*----------------------------------------------------------------------------*/
+void endpoint_toggle_reset(link_t *ep)
+{
+	endpoint_t *instance =
+	    list_get_instance(ep, endpoint_t, same_device_eps);
+	assert(instance);
+	instance->toggle = 0;
+}
+/*----------------------------------------------------------------------------*/
+void endpoint_toggle_reset_filtered(link_t *ep, usb_endpoint_t epn)
+{
+	endpoint_t *instance =
+	    list_get_instance(ep, endpoint_t, same_device_eps);
+	assert(instance);
+	if (instance->endpoint == epn)
+		instance->toggle = 0;
+}
+/**
+ * @}
+ */
Index: uspace/lib/usb/src/host/usb_endpoint_manager.c
===================================================================
--- uspace/lib/usb/src/host/usb_endpoint_manager.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
+++ uspace/lib/usb/src/host/usb_endpoint_manager.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * All rights eps.
+ *
+ * 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 <bool.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <usb/host/usb_endpoint_manager.h>
+
+#define BUCKET_COUNT 7
+
+#define MAX_KEYS (3)
+typedef struct {
+	link_t link;
+	size_t bw;
+	endpoint_t *ep;
+} node_t;
+/*----------------------------------------------------------------------------*/
+static hash_index_t node_hash(unsigned long key[])
+{
+	hash_index_t hash = 0;
+	unsigned i = 0;
+	for (;i < MAX_KEYS; ++i) {
+		hash ^= key[i];
+	}
+	hash %= BUCKET_COUNT;
+	return hash;
+}
+/*----------------------------------------------------------------------------*/
+static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
+{
+	assert(item);
+	node_t *node = hash_table_get_instance(item, node_t, link);
+	assert(node);
+	assert(node->ep);
+	bool match = true;
+	switch (keys) {
+	case 3:
+		match = match && (key[2] == node->ep->direction);
+	case 2:
+		match = match && (key[1] == (unsigned long)node->ep->endpoint);
+	case 1:
+		match = match && (key[0] == (unsigned long)node->ep->address);
+		break;
+	default:
+		match = false;
+	}
+	return match;
+}
+/*----------------------------------------------------------------------------*/
+static void node_remove(link_t *item)
+{
+	assert(item);
+	node_t *node = hash_table_get_instance(item, node_t, link);
+	endpoint_destroy(node->ep);
+	free(node);
+}
+/*----------------------------------------------------------------------------*/
+static hash_table_operations_t op = {
+	.hash = node_hash,
+	.compare = node_compare,
+	.remove_callback = node_remove,
+};
+/*----------------------------------------------------------------------------*/
+size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
+    size_t size, size_t max_packet_size)
+{
+	/* We care about bandwidth only for interrupt and isochronous. */
+	if ((type != USB_TRANSFER_INTERRUPT)
+	    && (type != USB_TRANSFER_ISOCHRONOUS)) {
+		return 0;
+	}
+
+	const unsigned packet_count =
+	    (size + max_packet_size - 1) / max_packet_size;
+	/* TODO: It may be that ISO and INT transfers use only one data packet
+	 * per transaction, but I did not find text in UB spec that confirms
+	 * this */
+	/* NOTE: All data packets will be considered to be max_packet_size */
+	switch (speed)
+	{
+	case USB_SPEED_LOW:
+		assert(type == USB_TRANSFER_INTERRUPT);
+		/* Protocol overhead 13B
+		 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
+		 * CRC bytes, and a 3-byte interpacket delay)
+		 * see USB spec page 45-46. */
+		/* Speed penalty 8: low speed is 8-times slower*/
+		return packet_count * (13 + max_packet_size) * 8;
+	case USB_SPEED_FULL:
+		/* Interrupt transfer overhead see above
+		 * or page 45 of USB spec */
+		if (type == USB_TRANSFER_INTERRUPT)
+			return packet_count * (13 + max_packet_size);
+
+		assert(type == USB_TRANSFER_ISOCHRONOUS);
+		/* Protocol overhead 9B
+		 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
+		 * bytes, and a 1-byte interpacket delay)
+		 * see USB spec page 42 */
+		return packet_count * (9 + max_packet_size);
+	default:
+		return 0;
+	}
+}
+/*----------------------------------------------------------------------------*/
+int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
+    size_t available_bandwidth)
+{
+	assert(instance);
+	fibril_mutex_initialize(&instance->guard);
+	fibril_condvar_initialize(&instance->change);
+	instance->free_bw = available_bandwidth;
+	bool ht =
+	    hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
+	return ht ? EOK : ENOMEM;
+}
+/*----------------------------------------------------------------------------*/
+void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
+{
+	hash_table_destroy(&instance->ep_table);
+}
+/*----------------------------------------------------------------------------*/
+int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
+    endpoint_t *ep, size_t data_size)
+{
+	assert(ep);
+	size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
+	    data_size, ep->max_packet_size);
+	assert(instance);
+
+	unsigned long key[MAX_KEYS] =
+	    {ep->address, ep->endpoint, ep->direction};
+	fibril_mutex_lock(&instance->guard);
+
+	link_t *item =
+	    hash_table_find(&instance->ep_table, key);
+	if (item != NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return EEXISTS;
+	}
+
+	if (bw > instance->free_bw) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOSPC;
+	}
+
+	node_t *node = malloc(sizeof(node_t));
+	if (node == NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOMEM;
+	}
+
+	node->bw = bw;
+	node->ep = ep;
+	link_initialize(&node->link);
+
+	hash_table_insert(&instance->ep_table, key, &node->link);
+	instance->free_bw -= bw;
+	fibril_mutex_unlock(&instance->guard);
+	fibril_condvar_broadcast(&instance->change);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	assert(instance);
+	unsigned long key[MAX_KEYS] = {address, endpoint, direction};
+
+	fibril_mutex_lock(&instance->guard);
+	link_t *item = hash_table_find(&instance->ep_table, key);
+	if (item == NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return EINVAL;
+	}
+
+	node_t *node = hash_table_get_instance(item, node_t, link);
+	instance->free_bw += node->bw;
+	hash_table_remove(&instance->ep_table, key, MAX_KEYS);
+
+	fibril_mutex_unlock(&instance->guard);
+	fibril_condvar_broadcast(&instance->change);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
+    size_t *bw)
+{
+	assert(instance);
+	unsigned long key[MAX_KEYS] = {address, endpoint, direction};
+
+	fibril_mutex_lock(&instance->guard);
+	link_t *item = hash_table_find(&instance->ep_table, key);
+	if (item == NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return NULL;
+	}
+	node_t *node = hash_table_get_instance(item, node_t, link);
+	if (bw)
+		*bw = node->bw;
+
+	fibril_mutex_unlock(&instance->guard);
+	return node->ep;
+}
Index: uspace/lib/usb/src/hub.c
===================================================================
--- uspace/lib/usb/src/hub.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/hub.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -142,4 +142,23 @@
 	    DEV_IFACE_ID(USBHC_DEV_IFACE),
 	    IPC_M_USBHC_RELEASE_ADDRESS, address);
+}
+
+static void unregister_control_endpoint_on_default_address(
+    usb_hc_connection_t *connection)
+{
+	usb_device_connection_t dev_conn;
+	int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
+	    connection);
+	if (rc != EOK) {
+		return;
+	}
+
+	usb_pipe_t ctrl_pipe;
+	rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
+	if (rc != EOK) {
+		return;
+	}
+
+	usb_pipe_unregister(&ctrl_pipe, connection);
 }
 
@@ -235,4 +254,13 @@
 		goto leave_release_default_address;
 	}
+
+	/* Before sending any traffic, we need to register this
+	 * endpoint.
+	 */
+	rc = usb_pipe_register(&ctrl_pipe, 0, connection);
+	if (rc != EOK) {
+		rc = EREFUSED;
+		goto leave_release_default_address;
+	}
 	rc = usb_pipe_probe_default_control(&ctrl_pipe);
 	if (rc != EOK) {
@@ -244,5 +272,5 @@
 	if (rc != EOK) {
 		rc = ENOTCONN;
-		goto leave_release_default_address;
+		goto leave_unregister_endpoint;
 	}
 
@@ -256,7 +284,22 @@
 
 	/*
+	 * Register the control endpoint for the new device.
+	 */
+	rc = usb_pipe_register(&ctrl_pipe, 0, connection);
+	if (rc != EOK) {
+		rc = EREFUSED;
+		goto leave_unregister_endpoint;
+	}
+
+	/*
+	 * Release the original endpoint.
+	 */
+	unregister_control_endpoint_on_default_address(connection);
+
+	/*
 	 * Once the address is changed, we can return the default address.
 	 */
 	usb_hc_release_default_address(connection);
+
 
 	/*
@@ -273,4 +316,6 @@
 	}
 
+
+
 	/*
 	 * And now inform the host controller about the handle.
@@ -308,4 +353,7 @@
 	usb_pipe_end_session(&ctrl_pipe);
 
+leave_unregister_endpoint:
+	usb_pipe_unregister(&ctrl_pipe, connection);
+
 leave_release_default_address:
 	usb_hc_release_default_address(connection);
Index: uspace/lib/usb/src/pipesinit.c
===================================================================
--- uspace/lib/usb/src/pipesinit.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/pipesinit.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -121,5 +121,5 @@
     usb_endpoint_mapping_t *mapping, size_t mapping_count,
     usb_endpoint_description_t *found_endpoint,
-    int interface_number)
+    int interface_number, int interface_setting)
 {
 	while (mapping_count > 0) {
@@ -127,8 +127,13 @@
 		    || (mapping->interface_no == interface_number);
 
+		bool interface_setting_fits = (mapping->interface_setting < 0)
+		    || (mapping->interface_setting == interface_setting);
+
 		bool endpoint_descriptions_fits = endpoint_fits_description(
 		    mapping->description, found_endpoint);
 
-		if (interface_number_fits && endpoint_descriptions_fits) {
+		if (interface_number_fits
+		    && interface_setting_fits
+		    && endpoint_descriptions_fits) {
 			return mapping;
 		}
@@ -181,5 +186,6 @@
 	 */
 	usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
-	    mapping_count, &description, interface->interface_number);
+	    mapping_count, &description,
+	    interface->interface_number, interface->alternate_setting);
 	if (ep_mapping == NULL) {
 		return ENOENT;
Index: uspace/lib/usb/src/request.c
===================================================================
--- uspace/lib/usb/src/request.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/lib/usb/src/request.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -529,5 +529,4 @@
 		return rc;
 	}
-
 	if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
 		return ENOENT;
Index: uspace/srv/devman/devman.c
===================================================================
--- uspace/srv/devman/devman.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devman/devman.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -34,4 +34,5 @@
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <io/log.h>
 #include <ipc/driver.h>
 #include <ipc/devman.h>
@@ -146,14 +147,6 @@
 	fibril_mutex_unlock(&drivers_list->drivers_mutex);
 
-	printf(NAME": the '%s' driver was added to the list of available "
-	    "drivers.\n", drv->name);
-
-	printf(NAME ": match ids:");
-	link_t *cur;
-	for (cur = drv->match_ids.ids.next; cur != &drv->match_ids.ids; cur = cur->next) {
-		match_id_t *match_id = list_get_instance(cur, match_id_t, link);
-		printf(" %d:%s", match_id->score, match_id->id);
-	}
-	printf("\n");
+	log_msg(LVL_NOTE, "Driver `%s' was added to the list of available "
+	    "drivers.", drv->name);
 }
 
@@ -245,5 +238,5 @@
 bool read_match_ids(const char *conf_path, match_id_list_t *ids)
 {
-	printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
+	log_msg(LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
 	
 	bool suc = false;
@@ -255,5 +248,6 @@
 	fd = open(conf_path, O_RDONLY);
 	if (fd < 0) {
-		printf(NAME ": unable to open %s\n", conf_path);
+		log_msg(LVL_ERROR, "Unable to open `%s' for reading: %s.",
+		    conf_path, str_error(fd));
 		goto cleanup;
 	}
@@ -263,5 +257,6 @@
 	lseek(fd, 0, SEEK_SET);
 	if (len == 0) {
-		printf(NAME ": configuration file '%s' is empty.\n", conf_path);
+		log_msg(LVL_ERROR, "Configuration file '%s' is empty.",
+		    conf_path);
 		goto cleanup;
 	}
@@ -269,14 +264,15 @@
 	buf = malloc(len + 1);
 	if (buf == NULL) {
-		printf(NAME ": memory allocation failed when parsing file "
-		    "'%s'.\n", conf_path);
+		log_msg(LVL_ERROR, "Memory allocation failed when parsing file "
+		    "'%s'.", conf_path);
 		goto cleanup;
 	}
 	
-	if (read(fd, buf, len) <= 0) {
-		printf(NAME ": unable to read file '%s'.\n", conf_path);
+	ssize_t read_bytes = safe_read(fd, buf, len);
+	if (read_bytes <= 0) {
+		log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
 		goto cleanup;
 	}
-	buf[len] = 0;
+	buf[read_bytes] = 0;
 	
 	suc = parse_match_ids(buf, ids);
@@ -313,5 +309,5 @@
 bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
 {
-	printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
+	log_msg(LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
 	    base_path, name);
 	
@@ -345,5 +341,6 @@
 	struct stat s;
 	if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
-		printf(NAME ": driver not found at path %s.", drv->binary_path);
+		log_msg(LVL_ERROR, "Driver not found at path `%s'.",
+		    drv->binary_path);
 		goto cleanup;
 	}
@@ -372,5 +369,5 @@
 int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
 {
-	printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
+	log_msg(LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
 	
 	int drv_cnt = 0;
@@ -406,5 +403,5 @@
 	dev_node_t *dev;
 	
-	printf(NAME ": create_root_nodes\n");
+	log_msg(LVL_DEBUG, "create_root_nodes()");
 	
 	fibril_rwlock_write_lock(&tree->rwlock);
@@ -491,6 +488,6 @@
 void attach_driver(dev_node_t *dev, driver_t *drv)
 {
-	printf(NAME ": attach_driver %s to device %s\n",
-	    drv->name, dev->pfun->pathname);
+	log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
+	    dev->pfun->pathname, drv->name);
 	
 	fibril_mutex_lock(&drv->driver_mutex);
@@ -514,10 +511,10 @@
 	assert(fibril_mutex_is_locked(&drv->driver_mutex));
 	
-	printf(NAME ": start_driver '%s'\n", drv->name);
+	log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
 	
 	rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
 	if (rc != EOK) {
-		printf(NAME ": error spawning %s (%s)\n",
-		    drv->name, str_error(rc));
+		log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
+		    drv->name, drv->binary_path, str_error(rc));
 		return false;
 	}
@@ -581,5 +578,6 @@
 	int phone;
 
-	printf(NAME ": pass_devices_to_driver(`%s')\n", driver->name);
+	log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
+	    driver->name);
 
 	fibril_mutex_lock(&driver->driver_mutex);
@@ -648,5 +646,5 @@
 	 * immediately and possibly started here as well.
 	 */
-	printf(NAME ": driver %s goes into running state.\n", driver->name);
+	log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
 	driver->state = DRIVER_RUNNING;
 
@@ -665,5 +663,6 @@
 void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
 {
-	printf(NAME ": initialize_running_driver (`%s')\n", driver->name);
+	log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
+	    driver->name);
 	
 	/*
@@ -755,6 +754,6 @@
 	 * access any structures that would affect driver_t.
 	 */
-	printf(NAME ": add_device (driver `%s', device `%s')\n", drv->name,
-	    dev->pfun->name);
+	log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
+	    drv->name, dev->pfun->name);
 	
 	sysarg_t rc;
@@ -817,5 +816,5 @@
 	driver_t *drv = find_best_match_driver(drivers_list, dev);
 	if (drv == NULL) {
-		printf(NAME ": no driver found for device '%s'.\n",
+		log_msg(LVL_ERROR, "No driver found for device `%s'.",
 		    dev->pfun->pathname);
 		return false;
@@ -855,5 +854,5 @@
 bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
 {
-	printf(NAME ": init_device_tree.\n");
+	log_msg(LVL_DEBUG, "init_device_tree()");
 	
 	tree->current_handle = 0;
@@ -1034,5 +1033,5 @@
 	fun->pathname = (char *) malloc(pathsize);
 	if (fun->pathname == NULL) {
-		printf(NAME ": failed to allocate device path.\n");
+		log_msg(LVL_ERROR, "Failed to allocate device path.");
 		return false;
 	}
@@ -1065,4 +1064,7 @@
 	assert(fibril_rwlock_is_write_locked(&tree->rwlock));
 	
+	log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
+	    dev, pfun, pfun->pathname);
+
 	/* Add the node to the handle-to-node map. */
 	dev->handle = ++tree->current_handle;
@@ -1071,5 +1073,4 @@
 
 	/* Add the node to the list of its parent's children. */
-	printf("insert_dev_node: dev=%p, dev->pfun := %p\n", dev, pfun);
 	dev->pfun = pfun;
 	pfun->child = dev;
@@ -1131,4 +1132,11 @@
 fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
 {
+	assert(path != NULL);
+
+	bool is_absolute = path[0] == '/';
+	if (!is_absolute) {
+		return NULL;
+	}
+
 	fibril_rwlock_read_lock(&tree->rwlock);
 	
@@ -1140,5 +1148,5 @@
 	char *rel_path = path;
 	char *next_path_elem = NULL;
-	bool cont = (rel_path[0] == '/');
+	bool cont = true;
 	
 	while (cont && fun != NULL) {
@@ -1165,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.
  *
@@ -1175,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);
 }
 
@@ -1351,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 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devman/devman.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devman/main.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -43,4 +43,5 @@
 #include <stdio.h>
 #include <errno.h>
+#include <str_error.h>
 #include <bool.h>
 #include <fibril_synch.h>
@@ -51,4 +52,5 @@
 #include <sys/stat.h>
 #include <ctype.h>
+#include <io/log.h>
 #include <ipc/devman.h>
 #include <ipc/driver.h>
@@ -71,5 +73,5 @@
 	driver_t *driver = NULL;
 
-	printf(NAME ": devman_driver_register \n");
+	log_msg(LVL_DEBUG, "devman_driver_register");
 	
 	iid = async_get_call(&icall);
@@ -88,5 +90,5 @@
 	}
 
-	printf(NAME ": the %s driver is trying to register by the service.\n",
+	log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
 	    drv_name);
 	
@@ -95,5 +97,5 @@
 	
 	if (driver == NULL) {
-		printf(NAME ": no driver named %s was found.\n", drv_name);
+		log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
 		free(drv_name);
 		drv_name = NULL;
@@ -106,5 +108,6 @@
 	
 	/* Create connection to the driver. */
-	printf(NAME ":  creating connection to the %s driver.\n", driver->name);
+	log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
+	    driver->name);
 	ipc_call_t call;
 	ipc_callid_t callid = async_get_call(&call);
@@ -118,5 +121,6 @@
 	set_driver_phone(driver, IPC_GET_ARG5(call));
 	
-	printf(NAME ": the %s driver was successfully registered as running.\n",
+	log_msg(LVL_NOTE, 
+	    "The `%s' driver was successfully registered as running.",
 	    driver->name);
 	
@@ -142,6 +146,6 @@
 	callid = async_get_call(&call);
 	if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
-		printf(NAME ": ERROR: devman_receive_match_id - invalid "
-		    "protocol.\n");
+		log_msg(LVL_ERROR, 
+		    "Invalid protocol when trying to receive match id.");
 		async_answer_0(callid, EINVAL); 
 		delete_match_id(match_id);
@@ -150,6 +154,5 @@
 	
 	if (match_id == NULL) {
-		printf(NAME ": ERROR: devman_receive_match_id - failed to "
-		    "allocate match id.\n");
+		log_msg(LVL_ERROR, "Failed to allocate match id.");
 		async_answer_0(callid, ENOMEM);
 		return ENOMEM;
@@ -165,6 +168,6 @@
 	if (rc != EOK) {
 		delete_match_id(match_id);
-		printf(NAME ": devman_receive_match_id - failed to receive "
-		    "match id string.\n");
+		log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
+		    str_error(rc));
 		return rc;
 	}
@@ -172,5 +175,5 @@
 	list_append(&match_id->link, &match_ids->ids);
 	
-	printf(NAME ": received match id '%s', score = %d \n",
+	log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
 	    match_id->id, match_id->score);
 	return rc;
@@ -228,5 +231,7 @@
 	if (ftype != fun_inner && ftype != fun_exposed) {
 		/* Unknown function type */
-		printf(NAME ": Error, unknown function type provided by driver!\n");
+		log_msg(LVL_ERROR, 
+		    "Unknown function type %d provided by driver.",
+		    (int) ftype);
 
 		fibril_rwlock_write_unlock(&tree->rwlock);
@@ -243,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)) {
@@ -265,5 +280,5 @@
 	fibril_rwlock_write_unlock(&tree->rwlock);
 	
-	printf(NAME ": devman_add_function %s\n", fun->pathname);
+	log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
 	
 	devman_receive_match_ids(match_count, &fun->match_ids);
@@ -347,6 +362,6 @@
 	devmap_register_class_dev(class_info);
 	
-	printf(NAME ": function'%s' added to class '%s', class name '%s' was "
-	    "asigned to it\n", fun->pathname, class_name, class_info->dev_name);
+	log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.",
+	    fun->pathname, class_name, class_info->dev_name);
 
 	async_answer_0(callid, EOK);
@@ -363,5 +378,5 @@
 	
 	initialize_running_driver(driver, &device_tree);
-	printf(NAME ": the %s driver was successfully initialized. \n",
+	log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
 	    driver->name);
 	return 0;
@@ -385,6 +400,6 @@
 	fid_t fid = fibril_create(init_running_drv, driver);
 	if (fid == 0) {
-		printf(NAME ": Error creating fibril for the initialization of "
-		    "the newly registered running driver.\n");
+		log_msg(LVL_ERROR, "Failed to create initialization fibril " \
+		    "for driver `%s'.", driver->name);
 		return;
 	}
@@ -438,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. */
@@ -457,4 +506,7 @@
 			devman_function_get_handle(callid, &call);
 			break;
+		case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
+			devman_function_get_handle_by_class(callid, &call);
+			break;
 		default:
 			async_answer_0(callid, ENOENT);
@@ -477,7 +529,13 @@
 		dev = fun->dev;
 
-	if (fun == NULL && dev == NULL) {
-		printf(NAME ": devman_forward error - no device or function with "
-		    "handle %" PRIun " was found.\n", handle);
+	/*
+	 * 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(LVL_ERROR, "IPC forwarding failed - no device or "
+		    "function with handle %" PRIun " was found.", handle);
 		async_answer_0(iid, ENOENT);
 		return;
@@ -485,6 +543,7 @@
 
 	if (fun == NULL && !drv_to_parent) {
-		printf(NAME ": devman_forward error - cannot connect to "
-		    "handle %" PRIun ", refers to a device.\n", handle);
+		log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
+		    "connect to handle %" PRIun ", refers to a device.",
+		    handle);
 		async_answer_0(iid, ENOENT);
 		return;
@@ -507,6 +566,6 @@
 	
 	if (driver == NULL) {
-		printf(NAME ": devman_forward error - the device %" PRIun \
-		    " (%s) is not in usable state.\n",
+		log_msg(LVL_ERROR, "IPC forwarding refused - " \
+		    "the device %" PRIun "(%s) is not in usable state.",
 		    handle, dev->pfun->pathname);
 		async_answer_0(iid, ENOENT);
@@ -521,7 +580,7 @@
 	
 	if (driver->phone <= 0) {
-		printf(NAME ": devman_forward: cound not forward to driver %s ",
-		    driver->name);
-		printf("the driver's phone is %" PRIun ").\n", driver->phone);
+		log_msg(LVL_ERROR, 
+		    "Could not forward to driver `%s' (phone is %d).",
+		    driver->name, (int) driver->phone);
 		async_answer_0(iid, EINVAL);
 		return;
@@ -529,9 +588,11 @@
 
 	if (fun != NULL) {
-		printf(NAME ": devman_forward: forward connection to function %s to "
-		    "driver %s.\n", fun->pathname, driver->name);
+		log_msg(LVL_DEBUG, 
+		    "Forwarding request for `%s' function to driver `%s'.",
+		    fun->pathname, driver->name);
 	} else {
-		printf(NAME ": devman_forward: forward connection to device %s to "
-		    "driver %s.\n", dev->pfun->pathname, driver->name);
+		log_msg(LVL_DEBUG, 
+		    "Forwarding request for `%s' device to driver `%s'.",
+		    dev->pfun->pathname, driver->name);
 	}
 
@@ -565,6 +626,7 @@
 	async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
 	    IPC_FF_NONE);
-	printf(NAME ": devman_connection_devmapper: forwarded connection to "
-	    "device %s to driver %s.\n", fun->pathname, dev->drv->name);
+	log_msg(LVL_DEBUG, 
+	    "Forwarding devmapper request for `%s' function to driver `%s'.",
+	    fun->pathname, dev->drv->name);
 }
 
@@ -601,5 +663,5 @@
 static bool devman_init(void)
 {
-	printf(NAME ": devman_init - looking for available drivers.\n");
+	log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
 	
 	/* Initialize list of available drivers. */
@@ -607,13 +669,13 @@
 	if (lookup_available_drivers(&drivers_list,
 	    DRIVER_DEFAULT_STORE) == 0) {
-		printf(NAME " no drivers found.");
+		log_msg(LVL_FATAL, "No drivers found.");
 		return false;
 	}
 
-	printf(NAME ": devman_init  - list of drivers has been initialized.\n");
+	log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
 
 	/* Create root device node. */
 	if (!init_device_tree(&device_tree, &drivers_list)) {
-		printf(NAME " failed to initialize device tree.");
+		log_msg(LVL_FATAL, "Failed to initialize device tree.");
 		return false;
 	}
@@ -636,6 +698,11 @@
 	printf(NAME ": HelenOS Device Manager\n");
 
+	if (log_init(NAME, LVL_ERROR) != EOK) {
+		printf(NAME ": Error initializing logging subsystem.\n");
+		return -1;
+	}
+
 	if (!devman_init()) {
-		printf(NAME ": Error while initializing service\n");
+		log_msg(LVL_ERROR, "Error while initializing service.");
 		return -1;
 	}
@@ -645,8 +712,10 @@
 
 	/* Register device manager at naming service. */
-	if (service_register(SERVICE_DEVMAN) != EOK)
+	if (service_register(SERVICE_DEVMAN) != EOK) {
+		log_msg(LVL_ERROR, "Failed registering as a service.");
 		return -1;
-
-	printf(NAME ": Accepting connections\n");
+	}
+
+	printf(NAME ": Accepting connections.\n");
 	async_manager();
 
Index: uspace/srv/devman/util.c
===================================================================
--- uspace/srv/devman/util.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devman/util.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -111,4 +111,31 @@
 }
 
+ssize_t safe_read(int fd, void *buffer, size_t size)
+{
+	if (size == 0) {
+		return 0;
+	}
+
+	uint8_t *buf_ptr = (uint8_t *) buffer;
+
+	size_t total_read = 0;
+	while (total_read < size) {
+		ssize_t bytes_read = read(fd, buf_ptr, size - total_read);
+		if (bytes_read < 0) {
+			/* Error. */
+			return bytes_read;
+		} else if (bytes_read == 0) {
+			/* Possibly end of file. */
+			break;
+		} else {
+			/* Read at least something. */
+			buf_ptr += bytes_read;
+			total_read += bytes_read;
+		}
+	}
+
+	return (ssize_t) total_read;
+}
+
 /** @}
  */
Index: uspace/srv/devman/util.h
===================================================================
--- uspace/srv/devman/util.h	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devman/util.h	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -47,4 +47,6 @@
 extern void replace_char(char *, char, char);
 
+extern ssize_t safe_read(int, void *, size_t);
+
 #endif
 
Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/devmap/devmap.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -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);
Index: uspace/srv/hw/bus/cuda_adb/cuda_adb.c
===================================================================
--- uspace/srv/hw/bus/cuda_adb/cuda_adb.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/hw/bus/cuda_adb/cuda_adb.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -367,9 +367,9 @@
 static void cuda_irq_rcv_end(void *buf, size_t *len)
 {
-	uint8_t data, b;
-
+	uint8_t b;
+	
 	b = pio_read_8(&dev->b);
-	data = pio_read_8(&dev->sr);
-
+	pio_read_8(&dev->sr);
+	
 	if ((b & TREQ) == 0) {
 		instance->xstate = cx_receive;
@@ -379,7 +379,7 @@
 		cuda_send_start();
 	}
-
-        memcpy(buf, instance->rcv_buf, instance->bidx);
-        *len = instance->bidx;
+	
+	memcpy(buf, instance->rcv_buf, instance->bidx);
+	*len = instance->bidx;
 	instance->bidx = 0;
 }
Index: uspace/srv/hw/irc/apic/apic.c
===================================================================
--- uspace/srv/hw/irc/apic/apic.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/hw/irc/apic/apic.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -87,4 +87,8 @@
 			async_answer_0(callid, EOK);
 			break;
+		case IPC_M_PHONE_HUNGUP:
+			/* The other side has hung up. */
+			async_answer_0(callid, EOK);
+			return;
 		default:
 			async_answer_0(callid, EINVAL);
Index: uspace/srv/hw/irc/i8259/i8259.c
===================================================================
--- uspace/srv/hw/irc/i8259/i8259.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/hw/irc/i8259/i8259.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -121,4 +121,8 @@
 			async_answer_0(callid, EOK);
 			break;
+		case IPC_M_PHONE_HUNGUP:
+			/* The other side has hung up. */
+			async_answer_0(callid, EOK);
+			return;
 		default:
 			async_answer_0(callid, EINVAL);
Index: uspace/srv/hw/netif/ne2000/dp8390.c
===================================================================
--- uspace/srv/hw/netif/ne2000/dp8390.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/hw/netif/ne2000/dp8390.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -391,4 +391,5 @@
 	
 	if ((size < ETH_MIN_PACK_SIZE) || (size > ETH_MAX_PACK_SIZE_TAGGED)) {
+		fibril_mutex_unlock(&ne2k->sq_mutex);
 		fprintf(stderr, "%s: Frame dropped (invalid size %zu bytes)\n",
 		    NAME, size);
Index: uspace/srv/loader/arch/abs32le/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/abs32le/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/abs32le/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -21,6 +21,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/amd64/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/amd64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/amd64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -27,6 +27,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/arm32/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/arm32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/arm32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -25,6 +25,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/ia32/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/ia32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/ia32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -26,6 +26,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/ia64/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/ia64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/ia64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -21,6 +21,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
@@ -29,5 +29,5 @@
 	.got : {
 		_gp = .;
-		*(.got*);
+		*(.got .got.*);
 	} :data
 	
Index: uspace/srv/loader/arch/mips32/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/mips32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/mips32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -25,6 +25,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/ppc32/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/ppc32/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/ppc32/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -25,6 +25,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/arch/sparc64/_link.ld.in
===================================================================
--- uspace/srv/loader/arch/sparc64/_link.ld.in	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/arch/sparc64/_link.ld.in	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -20,6 +20,6 @@
 	
 	.text : {
-		*(.text);
-		*(.rodata*);
+		*(.text .text.*);
+		*(.rodata .rodata.*);
 	} :text
 	
Index: uspace/srv/loader/main.c
===================================================================
--- uspace/srv/loader/main.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/loader/main.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -407,12 +407,10 @@
 			/* Not reached */
 		default:
-			retval = ENOENT;
+			retval = EINVAL;
 			break;
 		}
-		if (IPC_GET_IMETHOD(call) != IPC_M_PHONE_HUNGUP) {
-			DPRINTF("Responding EINVAL to method %d.\n",
-			    IPC_GET_IMETHOD(call));
-			async_answer_0(callid, EINVAL);
-		}
+		
+		if (IPC_GET_IMETHOD(call) != IPC_M_PHONE_HUNGUP)
+			async_answer_0(callid, retval);
 	}
 }
Index: uspace/srv/net/il/arp/arp.c
===================================================================
--- uspace/srv/net/il/arp/arp.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/il/arp/arp.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -157,9 +157,9 @@
 			
 			arp_clear_addr(&proto->addresses);
-			arp_addr_destroy(&proto->addresses);
-		}
-	}
-	
-	arp_protos_clear(&device->protos);
+			arp_addr_destroy(&proto->addresses, free);
+		}
+	}
+	
+	arp_protos_clear(&device->protos, free);
 }
 
@@ -184,5 +184,5 @@
 	}
 	
-	arp_cache_clear(&arp_globals.cache);
+	arp_cache_clear(&arp_globals.cache, free);
 	fibril_mutex_unlock(&arp_globals.lock);
 	
@@ -212,5 +212,5 @@
 		arp_clear_trans(trans);
 	
-	arp_addr_exclude(&proto->addresses, address->value, address->length);
+	arp_addr_exclude(&proto->addresses, address->value, address->length, free);
 	
 	fibril_mutex_unlock(&arp_globals.lock);
@@ -345,5 +345,5 @@
 			    header->protocol_length, trans);
 			if (rc != EOK) {
-				/* The generic char map has already freed trans! */
+				free(trans);
 				return rc;
 			}
@@ -556,5 +556,5 @@
 		if (index < 0) {
 			fibril_mutex_unlock(&arp_globals.lock);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return index;
@@ -569,5 +569,5 @@
 		if (device->phone < 0) {
 			fibril_mutex_unlock(&arp_globals.lock);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return EREFUSED;
@@ -579,5 +579,5 @@
 		if (rc != EOK) {
 			fibril_mutex_unlock(&arp_globals.lock);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return rc;
@@ -589,5 +589,5 @@
 		if (rc != EOK) {
 			fibril_mutex_unlock(&arp_globals.lock);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return rc;
@@ -601,5 +601,5 @@
 			free(device->addr);
 			free(device->addr_data);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return rc;
@@ -614,5 +614,5 @@
 			free(device->broadcast_addr);
 			free(device->broadcast_data);
-			arp_protos_destroy(&device->protos);
+			arp_protos_destroy(&device->protos, free);
 			free(device);
 			return rc;
@@ -746,5 +746,5 @@
 			arp_clear_trans(trans);
 			arp_addr_exclude(&proto->addresses, target->value,
-			    target->length);
+			    target->length, free);
 			return EAGAIN;
 		}
@@ -794,5 +794,5 @@
 	    trans);
 	if (rc != EOK) {
-		/* The generic char map has already freed trans! */
+		free(trans);
 		return rc;
 	}
@@ -807,5 +807,5 @@
 		arp_clear_trans(trans);
 		arp_addr_exclude(&proto->addresses, target->value,
-		    target->length);
+		    target->length, free);
 		return ENOENT;
 	}
Index: uspace/srv/net/il/ip/ip.c
===================================================================
--- uspace/srv/net/il/ip/ip.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/il/ip/ip.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -505,5 +505,5 @@
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
-		ip_routes_destroy(&ip_netif->routes);
+		ip_routes_destroy(&ip_netif->routes, free);
 		free(ip_netif);
 		return rc;
Index: uspace/srv/net/net/net.c
===================================================================
--- uspace/srv/net/net/net.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/net/net.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -555,5 +555,5 @@
 		rc = read_netif_configuration(conf_files[i], netif);
 		if (rc != EOK) {
-			measured_strings_destroy(&netif->configuration);
+			measured_strings_destroy(&netif->configuration, free);
 			free(netif);
 			return rc;
@@ -565,5 +565,5 @@
 		if (!setting) {
 			fprintf(stderr, "%s: Network interface name is missing\n", NAME);
-			measured_strings_destroy(&netif->configuration);
+			measured_strings_destroy(&netif->configuration, free);
 			free(netif);
 			return EINVAL;
@@ -574,5 +574,5 @@
 		int index = netifs_add(&net_globals.netifs, netif->id, netif);
 		if (index < 0) {
-			measured_strings_destroy(&netif->configuration);
+			measured_strings_destroy(&netif->configuration, free);
 			free(netif);
 			return index;
@@ -586,6 +586,6 @@
 		    index);
 		if (rc != EOK) {
-			measured_strings_destroy(&netif->configuration);
-			netifs_exclude_index(&net_globals.netifs, index);
+			measured_strings_destroy(&netif->configuration, free);
+			netifs_exclude_index(&net_globals.netifs, index, free);
 			return rc;
 		}
@@ -595,6 +595,6 @@
 			printf("%s: Ignoring failed interface %s (%s)\n", NAME,
 			    netif->name, str_error(rc));
-			measured_strings_destroy(&netif->configuration);
-			netifs_exclude_index(&net_globals.netifs, index);
+			measured_strings_destroy(&netif->configuration, free);
+			netifs_exclude_index(&net_globals.netifs, index, free);
 			continue;
 		}
Index: uspace/srv/net/nil/eth/eth.c
===================================================================
--- uspace/srv/net/nil/eth/eth.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/nil/eth/eth.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -214,5 +214,5 @@
 	if (rc != EOK) {
 		free(eth_globals.broadcast_addr);
-		eth_devices_destroy(&eth_globals.devices);
+		eth_devices_destroy(&eth_globals.devices, free);
 	}
 out:
Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -1707,5 +1707,5 @@
 		if (socket->port > 0) {
 			socket_ports_exclude(&tcp_globals.sockets,
-			    socket->port);
+			    socket->port, free);
 			socket->port = 0;
 		}
@@ -2492,5 +2492,5 @@
 	rc = packet_dimensions_initialize(&tcp_globals.dimensions);
 	if (rc != EOK) {
-		socket_ports_destroy(&tcp_globals.sockets);
+		socket_ports_destroy(&tcp_globals.sockets, free);
 		goto out;
 	}
Index: uspace/srv/net/tl/udp/udp.c
===================================================================
--- uspace/srv/net/tl/udp/udp.c	(revision 61257f414d6dfc36c096abb542ed95e187bfb391)
+++ uspace/srv/net/tl/udp/udp.c	(revision f8e8738d31d2b92e00a460fe812bda69bfb6ef51)
@@ -417,5 +417,5 @@
 	rc = packet_dimensions_initialize(&udp_globals.dimensions);
 	if (rc != EOK) {
-		socket_ports_destroy(&udp_globals.sockets);
+		socket_ports_destroy(&udp_globals.sockets, free);
 		fibril_rwlock_write_unlock(&udp_globals.lock);
 		return rc;
@@ -434,5 +434,5 @@
 	    &data);
 	if (rc != EOK) {
-		socket_ports_destroy(&udp_globals.sockets);
+		socket_ports_destroy(&udp_globals.sockets, free);
 		fibril_rwlock_write_unlock(&udp_globals.lock);
 		return rc;
