finally move buildroot-ng to trunk
[openwrt/staging/florian.git] / package / wlcompat / src / include / bcmutils.h
1 /*
2 * Misc useful os-independent macros and functions.
3 *
4 * Copyright 2004, 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$
12 */
13
14 #ifndef _bcmutils_h_
15 #define _bcmutils_h_
16
17 #ifndef MIN
18 #define MIN(a, b) (((a)<(b))?(a):(b))
19 #endif
20
21 #ifndef MAX
22 #define MAX(a, b) (((a)>(b))?(a):(b))
23 #endif
24
25 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
26 #define ROUNDUP(x, y) ((((ulong)(x)+((y)-1))/(y))*(y))
27 #define ISALIGNED(a, x) (((uint)(a) & ((x)-1)) == 0)
28 #define ISPOWEROF2(x) ((((x)-1)&(x))==0)
29 #define OFFSETOF(type, member) ((uint) &((type *)0)->member)
30 #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
31
32 /* bit map related macros */
33 #ifndef setbit
34 #define NBBY 8 /* 8 bits per byte */
35 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
36 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
37 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
38 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
39 #endif
40
41 #define NBITS(type) (sizeof (type) * 8)
42
43 #define _BCM_U 0x01 /* upper */
44 #define _BCM_L 0x02 /* lower */
45 #define _BCM_D 0x04 /* digit */
46 #define _BCM_C 0x08 /* cntrl */
47 #define _BCM_P 0x10 /* punct */
48 #define _BCM_S 0x20 /* white space (space/lf/tab) */
49 #define _BCM_X 0x40 /* hex digit */
50 #define _BCM_SP 0x80 /* hard space (0x20) */
51
52 extern unsigned char bcm_ctype[];
53 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
54
55 #define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
56 #define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
57 #define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
58 #define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
59 #define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
60 #define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
61 #define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
62 #define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
63 #define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
64 #define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
65 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
66
67 /*
68 * Spin at most 'us' microseconds while 'exp' is true.
69 * Caller should explicitly test 'exp' when this completes
70 * and take appropriate error action if 'exp' is still true.
71 */
72 #define SPINWAIT(exp, us) { \
73 uint countdown = (us) + 9; \
74 while ((exp) && (countdown >= 10)) {\
75 OSL_DELAY(10); \
76 countdown -= 10; \
77 } \
78 }
79
80 /* generic osl packet queue */
81 struct pktq {
82 void *head; /* first packet to dequeue */
83 void *tail; /* last packet to dequeue */
84 uint len; /* number of queued packets */
85 uint maxlen; /* maximum number of queued packets */
86 bool priority; /* enqueue by packet priority */
87 };
88 #define DEFAULT_QLEN 128
89
90 #define pktq_len(q) ((q)->len)
91 #define pktq_avail(q) ((q)->maxlen - (q)->len)
92 #define pktq_head(q) ((q)->head)
93 #define pktq_full(q) ((q)->len >= (q)->maxlen)
94
95 /* crc defines */
96 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
97 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
98 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
99 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
100 #define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
101 #define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
102
103 /* tag_ID/length/value_buffer tuple */
104 typedef struct bcm_tlv {
105 uint8 id;
106 uint8 len;
107 uint8 data[1];
108 } bcm_tlv_t;
109
110 /* Check that bcm_tlv_t fits into the given buflen */
111 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (buflen) >= 2 + (elt)->len)
112
113 /* buffer length for ethernet address from bcm_ether_ntoa() */
114 #define ETHER_ADDR_STR_LEN 18
115
116 /*
117 * load 32-bit value from unaligned byte array
118 */
119 #ifdef IL_BIGENDIAN
120 #define load32_ua(a) ((((uint8 *)(a))[0] << 24) + (((uint8 *)(a))[1] << 16) + \
121 (((uint8 *)(a))[2] << 8) + ((uint8 *)(a))[3])
122 #else
123 #define load32_ua(a) ((((uint8 *)(a))[3] << 24) + (((uint8 *)(a))[2] << 16) + \
124 (((uint8 *)(a))[1] << 8) + ((uint8 *)(a))[0])
125 #endif
126
127 /* externs */
128 extern uint bcm_atoi(char *s);
129 extern uchar bcm_toupper(uchar c);
130 extern ulong bcm_strtoul(char *cp, char **endp, uint base);
131 extern void deadbeef(char *p, uint len);
132 extern void prhex(char *msg, uchar *buf, uint len);
133 extern void prpkt(char *msg, void *drv, void *p0);
134 extern uint pktcopy(void *drv, void *p, uint offset, int len, uchar *buf);
135 extern uint pkttotlen(void *drv, void *);
136 extern uchar *bcm_ether_ntoa(char *ea, char *buf);
137 extern int bcm_ether_atoe(char *p, char *ea);
138 extern void bcm_mdelay(uint ms);
139 extern char *getvar(char *vars, char *name);
140 extern int getintvar(char *vars, char *name);
141 extern char *bcmstrstr(char *haystack, char *needle);
142
143 extern uint8 crc8(uint8 *p, uint nbytes, uint8 crc);
144 extern uint16 crc16(uint8 *p, uint nbytes, uint16 crc);
145 extern uint32 crc32(uint8 *p, uint nbytes, uint32 crc);
146 extern bcm_tlv_t *bcm_next_tlv(bcm_tlv_t *elt, int *buflen);
147 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen, uint key);
148 extern bcm_tlv_t *bcm_parse_ordered_tlvs(void *buf, int buflen, uint key);
149 extern void pktq_init(struct pktq *q, uint maxlen, bool priority);
150 extern bool pktenq(struct pktq *q, void *p, bool lifo);
151 extern void *pktdeq(struct pktq *q);
152
153 #define bcmlog(fmt, a1, a2)
154 #define bcmdumplog(buf, size) *buf = '\0'
155 #define bcmdumplogent(buf, idx) -1
156
157 #endif /* _bcmutils_h_ */