Skip to contentSkip to conversation

media: vimc: use configfs in order to configure devices topologies

PATCH In review Dafna Hirschfeld 2 participants 5 messages Nov 26, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover media: vimc: use configfs in order to configure devices topologies

This patchset introduces the usage of configfs in order to create vimc devices with a configured topology. The default hardcoded device is removed and replaced with an API that allows userspace to configure the device's topology.

The patchset is tested with the following script that runs basic unit tests:
https://gitlab.collabora.com/dafna/scripts/blob/master/unit-tests.sh

I also tested it with two scripts that test concurrency:
- https://gitlab.collabora.com/dafna/scripts/blob/master/stream-tests.sh
This creates a sensor->debayer->scaler->capture device. Then one function runs in the background and constantly tries to stream the capture while different entities are removed and added again.
- https://gitlab.collabora.com/dafna/scripts/blob/master/configfs-conc.sh
This scripts adds and removes entities and links while plugging and unplugging the device in the backgroung.

The patchset is rebased on v3 of the patchset media: vimc: crash fix - add refcount to vimc entities

In the configfs API, entities are represented by directories and links between entities are represented by symlinks.

For example, a topology of sensor->capture will be created with the following commands:

CONFIGFS_ROOT=/sys/kernel/config

mkdir ${CONFIGFS_ROOT}/vimc/mdev/
mkdir ${CONFIGFS_ROOT}/vimc/mdev/vimc-sensor:sen
mkdir ${CONFIGFS_ROOT}/vimc/mdev/vimc-capture:cap
mkdir ${CONFIGFS_ROOT}/vimc/mdev/vimc-capture:cap/sink:0/sen-to-cap
ln -s "${CONFIGFS_ROOT}/vimc/mdev/vimc-capture:cap/sink:0/sen-to-cap" "${CONFIGFS_ROOT}/vimc/mdev/vimc-sensor:sen/source:0/"

tree ${CONFIGFS_ROOT}
`-- vimc

    `-- mdev
        |-- hotplug
        |-- vimc-capture:cap
        |   `-- sink:0
        |       `-- sen-to-cap
        |           `-- type
        `-- vimc-sensor:sen
            `-- source:0
                `-- sen-to-cap -> ../../../../../vimc/mdev/cap/sink:0/sen-to-cap

The file type is the type attribute of the link, it can be one of 'enabled' 'disabled' or 'immutable'. By writing to the hotplug file, usrspace can plug and unplug the device. The device can also be unplugged while it streams. The directories sink:0 and source:0 are default subdirectories which are always present and represent the entity's pads.

If userspace reconfigures the topology while the device is plugged, the changes will not
take effect until userspace unplugs and replugs the device. The way configfs API is used here can be compared to a configuration file: changes in the file will only take effect when the software is reloaded.

Code Design:
There are two separate lists for the entities. One list is maintained only by the configfs and each
object in the list contains only the name and type of the entity. Only when userspace asks to plug the device, the configfs list is passed to the driver's probe callback through a platform_data object. The driver then iterates it and creates its own list of allocated vimc_ent_device objects and registers them with the media controller. There is one list in the configfs that holds the links between entities which is also passed to the driver through the platform_data object. The driver iterates it and calls media_create_pad_link for each link.
There are two mutexes used for code synchronization: pdev_mutex is used to lock the platform_data while plugging and unplugging. topology_mutex is used to lock the the configfs lists of the entities and the links. The default directories for the pads sink:<pad> source:<pad> are created in configfs by a callback function that each vimc entity registers in init (when loading vimc.ko).

There is currently a bug when trying to open a subdevice while it is unregistered - The function subdev_open tries to reference sd->v4l2_dev while it is set to null in v4l2_device_unregister_subdev, this can be reproduced with the script and the program:

http://ix.io/22P6
http://ix.io/22P5

This bug also occurs when unregistering the device through configfs while trying to open a subdevice node.

Changes from v2:
- rebase on top of v3 of media: vimc: crash fix - add refcount to vimc entities instead of v1
- fixes in patch 1 due to comments from Andrzej Pietrasiewicz
- bug fix - ensure that an entity can't have a link to itself - this cause a kernel crash

