Index: uspace/lib/libc/include/ipc/dev_iface.h
===================================================================
--- uspace/lib/libc/include/ipc/dev_iface.h	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
+++ uspace/lib/libc/include/ipc/dev_iface.h	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010 Lenka Trochtova
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef LIBC_IPC_DEV_IFACE_H_
+#define LIBC_IPC_DEV_IFACE_H_
+
+#include <ipc/ipc.h>
+
+#define DEV_IFACE_FIRST IPC_FIRST_USER_METHOD
+
+typedef enum {	
+	GENERIC_DEV_IFACE = DEV_IFACE_FIRST,
+	DIRECTLY_ADDRESSABLE_DEV_IFACE,	
+	// TODO add more interfaces
+	DEV_IFACE_MAX
+} dev_inferface_id_t;
+
+
+#define DEV_IFACE_COUNT (DEV_IFACE_MAX - DEV_IFACE_FIRST)
+
+
+
+
+
+
+
+#endif
Index: uspace/lib/libdrv/generic/dev_iface.c
===================================================================
--- uspace/lib/libdrv/generic/dev_iface.c	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
+++ uspace/lib/libdrv/generic/dev_iface.c	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2010 Lenka Trochtova
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @defgroup libdrv generic device driver support.
+ * @brief HelenOS generic device driver support.
+ * @{
+ */
+
+/** @file
+ */
+ 
+ #include "driver.h"
+ 
+ 
+ 
+/**
+ * @}
+ */
Index: uspace/lib/libdrv/generic/driver.c
===================================================================
--- uspace/lib/libdrv/generic/driver.c	(revision eff1a5909c27adfaa321c4725af148a4edce7683)
+++ uspace/lib/libdrv/generic/driver.c	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
@@ -58,5 +58,5 @@
 LIST_INITIALIZE(devices);
 
-static device_t* driver_create_device()
+static device_t * driver_create_device()
 {
 	device_t *dev = (device_t *)malloc(sizeof(device_t));
@@ -65,4 +65,19 @@
 	}	
 	return dev;	
+}
+
+static device_t * driver_get_device(link_t *devices, device_handle_t handle) 
+{	
+	device_t *dev = NULL;
+	link_t *link = devices->next;
+	
+	while (link != devices) {
+		dev = list_get_instance(link, device_t, link);
+		if (handle == dev->handle) {
+			return dev;
+		}
+	}
+	
+	return NULL;
 }
 
@@ -82,5 +97,5 @@
 	printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
 	
-	ipcarg_t r = ipc_answer_1(iid, EOK, ret);
+	ipc_answer_1(iid, EOK, ret);
 }
 
@@ -111,12 +126,58 @@
 }
 
+/** 
+ * Generic client connection handler both for applications and drivers.
+ * 
+ * @param driver true for driver client, false for other clients (applications, services etc.). 
+ */
+static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool driver) {
+	/*
+	 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
+	 */
+	device_handle_t handle = IPC_GET_ARG1(*icall); 
+	device_t *dev = driver_get_device(&devices, handle);
+	
+	if (dev == NULL) {
+		ipc_answer_0(iid, ENOENT);
+		return;
+	}
+	
+	// TODO introduce generic device interface for opening and closing devices
+	// and call its open callback here to find out wheter the device can be used by the connecting client
+		
+
+	ipc_answer_0(iid, EOK);
+	
+	while (1) {
+		ipc_callid_t callid;
+		ipc_call_t call;
+	
+		callid = async_get_call(&call);
+		ipcarg_t method = IPC_GET_METHOD(call);
+		switch  (method) {
+		case IPC_M_PHONE_HUNGUP:
+			// TODO close the device 
+			ipc_answer_0(callid, EOK);
+			return;
+		default:
+			if (DEV_IFACE_FIRST <= method && method < DEV_IFACE_MAX) {
+				// TODO - try to find interface, if supported
+				// otherwise return  ENOTSUP				
+			} else {
+				ipc_answer_0(callid, ENOTSUP);
+			}
+			break;
+		}
+	}
+}
+
 static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
 {
-	// TODO later
+	driver_connection_gen(iid, icall, true); 
 }
 
 static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
 {
-	// TODO later
+	driver_connection_gen(iid, icall, false); 
 }
 
Index: uspace/lib/libdrv/include/dev_iface.h
===================================================================
--- uspace/lib/libdrv/include/dev_iface.h	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
+++ uspace/lib/libdrv/include/dev_iface.h	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010 Lenka Trochtova 
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libdrv
+ * @{
+ */
+/** @file
+ */
+#ifndef LIBDRV_DEV_IFACE_H_
+#define LIBDRV_DEV_IFACE_H_
+
+#include "driver.h"
+
+
+// TODO declare device interface structures here
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/libdrv/include/driver.h
===================================================================
--- uspace/lib/libdrv/include/driver.h	(revision eff1a5909c27adfaa321c4725af148a4edce7683)
+++ uspace/lib/libdrv/include/driver.h	(revision a1769ee69bfe08ce9226b2933a4d000a1218afd1)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Lenka Trochtova 
+ * Copyright (c) 2010 Lenka Trochtova
  * All rights reserved.
  *
@@ -38,18 +38,53 @@
 #include <adt/list.h>
 #include <ipc/devman.h>
+#include <ipc/dev_iface.h>
 
-typedef struct device {
+struct device;
+typedef struct device device_t;
+
+// device interface
+
+// first two parameters: device and interface structure registered by the devices driver
+typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
+typedef remote_iface_func_t *remote_iface_func_ptr_t;
+
+typedef struct {
+	int method_count;
+	remote_iface_func_ptr_t *methods;
+} remote_iface_t;
+
+typedef struct {
+	remote_iface_t * ifaces[DEV_IFACE_COUNT];
+} iface_dipatch_table_t;
+
+static inline int get_iface_index(dev_inferface_id_t id)
+{
+	return id - DEV_IFACE_FIRST;
+}
+
+static inline bool is_valid_iface_id(dev_inferface_id_t id)
+{
+	return DEV_IFACE_FIRST <= id && id < DEV_IFACE_MAX;
+}
+
+// device
+
+struct device {
 	device_handle_t handle;
-	ipcarg_t parent_phone;	
+	ipcarg_t parent_phone;
 	const char *name;
 	match_id_list_t match_ids;
 	void *driver_data;
-	
+	void *interfaces[DEV_IFACE_COUNT];
+
 	// TODO add more items
-	
+
 	link_t link;
-} device_t;
+};
 
-typedef struct driver_ops {	
+
+// driver
+
+typedef struct driver_ops {
 	bool (*add_device)(device_t *dev);
 	// TODO add other generic driver operations
@@ -61,4 +96,8 @@
 } driver_t;
 
+
+
+
+
 int driver_main(driver_t *drv);
 
@@ -68,10 +107,11 @@
 	if (NULL != dev) {
 		memset(dev, 0, sizeof(device_t));
-	}	
+	}
 	list_initialize(&dev->match_ids.ids);
 	return dev;
 }
 
-static inline void delete_device(device_t *dev) {
+static inline void delete_device(device_t *dev)
+{
 	clean_match_ids(&dev->match_ids);
 	if (NULL != dev->name) {
@@ -79,4 +119,12 @@
 	}
 	free(dev);
+}
+
+static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
+{
+	assert(is_valid_iface_id(id));
+
+	int idx = get_iface_index(id);
+	dev->interfaces[idx] = iface;
 }
 
