162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci *	V 4 L 2   D R I V E R   H E L P E R   A P I
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Moved from videodev2.h
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci *	Some commonly needed functions for drivers (v4l2-common.o module)
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci#ifndef _V4L2_DEV_H
1162306a36Sopenharmony_ci#define _V4L2_DEV_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/poll.h>
1462306a36Sopenharmony_ci#include <linux/fs.h>
1562306a36Sopenharmony_ci#include <linux/device.h>
1662306a36Sopenharmony_ci#include <linux/cdev.h>
1762306a36Sopenharmony_ci#include <linux/mutex.h>
1862306a36Sopenharmony_ci#include <linux/videodev2.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <media/media-entity.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define VIDEO_MAJOR	81
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/**
2562306a36Sopenharmony_ci * enum vfl_devnode_type - type of V4L2 device node
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * @VFL_TYPE_VIDEO:	for video input/output devices
2862306a36Sopenharmony_ci * @VFL_TYPE_VBI:	for vertical blank data (i.e. closed captions, teletext)
2962306a36Sopenharmony_ci * @VFL_TYPE_RADIO:	for radio tuners
3062306a36Sopenharmony_ci * @VFL_TYPE_SUBDEV:	for V4L2 subdevices
3162306a36Sopenharmony_ci * @VFL_TYPE_SDR:	for Software Defined Radio tuners
3262306a36Sopenharmony_ci * @VFL_TYPE_TOUCH:	for touch sensors
3362306a36Sopenharmony_ci * @VFL_TYPE_MAX:	number of VFL types, must always be last in the enum
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_cienum vfl_devnode_type {
3662306a36Sopenharmony_ci	VFL_TYPE_VIDEO,
3762306a36Sopenharmony_ci	VFL_TYPE_VBI,
3862306a36Sopenharmony_ci	VFL_TYPE_RADIO,
3962306a36Sopenharmony_ci	VFL_TYPE_SUBDEV,
4062306a36Sopenharmony_ci	VFL_TYPE_SDR,
4162306a36Sopenharmony_ci	VFL_TYPE_TOUCH,
4262306a36Sopenharmony_ci	VFL_TYPE_MAX /* Shall be the last one */
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/**
4662306a36Sopenharmony_ci * enum  vfl_devnode_direction - Identifies if a &struct video_device
4762306a36Sopenharmony_ci * 	corresponds to a receiver, a transmitter or a mem-to-mem device.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * @VFL_DIR_RX:		device is a receiver.
5062306a36Sopenharmony_ci * @VFL_DIR_TX:		device is a transmitter.
5162306a36Sopenharmony_ci * @VFL_DIR_M2M:	device is a memory to memory device.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_cienum vfl_devnode_direction {
5662306a36Sopenharmony_ci	VFL_DIR_RX,
5762306a36Sopenharmony_ci	VFL_DIR_TX,
5862306a36Sopenharmony_ci	VFL_DIR_M2M,
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistruct v4l2_ioctl_callbacks;
6262306a36Sopenharmony_cistruct video_device;
6362306a36Sopenharmony_cistruct v4l2_device;
6462306a36Sopenharmony_cistruct v4l2_ctrl_handler;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * enum v4l2_video_device_flags - Flags used by &struct video_device
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * @V4L2_FL_REGISTERED:
7062306a36Sopenharmony_ci *	indicates that a &struct video_device is registered.
7162306a36Sopenharmony_ci *	Drivers can clear this flag if they want to block all future
7262306a36Sopenharmony_ci *	device access. It is cleared by video_unregister_device.
7362306a36Sopenharmony_ci * @V4L2_FL_USES_V4L2_FH:
7462306a36Sopenharmony_ci *	indicates that file->private_data points to &struct v4l2_fh.
7562306a36Sopenharmony_ci *	This flag is set by the core when v4l2_fh_init() is called.
7662306a36Sopenharmony_ci *	All new drivers should use it.
7762306a36Sopenharmony_ci * @V4L2_FL_QUIRK_INVERTED_CROP:
7862306a36Sopenharmony_ci *	some old M2M drivers use g/s_crop/cropcap incorrectly: crop and
7962306a36Sopenharmony_ci *	compose are swapped. If this flag is set, then the selection
8062306a36Sopenharmony_ci *	targets are swapped in the g/s_crop/cropcap functions in v4l2-ioctl.c.
8162306a36Sopenharmony_ci *	This allows those drivers to correctly implement the selection API,
8262306a36Sopenharmony_ci *	but the old crop API will still work as expected in order to preserve
8362306a36Sopenharmony_ci *	backwards compatibility.
8462306a36Sopenharmony_ci *	Never set this flag for new drivers.
8562306a36Sopenharmony_ci * @V4L2_FL_SUBDEV_RO_DEVNODE:
8662306a36Sopenharmony_ci *	indicates that the video device node is registered in read-only mode.
8762306a36Sopenharmony_ci *	The flag only applies to device nodes registered for sub-devices, it is
8862306a36Sopenharmony_ci *	set by the core when the sub-devices device nodes are registered with
8962306a36Sopenharmony_ci *	v4l2_device_register_ro_subdev_nodes() and used by the sub-device ioctl
9062306a36Sopenharmony_ci *	handler to restrict access to some ioctl calls.
9162306a36Sopenharmony_ci */
9262306a36Sopenharmony_cienum v4l2_video_device_flags {
9362306a36Sopenharmony_ci	V4L2_FL_REGISTERED		= 0,
9462306a36Sopenharmony_ci	V4L2_FL_USES_V4L2_FH		= 1,
9562306a36Sopenharmony_ci	V4L2_FL_QUIRK_INVERTED_CROP	= 2,
9662306a36Sopenharmony_ci	V4L2_FL_SUBDEV_RO_DEVNODE	= 3,
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/* Priority helper functions */
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/**
10262306a36Sopenharmony_ci * struct v4l2_prio_state - stores the priority states
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * @prios: array with elements to store the array priorities
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci *
10762306a36Sopenharmony_ci * .. note::
10862306a36Sopenharmony_ci *    The size of @prios array matches the number of priority types defined
10962306a36Sopenharmony_ci *    by enum &v4l2_priority.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_cistruct v4l2_prio_state {
11262306a36Sopenharmony_ci	atomic_t prios[4];
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/**
11662306a36Sopenharmony_ci * v4l2_prio_init - initializes a struct v4l2_prio_state
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * @global: pointer to &struct v4l2_prio_state
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_civoid v4l2_prio_init(struct v4l2_prio_state *global);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci/**
12362306a36Sopenharmony_ci * v4l2_prio_change - changes the v4l2 file handler priority
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * @global: pointer to the &struct v4l2_prio_state of the device node.
12662306a36Sopenharmony_ci * @local: pointer to the desired priority, as defined by enum &v4l2_priority
12762306a36Sopenharmony_ci * @new: Priority type requested, as defined by enum &v4l2_priority.
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * .. note::
13062306a36Sopenharmony_ci *	This function should be used only by the V4L2 core.
13162306a36Sopenharmony_ci */
13262306a36Sopenharmony_ciint v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
13362306a36Sopenharmony_ci		     enum v4l2_priority new);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci/**
13662306a36Sopenharmony_ci * v4l2_prio_open - Implements the priority logic for a file handler open
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * @global: pointer to the &struct v4l2_prio_state of the device node.
13962306a36Sopenharmony_ci * @local: pointer to the desired priority, as defined by enum &v4l2_priority
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci * .. note::
14262306a36Sopenharmony_ci *	This function should be used only by the V4L2 core.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_civoid v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/**
14762306a36Sopenharmony_ci * v4l2_prio_close - Implements the priority logic for a file handler close
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * @global: pointer to the &struct v4l2_prio_state of the device node.
15062306a36Sopenharmony_ci * @local: priority to be released, as defined by enum &v4l2_priority
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci * .. note::
15362306a36Sopenharmony_ci *	This function should be used only by the V4L2 core.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_civoid v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/**
15862306a36Sopenharmony_ci * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
15962306a36Sopenharmony_ci *
16062306a36Sopenharmony_ci * @global: pointer to the &struct v4l2_prio_state of the device node.
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * .. note::
16362306a36Sopenharmony_ci *	This function should be used only by the V4L2 core.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_cienum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/**
16862306a36Sopenharmony_ci * v4l2_prio_check - Implements the priority logic for a file handler close
16962306a36Sopenharmony_ci *
17062306a36Sopenharmony_ci * @global: pointer to the &struct v4l2_prio_state of the device node.
17162306a36Sopenharmony_ci * @local: desired priority, as defined by enum &v4l2_priority local
17262306a36Sopenharmony_ci *
17362306a36Sopenharmony_ci * .. note::
17462306a36Sopenharmony_ci *	This function should be used only by the V4L2 core.
17562306a36Sopenharmony_ci */
17662306a36Sopenharmony_ciint v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci/**
17962306a36Sopenharmony_ci * struct v4l2_file_operations - fs operations used by a V4L2 device
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci * @owner: pointer to struct module
18262306a36Sopenharmony_ci * @read: operations needed to implement the read() syscall
18362306a36Sopenharmony_ci * @write: operations needed to implement the write() syscall
18462306a36Sopenharmony_ci * @poll: operations needed to implement the poll() syscall
18562306a36Sopenharmony_ci * @unlocked_ioctl: operations needed to implement the ioctl() syscall
18662306a36Sopenharmony_ci * @compat_ioctl32: operations needed to implement the ioctl() syscall for
18762306a36Sopenharmony_ci *	the special case where the Kernel uses 64 bits instructions, but
18862306a36Sopenharmony_ci *	the userspace uses 32 bits.
18962306a36Sopenharmony_ci * @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU
19062306a36Sopenharmony_ci * @mmap: operations needed to implement the mmap() syscall
19162306a36Sopenharmony_ci * @open: operations needed to implement the open() syscall
19262306a36Sopenharmony_ci * @release: operations needed to implement the release() syscall
19362306a36Sopenharmony_ci *
19462306a36Sopenharmony_ci * .. note::
19562306a36Sopenharmony_ci *
19662306a36Sopenharmony_ci *	Those operations are used to implemente the fs struct file_operations
19762306a36Sopenharmony_ci *	at the V4L2 drivers. The V4L2 core overrides the fs ops with some
19862306a36Sopenharmony_ci *	extra logic needed by the subsystem.
19962306a36Sopenharmony_ci */
20062306a36Sopenharmony_cistruct v4l2_file_operations {
20162306a36Sopenharmony_ci	struct module *owner;
20262306a36Sopenharmony_ci	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
20362306a36Sopenharmony_ci	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
20462306a36Sopenharmony_ci	__poll_t (*poll) (struct file *, struct poll_table_struct *);
20562306a36Sopenharmony_ci	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
20662306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
20762306a36Sopenharmony_ci	long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
20862306a36Sopenharmony_ci#endif
20962306a36Sopenharmony_ci	unsigned long (*get_unmapped_area) (struct file *, unsigned long,
21062306a36Sopenharmony_ci				unsigned long, unsigned long, unsigned long);
21162306a36Sopenharmony_ci	int (*mmap) (struct file *, struct vm_area_struct *);
21262306a36Sopenharmony_ci	int (*open) (struct file *);
21362306a36Sopenharmony_ci	int (*release) (struct file *);
21462306a36Sopenharmony_ci};
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci/*
21762306a36Sopenharmony_ci * Newer version of video_device, handled by videodev2.c
21862306a36Sopenharmony_ci *	This version moves redundant code from video device code to
21962306a36Sopenharmony_ci *	the common handler
22062306a36Sopenharmony_ci */
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/**
22362306a36Sopenharmony_ci * struct video_device - Structure used to create and manage the V4L2 device
22462306a36Sopenharmony_ci *	nodes.
22562306a36Sopenharmony_ci *
22662306a36Sopenharmony_ci * @entity: &struct media_entity
22762306a36Sopenharmony_ci * @intf_devnode: pointer to &struct media_intf_devnode
22862306a36Sopenharmony_ci * @pipe: &struct media_pipeline
22962306a36Sopenharmony_ci * @fops: pointer to &struct v4l2_file_operations for the video device
23062306a36Sopenharmony_ci * @device_caps: device capabilities as used in v4l2_capabilities
23162306a36Sopenharmony_ci * @dev: &struct device for the video device
23262306a36Sopenharmony_ci * @cdev: character device
23362306a36Sopenharmony_ci * @v4l2_dev: pointer to &struct v4l2_device parent
23462306a36Sopenharmony_ci * @dev_parent: pointer to &struct device parent
23562306a36Sopenharmony_ci * @ctrl_handler: Control handler associated with this device node.
23662306a36Sopenharmony_ci *	 May be NULL.
23762306a36Sopenharmony_ci * @queue: &struct vb2_queue associated with this device node. May be NULL.
23862306a36Sopenharmony_ci * @prio: pointer to &struct v4l2_prio_state with device's Priority state.
23962306a36Sopenharmony_ci *	 If NULL, then v4l2_dev->prio will be used.
24062306a36Sopenharmony_ci * @name: video device name
24162306a36Sopenharmony_ci * @vfl_type: V4L device type, as defined by &enum vfl_devnode_type
24262306a36Sopenharmony_ci * @vfl_dir: V4L receiver, transmitter or m2m
24362306a36Sopenharmony_ci * @minor: device node 'minor'. It is set to -1 if the registration failed
24462306a36Sopenharmony_ci * @num: number of the video device node
24562306a36Sopenharmony_ci * @flags: video device flags. Use bitops to set/clear/test flags.
24662306a36Sopenharmony_ci *	   Contains a set of &enum v4l2_video_device_flags.
24762306a36Sopenharmony_ci * @index: attribute to differentiate multiple indices on one physical device
24862306a36Sopenharmony_ci * @fh_lock: Lock for all v4l2_fhs
24962306a36Sopenharmony_ci * @fh_list: List of &struct v4l2_fh
25062306a36Sopenharmony_ci * @dev_debug: Internal device debug flags, not for use by drivers
25162306a36Sopenharmony_ci * @tvnorms: Supported tv norms
25262306a36Sopenharmony_ci *
25362306a36Sopenharmony_ci * @release: video device release() callback
25462306a36Sopenharmony_ci * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
25562306a36Sopenharmony_ci *
25662306a36Sopenharmony_ci * @valid_ioctls: bitmap with the valid ioctls for this device
25762306a36Sopenharmony_ci * @lock: pointer to &struct mutex serialization lock
25862306a36Sopenharmony_ci *
25962306a36Sopenharmony_ci * .. note::
26062306a36Sopenharmony_ci *	Only set @dev_parent if that can't be deduced from @v4l2_dev.
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_cistruct video_device {
26462306a36Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
26562306a36Sopenharmony_ci	struct media_entity entity;
26662306a36Sopenharmony_ci	struct media_intf_devnode *intf_devnode;
26762306a36Sopenharmony_ci	struct media_pipeline pipe;
26862306a36Sopenharmony_ci#endif
26962306a36Sopenharmony_ci	const struct v4l2_file_operations *fops;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	u32 device_caps;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* sysfs */
27462306a36Sopenharmony_ci	struct device dev;
27562306a36Sopenharmony_ci	struct cdev *cdev;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	struct v4l2_device *v4l2_dev;
27862306a36Sopenharmony_ci	struct device *dev_parent;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	struct v4l2_ctrl_handler *ctrl_handler;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	struct vb2_queue *queue;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	struct v4l2_prio_state *prio;
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	/* device info */
28762306a36Sopenharmony_ci	char name[32];
28862306a36Sopenharmony_ci	enum vfl_devnode_type vfl_type;
28962306a36Sopenharmony_ci	enum vfl_devnode_direction vfl_dir;
29062306a36Sopenharmony_ci	int minor;
29162306a36Sopenharmony_ci	u16 num;
29262306a36Sopenharmony_ci	unsigned long flags;
29362306a36Sopenharmony_ci	int index;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	/* V4L2 file handles */
29662306a36Sopenharmony_ci	spinlock_t		fh_lock;
29762306a36Sopenharmony_ci	struct list_head	fh_list;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	int dev_debug;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	v4l2_std_id tvnorms;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	/* callbacks */
30462306a36Sopenharmony_ci	void (*release)(struct video_device *vdev);
30562306a36Sopenharmony_ci	const struct v4l2_ioctl_ops *ioctl_ops;
30662306a36Sopenharmony_ci	DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	struct mutex *lock;
30962306a36Sopenharmony_ci};
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci/**
31262306a36Sopenharmony_ci * media_entity_to_video_device - Returns a &struct video_device from
31362306a36Sopenharmony_ci *	the &struct media_entity embedded on it.
31462306a36Sopenharmony_ci *
31562306a36Sopenharmony_ci * @__entity: pointer to &struct media_entity
31662306a36Sopenharmony_ci */
31762306a36Sopenharmony_ci#define media_entity_to_video_device(__entity) \
31862306a36Sopenharmony_ci	container_of(__entity, struct video_device, entity)
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci/**
32162306a36Sopenharmony_ci * to_video_device - Returns a &struct video_device from the
32262306a36Sopenharmony_ci *	&struct device embedded on it.
32362306a36Sopenharmony_ci *
32462306a36Sopenharmony_ci * @cd: pointer to &struct device
32562306a36Sopenharmony_ci */
32662306a36Sopenharmony_ci#define to_video_device(cd) container_of(cd, struct video_device, dev)
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci/**
32962306a36Sopenharmony_ci * __video_register_device - register video4linux devices
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * @vdev: struct video_device to register
33262306a36Sopenharmony_ci * @type: type of device to register, as defined by &enum vfl_devnode_type
33362306a36Sopenharmony_ci * @nr:   which device node number is desired:
33462306a36Sopenharmony_ci *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
33562306a36Sopenharmony_ci * @warn_if_nr_in_use: warn if the desired device node number
33662306a36Sopenharmony_ci *        was already in use and another number was chosen instead.
33762306a36Sopenharmony_ci * @owner: module that owns the video device node
33862306a36Sopenharmony_ci *
33962306a36Sopenharmony_ci * The registration code assigns minor numbers and device node numbers
34062306a36Sopenharmony_ci * based on the requested type and registers the new device node with
34162306a36Sopenharmony_ci * the kernel.
34262306a36Sopenharmony_ci *
34362306a36Sopenharmony_ci * This function assumes that struct video_device was zeroed when it
34462306a36Sopenharmony_ci * was allocated and does not contain any stale date.
34562306a36Sopenharmony_ci *
34662306a36Sopenharmony_ci * An error is returned if no free minor or device node number could be
34762306a36Sopenharmony_ci * found, or if the registration of the device node failed.
34862306a36Sopenharmony_ci *
34962306a36Sopenharmony_ci * Returns 0 on success.
35062306a36Sopenharmony_ci *
35162306a36Sopenharmony_ci * .. note::
35262306a36Sopenharmony_ci *
35362306a36Sopenharmony_ci *	This function is meant to be used only inside the V4L2 core.
35462306a36Sopenharmony_ci *	Drivers should use video_register_device() or
35562306a36Sopenharmony_ci *	video_register_device_no_warn().
35662306a36Sopenharmony_ci */
35762306a36Sopenharmony_ciint __must_check __video_register_device(struct video_device *vdev,
35862306a36Sopenharmony_ci					 enum vfl_devnode_type type,
35962306a36Sopenharmony_ci					 int nr, int warn_if_nr_in_use,
36062306a36Sopenharmony_ci					 struct module *owner);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci/**
36362306a36Sopenharmony_ci *  video_register_device - register video4linux devices
36462306a36Sopenharmony_ci *
36562306a36Sopenharmony_ci * @vdev: struct video_device to register
36662306a36Sopenharmony_ci * @type: type of device to register, as defined by &enum vfl_devnode_type
36762306a36Sopenharmony_ci * @nr:   which device node number is desired:
36862306a36Sopenharmony_ci *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
36962306a36Sopenharmony_ci *
37062306a36Sopenharmony_ci * Internally, it calls __video_register_device(). Please see its
37162306a36Sopenharmony_ci * documentation for more details.
37262306a36Sopenharmony_ci *
37362306a36Sopenharmony_ci * .. note::
37462306a36Sopenharmony_ci *	if video_register_device fails, the release() callback of
37562306a36Sopenharmony_ci *	&struct video_device structure is *not* called, so the caller
37662306a36Sopenharmony_ci *	is responsible for freeing any data. Usually that means that
37762306a36Sopenharmony_ci *	you video_device_release() should be called on failure.
37862306a36Sopenharmony_ci */
37962306a36Sopenharmony_cistatic inline int __must_check video_register_device(struct video_device *vdev,
38062306a36Sopenharmony_ci						     enum vfl_devnode_type type,
38162306a36Sopenharmony_ci						     int nr)
38262306a36Sopenharmony_ci{
38362306a36Sopenharmony_ci	return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
38462306a36Sopenharmony_ci}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci/**
38762306a36Sopenharmony_ci *  video_register_device_no_warn - register video4linux devices
38862306a36Sopenharmony_ci *
38962306a36Sopenharmony_ci * @vdev: struct video_device to register
39062306a36Sopenharmony_ci * @type: type of device to register, as defined by &enum vfl_devnode_type
39162306a36Sopenharmony_ci * @nr:   which device node number is desired:
39262306a36Sopenharmony_ci *	(0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
39362306a36Sopenharmony_ci *
39462306a36Sopenharmony_ci * This function is identical to video_register_device() except that no
39562306a36Sopenharmony_ci * warning is issued if the desired device node number was already in use.
39662306a36Sopenharmony_ci *
39762306a36Sopenharmony_ci * Internally, it calls __video_register_device(). Please see its
39862306a36Sopenharmony_ci * documentation for more details.
39962306a36Sopenharmony_ci *
40062306a36Sopenharmony_ci * .. note::
40162306a36Sopenharmony_ci *	if video_register_device fails, the release() callback of
40262306a36Sopenharmony_ci *	&struct video_device structure is *not* called, so the caller
40362306a36Sopenharmony_ci *	is responsible for freeing any data. Usually that means that
40462306a36Sopenharmony_ci *	you video_device_release() should be called on failure.
40562306a36Sopenharmony_ci */
40662306a36Sopenharmony_cistatic inline int __must_check
40762306a36Sopenharmony_civideo_register_device_no_warn(struct video_device *vdev,
40862306a36Sopenharmony_ci			      enum vfl_devnode_type type, int nr)
40962306a36Sopenharmony_ci{
41062306a36Sopenharmony_ci	return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
41162306a36Sopenharmony_ci}
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci/**
41462306a36Sopenharmony_ci * video_unregister_device - Unregister video devices.
41562306a36Sopenharmony_ci *
41662306a36Sopenharmony_ci * @vdev: &struct video_device to register
41762306a36Sopenharmony_ci *
41862306a36Sopenharmony_ci * Does nothing if vdev == NULL or if video_is_registered() returns false.
41962306a36Sopenharmony_ci */
42062306a36Sopenharmony_civoid video_unregister_device(struct video_device *vdev);
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci/**
42362306a36Sopenharmony_ci * video_device_alloc - helper function to alloc &struct video_device
42462306a36Sopenharmony_ci *
42562306a36Sopenharmony_ci * Returns NULL if %-ENOMEM or a &struct video_device on success.
42662306a36Sopenharmony_ci */
42762306a36Sopenharmony_cistruct video_device * __must_check video_device_alloc(void);
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/**
43062306a36Sopenharmony_ci * video_device_release - helper function to release &struct video_device
43162306a36Sopenharmony_ci *
43262306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
43362306a36Sopenharmony_ci *
43462306a36Sopenharmony_ci * Can also be used for video_device->release\(\).
43562306a36Sopenharmony_ci */
43662306a36Sopenharmony_civoid video_device_release(struct video_device *vdev);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci/**
43962306a36Sopenharmony_ci * video_device_release_empty - helper function to implement the
44062306a36Sopenharmony_ci *	video_device->release\(\) callback.
44162306a36Sopenharmony_ci *
44262306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
44362306a36Sopenharmony_ci *
44462306a36Sopenharmony_ci * This release function does nothing.
44562306a36Sopenharmony_ci *
44662306a36Sopenharmony_ci * It should be used when the video_device is a static global struct.
44762306a36Sopenharmony_ci *
44862306a36Sopenharmony_ci * .. note::
44962306a36Sopenharmony_ci *	Having a static video_device is a dubious construction at best.
45062306a36Sopenharmony_ci */
45162306a36Sopenharmony_civoid video_device_release_empty(struct video_device *vdev);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci/**
45462306a36Sopenharmony_ci * v4l2_disable_ioctl- mark that a given command isn't implemented.
45562306a36Sopenharmony_ci *	shouldn't use core locking
45662306a36Sopenharmony_ci *
45762306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
45862306a36Sopenharmony_ci * @cmd: ioctl command
45962306a36Sopenharmony_ci *
46062306a36Sopenharmony_ci * This function allows drivers to provide just one v4l2_ioctl_ops struct, but
46162306a36Sopenharmony_ci * disable ioctls based on the specific card that is actually found.
46262306a36Sopenharmony_ci *
46362306a36Sopenharmony_ci * .. note::
46462306a36Sopenharmony_ci *
46562306a36Sopenharmony_ci *    This must be called before video_register_device.
46662306a36Sopenharmony_ci *    See also the comments for determine_valid_ioctls().
46762306a36Sopenharmony_ci */
46862306a36Sopenharmony_cistatic inline void v4l2_disable_ioctl(struct video_device *vdev,
46962306a36Sopenharmony_ci				      unsigned int cmd)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
47262306a36Sopenharmony_ci		set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
47362306a36Sopenharmony_ci}
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci/**
47662306a36Sopenharmony_ci * video_get_drvdata - gets private data from &struct video_device.
47762306a36Sopenharmony_ci *
47862306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
47962306a36Sopenharmony_ci *
48062306a36Sopenharmony_ci * returns a pointer to the private data
48162306a36Sopenharmony_ci */
48262306a36Sopenharmony_cistatic inline void *video_get_drvdata(struct video_device *vdev)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	return dev_get_drvdata(&vdev->dev);
48562306a36Sopenharmony_ci}
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci/**
48862306a36Sopenharmony_ci * video_set_drvdata - sets private data from &struct video_device.
48962306a36Sopenharmony_ci *
49062306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
49162306a36Sopenharmony_ci * @data: private data pointer
49262306a36Sopenharmony_ci */
49362306a36Sopenharmony_cistatic inline void video_set_drvdata(struct video_device *vdev, void *data)
49462306a36Sopenharmony_ci{
49562306a36Sopenharmony_ci	dev_set_drvdata(&vdev->dev, data);
49662306a36Sopenharmony_ci}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci/**
49962306a36Sopenharmony_ci * video_devdata - gets &struct video_device from struct file.
50062306a36Sopenharmony_ci *
50162306a36Sopenharmony_ci * @file: pointer to struct file
50262306a36Sopenharmony_ci */
50362306a36Sopenharmony_cistruct video_device *video_devdata(struct file *file);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci/**
50662306a36Sopenharmony_ci * video_drvdata - gets private data from &struct video_device using the
50762306a36Sopenharmony_ci *	struct file.
50862306a36Sopenharmony_ci *
50962306a36Sopenharmony_ci * @file: pointer to struct file
51062306a36Sopenharmony_ci *
51162306a36Sopenharmony_ci * This is function combines both video_get_drvdata() and video_devdata()
51262306a36Sopenharmony_ci * as this is used very often.
51362306a36Sopenharmony_ci */
51462306a36Sopenharmony_cistatic inline void *video_drvdata(struct file *file)
51562306a36Sopenharmony_ci{
51662306a36Sopenharmony_ci	return video_get_drvdata(video_devdata(file));
51762306a36Sopenharmony_ci}
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci/**
52062306a36Sopenharmony_ci * video_device_node_name - returns the video device name
52162306a36Sopenharmony_ci *
52262306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
52362306a36Sopenharmony_ci *
52462306a36Sopenharmony_ci * Returns the device name string
52562306a36Sopenharmony_ci */
52662306a36Sopenharmony_cistatic inline const char *video_device_node_name(struct video_device *vdev)
52762306a36Sopenharmony_ci{
52862306a36Sopenharmony_ci	return dev_name(&vdev->dev);
52962306a36Sopenharmony_ci}
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci/**
53262306a36Sopenharmony_ci * video_is_registered - returns true if the &struct video_device is registered.
53362306a36Sopenharmony_ci *
53462306a36Sopenharmony_ci *
53562306a36Sopenharmony_ci * @vdev: pointer to &struct video_device
53662306a36Sopenharmony_ci */
53762306a36Sopenharmony_cistatic inline int video_is_registered(struct video_device *vdev)
53862306a36Sopenharmony_ci{
53962306a36Sopenharmony_ci	return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
54062306a36Sopenharmony_ci}
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci#if defined(CONFIG_MEDIA_CONTROLLER)
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci/**
54562306a36Sopenharmony_ci * video_device_pipeline_start - Mark a pipeline as streaming
54662306a36Sopenharmony_ci * @vdev: Starting video device
54762306a36Sopenharmony_ci * @pipe: Media pipeline to be assigned to all entities in the pipeline.
54862306a36Sopenharmony_ci *
54962306a36Sopenharmony_ci * Mark all entities connected to a given video device through enabled links,
55062306a36Sopenharmony_ci * either directly or indirectly, as streaming. The given pipeline object is
55162306a36Sopenharmony_ci * assigned to every pad in the pipeline and stored in the media_pad pipe
55262306a36Sopenharmony_ci * field.
55362306a36Sopenharmony_ci *
55462306a36Sopenharmony_ci * Calls to this function can be nested, in which case the same number of
55562306a36Sopenharmony_ci * video_device_pipeline_stop() calls will be required to stop streaming. The
55662306a36Sopenharmony_ci * pipeline pointer must be identical for all nested calls to
55762306a36Sopenharmony_ci * video_device_pipeline_start().
55862306a36Sopenharmony_ci *
55962306a36Sopenharmony_ci * The video device must contain a single pad.
56062306a36Sopenharmony_ci *
56162306a36Sopenharmony_ci * This is a convenience wrapper around media_pipeline_start().
56262306a36Sopenharmony_ci */
56362306a36Sopenharmony_ci__must_check int video_device_pipeline_start(struct video_device *vdev,
56462306a36Sopenharmony_ci					     struct media_pipeline *pipe);
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci/**
56762306a36Sopenharmony_ci * __video_device_pipeline_start - Mark a pipeline as streaming
56862306a36Sopenharmony_ci * @vdev: Starting video device
56962306a36Sopenharmony_ci * @pipe: Media pipeline to be assigned to all entities in the pipeline.
57062306a36Sopenharmony_ci *
57162306a36Sopenharmony_ci * ..note:: This is the non-locking version of video_device_pipeline_start()
57262306a36Sopenharmony_ci *
57362306a36Sopenharmony_ci * The video device must contain a single pad.
57462306a36Sopenharmony_ci *
57562306a36Sopenharmony_ci * This is a convenience wrapper around __media_pipeline_start().
57662306a36Sopenharmony_ci */
57762306a36Sopenharmony_ci__must_check int __video_device_pipeline_start(struct video_device *vdev,
57862306a36Sopenharmony_ci					       struct media_pipeline *pipe);
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci/**
58162306a36Sopenharmony_ci * video_device_pipeline_stop - Mark a pipeline as not streaming
58262306a36Sopenharmony_ci * @vdev: Starting video device
58362306a36Sopenharmony_ci *
58462306a36Sopenharmony_ci * Mark all entities connected to a given video device through enabled links,
58562306a36Sopenharmony_ci * either directly or indirectly, as not streaming. The media_pad pipe field
58662306a36Sopenharmony_ci * is reset to %NULL.
58762306a36Sopenharmony_ci *
58862306a36Sopenharmony_ci * If multiple calls to media_pipeline_start() have been made, the same
58962306a36Sopenharmony_ci * number of calls to this function are required to mark the pipeline as not
59062306a36Sopenharmony_ci * streaming.
59162306a36Sopenharmony_ci *
59262306a36Sopenharmony_ci * The video device must contain a single pad.
59362306a36Sopenharmony_ci *
59462306a36Sopenharmony_ci * This is a convenience wrapper around media_pipeline_stop().
59562306a36Sopenharmony_ci */
59662306a36Sopenharmony_civoid video_device_pipeline_stop(struct video_device *vdev);
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci/**
59962306a36Sopenharmony_ci * __video_device_pipeline_stop - Mark a pipeline as not streaming
60062306a36Sopenharmony_ci * @vdev: Starting video device
60162306a36Sopenharmony_ci *
60262306a36Sopenharmony_ci * .. note:: This is the non-locking version of media_pipeline_stop()
60362306a36Sopenharmony_ci *
60462306a36Sopenharmony_ci * The video device must contain a single pad.
60562306a36Sopenharmony_ci *
60662306a36Sopenharmony_ci * This is a convenience wrapper around __media_pipeline_stop().
60762306a36Sopenharmony_ci */
60862306a36Sopenharmony_civoid __video_device_pipeline_stop(struct video_device *vdev);
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci/**
61162306a36Sopenharmony_ci * video_device_pipeline_alloc_start - Mark a pipeline as streaming
61262306a36Sopenharmony_ci * @vdev: Starting video device
61362306a36Sopenharmony_ci *
61462306a36Sopenharmony_ci * video_device_pipeline_alloc_start() is similar to video_device_pipeline_start()
61562306a36Sopenharmony_ci * but instead of working on a given pipeline the function will use an
61662306a36Sopenharmony_ci * existing pipeline if the video device is already part of a pipeline, or
61762306a36Sopenharmony_ci * allocate a new pipeline.
61862306a36Sopenharmony_ci *
61962306a36Sopenharmony_ci * Calls to video_device_pipeline_alloc_start() must be matched with
62062306a36Sopenharmony_ci * video_device_pipeline_stop().
62162306a36Sopenharmony_ci */
62262306a36Sopenharmony_ci__must_check int video_device_pipeline_alloc_start(struct video_device *vdev);
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci/**
62562306a36Sopenharmony_ci * video_device_pipeline - Get the media pipeline a video device is part of
62662306a36Sopenharmony_ci * @vdev: The video device
62762306a36Sopenharmony_ci *
62862306a36Sopenharmony_ci * This function returns the media pipeline that a video device has been
62962306a36Sopenharmony_ci * associated with when constructing the pipeline with
63062306a36Sopenharmony_ci * video_device_pipeline_start(). The pointer remains valid until
63162306a36Sopenharmony_ci * video_device_pipeline_stop() is called.
63262306a36Sopenharmony_ci *
63362306a36Sopenharmony_ci * Return: The media_pipeline the video device is part of, or NULL if the video
63462306a36Sopenharmony_ci * device is not part of any pipeline.
63562306a36Sopenharmony_ci *
63662306a36Sopenharmony_ci * The video device must contain a single pad.
63762306a36Sopenharmony_ci *
63862306a36Sopenharmony_ci * This is a convenience wrapper around media_entity_pipeline().
63962306a36Sopenharmony_ci */
64062306a36Sopenharmony_cistruct media_pipeline *video_device_pipeline(struct video_device *vdev);
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci#endif /* CONFIG_MEDIA_CONTROLLER */
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci#endif /* _V4L2_DEV_H */
645