2 * drivers/media/video/v4l2-int-device.c
4 * V4L2 internal ioctl interface.
6 * Copyright (C) 2007 Nokia Corporation.
8 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/sort.h>
28 #include <linux/string.h>
30 #include <media/v4l2-int-device.h>
32 static DEFINE_MUTEX(mutex
);
33 static LIST_HEAD(int_list
);
35 static void __v4l2_int_device_try_attach_all(void)
37 struct v4l2_int_device
*m
, *s
;
39 list_for_each_entry(m
, &int_list
, head
) {
40 if (m
->type
!= v4l2_int_type_master
)
43 list_for_each_entry(s
, &int_list
, head
) {
44 if (s
->type
!= v4l2_int_type_slave
)
47 /* Slave is connected? */
48 if (s
->u
.slave
->master
)
51 /* Slave wants to attach to master? */
52 if (s
->u
.slave
->attach_to
[0] != 0
53 && strncmp(m
->name
, s
->u
.slave
->attach_to
,
57 if (!try_module_get(m
->module
))
60 s
->u
.slave
->master
= m
;
61 if (m
->u
.master
->attach(s
)) {
62 s
->u
.slave
->master
= NULL
;
63 module_put(m
->module
);
70 static struct v4l2_int_slave dummy_slave
= {
71 /* Dummy pointer to avoid underflow in find_ioctl. */
72 .ioctls
= (void *)0x80000000,
76 static struct v4l2_int_device dummy
= {
77 .type
= v4l2_int_type_slave
,
79 .slave
= &dummy_slave
,
83 struct v4l2_int_device
*v4l2_int_device_dummy()
87 EXPORT_SYMBOL_GPL(v4l2_int_device_dummy
);
89 void v4l2_int_device_try_attach_all(void)
92 __v4l2_int_device_try_attach_all();
96 EXPORT_SYMBOL_GPL(v4l2_int_device_try_attach_all
);
98 static int ioctl_sort_cmp(const void *a
, const void *b
)
100 const struct v4l2_int_ioctl_desc
*d1
= a
, *d2
= b
;
102 if (d1
->num
> d2
->num
)
105 if (d1
->num
< d2
->num
)
111 int v4l2_int_device_register(struct v4l2_int_device
*d
)
113 if (d
->type
== v4l2_int_type_slave
)
114 sort(d
->u
.slave
->ioctls
, d
->u
.slave
->num_ioctls
,
115 sizeof(struct v4l2_int_ioctl_desc
),
116 &ioctl_sort_cmp
, NULL
);
118 list_add(&d
->head
, &int_list
);
119 __v4l2_int_device_try_attach_all();
120 mutex_unlock(&mutex
);
124 EXPORT_SYMBOL_GPL(v4l2_int_device_register
);
126 void v4l2_int_device_unregister(struct v4l2_int_device
*d
)
130 if (d
->type
== v4l2_int_type_slave
131 && d
->u
.slave
->master
!= NULL
) {
132 d
->u
.slave
->master
->u
.master
->detach(d
);
133 module_put(d
->u
.slave
->master
->module
);
134 d
->u
.slave
->master
= NULL
;
136 mutex_unlock(&mutex
);
138 EXPORT_SYMBOL_GPL(v4l2_int_device_unregister
);
140 /* Adapted from search_extable in extable.c. */
141 static v4l2_int_ioctl_func
*find_ioctl(struct v4l2_int_slave
*slave
, int cmd
,
142 v4l2_int_ioctl_func
*no_such_ioctl
)
144 const struct v4l2_int_ioctl_desc
*first
= slave
->ioctls
;
145 const struct v4l2_int_ioctl_desc
*last
=
146 first
+ slave
->num_ioctls
- 1;
148 while (first
<= last
) {
149 const struct v4l2_int_ioctl_desc
*mid
;
151 mid
= (last
- first
) / 2 + first
;
155 else if (mid
->num
> cmd
)
161 return no_such_ioctl
;
164 static int no_such_ioctl_0(struct v4l2_int_device
*d
)
169 int v4l2_int_ioctl_0(struct v4l2_int_device
*d
, int cmd
)
171 return ((v4l2_int_ioctl_func_0
*)
172 find_ioctl(d
->u
.slave
, cmd
,
173 (v4l2_int_ioctl_func
*)no_such_ioctl_0
))(d
);
175 EXPORT_SYMBOL_GPL(v4l2_int_ioctl_0
);
177 static int no_such_ioctl_1(struct v4l2_int_device
*d
, void *arg
)
182 int v4l2_int_ioctl_1(struct v4l2_int_device
*d
, int cmd
, void *arg
)
184 return ((v4l2_int_ioctl_func_1
*)
185 find_ioctl(d
->u
.slave
, cmd
,
186 (v4l2_int_ioctl_func
*)no_such_ioctl_1
))(d
, arg
);
188 EXPORT_SYMBOL_GPL(v4l2_int_ioctl_1
);
190 MODULE_LICENSE("GPL");