]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/ipc/mm/MmRpc.c
Remove references to SysLink in QNX code base
[ipc/ipcdev.git] / packages / ti / ipc / mm / MmRpc.c
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)
106     params->reserved = 0;
109 /*
110  *  ======== MmRpc_create ========
111  */
112 int MmRpc_create(const char *service, const MmRpc_Params *params,
113         MmRpc_Handle *handlePtr)
115     int             status = MmRpc_S_SUCCESS;
116     MmRpc_Object *  obj;
117     char            cbuf[RPPC_MAX_INST_NAMELEN+16];
119     /* allocate the instance object */
120     obj = (MmRpc_Object *)calloc(1, sizeof(MmRpc_Object));
122     if (obj == NULL) {
123         status = MmRpc_E_FAIL;
124         goto leave;
125     }
127     /* open the driver */
128     sprintf(cbuf, "/dev/%s", service);
129     obj->fd = open(cbuf, O_RDWR);
131     if (obj->fd < 0) {
132         printf("MmRpc_create: Error: open failed, name=%s\n", cbuf);
133         status = MmRpc_E_FAIL;
134         goto leave;
135     }
137     strncpy(obj->connect.name, service, (RPPC_MAX_INST_NAMELEN - 1));
138     obj->connect.name[RPPC_MAX_INST_NAMELEN - 1] = '\0';
140     /* create a server instance, rebind its address to this file descriptor */
141     status = ioctl(obj->fd, RPPC_IOC_CREATE, &obj->connect);
143     if (status < 0) {
144         printf("MmRpc_create: Error: connect failed\n");
145         status = MmRpc_E_FAIL;
146         goto leave;
147     }
149 leave:
150     if (status < 0) {
151         if ((obj != NULL) && (obj->fd >= 0)) {
152             close(obj->fd);
153         }
154         if (obj != NULL) {
155             free(obj);
156         }
157         *handlePtr = NULL;
158     }
159     else {
160         *handlePtr = (MmRpc_Handle)obj;
161     }
163     return(status);
166 /*
167  *  ======== MmRpc_delete ========
168  */
169 int MmRpc_delete(MmRpc_Handle *handlePtr)
171     int status = MmRpc_S_SUCCESS;
172     MmRpc_Object *obj;
174     obj = (MmRpc_Object *)(*handlePtr);
176     /* close the device */
177     if ((obj != NULL) && (obj->fd >= 0)) {
178         close(obj->fd);
179     }
181     /* free the instance object */
182     free((void *)(*handlePtr));
183     *handlePtr = NULL;
185     return(status);
188 /*
189  *  ======== MmRpc_call ========
190  */
191 int MmRpc_call(MmRpc_Handle handle, MmRpc_FxnCtx *ctx, int32_t *ret)
193     int status = MmRpc_S_SUCCESS;
194     MmRpc_Object *obj = (MmRpc_Object *)handle;
195     struct rppc_function *rpfxn;
196     struct rppc_function_return reply_msg;
197     MmRpc_Param *param;
198     void *msg;
199     int len;
200     int i;
202     /* combine params and translation array into one contiguous message */
203     len = sizeof(struct rppc_function) +
204                 (ctx->num_xlts * sizeof(struct rppc_param_translation));
205     msg = (void *)calloc(len, sizeof(char));
207     if (msg == NULL) {
208         printf("MmRpc_call: Error: msg alloc failed\n");
209         status = MmRpc_E_FAIL;
210         goto leave;
211     }
213     /* copy function parameters into message */
214     rpfxn = (struct rppc_function *)msg;
215     rpfxn->fxn_id = ctx->fxn_id;
216     rpfxn->num_params = ctx->num_params;
218     for (i = 0; i < ctx->num_params; i++) {
219         param = &ctx->params[i];
221         switch (param->type) {
222             case MmRpc_ParamType_Scalar:
223                 rpfxn->params[i].type = RPPC_PARAM_TYPE_ATOMIC;
224                 rpfxn->params[i].size = param->param.scalar.size;
225                 rpfxn->params[i].data = param->param.scalar.data;
226                 rpfxn->params[i].base = 0;
227                 rpfxn->params[i].fd = 0;
228                 break;
230             case MmRpc_ParamType_Ptr:
231                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
232                 rpfxn->params[i].size = param->param.ptr.size;
233                 rpfxn->params[i].data = param->param.ptr.addr;
234                 rpfxn->params[i].base = param->param.ptr.addr;
235                 rpfxn->params[i].fd = param->param.ptr.handle;
236                 break;
238             case MmRpc_ParamType_OffPtr:
239                 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
240                 rpfxn->params[i].size = param->param.offPtr.size;
241                 rpfxn->params[i].data = param->param.offPtr.base +
242                         param->param.offPtr.offset;
243                 rpfxn->params[i].base = param->param.offPtr.base;
244                 rpfxn->params[i].fd = param->param.offPtr.handle;
245                 break;
247             default:
248                 printf("MmRpc_call: Error: invalid parameter type\n");
249                 status = MmRpc_E_INVALIDPARAM;
250                 goto leave;
251                 break;
252         }
253     }
255     /* copy offset array into message */
256     rpfxn->num_translations = ctx->num_xlts;
258     for (i = 0; i < ctx->num_xlts; i++) {
259         /* pack the pointer translation entry */
260         rpfxn->translations[i].index = ctx->xltAry[i].index;
261         rpfxn->translations[i].offset = ctx->xltAry[i].offset;
262         rpfxn->translations[i].base = ctx->xltAry[i].base;
263         rpfxn->translations[i].fd = (int32_t)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);
303 /*
304  *  ======== MmRcp_release ========
305  */
306 int MmRpc_release(MmRpc_Handle handle, MmRpc_BufType type, int num,
307         MmRpc_BufDesc *desc)
309     int stat = MmRpc_S_SUCCESS;
311     switch (type) {
313 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
314         case MmRpc_BufType_Handle:
315             stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFUNREGISTER, num, desc);
316             break;
318 #elif defined(IPC_BUILDOS_QNX)
319         case MmRpc_BufType_Ptr:
320             break;
321 #endif
322         default:
323             printf("MmRpc_release: Error: unsupported type value: %d\n", type);
324             stat = MmRpc_E_INVALIDPARAM;
325             break;
326     }
328     if (stat < 0) {
329         printf("MmRpc_release: Error: unable to release buffer\n");
330     }
332     return(stat);
335 /*
336  *  ======== MmRcp_use ========
337  */
338 int MmRpc_use(MmRpc_Handle handle, MmRpc_BufType type, int num,
339         MmRpc_BufDesc *desc)
341     int stat = MmRpc_S_SUCCESS;
343     switch (type) {
345 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
346         case MmRpc_BufType_Handle:
347             stat = MmRpc_bufHandle(handle, RPPC_IOC_BUFREGISTER, num, desc);
348             break;
350 #elif defined(IPC_BUILDOS_QNX)
351         case MmRpc_BufType_Ptr:
352             break;
353 #endif
354         default:
355             printf("MmRpc_use: Error: unsupported type value: %d\n", type);
356             stat = MmRpc_E_INVALIDPARAM;
357             break;
358     }
360     if (stat < 0) {
361         printf("MmRpc_use: Error: unable to declare buffer use\n");
362     }
364     return(stat);
367 #if defined(KERNEL_INSTALL_DIR) || defined(IPC_BUILDOS_ANDROID)
368 /*
369  *  ======== MmRpc_bufHandle ========
370  */
371 int MmRpc_bufHandle(MmRpc_Handle handle, int cmd, int num, MmRpc_BufDesc *desc)
373     int stat = MmRpc_S_SUCCESS;
374     MmRpc_Object *obj = (MmRpc_Object *)handle;
375     int i;
376     struct rppc_buf_fds reg = { num, NULL };
378     reg.fds = (int32_t *)malloc(num * sizeof(int32_t));
380     if (reg.fds == NULL) {
381         stat = MmRpc_E_NOMEM;
382         goto leave;
383     }
385     for (i = 0; i < num; i++) {
386         reg.fds[i] = desc[i].handle;
387     }
389     stat = ioctl(obj->fd, cmd, &reg);
391     if (stat < 0) {
392         stat = MmRpc_E_SYS;
393     }
395 leave:
396     if (reg.fds != NULL) {
397         free(reg.fds);
398     }
400     return(stat);
402 #endif