]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/ipc/mm/MmRpc.c
Added offset pointer support to MmRcp.
[ipc/ipcdev.git] / packages / ti / ipc / mm / MmRpc.c
1 /*
2  * Copyright (c) 2012-2013, 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 #else
69 #error Unsupported Operating System
70 #endif
72 #include "MmRpc.h"
75 /*
76  *  ======== MmRpc_Object ========
77  */
78 typedef struct {
79     int                         fd;         /* device file descriptor */
80     struct rppc_create_instance connect;    /* connection object */
81 } MmRpc_Object;
83 /*
84  *  ======== MmRpc_Params_init ========
85  */
86 void MmRpc_Params_init(MmRpc_Params *params)
87 {
88     params->reserved = 0;
89 }
91 /*
92  *  ======== MmRpc_create ========
93  */
94 int MmRpc_create(const char *service, const MmRpc_Params *params,
95         MmRpc_Handle *handlePtr)
96 {
97     int             status = MmRpc_S_SUCCESS;
98     MmRpc_Object *  obj;
99     char            cbuf[RPPC_MAX_INST_NAMELEN+16];
101     /* allocate the instance object */
102     obj = (MmRpc_Object *)calloc(1, sizeof(MmRpc_Object));
104     if (obj == NULL) {
105         status = MmRpc_E_FAIL;
106         goto leave;
107     }
109     /* open the driver */
110     sprintf(cbuf, "/dev/%s", service);
111     obj->fd = open(cbuf, O_RDWR);
113     if (obj->fd < 0) {
114         printf("MmRpc_create: Error: open failed, name=%s\n", cbuf);
115         status = MmRpc_E_FAIL;
116         goto leave;
117     }
119     strncpy(obj->connect.name, service, (RPPC_MAX_INST_NAMELEN - 1));
120     obj->connect.name[RPPC_MAX_INST_NAMELEN - 1] = '\0';
122     /* create a server instance, rebind its address to this file descriptor */
123     status = ioctl(obj->fd, RPPC_IOC_CREATE, &obj->connect);
125     if (status < 0) {
126         printf("MmRpc_create: Error: connect failed\n");
127         status = MmRpc_E_FAIL;
128         goto leave;
129     }
131 leave:
132     if (status < 0) {
133         if ((obj != NULL) && (obj->fd >= 0)) {
134             close(obj->fd);
135         }
136         if (obj != NULL) {
137             free(obj);
138         }
139         *handlePtr = NULL;
140     }
141     else {
142         *handlePtr = (MmRpc_Handle)obj;
143     }
145     return(status);
148 /*
149  *  ======== MmRpc_delete ========
150  */
151 int MmRpc_delete(MmRpc_Handle *handlePtr)
153     int status = MmRpc_S_SUCCESS;
154     MmRpc_Object *obj;
156     obj = (MmRpc_Object *)(*handlePtr);
158     /* close the device */
159     if ((obj != NULL) && (obj->fd >= 0)) {
160         close(obj->fd);
161     }
163     /* free the instance object */
164     free((void *)(*handlePtr));
165     *handlePtr = NULL;
167     return(status);
170 /*
171  *  ======== MmRpc_call ========
172  */
173 int MmRpc_call(MmRpc_Handle handle, MmRpc_FxnCtx *ctx, int32_t *ret)
175     int status = MmRpc_S_SUCCESS;
176     MmRpc_Object *obj = (MmRpc_Object *)handle;
177     struct rppc_function *rpfxn;
178     struct rppc_function_return reply_msg;
179     MmRpc_Param *param;
180     void *msg;
181     int len;
182     int i;
184     /* Combine function parameters and translation array into one contiguous
185      * message. TODO, modify driver to accept two separate buffers in order
186      * to eliminate this step. */
187     len = sizeof(struct rppc_function) +
188                 (ctx->num_xlts * sizeof(struct rppc_param_translation));
189     msg = (void *)calloc(len, sizeof(char));
191     if (msg == NULL) {
192         printf("MmRpc_call: Error: msg alloc failed\n");
193         status = MmRpc_E_FAIL;
194         goto leave;
195     }
197     /* copy function parameters into message */
198     rpfxn = (struct rppc_function *)msg;
199     rpfxn->fxn_id = ctx->fxn_id;
200     rpfxn->num_params = ctx->num_params;
202     for (i = 0; i < ctx->num_params; i++) {
203         param = &ctx->params[i];
205         switch (param->type) {
206             case MmRpc_ParamType_Scalar:
207                 rpfxn->params[i].type = RPPC_PARAM_TYPE_ATOMIC;
208                 rpfxn->params[i].size = param->param.scalar.size;
209                 rpfxn->params[i].data = param->param.scalar.data;
210                 rpfxn->params[i].base = 0;
211                 rpfxn->params[i].reserved = 0;
212                 break;
214             case MmRpc_ParamType_Ptr:
215                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
216                 rpfxn->params[i].size = param->param.ptr.size;
217                 rpfxn->params[i].data = param->param.ptr.addr;
218                 rpfxn->params[i].base = param->param.ptr.addr;
219                 rpfxn->params[i].reserved = param->param.ptr.handle;
220                 break;
222             case MmRpc_ParamType_OffPtr:
223                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
224                 rpfxn->params[i].size = param->param.offPtr.size;
225                 rpfxn->params[i].data = param->param.offPtr.base +
226                         param->param.offPtr.offset;
227                 rpfxn->params[i].base = param->param.offPtr.base;
228                 rpfxn->params[i].reserved = param->param.offPtr.handle;
229                 break;
231 #if 0 /* TBD */
232             case MmRpc_ParamType_Elem:
233                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
234                 rpfxn->params[i].size = param->param.elem.size;
235                 rpfxn->params[i].data = param->param.elem.offset;
236                 rpfxn->params[i].base = param->param.elem.base;
237                 rpfxn->params[i].reserved = param->param.elem.handle;
238                 break;
239 #endif
240             default:
241                 printf("MmRpc_call: Error: invalid parameter type\n");
242                 status = MmRpc_E_INVALIDPARAM;
243                 goto leave;
244                 break;
245         }
246     }
248     /* copy offset array into message */
249     rpfxn->num_translations = ctx->num_xlts;
251     for (i = 0; i < ctx->num_xlts; i++) {
252         uint32_t index;
253         size_t ptr;
255         /* compute base value */
256         index = ctx->xltAry[i].index;
257         ptr = rpfxn->params[index].base + ctx->xltAry[i].offset;
259         /* pack the pointer translation entry */
260         rpfxn->translations[i].index    = index;
261         rpfxn->translations[i].offset   = ctx->xltAry[i].offset;
262         rpfxn->translations[i].base     = (size_t)(*(void **)ptr);
263         rpfxn->translations[i].reserved = ctx->xltAry[i].handle;
264     }
266     /* send message for remote execution */
267     status = write(obj->fd, msg, len);
269     if (status < 0) {
270         printf("MmRpc_call: Error: write failed\n");
271         status = MmRpc_E_FAIL;
272         goto leave;
273     }
275     /* wait for return status from remote service */
276     status = read(obj->fd, &reply_msg, sizeof(struct rppc_function_return));
278     if (status < 0) {
279         printf("MmRpc_call: Error: read failed\n");
280         status = MmRpc_E_FAIL;
281         goto leave;
282     }
283     else if (status != sizeof(struct rppc_function_return)) {
284         printf("MmRpc_call: Error: reply bytes=%d, expected %d\n",
285                 status, sizeof(struct rppc_function_return));
286         status = MmRpc_E_FAIL;
287         goto leave;
288     }
289     else {
290         status = MmRpc_S_SUCCESS;
291     }
293     *ret = (int32_t)reply_msg.status;
295 leave:
296     if (msg != NULL) {
297         free(msg);
298     }
300     return(status);