blob: f497c9458344e36eeaf32026a0e440414b5b5496 [file] [log] [blame]
Sakari Ailus9b5d0f12007-07-18 17:59:15 -03001/*
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}
Adrian Bunk261efd12007-07-30 11:43:55 -0300104EXPORT_SYMBOL_GPL(v4l2_int_device_register);
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300105
106void v4l2_int_device_unregister(struct v4l2_int_device *d)
107{
108 mutex_lock(&mutex);
109 list_del(&d->head);
110 if (d->type == v4l2_int_type_slave
111 && d->u.slave->master != NULL) {
112 d->u.slave->master->u.master->detach(d);
113 module_put(d->u.slave->master->module);
114 d->u.slave->master = NULL;
115 }
116 mutex_unlock(&mutex);
117}
Adrian Bunk261efd12007-07-30 11:43:55 -0300118EXPORT_SYMBOL_GPL(v4l2_int_device_unregister);
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300119
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300120/* Adapted from search_extable in extable.c. */
Sakari Ailus63116fe2007-07-20 13:12:51 -0300121static v4l2_int_ioctl_func *find_ioctl(struct v4l2_int_slave *slave, int cmd,
122 v4l2_int_ioctl_func *no_such_ioctl)
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300123{
124 const struct v4l2_int_ioctl_desc *first = slave->ioctls;
125 const struct v4l2_int_ioctl_desc *last =
126 first + slave->num_ioctls - 1;
127
128 while (first <= last) {
129 const struct v4l2_int_ioctl_desc *mid;
130
131 mid = (last - first) / 2 + first;
132
133 if (mid->num < cmd)
134 first = mid + 1;
135 else if (mid->num > cmd)
136 last = mid - 1;
137 else
138 return mid->func;
139 }
140
Sakari Ailus63116fe2007-07-20 13:12:51 -0300141 return no_such_ioctl;
142}
143
144static int no_such_ioctl_0(struct v4l2_int_device *d)
145{
Sakari Ailus61c310d2007-08-30 09:20:40 -0300146 return -ENOIOCTLCMD;
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300147}
148
149int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd)
150{
Sakari Ailus63116fe2007-07-20 13:12:51 -0300151 return ((v4l2_int_ioctl_func_0 *)
152 find_ioctl(d->u.slave, cmd,
Sakari Ailus098c6452007-08-30 09:20:38 -0300153 (v4l2_int_ioctl_func *)no_such_ioctl_0))(d);
Sakari Ailus63116fe2007-07-20 13:12:51 -0300154}
155
156static int no_such_ioctl_1(struct v4l2_int_device *d, void *arg)
157{
Sakari Ailus61c310d2007-08-30 09:20:40 -0300158 return -ENOIOCTLCMD;
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300159}
160
161int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg)
162{
Sakari Ailus63116fe2007-07-20 13:12:51 -0300163 return ((v4l2_int_ioctl_func_1 *)
164 find_ioctl(d->u.slave, cmd,
Sakari Ailus098c6452007-08-30 09:20:38 -0300165 (v4l2_int_ioctl_func *)no_such_ioctl_1))(d, arg);
Sakari Ailus9b5d0f12007-07-18 17:59:15 -0300166}