Home | History | Annotate | Download | only in include
      1 /*
      2  * Misc useful os-independent macros and functions.
      3  *
      4  * Copyright (C) 1999-2011, Broadcom Corporation
      5  *
      6  * Permission to use, copy, modify, and/or distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     13  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     15  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     16  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  *
     18  * $Id: bcmutils.h,v 13.236.2.16 2011-01-26 00:45:06 Exp $
     19  */
     20 
     21 
     22 #ifndef	_bcmutils_h_
     23 #define	_bcmutils_h_
     24 
     25 #define bcm_strcpy_s(dst, noOfElements, src)            strcpy((dst), (src))
     26 #define bcm_strncpy_s(dst, noOfElements, src, count)    strncpy((dst), (src), (count))
     27 #define bcm_strcat_s(dst, noOfElements, src)            strcat((dst), (src))
     28 
     29 #ifdef __cplusplus
     30 extern "C" {
     31 #endif
     32 
     33 
     34 #define _BCM_U	0x01
     35 #define _BCM_L	0x02
     36 #define _BCM_D	0x04
     37 #define _BCM_C	0x08
     38 #define _BCM_P	0x10
     39 #define _BCM_S	0x20
     40 #define _BCM_X	0x40
     41 #define _BCM_SP	0x80
     42 
     43 extern const unsigned char bcm_ctype[];
     44 #define bcm_ismask(x)	(bcm_ctype[(int)(unsigned char)(x)])
     45 
     46 #define bcm_isalnum(c)	((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
     47 #define bcm_isalpha(c)	((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
     48 #define bcm_iscntrl(c)	((bcm_ismask(c)&(_BCM_C)) != 0)
     49 #define bcm_isdigit(c)	((bcm_ismask(c)&(_BCM_D)) != 0)
     50 #define bcm_isgraph(c)	((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
     51 #define bcm_islower(c)	((bcm_ismask(c)&(_BCM_L)) != 0)
     52 #define bcm_isprint(c)	((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
     53 #define bcm_ispunct(c)	((bcm_ismask(c)&(_BCM_P)) != 0)
     54 #define bcm_isspace(c)	((bcm_ismask(c)&(_BCM_S)) != 0)
     55 #define bcm_isupper(c)	((bcm_ismask(c)&(_BCM_U)) != 0)
     56 #define bcm_isxdigit(c)	((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
     57 #define bcm_tolower(c)	(bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
     58 #define bcm_toupper(c)	(bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
     59 
     60 
     61 
     62 struct bcmstrbuf {
     63 	char *buf;
     64 	unsigned int size;
     65 	char *origbuf;
     66 	unsigned int origsize;
     67 };
     68 
     69 
     70 #ifdef BCMDRIVER
     71 #include <osl.h>
     72 
     73 #define GPIO_PIN_NOTDEFINED 	0x20
     74 
     75 
     76 #define SPINWAIT(exp, us) { \
     77 	uint countdown = (us) + 9; \
     78 	while ((exp) && (countdown >= 10)) {\
     79 		OSL_DELAY(10); \
     80 		countdown -= 10; \
     81 	} \
     82 }
     83 
     84 
     85 #ifndef PKTQ_LEN_DEFAULT
     86 #define PKTQ_LEN_DEFAULT        128
     87 #endif
     88 #ifndef PKTQ_MAX_PREC
     89 #define PKTQ_MAX_PREC           16
     90 #endif
     91 
     92 typedef struct pktq_prec {
     93 	void *head;
     94 	void *tail;
     95 	uint16 len;
     96 	uint16 max;
     97 } pktq_prec_t;
     98 
     99 
    100 
    101 struct pktq {
    102 	uint16 num_prec;
    103 	uint16 hi_prec;
    104 	uint16 max;
    105 	uint16 len;
    106 
    107 	struct pktq_prec q[PKTQ_MAX_PREC];
    108 };
    109 
    110 
    111 struct spktq {
    112 	uint16 num_prec;
    113 	uint16 hi_prec;
    114 	uint16 max;
    115 	uint16 len;
    116 
    117 	struct pktq_prec q[1];
    118 };
    119 
    120 #define PKTQ_PREC_ITER(pq, prec)        for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
    121 
    122 
    123 typedef bool (*ifpkt_cb_t)(void*, int);
    124 
    125 #ifdef BCMPKTPOOL
    126 #define POOL_ENAB(pool)		((pool) && (pool)->inited)
    127 #if defined(BCM4329C0)
    128 #define SHARED_POOL		(pktpool_shared_ptr)
    129 #else
    130 #define SHARED_POOL		(pktpool_shared)
    131 #endif
    132 #else
    133 #define POOL_ENAB(bus)		0
    134 #define SHARED_POOL		((struct pktpool *)NULL)
    135 #endif
    136 
    137 #define PKTPOOL_LEN_MAX		40
    138 #define PKTPOOL_CB_MAX		3
    139 
    140 struct pktpool;
    141 typedef void (*pktpool_cb_t)(struct pktpool *pool, void *arg);
    142 typedef struct {
    143 	pktpool_cb_t cb;
    144 	void *arg;
    145 } pktpool_cbinfo_t;
    146 
    147 #ifdef BCMDBG_POOL
    148 
    149 #define POOL_IDLE	0
    150 #define POOL_RXFILL	1
    151 #define POOL_RXDH	2
    152 #define POOL_RXD11	3
    153 #define POOL_TXDH	4
    154 #define POOL_TXD11	5
    155 #define POOL_AMPDU	6
    156 #define POOL_TXENQ	7
    157 
    158 typedef struct {
    159 	void *p;
    160 	uint32 cycles;
    161 	uint32 dur;
    162 } pktpool_dbg_t;
    163 
    164 typedef struct {
    165 	uint8 txdh;
    166 	uint8 txd11;
    167 	uint8 enq;
    168 	uint8 rxdh;
    169 	uint8 rxd11;
    170 	uint8 rxfill;
    171 	uint8 idle;
    172 } pktpool_stats_t;
    173 #endif
    174 
    175 typedef struct pktpool {
    176 	bool inited;
    177 	uint16 r;
    178 	uint16 w;
    179 	uint16 len;
    180 	uint16 maxlen;
    181 	uint16 plen;
    182 	bool istx;
    183 	bool empty;
    184 	uint8 cbtoggle;
    185 	uint8 cbcnt;
    186 	uint8 ecbcnt;
    187 	bool emptycb_disable;
    188 	pktpool_cbinfo_t cbs[PKTPOOL_CB_MAX];
    189 	pktpool_cbinfo_t ecbs[PKTPOOL_CB_MAX];
    190 	void *q[PKTPOOL_LEN_MAX + 1];
    191 
    192 #ifdef BCMDBG_POOL
    193 	uint8 dbg_cbcnt;
    194 	pktpool_cbinfo_t dbg_cbs[PKTPOOL_CB_MAX];
    195 	uint16 dbg_qlen;
    196 	pktpool_dbg_t dbg_q[PKTPOOL_LEN_MAX + 1];
    197 #endif
    198 } pktpool_t;
    199 
    200 #if defined(BCM4329C0)
    201 extern pktpool_t *pktpool_shared_ptr;
    202 #else
    203 extern pktpool_t *pktpool_shared;
    204 #endif
    205 
    206 extern int pktpool_init(osl_t *osh, pktpool_t *pktp, int *pktplen, int plen, bool istx);
    207 extern int pktpool_deinit(osl_t *osh, pktpool_t *pktp);
    208 extern int pktpool_fill(osl_t *osh, pktpool_t *pktp, bool minimal);
    209 extern void* pktpool_get(pktpool_t *pktp);
    210 extern void pktpool_free(pktpool_t *pktp, void *p);
    211 extern int pktpool_add(pktpool_t *pktp, void *p);
    212 extern uint16 pktpool_avail(pktpool_t *pktp);
    213 extern int pktpool_avail_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
    214 extern int pktpool_empty_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
    215 extern int pktpool_setmaxlen(pktpool_t *pktp, uint16 maxlen);
    216 extern void pktpool_emptycb_disable(pktpool_t *pktp, bool disable);
    217 
    218 #define POOLPTR(pp)			((pktpool_t *)(pp))
    219 #define pktpool_len(pp)			(POOLPTR(pp)->len - 1)
    220 #define pktpool_plen(pp)		(POOLPTR(pp)->plen)
    221 #define pktpool_maxlen(pp)		(POOLPTR(pp)->maxlen)
    222 
    223 #ifdef BCMDBG_POOL
    224 extern int pktpool_dbg_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
    225 extern int pktpool_start_trigger(pktpool_t *pktp, void *p);
    226 extern int pktpool_dbg_dump(pktpool_t *pktp);
    227 extern int pktpool_dbg_notify(pktpool_t *pktp);
    228 extern int pktpool_stats_dump(pktpool_t *pktp, pktpool_stats_t *stats);
    229 #endif
    230 
    231 
    232 
    233 struct ether_addr;
    234 
    235 extern int ether_isbcast(const void *ea);
    236 extern int ether_isnulladdr(const void *ea);
    237 
    238 
    239 
    240 #define pktq_psetmax(pq, prec, _max)    ((pq)->q[prec].max = (_max))
    241 #define pktq_plen(pq, prec)             ((pq)->q[prec].len)
    242 #define pktq_pavail(pq, prec)           ((pq)->q[prec].max - (pq)->q[prec].len)
    243 #define pktq_pfull(pq, prec)            ((pq)->q[prec].len >= (pq)->q[prec].max)
    244 #define pktq_pempty(pq, prec)           ((pq)->q[prec].len == 0)
    245 
    246 #define pktq_ppeek(pq, prec)            ((pq)->q[prec].head)
    247 #define pktq_ppeek_tail(pq, prec)       ((pq)->q[prec].tail)
    248 
    249 extern void *pktq_penq(struct pktq *pq, int prec, void *p);
    250 extern void *pktq_penq_head(struct pktq *pq, int prec, void *p);
    251 extern void *pktq_pdeq(struct pktq *pq, int prec);
    252 extern void *pktq_pdeq_tail(struct pktq *pq, int prec);
    253 
    254 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec, bool dir,
    255 	ifpkt_cb_t fn, int arg);
    256 
    257 extern bool pktq_pdel(struct pktq *pq, void *p, int prec);
    258 
    259 
    260 
    261 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
    262 extern void *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
    263 
    264 
    265 
    266 #define pktq_len(pq)                    ((int)(pq)->len)
    267 #define pktq_max(pq)                    ((int)(pq)->max)
    268 #define pktq_avail(pq)                  ((int)((pq)->max - (pq)->len))
    269 #define pktq_full(pq)                   ((pq)->len >= (pq)->max)
    270 #define pktq_empty(pq)                  ((pq)->len == 0)
    271 
    272 
    273 #define pktenq(pq, p)		pktq_penq(((struct pktq *)pq), 0, (p))
    274 #define pktenq_head(pq, p)	pktq_penq_head(((struct pktq *)pq), 0, (p))
    275 #define pktdeq(pq)		pktq_pdeq(((struct pktq *)pq), 0)
    276 #define pktdeq_tail(pq)		pktq_pdeq_tail(((struct pktq *)pq), 0)
    277 #define pktqinit(pq, len) pktq_init(((struct pktq *)pq), 1, len)
    278 
    279 extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
    280 
    281 extern void *pktq_deq(struct pktq *pq, int *prec_out);
    282 extern void *pktq_deq_tail(struct pktq *pq, int *prec_out);
    283 extern void *pktq_peek(struct pktq *pq, int *prec_out);
    284 extern void *pktq_peek_tail(struct pktq *pq, int *prec_out);
    285 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir, ifpkt_cb_t fn, int arg);
    286 
    287 
    288 
    289 extern uint pktcopy(osl_t *osh, void *p, uint offset, int len, uchar *buf);
    290 extern uint pktfrombuf(osl_t *osh, void *p, uint offset, int len, uchar *buf);
    291 extern uint pkttotlen(osl_t *osh, void *p);
    292 extern void *pktlast(osl_t *osh, void *p);
    293 extern uint pktsegcnt(osl_t *osh, void *p);
    294 
    295 
    296 extern uint pktsetprio(void *pkt, bool update_vtag);
    297 #define	PKTPRIO_VDSCP	0x100
    298 #define	PKTPRIO_VLAN	0x200
    299 #define	PKTPRIO_UPD	0x400
    300 #define	PKTPRIO_DSCP	0x800
    301 
    302 
    303 extern int bcm_atoi(char *s);
    304 extern ulong bcm_strtoul(char *cp, char **endp, uint base);
    305 extern char *bcmstrstr(char *haystack, char *needle);
    306 extern char *bcmstrcat(char *dest, const char *src);
    307 extern char *bcmstrncat(char *dest, const char *src, uint size);
    308 extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
    309 char* bcmstrtok(char **string, const char *delimiters, char *tokdelim);
    310 int bcmstricmp(const char *s1, const char *s2);
    311 int bcmstrnicmp(const char* s1, const char* s2, int cnt);
    312 
    313 
    314 
    315 extern char *bcm_ether_ntoa(const struct ether_addr *ea, char *buf);
    316 extern int bcm_ether_atoe(char *p, struct ether_addr *ea);
    317 
    318 
    319 struct ipv4_addr;
    320 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
    321 
    322 
    323 extern void bcm_mdelay(uint ms);
    324 
    325 extern char *getvar(char *vars, const char *name);
    326 extern int getintvar(char *vars, const char *name);
    327 extern int getintvararray(char *vars, const char *name, int index);
    328 extern int getintvararraysize(char *vars, const char *name);
    329 extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
    330 #define bcm_perf_enable()
    331 #define bcmstats(fmt)
    332 #define	bcmlog(fmt, a1, a2)
    333 #define	bcmdumplog(buf, size)	*buf = '\0'
    334 #define	bcmdumplogent(buf, idx)	-1
    335 
    336 #define bcmtslog(tstamp, fmt, a1, a2)
    337 #define bcmprinttslogs()
    338 #define bcmprinttstamp(us)
    339 
    340 extern char *bcm_nvram_vars(uint *length);
    341 extern int bcm_nvram_cache(void *sih);
    342 
    343 
    344 
    345 
    346 typedef struct bcm_iovar {
    347 	const char *name;
    348 	uint16 varid;
    349 	uint16 flags;
    350 	uint16 type;
    351 	uint16 minlen;
    352 } bcm_iovar_t;
    353 
    354 
    355 
    356 
    357 #define IOV_GET 0
    358 #define IOV_SET 1
    359 
    360 
    361 #define IOV_GVAL(id)		((id)*2)
    362 #define IOV_SVAL(id)		(((id)*2)+IOV_SET)
    363 #define IOV_ISSET(actionid)	((actionid & IOV_SET) == IOV_SET)
    364 #define IOV_ID(actionid)	(actionid >> 1)
    365 
    366 
    367 
    368 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
    369 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, int len, bool set);
    370 #if defined(WLTINYDUMP) || defined(WLMSG_INFORM) || defined(WLMSG_ASSOC) || \
    371 	defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
    372 extern int bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len);
    373 #endif
    374 #endif
    375 
    376 
    377 #define IOVT_VOID	0
    378 #define IOVT_BOOL	1
    379 #define IOVT_INT8	2
    380 #define IOVT_UINT8	3
    381 #define IOVT_INT16	4
    382 #define IOVT_UINT16	5
    383 #define IOVT_INT32	6
    384 #define IOVT_UINT32	7
    385 #define IOVT_BUFFER	8
    386 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
    387 
    388 
    389 #define BCM_IOV_TYPE_INIT { \
    390 	"void", \
    391 	"bool", \
    392 	"int8", \
    393 	"uint8", \
    394 	"int16", \
    395 	"uint16", \
    396 	"int32", \
    397 	"uint32", \
    398 	"buffer", \
    399 	"" }
    400 
    401 #define BCM_IOVT_IS_INT(type) (\
    402 	(type == IOVT_BOOL) || \
    403 	(type == IOVT_INT8) || \
    404 	(type == IOVT_UINT8) || \
    405 	(type == IOVT_INT16) || \
    406 	(type == IOVT_UINT16) || \
    407 	(type == IOVT_INT32) || \
    408 	(type == IOVT_UINT32))
    409 
    410 
    411 
    412 #define BCME_STRLEN 		64
    413 #define VALID_BCMERROR(e)  ((e <= 0) && (e >= BCME_LAST))
    414 
    415 
    416 
    417 
    418 #define BCME_OK				0
    419 #define BCME_ERROR			-1
    420 #define BCME_BADARG			-2
    421 #define BCME_BADOPTION			-3
    422 #define BCME_NOTUP			-4
    423 #define BCME_NOTDOWN			-5
    424 #define BCME_NOTAP			-6
    425 #define BCME_NOTSTA			-7
    426 #define BCME_BADKEYIDX			-8
    427 #define BCME_RADIOOFF 			-9
    428 #define BCME_NOTBANDLOCKED		-10
    429 #define BCME_NOCLK			-11
    430 #define BCME_BADRATESET			-12
    431 #define BCME_BADBAND			-13
    432 #define BCME_BUFTOOSHORT		-14
    433 #define BCME_BUFTOOLONG			-15
    434 #define BCME_BUSY			-16
    435 #define BCME_NOTASSOCIATED		-17
    436 #define BCME_BADSSIDLEN			-18
    437 #define BCME_OUTOFRANGECHAN		-19
    438 #define BCME_BADCHAN			-20
    439 #define BCME_BADADDR			-21
    440 #define BCME_NORESOURCE			-22
    441 #define BCME_UNSUPPORTED		-23
    442 #define BCME_BADLEN			-24
    443 #define BCME_NOTREADY			-25
    444 #define BCME_EPERM			-26
    445 #define BCME_NOMEM			-27
    446 #define BCME_ASSOCIATED			-28
    447 #define BCME_RANGE			-29
    448 #define BCME_NOTFOUND			-30
    449 #define BCME_WME_NOT_ENABLED		-31
    450 #define BCME_TSPEC_NOTFOUND		-32
    451 #define BCME_ACM_NOTSUPPORTED		-33
    452 #define BCME_NOT_WME_ASSOCIATION	-34
    453 #define BCME_SDIO_ERROR			-35
    454 #define BCME_DONGLE_DOWN		-36
    455 #define BCME_VERSION			-37
    456 #define BCME_TXFAIL			-38
    457 #define BCME_RXFAIL			-39
    458 #define BCME_NODEVICE			-40
    459 #define BCME_NMODE_DISABLED		-41
    460 #define BCME_NONRESIDENT		-42
    461 #define BCME_LAST			BCME_NONRESIDENT
    462 
    463 
    464 #define BCMERRSTRINGTABLE {		\
    465 	"OK",				\
    466 	"Undefined error",		\
    467 	"Bad Argument",			\
    468 	"Bad Option",			\
    469 	"Not up",			\
    470 	"Not down",			\
    471 	"Not AP",			\
    472 	"Not STA",			\
    473 	"Bad Key Index",		\
    474 	"Radio Off",			\
    475 	"Not band locked",		\
    476 	"No clock",			\
    477 	"Bad Rate valueset",		\
    478 	"Bad Band",			\
    479 	"Buffer too short",		\
    480 	"Buffer too long",		\
    481 	"Busy",				\
    482 	"Not Associated",		\
    483 	"Bad SSID len",			\
    484 	"Out of Range Channel",		\
    485 	"Bad Channel",			\
    486 	"Bad Address",			\
    487 	"Not Enough Resources",		\
    488 	"Unsupported",			\
    489 	"Bad length",			\
    490 	"Not Ready",			\
    491 	"Not Permitted",		\
    492 	"No Memory",			\
    493 	"Associated",			\
    494 	"Not In Range",			\
    495 	"Not Found",			\
    496 	"WME Not Enabled",		\
    497 	"TSPEC Not Found",		\
    498 	"ACM Not Supported",		\
    499 	"Not WME Association",		\
    500 	"SDIO Bus Error",		\
    501 	"Dongle Not Accessible",	\
    502 	"Incorrect version",		\
    503 	"TX Failure",			\
    504 	"RX Failure",			\
    505 	"Device Not Present",		\
    506 	"NMODE Disabled",		\
    507 	"Nonresident overlay access", \
    508 }
    509 
    510 #ifndef ABS
    511 #define	ABS(a)			(((a) < 0)?-(a):(a))
    512 #endif
    513 
    514 #ifndef MIN
    515 #define	MIN(a, b)		(((a) < (b))?(a):(b))
    516 #endif
    517 
    518 #ifndef MAX
    519 #define	MAX(a, b)		(((a) > (b))?(a):(b))
    520 #endif
    521 
    522 #define CEIL(x, y)		(((x) + ((y)-1)) / (y))
    523 #define	ROUNDUP(x, y)		((((x)+((y)-1))/(y))*(y))
    524 #define	ISALIGNED(a, x)		(((uintptr)(a) & ((x)-1)) == 0)
    525 #define ALIGN_ADDR(addr, boundary) (void *)(((uintptr)(addr) + (boundary) - 1) \
    526 	                                         & ~((boundary) - 1))
    527 #define	ISPOWEROF2(x)		((((x)-1)&(x)) == 0)
    528 #define VALID_MASK(mask)	!((mask) & ((mask) + 1))
    529 #ifndef OFFSETOF
    530 #define	OFFSETOF(type, member)	((uint)(uintptr)&((type *)0)->member)
    531 #endif
    532 #ifndef ARRAYSIZE
    533 #define ARRAYSIZE(a)		(sizeof(a)/sizeof(a[0]))
    534 #endif
    535 
    536 
    537 extern void *_bcmutils_dummy_fn;
    538 #define REFERENCE_FUNCTION(f)	(_bcmutils_dummy_fn = (void *)(f))
    539 
    540 
    541 #ifndef setbit
    542 #ifndef NBBY
    543 #define	NBBY	8
    544 #endif
    545 #define	setbit(a, i)	(((uint8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
    546 #define	clrbit(a, i)	(((uint8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    547 #define	isset(a, i)	(((const uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
    548 #define	isclr(a, i)	((((const uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    549 #endif
    550 
    551 #define	NBITS(type)	(sizeof(type) * 8)
    552 #define NBITVAL(nbits)	(1 << (nbits))
    553 #define MAXBITVAL(nbits)	((1 << (nbits)) - 1)
    554 #define	NBITMASK(nbits)	MAXBITVAL(nbits)
    555 #define MAXNBVAL(nbyte)	MAXBITVAL((nbyte) * 8)
    556 
    557 
    558 #define MUX(pred, true, false) ((pred) ? (true) : (false))
    559 
    560 
    561 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
    562 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
    563 
    564 
    565 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
    566 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
    567 
    568 
    569 #define MODADD(x, y, bound) \
    570     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
    571 #define MODSUB(x, y, bound) \
    572     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
    573 
    574 
    575 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
    576 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
    577 
    578 
    579 #define CRC8_INIT_VALUE  0xff
    580 #define CRC8_GOOD_VALUE  0x9f
    581 #define CRC16_INIT_VALUE 0xffff
    582 #define CRC16_GOOD_VALUE 0xf0b8
    583 #define CRC32_INIT_VALUE 0xffffffff
    584 #define CRC32_GOOD_VALUE 0xdebb20e3
    585 
    586 
    587 typedef struct bcm_bit_desc {
    588 	uint32	bit;
    589 	const char* name;
    590 } bcm_bit_desc_t;
    591 
    592 
    593 typedef struct bcm_tlv {
    594 	uint8	id;
    595 	uint8	len;
    596 	uint8	data[1];
    597 } bcm_tlv_t;
    598 
    599 
    600 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
    601 
    602 
    603 #define ETHER_ADDR_STR_LEN	18
    604 
    605 
    606 
    607 static INLINE void
    608 xor_128bit_block(const uint8 *src1, const uint8 *src2, uint8 *dst)
    609 {
    610 	if (
    611 #ifdef __i386__
    612 	    1 ||
    613 #endif
    614 	    (((uintptr)src1 | (uintptr)src2 | (uintptr)dst) & 3) == 0) {
    615 
    616 
    617 		((uint32 *)dst)[0] = ((const uint32 *)src1)[0] ^ ((const uint32 *)src2)[0];
    618 		((uint32 *)dst)[1] = ((const uint32 *)src1)[1] ^ ((const uint32 *)src2)[1];
    619 		((uint32 *)dst)[2] = ((const uint32 *)src1)[2] ^ ((const uint32 *)src2)[2];
    620 		((uint32 *)dst)[3] = ((const uint32 *)src1)[3] ^ ((const uint32 *)src2)[3];
    621 	} else {
    622 
    623 		int k;
    624 		for (k = 0; k < 16; k++)
    625 			dst[k] = src1[k] ^ src2[k];
    626 	}
    627 }
    628 
    629 
    630 
    631 extern uint8 hndcrc8(uint8 *p, uint nbytes, uint8 crc);
    632 extern uint16 hndcrc16(uint8 *p, uint nbytes, uint16 crc);
    633 extern uint32 hndcrc32(uint8 *p, uint nbytes, uint32 crc);
    634 
    635 #if defined(DHD_DEBUG) || defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || \
    636 	defined(WLMSG_ASSOC)
    637 extern int bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, int len);
    638 #endif
    639 
    640 #if defined(DHD_DEBUG) || defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || \
    641 	defined(WLMSG_ASSOC) || defined(WLMEDIA_PEAKRATE)
    642 extern int bcm_format_hex(char *str, const void *bytes, int len);
    643 #endif
    644 
    645 extern const char *bcm_crypto_algo_name(uint algo);
    646 extern char *bcm_chipname(uint chipid, char *buf, uint len);
    647 extern char *bcm_brev_str(uint32 brev, char *buf);
    648 extern void printbig(char *buf);
    649 extern void prhex(const char *msg, uchar *buf, uint len);
    650 
    651 
    652 extern bcm_tlv_t *bcm_next_tlv(bcm_tlv_t *elt, int *buflen);
    653 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen, uint key);
    654 extern bcm_tlv_t *bcm_parse_ordered_tlvs(void *buf, int buflen, uint key);
    655 
    656 
    657 extern const char *bcmerrorstr(int bcmerror);
    658 
    659 
    660 typedef uint32 mbool;
    661 #define mboolset(mb, bit)		((mb) |= (bit))
    662 #define mboolclr(mb, bit)		((mb) &= ~(bit))
    663 #define mboolisset(mb, bit)		(((mb) & (bit)) != 0)
    664 #define	mboolmaskset(mb, mask, val)	((mb) = (((mb) & ~(mask)) | (val)))
    665 
    666 
    667 extern uint16 bcm_qdbm_to_mw(uint8 qdbm);
    668 extern uint8 bcm_mw_to_qdbm(uint16 mw);
    669 
    670 
    671 struct fielddesc {
    672 	const char *nameandfmt;
    673 	uint32 	offset;
    674 	uint32 	len;
    675 };
    676 
    677 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
    678 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
    679 extern void bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount);
    680 extern int bcm_cmp_bytes(uchar *arg1, uchar *arg2, uint8 nbytes);
    681 extern void bcm_print_bytes(char *name, const uchar *cdata, int len);
    682 
    683 typedef  uint32 (*bcmutl_rdreg_rtn)(void *arg0, uint arg1, uint32 offset);
    684 extern uint bcmdumpfields(bcmutl_rdreg_rtn func_ptr, void *arg0, uint arg1, struct fielddesc *str,
    685                           char *buf, uint32 bufsize);
    686 
    687 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf, uint len);
    688 extern uint bcm_bitcount(uint8 *bitmap, uint bytelength);
    689 
    690 
    691 
    692 #define SSID_FMT_BUF_LEN	((4 * DOT11_MAX_SSID_LEN) + 1)
    693 
    694 unsigned int process_nvram_vars(char *varbuf, unsigned int len);
    695 
    696 #ifdef __cplusplus
    697 	}
    698 #endif
    699 
    700 #endif
    701