Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1 | |
| 2 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 3 | #include "fifo_buffer.h" |
| 4 | |
| 5 | |
| 6 | |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 7 | u32 FIFO_InitBuffer(tHANDLE *hBuffer, u32 u32BufferLength) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 8 | { |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 9 | u32 u32Error = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 10 | 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 Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 15 | pstrFifoHandler->u32BufferLength = u32BufferLength; |
| 16 | WILC_memset (pstrFifoHandler->pu8Buffer, 0, u32BufferLength); |
| 17 | /* create semaphore */ |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 18 | sema_init(&pstrFifoHandler->SemBuffer, 1); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 19 | *hBuffer = pstrFifoHandler; |
| 20 | } else { |
| 21 | *hBuffer = NULL; |
| 22 | u32Error = 1; |
| 23 | } |
| 24 | } else { |
| 25 | u32Error = 1; |
| 26 | } |
| 27 | return u32Error; |
| 28 | } |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 29 | u32 FIFO_DeInit(tHANDLE hFifo) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 30 | { |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 31 | u32 u32Error = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 32 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
| 33 | if (pstrFifoHandler) { |
| 34 | if (pstrFifoHandler->pu8Buffer) { |
| 35 | WILC_FREE (pstrFifoHandler->pu8Buffer); |
| 36 | } else { |
| 37 | u32Error = 1; |
| 38 | } |
| 39 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 40 | WILC_FREE (pstrFifoHandler); |
| 41 | } else { |
| 42 | u32Error = 1; |
| 43 | } |
| 44 | return u32Error; |
| 45 | } |
| 46 | |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 47 | u32 FIFO_ReadBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToRead, u32 *pu32BytesRead) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 48 | { |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 49 | u32 u32Error = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 50 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
| 51 | if (pstrFifoHandler && pu32BytesRead) { |
| 52 | if (pstrFifoHandler->u32TotalBytes) { |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 53 | down(&pstrFifoHandler->SemBuffer); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 54 | |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 55 | if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) { |
| 56 | *pu32BytesRead = pstrFifoHandler->u32TotalBytes; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 57 | } else { |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 58 | *pu32BytesRead = u32BytesToRead; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 59 | } |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 60 | 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 Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 68 | u32 u32FirstPart = |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 69 | 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 Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 79 | } else { |
| 80 | u32Error = 1; |
| 81 | } |
| 82 | } else { |
| 83 | u32Error = 1; |
| 84 | } |
| 85 | return u32Error; |
| 86 | } |
| 87 | |
Dean Lee | 72ed4dc | 2015-06-12 14:11:44 +0900 | [diff] [blame] | 88 | u32 FIFO_WriteBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToWrite, bool bForceOverWrite) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 89 | { |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 90 | u32 u32Error = 0; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 91 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
| 92 | if (pstrFifoHandler) { |
| 93 | if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) { |
| 94 | if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength || |
| 95 | bForceOverWrite) { |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 96 | 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 Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 103 | |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 104 | } else { |
Chaehyun Lim | 4e4467f | 2015-06-11 14:35:55 +0900 | [diff] [blame] | 105 | u32 u32FirstPart = |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 106 | 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 Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 114 | } |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 115 | /* 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 Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 123 | } else { |
| 124 | u32Error = 1; |
| 125 | } |
| 126 | } else { |
| 127 | u32Error = 1; |
| 128 | } |
| 129 | } else { |
| 130 | u32Error = 1; |
| 131 | } |
| 132 | return u32Error; |
Arnd Bergmann | 83383ea | 2015-06-01 21:06:43 +0200 | [diff] [blame] | 133 | } |