blob: b6c07cfc43d28a960f8e4722d7f4718844f86436 [file] [log] [blame]
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001
2
Johnny Kimc5c77ba2015-05-11 14:30:56 +09003#include "fifo_buffer.h"
4
5
6
Chaehyun Lim4e4467f2015-06-11 14:35:55 +09007u32 FIFO_InitBuffer(tHANDLE *hBuffer, u32 u32BufferLength)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09008{
Chaehyun Lim4e4467f2015-06-11 14:35:55 +09009 u32 u32Error = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090010 tstrFifoHandler *pstrFifoHandler = WILC_MALLOC (sizeof (tstrFifoHandler));
11 if (pstrFifoHandler) {
12 WILC_memset (pstrFifoHandler, 0, sizeof (tstrFifoHandler));
13 pstrFifoHandler->pu8Buffer = WILC_MALLOC (u32BufferLength);
14 if (pstrFifoHandler->pu8Buffer) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +090015 pstrFifoHandler->u32BufferLength = u32BufferLength;
16 WILC_memset (pstrFifoHandler->pu8Buffer, 0, u32BufferLength);
17 /* create semaphore */
Arnd Bergmann83383ea2015-06-01 21:06:43 +020018 sema_init(&pstrFifoHandler->SemBuffer, 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090019 *hBuffer = pstrFifoHandler;
20 } else {
21 *hBuffer = NULL;
22 u32Error = 1;
23 }
24 } else {
25 u32Error = 1;
26 }
27 return u32Error;
28}
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090029u32 FIFO_DeInit(tHANDLE hFifo)
Johnny Kimc5c77ba2015-05-11 14:30:56 +090030{
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090031 u32 u32Error = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090032 tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
33 if (pstrFifoHandler) {
34 if (pstrFifoHandler->pu8Buffer) {
35 WILC_FREE (pstrFifoHandler->pu8Buffer);
36 } else {
37 u32Error = 1;
38 }
39
Johnny Kimc5c77ba2015-05-11 14:30:56 +090040 WILC_FREE (pstrFifoHandler);
41 } else {
42 u32Error = 1;
43 }
44 return u32Error;
45}
46
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090047u32 FIFO_ReadBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToRead, u32 *pu32BytesRead)
Johnny Kimc5c77ba2015-05-11 14:30:56 +090048{
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090049 u32 u32Error = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090050 tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
51 if (pstrFifoHandler && pu32BytesRead) {
52 if (pstrFifoHandler->u32TotalBytes) {
Arnd Bergmann83383ea2015-06-01 21:06:43 +020053 down(&pstrFifoHandler->SemBuffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090054
Arnd Bergmann83383ea2015-06-01 21:06:43 +020055 if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) {
56 *pu32BytesRead = pstrFifoHandler->u32TotalBytes;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090057 } else {
Arnd Bergmann83383ea2015-06-01 21:06:43 +020058 *pu32BytesRead = u32BytesToRead;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090059 }
Arnd Bergmann83383ea2015-06-01 21:06:43 +020060 if ((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) {
61 WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
62 *pu32BytesRead);
63 /* update read offset and total bytes */
64 pstrFifoHandler->u32ReadOffset += u32BytesToRead;
65 pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
66
67 } else {
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090068 u32 u32FirstPart =
Arnd Bergmann83383ea2015-06-01 21:06:43 +020069 pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset;
70 WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
71 u32FirstPart);
72 WILC_memcpy(pu8Buffer + u32FirstPart, pstrFifoHandler->pu8Buffer,
73 u32BytesToRead - u32FirstPart);
74 /* update read offset and total bytes */
75 pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart;
76 pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
77 }
78 up(&pstrFifoHandler->SemBuffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090079 } else {
80 u32Error = 1;
81 }
82 } else {
83 u32Error = 1;
84 }
85 return u32Error;
86}
87
Dean Lee72ed4dc2015-06-12 14:11:44 +090088u32 FIFO_WriteBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToWrite, bool bForceOverWrite)
Johnny Kimc5c77ba2015-05-11 14:30:56 +090089{
Chaehyun Lim4e4467f2015-06-11 14:35:55 +090090 u32 u32Error = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090091 tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
92 if (pstrFifoHandler) {
93 if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) {
94 if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength ||
95 bForceOverWrite) {
Arnd Bergmann83383ea2015-06-01 21:06:43 +020096 down(&pstrFifoHandler->SemBuffer);
97 if ((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) {
98 WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
99 u32BytesToWrite);
100 /* update read offset and total bytes */
101 pstrFifoHandler->u32WriteOffset += u32BytesToWrite;
102 pstrFifoHandler->u32TotalBytes += u32BytesToWrite;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900103
Arnd Bergmann83383ea2015-06-01 21:06:43 +0200104 } else {
Chaehyun Lim4e4467f2015-06-11 14:35:55 +0900105 u32 u32FirstPart =
Arnd Bergmann83383ea2015-06-01 21:06:43 +0200106 pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset;
107 WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
108 u32FirstPart);
109 WILC_memcpy(pstrFifoHandler->pu8Buffer, pu8Buffer + u32FirstPart,
110 u32BytesToWrite - u32FirstPart);
111 /* update read offset and total bytes */
112 pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart;
113 pstrFifoHandler->u32TotalBytes += u32BytesToWrite;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900114 }
Arnd Bergmann83383ea2015-06-01 21:06:43 +0200115 /* if data overwriten */
116 if (pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) {
117 /* adjust read offset to the oldest data available */
118 pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset;
119 /* data availabe is the buffer length */
120 pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength;
121 }
122 up(&pstrFifoHandler->SemBuffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900123 } else {
124 u32Error = 1;
125 }
126 } else {
127 u32Error = 1;
128 }
129 } else {
130 u32Error = 1;
131 }
132 return u32Error;
Arnd Bergmann83383ea2015-06-01 21:06:43 +0200133}