Skip to contentSkip to conversation

media-ctl API changes to prepare for device enumeration library

PATCH In review Laurent Pinchart 3 participants 9 messages Mar 5, 2014 – Mar 7, 2014 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

v2 cover media-ctl API changes to prepare for device enumeration library

Hello,

Here's the second version of the patch set that make libmediactl usable by the upcoming media device enumeration library. In order to do so I need to change the libmediactl API and add support for emulated media devices.

In order to avoid further API/ABI breakages I've decided to make the media_device and media_entity structures private and provide accessors for the fields that need to be read. I'm open to suggestions on whether I should make the media_pad and media_link structures private now as well, or on any other aspect of these changes.

For reference the (work in progress) media device enumeration library is available at

http://git.ideasonboard.org/media-enum.git

Changes since v1:

- Made struct media_entity private

Laurent Pinchart (5):
Split media_device creation and opening
Make the media_device structure private
Make the media_entity structure private
Expose default devices
Add support for emulated devices

 src/main.c          | 137 +++++++++++++---------
 src/mediactl-priv.h |  64 ++++++++++
 src/mediactl.c      | 330 +++++++++++++++++++++++++++++++++++++++++++++-------
 src/mediactl.h      | 281 ++++++++++++++++++++++++++++++++++----------
 src/v4l2subdev.c    |   1 +
 5 files changed, 652 insertions(+), 161 deletions(-)
 create mode 100644 src/mediactl-priv.h
Signature · 3 lines
Regards,

Laurent Pinchart
#1 lore raw

PATCH v2 1/5 Split media_device creation and opening

Make the media_device refcounted to manage its life time.

---
 src/main.c     |  21 ++++---
 src/mediactl.c | 172 ++++++++++++++++++++++++++++++++++++++++-----------------
 src/mediactl.h |  84 +++++++++++++++-------------
 3 files changed, 180 insertions(+), 97 deletions(-)
src/main.c +13 -8
index 4a27c8c..8b48fde 100644
@@ -329,15 +329,20 @@ int main(int argc, char **argv)
 	if (parse_cmdline(argc, argv))
 		return EXIT_FAILURE;
 
-	/* Open the media device and enumerate entities, pads and links. */
+	media = media_device_new(media_opts.devname);
+	if (media == NULL) {
+		printf("Failed to create media device\n");
+		goto out;
+	}
+
 	if (media_opts.verbose)
