1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Media device
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8  *	     Sakari Ailus <sakari.ailus@iki.fi>
9  */
10 
11 #ifndef _MEDIA_DEVICE_H
12 #define _MEDIA_DEVICE_H
13 
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 
19 #include <media/media-devnode.h>
20 #include <media/media-entity.h>
21 
22 struct ida;
23 struct media_device;
24 
25 /**
26  * struct media_entity_notify - Media Entity Notify
27  *
28  * @list: List head
29  * @notify_data: Input data to invoke the callback
30  * @notify: Callback function pointer
31  *
32  * Drivers may register a callback to take action when new entities get
33  * registered with the media device. This handler is intended for creating
34  * links between existing entities and should not create entities and register
35  * them.
36  */
37 struct media_entity_notify {
38 	struct list_head list;
39 	void *notify_data;
40 	void (*notify)(struct media_entity *entity, void *notify_data);
41 };
42 
43 /**
44  * struct media_device_ops - Media device operations
45  * @link_notify: Link state change notification callback. This callback is
46  *		 called with the graph_mutex held.
47  * @req_alloc: Allocate a request. Set this if you need to allocate a struct
48  *	       larger then struct media_request. @req_alloc and @req_free must
49  *	       either both be set or both be NULL.
50  * @req_free: Free a request. Set this if @req_alloc was set as well, leave
51  *	      to NULL otherwise.
52  * @req_validate: Validate a request, but do not queue yet. The req_queue_mutex
53  *	          lock is held when this op is called.
54  * @req_queue: Queue a validated request, cannot fail. If something goes
55  *	       wrong when queueing this request then it should be marked
56  *	       as such internally in the driver and any related buffers
57  *	       must eventually return to vb2 with state VB2_BUF_STATE_ERROR.
58  *	       The req_queue_mutex lock is held when this op is called.
59  *	       It is important that vb2 buffer objects are queued last after
60  *	       all other object types are queued: queueing a buffer kickstarts
61  *	       the request processing, so all other objects related to the
62  *	       request (and thus the buffer) must be available to the driver.
63  *	       And once a buffer is queued, then the driver can complete
64  *	       or delete objects from the request before req_queue exits.
65  */
66 struct media_device_ops {
67 	int (*link_notify)(struct media_link *link, u32 flags,
68 			   unsigned int notification);
69 	struct media_request *(*req_alloc)(struct media_device *mdev);
70 	void (*req_free)(struct media_request *req);
71 	int (*req_validate)(struct media_request *req);
72 	void (*req_queue)(struct media_request *req);
73 };
74 
75 /**
76  * struct media_device - Media device
77  * @dev:	Parent device
78  * @devnode:	Media device node
79  * @driver_name: Optional device driver name. If not set, calls to
80  *		%MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.
81  *		This is needed for USB drivers for example, as otherwise
82  *		they'll all appear as if the driver name was "usb".
83  * @model:	Device model name
84  * @serial:	Device serial number (optional)
85  * @bus_info:	Unique and stable device location identifier
86  * @hw_revision: Hardware device revision
87  * @topology_version: Monotonic counter for storing the version of the graph
88  *		topology. Should be incremented each time the topology changes.
89  * @id:		Unique ID used on the last registered graph object
90  * @entity_internal_idx: Unique internal entity ID used by the graph traversal
91  *		algorithms
92  * @entity_internal_idx_max: Allocated internal entity indices
93  * @entities:	List of registered entities
94  * @interfaces:	List of registered interfaces
95  * @pads:	List of registered pads
96  * @links:	List of registered links
97  * @entity_notify: List of registered entity_notify callbacks
98  * @graph_mutex: Protects access to struct media_device data
99  * @pm_count_walk: Graph walk for power state walk. Access serialised using
100  *		   graph_mutex.
101  *
102  * @source_priv: Driver Private data for enable/disable source handlers
103  * @enable_source: Enable Source Handler function pointer
104  * @disable_source: Disable Source Handler function pointer
105  *
106  * @ops:	Operation handler callbacks
107  * @req_queue_mutex: Serialise the MEDIA_REQUEST_IOC_QUEUE ioctl w.r.t.
108  *		     other operations that stop or start streaming.
109  * @request_id: Used to generate unique request IDs
110  *
111  * This structure represents an abstract high-level media device. It allows easy
112  * access to entities and provides basic media device-level support. The
113  * structure can be allocated directly or embedded in a larger structure.
114  *
115  * The parent @dev is a physical device. It must be set before registering the
116  * media device.
117  *
118  * @model is a descriptive model name exported through sysfs. It doesn't have to
119  * be unique.
120  *
121  * @enable_source is a handler to find source entity for the
122  * sink entity  and activate the link between them if source
123  * entity is free. Drivers should call this handler before
124  * accessing the source.
125  *
126  * @disable_source is a handler to find source entity for the
127  * sink entity  and deactivate the link between them. Drivers
128  * should call this handler to release the source.
129  *
130  * Use-case: find tuner entity connected to the decoder
131  * entity and check if it is available, and activate the
132  * link between them from @enable_source and deactivate
133  * from @disable_source.
134  *
135  * .. note::
136  *
137  *    Bridge driver is expected to implement and set the
138  *    handler when &media_device is registered or when
139  *    bridge driver finds the media_device during probe.
140  *    Bridge driver sets source_priv with information
141  *    necessary to run @enable_source and @disable_source handlers.
142  *    Callers should hold graph_mutex to access and call @enable_source
143  *    and @disable_source handlers.
144  */
145 struct media_device {
146 	/* dev->driver_data points to this struct. */
147 	struct device *dev;
148 	struct media_devnode *devnode;
149 
150 	char model[32];
151 	char driver_name[32];
152 	char serial[40];
153 	char bus_info[32];
154 	u32 hw_revision;
155 
156 	u64 topology_version;
157 
158 	u32 id;
159 	struct ida entity_internal_idx;
160 	int entity_internal_idx_max;
161 
162 	struct list_head entities;
163 	struct list_head interfaces;
164 	struct list_head pads;
165 	struct list_head links;
166 
167 	/* notify callback list invoked when a new entity is registered */
168 	struct list_head entity_notify;
169 
170 	/* Serializes graph operations. */
171 	struct mutex graph_mutex;
172 	struct media_graph pm_count_walk;
173 
174 	void *source_priv;
175 	int (*enable_source)(struct media_entity *entity,
176 			     struct media_pipeline *pipe);
177 	void (*disable_source)(struct media_entity *entity);
178 
179 	const struct media_device_ops *ops;
180 
181 	struct mutex req_queue_mutex;
182 	atomic_t request_id;
183 };
184 
185 /* We don't need to include usb.h here */
186 struct usb_device;
187 
188 #ifdef CONFIG_MEDIA_CONTROLLER
189 
190 /* Supported link_notify @notification values. */
191 #define MEDIA_DEV_NOTIFY_PRE_LINK_CH	0
192 #define MEDIA_DEV_NOTIFY_POST_LINK_CH	1
193 
194 /**
195  * media_entity_enum_init - Initialise an entity enumeration
196  *
197  * @ent_enum: Entity enumeration to be initialised
198  * @mdev: The related media device
199  *
200  * Return: zero on success or a negative error code.
201  */
media_entity_enum_init(struct media_entity_enum * ent_enum,struct media_device * mdev)202 static inline __must_check int media_entity_enum_init(
203 	struct media_entity_enum *ent_enum, struct media_device *mdev)
204 {
205 	return __media_entity_enum_init(ent_enum,
206 					mdev->entity_internal_idx_max + 1);
207 }
208 
209 /**
210  * media_device_init() - Initializes a media device element
211  *
212  * @mdev:	pointer to struct &media_device
213  *
214  * This function initializes the media device prior to its registration.
215  * The media device initialization and registration is split in two functions
216  * to avoid race conditions and make the media device available to user-space
217  * before the media graph has been completed.
218  *
219  * So drivers need to first initialize the media device, register any entity
220  * within the media device, create pad to pad links and then finally register
221  * the media device by calling media_device_register() as a final step.
222  *
223  * The caller is responsible for initializing the media device before
224  * registration. The following fields must be set:
225  *
226  * - dev must point to the parent device
227  * - model must be filled with the device model name
228  *
229  * The bus_info field is set by media_device_init() for PCI and platform devices
230  * if the field begins with '\0'.
231  */
232 void media_device_init(struct media_device *mdev);
233 
234 /**
235  * media_device_cleanup() - Cleanups a media device element
236  *
237  * @mdev:	pointer to struct &media_device
238  *
239  * This function that will destroy the graph_mutex that is
240  * initialized in media_device_init().
241  */
242 void media_device_cleanup(struct media_device *mdev);
243 
244 /**
245  * __media_device_register() - Registers a media device element
246  *
247  * @mdev:	pointer to struct &media_device
248  * @owner:	should be filled with %THIS_MODULE
249  *
250  * Users, should, instead, call the media_device_register() macro.
251  *
252  * The caller is responsible for initializing the &media_device structure
253  * before registration. The following fields of &media_device must be set:
254  *
255  *  - &media_device.model must be filled with the device model name as a
256  *    NUL-terminated UTF-8 string. The device/model revision must not be
257  *    stored in this field.
258  *
259  * The following fields are optional:
260  *
261  *  - &media_device.serial is a unique serial number stored as a
262  *    NUL-terminated ASCII string. The field is big enough to store a GUID
263  *    in text form. If the hardware doesn't provide a unique serial number
264  *    this field must be left empty.
265  *
266  *  - &media_device.bus_info represents the location of the device in the
267  *    system as a NUL-terminated ASCII string. For PCI/PCIe devices
268  *    &media_device.bus_info must be set to "PCI:" (or "PCIe:") followed by
269  *    the value of pci_name(). For USB devices,the usb_make_path() function
270  *    must be used. This field is used by applications to distinguish between
271  *    otherwise identical devices that don't provide a serial number.
272  *
273  *  - &media_device.hw_revision is the hardware device revision in a
274  *    driver-specific format. When possible the revision should be formatted
275  *    with the KERNEL_VERSION() macro.
276  *
277  * .. note::
278  *
279  *    #) 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.
280  *
281  *    #) Unregistering a media device that hasn't been registered is **NOT** safe.
282  *
283  * Return: returns zero on success or a negative error code.
284  */
285 int __must_check __media_device_register(struct media_device *mdev,
286 					 struct module *owner);
287 
288 
289 /**
290  * media_device_register() - Registers a media device element
291  *
292  * @mdev:	pointer to struct &media_device
293  *
294  * This macro calls __media_device_register() passing %THIS_MODULE as
295  * the __media_device_register() second argument (**owner**).
296  */
297 #define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
298 
299 /**
300  * media_device_unregister() - Unregisters a media device element
301  *
302  * @mdev:	pointer to struct &media_device
303  *
304  * It is safe to call this function on an unregistered (but initialised)
305  * media device.
306  */
307 void media_device_unregister(struct media_device *mdev);
308 
309 /**
310  * media_device_register_entity() - registers a media entity inside a
311  *	previously registered media device.
312  *
313  * @mdev:	pointer to struct &media_device
314  * @entity:	pointer to struct &media_entity to be registered
315  *
316  * Entities are identified by a unique positive integer ID. The media
317  * controller framework will such ID automatically. IDs are not guaranteed
318  * to be contiguous, and the ID number can change on newer Kernel versions.
319  * So, neither the driver nor userspace should hardcode ID numbers to refer
320  * to the entities, but, instead, use the framework to find the ID, when
321  * needed.
322  *
323  * The media_entity name, type and flags fields should be initialized before
324  * calling media_device_register_entity(). Entities embedded in higher-level
325  * standard structures can have some of those fields set by the higher-level
326  * framework.
327  *
328  * If the device has pads, media_entity_pads_init() should be called before
329  * this function. Otherwise, the &media_entity.pad and &media_entity.num_pads
330  * should be zeroed before calling this function.
331  *
332  * Entities have flags that describe the entity capabilities and state:
333  *
334  * %MEDIA_ENT_FL_DEFAULT
335  *    indicates the default entity for a given type.
336  *    This can be used to report the default audio and video devices or the
337  *    default camera sensor.
338  *
339  * .. note::
340  *
341  *    Drivers should set the entity function before calling this function.
342  *    Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and
343  *    %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
344  */
345 int __must_check media_device_register_entity(struct media_device *mdev,
346 					      struct media_entity *entity);
347 
348 /**
349  * media_device_unregister_entity() - unregisters a media entity.
350  *
351  * @entity:	pointer to struct &media_entity to be unregistered
352  *
353  * All links associated with the entity and all PADs are automatically
354  * unregistered from the media_device when this function is called.
355  *
356  * Unregistering an entity will not change the IDs of the other entities and
357  * the previoully used ID will never be reused for a newly registered entities.
358  *
359  * When a media device is unregistered, all its entities are unregistered
360  * automatically. No manual entities unregistration is then required.
361  *
362  * .. note::
363  *
364  *    The media_entity instance itself must be freed explicitly by
365  *    the driver if required.
366  */
367 void media_device_unregister_entity(struct media_entity *entity);
368 
369 /**
370  * media_device_register_entity_notify() - Registers a media entity_notify
371  *					   callback
372  *
373  * @mdev:      The media device
374  * @nptr:      The media_entity_notify
375  *
376  * .. note::
377  *
378  *    When a new entity is registered, all the registered
379  *    media_entity_notify callbacks are invoked.
380  */
381 
382 int __must_check media_device_register_entity_notify(struct media_device *mdev,
383 					struct media_entity_notify *nptr);
384 
385 /**
386  * media_device_unregister_entity_notify() - Unregister a media entity notify
387  *					     callback
388  *
389  * @mdev:      The media device
390  * @nptr:      The media_entity_notify
391  *
392  */
393 void media_device_unregister_entity_notify(struct media_device *mdev,
394 					struct media_entity_notify *nptr);
395 
396 /* Iterate over all entities. */
397 #define media_device_for_each_entity(entity, mdev)			\
398 	list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
399 
400 /* Iterate over all interfaces. */
401 #define media_device_for_each_intf(intf, mdev)			\
402 	list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
403 
404 /* Iterate over all pads. */
405 #define media_device_for_each_pad(pad, mdev)			\
406 	list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
407 
408 /* Iterate over all links. */
409 #define media_device_for_each_link(link, mdev)			\
410 	list_for_each_entry(link, &(mdev)->links, graph_obj.list)
411 
412 /**
413  * media_device_pci_init() - create and initialize a
414  *	struct &media_device from a PCI device.
415  *
416  * @mdev:	pointer to struct &media_device
417  * @pci_dev:	pointer to struct pci_dev
418  * @name:	media device name. If %NULL, the routine will use the default
419  *		name for the pci device, given by pci_name() macro.
420  */
421 void media_device_pci_init(struct media_device *mdev,
422 			   struct pci_dev *pci_dev,
423 			   const char *name);
424 /**
425  * __media_device_usb_init() - create and initialize a
426  *	struct &media_device from a PCI device.
427  *
428  * @mdev:	pointer to struct &media_device
429  * @udev:	pointer to struct usb_device
430  * @board_name:	media device name. If %NULL, the routine will use the usb
431  *		product name, if available.
432  * @driver_name: name of the driver. if %NULL, the routine will use the name
433  *		given by ``udev->dev->driver->name``, with is usually the wrong
434  *		thing to do.
435  *
436  * .. note::
437  *
438  *    It is better to call media_device_usb_init() instead, as
439  *    such macro fills driver_name with %KBUILD_MODNAME.
440  */
441 void __media_device_usb_init(struct media_device *mdev,
442 			     struct usb_device *udev,
443 			     const char *board_name,
444 			     const char *driver_name);
445 
446 #else
media_device_register(struct media_device * mdev)447 static inline int media_device_register(struct media_device *mdev)
448 {
449 	return 0;
450 }
media_device_unregister(struct media_device * mdev)451 static inline void media_device_unregister(struct media_device *mdev)
452 {
453 }
media_device_register_entity(struct media_device * mdev,struct media_entity * entity)454 static inline int media_device_register_entity(struct media_device *mdev,
455 						struct media_entity *entity)
456 {
457 	return 0;
458 }
media_device_unregister_entity(struct media_entity * entity)459 static inline void media_device_unregister_entity(struct media_entity *entity)
460 {
461 }
media_device_register_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)462 static inline int media_device_register_entity_notify(
463 					struct media_device *mdev,
464 					struct media_entity_notify *nptr)
465 {
466 	return 0;
467 }
media_device_unregister_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)468 static inline void media_device_unregister_entity_notify(
469 					struct media_device *mdev,
470 					struct media_entity_notify *nptr)
471 {
472 }
473 
media_device_pci_init(struct media_device * mdev,struct pci_dev * pci_dev,char * name)474 static inline void media_device_pci_init(struct media_device *mdev,
475 					 struct pci_dev *pci_dev,
476 					 char *name)
477 {
478 }
479 
__media_device_usb_init(struct media_device * mdev,struct usb_device * udev,char * board_name,char * driver_name)480 static inline void __media_device_usb_init(struct media_device *mdev,
481 					   struct usb_device *udev,
482 					   char *board_name,
483 					   char *driver_name)
484 {
485 }
486 
487 #endif /* CONFIG_MEDIA_CONTROLLER */
488 
489 /**
490  * media_device_usb_init() - create and initialize a
491  *	struct &media_device from a PCI device.
492  *
493  * @mdev:	pointer to struct &media_device
494  * @udev:	pointer to struct usb_device
495  * @name:	media device name. If %NULL, the routine will use the usb
496  *		product name, if available.
497  *
498  * This macro calls media_device_usb_init() passing the
499  * media_device_usb_init() **driver_name** parameter filled with
500  * %KBUILD_MODNAME.
501  */
502 #define media_device_usb_init(mdev, udev, name) \
503 	__media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
504 
505 /**
506  * media_set_bus_info() - Set bus_info field
507  *
508  * @bus_info:		Variable where to write the bus info (char array)
509  * @bus_info_size:	Length of the bus_info
510  * @dev:		Related struct device
511  *
512  * Sets bus information based on &dev. This is currently done for PCI and
513  * platform devices. dev is required to be non-NULL for this to happen.
514  *
515  * This function is not meant to be called from drivers.
516  */
517 static inline void
media_set_bus_info(char * bus_info,size_t bus_info_size,struct device * dev)518 media_set_bus_info(char *bus_info, size_t bus_info_size, struct device *dev)
519 {
520 	if (!dev)
521 		strscpy(bus_info, "no bus info", bus_info_size);
522 	else if (dev_is_platform(dev))
523 		snprintf(bus_info, bus_info_size, "platform:%s", dev_name(dev));
524 	else if (dev_is_pci(dev))
525 		snprintf(bus_info, bus_info_size, "PCI:%s", dev_name(dev));
526 }
527 
528 #endif
529