staging: brcm80211: cleaned brcmu_utils.h
[deliverable/linux.git] / drivers / staging / brcm80211 / include / brcmu_utils.h
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _BRCMU_UTILS_H_
18 #define _BRCMU_UTILS_H_
19
20 /* Buffer structure for collecting string-formatted data
21 * using brcmu_bprintf() API.
22 * Use brcmu_binit() to initialize before use
23 */
24
25 struct brcmu_strbuf {
26 char *buf; /* pointer to current position in origbuf */
27 unsigned int size; /* current (residual) size in bytes */
28 char *origbuf; /* unmodified pointer to orignal buffer */
29 unsigned int origsize; /* unmodified orignal buffer size in bytes */
30 };
31
32 /*
33 * Spin at most 'us' microseconds while 'exp' is true.
34 * Caller should explicitly test 'exp' when this completes
35 * and take appropriate error action if 'exp' is still true.
36 */
37 #define SPINWAIT(exp, us) { \
38 uint countdown = (us) + 9; \
39 while ((exp) && (countdown >= 10)) {\
40 udelay(10); \
41 countdown -= 10; \
42 } \
43 }
44
45 /* osl multi-precedence packet queue */
46 #ifndef PKTQ_LEN_DEFAULT
47 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
48 #endif
49 #ifndef PKTQ_MAX_PREC
50 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
51 #endif
52
53 struct pktq_prec {
54 struct sk_buff *head; /* first packet to dequeue */
55 struct sk_buff *tail; /* last packet to dequeue */
56 u16 len; /* number of queued packets */
57 u16 max; /* maximum number of queued packets */
58 };
59
60 /* multi-priority pkt queue */
61 struct pktq {
62 u16 num_prec; /* number of precedences in use */
63 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
64 u16 max; /* total max packets */
65 u16 len; /* total number of packets */
66 /*
67 * q array must be last since # of elements can be either
68 * PKTQ_MAX_PREC or 1
69 */
70 struct pktq_prec q[PKTQ_MAX_PREC];
71 };
72
73 /* fn(pkt, arg). return true if pkt belongs to if */
74 typedef bool(*ifpkt_cb_t) (struct sk_buff *, void *);
75
76 /* operations on a specific precedence in packet queue */
77
78 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
79 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
80 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
81 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
82 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
83
84 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
85 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
86
87 extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
88 struct sk_buff *p);
89 extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
90 struct sk_buff *p);
91 extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
92 extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
93
94 /* packet primitives */
95 extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
96 extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
97
98 /* Empty the queue at particular precedence level */
99 extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
100 bool dir, ifpkt_cb_t fn, void *arg);
101
102 /* operations on a set of precedences in packet queue */
103
104 extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
105 extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
106 int *prec_out);
107
108 /* operations on packet queue as a whole */
109
110 #define pktq_len(pq) ((int)(pq)->len)
111 #define pktq_max(pq) ((int)(pq)->max)
112 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
113 #define pktq_full(pq) ((pq)->len >= (pq)->max)
114 #define pktq_empty(pq) ((pq)->len == 0)
115
116 /* operations for single precedence queues */
117 #define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p))
118 #define pktenq_head(pq, p)\
119 brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p))
120 #define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0)
121 #define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0)
122 #define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len)
123
124 extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
125 /* prec_out may be NULL if caller is not interested in return value */
126 extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
127 extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
128 ifpkt_cb_t fn, void *arg);
129
130 /* externs */
131 /* packet */
132 extern uint brcmu_pktfrombuf(struct sk_buff *p,
133 uint offset, int len, unsigned char *buf);
134 extern uint brcmu_pkttotlen(struct sk_buff *p);
135
136 /* ethernet address */
137 extern int brcmu_ether_atoe(char *p, u8 *ea);
138
139 /* ip address */
140 struct ipv4_addr;
141
142 #ifdef BCMDBG
143 extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
144 #else
145 #define brcmu_prpkt(a, b)
146 #endif /* BCMDBG */
147
148 /* Support for sharing code across in-driver iovar implementations.
149 * The intent is that a driver use this structure to map iovar names
150 * to its (private) iovar identifiers, and the lookup function to
151 * find the entry. Macros are provided to map ids and get/set actions
152 * into a single number space for a switch statement.
153 */
154
155 /* iovar structure */
156 struct brcmu_iovar {
157 const char *name; /* name for lookup and display */
158 u16 varid; /* id for switch */
159 u16 flags; /* driver-specific flag bits */
160 u16 type; /* base type of argument */
161 u16 minlen; /* min length for buffer vars */
162 };
163
164 /* varid definitions are per-driver, may use these get/set bits */
165
166 /* IOVar action bits for id mapping */
167 #define IOV_GET 0 /* Get an iovar */
168 #define IOV_SET 1 /* Set an iovar */
169
170 /* Varid to actionid mapping */
171 #define IOV_GVAL(id) ((id)*2)
172 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
173 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
174 #define IOV_ID(actionid) (actionid >> 1)
175
176 extern const struct
177 brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
178 const char *name);
179 extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
180 int len, bool set);
181
182 /* Base type definitions */
183 #define IOVT_VOID 0 /* no value (implictly set only) */
184 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
185 #define IOVT_INT8 2 /* integer values are range-checked */
186 #define IOVT_UINT8 3 /* unsigned int 8 bits */
187 #define IOVT_INT16 4 /* int 16 bits */
188 #define IOVT_UINT16 5 /* unsigned int 16 bits */
189 #define IOVT_INT32 6 /* int 32 bits */
190 #define IOVT_UINT32 7 /* unsigned int 32 bits */
191 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
192 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
193
194 /* ** driver/apps-shared section ** */
195
196 #define BCME_STRLEN 64 /* Max string length for BCM errors */
197
198 #ifndef ABS
199 #define ABS(a) (((a) < 0) ? -(a) : (a))
200 #endif /* ABS */
201
202 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
203 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
204
205 /* map physical to virtual I/O */
206 #if !defined(CONFIG_MMC_MSM7X00A)
207 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
208 (unsigned long)(size))
209 #else
210 #define REG_MAP(pa, size) (void *)(0)
211 #endif
212
213 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
214 #define PKTBUFSZ 2048
215
216 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
217
218 #ifndef setbit
219 #ifndef NBBY /* the BSD family defines NBBY */
220 #define NBBY 8 /* 8 bits per byte */
221 #endif /* #ifndef NBBY */
222 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
223 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
224 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
225 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
226 #endif /* setbit */
227
228 #define NBITS(type) (sizeof(type) * 8)
229 #define NBITVAL(nbits) (1 << (nbits))
230 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
231 #define NBITMASK(nbits) MAXBITVAL(nbits)
232 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
233
234 /* basic mux operation - can be optimized on several architectures */
235 #define MUX(pred, true, false) ((pred) ? (true) : (false))
236
237 /* modulo inc/dec - assumes x E [0, bound - 1] */
238 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
239 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
240
241 /* modulo inc/dec, bound = 2^k */
242 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
243 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
244
245 /* modulo add/sub - assumes x, y E [0, bound - 1] */
246 #define MODADD(x, y, bound) \
247 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
248 #define MODSUB(x, y, bound) \
249 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
250
251 /* module add/sub, bound = 2^k */
252 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
253 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
254
255 /* crc defines */
256 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
257 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
258 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
259 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
260
261 /* brcmu_format_flags() bit description structure */
262 struct brcmu_bit_desc {
263 u32 bit;
264 const char *name;
265 };
266
267 /* tag_ID/length/value_buffer tuple */
268 struct brcmu_tlv {
269 u8 id;
270 u8 len;
271 u8 data[1];
272 };
273
274 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
275
276 /* externs */
277 /* crc */
278 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
279
280 /* format/print */
281 #if defined(BCMDBG)
282 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
283 char *buf, int len);
284 extern int brcmu_format_hex(char *str, const void *bytes, int len);
285 #endif
286
287 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
288
289 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
290 uint key);
291
292 /* power conversion */
293 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
294 extern u8 brcmu_mw_to_qdbm(u16 mw);
295
296 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
297 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
298
299 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
300 char *buf, uint len);
301 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
302
303 #endif /* _BRCMU_UTILS_H_ */
This page took 0.062863 seconds and 5 git commands to generate.