| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2008, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bvbi_util_priv.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 12/3/08 7:49p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/portinginterface/vbi/7400/bvbi_util_priv.c $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/1 12/3/08 7:49p darnstein |
|---|
| 21 | * PR45819: Source files for 7400 are no longer symbolic links. |
|---|
| 22 | * |
|---|
| 23 | * Hydra_Software_Devel/1 9/7/07 5:06p darnstein |
|---|
| 24 | * PR25708: Miscellaneous support routines, not for public use. |
|---|
| 25 | * |
|---|
| 26 | ***************************************************************************/ |
|---|
| 27 | |
|---|
| 28 | #include "bvbi_util_priv.h" |
|---|
| 29 | #include "bvbi_priv.h" |
|---|
| 30 | #include "bkni.h" |
|---|
| 31 | |
|---|
| 32 | BDBG_MODULE(BVBI); |
|---|
| 33 | |
|---|
| 34 | /* |
|---|
| 35 | * Handle processing |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | /* The LineBuilder object implements a double buffer. This struct defines |
|---|
| 39 | properties common to both buffers. */ |
|---|
| 40 | typedef struct P_Bank |
|---|
| 41 | { |
|---|
| 42 | uint8_t* rawData; /* The raw data being stored/buffered. */ |
|---|
| 43 | size_t accumLength; /* How many bytes have been accumulated. */ |
|---|
| 44 | int sequenceNumber; /* A trigger that causes accumulation to reset. |
|---|
| 45 | If zero, indicates there is no valid data. */ |
|---|
| 46 | int lineNumber; /* Another trigger that causes accumulation |
|---|
| 47 | to reset. */ |
|---|
| 48 | |
|---|
| 49 | } P_Bank; |
|---|
| 50 | |
|---|
| 51 | typedef struct BVBI_P_LineBuilder_Handle |
|---|
| 52 | { |
|---|
| 53 | /* Black magic */ |
|---|
| 54 | uint32_t ulBlackMagic; |
|---|
| 55 | |
|---|
| 56 | /* Needed to allocate memory */ |
|---|
| 57 | BMEM_Handle hMem; |
|---|
| 58 | |
|---|
| 59 | /* Usable line length in bytes, set at creation time. */ |
|---|
| 60 | size_t lineCount; |
|---|
| 61 | |
|---|
| 62 | /* Physical line length in bytes, set at creation time. */ |
|---|
| 63 | size_t lineSize; |
|---|
| 64 | |
|---|
| 65 | /* Which bank is being accumulated */ |
|---|
| 66 | int accumBank; |
|---|
| 67 | |
|---|
| 68 | /* Raw storage, double buffered. */ |
|---|
| 69 | P_Bank bank[2]; |
|---|
| 70 | |
|---|
| 71 | } BVBI_P_LineBuilder_Handle; |
|---|
| 72 | |
|---|
| 73 | /* Convenience function to translate from public handle to private context */ |
|---|
| 74 | #define BVBI_P_GET_LINEBUILDER_CONTEXT(handle, context) \ |
|---|
| 75 | BVBI_P_GENERIC_GET_CONTEXT(handle, context, BVBI_P_LineBuilder_Handle) |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /*************************************************************************** |
|---|
| 79 | * |
|---|
| 80 | */ |
|---|
| 81 | BERR_Code BVBI_LineBuilder_Open ( |
|---|
| 82 | BVBI_LineBuilder_Handle* pHandle, |
|---|
| 83 | BMEM_Handle hMem, size_t lineCount, size_t lineSize) |
|---|
| 84 | { |
|---|
| 85 | |
|---|
| 86 | int iBank; |
|---|
| 87 | size_t iEntry; |
|---|
| 88 | BVBI_P_LineBuilder_Handle* context; |
|---|
| 89 | |
|---|
| 90 | BDBG_ENTER(BVBI_LineBuilder_Open); |
|---|
| 91 | |
|---|
| 92 | /* A few sanity checks */ |
|---|
| 93 | if(!pHandle) |
|---|
| 94 | { |
|---|
| 95 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 96 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 97 | } |
|---|
| 98 | if ((lineSize == 0) || |
|---|
| 99 | (lineCount == 0) || |
|---|
| 100 | (lineSize < lineCount)) |
|---|
| 101 | { |
|---|
| 102 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 103 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /* Alloc the main context. */ |
|---|
| 107 | context = (BVBI_P_LineBuilder_Handle*) |
|---|
| 108 | (BKNI_Malloc(sizeof(BVBI_P_LineBuilder_Handle))); |
|---|
| 109 | |
|---|
| 110 | if(!context) |
|---|
| 111 | { |
|---|
| 112 | return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /* Store attributes provided by user */ |
|---|
| 116 | context->hMem = hMem; |
|---|
| 117 | context->lineCount = lineCount; |
|---|
| 118 | context->lineSize = lineSize; |
|---|
| 119 | |
|---|
| 120 | /* Two allocations to go. Avoid memory leaks after failure. */ |
|---|
| 121 | context->bank[0].rawData = |
|---|
| 122 | (uint8_t*)(BMEM_AllocAligned ( |
|---|
| 123 | context->hMem, |
|---|
| 124 | lineSize, |
|---|
| 125 | 4, |
|---|
| 126 | 0)); |
|---|
| 127 | if (!(context->bank[0].rawData)) |
|---|
| 128 | { |
|---|
| 129 | BKNI_Free (context); |
|---|
| 130 | return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 131 | } |
|---|
| 132 | context->bank[1].rawData = |
|---|
| 133 | (uint8_t*)(BMEM_AllocAligned ( |
|---|
| 134 | context->hMem, |
|---|
| 135 | lineSize, |
|---|
| 136 | 4, |
|---|
| 137 | 0)); |
|---|
| 138 | if (!(context->bank[1].rawData)) |
|---|
| 139 | { |
|---|
| 140 | BMEM_Free (context->hMem, context->bank[0].rawData); |
|---|
| 141 | BKNI_Free (context); |
|---|
| 142 | return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* Initialize state */ |
|---|
| 146 | context->accumBank = 0; |
|---|
| 147 | for (iBank = 0 ; iBank < 2 ; ++iBank) |
|---|
| 148 | { |
|---|
| 149 | P_Bank* bank = &(context->bank[iBank]); |
|---|
| 150 | bank->accumLength = 0; |
|---|
| 151 | bank->sequenceNumber = 0; |
|---|
| 152 | bank->lineNumber = 0; |
|---|
| 153 | for (iEntry = lineCount ; iEntry < lineSize ; ++iEntry) |
|---|
| 154 | bank->rawData[iEntry] = 0; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /* Set magic number to the size of the struct */ |
|---|
| 158 | context->ulBlackMagic = sizeof(BVBI_P_LineBuilder_Handle); |
|---|
| 159 | |
|---|
| 160 | /* All done. now return the new context to user. */ |
|---|
| 161 | *pHandle = (BVBI_LineBuilder_Handle)context; |
|---|
| 162 | |
|---|
| 163 | BDBG_LEAVE(BVBI_LineBuilder_Open); |
|---|
| 164 | return BERR_SUCCESS; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | /*************************************************************************** |
|---|
| 169 | * |
|---|
| 170 | */ |
|---|
| 171 | void BVBI_LineBuilder_Close (BVBI_LineBuilder_Handle handle) |
|---|
| 172 | { |
|---|
| 173 | BVBI_P_LineBuilder_Handle* context; |
|---|
| 174 | int iBank; |
|---|
| 175 | |
|---|
| 176 | BDBG_ENTER (BVBI_LineBuilder_Close); |
|---|
| 177 | |
|---|
| 178 | /* check parameters */ |
|---|
| 179 | BVBI_P_GET_LINEBUILDER_CONTEXT(handle, context); |
|---|
| 180 | BDBG_ASSERT (context != NULL); |
|---|
| 181 | |
|---|
| 182 | /* Wipe out bank storage */ |
|---|
| 183 | for (iBank = 0 ; iBank < 2 ; ++iBank) |
|---|
| 184 | BMEM_Free (context->hMem, context->bank[iBank].rawData); |
|---|
| 185 | |
|---|
| 186 | /* The handle is invalid */ |
|---|
| 187 | context->ulBlackMagic = 0; |
|---|
| 188 | |
|---|
| 189 | /* Release context in system memory */ |
|---|
| 190 | BKNI_Free((void*)context); |
|---|
| 191 | |
|---|
| 192 | BDBG_LEAVE (BVBI_LineBuilder_Close); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | /*************************************************************************** |
|---|
| 197 | * |
|---|
| 198 | */ |
|---|
| 199 | BERR_Code BVBI_LineBuilder_Put ( |
|---|
| 200 | BVBI_LineBuilder_Handle handle, uint8_t* sectionData, size_t sectionSize, |
|---|
| 201 | size_t sectionOffset, int sequenceNumber, int lineNumber) |
|---|
| 202 | { |
|---|
| 203 | BVBI_P_LineBuilder_Handle* context; |
|---|
| 204 | P_Bank* bank; |
|---|
| 205 | |
|---|
| 206 | BDBG_ENTER (BVBI_LineBuilder_Put); |
|---|
| 207 | |
|---|
| 208 | /* check parameters */ |
|---|
| 209 | BVBI_P_GET_LINEBUILDER_CONTEXT(handle, context); |
|---|
| 210 | if (!context) |
|---|
| 211 | { |
|---|
| 212 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 213 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /* Will work on the accumulation bank, mostly. */ |
|---|
| 217 | bank = &context->bank[context->accumBank]; |
|---|
| 218 | |
|---|
| 219 | /* Debug code: if non-accumulation bank is complete, freeze everything! |
|---|
| 220 | if (context->bank[1-context->accumBank].sequenceNumber) |
|---|
| 221 | return BERR_SUCCESS; |
|---|
| 222 | */ |
|---|
| 223 | |
|---|
| 224 | /* Special case: forced reset. */ |
|---|
| 225 | if (sequenceNumber == 0) |
|---|
| 226 | { |
|---|
| 227 | bank->sequenceNumber = 0; |
|---|
| 228 | bank->lineNumber = 0; |
|---|
| 229 | bank->accumLength = 0; |
|---|
| 230 | bank = &context->bank[1 - context->accumBank]; |
|---|
| 231 | bank->sequenceNumber = 0; |
|---|
| 232 | bank->lineNumber = 0; |
|---|
| 233 | bank->accumLength = 0; |
|---|
| 234 | BDBG_LEAVE (BVBI_LineBuilder_Put); |
|---|
| 235 | return BERR_SUCCESS; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /* Can the new data be added? */ |
|---|
| 239 | if ((sequenceNumber == bank->sequenceNumber) && |
|---|
| 240 | (lineNumber == bank->lineNumber ) && |
|---|
| 241 | (sectionOffset == bank->accumLength ) ) |
|---|
| 242 | { |
|---|
| 243 | /* Slide the new data in at the end */ |
|---|
| 244 | if (bank->accumLength + sectionSize > context->lineCount) |
|---|
| 245 | { |
|---|
| 246 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 247 | BDBG_LEAVE (BVBI_LineBuilder_Put); |
|---|
| 248 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 249 | } |
|---|
| 250 | BKNI_Memcpy ( |
|---|
| 251 | bank->rawData + sectionOffset, sectionData, sectionSize); |
|---|
| 252 | bank->accumLength += sectionSize; |
|---|
| 253 | } |
|---|
| 254 | else |
|---|
| 255 | { |
|---|
| 256 | /* Replace data completely, it does not match the new data */ |
|---|
| 257 | if (sectionOffset == 0) |
|---|
| 258 | { |
|---|
| 259 | if (sectionSize > context->lineCount) |
|---|
| 260 | { |
|---|
| 261 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 262 | BDBG_LEAVE (BVBI_LineBuilder_Put); |
|---|
| 263 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 264 | } |
|---|
| 265 | BKNI_Memcpy (bank->rawData, sectionData, sectionSize); |
|---|
| 266 | bank->accumLength = sectionSize; |
|---|
| 267 | bank->sequenceNumber = sequenceNumber; |
|---|
| 268 | bank->lineNumber = lineNumber; |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | bank->accumLength = 0; |
|---|
| 273 | bank->sequenceNumber = 0; |
|---|
| 274 | bank->lineNumber = 0; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | /* If the accumulation bank is full, start using it for output. */ |
|---|
| 279 | if (bank->accumLength == context->lineCount) |
|---|
| 280 | { |
|---|
| 281 | context->accumBank = 1 - context->accumBank; |
|---|
| 282 | bank = &context->bank[context->accumBank]; |
|---|
| 283 | bank->sequenceNumber = 0; |
|---|
| 284 | bank->lineNumber = 0; |
|---|
| 285 | bank->accumLength = 0; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | BDBG_LEAVE (BVBI_LineBuilder_Put); |
|---|
| 289 | return BERR_SUCCESS; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | /*************************************************************************** |
|---|
| 294 | * |
|---|
| 295 | */ |
|---|
| 296 | BERR_Code BVBI_LineBuilder_Get ( |
|---|
| 297 | BVBI_LineBuilder_Handle handle, uint8_t** pLineData, int* pSequenceNumber, |
|---|
| 298 | int* pLineNumber) |
|---|
| 299 | { |
|---|
| 300 | BVBI_P_LineBuilder_Handle* context; |
|---|
| 301 | P_Bank* bank; |
|---|
| 302 | |
|---|
| 303 | BDBG_ENTER (BVBI_LineBuilder_Get); |
|---|
| 304 | |
|---|
| 305 | /* check parameters */ |
|---|
| 306 | BVBI_P_GET_LINEBUILDER_CONTEXT(handle, context); |
|---|
| 307 | if (!context) |
|---|
| 308 | { |
|---|
| 309 | BDBG_ERR(("Invalid parameter\n")); |
|---|
| 310 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /* Will work on the non-accumulation bank */ |
|---|
| 314 | bank = &context->bank[1 - context->accumBank]; |
|---|
| 315 | |
|---|
| 316 | /* Point to data, or lack thereof. */ |
|---|
| 317 | *pLineData = bank->rawData; |
|---|
| 318 | *pSequenceNumber = bank->sequenceNumber; |
|---|
| 319 | *pLineNumber = bank->lineNumber; |
|---|
| 320 | |
|---|
| 321 | BDBG_LEAVE (BVBI_LineBuilder_Get); |
|---|
| 322 | return BERR_SUCCESS; |
|---|
| 323 | } |
|---|