summaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/smd_rpcrouter_clients.c
blob: 5baf79a7ae4bb7ea64895d47ce70376d2d4042e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* Copyright (c) 2009, Code Aurora Forum. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Code Aurora Forum nor
 *       the names of its contributors may be used to endorse or promote
 *       products derived from this software without specific prior written
 *       permission.
 *
 * Alternatively, provided that this notice is retained in full, this software
 * may be relicensed by the recipient under the terms of the GNU General Public
 * License version 2 ("GPL") and only version 2, in which case the provisions of
 * the GPL apply INSTEAD OF those given above.  If the recipient relicenses the
 * software under the GPL, then the identification text in the MODULE_LICENSE
 * macro must be changed to reflect "GPLv2" instead of "Dual BSD/GPL".  Once a
 * recipient changes the license terms to the GPL, subsequent recipients shall
 * not relicense under alternate licensing terms, including the BSD or dual
 * BSD/GPL terms.  In addition, the following license statement immediately
 * below and between the words START and END shall also then apply when this
 * software is relicensed under the GPL:
 *
 * START
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 and only version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * END
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * SMD RPCROUTER CLIENTS module.
 */

#include <linux/kernel.h>
#include <linux/kthread.h>

#include <mach/msm_rpcrouter.h>
#include "smd_rpcrouter.h"

struct msm_rpc_client_cb_item {
	struct list_head list;

	void *buf;
	int size;
};

struct msm_rpc_cb_table_item {
	struct list_head list;

	uint32_t cb_id;
	void *cb_func;
};

static int rpc_clients_cb_thread(void *data)
{
	struct msm_rpc_client_cb_item *cb_item;
	struct msm_rpc_client *client;
	struct rpc_request_hdr *req;

	client = data;
	for (;;) {
		wait_event(client->cb_wait, client->cb_avail);
		if (client->exit_flag)
			break;

		client->cb_avail = 0;
		mutex_lock(&client->cb_item_list_lock);
		while (!list_empty(&client->cb_item_list)) {
			cb_item = list_first_entry(
				&client->cb_item_list,
				struct msm_rpc_client_cb_item,
				list);
			list_del(&cb_item->list);
			mutex_unlock(&client->cb_item_list_lock);
			req = (struct rpc_request_hdr *)cb_item->buf;

			if (be32_to_cpu(req->type) != 0)
				goto bad_rpc;
			if (be32_to_cpu(req->rpc_vers) != 2)
				goto bad_rpc;
			if (be32_to_cpu(req->prog) !=
			    (client->prog | 0x01000000))
				goto bad_rpc;

			client->cb_func(client,
					cb_item->buf, cb_item->size);
 bad_rpc:
			kfree(cb_item->buf);
			kfree(cb_item);
			mutex_lock(&client->cb_item_list_lock);
		}
		mutex_unlock(&client->cb_item_list_lock);
	}
	complete_and_exit(&client->cb_complete, 0);
}

static int rpc_clients_thread(void *data)
{
	void *buffer;
	uint32_t type;
	struct msm_rpc_client *client;
	int rc = 0;
	struct msm_rpc_client_cb_item *cb_item;
	struct rpc_request_hdr *req;

	client = data;
	for (;;) {
		rc = msm_rpc_read(client->ept, &buffer, -1, HZ);
		if (client->exit_flag)
			break;
		if (rc < ((int)(sizeof(uint32_t) * 2)))
			continue;

		type = be32_to_cpu(*((uint32_t *)buffer + 1));
		if (type == 1) {
			client->buf = buffer;
			client->read_avail = 1;
			wake_up(&client->reply_wait);
		} else if (type == 0) {
			cb_item = kmalloc(sizeof(*cb_item), GFP_KERNEL);
			if (!cb_item) {
				pr_err("%s: no memory for cb item\n",
				       __func__);
				continue;
			}

			if (client->cb_thread == NULL) {
				req = (struct rpc_request_hdr *)buffer;

				if ((be32_to_cpu(req->rpc_vers) == 2) &&
				    (be32_to_cpu(req->prog) ==
				     (client->prog | 0x01000000)))
					client->cb_func(client, buffer, rc);
				kfree(buffer);
			} else {
				INIT_LIST_HEAD(&cb_item->list);
				cb_item->buf = buffer;
				cb_item->size = rc;
				mutex_lock(&client->cb_item_list_lock);
				list_add_tail(&cb_item->list,
					      &client->cb_item_list);
				mutex_unlock(&client->cb_item_list_lock);
				client->cb_avail = 1;
				wake_up(&client->cb_wait);
			}
		}
	}
	complete_and_exit(&client->complete, 0);
}

