1 /* arch/arm/mach-msm/rpc_servers.c
3 * Copyright (C) 2007 Google, Inc.
4 * Author: Iliyan Malchev <ibm@android.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/cdev.h>
22 #include <linux/init.h>
23 #include <linux/device.h>
24 #include <linux/types.h>
26 #include <linux/kthread.h>
27 #include <linux/delay.h>
28 #include <linux/platform_device.h>
29 #include <linux/wakelock.h>
30 #include <linux/slab.h>
32 #include <linux/msm_rpcrouter.h>
33 #include <linux/uaccess.h>
35 #include <mach/msm_rpcrouter.h>
36 #include "smd_rpcrouter.h"
38 static struct msm_rpc_endpoint *endpoint;
40 #define FLAG_REGISTERED 0x0001
42 static LIST_HEAD(rpc_server_list);
43 static DEFINE_MUTEX(rpc_server_list_lock);
44 static int rpc_servers_active;
45 static struct wake_lock rpc_servers_wake_lock;
47 static void rpc_server_register(struct msm_rpc_server *server)
50 rc = msm_rpc_register_server(endpoint, server->prog, server->vers);
52 printk(KERN_ERR "[rpcserver] error registering %p @ %08x:%d\n",
53 server, server->prog, server->vers);
56 static struct msm_rpc_server *rpc_server_find(uint32_t prog, uint32_t vers)
58 struct msm_rpc_server *server;
60 mutex_lock(&rpc_server_list_lock);
61 list_for_each_entry(server, &rpc_server_list, list) {
62 if ((server->prog == prog) &&
63 #if CONFIG_MSM_AMSS_VERSION >= 6350
64 msm_rpc_is_compatible_version(server->vers, vers)) {
66 server->vers == vers) {
68 mutex_unlock(&rpc_server_list_lock);
72 mutex_unlock(&rpc_server_list_lock);
76 static void rpc_server_register_all(void)
78 struct msm_rpc_server *server;
80 mutex_lock(&rpc_server_list_lock);
81 list_for_each_entry(server, &rpc_server_list, list) {
82 if (!(server->flags & FLAG_REGISTERED)) {
83 rpc_server_register(server);
84 server->flags |= FLAG_REGISTERED;
87 mutex_unlock(&rpc_server_list_lock);
90 int msm_rpc_create_server(struct msm_rpc_server *server)
92 /* make sure we're in a sane state first */
94 INIT_LIST_HEAD(&server->list);
96 mutex_lock(&rpc_server_list_lock);
97 list_add(&server->list, &rpc_server_list);
98 if (rpc_servers_active) {
99 rpc_server_register(server);
100 server->flags |= FLAG_REGISTERED;
102 mutex_unlock(&rpc_server_list_lock);
107 static int rpc_send_accepted_void_reply(struct msm_rpc_endpoint *client,
108 uint32_t xid, uint32_t accept_status)
111 uint8_t reply_buf[sizeof(struct rpc_reply_hdr)];
112 struct rpc_reply_hdr *reply = (struct rpc_reply_hdr *)reply_buf;
114 reply->xid = cpu_to_be32(xid);
115 reply->type = cpu_to_be32(1); /* reply */
116 reply->reply_stat = cpu_to_be32(RPCMSG_REPLYSTAT_ACCEPTED);
118 reply->data.acc_hdr.accept_stat = cpu_to_be32(accept_status);
119 reply->data.acc_hdr.verf_flavor = 0;
120 reply->data.acc_hdr.verf_length = 0;
122 rc = msm_rpc_write(client, reply_buf, sizeof(reply_buf));
125 "%s: could not write response: %d\n",
131 static int rpc_servers_thread(void *data)
134 struct rpc_request_hdr *req;
135 struct msm_rpc_server *server;
139 wake_unlock(&rpc_servers_wake_lock);
140 rc = wait_event_interruptible(endpoint->wait_q,
141 !list_empty(&endpoint->read_q));
142 wake_lock(&rpc_servers_wake_lock);
143 rc = msm_rpc_read(endpoint, &buffer, -1, -1);
145 printk(KERN_ERR "%s: could not read: %d\n",
149 req = (struct rpc_request_hdr *)buffer;
151 req->type = be32_to_cpu(req->type);
152 req->xid = be32_to_cpu(req->xid);
153 req->rpc_vers = be32_to_cpu(req->rpc_vers);
154 req->prog = be32_to_cpu(req->prog);
155 req->vers = be32_to_cpu(req->vers);
156 req->procedure = be32_to_cpu(req->procedure);
158 server = rpc_server_find(req->prog, req->vers);
160 if (req->rpc_vers != 2)
165 rpc_send_accepted_void_reply(
167 RPC_ACCEPTSTAT_PROG_UNAVAIL);
171 rc = server->rpc_call(server, req, rc);
175 rpc_send_accepted_void_reply(
177 RPC_ACCEPTSTAT_SUCCESS);
180 rpc_send_accepted_void_reply(
182 RPC_ACCEPTSTAT_PROG_UNAVAIL);
192 static int rpcservers_probe(struct platform_device *pdev)
194 struct task_struct *server_thread;
196 endpoint = msm_rpc_open();
197 if (IS_ERR(endpoint))
198 return PTR_ERR(endpoint);
200 /* we're online -- register any servers installed beforehand */
201 rpc_servers_active = 1;
202 rpc_server_register_all();
204 /* start the kernel thread */
205 server_thread = kthread_run(rpc_servers_thread, NULL, "krpcserversd");
206 if (IS_ERR(server_thread))
207 return PTR_ERR(server_thread);
212 static struct platform_driver rpcservers_driver = {
213 .probe = rpcservers_probe,
215 .name = "oncrpc_router",
216 .owner = THIS_MODULE,
220 static int __init rpc_servers_init(void)
222 wake_lock_init(&rpc_servers_wake_lock, WAKE_LOCK_SUSPEND, "rpc_server");
223 return platform_driver_register(&rpcservers_driver);
226 module_init(rpc_servers_init);
228 MODULE_DESCRIPTION("MSM RPC Servers");
229 MODULE_AUTHOR("Iliyan Malchev <ibm@android.com>");
230 MODULE_LICENSE("GPL");