V4l2 SoC-camera analysis-call relationship

Source: Internet
Author: User

The application layer opens video4linux devices through the device node/dev/videox. /Dev/videox is a character device with the master device number 81. Sub-device Number: (0 ~ 63) assigned to the capture device, 64 ~ 127 allocate to radio device, 223 ~ 255 allocated to VBI devices, 128 ~ 191.

If the driver needs to register a video4linux device, call the video_register_device function.

The dump_stack function can be used to conveniently obtain the kernel function call relationship.

Open call relationship

From the call relationship perspective, open is the most complex, because it not only needs to perform the real open operation, but also needs to set the operation function for mmap ioctl.

1. Application opens the device node/dev/videox through open

2. Go to the system to call sys_open and sys_open to call do_sys_open.

3. do_sys_open call do_flip_open

4. The do_filp_open function is a long function. Most of the Code is related to path search. Here you only need to pay attention to do_last.

5. Most of do_last is path-related code. You only need to pay attention to finish_open.

6. When finish_open calls this function, the nameidata structure corresponding to the path name has been filled and nameidata_to_flip is called.

7. nameidata_to_flip call _ dentry_open

8. _ dentry_open will call chrdev_open. The following code snippets are contained in _ dentry_open:

    f->f_op = fops_get(inode->i_fop);    .....    if (!open && f->f_op)        open = f->f_op->open;    if (open) {        error = open(inode, f);        if (error)            goto cleanup_all;    }

During the system path lookup, The inode corresponding to the device file will be read into the memory. During the process of reading the inode file, determine which of the following types of inode is used: regualr, Char, block, and pipe. In this case, different inode types are assigned to inode-> I _fop different operation functions.

In the current case, shmem_get_inode will call init_special_inode to initialize this inode. Since/dev/videox is a character device, inode-> I _fop = & def_chr_fops. Def_chr_fops.open = chrdev_open

9. chrdev_open searches for the corresponding cdev object based on the primary and secondary devices of the device node, and assigns the cdev-> Ops to filp-> f_op (this assignment operation is an important step, it associates filp with the operation function of a specific device). The current case, cdev-> ops is v4l2_ops. The open operation is completed by calling filp-> f_op-> open, and flip-> f_op-> open is v4l2_open.

10. In v4l2_open, call vdev-> fops-> open. For SOC camera, we have set vdev-> fops to soc_camera_fops before calling video_register_device. Vdev-> fops-> open: Call soc_camera_open

11. After a large circle, soc_camera_open was finally called.

MMAP call relationship

1. The system calls mmap_pgoff, and mmap_pgoff calls do_mmap_pgoff.

2. do_mmap_pgoff, call mmap_region

3. mmap_region calls filp-> f_op-> MMAP. When the file is opened for the first time, file-> f_op is set to v4l2_ops, and file-> f_op-> MMAP is set to v4l2_mmap.

4. v4l2_mmap: v4l2_mmap calls vdev-> fops-> MMAP. For soc_camera, vdev-> fops is soc_camera_ops, so vdev-> fops-> MMAP is soc_camera_mmap

5. soc_camera_mmap

IOCTL call relationship

1. The system calls IOCTL and do_vfs_ioctl.

2. do_vfs_ioctl, call vfs_ioctl

3. vfs_ioctl code snippet

    if (filp->f_op->unlocked_ioctl) {        error = filp->f_op->unlocked_ioctl(filp, cmd, arg);        if (error == -ENOIOCTLCMD)            error = -EINVAL;        goto out;    } else if (filp->f_op->ioctl) {        lock_kernel();        error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,                      filp, cmd, arg);        unlock_kernel();    }

When you open the file for the first time, you have set filp-> f_op to v4l2_ops. Note that the implementation of v4l2_ops is different in different kernel versions. IOCTL may be defined, or unlocked_ioctl may be defined. My kernel v4l2_ops is defined as follows:

static const struct file_operations v4l2_fops = {    .owner = THIS_MODULE,    .read = v4l2_read,    .write = v4l2_write,    .open = v4l2_open,    .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,};

Therefore, filep-> f_op_unlocked_ioctl is v4l2_ioctl.

5. The v4l2_ioctl code snippet is as follows:

    if (vdev->fops->unlocked_ioctl) {        if (vdev->lock && mutex_lock_interruptible(vdev->lock))            return -ERESTARTSYS;        if (video_is_registered(vdev))            ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);        if (vdev->lock)            mutex_unlock(vdev->lock);

Soc_camera_fops.unlocked_ioctl = video_ioctl2

6. video_ioctl2 call _ video_do_ioctl

7. _ video_do_ioctl: This function makes some basic judgment on the parameter and then calls the ioctl_ops of video_device. For the soc_camera system, it is soc_camera_ioctl_ops.

8 soc_camera_ioctl_ops implementation:

static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {    .vidioc_querycap     = soc_camera_querycap,    .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,    .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,    .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,    .vidioc_enum_input   = soc_camera_enum_input,    .vidioc_g_input      = soc_camera_g_input,    .vidioc_s_input      = soc_camera_s_input,    .vidioc_s_std        = soc_camera_s_std,    .vidioc_reqbufs      = soc_camera_reqbufs,    .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,    .vidioc_querybuf     = soc_camera_querybuf,    .vidioc_qbuf         = soc_camera_qbuf,    .vidioc_dqbuf        = soc_camera_dqbuf,    .vidioc_streamon     = soc_camera_streamon,    .vidioc_streamoff    = soc_camera_streamoff,    .vidioc_queryctrl    = soc_camera_queryctrl,    .vidioc_g_ctrl       = soc_camera_g_ctrl,    .vidioc_s_ctrl       = soc_camera_s_ctrl,    .vidioc_cropcap      = soc_camera_cropcap,    .vidioc_g_crop       = soc_camera_g_crop,    .vidioc_s_crop       = soc_camera_s_crop,    .vidioc_g_parm       = soc_camera_g_parm,    .vidioc_s_parm       = soc_camera_s_parm,    .vidioc_g_chip_ident     = soc_camera_g_chip_ident,#ifdef CONFIG_VIDEO_ADV_DEBUG    .vidioc_g_register   = soc_camera_g_register,    .vidioc_s_register   = soc_camera_s_register,#endif};

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.