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