Index: uspace/lib/c/generic/async/ports.c
===================================================================
--- uspace/lib/c/generic/async/ports.c	(revision 49a796f1d5e0783f179376793f1b84d4f6cf0bde)
+++ uspace/lib/c/generic/async/ports.c	(revision 49a796f1d5e0783f179376793f1b84d4f6cf0bde)
@@ -0,0 +1,304 @@
+/*
+ * Copyright (c) 2006 Ondrej Palkovsky
+ * 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.
+ */
+
+#define LIBC_ASYNC_C_
+#include <ipc/ipc.h>
+#include <async.h>
+#include "../private/async.h"
+#undef LIBC_ASYNC_C_
+
+#include <ipc/irq.h>
+#include <ipc/event.h>
+#include <futex.h>
+#include <fibril.h>
+#include <adt/hash_table.h>
+#include <adt/hash.h>
+#include <adt/list.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <libarch/barrier.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <mem.h>
+#include <stdlib.h>
+#include <macros.h>
+#include <as.h>
+#include <abi/mm/as.h>
+#include "../private/libc.h"
+
+/** Interface data */
+typedef struct {
+	ht_link_t link;
+
+	/** Interface ID */
+	iface_t iface;
+
+	/** Futex protecting the hash table */
+	futex_t futex;
+
+	/** Interface ports */
+	hash_table_t port_hash_table;
+
+	/** Next available port ID */
+	port_id_t port_id_avail;
+} interface_t;
+
+/* Port data */
+typedef struct {
+	ht_link_t link;
+
+	/** Port ID */
+	port_id_t id;
+
+	/** Port connection handler */
+	async_port_handler_t handler;
+
+	/** Client data */
+	void *data;
+} port_t;
+
+/** Default fallback fibril function.
+ *
+ * This fallback fibril function gets called on incomming connections that do
+ * not have a specific handler defined.
+ *
+ * @param chandle  Handle of the incoming call.
+ * @param call     Data of the incoming call.
+ * @param arg      Local argument
+ *
+ */
+static void default_fallback_port_handler(cap_call_handle_t chandle,
+    ipc_call_t *call, void *arg)
+{
+	ipc_answer_0(chandle, ENOENT);
+}
+
+static async_port_handler_t fallback_port_handler =
+    default_fallback_port_handler;
+static void *fallback_port_data = NULL;
+
+static hash_table_t interface_hash_table;
+
+static size_t interface_key_hash(void *key)
+{
+	iface_t iface = *(iface_t *) key;
+	return iface;
+}
+
+static size_t interface_hash(const ht_link_t *item)
+{
+	interface_t *interface = hash_table_get_inst(item, interface_t, link);
+	return interface_key_hash(&interface->iface);
+}
+
+static bool interface_key_equal(void *key, const ht_link_t *item)
+{
+	iface_t iface = *(iface_t *) key;
+	interface_t *interface = hash_table_get_inst(item, interface_t, link);
+	return iface == interface->iface;
+}
+
+/** Operations for the port hash table. */
+static hash_table_ops_t interface_hash_table_ops = {
+	.hash = interface_hash,
+	.key_hash = interface_key_hash,
+	.key_equal = interface_key_equal,
+	.equal = NULL,
+	.remove_callback = NULL
+};
+
+static size_t port_key_hash(void *key)
+{
+	port_id_t port_id = *(port_id_t *) key;
+	return port_id;
+}
+
+static size_t port_hash(const ht_link_t *item)
+{
+	port_t *port = hash_table_get_inst(item, port_t, link);
+	return port_key_hash(&port->id);
+}
+
+static bool port_key_equal(void *key, const ht_link_t *item)
+{
+	port_id_t port_id = *(port_id_t *) key;
+	port_t *port = hash_table_get_inst(item, port_t, link);
+	return port_id == port->id;
+}
+
+/** Operations for the port hash table. */
+static hash_table_ops_t port_hash_table_ops = {
+	.hash = port_hash,
+	.key_hash = port_key_hash,
+	.key_equal = port_key_equal,
+	.equal = NULL,
+	.remove_callback = NULL
+};
+
+static interface_t *async_new_interface(iface_t iface)
+{
+	interface_t *interface =
+	    (interface_t *) malloc(sizeof(interface_t));
+	if (!interface)
+		return NULL;
+
+	bool ret = hash_table_create(&interface->port_hash_table, 0, 0,
+	    &port_hash_table_ops);
+	if (!ret) {
+		free(interface);
+		return NULL;
+	}
+
+	interface->iface = iface;
+	futex_initialize(&interface->futex, 1);
+	interface->port_id_avail = 0;
+
+	hash_table_insert(&interface_hash_table, &interface->link);
+
+	return interface;
+}
+
+static port_t *async_new_port(interface_t *interface,
+    async_port_handler_t handler, void *data)
+{
+	port_t *port = (port_t *) malloc(sizeof(port_t));
+	if (!port)
+		return NULL;
+
+	futex_down(&interface->futex);
+
+	port_id_t id = interface->port_id_avail;
+	interface->port_id_avail++;
+
+	port->id = id;
+	port->handler = handler;
+	port->data = data;
+
+	hash_table_insert(&interface->port_hash_table, &port->link);
+
+	futex_up(&interface->futex);
+
+	return port;
+}
+
+errno_t async_create_port_internal(iface_t iface, async_port_handler_t handler,
+    void *data, port_id_t *port_id)
+{
+	interface_t *interface;
+
+	futex_down(&async_futex);
+
+	ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
+	if (link)
+		interface = hash_table_get_inst(link, interface_t, link);
+	else
+		interface = async_new_interface(iface);
+
+	if (!interface) {
+		futex_up(&async_futex);
+		return ENOMEM;
+	}
+
+	port_t *port = async_new_port(interface, handler, data);
+	if (!port) {
+		futex_up(&async_futex);
+		return ENOMEM;
+	}
+
+	*port_id = port->id;
+
+	futex_up(&async_futex);
+
+	return EOK;
+}
+
+errno_t async_create_port(iface_t iface, async_port_handler_t handler,
+    void *data, port_id_t *port_id)
+{
+	if ((iface & IFACE_MOD_MASK) == IFACE_MOD_CALLBACK)
+		return EINVAL;
+
+	return async_create_port_internal(iface, handler, data, port_id);
+}
+
+void async_set_fallback_port_handler(async_port_handler_t handler, void *data)
+{
+	assert(handler != NULL);
+
+	fallback_port_handler = handler;
+	fallback_port_data = data;
+}
+
+static port_t *async_find_port(iface_t iface, port_id_t port_id)
+{
+	port_t *port = NULL;
+
+	futex_down(&async_futex);
+
+	ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
+	if (link) {
+		interface_t *interface =
+		    hash_table_get_inst(link, interface_t, link);
+
+		link = hash_table_find(&interface->port_hash_table, &port_id);
+		if (link)
+			port = hash_table_get_inst(link, port_t, link);
+	}
+
+	futex_up(&async_futex);
+
+	return port;
+}
+
+async_port_handler_t async_get_port_handler(iface_t iface, port_id_t port_id,
+    void **data)
+{
+	assert(data);
+
+	async_port_handler_t handler = fallback_port_handler;
+	*data = fallback_port_data;
+
+	port_t *port = async_find_port(iface, port_id);
+	if (port) {
+		handler = port->handler;
+		*data = port->data;
+	}
+
+	return handler;
+}
+
+/** Initialize the async framework.
+ *
+ */
+void __async_ports_init(void)
+{
+	if (!hash_table_create(&interface_hash_table, 0, 0,
+	    &interface_hash_table_ops))
+		abort();
+}
