aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/media/video/Makefile3
-rw-r--r--drivers/media/video/v4l2-int-device.c154
-rw-r--r--include/media/v4l2-int-device.h210
3 files changed, 366 insertions, 1 deletions
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index 10b4d4469016..d35306daac2f 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -11,7 +11,8 @@ tuner-$(CONFIG_TUNER_TEA5761) += tea5761.o
11 11
12msp3400-objs := msp3400-driver.o msp3400-kthreads.o 12msp3400-objs := msp3400-driver.o msp3400-kthreads.o
13 13
14obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o 14obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o \
15 v4l2-int-device.o
15 16
16ifeq ($(CONFIG_VIDEO_V4L1_COMPAT),y) 17ifeq ($(CONFIG_VIDEO_V4L1_COMPAT),y)
17 obj-$(CONFIG_VIDEO_DEV) += v4l1-compat.o 18 obj-$(CONFIG_VIDEO_DEV) += v4l1-compat.o
diff --git a/drivers/media/video/v4l2-int-device.c b/drivers/media/video/v4l2-int-device.c
new file mode 100644
index 000000000000..7885d9b38c92
--- /dev/null
+++ b/drivers/media/video/v4l2-int-device.c
@@ -0,0 +1,154 @@
1/*
2 * drivers/media/video/v4l2-int-device.c
3 *
4 * V4L2 internal ioctl interface.
5 *
6 * Copyright (C) 2007 Nokia Corporation.
7 *
8 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25#include <linux/kernel.h>
26#include <linux/list.h>
27#include <linux/sort.h>
28#include <linux/string.h>
29
30#include <media/v4l2-int-device.h>
31
32static DEFINE_MUTEX(mutex);
33static LIST_HEAD(int_list);
34
35static void v4l2_int_device_try_attach_all(void)
36{
37 struct list_head *head_master;
38
39 list_for_each(head_master, &int_list) {
40 struct list_head *head_slave;
41 struct v4l2_int_device *m =
42 list_entry(head_master, struct v4l2_int_device, head);
43
44 if (m->type != v4l2_int_type_master)
45 continue;
46
47 list_for_each(head_slave, &int_list) {
48 struct v4l2_int_device *s =
49 list_entry(head_slave,
50 struct v4l2_int_device, head);
51
52 if (s->type != v4l2_int_type_slave)
53 continue;
54
55 /* Slave is connected? */
56 if (s->u.slave->master)
57 continue;
58
59 /* Slave wants to attach to master? */
60 if (s->u.slave->attach_to[0] != 0
61 && strncmp(m->name, s->u.slave->attach_to,
62 V4L2NAMESIZE))
63 continue;
64
65 if (!try_module_get(m->module))
66 continue;
67
68 if (m->u.master->attach(m, s)) {
69 module_put(m->module);
70 continue;
71 }
72
73 s->u.slave->master = m;
74 }
75 }
76}
77
78static int ioctl_sort_cmp(const void *a, const void *b)
79{
80 const struct v4l2_int_ioctl_desc *d1 = a, *d2 = b;
81
82 if (d1->num > d2->num)
83 return 1;
84
85 if (d1->num < d2->num)
86 return -1;
87
88 return 0;
89}
90
91int v4l2_int_device_register(struct v4l2_int_device *d)
92{
93 if (d->type == v4l2_int_type_slave)
94 sort(d->u.slave->ioctls, d->u.slave->num_ioctls,
95 sizeof(struct v4l2_int_ioctl_desc),
96 &ioctl_sort_cmp, NULL);
97 mutex_lock(&mutex);
98 list_add(&d->head, &int_list);
99 v4l2_int_device_try_attach_all();
100 mutex_unlock(&mutex);
101
102 return 0;
103}
104
105void v4l2_int_device_unregister(struct v4l2_int_device *d)
106{
107 mutex_lock(&mutex);
108 list_del(&d->head);
109 if (d->type == v4l2_int_type_slave
110 && d->u.slave->master != NULL) {
111 d->u.slave->master->u.master->detach(d);
112 module_put(d->u.slave->master->module);
113 d->u.slave->master = NULL;
114 }
115 mutex_unlock(&mutex);
116}
117
118static int no_such_ioctl(struct v4l2_int_device *d)
119{
120 return -EINVAL;
121}
122
123/* Adapted from search_extable in extable.c. */
124static v4l2_int_ioctl_func *find_ioctl(struct v4l2_int_slave *slave, int cmd)
125{
126 const struct v4l2_int_ioctl_desc *first = slave->ioctls;
127 const struct v4l2_int_ioctl_desc *last =
128 first + slave->num_ioctls - 1;
129
130 while (first <= last) {
131 const struct v4l2_int_ioctl_desc *mid;
132
133 mid = (last - first) / 2 + first;
134
135 if (mid->num < cmd)
136 first = mid + 1;
137 else if (mid->num > cmd)
138 last = mid - 1;
139 else
140 return mid->func;
141 }
142
143 return &no_such_ioctl;
144}
145
146int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd)
147{
148 return ((v4l2_int_ioctl_func_0 *)find_ioctl(d->u.slave, cmd))(d);
149}
150
151int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg)
152{
153 return ((v4l2_int_ioctl_func_1 *)find_ioctl(d->u.slave, cmd))(d, arg);
154}
diff --git a/include/media/v4l2-int-device.h b/include/media/v4l2-int-device.h
new file mode 100644
index 000000000000..2b6fc1122f6c
--- /dev/null
+++ b/include/media/v4l2-int-device.h
@@ -0,0 +1,210 @@
1/*
2 * include/media/v4l2-int-device.h
3 *
4 * V4L2 internal ioctl interface.
5 *
6 * Copyright (C) 2007 Nokia Corporation.
7 *
8 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25#ifndef V4L2_INT_DEVICE_H
26#define V4L2_INT_DEVICE_H
27
28#include <linux/module.h>
29#include <media/v4l2-common.h>
30
31#define V4L2NAMESIZE 32
32
33enum v4l2_int_type {
34 v4l2_int_type_master = 1,
35 v4l2_int_type_slave
36};
37
38enum v4l2_int_ioctl_num {
39 /*
40 *
41 * "Proper" V4L ioctls, as in struct video_device.
42 *
43 */
44 vidioc_int_enum_fmt_cap_num = 1,
45 vidioc_int_g_fmt_cap_num,
46 vidioc_int_s_fmt_cap_num,
47 vidioc_int_try_fmt_cap_num,
48 vidioc_int_queryctrl_num,
49 vidioc_int_g_ctrl_num,
50 vidioc_int_s_ctrl_num,
51 vidioc_int_g_parm_num,
52 vidioc_int_s_parm_num,
53
54 /*
55 *
56 * Strictly internal ioctls.
57 *
58 */
59 /* Initialise the device when slave attaches to the master. */
60 vidioc_int_dev_init_num = 1000,
61 /* Delinitialise the device at slave detach. */
62 vidioc_int_dev_exit_num,
63 /* Set device power state: 0 is off, non-zero is on. */
64 vidioc_int_s_power_num,
65 /* Get parallel interface clock speed for current settings. */
66 vidioc_int_g_ext_clk_num,
67 /*
68 * Tell what the parallel interface clock speed actually is.
69 */
70 vidioc_int_s_ext_clk_num,
71 /* Does the slave need to be reset after VIDIOC_DQBUF? */
72 vidioc_int_g_needs_reset_num,
73
74 /*
75 *
76 * VIDIOC_INT_* ioctls.
77 *
78 */
79 /* VIDIOC_INT_RESET */
80 vidioc_int_reset_num,
81 /* VIDIOC_INT_INIT */
82 vidioc_int_init_num,
83 /* VIDIOC_INT_G_CHIP_IDENT */
84 vidioc_int_g_chip_ident_num,
85
86 /*
87 *
88 * Start of private ioctls.
89 *
90 */
91 vidioc_int_priv_start_num = 2000,
92};
93
94struct v4l2_int_device;
95
96struct v4l2_int_master {
97 int (*attach)(struct v4l2_int_device *master,
98 struct v4l2_int_device *slave);
99 void (*detach)(struct v4l2_int_device *master);
100};
101
102typedef int (v4l2_int_ioctl_func)(struct v4l2_int_device *);
103typedef int (v4l2_int_ioctl_func_0)(struct v4l2_int_device *);
104typedef int (v4l2_int_ioctl_func_1)(struct v4l2_int_device *, void *);
105
106struct v4l2_int_ioctl_desc {
107 int num;
108 v4l2_int_ioctl_func *func;
109};
110
111struct v4l2_int_slave {
112 /* Don't touch master. */
113 struct v4l2_int_device *master;
114
115 char attach_to[V4L2NAMESIZE];
116
117 int num_ioctls;
118 struct v4l2_int_ioctl_desc *ioctls;
119};
120
121struct v4l2_int_device {
122 /* Don't touch head. */
123 struct list_head head;
124
125 struct module *module;
126
127 char name[V4L2NAMESIZE];
128
129 enum v4l2_int_type type;
130 union {
131 struct v4l2_int_master *master;
132 struct v4l2_int_slave *slave;
133 } u;
134
135 void *priv;
136};
137
138int v4l2_int_device_register(struct v4l2_int_device *d);
139void v4l2_int_device_unregister(struct v4l2_int_device *d);
140
141int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd);
142int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg);
143
144/*
145 *
146 * IOCTL wrapper functions for better type checking.
147 *
148 */
149
150#define V4L2_INT_WRAPPER_0(name) \
151 static inline int vidioc_int_##name(struct v4l2_int_device *d) \
152 { \
153 return v4l2_int_ioctl_0(d, vidioc_int_##name##_num); \
154 } \
155 \
156 static inline struct v4l2_int_ioctl_desc \
157 vidioc_int_##name##_cb(int (*func) \
158 (struct v4l2_int_device *)) \
159 { \
160 struct v4l2_int_ioctl_desc desc; \
161 \
162 desc.num = vidioc_int_##name##_num; \
163 desc.func = (v4l2_int_ioctl_func *)func; \
164 \
165 return desc; \
166 }
167
168#define V4L2_INT_WRAPPER_1(name, arg_type, asterisk) \
169 static inline int vidioc_int_##name(struct v4l2_int_device *d, \
170 arg_type asterisk arg) \
171 { \
172 return v4l2_int_ioctl_1(d, vidioc_int_##name##_num, \
173 (void *)arg); \
174 } \
175 \
176 static inline struct v4l2_int_ioctl_desc \
177 vidioc_int_##name##_cb(int (*func) \
178 (struct v4l2_int_device *, \
179 arg_type asterisk)) \
180 { \
181 struct v4l2_int_ioctl_desc desc; \
182 \
183 desc.num = vidioc_int_##name##_num; \
184 desc.func = (v4l2_int_ioctl_func *)func; \
185 \
186 return desc; \
187 }
188
189V4L2_INT_WRAPPER_1(enum_fmt_cap, struct v4l2_fmtdesc, *);
190V4L2_INT_WRAPPER_1(g_fmt_cap, struct v4l2_format, *);
191V4L2_INT_WRAPPER_1(s_fmt_cap, struct v4l2_format, *);
192V4L2_INT_WRAPPER_1(try_fmt_cap, struct v4l2_format, *);
193V4L2_INT_WRAPPER_1(queryctrl, struct v4l2_queryctrl, *);
194V4L2_INT_WRAPPER_1(g_ctrl, struct v4l2_control, *);
195V4L2_INT_WRAPPER_1(s_ctrl, struct v4l2_control, *);
196V4L2_INT_WRAPPER_1(g_parm, struct v4l2_streamparm, *);
197V4L2_INT_WRAPPER_1(s_parm, struct v4l2_streamparm, *);
198
199V4L2_INT_WRAPPER_0(dev_init);
200V4L2_INT_WRAPPER_0(dev_exit);
201V4L2_INT_WRAPPER_1(s_power, int, );
202V4L2_INT_WRAPPER_1(s_ext_clk, u32, );
203V4L2_INT_WRAPPER_1(g_ext_clk, u32, *);
204V4L2_INT_WRAPPER_1(g_needs_reset, void, *);
205
206V4L2_INT_WRAPPER_0(reset);
207V4L2_INT_WRAPPER_0(init);
208V4L2_INT_WRAPPER_1(g_chip_ident, int, *);
209
210#endif