Index: uspace/srv/char/i8042/Makefile
===================================================================
--- uspace/srv/char/i8042/Makefile	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/char/i8042/Makefile	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+include Makefile.common
+
+.PHONY: all clean
+
+all: $(LIBC_PREFIX)/../../../Makefile.config $(LIBC_PREFIX)/../../../config.h $(LIBC_PREFIX)/../../../config.defs $(LIBS)
+	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
+	$(MAKE) -f Makefile.build
+
+clean:
+	rm -f $(DEPEND) $(DEPEND_PREV) $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm
+	find . -name '*.o' -follow -exec rm \{\} \;
Index: uspace/srv/char/i8042/Makefile.build
===================================================================
--- uspace/srv/char/i8042/Makefile.build	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/char/i8042/Makefile.build	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,61 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+## Setup toolchain
+#
+
+include Makefile.common
+include $(LIBC_PREFIX)/Makefile.toolchain
+
+## Sources
+#
+
+SOURCES = \
+	i8042.c
+
+OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
+
+.PHONY: all
+
+all: $(OUTPUT) $(OUTPUT).disasm
+
+-include $(DEPEND)
+
+$(OUTPUT).disasm: $(OUTPUT)
+	$(OBJDUMP) -d $< > $@
+
+$(OUTPUT): $(OBJECTS) $(LIBS)
+	$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
+
+%.o: %.c $(DEPEND)
+	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
+
+$(DEPEND):
+	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
+	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: uspace/srv/char/i8042/Makefile.common
===================================================================
--- uspace/srv/char/i8042/Makefile.common	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/char/i8042/Makefile.common	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2005 Martin Decky
+# 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.
+#
+
+
+## Common names
+#
+
+LIBC_PREFIX = ../../../lib/libc
+SOFTINT_PREFIX = ../../../lib/softint
+LIBS = $(LIBC_PREFIX)/libc.a
+
+DEPEND = Makefile.depend
+DEPEND_PREV = $(DEPEND).prev
+OUTPUT = i8042
Index: uspace/srv/char/i8042/i8042.c
===================================================================
--- uspace/srv/char/i8042/i8042.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/char/i8042/i8042.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,305 @@
+/*
+ * Copyright (c) 2001-2004 Jakub Jermar
+ * Copyright (c) 2006 Josef Cejka
+ * Copyright (c) 2009 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 kbd_port
+ * @ingroup kbd
+ * @{
+ */ 
+/** @file
+ * @brief i8042 PS/2 port driver.
+ */
+
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <devmap.h>
+#include <ipc/ipc.h>
+#include <async.h>
+#include <unistd.h>
+#include <sysinfo.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "i8042.h"
+
+#define NAME "i8042"
+#define NAMESPACE "char"
+
+/* Interesting bits for status register */
+#define i8042_OUTPUT_FULL	0x01
+#define i8042_INPUT_FULL	0x02
+#define i8042_AUX_DATA		0x20
+
+/* Command constants */
+#define i8042_CMD_WRITE_CMDB	0x60	/**< write command byte */
+#define i8042_CMD_WRITE_AUX	0xd4	/**< write aux device */
+
+/* Command byte fields */
+#define i8042_KBD_IE		0x01
+#define i8042_AUX_IE		0x02
+#define i8042_KBD_DISABLE	0x10
+#define i8042_AUX_DISABLE	0x20
+#define i8042_KBD_TRANSLATE	0x40
+
+/* Mouse constants */
+#define MOUSE_OUT_INIT  0xf4
+#define MOUSE_ACK       0xfa
+
+enum {
+	DEVID_PRI = 0, /**< primary device */
+        DEVID_AUX = 1, /**< AUX device */
+	MAX_DEVS  = 2
+};
+
+static irq_cmd_t i8042_cmds[] = {
+	{
+		.cmd = CMD_PIO_READ_8,
+		.addr = NULL,	/* will be patched in run-time */
+		.dstarg = 1
+	},
+	{
+		.cmd = CMD_BTEST,
+		.value = i8042_OUTPUT_FULL,
+		.srcarg = 1,
+		.dstarg = 3
+	},
+	{
+		.cmd = CMD_PREDICATE,
+		.value = 2,
+		.srcarg = 3
+	},
+	{
+		.cmd = CMD_PIO_READ_8,
+		.addr = NULL,	/* will be patched in run-time */
+		.dstarg = 2
+	},
+	{
+		.cmd = CMD_ACCEPT
+	}
+};
+
+static irq_code_t i8042_kbd = {
+	sizeof(i8042_cmds) / sizeof(irq_cmd_t),
+	i8042_cmds
+};
+
+static uintptr_t i8042_physical;
+static uintptr_t i8042_kernel;
+static i8042_t * i8042;
+
+static i8042_port_t i8042_port[MAX_DEVS];
+
+static void wait_ready(void)
+{
+	while (pio_read_8(&i8042->status) & i8042_INPUT_FULL);
+}
+
+static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
+static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall);
+static int i8042_init(void);
+static void i8042_port_write(int devid, uint8_t data);
+
+
+int main(int argc, char *argv[])
+{
+	char name[16];
+	int i, rc;
+	char dchar[MAX_DEVS] = { 'a', 'b' };
+
+	printf(NAME ": i8042 PS/2 port driver\n");
+
+	rc = devmap_driver_register(NAME, i8042_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver.\n");
+		return rc;
+	}
+
+	if (i8042_init() != EOK)
+		return -1;
+
+	for (i = 0; i < MAX_DEVS; i++) {
+		i8042_port[i].client_phone = -1;
+
+		snprintf(name, 16, "%s/ps2%c", NAMESPACE, dchar[i]);
+		rc = devmap_device_register(name, &i8042_port[i].dev_handle);
+		if (rc != EOK) {
+			devmap_hangup_phone(DEVMAP_DRIVER);
+			printf(NAME ": Unable to register device %s.\n", name);
+			return rc;
+		}
+		printf(NAME ": Registered device %s\n", name);
+	}
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached */
+	return 0;
+}
+
+static int i8042_init(void)
+{
+	void *vaddr;
+
+	i8042_physical = sysinfo_value("kbd.address.physical");
+	i8042_kernel = sysinfo_value("kbd.address.kernel");
+	if (pio_enable((void *) i8042_physical, sizeof(i8042_t), &vaddr) != 0)
+		return -1;
+	i8042 = vaddr;
+
+	async_set_interrupt_received(i8042_irq_handler);
+
+	/* Disable kbd, enable mouse */
+	pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
+	wait_ready();
+	pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
+	wait_ready();
+	pio_write_8(&i8042->data, i8042_KBD_DISABLE);
+	wait_ready();
+
+	/* Flush all current IO */
+	while (pio_read_8(&i8042->status) & i8042_OUTPUT_FULL)
+		(void) pio_read_8(&i8042->data);
+
+	i8042_port_write(DEVID_AUX, MOUSE_OUT_INIT);
+
+	i8042_kbd.cmds[0].addr = (void *) &((i8042_t *) i8042_kernel)->status;
+	i8042_kbd.cmds[3].addr = (void *) &((i8042_t *) i8042_kernel)->data;
+	ipc_register_irq(sysinfo_value("kbd.inr"), device_assign_devno(), 0, &i8042_kbd);
+	ipc_register_irq(sysinfo_value("mouse.inr"), device_assign_devno(), 0, &i8042_kbd);
+
+	pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
+	wait_ready();
+	pio_write_8(&i8042->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
+	    i8042_AUX_IE);
+	wait_ready();
+
+	return 0;
+}
+
+/** Character device connection handler */
+static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	void *fs_va = NULL;
+	ipc_callid_t callid;
+	ipc_call_t call;
+	ipcarg_t method;
+	dev_handle_t dh;
+	int flags;
+	int retval;
+	size_t cnt;
+	int dev_id, i;
+
+	printf(NAME ": connection handler\n");
+
+	/* Get the device handle. */
+	dh = IPC_GET_ARG1(*icall);
+
+	/* Determine which disk device is the client connecting to. */
+	dev_id = -1;
+	for (i = 0; i < MAX_DEVS; i++)
+		if (i8042_port[i].dev_handle == dh)
+			dev_id = i;
+
+	if (dev_id < 0/* || disk[dev_id].present == false*/) {
+		ipc_answer_0(iid, EINVAL);
+		return;
+	}
+
+	/* Answer the IPC_M_CONNECT_ME_TO call. */
+	ipc_answer_0(iid, EOK);
+
+	printf(NAME ": accepted connection\n");
+
+	while (1) {
+		callid = async_get_call(&call);
+		method = IPC_GET_METHOD(call);
+		switch (method) {
+		case IPC_M_PHONE_HUNGUP:
+			/* The other side has hung up. */
+			ipc_answer_0(callid, EOK);
+			return;
+		case IPC_M_CONNECT_TO_ME:
+			printf(NAME ": creating callback connection\n");
+			if (i8042_port[dev_id].client_phone != -1) {
+				retval = ELIMIT;
+				break;
+			}
+			i8042_port[dev_id].client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		case IPC_FIRST_USER_METHOD:
+			printf(NAME ": write %d to devid %d\n",
+			    IPC_GET_ARG1(call), dev_id);
+			i8042_port_write(dev_id, IPC_GET_ARG1(call));
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+			break;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+void i8042_port_write(int devid, uint8_t data)
+{
+	if (devid == DEVID_AUX) {
+		pio_write_8(&i8042->status, i8042_CMD_WRITE_AUX);
+		wait_ready();
+	}
+	pio_write_8(&i8042->data, data);
+	wait_ready();
+}
+
+static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
+{
+	int status, data;
+	int devid;
+
+	status = IPC_GET_ARG1(*call);
+	data = IPC_GET_ARG2(*call);
+
+	if ((status & i8042_AUX_DATA)) {
+		devid = DEVID_AUX;
+	} else {
+		devid = DEVID_PRI;
+	}
+
+	if (i8042_port[devid].client_phone != -1) {
+		if (devid == DEVID_PRI)
+		async_msg_1(i8042_port[devid].client_phone,
+		    IPC_FIRST_USER_METHOD, data);
+	}
+}
+
+/**
+ * @}
+ */
Index: uspace/srv/char/i8042/i8042.h
===================================================================
--- uspace/srv/char/i8042/i8042.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/char/i8042/i8042.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006 Josef Cejka
+ * 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 kbd_port
+ * @ingroup  kbd
+ * @{
+ */
+
+/** @file
+ * @brief i8042 port driver.
+ */
+
+#ifndef i8042_H_
+#define i8042_H_
+
+#include <libarch/ddi.h>
+#include <libarch/types.h>
+
+/** i8042 HW I/O interface */
+struct i8042 {
+	ioport8_t data;
+	uint8_t pad[3];
+	ioport8_t status;
+} __attribute__ ((packed));
+typedef struct i8042 i8042_t;
+
+/** Softstate structure, one for each serial port (primary and aux). */
+typedef struct {
+	dev_handle_t dev_handle;
+	int client_phone;
+} i8042_port_t;
+
+#endif
+
+/**
+ * @}
+ */ 
Index: uspace/srv/console/console.c
===================================================================
--- uspace/srv/console/console.c	(revision 3f29834eb51939e4c9b85b216b52106b8ae5f5df)
+++ uspace/srv/console/console.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -37,4 +37,5 @@
 #include <ipc/kbd.h>
 #include <io/keycode.h>
+#include <ipc/mouse.h>
 #include <ipc/fb.h>
 #include <ipc/services.h>
@@ -64,4 +65,7 @@
 /** Phone to the keyboard driver. */
 static int kbd_phone;
+
+/** Phone to the mouse driver. */
+static int mouse_phone;
 
 /** Information about framebuffer */
@@ -426,4 +430,47 @@
 }
 
+/** Handler for mouse events */
+static void mouse_events(ipc_callid_t iid, ipc_call_t *icall)
+{
+	int button, press;
+	int dx, dy;
+	int newcon;
+
+	/* Ignore parameters, the connection is already opened */
+	while (true) {
+
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+
+		int retval;
+
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			/* TODO: Handle hangup */
+			return;
+		case MEVENT_BUTTON:
+			button = IPC_GET_ARG1(call);
+			press = IPC_GET_ARG2(call);
+			if (button == 1) {
+				newcon = gcons_mouse_btn(press);
+				if (newcon != -1)
+					change_console(&consoles[newcon]);
+			}
+			retval = 0;
+			break;
+		case MEVENT_MOVE:
+			dx = IPC_GET_ARG1(call);
+			dy = IPC_GET_ARG2(call);
+			gcons_mouse_move(dx, dy);
+			retval = 0;
+			break;
+		default:
+			retval = ENOENT;
+		}
+
+		ipc_answer_0(callid, retval);
+	}
+}
+
 static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
 {
@@ -675,5 +722,5 @@
 		return false;
 	}
-	
+
 	kbd_phone = fd_phone(input_fd);
 	if (kbd_phone < 0) {
@@ -681,5 +728,5 @@
 		return false;
 	}
-	
+
 	/* NB: The callback connection is slotted for removal */
 	ipcarg_t phonehash;
@@ -688,6 +735,30 @@
 		return false;
 	}
