/* SPDX-License-Identifier: GPL-2.0 */1/*2*3* V 4 L 2 D R I V E R H E L P E R A P I4*5* Moved from videodev2.h6*7* Some commonly needed functions for drivers (v4l2-common.o module)8*/9#ifndef _V4L2_DEV_H10#define _V4L2_DEV_H1112#include <linux/poll.h>13#include <linux/fs.h>14#include <linux/device.h>15#include <linux/cdev.h>16#include <linux/mutex.h>17#include <linux/videodev2.h>1819#include <media/media-entity.h>2021#define VIDEO_MAJOR 812223/**24* enum vfl_devnode_type - type of V4L2 device node25*26* @VFL_TYPE_VIDEO: for video input/output devices27* @VFL_TYPE_VBI: for vertical blank data (i.e. closed captions, teletext)28* @VFL_TYPE_RADIO: for radio tuners29* @VFL_TYPE_SUBDEV: for V4L2 subdevices30* @VFL_TYPE_SDR: for Software Defined Radio tuners31* @VFL_TYPE_TOUCH: for touch sensors32* @VFL_TYPE_MAX: number of VFL types, must always be last in the enum33*/34enum vfl_devnode_type {35VFL_TYPE_VIDEO,36VFL_TYPE_VBI,37VFL_TYPE_RADIO,38VFL_TYPE_SUBDEV,39VFL_TYPE_SDR,40VFL_TYPE_TOUCH,41VFL_TYPE_MAX /* Shall be the last one */42};4344/**45* enum vfl_devnode_direction - Identifies if a &struct video_device46* corresponds to a receiver, a transmitter or a mem-to-mem device.47*48* @VFL_DIR_RX: device is a receiver.49* @VFL_DIR_TX: device is a transmitter.50* @VFL_DIR_M2M: device is a memory to memory device.51*52* Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.53*/54enum vfl_devnode_direction {55VFL_DIR_RX,56VFL_DIR_TX,57VFL_DIR_M2M,58};5960struct v4l2_ioctl_callbacks;61struct video_device;62struct v4l2_device;63struct v4l2_ctrl_handler;64struct dentry;6566/**67* enum v4l2_video_device_flags - Flags used by &struct video_device68*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 future72* 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 and79* compose are swapped. If this flag is set, then the selection80* 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 preserve83* 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 is88* set by the core when the sub-devices device nodes are registered with89* v4l2_device_register_ro_subdev_nodes() and used by the sub-device ioctl90* handler to restrict access to some ioctl calls.91*/92enum v4l2_video_device_flags {93V4L2_FL_REGISTERED = 0,94V4L2_FL_USES_V4L2_FH = 1,95V4L2_FL_QUIRK_INVERTED_CROP = 2,96V4L2_FL_SUBDEV_RO_DEVNODE = 3,97};9899/* Priority helper functions */100101/**102* struct v4l2_prio_state - stores the priority states103*104* @prios: array with elements to store the array priorities105*106*107* .. note::108* The size of @prios array matches the number of priority types defined109* by enum &v4l2_priority.110*/111struct v4l2_prio_state {112atomic_t prios[4];113};114115/**116* v4l2_prio_init - initializes a struct v4l2_prio_state117*118* @global: pointer to &struct v4l2_prio_state119*/120void v4l2_prio_init(struct v4l2_prio_state *global);121122/**123* v4l2_prio_change - changes the v4l2 file handler priority124*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_priority127* @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*/132int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,133enum v4l2_priority new);134135/**136* v4l2_prio_open - Implements the priority logic for a file handler open137*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_priority140*141* .. note::142* This function should be used only by the V4L2 core.143*/144void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);145146/**147* v4l2_prio_close - Implements the priority logic for a file handler close148*149* @global: pointer to the &struct v4l2_prio_state of the device node.150* @local: priority to be released, as defined by enum &v4l2_priority151*152* .. note::153* This function should be used only by the V4L2 core.154*/155void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);156157/**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*/165enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);166167/**168* v4l2_prio_check - Implements the priority logic for a file handler close169*170* @global: pointer to the &struct v4l2_prio_state of the device node.171* @local: desired priority, as defined by enum &v4l2_priority local172*173* .. note::174* This function should be used only by the V4L2 core.175*/176int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);177178/**179* struct v4l2_file_operations - fs operations used by a V4L2 device180*181* @owner: pointer to struct module182* @read: operations needed to implement the read() syscall183* @write: operations needed to implement the write() syscall184* @poll: operations needed to implement the poll() syscall185* @unlocked_ioctl: operations needed to implement the ioctl() syscall186* @compat_ioctl32: operations needed to implement the ioctl() syscall for187* the special case where the Kernel uses 64 bits instructions, but188* the userspace uses 32 bits.189* @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU190* @mmap: operations needed to implement the mmap() syscall191* @open: operations needed to implement the open() syscall192* @release: operations needed to implement the release() syscall193*194* .. note::195*196* Those operations are used to implemente the fs struct file_operations197* at the V4L2 drivers. The V4L2 core overrides the fs ops with some198* extra logic needed by the subsystem.199*/200struct v4l2_file_operations {201struct module *owner;202ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);203ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);204__poll_t (*poll) (struct file *, struct poll_table_struct *);205long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);206#ifdef CONFIG_COMPAT207long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);208#endif209unsigned long (*get_unmapped_area) (struct file *, unsigned long,210unsigned long, unsigned long, unsigned long);211int (*mmap) (struct file *, struct vm_area_struct *);212int (*open) (struct file *);213int (*release) (struct file *);214};215216/*217* Newer version of video_device, handled by videodev2.c218* This version moves redundant code from video device code to219* the common handler220*/221222/**223* struct video_device - Structure used to create and manage the V4L2 device224* nodes.225*226* @entity: &struct media_entity227* @intf_devnode: pointer to &struct media_intf_devnode228* @pipe: &struct media_pipeline229* @fops: pointer to &struct v4l2_file_operations for the video device230* @device_caps: device capabilities as used in v4l2_capabilities231* @dev: &struct device for the video device232* @cdev: character device233* @v4l2_dev: pointer to &struct v4l2_device parent234* @dev_parent: pointer to &struct device parent235* @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 name241* @vfl_type: V4L device type, as defined by &enum vfl_devnode_type242* @vfl_dir: V4L receiver, transmitter or m2m243* @minor: device node 'minor'. It is set to -1 if the registration failed244* @num: number of the video device node245* @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 device248* @fh_lock: Lock for all v4l2_fhs249* @fh_list: List of &struct v4l2_fh250* @dev_debug: Internal device debug flags, not for use by drivers251* @tvnorms: Supported tv norms252*253* @release: video device release() callback254* @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks255*256* @valid_ioctls: bitmap with the valid ioctls for this device257* @lock: pointer to &struct mutex serialization lock258*259* .. note::260* Only set @dev_parent if that can't be deduced from @v4l2_dev.261*/262263struct video_device {264#if defined(CONFIG_MEDIA_CONTROLLER)265struct media_entity entity;266struct media_intf_devnode *intf_devnode;267struct media_pipeline pipe;268#endif269const struct v4l2_file_operations *fops;270271u32 device_caps;272273/* sysfs */274struct device dev;275struct cdev *cdev;276277struct v4l2_device *v4l2_dev;278struct device *dev_parent;279280struct v4l2_ctrl_handler *ctrl_handler;281282struct vb2_queue *queue;283284struct v4l2_prio_state *prio;285286/* device info */287char name[64];288enum vfl_devnode_type vfl_type;289enum vfl_devnode_direction vfl_dir;290int minor;291u16 num;292unsigned long flags;293int index;294295/* V4L2 file handles */296spinlock_t fh_lock;297struct list_head fh_list;298299int dev_debug;300301v4l2_std_id tvnorms;302303/* callbacks */304void (*release)(struct video_device *vdev);305const struct v4l2_ioctl_ops *ioctl_ops;306DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);307308struct mutex *lock;309};310311/**312* media_entity_to_video_device - Returns a &struct video_device from313* the &struct media_entity embedded on it.314*315* @__entity: pointer to &struct media_entity, may be NULL316*/317#define media_entity_to_video_device(__entity) \318({ \319typeof(__entity) __me_vdev_ent = __entity; \320\321__me_vdev_ent ? \322container_of(__me_vdev_ent, struct video_device, entity) : \323NULL; \324})325326/**327* to_video_device - Returns a &struct video_device from the328* &struct device embedded on it.329*330* @cd: pointer to &struct device331*/332#define to_video_device(cd) container_of(cd, struct video_device, dev)333334/**335* __video_register_device - register video4linux devices336*337* @vdev: struct video_device to register338* @type: type of device to register, as defined by &enum vfl_devnode_type339* @nr: which device node number is desired:340* (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)341* @warn_if_nr_in_use: warn if the desired device node number342* was already in use and another number was chosen instead.343* @owner: module that owns the video device node344*345* The registration code assigns minor numbers and device node numbers346* based on the requested type and registers the new device node with347* the kernel.348*349* This function assumes that struct video_device was zeroed when it350* was allocated and does not contain any stale date.351*352* An error is returned if no free minor or device node number could be353* found, or if the registration of the device node failed.354*355* Returns 0 on success.356*357* .. note::358*359* This function is meant to be used only inside the V4L2 core.360* Drivers should use video_register_device() or361* video_register_device_no_warn().362*/363int __must_check __video_register_device(struct video_device *vdev,364enum vfl_devnode_type type,365int nr, int warn_if_nr_in_use,366struct module *owner);367368/**369* video_register_device - register video4linux devices370*371* @vdev: struct video_device to register372* @type: type of device to register, as defined by &enum vfl_devnode_type373* @nr: which device node number is desired:374* (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)375*376* Internally, it calls __video_register_device(). Please see its377* documentation for more details.378*379* .. note::380* if video_register_device fails, the release() callback of381* &struct video_device structure is *not* called, so the caller382* is responsible for freeing any data. Usually that means that383* you video_device_release() should be called on failure.384*/385static inline int __must_check video_register_device(struct video_device *vdev,386enum vfl_devnode_type type,387int nr)388{389return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);390}391392/**393* video_register_device_no_warn - register video4linux devices394*395* @vdev: struct video_device to register396* @type: type of device to register, as defined by &enum vfl_devnode_type397* @nr: which device node number is desired:398* (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)399*400* This function is identical to video_register_device() except that no401* warning is issued if the desired device node number was already in use.402*403* Internally, it calls __video_register_device(). Please see its404* documentation for more details.405*406* .. note::407* if video_register_device fails, the release() callback of408* &struct video_device structure is *not* called, so the caller409* is responsible for freeing any data. Usually that means that410* you video_device_release() should be called on failure.411*/412static inline int __must_check413video_register_device_no_warn(struct video_device *vdev,414enum vfl_devnode_type type, int nr)415{416return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);417}418419/**420* video_unregister_device - Unregister video devices.421*422* @vdev: &struct video_device to register423*424* Does nothing if vdev == NULL or if video_is_registered() returns false.425*/426void video_unregister_device(struct video_device *vdev);427428/**429* video_device_alloc - helper function to alloc &struct video_device430*431* Returns NULL if %-ENOMEM or a &struct video_device on success.432*/433struct video_device * __must_check video_device_alloc(void);434435/**436* video_device_release - helper function to release &struct video_device437*438* @vdev: pointer to &struct video_device439*440* Can also be used for video_device->release\(\).441*/442void video_device_release(struct video_device *vdev);443444/**445* video_device_release_empty - helper function to implement the446* video_device->release\(\) callback.447*448* @vdev: pointer to &struct video_device449*450* This release function does nothing.451*452* It should be used when the video_device is a static global struct.453*454* .. note::455* Having a static video_device is a dubious construction at best.456*/457void video_device_release_empty(struct video_device *vdev);458459/**460* v4l2_disable_ioctl- mark that a given command isn't implemented.461* shouldn't use core locking462*463* @vdev: pointer to &struct video_device464* @cmd: ioctl command465*466* This function allows drivers to provide just one v4l2_ioctl_ops struct, but467* disable ioctls based on the specific card that is actually found.468*469* .. note::470*471* This must be called before video_register_device.472* See also the comments for determine_valid_ioctls().473*/474static inline void v4l2_disable_ioctl(struct video_device *vdev,475unsigned int cmd)476{477if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)478set_bit(_IOC_NR(cmd), vdev->valid_ioctls);479}480481/**482* video_get_drvdata - gets private data from &struct video_device.483*484* @vdev: pointer to &struct video_device485*486* returns a pointer to the private data487*/488static inline void *video_get_drvdata(struct video_device *vdev)489{490return dev_get_drvdata(&vdev->dev);491}492493/**494* video_set_drvdata - sets private data from &struct video_device.495*496* @vdev: pointer to &struct video_device497* @data: private data pointer498*/499static inline void video_set_drvdata(struct video_device *vdev, void *data)500{501dev_set_drvdata(&vdev->dev, data);502}503504/**505* video_devdata - gets &struct video_device from struct file.506*507* @file: pointer to struct file508*/509struct video_device *video_devdata(struct file *file);510511/**512* video_drvdata - gets private data from &struct video_device using the513* struct file.514*515* @file: pointer to struct file516*517* This is function combines both video_get_drvdata() and video_devdata()518* as this is used very often.519*/520static inline void *video_drvdata(struct file *file)521{522return video_get_drvdata(video_devdata(file));523}524525/**526* video_device_node_name - returns the video device name527*528* @vdev: pointer to &struct video_device529*530* Returns the device name string531*/532static inline const char *video_device_node_name(struct video_device *vdev)533{534return dev_name(&vdev->dev);535}536537/**538* video_is_registered - returns true if the &struct video_device is registered.539*540*541* @vdev: pointer to &struct video_device542*/543static inline int video_is_registered(struct video_device *vdev)544{545return test_bit(V4L2_FL_REGISTERED, &vdev->flags);546}547548/**549* v4l2_debugfs_root - returns the dentry of the top-level "v4l2" debugfs dir550*551* If this directory does not yet exist, then it will be created.552*/553#ifdef CONFIG_DEBUG_FS554struct dentry *v4l2_debugfs_root(void);555#else556static inline struct dentry *v4l2_debugfs_root(void)557{558return NULL;559}560#endif561562#if defined(CONFIG_MEDIA_CONTROLLER)563564/**565* video_device_pipeline_start - Mark a pipeline as streaming566* @vdev: Starting video device567* @pipe: Media pipeline to be assigned to all entities in the pipeline.568*569* Mark all entities connected to a given video device through enabled links,570* either directly or indirectly, as streaming. The given pipeline object is571* assigned to every pad in the pipeline and stored in the media_pad pipe572* field.573*574* Calls to this function can be nested, in which case the same number of575* video_device_pipeline_stop() calls will be required to stop streaming. The576* pipeline pointer must be identical for all nested calls to577* video_device_pipeline_start().578*579* The video device must contain a single pad.580*581* This is a convenience wrapper around media_pipeline_start().582*/583__must_check int video_device_pipeline_start(struct video_device *vdev,584struct media_pipeline *pipe);585586/**587* __video_device_pipeline_start - Mark a pipeline as streaming588* @vdev: Starting video device589* @pipe: Media pipeline to be assigned to all entities in the pipeline.590*591* ..note:: This is the non-locking version of video_device_pipeline_start()592*593* The video device must contain a single pad.594*595* This is a convenience wrapper around __media_pipeline_start().596*/597__must_check int __video_device_pipeline_start(struct video_device *vdev,598struct media_pipeline *pipe);599600/**601* video_device_pipeline_stop - Mark a pipeline as not streaming602* @vdev: Starting video device603*604* Mark all entities connected to a given video device through enabled links,605* either directly or indirectly, as not streaming. The media_pad pipe field606* is reset to %NULL.607*608* If multiple calls to media_pipeline_start() have been made, the same609* number of calls to this function are required to mark the pipeline as not610* streaming.611*612* The video device must contain a single pad.613*614* This is a convenience wrapper around media_pipeline_stop().615*/616void video_device_pipeline_stop(struct video_device *vdev);617618/**619* __video_device_pipeline_stop - Mark a pipeline as not streaming620* @vdev: Starting video device621*622* .. note:: This is the non-locking version of media_pipeline_stop()623*624* The video device must contain a single pad.625*626* This is a convenience wrapper around __media_pipeline_stop().627*/628void __video_device_pipeline_stop(struct video_device *vdev);629630/**631* video_device_pipeline_alloc_start - Mark a pipeline as streaming632* @vdev: Starting video device633*634* video_device_pipeline_alloc_start() is similar to video_device_pipeline_start()635* but instead of working on a given pipeline the function will use an636* existing pipeline if the video device is already part of a pipeline, or637* allocate a new pipeline.638*639* Calls to video_device_pipeline_alloc_start() must be matched with640* video_device_pipeline_stop().641*/642__must_check int video_device_pipeline_alloc_start(struct video_device *vdev);643644/**645* video_device_pipeline - Get the media pipeline a video device is part of646* @vdev: The video device647*648* This function returns the media pipeline that a video device has been649* associated with when constructing the pipeline with650* video_device_pipeline_start(). The pointer remains valid until651* video_device_pipeline_stop() is called.652*653* Return: The media_pipeline the video device is part of, or NULL if the video654* device is not part of any pipeline.655*656* The video device must contain a single pad.657*658* This is a convenience wrapper around media_entity_pipeline().659*/660struct media_pipeline *video_device_pipeline(struct video_device *vdev);661662#endif /* CONFIG_MEDIA_CONTROLLER */663664#endif /* _V4L2_DEV_H */665666667