xref: /openbmc/linux/include/media/v4l2-dev.h (revision 6e63153d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *
4  *	V 4 L 2   D R I V E R   H E L P E R   A P I
5  *
6  * Moved from videodev2.h
7  *
8  *	Some commonly needed functions for drivers (v4l2-common.o module)
9  */
10 #ifndef _V4L2_DEV_H
11 #define _V4L2_DEV_H
12 
13 #include <linux/poll.h>
14 #include <linux/fs.h>
15 #include <linux/device.h>
16 #include <linux/cdev.h>
17 #include <linux/mutex.h>
18 #include <linux/videodev2.h>
19 
20 #include <media/media-entity.h>
21 
22 #define VIDEO_MAJOR	81
23 
24 /**
25  * enum vfl_devnode_type - type of V4L2 device node
26  *
27  * @VFL_TYPE_VIDEO:	for video input/output devices
28  * @VFL_TYPE_VBI:	for vertical blank data (i.e. closed captions, teletext)
29  * @VFL_TYPE_RADIO:	for radio tuners
30  * @VFL_TYPE_SUBDEV:	for V4L2 subdevices
31  * @VFL_TYPE_SDR:	for Software Defined Radio tuners
32  * @VFL_TYPE_TOUCH:	for touch sensors
33  * @VFL_TYPE_MAX:	number of VFL types, must always be last in the enum
34  */
35 enum vfl_devnode_type {
36 	VFL_TYPE_VIDEO,
37 	VFL_TYPE_VBI,
38 	VFL_TYPE_RADIO,
39 	VFL_TYPE_SUBDEV,
40 	VFL_TYPE_SDR,
41 	VFL_TYPE_TOUCH,
42 	VFL_TYPE_MAX /* Shall be the last one */
43 };
44 
45 /**
46  * enum  vfl_devnode_direction - Identifies if a &struct video_device
47  * 	corresponds to a receiver, a transmitter or a mem-to-mem device.
48  *
49  * @VFL_DIR_RX:		device is a receiver.
50  * @VFL_DIR_TX:		device is a transmitter.
51  * @VFL_DIR_M2M:	device is a memory to memory device.
52  *
53  * Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.
54  */
55 enum vfl_devnode_direction {
56 	VFL_DIR_RX,
57 	VFL_DIR_TX,
58 	VFL_DIR_M2M,
59 };
60 
61 struct v4l2_ioctl_callbacks;
62 struct video_device;
63 struct v4l2_device;
64 struct v4l2_ctrl_handler;
65 
66 /**
67  * enum v4l2_video_device_flags - Flags used by &struct video_device
68  *
69  * @V4L2_FL_REGISTERED:
70  *	indicates that a &struct video_device is registered.
71  *	Drivers can clear this flag if they want to block all future
72  *	device access. It is cleared by video_unregister_device.
73  * @V4L2_FL_USES_V4L2_FH:
74  *	indicates that file->private_data points to &struct v4l2_fh.
75  *	This flag is set by the core when v4l2_fh_init() is called.
76  *	All new drivers should use it.
77  * @V4L2_FL_QUIRK_INVERTED_CROP:
78  *	some old M2M drivers use g/s_crop/cropcap incorrectly: crop and
79  *	compose are swapped. If this flag is set, then the selection
80  *	targets are swapped in the g/s_crop/cropcap functions in v4l2-ioctl.c.
81  *	This allows those drivers to correctly implement the selection API,
82  *	but the old crop API will still work as expected in order to preserve
83  *	backwards compatibility.
84  *	Never set this flag for new drivers.
85  * @V4L2_FL_SUBDEV_RO_DEVNODE:
86  *	indicates that the video device node is registered in read-only mode.
87  *	The flag only applies to device nodes registered for sub-devices, it is
88  *	set by the core when the sub-devices device nodes are registered with
89  *	v4l2_device_register_ro_subdev_nodes() and used by the sub-device ioctl
90  *	handler to restrict access to some ioctl calls.
91  */
92 enum v4l2_video_device_flags {
93 	V4L2_FL_REGISTERED		= 0,
94 	V4L2_FL_USES_V4L2_FH		= 1,
95 	V4L2_FL_QUIRK_INVERTED_CROP	= 2,
96 	V4L2_FL_SUBDEV_RO_DEVNODE	= 3,
97 };
98 
99 /* Priority helper functions */
100 
101 /**
102  * struct v4l2_prio_state - stores the priority states
103  *
104  * @prios: array with elements to store the array priorities
105  *
106  *
107  * .. note::
108  *    The size of @prios array matches the number of priority types defined
109  *    by enum &v4l2_priority.
110  */
111 struct v4l2_prio_state {
112 	atomic_t prios[4];
113 };
114 
115 /**
116  * v4l2_prio_init - initializes a struct v4l2_prio_state
117  *
118  * @global: pointer to &struct v4l2_prio_state
119  */
120 void v4l2_prio_init(struct v4l2_prio_state *global);
121 
122 /**
123  * v4l2_prio_change - changes the v4l2 file handler priority
124  *
125  * @global: pointer to the &struct v4l2_prio_state of the device node.
126  * @local: pointer to the desired priority, as defined by enum &v4l2_priority
127  * @new: Priority type requested, as defined by enum &v4l2_priority.
128  *
129  * .. note::
130  *	This function should be used only by the V4L2 core.
131  */
132 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
133 		     enum v4l2_priority new);
134 
135 /**
136  * v4l2_prio_open - Implements the priority logic for a file handler open
137  *
138  * @global: pointer to the &struct v4l2_prio_state of the device node.
139  * @local: pointer to the desired priority, as defined by enum &v4l2_priority
140  *
141  * .. note::
142  *	This function should be used only by the V4L2 core.
143  */
144 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
145 
146 /**
147  * v4l2_prio_close - Implements the priority logic for a file handler close
148  *
149  * @global: pointer to the &struct v4l2_prio_state of the device node.
150  * @local: priority to be released, as defined by enum &v4l2_priority
151  *
152  * .. note::
153  *	This function should be used only by the V4L2 core.
154  */
155 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
156 
157 /**
158  * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
159  *
160  * @global: pointer to the &struct v4l2_prio_state of the device node.
161  *
162  * .. note::
163  *	This function should be used only by the V4L2 core.
164  */
165 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
166 
167 /**
168  * v4l2_prio_check - Implements the priority logic for a file handler close
169  *
170  * @global: pointer to the &struct v4l2_prio_state of the device node.
171  * @local: desired priority, as defined by enum &v4l2_priority local
172  *
173  * .. note::
174  *	This function should be used only by the V4L2 core.
175  */
176 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
177 
178 /**
179  * struct v4l2_file_operations - fs operations used by a V4L2 device
180  *
181  * @owner: pointer to struct module
182  * @read: operations needed to implement the read() syscall
183  * @write: operations needed to implement the write() syscall
184  * @poll: operations needed to implement the poll() syscall
185  * @unlocked_ioctl: operations needed to implement the ioctl() syscall
186  * @compat_ioctl32: operations needed to implement the ioctl() syscall for
187  *	the special case where the Kernel uses 64 bits instructions, but
188  *	the userspace uses 32 bits.
189  * @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU
190  * @mmap: operations needed to implement the mmap() syscall
191  * @open: operations needed to implement the open() syscall
192  * @release: operations needed to implement the release() syscall
193  *
194  * .. note::
195  *
196  *	Those operations are used to implemente the fs struct file_operations
197  *	at the V4L2 drivers. The V4L2 core overrides the fs ops with some
198  *	extra logic needed by the subsystem.
199  */
200 struct v4l2_file_operations {
201 	struct module *owner;
202 	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
203 	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
204 	__poll_t (*poll) (struct file *, struct poll_table_struct *);
205 	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
206 #ifdef CONFIG_COMPAT
207 	long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
208 #endif
209 	unsigned long (*get_unmapped_area) (struct file *, unsigned long,
210 				unsigned long, unsigned long, unsigned long);
211 	int (*mmap) (struct file *, struct vm_area_struct *);
212 	int (*open) (struct file *);
213 	int (*release) (struct file *);
214 };
215 
216 /*
217  * Newer version of video_device, handled by videodev2.c
218  *	This version moves redundant code from video device code to
219  *	the common handler
220  */
221 
222 /**
223  * struct video_device - Structure used to create and manage the V4L2 device
224  *	nodes.
225  *
226  * @entity: &struct media_entity
227  * @intf_devnode: pointer to &struct media_intf_devnode
228  * @pipe: &struct media_pipeline
229  * @fops: pointer to &struct v4l2_file_operations for the video device
230  * @device_caps: device capabilities as used in v4l2_capabilities
231  * @dev: &struct device for the video device
232  * @cdev: character device
233  * @v4l2_dev: pointer to &struct v4l2_device parent
234  * @dev_parent: pointer to &struct device parent
235  * @ctrl_handler: Control handler associated with this device node.
236  *	 May be NULL.
237  * @queue: &struct vb2_queue associated with this device node. May be NULL.
238  * @prio: pointer to &struct v4l2_prio_state with device's Priority state.
239  *	 If NULL, then v4l2_dev->prio will be used.
240  * @name: video device name
241  * @vfl_type: V4L device type, as defined by &enum vfl_devnode_type
242  * @vfl_dir: V4L receiver, transmitter or m2m
243  * @minor: device node 'minor'. It is set to -1 if the registration failed
244  * @num: number of the video device node
245  * @flags: video device flags. Use bitops to set/clear/test flags.
246  *	   Contains a set of &enum v4l2_video_device_flags.
247  * @index: attribute to differentiate multiple indices on one physical device
248  * @fh_lock: Lock for all v4l2_fhs
249  * @fh_list: List of &struct v4l2_fh
250  * @dev_debug: Internal device debug flags, not for use by drivers
251  * @tvnorms: Supported tv norms
252  *
253  * @release: video device release() callback
254  * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
255  *
256  * @valid_ioctls: bitmap with the valid ioctls for this device
257  * @lock: pointer to &struct mutex serialization lock
258  *
259  * .. note::
260  *	Only set @dev_parent if that can't be deduced from @v4l2_dev.
261  */
262 
263 struct video_device {
264 #if defined(CONFIG_MEDIA_CONTROLLER)
265 	struct media_entity entity;
266 	struct media_intf_devnode *intf_devnode;
267 	struct media_pipeline pipe;
268 #endif
269 	const struct v4l2_file_operations *fops;
270 
271 	u32 device_caps;
272 
273 	/* sysfs */
274 	struct device dev;
275 	struct cdev *cdev;
276 
277 	struct v4l2_device *v4l2_dev;
278 	struct device *dev_parent;
279 
280 	struct v4l2_ctrl_handler *ctrl_handler;
281 
282 	struct vb2_queue *queue;
283 
284 	struct v4l2_prio_state *prio;
285 
286 	/* device info */
287 	char name[32];
288 	enum vfl_devnode_type vfl_type;
289 	enum vfl_devnode_direction vfl_dir;
290 	int minor;
291 	u16 num;
292 	unsigned long flags;
293 	int index;
294 
295 	/* V4L2 file handles */
296 	spinlock_t		fh_lock;
297 	struct list_head	fh_list;
298 
299 	int dev_debug;
300 
301 	v4l2_std_id tvnorms;
302 
303 	/* callbacks */
304 	void (*release)(struct video_device *vdev);
305 	const struct v4l2_ioctl_ops *ioctl_ops;
306 	DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
307 
308 	struct mutex *lock;
309 };
310 
311 /**
312  * media_entity_to_video_device - Returns a &struct video_device from
313  *	the &struct media_entity embedded on it.
314  *
315  * @__entity: pointer to &struct media_entity
316  */
317 #define media_entity_to_video_device(__entity) \
318 	container_of(__entity, struct video_device, entity)
319 
320 /**
321  * to_video_device - Returns a &struct video_device from the
322  *	&struct device embedded on it.
323  *
324  * @cd: pointer to &struct device
325  */
326 #define to_video_device(cd) container_of(cd, struct video_device, dev)
327 
328 /**
329  * __video_register_device - register video4linux devices
330  *
331  * @vdev: struct video_device to register
332  * @type: type of device to register, as defined by &enum vfl_devnode_type
333  * @nr:   which device node number is desired:
334  *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
335  * @warn_if_nr_in_use: warn if the desired device node number
336  *        was already in use and another number was chosen instead.
337  * @owner: module that owns the video device node
338  *
339  * The registration code assigns minor numbers and device node numbers
340  * based on the requested type and registers the new device node with
341  * the kernel.
342  *
343  * This function assumes that struct video_device was zeroed when it
344  * was allocated and does not contain any stale date.
345  *
346  * An error is returned if no free minor or device node number could be
347  * found, or if the registration of the device node failed.
348  *
349  * Returns 0 on success.
350  *
351  * .. note::
352  *
353  *	This function is meant to be used only inside the V4L2 core.
354  *	Drivers should use video_register_device() or
355  *	video_register_device_no_warn().
356  */
357 int __must_check __video_register_device(struct video_device *vdev,
358 					 enum vfl_devnode_type type,
359 					 int nr, int warn_if_nr_in_use,
360 					 struct module *owner);
361 
362 /**
363  *  video_register_device - register video4linux devices
364  *
365  * @vdev: struct video_device to register
366  * @type: type of device to register, as defined by &enum vfl_devnode_type
367  * @nr:   which device node number is desired:
368  *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
369  *
370  * Internally, it calls __video_register_device(). Please see its
371  * documentation for more details.
372  *
373  * .. note::
374  *	if video_register_device fails, the release() callback of
375  *	&struct video_device structure is *not* called, so the caller
376  *	is responsible for freeing any data. Usually that means that
377  *	you video_device_release() should be called on failure.
378  */
379 static inline int __must_check video_register_device(struct video_device *vdev,
380 						     enum vfl_devnode_type type,
381 						     int nr)
382 {
383 	return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
384 }
385 
386 /**
387  *  video_register_device_no_warn - register video4linux devices
388  *
389  * @vdev: struct video_device to register
390  * @type: type of device to register, as defined by &enum vfl_devnode_type
391  * @nr:   which device node number is desired:
392  *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
393  *
394  * This function is identical to video_register_device() except that no
395  * warning is issued if the desired device node number was already in use.
396  *
397  * Internally, it calls __video_register_device(). Please see its
398  * documentation for more details.
399  *
400  * .. note::
401  *	if video_register_device fails, the release() callback of
402  *	&struct video_device structure is *not* called, so the caller
403  *	is responsible for freeing any data. Usually that means that
404  *	you video_device_release() should be called on failure.
405  */
406 static inline int __must_check
407 video_register_device_no_warn(struct video_device *vdev,
408 			      enum vfl_devnode_type type, int nr)
409 {
410 	return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
411 }
412 
413 /**
414  * video_unregister_device - Unregister video devices.
415  *
416  * @vdev: &struct video_device to register
417  *
418  * Does nothing if vdev == NULL or if video_is_registered() returns false.
419  */
420 void video_unregister_device(struct video_device *vdev);
421 
422 /**
423  * video_device_alloc - helper function to alloc &struct video_device
424  *
425  * Returns NULL if %-ENOMEM or a &struct video_device on success.
426  */
427 struct video_device * __must_check video_device_alloc(void);
428 
429 /**
430  * video_device_release - helper function to release &struct video_device
431  *
432  * @vdev: pointer to &struct video_device
433  *
434  * Can also be used for video_device->release\(\).
435  */
436 void video_device_release(struct video_device *vdev);
437 
438 /**
439  * video_device_release_empty - helper function to implement the
440  *	video_device->release\(\) callback.
441  *
442  * @vdev: pointer to &struct video_device
443  *
444  * This release function does nothing.
445  *
446  * It should be used when the video_device is a static global struct.
447  *
448  * .. note::
449  *	Having a static video_device is a dubious construction at best.
450  */
451 void video_device_release_empty(struct video_device *vdev);
452 
453 /**
454  * v4l2_disable_ioctl- mark that a given command isn't implemented.
455  *	shouldn't use core locking
456  *
457  * @vdev: pointer to &struct video_device
458  * @cmd: ioctl command
459  *
460  * This function allows drivers to provide just one v4l2_ioctl_ops struct, but
461  * disable ioctls based on the specific card that is actually found.
462  *
463  * .. note::
464  *
465  *    This must be called before video_register_device.
466  *    See also the comments for determine_valid_ioctls().
467  */
468 static inline void v4l2_disable_ioctl(struct video_device *vdev,
469 				      unsigned int cmd)
470 {
471 	if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
472 		set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
473 }
474 
475 /**
476  * video_get_drvdata - gets private data from &struct video_device.
477  *
478  * @vdev: pointer to &struct video_device
479  *
480  * returns a pointer to the private data
481  */
482 static inline void *video_get_drvdata(struct video_device *vdev)
483 {
484 	return dev_get_drvdata(&vdev->dev);
485 }
486 
487 /**
488  * video_set_drvdata - sets private data from &struct video_device.
489  *
490  * @vdev: pointer to &struct video_device
491  * @data: private data pointer
492  */
493 static inline void video_set_drvdata(struct video_device *vdev, void *data)
494 {
495 	dev_set_drvdata(&vdev->dev, data);
496 }
497 
498 /**
499  * video_devdata - gets &struct video_device from struct file.
500  *
501  * @file: pointer to struct file
502  */
503 struct video_device *video_devdata(struct file *file);
504 
505 /**
506  * video_drvdata - gets private data from &struct video_device using the
507  *	struct file.
508  *
509  * @file: pointer to struct file
510  *
511  * This is function combines both video_get_drvdata() and video_devdata()
512  * as this is used very often.
513  */
514 static inline void *video_drvdata(struct file *file)
515 {
516 	return video_get_drvdata(video_devdata(file));
517 }
518 
519 /**
520  * video_device_node_name - returns the video device name
521  *
522  * @vdev: pointer to &struct video_device
523  *
524  * Returns the device name string
525  */
526 static inline const char *video_device_node_name(struct video_device *vdev)
527 {
528 	return dev_name(&vdev->dev);
529 }
530 
531 /**
532  * video_is_registered - returns true if the &struct video_device is registered.
533  *
534  *
535  * @vdev: pointer to &struct video_device
536  */
537 static inline int video_is_registered(struct video_device *vdev)
538 {
539 	return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
540 }
541 
542 #if defined(CONFIG_MEDIA_CONTROLLER)
543 
544 /**
545  * video_device_pipeline_start - Mark a pipeline as streaming
546  * @vdev: Starting video device
547  * @pipe: Media pipeline to be assigned to all entities in the pipeline.
548  *
549  * Mark all entities connected to a given video device through enabled links,
550  * either directly or indirectly, as streaming. The given pipeline object is
551  * assigned to every pad in the pipeline and stored in the media_pad pipe
552  * field.
553  *
554  * Calls to this function can be nested, in which case the same number of
555  * video_device_pipeline_stop() calls will be required to stop streaming. The
556  * pipeline pointer must be identical for all nested calls to
557  * video_device_pipeline_start().
558  *
559  * The video device must contain a single pad.
560  *
561  * This is a convenience wrapper around media_pipeline_start().
562  */
563 __must_check int video_device_pipeline_start(struct video_device *vdev,
564 					     struct media_pipeline *pipe);
565 
566 /**
567  * __video_device_pipeline_start - Mark a pipeline as streaming
568  * @vdev: Starting video device
569  * @pipe: Media pipeline to be assigned to all entities in the pipeline.
570  *
571  * ..note:: This is the non-locking version of video_device_pipeline_start()
572  *
573  * The video device must contain a single pad.
574  *
575  * This is a convenience wrapper around __media_pipeline_start().
576  */
577 __must_check int __video_device_pipeline_start(struct video_device *vdev,
578 					       struct media_pipeline *pipe);
579 
580 /**
581  * video_device_pipeline_stop - Mark a pipeline as not streaming
582  * @vdev: Starting video device
583  *
584  * Mark all entities connected to a given video device through enabled links,
585  * either directly or indirectly, as not streaming. The media_pad pipe field
586  * is reset to %NULL.
587  *
588  * If multiple calls to media_pipeline_start() have been made, the same
589  * number of calls to this function are required to mark the pipeline as not
590  * streaming.
591  *
592  * The video device must contain a single pad.
593  *
594  * This is a convenience wrapper around media_pipeline_stop().
595  */
596 void video_device_pipeline_stop(struct video_device *vdev);
597 
598 /**
599  * __video_device_pipeline_stop - Mark a pipeline as not streaming
600  * @vdev: Starting video device
601  *
602  * .. note:: This is the non-locking version of media_pipeline_stop()
603  *
604  * The video device must contain a single pad.
605  *
606  * This is a convenience wrapper around __media_pipeline_stop().
607  */
608 void __video_device_pipeline_stop(struct video_device *vdev);
609 
610 /**
611  * video_device_pipeline_alloc_start - Mark a pipeline as streaming
612  * @vdev: Starting video device
613  *
614  * video_device_pipeline_alloc_start() is similar to video_device_pipeline_start()
615  * but instead of working on a given pipeline the function will use an
616  * existing pipeline if the video device is already part of a pipeline, or
617  * allocate a new pipeline.
618  *
619  * Calls to video_device_pipeline_alloc_start() must be matched with
620  * video_device_pipeline_stop().
621  */
622 __must_check int video_device_pipeline_alloc_start(struct video_device *vdev);
623 
624 /**
625  * video_device_pipeline - Get the media pipeline a video device is part of
626  * @vdev: The video device
627  *
628  * This function returns the media pipeline that a video device has been
629  * associated with when constructing the pipeline with
630  * video_device_pipeline_start(). The pointer remains valid until
631  * video_device_pipeline_stop() is called.
632  *
633  * Return: The media_pipeline the video device is part of, or NULL if the video
634  * device is not part of any pipeline.
635  *
636  * The video device must contain a single pad.
637  *
638  * This is a convenience wrapper around media_entity_pipeline().
639  */
640 struct media_pipeline *video_device_pipeline(struct video_device *vdev);
641 
642 #endif /* CONFIG_MEDIA_CONTROLLER */
643 
644 #endif /* _V4L2_DEV_H */
645