static struct msm_rpc_client *msm_rpc_create_client(void)
{
	struct msm_rpc_client *client;

	client = kmalloc(sizeof(struct msm_rpc_client), GFP_KERNEL);
	if (!client)
		return ERR_PTR(-ENOMEM);

	client->req = kmalloc(MSM_RPC_MSGSIZE_MAX, GFP_KERNEL);
	if (!client->req) {
		kfree(client);
		return ERR_PTR(-ENOMEM);
	}

	client->reply = kmalloc(MSM_RPC_MSGSIZE_MAX, GFP_KERNEL);
	if (!client->reply) {
		kfree(client->req);
		kfree(client);
		return ERR_PTR(-ENOMEM);
	}

	init_waitqueue_head(&client->reply_wait);
	mutex_init(&client->req_lock);
	mutex_init(&client->reply_lock);
	client->buf = NULL;
	client->read_avail = 0;
	client->cb_buf = NULL;
	client->cb_size = 0;
	client->exit_flag = 0;
	init_completion(&client->complete);
	init_completion(&client->cb_complete);
	INIT_LIST_HEAD(&client->cb_item_list);
	mutex_init(&client->cb_item_list_lock);
	client->cb_avail = 0;
	init_waitqueue_head(&client->cb_wait);
	INIT_LIST_HEAD(&client->cb_list);
	mutex_init(&client->cb_list_lock);
	atomic_set(&client->next_cb_id, 1);

	return client;
}

void msm_rpc_remove_all_cb_func(struct msm_rpc_client *client)
{
	struct msm_rpc_cb_table_item *cb_item, *tmp_cb_item;

	mutex_lock(&client->cb_list_lock);
	list_for_each_entry_safe(cb_item, tmp_cb_item,
				 &client->cb_list, list) {
		list_del(&cb_item->list);
		kfree(cb_item);
	}
	mutex_unlock(&client->cb_list_lock);
}

/*
 * Interface to be used to register the client.
 *
 * name: string representing the client
 *
 * prog: program number of the client
 *
 * ver: version number of the client
 *
 * create_cb_thread: if set calls the callback function from a seprate thread
 *                   which helps the client requests to be processed without
 *                   getting loaded by callback handling.
 *
 * cb_func: function to be called if callback request is received.
 *          unmarshaling should be handled by the user in callback function
 *
 * Return Value:
 *        Pointer to initialized client data sturcture
 *        Or, the error code if registration fails.
 *
 */
struct msm_rpc_client *msm_rpc_register_client(
	const char *name,
	uint32_t prog, uint32_t ver,
	uint32_t create_cb_thread,
	int (*cb_func)(struct msm_rpc_client *, void *, int))
{
	struct msm_rpc_client *client;
	struct msm_rpc_endpoint *ept;
	int rc;

	client = msm_rpc_create_client();
	if (IS_ERR(client))
		return client;

	ept = msm_rpc_connect_compatible(prog, ver, MSM_RPC_UNINTERRUPTIBLE);
	if (IS_ERR(ept)) {
		kfree(client);
		return (struct msm_rpc_client *)ept;
	}

	client->prog = prog;
	client->ver = ver;
	client->ept = ept;
	client->cb_func = cb_func;

	/* start the read thread */
	client->read_thread = kthread_run(rpc_clients_thread, client,
					  "k%sclntd", name);
	if (IS_ERR(client->read_thread)) {
		rc = PTR_ERR(client->read_thread);
		msm_rpc_close(client->ept);
		kfree(client);
		return ERR_PTR(rc);
	}

	if (!create_cb_thread || (cb_func == NULL)) {
		client->cb_thread = NULL;
		return client;
	}

	/* start the callback thread */
	client->cb_thread = kthread_run(rpc_clients_cb_thread, client,
					"k%sclntcbd", name);
	if (IS_ERR(client->cb_thread)) {
		rc = PTR_ERR(client->cb_thread);
		client->exit_flag = 1;
		wait_for_completion(&client->complete);
		msm_rpc_close(client->ept);
		kfree(client);
		return ERR_PTR(rc);
	}

	return client;
}
EXPORT_SYMBOL(msm_rpc_register_client);

/*
 * Interface to be used to unregister the client
 * No client operations should be done once the unregister function
 * is called.
 *
 * client: pointer to client data structure.
 *
 * Return Value:
 *        Always returns 0 (success).
 */
