Reference counting to subdev file handles and media bus code enumeration
cover Reference counting to subdev file handles and media bus code enumeration
Hi,
This patchset adds reference counting to subdev file handles, which leaves it up to the user whether to keep the file handles open and thus the devices powered.
Media bus code enumeration is added by the two latter patches of which the second one makes the result sorted by codes.
Sakari Ailus (3):
Count users for entities
Implement v4l2_subdev_enum_mbus_code()
Sort enumerated media bus codes
src/mediactl.c | 1 - src/mediactl.h | 1 + src/v4l2subdev.c | 154 +++++++++++++++++++++++++++++++++++++++++++++--------- src/v4l2subdev.h | 32 ++++++++++-- 4 files changed, 158 insertions(+), 30 deletions(-)
Signature · 4 lines
Kind regards, Sakari Ailus [email protected]
PATCH 1/3 Count users for entities
The subdev nodes used to be closed immediately on v4l2_subdev_close(), now the subdev is only closed if there are no users left anymore. This changes the API from immediate effect (close) to a reference counting one.
Also make functions opening subdevs to close them before returning. This resolves issues on some machines where not all subdevs can be opened at the same time. Power management wise this is a sound choice since it forces to make the decision when to keep a device powered rather than keeping everything open all the time.
--- src/mediactl.c | 1 - src/mediactl.h | 1 + src/v4l2subdev.c | 94 ++++++++++++++++++++++++++++++++++++++++-------------- src/v4l2subdev.h | 12 ++++--- 4 files changed, 78 insertions(+), 30 deletions(-)
src/mediactl.c +0 -1
@@ -382,7 +382,6 @@ static int media_enum_entities(struct media_device *media) entity = &media->entities[media->entities_count];memset (entity,0 ,sizeof (*entity)); - entity->fd =-1 ; entity->info.id = id |MEDIA_ENT_ID_FLAG_NEXT ; entity->media = media;
src/mediactl.h +1 -0
@@ -49,6 +49,7 @@ struct media_entity {char devname[32 ];int fd; +unsigned int open_count;__u32 padding[6 ]; };
src/v4l2subdev.c +70 -24
174 lines not on this page · raw message
src/v4l2subdev.h +7 -5
@@ -30,19 +30,21 @@ struct media_entity;* @brief Open a sub-device. * @param entity - sub-device media entity. * -* Open the V4L2 subdev device node associated with @a entity. The file -* descriptor is stored in the media_entity structure. +* Open the V4L2 subdev device node associated with @a entity. The +* file descriptor is stored in the media_entity structure. The subdev +* will stay open until the last user has closed it using +* v4l2_subdev_close(), i.e. the calls are reference-counted. * * @return 0 on success, or a negative error code on failure. */ int v4l2_subdev_open (struct media_entity *entity);/** -* @brief Close a sub-device. +* @brief Release a sub-device. * @param entity - sub-device media entity. * -* Close the V4L2 subdev device node associated with the @a entity and opened by -* a previous call to v4l2_subdev_open() (either explicit or implicit). +* Releases a reference to a sub-device. The device will stay open +* until the last remaining user has released the device. */ void v4l2_subdev_close (struct media_entity *entity);
Signature · 1 line
1.7.10.4
PATCH 2/3 Implement v4l2_subdev_enum_mbus_code()
v4l2_subdev_enum_mbus_code() enumerates the media bus pixel codes on a pad of a given entity. The function returns a struct containing the array of enumerated pixel codes. Once the user no longer needs the array, its memory is released using free(3).
The decision not to store the media bus pixel codes in the library's internal data structures is an informed one: the configuration of the device could affect the selection of possible pixel codes on the device. One example of such is the chosen try pixel code on the sink pad which is known to affect the selection of available pixel codes on the source pad of the OMAP 3 ISP resizer.
--- src/v4l2subdev.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/v4l2subdev.h | 20 ++++++++++++++++++++ 2 files changed, 66 insertions(+)
src/v4l2subdev.c +46 -0
@@ -27,6 +27,7 @@#include <fcntl.h> #include <stdbool.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h>
@@ -64,6 +65,51 @@ void v4l2_subdev_close(struct media_entity *entity) } } +struct v4l2_subdev_mbus_codes * +v4l2_subdev_enum_mbus_code (struct media_entity *entity,unsigned int pad) +{ +struct v4l2_subdev_mbus_code_enum c; +struct v4l2_subdev_mbus_codes *codes =NULL ; +int ret; + + ret =v4l2_subdev_open (entity); +if (ret <0 ) +return NULL ; + +memset (&c,0 ,sizeof (c)); + c.pad = pad; + +for (; ; c.index++) { +void *tmp; + + ret =ioctl (entity->fd,VIDIOC_SUBDEV_ENUM_MBUS_CODE , &c); +if (ret ==-1 ) { +if (errno ==EINVAL ) +break ; +else + goto out_err; + } + + tmp =realloc (codes,sizeof (*codes) + + (c.index +1 ) *sizeof (codes->code[0 ])); +if (!tmp) + goto out_err; + codes = tmp; + + codes->code[c.index] = c.code; + } + + codes->ncode = c.index; + +v4l2_subdev_close (entity); +return codes; + +out_err: +v4l2_subdev_close (entity); +free (codes); +return NULL ; +} +int v4l2_subdev_get_format (struct media_entity *entity,struct v4l2_mbus_framefmt *format,unsigned int pad,enum v4l2_subdev_format_whence which)
src/v4l2subdev.h +20 -0
@@ -48,6 +48,26 @@ int v4l2_subdev_open(struct media_entity *entity);*/ void v4l2_subdev_close (struct media_entity *entity); +struct v4l2_subdev_mbus_codes { +unsigned int ncode; +unsigned int code[0 ]; +} __packed; + +/** +* @brief Enumerate mbus pixel codes. +* @param entity - subdev-device media entity. +* @param pad - the pad of the media entity +* +* Enumerate media bus pixel codes. This is just a wrapper for +* VIDIOC_SUBDEV_ENUM_MBUS_CODE IOCTL. +* +* @return NULL on error; otherwise struct v4l2_subdev_mbus_code_enum +* containing the media bus codes. The user MUST use free(3) to +* release the returned struct once the user no longer needs it. +*/ +struct v4l2_subdev_mbus_codes * +v4l2_subdev_enum_mbus_code (struct media_entity *entity,unsigned int pad); +/** * @brief Retrieve the format on a pad. * @param entity - subdev-device media entity.
Signature · 1 line
1.7.10.4
PATCH 3/3 Sort enumerated media bus codes
In reply to Sakari AilusSort the enumeration result. This makes e.g. matching formats at both ends of the link much easier.
--- src/v4l2subdev.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
src/v4l2subdev.c +14 -0
@@ -65,6 +65,19 @@ void v4l2_subdev_close(struct media_entity *entity) } } +static int mbus_code_sort (const void *p1,const void *p2) +{ +const unsigned int *c1 = p1, *c2 = p2; + +if (*c1 < *c2) +return -1 ; + +if (*c1 > *c2) +return 1 ; + +return 0 ; +} +struct v4l2_subdev_mbus_codes *v4l2_subdev_enum_mbus_code (struct media_entity *entity,unsigned int pad) {
@@ -100,6 +113,7 @@ v4l2_subdev_enum_mbus_code(struct media_entity *entity, unsigned int pad) } codes->ncode = c.index; +qsort (codes->code, codes->ncode,sizeof (*codes->code), &mbus_code_sort);v4l2_subdev_close (entity);return codes;
Signature · 1 line
1.7.10.4