LWN.net Logo

V4L: Add internal ioctl interface.

From:  Sakari Ailus <sakari.ailus@nokia.com>
To:  video4linux-list@redhat.com
Subject:  [PATCH] V4L: Add internal ioctl interface.
Date:  Wed, 27 Jun 2007 14:57:44 +0300
Archive-link:  Article, Thread

Signed-off-by: Sakari Ailus <sakari.ailus@nokia.com>
---
 drivers/media/video/v4l2-int-device.c |  137 ++++++++++++++++++++++++++++++
 include/media/v4l2-int-device.h       |  150 +++++++++++++++++++++++++++++++++
 2 files changed, 287 insertions(+), 0 deletions(-)
 create mode 100644 drivers/media/video/v4l2-int-device.c
 create mode 100644 include/media/v4l2-int-device.h

diff --git a/drivers/media/video/v4l2-int-device.c b/drivers/media/video/v4l2-int-device.c
new file mode 100644
index 0000000..5e17eea
--- /dev/null
+++ b/drivers/media/video/v4l2-int-device.c
@@ -0,0 +1,137 @@
+/*
+ * v4l2-int-device.c
+ *
+ * V4L2 internal ioctl interface.
+ *
+ * Copyright (C) 2007 Nokia Corporation.
+ *
+ * Contact: Sakari Ailus <sakari.ailus@nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/sort.h>
+#include <linux/string.h>
+#include <media/v4l2-int-device.h>
+
+static DEFINE_MUTEX(mutex);
+static LIST_HEAD(int_list);
+
+static void v4l2_int_device_try_attach_all(void)
+{
+	struct list_head *head_master;
+
+	list_for_each(head_master, &int_list) {
+		struct list_head *head_slave;
+		struct v4l2_int_device *m =
+			list_entry(head_master, struct v4l2_int_device, head);
+
+		if (m->type != v4l2_int_type_master)
+			continue;
+
+		list_for_each(head_slave, &int_list) {
+			struct v4l2_int_device *s =
+				list_entry(head_slave,
+					   struct v4l2_int_device, head);
+
+			if (s->type != v4l2_int_type_slave)
+				continue;
+
+			/* Slave is connected? */
+			if (s->u.slave->master)
+				continue;
+
+			/* Slave wants to attach to master? */
+			if (s->u.slave->attach_to[0] != 0
+			    && strncmp(m->name, s->u.slave->attach_to,
+				       V4L2NAMESIZE))
+				continue;
+
+			if (!try_module_get(m->module))
+				continue;
+
+			if (m->u.master->attach(m, s)) {
+				module_put(m->module);
+				continue;
+			}
+
+			s->u.slave->master = m;
+		}
+	}
+}
+
+static int ioctl_sort_cmp(const void *a, const void *b)
+{
+	const struct v4l2_int_ioctl_desc *d1 = a, *d2 = b;
+
+	if (d1->num > d2->num)
+		return 1;
+
+	if (d1->num < d2->num)
+		return -1;
+
+	return 0;
+}
+
+int v4l2_int_device_register(struct v4l2_int_device *d)
+{
+	if (d->type == v4l2_int_type_slave)
+		sort(d->u.slave->ioctls, d->u.slave->num_ioctls,
+		     sizeof(struct v4l2_int_ioctl_desc),
+		     &ioctl_sort_cmp, NULL);
+	mutex_lock(&mutex);
+	list_add(&d->head, &int_list);
+	v4l2_int_device_try_attach_all();
+	mutex_unlock(&mutex);
+
+	return 0;
+}
+
+void v4l2_int_device_unregister(struct v4l2_int_device *d)
+{
+	mutex_lock(&mutex);
+	list_del(&d->head);
+	if (d->type == v4l2_int_type_slave
+	    && d->u.slave->master != NULL) {
+		d->u.slave->master->u.master->detach(d);
+		module_put(d->u.slave->master->module);
+		d->u.slave->master = NULL;
+	}
+	mutex_unlock(&mutex);
+}
+
+/* Adapted from search_extable in extable.c. */
+int v4l2_int_ioctl(struct v4l2_int_device *d, int cmd, void *arg)
+{
+	const struct v4l2_int_ioctl_desc *first = d->u.slave->ioctls;
+	const struct v4l2_int_ioctl_desc *last =
+		first + d->u.slave->num_ioctls - 1;
+
+        while (first <= last) {
+                const struct v4l2_int_ioctl_desc *mid;
+
+                mid = (last - first) / 2 + first;
+
+                if (mid->num < cmd)
+                        first = mid + 1;
+                else if (mid->num > cmd)
+                        last = mid - 1;
+                else
+                        return mid->func(d, arg);
+        }
+        return -EINVAL;
+}
diff --git a/include/media/v4l2-int-device.h b/include/media/v4l2-int-device.h
new file mode 100644
index 0000000..5bec692
--- /dev/null
+++ b/include/media/v4l2-int-device.h
@@ -0,0 +1,150 @@
+/*
+ * v4l2-int-device.h
+ *
+ * V4L2 internal ioctl interface.
+ *
+ * Copyright (C) 2007 Nokia Corporation.
+ *
+ * Contact: Sakari Ailus <sakari.ailus@nokia.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef V4L2_DEVICE_REG_H
+#define V4L2_DEVICE_REG_H
+
+#include <linux/module.h>
+#include <media/v4l2-common.h>
+
+#define V4L2NAMESIZE 32
+
+enum v4l2_int_type {
+	v4l2_int_type_master = 1,
+	v4l2_int_type_slave
+};
+
+enum v4l2_int_ioctl_num {
+	vidioc_int_dev_init_num = 1,
+	vidioc_int_dev_exit_num,
+	vidioc_int_s_power_num,
+	vidioc_int_g_ext_clk_num,
+	vidioc_int_s_ext_clk_num,
+	vidioc_int_g_needs_reset_num,
+	vidioc_int_g_chip_ident_num,
+	vidioc_int_reset_num,
+	vidioc_int_init_num,
+	vidioc_int_enum_fmt_cap_num,
+	vidioc_int_s_fmt_cap_num,
+	vidioc_int_g_fmt_cap_num,
+	vidioc_int_try_fmt_cap_num,
+	vidioc_int_queryctrl_num,
+	vidioc_int_g_ctrl_num,
+	vidioc_int_s_ctrl_num,
+	vidioc_int_g_parm_num,
+	vidioc_int_s_parm_num,
+};
+
+struct v4l2_int_device;
+
+struct v4l2_int_master {
+	int (*attach)(struct v4l2_int_device *master,
+		      struct v4l2_int_device *slave);
+	void (*detach)(struct v4l2_int_device *master);
+};
+
+typedef int (v4l2_int_ioctl_func)(struct v4l2_int_device *, void *);
+
+struct v4l2_int_ioctl_desc {
+	int num;
+	v4l2_int_ioctl_func *func;
+};
+
+struct v4l2_int_slave {
+	/* Don't touch master. */
+	struct v4l2_int_device *master;
+
+	char attach_to[V4L2NAMESIZE];
+
+	int num_ioctls;
+	struct v4l2_int_ioctl_desc *ioctls;
+};
+
+struct v4l2_int_device {
+	/* Don't touch head. */
+	struct list_head head;
+
+	struct module *module;
+
+	char name[V4L2NAMESIZE];
+
+	enum v4l2_int_type type;
+	union {
+		struct v4l2_int_master *master;
+		struct v4l2_int_slave *slave;
+	} u;
+
+	void *priv;
+};
+
+int v4l2_int_device_register(struct v4l2_int_device *d);
+void v4l2_int_device_unregister(struct v4l2_int_device *d);
+
+int v4l2_int_ioctl(struct v4l2_int_device *d, int cmd, void *arg);
+
+/*
+ *
+ * IOCTL wrapper functions for better type checking.
+ *
+ */
+
+#define DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(name, arg_type)		\
+	static inline int vidioc_int_##name(struct v4l2_int_device *d,	\
+					    arg_type *arg)		\
+	{								\
+		return v4l2_int_ioctl(d, vidioc_int_##name##_num, arg); \
+	}
+
+#define DECLARE_V4L2_INT_IOCTL_WRAPPER_VALUE(name, arg_type)		\
+	static inline int vidioc_int_##name(struct v4l2_int_device *d,	\
+					    arg_type arg)		\
+	{								\
+		return v4l2_int_ioctl(d, vidioc_int_##name##_num, &arg); \
+	}
+
+#define DECLARE_V4L2_INT_IOCTL_WRAPPER_VOID(name)			\
+	static inline int vidioc_int_##name(struct v4l2_int_device *d)	\
+	{								\
+		return v4l2_int_ioctl(d, vidioc_int_##name##_num, NULL); \
+	}
+
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VOID(dev_init);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VOID(dev_exit);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VALUE(s_power, int);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VALUE(s_ext_clk, u32);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(g_ext_clk, u32);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(g_needs_reset, void);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VOID(reset);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_VOID(init);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(enum_fmt_cap, struct v4l2_fmtdesc);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(try_fmt_cap, struct v4l2_format);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(s_fmt_cap, struct v4l2_format);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(g_fmt_cap, struct v4l2_format);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(g_parm, struct v4l2_streamparm);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(s_parm, struct v4l2_streamparm);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(queryctrl, struct v4l2_queryctrl);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(g_ctrl, struct v4l2_control);
+DECLARE_V4L2_INT_IOCTL_WRAPPER_REF(s_ctrl, struct v4l2_control);
+
+#endif
-- 
1.5.0.6

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubs...
https://www.redhat.com/mailman/listinfo/video4linux-list


Copyright © 2007, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds