Index: uspace/drv/usbhid/Makefile
===================================================================
--- uspace/drv/usbhid/Makefile	(revision 269bd9b49548cffddc199098ad8295d789108f4c)
+++ uspace/drv/usbhid/Makefile	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
@@ -42,4 +42,5 @@
 	hidreq.c \
 	kbddev.c \
+	kbdrepeat.c \
 	hiddev.c \
 	$(STOLEN_LAYOUT_SOURCES)
Index: uspace/drv/usbhid/kbddev.c
===================================================================
--- uspace/drv/usbhid/kbddev.c	(revision 269bd9b49548cffddc199098ad8295d789108f4c)
+++ uspace/drv/usbhid/kbddev.c	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
@@ -37,5 +37,4 @@
 #include <errno.h>
 #include <str_error.h>
-#include <fibril.h>
 #include <stdio.h>
 
@@ -43,4 +42,6 @@
 #include <ipc/kbd.h>
 #include <async.h>
+#include <fibril.h>
+#include <fibril_synch.h>
 
 #include <usb/usb.h>
@@ -56,4 +57,5 @@
 #include "layout.h"
 #include "conv.h"
+#include "kbdrepeat.h"
 
 /*----------------------------------------------------------------------------*/
@@ -65,4 +67,6 @@
 static const uint8_t BOOTP_ERROR_ROLLOVER = 1;
 static const uint8_t IDLE_RATE = 0;
+static const unsigned int DEFAULT_DELAY_BEFORE_FIRST_REPEAT = 500 * 1000;
+static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
 
 /** Keyboard polling endpoint description for boot protocol class. */
@@ -190,6 +194,5 @@
 /*----------------------------------------------------------------------------*/
 
-static void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type, 
-    unsigned int key)
+void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type, unsigned int key)
 {
 	console_event_t ev;
@@ -233,4 +236,6 @@
 			 * up the lock state.
 			 */
+			unsigned int locks_old = kbd_dev->lock_keys;
+			
 			kbd_dev->mods = 
 			    kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
@@ -238,5 +243,7 @@
 
 			/* Update keyboard lock indicator lights. */
- 			usbhid_kbd_set_led(kbd_dev);
+			if (kbd_dev->lock_keys != locks_old) {
+				usbhid_kbd_set_led(kbd_dev);
+			}
 		} else {
 			kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
@@ -359,4 +366,5 @@
 			// not found, i.e. the key was released
 			key = usbhid_parse_scancode(kbd_dev->keys[j]);
+			usbhid_kbd_repeat_stop(kbd_dev, key);
 			usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
 			usb_log_debug2("Key released: %d\n", key);
@@ -383,4 +391,5 @@
 			    key_codes[i]);
 			usbhid_kbd_push_ev(kbd_dev, KEY_PRESS, key);
+			usbhid_kbd_repeat_start(kbd_dev, key);
 		} else {
 			// found, nothing happens
@@ -527,4 +536,10 @@
 	}
 	
+	if ((*kbd_dev)->repeat_mtx != NULL) {
+		/* TODO: replace by some check and wait */
+		assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
+		free((*kbd_dev)->repeat_mtx);
+	}
+	
 	free(*kbd_dev);
 	*kbd_dev = NULL;
@@ -598,4 +613,19 @@
 	kbd_dev->mods = DEFAULT_ACTIVE_MODS;
 	kbd_dev->lock_keys = 0;
+	
+	kbd_dev->repeat.key_new = 0;
+	kbd_dev->repeat.key_repeated = 0;
+	kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
+	kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
+	
+	kbd_dev->repeat_mtx = (fibril_mutex_t *)(
+	    malloc(sizeof(fibril_mutex_t)));
+	if (kbd_dev->repeat_mtx == NULL) {
+		usb_log_fatal("No memory!\n");
+		free(kbd_dev->keys);
+		return ENOMEM;
+	}
+	
+	fibril_mutex_initialize(kbd_dev->repeat_mtx);
 	
 	/*
@@ -833,4 +863,14 @@
 	}
 	fibril_add_ready(fid);
+	
+	/*
+	 * Create new fibril for auto-repeat
+	 */
+	fid = fibril_create(usbhid_kbd_repeat_fibril, kbd_dev);
+	if (fid == 0) {
+		usb_log_error("Failed to start fibril for KBD auto-repeat");
+		return ENOMEM;
+	}
+	fibril_add_ready(fid);
 
 	(void)keyboard_ops;
Index: uspace/drv/usbhid/kbddev.h
===================================================================
--- uspace/drv/usbhid/kbddev.h	(revision 269bd9b49548cffddc199098ad8295d789108f4c)
+++ uspace/drv/usbhid/kbddev.h	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
@@ -39,4 +39,6 @@
 #include <stdint.h>
 
+#include <fibril_synch.h>
+
 #include <usb/classes/hid.h>
 #include <ddf/driver.h>
@@ -46,4 +48,18 @@
 
 /*----------------------------------------------------------------------------*/
