简体   繁体   中英

Setting ctrl with V4L2 on ov5640

I would like to control various ov5640 camera parameters by using ioctl and VIDIOC_S_CTRL from V4L2 in the following manner:

#include <string>
#include <iostream>

#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <cstring>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>

#define IOCTL_TRIES 3
#define CLEAR(x) memset (&(x), 0, sizeof (x))

static int xioctl(int fd, int request, void *arg)
{
    int r;
    int tries = IOCTL_TRIES;

    do {
        r = ioctl(fd, request, arg);
    } while (--tries > 0 && r == -1 && EINTR == errno);

    return r;
}

bool v4l2_ctrl_set(int fd, uint32_t id, int val)
{
    struct v4l2_control ctrl;
    CLEAR(ctrl);

    ctrl.id = id;
    ctrl.value = val;
    if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
        std::cout << "Failed to set ctrl with id " 
                  << id << " to value " << val
                  << "\nerror (" << errno << "): " << strerror(errno) << std::endl;

        return false;
    }

    return true;
}

int main()
{

    int fd = open("/dev/video0", O_RDWR | O_NONBLOCK);
    if (fd == -1) {
        std::cout << "Failed to open the camera" << std::endl;
        return -1;
    }

    v4l2_ctrl_set(fd, V4L2_CID_SATURATION, 100);

    return 0;
}

Unfortunately ioctl fails and I get error (25): Inappropriate ioctl for device . I'm using Intrinsyc Open-Q 820 µSOM with linaro 4.14 . I've managed to add some debugs prints to ov5640 driver file in ov5640_s_ctrl function before if (sensor->power_count == 0) { (in case there were problems with power save mode) and recompile the kernel. I ran the code, but looking through dmesg my printk message doesn't get printed, so that means that ov5640_s_ctrl doesn't get called even though the callback is set:

static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
    .g_volatile_ctrl = ov5640_g_volatile_ctrl,
    .s_ctrl = ov5640_s_ctrl,
};

Am I using V4L2 wrong? Should I enable something before setting the controls? It's even more confusing since I manage to get an image from the camera with v4l2, but I can't set/get any controls.

In the the kernel source code of ov5640.c that you supplied, the driver is assigned the flag V4L2_SUBDEV_FL_HAS_DEVNODE which means it might supply a subdev node /dev/v4l-subdevX . According to the kernel docs:

Device nodes named v4l-subdevX can be created in /dev to access sub-devices directly. If a sub-device supports direct userspace configuration it must set the V4L2_SUBDEV_FL_HAS_DEVNODE flag before being registered.`

So you can try to set the control directly from the v4l-subdevX node if it exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM