Skip to contentSkip to conversation

Add support to reset device controls

PATCH Superseded Kieran Bingham 2 participants 4 messages Dec 3, 2018 – Feb 19, 2019 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support to reset device controls

Provide a new option '--reset-controls' which will enumerate the available controls on a device or sub-device, and re-initialise them to defaults.

---

This 'extends' the video_list_controls() function with an extra bool flag for 'reset' to prevent duplicating the iteration functionality.

The patch could also pass the same flag into 'video_print_control()' rather than implementing 'video_reset_control()' which necessitates calling query_control() a second time.

I have chosen to add the extra call, as I feel it makes the code cleaner, and pollutes the existing implementation less. The cost of the extra query, while a little redundant should be minimal and produces a simple function to reset the control independently.

---
 yavta.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
yavta.c +39 -2
index c7986bd9e031..30022c45ed5b 100644
@@ -1186,7 +1186,28 @@ static int video_print_control(struct device *dev, unsigned int id, bool full)
 	return query.id;
 }
 
-static void video_list_controls(struct device *dev)
+static int video_reset_control(struct device *dev, unsigned int id)
+{
+	struct v4l2_queryctrl query;
+	int ret;
+
+	ret = query_control(dev, id, &query);
+	if (ret < 0)
+		return ret;
+
+	if (query.flags & V4L2_CTRL_FLAG_DISABLED)
+		return query.id;
+
+	if (query.type == V4L2_CTRL_TYPE_CTRL_CLASS)
+		return query.id;
+
+	printf("Reset control 0x%08x to %d:\n", query.id, query.default_value);
+	set_control(dev, query.id, query.default_value);
+
+	return query.id;
+}
+
+static void video_list_controls(struct device *dev, bool reset)
 {
 	unsigned int nctrls = 0;
 	unsigned int id;
@@ -1207,6 +1228,12 @@ static void video_list_controls(struct device *dev)
 		if (ret < 0)
 			break;
 
+		if (reset) {
+			ret = video_reset_control(dev, id);
+			if (ret < 0)
+				break;
+		}
+
 		id = ret;
 		nctrls++;
 	}
@@ -1837,6 +1864,7 @@ static void usage(const char *argv0)
 	printf("    --offset			User pointer buffer offset from page start\n");
 	printf("    --premultiplied		Color components are premultiplied by alpha value\n");
 	printf("    --queue-late		Queue buffers after streamon, not before\n");
+	printf("    --reset-controls		Enumerate available controls and reset to defaults\n");
 	printf("    --requeue-last		Requeue the last buffers before streamoff\n");
 	printf("    --timestamp-source		Set timestamp source on output buffers [eof, soe]\n");
 	printf("    --skip n			Skip the first n frames\n");
@@ -1860,6 +1888,7 @@ static void usage(const char *argv0)
 #define OPT_PREMULTIPLIED	269
 #define OPT_QUEUE_LATE		270
 #define OPT_DATA_PREFIX		271
