Staging: csr: update to version 5.1.0 of the driver
[linux-2.6.git] / drivers / staging / csr / csr_util.c
blob939c87c638a199feb5a8450564934fd575377975
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
7 on the license terms.
9 *****************************************************************************/
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <stdarg.h>
15 #include "csr_types.h"
16 #include "csr_pmem.h"
17 #include "csr_util.h"
19 /*------------------------------------------------------------------*/
20 /* Bits */
21 /*------------------------------------------------------------------*/
23 /* Time proportional with the number of 1's */
24 CsrUint8 CsrBitCountSparse(CsrUint32 n)
26 CsrUint8 count = 0;
28 while (n)
30 count++;
31 n &= (n - 1);
34 return count;
37 /* Time proportional with the number of 0's */
38 CsrUint8 CsrBitCountDense(CsrUint32 n)
40 CsrUint8 count = 8 * sizeof(CsrUint32);
42 n ^= (CsrUint32) (-1);
44 while (n)
46 count--;
47 n &= (n - 1);
50 return count;
53 /*------------------------------------------------------------------*/
54 /* Base conversion */
55 /*------------------------------------------------------------------*/
56 CsrBool CsrHexStrToUint8(const CsrCharString *string, CsrUint8 *returnValue)
58 CsrUint16 currentIndex = 0;
59 *returnValue = 0;
60 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
62 string += 2;
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));
69 currentIndex++;
70 if (currentIndex >= 2)
72 break;
75 return TRUE;
77 return FALSE;
80 CsrBool CsrHexStrToUint16(const CsrCharString *string, CsrUint16 *returnValue)
82 CsrUint16 currentIndex = 0;
83 *returnValue = 0;
84 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
86 string += 2;
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));
93 currentIndex++;
94 if (currentIndex >= 4)
96 break;
99 return TRUE;
101 return FALSE;
104 CsrBool CsrHexStrToUint32(const CsrCharString *string, CsrUint32 *returnValue)
106 CsrUint16 currentIndex = 0;
107 *returnValue = 0;
108 if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
110 string += 2;
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);
117 currentIndex++;
118 if (currentIndex >= 8)
120 break;
123 return TRUE;
125 return FALSE;
128 CsrUint32 CsrPow(CsrUint32 base, CsrUint32 exponent)
130 if (exponent == 0)
132 return 1;
134 else
136 CsrUint32 i, t = base;
138 for (i = 1; i < exponent; i++)
140 t = t * base;
142 return t;
146 /* Convert signed 32 bit (or less) integer to string */
147 #define I2B10_MAX 12
148 void CsrIntToBase10(CsrInt32 number, CsrCharString *str)
150 CsrInt32 digit;
151 CsrUint8 index;
152 CsrCharString res[I2B10_MAX];
153 CsrBool foundDigit = FALSE;
155 for (digit = 0; digit < I2B10_MAX; digit++)
157 res[digit] = '\0';
160 /* Catch sign - and deal with positive numbers only afterwards */
161 index = 0;
162 if (number < 0)
164 res[index++] = '-';
165 number = -1 * number;
168 digit = 1000000000;
169 if (number > 0)
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;
182 digit = digit / 10;
185 else
187 res[index] = (char) '0';
190 CsrStrCpy(str, res);
193 void CsrUInt16ToHex(CsrUint16 number, CsrCharString *str)
195 CsrUint16 index;
196 CsrUint16 currentValue;
198 for (index = 0; index < 4; index++)
200 currentValue = (CsrUint16) (number & 0x000F);
201 number >>= 4;
202 str[3 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
204 str[4] = '\0';
207 void CsrUInt32ToHex(CsrUint32 number, CsrCharString *str)
209 CsrUint16 index;
210 CsrUint32 currentValue;
212 for (index = 0; index < 8; index++)
214 currentValue = (CsrUint32) (number & 0x0000000F);
215 number >>= 4;
216 str[7 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
218 str[8] = '\0';
221 /*------------------------------------------------------------------*/
222 /* String */
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)
251 void *buf2 = NULL;
253 if (buf1)
255 buf2 = CsrPmemAlloc(count);
256 CsrMemCpy(buf2, buf1, count);
259 return buf2;
261 #endif
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);
309 #endif
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,
319 CsrSize count)
321 CsrStrNCpy(dest, src, count - 1);
322 dest[count - 1] = '\0';
323 return dest;
326 /* Convert string with base 10 to integer */
327 CsrUint32 CsrStrToInt(const CsrCharString *str)
329 CsrInt16 i;
330 CsrUint32 res;
331 CsrUint32 digit;
333 res = 0;
334 digit = 1;
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');
343 digit = digit * 10;
347 return res;
350 CsrCharString *CsrStrDup(const CsrCharString *string)
352 CsrCharString *copy;
353 CsrUint32 len;
355 copy = NULL;
356 if (string != NULL)
358 len = CsrStrLen(string) + 1;
359 copy = CsrPmemAlloc(len);
360 CsrMemCpy(copy, string, len);
362 return copy;
365 int CsrStrNICmp(const CsrCharString *string1,
366 const CsrCharString *string2,
367 CsrSize count)
369 CsrUint32 index;
370 int returnValue = 0;
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]))
378 returnValue = 1;
380 else
382 returnValue = -1;
384 break;
386 if (string1[index] == '\0')
388 break;
391 return returnValue;
394 const CsrCharString *CsrGetBaseName(const CsrCharString *file)
396 const CsrCharString *pch;
397 static const CsrCharString dotDir[] = ".";
399 if (!file)
401 return NULL;
404 if (file[0] == '\0')
406 return dotDir;
409 pch = file + CsrStrLen(file) - 1;
411 while (*pch != '\\' && *pch != '/' && *pch != ':')
413 if (pch == file)
415 return pch;
417 --pch;
420 return ++pch;
423 /*------------------------------------------------------------------*/
424 /* Misc */
425 /*------------------------------------------------------------------*/
426 CsrBool CsrIsSpace(CsrUint8 c)
428 switch (c)
430 case '\t':
431 case '\n':
432 case '\f':
433 case '\r':
434 case ' ':
435 return TRUE;
436 default:
437 return FALSE;