Commit 52d78296 authored by Dafna Hirschfeld's avatar Dafna Hirschfeld

18.08.2019

parent 309ad9e0
......@@ -17,6 +17,7 @@ mkdir "/configfs/vimc/mdev/entities/vimc-sensor:RGB/YUV Input"
mkdir "/configfs/vimc/mdev/entities/vimc-scaler:Scaler"
mkdir "/configfs/vimc/mdev/entities/vimc-capture:RGB/YUV Capture"
mkdir "/configfs/vimc/mdev/links/my-sensor:0->my-capture:0"
# TODO - add the links
# mkdir "/configfs/vimc/mdev/links/my-sensor:0->my-capture:0"
echo 3 > "/configfs/vimc/mdev/links/my-sensor:0->my-capture:0/flags"
echo 1 > /configfs/vimc/mdev/hotplug
This diff is collapsed.
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* vimc-common.h Virtual Media Controller Driver
*
* Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
*/
#ifndef _VIMC_COMMON_H_
#define _VIMC_COMMON_H_
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <media/media-device.h>
#include <media/v4l2-device.h>
#define VIMC_PDEV_NAME "vimc"
#define VIMC_MAX_NAME_LEN 32
/* VIMC-specific controls */
#define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
#define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
#define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
#define VIMC_FRAME_MAX_WIDTH 4096
#define VIMC_FRAME_MAX_HEIGHT 2160
#define VIMC_FRAME_MIN_WIDTH 16
#define VIMC_FRAME_MIN_HEIGHT 16
#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
/**
* struct vimc_colorimetry_clamp - Adjust colorimetry parameters
*
* @fmt: the pointer to struct v4l2_pix_format or
* struct v4l2_mbus_framefmt
*
* Entities must check if colorimetry given by the userspace is valid, if not
* then set them as DEFAULT
*/
#define vimc_colorimetry_clamp(fmt) \
do { \
if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
|| (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
(fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
} \
if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
} while (0)
/**
* struct vimc_platform_data_core - platform data to the core
*
* @ents: list of vimc_entity objects allocated by the configfs
* @links: list of vimc_links objects allocated by the configfs
*/
struct vimc_platform_data_core {
struct list_head *ents;
struct list_head *links;
};
/**
* struct vimc_pix_map - maps media bus code with v4l2 pixel format
*
* @code: media bus format code defined by MEDIA_BUS_FMT_* macros
* @bbp: number of bytes each pixel occupies
* @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
*
* Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
* V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
*/
struct vimc_pix_map {
unsigned int code;
unsigned int bpp;
u32 pixelformat;
bool bayer;
};
/**
* struct vimc_device - main device for vimc driver
*
* @pdev pointer to the platform device
* @pipe_cfg pointer to the vimc pipeline configuration structure
* @mdev the associated media_device parent
* @v4l2_dev Internal v4l2 parent device
*/
struct vimc_device {
/* The Associated media_device parent */
struct media_device mdev;
/* Internal v4l2 parent device */
struct v4l2_device v4l2_dev;
};
/**
* struct vimc_ent_device - core struct that represents a node in the topology
*
* @ent: the pointer to struct media_entity for the node
* @pads: the list of pads of the node
* @process_frame: callback send a frame to that node
* @vdev_get_format: callback that returns the current format a pad, used
* only when is_media_entity_v4l2_video_device(ent) returns
* true
*
* Each node of the topology must create a vimc_ent_device struct. Depending on
* the node it will be of an instance of v4l2_subdev or video_device struct
* where both contains a struct media_entity.
* Those structures should embedded the vimc_ent_device struct through
* v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
* vimc_ent_device struct to be retrieved from the corresponding struct
* media_entity
*/
struct vimc_ent_device {
struct media_entity *ent;
struct media_pad *pads;
void * (*process_frame)(struct vimc_ent_device *ved,
const void *frame);
void (*vdev_get_format)(struct vimc_ent_device *ved,
struct v4l2_pix_format *fmt);
};
struct vimc_entity {
char name[VIMC_MAX_NAME_LEN];
char drv_name[VIMC_MAX_NAME_LEN];
struct vimc_ent_device *vimc_ent_dev;
struct list_head list;
};
struct vimc_link {
char source_name[VIMC_MAX_NAME_LEN];
char sink_name[VIMC_MAX_NAME_LEN];
u16 source_pad;
u16 sink_pad;
u32 flags;
struct vimc_ent_device **source;
struct vimc_ent_device **sink;
struct list_head list;
};
#define VIMC_DEB_NAME "vimc-debayer"
#define VIMC_SEN_NAME "vimc-sensor"
#define VIMC_SCA_NAME "vimc-scaler"
#define VIMC_CAP_NAME "vimc-capture"
/**
* struct vimc_ent_config Structure which describes individual
* configuration for each entity
*
* @name entity name
* @ved pointer to vimc_ent_device (a node in the topology)
* @add subdev add hook - initializes and registers subdev
* called from vimc-core
* @rm subdev rm hook - unregisters and frees subdev
* called from vimc-core
*/
struct vimc_ent_type {
const char *name;
int (*add)(struct vimc_device *vimc, struct vimc_entity *ent);
void (*rm)(struct vimc_device *vimc, struct vimc_entity *ent);
};
/* prototypes for vimc_ent_config add and rm hooks */
int vimc_cap_add(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_cap_rm(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_cap_init(void);
void vimc_cap_exit(void);
int vimc_deb_add(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_deb_rm(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_deb_init(void);
void vimc_deb_exit(void);
int vimc_sca_add(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_sca_rm(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_sca_init(void);
void vimc_sca_exit(void);
int vimc_sen_add(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_sen_rm(struct vimc_device *vimc, struct vimc_entity *ent);
void vimc_sen_init(void);
void vimc_sen_exit(void);
/**
* vimc_pads_init - initialize pads
*
* @num_pads: number of pads to initialize
* @pads_flags: flags to use in each pad
*
* Helper functions to allocate/initialize pads
*/
struct media_pad *vimc_pads_init(u16 num_pads,
const unsigned long *pads_flag);
/**
* vimc_pads_cleanup - free pads
*
* @pads: pointer to the pads
*
* Helper function to free the pads initialized with vimc_pads_init
*/
static inline void vimc_pads_cleanup(struct media_pad *pads)
{
kfree(pads);
}
/**
* vimc_pipeline_s_stream - start stream through the pipeline
*
* @ent: the pointer to struct media_entity for the node
* @enable: 1 to start the stream and 0 to stop
*
* Helper function to call the s_stream of the subdevices connected
* in all the sink pads of the entity
*/
int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
/**
* vimc_pix_map_by_index - get vimc_pix_map struct by its index
*
* @i: index of the vimc_pix_map struct in vimc_pix_map_list
*/
const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
/**
* vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
*
* @code: media bus format code defined by MEDIA_BUS_FMT_* macros
*/
const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
/**
* vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
*
* @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
*/
const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
/**
* vimc_ent_sd_register - initialize and register a subdev node
*
* @ved: the vimc_ent_device struct to be initialize
* @sd: the v4l2_subdev struct to be initialize and registered
* @v4l2_dev: the v4l2 device to register the v4l2_subdev
* @name: name of the sub-device. Please notice that the name must be
* unique.
* @function: media entity function defined by MEDIA_ENT_F_* macros
* @num_pads: number of pads to initialize
* @pads_flag: flags to use in each pad
* @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
* @sd_ops: pointer to &struct v4l2_subdev_ops.
*
* Helper function initialize and register the struct vimc_ent_device and struct
* v4l2_subdev which represents a subdev node in the topology
*/
int vimc_ent_sd_register(struct vimc_ent_device *ved,
struct v4l2_subdev *sd,
struct v4l2_device *v4l2_dev,
const char *const name,
u32 function,
u16 num_pads,
const unsigned long *pads_flag,
const struct v4l2_subdev_internal_ops *sd_int_ops,
const struct v4l2_subdev_ops *sd_ops);
/**
* vimc_ent_sd_unregister - cleanup and unregister a subdev node
*
* @ved: the vimc_ent_device struct to be cleaned up
* @sd: the v4l2_subdev struct to be unregistered
*
* Helper function cleanup and unregister the struct vimc_ent_device and struct
* v4l2_subdev which represents a subdev node in the topology
*/
void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
struct v4l2_subdev *sd);
/**
* vimc_link_validate - validates a media link
*
* @link: pointer to &struct media_link
*
* This function calls validates if a media link is valid for streaming.
*/
int vimc_link_validate(struct media_link *link);
#endif
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* vimc-configfs.h Virtual Media Controller Driver
*
* Copyright (C) 2018 Helen Koike <helen.koike@collabora.com>
*/
#ifndef _VIMC_CONFIGFS_H_
#define _VIMC_CONFIGFS_H_
#include <linux/configfs.h>
#define VIMC_CFS_SRC_PAD_NAME(n) "pad:source:" #n
#define VIMC_CFS_SINK_PAD_NAME(n) "pad:sink:" #n
extern struct config_item_type vimc_default_cfs_pad_type;
struct vimc_cfs_drv {
const char *name;
struct list_head list;
void (*const configfs_cb)(struct config_group *parent,
struct config_group childs[]);
};
int vimc_cfs_subsys_register(void);
void vimc_cfs_subsys_unregister(void);
void vimc_cfs_drv_register(struct vimc_cfs_drv *c_drv);
void vimc_cfs_drv_unregister(struct vimc_cfs_drv *c_drv);
#endif
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* vimc-core.c Virtual Media Controller Driver
*
* Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <media/media-device.h>
#include <media/v4l2-device.h>
#include "vimc-common.h"
#define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
#include "vimc-configfs.h"
static struct vimc_ent_type ent_types[] = {
{
.name = VIMC_SEN_NAME,
.add = vimc_sen_add,
.rm = vimc_sen_rm,
},
{
.name = VIMC_DEB_NAME,
.add = vimc_deb_add,
.rm = vimc_deb_rm,
},
{
.name = VIMC_CAP_NAME,
.add = vimc_cap_add,
.rm = vimc_cap_rm,
},
{
.name = VIMC_SCA_NAME,
.add = vimc_sca_add,
.rm = vimc_sca_rm,
},
};
/* -------------------------------------------------------------------------- */
static int vimc_core_links_create(const struct vimc_device *vimc,
const struct vimc_platform_data_core *pdata)
{
struct vimc_link *link;
list_for_each_entry(link, pdata->links, list) {
int ret = media_create_pad_link((*(link->source))->ent,
link->source_pad,
(*(link->sink))->ent,
link->sink_pad,
link->flags);
pr_info("%s: created link\n",__func__);
if (ret)
return ret;
}
return 0;
}
static struct vimc_ent_type *vimc_get_ent_type(const char *drv_name)
{
int i;
for (i = 0; i < ARRAY_SIZE(ent_types); i++)
if (!strcmp(drv_name, ent_types[i].name))
return &ent_types[i];
return NULL;
}
static int vimc_add_subdevs(struct vimc_device *vimc,
const struct vimc_platform_data_core *pdata)
{
struct vimc_entity *ent;
struct vimc_entity *r_ent = NULL;
int ret;
list_for_each_entry(ent, pdata->ents, list) {
struct vimc_ent_type *ent_type =
vimc_get_ent_type(ent->drv_name);
//the configfs should have already validate userspace input
BUG_ON(!ent_type);
//this pointer will be filled by the .add callback and so
//if it is not NULL then something isn't right
if (!ent_type) {
ret = -EINVAL;
goto err;
}
BUG_ON(ent->vimc_ent_dev);
pr_info("%s: registering entity %s:%s\n", __func__,
ent->drv_name, ent->name);
ret = ent_type->add(vimc, ent);
if (ret) {
dev_err(vimc->mdev.dev, "failed to add entity %s:%s\n",
ent->drv_name, ent->name);
goto err;
}
}
return 0;
err:
list_for_each_entry_continue_reverse(r_ent, pdata->ents, list) {
struct vimc_ent_type *ent_type =
vimc_get_ent_type(r_ent->drv_name);
ent_type->rm(vimc, r_ent);
r_ent->vimc_ent_dev = NULL;
}
return ret;
}
static int vimc_register_devices(struct vimc_device *vimc,
const struct vimc_platform_data_core *pdata)
{
int ret;
/* Register the v4l2 struct */
ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
if (ret) {
dev_err(vimc->mdev.dev,
"v4l2 device register failed (err=%d)\n", ret);
return ret;
}
ret = vimc_add_subdevs(vimc, pdata);
if (ret)
goto err_v4l2_unregister;
ret = vimc_core_links_create(vimc, pdata);
if (ret)
goto err_v4l2_unregister;
/* Register the media device */
ret = media_device_register(&vimc->mdev);
if (ret) {
dev_err(vimc->mdev.dev,
"media device register failed (err=%d)\n", ret);
goto err_v4l2_unregister;
}
/* Expose all subdev's nodes*/
ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
if (ret) {
dev_err(vimc->mdev.dev,
"vimc subdev nodes registration failed (err=%d)\n",
ret);
goto err_mdev_unregister;
}
return 0;
err_mdev_unregister:
media_device_unregister(&vimc->mdev);
media_device_cleanup(&vimc->mdev);
err_v4l2_unregister:
v4l2_device_unregister(&vimc->v4l2_dev);
return ret;
}
static void vimc_rm_subdevs(struct vimc_device *vimc,
struct vimc_platform_data_core *pdata)
{
struct vimc_entity *ent;
int i = 0;
list_for_each_entry(ent, pdata->ents, list) {
struct vimc_ent_type *ent_type = vimc_get_ent_type(ent->drv_name);
i++;
pr_info("%s: removing entity %s:%s\n", __func__, ent->drv_name, ent->name);
//the configfs should have already validate userspace input
BUG_ON(!ent_type);
if (!ent_type)
return;
//this should not be null when removing the devices
BUG_ON(!ent->vimc_ent_dev);
if (!ent->vimc_ent_dev)
return;
dev_dbg(vimc->mdev.dev, "removing entity %s:%s\n", ent->drv_name, ent->name);
ent_type->rm(vimc, ent);
if (i>100)
return;
}
}
static int vimc_probe(struct platform_device *pdev)
{
const struct vimc_platform_data_core *pdata = pdev->dev.platform_data;
struct vimc_device *vimc;
int ret = 0;
dev_dbg(&pdev->dev, "probe\n");
vimc = devm_kzalloc(&pdev->dev, sizeof(*vimc),
GFP_KERNEL);
memset(&vimc->mdev, 0, sizeof(vimc->mdev));
pr_info("%s: %px\n", __func__, vimc);
/* Link the media device within the v4l2_device */
vimc->v4l2_dev.mdev = &vimc->mdev;
/* Initialize media device */
strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
sizeof(vimc->mdev.model));
snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
"platform:%s", VIMC_PDEV_NAME);
vimc->mdev.dev = &pdev->dev;
media_device_init(&vimc->mdev);
ret = vimc_register_devices(vimc, pdata);
if (ret) {
media_device_cleanup(&vimc->mdev);
kfree(vimc);
return ret;
}
platform_set_drvdata(pdev, vimc);
return 0;
}
//ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
//vimc_add_subdevs(vimc, pdata);
//ret = vimc_core_links_create(vimc, pdata);
//ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
//ret = media_device_register(&vimc->mdev);
static int vimc_remove(struct platform_device *pdev)
{
struct vimc_device *vimc = platform_get_drvdata(pdev);
struct vimc_platform_data_core *pdata = pdev->dev.platform_data;
dev_dbg(&pdev->dev, "remove\n");
pr_info("%s: %px\n", __func__, vimc);
media_device_unregister(&vimc->mdev);
media_device_cleanup(&vimc->mdev);
vimc_rm_subdevs(vimc, pdata);
v4l2_device_unregister(&vimc->v4l2_dev);
// no need to free vimc, since according to the devm_kmalloc's doc:
// Memory allocated with this function is automatically freed on driver detach ..
return 0;
}
static struct platform_driver vimc_pdrv = {
.probe = vimc_probe,
.remove = vimc_remove,
.driver = {
.name = "vimc-core",
},
};
static int __init vimc_init(void)
{
int ret;
ret = platform_driver_register(&vimc_pdrv);
if (ret)
return ret;
ret = vimc_cfs_subsys_register();
if (ret) {
pr_err("%s: vimc_cfs_subsys_register failed (%d)\n", __func__, ret);
platform_driver_unregister(&vimc_pdrv);
return ret;
}
vimc_sen_init();
vimc_deb_init();
vimc_sca_init();
vimc_cap_init();
return 0;
}
static void __exit vimc_exit(void)
{
vimc_sen_exit();
vimc_deb_exit();
vimc_sca_exit();
vimc_cap_exit();
vimc_cfs_subsys_unregister();
platform_driver_unregister(&vimc_pdrv);
}
module_init(vimc_init);
module_exit(vimc_exit);
MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
MODULE_LICENSE("GPL");
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0+
/*
* vimc-streamer.c Virtual Media Controller Driver
*
* Copyright (C) 2018 Lucas A. M. Magalhães <lucmaga@gmail.com>
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include "vimc-streamer.h"
/**
* vimc_get_source_entity - get the entity connected with the first sink pad
*
* @ent: reference media_entity
*
* Helper function that returns the media entity containing the source pad
* linked with the first sink pad from the given media entity pad list.
*
* Return: The source pad or NULL, if it wasn't found.
*/
static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
{
struct media_pad *pad;
int i;
for (i = 0; i < ent->num_pads; i++) {
if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
continue;
pad = media_entity_remote_pad(&ent->pads[i]);
return pad ? pad->entity : NULL;
}
return NULL;
}
/**
* vimc_streamer_pipeline_terminate - Disable stream in all ved in stream
*
* @stream: the pointer to the stream structure with the pipeline to be
* disabled.
*
* Calls s_stream to disable the stream in each entity of the pipeline
*
*/
static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
{
struct vimc_ent_device *ved;
struct v4l2_subdev *sd;
while (stream->pipe_size) {
stream->pipe_size--;
ved = stream->ved_pipeline[stream->pipe_size];
stream->ved_pipeline[stream->pipe_size] = NULL;
if (!is_media_entity_v4l2_subdev(ved->ent))
continue;
sd = media_entity_to_v4l2_subdev(ved->ent);
v4l2_subdev_call(sd, video, s_stream, 0);
}
}
/**
* vimc_streamer_pipeline_init - Initializes the stream structure
*
* @stream: the pointer to the stream structure to be initialized
* @ved: the pointer to the vimc entity initializing the stream
*
* Initializes the stream structure. Walks through the entity graph to
* construct the pipeline used later on the streamer thread.
* Calls vimc_streamer_s_stream() to enable stream in all entities of
* the pipeline.
*
* Return: 0 if success, error code otherwise.
*/
static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
struct vimc_ent_device *ved)
{
struct media_entity *entity;
struct video_device *vdev;
struct v4l2_subdev *sd;
int ret = 0;
stream->pipe_size = 0;
while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
if (!ved) {
vimc_streamer_pipeline_terminate(stream);
return -EINVAL;
}
stream->ved_pipeline[stream->pipe_size++] = ved;
if (is_media_entity_v4l2_subdev(ved->ent)) {
sd = media_entity_to_v4l2_subdev(ved->ent);
ret = v4l2_subdev_call(sd, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD) {
pr_err("subdev_call error %s\n",
ved->ent->name);
vimc_streamer_pipeline_terminate(stream);
return ret;
}