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