Index: uspace/drv/bus/usb/usbhid/kbd/kbddev.c
===================================================================
--- uspace/drv/bus/usb/usbhid/kbd/kbddev.c	(revision cf99c4c5945fbcdec33cfe5f6e1cfa597aa033f9)
+++ uspace/drv/bus/usb/usbhid/kbd/kbddev.c	(revision 50f209541655521bca471f620228c78687b7acde)
@@ -226,5 +226,5 @@
  * @param kbd_dev Keyboard device structure.
  */
-static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev) 
+static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
 {
 	if (kbd_dev->output_size == 0) {
@@ -274,10 +274,10 @@
 	}
 
-	usb_log_debug("Output report buffer: %s\n", 
-	    usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size, 
+	usb_log_debug("Output report buffer: %s\n",
+	    usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
 	        0));
 
-	usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe, 
-	    hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT, 
+	usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
+	    hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
 	    kbd_dev->output_buffer, kbd_dev->output_size);
 }
@@ -291,5 +291,5 @@
  * @param key Key code
  */
-void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type, 
+void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
     unsigned int key)
 {
@@ -308,5 +308,5 @@
 /*----------------------------------------------------------------------------*/
 
-static inline int usb_kbd_is_lock(unsigned int key_code) 
+static inline int usb_kbd_is_lock(unsigned int key_code)
 {
 	return (key_code == KC_NUM_LOCK
@@ -342,5 +342,5 @@
  * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
  */
-static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev, 
+static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
     usb_kbd_t *kbd_dev)
 {
@@ -405,4 +405,5 @@
 	memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
 
+	// TODO Get rid of this
 	char key_buffer[512];
 	ddf_dump_buffer(key_buffer, 512,
@@ -438,5 +439,5 @@
 	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
 
-	usb_hid_report_path_set_report_id (path, hid_dev->report_id);
+	usb_hid_report_path_set_report_id(path, hid_dev->report_id);
 
 	// fill in the currently pressed keys
@@ -449,7 +450,7 @@
 
 	while (field != NULL) {
-		usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n", 
+		usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
 		    field, field->value, field->usage);
-		
+
 		assert(i < kbd_dev->key_count);
 
@@ -482,31 +483,4 @@
 {
 	kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
-}
-
-/*----------------------------------------------------------------------------*/
-
-/**
- * Creates a new USB/HID keyboard structure.
- *
- * The structure returned by this function is not initialized. Use 
- * usb_kbd_init() to initialize it prior to polling.
- *
- * @return New uninitialized structure for representing a USB/HID keyboard or
- *         NULL if not successful (memory error).
- */
-static usb_kbd_t *usb_kbd_new(void)
-{
-	usb_kbd_t *kbd_dev = 
-	    (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
-
-	if (kbd_dev == NULL) {
-		usb_log_error("No memory!\n");
-		return NULL;
-	}
-
-	kbd_dev->console_sess = NULL;
-	kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
-
-	return kbd_dev;
 }
 
@@ -591,22 +565,36 @@
 
 	if (hid_dev == NULL) {
-		usb_log_error("Failed to init keyboard structure: no structure"
-		    " given.\n");
+		usb_log_error(
+		    "Failed to init keyboard structure: no structure given.\n");
 		return EINVAL;
 	}
 
-	usb_kbd_t *kbd_dev = usb_kbd_new();
+	usb_kbd_t *kbd_dev = malloc(sizeof(usb_kbd_t));
 	if (kbd_dev == NULL) {
-		usb_log_error("Error while creating USB/HID KBD device "
-		    "structure.\n");
-		return ENOMEM;  // TODO: some other code??
-	}
+		usb_log_error("Failed to allocate KBD device structure.\n");
+		return ENOMEM;
+	}
+	/* Default values */
+	fibril_mutex_initialize(&kbd_dev->repeat_mtx);
+	kbd_dev->console_sess = NULL;
+	kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
+	kbd_dev->ops.default_handler = default_connection_handler;
 
 	/* Store link to HID device */
 	kbd_dev->hid_dev = hid_dev;
 
-	/*
-	 * TODO: make more general
-	 */
+	/* Modifiers and locks */
+	kbd_dev->modifiers = 0;
+	kbd_dev->mods = DEFAULT_ACTIVE_MODS;
+	kbd_dev->lock_keys = 0;
+
+	/* Autorepeat */
+	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;
+
+
+	// TODO: make more general
 	usb_hid_report_path_t *path = usb_hid_report_path();
 	usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
@@ -621,16 +609,11 @@
 
 	kbd_dev->keys = calloc(kbd_dev->key_count, sizeof(int32_t));
-
 	if (kbd_dev->keys == NULL) {
-		usb_log_error("No memory!\n");
 		free(kbd_dev);
 		return ENOMEM;
 	}
 
-	kbd_dev->keys_old = 
-		(int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
-
+	kbd_dev->keys_old = calloc(kbd_dev->key_count, sizeof(int32_t));
 	if (kbd_dev->keys_old == NULL) {
-		usb_log_error("No memory!\n");
 		free(kbd_dev->keys);
 		free(kbd_dev);
@@ -638,13 +621,13 @@
 	}
 
-	/*
-	 * Output report
-	 */
+	/* Output report */
 	kbd_dev->output_size = 0;
 	kbd_dev->output_buffer = usb_hid_report_output(&hid_dev->report,
 	    &kbd_dev->output_size, 0);
 	if (kbd_dev->output_buffer == NULL) {
-		usb_log_warning("Error creating output report buffer.\n");
+		usb_log_error("Error creating output report buffer.\n");
+		free(kbd_dev->keys_old);
 		free(kbd_dev->keys);
+		free(kbd_dev);
 		return ENOMEM;
 	}
@@ -659,41 +642,17 @@
 	    &hid_dev->report, 0, USB_HID_REPORT_TYPE_OUTPUT);
 
-	usb_log_debug("Output report size (in items): %zu\n", 
+	usb_log_debug("Output report size (in items): %zu\n",
 	    kbd_dev->led_output_size);
 
-	kbd_dev->led_data = (int32_t *)calloc(
-	    kbd_dev->led_output_size, sizeof(int32_t));
-
+	kbd_dev->led_data = calloc(kbd_dev->led_output_size, sizeof(int32_t));
 	if (kbd_dev->led_data == NULL) {
-		usb_log_warning("Error creating buffer for LED output report."
-		    "\n");
+		usb_log_error("Error creating buffer for LED output report.\n");
+		usb_hid_report_output_free(kbd_dev->output_buffer);
+		free(kbd_dev->keys_old);
 		free(kbd_dev->keys);
-		usb_hid_report_output_free(kbd_dev->output_buffer);
 		free(kbd_dev);
 		return ENOMEM;
 	}
 
-	/*
-	 * Modifiers and locks
-	 */
-	kbd_dev->modifiers = 0;
-	kbd_dev->mods = DEFAULT_ACTIVE_MODS;
-	kbd_dev->lock_keys = 0;
-
-	/*
-	 * Autorepeat
-	 */
-	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;
-
-	fibril_mutex_initialize(&kbd_dev->repeat_mtx);
-
-	// save the KBD device structure into the HID device structure
-	*data = kbd_dev;
-
-	// set handler for incoming calls
-	kbd_dev->ops.default_handler = default_connection_handler;
 
 	/*
@@ -703,5 +662,5 @@
 	usb_kbd_set_led(hid_dev, kbd_dev);
 
-	usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 
+	usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
 	    hid_dev->usb_dev->interface_no, IDLE_RATE);
 
@@ -712,4 +671,5 @@
 	if (fid == 0) {
 		usb_log_error("Failed to start fibril for KBD auto-repeat");
+		usb_kbd_destroy(kbd_dev);
 		return ENOMEM;
 	}
@@ -726,4 +686,8 @@
 	}
 
+	/* Save the KBD device structure into the HID device structure. */
+	*data = kbd_dev;
+
+
 	return EOK;
 }
@@ -733,12 +697,10 @@
 bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
 {
-	if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
-		// do not continue polling (???)
+	if (hid_dev == NULL || data == NULL) {
+		/* This means something serious */
 		return false;
 	}
 
-	usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
-	assert(kbd_dev != NULL);
-
+	usb_kbd_t *kbd_dev = data;
 	// TODO: add return value from this function
 	usb_kbd_process_data(hid_dev, kbd_dev);
@@ -773,6 +735,7 @@
 	}
 
-	// hangup session to the console
-	async_hangup(kbd_dev->console_sess);
+	/* hangup session to the console. */
+	if (kbd_dev->console_sess)
+		async_hangup(kbd_dev->console_sess);
 
 	//assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
@@ -789,15 +752,17 @@
 		usb_hid_report_path_free(kbd_dev->led_path);
 	}
-	if (kbd_dev->output_buffer != NULL) {
-		usb_hid_report_output_free(kbd_dev->output_buffer);
-	}
-
-	if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
-		usb_log_warning("Failed to unbind kbd function.\n");
-	} else {
-		usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
-		kbd_dev->fun->driver_data = NULL;
-		ddf_fun_destroy(kbd_dev->fun);
-	}
+	usb_hid_report_output_free(kbd_dev->output_buffer);
+
+	if (kbd_dev->fun) {
+		if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
+			usb_log_warning("Failed to unbind %s.\n",
+			    kbd_dev->fun->name);
+		} else {
+			usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
+			kbd_dev->fun->driver_data = NULL;
+			ddf_fun_destroy(kbd_dev->fun);
+		}
+	}
+	free(kbd_dev);
 }
 
@@ -806,8 +771,4 @@
 void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
 {
-	if (hid_dev == NULL) {
-		return;
-	}
-
 	if (data != NULL) {
 		usb_kbd_t *kbd_dev = data;
@@ -825,4 +786,5 @@
 int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
 {
+	assert(hid_dev);
 	int rc = usb_hid_parse_report_descriptor(
 	    &hid_dev->report, USB_KBD_BOOT_REPORT_DESCRIPTOR,
