]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/include/bcmutils.h
f2e81e5fd9d0a70b72f25bc33c785576074027fa
[mv-sheeva.git] / drivers / staging / brcm80211 / include / bcmutils.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 _bcmutils_h_
18 #define _bcmutils_h_
19
20 /* Buffer structure for collecting string-formatted data
21 * using bcm_bprintf() API.
22 * Use bcm_binit() to initialize before use
23 */
24
25         struct bcmstrbuf {
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 /* ** driver-only section ** */
33
34 #define GPIO_PIN_NOTDEFINED     0x20    /* Pin not defined */
35
36 /*
37  * Spin at most 'us' microseconds while 'exp' is true.
38  * Caller should explicitly test 'exp' when this completes
39  * and take appropriate error action if 'exp' is still true.
40  */
41 #define SPINWAIT(exp, us) { \
42         uint countdown = (us) + 9; \
43         while ((exp) && (countdown >= 10)) {\
44                 udelay(10); \
45                 countdown -= 10; \
46         } \
47 }
48
49 /* osl multi-precedence packet queue */
50 #ifndef PKTQ_LEN_DEFAULT
51 #define PKTQ_LEN_DEFAULT        128     /* Max 128 packets */
52 #endif
53 #ifndef PKTQ_MAX_PREC
54 #define PKTQ_MAX_PREC           16      /* Maximum precedence levels */
55 #endif
56
57         struct pktq_prec {
58                 struct sk_buff *head;   /* first packet to dequeue */
59                 struct sk_buff *tail;   /* last packet to dequeue */
60                 u16 len;                /* number of queued packets */
61                 u16 max;                /* maximum number of queued packets */
62         };
63
64 /* multi-priority pkt queue */
65         struct pktq {
66                 u16 num_prec;   /* number of precedences in use */
67                 u16 hi_prec;    /* rapid dequeue hint (>= highest non-empty prec) */
68                 u16 max;        /* total max packets */
69                 u16 len;        /* total number of packets */
70                 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
71                 struct pktq_prec q[PKTQ_MAX_PREC];
72         };
73
74 #define PKTQ_PREC_ITER(pq, prec)        for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
75
76 /* fn(pkt, arg).  return true if pkt belongs to if */
77         typedef bool(*ifpkt_cb_t) (void *, int);
78
79 /* operations on a specific precedence in packet queue */
80
81 #define pktq_psetmax(pq, prec, _max)    ((pq)->q[prec].max = (_max))
82 #define pktq_plen(pq, prec)             ((pq)->q[prec].len)
83 #define pktq_pavail(pq, prec)           ((pq)->q[prec].max - (pq)->q[prec].len)
84 #define pktq_pfull(pq, prec)            ((pq)->q[prec].len >= (pq)->q[prec].max)
85 #define pktq_pempty(pq, prec)           ((pq)->q[prec].len == 0)
86
87 #define pktq_ppeek(pq, prec)            ((pq)->q[prec].head)
88 #define pktq_ppeek_tail(pq, prec)       ((pq)->q[prec].tail)
89
90 extern struct sk_buff *pktq_penq(struct pktq *pq, int prec,
91                                  struct sk_buff *p);
92 extern struct sk_buff *pktq_penq_head(struct pktq *pq, int prec,
93                                       struct sk_buff *p);
94 extern struct sk_buff *pktq_pdeq(struct pktq *pq, int prec);
95 extern struct sk_buff *pktq_pdeq_tail(struct pktq *pq, int prec);
96
97 /* packet primitives */
98 extern struct sk_buff *pkt_buf_get_skb(uint len);
99 extern void pkt_buf_free_skb(struct sk_buff *skb);
100
101 /* Empty the queue at particular precedence level */
102 #ifdef BRCM_FULLMAC
103         extern void pktq_pflush(struct pktq *pq, int prec,
104                 bool dir);
105 #else
106         extern void pktq_pflush(struct pktq *pq, int prec,
107                 bool dir, ifpkt_cb_t fn, int arg);
108 #endif /* BRCM_FULLMAC */
109
110 /* operations on a set of precedences in packet queue */
111
112 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
113 extern struct sk_buff *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
114
115 /* operations on packet queue as a whole */
116
117 #define pktq_len(pq)                    ((int)(pq)->len)
118 #define pktq_max(pq)                    ((int)(pq)->max)
119 #define pktq_avail(pq)                  ((int)((pq)->max - (pq)->len))
120 #define pktq_full(pq)                   ((pq)->len >= (pq)->max)
121 #define pktq_empty(pq)                  ((pq)->len == 0)
122
123 /* operations for single precedence queues */
124 #define pktenq(pq, p)           pktq_penq(((struct pktq *)pq), 0, (p))
125 #define pktenq_head(pq, p)      pktq_penq_head(((struct pktq *)pq), 0, (p))
126 #define pktdeq(pq)              pktq_pdeq(((struct pktq *)pq), 0)
127 #define pktdeq_tail(pq)         pktq_pdeq_tail(((struct pktq *)pq), 0)
128 #define pktqinit(pq, len) pktq_init(((struct pktq *)pq), 1, len)
129
130         extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
131 /* prec_out may be NULL if caller is not interested in return value */
132         extern struct sk_buff *pktq_peek_tail(struct pktq *pq, int *prec_out);
133 #ifdef BRCM_FULLMAC
134         extern void pktq_flush(struct pktq *pq, bool dir);
135 #else
136         extern void pktq_flush(struct pktq *pq, bool dir,
137                 ifpkt_cb_t fn, int arg);
138 #endif
139
140 /* externs */
141 /* packet */
142         extern uint pktfrombuf(struct sk_buff *p,
143                                uint offset, int len, unsigned char *buf);
144         extern uint pkttotlen(struct sk_buff *p);
145
146 /* ethernet address */
147         extern int bcm_ether_atoe(char *p, u8 *ea);
148
149 /* ip address */
150         struct ipv4_addr;
151         extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
152
153 /* variable access */
154         extern char *getvar(char *vars, const char *name);
155         extern int getintvar(char *vars, const char *name);
156 #ifdef BCMDBG
157         extern void prpkt(const char *msg, struct sk_buff *p0);
158 #else
159 #define prpkt(a, b)
160 #endif                          /* BCMDBG */
161
162 #define bcm_perf_enable()
163 #define bcmstats(fmt)
164 #define bcmlog(fmt, a1, a2)
165 #define bcmdumplog(buf, size)   (*buf = '\0')
166 #define bcmdumplogent(buf, idx) -1
167
168 #define bcmtslog(tstamp, fmt, a1, a2)
169 #define bcmprinttslogs()
170 #define bcmprinttstamp(us)
171
172 /* Support for sharing code across in-driver iovar implementations.
173  * The intent is that a driver use this structure to map iovar names
174  * to its (private) iovar identifiers, and the lookup function to
175  * find the entry.  Macros are provided to map ids and get/set actions
176  * into a single number space for a switch statement.
177  */
178
179 /* iovar structure */
180         typedef struct bcm_iovar {
181                 const char *name;       /* name for lookup and display */
182                 u16 varid;      /* id for switch */
183                 u16 flags;      /* driver-specific flag bits */
184                 u16 type;       /* base type of argument */
185                 u16 minlen;     /* min length for buffer vars */
186         } bcm_iovar_t;
187
188 /* varid definitions are per-driver, may use these get/set bits */
189
190 /* IOVar action bits for id mapping */
191 #define IOV_GET 0               /* Get an iovar */
192 #define IOV_SET 1               /* Set an iovar */
193
194 /* Varid to actionid mapping */
195 #define IOV_GVAL(id)            ((id)*2)
196 #define IOV_SVAL(id)            (((id)*2)+IOV_SET)
197 #define IOV_ISSET(actionid)     ((actionid & IOV_SET) == IOV_SET)
198 #define IOV_ID(actionid)        (actionid >> 1)
199
200 /* flags are per-driver based on driver attributes */
201
202         extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table,
203                                                    const char *name);
204         extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg,
205                                       int len, bool set);
206
207 /* Base type definitions */
208 #define IOVT_VOID       0       /* no value (implictly set only) */
209 #define IOVT_BOOL       1       /* any value ok (zero/nonzero) */
210 #define IOVT_INT8       2       /* integer values are range-checked */
211 #define IOVT_UINT8      3       /* unsigned int 8 bits */
212 #define IOVT_INT16      4       /* int 16 bits */
213 #define IOVT_UINT16     5       /* unsigned int 16 bits */
214 #define IOVT_INT32      6       /* int 32 bits */
215 #define IOVT_UINT32     7       /* unsigned int 32 bits */
216 #define IOVT_BUFFER     8       /* buffer is size-checked as per minlen */
217 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
218
219 /* Initializer for IOV type strings */
220 #define BCM_IOV_TYPE_INIT { \
221         "void", \
222         "bool", \
223         "s8", \
224         "u8", \
225         "s16", \
226         "u16", \
227         "s32", \
228         "u32", \
229         "buffer", \
230         "" }
231
232 #define BCM_IOVT_IS_INT(type) (\
233         (type == IOVT_BOOL) || \
234         (type == IOVT_INT8) || \
235         (type == IOVT_UINT8) || \
236         (type == IOVT_INT16) || \
237         (type == IOVT_UINT16) || \
238         (type == IOVT_INT32) || \
239         (type == IOVT_UINT32))
240
241 /* ** driver/apps-shared section ** */
242
243 #define BCME_STRLEN             64      /* Max string length for BCM errors */
244
245 #ifndef ABS
246 #define ABS(a)                  (((a) < 0) ? -(a) : (a))
247 #endif                          /* ABS */
248
249 #define CEIL(x, y)              (((x) + ((y)-1)) / (y))
250 #define ISPOWEROF2(x)           ((((x)-1)&(x)) == 0)
251
252 /* map physical to virtual I/O */
253 #if !defined(CONFIG_MMC_MSM7X00A)
254 #define REG_MAP(pa, size)       ioremap_nocache((unsigned long)(pa), \
255                                         (unsigned long)(size))
256 #else
257 #define REG_MAP(pa, size)       (void *)(0)
258 #endif
259
260 extern u32 g_assert_type;
261
262 #if defined(BCMDBG_ASSERT)
263 #define ASSERT(exp) \
264           do { if (!(exp)) osl_assert(#exp, __FILE__, __LINE__); } while (0)
265 extern void osl_assert(char *exp, char *file, int line);
266 #else
267 #define ASSERT(exp)     do {} while (0)
268 #endif  /* defined(BCMDBG_ASSERT) */
269
270 /* register access macros */
271 #if defined(BCMSDIO)
272 #ifdef BRCM_FULLMAC
273 #include <bcmsdh.h>
274 #endif
275 #define OSL_WRITE_REG(r, v) \
276                 (bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)))
277 #define OSL_READ_REG(r) \
278                 (bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r))))
279 #endif
280
281 #if defined(BCMSDIO)
282 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
283 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
284 #else
285 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
286 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
287 #endif
288
289 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
290 #define PKTBUFSZ        2048
291
292 #define OSL_SYSUPTIME()         ((u32)jiffies * (1000 / HZ))
293 #ifdef BRCM_FULLMAC
294 #include <linux/kernel.h>       /* for vsn/printf's */
295 #include <linux/string.h>       /* for mem*, str* */
296 #endif
297 /* bcopy's: Linux kernel doesn't provide these (anymore) */
298 #define bcopy(src, dst, len)    memcpy((dst), (src), (len))
299
300 /* register access macros */
301 #ifndef IL_BIGENDIAN
302 #ifndef __mips__
303 #define R_REG(r) (\
304         SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
305         readb((volatile u8*)(r)) : \
306         sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
307         readl((volatile u32*)(r)), OSL_READ_REG(r)) \
308 )
309 #else                           /* __mips__ */
310 #define R_REG(r) (\
311         SELECT_BUS_READ( \
312                 ({ \
313                         __typeof(*(r)) __osl_v; \
314                         __asm__ __volatile__("sync"); \
315                         switch (sizeof(*(r))) { \
316                         case sizeof(u8): \
317                                 __osl_v = readb((volatile u8*)(r)); \
318                                 break; \
319                         case sizeof(u16): \
320                                 __osl_v = readw((volatile u16*)(r)); \
321                                 break; \
322                         case sizeof(u32): \
323                                 __osl_v = \
324                                 readl((volatile u32*)(r)); \
325                                 break; \
326                         } \
327                         __asm__ __volatile__("sync"); \
328                         __osl_v; \
329                 }), \
330                 ({ \
331                         __typeof(*(r)) __osl_v; \
332                         __asm__ __volatile__("sync"); \
333                         __osl_v = OSL_READ_REG(r); \
334                         __asm__ __volatile__("sync"); \
335                         __osl_v; \
336                 })) \
337 )
338 #endif                          /* __mips__ */
339
340 #define W_REG(r, v) do { \
341         SELECT_BUS_WRITE( \
342                 switch (sizeof(*(r))) { \
343                 case sizeof(u8): \
344                         writeb((u8)(v), (volatile u8*)(r)); break; \
345                 case sizeof(u16): \
346                         writew((u16)(v), (volatile u16*)(r)); break; \
347                 case sizeof(u32): \
348                         writel((u32)(v), (volatile u32*)(r)); break; \
349                 }, \
350                 (OSL_WRITE_REG(r, v))); \
351         } while (0)
352 #else                           /* IL_BIGENDIAN */
353 #define R_REG(r) (\
354         SELECT_BUS_READ( \
355                 ({ \
356                         __typeof(*(r)) __osl_v; \
357                         switch (sizeof(*(r))) { \
358                         case sizeof(u8): \
359                                 __osl_v = \
360                                 readb((volatile u8*)((r)^3)); \
361                                 break; \
362                         case sizeof(u16): \
363                                 __osl_v = \
364                                 readw((volatile u16*)((r)^2)); \
365                                 break; \
366                         case sizeof(u32): \
367                                 __osl_v = readl((volatile u32*)(r)); \
368                                 break; \
369                         } \
370                         __osl_v; \
371                 }), \
372                 OSL_READ_REG(r)) \
373 )
374 #define W_REG(r, v) do { \
375         SELECT_BUS_WRITE( \
376                 switch (sizeof(*(r))) { \
377                 case sizeof(u8):        \
378                         writeb((u8)(v), \
379                         (volatile u8*)((r)^3)); break; \
380                 case sizeof(u16):       \
381                         writew((u16)(v), \
382                         (volatile u16*)((r)^2)); break; \
383                 case sizeof(u32):       \
384                         writel((u32)(v), \
385                         (volatile u32*)(r)); break; \
386                 }, \
387                 (OSL_WRITE_REG(r, v))); \
388         } while (0)
389 #endif                          /* IL_BIGENDIAN */
390
391 #define AND_REG(r, v)   W_REG((r), R_REG(r) & (v))
392 #define OR_REG(r, v)    W_REG((r), R_REG(r) | (v))
393
394 #define SET_REG(r, mask, val) \
395                 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
396
397 #ifndef setbit
398 #ifndef NBBY                    /* the BSD family defines NBBY */
399 #define NBBY    8               /* 8 bits per byte */
400 #endif                          /* #ifndef NBBY */
401 #define setbit(a, i)    (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
402 #define clrbit(a, i)    (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
403 #define isset(a, i)     (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
404 #define isclr(a, i)     ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
405 #endif                          /* setbit */
406
407 #define NBITS(type)     (sizeof(type) * 8)
408 #define NBITVAL(nbits)  (1 << (nbits))
409 #define MAXBITVAL(nbits)        ((1 << (nbits)) - 1)
410 #define NBITMASK(nbits) MAXBITVAL(nbits)
411 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
412
413 /* basic mux operation - can be optimized on several architectures */
414 #define MUX(pred, true, false) ((pred) ? (true) : (false))
415
416 /* modulo inc/dec - assumes x E [0, bound - 1] */
417 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
418 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
419
420 /* modulo inc/dec, bound = 2^k */
421 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
422 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
423
424 /* modulo add/sub - assumes x, y E [0, bound - 1] */
425 #define MODADD(x, y, bound) \
426     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
427 #define MODSUB(x, y, bound) \
428     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
429
430 /* module add/sub, bound = 2^k */
431 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
432 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
433
434 /* crc defines */
435 #define CRC8_INIT_VALUE  0xff   /* Initial CRC8 checksum value */
436 #define CRC8_GOOD_VALUE  0x9f   /* Good final CRC8 checksum value */
437 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
438 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
439
440 /* bcm_format_flags() bit description structure */
441         typedef struct bcm_bit_desc {
442                 u32 bit;
443                 const char *name;
444         } bcm_bit_desc_t;
445
446 /* tag_ID/length/value_buffer tuple */
447         typedef struct bcm_tlv {
448                 u8 id;
449                 u8 len;
450                 u8 data[1];
451         } bcm_tlv_t;
452
453 /* Check that bcm_tlv_t fits into the given buflen */
454 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
455
456 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
457
458 /* crypto utility function */
459 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
460         static inline void
461          xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
462                 if (
463 #ifdef __i386__
464                            1 ||
465 #endif
466                            (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
467                             3) == 0) {
468                         /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
469                         /* x86 supports unaligned.  This version runs 6x-9x faster on x86. */
470                         ((u32 *) dst)[0] =
471                             ((const u32 *)src1)[0] ^ ((const u32 *)
472                                                          src2)[0];
473                         ((u32 *) dst)[1] =
474                             ((const u32 *)src1)[1] ^ ((const u32 *)
475                                                          src2)[1];
476                         ((u32 *) dst)[2] =
477                             ((const u32 *)src1)[2] ^ ((const u32 *)
478                                                          src2)[2];
479                         ((u32 *) dst)[3] =
480                             ((const u32 *)src1)[3] ^ ((const u32 *)
481                                                          src2)[3];
482                 } else {
483                         /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
484                         int k;
485                         for (k = 0; k < 16; k++)
486                                 dst[k] = src1[k] ^ src2[k];
487                 }
488         }
489
490 /* externs */
491 /* crc */
492         extern u8 hndcrc8(u8 *p, uint nbytes, u8 crc);
493         extern u16 hndcrc16(u8 *p, uint nbytes, u16 crc);
494 /* format/print */
495 #if defined(BCMDBG)
496         extern int bcm_format_flags(const bcm_bit_desc_t *bd, u32 flags,
497                                     char *buf, int len);
498         extern int bcm_format_hex(char *str, const void *bytes, int len);
499 #endif
500         extern char *bcm_chipname(uint chipid, char *buf, uint len);
501         extern void prhex(const char *msg, unsigned char *buf, uint len);
502
503         extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen,
504                                                     uint key);
505
506 /* multi-bool data type: set of bools, mbool is true if any is set */
507         typedef u32 mbool;
508 #define mboolset(mb, bit)               ((mb) |= (bit)) /* set one bool */
509 #define mboolclr(mb, bit)               ((mb) &= ~(bit))        /* clear one bool */
510 #define mboolisset(mb, bit)             (((mb) & (bit)) != 0)   /* true if one bool is set */
511 #define mboolmaskset(mb, mask, val)     ((mb) = (((mb) & ~(mask)) | (val)))
512
513 /* power conversion */
514         extern u16 bcm_qdbm_to_mw(u8 qdbm);
515         extern u8 bcm_mw_to_qdbm(u16 mw);
516
517         extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
518         extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
519
520         extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf,
521                                 uint len);
522         extern uint bcm_bitcount(u8 *bitmap, uint bytelength);
523
524 #endif                          /* _bcmutils_h_ */