staging: csr: remove csr_types.h
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / csr / csr_util.c
1 /*****************************************************************************
2
3             (c) Cambridge Silicon Radio Limited 2010
4             All rights reserved and confidential information of CSR
5
6             Refer to LICENSE.txt included with this source for details
7             on the license terms.
8
9 *****************************************************************************/
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <stdarg.h>
14
15 #include "csr_pmem.h"
16 #include "csr_util.h"
17
18 /*------------------------------------------------------------------*/
19 /* Bits */
20 /*------------------------------------------------------------------*/
21
22 /* Time proportional with the number of 1's */
23 u8 CsrBitCountSparse(u32 n)
24 {
25     u8 count = 0;
26
27     while (n)
28     {
29         count++;
30         n &= (n - 1);
31     }
32
33     return count;
34 }
35
36 /* Time proportional with the number of 0's */
37 u8 CsrBitCountDense(u32 n)
38 {
39     u8 count = 8 * sizeof(u32);
40
41     n ^= (u32) (-1);
42
43     while (n)
44     {
45         count--;
46         n &= (n - 1);
47     }
48
49     return count;
50 }
51
52 /*------------------------------------------------------------------*/
53 /* Base conversion */
54 /*------------------------------------------------------------------*/
55 u8 CsrHexStrToUint8(const char *string, u8 *returnValue)
56 {
57     u16 currentIndex = 0;
58     *returnValue = 0;
59     if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
60     {
61         string += 2;
62     }
63     if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
64     {
65         while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
66         {
67             *returnValue = (u8) (*returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10));
68             currentIndex++;
69             if (currentIndex >= 2)
70             {
71                 break;
72             }
73         }
74         return TRUE;
75     }
76     return FALSE;
77 }
78
79 u8 CsrHexStrToUint16(const char *string, u16 *returnValue)
80 {
81     u16 currentIndex = 0;
82     *returnValue = 0;
83     if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
84     {
85         string += 2;
86     }
87     if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
88     {
89         while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
90         {
91             *returnValue = (u16) (*returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10));
92             currentIndex++;
93             if (currentIndex >= 4)
94             {
95                 break;
96             }
97         }
98         return TRUE;
99     }
100     return FALSE;
101 }
102
103 u8 CsrHexStrToUint32(const char *string, u32 *returnValue)
104 {
105     u16 currentIndex = 0;
106     *returnValue = 0;
107     if ((string[currentIndex] == '0') && (CSR_TOUPPER(string[currentIndex + 1]) == 'X'))
108     {
109         string += 2;
110     }
111     if (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
112     {
113         while (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) || ((CSR_TOUPPER(string[currentIndex]) >= 'A') && (CSR_TOUPPER(string[currentIndex]) <= 'F')))
114         {
115             *returnValue = *returnValue * 16 + (((string[currentIndex] >= '0') && (string[currentIndex] <= '9')) ? string[currentIndex] - '0' : CSR_TOUPPER(string[currentIndex]) - 'A' + 10);
116             currentIndex++;
117             if (currentIndex >= 8)
118             {
119                 break;
120             }
121         }
122         return TRUE;
123     }
124     return FALSE;
125 }
126
127 u32 CsrPow(u32 base, u32 exponent)
128 {
129     if (exponent == 0)
130     {
131         return 1;
132     }
133     else
134     {
135         u32 i, t = base;
136
137         for (i = 1; i < exponent; i++)
138         {
139             t = t * base;
140         }
141         return t;
142     }
143 }
144
145 /* Convert signed 32 bit (or less) integer to string */
146 #define I2B10_MAX 12
147 void CsrIntToBase10(s32 number, char *str)
148 {
149     s32 digit;
150     u8 index;
151     char res[I2B10_MAX];
152     u8 foundDigit = FALSE;
153
154     for (digit = 0; digit < I2B10_MAX; digit++)
155     {
156         res[digit] = '\0';
157     }
158
159     /* Catch sign - and deal with positive numbers only afterwards */
160     index = 0;
161     if (number < 0)
162     {
163         res[index++] = '-';
164         number = -1 * number;
165     }
166
167     digit = 1000000000;
168     if (number > 0)
169     {
170         while ((index < I2B10_MAX - 1) && (digit > 0))
171         {
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)
175             {
176                 foundDigit = TRUE; /* set foundDigit flag to TRUE*/
177                 res[index++] = (char) ('0' + (number / digit));
178                 number = number % digit;
179             }
180
181             digit = digit / 10;
182         }
183     }
184     else
185     {
186         res[index] = (char) '0';
187     }
188
189     CsrStrCpy(str, res);
190 }
191
192 void CsrUInt16ToHex(u16 number, char *str)
193 {
194     u16 index;
195     u16 currentValue;
196
197     for (index = 0; index < 4; index++)
198     {
199         currentValue = (u16) (number & 0x000F);
200         number >>= 4;
201         str[3 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
202     }
203     str[4] = '\0';
204 }
205
206 void CsrUInt32ToHex(u32 number, char *str)
207 {
208     u16 index;
209     u32 currentValue;
210
211     for (index = 0; index < 8; index++)
212     {
213         currentValue = (u32) (number & 0x0000000F);
214         number >>= 4;
215         str[7 - index] = (char) (currentValue > 9 ? currentValue + 55 : currentValue + '0');
216     }
217     str[8] = '\0';
218 }
219
220 /*------------------------------------------------------------------*/
221 /*  String */
222 /*------------------------------------------------------------------*/
223 #ifndef CSR_USE_STDC_LIB
224 void *CsrMemCpy(void *dest, const void *src, size_t count)
225 {
226     return memcpy(dest, src, count);
227 }
228 EXPORT_SYMBOL_GPL(CsrMemCpy);
229
230 void *CsrMemSet(void *dest, u8 c, size_t count)
231 {
232     return memset(dest, c, count);
233 }
234 EXPORT_SYMBOL_GPL(CsrMemSet);
235
236 void *CsrMemMove(void *dest, const void *src, size_t count)
237 {
238     return memmove(dest, src, count);
239 }
240 EXPORT_SYMBOL_GPL(CsrMemMove);
241
242 s32 CsrMemCmp(const void *buf1, const void *buf2, size_t count)
243 {
244     return memcmp(buf1, buf2, count);
245 }
246 EXPORT_SYMBOL_GPL(CsrMemCmp);
247
248 void *CsrMemDup(const void *buf1, size_t count)
249 {
250     void *buf2 = NULL;
251
252     if (buf1)
253     {
254         buf2 = CsrPmemAlloc(count);
255         CsrMemCpy(buf2, buf1, count);
256     }
257
258     return buf2;
259 }
260 #endif
261
262 #ifndef CSR_USE_STDC_LIB
263 char *CsrStrCpy(char *dest, const char *src)
264 {
265     return strcpy(dest, src);
266 }
267
268 char *CsrStrNCpy(char *dest, const char *src, size_t count)
269 {
270     return strncpy(dest, src, count);
271 }
272
273 char *CsrStrCat(char *dest, const char *src)
274 {
275     return strcat(dest, src);
276 }
277
278 char *CsrStrNCat(char *dest, const char *src, size_t count)
279 {
280     return strncat(dest, src, count);
281 }
282
283 char *CsrStrStr(const char *string1, const char *string2)
284 {
285     return strstr(string1, string2);
286 }
287
288 size_t CsrStrLen(const char *string)
289 {
290     return strlen(string);
291 }
292 EXPORT_SYMBOL_GPL(CsrStrLen);
293
294 s32 CsrStrCmp(const char *string1, const char *string2)
295 {
296     return strcmp(string1, string2);
297 }
298
299 s32 CsrStrNCmp(const char *string1, const char *string2, size_t count)
300 {
301     return strncmp(string1, string2, count);
302 }
303
304 char *CsrStrChr(const char *string, char c)
305 {
306     return strchr(string, c);
307 }
308 #endif
309
310 s32 CsrVsnprintf(char *string, size_t count, const char *format, va_list args)
311 {
312     return vsnprintf(string, count, format, args);
313 }
314 EXPORT_SYMBOL_GPL(CsrVsnprintf);
315
316 char *CsrStrNCpyZero(char *dest,
317     const char *src,
318     size_t count)
319 {
320     CsrStrNCpy(dest, src, count - 1);
321     dest[count - 1] = '\0';
322     return dest;
323 }
324
325 /* Convert string with base 10 to integer */
326 u32 CsrStrToInt(const char *str)
327 {
328     s16 i;
329     u32 res;
330     u32 digit;
331
332     res = 0;
333     digit = 1;
334
335     /* Start from the string end */
336     for (i = (u16) (CsrStrLen(str) - 1); i >= 0; i--)
337     {
338         /* Only convert numbers */
339         if ((str[i] >= '0') && (str[i] <= '9'))
340         {
341             res += digit * (str[i] - '0');
342             digit = digit * 10;
343         }
344     }
345
346     return res;
347 }
348
349 char *CsrStrDup(const char *string)
350 {
351     char *copy;
352     u32 len;
353
354     copy = NULL;
355     if (string != NULL)
356     {
357         len = CsrStrLen(string) + 1;
358         copy = CsrPmemAlloc(len);
359         CsrMemCpy(copy, string, len);
360     }
361     return copy;
362 }
363
364 int CsrStrNICmp(const char *string1,
365     const char *string2,
366     size_t count)
367 {
368     u32 index;
369     int returnValue = 0;
370
371     for (index = 0; index < count; index++)
372     {
373         if (CSR_TOUPPER(string1[index]) != CSR_TOUPPER(string2[index]))
374         {
375             if (CSR_TOUPPER(string1[index]) > CSR_TOUPPER(string2[index]))
376             {
377                 returnValue = 1;
378             }
379             else
380             {
381                 returnValue = -1;
382             }
383             break;
384         }
385         if (string1[index] == '\0')
386         {
387             break;
388         }
389     }
390     return returnValue;
391 }
392
393 const char *CsrGetBaseName(const char *file)
394 {
395     const char *pch;
396     static const char dotDir[] = ".";
397
398     if (!file)
399     {
400         return NULL;
401     }
402
403     if (file[0] == '\0')
404     {
405         return dotDir;
406     }
407
408     pch = file + CsrStrLen(file) - 1;
409
410     while (*pch != '\\' && *pch != '/' && *pch != ':')
411     {
412         if (pch == file)
413         {
414             return pch;
415         }
416         --pch;
417     }
418
419     return ++pch;
420 }
421
422 /*------------------------------------------------------------------*/
423 /* Misc */
424 /*------------------------------------------------------------------*/
425 u8 CsrIsSpace(u8 c)
426 {
427     switch (c)
428     {
429         case '\t':
430         case '\n':
431         case '\f':
432         case '\r':
433         case ' ':
434             return TRUE;
435         default:
436             return FALSE;
437     }
438 }
439
440 MODULE_DESCRIPTION("CSR Operating System Kernel Abstraction");
441 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
442 MODULE_LICENSE("GPL and additional rights");