b996c47af15d423fc2f3ed6b32dead48c9501341
[deliverable/linux.git] / drivers / staging / wilc1000 / wilc_msgqueue.c
1
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>
7
8 /*!
9 * @author syounan
10 * @date 1 Sep 2010
11 * @note copied from FLO glue implementatuion
12 * @version 1.0
13 */
14 int wilc_mq_create(struct message_queue *pHandle)
15 {
16 spin_lock_init(&pHandle->strCriticalSection);
17 sema_init(&pHandle->sem, 0);
18 pHandle->pstrMessageList = NULL;
19 pHandle->u32ReceiversCount = 0;
20 pHandle->bExiting = false;
21 return 0;
22 }
23
24 /*!
25 * @author syounan
26 * @date 1 Sep 2010
27 * @note copied from FLO glue implementatuion
28 * @version 1.0
29 */
30 int wilc_mq_destroy(struct message_queue *pHandle)
31 {
32 pHandle->bExiting = true;
33
34 /* Release any waiting receiver thread. */
35 while (pHandle->u32ReceiversCount > 0) {
36 up(&pHandle->sem);
37 pHandle->u32ReceiversCount--;
38 }
39
40 while (pHandle->pstrMessageList) {
41 struct message *pstrMessge = pHandle->pstrMessageList->next;
42
43 kfree(pHandle->pstrMessageList);
44 pHandle->pstrMessageList = pstrMessge;
45 }
46
47 return 0;
48 }
49
50 /*!
51 * @author syounan
52 * @date 1 Sep 2010
53 * @note copied from FLO glue implementatuion
54 * @version 1.0
55 */
56 int wilc_mq_send(struct message_queue *pHandle,
57 const void *pvSendBuffer, u32 u32SendBufferSize)
58 {
59 unsigned long flags;
60 struct message *pstrMessage = NULL;
61
62 if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
63 PRINT_ER("pHandle or pvSendBuffer is null\n");
64 return -EFAULT;
65 }
66
67 if (pHandle->bExiting) {
68 PRINT_ER("pHandle fail\n");
69 return -EFAULT;
70 }
71
72 /* construct a new message */
73 pstrMessage = kmalloc(sizeof(struct message), GFP_ATOMIC);
74 if (!pstrMessage)
75 return -ENOMEM;
76
77 pstrMessage->len = u32SendBufferSize;
78 pstrMessage->next = NULL;
79 pstrMessage->buf = kmemdup(pvSendBuffer, u32SendBufferSize,
80 GFP_ATOMIC);
81 if (!pstrMessage->buf) {
82 kfree(pstrMessage);
83 return -ENOMEM;
84 }
85
86 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
87
88 /* add it to the message queue */
89 if (!pHandle->pstrMessageList) {
90 pHandle->pstrMessageList = pstrMessage;
91 } else {
92 struct message *pstrTailMsg = pHandle->pstrMessageList;
93
94 while (pstrTailMsg->next)
95 pstrTailMsg = pstrTailMsg->next;
96
97 pstrTailMsg->next = pstrMessage;
98 }
99
100 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
101
102 up(&pHandle->sem);
103
104 return 0;
105 }
106
107 /*!
108 * @author syounan
109 * @date 1 Sep 2010
110 * @note copied from FLO glue implementatuion
111 * @version 1.0
112 */
113 int wilc_mq_recv(struct message_queue *pHandle,
114 void *pvRecvBuffer, u32 u32RecvBufferSize,
115 u32 *pu32ReceivedLength)
116 {
117 struct message *pstrMessage;
118 unsigned long flags;
119
120 if ((!pHandle) || (u32RecvBufferSize == 0)
121 || (!pvRecvBuffer) || (!pu32ReceivedLength)) {
122 PRINT_ER("pHandle or pvRecvBuffer is null\n");
123 return -EINVAL;
124 }
125
126 if (pHandle->bExiting) {
127 PRINT_ER("pHandle fail\n");
128 return -EFAULT;
129 }
130
131 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
132 pHandle->u32ReceiversCount++;
133 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
134
135 down(&pHandle->sem);
136 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
137
138 pstrMessage = pHandle->pstrMessageList;
139 if (!pstrMessage) {
140 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
141 PRINT_ER("pstrMessage is null\n");
142 return -EFAULT;
143 }
144 /* check buffer size */
145 if (u32RecvBufferSize < pstrMessage->len) {
146 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
147 up(&pHandle->sem);
148 PRINT_ER("u32RecvBufferSize overflow\n");
149 return -EOVERFLOW;
150 }
151
152 /* consume the message */
153 pHandle->u32ReceiversCount--;
154 memcpy(pvRecvBuffer, pstrMessage->buf, pstrMessage->len);
155 *pu32ReceivedLength = pstrMessage->len;
156
157 pHandle->pstrMessageList = pstrMessage->next;
158
159 kfree(pstrMessage->buf);
160 kfree(pstrMessage);
161
162 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
163
164 return 0;
165 }
This page took 0.033563 seconds and 4 git commands to generate.