2 * Copyright (C) 2010 Google, Inc.
5 * Dima Zavin <dima@android.com>
7 * Based on original NVRM code from NVIDIA, and a partial rewrite by
8 * Gary King <gking@nvidia.com>
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.
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.
21 #include <linux/err.h>
22 #include <linux/file.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>
35 #include "trpc_sema.h"
38 struct trpc_endpoint *rpc_ep;
39 struct file *sema_file;
42 /* ports names reserved for system functions, i.e. communicating with the
44 static const char reserved_ports[][TEGRA_RPC_MAX_NAME_LEN] = {
48 static int num_reserved_ports = ARRAY_SIZE(reserved_ports);
50 static void rpc_notify_recv(struct trpc_endpoint *ep);
52 /* TODO: do we need to do anything when port is closed from the other side? */
53 static struct trpc_ep_ops ep_ops = {
54 .notify_recv = rpc_notify_recv,
57 static struct trpc_node rpc_node = {
59 .type = TRPC_NODE_LOCAL,
62 static void rpc_notify_recv(struct trpc_endpoint *ep)
64 struct rpc_info *info = trpc_priv(ep);
69 trpc_sema_signal(info->sema_file);
72 static int local_rpc_open(struct inode *inode, struct file *file)
74 struct rpc_info *info;
76 info = kzalloc(sizeof(struct rpc_info), GFP_KERNEL);
80 nonseekable_open(inode, file);
81 file->private_data = info;
85 static int local_rpc_release(struct inode *inode, struct file *file)
87 struct rpc_info *info = file->private_data;
90 trpc_close(info->rpc_ep);
92 fput(info->sema_file);
94 file->private_data = NULL;
98 static int __get_port_desc(struct tegra_rpc_port_desc *desc,
99 unsigned int cmd, unsigned long arg)
101 unsigned int size = _IOC_SIZE(cmd);
103 if (size != sizeof(struct tegra_rpc_port_desc))
105 if (copy_from_user(desc, (void __user *)arg, sizeof(*desc)))
108 desc->name[TEGRA_RPC_MAX_NAME_LEN - 1] = '\0';
112 static char uniq_name[] = "aaaaaaaa+";
113 static const int uniq_len = sizeof(uniq_name) - 1;
114 static DEFINE_MUTEX(uniq_lock);
116 static void _gen_port_name(char *new_name)
120 mutex_lock(&uniq_lock);
121 for (i = 0; i < uniq_len - 1; i++) {
123 if (uniq_name[i] != 'z')
127 strlcpy(new_name, uniq_name, TEGRA_RPC_MAX_NAME_LEN);
128 mutex_unlock(&uniq_lock);
131 static int _validate_port_name(const char *name)
135 for (i = 0; i < num_reserved_ports; i++)
136 if (!strncmp(name, reserved_ports[i], TEGRA_RPC_MAX_NAME_LEN))
141 static long local_rpc_ioctl(struct file *file, unsigned int cmd,
144 struct rpc_info *info = file->private_data;
145 struct tegra_rpc_port_desc desc;
146 struct trpc_endpoint *ep;
149 if (_IOC_TYPE(cmd) != TEGRA_RPC_IOCTL_MAGIC ||
150 _IOC_NR(cmd) < TEGRA_RPC_IOCTL_MIN_NR ||
151 _IOC_NR(cmd) > TEGRA_RPC_IOCTL_MAX_NR) {
157 case TEGRA_RPC_IOCTL_PORT_CREATE:
162 ret = __get_port_desc(&desc, cmd, arg);
166 ret = _validate_port_name(desc.name);
170 _gen_port_name(desc.name);
172 if (desc.notify_fd != -1) {
173 /* grab a reference to the trpc_sema fd */
174 info->sema_file = trpc_sema_get_from_fd(desc.notify_fd);
175 if (IS_ERR(info->sema_file)) {
176 ret = PTR_ERR(info->sema_file);
177 info->sema_file = NULL;
181 ep = trpc_create(&rpc_node, desc.name, &ep_ops, info);
185 fput(info->sema_file);
186 info->sema_file = NULL;
191 case TEGRA_RPC_IOCTL_PORT_GET_NAME:
196 if (copy_to_user((void __user *)arg,
197 trpc_name(info->rpc_ep),
198 TEGRA_RPC_MAX_NAME_LEN)) {
203 case TEGRA_RPC_IOCTL_PORT_CONNECT:
208 ret = trpc_connect(info->rpc_ep, (long)arg);
210 pr_err("%s: can't connect to '%s' (%d)\n", __func__,
211 trpc_name(info->rpc_ep), ret);
215 case TEGRA_RPC_IOCTL_PORT_LISTEN:
220 ret = trpc_wait_peer(info->rpc_ep, (long)arg);
222 pr_err("%s: error waiting for peer for '%s' (%d)\n",
223 __func__, trpc_name(info->rpc_ep), ret);
228 pr_err("%s: unknown cmd %d\n", __func__, _IOC_NR(cmd));
236 if (ret && ret != -ERESTARTSYS)
237 pr_err("tegra_rpc: pid=%d ioctl=%x/%lx (%x) ret=%d\n",
238 current->pid, cmd, arg, _IOC_NR(cmd), ret);
242 static ssize_t local_rpc_write(struct file *file, const char __user *buf,
243 size_t count, loff_t *ppos)
245 struct rpc_info *info = file->private_data;
246 u8 data[TEGRA_RPC_MAX_MSG_LEN];
251 else if (count > TEGRA_RPC_MAX_MSG_LEN)
254 if (copy_from_user(data, buf, count))
257 ret = trpc_send_msg(&rpc_node, info->rpc_ep, data, count,
264 static ssize_t local_rpc_read(struct file *file, char __user *buf, size_t max,
267 struct rpc_info *info = file->private_data;
269 u8 data[TEGRA_RPC_MAX_MSG_LEN];
271 if (max > TEGRA_RPC_MAX_MSG_LEN)
274 ret = trpc_recv_msg(&rpc_node, info->rpc_ep, data,
275 TEGRA_RPC_MAX_MSG_LEN, 0);
282 else if (copy_to_user(buf, data, ret))
288 static const struct file_operations local_rpc_misc_fops = {
289 .owner = THIS_MODULE,
290 .open = local_rpc_open,
291 .release = local_rpc_release,
292 .unlocked_ioctl = local_rpc_ioctl,
293 .write = local_rpc_write,
294 .read = local_rpc_read,
297 static struct miscdevice local_rpc_misc_device = {
298 .minor = MISC_DYNAMIC_MINOR,
300 .fops = &local_rpc_misc_fops,
303 int __init rpc_local_init(void)
307 ret = trpc_sema_init();
309 pr_err("%s: error in trpc_sema_init\n", __func__);
313 ret = misc_register(&local_rpc_misc_device);
315 pr_err("%s: can't register misc device\n", __func__);
319 ret = trpc_node_register(&rpc_node);
321 pr_err("%s: can't register rpc node\n", __func__);
327 misc_deregister(&local_rpc_misc_device);
333 module_init(rpc_local_init);