Commit b76a2a8c authored by Laurent Pinchart's avatar Laurent Pinchart Committed by Mauro Carvalho Chehab

[media] media: Add obj_type field to struct media_entity

Code that processes media entities can require knowledge of the
structure type that embeds a particular media entity instance in order
to cast the entity to the proper object type. This needs is shown by the
presence of the is_media_entity_v4l2_io and is_media_entity_v4l2_subdev
functions.

The implementation of those two functions relies on the entity function
field, which is both a wrong and an inefficient design, without even
mentioning the maintenance issue involved in updating the functions
every time a new entity function is added. Fix this by adding add an
obj_type field to the media entity structure to carry the information.
Signed-off-by: default avatarLaurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
Acked-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
Acked-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent 1d260123
...@@ -735,6 +735,7 @@ static int video_register_media_controller(struct video_device *vdev, int type) ...@@ -735,6 +735,7 @@ static int video_register_media_controller(struct video_device *vdev, int type)
if (!vdev->v4l2_dev->mdev) if (!vdev->v4l2_dev->mdev)
return 0; return 0;
vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
vdev->entity.function = MEDIA_ENT_F_UNKNOWN; vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
switch (type) { switch (type) {
......
...@@ -584,6 +584,7 @@ void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) ...@@ -584,6 +584,7 @@ void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
sd->host_priv = NULL; sd->host_priv = NULL;
#if defined(CONFIG_MEDIA_CONTROLLER) #if defined(CONFIG_MEDIA_CONTROLLER)
sd->entity.name = sd->name; sd->entity.name = sd->name;
sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
#endif #endif
} }
......
...@@ -187,11 +187,39 @@ struct media_entity_operations { ...@@ -187,11 +187,39 @@ struct media_entity_operations {
int (*link_validate)(struct media_link *link); int (*link_validate)(struct media_link *link);
}; };
/**
* enum media_entity_type - Media entity type
*
* @MEDIA_ENTITY_TYPE_BASE:
* The entity isn't embedded in another subsystem structure.
* @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
* The entity is embedded in a struct video_device instance.
* @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
* The entity is embedded in a struct v4l2_subdev instance.
*
* Media entity objects are often not instantiated directly, but the media
* entity structure is inherited by (through embedding) other subsystem-specific
* structures. The media entity type identifies the type of the subclass
* structure that implements a media entity instance.
*
* This allows runtime type identification of media entities and safe casting to
* the correct object type. For instance, a media entity structure instance
* embedded in a v4l2_subdev structure instance will have the type
* MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a v4l2_subdev
* structure using the container_of() macro.
*/
enum media_entity_type {
MEDIA_ENTITY_TYPE_BASE,
MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
};
/** /**
* struct media_entity - A media entity graph object. * struct media_entity - A media entity graph object.
* *
* @graph_obj: Embedded structure containing the media object common data. * @graph_obj: Embedded structure containing the media object common data.
* @name: Entity name. * @name: Entity name.
* @obj_type: Type of the object that implements the media_entity.
* @function: Entity main function, as defined in uapi/media.h * @function: Entity main function, as defined in uapi/media.h
* (MEDIA_ENT_F_*) * (MEDIA_ENT_F_*)
* @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*) * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
...@@ -220,6 +248,7 @@ struct media_entity_operations { ...@@ -220,6 +248,7 @@ struct media_entity_operations {
struct media_entity { struct media_entity {
struct media_gobj graph_obj; /* must be first field in struct */ struct media_gobj graph_obj; /* must be first field in struct */
const char *name; const char *name;
enum media_entity_type obj_type;
u32 function; u32 function;
unsigned long flags; unsigned long flags;
...@@ -329,56 +358,29 @@ static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id) ...@@ -329,56 +358,29 @@ static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
} }
/** /**
* is_media_entity_v4l2_io() - identify if the entity main function * is_media_entity_v4l2_io() - Check if the entity is a video_device
* is a V4L2 I/O
*
* @entity: pointer to entity * @entity: pointer to entity
* *
* Return: true if the entity main function is one of the V4L2 I/O types * Return: true if the entity is an instance of a video_device object and can
* (video, VBI or SDR radio); false otherwise. * safely be cast to a struct video_device using the container_of() macro, or
* false otherwise.
*/ */
static inline bool is_media_entity_v4l2_io(struct media_entity *entity) static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
{ {
if (!entity) return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
return false;
switch (entity->function) {
case MEDIA_ENT_F_IO_V4L:
case MEDIA_ENT_F_IO_VBI:
case MEDIA_ENT_F_IO_SWRADIO:
return true;
default:
return false;
}
} }
/** /**
* is_media_entity_v4l2_subdev - return true if the entity main function is * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
* associated with the V4L2 API subdev usage
*
* @entity: pointer to entity * @entity: pointer to entity
* *
* This is an ancillary function used by subdev-based V4L2 drivers. * Return: true if the entity is an instance of a v4l2_subdev object and can
* It checks if the entity function is one of functions used by a V4L2 subdev, * safely be cast to a struct v4l2_subdev using the container_of() macro, or
* e. g. camera-relatef functions, analog TV decoder, TV tuner, V4L2 DSPs. * false otherwise.
*/ */
static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity) static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
{ {
if (!entity) return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
return false;
switch (entity->function) {
case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
case MEDIA_ENT_F_CAM_SENSOR:
case MEDIA_ENT_F_FLASH:
case MEDIA_ENT_F_LENS:
case MEDIA_ENT_F_ATV_DECODER:
case MEDIA_ENT_F_TUNER:
return true;
default:
return false;
}
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment