finally move buildroot-ng to trunk
[openwrt.git] / package / broadcom-wl / src / wlc / 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/apps-shared section ** */
18
19 #define BCME_STRLEN             64      /* Max string length for BCM errors */
20 #define VALID_BCMERROR(e)  ((e <= 0) && (e >= BCME_LAST))
21
22
23 /*
24  * error codes could be added but the defined ones shouldn't be changed/deleted
25  * these error codes are exposed to the user code
26  * when ever a new error code is added to this list
27  * please update errorstring table with the related error string and
28  * update osl files with os specific errorcode map
29 */
30
31 #define BCME_OK                         0       /* Success */
32 #define BCME_ERROR                      -1      /* Error generic */
33 #define BCME_BADARG                     -2      /* Bad Argument */
34 #define BCME_BADOPTION                  -3      /* Bad option */
35 #define BCME_NOTUP                      -4      /* Not up */
36 #define BCME_NOTDOWN                    -5      /* Not down */
37 #define BCME_NOTAP                      -6      /* Not AP */
38 #define BCME_NOTSTA                     -7      /* Not STA  */
39 #define BCME_BADKEYIDX                  -8      /* BAD Key Index */
40 #define BCME_RADIOOFF                   -9      /* Radio Off */
41 #define BCME_NOTBANDLOCKED              -10     /* Not  band locked */
42 #define BCME_NOCLK                      -11     /* No Clock */
43 #define BCME_BADRATESET                 -12     /* BAD Rate valueset */
44 #define BCME_BADBAND                    -13     /* BAD Band */
45 #define BCME_BUFTOOSHORT                -14     /* Buffer too short */
46 #define BCME_BUFTOOLONG                 -15     /* Buffer too long */
47 #define BCME_BUSY                       -16     /* Busy */
48 #define BCME_NOTASSOCIATED              -17     /* Not Associated */
49 #define BCME_BADSSIDLEN                 -18     /* Bad SSID len */
50 #define BCME_OUTOFRANGECHAN             -19     /* Out of Range Channel */
51 #define BCME_BADCHAN                    -20     /* Bad Channel */
52 #define BCME_BADADDR                    -21     /* Bad Address */
53 #define BCME_NORESOURCE                 -22     /* Not Enough Resources */
54 #define BCME_UNSUPPORTED                -23     /* Unsupported */
55 #define BCME_BADLEN                     -24     /* Bad length */
56 #define BCME_NOTREADY                   -25     /* Not Ready */
57 #define BCME_EPERM                      -26     /* Not Permitted */
58 #define BCME_NOMEM                      -27     /* No Memory */
59 #define BCME_ASSOCIATED                 -28     /* Associated */
60 #define BCME_RANGE                      -29     /* Not In Range */
61 #define BCME_NOTFOUND                   -30     /* Not Found */
62 #define BCME_WME_NOT_ENABLED            -31     /* WME Not Enabled */
63 #define BCME_TSPEC_NOTFOUND             -32     /* TSPEC Not Found */
64 #define BCME_ACM_NOTSUPPORTED           -33     /* ACM Not Supported */
65 #define BCME_NOT_WME_ASSOCIATION        -34     /* Not WME Association */
66 #define BCME_SDIO_ERROR                 -35     /* SDIO Bus Error */
67 #define BCME_DONGLE_DOWN                -36     /* Dongle Not Accessible */
68 #define BCME_LAST                       BCME_DONGLE_DOWN
69
70 /* These are collection of BCME Error strings */
71 #define BCMERRSTRINGTABLE {             \
72         "OK",                           \
73         "Undefined error",              \
74         "Bad Argument",                 \
75         "Bad Option",                   \
76         "Not up",                       \
77         "Not down",                     \
78         "Not AP",                       \
79         "Not STA",                      \
80         "Bad Key Index",                \
81         "Radio Off",                    \
82         "Not band locked",              \
83         "No clock",                     \
84         "Bad Rate valueset",            \
85         "Bad Band",                     \
86         "Buffer too short",             \
87         "Buffer too long",              \
88         "Busy",                         \
89         "Not Associated",               \
90         "Bad SSID len",                 \
91         "Out of Range Channel",         \
92         "Bad Channel",                  \
93         "Bad Address",                  \
94         "Not Enough Resources",         \
95         "Unsupported",                  \
96         "Bad length",                   \
97         "Not Ready",                    \
98         "Not Permitted",                \
99         "No Memory",                    \
100         "Associated",                   \
101         "Not In Range",                 \
102         "Not Found",                    \
103         "WME Not Enabled",              \
104         "TSPEC Not Found",              \
105         "ACM Not Supported",            \
106         "Not WME Association",          \
107         "SDIO Bus Error",               \
108         "Dongle Not Accessible"         \
109 }
110
111 #ifndef ABS
112 #define ABS(a)                  (((a) < 0)?-(a):(a))
113 #endif /* ABS */
114
115 #ifndef MIN
116 #define MIN(a, b)               (((a) < (b))?(a):(b))
117 #endif /* MIN */
118
119 #ifndef MAX
120 #define MAX(a, b)               (((a) > (b))?(a):(b))
121 #endif /* MAX */
122
123 #define CEIL(x, y)              (((x) + ((y)-1)) / (y))
124 #define ROUNDUP(x, y)           ((((x)+((y)-1))/(y))*(y))
125 #define ISALIGNED(a, x)         (((a) & ((x)-1)) == 0)
126 #define ISPOWEROF2(x)           ((((x)-1)&(x)) == 0)
127 #define VALID_MASK(mask)        !((mask) & ((mask) + 1))
128 #define OFFSETOF(type, member)  ((uint)(uintptr)&((type *)0)->member)
129 #define ARRAYSIZE(a)            (sizeof(a)/sizeof(a[0]))
130
131 /* bit map related macros */
132 #ifndef setbit
133 #ifndef NBBY                /* the BSD family defines NBBY */
134 #define NBBY    8       /* 8 bits per byte */
135 #endif /* #ifndef NBBY */
136 #define setbit(a, i)    (((uint8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
137 #define clrbit(a, i)    (((uint8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
138 #define isset(a, i)     (((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
139 #define isclr(a, i)     ((((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
140 #endif /* setbit */
141
142 #define NBITS(type)     (sizeof(type) * 8)
143 #define NBITVAL(nbits)  (1 << (nbits))
144 #define MAXBITVAL(nbits)        ((1 << (nbits)) - 1)
145 #define NBITMASK(nbits) MAXBITVAL(nbits)
146 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
147
148 /* basic mux operation - can be optimized on several architectures */
149 #define MUX(pred, true, false) ((pred) ? (true) : (false))
150
151 /* modulo inc/dec - assumes x E [0, bound - 1] */
152 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
153 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
154
155 /* modulo inc/dec, bound = 2^k */
156 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
157 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
158
159 /* modulo add/sub - assumes x, y E [0, bound - 1] */
160 #define MODADD(x, y, bound) \
161     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
162 #define MODSUB(x, y, bound) \
163     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
164
165 /* module add/sub, bound = 2^k */
166 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
167 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
168
169 /* crc defines */
170 #define CRC8_INIT_VALUE  0xff           /* Initial CRC8 checksum value */
171 #define CRC8_GOOD_VALUE  0x9f           /* Good final CRC8 checksum value */
172 #define CRC16_INIT_VALUE 0xffff         /* Initial CRC16 checksum value */
173 #define CRC16_GOOD_VALUE 0xf0b8         /* Good final CRC16 checksum value */
174 #define CRC32_INIT_VALUE 0xffffffff     /* Initial CRC32 checksum value */
175 #define CRC32_GOOD_VALUE 0xdebb20e3     /* Good final CRC32 checksum value */
176
177 /* bcm_format_flags() bit description structure */
178 typedef struct bcm_bit_desc {
179         uint32  bit;
180         char*   name;
181 } bcm_bit_desc_t;
182
183 /* tag_ID/length/value_buffer tuple */
184 typedef struct bcm_tlv {
185         uint8   id;
186         uint8   len;
187         uint8   data[1];
188 } bcm_tlv_t;
189
190 /* Check that bcm_tlv_t fits into the given buflen */
191 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
192
193 /* buffer length for ethernet address from bcm_ether_ntoa() */
194 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
195
196 /* unaligned load and store macros */
197 #ifdef IL_BIGENDIAN
198 static INLINE uint32
199 load32_ua(uint8 *a)
200 {
201         return ((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
202 }
203
204 static INLINE void
205 store32_ua(uint8 *a, uint32 v)
206 {
207         a[0] = (v >> 24) & 0xff;
208         a[1] = (v >> 16) & 0xff;
209         a[2] = (v >> 8) & 0xff;
210         a[3] = v & 0xff;
211 }
212
213 static INLINE uint16
214 load16_ua(uint8 *a)
215 {
216         return ((a[0] << 8) | a[1]);
217 }
218
219 static INLINE void
220 store16_ua(uint8 *a, uint16 v)
221 {
222         a[0] = (v >> 8) & 0xff;
223         a[1] = v & 0xff;
224 }
225
226 #else
227
228 static INLINE uint32
229 load32_ua(uint8 *a)
230 {
231         return ((a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]);
232 }
233
234 static INLINE void
235 store32_ua(uint8 *a, uint32 v)
236 {
237         a[3] = (v >> 24) & 0xff;
238         a[2] = (v >> 16) & 0xff;
239         a[1] = (v >> 8) & 0xff;
240         a[0] = v & 0xff;
241 }
242
243 static INLINE uint16
244 load16_ua(uint8 *a)
245 {
246         return ((a[1] << 8) | a[0]);
247 }
248
249 static INLINE void
250 store16_ua(uint8 *a, uint16 v)
251 {
252         a[1] = (v >> 8) & 0xff;
253         a[0] = v & 0xff;
254 }
255
256 #endif /* IL_BIGENDIAN */
257
258 #endif  /* _bcmutils_h_ */