]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/fifo_buffer.c
733d81f2eecaded330ed1621d64039f20ff839bf
[karo-tx-linux.git] / drivers / staging / wilc1000 / fifo_buffer.c
1
2
3 #include "wilc_oswrapper.h"
4 #include "fifo_buffer.h"
5
6
7
8 WILC_Uint32 FIFO_InitBuffer(tHANDLE *hBuffer, WILC_Uint32 u32BufferLength)
9 {
10         WILC_Uint32 u32Error = 0;
11         tstrFifoHandler *pstrFifoHandler = WILC_MALLOC (sizeof (tstrFifoHandler));
12         if (pstrFifoHandler) {
13                 WILC_memset (pstrFifoHandler, 0, sizeof (tstrFifoHandler));
14                 pstrFifoHandler->pu8Buffer = WILC_MALLOC (u32BufferLength);
15                 if (pstrFifoHandler->pu8Buffer) {
16                         tstrWILC_SemaphoreAttrs strSemBufferAttrs;
17                         pstrFifoHandler->u32BufferLength = u32BufferLength;
18                         WILC_memset (pstrFifoHandler->pu8Buffer, 0, u32BufferLength);
19                         /* create semaphore */
20                         WILC_SemaphoreFillDefault (&strSemBufferAttrs);
21                         strSemBufferAttrs.u32InitCount = 1;
22                         WILC_SemaphoreCreate(&pstrFifoHandler->SemBuffer, &strSemBufferAttrs);
23                         *hBuffer = pstrFifoHandler;
24                 } else {
25                         *hBuffer = NULL;
26                         u32Error = 1;
27                 }
28         } else {
29                 u32Error = 1;
30         }
31         return u32Error;
32 }
33 WILC_Uint32 FIFO_DeInit(tHANDLE hFifo)
34 {
35         WILC_Uint32 u32Error = 0;
36         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
37         if (pstrFifoHandler) {
38                 if (pstrFifoHandler->pu8Buffer) {
39                         WILC_FREE (pstrFifoHandler->pu8Buffer);
40                 } else {
41                         u32Error = 1;
42                 }
43
44                 WILC_SemaphoreDestroy (&pstrFifoHandler->SemBuffer, WILC_NULL);
45
46                 WILC_FREE (pstrFifoHandler);
47         } else {
48                 u32Error = 1;
49         }
50         return u32Error;
51 }
52
53 WILC_Uint32 FIFO_ReadBytes(tHANDLE hFifo, WILC_Uint8 *pu8Buffer, WILC_Uint32 u32BytesToRead, WILC_Uint32 *pu32BytesRead)
54 {
55         WILC_Uint32 u32Error = 0;
56         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
57         if (pstrFifoHandler && pu32BytesRead) {
58                 if (pstrFifoHandler->u32TotalBytes) {
59                         if (WILC_SemaphoreAcquire(&pstrFifoHandler->SemBuffer, WILC_NULL) == WILC_SUCCESS) {
60                                 if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) {
61                                         *pu32BytesRead = pstrFifoHandler->u32TotalBytes;
62                                 } else {
63                                         *pu32BytesRead = u32BytesToRead;
64                                 }
65                                 if ((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) {
66                                         WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
67                                                     *pu32BytesRead);
68                                         /* update read offset and total bytes */
69                                         pstrFifoHandler->u32ReadOffset += u32BytesToRead;
70                                         pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
71
72                                 } else {
73                                         WILC_Uint32 u32FirstPart =
74                                                 pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset;
75                                         WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
76                                                     u32FirstPart);
77                                         WILC_memcpy(pu8Buffer + u32FirstPart, pstrFifoHandler->pu8Buffer,
78                                                     u32BytesToRead - u32FirstPart);
79                                         /* update read offset and total bytes */
80                                         pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart;
81                                         pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
82                                 }
83                                 WILC_SemaphoreRelease (&pstrFifoHandler->SemBuffer, WILC_NULL);
84                         } else {
85                                 u32Error = 1;
86                         }
87                 } else {
88                         u32Error = 1;
89                 }
90         } else {
91                 u32Error = 1;
92         }
93         return u32Error;
94 }
95
96 WILC_Uint32 FIFO_WriteBytes(tHANDLE hFifo, WILC_Uint8 *pu8Buffer, WILC_Uint32 u32BytesToWrite, WILC_Bool bForceOverWrite)
97 {
98         WILC_Uint32 u32Error = 0;
99         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
100         if (pstrFifoHandler) {
101                 if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) {
102                         if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength ||
103                             bForceOverWrite) {
104                                 if (WILC_SemaphoreAcquire(&pstrFifoHandler->SemBuffer, WILC_NULL) == WILC_SUCCESS) {
105                                         if ((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) {
106                                                 WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
107                                                             u32BytesToWrite);
108                                                 /* update read offset and total bytes */
109                                                 pstrFifoHandler->u32WriteOffset += u32BytesToWrite;
110                                                 pstrFifoHandler->u32TotalBytes  += u32BytesToWrite;
111
112                                         } else {
113                                                 WILC_Uint32 u32FirstPart =
114                                                         pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset;
115                                                 WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
116                                                             u32FirstPart);
117                                                 WILC_memcpy(pstrFifoHandler->pu8Buffer, pu8Buffer + u32FirstPart,
118                                                             u32BytesToWrite - u32FirstPart);
119                                                 /* update read offset and total bytes */
120                                                 pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart;
121                                                 pstrFifoHandler->u32TotalBytes += u32BytesToWrite;
122                                         }
123                                         /* if data overwriten */
124                                         if (pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) {
125                                                 /* adjust read offset to the oldest data available */
126                                                 pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset;
127                                                 /* data availabe is the buffer length */
128                                                 pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength;
129                                         }
130                                         WILC_SemaphoreRelease(&pstrFifoHandler->SemBuffer, WILC_NULL);
131                                 }
132                         } else {
133                                 u32Error = 1;
134                         }
135                 } else {
136                         u32Error = 1;
137                 }
138         } else {
139                 u32Error = 1;
140         }
141         return u32Error;
142 }