Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *
0004  *  V 4 L 2   D R I V E R   H E L P E R   A P I
0005  *
0006  * Moved from videodev2.h
0007  *
0008  *  Some commonly needed functions for drivers (v4l2-common.o module)
0009  */
0010 #ifndef _V4L2_DEV_H
0011 #define _V4L2_DEV_H
0012 
0013 #include <linux/poll.h>
0014 #include <linux/fs.h>
0015 #include <linux/device.h>
0016 #include <linux/cdev.h>
0017 #include <linux/mutex.h>
0018 #include <linux/videodev2.h>
0019 
0020 #include <media/media-entity.h>
0021 
0022 #define VIDEO_MAJOR 81
0023 
0024 /**
0025  * enum vfl_devnode_type - type of V4L2 device node
0026  *
0027  * @VFL_TYPE_VIDEO: for video input/output devices
0028  * @VFL_TYPE_VBI:   for vertical blank data (i.e. closed captions, teletext)
0029  * @VFL_TYPE_RADIO: for radio tuners
0030  * @VFL_TYPE_SUBDEV:    for V4L2 subdevices
0031  * @VFL_TYPE_SDR:   for Software Defined Radio tuners
0032  * @VFL_TYPE_TOUCH: for touch sensors
0033  * @VFL_TYPE_MAX:   number of VFL types, must always be last in the enum
0034  */
0035 enum vfl_devnode_type {
0036     VFL_TYPE_VIDEO,
0037     VFL_TYPE_VBI,
0038     VFL_TYPE_RADIO,
0039     VFL_TYPE_SUBDEV,
0040     VFL_TYPE_SDR,
0041     VFL_TYPE_TOUCH,
0042     VFL_TYPE_MAX /* Shall be the last one */
0043 };
0044 
0045 /**
0046  * enum  vfl_devnode_direction - Identifies if a &struct video_device
0047  *  corresponds to a receiver, a transmitter or a mem-to-mem device.
0048  *
0049  * @VFL_DIR_RX:     device is a receiver.
0050  * @VFL_DIR_TX:     device is a transmitter.
0051  * @VFL_DIR_M2M:    device is a memory to memory device.
0052  *
0053  * Note: Ignored if &enum vfl_devnode_type is %VFL_TYPE_SUBDEV.
0054  */
0055 enum vfl_devnode_direction {
0056     VFL_DIR_RX,
0057     VFL_DIR_TX,
0058     VFL_DIR_M2M,
0059 };
0060 
0061 struct v4l2_ioctl_callbacks;
0062 struct video_device;
0063 struct v4l2_device;
0064 struct v4l2_ctrl_handler;
0065 
0066 /**
0067  * enum v4l2_video_device_flags - Flags used by &struct video_device
0068  *
0069  * @V4L2_FL_REGISTERED:
0070  *  indicates that a &struct video_device is registered.
0071  *  Drivers can clear this flag if they want to block all future
0072  *  device access. It is cleared by video_unregister_device.
0073  * @V4L2_FL_USES_V4L2_FH:
0074  *  indicates that file->private_data points to &struct v4l2_fh.
0075  *  This flag is set by the core when v4l2_fh_init() is called.
0076  *  All new drivers should use it.
0077  * @V4L2_FL_QUIRK_INVERTED_CROP:
0078  *  some old M2M drivers use g/s_crop/cropcap incorrectly: crop and
0079  *  compose are swapped. If this flag is set, then the selection
0080  *  targets are swapped in the g/s_crop/cropcap functions in v4l2-ioctl.c.
0081  *  This allows those drivers to correctly implement the selection API,
0082  *  but the old crop API will still work as expected in order to preserve
0083  *  backwards compatibility.
0084  *  Never set this flag for new drivers.
0085  * @V4L2_FL_SUBDEV_RO_DEVNODE:
0086  *  indicates that the video device node is registered in read-only mode.
0087  *  The flag only applies to device nodes registered for sub-devices, it is
0088  *  set by the core when the sub-devices device nodes are registered with
0089  *  v4l2_device_register_ro_subdev_nodes() and used by the sub-device ioctl
0090  *  handler to restrict access to some ioctl calls.
0091  */
0092 enum v4l2_video_device_flags {
0093     V4L2_FL_REGISTERED      = 0,
0094     V4L2_FL_USES_V4L2_FH        = 1,
0095     V4L2_FL_QUIRK_INVERTED_CROP = 2,
0096     V4L2_FL_SUBDEV_RO_DEVNODE   = 3,
0097 };
0098 
0099 /* Priority helper functions */
0100 
0101 /**
0102  * struct v4l2_prio_state - stores the priority states
0103  *
0104  * @prios: array with elements to store the array priorities
0105  *
0106  *
0107  * .. note::
0108  *    The size of @prios array matches the number of priority types defined
0109  *    by enum &v4l2_priority.
0110  */
0111 struct v4l2_prio_state {
0112     atomic_t prios[4];
0113 };
0114 
0115 /**
0116  * v4l2_prio_init - initializes a struct v4l2_prio_state
0117  *
0118  * @global: pointer to &struct v4l2_prio_state
0119  */
0120 void v4l2_prio_init(struct v4l2_prio_state *global);
0121 
0122 /**
0123  * v4l2_prio_change - changes the v4l2 file handler priority
0124  *
0125  * @global: pointer to the &struct v4l2_prio_state of the device node.
0126  * @local: pointer to the desired priority, as defined by enum &v4l2_priority
0127  * @new: Priority type requested, as defined by enum &v4l2_priority.
0128  *
0129  * .. note::
0130  *  This function should be used only by the V4L2 core.
0131  */
0132 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
0133              enum v4l2_priority new);
0134 
0135 /**
0136  * v4l2_prio_open - Implements the priority logic for a file handler open
0137  *
0138  * @global: pointer to the &struct v4l2_prio_state of the device node.
0139  * @local: pointer to the desired priority, as defined by enum &v4l2_priority
0140  *
0141  * .. note::
0142  *  This function should be used only by the V4L2 core.
0143  */
0144 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);
0145 
0146 /**
0147  * v4l2_prio_close - Implements the priority logic for a file handler close
0148  *
0149  * @global: pointer to the &struct v4l2_prio_state of the device node.
0150  * @local: priority to be released, as defined by enum &v4l2_priority
0151  *
0152  * .. note::
0153  *  This function should be used only by the V4L2 core.
0154  */
0155 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);
0156 
0157 /**
0158  * v4l2_prio_max - Return the maximum priority, as stored at the @global array.
0159  *
0160  * @global: pointer to the &struct v4l2_prio_state of the device node.
0161  *
0162  * .. note::
0163  *  This function should be used only by the V4L2 core.
0164  */
0165 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);
0166 
0167 /**
0168  * v4l2_prio_check - Implements the priority logic for a file handler close
0169  *
0170  * @global: pointer to the &struct v4l2_prio_state of the device node.
0171  * @local: desired priority, as defined by enum &v4l2_priority local
0172  *
0173  * .. note::
0174  *  This function should be used only by the V4L2 core.
0175  */
0176 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);
0177 
0178 /**
0179  * struct v4l2_file_operations - fs operations used by a V4L2 device
0180  *
0181  * @owner: pointer to struct module
0182  * @read: operations needed to implement the read() syscall
0183  * @write: operations needed to implement the write() syscall
0184  * @poll: operations needed to implement the poll() syscall
0185  * @unlocked_ioctl: operations needed to implement the ioctl() syscall
0186  * @compat_ioctl32: operations needed to implement the ioctl() syscall for
0187  *  the special case where the Kernel uses 64 bits instructions, but
0188  *  the userspace uses 32 bits.
0189  * @get_unmapped_area: called by the mmap() syscall, used when %!CONFIG_MMU
0190  * @mmap: operations needed to implement the mmap() syscall
0191  * @open: operations needed to implement the open() syscall
0192  * @release: operations needed to implement the release() syscall
0193  *
0194  * .. note::
0195  *
0196  *  Those operations are used to implemente the fs struct file_operations
0197  *  at the V4L2 drivers. The V4L2 core overrides the fs ops with some
0198  *  extra logic needed by the subsystem.
0199  */
0200 struct v4l2_file_operations {
0201     struct module *owner;
0202     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
0203     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
0204     __poll_t (*poll) (struct file *, struct poll_table_struct *);
0205     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
0206 #ifdef CONFIG_COMPAT
0207     long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);
0208 #endif
0209     unsigned long (*get_unmapped_area) (struct file *, unsigned long,
0210                 unsigned long, unsigned long, unsigned long);
0211     int (*mmap) (struct file *, struct vm_area_struct *);
0212     int (*open) (struct file *);
0213     int (*release) (struct file *);
0214 };
0215 
0216 /*
0217  * Newer version of video_device, handled by videodev2.c
0218  *  This version moves redundant code from video device code to
0219  *  the common handler
0220  */
0221 
0222 /**
0223  * struct video_device - Structure used to create and manage the V4L2 device
0224  *  nodes.
0225  *
0226  * @entity: &struct media_entity
0227  * @intf_devnode: pointer to &struct media_intf_devnode
0228  * @pipe: &struct media_pipeline
0229  * @fops: pointer to &struct v4l2_file_operations for the video device
0230  * @device_caps: device capabilities as used in v4l2_capabilities
0231  * @dev: &struct device for the video device
0232  * @cdev: character device
0233  * @v4l2_dev: pointer to &struct v4l2_device parent
0234  * @dev_parent: pointer to &struct device parent
0235  * @ctrl_handler: Control handler associated with this device node.
0236  *   May be NULL.
0237  * @queue: &struct vb2_queue associated with this device node. May be NULL.
0238  * @prio: pointer to &struct v4l2_prio_state with device's Priority state.
0239  *   If NULL, then v4l2_dev->prio will be used.
0240  * @name: video device name
0241  * @vfl_type: V4L device type, as defined by &enum vfl_devnode_type
0242  * @vfl_dir: V4L receiver, transmitter or m2m
0243  * @minor: device node 'minor'. It is set to -1 if the registration failed
0244  * @num: number of the video device node
0245  * @flags: video device flags. Use bitops to set/clear/test flags.
0246  *     Contains a set of &enum v4l2_video_device_flags.
0247  * @index: attribute to differentiate multiple indices on one physical device
0248  * @fh_lock: Lock for all v4l2_fhs
0249  * @fh_list: List of &struct v4l2_fh
0250  * @dev_debug: Internal device debug flags, not for use by drivers
0251  * @tvnorms: Supported tv norms
0252  *
0253  * @release: video device release() callback
0254  * @ioctl_ops: pointer to &struct v4l2_ioctl_ops with ioctl callbacks
0255  *
0256  * @valid_ioctls: bitmap with the valid ioctls for this device
0257  * @lock: pointer to &struct mutex serialization lock
0258  *
0259  * .. note::
0260  *  Only set @dev_parent if that can't be deduced from @v4l2_dev.
0261  */
0262 
0263 struct video_device {
0264 #if defined(CONFIG_MEDIA_CONTROLLER)
0265     struct media_entity entity;
0266     struct media_intf_devnode *intf_devnode;
0267     struct media_pipeline pipe;
0268 #endif
0269     const struct v4l2_file_operations *fops;
0270 
0271     u32 device_caps;
0272 
0273     /* sysfs */
0274     struct device dev;
0275     struct cdev *cdev;
0276 
0277     struct v4l2_device *v4l2_dev;
0278     struct device *dev_parent;
0279 
0280     struct v4l2_ctrl_handler *ctrl_handler;
0281 
0282     struct vb2_queue *queue;
0283 
0284     struct v4l2_prio_state *prio;
0285 
0286     /* device info */
0287     char name[32];
0288     enum vfl_devnode_type vfl_type;
0289     enum vfl_devnode_direction vfl_dir;
0290     int minor;
0291     u16 num;
0292     unsigned long flags;
0293     int index;
0294 
0295     /* V4L2 file handles */
0296     spinlock_t      fh_lock;
0297     struct list_head    fh_list;
0298 
0299     int dev_debug;
0300 
0301     v4l2_std_id tvnorms;
0302 
0303     /* callbacks */
0304     void (*release)(struct video_device *vdev);
0305     const struct v4l2_ioctl_ops *ioctl_ops;
0306     DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
0307 
0308     struct mutex *lock;
0309 };
0310 
0311 /**
0312  * media_entity_to_video_device - Returns a &struct video_device from
0313  *  the &struct media_entity embedded on it.
0314  *
0315  * @__entity: pointer to &struct media_entity
0316  */
0317 #define media_entity_to_video_device(__entity) \
0318     container_of(__entity, struct video_device, entity)
0319 
0320 /**
0321  * to_video_device - Returns a &struct video_device from the
0322  *  &struct device embedded on it.
0323  *
0324  * @cd: pointer to &struct device
0325  */
0326 #define to_video_device(cd) container_of(cd, struct video_device, dev)
0327 
0328 /**
0329  * __video_register_device - register video4linux devices
0330  *
0331  * @vdev: struct video_device to register
0332  * @type: type of device to register, as defined by &enum vfl_devnode_type
0333  * @nr:   which device node number is desired:
0334  *  (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
0335  * @warn_if_nr_in_use: warn if the desired device node number
0336  *        was already in use and another number was chosen instead.
0337  * @owner: module that owns the video device node
0338  *
0339  * The registration code assigns minor numbers and device node numbers
0340  * based on the requested type and registers the new device node with
0341  * the kernel.
0342  *
0343  * This function assumes that struct video_device was zeroed when it
0344  * was allocated and does not contain any stale date.
0345  *
0346  * An error is returned if no free minor or device node number could be
0347  * found, or if the registration of the device node failed.
0348  *
0349  * Returns 0 on success.
0350  *
0351  * .. note::
0352  *
0353  *  This function is meant to be used only inside the V4L2 core.
0354  *  Drivers should use video_register_device() or
0355  *  video_register_device_no_warn().
0356  */
0357 int __must_check __video_register_device(struct video_device *vdev,
0358                      enum vfl_devnode_type type,
0359                      int nr, int warn_if_nr_in_use,
0360                      struct module *owner);
0361 
0362 /**
0363  *  video_register_device - register video4linux devices
0364  *
0365  * @vdev: struct video_device to register
0366  * @type: type of device to register, as defined by &enum vfl_devnode_type
0367  * @nr:   which device node number is desired:
0368  *  (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
0369  *
0370  * Internally, it calls __video_register_device(). Please see its
0371  * documentation for more details.
0372  *
0373  * .. note::
0374  *  if video_register_device fails, the release() callback of
0375  *  &struct video_device structure is *not* called, so the caller
0376  *  is responsible for freeing any data. Usually that means that
0377  *  you video_device_release() should be called on failure.
0378  */
0379 static inline int __must_check video_register_device(struct video_device *vdev,
0380                              enum vfl_devnode_type type,
0381                              int nr)
0382 {
0383     return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
0384 }
0385 
0386 /**
0387  *  video_register_device_no_warn - register video4linux devices
0388  *
0389  * @vdev: struct video_device to register
0390  * @type: type of device to register, as defined by &enum vfl_devnode_type
0391  * @nr:   which device node number is desired:
0392  *  (0 == /dev/video0, 1 == /dev/video1, ..., -1 == first free)
0393  *
0394  * This function is identical to video_register_device() except that no
0395  * warning is issued if the desired device node number was already in use.
0396  *
0397  * Internally, it calls __video_register_device(). Please see its
0398  * documentation for more details.
0399  *
0400  * .. note::
0401  *  if video_register_device fails, the release() callback of
0402  *  &struct video_device structure is *not* called, so the caller
0403  *  is responsible for freeing any data. Usually that means that
0404  *  you video_device_release() should be called on failure.
0405  */
0406 static inline int __must_check
0407 video_register_device_no_warn(struct video_device *vdev,
0408                   enum vfl_devnode_type type, int nr)
0409 {
0410     return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
0411 }
0412 
0413 /**
0414  * video_unregister_device - Unregister video devices.
0415  *
0416  * @vdev: &struct video_device to register
0417  *
0418  * Does nothing if vdev == NULL or if video_is_registered() returns false.
0419  */
0420 void video_unregister_device(struct video_device *vdev);
0421 
0422 /**
0423  * video_device_alloc - helper function to alloc &struct video_device
0424  *
0425  * Returns NULL if %-ENOMEM or a &struct video_device on success.
0426  */
0427 struct video_device * __must_check video_device_alloc(void);
0428 
0429 /**
0430  * video_device_release - helper function to release &struct video_device
0431  *
0432  * @vdev: pointer to &struct video_device
0433  *
0434  * Can also be used for video_device->release\(\).
0435  */
0436 void video_device_release(struct video_device *vdev);
0437 
0438 /**
0439  * video_device_release_empty - helper function to implement the
0440  *  video_device->release\(\) callback.
0441  *
0442  * @vdev: pointer to &struct video_device
0443  *
0444  * This release function does nothing.
0445  *
0446  * It should be used when the video_device is a static global struct.
0447  *
0448  * .. note::
0449  *  Having a static video_device is a dubious construction at best.
0450  */
0451 void video_device_release_empty(struct video_device *vdev);
0452 
0453 /**
0454  * v4l2_disable_ioctl- mark that a given command isn't implemented.
0455  *  shouldn't use core locking
0456  *
0457  * @vdev: pointer to &struct video_device
0458  * @cmd: ioctl command
0459  *
0460  * This function allows drivers to provide just one v4l2_ioctl_ops struct, but
0461  * disable ioctls based on the specific card that is actually found.
0462  *
0463  * .. note::
0464  *
0465  *    This must be called before video_register_device.
0466  *    See also the comments for determine_valid_ioctls().
0467  */
0468 static inline void v4l2_disable_ioctl(struct video_device *vdev,
0469                       unsigned int cmd)
0470 {
0471     if (_IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
0472         set_bit(_IOC_NR(cmd), vdev->valid_ioctls);
0473 }
0474 
0475 /**
0476  * video_get_drvdata - gets private data from &struct video_device.
0477  *
0478  * @vdev: pointer to &struct video_device
0479  *
0480  * returns a pointer to the private data
0481  */
0482 static inline void *video_get_drvdata(struct video_device *vdev)
0483 {
0484     return dev_get_drvdata(&vdev->dev);
0485 }
0486 
0487 /**
0488  * video_set_drvdata - sets private data from &struct video_device.
0489  *
0490  * @vdev: pointer to &struct video_device
0491  * @data: private data pointer
0492  */
0493 static inline void video_set_drvdata(struct video_device *vdev, void *data)
0494 {
0495     dev_set_drvdata(&vdev->dev, data);
0496 }
0497 
0498 /**
0499  * video_devdata - gets &struct video_device from struct file.
0500  *
0501  * @file: pointer to struct file
0502  */
0503 struct video_device *video_devdata(struct file *file);
0504 
0505 /**
0506  * video_drvdata - gets private data from &struct video_device using the
0507  *  struct file.
0508  *
0509  * @file: pointer to struct file
0510  *
0511  * This is function combines both video_get_drvdata() and video_devdata()
0512  * as this is used very often.
0513  */
0514 static inline void *video_drvdata(struct file *file)
0515 {
0516     return video_get_drvdata(video_devdata(file));
0517 }
0518 
0519 /**
0520  * video_device_node_name - returns the video device name
0521  *
0522  * @vdev: pointer to &struct video_device
0523  *
0524  * Returns the device name string
0525  */
0526 static inline const char *video_device_node_name(struct video_device *vdev)
0527 {
0528     return dev_name(&vdev->dev);
0529 }
0530 
0531 /**
0532  * video_is_registered - returns true if the &struct video_device is registered.
0533  *
0534  *
0535  * @vdev: pointer to &struct video_device
0536  */
0537 static inline int video_is_registered(struct video_device *vdev)
0538 {
0539     return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
0540 }
0541 
0542 #endif /* _V4L2_DEV_H */