+/**
+ * Structure for keeping information needed for auto-repeat of keys.
+ */
+typedef struct {
+	/** Last pressed key. */
+	unsigned int key_new;
+	/** Key to be repeated. */
+	unsigned int key_repeated;
+	/** Delay before first repeat in microseconds. */
+	unsigned int delay_before;
+	/** Delay between repeats in microseconds. */
+	unsigned int delay_between;
+} usbhid_kbd_repeat_t;
+
 /**
  * USB/HID keyboard device type.
@@ -78,4 +94,10 @@
 	int console_phone;
 	
+	/** Information for auto-repeat of keys. */
+	usbhid_kbd_repeat_t repeat;
+	
+	/** Mutex for accessing the information about auto-repeat. */
+	fibril_mutex_t *repeat_mtx;
+	
 	/** State of the structure (for checking before use). */
 	int initialized;
@@ -86,4 +108,6 @@
 int usbhid_kbd_try_add_device(ddf_dev_t *dev);
 
+void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type, unsigned int key);
+
 #endif /* USBHID_KBDDEV_H_ */
 
Index: uspace/drv/usbhid/kbdrepeat.c
===================================================================
--- uspace/drv/usbhid/kbdrepeat.c	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
+++ uspace/drv/usbhid/kbdrepeat.c	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2011 Lubos Slovak
+ * 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 drvusbhid
+ * @{
+ */
+/**
+ * @file
+ * USB HID keyboard autorepeat facilities
+ */
+
+#include <fibril_synch.h>
+#include <io/keycode.h>
+#include <io/console.h>
+#include <errno.h>
+
+#include <usb/debug.h>
+
+#include "kbdrepeat.h"
+#include "kbddev.h"
+
+static unsigned int CHECK_DELAY = 1000;
+
+/*----------------------------------------------------------------------------*/
+
+static void usbhid_kbd_repeat_loop(usbhid_kbd_t *kbd)
+{
+	unsigned int delay = 0;
+	
+	usb_log_debug("Starting autorepeat loop.\n");
+
+	while (true) {
+		fibril_mutex_lock(kbd->repeat_mtx);
+
+		if (kbd->repeat.key_new > 0) {
+			if (kbd->repeat.key_new == kbd->repeat.key_repeated) {
+				usb_log_debug2("Repeating key: %u.\n", 
+				    kbd->repeat.key_repeated);
+				usbhid_kbd_push_ev(kbd, KEY_PRESS, 
+				    kbd->repeat.key_repeated);
+				delay = kbd->repeat.delay_between;
+			} else {
+				usb_log_debug("New key to repeat: %u.\n", 
+				    kbd->repeat.key_new);
+				kbd->repeat.key_repeated = kbd->repeat.key_new;
+				delay = kbd->repeat.delay_before;
+			}
+		} else {
+			if (kbd->repeat.key_repeated > 0) {
+				usb_log_debug("Stopping to repeat key: %u.\n", 
+				    kbd->repeat.key_repeated);
+				kbd->repeat.key_repeated = 0;
+			}
+			delay = CHECK_DELAY;
+		}
+		fibril_mutex_unlock(kbd->repeat_mtx);
+		
+		async_usleep(delay);
+	}
+}
+
+/*----------------------------------------------------------------------------*/
+
+int usbhid_kbd_repeat_fibril(void *arg)
+{
+	usb_log_debug("Autorepeat fibril spawned.\n");
+	
+	if (arg == NULL) {
+		usb_log_error("No device!\n");
+		return EINVAL;
+	}
+	
+	usbhid_kbd_t *kbd = (usbhid_kbd_t *)arg;
+	
+	usbhid_kbd_repeat_loop(kbd);
+	
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+void usbhid_kbd_repeat_start(usbhid_kbd_t *kbd, unsigned int key)
+{
+	fibril_mutex_lock(kbd->repeat_mtx);
+	kbd->repeat.key_new = key;
+	fibril_mutex_unlock(kbd->repeat_mtx);
+}
+
+/*----------------------------------------------------------------------------*/
+
+void usbhid_kbd_repeat_stop(usbhid_kbd_t *kbd, unsigned int key)
+{
+	fibril_mutex_lock(kbd->repeat_mtx);
+	if (key == kbd->repeat.key_new) {
+		kbd->repeat.key_new = 0;
+	}
+	fibril_mutex_unlock(kbd->repeat_mtx);
+}
+
+/**
+ * @}
+ */
Index: uspace/drv/usbhid/kbdrepeat.h
===================================================================
--- uspace/drv/usbhid/kbdrepeat.h	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
+++ uspace/drv/usbhid/kbdrepeat.h	(revision dfe53af21d5a566e3fe8b01a6aa5010394ed77d2)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2011 Lubos Slovak
+ * 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 drvusbhid
+ * @{
+ */
+/** @file
+ * USB HID keyboard autorepeat facilities
+ */
+
+#ifndef USBHID_KBDREPEAT_H_
+#define USBHID_KBDREPEAT_H_
+
+#include "kbddev.h"
+
+/*----------------------------------------------------------------------------*/
+
+int usbhid_kbd_repeat_fibril(void *arg);
+
+void usbhid_kbd_repeat_start(usbhid_kbd_t *kbd, unsigned int key);
+
+void usbhid_kbd_repeat_stop(usbhid_kbd_t *kbd, unsigned int key);
+
+#endif /* USBHID_KBDREPEAT_H_ */
+
+/**
+ * @}
+ */
