2 #include "wilc_msgqueue.h"
3 #include <linux/spinlock.h>
4 #include "linux_wlan_common.h"
5 #include <linux/errno.h>
6 #include <linux/slab.h>
11 * @note copied from FLO glue implementatuion
14 int wilc_mq_create(WILC_MsgQueueHandle *pHandle)
16 spin_lock_init(&pHandle->strCriticalSection);
17 sema_init(&pHandle->hSem, 0);
18 pHandle->pstrMessageList = NULL;
19 pHandle->u32ReceiversCount = 0;
20 pHandle->bExiting = false;
27 * @note copied from FLO glue implementatuion
30 int wilc_mq_destroy(WILC_MsgQueueHandle *pHandle)
32 pHandle->bExiting = true;
34 /* Release any waiting receiver thread. */
35 while (pHandle->u32ReceiversCount > 0) {
37 pHandle->u32ReceiversCount--;
40 while (pHandle->pstrMessageList) {
41 Message *pstrMessge = pHandle->pstrMessageList->pstrNext;
43 kfree(pHandle->pstrMessageList);
44 pHandle->pstrMessageList = pstrMessge;
53 * @note copied from FLO glue implementatuion
56 int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
57 const void *pvSendBuffer, u32 u32SendBufferSize)
60 Message *pstrMessage = NULL;
62 if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
63 PRINT_ER("pHandle or pvSendBuffer is null\n");
67 if (pHandle->bExiting) {
68 PRINT_ER("pHandle fail\n");
72 /* construct a new message */
73 pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
77 pstrMessage->u32Length = u32SendBufferSize;
78 pstrMessage->pstrNext = NULL;
79 pstrMessage->pvBuffer = kmemdup(pvSendBuffer, u32SendBufferSize,
81 if (!pstrMessage->pvBuffer) {
86 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
88 /* add it to the message queue */
89 if (!pHandle->pstrMessageList) {
90 pHandle->pstrMessageList = pstrMessage;
92 Message *pstrTailMsg = pHandle->pstrMessageList;
94 while (pstrTailMsg->pstrNext)
95 pstrTailMsg = pstrTailMsg->pstrNext;
97 pstrTailMsg->pstrNext = pstrMessage;
100 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
110 * @note copied from FLO glue implementatuion
113 int wilc_mq_recv(WILC_MsgQueueHandle *pHandle,
114 void *pvRecvBuffer, u32 u32RecvBufferSize,
115 u32 *pu32ReceivedLength)
117 Message *pstrMessage;
121 if ((!pHandle) || (u32RecvBufferSize == 0)
122 || (!pvRecvBuffer) || (!pu32ReceivedLength)) {
123 PRINT_ER("pHandle or pvRecvBuffer is null\n");
127 if (pHandle->bExiting) {
128 PRINT_ER("pHandle fail\n");
132 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
133 pHandle->u32ReceiversCount++;
134 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
136 down(&pHandle->hSem);
138 /* other non-timeout scenarios */
140 PRINT_ER("Non-timeout\n");
144 if (pHandle->bExiting) {
145 PRINT_ER("pHandle fail\n");
149 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
151 pstrMessage = pHandle->pstrMessageList;
153 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
154 PRINT_ER("pstrMessage is null\n");
157 /* check buffer size */
158 if (u32RecvBufferSize < pstrMessage->u32Length) {
159 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
161 PRINT_ER("u32RecvBufferSize overflow\n");
165 /* consume the message */
166 pHandle->u32ReceiversCount--;
167 memcpy(pvRecvBuffer, pstrMessage->pvBuffer, pstrMessage->u32Length);
168 *pu32ReceivedLength = pstrMessage->u32Length;
170 pHandle->pstrMessageList = pstrMessage->pstrNext;
172 kfree(pstrMessage->pvBuffer);
175 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);