xref: /linux/include/media/media-device.h (revision 6fd600d742744dc7ef7fc65ca26daa2b1163158a)
11802d0beSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2176fb0d1SLaurent Pinchart /*
3176fb0d1SLaurent Pinchart  * Media device
4176fb0d1SLaurent Pinchart  *
5176fb0d1SLaurent Pinchart  * Copyright (C) 2010 Nokia Corporation
6176fb0d1SLaurent Pinchart  *
7176fb0d1SLaurent Pinchart  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8176fb0d1SLaurent Pinchart  *	     Sakari Ailus <sakari.ailus@iki.fi>
9176fb0d1SLaurent Pinchart  */
10176fb0d1SLaurent Pinchart 
11176fb0d1SLaurent Pinchart #ifndef _MEDIA_DEVICE_H
12176fb0d1SLaurent Pinchart #define _MEDIA_DEVICE_H
13176fb0d1SLaurent Pinchart 
14176fb0d1SLaurent Pinchart #include <linux/list.h>
15503c3d82SLaurent Pinchart #include <linux/mutex.h>
1678a171e5SSakari Ailus #include <linux/pci.h>
1778a171e5SSakari Ailus #include <linux/platform_device.h>
18176fb0d1SLaurent Pinchart 
19176fb0d1SLaurent Pinchart #include <media/media-devnode.h>
2053e269c1SLaurent Pinchart #include <media/media-entity.h>
21176fb0d1SLaurent Pinchart 
22665faa97SSakari Ailus struct ida;
2310905d70SHans Verkuil struct media_device;
24313162d0SPaul Gortmaker 
25176fb0d1SLaurent Pinchart /**
26afcbdb55SShuah Khan  * struct media_entity_notify - Media Entity Notify
27afcbdb55SShuah Khan  *
28afcbdb55SShuah Khan  * @list: List head
29afcbdb55SShuah Khan  * @notify_data: Input data to invoke the callback
30afcbdb55SShuah Khan  * @notify: Callback function pointer
31afcbdb55SShuah Khan  *
32fc641261SShuah Khan  * Drivers may register a callback to take action when new entities get
33fc641261SShuah Khan  * registered with the media device. This handler is intended for creating
34fc641261SShuah Khan  * links between existing entities and should not create entities and register
35fc641261SShuah Khan  * them.
36afcbdb55SShuah Khan  */
37afcbdb55SShuah Khan struct media_entity_notify {
38afcbdb55SShuah Khan 	struct list_head list;
39afcbdb55SShuah Khan 	void *notify_data;
40afcbdb55SShuah Khan 	void (*notify)(struct media_entity *entity, void *notify_data);
41afcbdb55SShuah Khan };
42afcbdb55SShuah Khan 
43afcbdb55SShuah Khan /**
4468429f50SLaurent Pinchart  * struct media_device_ops - Media device operations
4568429f50SLaurent Pinchart  * @link_notify: Link state change notification callback. This callback is
4668429f50SLaurent Pinchart  *		 called with the graph_mutex held.
4710905d70SHans Verkuil  * @req_alloc: Allocate a request. Set this if you need to allocate a struct
4810905d70SHans Verkuil  *	       larger then struct media_request. @req_alloc and @req_free must
4910905d70SHans Verkuil  *	       either both be set or both be NULL.
5010905d70SHans Verkuil  * @req_free: Free a request. Set this if @req_alloc was set as well, leave
5110905d70SHans Verkuil  *	      to NULL otherwise.
5210905d70SHans Verkuil  * @req_validate: Validate a request, but do not queue yet. The req_queue_mutex
5310905d70SHans Verkuil  *	          lock is held when this op is called.
5410905d70SHans Verkuil  * @req_queue: Queue a validated request, cannot fail. If something goes
5510905d70SHans Verkuil  *	       wrong when queueing this request then it should be marked
5610905d70SHans Verkuil  *	       as such internally in the driver and any related buffers
5710905d70SHans Verkuil  *	       must eventually return to vb2 with state VB2_BUF_STATE_ERROR.
5810905d70SHans Verkuil  *	       The req_queue_mutex lock is held when this op is called.
5910905d70SHans Verkuil  *	       It is important that vb2 buffer objects are queued last after
6010905d70SHans Verkuil  *	       all other object types are queued: queueing a buffer kickstarts
6110905d70SHans Verkuil  *	       the request processing, so all other objects related to the
6210905d70SHans Verkuil  *	       request (and thus the buffer) must be available to the driver.
6310905d70SHans Verkuil  *	       And once a buffer is queued, then the driver can complete
6410905d70SHans Verkuil  *	       or delete objects from the request before req_queue exits.
6568429f50SLaurent Pinchart  */
6668429f50SLaurent Pinchart struct media_device_ops {
6768429f50SLaurent Pinchart 	int (*link_notify)(struct media_link *link, u32 flags,
6868429f50SLaurent Pinchart 			   unsigned int notification);
6910905d70SHans Verkuil 	struct media_request *(*req_alloc)(struct media_device *mdev);
7010905d70SHans Verkuil 	void (*req_free)(struct media_request *req);
7110905d70SHans Verkuil 	int (*req_validate)(struct media_request *req);
7210905d70SHans Verkuil 	void (*req_queue)(struct media_request *req);
7368429f50SLaurent Pinchart };
7468429f50SLaurent Pinchart 
7568429f50SLaurent Pinchart /**
76176fb0d1SLaurent Pinchart  * struct media_device - Media device
77176fb0d1SLaurent Pinchart  * @dev:	Parent device
78176fb0d1SLaurent Pinchart  * @devnode:	Media device node
79bb07bd6bSMauro Carvalho Chehab  * @driver_name: Optional device driver name. If not set, calls to
8048a7c4baSMauro Carvalho Chehab  *		%MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.
81bb07bd6bSMauro Carvalho Chehab  *		This is needed for USB drivers for example, as otherwise
82bb07bd6bSMauro Carvalho Chehab  *		they'll all appear as if the driver name was "usb".
83176fb0d1SLaurent Pinchart  * @model:	Device model name
84176fb0d1SLaurent Pinchart  * @serial:	Device serial number (optional)
85176fb0d1SLaurent Pinchart  * @bus_info:	Unique and stable device location identifier
86176fb0d1SLaurent Pinchart  * @hw_revision: Hardware device revision
872521fdacSMauro Carvalho Chehab  * @topology_version: Monotonic counter for storing the version of the graph
882521fdacSMauro Carvalho Chehab  *		topology. Should be incremented each time the topology changes.
8905b3b77cSMauro Carvalho Chehab  * @id:		Unique ID used on the last registered graph object
9003e49338SMauro Carvalho Chehab  * @entity_internal_idx: Unique internal entity ID used by the graph traversal
9103e49338SMauro Carvalho Chehab  *		algorithms
9203e49338SMauro Carvalho Chehab  * @entity_internal_idx_max: Allocated internal entity indices
9353e269c1SLaurent Pinchart  * @entities:	List of registered entities
9457cf79b7SMauro Carvalho Chehab  * @interfaces:	List of registered interfaces
959155d859SMauro Carvalho Chehab  * @pads:	List of registered pads
969155d859SMauro Carvalho Chehab  * @links:	List of registered links
97afcbdb55SShuah Khan  * @entity_notify: List of registered entity_notify callbacks
98e2c91d4dSMauro Carvalho Chehab  * @graph_mutex: Protects access to struct media_device data
990c426c47SSakari Ailus  * @pm_count_walk: Graph walk for power state walk. Access serialised using
1000c426c47SSakari Ailus  *		   graph_mutex.
101cd87ce87SShuah Khan  *
102cd87ce87SShuah Khan  * @source_priv: Driver Private data for enable/disable source handlers
103cd87ce87SShuah Khan  * @enable_source: Enable Source Handler function pointer
104cd87ce87SShuah Khan  * @disable_source: Disable Source Handler function pointer
105cd87ce87SShuah Khan  *
10668429f50SLaurent Pinchart  * @ops:	Operation handler callbacks
10710905d70SHans Verkuil  * @req_queue_mutex: Serialise the MEDIA_REQUEST_IOC_QUEUE ioctl w.r.t.
10810905d70SHans Verkuil  *		     other operations that stop or start streaming.
10910905d70SHans Verkuil  * @request_id: Used to generate unique request IDs
110176fb0d1SLaurent Pinchart  *
111176fb0d1SLaurent Pinchart  * This structure represents an abstract high-level media device. It allows easy
112176fb0d1SLaurent Pinchart  * access to entities and provides basic media device-level support. The
113176fb0d1SLaurent Pinchart  * structure can be allocated directly or embedded in a larger structure.
114176fb0d1SLaurent Pinchart  *
115176fb0d1SLaurent Pinchart  * The parent @dev is a physical device. It must be set before registering the
116176fb0d1SLaurent Pinchart  * media device.
117176fb0d1SLaurent Pinchart  *
118176fb0d1SLaurent Pinchart  * @model is a descriptive model name exported through sysfs. It doesn't have to
119176fb0d1SLaurent Pinchart  * be unique.
120cd87ce87SShuah Khan  *
121cd87ce87SShuah Khan  * @enable_source is a handler to find source entity for the
122cd87ce87SShuah Khan  * sink entity  and activate the link between them if source
123cd87ce87SShuah Khan  * entity is free. Drivers should call this handler before
124cd87ce87SShuah Khan  * accessing the source.
125cd87ce87SShuah Khan  *
126cd87ce87SShuah Khan  * @disable_source is a handler to find source entity for the
127cd87ce87SShuah Khan  * sink entity  and deactivate the link between them. Drivers
128cd87ce87SShuah Khan  * should call this handler to release the source.
129cd87ce87SShuah Khan  *
130cd87ce87SShuah Khan  * Use-case: find tuner entity connected to the decoder
131cd87ce87SShuah Khan  * entity and check if it is available, and activate the
132216964faSRandy Dunlap  * link between them from @enable_source and deactivate
13348a7c4baSMauro Carvalho Chehab  * from @disable_source.
13448a7c4baSMauro Carvalho Chehab  *
13548a7c4baSMauro Carvalho Chehab  * .. note::
13648a7c4baSMauro Carvalho Chehab  *
13748a7c4baSMauro Carvalho Chehab  *    Bridge driver is expected to implement and set the
13848a7c4baSMauro Carvalho Chehab  *    handler when &media_device is registered or when
13948a7c4baSMauro Carvalho Chehab  *    bridge driver finds the media_device during probe.
14048a7c4baSMauro Carvalho Chehab  *    Bridge driver sets source_priv with information
14148a7c4baSMauro Carvalho Chehab  *    necessary to run @enable_source and @disable_source handlers.
14290cd366bSShuah Khan  *    Callers should hold graph_mutex to access and call @enable_source
14390cd366bSShuah Khan  *    and @disable_source handlers.
144176fb0d1SLaurent Pinchart  */
145176fb0d1SLaurent Pinchart struct media_device {
146176fb0d1SLaurent Pinchart 	/* dev->driver_data points to this struct. */
147176fb0d1SLaurent Pinchart 	struct device *dev;
148a087ce70SMauro Carvalho Chehab 	struct media_devnode *devnode;
149176fb0d1SLaurent Pinchart 
150176fb0d1SLaurent Pinchart 	char model[32];
151bb07bd6bSMauro Carvalho Chehab 	char driver_name[32];
152176fb0d1SLaurent Pinchart 	char serial[40];
153176fb0d1SLaurent Pinchart 	char bus_info[32];
154176fb0d1SLaurent Pinchart 	u32 hw_revision;
15553e269c1SLaurent Pinchart 
156952f8eefSMauro Carvalho Chehab 	u64 topology_version;
1572521fdacSMauro Carvalho Chehab 
15805b3b77cSMauro Carvalho Chehab 	u32 id;
159665faa97SSakari Ailus 	struct ida entity_internal_idx;
160665faa97SSakari Ailus 	int entity_internal_idx_max;
161bfab2aacSMauro Carvalho Chehab 
16253e269c1SLaurent Pinchart 	struct list_head entities;
16357cf79b7SMauro Carvalho Chehab 	struct list_head interfaces;
1649155d859SMauro Carvalho Chehab 	struct list_head pads;
1659155d859SMauro Carvalho Chehab 	struct list_head links;
16653e269c1SLaurent Pinchart 
167afcbdb55SShuah Khan 	/* notify callback list invoked when a new entity is registered */
168afcbdb55SShuah Khan 	struct list_head entity_notify;
169afcbdb55SShuah Khan 
170503c3d82SLaurent Pinchart 	/* Serializes graph operations. */
171503c3d82SLaurent Pinchart 	struct mutex graph_mutex;
17220b85227SSakari Ailus 	struct media_graph pm_count_walk;
17397548ed4SLaurent Pinchart 
174cd87ce87SShuah Khan 	void *source_priv;
175cd87ce87SShuah Khan 	int (*enable_source)(struct media_entity *entity,
176cd87ce87SShuah Khan 			     struct media_pipeline *pipe);
177cd87ce87SShuah Khan 	void (*disable_source)(struct media_entity *entity);
178cd87ce87SShuah Khan 
17968429f50SLaurent Pinchart 	const struct media_device_ops *ops;
18010905d70SHans Verkuil 
18110905d70SHans Verkuil 	struct mutex req_queue_mutex;
18210905d70SHans Verkuil 	atomic_t request_id;
183176fb0d1SLaurent Pinchart };
184176fb0d1SLaurent Pinchart 
18578a171e5SSakari Ailus /* We don't need to include usb.h here */
18641b44e35SMauro Carvalho Chehab struct usb_device;
18741b44e35SMauro Carvalho Chehab 
188e576d60bSShuah Khan #ifdef CONFIG_MEDIA_CONTROLLER
189e576d60bSShuah Khan 
190813f5c0aSSylwester Nawrocki /* Supported link_notify @notification values. */
191813f5c0aSSylwester Nawrocki #define MEDIA_DEV_NOTIFY_PRE_LINK_CH	0
192813f5c0aSSylwester Nawrocki #define MEDIA_DEV_NOTIFY_POST_LINK_CH	1
193813f5c0aSSylwester Nawrocki 
194db7ee32aSMauro Carvalho Chehab /**
1959832e155SJavier Martinez Canillas  * media_device_init() - Initializes a media device element
1969832e155SJavier Martinez Canillas  *
1979832e155SJavier Martinez Canillas  * @mdev:	pointer to struct &media_device
1989832e155SJavier Martinez Canillas  *
1999832e155SJavier Martinez Canillas  * This function initializes the media device prior to its registration.
2009832e155SJavier Martinez Canillas  * The media device initialization and registration is split in two functions
2019832e155SJavier Martinez Canillas  * to avoid race conditions and make the media device available to user-space
2029832e155SJavier Martinez Canillas  * before the media graph has been completed.
2039832e155SJavier Martinez Canillas  *
2049832e155SJavier Martinez Canillas  * So drivers need to first initialize the media device, register any entity
2059832e155SJavier Martinez Canillas  * within the media device, create pad to pad links and then finally register
2069832e155SJavier Martinez Canillas  * the media device by calling media_device_register() as a final step.
207a8a2bd10SSakari Ailus  *
208a8a2bd10SSakari Ailus  * The caller is responsible for initializing the media device before
209a8a2bd10SSakari Ailus  * registration. The following fields must be set:
210a8a2bd10SSakari Ailus  *
211a8a2bd10SSakari Ailus  * - dev must point to the parent device
212a8a2bd10SSakari Ailus  * - model must be filled with the device model name
213cef69974SSakari Ailus  *
214cef69974SSakari Ailus  * The bus_info field is set by media_device_init() for PCI and platform devices
215cef69974SSakari Ailus  * if the field begins with '\0'.
2169832e155SJavier Martinez Canillas  */
2179832e155SJavier Martinez Canillas void media_device_init(struct media_device *mdev);
2189832e155SJavier Martinez Canillas 
2199832e155SJavier Martinez Canillas /**
2209832e155SJavier Martinez Canillas  * media_device_cleanup() - Cleanups a media device element
2219832e155SJavier Martinez Canillas  *
2229832e155SJavier Martinez Canillas  * @mdev:	pointer to struct &media_device
2239832e155SJavier Martinez Canillas  *
2249832e155SJavier Martinez Canillas  * This function that will destroy the graph_mutex that is
2259832e155SJavier Martinez Canillas  * initialized in media_device_init().
2269832e155SJavier Martinez Canillas  */
2279832e155SJavier Martinez Canillas void media_device_cleanup(struct media_device *mdev);
2289832e155SJavier Martinez Canillas 
2299832e155SJavier Martinez Canillas /**
230db7ee32aSMauro Carvalho Chehab  * __media_device_register() - Registers a media device element
231db7ee32aSMauro Carvalho Chehab  *
232db7ee32aSMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
233db7ee32aSMauro Carvalho Chehab  * @owner:	should be filled with %THIS_MODULE
234db7ee32aSMauro Carvalho Chehab  *
235db7ee32aSMauro Carvalho Chehab  * Users, should, instead, call the media_device_register() macro.
236db7ee32aSMauro Carvalho Chehab  *
23748a7c4baSMauro Carvalho Chehab  * The caller is responsible for initializing the &media_device structure
23848a7c4baSMauro Carvalho Chehab  * before registration. The following fields of &media_device must be set:
239db7ee32aSMauro Carvalho Chehab  *
240db1fa290SSakari Ailus  *  - &media_device.model must be filled with the device model name as a
24148a7c4baSMauro Carvalho Chehab  *    NUL-terminated UTF-8 string. The device/model revision must not be
24248a7c4baSMauro Carvalho Chehab  *    stored in this field.
243db7ee32aSMauro Carvalho Chehab  *
244db7ee32aSMauro Carvalho Chehab  * The following fields are optional:
245db7ee32aSMauro Carvalho Chehab  *
246db1fa290SSakari Ailus  *  - &media_device.serial is a unique serial number stored as a
24748a7c4baSMauro Carvalho Chehab  *    NUL-terminated ASCII string. The field is big enough to store a GUID
24848a7c4baSMauro Carvalho Chehab  *    in text form. If the hardware doesn't provide a unique serial number
24948a7c4baSMauro Carvalho Chehab  *    this field must be left empty.
250db7ee32aSMauro Carvalho Chehab  *
251db1fa290SSakari Ailus  *  - &media_device.bus_info represents the location of the device in the
25248a7c4baSMauro Carvalho Chehab  *    system as a NUL-terminated ASCII string. For PCI/PCIe devices
253db1fa290SSakari Ailus  *    &media_device.bus_info must be set to "PCI:" (or "PCIe:") followed by
25448a7c4baSMauro Carvalho Chehab  *    the value of pci_name(). For USB devices,the usb_make_path() function
25548a7c4baSMauro Carvalho Chehab  *    must be used. This field is used by applications to distinguish between
25648a7c4baSMauro Carvalho Chehab  *    otherwise identical devices that don't provide a serial number.
257db7ee32aSMauro Carvalho Chehab  *
258db1fa290SSakari Ailus  *  - &media_device.hw_revision is the hardware device revision in a
25948a7c4baSMauro Carvalho Chehab  *    driver-specific format. When possible the revision should be formatted
26048a7c4baSMauro Carvalho Chehab  *    with the KERNEL_VERSION() macro.
261db7ee32aSMauro Carvalho Chehab  *
26274604b73SMauro Carvalho Chehab  * .. note::
263db7ee32aSMauro Carvalho Chehab  *
26474604b73SMauro Carvalho Chehab  *    #) Upon successful registration a character device named media[0-9]+ is created. The device major and minor numbers are dynamic. The model name is exported as a sysfs attribute.
265db7ee32aSMauro Carvalho Chehab  *
26674604b73SMauro Carvalho Chehab  *    #) Unregistering a media device that hasn't been registered is **NOT** safe.
26792777994SMauro Carvalho Chehab  *
26892777994SMauro Carvalho Chehab  * Return: returns zero on success or a negative error code.
269db7ee32aSMauro Carvalho Chehab  */
27085de721cSSakari Ailus int __must_check __media_device_register(struct media_device *mdev,
27185de721cSSakari Ailus 					 struct module *owner);
27248a7c4baSMauro Carvalho Chehab 
27348a7c4baSMauro Carvalho Chehab 
27448a7c4baSMauro Carvalho Chehab /**
27548a7c4baSMauro Carvalho Chehab  * media_device_register() - Registers a media device element
27648a7c4baSMauro Carvalho Chehab  *
27748a7c4baSMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
27848a7c4baSMauro Carvalho Chehab  *
27948a7c4baSMauro Carvalho Chehab  * This macro calls __media_device_register() passing %THIS_MODULE as
28048a7c4baSMauro Carvalho Chehab  * the __media_device_register() second argument (**owner**).
28148a7c4baSMauro Carvalho Chehab  */
28285de721cSSakari Ailus #define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
283db7ee32aSMauro Carvalho Chehab 
284db7ee32aSMauro Carvalho Chehab /**
2853047f3f9SMauro Carvalho Chehab  * media_device_unregister() - Unregisters a media device element
286db7ee32aSMauro Carvalho Chehab  *
287db7ee32aSMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
28892777994SMauro Carvalho Chehab  *
28992777994SMauro Carvalho Chehab  * It is safe to call this function on an unregistered (but initialised)
29092777994SMauro Carvalho Chehab  * media device.
291db7ee32aSMauro Carvalho Chehab  */
292176fb0d1SLaurent Pinchart void media_device_unregister(struct media_device *mdev);
293176fb0d1SLaurent Pinchart 
294db7ee32aSMauro Carvalho Chehab /**
295db7ee32aSMauro Carvalho Chehab  * media_device_register_entity() - registers a media entity inside a
296db7ee32aSMauro Carvalho Chehab  *	previously registered media device.
297db7ee32aSMauro Carvalho Chehab  *
298db7ee32aSMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
299db7ee32aSMauro Carvalho Chehab  * @entity:	pointer to struct &media_entity to be registered
300db7ee32aSMauro Carvalho Chehab  *
301db7ee32aSMauro Carvalho Chehab  * Entities are identified by a unique positive integer ID. The media
302db7ee32aSMauro Carvalho Chehab  * controller framework will such ID automatically. IDs are not guaranteed
303db7ee32aSMauro Carvalho Chehab  * to be contiguous, and the ID number can change on newer Kernel versions.
304db7ee32aSMauro Carvalho Chehab  * So, neither the driver nor userspace should hardcode ID numbers to refer
305db7ee32aSMauro Carvalho Chehab  * to the entities, but, instead, use the framework to find the ID, when
306db7ee32aSMauro Carvalho Chehab  * needed.
307db7ee32aSMauro Carvalho Chehab  *
308db7ee32aSMauro Carvalho Chehab  * The media_entity name, type and flags fields should be initialized before
309db7ee32aSMauro Carvalho Chehab  * calling media_device_register_entity(). Entities embedded in higher-level
310db7ee32aSMauro Carvalho Chehab  * standard structures can have some of those fields set by the higher-level
311db7ee32aSMauro Carvalho Chehab  * framework.
312db7ee32aSMauro Carvalho Chehab  *
313db7ee32aSMauro Carvalho Chehab  * If the device has pads, media_entity_pads_init() should be called before
31448a7c4baSMauro Carvalho Chehab  * this function. Otherwise, the &media_entity.pad and &media_entity.num_pads
315db7ee32aSMauro Carvalho Chehab  * should be zeroed before calling this function.
316db7ee32aSMauro Carvalho Chehab  *
317db7ee32aSMauro Carvalho Chehab  * Entities have flags that describe the entity capabilities and state:
318db7ee32aSMauro Carvalho Chehab  *
31948a7c4baSMauro Carvalho Chehab  * %MEDIA_ENT_FL_DEFAULT
32048a7c4baSMauro Carvalho Chehab  *    indicates the default entity for a given type.
321db7ee32aSMauro Carvalho Chehab  *    This can be used to report the default audio and video devices or the
322db7ee32aSMauro Carvalho Chehab  *    default camera sensor.
323d1b9da2dSMauro Carvalho Chehab  *
32474604b73SMauro Carvalho Chehab  * .. note::
32574604b73SMauro Carvalho Chehab  *
32674604b73SMauro Carvalho Chehab  *    Drivers should set the entity function before calling this function.
327d1b9da2dSMauro Carvalho Chehab  *    Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and
328d1b9da2dSMauro Carvalho Chehab  *    %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
329db7ee32aSMauro Carvalho Chehab  */
33053e269c1SLaurent Pinchart int __must_check media_device_register_entity(struct media_device *mdev,
33153e269c1SLaurent Pinchart 					      struct media_entity *entity);
332db7ee32aSMauro Carvalho Chehab 
33374604b73SMauro Carvalho Chehab /**
334db7ee32aSMauro Carvalho Chehab  * media_device_unregister_entity() - unregisters a media entity.
335db7ee32aSMauro Carvalho Chehab  *
336db7ee32aSMauro Carvalho Chehab  * @entity:	pointer to struct &media_entity to be unregistered
337db7ee32aSMauro Carvalho Chehab  *
338db7ee32aSMauro Carvalho Chehab  * All links associated with the entity and all PADs are automatically
339db7ee32aSMauro Carvalho Chehab  * unregistered from the media_device when this function is called.
340db7ee32aSMauro Carvalho Chehab  *
341db7ee32aSMauro Carvalho Chehab  * Unregistering an entity will not change the IDs of the other entities and
342db7ee32aSMauro Carvalho Chehab  * the previoully used ID will never be reused for a newly registered entities.
343db7ee32aSMauro Carvalho Chehab  *
344db7ee32aSMauro Carvalho Chehab  * When a media device is unregistered, all its entities are unregistered
345db7ee32aSMauro Carvalho Chehab  * automatically. No manual entities unregistration is then required.
346db7ee32aSMauro Carvalho Chehab  *
34774604b73SMauro Carvalho Chehab  * .. note::
34874604b73SMauro Carvalho Chehab  *
34974604b73SMauro Carvalho Chehab  *    The media_entity instance itself must be freed explicitly by
350db7ee32aSMauro Carvalho Chehab  *    the driver if required.
351db7ee32aSMauro Carvalho Chehab  */
35253e269c1SLaurent Pinchart void media_device_unregister_entity(struct media_entity *entity);
353b6e4ca81SMauro Carvalho Chehab 
354b6e4ca81SMauro Carvalho Chehab /**
355afcbdb55SShuah Khan  * media_device_register_entity_notify() - Registers a media entity_notify
356afcbdb55SShuah Khan  *					   callback
357afcbdb55SShuah Khan  *
358afcbdb55SShuah Khan  * @mdev:      The media device
359afcbdb55SShuah Khan  * @nptr:      The media_entity_notify
360afcbdb55SShuah Khan  *
36148a7c4baSMauro Carvalho Chehab  * .. note::
36248a7c4baSMauro Carvalho Chehab  *
36348a7c4baSMauro Carvalho Chehab  *    When a new entity is registered, all the registered
364afcbdb55SShuah Khan  *    media_entity_notify callbacks are invoked.
365afcbdb55SShuah Khan  */
366afcbdb55SShuah Khan 
367a7687fecSJason Kim void media_device_register_entity_notify(struct media_device *mdev,
368afcbdb55SShuah Khan 					struct media_entity_notify *nptr);
369afcbdb55SShuah Khan 
370afcbdb55SShuah Khan /**
371afcbdb55SShuah Khan  * media_device_unregister_entity_notify() - Unregister a media entity notify
372afcbdb55SShuah Khan  *					     callback
373afcbdb55SShuah Khan  *
374afcbdb55SShuah Khan  * @mdev:      The media device
375afcbdb55SShuah Khan  * @nptr:      The media_entity_notify
376afcbdb55SShuah Khan  *
377afcbdb55SShuah Khan  */
378afcbdb55SShuah Khan void media_device_unregister_entity_notify(struct media_device *mdev,
379afcbdb55SShuah Khan 					struct media_entity_notify *nptr);
380afcbdb55SShuah Khan 
38153e269c1SLaurent Pinchart /* Iterate over all entities. */
38253e269c1SLaurent Pinchart #define media_device_for_each_entity(entity, mdev)			\
38305bfa9faSMauro Carvalho Chehab 	list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
38453e269c1SLaurent Pinchart 
385cf975a4bSMauro Carvalho Chehab /* Iterate over all interfaces. */
386cf975a4bSMauro Carvalho Chehab #define media_device_for_each_intf(intf, mdev)			\
38705bfa9faSMauro Carvalho Chehab 	list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
388cf975a4bSMauro Carvalho Chehab 
3899155d859SMauro Carvalho Chehab /* Iterate over all pads. */
3909155d859SMauro Carvalho Chehab #define media_device_for_each_pad(pad, mdev)			\
3919155d859SMauro Carvalho Chehab 	list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
3929155d859SMauro Carvalho Chehab 
3939155d859SMauro Carvalho Chehab /* Iterate over all links. */
3949155d859SMauro Carvalho Chehab #define media_device_for_each_link(link, mdev)			\
3959155d859SMauro Carvalho Chehab 	list_for_each_entry(link, &(mdev)->links, graph_obj.list)
39641b44e35SMauro Carvalho Chehab 
39741b44e35SMauro Carvalho Chehab /**
39841b44e35SMauro Carvalho Chehab  * media_device_pci_init() - create and initialize a
39941b44e35SMauro Carvalho Chehab  *	struct &media_device from a PCI device.
40041b44e35SMauro Carvalho Chehab  *
4016cf5dad1SMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
40241b44e35SMauro Carvalho Chehab  * @pci_dev:	pointer to struct pci_dev
40341b44e35SMauro Carvalho Chehab  * @name:	media device name. If %NULL, the routine will use the default
40441b44e35SMauro Carvalho Chehab  *		name for the pci device, given by pci_name() macro.
40541b44e35SMauro Carvalho Chehab  */
4066cf5dad1SMauro Carvalho Chehab void media_device_pci_init(struct media_device *mdev,
4076cf5dad1SMauro Carvalho Chehab 			   struct pci_dev *pci_dev,
40841b44e35SMauro Carvalho Chehab 			   const char *name);
40941b44e35SMauro Carvalho Chehab /**
41041b44e35SMauro Carvalho Chehab  * __media_device_usb_init() - create and initialize a
41141b44e35SMauro Carvalho Chehab  *	struct &media_device from a PCI device.
41241b44e35SMauro Carvalho Chehab  *
4136cf5dad1SMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
41441b44e35SMauro Carvalho Chehab  * @udev:	pointer to struct usb_device
41541b44e35SMauro Carvalho Chehab  * @board_name:	media device name. If %NULL, the routine will use the usb
41641b44e35SMauro Carvalho Chehab  *		product name, if available.
41741b44e35SMauro Carvalho Chehab  * @driver_name: name of the driver. if %NULL, the routine will use the name
41848a7c4baSMauro Carvalho Chehab  *		given by ``udev->dev->driver->name``, with is usually the wrong
41941b44e35SMauro Carvalho Chehab  *		thing to do.
42041b44e35SMauro Carvalho Chehab  *
42148a7c4baSMauro Carvalho Chehab  * .. note::
42248a7c4baSMauro Carvalho Chehab  *
42348a7c4baSMauro Carvalho Chehab  *    It is better to call media_device_usb_init() instead, as
42441b44e35SMauro Carvalho Chehab  *    such macro fills driver_name with %KBUILD_MODNAME.
42541b44e35SMauro Carvalho Chehab  */
4266cf5dad1SMauro Carvalho Chehab void __media_device_usb_init(struct media_device *mdev,
4276cf5dad1SMauro Carvalho Chehab 			     struct usb_device *udev,
42841b44e35SMauro Carvalho Chehab 			     const char *board_name,
42941b44e35SMauro Carvalho Chehab 			     const char *driver_name);
43041b44e35SMauro Carvalho Chehab 
431e576d60bSShuah Khan #else
media_device_init(struct media_device * mdev)432*7cb8a625SSakari Ailus static inline void media_device_init(struct media_device *mdev)
433*7cb8a625SSakari Ailus {
434*7cb8a625SSakari Ailus }
media_device_register(struct media_device * mdev)435e576d60bSShuah Khan static inline int media_device_register(struct media_device *mdev)
436e576d60bSShuah Khan {
437e576d60bSShuah Khan 	return 0;
438e576d60bSShuah Khan }
media_device_unregister(struct media_device * mdev)439e576d60bSShuah Khan static inline void media_device_unregister(struct media_device *mdev)
440e576d60bSShuah Khan {
441e576d60bSShuah Khan }
media_device_cleanup(struct media_device * mdev)442*7cb8a625SSakari Ailus static inline void media_device_cleanup(struct media_device *mdev)
443*7cb8a625SSakari Ailus {
444*7cb8a625SSakari Ailus }
media_device_register_entity(struct media_device * mdev,struct media_entity * entity)445e576d60bSShuah Khan static inline int media_device_register_entity(struct media_device *mdev,
446e576d60bSShuah Khan 						struct media_entity *entity)
447e576d60bSShuah Khan {
448e576d60bSShuah Khan 	return 0;
449e576d60bSShuah Khan }
media_device_unregister_entity(struct media_entity * entity)450e576d60bSShuah Khan static inline void media_device_unregister_entity(struct media_entity *entity)
451e576d60bSShuah Khan {
452e576d60bSShuah Khan }
media_device_register_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)453a7687fecSJason Kim static inline void media_device_register_entity_notify(
454afcbdb55SShuah Khan 					struct media_device *mdev,
455afcbdb55SShuah Khan 					struct media_entity_notify *nptr)
456afcbdb55SShuah Khan {
457afcbdb55SShuah Khan }
media_device_unregister_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)458afcbdb55SShuah Khan static inline void media_device_unregister_entity_notify(
459afcbdb55SShuah Khan 					struct media_device *mdev,
460afcbdb55SShuah Khan 					struct media_entity_notify *nptr)
461afcbdb55SShuah Khan {
462afcbdb55SShuah Khan }
46341b44e35SMauro Carvalho Chehab 
media_device_pci_init(struct media_device * mdev,struct pci_dev * pci_dev,char * name)4646cf5dad1SMauro Carvalho Chehab static inline void media_device_pci_init(struct media_device *mdev,
4656cf5dad1SMauro Carvalho Chehab 					 struct pci_dev *pci_dev,
46641b44e35SMauro Carvalho Chehab 					 char *name)
46741b44e35SMauro Carvalho Chehab {
46841b44e35SMauro Carvalho Chehab }
46941b44e35SMauro Carvalho Chehab 
__media_device_usb_init(struct media_device * mdev,struct usb_device * udev,char * board_name,char * driver_name)4706cf5dad1SMauro Carvalho Chehab static inline void __media_device_usb_init(struct media_device *mdev,
4716cf5dad1SMauro Carvalho Chehab 					   struct usb_device *udev,
47241b44e35SMauro Carvalho Chehab 					   char *board_name,
47341b44e35SMauro Carvalho Chehab 					   char *driver_name)
47441b44e35SMauro Carvalho Chehab {
47541b44e35SMauro Carvalho Chehab }
47641b44e35SMauro Carvalho Chehab 
477e576d60bSShuah Khan #endif /* CONFIG_MEDIA_CONTROLLER */
47841b44e35SMauro Carvalho Chehab 
47948a7c4baSMauro Carvalho Chehab /**
48048a7c4baSMauro Carvalho Chehab  * media_device_usb_init() - create and initialize a
48148a7c4baSMauro Carvalho Chehab  *	struct &media_device from a PCI device.
48248a7c4baSMauro Carvalho Chehab  *
48348a7c4baSMauro Carvalho Chehab  * @mdev:	pointer to struct &media_device
48448a7c4baSMauro Carvalho Chehab  * @udev:	pointer to struct usb_device
48548a7c4baSMauro Carvalho Chehab  * @name:	media device name. If %NULL, the routine will use the usb
48648a7c4baSMauro Carvalho Chehab  *		product name, if available.
48748a7c4baSMauro Carvalho Chehab  *
48848a7c4baSMauro Carvalho Chehab  * This macro calls media_device_usb_init() passing the
48948a7c4baSMauro Carvalho Chehab  * media_device_usb_init() **driver_name** parameter filled with
49048a7c4baSMauro Carvalho Chehab  * %KBUILD_MODNAME.
49148a7c4baSMauro Carvalho Chehab  */
4926cf5dad1SMauro Carvalho Chehab #define media_device_usb_init(mdev, udev, name) \
4936cf5dad1SMauro Carvalho Chehab 	__media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
49441b44e35SMauro Carvalho Chehab 
49578a171e5SSakari Ailus /**
49678a171e5SSakari Ailus  * media_set_bus_info() - Set bus_info field
49778a171e5SSakari Ailus  *
49878a171e5SSakari Ailus  * @bus_info:		Variable where to write the bus info (char array)
49978a171e5SSakari Ailus  * @bus_info_size:	Length of the bus_info
50078a171e5SSakari Ailus  * @dev:		Related struct device
50178a171e5SSakari Ailus  *
50278a171e5SSakari Ailus  * Sets bus information based on &dev. This is currently done for PCI and
50378a171e5SSakari Ailus  * platform devices. dev is required to be non-NULL for this to happen.
50478a171e5SSakari Ailus  *
50578a171e5SSakari Ailus  * This function is not meant to be called from drivers.
50678a171e5SSakari Ailus  */
50778a171e5SSakari Ailus static inline void
media_set_bus_info(char * bus_info,size_t bus_info_size,struct device * dev)50878a171e5SSakari Ailus media_set_bus_info(char *bus_info, size_t bus_info_size, struct device *dev)
50978a171e5SSakari Ailus {
51078a171e5SSakari Ailus 	if (!dev)
51178a171e5SSakari Ailus 		strscpy(bus_info, "no bus info", bus_info_size);
51278a171e5SSakari Ailus 	else if (dev_is_platform(dev))
51378a171e5SSakari Ailus 		snprintf(bus_info, bus_info_size, "platform:%s", dev_name(dev));
51478a171e5SSakari Ailus 	else if (dev_is_pci(dev))
51578a171e5SSakari Ailus 		snprintf(bus_info, bus_info_size, "PCI:%s", dev_name(dev));
51678a171e5SSakari Ailus }
51778a171e5SSakari Ailus 
518176fb0d1SLaurent Pinchart #endif
519