Index: uspace/drv/time/cmos-rtc/cmos-rtc.c
===================================================================
--- uspace/drv/time/cmos-rtc/cmos-rtc.c	(revision 0883de88d3c2a6ddbe6f8b984a59a88b5b70af60)
+++ uspace/drv/time/cmos-rtc/cmos-rtc.c	(revision 4b44de571c95ea7b51b1b0452ce986e85339fe74)
@@ -79,4 +79,7 @@
 rtc_pio_enable(rtc_t *rtc);
 
+static void
+rtc_dev_cleanup(rtc_t *rtc);
+
 
 static ddf_dev_ops_t rtc_dev_ops;
@@ -113,4 +116,17 @@
 }
 
+/** Clean up the RTC soft state
+ *
+ * @param rtc  The RTC device
+ */
+static void
+rtc_dev_cleanup(rtc_t *rtc)
+{
+	if (rtc->dev->parent_sess) {
+		async_hangup(rtc->dev->parent_sess);
+		rtc->dev->parent_sess = NULL;
+	}
+}
+
 /** Enable the I/O ports of the device
  *
@@ -142,5 +158,4 @@
 rtc_dev_initialize(rtc_t *rtc)
 {
-	/* XXX Do cleanup in case of failure */
 	int rc;
 	size_t i;
@@ -160,5 +175,6 @@
 		ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
 		    of device %s.", rtc->dev->name);
-		return ENOENT;
+		rc = ENOENT;
+		goto error;
 	}
 
@@ -168,5 +184,5 @@
 		ddf_msg(LVL_ERROR, "Failed to get HW resources\
 		    for device %s", rtc->dev->name);
-		return rc;
+		goto error;
 	}
 
@@ -186,5 +202,6 @@
 		ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
 		    rtc->dev->name);
-		return ENOENT;
+		rc = ENOENT;
+		goto error;
 	}
 
@@ -192,4 +209,10 @@
 
 	return EOK;
+
+error:
+	rtc_dev_cleanup(rtc);
+	hw_res_clean_resource_list(&hw_resources);
+
+	return rc;
 }
 
@@ -230,6 +253,7 @@
 {
 	rtc_t *rtc;
-	ddf_fun_t *fun;
+	ddf_fun_t *fun = NULL;
 	int rc;
+	bool need_cleanup = false;
 
 	ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
@@ -245,14 +269,18 @@
 	rc = rtc_dev_initialize(rtc);
 	if (rc != EOK)
-		return rc;
-
-	/* XXX Need cleanup */
-	if (!rtc_pio_enable(rtc))
-		return EADDRNOTAVAIL;
+		goto error;
+
+	need_cleanup = true;
+
+	if (!rtc_pio_enable(rtc)) {
+		rc = EADDRNOTAVAIL;
+		goto error;
+	}
 
 	fun = ddf_fun_create(dev, fun_exposed, "a");
 	if (!fun) {
 		ddf_msg(LVL_ERROR, "Failed creating function");
-		return ENOENT;
+		rc = ENOENT;
+		goto error;
 	}
 
@@ -261,5 +289,5 @@
 	if (rc != EOK) {
 		ddf_msg(LVL_ERROR, "Failed binding function");
-		return rc;
+		goto error;
 	}
 
@@ -271,4 +299,11 @@
 	    dev->name);
 
+	return rc;
+
+error:
+	if (fun)
+		ddf_fun_destroy(fun);
+	if (need_cleanup)
+		rtc_dev_cleanup(rtc);
 	return rc;
 }