+#define OPT_RESET_CONTROLS	272
 
 static struct option opts[] = {
 	{"buffer-size", 1, 0, OPT_BUFFER_SIZE},
@@ -1888,6 +1917,7 @@ static struct option opts[] = {
 	{"queue-late", 0, 0, OPT_QUEUE_LATE},
 	{"get-control", 1, 0, 'r'},
 	{"requeue-last", 0, 0, OPT_REQUEUE_LAST},
+	{"reset-controls", 0, 0, OPT_RESET_CONTROLS},
 	{"realtime", 2, 0, 'R'},
 	{"size", 1, 0, 's'},
 	{"set-control", 1, 0, 'w'},
@@ -1915,6 +1945,7 @@ int main(int argc, char *argv[])
 	int do_enum_formats = 0, do_set_format = 0;
 	int do_enum_inputs = 0, do_set_input = 0;
 	int do_list_controls = 0, do_get_control = 0, do_set_control = 0;
+	int do_reset_controls = 0;
 	int do_sleep_forever = 0, do_requeue_last = 0;
 	int do_rt = 0, do_log_status = 0;
 	int no_query = 0, do_queue_late = 0;
@@ -2107,6 +2138,9 @@ int main(int argc, char *argv[])
 		case OPT_QUEUE_LATE:
 			do_queue_late = 1;
 			break;
+		case OPT_RESET_CONTROLS:
+			do_reset_controls = 1;
+			break;
 		case OPT_REQUEUE_LAST:
 			do_requeue_last = 1;
 			break;
@@ -2185,7 +2219,10 @@ int main(int argc, char *argv[])
 		set_control(&dev, ctrl_name, ctrl_value);
 
 	if (do_list_controls)
-		video_list_controls(&dev);
+		video_list_controls(&dev, false);
+
+	if (do_reset_controls)
+		video_list_controls(&dev, true);
 
 	if (do_enum_formats) {
 		printf("- Available formats:\n");
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 yavta: Refactor video_list_controls()

Separate iteration over controls from printing, in order to reuse the iteration to implement control reset.

---
 yavta.c | 134 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 83 insertions(+), 51 deletions(-)
yavta.c +83 -51
index 2d3b2d096f7d..607a2883af4c 100644
@@ -484,9 +484,12 @@ static int query_control(struct device *dev, unsigned int id,
 	query->id = id;
 
 	ret = ioctl(dev->fd, VIDIOC_QUERYCTRL, query);
-	if (ret < 0 && errno != EINVAL)
-		printf("unable to query control 0x%8.8x: %s (%d).\n",
-		       id, strerror(errno), errno);
+	if (ret < 0) {
+		ret = -errno;
+		if (ret != -EINVAL)
+			printf("unable to query control 0x%8.8x: %s (%d).\n",
+			       id, strerror(errno), errno);
+	}
 
 	return ret;
 }
@@ -1120,7 +1123,46 @@ static int video_enable(struct device *dev, int enable)
 	return 0;
 }
 
-static void video_query_menu(struct device *dev, struct v4l2_queryctrl *query,
+static int video_for_each_control(struct device *dev,
+				  int(*callback)(struct device *dev, void *data, const struct v4l2_queryctrl *query),
+				  void *data)
+{
+	struct v4l2_queryctrl query;
+	unsigned int nctrls = 0;
+	unsigned int id;
+	int ret;
+
+#ifndef V4L2_CTRL_FLAG_NEXT_CTRL
+	unsigned int i;
+
+	for (i = V4L2_CID_BASE; i <= V4L2_CID_LASTP1; ++i) {
+		id = i;
+#else
+	id = 0;
+	while (1) {
+		id |= V4L2_CTRL_FLAG_NEXT_CTRL;
+#endif
+
+		ret = query_control(dev, id, &query);
+		if (ret == -EINVAL)
+			break;
+		if (ret < 0)
+			return ret;
+
+		id = query.id;
+
+		ret = callback(dev, data, &query);
+		if (ret < 0)
+			return ret;
+
+		if (ret)
+			nctrls++;
+	}
+
+	return nctrls;
+}
+
+static void video_query_menu(struct device *dev, const struct v4l2_queryctrl *query,
 			     unsigned int value)
 {
 	struct v4l2_querymenu menu;
@@ -1142,83 +1184,68 @@ static void video_query_menu(struct device *dev, struct v4l2_queryctrl *query,
 	};
 }
 
-static int video_print_control(struct device *dev, unsigned int id, bool full)
+static int video_print_control(struct device *dev,
+			       const struct v4l2_queryctrl *query, bool full)
 {
 	struct v4l2_ext_control ctrl;
-	struct v4l2_queryctrl query;
 	char sval[24];
 	char *current = sval;
 	int ret;
 
-	ret = query_control(dev, id, &query);
-	if (ret < 0)
-		return ret;
+	if (query->flags & V4L2_CTRL_FLAG_DISABLED)
+		return 1;
 
-	if (query.flags & V4L2_CTRL_FLAG_DISABLED)
-		return query.id;
-
-	if (query.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
-		printf("--- %s (class 0x%08x) ---\n", query.name, query.id);
-		return query.id;
+	if (query->type == V4L2_CTRL_TYPE_CTRL_CLASS) {
+		printf("--- %s (class 0x%08x) ---\n", query->name, query->id);
+		return 1;
 	}
 
-	ret = get_control(dev, &query, &ctrl);
+	ret = get_control(dev, query, &ctrl);
 	if (ret < 0)
 		strcpy(sval, "n/a");
-	else if (query.type == V4L2_CTRL_TYPE_INTEGER64)
+	else if (query->type == V4L2_CTRL_TYPE_INTEGER64)
 		sprintf(sval, "%lld", ctrl.value64);
-	else if (query.type == V4L2_CTRL_TYPE_STRING)
+	else if (query->type == V4L2_CTRL_TYPE_STRING)
 		current = ctrl.string;
 	else
 		sprintf(sval, "%d", ctrl.value);
 
 	if (full)
 		printf("control 0x%08x `%s' min %d max %d step %d default %d current %s.\n",
-			query.id, query.name, query.minimum, query.maximum,
-			query.step, query.default_value, current);
+			query->id, query->name, query->minimum, query->maximum,
+			query->step, query->default_value, current);
 	else
-		printf("control 0x%08x current %s.\n", query.id, current);
+		printf("control 0x%08x current %s.\n", query->id, current);
 
-	if (query.type == V4L2_CTRL_TYPE_STRING)
+	if (query->type == V4L2_CTRL_TYPE_STRING)
 		free(ctrl.string);
 
 	if (!full)
-		return query.id;
+		return 1;
 
-	if (query.type == V4L2_CTRL_TYPE_MENU ||
-	    query.type == V4L2_CTRL_TYPE_INTEGER_MENU)
-		video_query_menu(dev, &query, ctrl.value);
+	if (query->type == V4L2_CTRL_TYPE_MENU ||
+	    query->type == V4L2_CTRL_TYPE_INTEGER_MENU)
+		video_query_menu(dev, query, ctrl.value);
 
-	return query.id;
+	return 1;
+}
+
+static int __video_print_control(struct device *dev, void *data,
+				 const struct v4l2_queryctrl *query)
+{
+	return video_print_control(dev, query, (bool)data);
 }
 
 static void video_list_controls(struct device *dev)
 {
-	unsigned int nctrls = 0;
-	unsigned int id;
 	int ret;
 
-#ifndef V4L2_CTRL_FLAG_NEXT_CTRL
-	unsigned int i;
+	ret = video_for_each_control(dev, __video_print_control, (void *)true);
+	if (ret < 0)
+		return;
 
-	for (i = V4L2_CID_BASE; i <= V4L2_CID_LASTP1; ++i) {
-		id = i;
-#else
-	id = 0;
-	while (1) {
-		id |= V4L2_CTRL_FLAG_NEXT_CTRL;
-#endif
-
-		ret = video_print_control(dev, id, true);
-		if (ret < 0)
-			break;
-
-		id = ret;
-		nctrls++;
-	}
-
-	if (nctrls)
-		printf("%u control%s found.\n", nctrls, nctrls > 1 ? "s" : "");
+	if (ret)
+		printf("%u control%s found.\n", ret, ret > 1 ? "s" : "");
 	else
 		printf("No control found.\n");
 }
@@ -2184,8 +2211,13 @@ int main(int argc, char *argv[])
 	if (do_log_status)
 		video_log_status(&dev);
 
-	if (do_get_control)
-		video_print_control(&dev, ctrl_name, false);
+	if (do_get_control) {
+		struct v4l2_queryctrl query;
+
+		ret = query_control(&dev, ctrl_name, &query);
+		if (ret == 0)
+			video_print_control(&dev, &query, false);
+	}
 
 	if (do_set_control)
 		set_control(&dev, ctrl_name, ctrl_value);
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

PATCH 2/2 Add support to reset device controls

From: Kieran Bingham <[email protected]>

Provide a new option '--reset-controls' which will enumerate the available controls on a device or sub-device, and re-initialise them to defaults.

---
 yavta.c | 45 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)
yavta.c +41 -4
index 607a2883af4c..1b5d55bb569b 100644
@@ -540,7 +540,7 @@ static int get_control(struct device *dev, const struct v4l2_queryctrl *query,
 }
 
 static void set_control(struct device *dev, unsigned int id,
-		        int64_t val)
+		        int64_t val, bool verbose)
 {
 	struct v4l2_ext_controls ctrls;
 	struct v4l2_ext_control ctrl;
@@ -590,8 +590,9 @@ static void set_control(struct device *dev, unsigned int id,
 		return;
 	}
 
-	printf("Control 0x%08x set to %" PRId64 ", is %" PRId64 "\n",
-	       id, old_val, val);
+	if (verbose)
+		printf("Control 0x%08x set to %" PRId64 ", is %" PRId64 "\n",
+		       id, old_val, val);
 }
 
 static int video_get_format(struct device *dev)
@@ -1250,6 +1251,32 @@ static void video_list_controls(struct device *dev)
 		printf("No control found.\n");
 }
 
+static int video_reset_control(struct device *dev, void *data __attribute__((__unused__)),
+			       const struct v4l2_queryctrl *query)
+{
+	if (query->flags & V4L2_CTRL_FLAG_DISABLED)
+		return 0;
+
+	if (query->type == V4L2_CTRL_TYPE_CTRL_CLASS)
+		return 0;
+
+	set_control(dev, query->id, query->default_value, false);
+
+	return 1;
+}
+
+static void video_reset_controls(struct device *dev)
+{
+	int ret;
+
+	ret = video_for_each_control(dev, video_reset_control, NULL);
+	if (ret < 0)
+		return;
+
+	if (ret)
+		printf("%u control%s reset.\n", ret, ret > 1 ? "s" : "");
+}
+
 static void video_enum_frame_intervals(struct device *dev, __u32 pixelformat,
 	unsigned int width, unsigned int height)
 {
@@ -1871,6 +1898,7 @@ static void usage(const char *argv0)
 	printf("    --premultiplied		Color components are premultiplied by alpha value\n");
 	printf("    --queue-late		Queue buffers after streamon, not before\n");
 	printf("    --requeue-last		Requeue the last buffers before streamoff\n");
+	printf("    --reset-controls		Reset all available controls to their default value\n");
 	printf("    --timestamp-source		Set timestamp source on output buffers [eof, soe]\n");
 	printf("    --skip n			Skip the first n frames\n");
 	printf("    --sleep-forever		Sleep forever after configuring the device\n");
@@ -1893,6 +1921,7 @@ static void usage(const char *argv0)
 #define OPT_PREMULTIPLIED	269
 #define OPT_QUEUE_LATE		270
 #define OPT_DATA_PREFIX		271
+#define OPT_RESET_CONTROLS	272
 
 static struct option opts[] = {
 	{"buffer-size", 1, 0, OPT_BUFFER_SIZE},
@@ -1921,6 +1950,7 @@ static struct option opts[] = {
 	{"queue-late", 0, 0, OPT_QUEUE_LATE},
 	{"get-control", 1, 0, 'r'},
 	{"requeue-last", 0, 0, OPT_REQUEUE_LAST},
+	{"reset-controls", 0, 0, OPT_RESET_CONTROLS},
 	{"realtime", 2, 0, 'R'},
 	{"size", 1, 0, 's'},
 	{"set-control", 1, 0, 'w'},
@@ -1948,6 +1978,7 @@ int main(int argc, char *argv[])
 	int do_enum_formats = 0, do_set_format = 0;
 	int do_enum_inputs = 0, do_set_input = 0;
 	int do_list_controls = 0, do_get_control = 0, do_set_control = 0;
+	int do_reset_controls = 0;
 	int do_sleep_forever = 0, do_requeue_last = 0;
 	int do_rt = 0, do_log_status = 0;
 	int no_query = 0, do_queue_late = 0;
@@ -2140,6 +2171,9 @@ int main(int argc, char *argv[])
 		case OPT_QUEUE_LATE:
 			do_queue_late = 1;
 			break;
+		case OPT_RESET_CONTROLS:
+			do_reset_controls = 1;
+			break;
 		case OPT_REQUEUE_LAST:
 			do_requeue_last = 1;
 			break;
@@ -2220,11 +2254,14 @@ int main(int argc, char *argv[])
 	}
 
 	if (do_set_control)
-		set_control(&dev, ctrl_name, ctrl_value);
+		set_control(&dev, ctrl_name, ctrl_value, true);
 
 	if (do_list_controls)
 		video_list_controls(&dev);
 
+	if (do_reset_controls)
+		video_reset_controls(&dev);
+
 	if (do_enum_formats) {
 		printf("- Available formats:\n");
 		video_enum_formats(&dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw
In reply to Kieran Bingham

Hi Kieran,

Thank you for the patch.

Quoted 11 lines · Kieran Bingham

Provide a new option '--reset-controls' which will enumerate the available controls on a device or sub-device, and re-initialise them to defaults.

---

This 'extends' the video_list_controls() function with an extra bool flag for 'reset' to prevent duplicating the iteration functionality.

Quoted 8 lines

The patch could also pass the same flag into 'video_print_control()' rather than implementing 'video_reset_control()' which necessitates calling query_control() a second time.

I have chosen to add the extra call, as I feel it makes the code cleaner, and pollutes the existing implementation less. The cost of the extra query, while a little redundant should be minimal and produces a simple function to reset the control independently.

I think we can do a bit better by separating control enumeration/iteration and control print, to implement reset on top of a function that would do enumeration/iteration only. I've sent two patches in reply to this mail thread.

Quoted 108 lines
---
 yavta.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
yavta.c +39 -2
index c7986bd9e031..30022c45ed5b 100644
@@ -1186,7 +1186,28 @@ static int video_print_control(struct device *dev, unsigned int id, bool full)
 	return query.id;
 }
 
-static void video_list_controls(struct device *dev)
+static int video_reset_control(struct device *dev, unsigned int id)
+{
+	struct v4l2_queryctrl query;
+	int ret;
+
+	ret = query_control(dev, id, &query);
+	if (ret < 0)
+		return ret;
+
+	if (query.flags & V4L2_CTRL_FLAG_DISABLED)
+		return query.id;
+
+	if (query.type == V4L2_CTRL_TYPE_CTRL_CLASS)
+		return query.id;
+
+	printf("Reset control 0x%08x to %d:\n", query.id, query.default_value);
+	set_control(dev, query.id, query.default_value);
+
+	return query.id;
+}
+
+static void video_list_controls(struct device *dev, bool reset)
 {
 	unsigned int nctrls = 0;
 	unsigned int id;
@@ -1207,6 +1228,12 @@ static void video_list_controls(struct device *dev)
 		if (ret < 0)
 			break;
 
+		if (reset) {
+			ret = video_reset_control(dev, id);
+			if (ret < 0)
+				break;
+		}
+
 		id = ret;
 		nctrls++;
 	}
@@ -1837,6 +1864,7 @@ static void usage(const char *argv0)
 	printf("    --offset			User pointer buffer offset from page start\n");
 	printf("    --premultiplied		Color components are premultiplied by alpha value\n");
 	printf("    --queue-late		Queue buffers after streamon, not before\n");
+	printf("    --reset-controls		Enumerate available controls and reset to defaults\n");
 	printf("    --requeue-last		Requeue the last buffers before streamoff\n");
 	printf("    --timestamp-source		Set timestamp source on output buffers [eof, soe]\n");
 	printf("    --skip n			Skip the first n frames\n");
@@ -1860,6 +1888,7 @@ static void usage(const char *argv0)
 #define OPT_PREMULTIPLIED	269
 #define OPT_QUEUE_LATE		270
 #define OPT_DATA_PREFIX		271
+#define OPT_RESET_CONTROLS	272
 
 static struct option opts[] = {
 	{"buffer-size", 1, 0, OPT_BUFFER_SIZE},
@@ -1888,6 +1917,7 @@ static struct option opts[] = {
 	{"queue-late", 0, 0, OPT_QUEUE_LATE},
 	{"get-control", 1, 0, 'r'},
 	{"requeue-last", 0, 0, OPT_REQUEUE_LAST},
+	{"reset-controls", 0, 0, OPT_RESET_CONTROLS},
 	{"realtime", 2, 0, 'R'},
 	{"size", 1, 0, 's'},
 	{"set-control", 1, 0, 'w'},
@@ -1915,6 +1945,7 @@ int main(int argc, char *argv[])
 	int do_enum_formats = 0, do_set_format = 0;
 	int do_enum_inputs = 0, do_set_input = 0;
 	int do_list_controls = 0, do_get_control = 0, do_set_control = 0;
+	int do_reset_controls = 0;
 	int do_sleep_forever = 0, do_requeue_last = 0;
 	int do_rt = 0, do_log_status = 0;
 	int no_query = 0, do_queue_late = 0;
@@ -2107,6 +2138,9 @@ int main(int argc, char *argv[])
 		case OPT_QUEUE_LATE:
 			do_queue_late = 1;
 			break;
+		case OPT_RESET_CONTROLS:
+			do_reset_controls = 1;
+			break;
 		case OPT_REQUEUE_LAST:
 			do_requeue_last = 1;
 			break;
@@ -2185,7 +2219,10 @@ int main(int argc, char *argv[])
 		set_control(&dev, ctrl_name, ctrl_value);
 
 	if (do_list_controls)
-		video_list_controls(&dev);
+		video_list_controls(&dev, false);
+
+	if (do_reset_controls)
+		video_list_controls(&dev, true);
 
 	if (do_enum_formats) {
 		printf("- Available formats:\n");
Signature · 1 line
2.17.1
Signature · 3 lines
Regards,

Laurent Pinchart