Index: uspace/drv/bus/usb/usbhid/mouse/mousedev.c
===================================================================
--- uspace/drv/bus/usb/usbhid/mouse/mousedev.c	(revision 4578a6eddb5caa32837573ce3c101eac5cdfa8ec)
+++ uspace/drv/bus/usb/usbhid/mouse/mousedev.c	(revision 0f12c171638dd9b3b72f9b28abe730158a86c78d)
@@ -296,7 +296,16 @@
 	return true;
 }
-
-/*----------------------------------------------------------------------------*/
-
+/*----------------------------------------------------------------------------*/
+#define FUN_UNBIND_DESTROY(fun) \
+if (fun) { \
+	if (ddf_fun_unbind((fun)) == EOK) { \
+		(fun)->driver_data = NULL; \
+		ddf_fun_destroy((fun)); \
+	} else { \
+		usb_log_error("Could not unbind function `%s', it " \
+		    "will not be destroyed.\n", (fun)->name); \
+	} \
+} else (void)0
+/*----------------------------------------------------------------------------*/
 static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
 {
@@ -306,8 +315,9 @@
 	/* Create the exposed function. */
 	usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
-	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 
+	ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
 	    HID_MOUSE_FUN_NAME);
 	if (fun == NULL) {
-		usb_log_error("Could not create DDF function node.\n");
+		usb_log_error("Could not create DDF function node `%s'.\n",
+		    HID_MOUSE_FUN_NAME);
 		return ENOMEM;
 	}
@@ -318,6 +328,6 @@
 	int rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		usb_log_error("Could not bind DDF function: %s.\n",
-		    str_error(rc));
+		usb_log_error("Could not bind DDF function `%s': %s.\n",
+		    fun->name, str_error(rc));
 		fun->driver_data = NULL;
 		ddf_fun_destroy(fun);
@@ -325,6 +335,6 @@
 	}
 
-	usb_log_debug("Adding DDF function to category %s...\n", 
-	    HID_MOUSE_CATEGORY);
+	usb_log_debug("Adding DDF function `%s' to category %s...\n",
+	    fun->name, HID_MOUSE_CATEGORY);
 	rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
 	if (rc != EOK) {
@@ -332,7 +342,5 @@
 		    "Could not add DDF function to category %s: %s.\n",
 		    HID_MOUSE_CATEGORY, str_error(rc));
-		ddf_fun_unbind(fun);
-		fun->driver_data = NULL;
-		ddf_fun_destroy(fun);
+		FUN_UNBIND_DESTROY(fun);
 		return rc;
 	}
@@ -347,8 +355,7 @@
 	    HID_MOUSE_WHEEL_FUN_NAME);
 	if (fun == NULL) {
-		usb_log_error("Could not create DDF function node.\n");
-		ddf_fun_unbind(mouse->mouse_fun);
-		mouse->mouse_fun->driver_data = NULL;
-		ddf_fun_destroy(mouse->mouse_fun);
+		usb_log_error("Could not create DDF function node `%s'.\n",
+		    HID_MOUSE_WHEEL_FUN_NAME);
+		FUN_UNBIND_DESTROY(mouse->mouse_fun);
 		mouse->mouse_fun = NULL;
 		return ENOMEM;
@@ -364,10 +371,9 @@
 	rc = ddf_fun_bind(fun);
 	if (rc != EOK) {
-		usb_log_error("Could not bind DDF function: %s.\n",
-		    str_error(rc));
-		ddf_fun_unbind(mouse->mouse_fun);
-		mouse->mouse_fun->driver_data = NULL;
-		ddf_fun_destroy(mouse->mouse_fun);
+		usb_log_error("Could not bind DDF function `%s': %s.\n",
+		    fun->name, str_error(rc));
+		FUN_UNBIND_DESTROY(mouse->mouse_fun);
 		mouse->mouse_fun = NULL;
+
 		fun->driver_data = NULL;
 		ddf_fun_destroy(fun);
@@ -382,11 +388,8 @@
 		    "Could not add DDF function to category %s: %s.\n",
 		    HID_MOUSE_WHEEL_CATEGORY, str_error(rc));
-		ddf_fun_unbind(mouse->mouse_fun);
-		mouse->mouse_fun->driver_data = NULL;
-		ddf_fun_destroy(mouse->mouse_fun);
+
+		FUN_UNBIND_DESTROY(mouse->mouse_fun);
 		mouse->mouse_fun = NULL;
-		ddf_fun_unbind(fun);
-		fun->driver_data = NULL;
-		ddf_fun_destroy(fun);
+		FUN_UNBIND_DESTROY(fun);
 		return rc;
 	}
@@ -437,7 +440,5 @@
 	return highest_button;
 }
-
-/*----------------------------------------------------------------------------*/
-
+/*----------------------------------------------------------------------------*/
 int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
 {
@@ -492,7 +493,5 @@
 	return EOK;
 }
-
-/*----------------------------------------------------------------------------*/
-
+/*----------------------------------------------------------------------------*/
 bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
 {
@@ -507,7 +506,5 @@
 	return usb_mouse_process_report(hid_dev, mouse_dev);
 }
-
-/*----------------------------------------------------------------------------*/
-
+/*----------------------------------------------------------------------------*/
 void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
 {
@@ -532,33 +529,11 @@
 	}
 
-	/* We might be called before being completely initialized */
-	if (mouse_dev->mouse_fun) {
-		const int ret = ddf_fun_unbind(mouse_dev->mouse_fun);
-		if (ret != EOK) {
-			usb_log_error("Failed to unbind mouse function.\n");
-		} else {
-			/* driver_data(mouse_dev) will be freed explicitly */
-			mouse_dev->mouse_fun->driver_data = NULL;
-			ddf_fun_destroy(mouse_dev->mouse_fun);
-		}
-	}
-
-	/* We might be called before being completely initialized */
-	if (mouse_dev->mouse_fun) {
-		const int ret = ddf_fun_unbind(mouse_dev->wheel_fun);
-		if (ret != EOK) {
-			usb_log_error("Failed to unbind wheel function.\n");
-		} else {
-			/* driver_data(mouse_dev) will be freed explicitly */
-			mouse_dev->wheel_fun->driver_data = NULL;
-			ddf_fun_destroy(mouse_dev->wheel_fun);
-		}
-	}
+	FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
+	FUN_UNBIND_DESTROY(mouse_dev->wheel_fun);
+
 	free(mouse_dev->buttons);
 	free(mouse_dev);
 }
-
-/*----------------------------------------------------------------------------*/
-
+/*----------------------------------------------------------------------------*/
 int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
 {
