Index: uspace/drv/usbhid/kbddev.c
===================================================================
--- uspace/drv/usbhid/kbddev.c	(revision fc5ed5dee7e65b8744a655665eee2259fa6febc4)
+++ uspace/drv/usbhid/kbddev.c	(revision fa8f1f7b9ad772eb166297777ff7ad9cd1836ebe)
@@ -94,4 +94,10 @@
 	.flags = 0
 };
+
+typedef enum usbhid_kbd_flags {
+	USBHID_KBD_STATUS_UNINITIALIZED = 0,
+	USBHID_KBD_STATUS_INITIALIZED = 1,
+	USBHID_KBD_STATUS_TO_DESTROY = -1
+} usbhid_kbd_flags;
 
 /*----------------------------------------------------------------------------*/
@@ -232,5 +238,5 @@
 	
 	assert(kbd_dev->hid_dev != NULL);
-	assert(kbd_dev->hid_dev->initialized);
+	assert(kbd_dev->hid_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
 	usbhid_req_set_report(kbd_dev->hid_dev, USB_HID_REPORT_TYPE_OUTPUT, 
 	    buffer, BOOTP_BUFFER_OUT_SIZE);
@@ -565,5 +571,5 @@
                                     uint8_t *buffer, size_t actual_size)
 {
-	assert(kbd_dev->initialized);
+	assert(kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
 	assert(kbd_dev->hid_dev->parser != NULL);
 	
@@ -619,5 +625,5 @@
 	
 	kbd_dev->console_phone = -1;
-	kbd_dev->initialized = 0;
+	kbd_dev->initialized = USBHID_KBD_STATUS_UNINITIALIZED;
 	
 	return kbd_dev;
@@ -625,31 +631,8 @@
 
 /*----------------------------------------------------------------------------*/
-/**
- * Properly destroys the USB/HID keyboard structure.
- *
- * @param kbd_dev Pointer to the structure to be destroyed.
- */
-static void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
-{
-	if (kbd_dev == NULL || *kbd_dev == NULL) {
-		return;
-	}
-	
-	// hangup phone to the console
-	async_hangup((*kbd_dev)->console_phone);
-	
-	if ((*kbd_dev)->hid_dev != NULL) {
-		usbhid_dev_free(&(*kbd_dev)->hid_dev);
-		assert((*kbd_dev)->hid_dev == NULL);
-	}
-	
-	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;
+
+static void usbhid_kbd_mark_unusable(usbhid_kbd_t *kbd_dev)
+{
+	kbd_dev->initialized = USBHID_KBD_STATUS_TO_DESTROY;
 }
 
@@ -693,5 +676,5 @@
 	}
 	
-	if (kbd_dev->initialized) {
+	if (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED) {
 		usb_log_warning("Keyboard structure already initialized.\n");
 		return EINVAL;
@@ -706,5 +689,5 @@
 	}
 	
-	assert(kbd_dev->hid_dev->initialized);
+	assert(kbd_dev->hid_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
 	
 	// save the size of the report (boot protocol report by default)
@@ -758,5 +741,5 @@
 	usbhid_req_set_idle(kbd_dev->hid_dev, IDLE_RATE);
 	
-	kbd_dev->initialized = 1;
+	kbd_dev->initialized = USBHID_KBD_STATUS_INITIALIZED;
 	usb_log_info("HID/KBD device structure initialized.\n");
 	
@@ -872,7 +855,10 @@
 	usbhid_kbd_poll(kbd_dev);
 	
+	// as there is another fibril using this device, so we must leave the
+	// structure to it, but mark it for destroying.
+	usbhid_kbd_mark_unusable(kbd_dev);
 	// at the end, properly destroy the KBD structure
-	usbhid_kbd_free(&kbd_dev);
-	assert(kbd_dev == NULL);
+//	usbhid_kbd_free(&kbd_dev);
+//	assert(kbd_dev == NULL);
 
 	return EOK;
@@ -996,4 +982,41 @@
 }
 
+/*----------------------------------------------------------------------------*/
+
+int usbhid_kbd_is_usable(const usbhid_kbd_t *kbd_dev)
+{
+	return (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
+}
+
+/*----------------------------------------------------------------------------*/
+/**
+ * Properly destroys the USB/HID keyboard structure.
+ *
+ * @param kbd_dev Pointer to the structure to be destroyed.
+ */
+void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
+{
+	if (kbd_dev == NULL || *kbd_dev == NULL) {
+		return;
+	}
+	
+	// hangup phone to the console
+	async_hangup((*kbd_dev)->console_phone);
+	
+	if ((*kbd_dev)->hid_dev != NULL) {
+		usbhid_dev_free(&(*kbd_dev)->hid_dev);
+		assert((*kbd_dev)->hid_dev == NULL);
+	}
+	
+	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;
+}
+
 /**
  * @}
