convert brcm-2.4 to the new target structure
[openwrt.git] / target / linux / brcm-2.4 / files / arch / mips / bcm947xx / include / bcmutils.h
1 /*
2  * Misc useful os-independent macros and functions.
3  *
4  * Copyright 2006, Broadcom Corporation
5  * All Rights Reserved.
6  * 
7  * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8  * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9  * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10  * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11  * $Id: bcmutils.h,v 1.1.1.16 2006/04/08 06:13:39 honor Exp $
12  */
13
14 #ifndef _bcmutils_h_
15 #define _bcmutils_h_
16
17 /* ** driver-only section ** */
18 #ifdef BCMDRIVER
19
20 #define _BCM_U  0x01    /* upper */
21 #define _BCM_L  0x02    /* lower */
22 #define _BCM_D  0x04    /* digit */
23 #define _BCM_C  0x08    /* cntrl */
24 #define _BCM_P  0x10    /* punct */
25 #define _BCM_S  0x20    /* white space (space/lf/tab) */
26 #define _BCM_X  0x40    /* hex digit */
27 #define _BCM_SP 0x80    /* hard space (0x20) */
28
29 #define GPIO_PIN_NOTDEFINED     0x20    /* Pin not defined */
30
31 extern unsigned char bcm_ctype[];
32 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
33
34 #define bcm_isalnum(c)  ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
35 #define bcm_isalpha(c)  ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
36 #define bcm_iscntrl(c)  ((bcm_ismask(c)&(_BCM_C)) != 0)
37 #define bcm_isdigit(c)  ((bcm_ismask(c)&(_BCM_D)) != 0)
38 #define bcm_isgraph(c)  ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
39 #define bcm_islower(c)  ((bcm_ismask(c)&(_BCM_L)) != 0)
40 #define bcm_isprint(c)  ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
41 #define bcm_ispunct(c)  ((bcm_ismask(c)&(_BCM_P)) != 0)
42 #define bcm_isspace(c)  ((bcm_ismask(c)&(_BCM_S)) != 0)
43 #define bcm_isupper(c)  ((bcm_ismask(c)&(_BCM_U)) != 0)
44 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
45
46 /*
47  * Spin at most 'us' microseconds while 'exp' is true.
48  * Caller should explicitly test 'exp' when this completes
49  * and take appropriate error action if 'exp' is still true.
50  */
51 #define SPINWAIT(exp, us) { \
52         uint countdown = (us) + 9; \
53         while ((exp) && (countdown >= 10)) {\
54                 OSL_DELAY(10); \
55                 countdown -= 10; \
56         } \
57 }
58
59 struct  ether_addr {
60         uint8 octet[6];
61 } __attribute__((packed));
62
63 /* string */
64 extern uchar bcm_toupper(uchar c);
65 extern ulong bcm_strtoul(char *cp, char **endp, uint base);
66 extern char *bcmstrstr(char *haystack, char *needle);
67 extern char *bcmstrcat(char *dest, const char *src);
68 extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
69 /* ethernet address */
70 extern char *bcm_ether_ntoa(struct ether_addr *ea, char *buf);
71 /* variable access */
72 extern char *getvar(char *vars, char *name);
73 extern int getintvar(char *vars, char *name);
74 extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
75 #ifdef BCMPERFSTATS
76 extern void bcm_perf_enable(void);
77 extern void bcmstats(char *fmt);
78 extern void bcmlog(char *fmt, uint a1, uint a2);
79 extern void bcmdumplog(char *buf, int size);
80 extern int bcmdumplogent(char *buf, uint idx);
81 #else
82 #define bcm_perf_enable()
83 #define bcmstats(fmt)
84 #define bcmlog(fmt, a1, a2)
85 #define bcmdumplog(buf, size)   *buf = '\0'
86 #define bcmdumplogent(buf, idx) -1
87 #endif /* BCMPERFSTATS */
88 extern char *bcm_nvram_vars(uint *length);
89 extern int bcm_nvram_cache(void *sbh);
90
91 /* Support for sharing code across in-driver iovar implementations.
92  * The intent is that a driver use this structure to map iovar names
93  * to its (private) iovar identifiers, and the lookup function to
94  * find the entry.  Macros are provided to map ids and get/set actions
95  * into a single number space for a switch statement.
96  */
97
98 /* iovar structure */
99 typedef struct bcm_iovar {
100         const char *name;       /* name for lookup and display */
101         uint16 varid;           /* id for switch */
102         uint16 flags;           /* driver-specific flag bits */
103         uint16 type;            /* base type of argument */
104         uint16 minlen;          /* min length for buffer vars */
105 } bcm_iovar_t;
106
107 /* varid definitions are per-driver, may use these get/set bits */
108
109 /* IOVar action bits for id mapping */
110 #define IOV_GET 0 /* Get an iovar */
111 #define IOV_SET 1 /* Set an iovar */
112
113 /* Varid to actionid mapping */
114 #define IOV_GVAL(id)            ((id)*2)
115 #define IOV_SVAL(id)            (((id)*2)+IOV_SET)
116 #define IOV_ISSET(actionid)     ((actionid & IOV_SET) == IOV_SET)
117
118 /* flags are per-driver based on driver attributes */
119
120 /* Base type definitions */
121 #define IOVT_VOID       0       /* no value (implictly set only) */
122 #define IOVT_BOOL       1       /* any value ok (zero/nonzero) */
123 #define IOVT_INT8       2       /* integer values are range-checked */
124 #define IOVT_UINT8      3       /* unsigned int 8 bits */
125 #define IOVT_INT16      4       /* int 16 bits */
126 #define IOVT_UINT16     5       /* unsigned int 16 bits */
127 #define IOVT_INT32      6       /* int 32 bits */
128 #define IOVT_UINT32     7       /* unsigned int 32 bits */
129 #define IOVT_BUFFER     8       /* buffer is size-checked as per minlen */
130
131 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
132 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, int len, bool set);
133
134 #endif  /* #ifdef BCMDRIVER */
135
136 /* ** driver/apps-shared section ** */
137
138 #define BCME_STRLEN             64      /* Max string length for BCM errors */
139 #define VALID_BCMERROR(e)  ((e <= 0) && (e >= BCME_LAST))
140
141
142 /*
143  * error codes could be added but the defined ones shouldn't be changed/deleted
144  * these error codes are exposed to the user code
145  * when ever a new error code is added to this list
146  * please update errorstring table with the related error string and
147  * update osl files with os specific errorcode map
148 */
149
150 #define BCME_OK                         0       /* Success */
151 #define BCME_ERROR                      -1      /* Error generic */
152 #define BCME_BADARG                     -2      /* Bad Argument */
153 #define BCME_BADOPTION                  -3      /* Bad option */
154 #define BCME_NOTUP                      -4      /* Not up */
155 #define BCME_NOTDOWN                    -5      /* Not down */
156 #define BCME_NOTAP                      -6      /* Not AP */
157 #define BCME_NOTSTA                     -7      /* Not STA  */
158 #define BCME_BADKEYIDX                  -8      /* BAD Key Index */
159 #define BCME_RADIOOFF                   -9      /* Radio Off */
160 #define BCME_NOTBANDLOCKED              -10     /* Not  band locked */
161 #define BCME_NOCLK                      -11     /* No Clock */
162 #define BCME_BADRATESET                 -12     /* BAD Rate valueset */
163 #define BCME_BADBAND                    -13     /* BAD Band */
164 #define BCME_BUFTOOSHORT                -14     /* Buffer too short */
165 #define BCME_BUFTOOLONG                 -15     /* Buffer too long */
166 #define BCME_BUSY                       -16     /* Busy */
167 #define BCME_NOTASSOCIATED              -17     /* Not Associated */
168 #define BCME_BADSSIDLEN                 -18     /* Bad SSID len */
169 #define BCME_OUTOFRANGECHAN             -19     /* Out of Range Channel */
170 #define BCME_BADCHAN                    -20     /* Bad Channel */
171 #define BCME_BADADDR                    -21     /* Bad Address */
172 #define BCME_NORESOURCE                 -22     /* Not Enough Resources */
173 #define BCME_UNSUPPORTED                -23     /* Unsupported */
174 #define BCME_BADLEN                     -24     /* Bad length */
175 #define BCME_NOTREADY                   -25     /* Not Ready */
176 #define BCME_EPERM                      -26     /* Not Permitted */
177 #define BCME_NOMEM                      -27     /* No Memory */
178 #define BCME_ASSOCIATED                 -28     /* Associated */
179 #define BCME_RANGE                      -29     /* Not In Range */
180 #define BCME_NOTFOUND                   -30     /* Not Found */
181 #define BCME_WME_NOT_ENABLED            -31     /* WME Not Enabled */
182 #define BCME_TSPEC_NOTFOUND             -32     /* TSPEC Not Found */
183 #define BCME_ACM_NOTSUPPORTED           -33     /* ACM Not Supported */
184 #define BCME_NOT_WME_ASSOCIATION        -34     /* Not WME Association */
185 #define BCME_SDIO_ERROR                 -35     /* SDIO Bus Error */
186 #define BCME_DONGLE_DOWN                -36     /* Dongle Not Accessible */
187 #define BCME_LAST                       BCME_DONGLE_DOWN
188
189 /* These are collection of BCME Error strings */
190 #define BCMERRSTRINGTABLE {             \
191         "OK",                           \
192         "Undefined error",              \
193         "Bad Argument",                 \
194         "Bad Option",                   \
195         "Not up",                       \
196         "Not down",                     \
197         "Not AP",                       \
198         "Not STA",                      \
199         "Bad Key Index",                \
200         "Radio Off",                    \
201         "Not band locked",              \
202         "No clock",                     \
203         "Bad Rate valueset",            \
204         "Bad Band",                     \
205         "Buffer too short",             \
206         "Buffer too long",              \
207         "Busy",                         \
208         "Not Associated",               \
209         "Bad SSID len",                 \
210         "Out of Range Channel",         \
211         "Bad Channel",                  \
212         "Bad Address",                  \
213         "Not Enough Resources",         \
214         "Unsupported",                  \
215         "Bad length",                   \
216         "Not Ready",                    \
217         "Not Permitted",                \
218         "No Memory",                    \
219         "Associated",                   \
220         "Not In Range",                 \
221         "Not Found",                    \
222         "WME Not Enabled",              \
223         "TSPEC Not Found",              \
224         "ACM Not Supported",            \
225         "Not WME Association",          \
226         "SDIO Bus Error",               \
227         "Dongle Not Accessible"         \
228 }
229
230 #ifndef ABS
231 #define ABS(a)                  (((a) < 0)?-(a):(a))
232 #endif /* ABS */
233
234 #ifndef MIN
235 #define MIN(a, b)               (((a) < (b))?(a):(b))
236 #endif /* MIN */
237
238 #ifndef MAX
239 #define MAX(a, b)               (((a) > (b))?(a):(b))
240 #endif /* MAX */
241
242 #define CEIL(x, y)              (((x) + ((y)-1)) / (y))
243 #define ROUNDUP(x, y)           ((((x)+((y)-1))/(y))*(y))
244 #define ISALIGNED(a, x)         (((a) & ((x)-1)) == 0)
245 #define ISPOWEROF2(x)           ((((x)-1)&(x)) == 0)
246 #define VALID_MASK(mask)        !((mask) & ((mask) + 1))
247 #define OFFSETOF(type, member)  ((uint)(uintptr)&((type *)0)->member)
248 #define ARRAYSIZE(a)            (sizeof(a)/sizeof(a[0]))
249
250 /* bit map related macros */
251 #ifndef setbit
252 #ifndef NBBY                /* the BSD family defines NBBY */
253 #define NBBY    8       /* 8 bits per byte */
254 #endif /* #ifndef NBBY */
255 #define setbit(a, i)    (((uint8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
256 #define clrbit(a, i)    (((uint8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
257 #define isset(a, i)     (((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
258 #define isclr(a, i)     ((((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
259 #endif /* setbit */
260
261 #define NBITS(type)     (sizeof(type) * 8)
262 #define NBITVAL(nbits)  (1 << (nbits))
263 #define MAXBITVAL(nbits)        ((1 << (nbits)) - 1)
264 #define NBITMASK(nbits) MAXBITVAL(nbits)
265 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
266
267 /* basic mux operation - can be optimized on several architectures */
268 #define MUX(pred, true, false) ((pred) ? (true) : (false))
269
270 /* modulo inc/dec - assumes x E [0, bound - 1] */
271 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
272 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
273
274 /* modulo inc/dec, bound = 2^k */
275 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
276 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
277
278 /* modulo add/sub - assumes x, y E [0, bound - 1] */
279 #define MODADD(x, y, bound) \
280     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
281 #define MODSUB(x, y, bound) \
282     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
283
284 /* module add/sub, bound = 2^k */
285 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
286 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
287
288 /* crc defines */
289 #define CRC8_INIT_VALUE  0xff           /* Initial CRC8 checksum value */
290 #define CRC8_GOOD_VALUE  0x9f           /* Good final CRC8 checksum value */
291 #define CRC16_INIT_VALUE 0xffff         /* Initial CRC16 checksum value */
292 #define CRC16_GOOD_VALUE 0xf0b8         /* Good final CRC16 checksum value */
293 #define CRC32_INIT_VALUE 0xffffffff     /* Initial CRC32 checksum value */
294 #define CRC32_GOOD_VALUE 0xdebb20e3     /* Good final CRC32 checksum value */
295
296 /* bcm_format_flags() bit description structure */
297 typedef struct bcm_bit_desc {
298         uint32  bit;
299         char*   name;
300 } bcm_bit_desc_t;
301
302 /* tag_ID/length/value_buffer tuple */
303 typedef struct bcm_tlv {
304         uint8   id;
305         uint8   len;
306         uint8   data[1];
307 } bcm_tlv_t;
308
309 /* Check that bcm_tlv_t fits into the given buflen */
310 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
311
312 /* buffer length for ethernet address from bcm_ether_ntoa() */
313 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
314
315 /* unaligned load and store macros */
316 #ifdef IL_BIGENDIAN
317 static INLINE uint32
318 load32_ua(uint8 *a)
319 {
320         return ((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
321 }
322
323 static INLINE void
324 store32_ua(uint8 *a, uint32 v)
325 {
326         a[0] = (v >> 24) & 0xff;
327         a[1] = (v >> 16) & 0xff;
328         a[2] = (v >> 8) & 0xff;
329         a[3] = v & 0xff;
330 }
331
332 static INLINE uint16
333 load16_ua(uint8 *a)
334 {
335         return ((a[0] << 8) | a[1]);
336 }
337
338 static INLINE void
339 store16_ua(uint8 *a, uint16 v)
340 {
341         a[0] = (v >> 8) & 0xff;
342         a[1] = v & 0xff;
343 }
344
345 #else
346
347 static INLINE uint32
348 load32_ua(uint8 *a)
349 {
350         return ((a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]);
351 }
352
353 static INLINE void
354 store32_ua(uint8 *a, uint32 v)
355 {
356         a[3] = (v >> 24) & 0xff;
357         a[2] = (v >> 16) & 0xff;
358         a[1] = (v >> 8) & 0xff;
359         a[0] = v & 0xff;
360 }
361
362 static INLINE uint16
363 load16_ua(uint8 *a)
364 {
365         return ((a[1] << 8) | a[0]);
366 }
367
368 static INLINE void
369 store16_ua(uint8 *a, uint16 v)
370 {
371         a[1] = (v >> 8) & 0xff;
372         a[0] = v & 0xff;
373 }
374
375 #endif /* IL_BIGENDIAN */
376
377 /* externs */
378 /* crc */
379 extern uint8 hndcrc8(uint8 *p, uint nbytes, uint8 crc);
380 extern uint16 hndcrc16(uint8 *p, uint nbytes, uint16 crc);
381 extern uint32 hndcrc32(uint8 *p, uint nbytes, uint32 crc);
382 /* format/print */
383 extern void printfbig(char *buf);
384
385 /* IE parsing */
386 extern bcm_tlv_t *bcm_next_tlv(bcm_tlv_t *elt, int *buflen);
387 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen, uint key);
388 extern bcm_tlv_t *bcm_parse_ordered_tlvs(void *buf, int buflen, uint key);
389
390 /* bcmerror */
391 extern const char *bcmerrorstr(int bcmerror);
392
393 /* multi-bool data type: set of bools, mbool is true if any is set */
394 typedef uint32 mbool;
395 #define mboolset(mb, bit)               (mb |= bit)             /* set one bool */
396 #define mboolclr(mb, bit)               (mb &= ~bit)            /* clear one bool */
397 #define mboolisset(mb, bit)             ((mb & bit) != 0)       /* TRUE if one bool is set */
398 #define mboolmaskset(mb, mask, val)     ((mb) = (((mb) & ~(mask)) | (val)))
399
400 /* power conversion */
401 extern uint16 bcm_qdbm_to_mw(uint8 qdbm);
402 extern uint8 bcm_mw_to_qdbm(uint16 mw);
403
404 /* generic datastruct to help dump routines */
405 struct fielddesc {
406         char    *nameandfmt;
407         uint32  offset;
408         uint32  len;
409 };
410
411 /* Buffer structure for collecting string-formatted data 
412 * using bcm_bprintf() API.
413 * Use bcm_binit() to initialize before use
414 */
415 struct bcmstrbuf
416 {
417         char *buf;      /* pointer to current position in origbuf */
418         uint size;      /* current (residual) size in bytes */
419         char *origbuf;  /* unmodified pointer to orignal buffer */
420         uint origsize;  /* unmodified orignal buffer size in bytes */
421 };
422
423 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
424 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
425
426 typedef  uint32 (*readreg_rtn)(void *arg0, void *arg1, uint32 offset);
427 extern uint bcmdumpfields(readreg_rtn func_ptr, void *arg0, void *arg1, struct fielddesc *str,
428                           char *buf, uint32 bufsize);
429
430 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf, uint len);
431 extern uint bcm_bitcount(uint8 *bitmap, uint bytelength);
432
433 #endif  /* _bcmutils_h_ */