-	
+
 	async_new_connection(phonehash, 0, NULL, keyboard_events);
+
+	/* Connect to mouse device */
+	mouse_phone = -1;
+	int mouse_fd = open("/dev/hid_in/mouse", O_RDONLY);
+
+	if (mouse_fd < 0) {
+		printf(NAME ": Notice - failed opening %s\n", "/dev/hid_in/mouse");
+		goto skip_mouse;
+	}
+
+	mouse_phone = fd_phone(mouse_fd);
+	if (mouse_phone < 0) {
+		printf(NAME ": Failed to connect to mouse device\n");
+		goto skip_mouse;
+	}
+
+	if (ipc_connect_to_me(mouse_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
+		printf(NAME ": Failed to create callback from mouse device\n");
+		mouse_phone = -1;
+		goto skip_mouse;
+	}
+
+	async_new_connection(phonehash, 0, NULL, mouse_events);
+skip_mouse:
 
 	/* Connect to framebuffer driver */
@@ -697,5 +768,5 @@
 		return -1;
 	}
-	
+
 	/* Register driver */
 	int rc = devmap_driver_register(NAME, client_connection);
Index: uspace/srv/kbd/Makefile.build
===================================================================
--- uspace/srv/kbd/Makefile.build	(revision 3f29834eb51939e4c9b85b216b52106b8ae5f5df)
+++ uspace/srv/kbd/Makefile.build	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -75,5 +75,5 @@
 ifeq ($(UARCH),ia32)
 	SOURCES += \
