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>
18 /*------------------------------------------------------------------*/
20 /*------------------------------------------------------------------*/
22 /* Time proportional with the number of 1's */
23 u8 CsrBitCountSparse(u32 n)
36 /* Time proportional with the number of 0's */
37 u8 CsrBitCountDense(u32 n)
39 u8 count = 8 * sizeof(u32);
52 /*------------------------------------------------------------------*/
54 /*------------------------------------------------------------------*/
55 u8 CsrHexStrToUint8(const char *string, u8 *returnValue)
59 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
63 if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
65 while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
67 *returnValue = (u8) (*returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10));
69 if (currentIndex >= 2)
79 u8 CsrHexStrToUint16(const char *string, u16 *returnValue)
83 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
87 if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
89 while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
91 *returnValue = (u16) (*returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10));
93 if (currentIndex >= 4)
103 u8 CsrHexStrToUint32(const char *string, u32 *returnValue)
105 u16 currentIndex = 0;
107 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
111 if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
113 while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
115 *returnValue = *returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10);
117 if (currentIndex >= 8)
127 u32 CsrPow(u32 base, u32 exponent)
137 for (i = 1; i < exponent; i++)
145 /* Convert signed 32 bit (or less) integer to string */
147 void CsrIntToBase10(s32 number, char *str)
152 u8 foundDigit = FALSE;
154 for (digit = 0; digit < I2B10_MAX; digit++)
159 /* Catch sign - and deal with positive numbers only afterwards */
164 number = -1 * number;
170 while ((index < I2B10_MAX - 1) && (digit > 0))
172 /* If the foundDigit flag is TRUE, this routine should be proceeded.
173 Otherwise the number which has '0' digit cannot be converted correctly */
174 if (((number / digit) > 0) || foundDigit)
176 foundDigit = TRUE; /* set foundDigit flag to TRUE*/
177 res[index++] = (char) ('0' + (number / digit));
178 number = number % digit;
186 res[index] = (char) '0';
192 void CsrUInt16ToHex(u16 number, char *str)
197 for (index = 0; index < 4; index++)
199 currentValue = (u16) (number & 0x000F);
201 str[3 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
206 void CsrUInt32ToHex(u32 number, char *str)
211 for (index = 0; index < 8; index++)
213 currentValue = (u32) (number & 0x0000000F);
215 str[7 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
220 /*------------------------------------------------------------------*/
222 /*------------------------------------------------------------------*/
223 #ifndef CSR_USE_STDC_LIB
224 void *CsrMemCpy(void *dest, const void *src, size_t count)
226 return memcpy(dest, src, count);
228 EXPORT_SYMBOL_GPL(CsrMemCpy);
230 void *CsrMemSet(void *dest, u8 c, size_t count)
232 return memset(dest, c, count);
234 EXPORT_SYMBOL_GPL(CsrMemSet);
236 void *CsrMemMove(void *dest, const void *src, size_t count)
238 return memmove(dest, src, count);
240 EXPORT_SYMBOL_GPL(CsrMemMove);
242 s32 CsrMemCmp(const void *buf1, const void *buf2, size_t count)
244 return memcmp(buf1, buf2, count);
246 EXPORT_SYMBOL_GPL(CsrMemCmp);
248 void *CsrMemDup(const void *buf1, size_t count)
254 buf2 = CsrPmemAlloc(count);
255 CsrMemCpy(buf2, buf1, count);
262 #ifndef CSR_USE_STDC_LIB
263 char *CsrStrCpy(char *dest, const char *src)
265 return strcpy(dest, src);
268 char *CsrStrNCpy(char *dest, const char *src, size_t count)
270 return strncpy(dest, src, count);
273 char *CsrStrCat(char *dest, const char *src)
275 return strcat(dest, src);
278 char *CsrStrNCat(char *dest, const char *src, size_t count)
280 return strncat(dest, src, count);
283 char *CsrStrStr(const char *string1, const char *string2)
285 return strstr(string1, string2);
288 size_t CsrStrLen(const char *string)
290 return strlen(string);
292 EXPORT_SYMBOL_GPL(CsrStrLen);
294 s32 CsrStrCmp(const char *string1, const char *string2)
296 return strcmp(string1, string2);
299 s32 CsrStrNCmp(const char *string1, const char *string2, size_t count)
301 return strncmp(string1, string2, count);
304 char *CsrStrChr(const char *string, char c)
306 return strchr(string, c);
310 s32 CsrVsnprintf(char *string, size_t count, const char *format, va_list args)
312 return vsnprintf(string, count, format, args);
314 EXPORT_SYMBOL_GPL(CsrVsnprintf);
316 char *CsrStrNCpyZero(char *dest,
320 CsrStrNCpy(dest, src, count - 1);
321 dest[count - 1] = '\0';
325 /* Convert string with base 10 to integer */
326 u32 CsrStrToInt(const char *str)
335 /* Start from the string end */
336 for (i = (u16) (CsrStrLen(str) - 1); i >= 0; i--)
338 /* Only convert numbers */
339 if ((str[i] >= '0') && (str[i] <= '9'))
341 res += digit * (str[i] - '0');
349 char *CsrStrDup(const char *string)
357 len = CsrStrLen(string) + 1;
358 copy = CsrPmemAlloc(len);
359 CsrMemCpy(copy, string, len);
364 int CsrStrNICmp(const char *string1,
371 for (index = 0; index < count; index++)
373 if (CSR_TOUPPER(string1[index]) != CSR_TOUPPER(string2[index]))
375 if (CSR_TOUPPER(string1[index]) > CSR_TOUPPER(string2[index]))
385 if (string1[index] == '\0')
393 const char *CsrGetBaseName(const char *file)
396 static const char dotDir[] = ".";
408 pch = file + CsrStrLen(file) - 1;
410 while (*pch != '\\' && *pch != '/' && *pch != ':')
422 /*------------------------------------------------------------------*/
424 /*------------------------------------------------------------------*/
440 MODULE_DESCRIPTION("CSR Operating System Kernel Abstraction");
441 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
442 MODULE_LICENSE("GPL and additional rights");