Patch 1 - is the new code for the configfs API. This only adds the new code but does not use it so the device still has the hardcoded topology and no configfs registration.
Patch 2 - removes the hardcoded topology and adds the usage of the configfs. This also contains the documentation of the new api.
Patch 3 - adds support to register up to 64 vimc devices at the same time by adding a device index to the bus_info.

Dafna Hirschfeld (3):
media: vimc: Add the implementation for the configfs api
media: vimc: use configfs instead of having hardcoded configuration
media: vimc: Add device index to the bus_info

 Documentation/ABI/testing/configfs-vimc     |   6 +
 Documentation/media/v4l-drivers/vimc.dot    |  28 +-
 Documentation/media/v4l-drivers/vimc.rst    | 296 ++++++--
 drivers/media/platform/vimc/Kconfig         |  10 +-
 drivers/media/platform/vimc/Makefile        |   2 +-
 drivers/media/platform/vimc/vimc-capture.c  |  37 +-
 drivers/media/platform/vimc/vimc-common.h   |  92 ++-
 drivers/media/platform/vimc/vimc-configfs.c | 720 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-configfs.h |  41 ++
 drivers/media/platform/vimc/vimc-core.c     | 383 ++++++-----
 drivers/media/platform/vimc/vimc-debayer.c  |  26 +-
 drivers/media/platform/vimc/vimc-scaler.c   |  25 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  25 +-
 13 files changed, 1401 insertions(+), 290 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-vimc
 create mode 100644 drivers/media/platform/vimc/vimc-configfs.c
 create mode 100644 drivers/media/platform/vimc/vimc-configfs.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/3 media: vimc: Add the implementation for the configfs api

Add the code that implements the usage of configfs in order to create and configure a device topology from userspace. The code is only added in this patch but is not used. It will be used in next patch in the series.

[refactored for upstream]
---
 drivers/media/platform/vimc/Kconfig         |   2 +-
 drivers/media/platform/vimc/Makefile        |   2 +-
 drivers/media/platform/vimc/vimc-capture.c  |  21 +
 drivers/media/platform/vimc/vimc-common.h   |  64 ++
 drivers/media/platform/vimc/vimc-configfs.c | 720 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-configfs.h |  41 ++
 drivers/media/platform/vimc/vimc-debayer.c  |  22 +
 drivers/media/platform/vimc/vimc-scaler.c   |  22 +
 drivers/media/platform/vimc/vimc-sensor.c   |  21 +
 9 files changed, 913 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-configfs.c
 create mode 100644 drivers/media/platform/vimc/vimc-configfs.h
drivers/media/platform/vimc/Kconfig +1 -1
index bd221d3e1a4a..6e292f19e859 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config VIDEO_VIMC
 	tristate "Virtual Media Controller Driver (VIMC)"
-	depends on VIDEO_DEV && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+	depends on VIDEO_DEV && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && CONFIGFS_FS
 	select VIDEOBUF2_VMALLOC
 	select VIDEO_V4L2_TPG
 	help
drivers/media/platform/vimc/Makefile +1 -1
index a53b2b532e9f..eb03d487f308 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 vimc-y := vimc-core.o vimc-common.o vimc-streamer.o vimc-capture.o \
-		vimc-debayer.o vimc-scaler.o vimc-sensor.o
+		vimc-debayer.o vimc-scaler.o vimc-sensor.o  vimc-configfs.o
 
 obj-$(CONFIG_VIDEO_VIMC) += vimc.o
 
drivers/media/platform/vimc/vimc-capture.c +21 -0
index c5a645f98c66..ab9d9d93b241 100644
@@ -9,6 +9,7 @@
 #include <media/videobuf2-core.h>
 #include <media/videobuf2-vmalloc.h>
 
+#include "vimc-configfs.h"
 #include "vimc-common.h"
 #include "vimc-streamer.h"
 
@@ -478,3 +479,23 @@ struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
 
 	return NULL;
 }
+
+static void vimc_cap_create_cfs_pads(struct config_group *ent_group)
+{
+	vimc_cfs_add_sink_pad(ent_group, 0, VIMC_CFS_SINK_PAD_NUM(0));
+}
+
+struct vimc_cfs_ent_type vimc_cap_cfs_ent_type = {
+	.name = VIMC_CAP_NAME,
+	.create_pads = vimc_cap_create_cfs_pads,
+};
+
+void vimc_cap_exit(void)
+{
+	vimc_cfs_ent_type_unregister(&vimc_cap_cfs_ent_type);
+}
+
+void vimc_cap_init(void)
+{
+	vimc_cfs_ent_type_register(&vimc_cap_cfs_ent_type);
+}
drivers/media/platform/vimc/vimc-common.h +64 -0

