Index: HelenOS.config
===================================================================
--- HelenOS.config	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ HelenOS.config	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -441,4 +441,7 @@
 ! [(CONFIG_HID_OUT=generic|CONFIG_HID_OUT=serial)&PLATFORM=arm32&MACHINE=gta02] CONFIG_S3C24XX_UART (y/n)
 
+% Support for Samsung S3C24XX on-chip interrupt controller
+! [PLATFORM=arm32&MACHINE=gta02] CONFIG_S3C24XX_IRQC (y)
+
 % Support for Z8530 controller
 ! [(CONFIG_HID_IN=generic|CONFIG_HID_IN=keyboard)&PLATFORM=sparc64&MACHINE=generic] CONFIG_Z8530 (y/n)
@@ -469,5 +472,5 @@
 
 % Serial line input module
-! [CONFIG_DSRLNIN=y|(PLATFORM=ia64&MACHINE=i460GX&CONFIG_NS16550=y)|(PLATFORM=ia64&MACHINE=ski)|(PLATFORM=sparc64&MACHINE=serengeti&CONFIG_SGCN_KBD=y)|(PLATFORM=sparc64&PROCESSOR=sun4v)] CONFIG_SRLN (y)
+! [CONFIG_DSRLNIN=y|(PLATFORM=arm32&MACHINE=gta02)|(PLATFORM=ia64&MACHINE=i460GX&CONFIG_NS16550=y)|(PLATFORM=ia64&MACHINE=ski)|(PLATFORM=sparc64&MACHINE=serengeti&CONFIG_SGCN_KBD=y)|(PLATFORM=sparc64&PROCESSOR=sun4v)] CONFIG_SRLN (y)
 
 % EGA support
@@ -543,2 +546,4 @@
 ! CONFIG_BAREBONE (n/y)
 
+% Line debugging information
+! [CONFIG_STRIP_BINARIES!=y] CONFIG_LINE_DEBUG (n/y)
Index: boot/Makefile
===================================================================
--- boot/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -48,5 +48,5 @@
 endif
 ifeq ($(RDFMT),fat)
-	$(MKFAT) $(DIST_PATH) $@
+	$(MKFAT) 1048576 $(DIST_PATH) $@
 endif
 
Index: boot/Makefile.build
===================================================================
--- boot/Makefile.build	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/Makefile.build	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -61,4 +61,11 @@
 	GCC_CFLAGS += -Werror
 	ICC_CFLAGS += -Werror
+endif
+
+ifeq ($(CONFIG_LINE_DEBUG),y)
+	GCC_CFLAGS += -g
+	ICC_CFLAGS += -g
+	SUNCC_CFLAGS += -g
+	CLANG_CFLAGS += -g
 endif
 
Index: boot/arch/arm32/Makefile.inc
===================================================================
--- boot/arch/arm32/Makefile.inc	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/arch/arm32/Makefile.inc	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,5 +41,7 @@
 PAGE_SIZE = 4096
 
-RD_SRVS_ESSENTIAL +=
+RD_SRVS_ESSENTIAL += \
+	$(USPACE_PATH)/srv/hid/s3c24xx_ts/s3c24ts \
+	$(USPACE_PATH)/srv/hw/char/s3c24xx_uart/s3c24ser
 
 RD_SRVS_NON_ESSENTIAL += \
Index: boot/arch/mips32/src/Makefile
===================================================================
--- boot/arch/mips32/src/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/arch/mips32/src/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -32,9 +32,13 @@
 .PHONY: all clean
 
-all: ../../../../version ../../../../Makefile.config ../../../../config.h ../../../../config.defs
+all: ../../../../version ../../../../Makefile.common ../../../../Makefile.config ../../../../config.h
 	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
 	$(MAKE) -f Makefile.build PRECHECK=$(PRECHECK)
 
 clean:
+	rm -f $(USPACEDIR)/dist/srv/*
+	rm -f $(USPACEDIR)/dist/app/*
+	rm -f $(USPACEDIR)/dist/cfg/net/*
+
 	for file in $(RD_SRVS) ; do \
 		rm -f $(USPACEDIR)/dist/srv/`basename $$file` ; \
@@ -43,4 +47,7 @@
 		rm -f $(USPACEDIR)/dist/app/`basename $$file` ; \
 	done
+	for file in $(NET_CFG) ; do \
+		rm -f $(USPACEDIR)/dist/cfg/net/`basename $$file` ; \
+	done
 	rm -f $(DEPEND) $(DEPEND_PREV) $(JOB) $(OUTPUT) $(RAW) $(COMPS).h $(COMPS).c $(LINK) $(INITRD).img $(INITRD).fs
 	find . -name '*.o' -follow -exec rm \{\} \;
Index: boot/arch/mips32/src/Makefile.build
===================================================================
--- boot/arch/mips32/src/Makefile.build	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/arch/mips32/src/Makefile.build	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -32,6 +32,6 @@
 
 include ../../../../version
+include ../../../../Makefile.common
 include ../../../../Makefile.config
-include ../../../../config.defs
 include Makefile.common
 include Makefile.toolchain
@@ -77,4 +77,8 @@
 
 $(DEPEND):
+	rm -f $(USPACEDIR)/dist/srv/*
+	rm -f $(USPACEDIR)/dist/app/*
+	rm -f $(USPACEDIR)/dist/cfg/net/*
+
 	for file in $(RD_SRVS) ; do \
 		cp $$file $(USPACEDIR)/dist/srv/ ; \
@@ -82,4 +86,7 @@
 	for file in $(RD_APPS) ; do \
 		cp $$file $(USPACEDIR)/dist/app/ ; \
+	done
+	for file in $(NET_CFG) ; do \
+		cp $$file $(USPACEDIR)/dist/cfg/net/ ; \
 	done
 ifeq ($(RDFMT),tmpfs)
Index: boot/arch/mips32/src/Makefile.toolchain
===================================================================
--- boot/arch/mips32/src/Makefile.toolchain	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ boot/arch/mips32/src/Makefile.toolchain	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -27,14 +27,5 @@
 #
 
-## Toolchain configuration
-#
-
-ifndef CROSS_PREFIX
-	CROSS_PREFIX = /usr/local
-endif
-
 BFD_ARCH = mips
-TARGET = mipsel-linux-gnu
-TOOLCHAIN_DIR = $(CROSS_PREFIX)/mips32/bin
 
 JOBFILE = ../../../../tools/jobfile.py
@@ -48,6 +39,4 @@
 	BFD_NAME = elf32-tradbigmips
 	BFD = ecoff-bigmips
-	TOOLCHAIN_DIR = $(CROSS_PREFIX)/mips32eb/bin
-	TARGET = mips-linux-gnu
 endif
 
@@ -55,20 +44,4 @@
 	BFD_NAME = elf32-tradlittlemips
 	BFD = binary
-endif
-
-ifeq ($(COMPILER),gcc_native)
-	CC = gcc
-	AS = as
-	LD = ld
-	OBJCOPY = objcopy
-	OBJDUMP = objdump
-endif
-
-ifeq ($(COMPILER),gcc_cross)
-	CC = $(TOOLCHAIN_DIR)/$(TARGET)-gcc
-	AS = $(TOOLCHAIN_DIR)/$(TARGET)-as
-	LD = $(TOOLCHAIN_DIR)/$(TARGET)-ld
-	OBJCOPY = $(TOOLCHAIN_DIR)/$(TARGET)-objcopy
-	OBJDUMP = $(TOOLCHAIN_DIR)/$(TARGET)-objdump
 endif
 
Index: contrib/conf/ia32-qe.sh
===================================================================
--- contrib/conf/ia32-qe.sh	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ contrib/conf/ia32-qe.sh	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -5,5 +5,5 @@
 # Create a disk image if it does not exist
 if [ ! -f "$DISK_IMG" ]; then
-	tools/mkfat.py uspace/dist/data "$DISK_IMG"
+	tools/mkfat.py 1048576 uspace/dist/data "$DISK_IMG"
 fi
 
Index: contrib/conf/mips32-gx.sh
===================================================================
--- contrib/conf/mips32-gx.sh	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ contrib/conf/mips32-gx.sh	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -5,5 +5,5 @@
 # Create a disk image if it does not exist
 if [ ! -f "$DISK_IMG" ]; then
-	tools/mkfat.py uspace/dist/data "$DISK_IMG"
+	tools/mkfat.py 1048576 uspace/dist/data "$DISK_IMG"
 fi
 
Index: kernel/Makefile
===================================================================
--- kernel/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -120,4 +120,11 @@
 ifeq ($(CONFIG_LTO),y)
 	GCC_CFLAGS += -flto
+endif
+
+ifeq ($(CONFIG_LINE_DEBUG),y)
+	GCC_CFLAGS += -g
+	ICC_CFLAGS += -g
+	SUNCC_CFLAGS += -g
+	CLANG_CFLAGS += -g
 endif
 
@@ -401,5 +408,9 @@
 
 $(DISASM): $(RAW)
+ifeq ($(CONFIG_LINE_DEBUG),y)
+	$(OBJDUMP) -d -S $< > $@
+else
 	$(OBJDUMP) -d $< > $@
+endif
 
 $(RAW): $(LINK) $(ARCH_OBJECTS) $(GENARCH_OBJECTS) $(GENERIC_OBJECTS) $(SYMTAB_OBJECTS)
Index: kernel/arch/amd64/_link.ld.in
===================================================================
--- kernel/arch/amd64/_link.ld.in	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/_link.ld.in	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -53,4 +53,17 @@
 	}
 	
+#ifdef CONFIG_LINE_DEBUG
+	.comment 0 : { *(.comment); }
+	.debug_abbrev 0 : { *(.debug_abbrev); }
+	.debug_aranges 0 : { *(.debug_aranges); }
+	.debug_info 0 : { *(.debug_info); }
+	.debug_line 0 : { *(.debug_line); }
+	.debug_loc 0 : { *(.debug_loc); }
+	.debug_pubnames 0 : { *(.debug_pubnames); }
+	.debug_pubtypes 0 : { *(.debug_pubtypes); }
+	.debug_ranges 0 : { *(.debug_ranges); }
+	.debug_str 0 : { *(.debug_str); }
+#endif
+	
 	/DISCARD/ : {
 		*(*);
Index: kernel/arch/amd64/include/pm.h
===================================================================
--- kernel/arch/amd64/include/pm.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/include/pm.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -65,6 +65,5 @@
 #endif /* CONFIG_FB */
 
-#define gdtselector(des)  ((des) << 3)
-#define idtselector(des)  ((des) << 4)
+#define GDT_SELECTOR(des)  ((des) << 3)
 
 #define PL_KERNEL  0
@@ -168,5 +167,4 @@
 
 extern ptr_16_64_t gdtr;
-extern ptr_16_32_t bootstrap_gdtr;
 extern ptr_16_32_t protected_ap_gdtr;
 
Index: kernel/arch/amd64/src/asm.S
===================================================================
--- kernel/arch/amd64/src/asm.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/asm.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -244,5 +244,5 @@
 	 */
 	xorq %rdx, %rdx
-	cmpq $(gdtselector(KTEXT_DES)), ISTATE_OFFSET_CS(%rsp)
+	cmpq $(GDT_SELECTOR(KTEXT_DES)), ISTATE_OFFSET_CS(%rsp)
 	cmovnzq %rdx, %rbp
 
Index: kernel/arch/amd64/src/boot/boot.S
===================================================================
--- kernel/arch/amd64/src/boot/boot.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/boot/boot.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -85,5 +85,5 @@
 	
 	/* Kernel data + stack */
-	movw $gdtselector(KDATA_DES), %cx
+	movw $GDT_SELECTOR(KDATA_DES), %cx
 	movw %cx, %es
 	movw %cx, %ds
@@ -94,9 +94,9 @@
 	 * when _visible_ part of GS does not point to user-mode segment.
 	 */
-	movw $gdtselector(UDATA_DES), %cx
+	movw $GDT_SELECTOR(UDATA_DES), %cx
 	movw %cx, %fs
 	movw %cx, %gs
 	
-	jmpl $gdtselector(KTEXT32_DES), $multiboot_meeting_point
+	jmpl $GDT_SELECTOR(KTEXT32_DES), $multiboot_meeting_point
 	multiboot_meeting_point:
 	
@@ -182,5 +182,5 @@
 	
 	/* At this point we are in compatibility mode */
-	jmpl $gdtselector(KTEXT_DES), $start64
+	jmpl $GDT_SELECTOR(KTEXT_DES), $start64
 
 /** Print string to EGA display (in light red) and halt.
@@ -645,7 +645,6 @@
 .section K_DATA_START, "aw", @progbits
 
-.global bootstrap_gdtr
 bootstrap_gdtr:
-	.word gdtselector(GDT_ITEMS)
+	.word GDT_SELECTOR(GDT_ITEMS)
 	.long KA2PA(gdt)
 
Index: kernel/arch/amd64/src/boot/vesa_ret.inc
===================================================================
--- kernel/arch/amd64/src/boot/vesa_ret.inc	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/boot/vesa_ret.inc	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -7,5 +7,5 @@
 	
 	/* Kernel data + stack */
-	movw $gdtselector(KDATA_DES), %cx
+	movw $GDT_SELECTOR(KDATA_DES), %cx
 	movw %cx, %es
 	movw %cx, %ds
@@ -17,7 +17,7 @@
 	 */
 	
-	movw $gdtselector(UDATA_DES), %cx
+	movw $GDT_SELECTOR(UDATA_DES), %cx
 	movw %cx, %fs
 	movw %cx, %gs
 	
-	jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
+	jmpl $GDT_SELECTOR(KTEXT32_DES), $vesa_meeting_point
Index: kernel/arch/amd64/src/ddi/ddi.c
===================================================================
--- kernel/arch/amd64/src/ddi/ddi.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/ddi/ddi.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -153,5 +153,5 @@
 	tss_descriptor_t *tss_desc = (tss_descriptor_t *) &gdt_p[TSS_DES];
 	tss_desc->type = AR_TSS;
-	tr_load(gdtselector(TSS_DES));
+	tr_load(GDT_SELECTOR(TSS_DES));
 	
 	/*
Index: kernel/arch/amd64/src/pm.c
===================================================================
--- kernel/arch/amd64/src/pm.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/pm.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -171,5 +171,5 @@
 
 		d->unused = 0;
-		d->selector = gdtselector(KTEXT_DES);
+		d->selector = GDT_SELECTOR(KTEXT_DES);
 
 		d->present = 1;
@@ -291,5 +291,5 @@
 	 * to its own TSS. We just need to load the TR register.
 	 */
-	tr_load(gdtselector(TSS_DES));
+	tr_load(GDT_SELECTOR(TSS_DES));
 }
 
Index: kernel/arch/amd64/src/smp/ap.S
===================================================================
--- kernel/arch/amd64/src/smp/ap.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/smp/ap.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -61,13 +61,13 @@
 	orl $1, %eax
 	movl %eax, %cr0     # switch to protected mode
-	jmpl $gdtselector(KTEXT32_DES), $jump_to_kernel - BOOT_OFFSET + AP_BOOT_OFFSET
+	jmpl $GDT_SELECTOR(KTEXT32_DES), $jump_to_kernel - BOOT_OFFSET + AP_BOOT_OFFSET
 
 jump_to_kernel:
 .code32
-	movw $gdtselector(KDATA_DES), %ax
+	movw $GDT_SELECTOR(KDATA_DES), %ax
 	movw %ax, %ds
 	movw %ax, %es
 	movw %ax, %ss
-	movw $gdtselector(UDATA_DES), %ax
+	movw $GDT_SELECTOR(UDATA_DES), %ax
 	movw %ax, %gs
 	
@@ -94,5 +94,5 @@
 	
 	# At this point we are in compatibility mode
-	jmpl $gdtselector(KTEXT_DES), $start64 - BOOT_OFFSET + AP_BOOT_OFFSET
+	jmpl $GDT_SELECTOR(KTEXT_DES), $start64 - BOOT_OFFSET + AP_BOOT_OFFSET
 
 .code64
Index: kernel/arch/amd64/src/syscall.c
===================================================================
--- kernel/arch/amd64/src/syscall.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/syscall.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -58,6 +58,6 @@
 	 */
 	write_msr(AMD_MSR_STAR,
-	    ((uint64_t)(gdtselector(KDATA_DES) | PL_USER) << 48) |
-	    ((uint64_t)(gdtselector(KTEXT_DES) | PL_KERNEL) << 32));
+	    ((uint64_t) (GDT_SELECTOR(KDATA_DES) | PL_USER) << 48) |
+	    ((uint64_t) (GDT_SELECTOR(KTEXT_DES) | PL_KERNEL) << 32));
 	write_msr(AMD_MSR_LSTAR, (uint64_t)syscall_entry);
 	/* Mask RFLAGS on syscall 
Index: kernel/arch/amd64/src/userspace.c
===================================================================
--- kernel/arch/amd64/src/userspace.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/amd64/src/userspace.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -65,8 +65,8 @@
 		"xorq %%rdi, %%rdi\n"
 		"iretq\n"
-		:: [udata_des] "i" (gdtselector(UDATA_DES) | PL_USER),
+		:: [udata_des] "i" (GDT_SELECTOR(UDATA_DES) | PL_USER),
 		   [stack_size] "r" (kernel_uarg->uspace_stack + THREAD_STACK_SIZE),
 		   [ipl] "r" (ipl),
-		   [utext_des] "i" (gdtselector(UTEXT_DES) | PL_USER),
+		   [utext_des] "i" (GDT_SELECTOR(UTEXT_DES) | PL_USER),
 		   [entry] "r" (kernel_uarg->uspace_entry),
 		   [uarg] "r" (kernel_uarg->uspace_uarg)
Index: kernel/arch/arm32/include/mach/integratorcp/integratorcp.h
===================================================================
--- kernel/arch/arm32/include/mach/integratorcp/integratorcp.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/include/mach/integratorcp/integratorcp.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -105,4 +105,5 @@
 extern void icp_get_memory_extents(uintptr_t *, uintptr_t *);
 extern void icp_frame_init(void);
+extern size_t icp_get_irq_count(void);
 
 extern struct arm_machine_ops icp_machine_ops;
Index: kernel/arch/arm32/include/mach/testarm/testarm.h
===================================================================
--- kernel/arch/arm32/include/mach/testarm/testarm.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/include/mach/testarm/testarm.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -73,4 +73,5 @@
 extern void gxemul_get_memory_extents(uintptr_t *, uintptr_t *);
 extern void gxemul_frame_init(void);
+extern size_t gxemul_get_irq_count(void);
 
 extern struct arm_machine_ops gxemul_machine_ops;
Index: kernel/arch/arm32/include/machine_func.h
===================================================================
--- kernel/arch/arm32/include/machine_func.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/include/machine_func.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -55,4 +55,5 @@
 	void (*machine_output_init)(void);
 	void (*machine_input_init)(void);
+	size_t (*machine_get_irq_count)(void);
 };
 
Index: kernel/arch/arm32/src/mach/gta02/gta02.c
===================================================================
--- kernel/arch/arm32/src/mach/gta02/gta02.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/src/mach/gta02/gta02.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -43,4 +43,6 @@
 #include <genarch/drivers/s3c24xx_irqc/s3c24xx_irqc.h>
 #include <genarch/drivers/s3c24xx_timer/s3c24xx_timer.h>
+#include <genarch/srln/srln.h>
+#include <sysinfo/sysinfo.h>
 #include <interrupt.h>
 #include <ddi/ddi.h>
@@ -68,4 +70,5 @@
 static void gta02_output_init(void);
 static void gta02_input_init(void);
+static size_t gta02_get_irq_count(void);
 
 static void gta02_timer_irq_init(void);
@@ -74,6 +77,6 @@
 static void gta02_timer_irq_handler(irq_t *irq);
 
-static void *gta02_scons_out;
-static s3c24xx_irqc_t *gta02_irqc;
+static outdev_t *gta02_scons_dev;
+static s3c24xx_irqc_t gta02_irqc;
 static s3c24xx_timer_t *gta02_timer;
 
@@ -88,21 +91,17 @@
 	gta02_frame_init,
 	gta02_output_init,
-	gta02_input_init
+	gta02_input_init,
+	gta02_get_irq_count
 };
 
 static void gta02_init(void)
 {
-	gta02_scons_out = (void *) hw_map(GTA02_SCONS_BASE, PAGE_SIZE);
-	gta02_irqc = (void *) hw_map(S3C24XX_IRQC_ADDRESS, PAGE_SIZE);
+	s3c24xx_irqc_regs_t *irqc_regs;
+
 	gta02_timer = (void *) hw_map(S3C24XX_TIMER_ADDRESS, PAGE_SIZE);
-
-	/* Make all interrupt sources use IRQ mode (not FIQ). */
-	pio_write_32(&gta02_irqc->intmod, 0x00000000);
-
-	/* Disable all interrupt sources. */
-	pio_write_32(&gta02_irqc->intmsk, 0xffffffff);
-
-	/* Disable interrupts from all sub-sources. */
-	pio_write_32(&gta02_irqc->intsubmsk, 0xffffffff);
+	irqc_regs = (void *) hw_map(S3C24XX_IRQC_ADDRESS, PAGE_SIZE);
+
+	/* Initialize interrupt controller. */
+	s3c24xx_irqc_init(&gta02_irqc, irqc_regs);
 }
 
@@ -132,5 +131,9 @@
 	uint32_t inum;
 
-	inum = pio_read_32(&gta02_irqc->intoffset);
+	/* Determine IRQ number. */
+	inum = s3c24xx_irqc_inum_get(&gta02_irqc);
+
+	/* Clear interrupt condition in the interrupt controller. */
+	s3c24xx_irqc_clear(&gta02_irqc, inum);
 
 	irq_t *irq = irq_dispatch_and_lock(inum);
@@ -144,8 +147,4 @@
 		    CPU->id, inum);
 	}
-
-	/* Clear interrupt condition in the interrupt controller. */
-	pio_write_32(&gta02_irqc->srcpnd, S3C24XX_INT_BIT(inum));
-	pio_write_32(&gta02_irqc->intpnd, S3C24XX_INT_BIT(inum));
 }
 
@@ -176,13 +175,62 @@
 	}
 #endif
-	outdev_t *scons_dev;
-
-	scons_dev = s3c24xx_uart_init((ioport8_t *) gta02_scons_out);
-	if (scons_dev)
-		stdout_wire(scons_dev);
+
+	/* Initialize serial port of the debugging console. */
+	s3c24xx_uart_io_t *scons_io;
+
+	scons_io = (void *) hw_map(GTA02_SCONS_BASE, PAGE_SIZE);
+	gta02_scons_dev = s3c24xx_uart_init(scons_io, S3C24XX_INT_UART2);
+
+	if (gta02_scons_dev) {
+		/* Create output device. */
+		stdout_wire(gta02_scons_dev);
+	}
+
+	/*
+	 * This is the necessary evil until the userspace driver is entirely
+	 * self-sufficient.
+	 */
+	sysinfo_set_item_val("s3c24xx_uart", NULL, true);
+	sysinfo_set_item_val("s3c24xx_uart.inr", NULL, S3C24XX_INT_UART2);
+	sysinfo_set_item_val("s3c24xx_uart.address.physical", NULL,
+	    (uintptr_t) GTA02_SCONS_BASE);
+
 }
 
 static void gta02_input_init(void)
 {
+	s3c24xx_uart_t *scons_inst;
+
+	if (gta02_scons_dev) {
+		/* Create input device. */
+		scons_inst = (void *) gta02_scons_dev->data;
+
+		srln_instance_t *srln_instance = srln_init();
+		if (srln_instance) {
+			indev_t *sink = stdin_wire();
+			indev_t *srln = srln_wire(srln_instance, sink);
+			s3c24xx_uart_input_wire(scons_inst, srln);
+
+			/* Enable interrupts from UART2 */
+			s3c24xx_irqc_src_enable(&gta02_irqc,
+			    S3C24XX_INT_UART2);
+
+			/* Enable interrupts from UART2 RXD */
+			s3c24xx_irqc_subsrc_enable(&gta02_irqc,
+			    S3C24XX_SUBINT_RXD2);
+		}
+	}
+
+	/* Enable interrupts from ADC */
+	s3c24xx_irqc_src_enable(&gta02_irqc, S3C24XX_INT_ADC);
+
+	/* Enable interrupts from ADC sub-sources */
+	s3c24xx_irqc_subsrc_enable(&gta02_irqc, S3C24XX_SUBINT_ADC_S);
+	s3c24xx_irqc_subsrc_enable(&gta02_irqc, S3C24XX_SUBINT_TC);
+}
+
+size_t gta02_get_irq_count(void)
+{
+	return GTA02_IRQ_COUNT;
 }
 
@@ -248,6 +296,5 @@
 
 	/* Enable interrupts from timer0 */
-	pio_write_32(&gta02_irqc->intmsk, pio_read_32(&gta02_irqc->intmsk) &
-	    ~S3C24XX_INT_BIT(S3C24XX_INT_TIMER0));
+	s3c24xx_irqc_src_enable(&gta02_irqc, S3C24XX_INT_TIMER0);
 
 	/* Load data from tcntb0/tcmpb0 into tcnt0/tcmp0. */
Index: kernel/arch/arm32/src/mach/integratorcp/integratorcp.c
===================================================================
--- kernel/arch/arm32/src/mach/integratorcp/integratorcp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/src/mach/integratorcp/integratorcp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -64,5 +64,6 @@
 	icp_frame_init,
 	icp_output_init,
-	icp_input_init
+	icp_input_init,
+	icp_get_irq_count
 };
 
@@ -336,4 +337,8 @@
 }
 
+size_t icp_get_irq_count(void)
+{
+	return ICP_IRQ_COUNT;
+}
 
 /** @}
Index: kernel/arch/arm32/src/mach/testarm/testarm.c
===================================================================
--- kernel/arch/arm32/src/mach/testarm/testarm.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/src/mach/testarm/testarm.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -64,5 +64,6 @@
 	gxemul_frame_init,
 	gxemul_output_init,
-	gxemul_input_init
+	gxemul_input_init,
+	gxemul_get_irq_count
 };
 
@@ -126,4 +127,9 @@
 }
 
+size_t gxemul_get_irq_count(void)
+{
+	return GXEMUL_IRQ_COUNT;
+}
+
 /** Starts gxemul Real Time Clock device, which asserts regular interrupts.
  *
Index: kernel/arch/arm32/src/machine_func.c
===================================================================
--- kernel/arch/arm32/src/machine_func.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/arm32/src/machine_func.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -128,16 +128,5 @@
 size_t machine_get_irq_count(void)
 {
-	size_t irq_count;
- 
-#if defined(MACHINE_gta02)
-	irq_count = GTA02_IRQ_COUNT;
-#elif defined(MACHINE_testarm)
-	irq_count = GXEMUL_IRQ_COUNT;
-#elif defined(MACHINE_integratorcp)
-	irq_count = ICP_IRQ_COUNT;
-#else
-#error Machine type not defined.
-#endif
-	return irq_count;
+	return (machine_ops->machine_get_irq_count)();
 }
 
Index: kernel/arch/ia32/include/bios/bios.h
===================================================================
--- kernel/arch/ia32/include/bios/bios.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/include/bios/bios.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,6 +38,4 @@
 #include <typedefs.h>
 
-#define BIOS_EBDA_PTR  0x40e
-
 extern uintptr_t ebda;
 
Index: kernel/arch/ia32/include/mm/as.h
===================================================================
--- kernel/arch/ia32/include/mm/as.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/include/mm/as.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -27,5 +27,5 @@
  */
 
-/** @addtogroup ia32mm	
+/** @addtogroup ia32mm
  * @{
  */
Index: kernel/arch/ia32/include/mm/page.h
===================================================================
--- kernel/arch/ia32/include/mm/page.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/include/mm/page.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -27,5 +27,5 @@
  */
 
-/** @addtogroup ia32mm	
+/** @addtogroup ia32mm
  * @{
  */
@@ -106,5 +106,5 @@
 
 /* Set PTE flags accessors for each level. */
-#define SET_PTL1_FLAGS_ARCH(ptl0, i, x)	\
+#define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
 	set_pt_flags((pte_t *) (ptl0), (size_t) (i), (x))
 #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
Index: kernel/arch/ia32/include/pm.h
===================================================================
--- kernel/arch/ia32/include/pm.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/include/pm.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -58,5 +58,5 @@
 #endif /* CONFIG_FB */
 
-#define gdtselector(des)  ((des) << 3)
+#define GDT_SELECTOR(des)  ((des) << 3)
 
 #define PL_KERNEL  0
@@ -153,5 +153,4 @@
 
 extern ptr_16_32_t gdtr;
-extern ptr_16_32_t bootstrap_gdtr;
 extern ptr_16_32_t protected_ap_gdtr;
 extern tss_t *tss_p;
Index: kernel/arch/ia32/src/asm.S
===================================================================
--- kernel/arch/ia32/src/asm.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/asm.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -225,5 +225,5 @@
 	 * Switch to kernel selectors.
 	 */
-	movw $(gdtselector(KDATA_DES)), %ax
+	movw $(GDT_SELECTOR(KDATA_DES)), %ax
 	movw %ax, %ds
 	movw %ax, %es
@@ -304,5 +304,5 @@
 	 * Switch to kernel selectors.
 	 */
-	movl $(gdtselector(KDATA_DES)), %eax
+	movl $(GDT_SELECTOR(KDATA_DES)), %eax
 	movl %eax, %ds
 	movl %eax, %es
@@ -407,5 +407,5 @@
 	 * Switch to kernel selectors.
 	 */
-	movl $(gdtselector(KDATA_DES)), %eax
+	movl $(GDT_SELECTOR(KDATA_DES)), %eax
 	movl %eax, %ds
 	movl %eax, %es
@@ -416,5 +416,5 @@
 	 */
 	xorl %eax, %eax
-	cmpl $(gdtselector(KTEXT_DES)), ISTATE_OFFSET_CS(%esp)
+	cmpl $(GDT_SELECTOR(KTEXT_DES)), ISTATE_OFFSET_CS(%esp)
 	cmovnzl %eax, %ebp
 
Index: kernel/arch/ia32/src/bios/bios.c
===================================================================
--- kernel/arch/ia32/src/bios/bios.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/bios/bios.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -36,4 +36,6 @@
 #include <typedefs.h>
 
+#define BIOS_EBDA_PTR  0x40e
+
 uintptr_t ebda = 0;
 
Index: kernel/arch/ia32/src/boot/boot.S
===================================================================
--- kernel/arch/ia32/src/boot/boot.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/boot/boot.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -78,8 +78,8 @@
 	
 	/* Initialize Global Descriptor Table register */
-	lgdtl KA2PA(bootstrap_gdtr)
+	lgdtl bootstrap_gdtr
 	
 	/* Kernel data + stack */
-	movw $gdtselector(KDATA_DES), %cx
+	movw $GDT_SELECTOR(KDATA_DES), %cx
 	movw %cx, %es
 	movw %cx, %fs
@@ -88,5 +88,5 @@
 	movw %cx, %ss
 	
-	jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point
+	jmpl $GDT_SELECTOR(KTEXT_DES), $multiboot_meeting_point
 	multiboot_meeting_point:
 	
@@ -514,4 +514,8 @@
 page_directory:
 	.space 4096, 0
+
+bootstrap_gdtr:
+	.word GDT_SELECTOR(GDT_ITEMS)
+	.long KA2PA(gdt)
 
 grub_eax:
Index: kernel/arch/ia32/src/boot/vesa_real.inc
===================================================================
--- kernel/arch/ia32/src/boot/vesa_real.inc	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/boot/vesa_real.inc	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -30,5 +30,5 @@
 .code32
 vesa_init:
-	jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
+	jmp $GDT_SELECTOR(VESA_INIT_DES), $vesa_init_real - vesa_init
 
 .code16
@@ -335,5 +335,5 @@
 		vesa_leave_real2:
 		
-			ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protected - vesa_init + VESA_INIT_SEGMENT << 4)
+			ljmpl $GDT_SELECTOR(KTEXT32_DES), $(vesa_init_protected - vesa_init + VESA_INIT_SEGMENT << 4)
 	
 	no_mode:
Index: kernel/arch/ia32/src/boot/vesa_ret.inc
===================================================================
--- kernel/arch/ia32/src/boot/vesa_ret.inc	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/boot/vesa_ret.inc	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -7,5 +7,5 @@
 	
 	/* Kernel data + stack */
-	movw $gdtselector(KDATA_DES), %cx
+	movw $GDT_SELECTOR(KDATA_DES), %cx
 	movw %cx, %es
 	movw %cx, %fs
@@ -14,3 +14,3 @@
 	movw %cx, %ss
 	
-	jmpl $gdtselector(KTEXT_DES), $vesa_meeting_point
+	jmpl $GDT_SELECTOR(KTEXT_DES), $vesa_meeting_point
Index: kernel/arch/ia32/src/ddi/ddi.c
===================================================================
--- kernel/arch/ia32/src/ddi/ddi.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/ddi/ddi.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -153,5 +153,5 @@
 	 */
 	gdt_p[TSS_DES].access = AR_PRESENT | AR_TSS | DPL_KERNEL;
-	tr_load(gdtselector(TSS_DES));
+	tr_load(GDT_SELECTOR(TSS_DES));
 	
 	/*
Index: kernel/arch/ia32/src/mm/frame.c
===================================================================
--- kernel/arch/ia32/src/mm/frame.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/mm/frame.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -131,7 +131,14 @@
 			if (last_frame < ALIGN_UP(new_base + new_size, FRAME_SIZE))
 				last_frame = ALIGN_UP(new_base + new_size, FRAME_SIZE);
-		}
-		
-		if (e820table[i].type == MEMMAP_MEMORY_RESERVED) {
+		} else if ((e820table[i].type == MEMMAP_MEMORY_ACPI) ||
+		    (e820table[i].type == MEMMAP_MEMORY_NVS)) {
+			/* To be safe, make the firmware zone possibly larger */
+			uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
+			uint64_t new_size = ALIGN_UP(size + (base - new_base),
+			    FRAME_SIZE);
+			
+			zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
+			    ZONE_FIRMWARE);
+		} else {
 			/* To be safe, make the reserved zone possibly larger */
 			uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
@@ -141,14 +148,4 @@
 			zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
 			    ZONE_RESERVED);
-		}
-		
-		if (e820table[i].type == MEMMAP_MEMORY_ACPI) {
-			/* To be safe, make the firmware zone possibly larger */
-			uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
-			uint64_t new_size = ALIGN_UP(size + (base - new_base),
-			    FRAME_SIZE);
-			
-			zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
-			    ZONE_FIRMWARE);
 		}
 	}
@@ -203,5 +200,5 @@
 #ifdef CONFIG_SMP
 		/* Reserve AP real mode bootstrap memory */
-		frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH, 
+		frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
 		    (hardcoded_unmapped_ktext_size +
 		    hardcoded_unmapped_kdata_size) >> FRAME_WIDTH);
Index: kernel/arch/ia32/src/pm.c
===================================================================
--- kernel/arch/ia32/src/pm.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/pm.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -75,6 +75,6 @@
 	/* VESA Init descriptor */
 #ifdef CONFIG_FB
-	{ 0xffff, 0, VESA_INIT_SEGMENT>>12, AR_PRESENT | AR_CODE | DPL_KERNEL, 0xf, 0, 0, 0, 0, 0 }
-#endif	
+	{ 0xffff, 0, VESA_INIT_SEGMENT >> 12, AR_PRESENT | AR_CODE | DPL_KERNEL, 0xf, 0, 0, 0, 0, 0 }
+#endif
 };
 
@@ -86,6 +86,8 @@
 
 /* gdtr is changed by kmp before next CPU is initialized */
-ptr_16_32_t bootstrap_gdtr = { .limit = sizeof(gdt), .base = KA2PA((uintptr_t) gdt) };
-ptr_16_32_t gdtr = { .limit = sizeof(gdt), .base = (uintptr_t) gdt };
+ptr_16_32_t gdtr = {
+	.limit = sizeof(gdt),
+	.base = (uintptr_t) gdt
+};
 
 void gdt_setbase(descriptor_t *d, uintptr_t base)
@@ -128,5 +130,5 @@
 
 		d->unused = 0;
-		d->selector = gdtselector(KTEXT_DES);
+		d->selector = GDT_SELECTOR(KTEXT_DES);
 
 		if (i == VECTOR_SYSCALL) {
@@ -283,5 +285,5 @@
 	 * to its own TSS. We just need to load the TR register.
 	 */
-	tr_load(gdtselector(TSS_DES));
+	tr_load(GDT_SELECTOR(TSS_DES));
 	
 	clean_IOPL_NT_flags();    /* Disable I/O on nonprivileged levels and clear NT flag. */
Index: kernel/arch/ia32/src/proc/scheduler.c
===================================================================
--- kernel/arch/ia32/src/proc/scheduler.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/proc/scheduler.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -67,5 +67,5 @@
 	/* Set kernel stack for CPL3 -> CPL0 switch via interrupt */
 	CPU->arch.tss->esp0 = kstk;
-	CPU->arch.tss->ss0 = gdtselector(KDATA_DES);
+	CPU->arch.tss->ss0 = GDT_SELECTOR(KDATA_DES);
 	
 	/* Set up TLS in GS register */
Index: kernel/arch/ia32/src/syscall.c
===================================================================
--- kernel/arch/ia32/src/syscall.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/syscall.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,5 +45,5 @@
 
 	/* set kernel mode CS selector */
-	write_msr(IA32_MSR_SYSENTER_CS, gdtselector(KTEXT_DES));
+	write_msr(IA32_MSR_SYSENTER_CS, GDT_SELECTOR(KTEXT_DES));
 	/* set kernel mode entry point */
 	write_msr(IA32_MSR_SYSENTER_EIP, (uint32_t) sysenter_handler);
Index: kernel/arch/ia32/src/userspace.c
===================================================================
--- kernel/arch/ia32/src/userspace.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/ia32/src/userspace.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -75,11 +75,11 @@
 		"iret\n"
 		:
-		: [udata_des] "i" (gdtselector(UDATA_DES) | PL_USER),
+		: [udata_des] "i" (GDT_SELECTOR(UDATA_DES) | PL_USER),
 		  [stack_size] "r" ((uint8_t *) kernel_uarg->uspace_stack + THREAD_STACK_SIZE),
 		  [ipl] "r" (ipl),
-		  [utext_des] "i" (gdtselector(UTEXT_DES) | PL_USER),
+		  [utext_des] "i" (GDT_SELECTOR(UTEXT_DES) | PL_USER),
 		  [entry] "r" (kernel_uarg->uspace_entry),
 		  [uarg] "r" (kernel_uarg->uspace_uarg),
-		  [tls_des] "r" (gdtselector(TLS_DES))
+		  [tls_des] "r" (GDT_SELECTOR(TLS_DES))
 		: "eax");
 	
Index: kernel/arch/mips32/include/context_offset.h
===================================================================
--- kernel/arch/mips32/include/context_offset.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/include/context_offset.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -60,32 +60,4 @@
 # define OFFSET_F30     0x5c
 #endif /* KERNEL */
-
-/* istate_t */
-#define EOFFSET_AT     0x0
-#define EOFFSET_V0     0x4
-#define EOFFSET_V1     0x8
-#define EOFFSET_A0     0xc
-#define EOFFSET_A1     0x10
-#define EOFFSET_A2     0x14
-#define EOFFSET_A3     0x18
-#define EOFFSET_T0     0x1c
-#define EOFFSET_T1     0x20
-#define EOFFSET_T2     0x24
-#define EOFFSET_T3     0x28
-#define EOFFSET_T4     0x2c
-#define EOFFSET_T5     0x30
-#define EOFFSET_T6     0x34
-#define EOFFSET_T7     0x38
-#define EOFFSET_T8     0x3c
-#define EOFFSET_T9     0x40
-#define EOFFSET_GP     0x44
-#define EOFFSET_SP     0x48
-#define EOFFSET_RA     0x4c
-#define EOFFSET_LO     0x50
-#define EOFFSET_HI     0x54
-#define EOFFSET_STATUS 0x58
-#define EOFFSET_EPC    0x5c
-#define EOFFSET_K1     0x60
-#define REGISTER_SPACE 104	/* respect stack alignment */
 
 #ifdef __ASM__
Index: kernel/arch/mips32/include/exception.h
===================================================================
--- kernel/arch/mips32/include/exception.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/include/exception.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -60,13 +60,17 @@
 
 typedef struct istate {
+	/*
+	 * The first seven registers are arranged so that the istate structure
+	 * can be used both for exception handlers and for the syscall handler.
+	 */
+	uint32_t a0;	/* arg1 */
+	uint32_t a1;	/* arg2 */
+	uint32_t a2;	/* arg3 */
+	uint32_t a3;	/* arg4 */
+	uint32_t t0;	/* arg5 */
+	uint32_t t1;	/* arg6 */
+	uint32_t v0;	/* arg7 */
+	uint32_t v1;
 	uint32_t at;
-	uint32_t v0;
-	uint32_t v1;
-	uint32_t a0;
-	uint32_t a1;
-	uint32_t a2;
-	uint32_t a3;
-	uint32_t t0;
-	uint32_t t1;
 	uint32_t t2;
 	uint32_t t3;
@@ -75,8 +79,19 @@
 	uint32_t t6;
 	uint32_t t7;
+	uint32_t s0;
+	uint32_t s1;
+	uint32_t s2;
+	uint32_t s3;
+	uint32_t s4;
+	uint32_t s5;
+	uint32_t s6;
+	uint32_t s7;
 	uint32_t t8;
 	uint32_t t9;
+	uint32_t kt0;
+	uint32_t kt1;	/* We use it as thread-local pointer */
 	uint32_t gp;
 	uint32_t sp;
+	uint32_t s8;
 	uint32_t ra;
 	
@@ -84,7 +99,8 @@
 	uint32_t hi;
 	
-	uint32_t status;  /* cp0_status */
-	uint32_t epc;     /* cp0_epc */
-	uint32_t k1;      /* We use it as thread-local pointer */
+	uint32_t status;	/* cp0_status */
+	uint32_t epc;		/* cp0_epc */
+
+	uint32_t alignment;	/* to make sizeof(istate_t) a multiple of 8 */
 } istate_t;
 
@@ -108,7 +124,5 @@
 NO_TRACE static inline unative_t istate_get_fp(istate_t *istate)
 {
-	/* FIXME */
-	
-	return 0;
+	return istate->sp;
 }
 
Index: kernel/arch/mips32/include/smp/dorder.h
===================================================================
--- kernel/arch/mips32/include/smp/dorder.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ kernel/arch/mips32/include/smp/dorder.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2007 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.
+ */
+
+/** @addtogroup mips32
+ * @{
+ */
+/** @file
+ */
+
+#ifndef KERN_mips32_DORDER_H_
+#define KERN_mips32_DORDER_H_
+
+#include <typedefs.h>
+
+extern uint32_t dorder_cpuid(void);
+extern void dorder_ipi_ack(uint32_t);
+
+#endif
+
+/** @}
+ */
Index: kernel/arch/mips32/src/debug/stacktrace.c
===================================================================
--- kernel/arch/mips32/src/debug/stacktrace.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/src/debug/stacktrace.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -31,4 +31,32 @@
  */
 /** @file
+ */
+
+/*
+ * This stack tracing code is based on the suggested algorithm described on page
+ * 3-27 and 3-28 of:
+ * 
+ * SYSTEM V
+ * APPLICATION BINARY INTERFACE
+ *
+ * MIPS RISC Processor
+ * Supplement
+ * 3rd Edition
+ *
+ * Unfortunately, GCC generates code which is not entirely compliant with this
+ * method. For example, it places the "jr ra" instruction quite arbitrarily in
+ * the middle of the function which makes the original algorithm unapplicable.
+ *
+ * We deal with this problem by simply not using those parts of the algorithm
+ * that rely on the "jr ra" instruction occurring in the last basic block of a
+ * function, which gives us still usable, but less reliable stack tracer. The
+ * unreliability stems from the fact that under some circumstances it can become
+ * confused and produce incorrect or incomplete stack trace. We apply extra
+ * sanity checks so that the algorithm is still safe and should not crash the
+ * system.
+ *
+ * Even though not perfect, our solution is pretty lightweight, especially when
+ * compared with a prospective alternative solution based on additional
+ * debugging information stored directly in the kernel image.
  */
 
@@ -96,4 +124,10 @@
 extern char ktext_end;
 
+static bool bounds_check(uintptr_t pc)
+{
+	return (pc >= (uintptr_t) &ktext_start) &&
+	    (pc < (uintptr_t) &ktext_end);
+}
+
 static bool
 scan(stack_trace_context_t *ctx, uintptr_t *prev_fp, uintptr_t *prev_ra)
@@ -106,4 +140,6 @@
 	do {
 		inst--;
+		if (!bounds_check((uintptr_t) inst))
+			return false;
 #if 0
 		/*
@@ -180,5 +216,5 @@
 					return false;
 				/* too big offsets are suspicious */
-				if (offset > 32 * 4)
+				if ((size_t) offset > sizeof(istate_t))
 					return false;
 
@@ -207,6 +243,5 @@
 {
 	return !((ctx->fp == 0) || ((ctx->fp % 8) != 0) ||
-	    (ctx->pc % 4 != 0) || (ctx->pc < (uintptr_t) &ktext_start) ||
-	    (ctx->pc >= (uintptr_t) &ktext_end));
+	    (ctx->pc % 4 != 0) || !bounds_check(ctx->pc));
 }
 
Index: kernel/arch/mips32/src/exception.c
===================================================================
--- kernel/arch/mips32/src/exception.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/src/exception.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -74,14 +74,22 @@
 void istate_decode(istate_t *istate)
 {
-	printf("at=%p\tv0=%p\tv1=%p\n", istate->at, istate->v0, istate->v1);
-	printf("a0=%p\ta1=%p\ta2=%p\n", istate->a0, istate->a1, istate->a2);
-	printf("a3=%p\tt0=%p\tt1=%p\n", istate->a3, istate->t0, istate->t1);
-	printf("t2=%p\tt3=%p\tt4=%p\n", istate->t2, istate->t3, istate->t4);
-	printf("t5=%p\tt6=%p\tt7=%p\n", istate->t5, istate->t6, istate->t7);
-	printf("t8=%p\tt9=%p\tgp=%p\n", istate->t8, istate->t9, istate->gp);
-	printf("sp=%p\tra=%p\t\n", istate->sp, istate->ra);
-	printf("lo=%p\thi=%p\t\n", istate->lo, istate->hi);
-	printf("cp0_status=%p\tcp0_epc=%p\tk1=%p\n",
-	    istate->status, istate->epc, istate->k1);
+	printf("epc=%p\tsta=%p\tlo =%p\thi =%p\n",
+	    istate->epc, istate->status, istate->lo, istate->hi);
+	printf("a0 =%p\ta1 =%p\ta2 =%p\ta3 =%p\n",
+	    istate->a0, istate->a1, istate->a2, istate->a3);
+	printf("t0 =%p\tt1 =%p\tt2 =%p\tt3 =%p\n",
+	    istate->t0, istate->t1, istate->t2, istate->t3);
+	printf("t4 =%p\tt5 =%p\tt6 =%p\tt7 =%p\n",
+	    istate->t4, istate->t5, istate->t6, istate->t7);
+	printf("t8 =%p\tt9 =%p\tv0 =%p\tv1 =%p\n",
+	    istate->t8, istate->t9, istate->v0, istate->v1);
+	printf("s0 =%p\ts1 =%p\ts2 =%p\ts3 =%p\n",
+	    istate->s0, istate->s1, istate->s2, istate->s3);
+	printf("s4 =%p\ts5 =%p\ts6 =%p\ts7 =%p\n",
+	    istate->s4, istate->s5, istate->s6, istate->s7);
+	printf("s8 =%p\tat =%p\tkt0=%p\tkt1=%p\n",
+	    istate->s8, istate->at, istate->kt0, istate->kt1);
+	printf("sp =%p\tra =%p\tgp =%p\n",
+	    istate->sp, istate->ra, istate->gp);
 }
 
@@ -97,5 +105,5 @@
 		ASSERT(THREAD);
 		istate->epc += 4;
-		istate->v1 = istate->k1;
+		istate->v1 = istate->kt1;
 	} else
 		unhandled_exception(n, istate);
Index: kernel/arch/mips32/src/interrupt.c
===================================================================
--- kernel/arch/mips32/src/interrupt.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/src/interrupt.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,4 +38,5 @@
 #include <arch.h>
 #include <arch/cp0.h>
+#include <arch/smp/dorder.h>
 #include <time/clock.h>
 #include <ipc/sysipc.h>
@@ -48,4 +49,5 @@
 function virtual_timer_fnc = NULL;
 static irq_t timer_irq;
+static irq_t dorder_irq;
 
 // TODO: This is SMP unsafe!!!
@@ -149,4 +151,14 @@
 }
 
+static irq_ownership_t dorder_claim(irq_t *irq)
+{
+	return IRQ_ACCEPT;
+}
+
+static void dorder_irq_handler(irq_t *irq)
+{
+	dorder_ipi_ack(1 << dorder_cpuid());
+}
+
 /* Initialize basic tables for exception dispatching */
 void interrupt_init(void)
@@ -163,4 +175,13 @@
 	timer_start();
 	cp0_unmask_int(TIMER_IRQ);
+	
+	irq_initialize(&dorder_irq);
+	dorder_irq.devno = device_assign_devno();
+	dorder_irq.inr = DORDER_IRQ;
+	dorder_irq.claim = dorder_claim;
+	dorder_irq.handler = dorder_irq_handler;
+	irq_register(&dorder_irq);
+	
+	cp0_unmask_int(DORDER_IRQ);
 }
 
Index: kernel/arch/mips32/src/smp/dorder.c
===================================================================
--- kernel/arch/mips32/src/smp/dorder.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/src/smp/dorder.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -33,17 +33,29 @@
  */
 
+#include <typedefs.h>
 #include <smp/ipi.h>
+#include <arch/smp/dorder.h>
+
+#define MSIM_DORDER_ADDRESS  0xB0000004
 
 #ifdef CONFIG_SMP
 
-#define MSIM_DORDER_ADDRESS  0xB0000004
-
 void ipi_broadcast_arch(int ipi)
 {
-	*((volatile unsigned int *) MSIM_DORDER_ADDRESS) = 0x7FFFFFFF;
+	*((volatile uint32_t *) MSIM_DORDER_ADDRESS) = 0x7fffffff;
 }
 
 #endif
 
+uint32_t dorder_cpuid(void)
+{
+	return *((volatile uint32_t *) MSIM_DORDER_ADDRESS);
+}
+
+void dorder_ipi_ack(uint32_t mask)
+{
+	*((volatile uint32_t *) (MSIM_DORDER_ADDRESS + 4)) = mask;
+}
+
 /** @}
  */
Index: kernel/arch/mips32/src/start.S
===================================================================
--- kernel/arch/mips32/src/start.S	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/arch/mips32/src/start.S	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -46,8 +46,58 @@
 
 /*
- * Which status bits should are thread-local:
+ * Which status bits are thread-local:
  * KSU(UM), EXL, ERL, IE
  */
 #define REG_SAVE_MASK 0x1f
+
+#define ISTATE_OFFSET_A0	0
+#define ISTATE_OFFSET_A1	4
+#define ISTATE_OFFSET_A2	8
+#define ISTATE_OFFSET_A3	12
+#define ISTATE_OFFSET_T0	16
+#define ISTATE_OFFSET_T1	20
+#define ISTATE_OFFSET_V0	24
+#define ISTATE_OFFSET_V1	28
+#define ISTATE_OFFSET_AT	32
+#define ISTATE_OFFSET_T2	36
+#define ISTATE_OFFSET_T3	40
+#define ISTATE_OFFSET_T4	44
+#define ISTATE_OFFSET_T5	48
+#define ISTATE_OFFSET_T6	52
+#define ISTATE_OFFSET_T7	56
+#define ISTATE_OFFSET_S0	60
+#define ISTATE_OFFSET_S1	64
+#define ISTATE_OFFSET_S2	68
+#define ISTATE_OFFSET_S3	72
+#define ISTATE_OFFSET_S4	76
+#define ISTATE_OFFSET_S5	80
+#define ISTATE_OFFSET_S6	84
+#define ISTATE_OFFSET_S7	88
+#define ISTATE_OFFSET_T8	92
+#define ISTATE_OFFSET_T9	96
+#define ISTATE_OFFSET_KT0	100
+#define ISTATE_OFFSET_KT1	104
+#define ISTATE_OFFSET_GP	108
+#define ISTATE_OFFSET_SP	112
+#define ISTATE_OFFSET_S8	116
+#define ISTATE_OFFSET_RA	120
+#define ISTATE_OFFSET_LO	124
+#define ISTATE_OFFSET_HI	128
+#define ISTATE_OFFSET_STATUS	132
+#define ISTATE_OFFSET_EPC	136
+#define ISTATE_OFFSET_ALIGNMENT	140
+
+#define ISTATE_SOFT_SIZE	144
+
+/*
+ * The fake ABI prologue is never executed and may not be part of the
+ * procedure's body. Instead, it should be immediately preceding the procedure's
+ * body. Its only purpose is to trick the stack trace walker into thinking that
+ * the exception is more or less just a normal function call.
+ */
+.macro FAKE_ABI_PROLOGUE
+	sub $sp, ISTATE_SOFT_SIZE
+	sw $ra, ISTATE_OFFSET_EPC($sp)
+.endm
 
 /*
@@ -58,30 +108,40 @@
  */
 .macro REGISTERS_STORE_AND_EXC_RESET r
-	sw $at, EOFFSET_AT(\r)
-	sw $v0, EOFFSET_V0(\r)
-	sw $v1, EOFFSET_V1(\r)
-	sw $a0, EOFFSET_A0(\r)
-	sw $a1, EOFFSET_A1(\r)
-	sw $a2, EOFFSET_A2(\r)
-	sw $a3, EOFFSET_A3(\r)
-	sw $t0, EOFFSET_T0(\r)
-	sw $t1, EOFFSET_T1(\r)
-	sw $t2, EOFFSET_T2(\r)
-	sw $t3, EOFFSET_T3(\r)
-	sw $t4, EOFFSET_T4(\r)
-	sw $t5, EOFFSET_T5(\r)
-	sw $t6, EOFFSET_T6(\r)
-	sw $t7, EOFFSET_T7(\r)
-	sw $t8, EOFFSET_T8(\r)
-	sw $t9, EOFFSET_T9(\r)
+	sw $at, ISTATE_OFFSET_AT(\r)
+	sw $v0, ISTATE_OFFSET_V0(\r)
+	sw $v1, ISTATE_OFFSET_V1(\r)
+	sw $a0, ISTATE_OFFSET_A0(\r)
+	sw $a1, ISTATE_OFFSET_A1(\r)
+	sw $a2, ISTATE_OFFSET_A2(\r)
+	sw $a3, ISTATE_OFFSET_A3(\r)
+	sw $t0, ISTATE_OFFSET_T0(\r)
+	sw $t1, ISTATE_OFFSET_T1(\r)
+	sw $t2, ISTATE_OFFSET_T2(\r)
+	sw $t3, ISTATE_OFFSET_T3(\r)
+	sw $t4, ISTATE_OFFSET_T4(\r)
+	sw $t5, ISTATE_OFFSET_T5(\r)
+	sw $t6, ISTATE_OFFSET_T6(\r)
+	sw $t7, ISTATE_OFFSET_T7(\r)
+	sw $t8, ISTATE_OFFSET_T8(\r)
+	sw $t9, ISTATE_OFFSET_T9(\r)
+	sw $s0, ISTATE_OFFSET_S0(\r)
+	sw $s1, ISTATE_OFFSET_S1(\r)
+	sw $s2, ISTATE_OFFSET_S2(\r)
+	sw $s3, ISTATE_OFFSET_S3(\r)
+	sw $s4, ISTATE_OFFSET_S4(\r)
+	sw $s5, ISTATE_OFFSET_S5(\r)
+	sw $s6, ISTATE_OFFSET_S6(\r)
+	sw $s7, ISTATE_OFFSET_S7(\r)
+	sw $s8, ISTATE_OFFSET_S8(\r)
 	
 	mflo $at
-	sw $at, EOFFSET_LO(\r)
+	sw $at, ISTATE_OFFSET_LO(\r)
 	mfhi $at
-	sw $at, EOFFSET_HI(\r)
-	
-	sw $gp, EOFFSET_GP(\r)
-	sw $ra, EOFFSET_RA(\r)
-	sw $k1, EOFFSET_K1(\r)
+	sw $at, ISTATE_OFFSET_HI(\r)
+	
+	sw $gp, ISTATE_OFFSET_GP(\r)
+	sw $ra, ISTATE_OFFSET_RA(\r)
+	sw $k0, ISTATE_OFFSET_KT0(\r)
+	sw $k1, ISTATE_OFFSET_KT1(\r)
 	
 	mfc0 $t0, $status
@@ -95,6 +155,6 @@
 	and $t0, $t0, $t3
 	
-	sw $t2, EOFFSET_STATUS(\r)
-	sw $t1, EOFFSET_EPC(\r)
+	sw $t2, ISTATE_OFFSET_STATUS(\r)
+	sw $t1, ISTATE_OFFSET_EPC(\r)
 	mtc0 $t0, $status
 .endm
@@ -106,5 +166,5 @@
 	 */
 	mfc0 $t0, $status
-	lw $t1,EOFFSET_STATUS(\r)
+	lw $t1, ISTATE_OFFSET_STATUS(\r)
 	
 	/* mask UM, EXL, ERL, IE */
@@ -116,35 +176,35 @@
 	mtc0 $t0, $status
 	
-	lw $v0, EOFFSET_V0(\r)
-	lw $v1, EOFFSET_V1(\r)
-	lw $a0, EOFFSET_A0(\r)
-	lw $a1, EOFFSET_A1(\r)
-	lw $a2, EOFFSET_A2(\r)
-	lw $a3, EOFFSET_A3(\r)
-	lw $t0, EOFFSET_T0(\r)
-	lw $t1, EOFFSET_T1(\r)
-	lw $t2, EOFFSET_T2(\r)
-	lw $t3, EOFFSET_T3(\r)
-	lw $t4, EOFFSET_T4(\r)
-	lw $t5, EOFFSET_T5(\r)
-	lw $t6, EOFFSET_T6(\r)
-	lw $t7, EOFFSET_T7(\r)
-	lw $t8, EOFFSET_T8(\r)
-	lw $t9, EOFFSET_T9(\r)
-	
-	lw $gp, EOFFSET_GP(\r)
-	lw $ra, EOFFSET_RA(\r)
-	lw $k1, EOFFSET_K1(\r)
-	
-	lw $at, EOFFSET_LO(\r)
+	lw $v0, ISTATE_OFFSET_V0(\r)
+	lw $v1, ISTATE_OFFSET_V1(\r)
+	lw $a0, ISTATE_OFFSET_A0(\r)
+	lw $a1, ISTATE_OFFSET_A1(\r)
+	lw $a2, ISTATE_OFFSET_A2(\r)
+	lw $a3, ISTATE_OFFSET_A3(\r)
+	lw $t0, ISTATE_OFFSET_T0(\r)
+	lw $t1, ISTATE_OFFSET_T1(\r)
+	lw $t2, ISTATE_OFFSET_T2(\r)
+	lw $t3, ISTATE_OFFSET_T3(\r)
+	lw $t4, ISTATE_OFFSET_T4(\r)
+	lw $t5, ISTATE_OFFSET_T5(\r)
+	lw $t6, ISTATE_OFFSET_T6(\r)
+	lw $t7, ISTATE_OFFSET_T7(\r)
+	lw $t8, ISTATE_OFFSET_T8(\r)
+	lw $t9, ISTATE_OFFSET_T9(\r)
+	
+	lw $gp, ISTATE_OFFSET_GP(\r)
+	lw $ra, ISTATE_OFFSET_RA(\r)
+	lw $k1, ISTATE_OFFSET_KT1(\r)
+	
+	lw $at, ISTATE_OFFSET_LO(\r)
 	mtlo $at
-	lw $at, EOFFSET_HI(\r)
+	lw $at, ISTATE_OFFSET_HI(\r)
 	mthi $at
 	
-	lw $at, EOFFSET_EPC(\r)
+	lw $at, ISTATE_OFFSET_EPC(\r)
 	mtc0 $at, $epc
 	
-	lw $at, EOFFSET_AT(\r)
-	lw $sp, EOFFSET_SP(\r)
+	lw $at, ISTATE_OFFSET_AT(\r)
+	lw $sp, ISTATE_OFFSET_SP(\r)
 .endm
 
@@ -159,5 +219,5 @@
 	
 	beq $k0, $0, 1f
-	add $k0, $sp, 0
+	move $k0, $sp
 	
 	/* move $k0 pointer to kernel stack */
@@ -166,5 +226,5 @@
 	
 	/* move $k0 (supervisor_sp) */
-	lw $k0, 0($k0)
+	lw $k0, ($k0)
 	
 	1:
@@ -202,9 +262,10 @@
 	nop
 
+	FAKE_ABI_PROLOGUE
 exception_handler:
 	KERNEL_STACK_TO_K0
 	
-	sub $k0, REGISTER_SPACE
-	sw $sp, EOFFSET_SP($k0)
+	sub $k0, ISTATE_SOFT_SIZE
+	sw $sp, ISTATE_OFFSET_SP($k0)
 	move $sp, $k0
 	
@@ -227,9 +288,4 @@
 	/* the $sp is automatically restored to former value */
 	eret
-
-#define SS_SP      EOFFSET_SP
-#define SS_STATUS  EOFFSET_STATUS
-#define SS_EPC     EOFFSET_EPC
-#define SS_K1      EOFFSET_K1
 
 /** Syscall entry
@@ -249,9 +305,8 @@
  */
 syscall_shortcut:
-	/* we have a lot of space on the stack, with free use */
 	mfc0 $t3, $epc
 	mfc0 $t2, $status
-	sw $t3, SS_EPC($sp)  /* save EPC */
-	sw $k1, SS_K1($sp)   /* save $k1 not saved on context switch */
+	sw $t3, ISTATE_OFFSET_EPC($sp)  /* save EPC */
+	sw $k1, ISTATE_OFFSET_KT1($sp)  /* save $k1 not saved on context switch */
 	
 	and $t4, $t2, REG_SAVE_MASK  /* save only KSU, EXL, ERL, IE */
@@ -260,20 +315,19 @@
 	ori $t2, $t2, 0x1  /* set IE */
 	
-	sw $t4, SS_STATUS($sp)
+	sw $t4, ISTATE_OFFSET_STATUS($sp)
 	mtc0 $t2, $status
 	
 	/*
 	 * Call the higher level system call handler.
-	 * We are going to reuse part of the unused exception stack frame.
 	 *
 	 */
-	sw $t0, STACK_ARG4($sp)  /* save the 5th argument on the stack */
-	sw $t1, STACK_ARG5($sp)  /* save the 6th argument on the stack */
+	sw $t0, ISTATE_OFFSET_T0($sp)  /* save the 5th argument on the stack */
+	sw $t1, ISTATE_OFFSET_T1($sp)  /* save the 6th argument on the stack */
 	jal syscall_handler
-	sw $v0, STACK_ARG6($sp)  /* save the syscall number on the stack */
+	sw $v0, ISTATE_OFFSET_V0($sp)  /* save the syscall number on the stack */
 	
 	/* restore status */
 	mfc0 $t2, $status
-	lw $t3, SS_STATUS($sp)
+	lw $t3, ISTATE_OFFSET_STATUS($sp)
 	
 	/*
@@ -288,34 +342,36 @@
 	
 	/* restore epc + 4 */
-	lw $t2, SS_EPC($sp)
-	lw $k1, SS_K1($sp)
+	lw $t2, ISTATE_OFFSET_EPC($sp)
+	lw $k1, ISTATE_OFFSET_KT1($sp)
 	addi $t2, $t2, 4
 	mtc0 $t2, $epc
 	
-	lw $sp, SS_SP($sp)  /* restore $sp */
-	eret
-
+	lw $sp, ISTATE_OFFSET_SP($sp)  /* restore $sp */
+	eret
+
+	FAKE_ABI_PROLOGUE
 tlb_refill_handler:
 	KERNEL_STACK_TO_K0
-	sub $k0, REGISTER_SPACE
+	sub $k0, ISTATE_SOFT_SIZE
 	REGISTERS_STORE_AND_EXC_RESET $k0
-	sw $sp,EOFFSET_SP($k0)
-	add $sp, $k0, 0
+	sw $sp, ISTATE_OFFSET_SP($k0)
+	move $sp, $k0
 	
 	jal tlb_refill
-	add $a0, $sp, 0 
+	move $a0, $sp 
 	
 	REGISTERS_LOAD $sp
 	eret
 
+	FAKE_ABI_PROLOGUE
 cache_error_handler:
 	KERNEL_STACK_TO_K0
-	sub $k0, REGISTER_SPACE
+	sub $k0, ISTATE_SOFT_SIZE 
 	REGISTERS_STORE_AND_EXC_RESET $k0
-	sw $sp,EOFFSET_SP($k0)
-	add $sp, $k0, 0
+	sw $sp, ISTATE_OFFSET_SP($k0)
+	move $sp, $k0
 	
 	jal cache_error
-	add $a0, $sp, 0
+	move $a0, $sp
 	
 	REGISTERS_LOAD $sp
@@ -323,7 +379,7 @@
 
 userspace_asm:
-	add $sp, $a0, 0
-	add $v0, $a1, 0
-	add $t9, $a2, 0    /* set up correct entry into PIC code */
+	move $sp, $a0
+	move $v0, $a1
+	move $t9, $a2      /* set up correct entry into PIC code */
 	xor $a0, $a0, $a0  /* $a0 is defined to hold pcb_ptr */
 	                   /* set it to 0 */
Index: kernel/genarch/Makefile.inc
===================================================================
--- kernel/genarch/Makefile.inc	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/genarch/Makefile.inc	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -90,4 +90,9 @@
 endif
 
+ifeq ($(CONFIG_S3C24XX_IRQC),y)
+	GENARCH_SOURCES += \
+		genarch/src/drivers/s3c24xx_irqc/s3c24xx_irqc.c
+endif
+
 ifeq ($(CONFIG_S3C24XX_UART),y)
 	GENARCH_SOURCES += \
Index: kernel/genarch/include/drivers/s3c24xx_irqc/s3c24xx_irqc.h
===================================================================
--- kernel/genarch/include/drivers/s3c24xx_irqc/s3c24xx_irqc.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/genarch/include/drivers/s3c24xx_irqc/s3c24xx_irqc.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -53,5 +53,5 @@
 	ioport32_t subsrcpnd;	/**< Sub source pending */
 	ioport32_t intsubmsk;	/** Interrupt sub mask */
-} s3c24xx_irqc_t;
+} s3c24xx_irqc_regs_t;
 
 /** S3C24xx Interrupt source numbers.
@@ -120,4 +120,16 @@
 #define S3C24XX_SUBINT_BIT(subsource) (1 << (subsource))
 
+typedef struct {
+	s3c24xx_irqc_regs_t *regs;
+} s3c24xx_irqc_t;
+
+extern void s3c24xx_irqc_init(s3c24xx_irqc_t *, s3c24xx_irqc_regs_t *);
+extern unsigned s3c24xx_irqc_inum_get(s3c24xx_irqc_t *);
+extern void s3c24xx_irqc_clear(s3c24xx_irqc_t *, unsigned);
+extern void s3c24xx_irqc_src_enable(s3c24xx_irqc_t *, unsigned);
+extern void s3c24xx_irqc_src_disable(s3c24xx_irqc_t *, unsigned);
+extern void s3c24xx_irqc_subsrc_enable(s3c24xx_irqc_t *, unsigned);
+extern void s3c24xx_irqc_subsrc_disable(s3c24xx_irqc_t *, unsigned);
+
 #endif
 
Index: kernel/genarch/include/drivers/s3c24xx_uart/s3c24xx_uart.h
===================================================================
--- kernel/genarch/include/drivers/s3c24xx_uart/s3c24xx_uart.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/genarch/include/drivers/s3c24xx_uart/s3c24xx_uart.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,8 +38,54 @@
 #define KERN_S3C24XX_UART_H_
 
+#include <ddi/irq.h>
+#include <console/chardev.h>
 #include <typedefs.h>
-#include <console/chardev.h>
 
-extern outdev_t *s3c24xx_uart_init(ioport8_t *);
+/** S3C24xx UART I/O */
+typedef struct {
+	uint32_t ulcon;
+	uint32_t ucon;
+	uint32_t ufcon;
+	uint32_t umcon;
+
+	uint32_t utrstat;
+	uint32_t uerstat;
+	uint32_t ufstat;
+	uint32_t umstat;
+
+	uint32_t utxh;
+	uint32_t urxh;
+
+	uint32_t ubrdiv;
+} s3c24xx_uart_io_t;
+
+/* Bits in UTRSTAT register */
+#define S3C24XX_UTRSTAT_TX_EMPTY	0x4
+#define S3C24XX_UTRSTAT_RDATA		0x1
+
+/* Bits in UFSTAT register */
+#define S3C24XX_UFSTAT_TX_FULL		0x4000
+#define S3C24XX_UFSTAT_RX_FULL		0x0040
+#define S3C24XX_UFSTAT_RX_COUNT		0x002f
+
+/* Bits in UCON register */
+#define UCON_RX_INT_LEVEL		0x100
+
+/* Bits in UFCON register */
+#define UFCON_TX_FIFO_TLEVEL_EMPTY	0x00
+#define UFCON_RX_FIFO_TLEVEL_1B		0x00
+#define UFCON_FIFO_ENABLE		0x01
+
+
+/** S3C24xx UART instance */
+typedef struct {
+	s3c24xx_uart_io_t *io;
+	indev_t *indev;
+	irq_t irq;
+} s3c24xx_uart_t;
+
+extern outdev_t *s3c24xx_uart_init(s3c24xx_uart_io_t *, inr_t inr);
+extern void s3c24xx_uart_input_wire(s3c24xx_uart_t *,
+    indev_t *);
 
 #endif
Index: kernel/genarch/src/drivers/s3c24xx_irqc/s3c24xx_irqc.c
===================================================================
--- kernel/genarch/src/drivers/s3c24xx_irqc/s3c24xx_irqc.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ kernel/genarch/src/drivers/s3c24xx_irqc/s3c24xx_irqc.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2010 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 genarch
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip interrupt controller.
+ *
+ * This IRQC is present on the Samsung S3C24xx CPU (on the gta02 platform).
+ */
+
+#include <genarch/drivers/s3c24xx_irqc/s3c24xx_irqc.h>
+#include <arch/asm.h>
+
+/** Correspondence between interrupt sources and sub-sources. */
+static unsigned s3c24xx_subsrc_src[][2] = {
+	{ S3C24XX_SUBINT_CAM_P, S3C24XX_INT_CAM },
+	{ S3C24XX_SUBINT_CAM_C, S3C24XX_INT_CAM },
+	{ S3C24XX_SUBINT_ADC_S, S3C24XX_INT_ADC },
+	{ S3C24XX_SUBINT_TC, S3C24XX_INT_ADC },
+	{ S3C24XX_SUBINT_ERR2, S3C24XX_INT_UART2 },
+	{ S3C24XX_SUBINT_TXD2, S3C24XX_INT_UART2 },
+	{ S3C24XX_SUBINT_RXD2, S3C24XX_INT_UART2 },
+	{ S3C24XX_SUBINT_ERR1, S3C24XX_INT_UART1 },
+	{ S3C24XX_SUBINT_TXD1, S3C24XX_INT_UART1 },
+	{ S3C24XX_SUBINT_RXD1, S3C24XX_INT_UART1 },
+	{ S3C24XX_SUBINT_ERR0, S3C24XX_INT_UART0 },
+	{ S3C24XX_SUBINT_TXD0, S3C24XX_INT_UART0 },
+	{ S3C24XX_SUBINT_RXD0, S3C24XX_INT_UART0 }
+};
+
+/** Initialize S3C24xx interrupt controller.
+ *
+ * @param irqc	Instance structure
+ * @param regs	Register I/O structure
+ */
+void s3c24xx_irqc_init(s3c24xx_irqc_t *irqc, s3c24xx_irqc_regs_t *regs)
+{
+	irqc->regs = regs;
+
+	/* Make all interrupt sources use IRQ mode (not FIQ). */
+	pio_write_32(&regs->intmod, 0x00000000);
+
+	/* Disable all interrupt sources. */
+	pio_write_32(&regs->intmsk, 0xffffffff);
+
+	/* Disable interrupts from all sub-sources. */
+	pio_write_32(&regs->intsubmsk, 0xffffffff);
+}
+
+/** Obtain number of pending interrupt. */
+unsigned s3c24xx_irqc_inum_get(s3c24xx_irqc_t *irqc)
+{
+	return pio_read_32(&irqc->regs->intoffset);
+}
+
+/** Clear pending interrupt condition including sub-sources.
+ *
+ * Clear source and interrupt pending condition and also automatically clear
+ * any sub-source pending condition pertaining to the source.
+ */
+void s3c24xx_irqc_clear(s3c24xx_irqc_t *irqc, unsigned inum)
+{
+	unsigned src, subsrc;
+	unsigned entries, i;
+
+	entries = sizeof(s3c24xx_subsrc_src) / sizeof(s3c24xx_subsrc_src[0]);
+
+	for (i = 0; i < entries; i++) {
+		subsrc = s3c24xx_subsrc_src[i][0];
+		src = s3c24xx_subsrc_src[i][1];
+
+		if (src == inum) {
+			pio_write_32(&irqc->regs->subsrcpnd,
+			    S3C24XX_SUBINT_BIT(subsrc));
+		}
+	}
+
+	pio_write_32(&irqc->regs->srcpnd, S3C24XX_INT_BIT(inum));
+	pio_write_32(&irqc->regs->intpnd, S3C24XX_INT_BIT(inum));
+}
+
+/** Enable interrupts from the specified source. */
+void s3c24xx_irqc_src_enable(s3c24xx_irqc_t *irqc, unsigned src)
+{
+	pio_write_32(&irqc->regs->intmsk, pio_read_32(&irqc->regs->intmsk) &
+	    ~S3C24XX_INT_BIT(src));
+}
+
+/** Disable interrupts from the specified source. */
+void s3c24xx_irqc_src_disable(s3c24xx_irqc_t *irqc, unsigned src)
+{
+	pio_write_32(&irqc->regs->intmsk, pio_read_32(&irqc->regs->intmsk) |
+	    S3C24XX_INT_BIT(src));
+}
+
+/** Enable interrupts from the specified sub-source. */
+void s3c24xx_irqc_subsrc_enable(s3c24xx_irqc_t *irqc, unsigned subsrc)
+{
+	pio_write_32(&irqc->regs->intsubmsk,
+	    pio_read_32(&irqc->regs->intsubmsk) &
+	    ~S3C24XX_SUBINT_BIT(subsrc));
+}
+
+/** Disable interrupts from the specified sub-source. */
+void s3c24xx_irqc_subsrc_disable(s3c24xx_irqc_t *irqc, unsigned subsrc)
+{
+	pio_write_32(&irqc->regs->intsubmsk,
+	    pio_read_32(&irqc->regs->intsubmsk) |
+	    S3C24XX_SUBINT_BIT(subsrc));
+}
+
+/** @}
+ */
Index: kernel/genarch/src/drivers/s3c24xx_uart/s3c24xx_uart.c
===================================================================
--- kernel/genarch/src/drivers/s3c24xx_uart/s3c24xx_uart.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/genarch/src/drivers/s3c24xx_uart/s3c24xx_uart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,35 +40,21 @@
 #include <genarch/drivers/s3c24xx_uart/s3c24xx_uart.h>
 #include <console/chardev.h>
+#include <console/console.h>
+#include <ddi/device.h>
 #include <arch/asm.h>
 #include <mm/slab.h>
-#include <console/console.h>
 #include <sysinfo/sysinfo.h>
 #include <str.h>
 
-/** S3C24xx UART register offsets */
-#define S3C24XX_UTRSTAT		0x10
-#define S3C24XX_UTXH		0x20
-
-/* Bits in UTXH register */
-#define S3C24XX_UTXH_TX_EMPTY	0x4
-
-typedef struct {
-	ioport8_t *base;
-} s3c24xx_uart_instance_t;
-
 static void s3c24xx_uart_sendb(outdev_t *dev, uint8_t byte)
 {
-	s3c24xx_uart_instance_t *instance =
-	    (s3c24xx_uart_instance_t *) dev->data;
-	ioport32_t *utrstat, *utxh;
+	s3c24xx_uart_t *uart =
+	    (s3c24xx_uart_t *) dev->data;
 
-	utrstat = (ioport32_t *) (instance->base + S3C24XX_UTRSTAT);
-	utxh = (ioport32_t *) (instance->base + S3C24XX_UTXH);
-
-	/* Wait for transmitter to be empty. */
-	while ((pio_read_32(utrstat) & S3C24XX_UTXH_TX_EMPTY) == 0)
+	/* Wait for space becoming available in Tx FIFO. */
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
 		;
 
-	pio_write_32(utxh, byte);
+	pio_write_32(&uart->io->utxh, byte);
 }
 
@@ -86,4 +72,20 @@
 }
 
+static irq_ownership_t s3c24xx_uart_claim(irq_t *irq)
+{
+	return IRQ_ACCEPT;
+}
+
+static void s3c24xx_uart_irq_handler(irq_t *irq)
+{
+	s3c24xx_uart_t *uart = irq->instance;
+
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
+		uint32_t data = pio_read_32(&uart->io->urxh);
+		pio_read_32(&uart->io->uerstat);
+		indev_push_character(uart->indev, data & 0xff);
+	}
+}
+
 static outdev_operations_t s3c24xx_uart_ops = {
 	.write = s3c24xx_uart_putchar,
@@ -91,5 +93,5 @@
 };
 
-outdev_t *s3c24xx_uart_init(ioport8_t *base)
+outdev_t *s3c24xx_uart_init(s3c24xx_uart_io_t *io, inr_t inr)
 {
 	outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
@@ -97,7 +99,7 @@
 		return NULL;
 
-	s3c24xx_uart_instance_t *instance =
-	    malloc(sizeof(s3c24xx_uart_instance_t), FRAME_ATOMIC);
-	if (!instance) {
+	s3c24xx_uart_t *uart =
+	    malloc(sizeof(s3c24xx_uart_t), FRAME_ATOMIC);
+	if (!uart) {
 		free(uart_dev);
 		return NULL;
@@ -105,7 +107,24 @@
 
 	outdev_initialize("s3c24xx_uart_dev", uart_dev, &s3c24xx_uart_ops);
-	uart_dev->data = instance;
+	uart_dev->data = uart;
 
-	instance->base = base;
+	uart->io = io;
+	uart->indev = NULL;
+
+	/* Initialize IRQ structure. */
+	irq_initialize(&uart->irq);
+	uart->irq.devno = device_assign_devno();
+	uart->irq.inr = inr;
+	uart->irq.claim = s3c24xx_uart_claim;
+	uart->irq.handler = s3c24xx_uart_irq_handler;
+	uart->irq.instance = uart;
+
+	/* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
+	pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
+	    UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
+
+	/* Set RX interrupt to pulse mode */
+	pio_write_32(&uart->io->ucon,
+	    pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
 
 	if (!fb_exported) {
@@ -116,5 +135,5 @@
 		sysinfo_set_item_val("fb", NULL, true);
 		sysinfo_set_item_val("fb.kind", NULL, 3);
-		sysinfo_set_item_val("fb.address.physical", NULL, KA2PA(base));
+		sysinfo_set_item_val("fb.address.physical", NULL, KA2PA(io));
 
 		fb_exported = true;
@@ -124,4 +143,13 @@
 }
 
+void s3c24xx_uart_input_wire(s3c24xx_uart_t *uart, indev_t *indev)
+{
+	ASSERT(uart);
+	ASSERT(indev);
+
+	uart->indev = indev;
+	irq_register(&uart->irq);
+}
+
 /** @}
  */
Index: kernel/generic/include/ddi/ddi.h
===================================================================
--- kernel/generic/include/ddi/ddi.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/include/ddi/ddi.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -54,5 +54,4 @@
 extern unative_t sys_physmem_map(unative_t, unative_t, unative_t, unative_t);
 extern unative_t sys_iospace_enable(ddi_ioarg_t *);
-extern unative_t sys_preempt_control(int);
 
 /*
Index: kernel/generic/include/security/cap.h
===================================================================
--- kernel/generic/include/security/cap.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/include/security/cap.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -70,12 +70,7 @@
 
 /**
- * CAP_PREEMPT_CONTROL allows its holder to disable/enable preemption.
- */
-#define CAP_PREEMPT_CONTROL	(1<<3)
-
-/**
  * CAP_IRQ_REG entitles its holder to register IRQ handlers.
  */
-#define CAP_IRQ_REG		(1<<4) 
+#define CAP_IRQ_REG		(1<<3) 
 
 typedef uint32_t cap_t;
Index: kernel/generic/include/syscall/syscall.h
===================================================================
--- kernel/generic/include/syscall/syscall.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/include/syscall/syscall.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -80,5 +80,4 @@
 	SYS_PHYSMEM_MAP,
 	SYS_IOSPACE_ENABLE,
-	SYS_PREEMPT_CONTROL,
 	
 	SYS_SYSINFO_GET_TAG,
Index: kernel/generic/src/ddi/ddi.c
===================================================================
--- kernel/generic/src/ddi/ddi.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/src/ddi/ddi.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -258,27 +258,4 @@
 }
 
-/** Disable or enable preemption.
- *
- * @param enable If non-zero, the preemption counter will be decremented,
- *               leading to potential enabling of preemption. Otherwise
- *               the preemption counter will be incremented, preventing
- *               preemption from occurring.
- *
- * @return Zero on success or EPERM if callers capabilities are not sufficient.
- *
- */
-unative_t sys_preempt_control(int enable)
-{
-	if (!(cap_get(TASK) & CAP_PREEMPT_CONTROL))
-		return EPERM;
-	
-	if (enable)
-		preemption_enable();
-	else
-		preemption_disable();
-	
-	return 0;
-}
-
 /** @}
  */
Index: kernel/generic/src/main/kinit.c
===================================================================
--- kernel/generic/src/main/kinit.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/src/main/kinit.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -208,5 +208,5 @@
 			 */
 			cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
-			    CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
+			    CAP_IO_MANAGER | CAP_IRQ_REG);
 			
 			if (!ipc_phone_0)
Index: kernel/generic/src/mm/frame.c
===================================================================
--- kernel/generic/src/mm/frame.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/src/mm/frame.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -161,6 +161,7 @@
 	for (j = zones.count; j > i; j--) {
 		zones.info[j] = zones.info[j - 1];
-		zones.info[j].buddy_system->data =
-		    (void *) &zones.info[j - 1];
+		if (zones.info[j].buddy_system != NULL)
+			zones.info[j].buddy_system->data =
+			    (void *) &zones.info[j];
 	}
 	
@@ -762,6 +763,7 @@
 	for (i = z2 + 1; i < zones.count; i++) {
 		zones.info[i - 1] = zones.info[i];
-		zones.info[i - 1].buddy_system->data =
-		    (void *) &zones.info[i - 1];
+		if (zones.info[i - 1].buddy_system != NULL)
+			zones.info[i - 1].buddy_system->data =
+			    (void *) &zones.info[i - 1];
 	}
 	
Index: kernel/generic/src/syscall/syscall.c
===================================================================
--- kernel/generic/src/syscall/syscall.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ kernel/generic/src/syscall/syscall.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -159,5 +159,4 @@
 	(syshandler_t) sys_physmem_map,
 	(syshandler_t) sys_iospace_enable,
-	(syshandler_t) sys_preempt_control,
 	
 	/* Sysinfo syscalls */
Index: tools/mkfat.py
===================================================================
--- tools/mkfat.py	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ tools/mkfat.py	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -343,12 +343,18 @@
 def usage(prname):
 	"Print usage syntax"
-	print prname + " <PATH> <IMAGE>"
+	print prname + " <EXTRA_BYTES> <PATH> <IMAGE>"
 
 def main():
-	if (len(sys.argv) < 3):
+	if (len(sys.argv) < 4):
 		usage(sys.argv[0])
 		return
 	
-	path = os.path.abspath(sys.argv[1])
+	if (not sys.argv[1].isdigit()):
+		print "<EXTRA_BYTES> must be a number"
+		return
+	
+	extra_bytes = int(sys.argv[1])
+	
+	path = os.path.abspath(sys.argv[2])
 	if (not os.path.isdir(path)):
 		print "<PATH> must be a directory"
@@ -365,9 +371,9 @@
 	
 	# Make sure the filesystem is large enought for FAT16
-	size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size
+	size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
 	while (size / cluster_size < fat16_clusters):
 		if (cluster_size > sector_size):
 			cluster_size /= 2
-			size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size
+			size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
 		else:
 			size = fat16_clusters * cluster_size + reserved_clusters * cluster_size
@@ -381,5 +387,5 @@
 	data_start = root_start + root_size
 	
-	outf = file(sys.argv[2], "w")
+	outf = file(sys.argv[3], "w")
 	
 	boot_sector = xstruct.create(BOOT_SECTOR)
Index: tools/toolchain.sh
===================================================================
--- tools/toolchain.sh	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ tools/toolchain.sh	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -1,3 +1,3 @@
-#!/bin/bash
+#! /bin/bash
 
 #
@@ -33,4 +33,5 @@
 		echo
 		echo "Script failed: $2"
+		
 		exit 1
 	fi
@@ -45,4 +46,5 @@
 		echo
 		echo "Checksum of ${FILE} does not match."
+		
 		exit 2
 	fi
@@ -71,4 +73,52 @@
 }
 
+change_title() {
+	echo -en "\e]0;$1\a"
+}
+
+show_countdown() {
+	TM="$1"
+	
+	if [ "${TM}" -eq 0 ] ; then
+		echo
+		return 0
+	fi
+	
+	echo -n "${TM} "
+	change_title "${TM}"
+	sleep 1
+	
+	TM="`expr "${TM}" - 1`"
+	show_countdown "${TM}"
+}
+
+show_dependencies() {
+	echo "IMPORTANT NOTICE:"
+	echo
+	echo "For a successful compilation and use of the cross-compiler"
+	echo "toolchain you need at least the following dependencies."
+	echo
+	echo "Please make sure that the dependencies are present in your"
+	echo "system. Otherwise the compilation process might fail after"
+	echo "a few seconds or minutes."
+	echo
+	echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
+	echo " - gettext, zlib, Texinfo, libelf, libgomp"
+	echo " - GNU Multiple Precision Library (GMP)"
+	echo " - GNU Make"
+	echo " - GNU tar"
+	echo " - GNU Coreutils"
+	echo " - GNU Sharutils"
+	echo " - MPFR"
+	echo " - MPC"
+	echo " - Parma Polyhedra Library (PPL)"
+	echo " - ClooG-PPL"
+	echo " - native C compiler, assembler and linker"
+	echo " - native C library with headers"
+	echo
+	
+	show_countdown 10
+}
+
 download_check() {
 	SOURCE="$1"
@@ -77,4 +127,5 @@
 	
 	if [ ! -f "${FILE}" ]; then
+		change_title "Downloading ${FILE}"
 		wget -c "${SOURCE}${FILE}"
 		check_error $? "Error downloading ${FILE}."
@@ -88,4 +139,5 @@
 	
 	if [ -d "${DIR}" ]; then
+		change_title "Removing ${DIR}"
 		echo " >>> Removing ${DIR}"
 		rm -fr "${DIR}"
@@ -97,4 +149,5 @@
 	DESC="$2"
 	
+	change_title "Creating ${DESC}"
 	echo ">>> Creating ${DESC}"
 	
@@ -108,5 +161,6 @@
 	DESC="$2"
 	
-	echo " >>> ${DESC}"
+	change_title "Unpacking ${DESC}"
+	echo " >>> Unpacking ${DESC}"
 	
 	tar -xjf "${FILE}"
@@ -142,5 +196,5 @@
 	
 	BINUTILS_VERSION="2.20"
-	GCC_VERSION="4.5.0"
+	GCC_VERSION="4.5.1"
 	
 	BINUTILS="binutils-${BINUTILS_VERSION}.tar.bz2"
@@ -165,7 +219,7 @@
 	echo ">>> Downloading tarballs"
 	download_check "${BINUTILS_SOURCE}" "${BINUTILS}" "ee2d3e996e9a2d669808713360fa96f8"
-	download_check "${GCC_SOURCE}" "${GCC_CORE}" "58eda33c3184303628f91c42a7ab15b5"
-	download_check "${GCC_SOURCE}" "${GCC_OBJC}" "8d8c01b6631b020cc6c167860fde2398"
-	download_check "${GCC_SOURCE}" "${GCC_CPP}" "5ab93605af40def4844eda09ca769c2d"
+	download_check "${GCC_SOURCE}" "${GCC_CORE}" "dc8959e31b01a65ce10d269614815054"
+	download_check "${GCC_SOURCE}" "${GCC_OBJC}" "3c11b7037896e967eddf8178af2ddd98"
+	download_check "${GCC_SOURCE}" "${GCC_CPP}" "b294953ff0bb2f20c7acb2bf005d832a"
 	
 	echo ">>> Removing previous content"
@@ -184,18 +238,26 @@
 	unpack_tarball "${GCC_CPP}" "C++"
 	
-	echo ">>> Compiling and installing binutils"
+	echo ">>> Processing binutils (${PLATFORM})"
 	cd "${BINUTILSDIR}"
 	check_error $? "Change directory failed."
 	patch_binutils "${PLATFORM}"
-	./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" "--disable-nls"
+	
+	change_title "binutils: configure (${PLATFORM})"
+	./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --disable-nls
 	check_error $? "Error configuring binutils."
+	
+	change_title "binutils: make (${PLATFORM})"
 	make all install
 	check_error $? "Error compiling/installing binutils."
 	
-	echo ">>> Compiling and installing GCC"
+	echo ">>> Processing GCC (${PLATFORM})"
 	cd "${OBJDIR}"
 	check_error $? "Change directory failed."
+	
+	change_title "GCC: configure (${PLATFORM})"
 	"${GCCDIR}/configure" "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --with-gnu-as --with-gnu-ld --disable-nls --disable-threads --enable-languages=c,objc,c++,obj-c++ --disable-multilib --disable-libgcj --without-headers --disable-shared --enable-lto
 	check_error $? "Error configuring GCC."
+	
+	change_title "GCC: make (${PLATFORM})"
 	PATH="${PATH}:${PREFIX}/bin" make all-gcc install-gcc
 	check_error $? "Error compiling/installing GCC."
@@ -216,4 +278,6 @@
 	show_usage
 fi
+
+show_dependencies
 
 case "$1" in
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -66,7 +66,9 @@
 	srv/hid/adb_mouse \
 	srv/hid/char_mouse \
+	srv/hid/s3c24xx_ts \
 	srv/hid/fb \
 	srv/hid/kbd \
 	srv/hw/char/i8042 \
+	srv/hw/char/s3c24xx_uart \
 	srv/hw/netif/dp8390 \
 	srv/net/cfg \
Index: uspace/Makefile.common
===================================================================
--- uspace/Makefile.common	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/Makefile.common	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -172,5 +172,9 @@
 ifneq ($(BINARY),)
 %.disasm: $(BINARY)
+ifeq ($(CONFIG_LINE_DEBUG),y)
+	$(OBJDUMP) -d -S $< > $@
+else
 	$(OBJDUMP) -d $< > $@
+endif
 
 $(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBS) $(BASE_LIBS)
Index: uspace/app/init/init.c
===================================================================
--- uspace/app/init/init.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/init/init.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -274,6 +274,8 @@
 	srv_start("/srv/cuda_adb");
 	srv_start("/srv/i8042");
+	srv_start("/srv/s3c24ser");
 	srv_start("/srv/adb_ms");
 	srv_start("/srv/char_ms");
+	srv_start("/srv/s3c24ts");
 	
 	spawn("/srv/fb");
Index: uspace/app/klog/klog.c
===================================================================
--- uspace/app/klog/klog.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/klog/klog.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -43,11 +43,15 @@
 #include <event.h>
 #include <errno.h>
+#include <str_error.h>
 #include <io/klog.h>
 
-#define NAME  "klog"
+#define NAME       "klog"
+#define LOG_FNAME  "/log/klog"
 
 /* Pointer to klog area */
 static wchar_t *klog;
 static size_t klog_length;
+
+static FILE *log;
 
 static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
@@ -58,6 +62,17 @@
 	size_t i;
 	
-	for (i = klog_len - klog_stored; i < klog_len; i++)
-		putchar(klog[(klog_start + i) % klog_length]);
+	for (i = klog_len - klog_stored; i < klog_len; i++) {
+		wchar_t ch = klog[(klog_start + i) % klog_length];
+		
+		putchar(ch);
+		
+		if (log != NULL)
+			fputc(ch, log);
+	}
+	
+	if (log != NULL) {
+		fflush(log);
+		fsync(fileno(log));
+	}
 }
 
@@ -91,4 +106,14 @@
 	}
 	
+	/*
+	 * Mode "a" would be definitively much better here, but it is
+	 * not well supported by the FAT driver.
+	 *
+	 */
+	log = fopen(LOG_FNAME, "w");
+	if (log == NULL)
+		printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
+		    str_error(errno));
+	
 	async_set_interrupt_received(interrupt_received);
 	klog_update();
Index: uspace/app/netecho/netecho.c
===================================================================
--- uspace/app/netecho/netecho.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/netecho/netecho.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,4 +41,5 @@
 #include <task.h>
 #include <arg_parse.h>
+#include <err.h>
 
 #include <in.h>
@@ -46,5 +47,4 @@
 #include <inet.h>
 #include <socket.h>
-#include <net_err.h>
 #include <socket_parse.h>
 
Index: uspace/app/netecho/print_error.c
===================================================================
--- uspace/app/netecho/print_error.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/netecho/print_error.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -36,7 +36,7 @@
 
 #include <stdio.h>
+#include <errno.h>
 
 #include <icmp_codes.h>
-#include <socket_errno.h>
 
 #include "print_error.h"
Index: uspace/app/nettest1/nettest.c
===================================================================
--- uspace/app/nettest1/nettest.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/nettest1/nettest.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -36,7 +36,7 @@
 
 #include <stdio.h>
+#include <err.h>
 
 #include <socket.h>
-#include <net_err.h>
 
 #include "nettest.h"
Index: uspace/app/nettest1/nettest1.c
===================================================================
--- uspace/app/nettest1/nettest1.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/nettest1/nettest1.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,4 +41,5 @@
 #include <time.h>
 #include <arg_parse.h>
+#include <err.h>
 
 #include <in.h>
@@ -46,5 +47,4 @@
 #include <inet.h>
 #include <socket.h>
-#include <net_err.h>
 #include <socket_parse.h>
 
Index: uspace/app/nettest2/nettest2.c
===================================================================
--- uspace/app/nettest2/nettest2.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/nettest2/nettest2.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,4 +41,5 @@
 #include <time.h>
 #include <arg_parse.h>
+#include <err.h>
 
 #include <in.h>
@@ -46,5 +47,4 @@
 #include <inet.h>
 #include <socket.h>
-#include <net_err.h>
 #include <socket_parse.h>
 
Index: uspace/app/ping/ping.c
===================================================================
--- uspace/app/ping/ping.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/ping/ping.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,4 +42,5 @@
 #include <ipc/services.h>
 #include <str_error.h>
+#include <errno.h>
 #include <arg_parse.h>
 
@@ -49,5 +50,4 @@
 #include <inet.h>
 #include <ip_codes.h>
-#include <socket_errno.h>
 #include <socket_parse.h>
 
Index: uspace/app/trace/syscalls.c
===================================================================
--- uspace/app/trace/syscalls.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/app/trace/syscalls.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -73,5 +73,4 @@
     [SYS_PHYSMEM_MAP] = { "physmem_map",		4,	V_ERRNO },
     [SYS_IOSPACE_ENABLE] = { "iospace_enable",		1,	V_ERRNO },
-    [SYS_PREEMPT_CONTROL] = { "preempt_control",	1,	V_ERRNO },
 
     [SYS_SYSINFO_GET_TAG] = { "sysinfo_get_tag",		2,	V_INTEGER },
Index: uspace/lib/c/Makefile
===================================================================
--- uspace/lib/c/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -84,8 +84,12 @@
 	generic/ipc.c \
 	generic/async.c \
+	generic/async_rel.c \
 	generic/loader.c \
 	generic/getopt.c \
 	generic/adt/list.o \
 	generic/adt/hash_table.o \
+	generic/adt/dynamic_fifo.c \
+	generic/adt/measured_strings.c \
+	generic/adt/char_map.c \
 	generic/time.c \
 	generic/err.c \
Index: pace/lib/c/Makefile.toolchain
===================================================================
--- uspace/lib/c/Makefile.toolchain	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,128 +1,0 @@
-#
-# 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.
-#
-
-GCC_CFLAGS = -I$(LIBC_PREFIX)/include -O3 -imacros $(LIBC_PREFIX)/../../../config.h \
-	-fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
-	-finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
-	-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
-	-Werror-implicit-function-declaration -pipe -g -D__$(ENDIANESS)__
-
-ICC_CFLAGS = -I$(LIBC_PREFIX)/include -O3 -imacros $(LIBC_PREFIX)/../../../config.h \
-	-fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
-	-finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
-	-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
-	-Werror-implicit-function-declaration -pipe -g -D__$(ENDIANESS)__
-
-CLANG_CFLAGS = -I$(LIBC_PREFIX)/include -O3 -imacros $(LIBC_PREFIX)/../../../config.h \
-	-fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
-	-finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
-	-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
-	-Werror-implicit-function-declaration -pipe -g -arch $(CLANG_ARCH) \
-	-D__$(ENDIANESS)__
-
-LFLAGS = -M -N $(SOFTINT_PREFIX)/libsoftint.a
-AFLAGS =
-
-## Cross-toolchain prefix
-#
-
-ifndef CROSS_PREFIX
-	CROSS_PREFIX = /usr/local
-endif
-
-## Setup platform configuration
-#
-
--include $(LIBC_PREFIX)/../../../Makefile.config
--include $(LIBC_PREFIX)/../../../config.defs
--include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.inc
-
-## Simple detection of the host system
-#
-HOST = $(shell uname)
-
-## On Solaris, some utilities have different names
-#
-ifeq ($(HOST),SunOS)
-	BINUTILS_PREFIX = "g"
-else
-	BINUTILS_PREFIX = ""
-endif
-
-## Toolchain configuration
-#
-
-JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
-
-ifeq ($(COMPILER),gcc_cross)
-	CC = $(TOOLCHAIN_DIR)/$(TARGET)-gcc
-	GCC = $(CC)
-	AS = $(TOOLCHAIN_DIR)/$(TARGET)-as
-	LD = $(TOOLCHAIN_DIR)/$(TARGET)-ld
-	AR = $(TOOLCHAIN_DIR)/$(TARGET)-ar
-	OBJCOPY = $(TOOLCHAIN_DIR)/$(TARGET)-objcopy
-	OBJDUMP = $(TOOLCHAIN_DIR)/$(TARGET)-objdump
-	CFLAGS = $(GCC_CFLAGS)
-	DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
-endif
-
-ifeq ($(COMPILER),gcc_native)
-	CC = gcc
-	GCC = $(CC)
-	AS = $(BINUTILS_PREFIX)as
-	LD = $(BINUTILS_PREFIX)ld
-	AR = $(BINUTILS_PREFIX)ar
-	OBJCOPY = $(BINUTILS_PREFIX)objcopy
-	OBJDUMP = $(BINUTILS_PREFIX)objdump
-	CFLAGS = $(GCC_CFLAGS)
-	DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
-endif
-
-ifeq ($(COMPILER),icc)
-	CC = icc
-	GCC = gcc
-	AS = as
-	LD = ld
-	AR = ar
-	OBJCOPY = objcopy
-	OBJDUMP = objdump
-	CFLAGS = $(ICC_CFLAGS)
-	DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
-endif
-
-ifeq ($(COMPILER),clang)
-	CC = clang
-	GCC = gcc
-	AS = $(BINUTILS_PREFIX)as
-	LD = $(BINUTILS_PREFIX)ld
-	AR = $(BINUTILS_PREFIX)ar
-	OBJCOPY = $(BINUTILS_PREFIX)objcopy
-	OBJDUMP = $(BINUTILS_PREFIX)objdump
-	CFLAGS = $(CLANG_CFLAGS)
-	DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
-endif
Index: pace/lib/c/arch/amd64/include/limits.h
===================================================================
--- uspace/lib/c/arch/amd64/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,46 +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 libcamd64
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_amd64_LIMITS_H_
-#define LIBC_amd64_LIMITS_H_
-
-# define LONG_MIN MIN_INT64
-# define LONG_MAX MAX_INT64
-# define ULONG_MIN MIN_UINT64
-# define ULONG_MAX MAX_UINT64
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/arm32/include/limits.h
===================================================================
--- uspace/lib/c/arch/arm32/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,47 +1,0 @@
-/*
- * Copyright (c) 2007 Michal Kebrt
- * 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 libcarm32	
- * @{
- */
-/** @file 
- *  @brief Limits declarations.
- */
-
-#ifndef LIBC_arm32__LIMITS_H_
-#define LIBC_arm32__LIMITS_H_
-
-#define LONG_MIN MIN_INT32
-#define LONG_MAX MAX_INT32
-#define ULONG_MIN MIN_UINT32
-#define ULONG_MAX MAX_UINT32
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/ia32/include/limits.h
===================================================================
--- uspace/lib/c/arch/ia32/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,46 +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 libcia32
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_ia32__LIMITS_H_
-#define LIBC_ia32__LIMITS_H_
-
-# define LONG_MIN MIN_INT32
-# define LONG_MAX MAX_INT32
-# define ULONG_MIN MIN_UINT32
-# define ULONG_MAX MAX_UINT32
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/ia64/include/limits.h
===================================================================
--- uspace/lib/c/arch/ia64/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,46 +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 libcia64	
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_ia64_LIMITS_H_
-#define LIBC_ia64_LIMITS_H_
-
-# define LONG_MIN MIN_INT64
-# define LONG_MAX MAX_INT64
-# define ULONG_MIN MIN_UINT64
-# define ULONG_MAX MAX_UINT64
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/mips32/include/limits.h
===================================================================
--- uspace/lib/c/arch/mips32/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,47 +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 libcmips32	
- * @{
- */
-/** @file
- * @ingroup libcmips32eb	
- */
-
-#ifndef LIBC_mips32__LIMITS_H_
-#define LIBC_mips32__LIMITS_H_
-
-# define LONG_MIN MIN_INT32
-# define LONG_MAX MAX_INT32
-# define ULONG_MIN MIN_UINT32
-# define ULONG_MAX MAX_UINT32
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/mips32eb/include/limits.h
===================================================================
--- uspace/lib/c/arch/mips32eb/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,1 +1,0 @@
-../../mips32/include/limits.h
Index: pace/lib/c/arch/ppc32/include/limits.h
===================================================================
--- uspace/lib/c/arch/ppc32/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,46 +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 libcppc32	
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_ppc32_LIMITS_H_
-#define LIBC_ppc32_LIMITS_H_
-
-#define LONG_MIN MIN_INT32
-#define LONG_MAX MAX_INT32
-#define ULONG_MIN MIN_UINT32
-#define ULONG_MAX MAX_UINT32
-
-#endif
-
-/** @}
- */
Index: pace/lib/c/arch/sparc64/include/limits.h
===================================================================
--- uspace/lib/c/arch/sparc64/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,46 +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 libcsparc64	
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_sparc64_LIMITS_H_
-#define LIBC_sparc64_LIMITS_H_
-
-#define LONG_MIN MIN_INT64
-#define LONG_MAX MAX_INT64
-#define ULONG_MIN MIN_UINT64
-#define ULONG_MAX MAX_UINT64
-
-#endif
-
-/** @}
- */
Index: uspace/lib/c/generic/adt/char_map.c
===================================================================
--- uspace/lib/c/generic/adt/char_map.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/generic/adt/char_map.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,347 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ *  Character string to integer map implementation.
+ *  @see char_map.h
+ */
+
+#include <adt/char_map.h>
+
+#include <errno.h>
+#include <malloc.h>
+#include <mem.h>
+#include <unistd.h>
+
+/** Internal magic value for a consistency check. */
+#define CHAR_MAP_MAGIC_VALUE	0x12345611
+
+/** Adds the value with the key to the map.
+ *
+ * Creates new nodes to map the key.
+ *
+ * @param[in,out] map	The character string to integer map.
+ * @param[in] identifier The key zero ('\0') terminated character string.
+ *			The key character string is processed until the first
+ *			terminating zero ('\0') character after the given
+ *			length is found.
+ * @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\0') character is found.
+ * @param[in] value	The integral value to be stored for the key character
+ *			string.
+ * @returns		EOK on success.
+ * @returns		ENOMEM if there is not enough memory left.
+ * @returns		EEXIST if the key character string is already used.
+ */
+static int
+char_map_add_item(char_map_ref map, const char *identifier, size_t length,
+    const int value)
+{
+	if (map->next == (map->size - 1)) {
+		char_map_ref *tmp;
+
+		tmp = (char_map_ref *) realloc(map->items,
+		    sizeof(char_map_ref) * 2 * map->size);
+		if (!tmp)
+			return ENOMEM;
+
+		map->size *= 2;
+		map->items = tmp;
+	}
+
+	map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t));
+	if (!map->items[map->next])
+		return ENOMEM;
+
+	if (char_map_initialize(map->items[map->next]) != EOK) {
+		free(map->items[map->next]);
+		map->items[map->next] = NULL;
+		return ENOMEM;
+	}
+
+	map->items[map->next]->c = * identifier;
+	++ identifier;
+	++ map->next;
+	if ((length > 1) || ((length == 0) && (*identifier))) {
+		map->items[map->next - 1]->value = CHAR_MAP_NULL;
+		return char_map_add_item(map->items[map->next - 1], identifier,
+		    length ? length - 1 : 0, value);
+	} else {
+		map->items[map->next - 1]->value = value;
+	}
+
+	return EOK;
+}
+
+/** Checks if the map is valid.
+ *
+ * @param[in] map	The character string to integer map.
+ * @returns		TRUE if the map is valid.
+ * @returns		FALSE otherwise.
+ */
+static int char_map_is_valid(const char_map_ref map)
+{
+	return map && (map->magic == CHAR_MAP_MAGIC_VALUE);
+}
+
+/** Adds the value with the key to the map.
+ *
+ * @param[in,out] map	The character string to integer map.
+ * @param[in] identifier The key zero ('\0') terminated character string. The
+ *			key character string is processed until the first
+ *			terminating zero ('\0') character after the given
+ *			length is found.
+ * @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\\0') character is found.
+ * @param[in] value	The integral value to be stored for the key character
+ *			string.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the map is not valid.
+ * @returns		EINVAL if the identifier parameter is NULL.
+ * @returns		EINVAL if the length parameter zero (0) and the
+ *			identifier parameter is an empty character string (the
+ *			first character is the terminating zero ('\0')
+ *			character.
+ * @returns		EEXIST if the key character string is already used.
+ * @returns		Other error codes as defined for the
+ *			char_map_add_item() function.
+ */
+int
+char_map_add(char_map_ref map, const char *identifier, size_t length,
+    const int value)
+{
+	if (char_map_is_valid(map) && (identifier) &&
+	    ((length) || (*identifier))) {
+		int index;
+
+		for (index = 0; index < map->next; ++ index) {
+			if (map->items[index]->c != *identifier)
+				continue;
+				
+			++ identifier;
+			if((length > 1) || ((length == 0) && (*identifier))) {
+				return char_map_add(map->items[index],
+				    identifier, length ? length - 1 : 0, value);
+			} else {
+				if (map->items[index]->value != CHAR_MAP_NULL)
+					return EEXISTS;
+
+				map->items[index]->value = value;
+				return EOK;
+			}
+		}
+		return char_map_add_item(map, identifier, length, value);
+	}
+
+	return EINVAL;
+}
+
+/** Clears and destroys the map.
+ *
+ * @param[in,out] map	The character string to integer map.
+ */
+void char_map_destroy(char_map_ref map)
+{
+	if (char_map_is_valid(map)) {
+		int index;
+
+		map->magic = 0;
+		for (index = 0; index < map->next; ++index)
+			char_map_destroy(map->items[index]);
+
+		free(map->items);
+		map->items = NULL;
+	}
+}
+
+/** Returns the node assigned to the key from the map.
+ *
+ * @param[in] map	The character string to integer map.
+ * @param[in] identifier The key zero ('\0') terminated character string. The
+ *			key character string is processed until the first
+ *			terminating zero ('\0') character after the given length
+ *			is found.
+ * @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\0') character is found.
+ * @returns		The node holding the integral value assigned to the key
+ *			character string.
+ * @returns		NULL if the key is not assigned a node.
+ */
+static char_map_ref
+char_map_find_node(const char_map_ref map, const char *identifier,
+    size_t length)
+{
+	if (!char_map_is_valid(map))
+		return NULL;
+
+	if (length || (*identifier)) {
+		int index;
+
+		for (index = 0; index < map->next; ++index) {
+			if (map->items[index]->c == *identifier) {
+				++identifier;
+				if (length == 1)
+					return map->items[index];
+
+				return char_map_find_node(map->items[index],
+				    identifier, length ? length - 1 : 0);
+			}
+		}
+
+		return NULL;
+	}
+
+	return map;
+}
+
+/** Excludes the value assigned to the key from the map.
+ *
+ * The entry is cleared from the map.
+ *
+ * @param[in,out] map	The character string to integer map.
+ * @param[in] identifier The key zero ('\0') terminated character string. The
+ *			key character string is processed until the first
+ *			terminating zero ('\0') character after the given length
+ *			is found.
+ * @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\0') character is found.
+ * @returns		The integral value assigned to the key character string.
+ * @returns		CHAR_MAP_NULL if the key is not assigned a value.
+ */
+int char_map_exclude(char_map_ref map, const char *identifier, size_t length)
+{
+	char_map_ref node;
+
+	node = char_map_find_node(map, identifier, length);
+	if (node) {
+		int value;
+
+		value = node->value;
+		node->value = CHAR_MAP_NULL;
+		return value;
+	}
+	return CHAR_MAP_NULL;
+}
+
+/** Returns the value assigned to the key from the map.
+ *
+ *  @param[in] map	The character string to integer map.
+ *  @param[in] identifier The key zero ('\0') terminated character string. The
+ *			key character string is processed until the first
+ *			terminating zero ('\0') character after the given length
+ *			is found.
+ *  @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\0') character is found.
+ *  @returns		The integral value assigned to the key character string.
+ *  @returns		CHAR_MAP_NULL if the key is not assigned a value.
+ */
+int char_map_find(const char_map_ref map, const char *identifier, size_t length)
+{
+	char_map_ref node;
+
+	node = char_map_find_node(map, identifier, length);
+	return node ? node->value : CHAR_MAP_NULL;
+}
+
+/** Initializes the map.
+ *
+ *  @param[in,out] map	The character string to integer map.
+ *  @returns		EOK on success.
+ *  @returns		EINVAL if the map parameter is NULL.
+ *  @returns		ENOMEM if there is not enough memory left.
+ */
+int char_map_initialize(char_map_ref map)
+{
+	if (!map)
+		return EINVAL;
+
+	map->c = '\0';
+	map->value = CHAR_MAP_NULL;
+	map->size = 2;
+	map->next = 0;
+
+	map->items = malloc(sizeof(char_map_ref) * map->size);
+	if (!map->items) {
+		map->magic = 0;
+		return ENOMEM;
+	}
+
+	map->items[map->next] = NULL;
+	map->magic = CHAR_MAP_MAGIC_VALUE;
+
+	return EOK;
+}
+
+/** Adds or updates the value with the key to the map.
+ *
+ *  @param[in,out] map	The character string to integer map.
+ *  @param[in] identifier The key zero ('\0') terminated character string. The
+ *			key character string is processed until the first
+ *			terminating zero ('\0') character after the given length
+ *			is found.
+ *  @param[in] length	The key character string length. The parameter may be
+ *			zero (0) which means that the string is processed until
+ *			the terminating zero ('\0') character is found.
+ *  @param[in] value	The integral value to be stored for the key character
+ *			string.
+ *  @returns		EOK on success.
+ *  @returns		EINVAL if the map is not valid.
+ *  @returns		EINVAL if the identifier parameter is NULL.
+ *  @returns		EINVAL if the length parameter zero (0) and the
+ *			identifier parameter is an empty character string (the
+ *			first character is the terminating zero ('\0) character.
+ *  @returns		EEXIST if the key character string is already used.
+ *  @returns		Other error codes as defined for the char_map_add_item()
+ *			function.
+ */
+int
+char_map_update(char_map_ref map, const char *identifier, const size_t length,
+    const int value)
+{
+	char_map_ref node;
+
+	node = char_map_find_node(map, identifier, length);
+	if (node) {
+		node->value = value;
+		return EOK;
+	}
+	
+	return char_map_add(map, identifier, length, value);
+}
+
+/** @}
+ */
Index: uspace/lib/c/generic/adt/dynamic_fifo.c
===================================================================
--- uspace/lib/c/generic/adt/dynamic_fifo.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/generic/adt/dynamic_fifo.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ * Dynamic first in first out positive integer queue implementation.
+ */
+
+#include <errno.h>
+#include <malloc.h>
+#include <mem.h>
+
+#include <adt/dynamic_fifo.h>
+
+/** Internal magic value for a consistency check. */
+#define DYN_FIFO_MAGIC_VALUE	0x58627659
+
+/** Returns the next queue index.
+ *
+ * The queue field is circular.
+ *
+ * @param[in] fifo	The dynamic queue.
+ * @param[in] index	The actual index to be shifted.
+ */
+#define NEXT_INDEX(fifo, index)	(((index) + 1) % ((fifo)->size + 1))
+
+/** Checks if the queue is valid.
+ *
+ * @param[in] fifo	The dynamic queue.
+ * @returns		TRUE if the queue is valid.
+ * @returns		FALSE otherwise.
+ */
+static int dyn_fifo_is_valid(dyn_fifo_ref fifo)
+{
+	return fifo && (fifo->magic_value == DYN_FIFO_MAGIC_VALUE);
+}
+
+/** Initializes the dynamic queue.
+ *
+ * @param[in,out] fifo	The dynamic queue.
+ * @param[in] size	The initial queue size.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the queue is not valid.
+ * @returns		EBADMEM if the fifo parameter is NULL.
+ * @returns		ENOMEM if there is not enough memory left.
+ */
+int dyn_fifo_initialize(dyn_fifo_ref fifo, int size)
+{
+	if (!fifo)
+		return EBADMEM;
+
+	if (size <= 0)
+		return EINVAL;
+
+	fifo->items = (int *) malloc(sizeof(int) * size + 1);
+	if (!fifo->items)
+		return ENOMEM;
+
+	fifo->size = size;
+	fifo->head = 0;
+	fifo->tail = 0;
+	fifo->magic_value = DYN_FIFO_MAGIC_VALUE;
+
+	return EOK;
+}
+
+/** Appends a new item to the queue end.
+ *
+ * @param[in,out] fifo	The dynamic queue.
+ * @param[in] value	The new item value. Should be positive.
+ * @param[in] max_size	The maximum queue size. The queue is not resized beyound
+ *			this limit. May be zero or negative to indicate no
+ *			limit.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the queue is not valid.
+ * @returns		ENOMEM if there is not enough memory left.
+ */
+int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size)
+{
+	int * new_items;
+
+	if (!dyn_fifo_is_valid(fifo))
+		return EINVAL;
+
+	if (NEXT_INDEX(fifo, fifo->tail) == fifo->head) {
+		if ((max_size > 0) && ((fifo->size * 2) > max_size)) {
+			if(fifo->size >= max_size)
+				return ENOMEM;
+		} else {
+			max_size = fifo->size * 2;
+		}
+
+		new_items = realloc(fifo->items, sizeof(int) * max_size + 1);
+		if (!new_items)
+			return ENOMEM;
+
+		fifo->items = new_items;
+		if (fifo->tail < fifo->head) {
+			if (fifo->tail < max_size - fifo->size) {
+				memcpy(fifo->items + fifo->size + 1,
+				    fifo->items, fifo->tail * sizeof(int));
+				fifo->tail += fifo->size + 1;
+			} else {
+				memcpy(fifo->items + fifo->size + 1,
+				    fifo->items,
+				    (max_size - fifo->size) * sizeof(int));
+				memcpy(fifo->items,
+				    fifo->items + max_size - fifo->size,
+				    fifo->tail - max_size + fifo->size);
+				fifo->tail -= max_size - fifo->size;
+			}
+		}
+		fifo->size = max_size;
+	}
+
+	fifo->items[fifo->tail] = value;
+	fifo->tail = NEXT_INDEX(fifo, fifo->tail);
+	return EOK;
+}
+
+/** Returns and excludes the first item in the queue.
+ *
+ * @param[in,out] fifoi	The dynamic queue.
+ * @returns		Value of the first item in the queue.
+ * @returns		EINVAL if the queue is not valid.
+ * @returns		ENOENT if the queue is empty.
+ */
+int dyn_fifo_pop(dyn_fifo_ref fifo)
+{
+	int value;
+
+	if (!dyn_fifo_is_valid(fifo))
+		return EINVAL;
+
+	if (fifo->head == fifo->tail)
+		return ENOENT;
+
+	value = fifo->items[fifo->head];
+	fifo->head = NEXT_INDEX(fifo, fifo->head);
+	return value;
+}
+
+/** Returns and keeps the first item in the queue.
+ *
+ * @param[in,out] fifo	The dynamic queue.
+ * @returnsi		Value of the first item in the queue.
+ * @returns		EINVAL if the queue is not valid.
+ * @returns		ENOENT if the queue is empty.
+ */
+int dyn_fifo_value(dyn_fifo_ref fifo)
+{
+	if (!dyn_fifo_is_valid(fifo))
+		return EINVAL;
+
+	if (fifo->head == fifo->tail)
+		return ENOENT;
+
+	return fifo->items[fifo->head];
+}
+
+/** Clears and destroys the queue.
+ *
+ *  @param[in,out] fifo		The dynamic queue.
+ *  @returns			EOK on success.
+ *  @returns			EINVAL if the queue is not valid.
+ */
+int dyn_fifo_destroy(dyn_fifo_ref fifo)
+{
+	if (!dyn_fifo_is_valid(fifo))
+		return EINVAL;
+
+	free(fifo->items);
+	fifo->magic_value = 0;
+	return EOK;
+}
+
+/** @}
+ */
Index: uspace/lib/c/generic/adt/measured_strings.c
===================================================================
--- uspace/lib/c/generic/adt/measured_strings.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/generic/adt/measured_strings.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,415 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ * Character string with measured length implementation.
+ * @see measured_strings.h
+ */
+
+#include <adt/measured_strings.h>
+#include <malloc.h>
+#include <mem.h>
+#include <unistd.h>
+#include <errno.h>
+#include <err.h>
+#include <async.h>
+
+/** Creates a new measured string bundled with a copy of the given string
+ * itself as one memory block.
+ *
+ * If the measured string is being freed, whole memory block is freed.
+ * The measured string should be used only as a constant.
+ *
+ * @param[in] string	The initial character string to be stored.
+ * @param[in] length	The length of the given string without the terminating
+ *			zero ('/0') character. If the length is zero (0), the
+ *			actual length is computed. The given length is used and
+ *			appended with the terminating zero ('\\0') character
+ *			otherwise.
+ * @returns		The new bundled character string with measured length.
+ * @returns		NULL if there is not enough memory left.
+ */
+measured_string_ref
+measured_string_create_bulk(const char * string, size_t length)
+{
+	measured_string_ref new;
+
+	if (length == 0) {
+		while (string[length])
+			++length;
+	}
+	new = (measured_string_ref) malloc(sizeof(measured_string_t) +
+	    (sizeof(char) * (length + 1)));
+	if (!new)
+		return NULL;
+
+	new->length = length;
+	new->value = ((char *) new) + sizeof(measured_string_t);
+	// append terminating zero explicitly - to be safe
+	memcpy(new->value, string, new->length);
+	new->value[new->length] = '\0';
+
+	return new;
+}
+
+/** Copies the given measured string with separated header and data parts.
+ *
+ * @param[in] source	The source measured string to be copied.
+ * @returns		The copy of the given measured string.
+ * @returns		NULL if the source parameter is NULL.
+ * @returns		NULL if there is not enough memory left.
+ */
+measured_string_ref measured_string_copy(measured_string_ref source)
+{
+	measured_string_ref new;
+
+	if (!source)
+		return NULL;
+
+	new = (measured_string_ref) malloc(sizeof(measured_string_t));
+	if (new) {
+		new->value = (char *) malloc(source->length + 1);
+		if (new->value) {
+			new->length = source->length;
+			memcpy(new->value, source->value, new->length);
+			new->value[new->length] = '\0';
+			return new;
+		} else {
+			free(new);
+		}
+	}
+
+	return NULL;
+}
+
+/** Receives a measured strings array from a calling module.
+ *
+ * Creates the array and the data memory blocks.
+ * This method should be used only while processing IPC messages as the array
+ * size has to be negotiated in advance.
+ *
+ *  @param[out] strings	The received measured strings array.
+ *  @param[out] data	The measured strings data. This memory block stores the
+ *			actual character strings.
+ *  @param[in] count	The size of the measured strings array.
+ *  @returns		EOK on success.
+ *  @returns		EINVAL if the strings or data parameter is NULL.
+ *  @returns		EINVAL if the count parameter is zero (0).
+ *  @returns		EINVAL if the sent array differs in size.
+ *  @returns		EINVAL if there is inconsistency in sent measured
+ *			strings' lengths (should not occur).
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		Other error codes as defined for the
+ *			async_data_write_finalize() function.
+ */
+int
+measured_strings_receive(measured_string_ref *strings, char **data,
+    size_t count)
+{
+	ERROR_DECLARE;
+
+	size_t *lengths;
+	size_t index;
+	size_t length;
+	char *next;
+	ipc_callid_t callid;
+
+	if ((!strings) || (!data) || (count <= 0))
+		return EINVAL;
+
+	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
+	if (!lengths)
+		return ENOMEM;
+
+	if ((!async_data_write_receive(&callid, &length)) ||
+	    (length != sizeof(size_t) * (count + 1))) {
+		free(lengths);
+		return EINVAL;
+	}
+	if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths,
+	    sizeof(size_t) * (count + 1)))) {
+		free(lengths);
+		return ERROR_CODE;
+	}
+
+	*data = malloc(lengths[count]);
+	if (!(*data)) {
+		free(lengths);
+		return ENOMEM;
+	}
+	(*data)[lengths[count] - 1] = '\0';
+
+	*strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
+	    count);
+	if (!(*strings)) {
+		free(lengths);
+		free(*data);
+		return ENOMEM;
+	}
+
+	next = *data;
+	for (index = 0; index < count; ++index) {
+		(*strings)[index].length = lengths[index];
+		if (lengths[index] > 0) {
+			if ((!async_data_write_receive(&callid, &length)) ||
+			    (length != lengths[index])) {
+				free(*data);
+				free(*strings);
+				free(lengths);
+				return EINVAL;
+			}
+			ERROR_PROPAGATE(async_data_write_finalize(callid, next,
+			    lengths[index]));
+			(*strings)[index].value = next;
+			next += lengths[index];
+			*next = '\0';
+			++next;
+		} else {
+			(*strings)[index].value = NULL;
+		}
+	}
+
+	free(lengths);
+	return EOK;
+}
+
+/** Computes the lengths of the measured strings in the given array.
+ *
+ * @param[in] strings	The measured strings array to be processed.
+ * @param[in] count	The measured strings array size.
+ * @returns		The computed sizes array.
+ * @returns		NULL if there is not enough memory left.
+ */
+static size_t *prepare_lengths(const measured_string_ref strings, size_t count)
+{
+	size_t *lengths;
+	size_t index;
+	size_t length;
+
+	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
+	if (!lengths)
+		return NULL;
+
+	length = 0;
+	for (index = 0; index < count; ++ index) {
+		lengths[index] = strings[index].length;
+		length += lengths[index] + 1;
+	}
+	lengths[count] = length;
+	return lengths;
+}
+
+/** Replies the given measured strings array to a calling module.
+ *
+ * This method should be used only while processing IPC messages as the array
+ * size has to be negotiated in advance.
+ *
+ * @param[in] strings	The measured strings array to be transferred.
+ * @param[in] count	The measured strings array size.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the strings parameter is NULL.
+ * @returns		EINVAL if the count parameter is zero (0).
+ * @returns		EINVAL if the calling module does not accept the given
+ *			array size.
+ * @returns		EINVAL if there is inconsistency in sent measured
+ *			strings' lengths (should not occur).
+ * @returns		Other error codes as defined for the
+ *			async_data_read_finalize() function.
+ */
+int measured_strings_reply(const measured_string_ref strings, size_t count)
+{
+	ERROR_DECLARE;
+
+	size_t *lengths;
+	size_t index;
+	size_t length;
+	ipc_callid_t callid;
+
+	if ((!strings) || (count <= 0))
+		return EINVAL;
+
+	lengths = prepare_lengths(strings, count);
+	if (!lengths)
+		return ENOMEM;
+
+	if ((!async_data_read_receive(&callid, &length)) ||
+	    (length != sizeof(size_t) * (count + 1))) {
+		free(lengths);
+		return EINVAL;
+	}
+	if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths,
+	    sizeof(size_t) * (count + 1)))) {
+		free(lengths);
+		return ERROR_CODE;
+	}
+	free(lengths);
+
+	for (index = 0; index < count; ++ index) {
+		if (strings[index].length > 0) {
+			if((!async_data_read_receive(&callid, &length))	||
+			    (length != strings[index].length)) {
+				return EINVAL;
+			}
+			ERROR_PROPAGATE(async_data_read_finalize(callid,
+			    strings[index].value, strings[index].length));
+		}
+	}
+
+	return EOK;
+}
+
+/** Receives a measured strings array from another module.
+ *
+ * Creates the array and the data memory blocks.
+ * This method should be used only following other IPC messages as the array
+ * size has to be negotiated in advance.
+ *
+ * @param[in] phone	The other module phone.
+ * @param[out] strings	The returned measured strings array.
+ * @param[out] data	The measured strings data. This memory block stores the
+ *			actual character strings.
+ * @param[in] count	The size of the measured strings array.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the strings or data parameter is NULL.
+ * @returns		EINVAL if the phone or count parameter is not positive.
+ * @returns		EINVAL if the sent array differs in size.
+ * @returns		ENOMEM if there is not enough memory left.
+ * @returns		Other error codes as defined for the
+ *			async_data_read_start() function.
+ */
+int
+measured_strings_return(int phone, measured_string_ref *strings, char **data,
+    size_t count)
+{
+	ERROR_DECLARE;
+
+	size_t *lengths;
+	size_t index;
+	char *next;
+
+	if ((phone <= 0) || (!strings) || (!data) || (count <= 0))
+		return EINVAL;
+
+	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
+	if (!lengths)
+		return ENOMEM;
+
+	if (ERROR_OCCURRED(async_data_read_start(phone, lengths,
+	    sizeof(size_t) * (count + 1)))) {
+		free(lengths);
+		return ERROR_CODE;
+	}
+
+	*data = malloc(lengths[count]);
+	if (!(*data)) {
+		free(lengths);
+		return ENOMEM;
+	}
+
+	*strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
+	    count);
+	if (!(*strings)) {
+		free(lengths);
+		free(*data);
+		return ENOMEM;
+	}
+
+	next = *data;
+	for (index = 0; index < count; ++ index) {
+		(*strings)[index].length = lengths[index];
+		if (lengths[index] > 0) {
+			ERROR_PROPAGATE(async_data_read_start(phone, next,
+			    lengths[index]));
+			(*strings)[index].value = next;
+			next += lengths[index];
+			*next = '\0';
+			++ next;
+		} else {
+			(*strings)[index].value = NULL;
+		}
+	}
+
+	free(lengths);
+	return EOK;
+}
+
+/** Sends the given measured strings array to another module.
+ *
+ * This method should be used only following other IPC messages as the array
+ * size has to be negotiated in advance.
+ *
+ * @param[in] phone	The other module phone.
+ * @param[in] strings	The measured strings array to be transferred.
+ * @param[in] count	The measured strings array size.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the strings parameter is NULL.
+ * @returns		EINVAL if the phone or count parameter is not positive.
+ * @returns		Other error codes as defined for the
+ *			async_data_write_start() function.
+ */
+int
+measured_strings_send(int phone, const measured_string_ref strings,
+    size_t count)
+{
+	ERROR_DECLARE;
+
+	size_t *lengths;
+	size_t index;
+
+	if ((phone <= 0) || (!strings) || (count <= 0))
+		return EINVAL;
+
+	lengths = prepare_lengths(strings, count);
+	if (!lengths)
+		return ENOMEM;
+
+	if (ERROR_OCCURRED(async_data_write_start(phone, lengths,
+	    sizeof(size_t) * (count + 1)))) {
+		free(lengths);
+		return ERROR_CODE;
+	}
+
+	free(lengths);
+
+	for (index = 0; index < count; ++index) {
+		if (strings[index].length > 0) {
+			ERROR_PROPAGATE(async_data_write_start(phone,
+			    strings[index].value, strings[index].length));
+		}
+	}
+
+	return EOK;
+}
+
+/** @}
+ */
+
Index: uspace/lib/c/generic/async_rel.c
===================================================================
--- uspace/lib/c/generic/async_rel.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/generic/async_rel.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,307 @@
+/*
+ * Copyright (c) 2010 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.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+/**
+ * This file implements simple relation support for the async framework.
+ *
+ * By the term "relation", we mean a logical data path between a client and a
+ * server over which the client can send multiple, potentially blocking,
+ * requests to the server.
+ *
+ * Clients and servers are naturally connected using IPC phones, thus an IPC
+ * phone represents a connection between a client and a server. In one
+ * connection, there can be many relations.
+ *
+ * Relations are useful in situations in which there is only one IPC connection
+ * between the client and the server, but the client wants to be able to make
+ * multiple parallel requests. Using only a single phone and without any other
+ * provisions, all requests would have to be serialized. On the other hand, the
+ * client can make as many parallel requests as there are active relations.
+ *
+ * There are several possible implementations of relations. This implementation
+ * uses additional phones to represent relations. Using phones both for the
+ * primary connection and also for its relations has several advantages:
+ *
+ * - to make a series of requests over a relation, the client can continue to
+ *   use the existing async framework APIs
+ * - the server supports relations by the virtue of spawning a new connection
+ *   fibril, just as it does for every new connection even without relations
+ * - the implementation is pretty straightforward; a very naive implementation
+ *   would be to make each request using a fresh phone (that is what we have
+ *   done in the past); a slightly better approach would be to cache connected
+ *   phones so that they can be reused by a later relation within the same
+ *   connection (that is what this implementation does)
+ *
+ * The main disadvantages of using phones to represent relations are:
+ *
+ * - if there are too many relations (even cached ones), the task may hit its
+ *   limit on the maximum number of connected phones, which could prevent the
+ *   task from making new IPC connections to other tasks
+ * - if there are too many IPC connections already, it may be impossible to
+ *   create a relation by connecting a new phone thanks to the task's limit on
+ *   the maximum number of connected phones
+ *
+ * These problems can be helped by increasing the limit on the maximum number of
+ * connected phones to some reasonable value and by limiting the number of
+ * phones cached to some fraction of this limit.
+ *
+ * The cache itself has a mechanism to close some number of unused phones if a
+ * new phone cannot be connected, but the outter world currently does not have a
+ * way to ask the phone cache to shrink.
+ *
+ * To minimize the confusion stemming from the fact that we use phones for two
+ * things (the primary IPC connection and also each relation), this file makes
+ * the distinction by using the term 'key phone' for the former and 'relation
+ * phone' for the latter. Under the hood, all phones remain equal, of course.
+ *
+ * There is a small inefficiency in that the cache repeatedly allocates and
+ * deallocated the rel_node_t structures when in fact it could keep the
+ * allocated structures around and reuse them later. But such a solution would
+ * be effectively implementing a poor man's slab allocator while it would be
+ * better to have the slab allocator ported to uspace so that everyone could
+ * benefit from it.
+ */
+
+#include <async_rel.h>
+#include <ipc/ipc.h>
+#include <fibril_synch.h>
+#include <adt/list.h>
+#include <adt/hash_table.h>
+#include <malloc.h>
+#include <errno.h>
+#include <assert.h>
+
+#define KEY_NODE_HASH_BUCKETS	16
+
+typedef struct {
+	link_t link;		/**< Key node hash table link. */
+	int key_phone;		/**< The phone serving as a key. */
+	link_t rel_head;	/**< List of open relation phones. */
+} key_node_t;
+
+typedef struct {
+	link_t rel_link;	/**< Link for the list of relation phones. */
+	link_t global_link;	/**< Link for the global list of phones. */
+	int rel_phone;		/**< Connected relation phone. */
+} rel_node_t;
+
+/**
+ * Mutex protecting the global_rel_head list and the key_node_hash hash table.
+ */
+static fibril_mutex_t async_rel_mutex;
+
+/**
+ * List of all currently unused relation phones.
+ */
+static LIST_INITIALIZE(global_rel_head);
+
+/**
+ * Hash table containing lists of available relation phones for all key
+ * phones.
+ */
+static hash_table_t key_node_hash;
+
+static hash_index_t kn_hash(unsigned long *key)
+{
+	return *key % KEY_NODE_HASH_BUCKETS;
+}
+
+static int kn_compare(unsigned long *key, hash_count_t keys, link_t *item)
+{
+	key_node_t *knp = hash_table_get_instance(item, key_node_t, link);
+
+	return *key == (unsigned long) knp->key_phone;
+}
+
+static void kn_remove_callback(link_t *item)
+{
+}
+
+static hash_table_operations_t key_node_hash_ops = {
+	.hash = kn_hash,
+	.compare = kn_compare,
+	.remove_callback = kn_remove_callback
+};
+
+/** Initialize the async_rel subsystem.
+ *
+ * Needs to be called prior to any other interface in this file.
+ */
+int async_rel_init(void)
+{
+	fibril_mutex_initialize(&async_rel_mutex);
+	list_initialize(&global_rel_head);
+	return hash_table_create(&key_node_hash, KEY_NODE_HASH_BUCKETS, 1,
+	    &key_node_hash_ops);
+}
+
+static void key_node_initialize(key_node_t *knp)
+{
+	link_initialize(&knp->link);
+	knp->key_phone = -1;
+	list_initialize(&knp->rel_head);
+}
+
+static void rel_node_initialize(rel_node_t *rnp)
+{
+	link_initialize(&rnp->rel_link);
+	link_initialize(&rnp->global_link);
+	rnp->rel_phone = -1;
+}
+
+/** Create a new relation for a connection represented by a key phone.
+ *
+ * @param key_phone	Phone representing the connection.
+ * @return		Phone representing the new relation or a negative error
+ *			code.
+ */
+int async_relation_create(int key_phone)
+{
+	unsigned long key = (unsigned long) key_phone;
+	link_t *lnk;
+	key_node_t *knp;
+	rel_node_t *rnp;
+	int rel_phone;
+
+	fibril_mutex_lock(&async_rel_mutex);
+	lnk = hash_table_find(&key_node_hash, &key);
+	if (!lnk) {
+		/*
+		 * The key node was not found in the hash table. Try to allocate
+		 * and hash in a new one.
+		 */
+		knp = (key_node_t *) malloc(sizeof(key_node_t));
+		if (!knp) {
+			/*
+			 * As a possible improvement, we could make a one-time
+			 * attempt to create a phone without trying to add the
+			 * key node into the hash.
+			 */
+			fibril_mutex_unlock(&async_rel_mutex);
+			return ENOMEM;
+		}
+		key_node_initialize(knp);
+		knp->key_phone = key_phone;
+		hash_table_insert(&key_node_hash, &key, &knp->link);
+	} else {
+		/*
+		 * Found the key node.
+		 */
+		knp = hash_table_get_instance(lnk, key_node_t, link);
+	}
+
+	if (!list_empty(&knp->rel_head)) {
+		/*
+		 * There are available relation phones for the key phone.
+		 */
+		rnp = list_get_instance(knp->rel_head.next, rel_node_t,
+		    rel_link);
+		list_remove(&rnp->rel_link);
+		list_remove(&rnp->global_link);
+		
+		rel_phone = rnp->rel_phone;
+		free(rnp);
+	} else {
+		/*
+		 * There are no available relation phones for the key phone.
+		 * Make a one-time attempt to connect a new relation phone.
+		 */
+retry:
+		rel_phone = ipc_connect_me_to(key_phone, 0, 0, 0);
+		if (rel_phone >= 0) {
+			/* success, do nothing */
+		} else if (!list_empty(&global_rel_head)) {
+			/*
+			 * We did not manage to connect a new phone. But we can
+			 * try to hangup some currently unused phones and try
+			 * again.
+			 */
+			rnp = list_get_instance(global_rel_head.next,
+			    rel_node_t, global_link);
+			list_remove(&rnp->global_link);
+			list_remove(&rnp->rel_link);
+			rel_phone = rnp->rel_phone;
+			free(rnp);
+			ipc_hangup(rel_phone);
+			goto retry;
+		} else {
+			/*
+			 * This is unfortunate. We failed both to find a cached
+			 * phone or to create a new one even after cleaning up
+			 * the cache. This is most likely due to too many key
+			 * phones being kept connected.
+			 */
+			rel_phone = ELIMIT;
+		}
+	}
+
+	fibril_mutex_unlock(&async_rel_mutex);
+	return rel_phone;
+}
+
+/** Destroy a relation.
+ *
+ * @param key_phone	Phone representing the connection.
+ * @param rel_phone	Phone representing the relation within the connection.
+ */
+void async_relation_destroy(int key_phone, int rel_phone)
+{
+	unsigned long key = (unsigned long) key_phone;
+	key_node_t *knp;
+	rel_node_t *rnp;
+	link_t *lnk;
+
+	fibril_mutex_lock(&async_rel_mutex);
+	lnk = hash_table_find(&key_node_hash, &key);
+	assert(lnk);
+	knp = hash_table_get_instance(lnk, key_node_t, link);
+	rnp = (rel_node_t *) malloc(sizeof(rel_node_t));
+	if (!rnp) {
+		/*
+		 * Being unable to remember the connected relation phone here
+		 * means that we simply hangup.
+		 */
+		fibril_mutex_unlock(&async_rel_mutex);
+		ipc_hangup(rel_phone);
+		return;
+	}
+	rel_node_initialize(rnp);
+	rnp->rel_phone = rel_phone;
+	list_append(&rnp->rel_link, &knp->rel_head);
+	list_append(&rnp->global_link, &global_rel_head);
+	fibril_mutex_unlock(&async_rel_mutex);
+}
+
+/** @}
+ */
Index: uspace/lib/c/generic/ddi.c
===================================================================
--- uspace/lib/c/generic/ddi.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/generic/ddi.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -96,13 +96,4 @@
 }
 
-/** Interrupt control
- *
- * @param enable	1 - enable interrupts, 0 - disable interrupts
- */
-int preemption_control(int enable)
-{
-	return __SYSCALL1(SYS_PREEMPT_CONTROL, (sysarg_t) enable);
-}
-
 /** Enable PIO for specified I/O range.
  *
Index: uspace/lib/c/generic/io/io.c
===================================================================
--- uspace/lib/c/generic/io/io.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/generic/io/io.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -757,4 +757,14 @@
 }
 
+int fileno(FILE *stream)
+{
+	if (stream->klog) {
+		errno = EBADF;
+		return -1;
+	}
+	
+	return stream->fd;
+}
+
 int fphone(FILE *stream)
 {
Index: uspace/lib/c/generic/libc.c
===================================================================
--- uspace/lib/c/generic/libc.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/generic/libc.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -50,4 +50,5 @@
 #include <ipc/ipc.h>
 #include <async.h>
+#include <async_rel.h>
 #include <as.h>
 #include <loader/pcb.h>
@@ -65,4 +66,5 @@
 	__heap_init();
 	__async_init();
+	(void) async_rel_init();
 	fibril_t *fibril = fibril_setup();
 	__tcb_set(fibril->tcb);
Index: uspace/lib/c/include/adt/char_map.h
===================================================================
--- uspace/lib/c/include/adt/char_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/char_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ *  Character string to integer map.
+ */
+
+#ifndef LIBC_CHAR_MAP_H_
+#define LIBC_CHAR_MAP_H_
+
+#include <libarch/types.h>
+
+/** Invalid assigned value used also if an&nbsp;entry does not exist. */
+#define CHAR_MAP_NULL	(-1)
+
+/** Type definition of the character string to integer map.
+ *  @see char_map
+ */
+typedef struct char_map	char_map_t;
+
+/** Type definition of the character string to integer map pointer.
+ *  @see char_map
+ */
+typedef char_map_t *char_map_ref;
+
+/** Character string to integer map item.
+ *
+ * This structure recursivelly contains itself as a character by character tree.
+ * The actually mapped character string consists of all the parent characters
+ * and the actual one.
+ */
+struct char_map {
+	/** Actually mapped character. */
+	char c;
+	/** Stored integral value. */
+	int value;
+	/** Next character array size. */
+	int size;
+	/** First free position in the next character array. */
+	int next;
+	/** Next character array. */
+	char_map_ref *items;
+	/** Consistency check magic value. */
+	int magic;
+};
+
+extern int char_map_initialize(char_map_ref);
+extern void char_map_destroy(char_map_ref);
+extern int char_map_exclude(char_map_ref, const char *, size_t);
+extern int char_map_add(char_map_ref, const char *, size_t, const int);
+extern int char_map_find(const char_map_ref, const char *, size_t);
+extern int char_map_update(char_map_ref, const char *, size_t, const int);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/adt/dynamic_fifo.h
===================================================================
--- uspace/lib/c/include/adt/dynamic_fifo.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/dynamic_fifo.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ * Dynamic first in first out positive integer queue.
+ * Possitive integer values only.
+ */
+
+#ifndef LIBC_DYNAMIC_FIFO_H_
+#define LIBC_DYNAMIC_FIFO_H_
+
+/** Type definition of the dynamic fifo queue.
+ * @see dyn_fifo
+ */
+typedef struct dyn_fifo	dyn_fifo_t;
+
+/** Type definition of the dynamic fifo queue pointer.
+ *  @see dyn_fifo
+ */
+typedef dyn_fifo_t *dyn_fifo_ref;
+
+/** Dynamic first in first out positive integer queue.
+ * Possitive integer values only.
+ * The queue automatically resizes if needed.
+ */
+struct dyn_fifo {
+	/** Stored item field. */
+	int *items;
+	/** Actual field size. */
+	int size;
+	/** First item in the queue index. */
+	int head;
+	/** Last item in the queue index. */
+	int tail;
+	/** Consistency check magic value. */
+	int magic_value;
+};
+
+extern int dyn_fifo_initialize(dyn_fifo_ref, int);
+extern int dyn_fifo_destroy(dyn_fifo_ref);
+extern int dyn_fifo_push(dyn_fifo_ref, int, int);
+extern int dyn_fifo_pop(dyn_fifo_ref);
+extern int dyn_fifo_value(dyn_fifo_ref);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/adt/generic_char_map.h
===================================================================
--- uspace/lib/c/include/adt/generic_char_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/generic_char_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ *  Character string to generic type map.
+ */
+
+#ifndef LIBC_GENERIC_CHAR_MAP_H_
+#define LIBC_GENERIC_CHAR_MAP_H_
+
+#include <unistd.h>
+#include <errno.h>
+#include <err.h>
+
+#include <adt/char_map.h>
+#include <adt/generic_field.h>
+
+/** Internal magic value for a&nbsp;map consistency check. */
+#define GENERIC_CHAR_MAP_MAGIC_VALUE	0x12345622
+
+/** Character string to generic type map declaration.
+ *  @param[in] name	Name of the map.
+ *  @param[in] type	Inner object type.
+ */
+#define GENERIC_CHAR_MAP_DECLARE(name, type) \
+	GENERIC_FIELD_DECLARE(name##_items, type) \
+	\
+	typedef	struct name name##_t; \
+	typedef	name##_t *name##_ref; \
+	\
+	struct	name { \
+		char_map_t names; \
+		name##_items_t values; \
+		int magic; \
+	}; \
+	\
+	int name##_add(name##_ref, const char *, const size_t, type *); \
+	int name##_count(name##_ref); \
+	void name##_destroy(name##_ref); \
+	void name##_exclude(name##_ref, const char *, const size_t); \
+	type *name##_find(name##_ref, const char *, const size_t); \
+	int name##_initialize(name##_ref); \
+	int name##_is_valid(name##_ref);
+
+/** Character string to generic type map implementation.
+ *
+ * Should follow declaration with the same parameters.
+ *
+ * @param[in] name	Name of the map.
+ * @param[in] type	Inner object type.
+ */
+#define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \
+	GENERIC_FIELD_IMPLEMENT(name##_items, type) \
+	\
+	int name##_add(name##_ref map, const char *name, const size_t length, \
+	     type *value) \
+	{ \
+		ERROR_DECLARE; \
+		int index; \
+		if (!name##_is_valid(map)) \
+			return EINVAL; \
+		index = name##_items_add(&map->values, value); \
+		if (index < 0) \
+			return index; \
+		if (ERROR_OCCURRED(char_map_add(&map->names, name, length, \
+		    index))) { \
+			name##_items_exclude_index(&map->values, index); \
+			return ERROR_CODE; \
+		} \
+		return EOK; \
+	} \
+	\
+	int name##_count(name##_ref map) \
+	{ \
+		return name##_is_valid(map) ? \
+		    name##_items_count(&map->values) : -1; \
+	} \
+	\
+	void name##_destroy(name##_ref map) \
+	{ \
+		if (name##_is_valid(map)) { \
+			char_map_destroy(&map->names); \
+			name##_items_destroy(&map->values); \
+		} \
+	} \
+	\
+	void name##_exclude(name##_ref map, const char *name, \
+	    const size_t length) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			index = char_map_exclude(&map->names, name, length); \
+			if (index != CHAR_MAP_NULL) \
+				name##_items_exclude_index(&map->values, \
+				     index); \
+		} \
+	} \
+	\
+	type *name##_find(name##_ref map, const char *name, \
+	    const size_t length) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			index = char_map_find(&map->names, name, length); \
+			if( index != CHAR_MAP_NULL) \
+				return name##_items_get_index(&map->values, \
+				    index); \
+		} \
+		return NULL; \
+	} \
+	\
+	int name##_initialize(name##_ref map) \
+	{ \
+		ERROR_DECLARE; \
+		if (!map) \
+			return EINVAL; \
+		ERROR_PROPAGATE(char_map_initialize(&map->names)); \
+		if (ERROR_OCCURRED(name##_items_initialize(&map->values))) { \
+			char_map_destroy(&map->names); \
+			return ERROR_CODE; \
+		} \
+		map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
+		return EOK; \
+	} \
+	\
+	int name##_is_valid(name##_ref map) \
+	{ \
+		return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
+	}
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/adt/generic_field.h
===================================================================
--- uspace/lib/c/include/adt/generic_field.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/generic_field.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ *  Generic type field.
+ */
+
+#ifndef LIBC_GENERIC_FIELD_H_
+#define LIBC_GENERIC_FIELD_H_
+
+#include <errno.h>
+#include <malloc.h>
+#include <mem.h>
+#include <unistd.h>
+
+/** Internal magic value for a&nbsp;field consistency check. */
+#define GENERIC_FIELD_MAGIC_VALUE		0x55667788
+
+/** Generic type field declaration.
+ *
+ * @param[in] name	Name of the field.
+ * @param[in] type	Inner object type.
+ */
+#define GENERIC_FIELD_DECLARE(name, type) \
+	typedef	struct name name##_t; \
+	typedef	name##_t *name##_ref; \
+	\
+	struct	name { \
+		int size; \
+		int next; \
+		type **items; \
+		int magic; \
+	}; \
+	\
+	int name##_add(name##_ref, type *); \
+	int name##_count(name##_ref); \
+	void name##_destroy(name##_ref); \
+	void name##_exclude_index(name##_ref, int); \
+	type **name##_get_field(name##_ref); \
+	type *name##_get_index(name##_ref, int); \
+	int name##_initialize(name##_ref); \
+	int name##_is_valid(name##_ref);
+
+/** Generic type field implementation.
+ *
+ * Should follow declaration with the same parameters.
+ *
+ * @param[in] name	Name of the field.
+ * @param[in] type	Inner object type.
+ */
+#define GENERIC_FIELD_IMPLEMENT(name, type) \
+	int name##_add(name##_ref field, type *value) \
+	{ \
+		if (name##_is_valid(field)) { \
+			if (field->next == (field->size - 1)) { \
+				type **tmp; \
+				tmp = (type **) realloc(field->items, \
+				    sizeof(type *) * 2 * field->size); \
+				if (!tmp) \
+					return ENOMEM; \
+				field->size *= 2; \
+				field->items = tmp; \
+			} \
+			field->items[field->next] = value; \
+			++field->next; \
+			field->items[field->next] = NULL; \
+			return field->next - 1; \
+		} \
+		return EINVAL; \
+	} \
+	\
+	int name##_count(name##_ref field) \
+	{ \
+		return name##_is_valid(field) ? field->next : -1; \
+	} \
+	\
+	void name##_destroy(name##_ref field) \
+	{ \
+		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]); \
+			} \
+			free(field->items); \
+		} \
+	} \
+	 \
+	void name##_exclude_index(name##_ref field, int index) \
+	{ \
+		if (name##_is_valid(field) && (index >= 0) && \
+		    (index < field->next) && (field->items[index])) { \
+			free(field->items[index]); \
+			field->items[index] = NULL; \
+		} \
+	} \
+	 \
+	type *name##_get_index(name##_ref field, int index) \
+	{ \
+		if (name##_is_valid(field) && (index >= 0) && \
+		    (index < field->next) && (field->items[index])) \
+			return field->items[index]; \
+		return NULL; \
+	} \
+	\
+	type **name##_get_field(name##_ref field) \
+	{ \
+		return name##_is_valid(field) ? field->items : NULL; \
+	} \
+	\
+	int name##_initialize(name##_ref field) \
+	{ \
+		if (!field) \
+			return EINVAL; \
+		field->size = 2; \
+		field->next = 0; \
+		field->items = (type **) malloc(sizeof(type *) * field->size); \
+		if (!field->items) \
+			return ENOMEM; \
+		field->items[field->next] = NULL; \
+		field->magic = GENERIC_FIELD_MAGIC_VALUE; \
+		return EOK; \
+	} \
+	\
+	int name##_is_valid(name##_ref field) \
+	{ \
+		return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE); \
+	}
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/adt/int_map.h
===================================================================
--- uspace/lib/c/include/adt/int_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/int_map.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,260 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ *  Integer to generic type map.
+ */
+
+#ifndef LIBC_INT_MAP_H_
+#define LIBC_INT_MAP_H_
+
+#include <errno.h>
+#include <malloc.h>
+#include <mem.h>
+#include <unistd.h>
+
+/** Internal magic value for a map consistency check. */
+#define INT_MAP_MAGIC_VALUE		0x11223344
+
+/** Internal magic value for an item consistency check. */
+#define INT_MAP_ITEM_MAGIC_VALUE	0x55667788
+
+/** Integer to generic type map declaration.
+ *
+ * @param[in] name	Name of the map.
+ * @param[in] type	Inner object type.
+ */
+#define INT_MAP_DECLARE(name, type) \
+	typedef	struct name name##_t; \
+	typedef	name##_t *name##_ref; \
+	typedef	struct name##_item name##_item_t; \
+	typedef	name##_item_t *name##_item_ref; \
+	\
+	struct	name##_item { \
+		int key; \
+		type *value; \
+		int magic; \
+	}; \
+	\
+	struct	name { \
+		int size; \
+		int next; \
+		name##_item_ref items; \
+		int magic; \
+	}; \
+	\
+	int name##_add(name##_ref, int, type *); \
+	void name##_clear(name##_ref); \
+	int name##_count(name##_ref); \
+	void name##_destroy(name##_ref); \
+	void name##_exclude(name##_ref, int); \
+	void name##_exclude_index(name##_ref, int); \
+	type *name##_find(name##_ref, int); \
+	int name##_update(name##_ref, int, int); \
+	type *name##_get_index(name##_ref, int); \
+	int name##_initialize(name##_ref); \
+	int name##_is_valid(name##_ref); \
+	void name##_item_destroy(name##_item_ref); \
+	int name##_item_is_valid(name##_item_ref);
+
+/** Integer to generic type map implementation.
+ *
+ * Should follow declaration with the same parameters.
+ *
+ * @param[in] name	Name of the map.
+ * @param[in] type	Inner object type.
+ */
+#define INT_MAP_IMPLEMENT(name, type) \
+	int name##_add(name##_ref map, int key, type *value) \
+	{ \
+		if (name##_is_valid(map)) { \
+			if (map->next == (map->size - 1)) { \
+				name##_item_ref tmp; \
+				tmp = (name##_item_ref) realloc(map->items, \
+				    sizeof(name##_item_t) * 2 * map->size); \
+				if (!tmp) \
+					return ENOMEM; \
+				map->size *= 2; \
+				map->items = tmp; \
+			} \
+			map->items[map->next].key = key; \
+			map->items[map->next].value = value; \
+			map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE; \
+			++map->next; \
+			map->items[map->next].magic = 0; \
+			return map->next - 1; \
+		} \
+		return EINVAL; \
+	} \
+	\
+	void name##_clear(name##_ref map) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			for (index = 0; index < map->next; ++index) { \
+				if (name##_item_is_valid(&map->items[index])) { \
+					name##_item_destroy( \
+					    &map->items[index]); \
+				} \
+			} \
+			map->next = 0; \
+			map->items[map->next].magic = 0; \
+		} \
+	} \
+	\
+	int name##_count(name##_ref map) \
+	{ \
+		return name##_is_valid(map) ? map->next : -1; \
+	} \
+	\
+	void name##_destroy(name##_ref map) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			map->magic = 0; \
+			for (index = 0; index < map->next; ++index) { \
+				if (name##_item_is_valid(&map->items[index])) { \
+					name##_item_destroy( \
+					    &map->items[index]); \
+				} \
+			} \
+			free(map->items); \
+		} \
+	} \
+	\
+	void name##_exclude(name##_ref map, int key) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			for (index = 0; index < map->next; ++index) { \
+				if (name##_item_is_valid(&map->items[index]) && \
+				    (map->items[index].key == key)) { \
+					name##_item_destroy( \
+					    &map->items[index]); \
+				} \
+			} \
+		} \
+	} \
+	\
+	void name##_exclude_index(name##_ref map, int index) \
+	{ \
+		if (name##_is_valid(map) && (index >= 0) && \
+		    (index < map->next) && \
+		    name##_item_is_valid(&map->items[index])) { \
+			name##_item_destroy(&map->items[index]); \
+		} \
+	} \
+	\
+	type *name##_find(name##_ref map, int key) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			for (index = 0; index < map->next; ++index) { \
+				if (name##_item_is_valid(&map->items[index]) && \
+				    (map->items[index].key == key)) { \
+					return map->items[index].value; \
+				} \
+			} \
+		} \
+		return NULL; \
+	} \
+	\
+	int name##_update(name##_ref map, int key, int new_key) \
+	{ \
+		if (name##_is_valid(map)) { \
+			int index; \
+			for (index = 0; index < map->next; ++index) { \
+				if (name##_item_is_valid(&map->items[index])) { \
+					if (map->items[index].key == new_key) \
+						return EEXIST; \
+					if (map->items[index].key == key) { \
+						map->items[index].key = \
+						    new_key; \
+						return EOK; \
+					} \
+				} \
+			} \
+		} \
+		return ENOENT; \
+	} \
+	\
+	type *name##_get_index(name##_ref map, int index) \
+	{ \
+		if (name##_is_valid(map) && (index >= 0) && \
+		    (index < map->next) && \
+		    name##_item_is_valid(&map->items[index])) { \
+			return map->items[index].value; \
+		} \
+		return NULL; \
+	} \
+	\
+	int name##_initialize(name##_ref map) \
+	{ \
+		if (!map) \
+			return EINVAL; \
+		map->size = 2; \
+		map->next = 0; \
+		map->items = (name##_item_ref) malloc(sizeof(name##_item_t) * \
+		    map->size); \
+		if (!map->items) \
+			return ENOMEM; \
+		map->items[map->next].magic = 0; \
+		map->magic = INT_MAP_MAGIC_VALUE; \
+		return EOK; \
+	} \
+	\
+	int name##_is_valid(name##_ref map) \
+	{ \
+		return map && (map->magic == INT_MAP_MAGIC_VALUE); \
+	} \
+	\
+	void name##_item_destroy(name##_item_ref item) \
+	{ \
+		if (name##_item_is_valid(item)) { \
+			item->magic = 0; \
+			if (item->value) { \
+				free(item->value); \
+				item->value = NULL; \
+			} \
+		} \
+	} \
+	\
+	int name##_item_is_valid(name##_item_ref item) \
+	{ \
+		return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \
+	}
+
+#endif
+
+/** @}
+ */
+
Index: uspace/lib/c/include/adt/measured_strings.h
===================================================================
--- uspace/lib/c/include/adt/measured_strings.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/adt/measured_strings.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * 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
+ *  @{
+ */
+
+/** @file
+ * Character string with measured length.
+ * The structure has been designed for serialization of character strings
+ * between modules.
+ */
+
+#ifndef LIBC_MEASURED_STRINGS_H_
+#define LIBC_MEASURED_STRINGS_H_
+
+#include <sys/types.h>
+
+/** Type definition of the character string with measured length.
+ *  @see measured_string
+ */
+typedef struct measured_string measured_string_t;
+
+/** Type definition of the character string with measured length pointer.
+ *  @see measured_string
+ */
+typedef measured_string_t *measured_string_ref;
+
+/** Character string with measured length.
+ *
+ * This structure has been designed for serialization of character strings
+ * between modules.
+ */
+struct measured_string {
+	/** Character string data. */
+	char * value;
+	/** Character string length. */
+	size_t length;
+};
+
+extern measured_string_ref measured_string_create_bulk(const char *, size_t);
+extern measured_string_ref measured_string_copy(measured_string_ref);
+extern int measured_strings_receive(measured_string_ref *, char **, size_t);
+extern int measured_strings_reply(const measured_string_ref, size_t);
+extern int measured_strings_return(int, measured_string_ref *, char **, size_t);
+extern int measured_strings_send(int, const measured_string_ref, size_t);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/async_rel.h
===================================================================
--- uspace/lib/c/include/async_rel.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/lib/c/include/async_rel.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2010 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.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_ASYNC_REL_H_
+#define LIBC_ASYNC_REL_H_
+
+extern int async_rel_init(void);
+extern int async_relation_create(int);
+extern void async_relation_destroy(int, int);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/ddi.h
===================================================================
--- uspace/lib/c/include/ddi.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/include/ddi.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,5 +41,4 @@
 extern int physmem_map(void *, void *, unsigned long, int);
 extern int iospace_enable(task_id_t, void *, unsigned long);
-extern int preemption_control(int);
 extern int pio_enable(void *, size_t, void **);
 
Index: uspace/lib/c/include/err.h
===================================================================
--- uspace/lib/c/include/err.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/include/err.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -36,4 +36,11 @@
 #define LIBC_ERR_H_
 
+#include <stdio.h>
+#include <errno.h>
+
+#ifdef CONFIG_DEBUG
+#include <str_error.h>
+#endif
+
 #define errx(status, fmt, ...) { \
 	printf((fmt), ##__VA_ARGS__); \
@@ -41,4 +48,52 @@
 }
 
+
+/** An actual stored error code.  */
+#define ERROR_CODE  error_check_return_value
+
+/** An error processing routines declaration.
+ *
+ * This has to be declared in the block where the error processing
+ * is desired.
+ */
+#define ERROR_DECLARE  int ERROR_CODE
+
+/** Store the value as an error code and checks if an error occurred.
+ *
+ * @param[in] value	The value to be checked. May be a function call.
+ * @return		False if the value indicates success (EOK).
+ * @return		True otherwise.
+ */
+#ifdef CONFIG_DEBUG
+
+#define ERROR_OCCURRED(value) \
+	(((ERROR_CODE = (value)) != EOK) && \
+	({ \
+		fprintf(stderr, "libsocket error at %s:%d (%s)\n", \
+		__FILE__, __LINE__, str_error(ERROR_CODE)); \
+		1; \
+	}))
+
+#else
+
+#define ERROR_OCCURRED(value)	((ERROR_CODE = (value)) != EOK)
+
+#endif
+
+#define ERROR_NONE(value)	!ERROR_OCCURRED((value))
+
+/** Error propagation
+ *
+ * Check if an error occurred and immediately exit the actual
+ * function returning the error code.
+ *
+ * @param[in] value	The value to be checked. May be a function call.
+ *
+ */
+
+#define ERROR_PROPAGATE(value) \
+	if (ERROR_OCCURRED(value)) \
+		return ERROR_CODE
+
 #endif
 
Index: uspace/lib/c/include/errno.h
===================================================================
--- uspace/lib/c/include/errno.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/include/errno.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -56,4 +56,40 @@
 #define EMLINK        (-266)
 
+/** An API function is called while another blocking function is in progress. */
+#define EINPROGRESS	(-10036)
+
+/** The socket identifier is not valid. */
+#define ENOTSOCK	(-10038)
+
+/** The destination address required. */
+#define EDESTADDRREQ	(-10039)
+
+/** Protocol is not supported.  */
+#define EPROTONOSUPPORT	(-10043)
+
+/** Socket type is not supported. */
+#define ESOCKTNOSUPPORT	(-10044)
+
+/** Protocol family is not supported. */
+#define EPFNOSUPPORT	(-10046)
+
+/** Address family is not supported. */
+#define EAFNOSUPPORT	(-10047)
+
+/** Address is already in use. */
+#define EADDRINUSE	(-10048)
+
+/** The socket is not connected or bound. */
+#define ENOTCONN	(-10057)
+
+/** The requested operation was not performed.
+ *  Try again later.
+ */
+#define TRY_AGAIN	(-11002)
+
+/** No data.
+ */
+#define NO_DATA		(-11004)
+
 #endif
 
Index: pace/lib/c/include/limits.h
===================================================================
--- uspace/lib/c/include/limits.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,74 +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 libc
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_LIMITS_H_
-#define LIBC_LIMITS_H_
-
-#include <stdint.h>
-#include <libarch/limits.h>
-
-/* char */
-#define SCHAR_MIN MIN_INT8
-#define SCHAR_MAX MAX_INT8
-#define UCHAR_MIN MIN_UINT8
-#define UCHAR_MAX MAX_UINT8
-
-#ifdef __CHAR_UNSIGNED__
-# define CHAR_MIN UCHAR_MIN
-# define CHAR_MAX UCHAR_MAX
-#else
-# define CHAR_MIN SCHAR_MIN
-# define CHAR_MAX SCHAR_MAX
-#endif
-
-/* short int */
-#define SHRT_MIN MIN_INT16
-#define SHRT_MAX MAX_INT16
-#define USHRT_MIN MIN_UINT16
-#define USHRT_MAX MAX_UINT16
-
-#define INT_MIN MIN_INT32
-#define INT_MAX MAX_INT32
-#define UINT_MIN MIN_UINT32
-#define UINT_MAX MAX_UINT32
-
-#define LLONG_MIN MIN_INT64
-#define LLONG_MAX MAX_INT64
-#define ULLONG_MIN MIN_UINT64
-#define ULLONG_MAX MAX_UINT64
-
-#endif
-
-/** @}
- */
Index: uspace/lib/c/include/stdio.h
===================================================================
--- uspace/lib/c/include/stdio.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/c/include/stdio.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -171,4 +171,5 @@
 extern off64_t ftell(FILE *);
 extern int feof(FILE *);
+extern int fileno(FILE *);
 
 extern int fflush(FILE *);
Index: pace/lib/libpci/Makefile.build
===================================================================
--- uspace/lib/libpci/Makefile.build	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,64 +1,0 @@
-#
-# 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 = \
-	access.c \
-	generic.c \
-	names.c \
-	i386-ports.c
-
-OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
-
-.PHONY: all
-
-all: $(LIBPCI)
-
--include $(DEPEND)
-
-$(LIBPCI): $(OBJECTS)
-	$(AR) rc $@ $(OBJECTS)
-
-%.o: %.c $(DEPEND)
-	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
-ifeq ($(PRECHECK),y)
-	$(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
-endif
-
-$(DEPEND):
-	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
-	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: pace/lib/libpci/Makefile.common
===================================================================
--- uspace/lib/libpci/Makefile.common	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,37 +1,0 @@
-#
-# 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
-DEPEND = Makefile.depend
-DEPEND_PREV = $(DEPEND).prev
-JOB = libpci.job
-LIBPCI = libpci.a
Index: uspace/lib/net/adt/module_map.c
===================================================================
--- uspace/lib/net/adt/module_map.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/adt/module_map.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,8 +38,8 @@
 #include <task.h>
 #include <unistd.h>
+#include <err.h>
 
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 
Index: uspace/lib/net/generic/net_remote.c
===================================================================
--- uspace/lib/net/generic/net_remote.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/generic/net_remote.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -47,23 +47,32 @@
 #include <net_net_messages.h>
 
-int net_connect_module(services_t service){
+int net_connect_module(services_t service)
+{
 	return connect_to_service(SERVICE_NETWORKING);
 }
 
-void net_free_settings(measured_string_ref settings, char * data){
-	if(settings){
+void net_free_settings(measured_string_ref settings, char * data)
+{
+	if (settings)
 		free(settings);
-	}
-	if(data){
+
+	if (data)
 		free(data);
-	}
 }
 
-int net_get_conf_req(int net_phone, measured_string_ref * configuration, size_t count, char ** data){
-	return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0, * configuration, count, configuration, data);
+int
+net_get_conf_req(int net_phone, measured_string_ref * configuration,
+    size_t count, char ** data)
+{
+	return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0,
+	    *configuration, count, configuration, data);
 }
 
-int net_get_device_conf_req(int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data){
-	return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, device_id, 0, * configuration, count, configuration, data);
+int
+net_get_device_conf_req(int net_phone, device_id_t device_id,
+    measured_string_ref * configuration, size_t count, char ** data)
+{
+	return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF,
+	    device_id, 0, *configuration, count, configuration, data);
 }
 
Index: uspace/lib/net/generic/packet_remote.c
===================================================================
--- uspace/lib/net/generic/packet_remote.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/generic/packet_remote.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,8 +38,8 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <sys/mman.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <packet/packet.h>
@@ -64,12 +64,15 @@
  *
  */
-static int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){
+static int
+packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size)
+{
 	ERROR_DECLARE;
 	
 	ipc_call_t answer;
 	aid_t message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
+
 	*packet = (packet_t) as_get_mappable_page(size);
-	if (ERROR_OCCURRED(async_share_in_start_0_0(phone, *packet, size))
-	    || ERROR_OCCURRED(pm_add(*packet))) {
+	if (ERROR_OCCURRED(async_share_in_start_0_0(phone, *packet, size)) ||
+	    ERROR_OCCURRED(pm_add(*packet))) {
 		munmap(*packet, size);
 		async_wait_for(message, NULL);
@@ -91,14 +94,15 @@
 	
 	*packet = pm_find(packet_id);
-	if (!(*packet)) {
+	if (!*packet) {
 		ipcarg_t size;
 		
-		ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size));
+		ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE,
+		    packet_id, &size));
 		ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size));
 	}
-	if ((** packet).next) {
+	if ((*packet)->next) {
 		packet_t next;
 		
-		return packet_translate_remote(phone, &next, (** packet).next);
+		return packet_translate_remote(phone, &next, (*packet)->next);
 	}
 	
@@ -114,6 +118,6 @@
 	ipcarg_t size;
 	
-	if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content,
-	    addr_len, max_prefix, max_suffix, &packet_id, &size)))
+	if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4,
+	    max_content, addr_len, max_prefix, max_suffix, &packet_id, &size)))
 		return NULL;
 	
@@ -121,5 +125,6 @@
 	packet_t packet = pm_find(packet_id);
 	if (!packet) {
-		if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size)))
+		if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id,
+		    size)))
 			return NULL;
 	}
@@ -141,5 +146,6 @@
 	packet_t packet = pm_find(packet_id);
 	if (!packet) {
-		if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size)))
+		if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id,
+		    size)))
 			return NULL;
 	}
Index: uspace/lib/net/il/ip_client.c
===================================================================
--- uspace/lib/net/il/ip_client.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/il/ip_client.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,5 +40,4 @@
 
 #include <ip_client.h>
-#include <socket_errno.h>
 #include <packet/packet.h>
 #include <packet/packet_client.h>
Index: uspace/lib/net/il/ip_remote.c
===================================================================
--- uspace/lib/net/il/ip_remote.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/il/ip_remote.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -127,5 +127,5 @@
 		return EINVAL;
 	
-	if ((!device_id) || (header) || (headerlen))
+	if ((!device_id) || (!header) || (!headerlen))
 		return EBADMEM;
 	
Index: uspace/lib/net/include/netif_local.h
===================================================================
--- uspace/lib/net/include/netif_local.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/include/netif_local.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -44,7 +44,7 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
+#include <err.h>
 
 #include <adt/measured_strings.h>
-#include <net_err.h>
 #include <net_device.h>
 #include <packet/packet.h>
Index: uspace/lib/net/netif/netif_local.c
===================================================================
--- uspace/lib/net/netif/netif_local.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/netif/netif_local.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,6 +42,6 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/lib/net/tl/tl_common.c
===================================================================
--- uspace/lib/net/tl/tl_common.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/net/tl/tl_common.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,6 +38,7 @@
 #include <async.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <packet/packet.h>
 #include <packet/packet_client.h>
@@ -51,5 +52,4 @@
 #include <ip_remote.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <ip_interface.h>
 #include <tl_interface.h>
@@ -58,29 +58,32 @@
 DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
 
-int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
-	const struct sockaddr_in * address_in;
-	const struct sockaddr_in6 * address_in6;
-
-	if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
-		return EINVAL;
-	}
-	switch(addr->sa_family){
-		case AF_INET:
-			if(addrlen != sizeof(struct sockaddr_in)){
+int
+tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
+{
+	const struct sockaddr_in *address_in;
+	const struct sockaddr_in6 *address_in6;
+
+	if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
+		return EINVAL;
+
+	switch (addr->sa_family) {
+	case AF_INET:
+		if (addrlen != sizeof(struct sockaddr_in))
+			return EINVAL;
+
+		address_in = (struct sockaddr_in *) addr;
+		*port = ntohs(address_in->sin_port);
+		break;
+	case AF_INET6:
+		if (addrlen != sizeof(struct sockaddr_in6))
 				return EINVAL;
-			}
-			address_in = (struct sockaddr_in *) addr;
-			*port = ntohs(address_in->sin_port);
-			break;
-		case AF_INET6:
-			if(addrlen != sizeof(struct sockaddr_in6)){
-				return EINVAL;
-			}
-			address_in6 = (struct sockaddr_in6 *) addr;
-			*port = ntohs(address_in6->sin6_port);
-			break;
-		default:
-			return EAFNOSUPPORT;
-	}
+
+		address_in6 = (struct sockaddr_in6 *) addr;
+		*port = ntohs(address_in6->sin6_port);
+		break;
+	default:
+		return EAFNOSUPPORT;
+	}
+
 	return EOK;
 }
@@ -112,5 +115,6 @@
 		return EBADMEM;
 	
-	*packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
+	*packet_dimension = packet_dimensions_find(packet_dimensions,
+	    device_id);
 	if (!*packet_dimension) {
 		/* Ask for and remember them if not found */
@@ -136,60 +140,69 @@
 }
 
-int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
+int
+tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
+    device_id_t device_id, size_t content)
+{
 	packet_dimension_ref packet_dimension;
 
 	packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
-	if(! packet_dimension){
+	if (!packet_dimension)
 		return ENOENT;
-	}
 	packet_dimension->content = content;
-	if(device_id != DEVICE_INVALID_ID){
-		packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
-		if(packet_dimension){
-			if(packet_dimension->content >= content){
+
+	if (device_id != DEVICE_INVALID_ID) {
+		packet_dimension = packet_dimensions_find(packet_dimensions,
+		    DEVICE_INVALID_ID);
+
+		if (packet_dimension) {
+			if (packet_dimension->content >= content)
 				packet_dimension->content = content;
-			}else{
-				packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
-			}
+			else
+				packet_dimensions_exclude(packet_dimensions,
+				    DEVICE_INVALID_ID);
+
 		}
 	}
+
 	return EOK;
 }
 
-int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
-	struct sockaddr_in * address_in;
-	struct sockaddr_in6 * address_in6;
+int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
+{
+	struct sockaddr_in *address_in;
+	struct sockaddr_in6 *address_in6;
 	size_t length;
 
-	if(addrlen < 0){
-		return EINVAL;
-	}
+	if (addrlen < 0)
+		return EINVAL;
+	
 	length = (size_t) addrlen;
-	if(length < sizeof(struct sockaddr)){
-		return EINVAL;
-	}
-	switch(addr->sa_family){
-		case AF_INET:
-			if(length != sizeof(struct sockaddr_in)){
+	if (length < sizeof(struct sockaddr))
+		return EINVAL;
+
+	switch (addr->sa_family) {
+	case AF_INET:
+		if (length != sizeof(struct sockaddr_in))
+			return EINVAL;
+		address_in = (struct sockaddr_in *) addr;
+		address_in->sin_port = htons(port);
+		return EOK;
+	case AF_INET6:
+		if (length != sizeof(struct sockaddr_in6))
 				return EINVAL;
-			}
-			address_in = (struct sockaddr_in *) addr;
-			address_in->sin_port = htons(port);
-			return EOK;
-		case AF_INET6:
-			if(length != sizeof(struct sockaddr_in6)){
-				return EINVAL;
-			}
-			address_in6 = (struct sockaddr_in6 *) addr;
-			address_in6->sin6_port = htons(port);
-			return EOK;
-		default:
-			return EAFNOSUPPORT;
-	}
-}
-
-int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
+		address_in6 = (struct sockaddr_in6 *) addr;
+		address_in6->sin6_port = htons(port);
+		return EOK;
+	default:
+		return EAFNOSUPPORT;
+	}
+}
+
+int
+tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
+    services_t error)
+{
 	packet_t next;
-	uint8_t * src;
+	uint8_t *src;
 	int length;
 
@@ -200,17 +213,20 @@
 	
 	length = packet_get_addr(packet, &src, NULL);
-	if((length > 0)
-		&& (! error)
-		&& (icmp_phone >= 0)
-	// set both addresses to the source one (avoids the source address deletion before setting the destination one)
-		&& (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
+	if ((length > 0) && (!error) && (icmp_phone >= 0) &&
+	    // set both addresses to the source one (avoids the source address
+	    // deletion before setting the destination one)
+	    (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
 		return EOK;
-	}else{
+	} else
 		pq_release_remote(packet_phone, packet_get_id(packet));
-	}
+
 	return ENOENT;
 }
 
-int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){
+int
+tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
+    const packet_dimension_ref dimension, const struct sockaddr *addr,
+    socklen_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -219,29 +235,33 @@
 	void * data;
 
-	if(! dimension){
-		return EINVAL;
-	}
+	if (!dimension)
+		return EINVAL;
+
 	// get the data length
-	if(! async_data_write_receive(&callid, &length)){
-		return EINVAL;
-	}
+	if (!async_data_write_receive(&callid, &length))
+		return EINVAL;
+
 	// get a new packet
-	*packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
-	if(! packet){
+	*packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
+	    prefix + dimension->prefix, dimension->suffix);
+	if (!packet)
 		return ENOMEM;
-	}
+
 	// allocate space in the packet
 	data = packet_suffix(*packet, length);
-	if(! data){
+	if (!data) {
 		pq_release_remote(packet_phone, packet_get_id(*packet));
 		return ENOMEM;
 	}
+
 	// read the data into the packet
-	if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
-	// set the packet destination address
-		|| ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
+	if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
+	    // set the packet destination address
+	    ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
+	    addrlen))) {
 		pq_release_remote(packet_phone, packet_get_id(*packet));
 		return ERROR_CODE;
 	}
+
 	return (int) length;
 }
Index: uspace/lib/socket/Makefile
===================================================================
--- uspace/lib/socket/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,8 +42,5 @@
 	packet/packet.c \
 	packet/packet_client.c \
-	packet/packet_server.c \
-	adt/dynamic_fifo.c \
-	adt/measured_strings.c \
-	adt/char_map.c
+	packet/packet_server.c 
 
 include $(USPACE_PREFIX)/Makefile.common
Index: pace/lib/socket/adt/char_map.c
===================================================================
--- uspace/lib/socket/adt/char_map.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,234 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Character string to integer map implementation.
- *  @see char_map.h
- */
-
-#include <errno.h>
-#include <malloc.h>
-#include <mem.h>
-#include <unistd.h>
-
-#include <adt/char_map.h>
-
-/** Internal magic value for a&nbsp;consistency check.
- */
-#define CHAR_MAP_MAGIC_VALUE	0x12345611
-
-/** Adds the value with the key to the map.
- *  Creates new nodes to map the key.
- *  @param[in,out] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @param[in] value The integral value to be stored for the key character string.
- *  @returns EOK on success.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns EEXIST if the key character string is already used.
- */
-int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value);
-
-/** Returns the node assigned to the key from the map.
- *  @param[in] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @returns The node holding the integral value assigned to the key character string.
- *  @returns NULL if the key is not assigned a&nbsp;node.
- */
-char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, const size_t length);
-
-/** Returns the value assigned to the map.
- *  @param[in] map The character string to integer map.
- *  @returns The integral value assigned to the map.
- *  @returns CHAR_MAP_NULL if the map is not assigned a&nbsp;value.
- */
-int char_map_get_value(const char_map_ref map);
-
-/** Checks if the map is valid.
- *  @param[in] map The character string to integer map.
- *  @returns TRUE if the map is valid.
- *  @returns FALSE otherwise.
- */
-int char_map_is_valid(const char_map_ref map);
-
-int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value){
-	if(char_map_is_valid(map) && (identifier) && ((length) || (*identifier))){
-		int index;
-
-		for(index = 0; index < map->next; ++ index){
-			if(map->items[index]->c == * identifier){
-				++ identifier;
-				if((length > 1) || ((length == 0) && (*identifier))){
-					return char_map_add(map->items[index], identifier, length ? length - 1 : 0, value);
-				}else{
-					if(map->items[index]->value != CHAR_MAP_NULL){
-						return EEXISTS;
-					}
-					map->items[index]->value = value;
-					return EOK;
-				}
-			}
-		}
-		return char_map_add_item(map, identifier, length, value);
-	}
-	return EINVAL;
-}
-
-int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value){
-	if(map->next == (map->size - 1)){
-		char_map_ref *tmp;
-
-		tmp = (char_map_ref *) realloc(map->items, sizeof(char_map_ref) * 2 * map->size);
-		if(! tmp){
-			return ENOMEM;
-		}
-		map->size *= 2;
-		map->items = tmp;
-	}
-	map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t));
-	if(! map->items[map->next]){
-		return ENOMEM;
-	}
-	if(char_map_initialize(map->items[map->next]) != EOK){
-		free(map->items[map->next]);
-		map->items[map->next] = NULL;
-		return ENOMEM;
-	}
-	map->items[map->next]->c = * identifier;
-	++ identifier;
-	++ map->next;
-	if((length > 1) || ((length == 0) && (*identifier))){
-		map->items[map->next - 1]->value = CHAR_MAP_NULL;
-		return char_map_add_item(map->items[map->next - 1], identifier, length ? length - 1 : 0, value);
-	}else{
-		map->items[map->next - 1]->value = value;
-	}
-	return EOK;
-}
-
-void char_map_destroy(char_map_ref map){
-	if(char_map_is_valid(map)){
-		int index;
-
-		map->magic = 0;
-		for(index = 0; index < map->next; ++ index){
-			char_map_destroy(map->items[index]);
-		}
-		free(map->items);
-		map->items = NULL;
-	}
-}
-
-int char_map_exclude(char_map_ref map, const char * identifier, size_t length){
-	char_map_ref node;
-
-	node = char_map_find_node(map, identifier, length);
-	if(node){
-		int value;
-
-		value = node->value;
-		node->value = CHAR_MAP_NULL;
-		return value;
-	}
-	return CHAR_MAP_NULL;
-}
-
-int char_map_find(const char_map_ref map, const char * identifier, size_t length){
-	char_map_ref node;
-
-	node = char_map_find_node(map, identifier, length);
-	return node ? node->value : CHAR_MAP_NULL;
-}
-
-char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, size_t length){
-	if(! char_map_is_valid(map)){
-		return NULL;
-	}
-	if(length || (*identifier)){
-		int index;
-
-		for(index = 0; index < map->next; ++ index){
-			if(map->items[index]->c == * identifier){
-				++ identifier;
-				if(length == 1){
-					return map->items[index];
-				}
-				return char_map_find_node(map->items[index], identifier, length ? length - 1 : 0);
-			}
-		}
-		return NULL;
-	}
-	return map;
-}
-
-int char_map_get_value(const char_map_ref map){
-	return char_map_is_valid(map) ? map->value : CHAR_MAP_NULL;
-}
-
-int char_map_initialize(char_map_ref map){
-	if(! map){
-		return EINVAL;
-	}
-	map->c = '\0';
-	map->value = CHAR_MAP_NULL;
-	map->size = 2;
-	map->next = 0;
-	map->items = malloc(sizeof(char_map_ref) * map->size);
-	if(! map->items){
-		map->magic = 0;
-		return ENOMEM;
-	}
-	map->items[map->next] = NULL;
-	map->magic = CHAR_MAP_MAGIC_VALUE;
-	return EOK;
-}
-
-int char_map_is_valid(const char_map_ref map){
-	return map && (map->magic == CHAR_MAP_MAGIC_VALUE);
-}
-
-int char_map_update(char_map_ref map, const char * identifier, const size_t length, const int value){
-	char_map_ref node;
-
-//	if(! char_map_is_valid(map)) return EINVAL;
-	node = char_map_find_node(map, identifier, length);
-	if(node){
-		node->value = value;
-		return EOK;
-	}else{
-		return char_map_add(map, identifier, length, value);
-	}
-}
-
-/** @}
- */
Index: pace/lib/socket/adt/dynamic_fifo.c
===================================================================
--- uspace/lib/socket/adt/dynamic_fifo.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,151 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Dynamic first in first out positive integer queue implementation.
- */
-
-#include <errno.h>
-#include <malloc.h>
-#include <mem.h>
-
-#include <adt/dynamic_fifo.h>
-
-/** Internal magic value for a&nbsp;consistency check.
- */
-#define DYN_FIFO_MAGIC_VALUE	0x58627659
-
-/** Returns the next queue index.
- *  The queue field is circular.
- *  @param[in] fifo The dynamic queue.
- *  @param[in] index The actual index to be shifted.
- */
-#define NEXT_INDEX(fifo, index)	(((index) + 1) % ((fifo)->size + 1))
-
-/** Checks if the queue is valid.
- *  @param[in] fifo The dynamic queue.
- *  @returns TRUE if the queue is valid.
- *  @returns FALSE otherwise.
- */
-static int dyn_fifo_is_valid(dyn_fifo_ref fifo){
-	return fifo && (fifo->magic_value == DYN_FIFO_MAGIC_VALUE);
-}
-
-int dyn_fifo_initialize(dyn_fifo_ref fifo, int size){
-	if(! fifo){
-		return EBADMEM;
-	}
-	if(size <= 0){
-		return EINVAL;
-	}
-	fifo->items = (int *) malloc(sizeof(int) * size + 1);
-	if(! fifo->items){
-		return ENOMEM;
-	}
-	fifo->size = size;
-	fifo->head = 0;
-	fifo->tail = 0;
-	fifo->magic_value = DYN_FIFO_MAGIC_VALUE;
-	return EOK;
-}
-
-int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size){
-	int * new_items;
-
-	if(! dyn_fifo_is_valid(fifo)){
-		return EINVAL;
-	}
-	if(NEXT_INDEX(fifo, fifo->tail) == fifo->head){
-		if((max_size > 0) && ((fifo->size * 2) > max_size)){
-			if(fifo->size >= max_size){
-				return ENOMEM;
-			}
-		}else{
-			max_size = fifo->size * 2;
-		}
-		new_items = realloc(fifo->items, sizeof(int) * max_size + 1);
-		if(! new_items){
-			return ENOMEM;
-		}
-		fifo->items = new_items;
-		if(fifo->tail < fifo->head){
-			if(fifo->tail < max_size - fifo->size){
-				memcpy(fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof(int));
-				fifo->tail += fifo->size + 1;
-			}else{
-				memcpy(fifo->items + fifo->size + 1, fifo->items, (max_size - fifo->size) * sizeof(int));
-				memcpy(fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size);
-				fifo->tail -= max_size - fifo->size;
-			}
-		}
-		fifo->size = max_size;
-	}
-	fifo->items[fifo->tail] = value;
-	fifo->tail = NEXT_INDEX(fifo, fifo->tail);
-	return EOK;
-}
-
-int dyn_fifo_pop(dyn_fifo_ref fifo){
-	int value;
-
-	if(! dyn_fifo_is_valid(fifo)){
-		return EINVAL;
-	}
-	if(fifo->head == fifo->tail){
-		return ENOENT;
-	}
-	value = fifo->items[fifo->head];
-	fifo->head = NEXT_INDEX(fifo, fifo->head);
-	return value;
-}
-
-int dyn_fifo_value(dyn_fifo_ref fifo){
-	if(! dyn_fifo_is_valid(fifo)){
-		return EINVAL;
-	}
-	if(fifo->head == fifo->tail){
-		return ENOENT;
-	}
-	return fifo->items[fifo->head];
-}
-
-int dyn_fifo_destroy(dyn_fifo_ref fifo){
-	if(! dyn_fifo_is_valid(fifo)){
-		return EINVAL;
-	}
-	free(fifo->items);
-	fifo->magic_value = 0;
-	return EOK;
-}
-
-/** @}
- */
Index: pace/lib/socket/adt/measured_strings.c
===================================================================
--- uspace/lib/socket/adt/measured_strings.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,284 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Character string with measured length implementation.
- *  @see measured_strings.h
- */
-
-#include <errno.h>
-#include <malloc.h>
-#include <mem.h>
-#include <unistd.h>
-
-#include <ipc/ipc.h>
-
-#include <net_err.h>
-#include <net_modules.h>
-#include <adt/measured_strings.h>
-
-measured_string_ref measured_string_create_bulk(const char * string, size_t length){
-	measured_string_ref new;
-
-	if(length == 0){
-		while(string[length]){
-			++ length;
-		}
-	}
-	new = (measured_string_ref) malloc(sizeof(measured_string_t) + (sizeof(char) * (length + 1)));
-	if(! new){
-		return NULL;
-	}
-	new->length = length;
-	new->value = ((char *) new) + sizeof(measured_string_t);
-	// append terminating zero explicitly - to be safe
-	memcpy(new->value, string, new->length);
-	new->value[new->length] = '\0';
-	return new;
-}
-
-measured_string_ref measured_string_copy(measured_string_ref source){
-	measured_string_ref new;
-
-	if(! source){
-		return NULL;
-	}
-	new = (measured_string_ref) malloc(sizeof(measured_string_t));
-	if(new){
-		new->value = (char *) malloc(source->length + 1);
-		if(new->value){
-			new->length = source->length;
-			memcpy(new->value, source->value, new->length);
-			new->value[new->length] = '\0';
-			return new;
-		}else{
-			free(new);
-		}
-	}
-	return NULL;
-}
-
-int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){
-	ERROR_DECLARE;
-
-	size_t * lengths;
-	size_t index;
-	size_t length;
-	char * next;
-	ipc_callid_t callid;
-
-	if((! strings) || (! data) || (count <= 0)){
-		return EINVAL;
-	}
-	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
-	if(! lengths){
-		return ENOMEM;
-	}
-	if((! async_data_write_receive(&callid, &length))
-		|| (length != sizeof(size_t) * (count + 1))){
-		free(lengths);
-		return EINVAL;
-	}
-	if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
-		free(lengths);
-		return ERROR_CODE;
-	}
-	*data = malloc(lengths[count]);
-	if(!(*data)){
-		return ENOMEM;
-	}
-	(*data)[lengths[count] - 1] = '\0';
-	*strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
-	if(!(*strings)){
-		free(lengths);
-		free(*data);
-		return ENOMEM;
-	}
-	next = * data;
-	for(index = 0; index < count; ++ index){
-		(*strings)[index].length = lengths[index];
-		if(lengths[index] > 0){
-			if((! async_data_write_receive(&callid, &length))
-				|| (length != lengths[index])){
-				free(*data);
-				free(*strings);
-				free(lengths);
-				return EINVAL;
-			}
-			ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
-			(*strings)[index].value = next;
-			next += lengths[index];
-			*next = '\0';
-			++ next;
-		}else{
-			(*strings)[index].value = NULL;
-		}
-	}
-	free(lengths);
-	return EOK;
-}
-
-/** Computes the lengths of the measured strings in the given array.
- *  @param[in] strings The measured strings array to be processed.
- *  @param[in] count The measured strings array size.
- *  @returns The computed sizes array.
- *  @returns NULL if there is not enough memory left.
- */
-static size_t * prepare_lengths(const measured_string_ref strings, size_t count){
-	size_t * lengths;
-	size_t index;
-	size_t length;
-
-	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
-	if(! lengths){
-		return NULL;
-	}
-	length = 0;
-	for(index = 0; index < count; ++ index){
-		lengths[index] = strings[index].length;
-		length += lengths[index] + 1;
-	}
-	lengths[count] = length;
-	return lengths;
-}
-
-int measured_strings_reply(const measured_string_ref strings, size_t count){
-	ERROR_DECLARE;
-
-	size_t * lengths;
-	size_t index;
-	size_t length;
-	ipc_callid_t callid;
-
-	if((! strings) || (count <= 0)){
-		return EINVAL;
-	}
-	lengths = prepare_lengths(strings, count);
-	if(! lengths){
-		return ENOMEM;
-	}
-	if((! async_data_read_receive(&callid, &length))
-		|| (length != sizeof(size_t) * (count + 1))){
-		free(lengths);
-		return EINVAL;
-	}
-	if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
-		free(lengths);
-		return ERROR_CODE;
-	}
-	free(lengths);
-	for(index = 0; index < count; ++ index){
-		if(strings[index].length > 0){
-			if((! async_data_read_receive(&callid, &length))
-				|| (length != strings[index].length)){
-				return EINVAL;
-			}
-			ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length));
-		}
-	}
-	return EOK;
-}
-
-int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){
-	ERROR_DECLARE;
-
-	size_t * lengths;
-	size_t index;
-	char * next;
-
-	if((phone <= 0) || (! strings) || (! data) || (count <= 0)){
-		return EINVAL;
-	}
-	lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
-	if(! lengths){
-		return ENOMEM;
-	}
-	if(ERROR_OCCURRED(async_data_read_start(phone, lengths, sizeof(size_t) * (count + 1)))){
-		free(lengths);
-		return ERROR_CODE;
-	}
-	*data = malloc(lengths[count]);
-	if(!(*data)){
-		return ENOMEM;
-	}
-	*strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
-	if(!(*strings)){
-		free(lengths);
-		free(*data);
-		return ENOMEM;
-	}
-	next = * data;
-	for(index = 0; index < count; ++ index){
-		(*strings)[index].length = lengths[index];
-		if(lengths[index] > 0){
-			ERROR_PROPAGATE(async_data_read_start(phone, next, lengths[index]));
-			(*strings)[index].value = next;
-			next += lengths[index];
-			*next = '\0';
-			++ next;
-		}else{
-			(*strings)[index].value = NULL;
-		}
-	}
-	free(lengths);
-	return EOK;
-}
-
-int measured_strings_send(int phone, const measured_string_ref strings, size_t count){
-	ERROR_DECLARE;
-
-	size_t * lengths;
-	size_t index;
-
-	if((phone <= 0) || (! strings) || (count <= 0)){
-		return EINVAL;
-	}
-	lengths = prepare_lengths(strings, count);
-	if(! lengths){
-		return ENOMEM;
-	}
-	if(ERROR_OCCURRED(async_data_write_start(phone, lengths, sizeof(size_t) * (count + 1)))){
-		free(lengths);
-		return ERROR_CODE;
-	}
-	free(lengths);
-	for(index = 0; index < count; ++ index){
-		if(strings[index].length > 0){
-			ERROR_PROPAGATE(async_data_write_start(phone, strings[index].value, strings[index].length));
-		}
-	}
-	return EOK;
-}
-
-/** @}
- */
-
Index: uspace/lib/socket/generic/net_modules.c
===================================================================
--- uspace/lib/socket/generic/net_modules.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/generic/net_modules.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -37,4 +37,5 @@
 #include <async.h>
 #include <malloc.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
@@ -43,5 +44,4 @@
 #include <sys/time.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 
Index: uspace/lib/socket/generic/socket_client.c
===================================================================
--- uspace/lib/socket/generic/socket_client.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/generic/socket_client.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,12 +42,12 @@
 #include <stdint.h>
 #include <stdlib.h>
+#include <errno.h>
+#include <err.h>
 
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <in.h>
 #include <socket.h>
-#include <socket_errno.h>
 #include <adt/dynamic_fifo.h>
 #include <adt/int_map.h>
Index: uspace/lib/socket/generic/socket_core.c
===================================================================
--- uspace/lib/socket/generic/socket_core.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/generic/socket_core.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -37,10 +37,10 @@
 #include <stdint.h>
 #include <stdlib.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <in.h>
 #include <inet.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <adt/dynamic_fifo.h>
 #include <adt/int_map.h>
Index: pace/lib/socket/include/adt/char_map.h
===================================================================
--- uspace/lib/socket/include/adt/char_map.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,142 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Character string to integer map.
- */
-
-#ifndef __CHAR_MAP_H__
-#define __CHAR_MAP_H__
-
-/** Invalid assigned value used also if an&nbsp;entry does not exist.
- */
-#define CHAR_MAP_NULL	(-1)
-
-/** Type definition of the character string to integer map.
- *  @see char_map
- */
-typedef struct char_map	char_map_t;
-
-/** Type definition of the character string to integer map pointer.
- *  @see char_map
- */
-typedef char_map_t *	char_map_ref;
-
-/** Character string to integer map item.
- *  This structure recursivelly contains itself as a&nbsp;character by character tree.
- *  The actually mapped character string consists o fall the parent characters and the actual one.
- */
-struct	char_map{
-	/** Actually mapped character.
-	 */
-	char c;
-	/** Stored integral value.
-	 */
-	int value;
-	/** Next character array size.
-	 */
-	int size;
-	/** First free position in the next character array.
-	 */
-	int next;
-	/** Next character array.
-	 */
-	char_map_ref * items;
-	/** Consistency check magic value.
-	 */
-	int magic;
-};
-
-/** Adds the value with the key to the map.
- *  @param[in,out] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @param[in] value The integral value to be stored for the key character string.
- *  @returns EOK on success.
- *  @returns EINVAL if the map is not valid.
- *  @returns EINVAL if the identifier parameter is NULL.
- *  @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\\0') character.
- *  @returns EEXIST if the key character string is already used.
- *  @returns Other error codes as defined for the char_map_add_item() function.
- */
-extern int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value);
-
-/** Clears and destroys the map.
- *  @param[in,out] map The character string to integer map.
- */
-extern void char_map_destroy(char_map_ref map);
-
-/** Excludes the value assigned to the key from the map.
- *  The entry is cleared from the map.
- *  @param[in,out] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @returns The integral value assigned to the key character string.
- *  @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
- */
-extern int char_map_exclude(char_map_ref map, const char * identifier, size_t length);
-
-/** Returns the value assigned to the key from the map.
- *  @param[in] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @returns The integral value assigned to the key character string.
- *  @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
- */
-extern int char_map_find(const char_map_ref map, const char * identifier, size_t length);
-
-/** Initializes the map.
- *  @param[in,out] map The character string to integer map.
- *  @returns EOK on success.
- *  @returns EINVAL if the map parameter is NULL.
- *  @returns ENOMEM if there is not enough memory left.
- */
-extern int char_map_initialize(char_map_ref map);
-
-/** Adds or updates the value with the key to the map.
- *  @param[in,out] map The character string to integer map.
- *  @param[in] identifier The key zero ('\\0') terminated character string. The key character string is processed until the first terminating zero ('\\0') character after the given length is found.
- *  @param[in] length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\\0') character is found.
- *  @param[in] value The integral value to be stored for the key character string.
- *  @returns EOK on success.
- *  @returns EINVAL if the map is not valid.
- *  @returns EINVAL if the identifier parameter is NULL.
- *  @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\\0) character.
- *  @returns EEXIST if the key character string is already used.
- *  @returns Other error codes as defined for the char_map_add_item() function.
- */
-extern int char_map_update(char_map_ref map, const char * identifier, size_t length, const int value);
-
-#endif
-
-/** @}
- */
Index: pace/lib/socket/include/adt/dynamic_fifo.h
===================================================================
--- uspace/lib/socket/include/adt/dynamic_fifo.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,119 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Dynamic first in first out positive integer queue.
- *  Possitive integer values only.
- */
-
-#ifndef __NET_DYNAMIC_FIFO_H__
-#define __NET_DYNAMIC_FIFO_H__
-
-/** Type definition of the dynamic fifo queue.
- *  @see dyn_fifo
- */
-typedef struct dyn_fifo	dyn_fifo_t;
-
-/** Type definition of the dynamic fifo queue pointer.
- *  @see dyn_fifo
- */
-typedef dyn_fifo_t *	dyn_fifo_ref;
-
-/** Dynamic first in first out positive integer queue.
- *  Possitive integer values only.
- *  The queue automatically resizes if needed.
- */
-struct dyn_fifo{
-	/** Stored item field.
-	 */
-	int *	items;
-	/** Actual field size.
-	 */
-	int size;
-	/** First item in the queue index.
-	 */
-	int head;
-	/** Last item in the queue index.
-	 */
-	int tail;
-	/** Consistency check magic value.
-	 */
-	int magic_value;
-};
-
-/** Initializes the dynamic queue.
- *  @param[in,out] fifo The dynamic queue.
- *  @param[in] size The initial queue size.
- *  @returns EOK on success.
- *  @returns EINVAL if the queue is not valid.
- *  @returns EBADMEM if the fifo parameter is NULL.
- *  @returns ENOMEM if there is not enough memory left.
- */
-extern int dyn_fifo_initialize(dyn_fifo_ref fifo, int size);
-
-/** Appends a new item to the queue end.
- *  @param[in,out] fifo The dynamic queue.
- *  @param[in] value The new item value. Should be positive.
- *  @param[in] max_size The maximum queue size. The queue is not resized beyound this limit. May be zero or negative (<=0) to indicate no limit.
- *  @returns EOK on success.
- *  @returns EINVAL if the queue is not valid.
- *  @returns ENOMEM if there is not enough memory left.
- */
-extern int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size);
-
-/** Returns and excludes the first item in the queue.
- *  @param[in,out] fifo The dynamic queue.
- *  @returns Value of the first item in the queue.
- *  @returns EINVAL if the queue is not valid.
- *  @returns ENOENT if the queue is empty.
- */
-extern int dyn_fifo_pop(dyn_fifo_ref fifo);
-
-/** Returns and keeps the first item in the queue.
- *  @param[in,out] fifo The dynamic queue.
- *  @returns Value of the first item in the queue.
- *  @returns EINVAL if the queue is not valid.
- *  @returns ENOENT if the queue is empty.
- */
-extern int dyn_fifo_value(dyn_fifo_ref fifo);
-
-/** Clears and destroys the queue.
- *  @param[in,out] fifo The dynamic queue.
- *  @returns EOK on success.
- *  @returns EINVAL if the queue is not valid.
- */
-extern int dyn_fifo_destroy(dyn_fifo_ref fifo);
-
-#endif
-
-/** @}
- */
Index: pace/lib/socket/include/adt/generic_char_map.h
===================================================================
--- uspace/lib/socket/include/adt/generic_char_map.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,161 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Character string to generic type map.
- */
-
-#ifndef __GENERIC_CHAR_MAP_H__
-#define __GENERIC_CHAR_MAP_H__
-
-#include <errno.h>
-#include <unistd.h>
-
-#include <net_err.h>
-
-#include <adt/char_map.h>
-#include <adt/generic_field.h>
-
-/** Internal magic value for a&nbsp;map consistency check.
- */
-#define GENERIC_CHAR_MAP_MAGIC_VALUE	0x12345622
-
-/** Character string to generic type map declaration.
- *  @param[in] name Name of the map.
- *  @param[in] type Inner object type.
- */
-#define GENERIC_CHAR_MAP_DECLARE(name, type)									\
-																				\
-GENERIC_FIELD_DECLARE(name##_items, type)										\
-																				\
-typedef	struct name		name##_t;												\
-typedef	name##_t *		name##_ref;												\
-																				\
-struct	name{																	\
-	char_map_t names;														\
-	name##_items_t values;														\
-	int magic;														\
-};																				\
-																				\
-int name##_add(name##_ref map, const char * name, const size_t length, type * value);	\
-int name##_count(name##_ref map);												\
-void name##_destroy(name##_ref map);											\
-void name##_exclude(name##_ref map, const char * name, const size_t length);	\
-type * name##_find(name##_ref map, const char * name, const size_t length);		\
-int name##_initialize(name##_ref map);											\
-int name##_is_valid(name##_ref map);
-
-/** Character string to generic type map implementation.
- *  Should follow declaration with the same parameters.
- *  @param[in] name Name of the map.
- *  @param[in] type Inner object type.
- */
-#define GENERIC_CHAR_MAP_IMPLEMENT(name, type)									\
-																				\
-GENERIC_FIELD_IMPLEMENT(name##_items, type)										\
-																				\
-int name##_add(name##_ref map, const char * name, const size_t length, type * value){	\
-	ERROR_DECLARE;																\
-																				\
-	int index;																	\
-																				\
-	if(! name##_is_valid(map)){													\
-		return EINVAL;															\
-	}																			\
-	index = name##_items_add(&map->values, value);								\
-	if(index < 0){																\
-		return index;															\
-	}																			\
-	if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){			\
-		name##_items_exclude_index(&map->values, index);						\
-		return ERROR_CODE;														\
-	}																			\
-	return EOK;																	\
-}																				\
-																				\
-int name##_count(name##_ref map){												\
-	return name##_is_valid(map) ? name##_items_count(&map->values) : -1;		\
-}																				\
-																				\
-void name##_destroy(name##_ref map){											\
-	if(name##_is_valid(map)){													\
-		char_map_destroy(&map->names);											\
-		name##_items_destroy(&map->values);										\
-	}																			\
-}																				\
-																				\
-void name##_exclude(name##_ref map, const char * name, const size_t length){	\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		index = char_map_exclude(&map->names, name, length);					\
-		if(index != CHAR_MAP_NULL){												\
-			name##_items_exclude_index(&map->values, index);					\
-		}																		\
-	}																			\
-}																				\
-																				\
-type * name##_find(name##_ref map, const char * name, const size_t length){		\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		index = char_map_find(&map->names, name, length);						\
-		if(index != CHAR_MAP_NULL){												\
-			return name##_items_get_index(&map->values, index);					\
-		}																		\
-	}																			\
-	return NULL;																\
-}																				\
-																				\
-int name##_initialize(name##_ref map){											\
-	ERROR_DECLARE;																\
-																				\
-	if(! map){																	\
-		return EINVAL;															\
-	}																			\
-	ERROR_PROPAGATE(char_map_initialize(&map->names));							\
-	if(ERROR_OCCURRED(name##_items_initialize(&map->values))){					\
-		char_map_destroy(&map->names);											\
-		return ERROR_CODE;														\
-	}																			\
-	map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE;									\
-	return EOK;																	\
-}																				\
-																				\
-int name##_is_valid(name##_ref map){											\
-	return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE);					\
-}
-
-#endif
-
-/** @}
- */
Index: pace/lib/socket/include/adt/generic_field.h
===================================================================
--- uspace/lib/socket/include/adt/generic_field.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,160 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Generic type field.
- */
-
-#ifndef __GENERIC_FIELD_H__
-#define __GENERIC_FIELD_H__
-
-#include <errno.h>
-#include <malloc.h>
-#include <mem.h>
-#include <unistd.h>
-
-/** Internal magic value for a&nbsp;field consistency check.
- */
-#define GENERIC_FIELD_MAGIC_VALUE		0x55667788
-
-/** Generic type field declaration.
- *  @param[in] name Name of the field.
- *  @param[in] type Inner object type.
- */
-#define GENERIC_FIELD_DECLARE(name, type)										\
-																				\
-typedef	struct name		name##_t;												\
-typedef	name##_t *		name##_ref;												\
-																				\
-struct	name{																	\
-	int size;																	\
-	int next;																	\
-	type **	items;																\
-	int magic;																	\
-};																				\
-																				\
-int name##_add(name##_ref field, type * value);									\
-int name##_count(name##_ref field);												\
-void name##_destroy(name##_ref field);											\
-void name##_exclude_index(name##_ref field, int index);							\
-type ** name##_get_field(name##_ref field);										\
-type * name##_get_index(name##_ref field, int index);							\
-int name##_initialize(name##_ref field);										\
-int name##_is_valid(name##_ref field);
-
-/** Generic type field implementation.
- *  Should follow declaration with the same parameters.
- *  @param[in] name Name of the field.
- *  @param[in] type Inner object type.
- */
-#define GENERIC_FIELD_IMPLEMENT(name, type)										\
-																				\
-int name##_add(name##_ref field, type * value){									\
-	if(name##_is_valid(field)){													\
-		if(field->next == (field->size - 1)){									\
-			type **	tmp;														\
-																				\
-			tmp = (type **) realloc(field->items, sizeof(type *) * 2 * field->size);	\
-			if(! tmp){															\
-				return ENOMEM;													\
-			}																	\
-			field->size *= 2;													\
-			field->items = tmp;													\
-		}																		\
-		field->items[field->next] = value;										\
-		++ field->next;															\
-		field->items[field->next] = NULL;										\
-		return field->next - 1;													\
-	}																			\
-	return EINVAL;																\
-}																				\
-																				\
-int name##_count(name##_ref field){												\
-	return name##_is_valid(field) ? field->next : -1;							\
-}																				\
-																				\
-void name##_destroy(name##_ref field){											\
-	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]);										\
-			}																	\
-		}																		\
-		free(field->items);														\
-	}																			\
-}																				\
-																				\
-void name##_exclude_index(name##_ref field, int index){							\
-	if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){	\
-		free(field->items[index]);												\
-		field->items[index] = NULL;												\
-	}																			\
-}																				\
-																				\
-type * name##_get_index(name##_ref field, int index){							\
-	if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){	\
-		return field->items[index];												\
-	}																			\
-	return NULL;																\
-}																				\
-																				\
-type ** name##_get_field(name##_ref field){										\
-	return name##_is_valid(field) ? field->items : NULL;						\
-}																				\
-																				\
-int name##_initialize(name##_ref field){										\
-	if(! field){																\
-		return EINVAL;															\
-	}																			\
-	field->size = 2;															\
-	field->next = 0;															\
-	field->items = (type **) malloc(sizeof(type *) * field->size);				\
-	if(! field->items){															\
-		return ENOMEM;															\
-	}																			\
-	field->items[field->next] = NULL;											\
-	field->magic = GENERIC_FIELD_MAGIC_VALUE;									\
-	return EOK;																	\
-}																				\
-																				\
-int name##_is_valid(name##_ref field){											\
-	return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE);				\
-}
-
-#endif
-
-/** @}
- */
-
Index: pace/lib/socket/include/adt/int_map.h
===================================================================
--- uspace/lib/socket/include/adt/int_map.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,247 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Integer to generic type map.
- */
-
-#ifndef __NET_INT_MAP_H__
-#define __NET_INT_MAP_H__
-
-#include <errno.h>
-#include <malloc.h>
-#include <mem.h>
-#include <unistd.h>
-
-/** Internal magic value for a&nbsp;map consistency check.
- */
-#define INT_MAP_MAGIC_VALUE			0x11223344
-
-/** Internal magic value for an item consistency check.
- */
-#define INT_MAP_ITEM_MAGIC_VALUE	0x55667788
-
-/** Integer to generic type map declaration.
- *  @param[in] name Name of the map.
- *  @param[in] type Inner object type.
- */
-#define INT_MAP_DECLARE(name, type)												\
-																				\
-typedef	struct name			name##_t;											\
-typedef	name##_t *			name##_ref;											\
-typedef	struct name##_item	name##_item_t;										\
-typedef	name##_item_t *		name##_item_ref;									\
-																				\
-struct	name##_item{															\
-	int key;																\
-	type * value;																\
-	int magic;																\
-};																				\
-																				\
-struct	name{																	\
-	int size;														\
-	int next;														\
-	name##_item_ref items;														\
-	int magic;														\
-};																				\
-																				\
-int name##_add(name##_ref map, int key, type * value);							\
-void name##_clear(name##_ref map);												\
-int name##_count(name##_ref map);												\
-void name##_destroy(name##_ref map);											\
-void name##_exclude(name##_ref map, int key);									\
-void name##_exclude_index(name##_ref map, int index);							\
-type * name##_find(name##_ref map, int key);									\
-int name##_update(name##_ref map, int key, int new_key);						\
-type * name##_get_index(name##_ref map, int index);								\
-int name##_initialize(name##_ref map);											\
-int name##_is_valid(name##_ref map);											\
-void name##_item_destroy(name##_item_ref item);									\
-int name##_item_is_valid(name##_item_ref item);
-
-/** Integer to generic type map implementation.
- *  Should follow declaration with the same parameters.
- *  @param[in] name Name of the map.
- *  @param[in] type Inner object type.
- */
-#define INT_MAP_IMPLEMENT(name, type)											\
-																				\
-int name##_add(name##_ref map, int key, type * value){							\
-	if(name##_is_valid(map)){													\
-		if(map->next == (map->size - 1)){										\
-			name##_item_ref tmp;												\
-																				\
-			tmp = (name##_item_ref) realloc(map->items, sizeof(name##_item_t) * 2 * map->size);	\
-			if(! tmp){															\
-				return ENOMEM;													\
-			}																	\
-			map->size *= 2;														\
-			map->items = tmp;													\
-		}																		\
-		map->items[map->next].key = key;										\
-		map->items[map->next].value = value;									\
-		map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE;					\
-		++ map->next;															\
-		map->items[map->next].magic = 0;										\
-		return map->next - 1;													\
-	}																			\
-	return EINVAL;																\
-}																				\
-																				\
-void name##_clear(name##_ref map){												\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-/*		map->magic = 0;*/														\
-		for(index = 0; index < map->next; ++ index){							\
-			if(name##_item_is_valid(&(map->items[index]))){						\
-				name##_item_destroy(&(map->items[index]));						\
-			}																	\
-		}																		\
-		map->next = 0;															\
-		map->items[map->next].magic = 0;										\
-/*		map->magic = INT_MAP_MAGIC_VALUE;*/										\
-	}																			\
-}																				\
-																				\
-int name##_count(name##_ref map){												\
-	return name##_is_valid(map) ? map->next : -1;								\
-}																				\
-																				\
-void name##_destroy(name##_ref map){											\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		map->magic = 0;															\
-		for(index = 0; index < map->next; ++ index){							\
-			if(name##_item_is_valid(&(map->items[index]))){						\
-				name##_item_destroy(&(map->items[index]));						\
-			}																	\
-		}																		\
-		free(map->items);														\
-	}																			\
-}																				\
-																				\
-void name##_exclude(name##_ref map, int key){									\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		for(index = 0; index < map->next; ++ index){							\
-			if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){	\
-				name##_item_destroy(&(map->items[index]));						\
-			}																	\
-		}																		\
-	}																			\
-}																				\
-																				\
-void name##_exclude_index(name##_ref map, int index){							\
-	if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){	\
-		name##_item_destroy(&(map->items[index]));								\
-	}																			\
-}																				\
-																				\
-type * name##_find(name##_ref map, int key){									\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		for(index = 0; index < map->next; ++ index){							\
-			if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){	\
-				return map->items[index].value;									\
-			}																	\
-		}																		\
-	}																			\
-	return NULL;																\
-}																				\
-																				\
-int name##_update(name##_ref map, int key, int new_key){						\
-	if(name##_is_valid(map)){													\
-		int index;																\
-																				\
-		for(index = 0; index < map->next; ++ index){							\
-			if(name##_item_is_valid(&(map->items[index]))){						\
-				if(map->items[index].key == new_key){							\
-					return EEXIST;												\
-				}else if(map->items[index].key == key){							\
-					map->items[index].key = new_key;							\
-					return EOK;													\
-				}																\
-			}																	\
-		}																		\
-	}																			\
-	return ENOENT;																\
-}																				\
-																				\
-type * name##_get_index(name##_ref map, int index){								\
-	if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){	\
-		return map->items[index].value;											\
-	}																			\
-	return NULL;																\
-}																				\
-																				\
-int name##_initialize(name##_ref map){											\
-	if(! map){																	\
-		return EINVAL;															\
-	}																			\
-	map->size = 2;																\
-	map->next = 0;																\
-	map->items = (name##_item_ref) malloc(sizeof(name##_item_t) * map->size);	\
-	if(! map->items){															\
-		return ENOMEM;															\
-	}																			\
-	map->items[map->next].magic = 0;											\
-	map->magic = INT_MAP_MAGIC_VALUE;											\
-	return EOK;																	\
-}																				\
-																				\
-int name##_is_valid(name##_ref map){											\
-	return map && (map->magic == INT_MAP_MAGIC_VALUE);							\
-}																				\
-																				\
-void name##_item_destroy(name##_item_ref item){									\
-	if(name##_item_is_valid(item)){												\
-		item->magic = 0;														\
-		if(item->value){														\
-			free(item->value);													\
-			item->value = NULL;													\
-		}																		\
-	}																			\
-}																				\
-																				\
-int name##_item_is_valid(name##_item_ref item){									\
-	return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE);					\
-}
-
-#endif
-
-/** @}
- */
-
Index: pace/lib/socket/include/adt/measured_strings.h
===================================================================
--- uspace/lib/socket/include/adt/measured_strings.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,144 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Character string with measured length.
- *  The structure has been designed for serialization of character strings between modules.
- */
-
-#ifndef __MEASURED_STRINGS_H__
-#define __MEASURED_STRINGS_H__
-
-#include <sys/types.h>
-
-/** Type definition of the character string with measured length.
- *  @see measured_string
- */
-typedef struct measured_string	measured_string_t;
-
-/** Type definition of the character string with measured length pointer.
- *  @see measured_string
- */
-typedef measured_string_t *		measured_string_ref;
-
-/** Character string with measured length.
- *  This structure has been designed for serialization of character strings between modules.
- */
-struct	measured_string{
-	/** Character string data.
-	 */
-	char * value;
-	/** Character string length.
-	 */
-	size_t length;
-};
-
-/** Creates a&nbsp;new measured string bundled with a&nbsp;copy of the given string itself as one memory block.
- *  If the measured string is being freed, whole memory block is freed.
- *  The measured string should be used only as a&nbsp;constant.
- *  @param[in] string The initial character string to be stored.
- *  @param[in] length The length of the given string without the terminating zero ('/0') character. If the length is zero (0), the actual length is computed. The given length is used and appended with the terminating zero ('\\0') character otherwise.
- *  @returns The new bundled character string with measured length.
- *  @returns NULL if there is not enough memory left.
- */
-extern measured_string_ref measured_string_create_bulk(const char * string, size_t length);
-
-/** Copies the given measured string with separated header and data parts.
- *  @param[in] source The source measured string to be copied.
- *  @returns The copy of the given measured string.
- *  @returns NULL if the source parameter is NULL.
- *  @returns NULL if there is not enough memory left.
- */
-extern measured_string_ref measured_string_copy(measured_string_ref source);
-
-/** Receives a&nbsp;measured strings array from a&nbsp;calling module.
- *  Creates the array and the data memory blocks.
- *  This method should be used only while processing IPC messages as the array size has to be negotiated in advance.
- *  @param[out] strings The received measured strings array.
- *  @param[out] data The measured strings data. This memory block stores the actual character strings.
- *  @param[in] count The size of the measured strings array.
- *  @returns EOK on success.
- *  @returns EINVAL if the strings or data parameter is NULL.
- *  @returns EINVAL if the count parameter is zero (0).
- *  @returns EINVAL if the sent array differs in size.
- *  @returns EINVAL if there is inconsistency in sent measured strings' lengths (should not occur).
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the async_data_write_finalize() function.
- */
-extern int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count);
-
-/** Replies the given measured strings array to a&nbsp;calling module.
- *  This method should be used only while processing IPC messages as the array size has to be negotiated in advance.
- *  @param[in] strings The measured strings array to be transferred.
- *  @param[in] count The measured strings array size.
- *  @returns EOK on success.
- *  @returns EINVAL if the strings parameter is NULL.
- *  @returns EINVAL if the count parameter is zero (0).
- *  @returns EINVAL if the calling module does not accept the given array size.
- *  @returns EINVAL if there is inconsistency in sent measured strings' lengths (should not occur).
- *  @returns Other error codes as defined for the async_data_read_finalize() function.
- */
-extern int measured_strings_reply(const measured_string_ref strings, size_t count);
-
-/** Receives a&nbsp;measured strings array from another module.
- *  Creates the array and the data memory blocks.
- *  This method should be used only following other IPC messages as the array size has to be negotiated in advance.
- *  @param[in] phone The other module phone.
- *  @param[out] strings The returned measured strings array.
- *  @param[out] data The measured strings data. This memory block stores the actual character strings.
- *  @param[in] count The size of the measured strings array.
- *  @returns EOK on success.
- *  @returns EINVAL if the strings or data parameter is NULL.
- *  @returns EINVAL if the phone or count parameter is not positive (<=0).
- *  @returns EINVAL if the sent array differs in size.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the async_data_read_start() function.
- */
-extern int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count);
-
-/** Sends the given measured strings array to another module.
- *  This method should be used only following other IPC messages as the array size has to be negotiated in advance.
- *  @param[in] phone The other module phone.
- *  @param[in] strings The measured strings array to be transferred.
- *  @param[in] count The measured strings array size.
- *  @returns EOK on success.
- *  @returns EINVAL if the strings parameter is NULL.
- *  @returns EINVAL if the phone or count parameter is not positive (<=0).
- *  @returns Other error codes as defined for the async_data_write_start() function.
- */
-extern int measured_strings_send(int phone, const measured_string_ref strings, size_t count);
-
-#endif
-
-/** @}
- */
-
Index: pace/lib/socket/include/net_err.h
===================================================================
--- uspace/lib/socket/include/net_err.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,99 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- * @{
- */
-
-/** @file
- * Common error processing codes and routines.
- */
-
-#ifndef __NET_ERR_H__
-#define __NET_ERR_H__
-
-#include <errno.h>
-
-#ifdef CONFIG_DEBUG
-	#include <stdio.h>
-	#include <str_error.h>
-#endif
-
-/** An actual stored error code.
- *
- */
-#define ERROR_CODE  error_check_return_value
-
-/** An error processing routines declaration.
- *
- * This has to be declared in the block where the error processing
- * is desired.
- *
- */
-#define ERROR_DECLARE  int ERROR_CODE
-
-/** Store the value as an error code and checks if an error occurred.
- *
- * @param[in] value The value to be checked. May be a function call.
- * @return False if the value indicates success (EOK).
- * @return True otherwise.
- *
- */
-#ifdef CONFIG_DEBUG
-
-#define ERROR_OCCURRED(value) \
-	(((ERROR_CODE = (value)) != EOK) \
-	&& ({ \
-		fprintf(stderr, "libsocket error at %s:%d (%s)\n", \
-		__FILE__, __LINE__, str_error(ERROR_CODE)); \
-		1; \
-	}))
-
-#else
-
-#define ERROR_OCCURRED(value)  ((ERROR_CODE = (value)) != EOK)
-
-#endif
-
-/** Error propagation
- *
- * Check if an error occurred and immediately exit the actual
- * function returning the error code.
- *
- * @param[in] value The value to be checked. May be a function call.
- *
- */
-
-#define ERROR_PROPAGATE(value) \
-	if (ERROR_OCCURRED(value)) \
-		return ERROR_CODE
-
-#endif
-
-/** @}
- */
Index: uspace/lib/socket/include/socket.h
===================================================================
--- uspace/lib/socket/include/socket.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/include/socket.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,5 +45,5 @@
 #include <inet.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
+#include <errno.h>
 
 /** @name Socket application programming interface
Index: pace/lib/socket/include/socket_errno.h
===================================================================
--- uspace/lib/socket/include/socket_errno.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,136 +1,0 @@
-/*
- * Copyright (c) 2009 Lukas Mejdrech
- * 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 net
- *  @{
- */
-
-/** @file
- *  Socket error codes.
- *  Based on BSD.
- */
-
-#ifndef __NET_SOCKET_ERR_H__
-#define __NET_SOCKET_ERR_H__
-
-#include <errno.h>
-
-/** @name Socket error codes definitions
- */
-/*@{*/
-
-////#define EINTR			(-10004)
-////#define EBADF			(-10009)
-//#define EACCES			(-10013)
-//#define EFAULT			(-10014)
-////#define EINVAL			(-10022)
-////#define EMFILE			(-10024)
-//#define EWOULDBLOCK		(-10035)
-
-/** An API function is called while another blocking function is in progress.
- */
-#define EINPROGRESS		(-10036)
-
-//#define EALREADY		(-10037)
-
-/** The socket identifier is not valid.
- */
-#define ENOTSOCK		(-10038)
-
-/** The destination address required.
- */
-#define EDESTADDRREQ	(-10039)
-
-//#define EMSGSIZE		(-10040)
-//#define EPROTOTYPE		(-10041)
-//#define ENOPROTOOPT		(-10042)
-
-/** Protocol is not supported.
- */
-#define EPROTONOSUPPORT	(-10043)
-
-/** Socket type is not supported.
- */
-#define ESOCKTNOSUPPORT	(-10044)
-
-//#define EOPNOTSUPP		(-10045)
-
-/** Protocol family is not supported.
- */
-#define EPFNOSUPPORT	(-10046)
-
-/** Address family is not supported.
- */
-#define EAFNOSUPPORT	(-10047)
-
-/** Address is already in use.
- */
-#define EADDRINUSE		(-10048)
-
-//#define EADDRNOTAVAIL	(-10049)
-/* May be reported at any time if the implementation detects an underlying failure.
- */
-//#define ENETDOWN		(-10050)
-//#define ENETUNREACH		(-10051)
-//#define ENETRESET		(-10052)
-//#define ECONNABORTED	(-10053)
-//#define ECONNRESET		(-10054)
-//#define ENOBUFS			(-10055)
-//#define EISCONN			(-10056)
-
-/** The socket is not connected or bound.
- */
-#define ENOTCONN		(-10057)
-
-//#define ESHUTDOWN		(-10058)
-//#define ETOOMANYREFS	(-10059)
-//#define ETIMEDOUT		(-10060)
-//#define ECONNREFUSED	(-10061)
-//#define ELOOP			(-10062)
-////#define ENAMETOOLONG	(-10063)
-//#define EHOSTDOWN		(-10064)
-//#define EHOSTUNREACH	(-10065)
-//#define HOST_NOT_FOUND	(-11001)
-
-/** The requested operation was not performed.
- *  Try again later.
- */
-#define TRY_AGAIN		(-11002)
-
-//#define NO_RECOVERY		(-11003)
-
-/** No data.
- */
-#define NO_DATA			(-11004)
-
-/*@}*/
-
-#endif
-
-/** @}
- */
Index: uspace/lib/socket/packet/packet.c
===================================================================
--- uspace/lib/socket/packet/packet.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/packet/packet.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -36,13 +36,13 @@
  */
 
-#include <errno.h>
 #include <malloc.h>
 #include <mem.h>
 #include <fibril_synch.h>
 #include <unistd.h>
+#include <errno.h>
+#include <err.h>
 
 #include <sys/mman.h>
 
-#include <net_err.h>
 #include <adt/generic_field.h>
 #include <packet/packet.h>
Index: uspace/lib/socket/packet/packet_client.c
===================================================================
--- uspace/lib/socket/packet/packet_client.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/packet/packet_client.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -46,41 +46,50 @@
 #include <packet/packet_client.h>
 
-int packet_copy_data(packet_t packet, const void * data, size_t length){
-	if(! packet_is_valid(packet)){
-		return EINVAL;
-	}
-	if(packet->data_start + length >= packet->length){
+int packet_copy_data(packet_t packet, const void * data, size_t length)
+{
+	if (!packet_is_valid(packet))
+		return EINVAL;
+
+	if (packet->data_start + length >= packet->length)
 		return ENOMEM;
-	}
+
 	memcpy((void *) packet + packet->data_start, data, length);
-	if(packet->data_start + length > packet->data_end){
+	if (packet->data_start + length > packet->data_end) 
 		packet->data_end = packet->data_start + length;
-	}
+
 	return EOK;
 }
 
-void * packet_prefix(packet_t packet, size_t length){
-	if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){
-		return NULL;
-	}
+void *packet_prefix(packet_t packet, size_t length)
+{
+	if ((!packet_is_valid(packet)) ||
+	    (packet->data_start - sizeof(struct packet) -
+	    2 * (packet->dest_addr - packet->src_addr) < length)) {
+		return NULL;
+	}
+
 	packet->data_start -= length;
 	return (void *) packet + packet->data_start;
 }
 
-void * packet_suffix(packet_t packet, size_t length){
-	if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){
-		return NULL;
-	}
+void *packet_suffix(packet_t packet, size_t length)
+{
+	if ((!packet_is_valid(packet)) ||
+	    (packet->data_end + length >= packet->length)) {
+		return NULL;
+	}
+
 	packet->data_end += length;
 	return (void *) packet + packet->data_end - length;
 }
 
-int packet_trim(packet_t packet, size_t prefix, size_t suffix){
-	if(! packet_is_valid(packet)){
-		return EINVAL;
-	}
-	if(prefix + suffix > PACKET_DATA_LENGTH(packet)){
+int packet_trim(packet_t packet, size_t prefix, size_t suffix)
+{
+	if (!packet_is_valid(packet))
+		return EINVAL;
+
+	if (prefix + suffix > PACKET_DATA_LENGTH(packet))
 		return ENOMEM;
-	}
+
 	packet->data_start += prefix;
 	packet->data_end -= suffix;
@@ -88,71 +97,79 @@
 }
 
-packet_id_t packet_get_id(const packet_t packet){
+packet_id_t packet_get_id(const packet_t packet)
+{
 	return packet_is_valid(packet) ? packet->packet_id : 0;
 }
 
-int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){
-	if(! packet_is_valid(packet)){
-		return EINVAL;
-	}
-	if(! packet->addr_len){
+int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest)
+{
+	if (!packet_is_valid(packet))
+		return EINVAL;
+	if (!packet->addr_len)
 		return 0;
-	}
-	if(src){
+	if (src)
 		*src = (void *) packet + packet->src_addr;
-	}
-	if(dest){
+	if (dest)
 		*dest = (void *) packet + packet->dest_addr;
-	}
+
 	return packet->addr_len;
 }
 
-size_t packet_get_data_length(const packet_t packet){
-	if(! packet_is_valid(packet)){
+size_t packet_get_data_length(const packet_t packet)
+{
+	if (!packet_is_valid(packet))
 		return 0;
-	}
+
 	return PACKET_DATA_LENGTH(packet);
 }
 
-void * packet_get_data(const packet_t packet){
-	if(! packet_is_valid(packet)){
-		return NULL;
-	}
+void *packet_get_data(const packet_t packet)
+{
+	if (!packet_is_valid(packet))
+		return NULL;
+
 	return (void *) packet + packet->data_start;
 }
 
-int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){
+int
+packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest,
+    size_t addr_len)
+{
 	size_t padding;
 	size_t allocated;
 
-	if(! packet_is_valid(packet)){
-		return EINVAL;
-	}
+	if (!packet_is_valid(packet))
+		return EINVAL;
+
 	allocated = PACKET_MAX_ADDRESS_LENGTH(packet);
-	if(allocated < addr_len){
+	if (allocated < addr_len)
 		return ENOMEM;
-	}
+
 	padding = allocated - addr_len;
 	packet->addr_len = addr_len;
-	if(src){
+
+	if (src) {
 		memcpy((void *) packet + packet->src_addr, src, addr_len);
-		if(padding){
-			bzero((void *) packet + packet->src_addr + addr_len, padding);
-		}
-	}else{
+		if (padding)
+			bzero((void *) packet + packet->src_addr + addr_len,
+			    padding);
+	} else {
 		bzero((void *) packet + packet->src_addr, allocated);
 	}
-	if(dest){
+
+	if (dest) {
 		memcpy((void *) packet + packet->dest_addr, dest, addr_len);
-		if(padding){
-			bzero((void *) packet + packet->dest_addr + addr_len, padding);
-		}
-	}else{
+		if (padding)
+			bzero((void *) packet + packet->dest_addr + addr_len,
+			    padding);
+	} else {
 		bzero((void *) packet + packet->dest_addr, allocated);
 	}
+
 	return EOK;
 }
 
-packet_t packet_get_copy(int phone, packet_t packet){
+packet_t packet_get_copy(int phone, packet_t packet)
+{
 	packet_t copy;
 	uint8_t * src = NULL;
@@ -160,22 +177,26 @@
 	size_t addrlen;
 
-	if(! packet_is_valid(packet)){
-		return NULL;
-	}
+	if (!packet_is_valid(packet))
+		return NULL;
+
 	// get a new packet
-	copy = packet_get_4_local(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet));
-	if(! copy){
-		return NULL;
-	}
+	copy = packet_get_4_local(phone, PACKET_DATA_LENGTH(packet),
+	    PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix,
+	    PACKET_MIN_SUFFIX(packet));
+	if (!copy)
+		return NULL;
+
 	// get addresses
 	addrlen = packet_get_addr(packet, &src, &dest);
 	// copy data
-	if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK)
-	// copy addresses if present
-		&& ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){
+	if ((packet_copy_data(copy, packet_get_data(packet),
+	    PACKET_DATA_LENGTH(packet)) == EOK) &&
+	    // copy addresses if present
+	    ((addrlen <= 0) ||
+	    (packet_set_addr(copy, src, dest, addrlen) == EOK))) {
 		copy->order = packet->order;
 		copy->metric = packet->metric;
 		return copy;
-	}else{
+	} else {
 		pq_release_local(phone, copy->packet_id);
 		return NULL;
Index: uspace/lib/socket/packet/packet_server.c
===================================================================
--- uspace/lib/socket/packet/packet_server.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/lib/socket/packet/packet_server.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -39,4 +39,5 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <fibril_synch.h>
 #include <unistd.h>
@@ -45,5 +46,4 @@
 #include <sys/mman.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <packet/packet.h>
Index: uspace/srv/fs/fat/fat_ops.c
===================================================================
--- uspace/srv/fs/fat/fat_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/fs/fat/fat_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -1527,6 +1527,25 @@
 void fat_sync(ipc_callid_t rid, ipc_call_t *request)
 {
-	/* Dummy implementation */
-	ipc_answer_0(rid, EOK);
+	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
+	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
+	
+	fs_node_t *fn;
+	int rc = fat_node_get(&fn, dev_handle, index);
+	if (rc != EOK) {
+		ipc_answer_0(rid, rc);
+		return;
+	}
+	if (!fn) {
+		ipc_answer_0(rid, ENOENT);
+		return;
+	}
+	
+	fat_node_t *nodep = FAT_NODE(fn);
+	
+	nodep->dirty = true;
+	rc = fat_node_sync(nodep);
+	
+	fat_node_put(fn);
+	ipc_answer_0(rid, rc);
 }
 
Index: uspace/srv/fs/tmpfs/tmpfs_ops.c
===================================================================
--- uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -736,5 +736,8 @@
 void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
 {
-	/* Dummy implementation */
+	/*
+	 * TMPFS keeps its data structures always consistent,
+	 * thus the sync operation is a no-op.
+	 */
 	ipc_answer_0(rid, EOK);
 }
Index: uspace/srv/hid/console/gcons.c
===================================================================
--- uspace/srv/hid/console/gcons.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/console/gcons.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -286,4 +286,7 @@
 	ssize_t nx = (ssize_t) mouse_x + dx;
 	ssize_t ny = (ssize_t) mouse_y + dy;
+
+	if (!use_gcons)
+		return;
 	
 	mouse_x = (size_t) limit(nx, 0, xres);
Index: uspace/srv/hid/fb/fb.c
===================================================================
--- uspace/srv/hid/fb/fb.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/fb/fb.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -1347,6 +1347,6 @@
 {
 	mouse_hide();
-	pointer_x = x;
-	pointer_y = y;
+	pointer_x = x % screen.xres;
+	pointer_y = y % screen.yres;
 	mouse_show();
 }
Index: uspace/srv/hid/kbd/Makefile
===================================================================
--- uspace/srv/hid/kbd/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/kbd/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -60,6 +60,6 @@
 	ifeq ($(MACHINE),gta02)
 		SOURCES += \
-			port/dummy.c \
-			ctl/pc.c
+			port/chardev.c \
+			ctl/stty.c
 	endif
 	ifeq ($(MACHINE),testarm)
Index: uspace/srv/hid/kbd/port/chardev.c
===================================================================
--- uspace/srv/hid/kbd/port/chardev.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/kbd/port/chardev.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,4 +41,5 @@
 #include <kbd.h>
 #include <vfs/vfs.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -50,21 +51,41 @@
 #define NAME "kbd"
 
+/** List of devices to try connecting to. */
+static const char *in_devs[] = {
+	"/dev/char/ps2a",
+	"/dev/char/s3c24ser"
+};
+
+static const int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
+
 int kbd_port_init(void)
 {
-	const char *input = "/dev/char/ps2a";
 	int input_fd;
+	int i;
 
-	printf(NAME ": open %s\n", input);
+	input_fd = -1;
+	for (i = 0; i < num_devs; i++) {
+		struct stat s;
 
-	input_fd = open(input, O_RDONLY);
+		if (stat(in_devs[i], &s) == EOK)
+			break;
+	}
+
+	if (i >= num_devs) {
+		printf(NAME ": Could not find any suitable input device.\n");
+		return -1;
+	}
+
+	input_fd = open(in_devs[i], O_RDONLY);
 	if (input_fd < 0) {
-		printf(NAME ": Failed opening %s (%d)\n", input, input_fd);
-		return false;
+		printf(NAME ": failed opening device %s (%d).\n", in_devs[i],
+		    input_fd);
+		return -1;
 	}
 
 	dev_phone = fd_phone(input_fd);
 	if (dev_phone < 0) {
-		printf(NAME ": Failed to connect to device\n");
-		return false;
+		printf(NAME ": Failed connecting to device\n");
+		return -1;
 	}
 
@@ -73,5 +94,5 @@
 	if (ipc_connect_to_me(dev_phone, 0, 0, 0, &phonehash) != 0) {
 		printf(NAME ": Failed to create callback from device\n");
-		return false;
+		return -1;
 	}
 
Index: pace/srv/hid/kbd/port/i8042.h
===================================================================
--- uspace/srv/hid/kbd/port/i8042.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(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/hid/s3c24xx_ts/Makefile
===================================================================
--- uspace/srv/hid/s3c24xx_ts/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2010 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.
+#
+
+USPACE_PREFIX = ../../..
+
+# Need to use short name because of FAT 8+3 limit
+BINARY = s3c24ts
+
+SOURCES = \
+	s3c24xx_ts.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c
===================================================================
--- uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,408 @@
+/*
+ * Copyright (c) 2010 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 Samsung Samsung S3C24xx on-chip ADC and touch-screen interface driver.
+ *
+ * This interface is present on the Samsung S3C24xx CPU (on the gta02 platform).
+ */
+
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <devmap.h>
+#include <io/console.h>
+#include <vfs/vfs.h>
+#include <ipc/ipc.h>
+#include <ipc/mouse.h>
+#include <async.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysinfo.h>
+#include <errno.h>
+
+#include "s3c24xx_ts.h"
+
+#define NAME "s3c24ser"
+#define NAMESPACE "hid_in"
+
+static irq_cmd_t ts_irq_cmds[] = {
+	{
+		.cmd = CMD_ACCEPT
+	}
+};
+
+static irq_code_t ts_irq_code = {
+	sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
+	ts_irq_cmds
+};
+
+/** S3C24xx touchscreen instance structure */
+static s3c24xx_ts_t *ts;
+
+static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall);
+static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call);
+static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
+static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
+static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
+static int lin_map_range(int v, int i0, int i1, int o0, int o1);
+
+int main(int argc, char *argv[])
+{
+	int rc;
+
+	printf(NAME ": S3C24xx touchscreen driver\n");
+
+	rc = devmap_driver_register(NAME, s3c24xx_ts_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver.\n");
+		return -1;
+	}
+
+	ts = malloc(sizeof(s3c24xx_ts_t));
+	if (ts == NULL)
+		return -1;
+
+	if (s3c24xx_ts_init(ts) != EOK)
+		return -1;
+
+	rc = devmap_device_register(NAMESPACE "/mouse", &ts->dev_handle);
+	if (rc != EOK) {
+		devmap_hangup_phone(DEVMAP_DRIVER);
+		printf(NAME ": Unable to register device %s.\n",
+		    NAMESPACE "/mouse");
+		return -1;
+	}
+
+	printf(NAME ": Registered device %s.\n", NAMESPACE "/mouse");
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached */
+	return 0;
+}
+
+/** Initialize S3C24xx touchscreen interface. */
+static int s3c24xx_ts_init(s3c24xx_ts_t *ts)
+{
+	void *vaddr;
+	sysarg_t inr;
+
+	inr = S3C24XX_TS_INR;
+	ts->paddr = S3C24XX_TS_ADDR;
+
+	if (pio_enable((void *) ts->paddr, sizeof(s3c24xx_adc_io_t),
+	    &vaddr) != 0)
+		return -1;
+
+	ts->io = vaddr;
+	ts->client_phone = -1;
+	ts->state = ts_wait_pendown;
+	ts->last_x = 0;
+	ts->last_y = 0;
+
+	printf(NAME ": device at physical address 0x%x, inr %d.\n",
+	    ts->paddr, inr);
+
+	async_set_interrupt_received(s3c24xx_ts_irq_handler);
+	ipc_register_irq(inr, device_assign_devno(), 0, &ts_irq_code);
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_down);
+
+	return EOK;
+}
+
+/** Switch interface to wait for interrupt mode.
+ *
+ * In this mode we receive an interrupt when pen goes up/down, depending
+ * on @a updn.
+ *
+ * @param ts	Touchscreen instance
+ * @param updn	@c updn_up to wait for pen up, @c updn_down to wait for pen
+ *		down.
+ */
+static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn)
+{
+	uint32_t con, tsc;
+
+	/*
+	 * Configure ADCCON register
+	 */
+
+	con = pio_read_32(&ts->io->con);
+
+	/* Disable standby, disable start-by-read, clear manual start bit */
+	con = con & ~(ADCCON_STDBM | ADCCON_READ_START | ADCCON_ENABLE_START);
+
+	/* Set prescaler value 0xff, XP for input. */
+	con = con | (ADCCON_PRSCVL(0xff) << 6) | ADCCON_SEL_MUX(SMUX_XP);
+
+	/* Enable prescaler. */
+	con = con | ADCCON_PRSCEN;
+
+ 	pio_write_32(&ts->io->con, con);
+
+	/*
+	 * Configure ADCTSC register
+	 */
+
+	tsc = pio_read_32(&ts->io->tsc);
+
+	/* Select whether waiting for pen up or pen down. */
+	if (updn == updn_up)
+		tsc |= ADCTSC_DSUD_UP;
+	else
+		tsc &= ~ADCTSC_DSUD_UP;
+
+	/*
+	 * Enable XP pull-up and disable all drivers except YM. This is
+	 * according to the manual. This gives us L on XP input when touching
+	 * and (pulled up to) H when not touching.
+	 */
+	tsc = tsc & ~(ADCTSC_XM_ENABLE | ADCTSC_AUTO_PST |
+	    ADCTSC_PULLUP_DISABLE);
+	tsc = tsc | ADCTSC_YP_DISABLE | ADCTSC_XP_DISABLE | ADCTSC_YM_ENABLE;
+
+	/* Select wait-for-interrupt mode. */
+	tsc = (tsc & ~ADCTSC_XY_PST_MASK) | ADCTSC_XY_PST_WAITINT;
+
+	pio_write_32(&ts->io->tsc, tsc);
+}
+
+/** Handle touchscreen interrupt */
+static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call)
+{
+	ts_updn_t updn;
+
+	(void) iid; (void) call;
+
+	/* Read up/down interrupt flags. */
+	updn = pio_read_32(&ts->io->updn);
+
+	if (updn & (ADCUPDN_TSC_DN | ADCUPDN_TSC_UP)) {
+		/* Clear up/down interrupt flags. */
+		pio_write_32(&ts->io->updn, updn &
+		    ~(ADCUPDN_TSC_DN | ADCUPDN_TSC_UP));
+	}
+
+	if (updn & ADCUPDN_TSC_DN) {
+		/* Pen-down interrupt */
+		s3c24xx_ts_pen_down(ts);
+	} else if (updn & ADCUPDN_TSC_UP) {
+		/* Pen-up interrupt */
+		s3c24xx_ts_pen_up(ts);
+	} else {
+		/* Presumably end-of-conversion interrupt */
+
+		/* Check end-of-conversion flag. */
+		if ((pio_read_32(&ts->io->con) & ADCCON_ECFLG) == 0) {
+			printf(NAME ": Unrecognized ts int.\n");
+			return;
+		}
+
+		if (ts->state != ts_sample_pos) {
+			/*
+			 * We got an extra interrupt ater switching to
+			 * wait for interrupt mode.
+			 */
+			return;
+		}
+
+		/* End-of-conversion interrupt */
+		s3c24xx_ts_eoc(ts);
+	}
+}
+
+/** Handle pen-down interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts)
+{
+	/* Pen-down interrupt */
+
+	ts->state = ts_sample_pos;
+
+	/* Enable auto xy-conversion mode */
+	pio_write_32(&ts->io->tsc, (pio_read_32(&ts->io->tsc)
+	    & ~3) | 4);
+
+	/* Start the conversion. */
+	pio_write_32(&ts->io->con, pio_read_32(&ts->io->con)
+	    | ADCCON_ENABLE_START);
+}
+
+/** Handle pen-up interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts)
+{
+	int button, press;
+
+	/* Pen-up interrupt */
+
+	ts->state = ts_wait_pendown;
+
+	button = 1;
+	press = 0;
+	async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_down);
+}
+
+/** Handle end-of-conversion interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
+{
+	uint32_t data;
+	int button, press;
+	int smp0, smp1;
+	int x_pos, y_pos;
+	int dx, dy;
+
+	ts->state = ts_wait_penup;
+
+	/* Read in sampled data. */
+
+	data = pio_read_32(&ts->io->dat0);
+	smp0 = data & 0x3ff;
+
+	data = pio_read_32(&ts->io->dat1);
+	smp1 = data & 0x3ff;
+
+	/* Convert to screen coordinates. */
+	s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
+
+	printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
+	    x_pos, y_pos);
+
+	/* Get differences. */
+	dx = x_pos - ts->last_x;
+	dy = y_pos - ts->last_y;
+
+	button = 1;
+	press = 1;
+
+	/* Send notifications to client. */
+	async_msg_2(ts->client_phone, MEVENT_MOVE, dx, dy);
+	async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
+
+	ts->last_x = x_pos;
+	ts->last_y = y_pos;
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_up);
+}
+
+/** Convert sampled data to screen coordinates. */
+static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
+{
+	/*
+	 * The orientation and display dimensions are GTA02-specific and the
+	 * calibration values might even specific to the individual piece
+	 * of hardware.
+	 *
+	 * The calibration values can be obtained by touching corners
+	 * of the screen with the stylus and noting the sampled values.
+	 */
+	*x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
+	*y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
+}
+
+/** Map integer from one range to another range in a linear fashion.
+ *
+ * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
+ * mapping will be descending. If v is outside of [i0, i1], it is clamped.
+ *
+ * @param v	Value to map.
+ * @param i0	Lower bound of input range.
+ * @param i1	Upper bound of input range.
+ * @param o0	First bound of output range.
+ * @param o1	Second bound of output range.
+ *
+ * @return	Mapped value ov, o0 <= ov <= o1.
+ */
+static int lin_map_range(int v, int i0, int i1, int o0, int o1)
+{
+	if (v < i0)
+		v = i0;
+
+	if (v > i1)
+		v = i1;
+
+	return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
+}
+
+/** Handle mouse client connection. */
+static void s3c24xx_ts_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 (ts->client_phone != -1) {
+				ipc_hangup(ts->client_phone);
+				ts->client_phone = -1;
+			}
+
+			ipc_answer_0(callid, EOK);
+			return;
+		case IPC_M_CONNECT_TO_ME:
+			if (ts->client_phone != -1) {
+				retval = ELIMIT;
+				break;
+			}
+			ts->client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+/** @}
+ */
Index: uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h
===================================================================
--- uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2010 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 genarch
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip ADC and touch-screen interface driver.
+ */
+
+#ifndef S3C24XX_TS_H_
+#define S3C24XX_TS_H_
+
+#include <sys/types.h>
+
+/** S3C24xx ADC and touch-screen I/O */
+typedef struct {
+	uint32_t con;
+	uint32_t tsc;
+	uint32_t dly;
+	uint32_t dat0;
+	uint32_t dat1;
+	uint32_t updn;
+} s3c24xx_adc_io_t;
+
+/* Fields in ADCCON register */
+#define ADCCON_ECFLG		0x8000
+#define ADCCON_PRSCEN		0x4000
+
+#define ADCCON_PRSCVL(val)	(((val) & 0xff) << 6)
+
+#define ADCCON_SEL_MUX(smux)	(((smux) & 7) << 3)
+
+#define ADCCON_STDBM		0x0004
+#define ADCCON_READ_START	0x0002
+#define ADCCON_ENABLE_START	0x0001
+
+/* Values for ADCCON_SEL_MUX */
+#define SMUX_AIN0		0
+#define SMUX_AIN1		1
+#define SMUX_AIN2		2
+#define SMUX_AIN3		3
+#define SMUX_YM			4
+#define SMUX_YP			5
+#define SMUX_XM			6
+#define SMUX_XP			7
+
+
+/* Fields in ADCTSC register */
+#define ADCTSC_DSUD_UP		0x0100
+#define ADCTSC_YM_ENABLE	0x0080
+#define ADCTSC_YP_DISABLE	0x0040
+#define ADCTSC_XM_ENABLE	0x0020
+#define ADCTSC_XP_DISABLE	0x0010
+#define ADCTSC_PULLUP_DISABLE	0x0008
+#define ADCTSC_AUTO_PST		0x0004
+
+#define ADCTSC_XY_PST_NOOP	0x0000
+#define ADCTSC_XY_PST_X		0x0001
+#define ADCTSC_XY_PST_Y		0x0002
+#define ADCTSC_XY_PST_WAITINT	0x0003
+#define ADCTSC_XY_PST_MASK	0x0003
+
+/* Fields in ADCDAT0, ADCDAT1 registers */
+#define ADCDAT_UPDOWN		0x8000
+#define ADCDAT_AUTO_PST		0x4000
+
+/* Fields in ADCUPDN register */
+#define ADCUPDN_TSC_UP		0x0002
+#define ADCUPDN_TSC_DN		0x0001
+
+/** Touchscreen interrupt number */
+#define S3C24XX_TS_INR		31
+
+/** Touchscreen I/O address */
+#define S3C24XX_TS_ADDR		0x58000000
+
+typedef enum {
+	ts_wait_pendown,
+	ts_sample_pos,
+	ts_wait_penup
+} ts_state_t;
+
+typedef enum {
+	updn_up,
+	updn_down
+} ts_updn_t;
+
+/** S3C24xx touchscreen driver instance */
+typedef struct {
+	/** Physical device address */
+	uintptr_t paddr;
+
+	/** Device I/O structure */
+	s3c24xx_adc_io_t *io;
+
+	/** Callback phone to the client */
+	int client_phone;
+
+	/** Device handle */
+	dev_handle_t dev_handle;
+
+	/** Device/driver state */
+	ts_state_t state;
+
+	/** Previous position reported to client. */
+	int last_x;
+	int last_y;
+} s3c24xx_ts_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/srv/hw/char/s3c24xx_uart/Makefile
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,38 @@
+#
+# 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.
+#
+
+USPACE_PREFIX = ../../../..
+
+# Need to use short name because of FAT 8+3 limit
+BINARY = s3c24ser
+
+SOURCES = \
+	s3c24xx_uart.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2010 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 driver_serial
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip UART driver.
+ *
+ * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
+ */
+
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <devmap.h>
+#include <ipc/ipc.h>
+#include <ipc/char.h>
+#include <async.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysinfo.h>
+#include <errno.h>
+
+#include "s3c24xx_uart.h"
+
+#define NAME "s3c24ser"
+#define NAMESPACE "char"
+
+static irq_cmd_t uart_irq_cmds[] = {
+	{
+		.cmd = CMD_ACCEPT
+	}
+};
+
+static irq_code_t uart_irq_code = {
+	sizeof(uart_irq_cmds) / sizeof(irq_cmd_t),
+	uart_irq_cmds
+};
+
+/** S3C24xx UART instance structure */
+static s3c24xx_uart_t *uart;
+
+static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall);
+static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call);
+static int s3c24xx_uart_init(s3c24xx_uart_t *uart);
+static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte);
+
+int main(int argc, char *argv[])
+{
+	int rc;
+
+	printf(NAME ": S3C24xx on-chip UART driver\n");
+
+	rc = devmap_driver_register(NAME, s3c24xx_uart_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver.\n");
+		return -1;
+	}
+
+	uart = malloc(sizeof(s3c24xx_uart_t));
+	if (uart == NULL)
+		return -1;
+
+	if (s3c24xx_uart_init(uart) != EOK)
+		return -1;
+
+	rc = devmap_device_register(NAMESPACE "/" NAME, &uart->dev_handle);
+	if (rc != EOK) {
+		devmap_hangup_phone(DEVMAP_DRIVER);
+		printf(NAME ": Unable to register device %s.\n");
+		return -1;
+	}
+
+	printf(NAME ": Registered device %s.\n", NAMESPACE "/" NAME);
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached */
+	return 0;
+}
+
+/** Character device connection handler. */
+static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	ipc_callid_t callid;
+	ipc_call_t call;
+	ipcarg_t method;
+	int retval;
+
+	/* Answer the IPC_M_CONNECT_ME_TO call. */
+	ipc_answer_0(iid, EOK);
+
+	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");
+			uart->client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		case CHAR_WRITE_BYTE:
+			printf(NAME ": write %d to device\n",
+			    IPC_GET_ARG1(call));
+			s3c24xx_uart_sendb(uart, (uint8_t) IPC_GET_ARG1(call));
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+			break;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call)
+{
+	(void) iid; (void) call;
+
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
+		uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
+		uint32_t status = pio_read_32(&uart->io->uerstat);
+
+		if (uart->client_phone != -1) {
+			async_msg_1(uart->client_phone, CHAR_NOTIF_BYTE,
+			    data);
+		}
+
+		if (status != 0)
+			printf(NAME ": Error status 0x%x\n", status);
+	}
+}
+
+/** Initialize S3C24xx on-chip UART. */
+static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
+{
+	void *vaddr;
+	sysarg_t inr;
+
+	if (sysinfo_get_value("s3c24xx_uart.address.physical",
+	    &uart->paddr) != EOK)
+		return -1;
+
+	if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
+	    &vaddr) != 0)
+		return -1;
+
+	if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
+		return -1;
+
+	uart->io = vaddr;
+	uart->client_phone = -1;
+
+	printf(NAME ": device at physical address 0x%x, inr %d.\n",
+	    uart->paddr, inr);
+
+	async_set_interrupt_received(s3c24xx_uart_irq_handler);
+
+	ipc_register_irq(inr, device_assign_devno(), 0, &uart_irq_code);
+
+	/* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
+	pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
+	    UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
+
+	/* Set RX interrupt to pulse mode */
+	pio_write_32(&uart->io->ucon,
+	    pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
+
+	return EOK;
+}
+
+/** Send a byte to the UART. */
+static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
+{
+	/* Wait for space becoming available in Tx FIFO. */
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
+		;
+
+	pio_write_32(&uart->io->utxh, byte);
+}
+
+/** @}
+ */
Index: uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2010 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 genarch
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip UART driver.
+ */
+
+#ifndef S3C24XX_UART_H_
+#define S3C24XX_UART_H_
+
+#include <sys/types.h>
+
+/** S3C24xx UART I/O */
+typedef struct {
+	uint32_t ulcon;
+	uint32_t ucon;
+	uint32_t ufcon;
+	uint32_t umcon;
+
+	uint32_t utrstat;
+	uint32_t uerstat;
+	uint32_t ufstat;
+	uint32_t umstat;
+
+	uint32_t utxh;
+	uint32_t urxh;
+
+	uint32_t ubrdiv;
+} s3c24xx_uart_io_t;
+
+/* Bits in UTRSTAT register */
+#define S3C24XX_UTRSTAT_TX_EMPTY	0x4
+#define S3C24XX_UTRSTAT_RDATA		0x1
+
+/* Bits in UFSTAT register */
+#define S3C24XX_UFSTAT_TX_FULL		0x4000
+#define S3C24XX_UFSTAT_RX_FULL		0x0040
+#define S3C24XX_UFSTAT_RX_COUNT		0x002f
+
+/* Bits in UCON register */
+#define UCON_RX_INT_LEVEL		0x100
+
+/* Bits in UFCON register */
+#define UFCON_TX_FIFO_TLEVEL_EMPTY	0x00
+#define UFCON_RX_FIFO_TLEVEL_1B		0x00
+#define UFCON_FIFO_ENABLE		0x01
+
+
+/** S3C24xx UART instance */
+typedef struct {
+	/** Physical device address */
+	uintptr_t paddr;
+
+	/** Device I/O structure */
+	s3c24xx_uart_io_t *io;
+
+	/** Callback phone to the client */
+	int client_phone;
+
+	/** Device handle */
+	dev_handle_t dev_handle;
+} s3c24xx_uart_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/srv/hw/netif/dp8390/dp8390_module.c
===================================================================
--- uspace/srv/hw/netif/dp8390/dp8390_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hw/netif/dp8390/dp8390_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -39,9 +39,9 @@
 #include <ddi.h>
 #include <errno.h>
+#include <err.h>
 #include <malloc.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/il/arp/arp.c
===================================================================
--- uspace/srv/net/il/arp/arp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/arp/arp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,6 +45,6 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/il/arp/arp_module.c
===================================================================
--- uspace/srv/net/il/arp/arp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/arp/arp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,9 +40,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/il/ip/ip.c
===================================================================
--- uspace/srv/net/il/ip/ip.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/ip/ip.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,4 +38,5 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <fibril_synch.h>
 #include <stdio.h>
@@ -45,5 +46,4 @@
 #include <sys/types.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
@@ -65,5 +65,4 @@
 #include <tl_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <adt/measured_strings.h>
 #include <adt/module_map.h>
Index: uspace/srv/net/il/ip/ip_module.c
===================================================================
--- uspace/srv/net/il/ip/ip_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/ip/ip_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,6 +42,6 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
+#include <err.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/net/net.c
===================================================================
--- uspace/srv/net/net/net.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/net/net.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,4 +40,5 @@
 #include <ddi.h>
 #include <errno.h>
+#include <err.h>
 #include <malloc.h>
 #include <stdio.h>
@@ -47,5 +48,4 @@
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/netif/lo/lo.c
===================================================================
--- uspace/srv/net/netif/lo/lo.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/netif/lo/lo.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -37,4 +37,5 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <stdio.h>
 #include <str.h>
@@ -43,5 +44,4 @@
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/netstart/netstart.c
===================================================================
--- uspace/srv/net/netstart/netstart.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/netstart/netstart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,8 +45,8 @@
 #include <task.h>
 #include <str_error.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_net_messages.h>
Index: uspace/srv/net/nil/eth/eth.c
===================================================================
--- uspace/srv/net/nil/eth/eth.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/eth/eth.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,9 +41,9 @@
 #include <stdio.h>
 #include <str.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/nil/eth/eth_module.c
===================================================================
--- uspace/srv/net/nil/eth/eth_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/eth/eth_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,9 +38,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/nil/nildummy/nildummy.c
===================================================================
--- uspace/srv/net/nil/nildummy/nildummy.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/nildummy/nildummy.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,8 +41,8 @@
 #include <stdio.h>
 #include <str.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/nil/nildummy/nildummy_module.c
===================================================================
--- uspace/srv/net/nil/nildummy/nildummy_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/nildummy/nildummy_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,9 +38,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/tl/icmp/icmp.c
===================================================================
--- uspace/srv/net/tl/icmp/icmp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/icmp/icmp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -46,6 +46,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
@@ -66,5 +67,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <tl_messages.h>
 #include <tl_interface.h>
Index: uspace/srv/net/tl/icmp/icmp_module.c
===================================================================
--- uspace/srv/net/tl/icmp/icmp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/icmp/icmp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,9 +42,10 @@
 //TODO remove stdio
 #include <stdio.h>
+#include <errno.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
@@ -63,5 +64,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <tcp_codes.h>
 #include <socket_core.h>
@@ -76,49 +76,40 @@
 #include "tcp_module.h"
 
-/** TCP module name.
- */
+/** TCP module name. */
 #define NAME	"TCP protocol"
 
-/** The TCP window default value.
- */
-#define NET_DEFAULT_TCP_WINDOW	10240
-
-/** Initial timeout for new connections.
- */
+/** The TCP window default value. */
+#define NET_DEFAULT_TCP_WINDOW		10240
+
+/** Initial timeout for new connections. */
 #define NET_DEFAULT_TCP_INITIAL_TIMEOUT	3000000L
 
-/** Default timeout for closing.
- */
-#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT	2000L
-
-/** The initial outgoing sequence number.
- */
-#define TCP_INITIAL_SEQUENCE_NUMBER		2999
-
-/** Maximum TCP fragment size.
- */
-#define MAX_TCP_FRAGMENT_SIZE	65535
-
-/** Free ports pool start.
- */
-#define TCP_FREE_PORTS_START	1025
-
-/** Free ports pool end.
- */
+/** Default timeout for closing. */
+#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
+
+/** The initial outgoing sequence number. */
+#define TCP_INITIAL_SEQUENCE_NUMBER	2999
+
+/** Maximum TCP fragment size. */
+#define MAX_TCP_FRAGMENT_SIZE		65535
+
+/** Free ports pool start. */
+#define TCP_FREE_PORTS_START		1025
+
+/** Free ports pool end. */
 #define TCP_FREE_PORTS_END		65535
 
-/** Timeout for connection initialization, SYN sent.
- */
-#define TCP_SYN_SENT_TIMEOUT	1000000L
-
-/** The maximum number of timeouts in a row before singaling connection lost.
- */
+/** Timeout for connection initialization, SYN sent. */
+#define TCP_SYN_SENT_TIMEOUT		1000000L
+
+/** The maximum number of timeouts in a row before singaling connection lost. */
 #define TCP_MAX_TIMEOUTS		8
 
-/** The number of acknowledgements before retransmit.
- */
+/** The number of acknowledgements before retransmit. */
 #define TCP_FAST_RETRANSMIT_COUNT	3
 
-/** Returns a value indicating whether the value is in the interval respecting the possible overflow.
+/** Returns a value indicating whether the value is in the interval respecting
+ *  the possible overflow.
+ *
  *  The high end and/or the value may overflow, be lower than the low value.
  *  @param[in] lower The last value before the interval.
@@ -126,15 +117,18 @@
  *  @param[in] higher_equal The last value in the interval.
  */
-#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal)	((((lower) < (value)) && (((value) <= (higher_equal)) || ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && ((higher_equal) < (lower))))
+#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
+	((((lower) < (value)) && (((value) <= (higher_equal)) || \
+	((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \
+	((higher_equal) < (lower))))
 
 /** Type definition of the TCP timeout.
  *  @see tcp_timeout
  */
-typedef struct tcp_timeout	tcp_timeout_t;
+typedef struct tcp_timeout tcp_timeout_t;
 
 /** Type definition of the TCP timeout pointer.
  *  @see tcp_timeout
  */
-typedef tcp_timeout_t *	tcp_timeout_ref;
+typedef tcp_timeout_t *tcp_timeout_ref;
 
 /** TCP reply timeout data.
@@ -142,31 +136,30 @@
  *  @see tcp_timeout()
  */
-struct tcp_timeout{
-	/** TCP global data are going to be read only.
-	 */
+struct tcp_timeout {
+	/** TCP global data are going to be read only. */
 	int globals_read_only;
-	/** Socket port.
-	 */
+
+	/** Socket port. */
 	int port;
-	/** Local sockets.
-	 */
+
+	/** Local sockets. */
 	socket_cores_ref local_sockets;
-	/** Socket identifier.
-	 */
+
+	/** Socket identifier. */
 	int socket_id;
-	/** Socket state.
-	 */
+
+	/** Socket state. */
 	tcp_socket_state_t state;
-	/** Sent packet sequence number.
-	 */
+
+	/** Sent packet sequence number. */
 	int sequence_number;
-	/** Timeout in microseconds.
-	 */
+
+	/** Timeout in microseconds. */
 	suseconds_t timeout;
-	/** Port map key.
-	 */
-	char * key;
-	/** Port map key length.
-	 */
+
+	/** Port map key. */
+	char *key;
+
+	/** Port map key length. */
 	size_t key_length;
 };
@@ -179,72 +172,121 @@
 int tcp_release_and_return(packet_t packet, int result);
 
-void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize);
-int tcp_prepare_timeout(int (*timeout_function)(void * tcp_timeout_t), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only);
+void tcp_prepare_operation_header(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
+    int finalize);
+int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
+    socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
+    int globals_read_only);
 void tcp_free_socket_data(socket_core_ref socket);
-int tcp_timeout(void * data);
-int tcp_release_after_timeout(void * data);
-int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error);
-int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen);
-int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
-int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
-packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data);
+
+int tcp_timeout(void *data);
+
+int tcp_release_after_timeout(void *data);
+
+int tcp_process_packet(device_id_t device_id, packet_t packet,
+    services_t error);
+int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
+    struct sockaddr *addr, socklen_t addrlen);
+int tcp_queue_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
+int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length);
+packet_t tcp_get_packets_to_send(socket_core_ref socket,
+    tcp_socket_data_ref socket_data);
 void tcp_send_packets(device_id_t device_id, packet_t packet);
-void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header);
-packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number);
-packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number);
-void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number);
-int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize);
+
+void tcp_process_acknowledgement(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header);
+packet_t tcp_send_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
+    size_t sequence_number);
+packet_t tcp_prepare_copy(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
+    size_t sequence_number);
+void tcp_retransmit_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, size_t sequence_number);
+int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
+    tcp_socket_data_ref socket_data, int synchronize, int finalize);
 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
+
 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
-int tcp_process_listen(socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen);
-int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length);
-int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length);
-
-int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
+
+int tcp_process_listen(socket_core_ref listening_socket,
+    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
+    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
+    size_t addrlen);
+int tcp_process_syn_sent(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
+int tcp_process_syn_received(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
+int tcp_process_established(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet,
+    int fragments, size_t total_length);
+int tcp_queue_received_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
+    size_t total_length);
+
+int tcp_received_msg(device_id_t device_id, packet_t packet,
+    services_t receiver, services_t error);
 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
-int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog);
-int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen);
-int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
-int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags);
-int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
+
+int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
+    int backlog);
+int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
+    struct sockaddr *addr, socklen_t addrlen);
+int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
+    int flags, size_t * addrlen);
+int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
+    int fragments, size_t * data_fragment_size, int flags);
+int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
+    int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
 int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
 
-/** TCP global data.
- */
-tcp_globals_t	tcp_globals;
-
-int tcp_initialize(async_client_conn_t client_connection){
+/** TCP global data. */
+tcp_globals_t tcp_globals;
+
+int tcp_initialize(async_client_conn_t client_connection)
+{
 	ERROR_DECLARE;
 
 	assert(client_connection);
+
 	fibril_rwlock_initialize(&tcp_globals.lock);
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
-	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg);
-	if(tcp_globals.ip_phone < 0){
+
+	tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
+	    ICMP_CONNECT_TIMEOUT);
+	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
+	    SERVICE_TCP, client_connection, tcp_received_msg);
+	if (tcp_globals.ip_phone < 0)
 		return tcp_globals.ip_phone;
-	}
+	
 	ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
-	if(ERROR_OCCURRED(packet_dimensions_initialize(&tcp_globals.dimensions))){
+	if (ERROR_OCCURRED(packet_dimensions_initialize(
+	    &tcp_globals.dimensions))) {
 		socket_ports_destroy(&tcp_globals.sockets);
 		return ERROR_CODE;
 	}
+
 	tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
+
 	return EOK;
 }
 
-int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
+int
+tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error)
+{
 	ERROR_DECLARE;
 
-	if(receiver != SERVICE_TCP){
+	if (receiver != SERVICE_TCP)
 		return EREFUSED;
-	}
+
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	if(ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))){
+	if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
-	}
+
 	printf("receive %d \n", ERROR_CODE);
 
@@ -252,5 +294,6 @@
 }
 
-int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error){
+int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
+{
 	ERROR_DECLARE;
 
@@ -259,5 +302,5 @@
 	int result;
 	tcp_header_ref header;
-	socket_core_ref  socket;
+	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_t next_packet;
@@ -267,24 +310,26 @@
 	icmp_type_t type;
 	icmp_code_t code;
-	struct sockaddr * src;
-	struct sockaddr * dest;
+	struct sockaddr *src;
+	struct sockaddr *dest;
 	size_t addrlen;
 
-	printf("p1 \n");
-	if(error){
-		switch(error){
-			case SERVICE_ICMP:
-				// process error
-				result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
-				if(result < 0){
-					return tcp_release_and_return(packet, result);
-				}
-				length = (size_t) result;
-				if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
-					return tcp_release_and_return(packet, ERROR_CODE);
-				}
-				break;
-			default:
-				return tcp_release_and_return(packet, ENOTSUP);
+	if (error) {
+		switch (error) {
+		case SERVICE_ICMP:
+			// process error
+			result = icmp_client_process_packet(packet, &type,
+			    &code, NULL, NULL);
+			if (result < 0)
+				return tcp_release_and_return(packet, result);
+
+			length = (size_t) result;
+			if (ERROR_OCCURRED(packet_trim(packet, length, 0))) {
+				return tcp_release_and_return(packet,
+				    ERROR_CODE);
+			}
+			break;
+
+		default:
+			return tcp_release_and_return(packet, ENOTSUP);
 		}
 	}
@@ -292,50 +337,52 @@
 	// TODO process received ipopts?
 	result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
-//	printf("ip len %d\n", result);
-	if(result < 0){
+	if (result < 0)
 		return tcp_release_and_return(packet, result);
-	}
+
 	offset = (size_t) result;
 
 	length = packet_get_data_length(packet);
-//	printf("packet len %d\n", length);
-	if(length <= 0){
+	if (length <= 0)
 		return tcp_release_and_return(packet, EINVAL);
-	}
-	if(length < TCP_HEADER_SIZE + offset){
+
+	if (length < TCP_HEADER_SIZE + offset)
 		return tcp_release_and_return(packet, NO_DATA);
-	}
 
 	// trim all but TCP header
-	if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
+	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
 
 	// get tcp header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return tcp_release_and_return(packet, NO_DATA);
-	}
-//	printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), ntohs(header->destination_port));
+
+//      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
+//	    ntohs(header->destination_port));
 
 	result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
-	if(result <= 0){
+	if (result <= 0)
 		return tcp_release_and_return(packet, result);
-	}
+
 	addrlen = (size_t) result;
 
-	if(ERROR_OCCURRED(tl_set_address_port(src, addrlen, ntohs(header->source_port)))){
+	if (ERROR_OCCURRED(tl_set_address_port(src, addrlen,
+	    ntohs(header->source_port)))) 
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
-
+	
 	// find the destination socket
-	socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), (const char *) src, addrlen);
-	if(! socket){
-//		printf("listening?\n");
+	socket = socket_port_find(&tcp_globals.sockets,
+	    ntohs(header->destination_port), (const char *) src, addrlen);
+	if (!socket) {
 		// find the listening destination socket
-		socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
-		if(! socket){
-			if(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error) == EOK){
-				icmp_destination_unreachable_msg(tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
+		socket = socket_port_find(&tcp_globals.sockets,
+		    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
+		    0);
+		if (!socket) {
+			if (tl_prepare_icmp_packet(tcp_globals.net_phone,
+			    tcp_globals.icmp_phone, packet, error) == EOK) {
+				icmp_destination_unreachable_msg(
+				    tcp_globals.icmp_phone, ICMP_PORT_UNREACH,
+				    0, packet);
 			}
 			return EADDRNOTAVAIL;
@@ -354,85 +401,113 @@
 	checksum = 0;
 	total_length = 0;
-	do{
-		++ fragments;
+	do {
+		++fragments;
 		length = packet_get_data_length(next_packet);
-		if(length <= 0){
+		if (length <= 0)
 			return tcp_release_and_return(packet, NO_DATA);
-		}
+
 		total_length += length;
+
 		// add partial checksum if set
-		if(! error){
-			checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
-		}
-	}while((next_packet = pq_next(next_packet)));
-//	printf("fragments %d of %d bytes\n", fragments, total_length);
-
-//	printf("lock?\n");
+		if (!error) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(packet),
+			    packet_get_data_length(packet));
+		}
+
+	} while ((next_packet = pq_next(next_packet)));
+
 	fibril_rwlock_write_lock(socket_data->local_lock);
-//	printf("locked\n");
-	if(! error){
-		if(socket_data->state == TCP_SOCKET_LISTEN){
-			if(socket_data->pseudo_header){
-				free(socket_data->pseudo_header);
-				socket_data->pseudo_header = NULL;
-				socket_data->headerlen = 0;
-			}
-			if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, &socket_data->pseudo_header, &socket_data->headerlen))){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-		}else if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, total_length))){
+
+	if (error)
+		goto error;
+	
+	if (socket_data->state == TCP_SOCKET_LISTEN) {
+
+		if (socket_data->pseudo_header) {
+			free(socket_data->pseudo_header);
+			socket_data->pseudo_header = NULL;
+			socket_data->headerlen = 0;
+		}
+
+		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src,
+		    addrlen, dest, addrlen, total_length,
+		    &socket_data->pseudo_header, &socket_data->headerlen))) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
 			return tcp_release_and_return(packet, ERROR_CODE);
 		}
-		checksum = compute_checksum(checksum, socket_data->pseudo_header, socket_data->headerlen);
-		if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
-			printf("checksum err %x -> %x\n", header->checksum, flip_checksum(compact_checksum(checksum)));
-			fibril_rwlock_write_unlock(socket_data->local_lock);
-			if(! ERROR_OCCURRED(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error))){
-				// checksum error ICMP
-				icmp_parameter_problem_msg(tcp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
-			}
-			return EINVAL;
-		}
-	}
-
+
+	} else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    total_length))) {
+		fibril_rwlock_write_unlock(socket_data->local_lock);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+	
+	checksum = compute_checksum(checksum, socket_data->pseudo_header,
+	    socket_data->headerlen);
+	if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) {
+		printf("checksum err %x -> %x\n", header->checksum,
+		    flip_checksum(compact_checksum(checksum)));
+		fibril_rwlock_write_unlock(socket_data->local_lock);
+
+		if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone,
+		    tcp_globals.icmp_phone, packet, error))) {
+			// checksum error ICMP
+			icmp_parameter_problem_msg(tcp_globals.icmp_phone,
+			    ICMP_PARAM_POINTER,
+			    ((size_t) ((void *) &header->checksum)) -
+			    ((size_t) ((void *) header)), packet);
+		}
+
+		return EINVAL;
+	}
+
+error:
 	fibril_rwlock_read_unlock(&tcp_globals.lock);
 
 	// TODO error reporting/handling
-//	printf("st %d\n", socket_data->state);
-	switch(socket_data->state){
-		case TCP_SOCKET_LISTEN:
-			ERROR_CODE = tcp_process_listen(socket, socket_data, header, packet, src, dest, addrlen);
-			break;
-		case TCP_SOCKET_SYN_RECEIVED:
-			ERROR_CODE = tcp_process_syn_received(socket, socket_data, header, packet);
-			break;
-		case TCP_SOCKET_SYN_SENT:
-			ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, packet);
-			break;
-		case TCP_SOCKET_FIN_WAIT_1:
-			// ack changing the state to FIN_WAIT_2 gets processed later
-		case TCP_SOCKET_FIN_WAIT_2:
-			// fin changing state to LAST_ACK gets processed later
-		case TCP_SOCKET_LAST_ACK:
-			// ack releasing the socket get processed later
-		case TCP_SOCKET_CLOSING:
-			// ack releasing the socket gets processed later
-		case TCP_SOCKET_ESTABLISHED:
-			ERROR_CODE = tcp_process_established(socket, socket_data, header, packet, fragments, total_length);
-			break;
-		default:
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-	}
-
-	if(ERROR_CODE != EOK){
+	switch (socket_data->state) {
+	case TCP_SOCKET_LISTEN:
+		ERROR_CODE = tcp_process_listen(socket, socket_data, header,
+		    packet, src, dest, addrlen);
+		break;
+	case TCP_SOCKET_SYN_RECEIVED:
+		ERROR_CODE = tcp_process_syn_received(socket, socket_data,
+		    header, packet);
+		break;
+	case TCP_SOCKET_SYN_SENT:
+		ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header,
+		    packet);
+		break;
+	case TCP_SOCKET_FIN_WAIT_1:
+		// ack changing the state to FIN_WAIT_2 gets processed later
+	case TCP_SOCKET_FIN_WAIT_2:
+		// fin changing state to LAST_ACK gets processed later
+	case TCP_SOCKET_LAST_ACK:
+		// ack releasing the socket get processed later
+	case TCP_SOCKET_CLOSING:
+		// ack releasing the socket gets processed later
+	case TCP_SOCKET_ESTABLISHED:
+		ERROR_CODE = tcp_process_established(socket, socket_data,
+		    header, packet, fragments, total_length);
+		break;
+	default:
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+	}
+
+	if (ERROR_CODE != EOK) {
 		printf("process %d\n", ERROR_CODE);
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 	}
+
 	return EOK;
 }
 
-int tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length){
+int
+tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    tcp_header_ref header, packet_t packet, int fragments,
+    size_t total_length)
+{
 	ERROR_DECLARE;
 
@@ -455,60 +530,72 @@
 	old_incoming = socket_data->next_incoming;
 
-	if(header->finalize){
+	if (header->finalize)
 		socket_data->fin_incoming = new_sequence_number;
-	}
-
-//	printf("pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length);
+
 	// trim begining if containing expected data
-	if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length)){
+	if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
+	    socket_data->next_incoming, new_sequence_number + total_length)) {
+
 		// get the acknowledged offset
-		if(socket_data->next_incoming < new_sequence_number){
-			offset = new_sequence_number - socket_data->next_incoming;
-		}else{
-			offset = socket_data->next_incoming - new_sequence_number;
-		}
-//		printf("offset %d\n", offset);
+		if (socket_data->next_incoming < new_sequence_number) {
+			offset = new_sequence_number -
+			    socket_data->next_incoming;
+		} else {
+			offset = socket_data->next_incoming -
+			    new_sequence_number;
+		}
+
 		new_sequence_number += offset;
 		total_length -= offset;
 		length = packet_get_data_length(packet);
 		// trim the acknowledged data
-		while(length <= offset){
+		while (length <= offset) {
 			// release the acknowledged packets
 			next_packet = pq_next(packet);
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(packet));
 			packet = next_packet;
 			offset -= length;
 			length = packet_get_data_length(packet);
 		}
-		if((offset > 0)
-			&& (ERROR_OCCURRED(packet_trim(packet, offset, 0)))){
+
+		if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet,
+		    offset, 0))))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
+
 		assert(new_sequence_number == socket_data->next_incoming);
 	}
 
 	// release if overflowing the window
-//	if(IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length)){
-//		return tcp_release_and_return(packet, EOVERFLOW);
-//	}
-
 /*
+	if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
+	    socket_data->window, new_sequence_number, new_sequence_number +
+	    total_length)) {
+		return tcp_release_and_return(packet, EOVERFLOW);
+	}
+
 	// trim end if overflowing the window
-	if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length)){
+	if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
+	    socket_data->next_incoming + socket_data->window,
+	    new_sequence_number + total_length)) {
 		// get the allowed data length
-		if(socket_data->next_incoming + socket_data->window < new_sequence_number){
-			offset = new_sequence_number - socket_data->next_incoming + socket_data->window;
-		}else{
-			offset = socket_data->next_incoming + socket_data->window - new_sequence_number;
+		if (socket_data->next_incoming + socket_data->window <
+		    new_sequence_number) {
+			offset = new_sequence_number -
+			    socket_data->next_incoming + socket_data->window;
+		} else {
+			offset = socket_data->next_incoming +
+			    socket_data->window - new_sequence_number;
 		}
 		next_packet = packet;
 		// trim the overflowing data
-		while(next_packet && (offset > 0)){
+		while (next_packet && (offset > 0)) {
 			length = packet_get_data_length(packet);
-			if(length <= offset){
+			if (length <= offset)
 				next_packet = pq_next(next_packet);
-			}else if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - offset))){
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
+			else if (ERROR_OCCURRED(packet_trim(next_packet, 0,
+			    length - offset)))
+				return tcp_release_and_return(packet,
+				    ERROR_CODE);
 			offset -= length;
 			total_length -= length - offset;
@@ -516,15 +603,17 @@
 		// release the overflowing packets
 		next_packet = pq_next(next_packet);
-		if(next_packet){
+		if (next_packet) {
 			tmp_packet = next_packet;
 			next_packet = pq_next(next_packet);
 			pq_insert_after(tmp_packet, next_packet);
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(tmp_packet));
-		}
-		assert(new_sequence_number + total_length == socket_data->next_incoming + socket_data->window);
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(tmp_packet));
+		}
+		assert(new_sequence_number + total_length ==
+		    socket_data->next_incoming + socket_data->window);
 	}
 */
 	// the expected one arrived?
-	if(new_sequence_number == socket_data->next_incoming){
+	if (new_sequence_number == socket_data->next_incoming) {
 		printf("expected\n");
 		// process acknowledgement
@@ -533,64 +622,88 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
-
-		if(total_length){
-			ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, fragments, total_length));
-		}else{
+
+		if (total_length) {
+			ERROR_PROPAGATE(tcp_queue_received_packet(socket,
+			    socket_data, packet, fragments, total_length));
+		} else {
 			total_length = 1;
 		}
+
 		socket_data->next_incoming = old_incoming + total_length;
 		packet = socket_data->incoming;
-		while(packet){
-			if(ERROR_OCCURRED(pq_get_order(socket_data->incoming, &order, NULL))){
+		while (packet) {
+
+			if (ERROR_OCCURRED(pq_get_order(socket_data->incoming,
+			    &order, NULL))) {
 				// remove the corrupted packet
 				next_packet = pq_detach(packet);
-				if(packet == socket_data->incoming){
+				if (packet == socket_data->incoming)
 					socket_data->incoming = next_packet;
-				}
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+				pq_release_remote(tcp_globals.net_phone,
+				    packet_get_id(packet));
 				packet = next_packet;
 				continue;
 			}
+
 			sequence_number = (uint32_t) order;
-			if(IS_IN_INTERVAL_OVERFLOW(sequence_number, old_incoming, socket_data->next_incoming)){
+			if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
+			    old_incoming, socket_data->next_incoming)) {
 				// move to the next
 				packet = pq_next(packet);
-			// coninual data?
-			}else if(IS_IN_INTERVAL_OVERFLOW(old_incoming, sequence_number, socket_data->next_incoming)){
+				// coninual data?
+			} else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
+			    sequence_number, socket_data->next_incoming)) {
 				// detach the packet
 				next_packet = pq_detach(packet);
-				if(packet == socket_data->incoming){
+				if (packet == socket_data->incoming)
 					socket_data->incoming = next_packet;
-				}
 				// get data length
 				length = packet_get_data_length(packet);
 				new_sequence_number = sequence_number + length;
-				if(length <= 0){
+				if (length <= 0) {
 					// remove the empty packet
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(packet));
 					packet = next_packet;
 					continue;
 				}
 				// exactly following
-				if(sequence_number == socket_data->next_incoming){
+				if (sequence_number ==
+				    socket_data->next_incoming) {
 					// queue received data
-					ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
-					socket_data->next_incoming = new_sequence_number;
+					ERROR_PROPAGATE(
+					    tcp_queue_received_packet(socket,
+					    socket_data, packet, 1,
+					    packet_get_data_length(packet)));
+					socket_data->next_incoming =
+					    new_sequence_number;
 					packet = next_packet;
 					continue;
-				// at least partly following data?
-				}else if(IS_IN_INTERVAL_OVERFLOW(sequence_number, socket_data->next_incoming, new_sequence_number)){
-					if(socket_data->next_incoming < new_sequence_number){
-						length = new_sequence_number - socket_data->next_incoming;
-					}else{
-						length = socket_data->next_incoming - new_sequence_number;
+					// at least partly following data?
+				} else if (IS_IN_INTERVAL_OVERFLOW(
+				    sequence_number, socket_data->next_incoming,
+				    new_sequence_number)) {
+					if (socket_data->next_incoming <
+					    new_sequence_number) {
+						length = new_sequence_number -
+						    socket_data->next_incoming;
+					} else {
+						length =
+						    socket_data->next_incoming -
+						    new_sequence_number;
 					}
-					if(! ERROR_OCCURRED(packet_trim(packet, length, 0))){
+					if (ERROR_NONE(packet_trim(packet,
+					    length, 0))) {
 						// queue received data
-						ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
-						socket_data->next_incoming = new_sequence_number;
+						ERROR_PROPAGATE(
+						    tcp_queue_received_packet(
+						    socket, socket_data, packet,
+						    1, packet_get_data_length(
+						    packet)));
+						socket_data->next_incoming =
+						    new_sequence_number;
 						packet = next_packet;
 						continue;
@@ -598,12 +711,15 @@
 				}
 				// remove the duplicit or corrupted packet
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+				pq_release_remote(tcp_globals.net_phone,
+				    packet_get_id(packet));
 				packet = next_packet;
 				continue;
-			}else{
+			} else {
 				break;
 			}
 		}
-	}else if(IS_IN_INTERVAL(socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window)){
+	} else if (IS_IN_INTERVAL(socket_data->next_incoming,
+	    new_sequence_number,
+	    socket_data->next_incoming + socket_data->window)) {
 		printf("in window\n");
 		// process acknowledgement
@@ -612,27 +728,33 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
 
 		next_packet = pq_detach(packet);
 		length = packet_get_data_length(packet);
-		if(ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, new_sequence_number, length))){
+		if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet,
+		    new_sequence_number, length))) {
 			// remove the corrupted packets
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-		}else{
-			while(next_packet){
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(packet));
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(next_packet));
+		} else {
+			while (next_packet) {
 				new_sequence_number += length;
 				tmp_packet = pq_detach(next_packet);
 				length = packet_get_data_length(next_packet);
-				if(ERROR_OCCURRED(pq_set_order(next_packet, new_sequence_number, length))
-					|| ERROR_OCCURRED(pq_insert_after(packet, next_packet))){
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
+				if (ERROR_OCCURRED(pq_set_order(next_packet,
+				    new_sequence_number, length)) ||
+				    ERROR_OCCURRED(pq_insert_after(packet,
+				    next_packet))) {
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(next_packet));
 				}
 				next_packet = tmp_packet;
 			}
 		}
-	}else{
+	} else {
 		printf("unexpected\n");
 		// release duplicite or restricted
@@ -641,32 +763,43 @@
 
 	// change state according to the acknowledging incoming fin
-	if(IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, socket_data->next_incoming)){
-		switch(socket_data->state){
-			case TCP_SOCKET_FIN_WAIT_1:
-			case TCP_SOCKET_FIN_WAIT_2:
-			case TCP_SOCKET_CLOSING:
-				socket_data->state = TCP_SOCKET_CLOSING;
-				break;
+	if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
+	    socket_data->next_incoming)) {
+		switch (socket_data->state) {
+		case TCP_SOCKET_FIN_WAIT_1:
+		case TCP_SOCKET_FIN_WAIT_2:
+		case TCP_SOCKET_CLOSING:
+			socket_data->state = TCP_SOCKET_CLOSING;
+			break;
 			//case TCP_ESTABLISHED:
-			default:
-				socket_data->state = TCP_SOCKET_CLOSE_WAIT;
-				break;
+		default:
+			socket_data->state = TCP_SOCKET_CLOSE_WAIT;
+			break;
 		}
 	}
 
 	packet = tcp_get_packets_to_send(socket, socket_data);
-	if(! packet){
+	if (!packet) {
 		// create the notification packet
-		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 0));
-		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, 1));
-		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, socket_data->last_outgoing + 1);
-	}
+		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+		    socket_data, 0, 0));
+		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data,
+		    packet, 1));
+		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
+		    socket_data->last_outgoing + 1);
+	}
+
 	fibril_rwlock_write_unlock(socket_data->local_lock);
+
 	// send the packet
 	tcp_send_packets(socket_data->device_id, packet);
+
 	return EOK;
 }
 
-int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length){
+int
+tcp_queue_received_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
+    size_t total_length)
+{
 	ERROR_DECLARE;
 
@@ -681,6 +814,9 @@
 
 	// queue the received packet
-	if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
-	    || ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension))){
+	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
+	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
+	    ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension))) {
 		return tcp_release_and_return(packet, ERROR_CODE);
 	}
@@ -690,9 +826,17 @@
 
 	// notify the destination socket
-	async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size), 0, 0, (ipcarg_t) fragments);
+	async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
+	    (ipcarg_t) socket->socket_id,
+	    ((packet_dimension->content < socket_data->data_fragment_size) ?
+	    packet_dimension->content : socket_data->data_fragment_size), 0, 0,
+	    (ipcarg_t) fragments);
+
 	return EOK;
 }
 
-int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
+int
+tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    tcp_header_ref header, packet_t packet)
+{
 	ERROR_DECLARE;
 
@@ -705,41 +849,50 @@
 	assert(packet);
 
-	if(header->synchronize){
-		// process acknowledgement
-		tcp_process_acknowledgement(socket, socket_data, header);
-
-		socket_data->next_incoming = ntohl(header->sequence_number) + 1;
-		// release additional packets
-		next_packet = pq_detach(packet);
-		if(next_packet){
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-		}
-		// trim if longer than the header
-		if((packet_get_data_length(packet) > sizeof(*header))
-			&& ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
-			return tcp_release_and_return(packet, ERROR_CODE);
-		}
-		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		fibril_mutex_lock(&socket_data->operation.mutex);
-		socket_data->operation.result = tcp_queue_packet(socket, socket_data, packet, 1);
-		if(socket_data->operation.result == EOK){
-			socket_data->state = TCP_SOCKET_ESTABLISHED;
-			packet = tcp_get_packets_to_send(socket, socket_data);
-			if(packet){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				// send the packet
-				tcp_send_packets(socket_data->device_id, packet);
-				// signal the result
-				fibril_condvar_signal(&socket_data->operation.condvar);
-				fibril_mutex_unlock(&socket_data->operation.mutex);
-				return EOK;
-			}
-		}
-		fibril_mutex_unlock(&socket_data->operation.mutex);
-	}
+	if (!header->synchronize)
+		return tcp_release_and_return(packet, EINVAL);
+	
+	// process acknowledgement
+	tcp_process_acknowledgement(socket, socket_data, header);
+
+	socket_data->next_incoming = ntohl(header->sequence_number) + 1;
+	// release additional packets
+	next_packet = pq_detach(packet);
+	if (next_packet) {
+		pq_release_remote(tcp_globals.net_phone,
+		    packet_get_id(next_packet));
+	}
+	// trim if longer than the header
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+	tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
+	fibril_mutex_lock(&socket_data->operation.mutex);
+	socket_data->operation.result = tcp_queue_packet(socket, socket_data,
+	    packet, 1);
+	if (socket_data->operation.result == EOK) {
+		socket_data->state = TCP_SOCKET_ESTABLISHED;
+		packet = tcp_get_packets_to_send(socket, socket_data);
+		if (packet) {
+			fibril_rwlock_write_unlock( socket_data->local_lock);
+			// send the packet
+			tcp_send_packets(socket_data->device_id, packet);
+			// signal the result
+			fibril_condvar_signal( &socket_data->operation.condvar);
+			fibril_mutex_unlock( &socket_data->operation.mutex);
+			return EOK;
+		}
+	}
+	fibril_mutex_unlock(&socket_data->operation.mutex);
 	return tcp_release_and_return(packet, EINVAL);
 }
 
-int tcp_process_listen(socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen){
+int
+tcp_process_listen(socket_core_ref listening_socket,
+    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
+    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
+    size_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -757,128 +910,144 @@
 	assert(packet);
 
-//	printf("syn %d\n", header->synchronize);
-	if(header->synchronize){
-		socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
-		if(! socket_data){
-			return tcp_release_and_return(packet, ENOMEM);
-		}else{
-			tcp_initialize_socket_data(socket_data);
-			socket_data->local_lock = listening_socket_data->local_lock;
-			socket_data->local_sockets = listening_socket_data->local_sockets;
-			socket_data->listening_socket_id = listening_socket->socket_id;
-
-			socket_data->next_incoming = ntohl(header->sequence_number);
-			socket_data->treshold = socket_data->next_incoming + ntohs(header->window);
-
-			socket_data->addrlen = addrlen;
-			socket_data->addr = malloc(socket_data->addrlen);
-			if(! socket_data->addr){
-				free(socket_data);
-				return tcp_release_and_return(packet, ENOMEM);
-			}
-			memcpy(socket_data->addr, src, socket_data->addrlen);
-
-			socket_data->dest_port = ntohs(header->source_port);
-			if(ERROR_OCCURRED(tl_set_address_port(socket_data->addr, socket_data->addrlen, socket_data->dest_port))){
-				free(socket_data->addr);
-				free(socket_data);
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-				return ERROR_CODE;
-			}
-
-//			printf("addr %p\n", socket_data->addr, socket_data->addrlen);
-			// create a socket
-			socket_id = -1;
-			if(ERROR_OCCURRED(socket_create(socket_data->local_sockets, listening_socket->phone, socket_data, &socket_id))){
-				free(socket_data->addr);
-				free(socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-
-			printf("new_sock %d\n", socket_id);
-			socket_data->pseudo_header = listening_socket_data->pseudo_header;
-			socket_data->headerlen = listening_socket_data->headerlen;
-			listening_socket_data->pseudo_header = NULL;
-			listening_socket_data->headerlen = 0;
-
-			fibril_rwlock_write_unlock(socket_data->local_lock);
-//			printf("list lg\n");
-			fibril_rwlock_write_lock(&tcp_globals.lock);
-//			printf("list locked\n");
-			// find the destination socket
-			listening_socket = socket_port_find(&tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0);
-			if((! listening_socket) || (listening_socket->socket_id != listening_socket_id)){
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				// a shadow may remain until app hangs up
-				return tcp_release_and_return(packet, EOK/*ENOTSOCK*/);
-			}
-//			printf("port %d\n", listening_socket->port);
-			listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
-			assert(listening_socket_data);
-
-//			printf("list ll\n");
-			fibril_rwlock_write_lock(listening_socket_data->local_lock);
-//			printf("list locked\n");
-
-			socket = socket_cores_find(listening_socket_data->local_sockets, socket_id);
-			if(! socket){
-				// where is the socket?!?
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				return ENOTSOCK;
-			}
-			socket_data = (tcp_socket_data_ref) socket->specific_data;
-			assert(socket_data);
-
-//			uint8_t * data = socket_data->addr;
-//			printf("addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
-
-			ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, socket, (const char *) socket_data->addr, socket_data->addrlen);
-			assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, (const char *) socket_data->addr, socket_data->addrlen));
-			//ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port);
-			//tcp_globals.last_used_port = socket->port;
-//			printf("bound %d\n", socket->port);
-			fibril_rwlock_write_unlock(&tcp_globals.lock);
-			if(ERROR_CODE != EOK){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-
-			socket_data->state = TCP_SOCKET_LISTEN;
-			socket_data->next_incoming = ntohl(header->sequence_number) + 1;
-			// release additional packets
-			next_packet = pq_detach(packet);
-			if(next_packet){
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-			}
-			// trim if longer than the header
-			if((packet_get_data_length(packet) > sizeof(*header))
-				&& ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-			tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
-			if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return ERROR_CODE;
-			}
-			packet = tcp_get_packets_to_send(socket, socket_data);
-//			printf("send %d\n", packet_get_id(packet));
-			if(! packet){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return EINVAL;
-			}else{
-				socket_data->state = TCP_SOCKET_SYN_RECEIVED;
-//				printf("unlock\n");
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				// send the packet
-				tcp_send_packets(socket_data->device_id, packet);
-				return EOK;
-			}
-		}
-	}
-	return tcp_release_and_return(packet, EINVAL);
-}
-
-int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
+	if (!header->synchronize)
+		return tcp_release_and_return(packet, EINVAL);
+
+	socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
+	if (!socket_data)
+		return tcp_release_and_return(packet, ENOMEM);
+
+	tcp_initialize_socket_data(socket_data);
+	socket_data->local_lock = listening_socket_data->local_lock;
+	socket_data->local_sockets = listening_socket_data->local_sockets;
+	socket_data->listening_socket_id = listening_socket->socket_id;
+	socket_data->next_incoming = ntohl(header->sequence_number);
+	socket_data->treshold = socket_data->next_incoming +
+	    ntohs(header->window);
+	socket_data->addrlen = addrlen;
+	socket_data->addr = malloc(socket_data->addrlen);
+	if (!socket_data->addr) {
+		free(socket_data);
+		return tcp_release_and_return(packet, ENOMEM);
+	}
+	memcpy(socket_data->addr, src, socket_data->addrlen);
+	socket_data->dest_port = ntohs(header->source_port);
+	if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr,
+	    socket_data->addrlen, socket_data->dest_port))) {
+		free(socket_data->addr);
+		free(socket_data);
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+		return ERROR_CODE;
+	}
+
+	// create a socket
+	socket_id = -1;
+	if (ERROR_OCCURRED(socket_create(socket_data->local_sockets,
+	    listening_socket->phone, socket_data, &socket_id))) {
+		free(socket_data->addr);
+		free(socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	printf("new_sock %d\n", socket_id);
+	socket_data->pseudo_header = listening_socket_data->pseudo_header;
+	socket_data->headerlen = listening_socket_data->headerlen;
+	listening_socket_data->pseudo_header = NULL;
+	listening_socket_data->headerlen = 0;
+
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+	fibril_rwlock_write_lock(&tcp_globals.lock);
+
+	// find the destination socket
+	listening_socket = socket_port_find(&tcp_globals.sockets,
+	    listening_port, SOCKET_MAP_KEY_LISTENING, 0);
+	if ((!listening_socket) ||
+	    (listening_socket->socket_id != listening_socket_id)) {
+		fibril_rwlock_write_unlock(&tcp_globals.lock);
+		// a shadow may remain until app hangs up
+		return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
+	}
+	listening_socket_data =
+	    (tcp_socket_data_ref) listening_socket->specific_data;
+	assert(listening_socket_data);
+
+	fibril_rwlock_write_lock(listening_socket_data->local_lock);
+
+	socket = socket_cores_find(listening_socket_data->local_sockets,
+	    socket_id);
+	if (!socket) {
+		// where is the socket?!?
+		fibril_rwlock_write_unlock(&tcp_globals.lock);
+		return ENOTSOCK;
+	}
+	socket_data = (tcp_socket_data_ref) socket->specific_data;
+	assert(socket_data);
+
+	ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port,
+	    socket, (const char *) socket_data->addr, socket_data->addrlen);
+	assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
+	    (const char *) socket_data->addr, socket_data->addrlen));
+
+//	ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket,
+//	    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+//	    tcp_globals.last_used_port);
+//	tcp_globals.last_used_port = socket->port;
+	fibril_rwlock_write_unlock(&tcp_globals.lock);
+	if (ERROR_CODE != EOK) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	socket_data->state = TCP_SOCKET_LISTEN;
+	socket_data->next_incoming = ntohl(header->sequence_number) + 1;
+
+	// release additional packets
+	next_packet = pq_detach(packet);
+	if (next_packet) {
+		pq_release_remote(tcp_globals.net_phone,
+		    packet_get_id(next_packet));
+	}
+
+	// trim if longer than the header
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
+
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return ERROR_CODE;
+	}
+
+	packet = tcp_get_packets_to_send(socket, socket_data);
+	if (!packet) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return EINVAL;
+	}
+
+	socket_data->state = TCP_SOCKET_SYN_RECEIVED;
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+
+	// send the packet
+	tcp_send_packets(socket_data->device_id, packet);
+
+	return EOK;
+}
+
+int
+tcp_process_syn_received(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
+{
 	ERROR_DECLARE;
 
@@ -892,49 +1061,60 @@
 	assert(packet);
 
-	printf("syn_rec\n");
-	if(header->acknowledge){
-		// process acknowledgement
-		tcp_process_acknowledgement(socket, socket_data, header);
-
-		socket_data->next_incoming = ntohl(header->sequence_number);// + 1;
-		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-		socket_data->state = TCP_SOCKET_ESTABLISHED;
-		listening_socket = socket_cores_find(socket_data->local_sockets, socket_data->listening_socket_id);
-		if(listening_socket){
-			listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
-			assert(listening_socket_data);
-
-			// queue the received packet
-			if(! ERROR_OCCURRED(dyn_fifo_push(&listening_socket->accepted, (-1 * socket->socket_id), listening_socket_data->backlog))){
-				// notify the destination socket
-				async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, (ipcarg_t) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, (ipcarg_t) socket->socket_id);
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				return EOK;
-			}
-		}
-		// send FIN
-		socket_data->state = TCP_SOCKET_FIN_WAIT_1;
-
-		// create the notification packet
-		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
-
+	if (!header->acknowledge)
+		return tcp_release_and_return(packet, EINVAL);
+
+	// process acknowledgement
+	tcp_process_acknowledgement(socket, socket_data, header);
+
+	socket_data->next_incoming = ntohl(header->sequence_number);	// + 1;
+	pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+	socket_data->state = TCP_SOCKET_ESTABLISHED;
+	listening_socket = socket_cores_find(socket_data->local_sockets,
+	    socket_data->listening_socket_id);
+	if (listening_socket) {
+		listening_socket_data =
+		    (tcp_socket_data_ref) listening_socket->specific_data;
+		assert(listening_socket_data);
+
+		// queue the received packet
+		if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted,
+		    (-1 * socket->socket_id),
+		    listening_socket_data->backlog))) {
+
+			// notify the destination socket
+			async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
+			    (ipcarg_t) listening_socket->socket_id,
+			    socket_data->data_fragment_size, TCP_HEADER_SIZE,
+			    0, (ipcarg_t) socket->socket_id);
+
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+			return EOK;
+		}
+	}
+	// send FIN
+	socket_data->state = TCP_SOCKET_FIN_WAIT_1;
+
+	// create the notification packet
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
+
+	// send the packet
+	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
+
+	// flush packets
+	packet = tcp_get_packets_to_send(socket, socket_data);
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+	if (packet) {
 		// send the packet
-		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
-
-		// flush packets
-		packet = tcp_get_packets_to_send(socket, socket_data);
-		fibril_rwlock_write_unlock(socket_data->local_lock);
-		if(packet){
-			// send the packet
-			tcp_send_packets(socket_data->device_id, packet);
-		}
-		return EOK;
-	}else{
-		return tcp_release_and_return(packet, EINVAL);
-	}
-	return EINVAL;
-}
-
-void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header){
+		tcp_send_packets(socket_data->device_id, packet);
+	}
+
+	return EOK;
+}
+
+void
+tcp_process_acknowledgement(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header)
+{
 	size_t number;
 	size_t length;
@@ -949,65 +1129,78 @@
 	assert(header);
 
-	if(header->acknowledge){
-		number = ntohl(header->acknowledgement_number);
-		// if more data acknowledged
-		if(number != socket_data->expected){
-			old = socket_data->expected;
-			if(IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, number)){
-				switch(socket_data->state){
-					case TCP_SOCKET_FIN_WAIT_1:
-						socket_data->state = TCP_SOCKET_FIN_WAIT_2;
-						break;
-					case TCP_SOCKET_LAST_ACK:
-					case TCP_SOCKET_CLOSING:
-						// fin acknowledged - release the socket in another fibril
-						tcp_prepare_timeout(tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
-						break;
-					default:
-						break;
-				}
+	if (!header->acknowledge)
+		return;
+
+	number = ntohl(header->acknowledgement_number);
+	// if more data acknowledged
+	if (number != socket_data->expected) {
+		old = socket_data->expected;
+		if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing,
+		    number)) {
+			switch (socket_data->state) {
+			case TCP_SOCKET_FIN_WAIT_1:
+				socket_data->state = TCP_SOCKET_FIN_WAIT_2;
+				break;
+			case TCP_SOCKET_LAST_ACK:
+			case TCP_SOCKET_CLOSING:
+				// fin acknowledged - release the socket in
+				// another fibril
+				tcp_prepare_timeout(tcp_release_after_timeout,
+				    socket, socket_data, 0,
+				    TCP_SOCKET_TIME_WAIT,
+				    NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
+				break;
+			default:
+				break;
 			}
-			// update the treshold if higher than set
-			if(number + ntohs(header->window) > socket_data->expected + socket_data->treshold){
-				socket_data->treshold = number + ntohs(header->window) - socket_data->expected;
-			}
-			// set new expected sequence number
-			socket_data->expected = number;
+		}
+		// update the treshold if higher than set
+		if (number + ntohs(header->window) >
+		    socket_data->expected + socket_data->treshold) {
+			socket_data->treshold = number + ntohs(header->window) -
+			    socket_data->expected;
+		}
+		// set new expected sequence number
+		socket_data->expected = number;
+		socket_data->expected_count = 1;
+		packet = socket_data->outgoing;
+		while (pq_get_order(packet, &number, &length) == EOK) {
+			if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old,
+			    (uint32_t) (number + length),
+			    (uint32_t) socket_data->expected)) {
+				next = pq_detach(packet);
+				if (packet == socket_data->outgoing)
+					socket_data->outgoing = next;
+
+				// add to acknowledged or release
+				if (pq_add(&acknowledged, packet, 0, 0) != EOK)
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(packet));
+				packet = next;
+			} else if (old < socket_data->expected)
+				break;
+		}
+		// release acknowledged
+		if (acknowledged) {
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(acknowledged));
+		}
+		return;
+		// if the same as the previous time
+	}
+	if (number == socket_data->expected) {
+		// increase the counter
+		++socket_data->expected_count;
+		if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
 			socket_data->expected_count = 1;
-			packet = socket_data->outgoing;
-			while(pq_get_order(packet, &number, &length) == EOK){
-				if(IS_IN_INTERVAL_OVERFLOW((uint32_t) old, (uint32_t)(number + length), (uint32_t) socket_data->expected)){
-					next = pq_detach(packet);
-					if(packet == socket_data->outgoing){
-						socket_data->outgoing = next;
-					}
-					// add to acknowledged or release
-					if(pq_add(&acknowledged, packet, 0, 0) != EOK){
-						pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-					}
-					packet = next;
-				}else if(old < socket_data->expected){
-					break;
-				}
-			}
-			// release acknowledged
-			if(acknowledged){
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(acknowledged));
-			}
-			return;
-		// if the same as the previous time
-		}else if(number == socket_data->expected){
-			// increase the counter
-			++ socket_data->expected_count;
-			if(socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT){
-				socket_data->expected_count = 1;
-				// TODO retransmit lock
-				//tcp_retransmit_packet(socket, socket_data, number);
-			}
-		}
-	}
-}
-
-int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
+			// TODO retransmit lock
+			//tcp_retransmit_packet(socket, socket_data, number);
+		}
+	}
+}
+
+int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
+    ipc_call_t * answer, int *answer_count)
+{
 	ERROR_DECLARE;
 
@@ -1019,19 +1212,24 @@
 
 	*answer_count = 0;
-	switch(IPC_GET_METHOD(*call)){
-		case NET_TL_RECEIVED:
-			//fibril_rwlock_read_lock(&tcp_globals.lock);
-			if(! ERROR_OCCURRED(packet_translate_remote(tcp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
-				ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, IPC_GET_ERROR(call));
-			}
-			//fibril_rwlock_read_unlock(&tcp_globals.lock);
-			return ERROR_CODE;
-		case IPC_M_CONNECT_TO_ME:
-			return tcp_process_client_messages(callid, * call);
-	}
+	switch (IPC_GET_METHOD(*call)) {
+	case NET_TL_RECEIVED:
+		//fibril_rwlock_read_lock(&tcp_globals.lock);
+		if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone,
+		    &packet, IPC_GET_PACKET(call)))) {
+			ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call),
+			    packet, SERVICE_TCP, IPC_GET_ERROR(call));
+		}
+		//fibril_rwlock_read_unlock(&tcp_globals.lock);
+		return ERROR_CODE;
+
+	case IPC_M_CONNECT_TO_ME:
+		return tcp_process_client_messages(callid, *call);
+	}
+
 	return ENOTSUP;
 }
 
-void tcp_refresh_socket_data(tcp_socket_data_ref socket_data){
+void tcp_refresh_socket_data(tcp_socket_data_ref socket_data)
+{
 	assert(socket_data);
 
@@ -1048,5 +1246,6 @@
 }
 
-void tcp_initialize_socket_data(tcp_socket_data_ref socket_data){
+void tcp_initialize_socket_data(tcp_socket_data_ref socket_data)
+{
 	assert(socket_data);
 
@@ -1057,10 +1256,11 @@
 }
 
-int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
+int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
+{
 	int res;
 	bool keep_on_going = true;
 	socket_cores_t local_sockets;
 	int app_phone = IPC_GET_PHONE(&call);
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	int socket_id;
 	size_t addrlen;
@@ -1083,168 +1283,208 @@
 	fibril_rwlock_initialize(&lock);
 
-	while(keep_on_going){
+	while (keep_on_going) {
 
 		// answer the call
 		answer_call(callid, res, &answer, answer_count);
-
 		// refresh data
 		refresh_answer(&answer, &answer_count);
-
 		// get the next call
 		callid = async_get_call(&call);
 
 		// process the call
-		switch(IPC_GET_METHOD(call)){
-			case IPC_M_PHONE_HUNGUP:
-				keep_on_going = false;
-				res = EHANGUP;
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			keep_on_going = false;
+			res = EHANGUP;
+			break;
+
+		case NET_SOCKET:
+			socket_data =
+			    (tcp_socket_data_ref) malloc(sizeof(*socket_data));
+			if (!socket_data) {
+				res = ENOMEM;
 				break;
-			case NET_SOCKET:
-				socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
-				if(! socket_data){
-					res = ENOMEM;
-				}else{
-					tcp_initialize_socket_data(socket_data);
-					socket_data->local_lock = &lock;
-					socket_data->local_sockets = &local_sockets;
-					fibril_rwlock_write_lock(&lock);
-					socket_id = SOCKET_GET_SOCKET_ID(call);
-					res = socket_create(&local_sockets, app_phone, socket_data, &socket_id);
-					SOCKET_SET_SOCKET_ID(answer, socket_id);
-					fibril_rwlock_write_unlock(&lock);
-					if(res == EOK){
-						if (tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
-							SOCKET_SET_DATA_FRAGMENT_SIZE(answer, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size));
-						}
-//						SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
-						SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
-						answer_count = 3;
-					}else{
-						free(socket_data);
-					}
+			}
+			
+			tcp_initialize_socket_data(socket_data);
+			socket_data->local_lock = &lock;
+			socket_data->local_sockets = &local_sockets;
+			fibril_rwlock_write_lock(&lock);
+			socket_id = SOCKET_GET_SOCKET_ID(call);
+			res = socket_create(&local_sockets, app_phone,
+			    socket_data, &socket_id);
+			SOCKET_SET_SOCKET_ID(answer, socket_id);
+			fibril_rwlock_write_unlock(&lock);
+			if (res != EOK) {
+				free(socket_data);
+				break;
+			}
+			if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+			    &tcp_globals.dimensions, DEVICE_INVALID_ID,
+			    &packet_dimension) == EOK) {
+				SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+				    ((packet_dimension->content <
+				    socket_data->data_fragment_size) ?
+				    packet_dimension->content :
+				    socket_data->data_fragment_size));
+			}
+//                      SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
+			SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
+			answer_count = 3;
+			break;
+
+		case NET_SOCKET_BIND:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
+				break;
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = socket_bind(&local_sockets, &tcp_globals.sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+			    tcp_globals.last_used_port);
+			if (res == EOK) {
+				socket = socket_cores_find(&local_sockets,
+				    SOCKET_GET_SOCKET_ID(call));
+				if (socket) {
+					socket_data = (tcp_socket_data_ref)
+					    socket->specific_data;
+					assert(socket_data);
+					socket_data->state = TCP_SOCKET_LISTEN;
 				}
+			}
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_write_unlock(&tcp_globals.lock);
+			free(addr);
+			break;
+
+		case NET_SOCKET_LISTEN:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+//                      fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_listen_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_BACKLOG(call));
+			fibril_rwlock_write_unlock(&lock);
+//                      fibril_rwlock_write_unlock(&tcp_globals.lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			break;
+
+		case NET_SOCKET_CONNECT:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_BIND:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = socket_bind(&local_sockets, &tcp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port);
-					if(res == EOK){
-						socket = socket_cores_find(&local_sockets, SOCKET_GET_SOCKET_ID(call));
-						if(socket){
-							socket_data = (tcp_socket_data_ref) socket->specific_data;
-							assert(socket_data);
-							socket_data->state = TCP_SOCKET_LISTEN;
-						}
-					}
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_write_unlock(&tcp_globals.lock);
-					free(addr);
-				}
-				break;
-			case NET_SOCKET_LISTEN:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-//				fibril_rwlock_write_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_listen_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_BACKLOG(call));
+			// the global lock may be released in the
+			// tcp_connect_message() function
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_connect_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
-//				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				break;
-			case NET_SOCKET_CONNECT:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					// the global lock may be released in the tcp_connect_message() function
-					fibril_rwlock_write_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = tcp_connect_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&lock);
-						fibril_rwlock_write_unlock(&tcp_globals.lock);
-						free(addr);
-					}
-				}
-				break;
-			case NET_SOCKET_ACCEPT:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_accept_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
-				SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+				fibril_rwlock_write_unlock(&tcp_globals.lock);
+				free(addr);
+			}
+			break;
+
+		case NET_SOCKET_ACCEPT:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_accept_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_SOCKET_ID(answer, res);
+				SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+				answer_count = 3;
+			}
+			break;
+
+		case NET_SOCKET_SEND:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_send_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
 				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_SOCKET_ID(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-				}
+			} else {
+				answer_count = 2;
+			}
+			break;
+
+		case NET_SOCKET_SENDTO:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_SEND:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-				SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-				if(res != EOK){
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_read_unlock(&tcp_globals.lock);
-				}else{
-					answer_count = 2;
-				}
-				break;
-			case NET_SOCKET_SENDTO:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_read_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&lock);
-						fibril_rwlock_read_unlock(&tcp_globals.lock);
-					}else{
-						answer_count = 2;
-					}
-					free(addr);
-				}
-				break;
-			case NET_SOCKET_RECV:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), NULL);
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_send_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
 				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					answer_count = 1;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_RECVFROM:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
+			} else {
+				answer_count = 2;
+			}
+			free(addr);
+			break;
+
+		case NET_SOCKET_RECV:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_recvfrom_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			    NULL);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_READ_DATA_LENGTH(answer, res);
+				answer_count = 1;
+				res = EOK;
+			}
+			break;
+
+		case NET_SOCKET_RECVFROM:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_recvfrom_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			    &addrlen);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_READ_DATA_LENGTH(answer, res);
+				SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+				answer_count = 3;
+				res = EOK;
+			}
+			break;
+
+		case NET_SOCKET_CLOSE:
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_close_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call));
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
-				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_CLOSE:
-				fibril_rwlock_write_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_close_message(&local_sockets, SOCKET_GET_SOCKET_ID(call));
-				if(res != EOK){
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_write_unlock(&tcp_globals.lock);
-				}
-				break;
-			case NET_SOCKET_GETSOCKOPT:
-			case NET_SOCKET_SETSOCKOPT:
-			default:
-				res = ENOTSUP;
-				break;
+				fibril_rwlock_write_unlock(&tcp_globals.lock);
+			}
+			break;
+
+		case NET_SOCKET_GETSOCKOPT:
+		case NET_SOCKET_SETSOCKOPT:
+		default:
+			res = ENOTSUP;
+			break;
 		}
 	}
@@ -1255,10 +1495,12 @@
 	printf("release\n");
 	// release all local sockets
-	socket_cores_release(tcp_globals.net_phone, &local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
+	socket_cores_release(tcp_globals.net_phone, &local_sockets,
+	    &tcp_globals.sockets, tcp_free_socket_data);
 
 	return EOK;
 }
 
-int tcp_timeout(void * data){
+int tcp_timeout(void *data)
+{
 	tcp_timeout_ref timeout = data;
 	int keep_write_lock = false;
@@ -1271,53 +1513,61 @@
 	async_usleep(timeout->timeout);
 	// lock the globals
-	if(timeout->globals_read_only){
+	if (timeout->globals_read_only) 
 		fibril_rwlock_read_lock(&tcp_globals.lock);
-	}else{
+	else 
 		fibril_rwlock_write_lock(&tcp_globals.lock);
-	}
+
 	// find the pending operation socket
-	socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
-	if(socket && (socket->socket_id == timeout->socket_id)){
-		socket_data = (tcp_socket_data_ref) socket->specific_data;
-		assert(socket_data);
-		if(socket_data->local_sockets == timeout->local_sockets){
-			fibril_rwlock_write_lock(socket_data->local_lock);
-			if(timeout->sequence_number){
-				// increase the timeout counter;
-				++ socket_data->timeout_count;
-				if(socket_data->timeout_count == TCP_MAX_TIMEOUTS){
-					// TODO release as connection lost
-					//tcp_refresh_socket_data(socket_data);
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}else{
-					// retransmit
-//					tcp_retransmit_packet(socket, socket_data, timeout->sequence_number);
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}
-			}else{
-				fibril_mutex_lock(&socket_data->operation.mutex);
-				// set the timeout operation result if state not changed
-				if(socket_data->state == timeout->state){
-					socket_data->operation.result = ETIMEOUT;
-					// notify the main fibril
-					fibril_condvar_signal(&socket_data->operation.condvar);
-					// keep the global write lock
-					keep_write_lock = true;
-				}else{
-					// operation is ok, do nothing
-					// unlocking from now on, so the unlock order does not matter...
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}
-				fibril_mutex_unlock(&socket_data->operation.mutex);
-			}
-		}
-	}
+	socket = socket_port_find(&tcp_globals.sockets, timeout->port,
+	    timeout->key, timeout->key_length);
+	if (!(socket && (socket->socket_id == timeout->socket_id)))
+		goto out;
+	
+	socket_data = (tcp_socket_data_ref) socket->specific_data;
+	assert(socket_data);
+	if (socket_data->local_sockets != timeout->local_sockets)
+		goto out;
+	
+	fibril_rwlock_write_lock(socket_data->local_lock);
+	if (timeout->sequence_number) {
+		// increase the timeout counter;
+		++socket_data->timeout_count;
+		if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
+			// TODO release as connection lost
+			//tcp_refresh_socket_data(socket_data);
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		} else {
+			// retransmit
+//                      tcp_retransmit_packet(socket,
+//			    socket_data, timeout->sequence_number);
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		}
+	} else {
+		fibril_mutex_lock(&socket_data->operation.mutex);
+		// set the timeout operation result if state not
+		// changed
+		if (socket_data->state == timeout->state) {
+			socket_data->operation.result = ETIMEOUT;
+			// notify the main fibril
+			fibril_condvar_signal(&socket_data->operation.condvar);
+			// keep the global write lock
+			keep_write_lock = true;
+		} else {
+			// operation is ok, do nothing
+			// unlocking from now on, so the unlocki
+			// order does not matter...
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		}
+		fibril_mutex_unlock(&socket_data->operation.mutex);
+	}
+
+out:
 	// unlock only if no socket
-	if(timeout->globals_read_only){
+	if (timeout->globals_read_only)
 		fibril_rwlock_read_unlock(&tcp_globals.lock);
-	}else if(! keep_write_lock){
+	else if (!keep_write_lock)
 		// release if not desired
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
-	}
+	
 	// release the timeout structure
 	free(timeout);
@@ -1325,9 +1575,10 @@
 }
 
-int tcp_release_after_timeout(void * data){
+int tcp_release_after_timeout(void *data)
+{
 	tcp_timeout_ref timeout = data;
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
-	fibril_rwlock_t * local_lock;
+	fibril_rwlock_t *local_lock;
 
 	assert(timeout);
@@ -1338,12 +1589,15 @@
 	fibril_rwlock_write_lock(&tcp_globals.lock);
 	// find the pending operation socket
-	socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
-	if(socket && (socket->socket_id == timeout->socket_id)){
+	socket = socket_port_find(&tcp_globals.sockets, timeout->port,
+	    timeout->key, timeout->key_length);
+	if (socket && (socket->socket_id == timeout->socket_id)) {
 		socket_data = (tcp_socket_data_ref) socket->specific_data;
 		assert(socket_data);
-		if(socket_data->local_sockets == timeout->local_sockets){
+		if (socket_data->local_sockets == timeout->local_sockets) {
 			local_lock = socket_data->local_lock;
 			fibril_rwlock_write_lock(local_lock);
-			socket_destroy(tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
+			socket_destroy(tcp_globals.net_phone,
+			    timeout->socket_id, timeout->local_sockets,
+			    &tcp_globals.sockets, tcp_free_socket_data);
 			fibril_rwlock_write_unlock(local_lock);
 		}
@@ -1356,5 +1610,8 @@
 }
 
-void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number){
+void
+tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number)
+{
 	packet_t packet;
 	packet_t copy;
@@ -1368,18 +1625,20 @@
 	packet = pq_find(socket_data->outgoing, sequence_number);
 	printf("retransmit %d\n", packet_get_id(packet));
-	if(packet){
+	if (packet) {
 		pq_get_order(packet, NULL, &data_length);
-		copy = tcp_prepare_copy(socket, socket_data, packet, data_length, sequence_number);
+		copy = tcp_prepare_copy(socket, socket_data, packet,
+		    data_length, sequence_number);
 		fibril_rwlock_write_unlock(socket_data->local_lock);
-//		printf("r send %d\n", packet_get_id(packet));
-		if(copy){
+//              printf("r send %d\n", packet_get_id(packet));
+		if (copy) 
 			tcp_send_packets(socket_data->device_id, copy);
-		}
-	}else{
+	} else {
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 	}
 }
 
-int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog){
+int
+tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog)
+{
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -1387,12 +1646,12 @@
 	assert(local_sockets);
 
-	if(backlog < 0){
+	if (backlog < 0) 
 		return EINVAL;
-	}
+
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket) 
 		return ENOTSOCK;
-	}
+	
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1400,8 +1659,12 @@
 	// set the backlog
 	socket_data->backlog = backlog;
+
 	return EOK;
 }
 
-int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen){
+int
+tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
+    struct sockaddr *addr, socklen_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1414,12 +1677,14 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
-	if(ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, addrlen))){
+	
+	if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr,
+	    addrlen))) {
 		tcp_free_socket_data(socket);
 		// unbind if bound
-		if(socket->port > 0){
-			socket_ports_exclude(&tcp_globals.sockets, socket->port);
+		if (socket->port > 0) {
+			socket_ports_exclude(&tcp_globals.sockets,
+			    socket->port);
 			socket->port = 0;
 		}
@@ -1428,5 +1693,8 @@
 }
 
-int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen){
+int
+tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
+    struct sockaddr *addr, socklen_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1442,20 +1710,28 @@
 	assert(socket_data);
 	assert(socket->specific_data == socket_data);
-	if((socket_data->state != TCP_SOCKET_INITIAL)
-		&& ((socket_data->state != TCP_SOCKET_LISTEN) || (socket->port <= 0))){
+	if ((socket_data->state != TCP_SOCKET_INITIAL) &&
+	    ((socket_data->state != TCP_SOCKET_LISTEN) ||
+	    (socket->port <= 0)))
 		return EINVAL;
-	}
+
 	// get the destination port
-	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &socket_data->dest_port));
-	if(socket->port <= 0){
+	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen,
+	    &socket_data->dest_port));
+	if (socket->port <= 0) {
 		// try to find a free port
-		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port));
+		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets,
+		    socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    tcp_globals.last_used_port));
 		// set the next port as the search starting port number
 		tcp_globals.last_used_port = socket->port;
 	}
-	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP, addr, addrlen, &socket_data->device_id, &socket_data->pseudo_header, &socket_data->headerlen));
+
+	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
+	    addr, addrlen, &socket_data->device_id,
+	    &socket_data->pseudo_header, &socket_data->headerlen));
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 1, 0));
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 1, 0));
 
 	// unlock the globals and wait for an operation
@@ -1465,12 +1741,16 @@
 	socket_data->addrlen = addrlen;
 	// send the packet
-	if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))
-		|| ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))){
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) ||
+	    ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data,
+	    0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) {
+
 		socket_data->addr = NULL;
 		socket_data->addrlen = 0;
 		fibril_rwlock_write_lock(&tcp_globals.lock);
-	}else{
+
+	} else {
+
 		packet = tcp_get_packets_to_send(socket, socket_data);
-		if(packet){
+		if (packet) {
 			fibril_mutex_lock(&socket_data->operation.mutex);
 			fibril_rwlock_write_unlock(socket_data->local_lock);
@@ -1478,12 +1758,14 @@
 			printf("connecting %d\n", packet_get_id(packet));
 			tcp_send_packets(socket_data->device_id, packet);
+
 			// wait for a reply
-			fibril_condvar_wait(&socket_data->operation.condvar, &socket_data->operation.mutex);
+			fibril_condvar_wait(&socket_data->operation.condvar,
+			    &socket_data->operation.mutex);
 			ERROR_CODE = socket_data->operation.result;
-			if(ERROR_CODE != EOK){
+			if (ERROR_CODE != EOK) {
 				socket_data->addr = NULL;
 				socket_data->addrlen = 0;
 			}
-		}else{
+		} else {
 			socket_data->addr = NULL;
 			socket_data->addrlen = 0;
@@ -1498,5 +1780,8 @@
 }
 
-int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
+int
+tcp_queue_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
+{
 	ERROR_DECLARE;
 
@@ -1509,21 +1794,26 @@
 	// get tcp header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return NO_DATA;
-	}
+	
 	header->destination_port = htons(socket_data->dest_port);
 	header->source_port = htons(socket->port);
 	header->sequence_number = htonl(socket_data->next_outgoing);
-	if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, socket_data->addrlen))){
+
+	if (ERROR_OCCURRED(packet_set_addr(packet, NULL,
+	    (uint8_t *) socket_data->addr, socket_data->addrlen)))
 		return tcp_release_and_return(packet, EINVAL);
-	}
+
 	// remember the outgoing FIN
-	if(header->finalize){
+	if (header->finalize) 
 		socket_data->fin_outgoing = socket_data->next_outgoing;
-	}
+	
 	return EOK;
 }
 
-int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
+int
+tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length)
+{
 	ERROR_DECLARE;
 
@@ -1532,14 +1822,18 @@
 	assert(socket->specific_data == socket_data);
 
-	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, data_length));
-
-	if(ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, data_length))){
+	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet,
+	    data_length));
+
+	if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet,
+	    socket_data->next_outgoing, data_length)))
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
+
 	socket_data->next_outgoing += data_length;
 	return EOK;
 }
 
-packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data){
+packet_t
+tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
+{
 	ERROR_DECLARE;
 
@@ -1555,38 +1849,47 @@
 
 	packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
-	while(packet){
+	while (packet) {
 		pq_get_order(packet, NULL, &data_length);
+
 		// send only if fits into the window
 		// respecting the possible overflow
-		if(IS_IN_INTERVAL_OVERFLOW((uint32_t) socket_data->last_outgoing, (uint32_t)(socket_data->last_outgoing + data_length), (uint32_t)(socket_data->expected + socket_data->treshold))){
-			copy = tcp_prepare_copy(socket, socket_data, packet, data_length, socket_data->last_outgoing + 1);
-			if(! copy){
-				return sending;
-			}
-			if(! sending){
-				sending = copy;
-			}else{
-				if(ERROR_OCCURRED(pq_insert_after(previous, copy))){
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(copy));
-					return sending;
-				}
-			}
-			previous = copy;
-			packet = pq_next(packet);
-			// overflow occurred ?
-			if((! packet) && (socket_data->last_outgoing > socket_data->next_outgoing)){
-				printf("gpts overflow\n");
-				// continue from the beginning
-				packet = socket_data->outgoing;
-			}
-			socket_data->last_outgoing += data_length;
-		}else{
+		if (!IS_IN_INTERVAL_OVERFLOW(
+		    (uint32_t) socket_data->last_outgoing,
+		    (uint32_t) (socket_data->last_outgoing + data_length),
+		    (uint32_t) (socket_data->expected + socket_data->treshold)))
 			break;
-		}
-	}
+
+		copy = tcp_prepare_copy(socket, socket_data, packet,
+		    data_length, socket_data->last_outgoing + 1);
+		if (!copy) 
+			return sending;
+			
+		if (!sending) {
+			sending = copy;
+		} else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) {
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(copy));
+			return sending;
+		}
+
+		previous = copy;
+		packet = pq_next(packet);
+		// overflow occurred ?
+		if ((!packet) &&
+		    (socket_data->last_outgoing > socket_data->next_outgoing)) {
+			printf("gpts overflow\n");
+			// continue from the beginning
+			packet = socket_data->outgoing;
+		}
+		socket_data->last_outgoing += data_length;
+	}
+
 	return sending;
 }
 
-packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number){
+packet_t
+tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length, size_t sequence_number)
+{
 	ERROR_DECLARE;
 
@@ -1599,5 +1902,7 @@
 
 	// adjust the pseudo header
-	if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length(packet)))){
+	if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    packet_get_data_length(packet)))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1606,5 +1911,5 @@
 	// get the header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1613,6 +1918,7 @@
 
 	// adjust the header
-	if(socket_data->next_incoming){
-		header->acknowledgement_number = htonl(socket_data->next_incoming);
+	if (socket_data->next_incoming) {
+		header->acknowledgement_number =
+		    htonl(socket_data->next_incoming);
 		header->acknowledge = 1;
 	}
@@ -1621,18 +1927,26 @@
 	// checksum
 	header->checksum = 0;
-	checksum = compute_checksum(0, socket_data->pseudo_header, socket_data->headerlen);
-	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), packet_get_data_length(packet));
+	checksum = compute_checksum(0, socket_data->pseudo_header,
+	    socket_data->headerlen);
+	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet),
+	    packet_get_data_length(packet));
 	header->checksum = htons(flip_checksum(compact_checksum(checksum)));
+
 	// prepare the packet
-	if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0))
-	// prepare the timeout
-		|| ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true))){
+	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0,
+	    0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket,
+	    socket_data, sequence_number, socket_data->state,
+	    socket_data->timeout, true))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
 	}
+
 	return packet;
 }
 
-packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number){
+packet_t
+tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length, size_t sequence_number)
+{
 	packet_t copy;
 
@@ -1643,22 +1957,28 @@
 	// make a copy of the packet
 	copy = packet_get_copy(tcp_globals.net_phone, packet);
-	if(! copy){
+	if (!copy)
 		return NULL;
-	}
-
-	return tcp_send_prepare_packet(socket, socket_data, copy, data_length, sequence_number);
-}
-
-void tcp_send_packets(device_id_t device_id, packet_t packet){
+
+	return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
+	    sequence_number);
+}
+
+void tcp_send_packets(device_id_t device_id, packet_t packet)
+{
 	packet_t next;
 
-	while(packet){
+	while (packet) {
 		next = pq_detach(packet);
-		ip_send_msg(tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0);
+		ip_send_msg(tcp_globals.ip_phone, device_id, packet,
+		    SERVICE_TCP, 0);
 		packet = next;
 	}
 }
 
-void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize){
+void
+tcp_prepare_operation_header(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
+    int finalize)
+{
 	assert(socket);
 	assert(socket_data);
@@ -1674,5 +1994,10 @@
 }
 
-int tcp_prepare_timeout(int (*timeout_function)(void * tcp_timeout_t), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only){
+int
+tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
+    socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
+    int globals_read_only)
+{
 	tcp_timeout_ref operation_timeout;
 	fid_t fibril;
@@ -1683,8 +2008,9 @@
 
 	// prepare the timeout with key bundle structure
-	operation_timeout = malloc(sizeof(*operation_timeout) + socket->key_length + 1);
-	if(! operation_timeout){
+	operation_timeout = malloc(sizeof(*operation_timeout) +
+	    socket->key_length + 1);
+	if (!operation_timeout)
 		return ENOMEM;
-	}
+
 	bzero(operation_timeout, sizeof(*operation_timeout));
 	operation_timeout->globals_read_only = globals_read_only;
@@ -1697,5 +2023,6 @@
 
 	// copy the key
-	operation_timeout->key = ((char *) operation_timeout) + sizeof(*operation_timeout);
+	operation_timeout->key = ((char *) operation_timeout) +
+	    sizeof(*operation_timeout);
 	operation_timeout->key_length = socket->key_length;
 	memcpy(operation_timeout->key, socket->key, socket->key_length);
@@ -1704,9 +2031,9 @@
 	// prepare the timeouting thread
 	fibril = fibril_create(timeout_function, operation_timeout);
-	if(! fibril){
+	if (!fibril) {
 		free(operation_timeout);
 		return EPARTY;
 	}
-//	fibril_mutex_lock(&socket_data->operation.mutex);
+//      fibril_mutex_lock(&socket_data->operation.mutex);
 	// start the timeouting fibril
 	fibril_add_ready(fibril);
@@ -1715,5 +2042,8 @@
 }
 
-int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
+int
+tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t * addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1728,21 +2058,22 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
-	if(! socket->specific_data){
+	if (!socket->specific_data)
 		return NO_DATA;
-	}
+
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
 
 	// check state
-	if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
+	if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
+	    (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
 		return ENOTCONN;
-	}
 
 	// send the source address if desired
-	if(addrlen){
-		ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
+	if (addrlen) {
+		ERROR_PROPAGATE(data_reply(socket_data->addr,
+		    socket_data->addrlen));
 		*addrlen = socket_data->addrlen;
 	}
@@ -1750,8 +2081,9 @@
 	// get the next received packet
 	packet_id = dyn_fifo_value(&socket->received);
-	if(packet_id < 0){
+	if (packet_id < 0)
 		return NO_DATA;
-	}
-	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, packet_id));
+
+	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet,
+	    packet_id));
 
 	// reply the packets
@@ -1765,5 +2097,8 @@
 }
 
-int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags){
+int
+tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
+    size_t * data_fragment_size, int flags)
+{
 	ERROR_DECLARE;
 
@@ -1782,36 +2117,42 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
-	if(! socket->specific_data){
+	if (!socket->specific_data)
 		return NO_DATA;
-	}
+
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
 
 	// check state
-	if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
+	if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
+	    (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
 		return ENOTCONN;
-	}
-
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
-
-	*data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
-
-	for(index = 0; index < fragments; ++ index){
+
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	   &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+
+	*data_fragment_size =
+	    ((packet_dimension->content < socket_data->data_fragment_size) ?
+	    packet_dimension->content : socket_data->data_fragment_size);
+
+	for (index = 0; index < fragments; ++index) {
 		// read the data fragment
-		result = tl_socket_read_packet_data(tcp_globals.net_phone, &packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen);
-		if(result < 0){
+		result = tl_socket_read_packet_data(tcp_globals.net_phone,
+		    &packet, TCP_HEADER_SIZE, packet_dimension,
+		    socket_data->addr, socket_data->addrlen);
+		if (result < 0)
 			return result;
-		}
+
 		total_length = (size_t) result;
 		// prefix the tcp header
 		header = PACKET_PREFIX(packet, tcp_header_t);
-		if(! header){
+		if (!header)
 			return tcp_release_and_return(packet, ENOMEM);
-		}
+
 		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 0));
+		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet,
+		    0));
 	}
 
@@ -1820,5 +2161,6 @@
 	fibril_rwlock_write_unlock(socket_data->local_lock);
 	fibril_rwlock_read_unlock(&tcp_globals.lock);
-	if(packet){
+
+	if (packet) {
 		// send the packet
 		tcp_send_packets(socket_data->device_id, packet);
@@ -1828,5 +2170,7 @@
 }
 
-int tcp_close_message(socket_cores_ref local_sockets, int socket_id){
+int
+tcp_close_message(socket_cores_ref local_sockets, int socket_id)
+{
 	ERROR_DECLARE;
 
@@ -1837,7 +2181,7 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1845,25 +2189,32 @@
 
 	// check state
-	switch(socket_data->state){
-		case TCP_SOCKET_ESTABLISHED:
-			socket_data->state = TCP_SOCKET_FIN_WAIT_1;
-			break;
-		case TCP_SOCKET_CLOSE_WAIT:
-			socket_data->state = TCP_SOCKET_LAST_ACK;
-			break;
-//		case TCP_SOCKET_LISTEN:
-		default:
-			// just destroy
-			if(! ERROR_OCCURRED(socket_destroy(tcp_globals.net_phone, socket_id, local_sockets, &tcp_globals.sockets, tcp_free_socket_data))){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-			}
-			return ERROR_CODE;
-	}
+	switch (socket_data->state) {
+	case TCP_SOCKET_ESTABLISHED:
+		socket_data->state = TCP_SOCKET_FIN_WAIT_1;
+		break;
+
+	case TCP_SOCKET_CLOSE_WAIT:
+		socket_data->state = TCP_SOCKET_LAST_ACK;
+		break;
+
+//      case TCP_SOCKET_LISTEN:
+
+	default:
+		// just destroy
+		if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id,
+		    local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data))) {
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+			fibril_rwlock_write_unlock(&tcp_globals.lock);
+		}
+		return ERROR_CODE;
+	}
+
 	// send FIN
 	// TODO should I wait to complete?
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
 
 	// send the packet
@@ -1874,12 +2225,17 @@
 	fibril_rwlock_write_unlock(socket_data->local_lock);
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-	if(packet){
+
+	if (packet) {
 		// send the packet
 		tcp_send_packets(socket_data->device_id, packet);
 	}
+
 	return EOK;
 }
 
-int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize){
+int
+tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
+    tcp_socket_data_ref socket_data, int synchronize, int finalize)
+{
 	ERROR_DECLARE;
 
@@ -1890,21 +2246,31 @@
 
 	// get the device packet dimension
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension));
+
 	// get a new packet
-	*packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix);
-	if(! * packet){
+	*packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
+	    packet_dimension->addr_len, packet_dimension->prefix,
+	    packet_dimension->suffix);
+	
+	if (!*packet) 
 		return ENOMEM;
-	}
+
 	// allocate space in the packet
 	header = PACKET_SUFFIX(*packet, tcp_header_t);
-	if(! header){
+	if (!header)
 		tcp_release_and_return(*packet, ENOMEM);
-	}
-
-	tcp_prepare_operation_header(socket, socket_data, header, synchronize, finalize);
+
+	tcp_prepare_operation_header(socket, socket_data, header, synchronize,
+	    finalize);
+
 	return EOK;
 }
 
-int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen){
+int
+tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
+    int new_socket_id, size_t * data_fragment_size, size_t * addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1920,7 +2286,7 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1928,40 +2294,52 @@
 
 	// check state
-	if(socket_data->state != TCP_SOCKET_LISTEN){
+	if (socket_data->state != TCP_SOCKET_LISTEN)
 		return EINVAL;
-	}
-
-	do{
+
+	do {
 		socket_id = dyn_fifo_value(&socket->accepted);
-		if(socket_id < 0){
+		if (socket_id < 0)
 			return ENOTSOCK;
-		}
 		socket_id *= -1;
 
 		accepted = socket_cores_find(local_sockets, socket_id);
-		if(! accepted){
+		if (!accepted)
 			return ENOTSOCK;
-		}
+
 		// get the socket specific data
 		socket_data = (tcp_socket_data_ref) accepted->specific_data;
 		assert(socket_data);
 		// TODO can it be in another state?
-		if(socket_data->state == TCP_SOCKET_ESTABLISHED){
-			ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
-			ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+		if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
+			ERROR_PROPAGATE(data_reply(socket_data->addr,
+			    socket_data->addrlen));
+			ERROR_PROPAGATE(tl_get_ip_packet_dimension(
+			    tcp_globals.ip_phone, &tcp_globals.dimensions,
+			    socket_data->device_id, &packet_dimension));
 			*addrlen = socket_data->addrlen;
-			*data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
-			if(new_socket_id > 0){
-				ERROR_PROPAGATE(socket_cores_update(local_sockets, accepted->socket_id, new_socket_id));
+
+			*data_fragment_size =
+			    ((packet_dimension->content <
+			    socket_data->data_fragment_size) ?
+			    packet_dimension->content :
+			    socket_data->data_fragment_size);
+	
+			if (new_socket_id > 0) {
+				ERROR_PROPAGATE(socket_cores_update(
+				    local_sockets, accepted->socket_id,
+				    new_socket_id));
 				accepted->socket_id = new_socket_id;
 			}
 		}
 		dyn_fifo_pop(&socket->accepted);
-	}while(socket_data->state != TCP_SOCKET_ESTABLISHED);
+	} while (socket_data->state != TCP_SOCKET_ESTABLISHED);
+
 	printf("ret accept %d\n", accepted->socket_id);
 	return accepted->socket_id;
 }
 
-void tcp_free_socket_data(socket_core_ref socket){
+void
+tcp_free_socket_data(socket_core_ref socket)
+{
 	tcp_socket_data_ref socket_data;
 
@@ -1974,6 +2352,6 @@
 	assert(socket_data);
 	//free the pseudo header
-	if(socket_data->pseudo_header){
-		if(socket_data->headerlen){
+	if (socket_data->pseudo_header) {
+		if (socket_data->headerlen) {
 			printf("d pseudo\n");
 			free(socket_data->pseudo_header);
@@ -1984,6 +2362,6 @@
 	socket_data->headerlen = 0;
 	// free the address
-	if(socket_data->addr){
-		if(socket_data->addrlen){
+	if (socket_data->addr) {
+		if (socket_data->addrlen) {
 			printf("d addr\n");
 			free(socket_data->addr);
@@ -1995,5 +2373,6 @@
 }
 
-int tcp_release_and_return(packet_t packet, int result){
+int tcp_release_and_return(packet_t packet, int result)
+{
 	pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 	return result;
@@ -2013,25 +2392,36 @@
 	 */
 	ipc_answer_0(iid, EOK);
-	
-	while(true) {
+
+	while (true) {
 		ipc_call_t answer;
 		int answer_count;
-		
-		/* Clear the answer structure */
+
+		/*
+		   Clear the answer structure 
+		 */
 		refresh_answer(&answer, &answer_count);
-		
-		/* Fetch the next message */
+
+		/*
+		   Fetch the next message 
+		 */
 		ipc_call_t call;
 		ipc_callid_t callid = async_get_call(&call);
-		
-		/* Process the message */
+
+		/*
+		   Process the message 
+		 */
 		int res = tl_module_message_standalone(callid, &call, &answer,
 		    &answer_count);
-		
-		/* End if said to either by the message or the processing result */
-		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
+
+		/*
+		   End if said to either by the message or the processing result 
+		 */
+		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
+		    (res == EHANGUP))
 			return;
-		
-		/* Answer the message */
+
+		/*
+		   Answer the message 
+		 */
 		answer_call(callid, res, &answer, answer_count);
 	}
@@ -2047,12 +2437,15 @@
  *
  */
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
 {
 	ERROR_DECLARE;
-	
-	/* Start the module */
+
+	/*
+	   Start the module 
+	 */
 	if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
 		return ERROR_CODE;
-	
+
 	return EOK;
 }
Index: uspace/srv/net/tl/tcp/tcp_module.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/tcp/tcp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/net/tl/udp/udp.c
===================================================================
--- uspace/srv/net/tl/udp/udp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/udp/udp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,6 +42,7 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
@@ -60,5 +61,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <socket_core.h>
 #include <socket_messages.h>
@@ -72,61 +72,69 @@
 #include "udp_module.h"
 
-/** UDP module name.
- */
+/** UDP module name. */
 #define NAME	"UDP protocol"
 
-/** Default UDP checksum computing.
- */
+/** Default UDP checksum computing. */
 #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING	true
 
-/** Default UDP autobind when sending via unbound sockets.
- */
+/** Default UDP autobind when sending via unbound sockets. */
 #define NET_DEFAULT_UDP_AUTOBINDING	true
 
-/** Maximum UDP fragment size.
- */
-#define MAX_UDP_FRAGMENT_SIZE	65535
-
-/** Free ports pool start.
- */
-#define UDP_FREE_PORTS_START	1025
-
-/** Free ports pool end.
- */
+/** Maximum UDP fragment size. */
+#define MAX_UDP_FRAGMENT_SIZE		65535
+
+/** Free ports pool start. */
+#define UDP_FREE_PORTS_START		1025
+
+/** Free ports pool end. */
 #define UDP_FREE_PORTS_END		65535
 
 /** Processes the received UDP packet queue.
+ *
  *  Is used as an entry point from the underlying IP module.
  *  Locks the global lock and calls udp_process_packet() function.
+ *
  *  @param[in] device_id The receiving device identifier.
  *  @param[in,out] packet The received packet queue.
- *  @param receiver The target service. Ignored parameter.
- *  @param[in] error The packet error reporting service. Prefixes the received packet.
- *  @returns EOK on success.
- *  @returns Other error codes as defined for the udp_process_packet() function.
- */
-int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
+ *  @param receiver	The target service. Ignored parameter.
+ *  @param[in] error	The packet error reporting service. Prefixes the
+ *			received packet.
+ *  @returns		EOK on success.
+ *  @returns		Other error codes as defined for the
+ *			udp_process_packet() function.
+ */
+int
+udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error);
 
 /** Processes the received UDP packet queue.
+ *
  *  Notifies the destination socket application.
- *  Releases the packet on error or sends an ICMP error notification..
+ *  Releases the packet on error or sends an ICMP error notification.
+ *
  *  @param[in] device_id The receiving device identifier.
  *  @param[in,out] packet The received packet queue.
- *  @param[in] error The packet error reporting service. Prefixes the received packet.
- *  @returns EOK on success.
- *  @returns EINVAL if the packet is not valid.
- *  @returns EINVAL if the stored packet address is not the an_addr_t.
- *  @returns EINVAL if the packet does not contain any data.
- *  @returns NO_DATA if the packet content is shorter than the user datagram header.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns EADDRNOTAVAIL if the destination socket does not exist.
- *  @returns Other error codes as defined for the ip_client_process_packet() function.
- */
-int udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
+ *  @param[in] error	The packet error reporting service. Prefixes the
+ *			received packet.
+ *  @returns		EOK on success.
+ *  @returns		EINVAL if the packet is not valid.
+ *  @returns		EINVAL if the stored packet address is not the
+ *			an_addr_t.
+ *  @returns		EINVAL if the packet does not contain any data.
+ *  @returns 		NO_DATA if the packet content is shorter than the user
+ *			datagram header.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		EADDRNOTAVAIL if the destination socket does not exist.
+ *  @returns		Other error codes as defined for the
+ *			ip_client_process_packet() function.
+ */
+int
+udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
 
 /** Releases the packet and returns the result.
- *  @param[in] packet The packet queue to be released.
- *  @param[in] result The result to be returned.
- *  @return The result parameter.
+ *
+ *  @param[in] packet	The packet queue to be released.
+ *  @param[in] result	The result to be returned.
+ *  @return		The result parameter.
  */
 int udp_release_and_return(packet_t packet, int result);
@@ -137,51 +145,68 @@
 
 /** Processes the socket client messages.
+ *
  *  Runs until the client module disconnects.
- *  @param[in] callid The message identifier.
- *  @param[in] call The message parameters.
- *  @returns EOK on success.
- *  @see socket.h
+ *
+ *  @param[in] callid 	The message identifier.
+ *  @param[in] call	The message parameters.
+ *  @returns		EOK on success.
+ *  @see		socket.h
  */
 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
 
 /** Sends data from the socket to the remote address.
+ *
  *  Binds the socket to a free port if not already connected/bound.
  *  Handles the NET_SOCKET_SENDTO message.
  *  Supports AF_INET and AF_INET6 address families.
+ *
  *  @param[in,out] local_sockets The application local sockets.
  *  @param[in] socket_id Socket identifier.
- *  @param[in] addr The destination address.
- *  @param[in] addrlen The address length.
+ *  @param[in] addr	The destination address.
+ *  @param[in] addrlen	The address length.
  *  @param[in] fragments The number of data fragments.
  *  @param[out] data_fragment_size The data fragment size in bytes.
- *  @param[in] flags Various send flags.
- *  @returns EOK on success.
- *  @returns EAFNOTSUPPORT if the address family is not supported.
- *  @returns ENOTSOCK if the socket is not found.
- *  @returns EINVAL if the address is invalid.
- *  @returns ENOTCONN if the sending socket is not and cannot be bound.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the socket_read_packet_data() function.
- *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
- *  @returns Other error codes as defined for the ip_send_msg() function.
- */
-int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags);
+ *  @param[in] flags	Various send flags.
+ *  @returns		EOK on success.
+ *  @returns		EAFNOTSUPPORT if the address family is not supported.
+ *  @returns		ENOTSOCK if the socket is not found.
+ *  @returns		EINVAL if the address is invalid.
+ *  @returns		ENOTCONN if the sending socket is not and cannot be
+ *			bound.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		Other error codes as defined for the
+ *			socket_read_packet_data() function.
+ *  @returns		Other error codes as defined for the
+ *			ip_client_prepare_packet() function.
+ *  @returns		Other error codes as defined for the ip_send_msg()
+ *			function.
+ */
+int
+udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
+    const struct sockaddr * addr, socklen_t addrlen, int fragments,
+    size_t * data_fragment_size, int flags);
 
 /** Receives data to the socket.
+ *
  *  Handles the NET_SOCKET_RECVFROM message.
  *  Replies the source address as well.
+ *
  *  @param[in] local_sockets The application local sockets.
  *  @param[in] socket_id Socket identifier.
- *  @param[in] flags Various receive flags.
- *  @param[out] addrlen The source address length.
- *  @returns The number of bytes received.
- *  @returns ENOTSOCK if the socket is not found.
- *  @returns NO_DATA if there are no received packets or data.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns EINVAL if the received address is not an IP address.
- *  @returns Other error codes as defined for the packet_translate() function.
- *  @returns Other error codes as defined for the data_reply() function.
- */
-int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
+ *  @param[in] flags	Various receive flags.
+ *  @param[out] addrlen	The source address length.
+ *  @returns		The number of bytes received.
+ *  @returns		ENOTSOCK if the socket is not found.
+ *  @returns		NO_DATA if there are no received packets or data.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		EINVAL if the received address is not an IP address.
+ *  @returns		Other error codes as defined for the packet_translate()
+ *			function.
+ *  @returns		Other error codes as defined for the data_reply()
+ *			function.
+ */
+int
+udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t * addrlen);
 
 /*@}*/
@@ -189,10 +214,20 @@
 /** UDP global data.
  */
-udp_globals_t	udp_globals;
-
-int udp_initialize(async_client_conn_t client_connection){
+udp_globals_t udp_globals;
+
+int udp_initialize(async_client_conn_t client_connection)
+{
 	ERROR_DECLARE;
 
-	measured_string_t names[] = {{str_dup("UDP_CHECKSUM_COMPUTING"), 22}, {str_dup("UDP_AUTOBINDING"), 15}};
+	measured_string_t names[] = {
+		{
+			str_dup("UDP_CHECKSUM_COMPUTING"),
+			22
+		},
+		{
+			str_dup("UDP_AUTOBINDING"),
+			15
+		}
+	};
 	measured_string_ref configuration;
 	size_t count = sizeof(names) / sizeof(measured_string_t);
@@ -201,13 +236,18 @@
 	fibril_rwlock_initialize(&udp_globals.lock);
 	fibril_rwlock_write_lock(&udp_globals.lock);
-	udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
-	udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg);
-	if(udp_globals.ip_phone < 0){
+
+	udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
+	    ICMP_CONNECT_TIMEOUT);
+	udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
+	    SERVICE_UDP, client_connection, udp_received_msg);
+	if (udp_globals.ip_phone < 0)
 		return udp_globals.ip_phone;
-	}
+
 	// read default packet dimensions
-	ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
+	ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
+	    &udp_globals.packet_dimension));
 	ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets));
-	if(ERROR_OCCURRED(packet_dimensions_initialize(&udp_globals.dimensions))){
+	if (ERROR_OCCURRED(packet_dimensions_initialize(
+	    &udp_globals.dimensions))) {
 		socket_ports_destroy(&udp_globals.sockets);
 		return ERROR_CODE;
@@ -216,35 +256,43 @@
 	udp_globals.packet_dimension.content -= sizeof(udp_header_t);
 	udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
+
 	// get configuration
 	udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
 	udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
 	configuration = &names[0];
-	ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, count, &data));
-	if(configuration){
-		if(configuration[0].value){
-			udp_globals.checksum_computing = (configuration[0].value[0] == 'y');
-		}
-		if(configuration[1].value){
-			udp_globals.autobinding = (configuration[1].value[0] == 'y');
-		}
+	ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration,
+	    count, &data));
+	if (configuration) {
+		if (configuration[0].value)
+			udp_globals.checksum_computing =
+			    (configuration[0].value[0] == 'y');
+		
+		if (configuration[1].value)
+			udp_globals.autobinding =
+			    (configuration[1].value[0] == 'y');
+
 		net_free_settings(configuration, data);
 	}
+
 	fibril_rwlock_write_unlock(&udp_globals.lock);
 	return EOK;
 }
 
-int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
+int
+udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error)
+{
 	int result;
 
 	fibril_rwlock_write_lock(&udp_globals.lock);
 	result = udp_process_packet(device_id, packet, error);
-	if(result != EOK){
+	if (result != EOK)
 		fibril_rwlock_write_unlock(&udp_globals.lock);
-	}
 
 	return result;
 }
 
-int udp_process_packet(device_id_t device_id, packet_t packet, services_t error){
+int udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
+{
 	ERROR_DECLARE;
 
@@ -262,57 +310,57 @@
 	icmp_code_t code;
 	void *ip_header;
-	struct sockaddr * src;
-	struct sockaddr * dest;
+	struct sockaddr *src;
+	struct sockaddr *dest;
 	packet_dimension_ref packet_dimension;
 
-	if(error){
-		switch(error){
-			case SERVICE_ICMP:
-				// ignore error
-				// length = icmp_client_header_length(packet);
-				// process error
-				result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
-				if(result < 0){
-					return udp_release_and_return(packet, result);
-				}
-				length = (size_t) result;
-				if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
-					return udp_release_and_return(packet, ERROR_CODE);
-				}
-				break;
-			default:
-				return udp_release_and_return(packet, ENOTSUP);
-		}
-	}
+	if (error) {
+		switch (error) {
+		case SERVICE_ICMP:
+			// ignore error
+			// length = icmp_client_header_length(packet);
+			// process error
+			result = icmp_client_process_packet(packet, &type,
+			    &code, NULL, NULL);
+			if (result < 0)
+				return udp_release_and_return(packet, result);
+			length = (size_t) result;
+			if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
+				return udp_release_and_return(packet,
+				    ERROR_CODE);
+			break;
+		default:
+			return udp_release_and_return(packet, ENOTSUP);
+		}
+	}
+
 	// TODO process received ipopts?
 	result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
-	if(result < 0){
+	if (result < 0)
 		return udp_release_and_return(packet, result);
-	}
 	offset = (size_t) result;
 
 	length = packet_get_data_length(packet);
-	if(length <= 0){
+	if (length <= 0)
 		return udp_release_and_return(packet, EINVAL);
-	}
-	if(length < UDP_HEADER_SIZE + offset){
+	if (length < UDP_HEADER_SIZE + offset)
 		return udp_release_and_return(packet, NO_DATA);
-	}
 
 	// trim all but UDP header
-	if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
+	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
 		return udp_release_and_return(packet, ERROR_CODE);
-	}
 
 	// get udp header
 	header = (udp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return udp_release_and_return(packet, NO_DATA);
-	}
+
 	// find the destination socket
-	socket = socket_port_find(&udp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
-	if(! socket){
-		if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
-			icmp_destination_unreachable_msg(udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
+	socket = socket_port_find(&udp_globals.sockets,
+	ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
+	if (!socket) {
+		if (tl_prepare_icmp_packet(udp_globals.net_phone,
+		    udp_globals.icmp_phone, packet, error) == EOK) {
+			icmp_destination_unreachable_msg(udp_globals.icmp_phone,
+			    ICMP_PORT_UNREACH, 0, packet);
 		}
 		return EADDRNOTAVAIL;
@@ -323,59 +371,81 @@
 	fragments = 0;
 	total_length = ntohs(header->total_length);
+
 	// compute header checksum if set
-	if(header->checksum && (! error)){
-		result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
-		if(result <= 0){
+	if (header->checksum && (!error)) {
+		result = packet_get_addr(packet, (uint8_t **) &src,
+		    (uint8_t **) &dest);
+		if( result <= 0)
 			return udp_release_and_return(packet, result);
-		}
-		if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest, result, total_length, &ip_header, &length))){
+
+		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP,
+		    src, result, dest, result, total_length, &ip_header,
+		    &length))) {
 			return udp_release_and_return(packet, ERROR_CODE);
-		}else{
+		} else {
 			checksum = compute_checksum(0, ip_header, length);
-			// the udp header checksum will be added with the first fragment later
+			// the udp header checksum will be added with the first
+			// fragment later
 			free(ip_header);
 		}
-	}else{
+	} else {
 		header->checksum = 0;
 		checksum = 0;
 	}
 
-	do{
+	do {
 		++ fragments;
 		length = packet_get_data_length(next_packet);
-		if(length <= 0){
+		if (length <= 0)
 			return udp_release_and_return(packet, NO_DATA);
-		}
-		if(total_length < length){
-			if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - total_length))){
-				return udp_release_and_return(packet, ERROR_CODE);
+
+		if (total_length < length) {
+			if (ERROR_OCCURRED(packet_trim(next_packet, 0,
+			    length - total_length))) {
+				return udp_release_and_return(packet,
+				    ERROR_CODE);
 			}
+
 			// add partial checksum if set
-			if(header->checksum){
-				checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
+			if (header->checksum) {
+				checksum = compute_checksum(checksum,
+				    packet_get_data(packet),
+				    packet_get_data_length(packet));
 			}
+
 			// relese the rest of the packet fragments
 			tmp_packet = pq_next(next_packet);
-			while(tmp_packet){
+			while (tmp_packet) {
 				next_packet = pq_detach(tmp_packet);
-				pq_release_remote(udp_globals.net_phone, packet_get_id(tmp_packet));
+				pq_release_remote(udp_globals.net_phone,
+				    packet_get_id(tmp_packet));
 				tmp_packet = next_packet;
 			}
+
 			// exit the loop
 			break;
 		}
 		total_length -= length;
+
 		// add partial checksum if set
-		if(header->checksum){
-			checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
-		}
-	}while((next_packet = pq_next(next_packet)) && (total_length > 0));
+		if (header->checksum) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(packet),
+			    packet_get_data_length(packet));
+		}
+
+	} while ((next_packet = pq_next(next_packet)) && (total_length > 0));
 
 	// check checksum
-	if(header->checksum){
-		if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
-			if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
+	if (header->checksum) {
+		if (flip_checksum(compact_checksum(checksum)) !=
+		    IP_CHECKSUM_ZERO) {
+			if (tl_prepare_icmp_packet(udp_globals.net_phone,
+			    udp_globals.icmp_phone, packet, error) == EOK) {
 				// checksum error ICMP
-				icmp_parameter_problem_msg(udp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
+				icmp_parameter_problem_msg(
+				    udp_globals.icmp_phone, ICMP_PARAM_POINTER,
+				    ((size_t) ((void *) &header->checksum)) -
+				    ((size_t) ((void *) header)), packet);
 			}
 			return EINVAL;
@@ -384,6 +454,8 @@
 
 	// queue the received packet
-	if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
-	    || ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension))){
+	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
+	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
+	    ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone,
+	    &udp_globals.dimensions, device_id, &packet_dimension))) {
 		return udp_release_and_return(packet, ERROR_CODE);
 	}
@@ -391,9 +463,15 @@
 	// notify the destination socket
 	fibril_rwlock_write_unlock(&udp_globals.lock);
-	async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0, (ipcarg_t) fragments);
+	async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
+	    (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0,
+	    (ipcarg_t) fragments);
+
 	return EOK;
 }
 
-int udp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
+int
+udp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
+    ipc_call_t * answer, int * answer_count)
+{
 	ERROR_DECLARE;
 
@@ -401,22 +479,28 @@
 
 	*answer_count = 0;
-	switch(IPC_GET_METHOD(*call)){
-		case NET_TL_RECEIVED:
-			if(! ERROR_OCCURRED(packet_translate_remote(udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
-				ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_UDP, IPC_GET_ERROR(call));
-			}
-			return ERROR_CODE;
-		case IPC_M_CONNECT_TO_ME:
-			return udp_process_client_messages(callid, * call);
-	}
+
+	switch (IPC_GET_METHOD(*call)) {
+	case NET_TL_RECEIVED:
+		if (!ERROR_OCCURRED(packet_translate_remote(
+		    udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))) {
+			ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call),
+			    packet, SERVICE_UDP, IPC_GET_ERROR(call));
+		}
+		return ERROR_CODE;
+	
+	case IPC_M_CONNECT_TO_ME:
+		return udp_process_client_messages(callid, * call);
+	}
+
 	return ENOTSUP;
 }
 
-int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
+int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
+{
 	int res;
 	bool keep_on_going = true;
 	socket_cores_t local_sockets;
 	int app_phone = IPC_GET_PHONE(&call);
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	int socket_id;
 	size_t addrlen;
@@ -433,9 +517,10 @@
 	answer_count = 0;
 
-	// The client connection is only in one fibril and therefore no additional locks are needed.
+	// The client connection is only in one fibril and therefore no
+	// additional locks are needed.
 
 	socket_cores_initialize(&local_sockets);
 
-	while(keep_on_going){
+	while (keep_on_going) {
 
 		// answer the call
@@ -449,67 +534,94 @@
 
 		// process the call
-		switch(IPC_GET_METHOD(call)){
-			case IPC_M_PHONE_HUNGUP:
-				keep_on_going = false;
-				res = EHANGUP;
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			keep_on_going = false;
+			res = EHANGUP;
+			break;
+
+		case NET_SOCKET:
+			socket_id = SOCKET_GET_SOCKET_ID(call);
+			res = socket_create(&local_sockets, app_phone, NULL,
+			    &socket_id);
+			SOCKET_SET_SOCKET_ID(answer, socket_id);
+
+			if (res != EOK)
 				break;
-			case NET_SOCKET:
-				socket_id = SOCKET_GET_SOCKET_ID(call);
-				res = socket_create(&local_sockets, app_phone, NULL, &socket_id);
-				SOCKET_SET_SOCKET_ID(answer, socket_id);
-
-				if(res == EOK){
-					if (tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
-						SOCKET_SET_DATA_FRAGMENT_SIZE(answer, packet_dimension->content);
-					}
-//					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_UDP_FRAGMENT_SIZE);
-					SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
-					answer_count = 3;
-				}
+			
+			if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
+			    &udp_globals.dimensions, DEVICE_INVALID_ID,
+			    &packet_dimension) == EOK) {
+				SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+				    packet_dimension->content);
+			}
+
+//			SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+//			    MAX_UDP_FRAGMENT_SIZE);
+			SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
+			answer_count = 3;
+			break;
+
+		case NET_SOCKET_BIND:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_BIND:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&udp_globals.lock);
-					res = socket_bind(&local_sockets, &udp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port);
-					fibril_rwlock_write_unlock(&udp_globals.lock);
-					free(addr);
-				}
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = socket_bind(&local_sockets, &udp_globals.sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
+			    udp_globals.last_used_port);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+			free(addr);
+			break;
+
+		case NET_SOCKET_SENDTO:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_SENDTO:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&udp_globals.lock);
-					res = udp_sendto_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&udp_globals.lock);
-					}else{
-						answer_count = 2;
-					}
-					free(addr);
-				}
+
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = udp_sendto_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+
+			if (res != EOK)
+				fibril_rwlock_write_unlock(&udp_globals.lock);
+			else
+				answer_count = 2;
+			
+			free(addr);
+			break;
+
+		case NET_SOCKET_RECVFROM:
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = udp_recvfrom_message(&local_sockets,
+			     SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			     &addrlen);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+
+			if (res <= 0)
 				break;
-			case NET_SOCKET_RECVFROM:
-				fibril_rwlock_write_lock(&udp_globals.lock);
-				res = udp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
-				fibril_rwlock_write_unlock(&udp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_CLOSE:
-				fibril_rwlock_write_lock(&udp_globals.lock);
-				res = socket_destroy(udp_globals.net_phone, SOCKET_GET_SOCKET_ID(call), &local_sockets, &udp_globals.sockets, NULL);
-				fibril_rwlock_write_unlock(&udp_globals.lock);
-				break;
-			case NET_SOCKET_GETSOCKOPT:
-			case NET_SOCKET_SETSOCKOPT:
-			default:
-				res = ENOTSUP;
-				break;
+
+			SOCKET_SET_READ_DATA_LENGTH(answer, res);
+			SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+			answer_count = 3;
+			res = EOK;
+			break;
+			
+		case NET_SOCKET_CLOSE:
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = socket_destroy(udp_globals.net_phone,
+			    SOCKET_GET_SOCKET_ID(call), &local_sockets,
+			    &udp_globals.sockets, NULL);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+			break;
+
+		case NET_SOCKET_GETSOCKOPT:
+		case NET_SOCKET_SETSOCKOPT:
+		default:
+			res = ENOTSUP;
+			break;
 		}
 	}
@@ -519,10 +631,15 @@
 
 	// release all local sockets
-	socket_cores_release(udp_globals.net_phone, &local_sockets, &udp_globals.sockets, NULL);
+	socket_cores_release(udp_globals.net_phone, &local_sockets,
+	    &udp_globals.sockets, NULL);
 
 	return res;
 }
 
-int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags){
+int
+udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
+    const struct sockaddr *addr, socklen_t addrlen, int fragments,
+    size_t *data_fragment_size, int flags)
+{
 	ERROR_DECLARE;
 
@@ -544,22 +661,27 @@
 
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
-
-	if((socket->port <= 0) && udp_globals.autobinding){
+
+	if ((socket->port <= 0) && udp_globals.autobinding) {
 		// bind the socket to a random free port if not bound
-//		do{
+//		do {
 			// try to find a free port
 //			fibril_rwlock_read_unlock(&udp_globals.lock);
 //			fibril_rwlock_write_lock(&udp_globals.lock);
 			// might be changed in the meantime
-//			if(socket->port <= 0){
-				if(ERROR_OCCURRED(socket_bind_free_port(&udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port))){
-//					fibril_rwlock_write_unlock(&udp_globals.lock);
-//					fibril_rwlock_read_lock(&udp_globals.lock);
+//			if (socket->port <= 0) {
+				if (ERROR_OCCURRED(socket_bind_free_port(
+				    &udp_globals.sockets, socket,
+				    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
+				    udp_globals.last_used_port))) {
+//					fibril_rwlock_write_unlock(
+//					    &udp_globals.lock);
+//					fibril_rwlock_read_lock(
+//					    &udp_globals.lock);
 					return ERROR_CODE;
 				}
-				// set the next port as the search starting port number
+				// set the next port as the search starting port
+				// number
 				udp_globals.last_used_port = socket->port;
 //			}
@@ -567,51 +689,61 @@
 //			fibril_rwlock_read_lock(&udp_globals.lock);
 			// might be changed in the meantime
-//		}while(socket->port <= 0);
-	}
-
-	if(udp_globals.checksum_computing){
-		if(ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, &headerlen))){
+//		} while (socket->port <= 0);
+	}
+
+	if (udp_globals.checksum_computing) {
+		if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone,
+		    IPPROTO_UDP, addr, addrlen, &device_id, &ip_header,
+		    &headerlen))) {
 			return udp_release_and_return(packet, ERROR_CODE);
 		}
 		// get the device packet dimension
-//		ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension));
-	}
-//	}else{
+//		ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone,
+//		    &udp_globals.dimensions, device_id, &packet_dimension));
+	}
+//	} else {
 		// do not ask all the time
-		ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
+		ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
+		    &udp_globals.packet_dimension));
 		packet_dimension = &udp_globals.packet_dimension;
 //	}
 
 	// read the first packet fragment
-	result = tl_socket_read_packet_data(udp_globals.net_phone, &packet, UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
-	if(result < 0){
+	result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
+	    UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
+	if (result < 0)
 		return result;
-	}
+
 	total_length = (size_t) result;
-	if(udp_globals.checksum_computing){
-		checksum = compute_checksum(0, packet_get_data(packet), packet_get_data_length(packet));
-	}else{
+	if (udp_globals.checksum_computing)
+		checksum = compute_checksum(0, packet_get_data(packet),
+		    packet_get_data_length(packet));
+	else
 		checksum = 0;
-	}
+
 	// prefix the udp header
 	header = PACKET_PREFIX(packet, udp_header_t);
-	if(! header){
+	if(! header)
 		return udp_release_and_return(packet, ENOMEM);
-	}
+
 	bzero(header, sizeof(*header));
 	// read the rest of the packet fragments
-	for(index = 1; index < fragments; ++ index){
-		result = tl_socket_read_packet_data(udp_globals.net_phone, &next_packet, 0, packet_dimension, addr, addrlen);
-		if(result < 0){
+	for (index = 1; index < fragments; ++ index) {
+		result = tl_socket_read_packet_data(udp_globals.net_phone,
+		    &next_packet, 0, packet_dimension, addr, addrlen);
+		if (result < 0)
 			return udp_release_and_return(packet, result);
-		}
-		if(ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))){
+
+		if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0)))
 			return udp_release_and_return(packet, ERROR_CODE);
-		}
+
 		total_length += (size_t) result;
-		if(udp_globals.checksum_computing){
-			checksum = compute_checksum(checksum, packet_get_data(next_packet), packet_get_data_length(next_packet));
-		}
-	}
+		if (udp_globals.checksum_computing) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(next_packet),
+			    packet_get_data_length(next_packet));
+		}
+	}
+
 	// set the udp header
 	header->source_port = htons((socket->port > 0) ? socket->port : 0);
@@ -619,29 +751,40 @@
 	header->total_length = htons(total_length + sizeof(*header));
 	header->checksum = 0;
-	if(udp_globals.checksum_computing){
+	if (udp_globals.checksum_computing) {
 		// update the pseudo header
-		if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(ip_header, headerlen, total_length + UDP_HEADER_SIZE))){
+		if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+		    ip_header, headerlen, total_length + UDP_HEADER_SIZE))) {
 			free(ip_header);
 			return udp_release_and_return(packet, ERROR_CODE);
 		}
+
 		// finish the checksum computation
 		checksum = compute_checksum(checksum, ip_header, headerlen);
-		checksum = compute_checksum(checksum, (uint8_t *) header, sizeof(*header));
-		header->checksum = htons(flip_checksum(compact_checksum(checksum)));
+		checksum = compute_checksum(checksum, (uint8_t *) header,
+		    sizeof(*header));
+		header->checksum =
+		    htons(flip_checksum(compact_checksum(checksum)));
 		free(ip_header);
-	}else{
+	} else {
 		device_id = DEVICE_INVALID_ID;
 	}
+
 	// prepare the first packet fragment
-	if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0))){
+	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0,
+	    0, 0))) {
 		return udp_release_and_return(packet, ERROR_CODE);
 	}
+
 	// send the packet
 	fibril_rwlock_write_unlock(&udp_globals.lock);
 	ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
+
 	return EOK;
 }
 
-int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
+int
+udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t *addrlen)
+{
 	ERROR_DECLARE;
 
@@ -650,23 +793,25 @@
 	packet_t packet;
 	udp_header_ref header;
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	size_t length;
-	uint8_t * data;
+	uint8_t *data;
 	int result;
 
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the next received packet
 	packet_id = dyn_fifo_value(&socket->received);
-	if(packet_id < 0){
+	if (packet_id < 0)
 		return NO_DATA;
-	}
-	ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, packet_id));
+
+	ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet,
+	    packet_id));
+
 	// get udp header
 	data = packet_get_data(packet);
-	if(! data){
+	if (!data) {
 		pq_release_remote(udp_globals.net_phone, packet_id);
 		return NO_DATA;
@@ -676,9 +821,11 @@
 	// set the source address port
 	result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
-	if(ERROR_OCCURRED(tl_set_address_port(addr, result, ntohs(header->source_port)))){
+	if (ERROR_OCCURRED(tl_set_address_port(addr, result,
+	    ntohs(header->source_port)))) {
 		pq_release_remote(udp_globals.net_phone, packet_id);
 		return ERROR_CODE;
 	}
 	*addrlen = (size_t) result;
+
 	// send the source address
 	ERROR_PROPAGATE(data_reply(addr, * addrlen));
@@ -693,9 +840,11 @@
 	dyn_fifo_pop(&socket->received);
 	pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
+
 	// return the total length
 	return (int) length;
 }
 
-int udp_release_and_return(packet_t packet, int result){
+int udp_release_and_return(packet_t packet, int result)
+{
 	pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
 	return result;
@@ -704,6 +853,6 @@
 /** Default thread for new connections.
  *
- *  @param[in] iid The initial message identifier.
- *  @param[in] icall The initial message call structure.
+ *  @param[in] iid	The initial message identifier.
+ *  @param[in] icall	The initial message call structure.
  *
  */
@@ -716,5 +865,5 @@
 	ipc_answer_0(iid, EOK);
 	
-	while(true) {
+	while (true) {
 		ipc_call_t answer;
 		int answer_count;
@@ -731,6 +880,9 @@
 		    &answer_count);
 		
-		/* End if said to either by the message or the processing result */
-		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
+		/*
+		 * End if said to either by the message or the processing result
+		 */
+		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
+		    (res == EHANGUP))
 			return;
 		
@@ -742,10 +894,11 @@
 /** Starts the module.
  *
- *  @param argc The count of the command line arguments. Ignored parameter.
- *  @param argv The command line parameters. Ignored parameter.
- *
- *  @returns EOK on success.
- *  @returns Other error codes as defined for each specific module start function.
- *
+ *  @param argc		The count of the command line arguments. Ignored
+ *			parameter.
+ *  @param argv		The command line parameters. Ignored parameter.
+ *
+ *  @returns		EOK on success.
+ *  @returns		Other error codes as defined for each specific module
+ *			start function.
  */
 int main(int argc, char *argv[])
Index: uspace/srv/net/tl/udp/udp_module.c
===================================================================
--- uspace/srv/net/tl/udp/udp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/udp/udp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/vfs/vfs.h
===================================================================
--- uspace/srv/vfs/vfs.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -169,5 +169,5 @@
 
 extern int vfs_grab_phone(fs_handle_t);
-extern void vfs_release_phone(int);
+extern void vfs_release_phone(fs_handle_t, int);
 
 extern fs_handle_t fs_name_to_handle(char *, bool);
Index: uspace/srv/vfs/vfs_lookup.c
===================================================================
--- uspace/srv/vfs/vfs_lookup.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_lookup.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -168,5 +168,5 @@
 	ipcarg_t rc;
 	async_wait_for(req, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(root->fs_handle, phone);
 	
 	fibril_mutex_lock(&plb_mutex);
@@ -215,5 +215,5 @@
 	ipcarg_t rc;
 	async_wait_for(req, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(result->triplet.fs_handle, phone);
 	
 	if (rc == EOK) {
Index: uspace/srv/vfs/vfs_node.c
===================================================================
--- uspace/srv/vfs/vfs_node.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_node.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -133,5 +133,5 @@
 		    (ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
 		assert(rc == EOK);
-		vfs_release_phone(phone);
+		vfs_release_phone(node->fs_handle, phone);
 	}
 	if (free_vfs_node)
Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -131,5 +131,5 @@
 			if (rc != EOK) {
 				async_wait_for(msg, NULL);
-				vfs_release_phone(phone);
+				vfs_release_phone(fs_handle, phone);
 				fibril_rwlock_write_unlock(&namespace_rwlock);
 				ipc_answer_0(rid, rc);
@@ -137,5 +137,5 @@
 			}
 			async_wait_for(msg, &rc);
-			vfs_release_phone(phone);
+			vfs_release_phone(fs_handle, phone);
 			
 			if (rc != EOK) {
@@ -196,6 +196,6 @@
 	if (rc != EOK) {
 		async_wait_for(msg, NULL);
-		vfs_release_phone(mountee_phone);
-		vfs_release_phone(phone);
+		vfs_release_phone(fs_handle, mountee_phone);
+		vfs_release_phone(mp_res.triplet.fs_handle, phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
@@ -206,5 +206,5 @@
 	}
 
-	vfs_release_phone(mountee_phone);
+	vfs_release_phone(fs_handle, mountee_phone);
 	
 	/* send the mount options */
@@ -212,5 +212,5 @@
 	if (rc != EOK) {
 		async_wait_for(msg, NULL);
-		vfs_release_phone(phone);
+		vfs_release_phone(mp_res.triplet.fs_handle, phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
@@ -221,5 +221,5 @@
 	}
 	async_wait_for(msg, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(mp_res.triplet.fs_handle, phone);
 	
 	if (rc == EOK) {
@@ -423,5 +423,5 @@
 		rc = async_req_1_0(phone, VFS_OUT_UNMOUNTED,
 		    mr_node->dev_handle);
-		vfs_release_phone(phone);
+		vfs_release_phone(mr_node->fs_handle, phone);
 		if (rc != EOK) {
 			fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -460,5 +460,5 @@
 		rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->dev_handle,
 		    mp_node->index);
-		vfs_release_phone(phone);
+		vfs_release_phone(mp_node->fs_handle, phone);
 		if (rc != EOK) {
 			fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -716,5 +716,5 @@
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 	fibril_mutex_unlock(&file->lock);
 	
@@ -747,5 +747,5 @@
 		async_wait_for(msg, &rc);
 		
-		vfs_release_phone(fs_phone);
+		vfs_release_phone(file->node->fs_handle, fs_phone);
 		fibril_mutex_unlock(&file->lock);
 		
@@ -846,5 +846,5 @@
 	}
 	
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 	
 	size_t bytes = IPC_GET_ARG1(answer);
@@ -970,5 +970,5 @@
 	rc = async_req_4_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t) dev_handle,
 	    (ipcarg_t) index, LOWER32(size), UPPER32(size));
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(fs_handle, fs_phone);
 	return (int)rc;
 }
@@ -1026,5 +1026,5 @@
 	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
 	async_wait_for(msg, &rc);
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 
 	fibril_mutex_unlock(&file->lock);
@@ -1077,5 +1077,5 @@
 	ipcarg_t rv;
 	async_wait_for(msg, &rv);
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(node->fs_handle, fs_phone);
 
 	ipc_answer_0(rid, rv);
Index: uspace/srv/vfs/vfs_register.c
===================================================================
--- uspace/srv/vfs/vfs_register.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_register.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -39,5 +39,7 @@
 #include <ipc/services.h>
 #include <async.h>
+#include <async_rel.h>
 #include <fibril.h>
+#include <fibril_synch.h>
 #include <errno.h>
 #include <stdio.h>
@@ -46,5 +48,4 @@
 #include <ctype.h>
 #include <bool.h>
-#include <fibril_synch.h>
 #include <adt/list.h>
 #include <as.h>
@@ -252,4 +253,6 @@
 int vfs_grab_phone(fs_handle_t handle)
 {
+	link_t *cur;
+	fs_info_t *fs;
 	int phone;
 
@@ -262,6 +265,4 @@
 	 */
 	fibril_mutex_lock(&fs_head_lock);
-	link_t *cur;
-	fs_info_t *fs;
 	for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
 		fs = list_get_instance(cur, fs_info_t, fs_link);
@@ -269,5 +270,5 @@
 			fibril_mutex_unlock(&fs_head_lock);
 			fibril_mutex_lock(&fs->phone_lock);
-			phone = ipc_connect_me_to(fs->phone, 0, 0, 0);
+			phone = async_relation_create(fs->phone);
 			fibril_mutex_unlock(&fs->phone_lock);
 
@@ -284,8 +285,23 @@
  * @param phone		Phone to FS task.
  */
-void vfs_release_phone(int phone)
-{
-	/* TODO: implement connection caching */
-	ipc_hangup(phone);
+void vfs_release_phone(fs_handle_t handle, int phone)
+{
+	link_t *cur;
+	fs_info_t *fs;
+
+	fibril_mutex_lock(&fs_head_lock);
+	for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
+		fs = list_get_instance(cur, fs_info_t, fs_link);
+		if (fs->fs_handle == handle) {
+			fibril_mutex_unlock(&fs_head_lock);
+			fibril_mutex_lock(&fs->phone_lock);
+			async_relation_destroy(fs->phone, phone);
+			fibril_mutex_unlock(&fs->phone_lock);
+			return;
+		}
+	}
+	/* should not really get here */
+	abort();
+	fibril_mutex_unlock(&fs_head_lock);
 }
 