-		port/i8042.c \
+		port/chardev.c \
 		ctl/pc.c
 endif
Index: uspace/srv/kbd/port/chardev.c
===================================================================
--- uspace/srv/kbd/port/chardev.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/kbd/port/chardev.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2009 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 kbd_port
+ * @ingroup kbd
+ * @{
+ */ 
+/** @file
+ * @brief Chardev keyboard port driver.
+ */
+
+#include <ipc/ipc.h>
+#include <ipc/char.h>
+#include <async.h>
+#include <kbd_port.h>
+#include <kbd.h>
+#include <vfs/vfs.h>
+#include <fcntl.h>
+#include <errno.h>
+
+static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall);
+
+static int dev_phone;
+
+#define NAME "kbd"
+
+int kbd_port_init(void)
+{
+	char *input = "/dev/char/ps2a";
+	int input_fd;
+
+	printf(NAME ": open %s\n", input);
+
+	input_fd = open(input, O_RDONLY);
+	if (input_fd < 0) {
+		printf(NAME ": Failed opening %s (%d)\n", input, input_fd);
+		return false;
+	}
+
+	dev_phone = fd_phone(input_fd);
+	if (dev_phone < 0) {
+		printf(NAME ": Failed to connect to device\n");
+		return false;
+	}
+
+	/* NB: The callback connection is slotted for removal */
+	ipcarg_t phonehash;
+	if (ipc_connect_to_me(dev_phone, 0, 0, 0, &phonehash) != 0) {
+		printf(NAME ": Failed to create callback from device\n");
+		return false;
+	}
+
+	async_new_connection(phonehash, 0, NULL, kbd_port_events);
+
+	return 0;
+}
+
+void kbd_port_yield(void)
+{
+}
+
+void kbd_port_reclaim(void)
+{
+}
+
+void kbd_port_write(uint8_t data)
+{
+	async_msg_1(dev_phone, CHAR_WRITE_BYTE, data);
+}
+
+static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall)
+{
+	/* Ignore parameters, the connection is already opened */
+	while (true) {
+
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+
+		int retval;
+
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			/* TODO: Handle hangup */
+			return;
+		case CHAR_NOTIF_BYTE:
+			kbd_push_scancode(IPC_GET_ARG1(call));
+			break;
+		default:
+			retval = ENOENT;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+
+/**
+ * @}
+ */ 
Index: uspace/srv/kbd/port/i8042.c
===================================================================
--- uspace/srv/kbd/port/i8042.c	(revision 3f29834eb51939e4c9b85b216b52106b8ae5f5df)
+++ 	(revision )
@@ -1,183 +1,0 @@
-/*
- * Copyright (c) 2001-2004 Jakub Jermar
- * Copyright (c) 2006 Josef Cejka
- * 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 kbd_port
- * @ingroup kbd
- * @{
- */ 
-/** @file
- * @brief i8042 port driver.
- */
-
-#include <ddi.h>
-#include <libarch/ddi.h>
-#include <ipc/ipc.h>
-#include <async.h>
-#include <unistd.h>
-#include <sysinfo.h>
-#include <kbd_port.h>
-#include <kbd.h>
-#include <ddi.h>
-#include "i8042.h"
-
-/* Interesting bits for status register */
-#define i8042_OUTPUT_FULL  0x1
-#define i8042_INPUT_FULL   0x2
-#define i8042_MOUSE_DATA   0x20
-
-/* Command constants */
-#define i8042_CMD_KBD 0x60
-#define i8042_CMD_MOUSE  0xd4
-
-/* Keyboard cmd byte */
-#define i8042_KBD_IE        0x1
-#define i8042_MOUSE_IE      0x2
-#define i8042_KBD_DISABLE   0x10
-#define i8042_MOUSE_DISABLE 0x20
-#define i8042_KBD_TRANSLATE 0x40
-
-/* Mouse constants */
-#define MOUSE_OUT_INIT  0xf4
-#define MOUSE_ACK       0xfa
-
-static irq_cmd_t i8042_cmds[] = {
-	{
-		.cmd = CMD_PIO_READ_8,
-		.addr = NULL,	/* will be patched in run-time */
-		.dstarg = 1
-	},
-	{
-		.cmd = CMD_BTEST,
-		.value = i8042_OUTPUT_FULL,
-		.srcarg = 1,
-		.dstarg = 3
-	},
-	{
-		.cmd = CMD_PREDICATE,
-		.value = 2,
-		.srcarg = 3
-	},
-	{
-		.cmd = CMD_PIO_READ_8,
-		.addr = NULL,	/* will be patched in run-time */
-		.dstarg = 2
-	},
-	{
-		.cmd = CMD_ACCEPT
-	}
-};
-
-static irq_code_t i8042_kbd = {
-	sizeof(i8042_cmds) / sizeof(irq_cmd_t),
-	i8042_cmds
-};
-
-static uintptr_t i8042_physical;
-static uintptr_t i8042_kernel;
-static i8042_t * i8042;
-
-static void wait_ready(void) {
-	while (pio_read_8(&i8042->status) & i8042_INPUT_FULL)
-		;
-}
-
-static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
-
-int kbd_port_init(void)
-{
-	int mouseenabled = 0;
-	void *vaddr;
-
-	i8042_physical = sysinfo_value("kbd.address.physical");
-	i8042_kernel = sysinfo_value("kbd.address.kernel");
-	if (pio_enable((void *) i8042_physical, sizeof(i8042_t), &vaddr) != 0)
-		return -1;
-	i8042 = vaddr;
-
-	async_set_interrupt_received(i8042_irq_handler);
-
-	/* Disable kbd, enable mouse */
-	pio_write_8(&i8042->status, i8042_CMD_KBD);
-	wait_ready();
-	pio_write_8(&i8042->status, i8042_CMD_KBD);
-	wait_ready();
-	pio_write_8(&i8042->data, i8042_KBD_DISABLE);
-	wait_ready();
-
-	/* Flush all current IO */
-	while (pio_read_8(&i8042->status) & i8042_OUTPUT_FULL)
-		(void) pio_read_8(&i8042->data);
-	
-	/* Enable kbd */
-	i8042_kbd.cmds[0].addr = (void *) &((i8042_t *) i8042_kernel)->status;
-	i8042_kbd.cmds[3].addr = (void *) &((i8042_t *) i8042_kernel)->data;
-	ipc_register_irq(sysinfo_value("kbd.inr"), device_assign_devno(), 0, &i8042_kbd);
-
-	int newcontrol = i8042_KBD_IE | i8042_KBD_TRANSLATE;
-	if (mouseenabled)
-		newcontrol |= i8042_MOUSE_IE;
-	
-	pio_write_8(&i8042->status, i8042_CMD_KBD);
-	wait_ready();
-	pio_write_8(&i8042->data, newcontrol);
-	wait_ready();
-	
-	return 0;
-}
-
-void kbd_port_yield(void)
-{
-}
-
-void kbd_port_reclaim(void)
-{
-}
-
-void kbd_port_write(uint8_t data)
-{
-	pio_write_8(&i8042->data, data);
-	wait_ready();
-}
-
-static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
-{
-	int status = IPC_GET_ARG1(*call);
-
-	if ((status & i8042_MOUSE_DATA))
-		return;
-
-	int scan_code = IPC_GET_ARG2(*call);
-
-	kbd_push_scancode(scan_code);
-	return;
-}
-
-/**
- * @}
- */ 
Index: uspace/srv/kbd/port/i8042.h
===================================================================
--- uspace/srv/kbd/port/i8042.h	(revision 3f29834eb51939e4c9b85b216b52106b8ae5f5df)
+++ 	(revision )
@@ -1,55 +1,0 @@
-/*
- * Copyright (c) 2006 Josef Cejka
- * 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 kbd_port
- * @ingroup  kbd
- * @{
- */
-
-/** @file
- * @brief i8042 port driver.
- */
-
-#ifndef KBD_PORT_i8042_H_
-#define KBD_PORT_i8042_H_
-
-#include <libarch/ddi.h>
-#include <libarch/types.h>
-
-struct i8042 {
-	ioport8_t data;
-	uint8_t pad[3];
-	ioport8_t status;
-} __attribute__ ((packed));
-typedef struct i8042 i8042_t;
-
-#endif
-
-/**
- * @}
- */ 
Index: uspace/srv/mouse/c_mouse/Makefile
===================================================================
--- uspace/srv/mouse/c_mouse/Makefile	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/Makefile	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+include Makefile.common
+
+.PHONY: all clean
+
+all: $(LIBC_PREFIX)/../../../Makefile.config $(LIBC_PREFIX)/../../../config.h $(LIBC_PREFIX)/../../../config.defs $(LIBS)
+	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
+	$(MAKE) -f Makefile.build
+
+clean:
+	rm -f $(DEPEND) $(DEPEND_PREV) $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm
+	find . -name '*.o' -follow -exec rm \{\} \;
Index: uspace/srv/mouse/c_mouse/Makefile.build
===================================================================
--- uspace/srv/mouse/c_mouse/Makefile.build	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/Makefile.build	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,65 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+## Setup toolchain
+#
+
+include Makefile.common
+include $(LIBC_PREFIX)/Makefile.toolchain
+
+CFLAGS += -Iinclude
+
+## Sources
+#
+
+SOURCES = \
+	proto/ps2.c \
+	c_mouse.c \
+	chardev.c
+
+OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
+
+.PHONY: all
+
+all: $(OUTPUT) $(OUTPUT).disasm
+
+-include $(DEPEND)
+
+$(OUTPUT).disasm: $(OUTPUT)
+	$(OBJDUMP) -d $< > $@
+
+$(OUTPUT): $(OBJECTS) $(LIBS)
+	$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
+
+%.o: %.c $(DEPEND)
+	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
+
+$(DEPEND):
+	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
+	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: uspace/srv/mouse/c_mouse/Makefile.common
===================================================================
--- uspace/srv/mouse/c_mouse/Makefile.common	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/Makefile.common	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2005 Martin Decky
+# 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.
+#
+
+
+## Common names
+#
+
+LIBC_PREFIX = ../../../lib/libc
+SOFTINT_PREFIX = ../../../lib/softint
+LIBS = $(LIBC_PREFIX)/libc.a
+
+DEPEND = Makefile.depend
+DEPEND_PREV = $(DEPEND).prev
+OUTPUT = c_mouse
Index: uspace/srv/mouse/c_mouse/c_mouse.c
===================================================================
--- uspace/srv/mouse/c_mouse/c_mouse.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/c_mouse.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2009 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 mouse
+ * @brief Chardev mouse driver.
+ *
+ * This is a common driver for mice attached to simple character devices
+ * (PS/2 mice, serial mice).
+ *
+ * @{
+ */
+/** @file
+ */
+
+#include <ipc/ipc.h>
+#include <ipc/services.h>
+#include <ipc/mouse.h>
+#include <sysinfo.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ipc/ns.h>
+#include <async.h>
+#include <errno.h>
+#include <adt/fifo.h>
+#include <io/console.h>
+#include <io/keycode.h>
+#include <devmap.h>
+
+#include <c_mouse.h>
+#include <mouse_port.h>
+#include <mouse_proto.h>
+
+#define NAME       "mouse"
+#define NAMESPACE  "hid_in"
+
+int client_phone = -1;
+
+void mouse_handle_byte(int byte)
+{
+/*	printf("mouse byte: 0x%x\n", byte);*/
+	mouse_proto_parse_byte(byte);
+}
+
+void mouse_ev_btn(int button, int press)
+{
+/*	printf("ev_btn: button %d, press %d\n", button, press);*/
+	if (client_phone != -1) {
+		async_msg_2(client_phone, MEVENT_BUTTON, button, press);
+	}
+}
+
+void mouse_ev_move(int dx, int dy)
+{
+/*	printf("ev_move: dx %d, dy %d\n", dx, dy);*/
+	if (client_phone != -1)
+		async_msg_2(client_phone, MEVENT_MOVE, dx, dy);
+}
+
+static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	ipc_callid_t callid;
+	ipc_call_t call;
+	int retval;
+
+	ipc_answer_0(iid, EOK);
+
+	while (1) {
+		callid = async_get_call(&call);
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			if (client_phone != -1) {
+				ipc_hangup(client_phone);
+				client_phone = -1;
+			}
+
+			ipc_answer_0(callid, EOK);
+			return;
+		case IPC_M_CONNECT_TO_ME:
+			if (client_phone != -1) {
+				retval = ELIMIT;
+				break;
+			}
+			client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+
+int main(int argc, char **argv)
+{
+	printf(NAME ": Chardev mouse driver\n");
+
+	/* Initialize port. */
+	if (mouse_port_init() != 0)
+		return -1;
+
+	/* Initialize protocol driver. */
+	if (mouse_proto_init() != 0)
+		return -1;
+
+	/* Register driver */
+	int rc = devmap_driver_register(NAME, client_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver (%d)\n", rc);
+		return -1;
+	}
+
+	char dev_path[DEVMAP_NAME_MAXLEN + 1];
+	snprintf(dev_path, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
+
+	dev_handle_t dev_handle;
+	if (devmap_device_register(dev_path, &dev_handle) != EOK) {
+		printf(NAME ": Unable to register device %s\n", dev_path);
+		return -1;
+	}
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached. */
+	return 0;
+}
+
+/**
+ * @}
+ */
Index: uspace/srv/mouse/c_mouse/chardev.c
===================================================================
--- uspace/srv/mouse/c_mouse/chardev.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/chardev.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2009 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 mouse
+ * @{
+ */ 
+/** @file
+ * @brief
+ */
+
+#include <ipc/ipc.h>
+#include <ipc/char.h>
+#include <async.h>
+#include <vfs/vfs.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <c_mouse.h>
+#include <mouse_port.h>
+
+static void chardev_events(ipc_callid_t iid, ipc_call_t *icall);
+
+static int dev_phone;
+
+#define NAME "kbd"
+
+int mouse_port_init(void)
+{
+	char *input = "/dev/char/ps2b";
+	int input_fd;
+
+	printf(NAME ": open %s\n", input);
+
+	input_fd = open(input, O_RDONLY);
+	if (input_fd < 0) {
+		printf(NAME ": Failed opening %s (%d)\n", input, input_fd);
+		return false;
+	}
+
+	dev_phone = fd_phone(input_fd);
+	if (dev_phone < 0) {
+		printf(NAME ": Failed to connect to device\n");
+		return false;
+	}
+
+	/* NB: The callback connection is slotted for removal */
+	ipcarg_t phonehash;
+	if (ipc_connect_to_me(dev_phone, 0, 0, 0, &phonehash) != 0) {
+		printf(NAME ": Failed to create callback from device\n");
+		return false;
+	}
+
+	async_new_connection(phonehash, 0, NULL, chardev_events);
+
+	return 0;
+}
+
+void mouse_port_yield(void)
+{
+}
+
+void mouse_port_reclaim(void)
+{
+}
+
+void mouse_port_write(uint8_t data)
+{
+	async_msg_1(dev_phone, CHAR_WRITE_BYTE, data);
+}
+
+static void chardev_events(ipc_callid_t iid, ipc_call_t *icall)
+{
+	/* Ignore parameters, the connection is already opened */
+	while (true) {
+
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+
+		int retval;
+
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			/* TODO: Handle hangup */
+			return;
+		case IPC_FIRST_USER_METHOD:
+			mouse_handle_byte(IPC_GET_ARG1(call));
+			break;
+		default:
+			retval = ENOENT;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+/**
+ * @}
+ */
Index: uspace/srv/mouse/c_mouse/include/c_mouse.h
===================================================================
--- uspace/srv/mouse/c_mouse/include/c_mouse.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/include/c_mouse.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2009 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 mouse
+ * @brief
+ * @{
+ */
+/** @file
+ */
+
+#ifndef C_MOUSE_H_
+#define C_MOUSE_H_
+
+extern void mouse_handle_byte(int);
+extern void mouse_ev_btn(int button, int press);
+extern void mouse_ev_move(int dx, int dy);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/srv/mouse/c_mouse/include/mouse_port.h
===================================================================
--- uspace/srv/mouse/c_mouse/include/mouse_port.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/include/mouse_port.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2009 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 mouse
+ * @brief
+ * @{
+ */
+/** @file
+ */
+
+#ifndef MOUSE_PORT_H_
+#define MOUSE_PORT_H_
+
+#include <sys/types.h>
+
+extern int mouse_port_init(void);
+extern void mouse_port_yield(void);
+extern void mouse_port_reclaim(void);
+extern void mouse_port_write(uint8_t);
+
+#endif
+
+/**
+ * @}
+ */ 
+
Index: uspace/srv/mouse/c_mouse/include/mouse_proto.h
===================================================================
--- uspace/srv/mouse/c_mouse/include/mouse_proto.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/include/mouse_proto.h	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2009 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 mouse
+ * @brief
+ * @{
+ */
+/** @file
+ */
+
+#ifndef MOUSE_PROTO_H_
+#define MOUSE_PROTO_H_
+
+extern void mouse_proto_parse_byte(int);
+extern int mouse_proto_init(void);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/srv/mouse/c_mouse/proto/ps2.c
===================================================================
--- uspace/srv/mouse/c_mouse/proto/ps2.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
+++ uspace/srv/mouse/c_mouse/proto/ps2.c	(revision 73f56e46be12c092b8b7bb91b34becbcb8535f43)
@@ -0,0 +1,125 @@
+/*
+ * 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.
+ */
+
+/** @addtogroup mouse
+ * @{
+ */
+/**
+ * @file
+ * @brief PS/2 mouse protocol driver.
+ */
+
+#include <stdio.h>
+#include <mouse_proto.h>
+#include <c_mouse.h>
+
+#define BUFSIZE 3
+
+typedef struct {
+	union {
+		unsigned char data[BUFSIZE];
+		struct {
+			unsigned leftbtn : 1;
+			unsigned rightbtn : 1;
+			unsigned middlebtn : 1;
+			unsigned isone : 1; /* Always one */
+			unsigned xsign : 1;
+			unsigned ysign : 1;
+			unsigned xovfl : 1;
+			unsigned yovfl : 1;
+			unsigned char x;
+			unsigned char y;
+		} val;
+	} u;
+} ps2packet_t;
+
+static ps2packet_t buf;
+static int bufpos = 0;
+static int leftbtn = 0;
+static int rightbtn = 0;
+static int middlebtn = 0;
+
+int mouse_proto_init(void)
+{
+	return 0;
+}
+
+/** Convert 9-bit 2-complement signed number to integer */
+static int bit9toint(int sign, unsigned char data)
+{
+	int tmp;
+
+	if (!sign)
+		return data;
+
+	tmp = ((unsigned char)~data) + 1;
+	return -tmp;
+}
+
+/** Process mouse data */
+void mouse_proto_parse_byte(int data)
+{
+	int x, y;
+
+	/* Check that we have not lost synchronization */
+	if (bufpos == 0 && !(data & 0x8))
+		return; /* Synchro lost, ignore byte */
+
+	buf.u.data[bufpos++] = data;
+	if (bufpos == BUFSIZE) {
+		bufpos = 0;
+
+		if (buf.u.val.leftbtn ^ leftbtn) {
+			leftbtn = buf.u.val.leftbtn;
+			mouse_ev_btn(1, leftbtn);
+		}
+
+		if (buf.u.val.rightbtn ^ rightbtn) {
+			rightbtn = buf.u.val.rightbtn;
+			mouse_ev_btn(2, rightbtn);
+		}
+
+		if (buf.u.val.middlebtn ^ middlebtn) {
+			middlebtn = buf.u.val.middlebtn;
+			mouse_ev_btn(3, middlebtn);
+		}
+
+		x =   bit9toint(buf.u.val.xsign, buf.u.val.x);
+		y = - bit9toint(buf.u.val.ysign, buf.u.val.y);
+
+		if (x != 0 || y != 0) {
+			mouse_ev_move(x, y);
+		}
+	}
+
+	return;
+}
+
+/**
+ * @}
+ */
