diff options
Diffstat (limited to 'include/linux')
0 files changed, 0 insertions, 0 deletions
![]() |
index : libcamera/jmondi/libcamera.git | |
Jacopo Mondi's clone of libcamera | git repository hosting on libcamera.org |
summaryrefslogtreecommitdiff |
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* v4l2_device.cpp - Common base for V4L2 video devices and subdevices
*/
#include "libcamera/internal/v4l2_device.h"
#include <fcntl.h>
#include <iomanip>
#include <limits.h>
#include <map>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <vector>
#include <linux/v4l2-mediabus.h>
#include <libcamera/base/event_notifier.h>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/sysfs.h"
/**
* \file v4l2_device.h
* \brief Common base for V4L2 devices and subdevices
*/
namespace libcamera {
LOG_DEFINE_CATEGORY(V4L2)
/**
* \class V4L2Device
* \brief Base class for V4L2VideoDevice and V4L2Subdevice
*
* The V4L2Device class groups together the functions and fields common to
* both the V4L2VideoDevice and V4L2Subdevice classes, and provides a base
* class with functions to open and close the device node associated with the
* device and to perform IOCTL system calls on it.
*
* The V4L2Device class cannot be instantiated directly, as its constructor
* is protected. Users should instead create instances of one the derived
* classes to model either a V4L2 video device or a V4L2 subdevice.
*/
/**
* \brief Construct a V4L2Device
* \param[in] deviceNode The device node filesystem path
*
* Initialize the file descriptor to -1 and store the \a deviceNode to be used
* at open() time, and the \a logTag to prefix log messages with.
*/
V4L2Device::V4L2Device(const std::string &deviceNode)
: deviceNode_(deviceNode), fdEventNotifier_(nullptr),
frameStartEnabled_(false)
{
}
/**
* \brief Destroy a V4L2Device
*/
V4L2Device::~V4L2Device()
{
}
/**
* \brief Open a V4L2 device node
* \param[in] flags Access mode flags
*
* Open the device node path with the provided access mode \a flags and
* initialize the file descriptor, which was initially set to -1.
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Device::open(unsigned int flags)
{
if (isOpen()) {
LOG(V4L2, Error) << "Device already open";
return -EBUSY;
}
UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));
if (!fd.isValid()) {
int ret = -errno;
LOG(V4L2, Error) << "Failed to open V4L2 device: "
<< strerror(-ret);
return ret;
}
setFd(std::move(fd));
listControls();
return 0;
}
/**
* \brief Set the file descriptor of a V4L2 device
* \param[in] fd The file descriptor handle
*
* This function allows a device to provide an already opened file descriptor
* referring to the V4L2 device node, instead of opening it with open(). This
* can be used for V4L2 M2M devices where a single video device node is used for
* both the output and capture devices, or when receiving an open file
* descriptor in a context that doesn't have permission to open the device node
* itself.
*
* This function and the open() function are mutually exclusive, only one of the
* two shall be used for a V4L2Device instance.
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Device::setFd(UniqueFD fd)
{
if (isOpen())
return -EBUSY;
fd_ = std::move(fd);
fdEventNotifier_ = new EventNotifier(fd_.get(), EventNotifier::Exception);
fdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);
fdEventNotifier_->setEnabled(false);
return 0;
}
/**
* \brief Close the device node
*
* Reset the file descriptor to -1
*/
void V4L2Device::close()
{
if (!isOpen())
return;
delete fdEventNotifier_;
fd_.reset();
}
/**
* \fn V4L2Device::isOpen()
* \brief Check if the V4L2 device node is open
* \return True if the V4L2 device node is open, false otherwise
*/
/**
* \fn V4L2Device::controls()
* \brief Retrieve the supported V4L2 controls and their information
* \return A map of the V4L2 controls supported by the device
*/
/**
* \brief Read controls from the device
* \param[in] ids The list of controls to read, specified by their ID
*