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.
17 /* ctype replacement */
18 #define _BCM_U 0x01 /* upper */
19 #define _BCM_L 0x02 /* lower */
20 #define _BCM_D 0x04 /* digit */
21 #define _BCM_C 0x08 /* cntrl */
22 #define _BCM_P 0x10 /* punct */
23 #define _BCM_S 0x20 /* white space (space/lf/tab) */
24 #define _BCM_X 0x40 /* hex digit */
25 #define _BCM_SP 0x80 /* hard space (0x20) */
27 extern const unsigned char bcm_ctype
[];
28 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
30 #define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
31 #define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
32 #define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
33 #define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
34 #define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
35 #define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
36 #define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
37 #define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
38 #define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
39 #define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
40 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
41 #define bcm_tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
42 #define bcm_toupper(c) (bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
44 /* Buffer structure for collecting string-formatted data
45 * using bcm_bprintf() API.
46 * Use bcm_binit() to initialize before use
50 char *buf
; /* pointer to current position in origbuf */
51 unsigned int size
; /* current (residual) size in bytes */
52 char *origbuf
; /* unmodified pointer to orignal buffer */
53 unsigned int origsize
; /* unmodified orignal buffer size in bytes */
56 /* ** driver-only section ** */
60 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
63 * Spin at most 'us' microseconds while 'exp' is true.
64 * Caller should explicitly test 'exp' when this completes
65 * and take appropriate error action if 'exp' is still true.
67 #define SPINWAIT(exp, us) { \
68 uint countdown = (us) + 9; \
69 while ((exp) && (countdown >= 10)) {\
76 /* osl multi-precedence packet queue */
77 #ifndef PKTQ_LEN_DEFAULT
78 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
81 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
84 typedef struct pktq_prec
{
85 void *head
; /* first packet to dequeue */
86 void *tail
; /* last packet to dequeue */
87 uint16 len
; /* number of queued packets */
88 uint16 max
; /* maximum number of queued packets */
92 /* multi-priority pkt queue */
94 uint16 num_prec
; /* number of precedences in use */
95 uint16 hi_prec
; /* rapid dequeue hint (>= highest non-empty prec) */
96 uint16 max
; /* total max packets */
97 uint16 len
; /* total number of packets */
98 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
99 struct pktq_prec q
[PKTQ_MAX_PREC
];
102 /* simple, non-priority pkt queue */
104 uint16 num_prec
; /* number of precedences in use (always 1) */
105 uint16 hi_prec
; /* rapid dequeue hint (>= highest non-empty prec) */
106 uint16 max
; /* total max packets */
107 uint16 len
; /* total number of packets */
108 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
109 struct pktq_prec q
[1];
112 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
114 /* forward definition of ether_addr structure used by some function prototypes */
118 /* operations on a specific precedence in packet queue */
120 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
121 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
122 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
123 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
124 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
126 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
127 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
129 extern void *pktq_penq(struct pktq
*pq
, int prec
, void *p
);
130 extern void *pktq_penq_head(struct pktq
*pq
, int prec
, void *p
);
131 extern void *pktq_pdeq(struct pktq
*pq
, int prec
);
132 extern void *pktq_pdeq_tail(struct pktq
*pq
, int prec
);
133 /* Empty the queue at particular precedence level */
134 extern void pktq_pflush(osl_t
*osh
, struct pktq
*pq
, int prec
, bool dir
);
135 /* Remove a specified packet from its queue */
136 extern bool pktq_pdel(struct pktq
*pq
, void *p
, int prec
);
138 /* operations on a set of precedences in packet queue */
140 extern int pktq_mlen(struct pktq
*pq
, uint prec_bmp
);
141 extern void *pktq_mdeq(struct pktq
*pq
, uint prec_bmp
, int *prec_out
);
142 extern int pktq_setmax(struct pktq
*pq
, int max_len
);
144 /* operations on packet queue as a whole */
146 #define pktq_len(pq) ((int)(pq)->len)
147 #define pktq_max(pq) ((int)(pq)->max)
148 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
149 #define pktq_full(pq) ((pq)->len >= (pq)->max)
150 #define pktq_empty(pq) ((pq)->len == 0)
152 /* operations for single precedence queues */
153 #define pktenq(pq, p) pktq_penq(((struct pktq *)pq), 0, (p))
154 #define pktenq_head(pq, p) pktq_penq_head(((struct pktq *)pq), 0, (p))
155 #define pktdeq(pq) pktq_pdeq(((struct pktq *)pq), 0)
156 #define pktdeq_tail(pq) pktq_pdeq_tail(((struct pktq *)pq), 0)
157 #define pktqinit(pq, len) pktq_init(((struct pktq *)pq), 1, len)
159 extern void pktq_init(struct pktq
*pq
, int num_prec
, int max_len
);
160 /* prec_out may be NULL if caller is not interested in return value */
161 extern void *pktq_deq(struct pktq
*pq
, int *prec_out
);
162 extern void *pktq_deq_tail(struct pktq
*pq
, int *prec_out
);
163 extern void *pktq_peek(struct pktq
*pq
, int *prec_out
);
164 extern void *pktq_peek_tail(struct pktq
*pq
, int *prec_out
);
165 extern void pktq_flush(osl_t
*osh
, struct pktq
*pq
, bool dir
); /* Empty the entire queue */
169 extern uint
pktcopy(osl_t
*osh
, void *p
, uint offset
, int len
, uchar
*buf
);
170 extern uint
pkttotlen(osl_t
*osh
, void *p
);
171 extern void *pktlast(osl_t
*osh
, void *p
);
173 extern void pktsetprio(void *pkt
, bool update_vtag
);
176 extern int BCMROMFN(bcm_atoi
)(char *s
);
177 extern ulong
BCMROMFN(bcm_strtoul
)(char *cp
, char **endp
, uint base
);
178 extern char *BCMROMFN(bcmstrstr
)(char *haystack
, char *needle
);
179 extern char *BCMROMFN(bcmstrcat
)(char *dest
, const char *src
);
180 extern ulong
wchar2ascii(char *abuf
, ushort
*wbuf
, ushort wbuflen
, ulong abuflen
);
181 /* ethernet address */
182 extern char *bcm_ether_ntoa(struct ether_addr
*ea
, char *buf
);
183 extern int BCMROMFN(bcm_ether_atoe
)(char *p
, struct ether_addr
*ea
);
185 extern void bcm_mdelay(uint ms
);
186 /* variable access */
187 extern char *getvar(char *vars
, const char *name
);
188 extern int getintvar(char *vars
, const char *name
);
189 extern uint
getgpiopin(char *vars
, char *pin_name
, uint def_pin
);
190 /*******************************
191 * modify by tanghui @ 2006-11-22
193 *******************************/
196 extern void prpkt(const char *msg
, osl_t
*osh
, void *p0
);
199 extern void bcm_perf_enable(void);
200 extern void bcmstats(char *fmt
);
201 extern void bcmlog(char *fmt
, uint a1
, uint a2
);
202 extern void bcmdumplog(char *buf
, int size
);
203 extern int bcmdumplogent(char *buf
, uint idx
);
205 #define bcm_perf_enable()
206 #define bcmstats(fmt)
207 #define bcmlog(fmt, a1, a2)
208 #define bcmdumplog(buf, size) *buf = '\0'
209 #define bcmdumplogent(buf, idx) -1
210 #endif /* BCMPERFSTATS */
211 extern char *bcm_nvram_vars(uint
*length
);
212 extern int bcm_nvram_cache(void *sbh
);
214 /* Support for sharing code across in-driver iovar implementations.
215 * The intent is that a driver use this structure to map iovar names
216 * to its (private) iovar identifiers, and the lookup function to
217 * find the entry. Macros are provided to map ids and get/set actions
218 * into a single number space for a switch statement.
221 /* iovar structure */
222 typedef struct bcm_iovar
{
223 const char *name
; /* name for lookup and display */
224 uint16 varid
; /* id for switch */
225 uint16 flags
; /* driver-specific flag bits */
226 uint16 type
; /* base type of argument */
227 uint16 minlen
; /* min length for buffer vars */
230 /* varid definitions are per-driver, may use these get/set bits */
232 /* IOVar action bits for id mapping */
233 #define IOV_GET 0 /* Get an iovar */
234 #define IOV_SET 1 /* Set an iovar */
236 /* Varid to actionid mapping */
237 #define IOV_GVAL(id) ((id)*2)
238 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
239 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
241 /* flags are per-driver based on driver attributes */
243 /* Base type definitions */
244 #define IOVT_VOID 0 /* no value (implictly set only) */
245 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
246 #define IOVT_INT8 2 /* integer values are range-checked */
247 #define IOVT_UINT8 3 /* unsigned int 8 bits */
248 #define IOVT_INT16 4 /* int 16 bits */
249 #define IOVT_UINT16 5 /* unsigned int 16 bits */
250 #define IOVT_INT32 6 /* int 32 bits */
251 #define IOVT_UINT32 7 /* unsigned int 32 bits */
252 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
254 extern const bcm_iovar_t
*bcm_iovar_lookup(const bcm_iovar_t
*table
, const char *name
);
255 extern int bcm_iovar_lencheck(const bcm_iovar_t
*table
, void *arg
, int len
, bool set
);
257 #endif /* BCMDRIVER */
259 /* ** driver/apps-shared section ** */
261 #define BCME_STRLEN 64 /* Max string length for BCM errors */
262 #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST))
266 * error codes could be added but the defined ones shouldn't be changed/deleted
267 * these error codes are exposed to the user code
268 * when ever a new error code is added to this list
269 * please update errorstring table with the related error string and
270 * update osl files with os specific errorcode map
273 #define BCME_OK 0 /* Success */
274 #define BCME_ERROR -1 /* Error generic */
275 #define BCME_BADARG -2 /* Bad Argument */
276 #define BCME_BADOPTION -3 /* Bad option */
277 #define BCME_NOTUP -4 /* Not up */
278 #define BCME_NOTDOWN -5 /* Not down */
279 #define BCME_NOTAP -6 /* Not AP */
280 #define BCME_NOTSTA -7 /* Not STA */
281 #define BCME_BADKEYIDX -8 /* BAD Key Index */
282 #define BCME_RADIOOFF -9 /* Radio Off */
283 #define BCME_NOTBANDLOCKED -10 /* Not band locked */
284 #define BCME_NOCLK -11 /* No Clock */
285 #define BCME_BADRATESET -12 /* BAD Rate valueset */
286 #define BCME_BADBAND -13 /* BAD Band */
287 #define BCME_BUFTOOSHORT -14 /* Buffer too short */
288 #define BCME_BUFTOOLONG -15 /* Buffer too long */
289 #define BCME_BUSY -16 /* Busy */
290 #define BCME_NOTASSOCIATED -17 /* Not Associated */
291 #define BCME_BADSSIDLEN -18 /* Bad SSID len */
292 #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
293 #define BCME_BADCHAN -20 /* Bad Channel */
294 #define BCME_BADADDR -21 /* Bad Address */
295 #define BCME_NORESOURCE -22 /* Not Enough Resources */
296 #define BCME_UNSUPPORTED -23 /* Unsupported */
297 #define BCME_BADLEN -24 /* Bad length */
298 #define BCME_NOTREADY -25 /* Not Ready */
299 #define BCME_EPERM -26 /* Not Permitted */
300 #define BCME_NOMEM -27 /* No Memory */
301 #define BCME_ASSOCIATED -28 /* Associated */
302 #define BCME_RANGE -29 /* Not In Range */
303 #define BCME_NOTFOUND -30 /* Not Found */
304 #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
305 #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
306 #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
307 #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
308 #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
309 #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
310 #define BCME_LAST BCME_DONGLE_DOWN
312 /* These are collection of BCME Error strings */
313 #define BCMERRSTRINGTABLE { \
326 "Bad Rate valueset", \
328 "Buffer too short", \
333 "Out of Range Channel", \
336 "Not Enough Resources", \
347 "ACM Not Supported", \
348 "Not WME Association", \
350 "Dongle Not Accessible" \
354 #define ABS(a) (((a) < 0)?-(a):(a))
358 #define MIN(a, b) (((a) < (b))?(a):(b))
362 #define MAX(a, b) (((a) > (b))?(a):(b))
365 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
366 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
367 #define ISALIGNED(a, x) (((a) & ((x)-1)) == 0)
368 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
369 #define VALID_MASK(mask) !((mask) & ((mask) + 1))
371 #define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member)
372 #endif /* OFFSETOF */
373 #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
375 /* bit map related macros */
377 #ifndef NBBY /* the BSD family defines NBBY */
378 #define NBBY 8 /* 8 bits per byte */
379 #endif /* #ifndef NBBY */
380 #define setbit(a, i) (((uint8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
381 #define clrbit(a, i) (((uint8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
382 #define isset(a, i) (((const uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
383 #define isclr(a, i) ((((const uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
386 #define NBITS(type) (sizeof(type) * 8)
387 #define NBITVAL(nbits) (1 << (nbits))
388 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
389 #define NBITMASK(nbits) MAXBITVAL(nbits)
390 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
392 /* basic mux operation - can be optimized on several architectures */
393 #define MUX(pred, true, false) ((pred) ? (true) : (false))
395 /* modulo inc/dec - assumes x E [0, bound - 1] */
396 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
397 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
399 /* modulo inc/dec, bound = 2^k */
400 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
401 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
403 /* modulo add/sub - assumes x, y E [0, bound - 1] */
404 #define MODADD(x, y, bound) \
405 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
406 #define MODSUB(x, y, bound) \
407 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
409 /* module add/sub, bound = 2^k */
410 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
411 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
414 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
415 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
416 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
417 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
418 #define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
419 #define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
421 /* bcm_format_flags() bit description structure */
422 typedef struct bcm_bit_desc
{
427 /* tag_ID/length/value_buffer tuple */
428 typedef struct bcm_tlv
{
434 /* Check that bcm_tlv_t fits into the given buflen */
435 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
437 /* buffer length for ethernet address from bcm_ether_ntoa() */
438 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
440 /* unaligned load and store macros */
445 return ((a
[0] << 24) | (a
[1] << 16) | (a
[2] << 8) | a
[3]);
449 store32_ua(uint8
*a
, uint32 v
)
451 a
[0] = (v
>> 24) & 0xff;
452 a
[1] = (v
>> 16) & 0xff;
453 a
[2] = (v
>> 8) & 0xff;
460 return ((a
[0] << 8) | a
[1]);
464 store16_ua(uint8
*a
, uint16 v
)
466 a
[0] = (v
>> 8) & 0xff;
470 #else /* IL_BIGENDIAN */
475 return ((a
[3] << 24) | (a
[2] << 16) | (a
[1] << 8) | a
[0]);
479 store32_ua(uint8
*a
, uint32 v
)
481 a
[3] = (v
>> 24) & 0xff;
482 a
[2] = (v
>> 16) & 0xff;
483 a
[1] = (v
>> 8) & 0xff;
490 return ((a
[1] << 8) | a
[0]);
494 store16_ua(uint8
*a
, uint16 v
)
496 a
[1] = (v
>> 8) & 0xff;
500 #endif /* IL_BIGENDIAN */
504 extern uint8
BCMROMFN(hndcrc8
)(uint8
*p
, uint nbytes
, uint8 crc
);
505 extern uint16
BCMROMFN(hndcrc16
)(uint8
*p
, uint nbytes
, uint16 crc
);
506 extern uint32
BCMROMFN(hndcrc32
)(uint8
*p
, uint nbytes
, uint32 crc
);
508 /*******************************
509 * modify by tanghui @ 2006-11-22
511 *******************************/
514 extern int bcm_format_flags(const bcm_bit_desc_t
*bd
, uint32 flags
, char* buf
, int len
);
515 extern int bcm_format_hex(char *str
, const void *bytes
, int len
);
516 extern void deadbeef(void *p
, uint len
);
517 extern void prhex(const char *msg
, uchar
*buf
, uint len
);
519 extern void printfbig(char *buf
);
522 extern bcm_tlv_t
*BCMROMFN(bcm_next_tlv
)(bcm_tlv_t
*elt
, int *buflen
);
523 extern bcm_tlv_t
*BCMROMFN(bcm_parse_tlvs
)(void *buf
, int buflen
, uint key
);
524 extern bcm_tlv_t
*BCMROMFN(bcm_parse_ordered_tlvs
)(void *buf
, int buflen
, uint key
);
527 extern const char *bcmerrorstr(int bcmerror
);
529 /* multi-bool data type: set of bools, mbool is true if any is set */
530 typedef uint32 mbool
;
531 #define mboolset(mb, bit) (mb |= bit) /* set one bool */
532 #define mboolclr(mb, bit) (mb &= ~bit) /* clear one bool */
533 #define mboolisset(mb, bit) ((mb & bit) != 0) /* TRUE if one bool is set */
534 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
536 /* power conversion */
537 extern uint16
BCMROMFN(bcm_qdbm_to_mw
)(uint8 qdbm
);
538 extern uint8
BCMROMFN(bcm_mw_to_qdbm
)(uint16 mw
);
540 /* generic datastruct to help dump routines */
542 const char *nameandfmt
;
547 extern void bcm_binit(struct bcmstrbuf
*b
, char *buf
, uint size
);
548 extern int bcm_bprintf(struct bcmstrbuf
*b
, const char *fmt
, ...);
550 typedef uint32 (*readreg_rtn
)(void *arg0
, void *arg1
, uint32 offset
);
551 extern uint
bcmdumpfields(readreg_rtn func_ptr
, void *arg0
, void *arg1
, struct fielddesc
*str
,
552 char *buf
, uint32 bufsize
);
554 extern uint
bcm_mkiovar(char *name
, char *data
, uint datalen
, char *buf
, uint len
);
555 extern uint
BCMROMFN(bcm_bitcount
)(uint8
*bitmap
, uint bytelength
);
557 #ifdef BCMDBG_PKT /* pkt logging for debugging */
558 #define PKTLIST_SIZE 1000
560 void *list
[PKTLIST_SIZE
]; /* List of pointers to packets */
561 uint count
; /* Total count of the packets */
564 extern void pktlist_add(pktlist_info_t
*pktlist
, void *p
);
565 extern void pktlist_remove(pktlist_info_t
*pktlist
, void *p
);
566 extern char* pktlist_dump(pktlist_info_t
*pktlist
, char *buf
);
567 #endif /* BCMDBG_PKT */
569 #endif /* _bcmutils_h_ */