int msm_rpc_unregister_client(struct msm_rpc_client *client)
{
	pr_info("%s: stopping client...\n", __func__);
	client->exit_flag = 1;
	if (client->cb_thread) {
		client->cb_avail = 1;
		wake_up(&client->cb_wait);
		wait_for_completion(&client->cb_complete);
	}

	wait_for_completion(&client->complete);

	msm_rpc_close(client->ept);
	msm_rpc_remove_all_cb_func(client);
	kfree(client->req);
	kfree(client->reply);
	kfree(client);
	return 0;
}
EXPORT_SYMBOL(msm_rpc_unregister_client);

/*
 * Interface to be used to send a client request.
 * If the request takes any arguments or expects any return, the user
 * should handle it in 'arg_func' and 'ret_func' respectively.
 * Marshaling and Unmarshaling should be handled by the user in argument
 * and return functions.
 *
 * client: pointer to client data sturcture
 *
 * proc: procedure being requested
 *
 * arg_func: argument function pointer.  'buf' is where arguments needs to
 *   be filled. 'data' is arg_data.
 *
 * ret_func: return function pointer.  'buf' is where returned data should
 *   be read from. 'data' is ret_data.
 *
 * arg_data: passed as an input parameter to argument function.
 *
 * ret_data: passed as an input parameter to return function.
 *
 * timeout: timeout for reply wait in jiffies.  If negative timeout is
 *   specified a default timeout of 10s is used.
 *
 * Return Value:
 *        0 on success, otherwise an error code is returned.
 */
int msm_rpc_client_req(struct msm_rpc_client *client, uint32_t proc,
		       int (*arg_func)(struct msm_rpc_client *client,
				       void *buf, void *data),
		       void *arg_data,
		       int (*ret_func)(struct msm_rpc_client *client,
				       void *buf, void *data),
		       void *ret_data, long timeout)
{
	int size = 0;
	struct rpc_reply_hdr *rpc_rsp;
	int rc = 0;

	mutex_lock(&client->req_lock);

	msm_rpc_setup_req((struct rpc_request_hdr *)client->req, client->prog,
			  client->ver, proc);
	size = sizeof(struct rpc_request_hdr);

	if (arg_func) {
		rc = arg_func(client, (void *)((struct rpc_request_hdr *)
					       client->req + 1), arg_data);
		if (rc < 0)
			goto release_locks;
		else
			size += rc;
	}

	rc = msm_rpc_write(client->ept, client->req, size);
	if (rc < 0) {
		pr_err("%s: couldn't send RPC request:%d\n", __func__, rc);
		goto release_locks;
	} else
		rc = 0;

	if (timeout < 0)
		timeout = msecs_to_jiffies(10000);

	rc = wait_event_timeout(client->reply_wait,
				client->read_avail, timeout);
	if (rc == 0) {
		rc = -ETIMEDOUT;
		goto release_locks;
	} else
		rc = 0;

	client->read_avail = 0;

	rpc_rsp = (struct rpc_reply_hdr *)client->buf;
	if (be32_to_cpu(rpc_rsp->reply_stat) != RPCMSG_REPLYSTAT_ACCEPTED) {
		pr_err("%s: RPC call was denied! %d\n", __func__,
		       be32_to_cpu(rpc_rsp->reply_stat));
		rc = -EPERM;
		goto free_and_release;
	}

	if (be32_to_cpu(rpc_rsp->data.acc_hdr.accept_stat) !=
	    RPC_ACCEPTSTAT_SUCCESS) {
		pr_err("%s: RPC call was not successful (%d)\n", __func__,
		       be32_to_cpu(rpc_rsp->data.acc_hdr.accept_stat));
		rc = -EINVAL;
		goto free_and_release;
	}

	if (ret_func)
		rc = ret_func(client, (void *)(rpc_rsp + 1), ret_data);

 free_and_release:
	kfree(client->buf);
 release_locks:
	mutex_unlock(&client->req_lock);
	return rc;
}
EXPORT_SYMBOL(msm_rpc_client_req);

/*
 * Interface to be used to start accepted reply message required in
 * callback handling. Returns the buffer pointer to attach any
 * payload.  Should call msm_rpc_send_accepted_reply to complete
 * sending reply.  Marshaling should be handled by user for the payload.
 *
 * client: pointer to client data structure
 *
 * xid: transaction id. Has to be same as the one in callback request.
 *
 * accept_status: acceptance status
 *
 * Return Value:
 *        pointer to buffer to attach the payload.
 */
