2 * Misc useful os-independent macros and functions.
4 * Copyright 2006, Broadcom Corporation
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 $
17 /* ** driver-only section ** */
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) */
29 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
31 extern unsigned char bcm_ctype
[];
32 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
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)
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.
51 #define SPINWAIT(exp, us) { \
52 uint countdown = (us) + 9; \
53 while ((exp) && (countdown >= 10)) {\
61 } __attribute__((packed
));
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
);
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
);
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
);
82 #define bcm_perf_enable()
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
);
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.
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 */
107 /* varid definitions are per-driver, may use these get/set bits */
109 /* IOVar action bits for id mapping */
110 #define IOV_GET 0 /* Get an iovar */
111 #define IOV_SET 1 /* Set an iovar */
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)
118 /* flags are per-driver based on driver attributes */
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 */
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
);
134 #endif /* #ifdef BCMDRIVER */
136 /* ** driver/apps-shared section ** */
138 #define BCME_STRLEN 64 /* Max string length for BCM errors */
139 #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST))
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
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
189 /* These are collection of BCME Error strings */
190 #define BCMERRSTRINGTABLE { \
203 "Bad Rate valueset", \
205 "Buffer too short", \
210 "Out of Range Channel", \
213 "Not Enough Resources", \
224 "ACM Not Supported", \
225 "Not WME Association", \
227 "Dongle Not Accessible" \
231 #define ABS(a) (((a) < 0)?-(a):(a))
235 #define MIN(a, b) (((a) < (b))?(a):(b))
239 #define MAX(a, b) (((a) > (b))?(a):(b))
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]))
250 /* bit map related macros */
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)
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)
267 /* basic mux operation - can be optimized on several architectures */
268 #define MUX(pred, true, false) ((pred) ? (true) : (false))
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)
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))
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))
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))
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 */
296 /* bcm_format_flags() bit description structure */
297 typedef struct bcm_bit_desc
{
302 /* tag_ID/length/value_buffer tuple */
303 typedef struct bcm_tlv
{
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))
312 /* buffer length for ethernet address from bcm_ether_ntoa() */
313 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
315 /* unaligned load and store macros */
320 return ((a
[0] << 24) | (a
[1] << 16) | (a
[2] << 8) | a
[3]);
324 store32_ua(uint8
*a
, uint32 v
)
326 a
[0] = (v
>> 24) & 0xff;
327 a
[1] = (v
>> 16) & 0xff;
328 a
[2] = (v
>> 8) & 0xff;
335 return ((a
[0] << 8) | a
[1]);
339 store16_ua(uint8
*a
, uint16 v
)
341 a
[0] = (v
>> 8) & 0xff;
350 return ((a
[3] << 24) | (a
[2] << 16) | (a
[1] << 8) | a
[0]);
354 store32_ua(uint8
*a
, uint32 v
)
356 a
[3] = (v
>> 24) & 0xff;
357 a
[2] = (v
>> 16) & 0xff;
358 a
[1] = (v
>> 8) & 0xff;
365 return ((a
[1] << 8) | a
[0]);
369 store16_ua(uint8
*a
, uint16 v
)
371 a
[1] = (v
>> 8) & 0xff;
375 #endif /* IL_BIGENDIAN */
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
);
383 extern void printfbig(char *buf
);
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
);
391 extern const char *bcmerrorstr(int bcmerror
);
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)))
400 /* power conversion */
401 extern uint16
bcm_qdbm_to_mw(uint8 qdbm
);
402 extern uint8
bcm_mw_to_qdbm(uint16 mw
);
404 /* generic datastruct to help dump routines */
411 /* Buffer structure for collecting string-formatted data
412 * using bcm_bprintf() API.
413 * Use bcm_binit() to initialize before use
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 */
423 extern void bcm_binit(struct bcmstrbuf
*b
, char *buf
, uint size
);
424 extern int bcm_bprintf(struct bcmstrbuf
*b
, const char *fmt
, ...);
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
);
430 extern uint
bcm_mkiovar(char *name
, char *data
, uint datalen
, char *buf
, uint len
);
431 extern uint
bcm_bitcount(uint8
*bitmap
, uint bytelength
);
433 #endif /* _bcmutils_h_ */
This page took 0.069388 seconds and 5 git commands to generate.