18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * v4l2-fh.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * V4L2 file handle. Store per file handle data for the V4L2
68c2ecf20Sopenharmony_ci * framework. Using file handles is optional for the drivers.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2009--2010 Nokia Corporation.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Contact: Sakari Ailus <sakari.ailus@iki.fi>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifndef V4L2_FH_H
148c2ecf20Sopenharmony_ci#define V4L2_FH_H
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/fs.h>
178c2ecf20Sopenharmony_ci#include <linux/kconfig.h>
188c2ecf20Sopenharmony_ci#include <linux/list.h>
198c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct video_device;
228c2ecf20Sopenharmony_cistruct v4l2_ctrl_handler;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * struct v4l2_fh - Describes a V4L2 file handler
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * @list: list of file handlers
288c2ecf20Sopenharmony_ci * @vdev: pointer to &struct video_device
298c2ecf20Sopenharmony_ci * @ctrl_handler: pointer to &struct v4l2_ctrl_handler
308c2ecf20Sopenharmony_ci * @prio: priority of the file handler, as defined by &enum v4l2_priority
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * @wait: event' s wait queue
338c2ecf20Sopenharmony_ci * @subscribe_lock: serialise changes to the subscribed list; guarantee that
348c2ecf20Sopenharmony_ci *		    the add and del event callbacks are orderly called
358c2ecf20Sopenharmony_ci * @subscribed: list of subscribed events
368c2ecf20Sopenharmony_ci * @available: list of events waiting to be dequeued
378c2ecf20Sopenharmony_ci * @navailable: number of available events at @available list
388c2ecf20Sopenharmony_ci * @sequence: event sequence number
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * @m2m_ctx: pointer to &struct v4l2_m2m_ctx
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistruct v4l2_fh {
438c2ecf20Sopenharmony_ci	struct list_head	list;
448c2ecf20Sopenharmony_ci	struct video_device	*vdev;
458c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *ctrl_handler;
468c2ecf20Sopenharmony_ci	enum v4l2_priority	prio;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* Events */
498c2ecf20Sopenharmony_ci	wait_queue_head_t	wait;
508c2ecf20Sopenharmony_ci	struct mutex		subscribe_lock;
518c2ecf20Sopenharmony_ci	struct list_head	subscribed;
528c2ecf20Sopenharmony_ci	struct list_head	available;
538c2ecf20Sopenharmony_ci	unsigned int		navailable;
548c2ecf20Sopenharmony_ci	u32			sequence;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx	*m2m_ctx;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * v4l2_fh_init - Initialise the file handle.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * @fh: pointer to &struct v4l2_fh
638c2ecf20Sopenharmony_ci * @vdev: pointer to &struct video_device
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Parts of the V4L2 framework using the
668c2ecf20Sopenharmony_ci * file handles should be initialised in this function. Must be called
678c2ecf20Sopenharmony_ci * from driver's v4l2_file_operations->open\(\) handler if the driver
688c2ecf20Sopenharmony_ci * uses &struct v4l2_fh.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_civoid v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * v4l2_fh_add - Add the fh to the list of file handles on a video_device.
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * @fh: pointer to &struct v4l2_fh
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * .. note::
788c2ecf20Sopenharmony_ci *    The @fh file handle must be initialised first.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_civoid v4l2_fh_add(struct v4l2_fh *fh);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/**
838c2ecf20Sopenharmony_ci * v4l2_fh_open - Ancillary routine that can be used as the open\(\) op
848c2ecf20Sopenharmony_ci *	of v4l2_file_operations.
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * @filp: pointer to struct file
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * It allocates a v4l2_fh and inits and adds it to the &struct video_device
898c2ecf20Sopenharmony_ci * associated with the file pointer.
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_ciint v4l2_fh_open(struct file *filp);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/**
948c2ecf20Sopenharmony_ci * v4l2_fh_del - Remove file handle from the list of file handles.
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * @fh: pointer to &struct v4l2_fh
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci * On error filp->private_data will be %NULL, otherwise it will point to
998c2ecf20Sopenharmony_ci * the &struct v4l2_fh.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * .. note::
1028c2ecf20Sopenharmony_ci *    Must be called in v4l2_file_operations->release\(\) handler if the driver
1038c2ecf20Sopenharmony_ci *    uses &struct v4l2_fh.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_civoid v4l2_fh_del(struct v4l2_fh *fh);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/**
1088c2ecf20Sopenharmony_ci * v4l2_fh_exit - Release resources related to a file handle.
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * @fh: pointer to &struct v4l2_fh
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Parts of the V4L2 framework using the v4l2_fh must release their
1138c2ecf20Sopenharmony_ci * resources here, too.
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * .. note::
1168c2ecf20Sopenharmony_ci *    Must be called in v4l2_file_operations->release\(\) handler if the
1178c2ecf20Sopenharmony_ci *    driver uses &struct v4l2_fh.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_civoid v4l2_fh_exit(struct v4l2_fh *fh);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/**
1228c2ecf20Sopenharmony_ci * v4l2_fh_release - Ancillary routine that can be used as the release\(\) op
1238c2ecf20Sopenharmony_ci *	of v4l2_file_operations.
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci * @filp: pointer to struct file
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci * It deletes and exits the v4l2_fh associated with the file pointer and
1288c2ecf20Sopenharmony_ci * frees it. It will do nothing if filp->private_data (the pointer to the
1298c2ecf20Sopenharmony_ci * v4l2_fh struct) is %NULL.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * This function always returns 0.
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ciint v4l2_fh_release(struct file *filp);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/**
1368c2ecf20Sopenharmony_ci * v4l2_fh_is_singular - Returns 1 if this filehandle is the only filehandle
1378c2ecf20Sopenharmony_ci *	 opened for the associated video_device.
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * @fh: pointer to &struct v4l2_fh
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * If @fh is NULL, then it returns 0.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_ciint v4l2_fh_is_singular(struct v4l2_fh *fh);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/**
1468c2ecf20Sopenharmony_ci * v4l2_fh_is_singular_file - Returns 1 if this filehandle is the only
1478c2ecf20Sopenharmony_ci *	filehandle opened for the associated video_device.
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * @filp: pointer to struct file
1508c2ecf20Sopenharmony_ci *
1518c2ecf20Sopenharmony_ci * This is a helper function variant of v4l2_fh_is_singular() with uses
1528c2ecf20Sopenharmony_ci * struct file as argument.
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci * If filp->private_data is %NULL, then it will return 0.
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_cistatic inline int v4l2_fh_is_singular_file(struct file *filp)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	return v4l2_fh_is_singular(filp->private_data);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#endif /* V4L2_EVENT_H */
162