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