Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement video control framework #82158

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions drivers/video/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
zephyr_library()

zephyr_library_sources(video_common.c)
zephyr_library_sources(video_device.c)

zephyr_library_sources_ifdef(CONFIG_VIDEO_MCUX_CSI video_mcux_csi.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_MCUX_MIPI_CSI2RX video_mcux_mipi_csi2rx.c)
Expand Down
82 changes: 82 additions & 0 deletions drivers/video/video_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "video_device.h"

#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(video_device, CONFIG_VIDEO_LOG_LEVEL);

#define VIDEO_NUM_DEVICES 256

static struct video_device *video_devices[VIDEO_NUM_DEVICES];

static bool video_is_vdev_registered(struct video_device *vdev)
{
for (uint8_t i = 0; i < VIDEO_NUM_DEVICES; ++i) {
if (!video_devices[i] && video_devices[i] == vdev) {
return true;
}
}

return false;
}

int video_register_vdev(struct video_device *vdev, const struct device *dev)
{
uint8_t i;

if (!vdev) {
return -EINVAL;
}

if (video_is_vdev_registered(vdev)) {
LOG_ERR("Video device %s is already registered at index %d", vdev->dev->name,
vdev->ind);
return -EALREADY;
}

vdev->dev = dev;

for (i = 0; i < VIDEO_NUM_DEVICES; ++i) {
if (!video_devices[i]) {
video_devices[i] = vdev;
vdev->ind = i;
break;
}
}

if (i == VIDEO_NUM_DEVICES) {
LOG_ERR("No space left to register a new video device");
vdev->ind = -1;
return -ENOSPC;
}

return 0;
}

void video_unregister_vdev(struct video_device *vdev)
{
if (!video_is_vdev_registered(vdev)) {
LOG_ERR("Video device %s, index = %d is not registered", vdev->dev->name,
vdev->ind);
return;
}

video_devices[vdev->ind] = NULL;
vdev->ind = -1;
}

struct video_device *video_find_vdev(const struct device *dev)
{
for (uint8_t i = 0; i < VIDEO_NUM_DEVICES; ++i) {
if (video_devices[i] != NULL && video_devices[i]->dev == dev) {
return video_devices[i];
}
}

return NULL;
}
Comment on lines +75 to +84
Copy link
Collaborator

@josuah josuah Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as here, I think this can be removed altogether if it is possible to have a vdata = dev->data pointer.

23 changes: 23 additions & 0 deletions drivers/video/video_device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_VIDEO_DEVICE_H_
#define ZEPHYR_INCLUDE_VIDEO_DEVICE_H_

#include <zephyr/device.h>

struct video_device {
int ind;
const struct device *dev;
};

int video_register_vdev(struct video_device *vdev, const struct device *dev);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it simply be video_device_register/unregister()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the naming pattern with the prefix video_dosomething() but yes we can simplify it as your suggestion.


void video_unregister_vdev(struct video_device *vdev);

struct video_device *video_find_vdev(const struct device *dev);

#endif /* ZEPHYR_INCLUDE_VIDEO_DEVICE_H_ */