/* 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/formats.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(),
|