bcmutils.h 7.5 KB

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