1 /*
2  * Copyright (c) 2014, Mentor Graphics Corporation
3  * Copyright (c) 2015 Xilinx, Inc.
4  * Copyright (c) 2016 Freescale Semiconductor, Inc.
5  * Copyright 2016-2022 NXP
6  * Copyright 2021 ACRIOS Systems s.r.o.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  *    this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include "rpmsg_lite.h"
34 #include "rpmsg_queue.h"
35 
rpmsg_queue_rx_cb(void * payload,uint32_t payload_len,uint32_t src,void * priv)36 int32_t rpmsg_queue_rx_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv)
37 {
38     rpmsg_queue_rx_cb_data_t msg;
39 
40     RL_ASSERT(priv != RL_NULL);
41 
42     msg.data = payload;
43     msg.len  = payload_len;
44     msg.src  = src;
45 
46     /* if message is successfully added into queue then hold rpmsg buffer */
47     if (0 != env_put_queue(priv, &msg, 0))
48     {
49         /* hold the rx buffer */
50         return RL_HOLD;
51     }
52 
53     return RL_RELEASE;
54 }
55 
56 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
rpmsg_queue_create(struct rpmsg_lite_instance * rpmsg_lite_dev,uint8_t * queue_storage,rpmsg_static_queue_ctxt * queue_ctxt)57 rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev,
58                                       uint8_t *queue_storage,
59                                       rpmsg_static_queue_ctxt *queue_ctxt)
60 #else
61 rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev)
62 #endif
63 {
64     int32_t status;
65     void *q = RL_NULL;
66 
67     if (rpmsg_lite_dev == RL_NULL)
68     {
69         return RL_NULL;
70     }
71 
72     /* create message queue for channel default endpoint */
73 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
74     if ((queue_storage == RL_NULL) || (queue_ctxt == RL_NULL))
75     {
76         return RL_NULL;
77     }
78     status = env_create_queue(&q, 2 * (int32_t)(rpmsg_lite_dev->rvq->vq_nentries),
79                               (int32_t)sizeof(rpmsg_queue_rx_cb_data_t), queue_storage, queue_ctxt);
80 #else
81     status = env_create_queue(&q, 2 * (int32_t)(rpmsg_lite_dev->rvq->vq_nentries),
82                               (int32_t)sizeof(rpmsg_queue_rx_cb_data_t));
83 #endif
84     if ((status != 0) || (q == RL_NULL))
85     {
86         return RL_NULL;
87     }
88 
89     return ((rpmsg_queue_handle)q);
90 }
91 
rpmsg_queue_destroy(struct rpmsg_lite_instance * rpmsg_lite_dev,rpmsg_queue_handle q)92 int32_t rpmsg_queue_destroy(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_queue_handle q)
93 {
94     if (rpmsg_lite_dev == RL_NULL)
95     {
96         return RL_ERR_PARAM;
97     }
98 
99     if (q == RL_NULL)
100     {
101         return RL_ERR_PARAM;
102     }
103     env_delete_queue((void *)q);
104     return RL_SUCCESS;
105 }
106 
rpmsg_queue_recv(struct rpmsg_lite_instance * rpmsg_lite_dev,rpmsg_queue_handle q,uint32_t * src,char * data,uint32_t maxlen,uint32_t * len,uintptr_t timeout)107 int32_t rpmsg_queue_recv(struct rpmsg_lite_instance *rpmsg_lite_dev,
108                          rpmsg_queue_handle q,
109                          uint32_t *src,
110                          char *data,
111                          uint32_t maxlen,
112                          uint32_t *len,
113                          uintptr_t timeout)
114 {
115     rpmsg_queue_rx_cb_data_t msg = {0};
116     int32_t retval               = RL_SUCCESS;
117 
118     if (rpmsg_lite_dev == RL_NULL)
119     {
120         return RL_ERR_PARAM;
121     }
122     if (q == RL_NULL)
123     {
124         return RL_ERR_PARAM;
125     }
126     if (data == RL_NULL)
127     {
128         return RL_ERR_PARAM;
129     }
130 
131     /* Get an element out of the message queue for the selected endpoint */
132     if (0 != env_get_queue((void *)q, &msg, timeout))
133     {
134         if (src != RL_NULL)
135         {
136             *src = msg.src;
137         }
138         if (len != RL_NULL)
139         {
140             *len = msg.len;
141         }
142 
143         if (maxlen >= msg.len)
144         {
145             env_memcpy(data, msg.data, msg.len);
146         }
147         else
148         {
149             retval = RL_ERR_BUFF_SIZE;
150         }
151 
152         /* Release used buffer. */
153         return ((RL_SUCCESS == rpmsg_lite_release_rx_buffer(rpmsg_lite_dev, msg.data)) ? retval : RL_ERR_PARAM);
154     }
155     else
156     {
157         return RL_ERR_NO_BUFF; /* failed */
158     }
159 }
160 
rpmsg_queue_recv_nocopy(struct rpmsg_lite_instance * rpmsg_lite_dev,rpmsg_queue_handle q,uint32_t * src,char ** data,uint32_t * len,uintptr_t timeout)161 int32_t rpmsg_queue_recv_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev,
162                                 rpmsg_queue_handle q,
163                                 uint32_t *src,
164                                 char **data,
165                                 uint32_t *len,
166                                 uintptr_t timeout)
167 {
168     rpmsg_queue_rx_cb_data_t msg = {0};
169 
170     if (rpmsg_lite_dev == RL_NULL)
171     {
172         return RL_ERR_PARAM;
173     }
174     if (data == RL_NULL)
175     {
176         return RL_ERR_PARAM;
177     }
178     if (q == RL_NULL)
179     {
180         return RL_ERR_PARAM;
181     }
182 
183     /* Get an element out of the message queue for the selected endpoint */
184     if (0 != env_get_queue((void *)q, &msg, timeout))
185     {
186         if (src != RL_NULL)
187         {
188             *src = msg.src;
189         }
190         if (len != RL_NULL)
191         {
192             *len = msg.len;
193         }
194 
195         *data = msg.data;
196 
197         return RL_SUCCESS; /* success */
198     }
199 
200     return RL_ERR_NO_BUFF; /* failed */
201 }
202 
rpmsg_queue_nocopy_free(struct rpmsg_lite_instance * rpmsg_lite_dev,void * data)203 int32_t rpmsg_queue_nocopy_free(struct rpmsg_lite_instance *rpmsg_lite_dev, void *data)
204 {
205     if (rpmsg_lite_dev == RL_NULL)
206     {
207         return RL_ERR_PARAM;
208     }
209     if (data == RL_NULL)
210     {
211         return RL_ERR_PARAM;
212     }
213 
214     /* Release used buffer. */
215     return ((RL_SUCCESS == rpmsg_lite_release_rx_buffer(rpmsg_lite_dev, data)) ? RL_SUCCESS : RL_ERR_PARAM);
216 }
217 
rpmsg_queue_get_current_size(rpmsg_queue_handle q)218 int32_t rpmsg_queue_get_current_size(rpmsg_queue_handle q)
219 {
220     if (q == RL_NULL)
221     {
222         return RL_ERR_PARAM;
223     }
224 
225     /* Return actual queue size. */
226     return env_get_current_queue_size((void *)q);
227 }
228