void *msm_rpc_start_accepted_reply(struct msm_rpc_client *client,
				   uint32_t xid, uint32_t accept_status)
{
	struct rpc_reply_hdr *reply;

	mutex_lock(&client->reply_lock);

	reply = (struct rpc_reply_hdr *)client->reply;

	reply->xid = cpu_to_be32(xid);
	reply->type = cpu_to_be32(1); /* reply */
	reply->reply_stat = cpu_to_be32(RPCMSG_REPLYSTAT_ACCEPTED);

	reply->data.acc_hdr.accept_stat = cpu_to_be32(accept_status);
	reply->data.acc_hdr.verf_flavor = 0;
	reply->data.acc_hdr.verf_length = 0;

	return reply + 1;
}
EXPORT_SYMBOL(msm_rpc_start_accepted_reply);

/*
 * Interface to be used to send accepted reply required in callback handling.
 * msm_rpc_start_accepted_reply should have been called before.
 * Marshaling should be handled by user for the payload.
 *
 * client: pointer to client data structure
 *
 * size: additional payload size
 *
 * Return Value:
 *        0 on success, otherwise returns an error code.
 */
int msm_rpc_send_accepted_reply(struct msm_rpc_client *client, uint32_t size)
{
	int rc = 0;

	size += sizeof(struct rpc_reply_hdr);
	rc = msm_rpc_write(client->ept, client->reply, size);
	if (rc > 0)
		rc = 0;

	mutex_unlock(&client->reply_lock);
	return rc;
}
EXPORT_SYMBOL(msm_rpc_send_accepted_reply);

/*
 * Interface to be used to add a callback function.
 * If the call back function is already in client's 'cb_id - cb_func'
 * table, then that cb_id is returned.  otherwise, new entry
 * is added to the above table and corresponding cb_id is returned.
 *
 * client: pointer to client data structure
 *
 * cb_func: callback function
 *
 * Return Value:
 *         callback ID on success, otherwise returns an error code.
 *         If cb_func is NULL, the callback Id returned is 0xffffffff.
 *         This tells the other processor that no callback is reqested.
 */
int msm_rpc_add_cb_func(struct msm_rpc_client *client, void *cb_func)
{
	struct msm_rpc_cb_table_item *cb_item;

	if (cb_func == NULL)
		return MSM_RPC_CLIENT_NULL_CB_ID;

	mutex_lock(&client->cb_list_lock);
	list_for_each_entry(cb_item, &client->cb_list, list) {
		if (cb_item->cb_func == cb_func) {
			mutex_unlock(&client->cb_list_lock);
			return cb_item->cb_id;
		}
	}
	mutex_unlock(&client->cb_list_lock);

	cb_item = kmalloc(sizeof(struct msm_rpc_cb_table_item), GFP_KERNEL);
	if (!cb_item)
		return -ENOMEM;

	INIT_LIST_HEAD(&cb_item->list);
	cb_item->cb_id = atomic_add_return(1, &client->next_cb_id);
	cb_item->cb_func = cb_func;

	mutex_lock(&client->cb_list_lock);
	list_add_tail(&cb_item->list, &client->cb_list);
	mutex_unlock(&client->cb_list_lock);

	return cb_item->cb_id;
}
EXPORT_SYMBOL(msm_rpc_add_cb_func);

/*
 * Interface to be used to get a callback function from a callback ID.
 * If no entry is found, NULL is returned.
 *
 * client: pointer to client data structure
 *
 * cb_id: callback ID
 *
 * Return Value:
 *         callback function pointer if entry with given cb_id is found,
 *         otherwise returns NULL.
 */
void *msm_rpc_get_cb_func(struct msm_rpc_client *client, uint32_t cb_id)
{
	struct msm_rpc_cb_table_item *cb_item;

	mutex_lock(&client->cb_list_lock);
	list_for_each_entry(cb_item, &client->cb_list, list) {
		if (cb_item->cb_id == cb_id) {
			mutex_unlock(&client->cb_list_lock);
			return cb_item->cb_func;
		}
	}
	mutex_unlock(&client->cb_list_lock);
	return NULL;
}
EXPORT_SYMBOL(msm_rpc_get_cb_func);

/*
 * Interface to be used to remove a callback function.
 *
 * client: pointer to client data structure
 *
 * cb_func: callback function
 *
 */
void msm_rpc_remove_cb_func(struct msm_rpc_client *client, void *cb_func)
{
	struct msm_rpc_cb_table_item *cb_item, *tmp_cb_item;

	if (cb_func == NULL)
		return;

	mutex_lock(&client->cb_list_lock);
	list_for_each_entry_safe(cb_item, tmp_cb_item,
				 &client->cb_list, list) {
		if (cb_item->cb_func == cb_func) {
			list_del(&cb_item->list);
			kfree(cb_item);
			mutex_unlock(&client->cb_list_lock);
			return;
		}
	}
	mutex_unlock(&client->cb_list_lock);
}
EXPORT_SYMBOL(msm_rpc_remove_cb_func);