xref: /linux/include/media/v4l2-fh.h (revision 1802d0beecafe581ad584634ba92f8a471d8a63a)
1*1802d0beSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
21babcb46SSakari Ailus /*
31babcb46SSakari Ailus  * v4l2-fh.h
41babcb46SSakari Ailus  *
51babcb46SSakari Ailus  * V4L2 file handle. Store per file handle data for the V4L2
61babcb46SSakari Ailus  * framework. Using file handles is optional for the drivers.
71babcb46SSakari Ailus  *
81babcb46SSakari Ailus  * Copyright (C) 2009--2010 Nokia Corporation.
91babcb46SSakari Ailus  *
108c5dff90SSakari Ailus  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
111babcb46SSakari Ailus  */
121babcb46SSakari Ailus 
131babcb46SSakari Ailus #ifndef V4L2_FH_H
141babcb46SSakari Ailus #define V4L2_FH_H
151babcb46SSakari Ailus 
164f1af2a3SLaurent Pinchart #include <linux/fs.h>
1714351d44SAlexandre Courbot #include <linux/kconfig.h>
181babcb46SSakari Ailus #include <linux/list.h>
19c8d185ceSLaurent Pinchart #include <linux/videodev2.h>
201babcb46SSakari Ailus 
211babcb46SSakari Ailus struct video_device;
222d28b686SHans Verkuil struct v4l2_ctrl_handler;
231babcb46SSakari Ailus 
244ada120eSMauro Carvalho Chehab /**
254ada120eSMauro Carvalho Chehab  * struct v4l2_fh - Describes a V4L2 file handler
264ada120eSMauro Carvalho Chehab  *
274ada120eSMauro Carvalho Chehab  * @list: list of file handlers
284ada120eSMauro Carvalho Chehab  * @vdev: pointer to &struct video_device
294ada120eSMauro Carvalho Chehab  * @ctrl_handler: pointer to &struct v4l2_ctrl_handler
304ada120eSMauro Carvalho Chehab  * @prio: priority of the file handler, as defined by &enum v4l2_priority
314ada120eSMauro Carvalho Chehab  *
324ada120eSMauro Carvalho Chehab  * @wait: event' s wait queue
33ad608fbcSSakari Ailus  * @subscribe_lock: serialise changes to the subscribed list; guarantee that
34ad608fbcSSakari Ailus  *		    the add and del event callbacks are orderly called
354ada120eSMauro Carvalho Chehab  * @subscribed: list of subscribed events
364ada120eSMauro Carvalho Chehab  * @available: list of events waiting to be dequeued
374ada120eSMauro Carvalho Chehab  * @navailable: number of available events at @available list
384ada120eSMauro Carvalho Chehab  * @sequence: event sequence number
39ad608fbcSSakari Ailus  *
404ada120eSMauro Carvalho Chehab  * @m2m_ctx: pointer to &struct v4l2_m2m_ctx
414ada120eSMauro Carvalho Chehab  */
421babcb46SSakari Ailus struct v4l2_fh {
431babcb46SSakari Ailus 	struct list_head	list;
441babcb46SSakari Ailus 	struct video_device	*vdev;
452d28b686SHans Verkuil 	struct v4l2_ctrl_handler *ctrl_handler;
46fc5602beSHans Verkuil 	enum v4l2_priority	prio;
47523f46d6SHans Verkuil 
48523f46d6SHans Verkuil 	/* Events */
49523f46d6SHans Verkuil 	wait_queue_head_t	wait;
50ad608fbcSSakari Ailus 	struct mutex		subscribe_lock;
514ada120eSMauro Carvalho Chehab 	struct list_head	subscribed;
524ada120eSMauro Carvalho Chehab 	struct list_head	available;
53523f46d6SHans Verkuil 	unsigned int		navailable;
54523f46d6SHans Verkuil 	u32			sequence;
558e6e8f93SSylwester Nawrocki 
568e6e8f93SSylwester Nawrocki #if IS_ENABLED(CONFIG_V4L2_MEM2MEM_DEV)
578e6e8f93SSylwester Nawrocki 	struct v4l2_m2m_ctx	*m2m_ctx;
588e6e8f93SSylwester Nawrocki #endif
591babcb46SSakari Ailus };
601babcb46SSakari Ailus 
614ada120eSMauro Carvalho Chehab /**
624ada120eSMauro Carvalho Chehab  * v4l2_fh_init - Initialise the file handle.
634ada120eSMauro Carvalho Chehab  *
644ada120eSMauro Carvalho Chehab  * @fh: pointer to &struct v4l2_fh
654ada120eSMauro Carvalho Chehab  * @vdev: pointer to &struct video_device
664ada120eSMauro Carvalho Chehab  *
674ada120eSMauro Carvalho Chehab  * Parts of the V4L2 framework using the
681babcb46SSakari Ailus  * file handles should be initialised in this function. Must be called
694ada120eSMauro Carvalho Chehab  * from driver's v4l2_file_operations->open\(\) handler if the driver
704ada120eSMauro Carvalho Chehab  * uses &struct v4l2_fh.
711babcb46SSakari Ailus  */
72523f46d6SHans Verkuil void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev);
734ada120eSMauro Carvalho Chehab 
744ada120eSMauro Carvalho Chehab /**
754ada120eSMauro Carvalho Chehab  * v4l2_fh_add - Add the fh to the list of file handles on a video_device.
764ada120eSMauro Carvalho Chehab  *
774ada120eSMauro Carvalho Chehab  * @fh: pointer to &struct v4l2_fh
784ada120eSMauro Carvalho Chehab  *
794ada120eSMauro Carvalho Chehab  * .. note::
804ada120eSMauro Carvalho Chehab  *    The @fh file handle must be initialised first.
811babcb46SSakari Ailus  */
821babcb46SSakari Ailus void v4l2_fh_add(struct v4l2_fh *fh);
834ada120eSMauro Carvalho Chehab 
844ada120eSMauro Carvalho Chehab /**
854ada120eSMauro Carvalho Chehab  * v4l2_fh_open - Ancillary routine that can be used as the open\(\) op
864ada120eSMauro Carvalho Chehab  *	of v4l2_file_operations.
874ada120eSMauro Carvalho Chehab  *
884ada120eSMauro Carvalho Chehab  * @filp: pointer to struct file
894ada120eSMauro Carvalho Chehab  *
904ada120eSMauro Carvalho Chehab  * It allocates a v4l2_fh and inits and adds it to the &struct video_device
914ada120eSMauro Carvalho Chehab  * associated with the file pointer.
9273cb4206SHans Verkuil  */
9373cb4206SHans Verkuil int v4l2_fh_open(struct file *filp);
944ada120eSMauro Carvalho Chehab 
954ada120eSMauro Carvalho Chehab /**
964ada120eSMauro Carvalho Chehab  * v4l2_fh_del - Remove file handle from the list of file handles.
974ada120eSMauro Carvalho Chehab  *
984ada120eSMauro Carvalho Chehab  * @fh: pointer to &struct v4l2_fh
994ada120eSMauro Carvalho Chehab  *
1004ada120eSMauro Carvalho Chehab  * On error filp->private_data will be %NULL, otherwise it will point to
1014ada120eSMauro Carvalho Chehab  * the &struct v4l2_fh.
1024ada120eSMauro Carvalho Chehab  *
1034ada120eSMauro Carvalho Chehab  * .. note::
1044ada120eSMauro Carvalho Chehab  *    Must be called in v4l2_file_operations->release\(\) handler if the driver
1054ada120eSMauro Carvalho Chehab  *    uses &struct v4l2_fh.
1061babcb46SSakari Ailus  */
1071babcb46SSakari Ailus void v4l2_fh_del(struct v4l2_fh *fh);
1084ada120eSMauro Carvalho Chehab 
1094ada120eSMauro Carvalho Chehab /**
1104ada120eSMauro Carvalho Chehab  * v4l2_fh_exit - Release resources related to a file handle.
1114ada120eSMauro Carvalho Chehab  *
1124ada120eSMauro Carvalho Chehab  * @fh: pointer to &struct v4l2_fh
1134ada120eSMauro Carvalho Chehab  *
1144ada120eSMauro Carvalho Chehab  * Parts of the V4L2 framework using the v4l2_fh must release their
1154ada120eSMauro Carvalho Chehab  * resources here, too.
1164ada120eSMauro Carvalho Chehab  *
1174ada120eSMauro Carvalho Chehab  * .. note::
1184ada120eSMauro Carvalho Chehab  *    Must be called in v4l2_file_operations->release\(\) handler if the
1194ada120eSMauro Carvalho Chehab  *    driver uses &struct v4l2_fh.
1201babcb46SSakari Ailus  */
1211babcb46SSakari Ailus void v4l2_fh_exit(struct v4l2_fh *fh);
1224ada120eSMauro Carvalho Chehab 
1234ada120eSMauro Carvalho Chehab /**
1244ada120eSMauro Carvalho Chehab  * v4l2_fh_release - Ancillary routine that can be used as the release\(\) op
1254ada120eSMauro Carvalho Chehab  *	of v4l2_file_operations.
1264ada120eSMauro Carvalho Chehab  *
1274ada120eSMauro Carvalho Chehab  * @filp: pointer to struct file
1284ada120eSMauro Carvalho Chehab  *
12973cb4206SHans Verkuil  * It deletes and exits the v4l2_fh associated with the file pointer and
13073cb4206SHans Verkuil  * frees it. It will do nothing if filp->private_data (the pointer to the
1314ada120eSMauro Carvalho Chehab  * v4l2_fh struct) is %NULL.
1324ada120eSMauro Carvalho Chehab  *
1334ada120eSMauro Carvalho Chehab  * This function always returns 0.
13473cb4206SHans Verkuil  */
13573cb4206SHans Verkuil int v4l2_fh_release(struct file *filp);
1364ada120eSMauro Carvalho Chehab 
1374ada120eSMauro Carvalho Chehab /**
1384ada120eSMauro Carvalho Chehab  * v4l2_fh_is_singular - Returns 1 if this filehandle is the only filehandle
1394ada120eSMauro Carvalho Chehab  *	 opened for the associated video_device.
1404ada120eSMauro Carvalho Chehab  *
1414ada120eSMauro Carvalho Chehab  * @fh: pointer to &struct v4l2_fh
1424ada120eSMauro Carvalho Chehab  *
1434ada120eSMauro Carvalho Chehab  * If @fh is NULL, then it returns 0.
144dfddb244SHans Verkuil  */
145dfddb244SHans Verkuil int v4l2_fh_is_singular(struct v4l2_fh *fh);
1464ada120eSMauro Carvalho Chehab 
1474ada120eSMauro Carvalho Chehab /**
1484ada120eSMauro Carvalho Chehab  * v4l2_fh_is_singular_file - Returns 1 if this filehandle is the only
1494ada120eSMauro Carvalho Chehab  *	filehandle opened for the associated video_device.
1504ada120eSMauro Carvalho Chehab  *
1514ada120eSMauro Carvalho Chehab  * @filp: pointer to struct file
1524ada120eSMauro Carvalho Chehab  *
1534ada120eSMauro Carvalho Chehab  * This is a helper function variant of v4l2_fh_is_singular() with uses
1544ada120eSMauro Carvalho Chehab  * struct file as argument.
1554ada120eSMauro Carvalho Chehab  *
1564ada120eSMauro Carvalho Chehab  * If filp->private_data is %NULL, then it will return 0.
157dfddb244SHans Verkuil  */
158dfddb244SHans Verkuil static inline int v4l2_fh_is_singular_file(struct file *filp)
159dfddb244SHans Verkuil {
160dfddb244SHans Verkuil 	return v4l2_fh_is_singular(filp->private_data);
161dfddb244SHans Verkuil }
1621babcb46SSakari Ailus 
1631babcb46SSakari Ailus #endif /* V4L2_EVENT_H */
164