-		media = media_open_debug(
-			media_opts.devname,
+		media_debug_set_handler(media,
 			(void (*)(void *, ...))fprintf, stdout);
-	else
-		media = media_open(media_opts.devname);
-	if (media == NULL) {
-		printf("Failed to open %s\n", media_opts.devname);
+
+	/* Enumerate entities, pads and links. */
+	ret = media_device_enumerate(media);
+	if (ret < 0) {
+		printf("Failed to enumerate %s (%d)\n", media_opts.devname, ret);
 		goto out;
 	}
 
@@ -446,7 +451,7 @@ int main(int argc, char **argv)
 
 out:
 	if (media)
-		media_close(media);
+		media_device_unref(media);
 
 	return ret ? EXIT_FAILURE : EXIT_SUCCESS;
 }
src/mediactl.c +121 -51

241 lines not on this page · raw message

src/mediactl.h +46 -38

113 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#2 lore raw

Re: [PATCH/RFC v2 1/5] Split media_device creation and opening

Hi Laurent,

Thanks for the set.

On Wed, Mar 05, 2014 at 06:32:17PM +0100, Laurent Pinchart wrote:
...

Quoted 63 lines
src/main.c +0 -0
index 4a27c8c..8b48fde 100644
src/mediactl.c +40 -0
index 57cf86b..c71d4e1 100644
@@ -101,6 +101,42 @@ struct media_entity *media_get_entity_by_id(struct media_device *media,
 	return NULL;
 }
 
+/* -----------------------------------------------------------------------------
+ * Open/close
+ */
+
+static int media_device_open(struct media_device *media)
+{
+	int ret;
+
+	if (media->fd != -1)
+		return 0;
+
+	media_dbg(media, "Opening media device %s\n", media->devnode);
+
+	media->fd = open(media->devnode, O_RDWR);
+	if (media->fd < 0) {
+		ret = -errno;
+		media_dbg(media, "%s: Can't open media device %s\n",
+			  __func__, media->devnode);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void media_device_close(struct media_device *media)
+{
+	if (media->fd != -1) {
+		close(media->fd);
+		media->fd = -1;
+	}
+}
+
+/* -----------------------------------------------------------------------------
+ * Link setup
+ */
+
 int media_setup_link(struct media_device *media,
 		     struct media_pad *source,
 		     struct media_pad *sink,
@@ -111,6 +147,10 @@ int media_setup_link(struct media_device *media,
 	unsigned int i;
 	int ret;
 
+	ret = media_device_open(media);
+	if (ret < 0)
+		goto done;
+
 	for (i = 0; i < source->entity->num_links; i++) {
 		link = &source->entity->links[i];
 

src/mediactl.c int media_setup_link(struct media_device *media, · line 166

Quoted 5 lines
src/mediactl.c +1 -1
@@ -123,7 +163,8 @@ int media_setup_link(struct media_device *media,
 
 	if (i == source->entity->num_links) {
 		media_dbg(media, "%s: Link not found\n", __func__);
-		return -ENOENT;
+		ret = -ENOENT;

Please use errno before further function calls, i.e. reverse the order of the print and ret assignment.

src/mediactl.c int media_setup_link(struct media_device *media, · line 186

Quoted 10 lines
src/mediactl.c +2 -1
+		goto done;
 	}
 
 	/* source pad */
@@ -142,12 +183,18 @@ int media_setup_link(struct media_device *media,
 	if (ret == -1) {
 		media_dbg(media, "%s: Unable to setup link (%s)\n",
 			  __func__, strerror(errno));
-		return -errno;
+		ret = -errno;

Same here.

Quoted 24 lines
src/mediactl.c +12 -1
+		goto done;
 	}
 
 	link->flags = ulink.flags;
 	link->twin->flags = ulink.flags;
-	return 0;
+
+	ret = 0;
+
+done:
+	media_device_close(media);
+	return ret;
 }
 
 int media_reset_links(struct media_device *media)
@@ -425,6 +472,58 @@ static int media_enum_entities(struct media_device *media)
 	return ret;
 }
 
+int media_device_enumerate(struct media_device *media)
+{
+	int ret;
+
+	if (media->entities)

src/mediactl.c static int media_enum_entities(struct media_device *media) · line 489

Quoted 10 lines
src/mediactl.c +10 -0
+		return 0;
+
+	ret = media_device_open(media);
+	if (ret < 0)
+		return ret;
+
+	ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
+	if (ret < 0) {
+		media_dbg(media, "%s: Unable to retrieve media device "
+			  "information for device %s (%s)\n", __func__,

Splitting strings is not recommended since this breaks grepping them. But perhaps this is so small project it doesn't make a big difference. I wouldn't still.

src/mediactl.c static int media_enum_entities(struct media_device *media) · line 491

Quoted 2 lines
src/mediactl.c +2 -0
+			  media->devnode, strerror(errno));
+		ret = -errno;

And here.

Quoted 38 lines
src/mediactl.c +35 -0
+		goto done;
+	}
+
+	media_dbg(media, "Enumerating entities\n");
+
+	ret = media_enum_entities(media);
+	if (ret < 0) {
+		media_dbg(media,
+			  "%s: Unable to enumerate entities for device %s (%s)\n",
+			  __func__, media->devnode, strerror(-ret));
+		goto done;
+	}
+
+	media_dbg(media, "Found %u entities\n", media->entities_count);
+	media_dbg(media, "Enumerating pads and links\n");
+
+	ret = media_enum_links(media);
+	if (ret < 0) {
+		media_dbg(media,
+			  "%s: Unable to enumerate pads and linksfor device %s\n",
+			  __func__, media->devnode);
+		goto done;
+	}
+
+	ret = 0;
+
+done:
+	media_device_close(media);
+	return ret;
+}
+
+/* -----------------------------------------------------------------------------
+ * Create/destroy
+ */
+
 static void media_debug_default(void *ptr, ...)
 {
 }
Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#4 lore raw

PATCH v2 2/5 Make the media_device structure private

In reply to Laurent Pinchart
---
 src/main.c          | 32 ++++++++++++++++-----------
 src/mediactl-priv.h | 45 ++++++++++++++++++++++++++++++++++++++
 src/mediactl.c      | 29 +++++++++++++++++++++++++
 src/mediactl.h      | 62 +++++++++++++++++++++++++++++++++++++++++------------
 src/v4l2subdev.c    |  1 +
 5 files changed, 142 insertions(+), 27 deletions(-)
 create mode 100644 src/mediactl-priv.h
src/main.c +19 -13
index 8b48fde..b0e2277 100644
@@ -171,13 +171,15 @@ static const char *media_pad_type_to_string(unsigned flag)
 
 static void media_print_topology_dot(struct media_device *media)
 {
+	struct media_entity *entities = media_get_entities(media);
+	unsigned int nents = media_get_entities_count(media);
 	unsigned int i, j;
 
 	printf("digraph board {\n");
 	printf("\trankdir=TB\n");
 
-	for (i = 0; i < media->entities_count; ++i) {
-		struct media_entity *entity = &media->entities[i];
+	for (i = 0; i < nents; ++i) {
+		struct media_entity *entity = &entities[i];
 		unsigned int npads;
 
 		switch (media_entity_type(entity)) {
@@ -254,13 +256,15 @@ static void media_print_topology_text(struct media_device *media)
 		{ MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" },
 	};
 
+	struct media_entity *entities = media_get_entities(media);
+	unsigned int nents = media_get_entities_count(media);
 	unsigned int i, j, k;
 	unsigned int padding;
 
 	printf("Device topology\n");
 
-	for (i = 0; i < media->entities_count; ++i) {
-		struct media_entity *entity = &media->entities[i];
+	for (i = 0; i < nents; ++i) {
+		struct media_entity *entity = &entities[i];
 
 		padding = printf("- entity %u: ", entity->info.id);
 		printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
@@ -347,10 +351,12 @@ int main(int argc, char **argv)
 	}
 
 	if (media_opts.print) {
+		const struct media_device_info *info = media_get_info(media);
+
 		printf("Media controller API version %u.%u.%u\n\n",
-		       (media->info.media_version << 16) & 0xff,
-		       (media->info.media_version << 8) & 0xff,
-		       (media->info.media_version << 0) & 0xff);
+		       (info->media_version << 16) & 0xff,
+		       (info->media_version << 8) & 0xff,
+		       (info->media_version << 0) & 0xff);
 		printf("Media device information\n"
 		       "------------------------\n"
 		       "driver          %s\n"
@@ -359,12 +365,12 @@ int main(int argc, char **argv)
 		       "bus info        %s\n"
 		       "hw revision     0x%x\n"
 		       "driver version  %u.%u.%u\n\n",
-		       media->info.driver, media->info.model,
-		       media->info.serial, media->info.bus_info,
-		       media->info.hw_revision,
-		       (media->info.driver_version << 16) & 0xff,
-		       (media->info.driver_version << 8) & 0xff,
-		       (media->info.driver_version << 0) & 0xff);
+		       info->driver, info->model,
+		       info->serial, info->bus_info,
+		       info->hw_revision,
+		       (info->driver_version << 16) & 0xff,
+		       (info->driver_version << 8) & 0xff,
+		       (info->driver_version << 0) & 0xff);
 	}
 
 	if (media_opts.entity) {
src/mediactl-priv.h new file +45 -0

45 lines not on this page · raw message

src/mediactl.c +29 -0
index c71d4e1..2ba0ab8 100644
@@ -38,8 +38,13 @@
 #include <linux/videodev2.h>
 
 #include "mediactl.h"
+#include "mediactl-priv.h"
 #include "tools.h"
 
+/* -----------------------------------------------------------------------------
+ * Graph access
+ */
+
 struct media_pad *media_entity_remote_source(struct media_pad *pad)
 {
 	unsigned int i;
@@ -101,6 +106,26 @@ struct media_entity *media_get_entity_by_id(struct media_device *media,
 	return NULL;
 }
 
+unsigned int media_get_entities_count(struct media_device *media)
+{
+	return media->entities_count;
+}
+
+struct media_entity *media_get_entities(struct media_device *media)
+{
+	return media->entities;
+}
+
+const struct media_device_info *media_get_info(struct media_device *media)
+{
+	return &media->info;
+}
+
+const char *media_get_devnode(struct media_device *media)
+{
+	return media->devnode;
+}
+
 /* -----------------------------------------------------------------------------
  * Open/close
  */
@@ -222,6 +247,10 @@ int media_reset_links(struct media_device *media)
 	return 0;
 }
 
+/* -----------------------------------------------------------------------------
+ * Entities, pads and links enumeration
+ */
+
 static struct media_link *media_entity_add_link(struct media_entity *entity)
 {
 	if (entity->num_links >= entity->max_links) {
src/mediactl.h +48 -14

74 lines not on this page · raw message

src/v4l2subdev.c +1 -0
index 2d45d7f..77fe420 100644
@@ -35,6 +35,7 @@
 #include <linux/v4l2-subdev.h>
 
 #include "mediactl.h"
+#include "mediactl-priv.h"
 #include "tools.h"
 #include "v4l2subdev.h"
 
Signature · 1 line
1.8.3.2
#5 lore raw

PATCH v2 3/5 Make the media_entity structure private

In reply to Laurent Pinchart
---
 src/main.c          | 94 ++++++++++++++++++++++++++++++-----------------------
 src/mediactl-priv.h | 12 +++++++
 src/mediactl.c      | 38 ++++++++++++++++++++--
 src/mediactl.h      | 78 +++++++++++++++++++++++++++++++++++---------
 4 files changed, 164 insertions(+), 58 deletions(-)
src/main.c +53 -41
index b0e2277..6f980b4 100644
@@ -171,7 +171,6 @@ static const char *media_pad_type_to_string(unsigned flag)
 
 static void media_print_topology_dot(struct media_device *media)
 {
-	struct media_entity *entities = media_get_entities(media);
 	unsigned int nents = media_get_entities_count(media);
 	unsigned int i, j;
 
@@ -179,34 +178,41 @@ static void media_print_topology_dot(struct media_device *media)
 	printf("\trankdir=TB\n");
 
 	for (i = 0; i < nents; ++i) {
-		struct media_entity *entity = &entities[i];
+		struct media_entity *entity = media_get_entity(media, i);
+		const struct media_entity_desc *info = media_entity_get_info(entity);
+		const char *devname = media_entity_get_devname(entity);
+		unsigned int num_links = media_entity_get_links_count(entity);
 		unsigned int npads;
 
 		switch (media_entity_type(entity)) {
 		case MEDIA_ENT_T_DEVNODE:
 			printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
 			       "fillcolor=yellow]\n",
-			       entity->info.id, entity->info.name, entity->devname);
+			       info->id, info->name, devname);
 			break;
 
 		case MEDIA_ENT_T_V4L2_SUBDEV:
-			printf("\tn%08x [label=\"{{", entity->info.id);
+			printf("\tn%08x [label=\"{{", info->id);
 
-			for (j = 0, npads = 0; j < entity->info.pads; ++j) {
-				if (!(entity->pads[j].flags & MEDIA_PAD_FL_SINK))
+			for (j = 0, npads = 0; j < info->pads; ++j) {
+				const struct media_pad *pad = media_entity_get_pad(entity, j);
+
+				if (!(pad->flags & MEDIA_PAD_FL_SINK))
 					continue;
 
 				printf("%s<port%u> %u", npads ? " | " : "", j, j);
 				npads++;
 			}
 
-			printf("} | %s", entity->info.name);
-			if (entity->devname)
-				printf("\\n%s", entity->devname);
+			printf("} | %s", info->name);
+			if (devname)
+				printf("\\n%s", devname);
 			printf(" | {");
 
-			for (j = 0, npads = 0; j < entity->info.pads; ++j) {
-				if (!(entity->pads[j].flags & MEDIA_PAD_FL_SOURCE))
+			for (j = 0, npads = 0; j < info->pads; ++j) {
+				const struct media_pad *pad = media_entity_get_pad(entity, j);
+
+				if (!(pad->flags & MEDIA_PAD_FL_SOURCE))
 					continue;
 
 				printf("%s<port%u> %u", npads ? " | " : "", j, j);
@@ -220,19 +226,21 @@ static void media_print_topology_dot(struct media_device *media)
 			continue;
 		}
 
-		for (j = 0; j < entity->num_links; j++) {
-			struct media_link *link = &entity->links[j];
+		for (j = 0; j < num_links; j++) {
+			const struct media_link *link = media_entity_get_link(entity, j);
+			const struct media_pad *source = link->source;
+			const struct media_pad *sink = link->sink;
 
-			if (link->source->entity != entity)
+			if (source->entity != entity)
 				continue;
 
-			printf("\tn%08x", link->source->entity->info.id);
-			if (media_entity_type(link->source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
-				printf(":port%u", link->source->index);
+			printf("\tn%08x", media_entity_get_info(source->entity)->id);
+			if (media_entity_type(source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
+				printf(":port%u", source->index);
 			printf(" -> ");
-			printf("n%08x", link->sink->entity->info.id);
-			if (media_entity_type(link->sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
-				printf(":port%u", link->sink->index);
+			printf("n%08x", media_entity_get_info(sink->entity)->id);
+			if (media_entity_type(sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
+				printf(":port%u", sink->index);
 
 			if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
 				printf(" [style=bold]");
@@ -256,7 +264,6 @@ static void media_print_topology_text(struct media_device *media)
 		{ MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" },
 	};
 
-	struct media_entity *entities = media_get_entities(media);
 	unsigned int nents = media_get_entities_count(media);
 	unsigned int i, j, k;
 	unsigned int padding;
@@ -264,40 +271,45 @@ static void media_print_topology_text(struct media_device *media)
 	printf("Device topology\n");
 
 	for (i = 0; i < nents; ++i) {
-		struct media_entity *entity = &entities[i];
-
-		padding = printf("- entity %u: ", entity->info.id);
-		printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
-			entity->info.pads, entity->info.pads > 1 ? "s" : "",
-			entity->num_links, entity->num_links > 1 ? "s" : "");
+		struct media_entity *entity = media_get_entity(media, i);
+		const struct media_entity_desc *info = media_entity_get_info(entity);
+		const char *devname = media_entity_get_devname(entity);
+		unsigned int num_links = media_entity_get_links_count(entity);
+
+		padding = printf("- entity %u: ", info->id);
+		printf("%s (%u pad%s, %u link%s)\n", info->name,
+			info->pads, info->pads > 1 ? "s" : "",
+			num_links, num_links > 1 ? "s" : "");
 		printf("%*ctype %s subtype %s flags %x\n", padding, ' ',
-			media_entity_type_to_string(entity->info.type),
-			media_entity_subtype_to_string(entity->info.type),
-			entity->info.flags);
-		if (entity->devname[0])
-			printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
+			media_entity_type_to_string(info->type),
+			media_entity_subtype_to_string(info->type),
+			info->flags);
+		if (devname)
+			printf("%*cdevice node name %s\n", padding, ' ', devname);
 
-		for (j = 0; j < entity->info.pads; j++) {
-			struct media_pad *pad = &entity->pads[j];
+		for (j = 0; j < info->pads; j++) {
+			const struct media_pad *pad = media_entity_get_pad(entity, j);
 
 			printf("\tpad%u: %s\n", j, media_pad_type_to_string(pad->flags));
 
 			if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
 				v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
 
-			for (k = 0; k < entity->num_links; k++) {
-				struct media_link *link = &entity->links[k];
-				struct media_pad *source = link->source;
-				struct media_pad *sink = link->sink;
+			for (k = 0; k < num_links; k++) {
+				const struct media_link *link = media_entity_get_link(entity, k);
+				const struct media_pad *source = link->source;
+				const struct media_pad *sink = link->sink;
 				bool first = true;
 				unsigned int i;
 
 				if (source->entity == entity && source->index == j)
 					printf("\t\t-> \"%s\":%u [",
-						sink->entity->info.name, sink->index);
+						media_entity_get_info(sink->entity)->name,
+						sink->index);
 				else if (sink->entity == entity && sink->index == j)
 					printf("\t\t<- \"%s\":%u [",
-						source->entity->info.name, source->index);
+						media_entity_get_info(source->entity)->name,
+						source->index);
 				else
 					continue;
 
@@ -383,7 +395,7 @@ int main(int argc, char **argv)
 			goto out;
 		}
 
-		printf("%s\n", entity->devname);
+		printf("%s\n", media_entity_get_devname(entity));
 	}
 
 	if (media_opts.pad) {
src/mediactl-priv.h +12 -0
index 844acc7..37e60aa 100644
@@ -26,6 +26,18 @@
 
 #include "mediactl.h"
 
+struct media_entity {
+	struct media_device *media;
+	struct media_entity_desc info;
+	struct media_pad *pads;
+	struct media_link *links;
+	unsigned int max_links;
+	unsigned int num_links;
+
+	char devname[32];
+	int fd;
+};
+
 struct media_device {
 	int fd;
 	int refcount;
src/mediactl.c +36 -2
index 2ba0ab8..a48953a 100644
@@ -111,9 +111,38 @@ unsigned int media_get_entities_count(struct media_device *media)
 	return media->entities_count;
 }
 
-struct media_entity *media_get_entities(struct media_device *media)
+struct media_entity *media_get_entity(struct media_device *media, unsigned int index)
 {
-	return media->entities;
+	if (index >= media->entities_count)
+		return NULL;
+
+	return &media->entities[index];
+}
+
+const struct media_pad *media_entity_get_pad(struct media_entity *entity, unsigned int index)
+{
+	if (index >= entity->info.pads)
+		return NULL;
+
+	return &entity->pads[index];
+}
+
+unsigned int media_entity_get_links_count(struct media_entity *entity)
+{
+	return entity->num_links;
+}
+
+const struct media_link *media_entity_get_link(struct media_entity *entity, unsigned int index)
+{
+	if (index >= entity->num_links)
+		return NULL;
+
+	return &entity->links[index];
+}
+
+const char *media_entity_get_devname(struct media_entity *entity)
+{
+	return entity->devname[0] ? entity->devname : NULL;
 }
 
 const struct media_device_info *media_get_info(struct media_device *media)
@@ -126,6 +155,11 @@ const char *media_get_devnode(struct media_device *media)
 	return media->devnode;
 }
 
+const struct media_entity_desc *media_entity_get_info(struct media_entity *entity)
+{
+	return &entity->info;
+}
+
 /* -----------------------------------------------------------------------------
  * Open/close
  */
src/mediactl.h +63 -15

103 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#6 lore raw

PATCH v2 4/5 Expose default devices

In reply to Laurent Pinchart
---
 src/mediactl-priv.h |  7 +++++++
 src/mediactl.c      | 32 ++++++++++++++++++++++++++++++++
 src/mediactl.h      | 19 +++++++++++++++++++
 3 files changed, 58 insertions(+)
src/mediactl-priv.h +7 -0
index 37e60aa..4644165 100644
@@ -49,6 +49,13 @@ struct media_device {
 
 	void (*debug_handler)(void *, ...);
 	void *debug_priv;
+
+	struct {
+		struct media_entity *v4l;
+		struct media_entity *fb;
+		struct media_entity *alsa;
+		struct media_entity *dvb;
+	} def;
 };
 
 #define media_dbg(media, ...) \
src/mediactl.c +32 -0
index a48953a..5bf76ea 100644
@@ -145,6 +145,21 @@ const char *media_entity_get_devname(struct media_entity *entity)
 	return entity->devname[0] ? entity->devname : NULL;
 }
 
+struct media_entity *media_get_default_entity(struct media_device *media,
+					      unsigned int type)
+{
+	switch (type) {
+	case MEDIA_ENT_T_DEVNODE_V4L:
+		return media->def.v4l;
+	case MEDIA_ENT_T_DEVNODE_FB:
+		return media->def.fb;
+	case MEDIA_ENT_T_DEVNODE_ALSA:
+		return media->def.alsa;
+	case MEDIA_ENT_T_DEVNODE_DVB:
+		return media->def.dvb;
+	}
+}
+
 const struct media_device_info *media_get_info(struct media_device *media)
 {
 	return &media->info;
@@ -518,6 +533,23 @@ static int media_enum_entities(struct media_device *media)
 
 		media->entities_count++;
 
+		if (entity->info.flags & MEDIA_ENT_FL_DEFAULT) {
+			switch (entity->info.type) {
+			case MEDIA_ENT_T_DEVNODE_V4L:
+				media->def.v4l = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_FB:
+				media->def.fb = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_ALSA:
+				media->def.alsa = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_DVB:
+				media->def.dvb = entity;
+				break;
+			}
+		}
+
 		/* Find the corresponding device name. */
 		if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
 		    media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
src/mediactl.h +19 -0
index e2c93b8..b74be0b 100644
@@ -244,6 +244,25 @@ unsigned int media_get_entities_count(struct media_device *media);
 struct media_entity *media_get_entity(struct media_device *media, unsigned int index);
 
 /**
+ * @brief Get the default entity for a given type
+ * @param media - media device.
+ * @param type - entity type.
+ *
+ * This function returns the default entity of the requested type. @a type must
+ * be one of
+ *
+ *	MEDIA_ENT_T_DEVNODE_V4L
+ *	MEDIA_ENT_T_DEVNODE_FB
+ *	MEDIA_ENT_T_DEVNODE_ALSA
+ *	MEDIA_ENT_T_DEVNODE_DVB
+ *
+ * @return A pointer to the default entity for the type if it exists, or NULL
+ * otherwise.
+ */
+struct media_entity *media_get_default_entity(struct media_device *media,
+					      unsigned int type);
+
+/**
  * @brief Get the media device information
  * @param media - media device.
  *
Signature · 1 line
1.8.3.2
#7 lore raw

PATCH v2 5/5 Add support for emulated devices

In reply to Laurent Pinchart

Emulated media devices are backed by real hardware devices for the functions they provide, but have no kernel media device counterpart.

---
 src/mediactl.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/mediactl.h | 52 +++++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+), 4 deletions(-)
src/mediactl.c +81 -4
index 5bf76ea..b904762 100644
@@ -636,7 +636,7 @@ void media_debug_set_handler(struct media_device *media,
 	}
 }
 
-struct media_device *media_device_new(const char *devnode)
+static struct media_device *__media_device_new(void)
 {
 	struct media_device *media;
 	int ret;
@@ -650,6 +650,18 @@ struct media_device *media_device_new(const char *devnode)
 
 	media_debug_set_handler(media, NULL, NULL);
 
+	return media;
+}
+
+struct media_device *media_device_new(const char *devnode)
+{
+	struct media_device *media;
+	int ret;
+
+	media = __media_device_new();
+	if (media == NULL)
+		return NULL;
+
 	media->devnode = strdup(devnode);
 	if (media->devnode == NULL) {
 		media_device_unref(media);
@@ -659,6 +671,19 @@ struct media_device *media_device_new(const char *devnode)
 	return media;
 }
 
+struct media_device *media_device_new_emulated(struct media_device_info *info)
+{
+	struct media_device *media;
+
+	media = __media_device_new();
+	if (media == NULL)
+		return NULL;
+
+	media->info = *info;
+
+	return media;
+}
+
 struct media_device *media_device_ref(struct media_device *media)
 {
 	media->refcount++;
@@ -687,9 +712,61 @@ void media_device_unref(struct media_device *media)
 	free(media);
 }
 
-/* -----------------------------------------------------------------------------
- * Parsing
- */
+int media_device_add_entity(struct media_device *media,
+			    const struct media_entity_desc *desc,
+			    const char *devnode)
+{
+	struct media_entity **defent;
+	struct media_entity *entity;
+	unsigned int size;
+
+	size = (media->entities_count + 1) * sizeof(*media->entities);
+	entity = realloc(media->entities, size);
+	if (entity == NULL)
+		return -ENOMEM;
+
+	media->entities = entity;
+	media->entities_count++;
+
+	entity = &media->entities[media->entities_count - 1];
+	memset(entity, 0, sizeof *entity);
+
+	entity->fd = -1;
+	entity->media = media;
+	strncpy(entity->devname, devnode, sizeof entity->devname);
+	entity->devname[sizeof entity->devname - 1] = '\0';
+
+	entity->info.id = 0;
+	entity->info.type = desc->type;
+	entity->info.flags = 0;
+	memcpy(entity->info.name, desc->name, sizeof entity->info.name);
+
+	switch (entity->info.type) {
+	case MEDIA_ENT_T_DEVNODE_V4L:
+		defent = &media->def.v4l;
+		entity->info.v4l = desc->v4l;
+		break;
+	case MEDIA_ENT_T_DEVNODE_FB:
+		defent = &media->def.fb;
+		entity->info.fb = desc->fb;
+		break;
+	case MEDIA_ENT_T_DEVNODE_ALSA:
+		defent = &media->def.alsa;
+		entity->info.alsa = desc->alsa;
+		break;
+	case MEDIA_ENT_T_DEVNODE_DVB:
+		defent = &media->def.dvb;
+		entity->info.dvb = desc->dvb;
+		break;
+	}
+
+	if (desc->flags & MEDIA_ENT_FL_DEFAULT) {
+		entity->info.flags |= MEDIA_ENT_FL_DEFAULT;
+		*defent = entity;
+	}
+
+	return 0;
+}
 
 struct media_pad *media_parse_pad(struct media_device *media,
 				  const char *p, char **endp)
src/mediactl.h +52 -0

64 lines not on this page · raw message

Signature · 1 line
1.8.3.2
Acked-by on the v2 series
#8 lore raw
In reply to Laurent Pinchart
Quoted 12 lines · Laurent Pinchart

Hello,

Here's the second version of the patch set that make libmediactl usable by the upcoming media device enumeration library. In order to do so I need to change the libmediactl API and add support for emulated media devices.

In order to avoid further API/ABI breakages I've decided to make the media_device and media_entity structures private and provide accessors for the fields that need to be read. I'm open to suggestions on whether I should make the media_pad and media_link structures private now as well, or on any other aspect of these changes.

Quoted 8 lines

For reference the (work in progress) media device enumeration library is available at

http://git.ideasonboard.org/media-enum.git

Changes since v1:

- Made struct media_entity private

For this patch series:

Thanks!

Hans

Quoted 16 lines

Laurent Pinchart (5):
Split media_device creation and opening
Make the media_device structure private
Make the media_entity structure private
Expose default devices
Add support for emulated devices

 src/main.c          | 137 +++++++++++++---------
 src/mediactl-priv.h |  64 ++++++++++
 src/mediactl.c      | 330 +++++++++++++++++++++++++++++++++++++++++++++-------
 src/mediactl.h      | 281 ++++++++++++++++++++++++++++++++++----------
 src/v4l2subdev.c    |   1 +
 5 files changed, 652 insertions(+), 161 deletions(-)
 create mode 100644 src/mediactl-priv.h