[PATCH v4 1/6] libusbg: Add remove binding functionality.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add function which allow to remove binding between function
and configuration. This functions also remove binding from
internal library structures wht means that after this
operation all pointers to removed binding are invalid.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 include/usbg/usbg.h |   10 ++++++++++
 src/usbg.c          |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 1407f10..7a60c0f 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -276,6 +276,16 @@ extern usbg_function *usbg_get_function(usbg_gadget *g,
  */
 extern usbg_config *usbg_get_config(usbg_gadget *g, int id, const char *label);
 
+/* USB gadget/config/function/binding removal */
+
+/**
+ * @brief Remove binding between configuration and function
+ * @details This function frees also the memory allocated for binding
+ * @param b Binding to be removed
+ * @return 0 on success, usbg_error if error occurred
+ */
+extern int usbg_rm_binding(usbg_binding *b);
+
 /* USB gadget allocation and configuration */
 
 /**
diff --git a/src/usbg.c b/src/usbg.c
index d73943c..4d014a1 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -146,6 +146,7 @@ static int usbg_translate_error(int error)
 		break;
 	case EACCES:
 	case EROFS:
+	case EPERM:
 		ret = USBG_ERROR_NO_ACCESS;
 		break;
 	case ENOENT:
@@ -677,6 +678,25 @@ static usbg_binding *usbg_allocate_binding(char *path, char *name,
 	return b;
 }
 
+static int ubsg_rm_file(char *path, char *name)
+{
+	int ret = USBG_SUCCESS;
+	int nmb;
+	char buf[USBG_MAX_PATH_LENGTH];
+
+	nmb = snprintf(buf, sizeof(buf), "%s/%s", path, name);
+	if (nmb < sizeof(buf)) {
+		nmb = unlink(buf);
+		if (nmb != 0)
+			ret = usbg_translate_error(errno);
+	} else {
+		ret = USBG_ERROR_PATH_TOO_LONG;
+	}
+
+	return ret;
+}
+
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
 		usbg_function_attrs *f_attrs)
 {
@@ -1263,6 +1283,25 @@ usbg_binding *usbg_get_link_binding(usbg_config *c, usbg_function *f)
 	return NULL;
 }
 
+int usbg_rm_binding(usbg_binding *b)
+{
+	int ret = USBG_SUCCESS;
+	usbg_config *c;
+
+	if (!b)
+		return USBG_ERROR_INVALID_PARAM;
+
+	c = b->parent;
+
+	ret = ubsg_rm_file(b->path, b->name);
+	if (ret == USBG_SUCCESS) {
+		TAILQ_REMOVE(&(c->bindings), b, bnode);
+		usbg_free_binding(b);
+	}
+
+	return ret;
+}
+
 static int usbg_create_empty_gadget(usbg_state *s, char *name, usbg_gadget **g)
 {
 	char gpath[USBG_MAX_PATH_LENGTH];
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux