Commit 2a1fcdf0 authored by Hans Verkuil's avatar Hans Verkuil Committed by Mauro Carvalho Chehab

V4L/DVB (9820): v4l2: add v4l2_device and v4l2_subdev structs to the v4l2 framework.

Start implementing a proper v4l2 framework as discussed during the
Linux Plumbers Conference 2008.

Introduces v4l2_device (for device instances) and v4l2_subdev (representing
sub-device instances).
Signed-off-by: default avatarHans Verkuil <hverkuil@xs4all.nl>
Reviewed-by: default avatarLaurent Pinchart <laurent.pinchart@skynet.be>
Reviewed-by: default avatarGuennadi Liakhovetski <g.liakhovetski@gmx.de>
Reviewed-by: default avatarAndy Walls <awalls@radix.net>
Reviewed-by: default avatarDavid Brownell <david-b@pacbell.net>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent 07b1747c
This diff is collapsed.
...@@ -10,7 +10,7 @@ stkwebcam-objs := stk-webcam.o stk-sensor.o ...@@ -10,7 +10,7 @@ stkwebcam-objs := stk-webcam.o stk-sensor.o
omap2cam-objs := omap24xxcam.o omap24xxcam-dma.o omap2cam-objs := omap24xxcam.o omap24xxcam-dma.o
videodev-objs := v4l2-dev.o v4l2-ioctl.o videodev-objs := v4l2-dev.o v4l2-ioctl.o v4l2-device.o v4l2-subdev.o
obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-compat-ioctl32.o v4l2-int-device.o obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-compat-ioctl32.o v4l2-int-device.o
......
/*
V4L2 device support.
Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/types.h>
#include <linux/ioctl.h>
#include <linux/i2c.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
{
if (dev == NULL || v4l2_dev == NULL)
return -EINVAL;
/* Warn if we apparently re-register a device */
WARN_ON(dev_get_drvdata(dev));
INIT_LIST_HEAD(&v4l2_dev->subdevs);
spin_lock_init(&v4l2_dev->lock);
v4l2_dev->dev = dev;
snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
dev->driver->name, dev->bus_id);
dev_set_drvdata(dev, v4l2_dev);
return 0;
}
EXPORT_SYMBOL_GPL(v4l2_device_register);
void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
{
struct v4l2_subdev *sd, *next;
if (v4l2_dev == NULL || v4l2_dev->dev == NULL)
return;
dev_set_drvdata(v4l2_dev->dev, NULL);
/* unregister subdevs */
list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list)
v4l2_device_unregister_subdev(sd);
v4l2_dev->dev = NULL;
}
EXPORT_SYMBOL_GPL(v4l2_device_unregister);
int v4l2_device_register_subdev(struct v4l2_device *dev, struct v4l2_subdev *sd)
{
/* Check for valid input */
if (dev == NULL || sd == NULL || !sd->name[0])
return -EINVAL;
/* Warn if we apparently re-register a subdev */
WARN_ON(sd->dev);
if (!try_module_get(sd->owner))
return -ENODEV;
sd->dev = dev;
spin_lock(&dev->lock);
list_add_tail(&sd->list, &dev->subdevs);
spin_unlock(&dev->lock);
return 0;
}
EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
{
/* return if it isn't registered */
if (sd == NULL || sd->dev == NULL)
return;
spin_lock(&sd->dev->lock);
list_del(&sd->list);
spin_unlock(&sd->dev->lock);
sd->dev = NULL;
module_put(sd->owner);
}
EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);
/*
V4L2 sub-device support.
Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/types.h>
#include <linux/ioctl.h>
#include <linux/i2c.h>
#include <linux/videodev2.h>
#include <media/v4l2-subdev.h>
int v4l2_subdev_command(struct v4l2_subdev *sd, unsigned cmd, void *arg)
{
switch (cmd) {
case VIDIOC_QUERYCTRL:
return v4l2_subdev_call(sd, core, querymenu, arg);
case VIDIOC_G_CTRL:
return v4l2_subdev_call(sd, core, g_ctrl, arg);
case VIDIOC_S_CTRL:
return v4l2_subdev_call(sd, core, s_ctrl, arg);
case VIDIOC_QUERYMENU:
return v4l2_subdev_call(sd, core, queryctrl, arg);
case VIDIOC_LOG_STATUS:
return v4l2_subdev_call(sd, core, log_status);
case VIDIOC_G_CHIP_IDENT:
return v4l2_subdev_call(sd, core, g_chip_ident, arg);
case VIDIOC_INT_S_STANDBY:
return v4l2_subdev_call(sd, core, s_standby, *(u32 *)arg);
case VIDIOC_INT_RESET:
return v4l2_subdev_call(sd, core, reset, *(u32 *)arg);
case VIDIOC_INT_S_GPIO:
return v4l2_subdev_call(sd, core, s_gpio, *(u32 *)arg);
case VIDIOC_INT_INIT:
return v4l2_subdev_call(sd, core, init, *(u32 *)arg);
#ifdef CONFIG_VIDEO_ADV_DEBUG
case VIDIOC_DBG_G_REGISTER:
return v4l2_subdev_call(sd, core, g_register, arg);
case VIDIOC_DBG_S_REGISTER:
return v4l2_subdev_call(sd, core, s_register, arg);
#endif
case VIDIOC_INT_S_TUNER_MODE:
return v4l2_subdev_call(sd, tuner, s_mode, *(enum v4l2_tuner_type *)arg);
case AUDC_SET_RADIO:
return v4l2_subdev_call(sd, tuner, s_radio);
case VIDIOC_S_TUNER:
return v4l2_subdev_call(sd, tuner, s_tuner, arg);
case VIDIOC_G_TUNER:
return v4l2_subdev_call(sd, tuner, g_tuner, arg);
case VIDIOC_S_STD:
return v4l2_subdev_call(sd, tuner, s_std, *(v4l2_std_id *)arg);
case VIDIOC_S_FREQUENCY:
return v4l2_subdev_call(sd, tuner, s_frequency, arg);
case VIDIOC_G_FREQUENCY:
return v4l2_subdev_call(sd, tuner, g_frequency, arg);
case TUNER_SET_TYPE_ADDR:
return v4l2_subdev_call(sd, tuner, s_type_addr, arg);
case TUNER_SET_CONFIG:
return v4l2_subdev_call(sd, tuner, s_config, arg);
case VIDIOC_INT_AUDIO_CLOCK_FREQ:
return v4l2_subdev_call(sd, audio, s_clock_freq, *(u32 *)arg);
case VIDIOC_INT_S_AUDIO_ROUTING:
return v4l2_subdev_call(sd, audio, s_routing, arg);
case VIDIOC_INT_I2S_CLOCK_FREQ:
return v4l2_subdev_call(sd, audio, s_i2s_clock_freq, *(u32 *)arg);
case VIDIOC_INT_S_VIDEO_ROUTING:
return v4l2_subdev_call(sd, video, s_routing, arg);
case VIDIOC_INT_S_CRYSTAL_FREQ:
return v4l2_subdev_call(sd, video, s_crystal_freq, arg);
case VIDIOC_INT_DECODE_VBI_LINE:
return v4l2_subdev_call(sd, video, decode_vbi_line, arg);
case VIDIOC_INT_S_VBI_DATA:
return v4l2_subdev_call(sd, video, s_vbi_data, arg);
case VIDIOC_INT_G_VBI_DATA:
return v4l2_subdev_call(sd, video, g_vbi_data, arg);
case VIDIOC_S_FMT:
return v4l2_subdev_call(sd, video, s_fmt, arg);
case VIDIOC_G_FMT:
return v4l2_subdev_call(sd, video, g_fmt, arg);
case VIDIOC_INT_S_STD_OUTPUT:
return v4l2_subdev_call(sd, video, s_std_output, *(v4l2_std_id *)arg);
case VIDIOC_STREAMON:
return v4l2_subdev_call(sd, video, s_stream, 1);
case VIDIOC_STREAMOFF:
return v4l2_subdev_call(sd, video, s_stream, 0);
default:
return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
}
}
EXPORT_SYMBOL_GPL(v4l2_subdev_command);
/*
V4L2 device support header.
Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _V4L2_DEVICE_H
#define _V4L2_DEVICE_H
#include <media/v4l2-subdev.h>
/* Each instance of a V4L2 device should create the v4l2_device struct,
either stand-alone or embedded in a larger struct.
It allows easy access to sub-devices (see v4l2-subdev.h) and provides
basic V4L2 device-level support.
*/
#define V4L2_DEVICE_NAME_SIZE (BUS_ID_SIZE + 16)
struct v4l2_device {
/* dev->driver_data points to this struct */
struct device *dev;
/* used to keep track of the registered subdevs */
struct list_head subdevs;
/* lock this struct; can be used by the driver as well if this
struct is embedded into a larger struct. */
spinlock_t lock;
/* unique device name, by default the driver name + bus ID */
char name[V4L2_DEVICE_NAME_SIZE];
};
/* Initialize v4l2_dev and make dev->driver_data point to v4l2_dev */
int __must_check v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
/* Set v4l2_dev->dev->driver_data to NULL and unregister all sub-devices */
void v4l2_device_unregister(struct v4l2_device *v4l2_dev);
/* Register a subdev with a v4l2 device. While registered the subdev module
is marked as in-use. An error is returned if the module is no longer
loaded when you attempt to register it. */
int __must_check v4l2_device_register_subdev(struct v4l2_device *dev, struct v4l2_subdev *sd);
/* Unregister a subdev with a v4l2 device. Can also be called if the subdev
wasn't registered. In that case it will do nothing. */
void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
/* Iterate over all subdevs. */
#define v4l2_device_for_each_subdev(sd, dev) \
list_for_each_entry(sd, &(dev)->subdevs, list)
/* Call the specified callback for all subdevs matching the condition.
Ignore any errors. Note that you cannot add or delete a subdev
while walking the subdevs list. */
#define __v4l2_device_call_subdevs(dev, cond, o, f, args...) \
do { \
struct v4l2_subdev *sd; \
\
list_for_each_entry(sd, &(dev)->subdevs, list) \
if ((cond) && sd->ops->o && sd->ops->o->f) \
sd->ops->o->f(sd , ##args); \
} while (0)
/* Call the specified callback for all subdevs matching the condition.
If the callback returns an error other than 0 or -ENOIOCTLCMD, then
return with that error code. Note that you cannot add or delete a
subdev while walking the subdevs list. */
#define __v4l2_device_call_subdevs_until_err(dev, cond, o, f, args...) \
({ \
struct v4l2_subdev *sd; \
int err = 0; \
\
list_for_each_entry(sd, &(dev)->subdevs, list) { \
if ((cond) && sd->ops->o && sd->ops->o->f) \
err = sd->ops->o->f(sd , ##args); \
if (err && err != -ENOIOCTLCMD) \
break; \
} \
(err == -ENOIOCTLCMD) ? 0 : err; \
})
/* Call the specified callback for all subdevs matching grp_id (if 0, then
match them all). Ignore any errors. Note that you cannot add or delete
a subdev while walking the subdevs list. */
#define v4l2_device_call_all(dev, grp_id, o, f, args...) \
__v4l2_device_call_subdevs(dev, \
!(grp_id) || sd->grp_id == (grp_id), o, f , ##args)
/* Call the specified callback for all subdevs matching grp_id (if 0, then
match them all). If the callback returns an error other than 0 or
-ENOIOCTLCMD, then return with that error code. Note that you cannot
add or delete a subdev while walking the subdevs list. */
#define v4l2_device_call_until_err(dev, grp_id, o, f, args...) \
__v4l2_device_call_subdevs_until_err(dev, \
!(grp_id) || sd->grp_id == (grp_id), o, f , ##args)
#endif
/*
V4L2 sub-device support header.
Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _V4L2_SUBDEV_H
#define _V4L2_SUBDEV_H
#include <media/v4l2-common.h>
struct v4l2_device;
struct v4l2_subdev;
struct tuner_setup;
/* Sub-devices are devices that are connected somehow to the main bridge
device. These devices are usually audio/video muxers/encoders/decoders or
sensors and webcam controllers.
Usually these devices are controlled through an i2c bus, but other busses
may also be used.
The v4l2_subdev struct provides a way of accessing these devices in a
generic manner. Most operations that these sub-devices support fall in
a few categories: core ops, audio ops, video ops and tuner ops.
More categories can be added if needed, although this should remain a
limited set (no more than approx. 8 categories).
Each category has its own set of ops that subdev drivers can implement.
A subdev driver can leave the pointer to the category ops NULL if
it does not implement them (e.g. an audio subdev will generally not
implement the video category ops). The exception is the core category:
this must always be present.
These ops are all used internally so it is no problem to change, remove
or add ops or move ops from one to another category. Currently these
ops are based on the original ioctls, but since ops are not limited to
one argument there is room for improvement here once all i2c subdev
drivers are converted to use these ops.
*/
/* Core ops: it is highly recommended to implement at least these ops:
g_chip_ident
log_status
g_register
s_register
This provides basic debugging support.
The ioctl ops is meant for generic ioctl-like commands. Depending on
the use-case it might be better to use subdev-specific ops (currently
not yet implemented) since ops provide proper type-checking.
*/
struct v4l2_subdev_core_ops {
int (*g_chip_ident)(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip);
int (*log_status)(struct v4l2_subdev *sd);
int (*init)(struct v4l2_subdev *sd, u32 val);
int (*s_standby)(struct v4l2_subdev *sd, u32 standby);
int (*reset)(struct v4l2_subdev *sd, u32 val);
int (*s_gpio)(struct v4l2_subdev *sd, u32 val);
int (*queryctrl)(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
int (*g_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
int (*s_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
int (*querymenu)(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
int (*ioctl)(struct v4l2_subdev *sd, int cmd, void *arg);
#ifdef CONFIG_VIDEO_ADV_DEBUG
int (*g_register)(struct v4l2_subdev *sd, struct v4l2_register *reg);
int (*s_register)(struct v4l2_subdev *sd, struct v4l2_register *reg);
#endif
};
struct v4l2_subdev_tuner_ops {
int (*s_mode)(struct v4l2_subdev *sd, enum v4l2_tuner_type);
int (*s_radio)(struct v4l2_subdev *sd);
int (*s_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
int (*g_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
int (*g_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
int (*s_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
int (*s_type_addr)(struct v4l2_subdev *sd, struct tuner_setup *type);
int (*s_config)(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *config);
};
struct v4l2_subdev_audio_ops {
int (*s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
int (*s_i2s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route);
};
struct v4l2_subdev_video_ops {
int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route);
int (*s_crystal_freq)(struct v4l2_subdev *sd, struct v4l2_crystal_freq *freq);
int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
int (*s_stream)(struct v4l2_subdev *sd, int enable);
int (*s_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt);
int (*g_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt);
};
struct v4l2_subdev_ops {
const struct v4l2_subdev_core_ops *core;
const struct v4l2_subdev_tuner_ops *tuner;
const struct v4l2_subdev_audio_ops *audio;
const struct v4l2_subdev_video_ops *video;
};
#define V4L2_SUBDEV_NAME_SIZE 32
/* Each instance of a subdev driver should create this struct, either
stand-alone or embedded in a larger struct.
*/
struct v4l2_subdev {
struct list_head list;
struct module *owner;
struct v4l2_device *dev;
const struct v4l2_subdev_ops *ops;
/* name must be unique */
char name[V4L2_SUBDEV_NAME_SIZE];
/* can be used to group similar subdevs, value is driver-specific */
u32 grp_id;
/* pointer to private data */
void *priv;
};
static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
{
sd->priv = p;
}
static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
{
return sd->priv;
}
/* Convert an ioctl-type command to the proper v4l2_subdev_ops function call.
This is used by subdev modules that can be called by both old-style ioctl
commands and through the v4l2_subdev_ops.
The ioctl API of the subdev driver can call this function to call the
right ops based on the ioctl cmd and arg.
Once all subdev drivers have been converted and all drivers no longer
use the ioctl interface, then this function can be removed.
*/
int v4l2_subdev_command(struct v4l2_subdev *sd, unsigned cmd, void *arg);
static inline void v4l2_subdev_init(struct v4l2_subdev *sd,
const struct v4l2_subdev_ops *ops)
{
INIT_LIST_HEAD(&sd->list);
/* ops->core MUST be set */
BUG_ON(!ops || !ops->core);
sd->ops = ops;
sd->dev = NULL;
sd->name[0] = '\0';
sd->grp_id = 0;
sd->priv = NULL;
}
/* Call an ops of a v4l2_subdev, doing the right checks against
NULL pointers.
Example: err = v4l2_subdev_call(sd, core, g_chip_ident, &chip);
*/
#define v4l2_subdev_call(sd, o, f, args...) \
(!(sd) ? -ENODEV : (((sd) && (sd)->ops->o && (sd)->ops->o->f) ? \
(sd)->ops->o->f((sd) , ##args) : -ENOIOCTLCMD))
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment