1 /*
2 * Copyright (c) 2012-2015, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
33 /*
34 * ======== MmRpc.c ========
35 */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <string.h>
46 #include <stdint.h> /* should be in linux/rpmsg_rpc.h */
47 #include <stddef.h> /* should be in linux/rpmsg_rpc.h */
51 #if defined(KERNEL_INSTALL_DIR)
53 #ifdef linux
54 #define _linux_ linux
55 #undef linux
56 #endif
58 #define linux_version_include(kd) <kd/include/generated/uapi/linux/version.h>
59 #include linux_version_include(KERNEL_INSTALL_DIR)
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
62 #define linux_include(kd,m) <kd/include/linux/m.h>
63 #else
64 #define linux_include(kd,m) <kd/include/uapi/linux/m.h>
65 #endif
67 #include linux_include(KERNEL_INSTALL_DIR,rpmsg_rpc)
69 #ifdef _linux_
70 #define linux _linux
71 #undef _linux_
72 #endif
74 #elif defined(IPC_BUILDOS_QNX)
76 #include <ti/ipc/rpmsg_rpc.h>
78 #elif defined(IPC_BUILDOS_ANDROID)
79 #include <uapi/linux/rpmsg_rpc.h>
81 #else
82 #error Unsupported Operating System
83 #endif
85 #include "MmRpc.h"
87 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
88 static int MmRpc_bufHandle(MmRpc_Handle handle, int cmd, int num,
89 MmRpc_BufDesc *desc);
90 #endif
93 /*
94 * ======== MmRpc_Object ========
95 */
96 typedef struct {
97 int fd; /* device file descriptor */
98 struct rppc_create_instance connect; /* connection object */
99 } MmRpc_Object;
101 /*
102 * ======== MmRpc_Params_init ========
103 */
104 void MmRpc_Params_init(MmRpc_Params *params)
105 {
106 params->reserved = 0;
107 }
109 /*
110 * ======== MmRpc_create ========
111 */
112 int MmRpc_create(const char *service, const MmRpc_Params *params,
113 MmRpc_Handle *handlePtr)
114 {
115 int status = MmRpc_S_SUCCESS;
116 MmRpc_Object * obj = NULL;
117 char cbuf[RPPC_MAX_INST_NAMELEN+16];
118 (void)params;
120 if (service == NULL || handlePtr == NULL) {
121 status = MmRpc_E_INVALIDPARAM;
122 goto leave;
123 }
125 /* allocate the instance object */
126 obj = (MmRpc_Object *)calloc(1, sizeof(MmRpc_Object));
128 if (obj == NULL) {
129 status = MmRpc_E_FAIL;
130 goto leave;
131 }
133 /* open the driver */
134 sprintf(cbuf, "/dev/%s", service);
135 obj->fd = open(cbuf, O_RDWR);
137 if (obj->fd < 0) {
138 printf("MmRpc_create: Error: open failed, name=%s\n", cbuf);
139 status = MmRpc_E_FAIL;
140 goto leave;
141 }
143 strncpy(obj->connect.name, service, (RPPC_MAX_INST_NAMELEN - 1));
144 obj->connect.name[RPPC_MAX_INST_NAMELEN - 1] = '\0';
146 /* create a server instance, rebind its address to this file descriptor */
147 status = ioctl(obj->fd, RPPC_IOC_CREATE, &obj->connect);
149 if (status < 0) {
150 printf("MmRpc_create: Error: connect failed\n");
151 status = MmRpc_E_FAIL;
152 goto leave;
153 }
155 leave:
156 if (status < 0) {
157 if ((obj != NULL) && (obj->fd >= 0)) {
158 close(obj->fd);
159 }
160 if (obj != NULL) {
161 free(obj);
162 }
163 if (handlePtr) {
164 *handlePtr = NULL;
165 }
166 }
167 else {
168 *handlePtr = (MmRpc_Handle)obj;
169 }
171 return(status);
172 }
174 /*
175 * ======== MmRpc_delete ========
176 */
177 int MmRpc_delete(MmRpc_Handle *handlePtr)
178 {
179 int status = MmRpc_S_SUCCESS;
180 MmRpc_Object *obj;
182 if (handlePtr == NULL) {
183 return MmRpc_E_INVALIDPARAM;
184 }
186 obj = (MmRpc_Object *)(*handlePtr);
188 /* close the device */
189 if ((obj != NULL) && (obj->fd >= 0)) {
190 close(obj->fd);
191 }
193 /* free the instance object */
194 free((void *)(*handlePtr));
195 *handlePtr = NULL;
197 return(status);
198 }
200 /*
201 * ======== MmRpc_call ========
202 */
203 int MmRpc_call(MmRpc_Handle handle, MmRpc_FxnCtx *ctx, int32_t *ret)
204 {
205 int status = MmRpc_S_SUCCESS;
206 MmRpc_Object *obj = (MmRpc_Object *)handle;
207 struct rppc_function *rpfxn;
208 struct rppc_function_return reply_msg;
209 MmRpc_Param *param;
210 void *msg = NULL;
211 int len;
212 unsigned int i;
214 if (handle == NULL || ctx == NULL || ret == NULL) {
215 status = MmRpc_E_INVALIDPARAM;
216 goto leave;
217 }
219 /* combine params and translation array into one contiguous message */
220 len = sizeof(struct rppc_function) +
221 (ctx->num_xlts * sizeof(struct rppc_param_translation));
222 msg = (void *)calloc(len, sizeof(char));
224 if (msg == NULL) {
225 printf("MmRpc_call: Error: msg alloc failed\n");
226 status = MmRpc_E_FAIL;
227 goto leave;
228 }
230 /* copy function parameters into message */
231 rpfxn = (struct rppc_function *)msg;
232 rpfxn->fxn_id = ctx->fxn_id;
233 rpfxn->num_params = ctx->num_params;
235 for (i = 0; i < ctx->num_params; i++) {
236 param = &ctx->params[i];
238 switch (param->type) {
239 case MmRpc_ParamType_Scalar:
240 rpfxn->params[i].type = RPPC_PARAM_TYPE_ATOMIC;
241 rpfxn->params[i].size = param->param.scalar.size;
242 rpfxn->params[i].data = param->param.scalar.data;
243 rpfxn->params[i].base = 0;
244 rpfxn->params[i].fd = 0;
245 break;
247 case MmRpc_ParamType_Ptr:
248 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
249 rpfxn->params[i].size = param->param.ptr.size;
250 rpfxn->params[i].data = param->param.ptr.addr;
251 rpfxn->params[i].base = param->param.ptr.addr;
252 rpfxn->params[i].fd = param->param.ptr.handle;
253 break;
255 case MmRpc_ParamType_OffPtr:
256 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
257 rpfxn->params[i].size = param->param.offPtr.size;
258 rpfxn->params[i].data = param->param.offPtr.base +
259 param->param.offPtr.offset;
260 rpfxn->params[i].base = param->param.offPtr.base;
261 rpfxn->params[i].fd = param->param.offPtr.handle;
262 break;
264 default:
265 printf("MmRpc_call: Error: invalid parameter type\n");
266 status = MmRpc_E_INVALIDPARAM;
267 goto leave;
268 break;
269 }
270 }
272 /* copy offset array into message */
273 rpfxn->num_translations = ctx->num_xlts;
275 for (i = 0; i < ctx->num_xlts; i++) {
276 /* pack the pointer translation entry */
277 rpfxn->translations[i].index = ctx->xltAry[i].index;
278 rpfxn->translations[i].offset = ctx->xltAry[i].offset;
279 rpfxn->translations[i].base = ctx->xltAry[i].base;
280 rpfxn->translations[i].fd = (int32_t)ctx->xltAry[i].handle;
281 }
283 /* send message for remote execution */
284 status = write(obj->fd, msg, len);
286 if (status < 0) {
287 printf("MmRpc_call: Error: write failed\n");
288 status = MmRpc_E_FAIL;
289 goto leave;
290 }
292 /* wait for return status from remote service */
293 status = read(obj->fd, &reply_msg, sizeof(struct rppc_function_return));
295 if (status < 0) {
296 printf("MmRpc_call: Error: read failed\n");
297 status = MmRpc_E_FAIL;
298 goto leave;
299 }
300 else if (status != sizeof(struct rppc_function_return)) {
301 printf("MmRpc_call: Error: reply bytes=%d, expected %d\n",
302 status, sizeof(struct rppc_function_return));
303 status = MmRpc_E_FAIL;
304 goto leave;
305 }
306 else {
307 status = MmRpc_S_SUCCESS;
308 }
310 *ret = (int32_t)reply_msg.status;
312 leave:
313 if (msg != NULL) {
314 free(msg);
315 }
317 return(status);
318 }
320 /*
321 * ======== MmRcp_release ========
322 */
323 int MmRpc_release(MmRpc_Handle handle, MmRpc_BufType type, int num,
324 MmRpc_BufDesc *desc)
325 {
326 int stat = MmRpc_S_SUCCESS;
328 switch (type) {
330 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
331 case MmRpc_BufType_Handle:
332 stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFUNREGISTER, num, desc);
333 break;
335 #elif defined(IPC_BUILDOS_QNX)
336 case MmRpc_BufType_Ptr:
337 break;
338 #endif
339 default:
340 printf("MmRpc_release: Error: unsupported type value: %d\n", type);
341 stat = MmRpc_E_INVALIDPARAM;
342 break;
343 }
345 if (stat < 0) {
346 printf("MmRpc_release: Error: unable to release buffer\n");
347 }
349 return(stat);
350 }
352 /*
353 * ======== MmRcp_use ========
354 */
355 int MmRpc_use(MmRpc_Handle handle, MmRpc_BufType type, int num,
356 MmRpc_BufDesc *desc)
357 {
358 int stat = MmRpc_S_SUCCESS;
360 switch (type) {
362 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
363 case MmRpc_BufType_Handle:
364 stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFREGISTER, num, desc);
365 break;
367 #elif defined(IPC_BUILDOS_QNX)
368 case MmRpc_BufType_Ptr:
369 break;
370 #endif
371 default:
372 printf("MmRpc_use: Error: unsupported type value: %d\n", type);
373 stat = MmRpc_E_INVALIDPARAM;
374 break;
375 }
377 if (stat < 0) {
378 printf("MmRpc_use: Error: unable to declare buffer use\n");
379 }
381 return(stat);
382 }
384 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
385 /*
386 * ======== MmRpc_bufHandle ========
387 */
388 int MmRpc_bufHandle(MmRpc_Handle handle, int cmd, int num, MmRpc_BufDesc *desc)
389 {
390 int stat = MmRpc_S_SUCCESS;
391 MmRpc_Object *obj = (MmRpc_Object *)handle;
392 int i;
393 struct rppc_buf_fds reg = { num, NULL };
395 if (handle == NULL || desc == NULL) {
396 stat = MmRpc_E_INVALIDPARAM;
397 goto leave;
398 }
400 reg.fds = (int32_t *)malloc(num * sizeof(int32_t));
402 if (reg.fds == NULL) {
403 stat = MmRpc_E_NOMEM;
404 goto leave;
405 }
407 for (i = 0; i < num; i++) {
408 reg.fds[i] = desc[i].handle;
409 }
411 stat = ioctl(obj->fd, cmd, ®);
413 if (stat < 0) {
414 stat = MmRpc_E_SYS;
415 }
417 leave:
418 if (reg.fds != NULL) {
419 free(reg.fds);
420 }
422 return(stat);
423 }
424 #endif