109 lines not on this page · raw message

drivers/media/platform/vimc/vimc-configfs.c new file +720 -0

720 lines not on this page · raw message

drivers/media/platform/vimc/vimc-configfs.h new file +41 -0

41 lines not on this page · raw message

drivers/media/platform/vimc/vimc-debayer.c +22 -0
index 3beec7f95b47..48a868c9e9f1 100644
@@ -14,6 +14,7 @@
 #include <media/v4l2-subdev.h>
 
 #include "vimc-common.h"
+#include "vimc-configfs.h"
 
 enum vimc_deb_rgb_colors {
 	VIMC_DEB_RED = 0,
@@ -587,3 +588,24 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
 
 	return NULL;
 }
+
+static void vimc_deb_create_cfs_pads(struct config_group *ent_group)
+{
+	vimc_cfs_add_source_pad(ent_group, 0, VIMC_CFS_SRC_PAD_NUM(1));
+	vimc_cfs_add_sink_pad(ent_group, 1, VIMC_CFS_SINK_PAD_NUM(0));
+}
+
+struct vimc_cfs_ent_type vimc_deb_cfs_ent_type = {
+	.name = VIMC_DEB_NAME,
+	.create_pads = vimc_deb_create_cfs_pads,
+};
+
+void vimc_deb_exit(void)
+{
+	vimc_cfs_ent_type_unregister(&vimc_deb_cfs_ent_type);
+}
+
+void vimc_deb_init(void)
+{
+	vimc_cfs_ent_type_register(&vimc_deb_cfs_ent_type);
+}
drivers/media/platform/vimc/vimc-scaler.c +22 -0
index 2d1415b97ff8..577b81312658 100644
@@ -10,6 +10,7 @@
 #include <linux/v4l2-mediabus.h>
 #include <media/v4l2-subdev.h>
 
+#include "vimc-configfs.h"
 #include "vimc-common.h"
 
 static unsigned int sca_mult = 3;
@@ -381,3 +382,24 @@ struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
 
 	return &vsca->ved;
 }
+
+static void vimc_sca_create_cfs_pads(struct config_group *ent_group)
+{
+	vimc_cfs_add_source_pad(ent_group, 0, VIMC_CFS_SRC_PAD_NUM(1));
+	vimc_cfs_add_sink_pad(ent_group, 1, VIMC_CFS_SINK_PAD_NUM(0));
+}
+
+struct vimc_cfs_ent_type vimc_sca_cfs_ent_type = {
+	.name = VIMC_SCA_NAME,
+	.create_pads = vimc_sca_create_cfs_pads,
+};
+
+void vimc_sca_exit(void)
+{
+	vimc_cfs_ent_type_unregister(&vimc_sca_cfs_ent_type);
+}
+
+void vimc_sca_init(void)
+{
+	vimc_cfs_ent_type_register(&vimc_sca_cfs_ent_type);
+}
drivers/media/platform/vimc/vimc-sensor.c +21 -0
index 14eeaf461e93..b3f8730b2598 100644
@@ -12,6 +12,7 @@
 #include <media/v4l2-subdev.h>
 #include <media/tpg/v4l2-tpg.h>
 
+#include "vimc-configfs.h"
 #include "vimc-common.h"
 
 struct vimc_sen_device {
@@ -382,3 +383,23 @@ struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
 
 	return NULL;
 }
+
+static void vimc_sen_create_cfs_pads(struct config_group *ent_group)
+{
+	vimc_cfs_add_source_pad(ent_group, 0, VIMC_CFS_SRC_PAD_NUM(0));
+}
+
+struct vimc_cfs_ent_type vimc_sen_cfs_ent_type = {
+	.name = VIMC_SEN_NAME,
+	.create_pads = vimc_sen_create_cfs_pads,
+};
+
+void vimc_sen_exit(void)
+{
+	vimc_cfs_ent_type_unregister(&vimc_sen_cfs_ent_type);
+}
+
+void vimc_sen_init(void)
+{
+	vimc_cfs_ent_type_register(&vimc_sen_cfs_ent_type);
+}
Signature · 1 line
2.20.1
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] media: vimc: Add the implementation for the configfs api

