| 1 | /* |
| 2 | * Misc useful os-independent macros and functions. |
| 3 | * |
| 4 | * Copyright 2006, 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 | /* ** driver/apps-shared section ** */ |
| 17 | |
| 18 | #define BCME_STRLEN 64 /* Max string length for BCM errors */ |
| 19 | #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST)) |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * error codes could be added but the defined ones shouldn't be changed/deleted |
| 24 | * these error codes are exposed to the user code |
| 25 | * when ever a new error code is added to this list |
| 26 | * please update errorstring table with the related error string and |
| 27 | * update osl files with os specific errorcode map |
| 28 | */ |
| 29 | |
| 30 | #define BCME_OK 0 /* Success */ |
| 31 | #define BCME_ERROR -1 /* Error generic */ |
| 32 | #define BCME_BADARG -2 /* Bad Argument */ |
| 33 | #define BCME_BADOPTION -3 /* Bad option */ |
| 34 | #define BCME_NOTUP -4 /* Not up */ |
| 35 | #define BCME_NOTDOWN -5 /* Not down */ |
| 36 | #define BCME_NOTAP -6 /* Not AP */ |
| 37 | #define BCME_NOTSTA -7 /* Not STA */ |
| 38 | #define BCME_BADKEYIDX -8 /* BAD Key Index */ |
| 39 | #define BCME_RADIOOFF -9 /* Radio Off */ |
| 40 | #define BCME_NOTBANDLOCKED -10 /* Not band locked */ |
| 41 | #define BCME_NOCLK -11 /* No Clock */ |
| 42 | #define BCME_BADRATESET -12 /* BAD Rate valueset */ |
| 43 | #define BCME_BADBAND -13 /* BAD Band */ |
| 44 | #define BCME_BUFTOOSHORT -14 /* Buffer too short */ |
| 45 | #define BCME_BUFTOOLONG -15 /* Buffer too long */ |
| 46 | #define BCME_BUSY -16 /* Busy */ |
| 47 | #define BCME_NOTASSOCIATED -17 /* Not Associated */ |
| 48 | #define BCME_BADSSIDLEN -18 /* Bad SSID len */ |
| 49 | #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */ |
| 50 | #define BCME_BADCHAN -20 /* Bad Channel */ |
| 51 | #define BCME_BADADDR -21 /* Bad Address */ |
| 52 | #define BCME_NORESOURCE -22 /* Not Enough Resources */ |
| 53 | #define BCME_UNSUPPORTED -23 /* Unsupported */ |
| 54 | #define BCME_BADLEN -24 /* Bad length */ |
| 55 | #define BCME_NOTREADY -25 /* Not Ready */ |
| 56 | #define BCME_EPERM -26 /* Not Permitted */ |
| 57 | #define BCME_NOMEM -27 /* No Memory */ |
| 58 | #define BCME_ASSOCIATED -28 /* Associated */ |
| 59 | #define BCME_RANGE -29 /* Not In Range */ |
| 60 | #define BCME_NOTFOUND -30 /* Not Found */ |
| 61 | #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */ |
| 62 | #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */ |
| 63 | #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */ |
| 64 | #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */ |
| 65 | #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */ |
| 66 | #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */ |
| 67 | #define BCME_LAST BCME_DONGLE_DOWN |
| 68 | |
| 69 | /* These are collection of BCME Error strings */ |
| 70 | #define BCMERRSTRINGTABLE { \ |
| 71 | "OK", \ |
| 72 | "Undefined error", \ |
| 73 | "Bad Argument", \ |
| 74 | "Bad Option", \ |
| 75 | "Not up", \ |
| 76 | "Not down", \ |
| 77 | "Not AP", \ |
| 78 | "Not STA", \ |
| 79 | "Bad Key Index", \ |
| 80 | "Radio Off", \ |
| 81 | "Not band locked", \ |
| 82 | "No clock", \ |
| 83 | "Bad Rate valueset", \ |
| 84 | "Bad Band", \ |
| 85 | "Buffer too short", \ |
| 86 | "Buffer too long", \ |
| 87 | "Busy", \ |
| 88 | "Not Associated", \ |
| 89 | "Bad SSID len", \ |
| 90 | "Out of Range Channel", \ |
| 91 | "Bad Channel", \ |
| 92 | "Bad Address", \ |
| 93 | "Not Enough Resources", \ |
| 94 | "Unsupported", \ |
| 95 | "Bad length", \ |
| 96 | "Not Ready", \ |
| 97 | "Not Permitted", \ |
| 98 | "No Memory", \ |
| 99 | "Associated", \ |
| 100 | "Not In Range", \ |
| 101 | "Not Found", \ |
| 102 | "WME Not Enabled", \ |
| 103 | "TSPEC Not Found", \ |
| 104 | "ACM Not Supported", \ |
| 105 | "Not WME Association", \ |
| 106 | "SDIO Bus Error", \ |
| 107 | "Dongle Not Accessible" \ |
| 108 | } |
| 109 | |
| 110 | #ifndef ABS |
| 111 | #define ABS(a) (((a) < 0)?-(a):(a)) |
| 112 | #endif /* ABS */ |
| 113 | |
| 114 | #ifndef MIN |
| 115 | #define MIN(a, b) (((a) < (b))?(a):(b)) |
| 116 | #endif /* MIN */ |
| 117 | |
| 118 | #ifndef MAX |
| 119 | #define MAX(a, b) (((a) > (b))?(a):(b)) |
| 120 | #endif /* MAX */ |
| 121 | |
| 122 | #define CEIL(x, y) (((x) + ((y)-1)) / (y)) |
| 123 | #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) |
| 124 | #define ISALIGNED(a, x) (((a) & ((x)-1)) == 0) |
| 125 | #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0) |
| 126 | #define VALID_MASK(mask) !((mask) & ((mask) + 1)) |
| 127 | #define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member) |
| 128 | #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0])) |
| 129 | |
| 130 | /* bit map related macros */ |
| 131 | #ifndef setbit |
| 132 | #ifndef NBBY /* the BSD family defines NBBY */ |
| 133 | #define NBBY 8 /* 8 bits per byte */ |
| 134 | #endif /* #ifndef NBBY */ |
| 135 | #define setbit(a, i) (((uint8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY)) |
| 136 | #define clrbit(a, i) (((uint8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) |
| 137 | #define isset(a, i) (((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) |
| 138 | #define isclr(a, i) ((((uint8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) |
| 139 | #endif /* setbit */ |
| 140 | |
| 141 | #define NBITS(type) (sizeof(type) * 8) |
| 142 | #define NBITVAL(nbits) (1 << (nbits)) |
| 143 | #define MAXBITVAL(nbits) ((1 << (nbits)) - 1) |
| 144 | #define NBITMASK(nbits) MAXBITVAL(nbits) |
| 145 | #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8) |
| 146 | |
| 147 | /* basic mux operation - can be optimized on several architectures */ |
| 148 | #define MUX(pred, true, false) ((pred) ? (true) : (false)) |
| 149 | |
| 150 | /* modulo inc/dec - assumes x E [0, bound - 1] */ |
| 151 | #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1) |
| 152 | #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1) |
| 153 | |
| 154 | /* modulo inc/dec, bound = 2^k */ |
| 155 | #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1)) |
| 156 | #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1)) |
| 157 | |
| 158 | /* modulo add/sub - assumes x, y E [0, bound - 1] */ |
| 159 | #define MODADD(x, y, bound) \ |
| 160 | MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y)) |
| 161 | #define MODSUB(x, y, bound) \ |
| 162 | MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y)) |
| 163 | |
| 164 | /* module add/sub, bound = 2^k */ |
| 165 | #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1)) |
| 166 | #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1)) |
| 167 | |
| 168 | /* crc defines */ |
| 169 | #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */ |
| 170 | #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */ |
| 171 | #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */ |
| 172 | #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */ |
| 173 | #define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */ |
| 174 | #define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */ |
| 175 | |
| 176 | /* bcm_format_flags() bit description structure */ |
| 177 | typedef struct bcm_bit_desc { |
| 178 | uint32 bit; |
| 179 | char* name; |
| 180 | } bcm_bit_desc_t; |
| 181 | |
| 182 | /* tag_ID/length/value_buffer tuple */ |
| 183 | typedef struct bcm_tlv { |
| 184 | uint8 id; |
| 185 | uint8 len; |
| 186 | uint8 data[1]; |
| 187 | } bcm_tlv_t; |
| 188 | |
| 189 | /* Check that bcm_tlv_t fits into the given buflen */ |
| 190 | #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len)) |
| 191 | |
| 192 | /* buffer length for ethernet address from bcm_ether_ntoa() */ |
| 193 | #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */ |
| 194 | |
| 195 | /* unaligned load and store macros */ |
| 196 | #ifdef IL_BIGENDIAN |
| 197 | static INLINE uint32 |
| 198 | load32_ua(uint8 *a) |
| 199 | { |
| 200 | return ((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]); |
| 201 | } |
| 202 | |
| 203 | static INLINE void |
| 204 | store32_ua(uint8 *a, uint32 v) |
| 205 | { |
| 206 | a[0] = (v >> 24) & 0xff; |
| 207 | a[1] = (v >> 16) & 0xff; |
| 208 | a[2] = (v >> 8) & 0xff; |
| 209 | a[3] = v & 0xff; |
| 210 | } |
| 211 | |
| 212 | static INLINE uint16 |
| 213 | load16_ua(uint8 *a) |
| 214 | { |
| 215 | return ((a[0] << 8) | a[1]); |
| 216 | } |
| 217 | |
| 218 | static INLINE void |
| 219 | store16_ua(uint8 *a, uint16 v) |
| 220 | { |
| 221 | a[0] = (v >> 8) & 0xff; |
| 222 | a[1] = v & 0xff; |
| 223 | } |
| 224 | |
| 225 | #else |
| 226 | |
| 227 | static INLINE uint32 |
| 228 | load32_ua(uint8 *a) |
| 229 | { |
| 230 | return ((a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]); |
| 231 | } |
| 232 | |
| 233 | static INLINE void |
| 234 | store32_ua(uint8 *a, uint32 v) |
| 235 | { |
| 236 | a[3] = (v >> 24) & 0xff; |
| 237 | a[2] = (v >> 16) & 0xff; |
| 238 | a[1] = (v >> 8) & 0xff; |
| 239 | a[0] = v & 0xff; |
| 240 | } |
| 241 | |
| 242 | static INLINE uint16 |
| 243 | load16_ua(uint8 *a) |
| 244 | { |
| 245 | return ((a[1] << 8) | a[0]); |
| 246 | } |
| 247 | |
| 248 | static INLINE void |
| 249 | store16_ua(uint8 *a, uint16 v) |
| 250 | { |
| 251 | a[1] = (v >> 8) & 0xff; |
| 252 | a[0] = v & 0xff; |
| 253 | } |
| 254 | |
| 255 | #endif /* IL_BIGENDIAN */ |
| 256 | |
| 257 | #endif /* _bcmutils_h_ */ |
| 258 | |