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