]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/ipc/mm/MmRpc.c
Add MmServiceMgr_getId() to allow retrieval of service instance id
[ipc/ipcdev.git] / packages / ti / ipc / mm / MmRpc.c
1 /*
2  * Copyright (c) 2012-2014, 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
57 #define linux_include(kd,m) <kd/include/linux/m.h>
58 #include linux_include(KERNEL_INSTALL_DIR,rpmsg_rpc)
59 #ifdef _linux_
60 #define linux _linux
61 #undef _linux_
62 #endif
64 #elif defined(SYSLINK_BUILDOS_QNX)
66 #include <ti/ipc/rpmsg_rpc.h>
68 #elif defined(IPC_BUILDOS_ANDROID)
69 #include <linux/rpmsg_rpc.h>
71 #else
72 #error Unsupported Operating System
73 #endif
75 #include "MmRpc.h"
77 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
78 static int MmRpc_bufHandle(MmRpc_Handle handle, int cmd, int num,
79         MmRpc_BufDesc *desc);
80 #endif
83 /*
84  *  ======== MmRpc_Object ========
85  */
86 typedef struct {
87     int                         fd;         /* device file descriptor */
88     struct rppc_create_instance connect;    /* connection object */
89 } MmRpc_Object;
91 /*
92  *  ======== MmRpc_Params_init ========
93  */
94 void MmRpc_Params_init(MmRpc_Params *params)
95 {
96     params->reserved = 0;
97 }
99 /*
100  *  ======== MmRpc_create ========
101  */
102 int MmRpc_create(const char *service, const MmRpc_Params *params,
103         MmRpc_Handle *handlePtr)
105     int             status = MmRpc_S_SUCCESS;
106     MmRpc_Object *  obj;
107     char            cbuf[RPPC_MAX_INST_NAMELEN+16];
109     /* allocate the instance object */
110     obj = (MmRpc_Object *)calloc(1, sizeof(MmRpc_Object));
112     if (obj == NULL) {
113         status = MmRpc_E_FAIL;
114         goto leave;
115     }
117     /* open the driver */
118     sprintf(cbuf, "/dev/%s", service);
119     obj->fd = open(cbuf, O_RDWR);
121     if (obj->fd < 0) {
122         printf("MmRpc_create: Error: open failed, name=%s\n", cbuf);
123         status = MmRpc_E_FAIL;
124         goto leave;
125     }
127     strncpy(obj->connect.name, service, (RPPC_MAX_INST_NAMELEN - 1));
128     obj->connect.name[RPPC_MAX_INST_NAMELEN - 1] = '\0';
130     /* create a server instance, rebind its address to this file descriptor */
131     status = ioctl(obj->fd, RPPC_IOC_CREATE, &obj->connect);
133     if (status < 0) {
134         printf("MmRpc_create: Error: connect failed\n");
135         status = MmRpc_E_FAIL;
136         goto leave;
137     }
139 leave:
140     if (status < 0) {
141         if ((obj != NULL) && (obj->fd >= 0)) {
142             close(obj->fd);
143         }
144         if (obj != NULL) {
145             free(obj);
146         }
147         *handlePtr = NULL;
148     }
149     else {
150         *handlePtr = (MmRpc_Handle)obj;
151     }
153     return(status);
156 /*
157  *  ======== MmRpc_delete ========
158  */
159 int MmRpc_delete(MmRpc_Handle *handlePtr)
161     int status = MmRpc_S_SUCCESS;
162     MmRpc_Object *obj;
164     obj = (MmRpc_Object *)(*handlePtr);
166     /* close the device */
167     if ((obj != NULL) && (obj->fd >= 0)) {
168         close(obj->fd);
169     }
171     /* free the instance object */
172     free((void *)(*handlePtr));
173     *handlePtr = NULL;
175     return(status);
178 /*
179  *  ======== MmRpc_call ========
180  */
181 int MmRpc_call(MmRpc_Handle handle, MmRpc_FxnCtx *ctx, int32_t *ret)
183     int status = MmRpc_S_SUCCESS;
184     MmRpc_Object *obj = (MmRpc_Object *)handle;
185     struct rppc_function *rpfxn;
186     struct rppc_function_return reply_msg;
187     MmRpc_Param *param;
188     void *msg;
189     int len;
190     int i;
192     /* combine params and translation array into one contiguous message */
193     len = sizeof(struct rppc_function) +
194                 (ctx->num_xlts * sizeof(struct rppc_param_translation));
195     msg = (void *)calloc(len, sizeof(char));
197     if (msg == NULL) {
198         printf("MmRpc_call: Error: msg alloc failed\n");
199         status = MmRpc_E_FAIL;
200         goto leave;
201     }
203     /* copy function parameters into message */
204     rpfxn = (struct rppc_function *)msg;
205     rpfxn->fxn_id = ctx->fxn_id;
206     rpfxn->num_params = ctx->num_params;
208     for (i = 0; i < ctx->num_params; i++) {
209         param = &ctx->params[i];
211         switch (param->type) {
212             case MmRpc_ParamType_Scalar:
213                 rpfxn->params[i].type = RPPC_PARAM_TYPE_ATOMIC;
214                 rpfxn->params[i].size = param->param.scalar.size;
215                 rpfxn->params[i].data = param->param.scalar.data;
216                 rpfxn->params[i].base = 0;
217                 rpfxn->params[i].fd = 0;
218                 break;
220             case MmRpc_ParamType_Ptr:
221                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
222                 rpfxn->params[i].size = param->param.ptr.size;
223                 rpfxn->params[i].data = param->param.ptr.addr;
224                 rpfxn->params[i].base = param->param.ptr.addr;
225                 rpfxn->params[i].fd = param->param.ptr.handle;
226                 break;
228             case MmRpc_ParamType_OffPtr:
229                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
230                 rpfxn->params[i].size = param->param.offPtr.size;
231                 rpfxn->params[i].data = param->param.offPtr.base +
232                         param->param.offPtr.offset;
233                 rpfxn->params[i].base = param->param.offPtr.base;
234                 rpfxn->params[i].fd = param->param.offPtr.handle;
235                 break;
237             default:
238                 printf("MmRpc_call: Error: invalid parameter type\n");
239                 status = MmRpc_E_INVALIDPARAM;
240                 goto leave;
241                 break;
242         }
243     }
245     /* copy offset array into message */
246     rpfxn->num_translations = ctx->num_xlts;
248     for (i = 0; i < ctx->num_xlts; i++) {
249         /* pack the pointer translation entry */
250         rpfxn->translations[i].index = ctx->xltAry[i].index;
251         rpfxn->translations[i].offset = ctx->xltAry[i].offset;
252         rpfxn->translations[i].base = ctx->xltAry[i].base;
253         rpfxn->translations[i].fd = (int32_t)ctx->xltAry[i].handle;
254     }
256     /* send message for remote execution */
257     status = write(obj->fd, msg, len);
259     if (status < 0) {
260         printf("MmRpc_call: Error: write failed\n");
261         status = MmRpc_E_FAIL;
262         goto leave;
263     }
265     /* wait for return status from remote service */
266     status = read(obj->fd, &reply_msg, sizeof(struct rppc_function_return));
268     if (status < 0) {
269         printf("MmRpc_call: Error: read failed\n");
270         status = MmRpc_E_FAIL;
271         goto leave;
272     }
273     else if (status != sizeof(struct rppc_function_return)) {
274         printf("MmRpc_call: Error: reply bytes=%d, expected %d\n",
275                 status, sizeof(struct rppc_function_return));
276         status = MmRpc_E_FAIL;
277         goto leave;
278     }
279     else {
280         status = MmRpc_S_SUCCESS;
281     }
283     *ret = (int32_t)reply_msg.status;
285 leave:
286     if (msg != NULL) {
287         free(msg);
288     }
290     return(status);
293 /*
294  *  ======== MmRcp_release ========
295  */
296 int MmRpc_release(MmRpc_Handle handle, MmRpc_BufType type, int num,
297         MmRpc_BufDesc *desc)
299     int stat = MmRpc_S_SUCCESS;
301     switch (type) {
303 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
304         case MmRpc_BufType_Handle:
305             stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFUNREGISTER, num, desc);
306             break;
308 #elif defined(SYSLINK_BUILDOS_QNX)
309         case MmRpc_BufType_Ptr:
310             break;
311 #endif
312         default:
313             printf("MmRpc_release: Error: unsupported type value: %d\n", type);
314             stat = MmRpc_E_INVALIDPARAM;
315             break;
316     }
318     if (stat < 0) {
319         printf("MmRpc_release: Error: unable to release buffer\n");
320     }
322     return(stat);
325 /*
326  *  ======== MmRcp_use ========
327  */
328 int MmRpc_use(MmRpc_Handle handle, MmRpc_BufType type, int num,
329         MmRpc_BufDesc *desc)
331     int stat = MmRpc_S_SUCCESS;
333     switch (type) {
335 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
336         case MmRpc_BufType_Handle:
337             stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFREGISTER, num, desc);
338             break;
340 #elif defined(SYSLINK_BUILDOS_QNX)
341         case MmRpc_BufType_Ptr:
342             break;
343 #endif
344         default:
345             printf("MmRpc_use: Error: unsupported type value: %d\n", type);
346             stat = MmRpc_E_INVALIDPARAM;
347             break;
348     }
350     if (stat < 0) {
351         printf("MmRpc_use: Error: unable to declare buffer use\n");
352     }
354     return(stat);
357 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
358 /*
359  *  ======== MmRpc_bufHandle ========
360  */
361 int MmRpc_bufHandle(MmRpc_Handle handle, int cmd, int num, MmRpc_BufDesc *desc)
363     int stat = MmRpc_S_SUCCESS;
364     MmRpc_Object *obj = (MmRpc_Object *)handle;
365     int i;
366     struct rppc_buf_fds reg = { num, NULL };
368     reg.fds = (int32_t *)malloc(num * sizeof(int32_t));
370     if (reg.fds == NULL) {
371         stat = MmRpc_E_NOMEM;
372         goto leave;
373     }
375     for (i = 0; i < num; i++) {
376         reg.fds[i] = desc[i].handle;
377     }
379     stat = ioctl(obj->fd, cmd, &reg);
381     if (stat < 0) {
382         stat = MmRpc_E_SYS;
383     }
385 leave:
386     if (reg.fds != NULL) {
387         free(reg.fds);
388     }
390     return(stat);
392 #endif