V4L2 driver (1). Overall framework, v4l2driver

Source: Internet
Author: User

V4L2 driver (1). Overall framework, v4l2driver
1.1 starting from character Devices

Anyone familiar with v4l2 user space programming knows that v4l2 programming mainly calls a series of ioctl functions to open, close, query, and set v4l2 devices. the v4l2 device is a character device, and the main task of its driver is to implement a variety of ioctl .?
Shows the overall framework of v4l2.

What is the overall framework of V4L2?

The light green background is the user space, and the light blue background is the kernel space. an ellipse indicates the relevant struct, subsystem, or instance. the box indicates the related interface. the blue ellipse indicates that this part belongs to the content of the v4l2-core. it can be seen that the user space interacts with the kernel space through the file_operations interface. Therefore, the driver must implement related content in file_operations. The important functions are:Open ,?Release ,?Ioctl ,?Mmap ,?Poll .?Read andWrite can be implemented without any need. For efficiency, v4l2 basically uses stream io for data transmission.The implementation of file_operations is as follows:

static const struct file_operations v4l2_fops = { .owner = THIS_MODULE, .read = v4l2_read, .write = v4l2_write, .open = v4l2_open, .get_unmapped_area = v4l2_get_unmapped_area, .mmap = v4l2_mmap, .unlocked_ioctl = v4l2_ioctl,#ifdef CONFIG_COMPAT .compat_ioctl = v4l2_compat_ioctl32,#endif .release = v4l2_release, .poll = v4l2_poll, .llseek = no_llseek,};

The v4l2_fops function is bound to a cdev and registered to the system. later, the v4l2 device is called through the cdev device node. the cdev pointer is referenced by a video_device struct to encapsulate cdev into the v4l2 framework.

1.2 v4l2_fops Encapsulation

The following uses the fops mmap as an example:

static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm){ struct video_device *vdev = video_devdata(filp); int ret = -ENODEV; if (!vdev->fops->mmap) return -ENODEV; if (video_is_registered(vdev)) ret = vdev->fops->mmap(filp, vm); if (vdev->debug) printk(KERN_DEBUG "%s: mmap (%d)\n", video_device_node_name(vdev), ret); return ret;}

We can see thisThe mmap function is still in use.Vdev-> fops-> mmap, hereVdev-> the fops struct isStruct v4l2_file_operations, which is defined as follows:

struct v4l2_file_operations { struct module *owner; ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*ioctl) (struct file *, unsigned int, unsigned long); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);#ifdef CONFIG_COMPAT long (*compat_ioctl32) (struct file *, unsigned int, unsigned long);#endif unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct file *); int (*release) (struct file *);};

We can see that this struct andStruct file_operations are very similar, and each function pointer correspondsA function pointer of struct file_operations. The difference is that these function pointers no longer haveIonode parameter.

1.3 v4l2_file_operations splitting

Let's take a look.Definition of struct video_device

struct video_device{#if defined(CONFIG_MEDIA_CONTROLLER) struct media_entity entity;#endif /* device ops */ const struct v4l2_file_operations *fops; /* sysfs */ struct device dev; /* v4l device */ struct cdev *cdev; /* character device */ /* Set either parent or v4l2_dev if your driver uses v4l2_device */ struct device *parent; /* device parent */ struct v4l2_device *v4l2_dev; /* v4l2_device parent */ /* Control handler associated with this device node. May be NULL. */ struct v4l2_ctrl_handler *ctrl_handler; /* vb2_queue associated with this device node. May be NULL. */ struct vb2_queue *queue; /* Priority state. If NULL, then v4l2_dev->prio will be used. */ struct v4l2_prio_state *prio; /* device info */ char name[32]; int vfl_type; /* device type */ int vfl_dir; /* receiver, transmitter or m2m */ /* 'minor' is set to -1 if the registration failed */ int minor; u16 num; /* use bitops to set/clear/test flags */ unsigned long flags; /* attribute to differentiate multiple indices on one physical device */ int index; /* V4L2 file handles */ spinlock_t fh_lock; /* Lock for all v4l2_fhs */ struct list_head fh_list; /* List of struct v4l2_fh */ int debug; /* Activates debug level*/ /* Video standard vars */ v4l2_std_id tvnorms; /* Supported tv norms */ v4l2_std_id current_norm; /* Current tvnorm */ /* callbacks */ void (*release)(struct video_device *vdev); /* ioctl callbacks */ const struct v4l2_ioctl_ops *ioctl_ops; DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE); /* serialization lock */ DECLARE_BITMAP(disable_locking, BASE_VIDIOC_PRIVATE); struct mutex *lock;};

Note that apart fromIn addition to the ops parameter of v4l2_file_operationsStruct v4l2_ioctl_ops parameter. What operation function is this parameter? This parameter is also a series of parameter sets.Every ioctl CMD of the IOCTL function in struct v4l2_file_operations is decomposed into one function. then all these functions are collected and encapsulated into this struct. the specific callback process will be discussed later. because the v4l2 driver mainly involves a variety of ioctl, there are too many ioctl, and all of them are written in a function using swich case, it must be smelly and long. this improves code readability and maintainability .?
This indicates the v4l2-core willV4l2_file_operations is divided into two interfaces, one isIoctl, anotherOther v4l2_file_operations members .?
Here we know that to implement a v4l2 driver, we must implement the following:

v4l2_ioctl_ops

Other v4l2_file_operations members

1.4 soc_camera convergence

There is a dotted line in the figure, where the dotted line above represents the content implemented by the v4l2-core, and below the dotted line is the soc_camera subsystem implemented on the v4l2-core. this subsystem is mainly used to implement the driver of the Image Acquisition Device. as shown in the figure, the soc_camera sub-system implements the two interfaces that must be implemented above, and then exposesSoc_camera_host_ops interface. This interface is mainly related to some operation function interfaces of soc_camera host. If you need to implement a soc_camera host, you only need to implement these interfaces. in this way, developers only need to focus on the design of operational functions closely related to camera host .?
(Here, the description of subdev is omitted for the time being. I will add it later)

1.5 Summary

File_operations is an interface used to implement user space requests, in the v4l2-coreV4l2_fops implements this interface and exposes it to the lower layer.V4l2_file_operations interface, the ioctl function of this interface isVideo_ioctl2 function implementation, and then exposed to the lower layerV4l2_ioctl_ops interface, which is implemented by the soc_camera SubsystemV4l2_ioctl_ops interface andV4l2_file_operations 'except for ioctlAnd then exposed soc_camera_host to the lower layer.Interface. Finally, the driver of soc_camera_host implements this interface, thus completing the registration of the entire v4l2 framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.