aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/tegra/avp/trpc_local.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/tegra/avp/trpc_local.c')
-rw-r--r--drivers/media/video/tegra/avp/trpc_local.c419
1 files changed, 419 insertions, 0 deletions
diff --git a/drivers/media/video/tegra/avp/trpc_local.c b/drivers/media/video/tegra/avp/trpc_local.c
new file mode 100644
index 00000000000..77692e09438
--- /dev/null
+++ b/drivers/media/video/tegra/avp/trpc_local.c
@@ -0,0 +1,419 @@
1/*
2 * Copyright (C) 2010 Google, Inc.
3 *
4 * Author:
5 * Dima Zavin <dima@android.com>
6 *
7 * Based on original NVRM code from NVIDIA, and a partial rewrite by
8 * Gary King <gking@nvidia.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/err.h>
22#include <linux/file.h>
23#include <linux/fs.h>
24#include <linux/list.h>
25#include <linux/miscdevice.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/tegra_rpc.h>
30#include <linux/types.h>
31#include <linux/uaccess.h>
32#include <linux/wait.h>
33
34#include "trpc.h"
35#include "trpc_sema.h"
36#include "nvavp.h"
37
38struct tegra_rpc_info {
39 struct trpc_endpoint *rpc_ep;
40 struct tegra_sema_info *sema;
41};
42
43/* ports names reserved for system functions, i.e. communicating with the
44 * AVP */
45static const char reserved_ports[][TEGRA_RPC_MAX_NAME_LEN] = {
46 "RPC_AVP_PORT",
47 "RPC_CPU_PORT",
48};
49static int num_reserved_ports = ARRAY_SIZE(reserved_ports);
50
51static void rpc_notify_recv(struct trpc_endpoint *ep);
52
53/* TODO: do we need to do anything when port is closed from the other side? */
54static struct trpc_ep_ops ep_ops = {
55 .notify_recv = rpc_notify_recv,
56};
57
58static struct trpc_node rpc_node = {
59 .name = "local",
60 .type = TRPC_NODE_LOCAL,
61};
62
63static void rpc_notify_recv(struct trpc_endpoint *ep)
64{
65 struct tegra_rpc_info *info = trpc_priv(ep);
66
67 if (WARN_ON(!info))
68 return;
69 if (info->sema)
70 tegra_sema_signal(info->sema);
71}
72
73int tegra_rpc_open(struct tegra_rpc_info **info)
74{
75 struct tegra_rpc_info *new_info;
76
77 new_info = kzalloc(sizeof(struct tegra_rpc_info), GFP_KERNEL);
78 if (!new_info)
79 return -ENOMEM;
80
81 *info = new_info;
82 return 0;
83}
84
85static int local_rpc_open(struct inode *inode, struct file *file)
86{
87 struct tegra_rpc_info *info;
88 int ret = 0;
89
90 ret = tegra_rpc_open(&info);
91 if (ret < 0)
92 return -ENOMEM;
93
94 nonseekable_open(inode, file);
95 file->private_data = info;
96 return 0;
97}
98
99int tegra_rpc_release(struct tegra_rpc_info *info)
100{
101 if (info->rpc_ep)
102 trpc_close(info->rpc_ep);
103 if (info->sema)
104 trpc_sema_put(info->sema);
105 kfree(info);
106 return 0;
107}
108EXPORT_SYMBOL(tegra_rpc_release);
109
110static int local_rpc_release(struct inode *inode, struct file *file)
111{
112 struct tegra_rpc_info *info = file->private_data;
113
114 tegra_rpc_release(info);
115 file->private_data = NULL;
116 return 0;
117}
118
119static char uniq_name[] = "aaaaaaaa+";
120static const int uniq_len = sizeof(uniq_name) - 1;
121static DEFINE_MUTEX(uniq_lock);
122
123static void _gen_port_name(char *new_name)
124{
125 int i;
126
127 mutex_lock(&uniq_lock);
128 for (i = 0; i < uniq_len - 1; i++) {
129 ++uniq_name[i];
130 if (uniq_name[i] != 'z')
131 break;
132 uniq_name[i] = 'a';
133 }
134 strlcpy(new_name, uniq_name, TEGRA_RPC_MAX_NAME_LEN);
135 mutex_unlock(&uniq_lock);
136}
137
138static int _validate_port_name(const char *name)
139{
140 int i;
141
142 for (i = 0; i < num_reserved_ports; i++)
143 if (!strncmp(name, reserved_ports[i], TEGRA_RPC_MAX_NAME_LEN))
144 return -EINVAL;
145 return 0;
146}
147
148int tegra_rpc_port_create(struct tegra_rpc_info *info, char *name,
149 struct tegra_sema_info *sema)
150{
151 struct trpc_endpoint *ep;
152 int ret = 0;
153
154 if (info->rpc_ep) {
155 ret = -EINVAL;
156 goto err;
157 }
158
159 name[TEGRA_RPC_MAX_NAME_LEN - 1] = '\0';
160 if (name[0]) {
161 ret = _validate_port_name(name);
162 if (ret)
163 goto err;
164 } else {
165 _gen_port_name(name);
166 }
167 ep = trpc_create(&rpc_node, name, &ep_ops, info);
168 if (IS_ERR(ep)) {
169 ret = PTR_ERR(ep);
170 goto err;
171 }
172 info->rpc_ep = ep;
173 info->sema = sema;
174 return 0;
175
176err:
177 return ret;
178}
179
180int tegra_rpc_get_name(struct tegra_rpc_info *info, char* name)
181{
182 if (!info->rpc_ep)
183 return -EINVAL;
184
185 strcpy(name, trpc_name(info->rpc_ep));
186 return 0;
187}
188
189int tegra_rpc_port_connect(struct tegra_rpc_info *info, long timeout)
190{
191 if (!info->rpc_ep)
192 return -EINVAL;
193
194 return trpc_connect(info->rpc_ep, timeout);
195
196}
197
198int tegra_rpc_port_listen(struct tegra_rpc_info *info, long timeout)
199{
200 if (!info->rpc_ep)
201 return -EINVAL;
202
203 return trpc_wait_peer(info->rpc_ep, timeout);
204}
205
206static long local_rpc_ioctl(struct file *file, unsigned int cmd,
207 unsigned long arg)
208{
209 struct tegra_rpc_info *info = file->private_data;
210 struct tegra_rpc_port_desc desc;
211 struct tegra_sema_info *sema = NULL;
212 int ret = 0;
213
214 if (_IOC_TYPE(cmd) != TEGRA_RPC_IOCTL_MAGIC ||
215 _IOC_NR(cmd) < TEGRA_RPC_IOCTL_MIN_NR ||
216 _IOC_NR(cmd) > TEGRA_RPC_IOCTL_MAX_NR) {
217 ret = -ENOTTY;
218 goto err;
219 }
220
221 switch (cmd) {
222 case TEGRA_RPC_IOCTL_PORT_CREATE:
223
224 if (_IOC_SIZE(cmd) != sizeof(struct tegra_rpc_port_desc))
225 return -EINVAL;
226 if (copy_from_user(&desc, (void __user *)arg, sizeof(desc)))
227 return -EFAULT;
228 if (desc.notify_fd != -1) {
229 sema = trpc_sema_get_from_fd(desc.notify_fd);
230 if (IS_ERR(sema)) {
231 ret = PTR_ERR(sema);
232 goto err;
233 }
234 }
235
236 ret = tegra_rpc_port_create(info, desc.name, sema);
237 if (ret < 0)
238 goto err;
239
240 break;
241 case TEGRA_RPC_IOCTL_PORT_GET_NAME:
242 if (!info->rpc_ep) {
243 ret = -EINVAL;
244 goto err;
245 }
246 if (copy_to_user((void __user *)arg,
247 trpc_name(info->rpc_ep),
248 TEGRA_RPC_MAX_NAME_LEN)) {
249 ret = -EFAULT;
250 goto err;
251 }
252 break;
253 case TEGRA_RPC_IOCTL_PORT_CONNECT:
254 if (!info->rpc_ep) {
255 ret = -EINVAL;
256 goto err;
257 }
258 ret = trpc_connect(info->rpc_ep, (long)arg);
259 if (ret) {
260 pr_err("%s: can't connect to '%s' (%d)\n", __func__,
261 trpc_name(info->rpc_ep), ret);
262 goto err;
263 }
264 break;
265 case TEGRA_RPC_IOCTL_PORT_LISTEN:
266 if (!info->rpc_ep) {
267 ret = -EINVAL;
268 goto err;
269 }
270 ret = trpc_wait_peer(info->rpc_ep, (long)arg);
271 if (ret) {
272 pr_err("%s: error waiting for peer for '%s' (%d)\n",
273 __func__, trpc_name(info->rpc_ep), ret);
274 goto err;
275 }
276 break;
277 default:
278 pr_err("%s: unknown cmd %d\n", __func__, _IOC_NR(cmd));
279 ret = -EINVAL;
280 goto err;
281 }
282
283 return 0;
284
285err:
286 if (ret && ret != -ERESTARTSYS)
287 pr_err("tegra_rpc: pid=%d ioctl=%x/%lx (%x) ret=%d\n",
288 current->pid, cmd, arg, _IOC_NR(cmd), ret);
289 return (long)ret;
290}
291
292int tegra_rpc_write(struct tegra_rpc_info *info, u8* buf, size_t size)
293{
294 int ret;
295
296 if (!info->rpc_ep)
297 return -EINVAL;
298
299 if (TEGRA_RPC_MAX_MSG_LEN < size)
300 return -EINVAL;
301
302 ret = trpc_send_msg(&rpc_node, info->rpc_ep, buf, size,
303 GFP_KERNEL);
304 if (ret)
305 return ret;
306 return size;
307}
308
309static ssize_t local_rpc_write(struct file *file, const char __user *buf,
310 size_t count, loff_t *ppos)
311{
312 struct tegra_rpc_info *info = file->private_data;
313 u8 data[TEGRA_RPC_MAX_MSG_LEN];
314 int ret;
315
316 if (!info)
317 return -EINVAL;
318 else if (count > TEGRA_RPC_MAX_MSG_LEN)
319 return -EINVAL;
320
321 if (copy_from_user(data, buf, count))
322 return -EFAULT;
323
324 ret = trpc_send_msg(&rpc_node, info->rpc_ep, data, count,
325 GFP_KERNEL);
326 if (ret)
327 return ret;
328 return count;
329}
330
331int tegra_rpc_read(struct tegra_rpc_info *info, u8 *buf, size_t max)
332{
333 int ret;
334
335 if (max > TEGRA_RPC_MAX_MSG_LEN)
336 return -EINVAL;
337
338 ret = trpc_recv_msg(&rpc_node, info->rpc_ep, buf,
339 TEGRA_RPC_MAX_MSG_LEN, 0);
340 if (ret == 0)
341 return 0;
342 else if (ret < 0)
343 return ret;
344 else if (ret > max)
345 return -ENOSPC;
346
347 return ret;
348}
349
350static ssize_t local_rpc_read(struct file *file, char __user *buf, size_t max,
351 loff_t *ppos)
352{
353 struct tegra_rpc_info *info = file->private_data;
354 int ret;
355 u8 data[TEGRA_RPC_MAX_MSG_LEN];
356
357 if (max > TEGRA_RPC_MAX_MSG_LEN)
358 return -EINVAL;
359
360 ret = trpc_recv_msg(&rpc_node, info->rpc_ep, data,
361 TEGRA_RPC_MAX_MSG_LEN, 0);
362 if (ret == 0)
363 return 0;
364 else if (ret < 0)
365 return ret;
366 else if (ret > max)
367 return -ENOSPC;
368 else if (copy_to_user(buf, data, ret))
369 return -EFAULT;
370
371 return ret;
372}
373
374static const struct file_operations local_rpc_misc_fops = {
375 .owner = THIS_MODULE,
376 .open = local_rpc_open,
377 .release = local_rpc_release,
378 .unlocked_ioctl = local_rpc_ioctl,
379 .write = local_rpc_write,
380 .read = local_rpc_read,
381};
382
383static struct miscdevice local_rpc_misc_device = {
384 .minor = MISC_DYNAMIC_MINOR,
385 .name = "tegra_rpc",
386 .fops = &local_rpc_misc_fops,
387};
388
389int __init rpc_local_init(void)
390{
391 int ret;
392
393 ret = trpc_sema_init();
394 if (ret) {
395 pr_err("%s: error in trpc_sema_init\n", __func__);
396 goto err_sema_init;
397 }
398
399 ret = misc_register(&local_rpc_misc_device);
400 if (ret) {
401 pr_err("%s: can't register misc device\n", __func__);
402 goto err_misc;
403 }
404
405 ret = trpc_node_register(&rpc_node);
406 if (ret) {
407 pr_err("%s: can't register rpc node\n", __func__);
408 goto err_node_reg;
409 }
410 return 0;
411
412err_node_reg:
413 misc_deregister(&local_rpc_misc_device);
414err_misc:
415err_sema_init:
416 return ret;
417}
418
419module_init(rpc_local_init);