Hi Dafna,

Thank you for the patch,

One more comment and few nitpicks follow.

With that addressed you can add

W dniu 26.11.2019 o 13:10, Dafna Hirschfeld pisze:

Quoted 5 lines

Add the code that implements the usage of configfs in order to create and configure a device topology from userspace. The code is only added in this patch but is not used. It will be used in next patch in the series.

<snip>

drivers/media/platform/vimc/vimc-capture.c struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc, · line 491

Quoted 4 lines
drivers/media/platform/vimc/vimc-capture.c +4 -0
+struct vimc_cfs_ent_type vimc_cap_cfs_ent_type = {
+	.name = VIMC_CAP_NAME,
+	.create_pads = vimc_cap_create_cfs_pads,
+};

You have some instances of struct vimc_cfs_ent_type, but at the same time you have an instance of struct config_item_type called vimc_cfs_ent_type.
I haven't noticed it earlier, this is confusing.

<snip>

Quoted 6 lines
drivers/media/platform/vimc/vimc-configfs.c new file +0 -0
new file mode 100644
index 000000000000..81e6be5b30c5
@@ -0,0 +1,720 @@

<snip>

drivers/media/platform/vimc/vimc-configfs.c line 73

Quoted 12 lines
drivers/media/platform/vimc/vimc-configfs.c new file +12 -0
+static struct vimc_cfs_subsystem {
+	struct configfs_subsystem subsys;
+	struct list_head ent_types;
+} vimc_cfs_subsys = {
+	.subsys = {
+		.su_group = {
+			.cg_item = {
+				.ci_namebuf = CFS_SUBSYS_NAME,
+				.ci_type = &vimc_cfs_subsys_type,
+			},
+		},
+		.su_mutex = __MUTEX_INITIALIZER(vimc_cfs_subsys.subsys.su_mutex),

A newline and tabs to fit 80 chars limit?

		.su_mutex =
			__MUTEX_INITIALIZER(vimc_cfs_subsys.subsys.su_mutex),

<snip>

Quoted 55 lines
drivers/media/platform/vimc/vimc-configfs.c new file +55 -0
+
+static struct config_group *vimc_cfs_dev_make_ent_group(
+			struct config_group *group, const char *name)
+{
+	struct vimc_cfs_device *cfs =
+		container_of(group, struct vimc_cfs_device, gdev);
+	char *type_name, *ent_name, *sep;
+	struct vimc_cfs_ent *c_ent;
+	struct vimc_entity_data *ent;
+	struct vimc_cfs_ent_type *c_ent_type = NULL;
+	struct vimc_cfs_ent_type *found_ent_type = NULL;
+	char buf[VIMC_MAX_CFS_NAME_LEN];
+
+	cg_dbg(group, "trying to make entity '%s'\n", name);
+	if (snprintf(buf, VIMC_MAX_CFS_NAME_LEN, "%s", name) >= sizeof(buf))
+		return ERR_PTR(-ENAMETOOLONG);
+
+	/* Parse format "type_name:ent_name" */
+	sep = strchr(buf, CHAR_SEPARATOR);
+	if (!sep) {
+		cg_warn(&cfs->gdev,
+			"Could not find separator '%c'\n", CHAR_SEPARATOR);
+		goto syntax_error;
+	}
+	*sep = '\0';
+
+	ent_name = &sep[1];
+	type_name = buf;
+
+	if (!*ent_name || sep == type_name) {
+		cg_warn(&cfs->gdev,
+			"%s: Driver name and entity name can't be empty.\n",
+			name);
+		goto syntax_error;
+	}
+	if (strlen(ent_name) >= VIMC_MAX_NAME_LEN) {
+		cg_err(&cfs->gdev,
+		       "%s: Driver name length should be less than %u.\n",
+		       name, VIMC_MAX_NAME_LEN);
+		goto syntax_error;
+	}
+	mutex_lock(&cfs->pdata.topology_mutex);
+	list_for_each_entry(ent, &cfs->pdata.ents, entry) {
+		if (!strncmp(ent->name, ent_name, sizeof(ent->name))) {
+			cg_err(&cfs->gdev, "entity `%s` already exist\n",
+			       ent->name);
+			mutex_unlock(&cfs->pdata.topology_mutex);
+			goto syntax_error;
+		}
+	}
+
+	c_ent = kzalloc(sizeof(*c_ent), GFP_KERNEL);
+	if (!c_ent) {
+		mutex_unlock(&cfs->pdata.topology_mutex);
+		return ERR_PTR(-ENOMEM);

drivers/media/platform/vimc/vimc-configfs.c line 580

Quoted 10 lines
drivers/media/platform/vimc/vimc-configfs.c new file +10 -0
+	}
+
+	strscpy(c_ent->ent.name, ent_name, sizeof(c_ent->ent.name));
+
+	list_for_each_entry(c_ent_type, &vimc_cfs_subsys.ent_types, entry) {
+		if (!strcmp(type_name, c_ent_type->name)) {
+			found_ent_type = c_ent_type;
+			break;
+		}
+	}

list_for_each_entry() ultimately expands to a "for" loop, and inside it you only have a single statement (the "if") so the outer braces seem redundant.

#3 lore raw

PATCH v3 2/3 media: vimc: use configfs instead of having hardcoded configuration

In reply to Dafna Hirschfeld

Use configfs in order to create a device and set its topology and remove the hardcoded device.

[refactored for upstream]
---
 Documentation/ABI/testing/configfs-vimc    |   6 +
 Documentation/media/v4l-drivers/vimc.dot   |  28 +-
 Documentation/media/v4l-drivers/vimc.rst   | 296 +++++++++++++----
 drivers/media/platform/vimc/Kconfig        |   8 +-
 drivers/media/platform/vimc/vimc-capture.c |   8 +-
 drivers/media/platform/vimc/vimc-common.h  |  26 +-
 drivers/media/platform/vimc/vimc-core.c    | 361 ++++++++++-----------
 drivers/media/platform/vimc/vimc-debayer.c |   4 +-
 drivers/media/platform/vimc/vimc-scaler.c  |   3 +-
 drivers/media/platform/vimc/vimc-sensor.c  |   4 +-
 10 files changed, 456 insertions(+), 288 deletions(-)
 create mode 100644 Documentation/ABI/testing/configfs-vimc
Documentation/ABI/testing/configfs-vimc new file +6 -0
new file mode 100644
index 000000000000..101af83eb249
@@ -0,0 +1,6 @@
+What:		/config/vimc
+Date:		November 2019
+KernelVersion:	//TODO
+Contact:	[email protected]
+Description:
+		Allow userspace to configure a vimc device
Documentation/media/v4l-drivers/vimc.dot +11 -17
index 57863a13fa39..e3b41ac2bc46 100644
@@ -2,21 +2,15 @@
 
 digraph board {
 	rankdir=TB
-	n00000001 [label="{{} | Sensor A\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000001:port0 -> n00000005:port0 [style=bold]
-	n00000001:port0 -> n0000000b [style=bold]
-	n00000003 [label="{{} | Sensor B\n/dev/v4l-subdev1 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000003:port0 -> n00000008:port0 [style=bold]
-	n00000003:port0 -> n0000000f [style=bold]
-	n00000005 [label="{{<port0> 0} | Debayer A\n/dev/v4l-subdev2 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000005:port1 -> n00000017:port0
-	n00000008 [label="{{<port0> 0} | Debayer B\n/dev/v4l-subdev3 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000008:port1 -> n00000017:port0 [style=dashed]
-	n0000000b [label="Raw Capture 0\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
-	n0000000f [label="Raw Capture 1\n/dev/video1", shape=box, style=filled, fillcolor=yellow]
-	n00000013 [label="RGB/YUV Input\n/dev/video2", shape=box, style=filled, fillcolor=yellow]
-	n00000013 -> n00000017:port0 [style=dashed]
-	n00000017 [label="{{<port0> 0} | Scaler\n/dev/v4l-subdev4 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000017:port1 -> n0000001a [style=bold]
-	n0000001a [label="RGB/YUV Capture\n/dev/video3", shape=box, style=filled, fillcolor=yellow]
+	n00000001 [label="cap-deb\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n00000005 [label="cap-sen\n/dev/video1", shape=box, style=filled, fillcolor=yellow]
+	n00000009 [label="cap-sca\n/dev/video2", shape=box, style=filled, fillcolor=yellow]
+	n0000000d [label="{{<port0> 0} | sca\n/dev/v4l-subdev0 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000d:port1 -> n00000009 [style=bold]
+	n00000010 [label="{{<port0> 0} | deb\n/dev/v4l-subdev1 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000010:port1 -> n00000001 [style=bold]
+	n00000010:port1 -> n0000000d:port0 [style=bold]
+	n00000013 [label="{{} | sen\n/dev/v4l-subdev2 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000013:port0 -> n00000005 [style=bold]
+	n00000013:port0 -> n00000010:port0 [style=bold]
 }
Documentation/media/v4l-drivers/vimc.rst +237 -59

336 lines not on this page · raw message

drivers/media/platform/vimc/Kconfig +3 -5
index 6e292f19e859..24eb5d661f4f 100644
@@ -5,11 +5,9 @@ config VIDEO_VIMC
 	select VIDEOBUF2_VMALLOC
 	select VIDEO_V4L2_TPG
 	help
-	  Skeleton driver for Virtual Media Controller
+	  Virtual Media Controller Driver
 
-	  This driver can be compared to the vivid driver for emulating
-	  a media node that exposes a complex media topology. The topology
-	  is hard coded for now but is meant to be highly configurable in
-	  the future.
+	  This driver emulates a media node that exposes a complex media topology.
+	  The topology is configurable through the configfs API.
 
 	  When in doubt, say N.
drivers/media/platform/vimc/vimc-capture.c +5 -3
index ab9d9d93b241..51772cc79508 100644
@@ -396,10 +396,12 @@ struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
 	/* Allocate the vimc_cap_device struct */
 	vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
 	if (!vcap)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
+
+	strscpy(vcap->ved.data.name, vcfg_name, sizeof(vcap->ved.data.name));
 
 	/* Initialize the media entity */
-	vcap->vdev.entity.name = vcfg_name;
+	vcap->vdev.entity.name = vcap->ved.data.name;
 	vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
 	vcap->pad.flags = MEDIA_PAD_FL_SINK;
 	ret = media_entity_pads_init(&vcap->vdev.entity,
@@ -477,7 +479,7 @@ struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
 err_free_vcap:
 	kfree(vcap);
 
-	return NULL;
+	return ERR_PTR(ret);
 }
 
 static void vimc_cap_create_cfs_pads(struct config_group *ent_group)
drivers/media/platform/vimc/vimc-common.h +14 -12
index 228f1354d766..b47ff58553db 100644
@@ -157,21 +157,21 @@ struct vimc_ent_device {
 				const void *frame);
 	void (*vdev_get_format)(struct vimc_ent_device *ved,
 			      struct v4l2_pix_format *fmt);
+	struct vimc_entity_data data;
 };
 
 /**
  * struct vimc_device - main device for vimc driver
  *
- * @pipe_cfg	pointer to the vimc pipeline configuration structure
- * @ent_devs	array of vimc_ent_device pointers
  * @mdev	the associated media_device parent
  * @v4l2_dev	Internal v4l2 parent device
+ * @ents	list of vimc_ent_device objects
  */
 struct vimc_device {
-	const struct vimc_pipeline_config *pipe_cfg;
-	struct vimc_ent_device **ent_devs;
 	struct media_device mdev;
 	struct v4l2_device v4l2_dev;
+
+	struct list_head ents;
 };
 
 /**
@@ -179,20 +179,22 @@ struct vimc_device {
  *				configuration for each entity
  *
  * @name			entity name
- * @ved				pointer to vimc_ent_device (a node in the
- *					topology)
- * @add				initializes and registers
- *					vim entity - called from vimc-core
- * @unregister			unregisters vimc entity - called from vimc-core
+ * @add				initializes and registers vimc entity
+ * @unregister			unregisters vimc entity
  * @release			releases vimc entity - called from the v4l2_dev
  *					release callback
+ * @init			called on driver init, used to register to configfs
+ * @exit			called on driver exit, used to unregister from configfs
  */
-struct vimc_ent_config {
-	const char *name;
-	struct vimc_ent_device *(*add)(struct vimc_device *vimc,
+
+struct vimc_ent_type {
+	const char name[VIMC_MAX_NAME_LEN];
+	struct vimc_ent_device*	(*add)(struct vimc_device *vimc,
 				       const char *vcfg_name);
 	void (*unregister)(struct vimc_ent_device *ved);
 	void (*release)(struct vimc_ent_device *ved);
+	void (*init)(void);
+	void (*exit)(void);
 };
 
 /**
drivers/media/platform/vimc/vimc-core.c +175 -186

508 lines not on this page · raw message

drivers/media/platform/vimc/vimc-debayer.c +2 -2
index 48a868c9e9f1..a1f22714458f 100644
@@ -541,7 +541,7 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
 	/* Allocate the vdeb struct */
 	vdeb = kzalloc(sizeof(*vdeb), GFP_KERNEL);
 	if (!vdeb)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	/* Create controls: */
 	v4l2_ctrl_handler_init(&vdeb->hdl, 2);
@@ -586,7 +586,7 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
 err_free_vdeb:
 	kfree(vdeb);
 
-	return NULL;
+	return ERR_PTR(ret);
 }
 
 static void vimc_deb_create_cfs_pads(struct config_group *ent_group)
drivers/media/platform/vimc/vimc-scaler.c +1 -2
index 577b81312658..8ab249fa09eb 100644
@@ -359,7 +359,7 @@ struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
 	/* Allocate the vsca struct */
 	vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
 	if (!vsca)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	/* Initialize ved and sd */
 	vsca->pads[0].flags = MEDIA_PAD_FL_SINK;
@@ -376,7 +376,6 @@ struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
 
 	vsca->ved.process_frame = vimc_sca_process_frame;
 	vsca->ved.dev = vimc->mdev.dev;
-
 	/* Initialize the frame format */
 	vsca->sink_fmt = sink_fmt_default;
 
drivers/media/platform/vimc/vimc-sensor.c +2 -2
index b3f8730b2598..e22ee571aa79 100644
@@ -326,7 +326,7 @@ struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
 	/* Allocate the vsen struct */
 	vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
 	if (!vsen)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	v4l2_ctrl_handler_init(&vsen->hdl, 4);
 
@@ -381,7 +381,7 @@ struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
 err_free_vsen:
 	kfree(vsen);
 
-	return NULL;
+	return ERR_PTR(ret);
 }
 
 static void vimc_sen_create_cfs_pads(struct config_group *ent_group)
Signature · 1 line
2.20.1
#4 lore raw

PATCH v3 3/3 media: vimc: Add device index to the bus_info

In reply to Dafna Hirschfeld

Use a bitmask to get the next available device index to be used for the bus_info field in the v4l2_capability struct in the VIDIOC_QUERYCAP ioctl
The bitmask is a global variable in vimc-core.c. It is set on probe and unset when a vimc device is unregisered.

---
 drivers/media/platform/vimc/vimc-capture.c |  8 +++++--
 drivers/media/platform/vimc/vimc-common.h  |  2 ++
 drivers/media/platform/vimc/vimc-core.c    | 28 +++++++++++++++++++++-
 3 files changed, 35 insertions(+), 3 deletions(-)
drivers/media/platform/vimc/vimc-capture.c +6 -2
index 51772cc79508..31490d39a3a6 100644
@@ -31,6 +31,7 @@ struct vimc_cap_device {
 	u32 sequence;
 	struct vimc_stream stream;
 	struct media_pad pad;
+	char bus_info[32];
 };
 
 static const struct v4l2_pix_format fmt_default = {
@@ -55,10 +56,12 @@ struct vimc_cap_buffer {
 static int vimc_cap_querycap(struct file *file, void *priv,
 			     struct v4l2_capability *cap)
 {
+	struct vimc_cap_device *vcap = video_drvdata(file);
+
 	strscpy(cap->driver, VIMC_PDEV_NAME, sizeof(cap->driver));
 	strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
-	snprintf(cap->bus_info, sizeof(cap->bus_info),
-		 "platform:%s", VIMC_PDEV_NAME);
+	strscpy(cap->bus_info, vcap->bus_info, sizeof(cap->bus_info));
+
 
 	return 0;
 }
@@ -447,6 +450,7 @@ struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
 	vcap->ved.process_frame = vimc_cap_process_frame;
 	vcap->ved.vdev_get_format = vimc_cap_get_format;
 	vcap->ved.dev = vimc->mdev.dev;
+	strscpy(vcap->bus_info, vimc->mdev.bus_info, sizeof(vcap->bus_info));
 
 	/* Initialize the video_device struct */
 	vdev = &vcap->vdev;
drivers/media/platform/vimc/vimc-common.h +2 -0
index b47ff58553db..3e6df17456c6 100644
@@ -164,12 +164,14 @@ struct vimc_ent_device {
  * struct vimc_device - main device for vimc driver
  *
  * @mdev	the associated media_device parent
+ * @idx		the index of the device, used for info_bus
  * @v4l2_dev	Internal v4l2 parent device
  * @ents	list of vimc_ent_device objects
  */
 struct vimc_device {
 	struct media_device mdev;
 	struct v4l2_device v4l2_dev;
+	unsigned int idx;
 
 	struct list_head ents;
 };
drivers/media/platform/vimc/vimc-core.c +27 -1
index 7431fd980f45..50aeb9cfcb43 100644
@@ -14,9 +14,15 @@
 #include "vimc-common.h"
 
 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
+#define VIMC_MAX_DEVS 64
 
 #include "vimc-configfs.h"
 
+
+
+static DECLARE_BITMAP(used, VIMC_MAX_DEVS);
+static DEFINE_MUTEX(vimc_dev_idx_lock);
+
 static const struct vimc_ent_type ent_types[] = {
 	{
 		.name = VIMC_SEN_NAME,
@@ -280,14 +286,26 @@ static int vimc_probe(struct platform_device *pdev)
 						pdev->dev.platform_data;
 	struct vimc_device *vimc;
 	int ret;
+	unsigned int idx;
 
 	dev_dbg(&pdev->dev, "probe\n");
 
 	vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
 	if (!vimc)
 		return -ENOMEM;
+	mutex_lock(&vimc_dev_idx_lock);
+	idx = find_first_zero_bit(used, VIMC_MAX_DEVS);
+	if (idx == VIMC_MAX_DEVS) {
+		mutex_unlock(&vimc_dev_idx_lock);
+		dev_err(&pdev->dev, "there are already %u devs which is the max allowed\n",
+			VIMC_MAX_DEVS);
+		return -EBUSY;
+	}
+	set_bit(idx, used);
+	mutex_unlock(&vimc_dev_idx_lock);
 
 	INIT_LIST_HEAD(&vimc->ents);
+	vimc->idx = idx;
 
 	/* Link the media device within the v4l2_device */
 	vimc->v4l2_dev.mdev = &vimc->mdev;
@@ -296,7 +314,8 @@ static int vimc_probe(struct platform_device *pdev)
 	strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
 		sizeof(vimc->mdev.model));
 	snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
-		 "platform:%s", VIMC_PDEV_NAME);
+		 "platform:%s-%03u", VIMC_PDEV_NAME, idx);
+
 	vimc->mdev.dev = &pdev->dev;
 	media_device_init(&vimc->mdev);
 
@@ -304,6 +323,9 @@ static int vimc_probe(struct platform_device *pdev)
 	if (ret) {
 		media_device_cleanup(&vimc->mdev);
 		kfree(vimc);
+		mutex_lock(&vimc_dev_idx_lock);
+		clear_bit(idx, used);
+		mutex_unlock(&vimc_dev_idx_lock);
 		return ret;
 	}
 	/*
@@ -319,12 +341,16 @@ static int vimc_probe(struct platform_device *pdev)
 static int vimc_remove(struct platform_device *pdev)
 {
 	struct vimc_device *vimc = platform_get_drvdata(pdev);
+	unsigned long idx = vimc->idx;
 
 	dev_dbg(&pdev->dev, "remove\n");
 
 	vimc_unregister(vimc);
 	v4l2_device_put(&vimc->v4l2_dev);
 
+	mutex_lock(&vimc_dev_idx_lock);
+	clear_bit(idx, used);
+	mutex_unlock(&vimc_dev_idx_lock);
 	return 0;
 }
 
Signature · 1 line
2.20.1