]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/fifo_buffer.c
staging: wilc1000: remove WILC_Uint32
[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 u32 FIFO_InitBuffer(tHANDLE *hBuffer, u32 u32BufferLength)
9 {
10         u32 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                         pstrFifoHandler->u32BufferLength = u32BufferLength;
17                         WILC_memset (pstrFifoHandler->pu8Buffer, 0, u32BufferLength);
18                         /* create semaphore */
19                         sema_init(&pstrFifoHandler->SemBuffer, 1);
20                         *hBuffer = pstrFifoHandler;
21                 } else {
22                         *hBuffer = NULL;
23                         u32Error = 1;
24                 }
25         } else {
26                 u32Error = 1;
27         }
28         return u32Error;
29 }
30 u32 FIFO_DeInit(tHANDLE hFifo)
31 {
32         u32 u32Error = 0;
33         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
34         if (pstrFifoHandler) {
35                 if (pstrFifoHandler->pu8Buffer) {
36                         WILC_FREE (pstrFifoHandler->pu8Buffer);
37                 } else {
38                         u32Error = 1;
39                 }
40
41                 WILC_FREE (pstrFifoHandler);
42         } else {
43                 u32Error = 1;
44         }
45         return u32Error;
46 }
47
48 u32 FIFO_ReadBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToRead, u32 *pu32BytesRead)
49 {
50         u32 u32Error = 0;
51         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
52         if (pstrFifoHandler && pu32BytesRead) {
53                 if (pstrFifoHandler->u32TotalBytes) {
54                         down(&pstrFifoHandler->SemBuffer);
55
56                         if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) {
57                                 *pu32BytesRead = pstrFifoHandler->u32TotalBytes;
58                         } else {
59                                 *pu32BytesRead = u32BytesToRead;
60                         }
61                         if ((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) {
62                                 WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
63                                             *pu32BytesRead);
64                                 /* update read offset and total bytes */
65                                 pstrFifoHandler->u32ReadOffset += u32BytesToRead;
66                                 pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
67
68                         } else {
69                                 u32 u32FirstPart =
70                                         pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset;
71                                 WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset,
72                                             u32FirstPart);
73                                 WILC_memcpy(pu8Buffer + u32FirstPart, pstrFifoHandler->pu8Buffer,
74                                             u32BytesToRead - u32FirstPart);
75                                 /* update read offset and total bytes */
76                                 pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart;
77                                 pstrFifoHandler->u32TotalBytes -= u32BytesToRead;
78                         }
79                         up(&pstrFifoHandler->SemBuffer);
80                 } else {
81                         u32Error = 1;
82                 }
83         } else {
84                 u32Error = 1;
85         }
86         return u32Error;
87 }
88
89 u32 FIFO_WriteBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToWrite, WILC_Bool bForceOverWrite)
90 {
91         u32 u32Error = 0;
92         tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo;
93         if (pstrFifoHandler) {
94                 if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) {
95                         if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength ||
96                             bForceOverWrite) {
97                                 down(&pstrFifoHandler->SemBuffer);
98                                 if ((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) {
99                                         WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
100                                                     u32BytesToWrite);
101                                         /* update read offset and total bytes */
102                                         pstrFifoHandler->u32WriteOffset += u32BytesToWrite;
103                                         pstrFifoHandler->u32TotalBytes  += u32BytesToWrite;
104
105                                 } else {
106                                         u32 u32FirstPart =
107                                                 pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset;
108                                         WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer,
109                                                     u32FirstPart);
110                                         WILC_memcpy(pstrFifoHandler->pu8Buffer, pu8Buffer + u32FirstPart,
111                                                     u32BytesToWrite - u32FirstPart);
112                                         /* update read offset and total bytes */
113                                         pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart;
114                                         pstrFifoHandler->u32TotalBytes += u32BytesToWrite;
115                                 }
116                                 /* if data overwriten */
117                                 if (pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) {
118                                         /* adjust read offset to the oldest data available */
119                                         pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset;
120                                         /* data availabe is the buffer length */
121                                         pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength;
122                                 }
123                                 up(&pstrFifoHandler->SemBuffer);
124                         } else {
125                                 u32Error = 1;
126                         }
127                 } else {
128                         u32Error = 1;
129                 }
130         } else {
131                 u32Error = 1;
132         }
133         return u32Error;
134 }