1 /*****************************************************************************
3 (c) Cambridge Silicon Radio Limited 2010
4 All rights reserved and confidential information of CSR
6 Refer to LICENSE.txt included with this source for details
9 *****************************************************************************/
11 #include <linux/kernel.h>
12 #include <linux/module.h>
15 #include "csr_types.h"
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 /* Time proportional with the number of 1's */
24 CsrUint8
CsrBitCountSparse(CsrUint32 n
)
37 /* Time proportional with the number of 0's */
38 CsrUint8
CsrBitCountDense(CsrUint32 n
)
40 CsrUint8 count
= 8 * sizeof(CsrUint32
);
42 n
^= (CsrUint32
) (-1);
53 /*------------------------------------------------------------------*/
55 /*------------------------------------------------------------------*/
56 CsrBool
CsrHexStrToUint8(const CsrCharString
*string
, CsrUint8
*returnValue
)
58 CsrUint16 currentIndex
= 0;
60 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
64 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
66 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
68 *returnValue
= (CsrUint8
) (*returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10));
70 if (currentIndex
>= 2)
80 CsrBool
CsrHexStrToUint16(const CsrCharString
*string
, CsrUint16
*returnValue
)
82 CsrUint16 currentIndex
= 0;
84 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
88 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
90 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
92 *returnValue
= (CsrUint16
) (*returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10));
94 if (currentIndex
>= 4)
104 CsrBool
CsrHexStrToUint32(const CsrCharString
*string
, CsrUint32
*returnValue
)
106 CsrUint16 currentIndex
= 0;
108 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
112 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
114 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F')))
116 *returnValue
= *returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10);
118 if (currentIndex
>= 8)
128 CsrUint32
CsrPow(CsrUint32 base
, CsrUint32 exponent
)
136 CsrUint32 i
, t
= base
;
138 for (i
= 1; i
< exponent
; i
++)
146 /* Convert signed 32 bit (or less) integer to string */
148 void CsrIntToBase10(CsrInt32 number
, CsrCharString
*str
)
152 CsrCharString res
[I2B10_MAX
];
153 CsrBool foundDigit
= FALSE
;
155 for (digit
= 0; digit
< I2B10_MAX
; digit
++)
160 /* Catch sign - and deal with positive numbers only afterwards */
165 number
= -1 * number
;
171 while ((index
< I2B10_MAX
- 1) && (digit
> 0))
173 /* If the foundDigit flag is TRUE, this routine should be proceeded.
174 Otherwise the number which has '0' digit cannot be converted correctly */
175 if (((number
/ digit
) > 0) || foundDigit
)
177 foundDigit
= TRUE
; /* set foundDigit flag to TRUE*/
178 res
[index
++] = (char) ('0' + (number
/ digit
));
179 number
= number
% digit
;
187 res
[index
] = (char) '0';
193 void CsrUInt16ToHex(CsrUint16 number
, CsrCharString
*str
)
196 CsrUint16 currentValue
;
198 for (index
= 0; index
< 4; index
++)
200 currentValue
= (CsrUint16
) (number
& 0x000F);
202 str
[3 - index
] = (char) (currentValue
> 9 ? currentValue
+ 55 : currentValue
+ '0');
207 void CsrUInt32ToHex(CsrUint32 number
, CsrCharString
*str
)
210 CsrUint32 currentValue
;
212 for (index
= 0; index
< 8; index
++)
214 currentValue
= (CsrUint32
) (number
& 0x0000000F);
216 str
[7 - index
] = (char) (currentValue
> 9 ? currentValue
+ 55 : currentValue
+ '0');
221 /*------------------------------------------------------------------*/
223 /*------------------------------------------------------------------*/
224 #ifndef CSR_USE_STDC_LIB
225 void *CsrMemCpy(void *dest
, const void *src
, CsrSize count
)
227 return memcpy(dest
, src
, count
);
229 EXPORT_SYMBOL_GPL(CsrMemCpy
);
231 void *CsrMemSet(void *dest
, CsrUint8 c
, CsrSize count
)
233 return memset(dest
, c
, count
);
235 EXPORT_SYMBOL_GPL(CsrMemSet
);
237 void *CsrMemMove(void *dest
, const void *src
, CsrSize count
)
239 return memmove(dest
, src
, count
);
241 EXPORT_SYMBOL_GPL(CsrMemMove
);
243 CsrInt32
CsrMemCmp(const void *buf1
, const void *buf2
, CsrSize count
)
245 return memcmp(buf1
, buf2
, count
);
247 EXPORT_SYMBOL_GPL(CsrMemCmp
);
249 void *CsrMemDup(const void *buf1
, CsrSize count
)
255 buf2
= CsrPmemAlloc(count
);
256 CsrMemCpy(buf2
, buf1
, count
);
263 #ifndef CSR_USE_STDC_LIB
264 CsrCharString
*CsrStrCpy(CsrCharString
*dest
, const CsrCharString
*src
)
266 return strcpy(dest
, src
);
269 CsrCharString
*CsrStrNCpy(CsrCharString
*dest
, const CsrCharString
*src
, CsrSize count
)
271 return strncpy(dest
, src
, count
);
274 CsrCharString
*CsrStrCat(CsrCharString
*dest
, const CsrCharString
*src
)
276 return strcat(dest
, src
);
279 CsrCharString
*CsrStrNCat(CsrCharString
*dest
, const CsrCharString
*src
, CsrSize count
)
281 return strncat(dest
, src
, count
);
284 CsrCharString
*CsrStrStr(const CsrCharString
*string1
, const CsrCharString
*string2
)
286 return strstr(string1
, string2
);
289 CsrSize
CsrStrLen(const CsrCharString
*string
)
291 return strlen(string
);
293 EXPORT_SYMBOL_GPL(CsrStrLen
);
295 CsrInt32
CsrStrCmp(const CsrCharString
*string1
, const CsrCharString
*string2
)
297 return strcmp(string1
, string2
);
300 CsrInt32
CsrStrNCmp(const CsrCharString
*string1
, const CsrCharString
*string2
, CsrSize count
)
302 return strncmp(string1
, string2
, count
);
305 CsrCharString
*CsrStrChr(const CsrCharString
*string
, CsrCharString c
)
307 return strchr(string
, c
);
311 CsrInt32
CsrVsnprintf(CsrCharString
*string
, CsrSize count
, const CsrCharString
*format
, va_list args
)
313 return vsnprintf(string
, count
, format
, args
);
315 EXPORT_SYMBOL_GPL(CsrVsnprintf
);
317 CsrCharString
*CsrStrNCpyZero(CsrCharString
*dest
,
318 const CsrCharString
*src
,
321 CsrStrNCpy(dest
, src
, count
- 1);
322 dest
[count
- 1] = '\0';
326 /* Convert string with base 10 to integer */
327 CsrUint32
CsrStrToInt(const CsrCharString
*str
)
336 /* Start from the string end */
337 for (i
= (CsrUint16
) (CsrStrLen(str
) - 1); i
>= 0; i
--)
339 /* Only convert numbers */
340 if ((str
[i
] >= '0') && (str
[i
] <= '9'))
342 res
+= digit
* (str
[i
] - '0');
350 CsrCharString
*CsrStrDup(const CsrCharString
*string
)
358 len
= CsrStrLen(string
) + 1;
359 copy
= CsrPmemAlloc(len
);
360 CsrMemCpy(copy
, string
, len
);
365 int CsrStrNICmp(const CsrCharString
*string1
,
366 const CsrCharString
*string2
,
372 for (index
= 0; index
< count
; index
++)
374 if (CSR_TOUPPER(string1
[index
]) != CSR_TOUPPER(string2
[index
]))
376 if (CSR_TOUPPER(string1
[index
]) > CSR_TOUPPER(string2
[index
]))
386 if (string1
[index
] == '\0')
394 const CsrCharString
*CsrGetBaseName(const CsrCharString
*file
)
396 const CsrCharString
*pch
;
397 static const CsrCharString dotDir
[] = ".";
409 pch
= file
+ CsrStrLen(file
) - 1;
411 while (*pch
!= '\\' && *pch
!= '/' && *pch
!= ':')
423 /*------------------------------------------------------------------*/
425 /*------------------------------------------------------------------*/
426 CsrBool
CsrIsSpace(CsrUint8 c
)