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

[media] media: Entity use count

Due to the wide differences between drivers regarding power management
needs, the media controller does not implement power management.
However, the media_entity structure includes a use_count field that
media drivers can use to track the number of users of every entity for
power management needs.

The use_count field is owned by media drivers and must not be touched by
entity drivers. Access to the field must be protected by the media
device graph_mutex lock.
Signed-off-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: default avatarHans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent a5ccc48a
...@@ -258,3 +258,16 @@ When the graph traversal is complete the function will return NULL. ...@@ -258,3 +258,16 @@ When the graph traversal is complete the function will return NULL.
Graph traversal can be interrupted at any moment. No cleanup function call is Graph traversal can be interrupted at any moment. No cleanup function call is
required and the graph structure can be freed normally. required and the graph structure can be freed normally.
Use count and power handling
----------------------------
Due to the wide differences between drivers regarding power management needs,
the media controller does not implement power management. However, the
media_entity structure includes a use_count field that media drivers can use to
track the number of users of every entity for power management needs.
The use_count field is owned by media drivers and must not be touched by entity
drivers. Access to the field must be protected by the media device graph_mutex
lock.
...@@ -73,6 +73,7 @@ int __must_check media_device_register(struct media_device *mdev) ...@@ -73,6 +73,7 @@ int __must_check media_device_register(struct media_device *mdev)
mdev->entity_id = 1; mdev->entity_id = 1;
INIT_LIST_HEAD(&mdev->entities); INIT_LIST_HEAD(&mdev->entities);
spin_lock_init(&mdev->lock); spin_lock_init(&mdev->lock);
mutex_init(&mdev->graph_mutex);
/* Register the device node. */ /* Register the device node. */
mdev->devnode.fops = &media_device_fops; mdev->devnode.fops = &media_device_fops;
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <media/media-entity.h> #include <media/media-entity.h>
#include <media/media-device.h>
/** /**
* media_entity_init - Initialize a media entity * media_entity_init - Initialize a media entity
...@@ -195,6 +196,51 @@ media_entity_graph_walk_next(struct media_entity_graph *graph) ...@@ -195,6 +196,51 @@ media_entity_graph_walk_next(struct media_entity_graph *graph)
} }
EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
/* -----------------------------------------------------------------------------
* Module use count
*/
/*
* media_entity_get - Get a reference to the parent module
* @entity: The entity
*
* Get a reference to the parent media device module.
*
* The function will return immediately if @entity is NULL.
*
* Return a pointer to the entity on success or NULL on failure.
*/
struct media_entity *media_entity_get(struct media_entity *entity)
{
if (entity == NULL)
return NULL;
if (entity->parent->dev &&
!try_module_get(entity->parent->dev->driver->owner))
return NULL;
return entity;
}
EXPORT_SYMBOL_GPL(media_entity_get);
/*
* media_entity_put - Release the reference to the parent module
* @entity: The entity
*
* Release the reference count acquired by media_entity_get().
*
* The function will return immediately if @entity is NULL.
*/
void media_entity_put(struct media_entity *entity)
{
if (entity == NULL)
return;
if (entity->parent->dev)
module_put(entity->parent->dev->driver->owner);
}
EXPORT_SYMBOL_GPL(media_entity_put);
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Links management * Links management
*/ */
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <media/media-devnode.h> #include <media/media-devnode.h>
...@@ -42,6 +43,7 @@ ...@@ -42,6 +43,7 @@
* @entity_id: ID of the next entity to be registered * @entity_id: ID of the next entity to be registered
* @entities: List of registered entities * @entities: List of registered entities
* @lock: Entities list lock * @lock: Entities list lock
* @graph_mutex: Entities graph operation lock
* *
* This structure represents an abstract high-level media device. It allows easy * This structure represents an abstract high-level media device. It allows easy
* access to entities and provides basic media device-level support. The * access to entities and provides basic media device-level support. The
...@@ -69,6 +71,8 @@ struct media_device { ...@@ -69,6 +71,8 @@ struct media_device {
/* Protects the entities list */ /* Protects the entities list */
spinlock_t lock; spinlock_t lock;
/* Serializes graph operations. */
struct mutex graph_mutex;
}; };
/* media_devnode to media_device */ /* media_devnode to media_device */
......
...@@ -81,6 +81,12 @@ struct media_entity { ...@@ -81,6 +81,12 @@ struct media_entity {
struct media_pad *pads; /* Pads array (num_pads elements) */ struct media_pad *pads; /* Pads array (num_pads elements) */
struct media_link *links; /* Links array (max_links elements)*/ struct media_link *links; /* Links array (max_links elements)*/
/* Reference counts must never be negative, but are signed integers on
* purpose: a simple WARN_ON(<0) check can be used to detect reference
* count bugs that would make them negative.
*/
int use_count; /* Use count for the entity. */
union { union {
/* Node specifications */ /* Node specifications */
struct { struct {
...@@ -129,6 +135,9 @@ void media_entity_cleanup(struct media_entity *entity); ...@@ -129,6 +135,9 @@ void media_entity_cleanup(struct media_entity *entity);
int media_entity_create_link(struct media_entity *source, u16 source_pad, int media_entity_create_link(struct media_entity *source, u16 source_pad,
struct media_entity *sink, u16 sink_pad, u32 flags); struct media_entity *sink, u16 sink_pad, u32 flags);
struct media_entity *media_entity_get(struct media_entity *entity);
void media_entity_put(struct media_entity *entity);
void media_entity_graph_walk_start(struct media_entity_graph *graph, void media_entity_graph_walk_start(struct media_entity_graph *graph,
struct media_entity *entity); struct media_entity *entity);
struct media_entity * struct media_entity *
......
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