/drivers/net/wireless/bcm4329/bcmutils.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 1838 lines · 1260 code · 330 blank · 248 comment · 321 complexity · ac1fadb9fd973ee1e14c43798345cefa MD5 · raw file

  1. /*
  2. * Driver O/S-independent utility routines
  3. *
  4. * Copyright (C) 1999-2010, Broadcom Corporation
  5. *
  6. * Unless you and Broadcom execute a separate written software license
  7. * agreement governing use of this software, this software is licensed to you
  8. * under the terms of the GNU General Public License version 2 (the "GPL"),
  9. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  10. * following added to such license:
  11. *
  12. * As a special exception, the copyright holders of this software give you
  13. * permission to link this software with independent modules, and to copy and
  14. * distribute the resulting executable under terms of your choice, provided that
  15. * you also meet, for each linked independent module, the terms and conditions of
  16. * the license of that module. An independent module is a module which is not
  17. * derived from this software. The special exception does not apply to any
  18. * modifications of the software.
  19. *
  20. * Notwithstanding the above, under no circumstances may you combine this
  21. * software in any way with any other Broadcom software provided under a license
  22. * other than the GPL, without Broadcom's express prior written consent.
  23. * $Id: bcmutils.c,v 1.210.4.5.2.4.6.19 2010/04/26 06:05:25 Exp $
  24. */
  25. #include <typedefs.h>
  26. #include <bcmdefs.h>
  27. #include <stdarg.h>
  28. #include <bcmutils.h>
  29. #ifdef BCMDRIVER
  30. #include <osl.h>
  31. #include <siutils.h>
  32. #else
  33. #include <stdio.h>
  34. #include <string.h>
  35. /* This case for external supplicant use */
  36. #if defined(BCMEXTSUP)
  37. #include <bcm_osl.h>
  38. #endif
  39. #endif /* BCMDRIVER */
  40. #include <bcmendian.h>
  41. #include <bcmdevs.h>
  42. #include <proto/ethernet.h>
  43. #include <proto/vlan.h>
  44. #include <proto/bcmip.h>
  45. #include <proto/802.1d.h>
  46. #include <proto/802.11.h>
  47. #ifdef BCMDRIVER
  48. /* copy a pkt buffer chain into a buffer */
  49. uint
  50. pktcopy(osl_t *osh, void *p, uint offset, int len, uchar *buf)
  51. {
  52. uint n, ret = 0;
  53. if (len < 0)
  54. len = 4096; /* "infinite" */
  55. /* skip 'offset' bytes */
  56. for (; p && offset; p = PKTNEXT(osh, p)) {
  57. if (offset < (uint)PKTLEN(osh, p))
  58. break;
  59. offset -= PKTLEN(osh, p);
  60. }
  61. if (!p)
  62. return 0;
  63. /* copy the data */
  64. for (; p && len; p = PKTNEXT(osh, p)) {
  65. n = MIN((uint)PKTLEN(osh, p) - offset, (uint)len);
  66. bcopy(PKTDATA(osh, p) + offset, buf, n);
  67. buf += n;
  68. len -= n;
  69. ret += n;
  70. offset = 0;
  71. }
  72. return ret;
  73. }
  74. /* copy a buffer into a pkt buffer chain */
  75. uint
  76. pktfrombuf(osl_t *osh, void *p, uint offset, int len, uchar *buf)
  77. {
  78. uint n, ret = 0;
  79. /* skip 'offset' bytes */
  80. for (; p && offset; p = PKTNEXT(osh, p)) {
  81. if (offset < (uint)PKTLEN(osh, p))
  82. break;
  83. offset -= PKTLEN(osh, p);
  84. }
  85. if (!p)
  86. return 0;
  87. /* copy the data */
  88. for (; p && len; p = PKTNEXT(osh, p)) {
  89. n = MIN((uint)PKTLEN(osh, p) - offset, (uint)len);
  90. bcopy(buf, PKTDATA(osh, p) + offset, n);
  91. buf += n;
  92. len -= n;
  93. ret += n;
  94. offset = 0;
  95. }
  96. return ret;
  97. }
  98. /* return total length of buffer chain */
  99. uint
  100. pkttotlen(osl_t *osh, void *p)
  101. {
  102. uint total;
  103. total = 0;
  104. for (; p; p = PKTNEXT(osh, p))
  105. total += PKTLEN(osh, p);
  106. return (total);
  107. }
  108. /* return the last buffer of chained pkt */
  109. void *
  110. pktlast(osl_t *osh, void *p)
  111. {
  112. for (; PKTNEXT(osh, p); p = PKTNEXT(osh, p))
  113. ;
  114. return (p);
  115. }
  116. /* count segments of a chained packet */
  117. uint
  118. pktsegcnt(osl_t *osh, void *p)
  119. {
  120. uint cnt;
  121. for (cnt = 0; p; p = PKTNEXT(osh, p))
  122. cnt++;
  123. return cnt;
  124. }
  125. /*
  126. * osl multiple-precedence packet queue
  127. * hi_prec is always >= the number of the highest non-empty precedence
  128. */
  129. void *
  130. pktq_penq(struct pktq *pq, int prec, void *p)
  131. {
  132. struct pktq_prec *q;
  133. ASSERT(prec >= 0 && prec < pq->num_prec);
  134. ASSERT(PKTLINK(p) == NULL); /* queueing chains not allowed */
  135. ASSERT(!pktq_full(pq));
  136. ASSERT(!pktq_pfull(pq, prec));
  137. q = &pq->q[prec];
  138. if (q->head)
  139. PKTSETLINK(q->tail, p);
  140. else
  141. q->head = p;
  142. q->tail = p;
  143. q->len++;
  144. pq->len++;
  145. if (pq->hi_prec < prec)
  146. pq->hi_prec = (uint8)prec;
  147. return p;
  148. }
  149. void *
  150. pktq_penq_head(struct pktq *pq, int prec, void *p)
  151. {
  152. struct pktq_prec *q;
  153. ASSERT(prec >= 0 && prec < pq->num_prec);
  154. ASSERT(PKTLINK(p) == NULL); /* queueing chains not allowed */
  155. ASSERT(!pktq_full(pq));
  156. ASSERT(!pktq_pfull(pq, prec));
  157. q = &pq->q[prec];
  158. if (q->head == NULL)
  159. q->tail = p;
  160. PKTSETLINK(p, q->head);
  161. q->head = p;
  162. q->len++;
  163. pq->len++;
  164. if (pq->hi_prec < prec)
  165. pq->hi_prec = (uint8)prec;
  166. return p;
  167. }
  168. void *
  169. pktq_pdeq(struct pktq *pq, int prec)
  170. {
  171. struct pktq_prec *q;
  172. void *p;
  173. ASSERT(prec >= 0 && prec < pq->num_prec);
  174. q = &pq->q[prec];
  175. if ((p = q->head) == NULL)
  176. return NULL;
  177. if ((q->head = PKTLINK(p)) == NULL)
  178. q->tail = NULL;
  179. q->len--;
  180. pq->len--;
  181. PKTSETLINK(p, NULL);
  182. return p;
  183. }
  184. void *
  185. pktq_pdeq_tail(struct pktq *pq, int prec)
  186. {
  187. struct pktq_prec *q;
  188. void *p, *prev;
  189. ASSERT(prec >= 0 && prec < pq->num_prec);
  190. q = &pq->q[prec];
  191. if ((p = q->head) == NULL)
  192. return NULL;
  193. for (prev = NULL; p != q->tail; p = PKTLINK(p))
  194. prev = p;
  195. if (prev)
  196. PKTSETLINK(prev, NULL);
  197. else
  198. q->head = NULL;
  199. q->tail = prev;
  200. q->len--;
  201. pq->len--;
  202. return p;
  203. }
  204. void
  205. pktq_pflush(osl_t *osh, struct pktq *pq, int prec, bool dir)
  206. {
  207. struct pktq_prec *q;
  208. void *p;
  209. q = &pq->q[prec];
  210. p = q->head;
  211. while (p) {
  212. q->head = PKTLINK(p);
  213. PKTSETLINK(p, NULL);
  214. PKTFREE(osh, p, dir);
  215. q->len--;
  216. pq->len--;
  217. p = q->head;
  218. }
  219. ASSERT(q->len == 0);
  220. q->tail = NULL;
  221. }
  222. bool
  223. pktq_pdel(struct pktq *pq, void *pktbuf, int prec)
  224. {
  225. struct pktq_prec *q;
  226. void *p;
  227. ASSERT(prec >= 0 && prec < pq->num_prec);
  228. if (!pktbuf)
  229. return FALSE;
  230. q = &pq->q[prec];
  231. if (q->head == pktbuf) {
  232. if ((q->head = PKTLINK(pktbuf)) == NULL)
  233. q->tail = NULL;
  234. } else {
  235. for (p = q->head; p && PKTLINK(p) != pktbuf; p = PKTLINK(p))
  236. ;
  237. if (p == NULL)
  238. return FALSE;
  239. PKTSETLINK(p, PKTLINK(pktbuf));
  240. if (q->tail == pktbuf)
  241. q->tail = p;
  242. }
  243. q->len--;
  244. pq->len--;
  245. PKTSETLINK(pktbuf, NULL);
  246. return TRUE;
  247. }
  248. void
  249. pktq_init(struct pktq *pq, int num_prec, int max_len)
  250. {
  251. int prec;
  252. ASSERT(num_prec > 0 && num_prec <= PKTQ_MAX_PREC);
  253. /* pq is variable size; only zero out what's requested */
  254. bzero(pq, OFFSETOF(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec));
  255. pq->num_prec = (uint16)num_prec;
  256. pq->max = (uint16)max_len;
  257. for (prec = 0; prec < num_prec; prec++)
  258. pq->q[prec].max = pq->max;
  259. }
  260. void *
  261. pktq_deq(struct pktq *pq, int *prec_out)
  262. {
  263. struct pktq_prec *q;
  264. void *p;
  265. int prec;
  266. if (pq->len == 0)
  267. return NULL;
  268. while ((prec = pq->hi_prec) > 0 && pq->q[prec].head == NULL)
  269. pq->hi_prec--;
  270. q = &pq->q[prec];
  271. if ((p = q->head) == NULL)
  272. return NULL;
  273. if ((q->head = PKTLINK(p)) == NULL)
  274. q->tail = NULL;
  275. q->len--;
  276. pq->len--;
  277. if (prec_out)
  278. *prec_out = prec;
  279. PKTSETLINK(p, NULL);
  280. return p;
  281. }
  282. void *
  283. pktq_deq_tail(struct pktq *pq, int *prec_out)
  284. {
  285. struct pktq_prec *q;
  286. void *p, *prev;
  287. int prec;
  288. if (pq->len == 0)
  289. return NULL;
  290. for (prec = 0; prec < pq->hi_prec; prec++)
  291. if (pq->q[prec].head)
  292. break;
  293. q = &pq->q[prec];
  294. if ((p = q->head) == NULL)
  295. return NULL;
  296. for (prev = NULL; p != q->tail; p = PKTLINK(p))
  297. prev = p;
  298. if (prev)
  299. PKTSETLINK(prev, NULL);
  300. else
  301. q->head = NULL;
  302. q->tail = prev;
  303. q->len--;
  304. pq->len--;
  305. if (prec_out)
  306. *prec_out = prec;
  307. PKTSETLINK(p, NULL);
  308. return p;
  309. }
  310. void *
  311. pktq_peek(struct pktq *pq, int *prec_out)
  312. {
  313. int prec;
  314. if (pq->len == 0)
  315. return NULL;
  316. while ((prec = pq->hi_prec) > 0 && pq->q[prec].head == NULL)
  317. pq->hi_prec--;
  318. if (prec_out)
  319. *prec_out = prec;
  320. return (pq->q[prec].head);
  321. }
  322. void *
  323. pktq_peek_tail(struct pktq *pq, int *prec_out)
  324. {
  325. int prec;
  326. if (pq->len == 0)
  327. return NULL;
  328. for (prec = 0; prec < pq->hi_prec; prec++)
  329. if (pq->q[prec].head)
  330. break;
  331. if (prec_out)
  332. *prec_out = prec;
  333. return (pq->q[prec].tail);
  334. }
  335. void
  336. pktq_flush(osl_t *osh, struct pktq *pq, bool dir)
  337. {
  338. int prec;
  339. for (prec = 0; prec < pq->num_prec; prec++)
  340. pktq_pflush(osh, pq, prec, dir);
  341. ASSERT(pq->len == 0);
  342. }
  343. /* Return sum of lengths of a specific set of precedences */
  344. int
  345. pktq_mlen(struct pktq *pq, uint prec_bmp)
  346. {
  347. int prec, len;
  348. len = 0;
  349. for (prec = 0; prec <= pq->hi_prec; prec++)
  350. if (prec_bmp & (1 << prec))
  351. len += pq->q[prec].len;
  352. return len;
  353. }
  354. /* Priority dequeue from a specific set of precedences */
  355. void *
  356. pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out)
  357. {
  358. struct pktq_prec *q;
  359. void *p;
  360. int prec;
  361. if (pq->len == 0)
  362. return NULL;
  363. while ((prec = pq->hi_prec) > 0 && pq->q[prec].head == NULL)
  364. pq->hi_prec--;
  365. while ((prec_bmp & (1 << prec)) == 0 || pq->q[prec].head == NULL)
  366. if (prec-- == 0)
  367. return NULL;
  368. q = &pq->q[prec];
  369. if ((p = q->head) == NULL)
  370. return NULL;
  371. if ((q->head = PKTLINK(p)) == NULL)
  372. q->tail = NULL;
  373. q->len--;
  374. if (prec_out)
  375. *prec_out = prec;
  376. pq->len--;
  377. PKTSETLINK(p, NULL);
  378. return p;
  379. }
  380. #endif /* BCMDRIVER */
  381. const unsigned char bcm_ctype[] = {
  382. _BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C, /* 0-7 */
  383. _BCM_C, _BCM_C|_BCM_S, _BCM_C|_BCM_S, _BCM_C|_BCM_S, _BCM_C|_BCM_S, _BCM_C|_BCM_S, _BCM_C,
  384. _BCM_C, /* 8-15 */
  385. _BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C, /* 16-23 */
  386. _BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C,_BCM_C, /* 24-31 */
  387. _BCM_S|_BCM_SP,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P, /* 32-39 */
  388. _BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P, /* 40-47 */
  389. _BCM_D,_BCM_D,_BCM_D,_BCM_D,_BCM_D,_BCM_D,_BCM_D,_BCM_D, /* 48-55 */
  390. _BCM_D,_BCM_D,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P, /* 56-63 */
  391. _BCM_P, _BCM_U|_BCM_X, _BCM_U|_BCM_X, _BCM_U|_BCM_X, _BCM_U|_BCM_X, _BCM_U|_BCM_X,
  392. _BCM_U|_BCM_X, _BCM_U, /* 64-71 */
  393. _BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U, /* 72-79 */
  394. _BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U,_BCM_U, /* 80-87 */
  395. _BCM_U,_BCM_U,_BCM_U,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_P, /* 88-95 */
  396. _BCM_P, _BCM_L|_BCM_X, _BCM_L|_BCM_X, _BCM_L|_BCM_X, _BCM_L|_BCM_X, _BCM_L|_BCM_X,
  397. _BCM_L|_BCM_X, _BCM_L, /* 96-103 */
  398. _BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L, /* 104-111 */
  399. _BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L,_BCM_L, /* 112-119 */
  400. _BCM_L,_BCM_L,_BCM_L,_BCM_P,_BCM_P,_BCM_P,_BCM_P,_BCM_C, /* 120-127 */
  401. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128-143 */
  402. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 144-159 */
  403. _BCM_S|_BCM_SP, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P,
  404. _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, /* 160-175 */
  405. _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P,
  406. _BCM_P, _BCM_P, _BCM_P, _BCM_P, _BCM_P, /* 176-191 */
  407. _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U,
  408. _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, /* 192-207 */
  409. _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_P, _BCM_U, _BCM_U, _BCM_U,
  410. _BCM_U, _BCM_U, _BCM_U, _BCM_U, _BCM_L, /* 208-223 */
  411. _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L,
  412. _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, /* 224-239 */
  413. _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_P, _BCM_L, _BCM_L, _BCM_L,
  414. _BCM_L, _BCM_L, _BCM_L, _BCM_L, _BCM_L /* 240-255 */
  415. };
  416. ulong
  417. bcm_strtoul(char *cp, char **endp, uint base)
  418. {
  419. ulong result, last_result = 0, value;
  420. bool minus;
  421. minus = FALSE;
  422. while (bcm_isspace(*cp))
  423. cp++;
  424. if (cp[0] == '+')
  425. cp++;
  426. else if (cp[0] == '-') {
  427. minus = TRUE;
  428. cp++;
  429. }
  430. if (base == 0) {
  431. if (cp[0] == '0') {
  432. if ((cp[1] == 'x') || (cp[1] == 'X')) {
  433. base = 16;
  434. cp = &cp[2];
  435. } else {
  436. base = 8;
  437. cp = &cp[1];
  438. }
  439. } else
  440. base = 10;
  441. } else if (base == 16 && (cp[0] == '0') && ((cp[1] == 'x') || (cp[1] == 'X'))) {
  442. cp = &cp[2];
  443. }
  444. result = 0;
  445. while (bcm_isxdigit(*cp) &&
  446. (value = bcm_isdigit(*cp) ? *cp-'0' : bcm_toupper(*cp)-'A'+10) < base) {
  447. result = result*base + value;
  448. /* Detected overflow */
  449. if (result < last_result && !minus)
  450. return (ulong)-1;
  451. last_result = result;
  452. cp++;
  453. }
  454. if (minus)
  455. result = (ulong)(-(long)result);
  456. if (endp)
  457. *endp = (char *)cp;
  458. return (result);
  459. }
  460. int
  461. bcm_atoi(char *s)
  462. {
  463. return (int)bcm_strtoul(s, NULL, 10);
  464. }
  465. /* return pointer to location of substring 'needle' in 'haystack' */
  466. char*
  467. bcmstrstr(char *haystack, char *needle)
  468. {
  469. int len, nlen;
  470. int i;
  471. if ((haystack == NULL) || (needle == NULL))
  472. return (haystack);
  473. nlen = strlen(needle);
  474. len = strlen(haystack) - nlen + 1;
  475. for (i = 0; i < len; i++)
  476. if (memcmp(needle, &haystack[i], nlen) == 0)
  477. return (&haystack[i]);
  478. return (NULL);
  479. }
  480. char*
  481. bcmstrcat(char *dest, const char *src)
  482. {
  483. char *p;
  484. p = dest + strlen(dest);
  485. while ((*p++ = *src++) != '\0')
  486. ;
  487. return (dest);
  488. }
  489. char*
  490. bcmstrncat(char *dest, const char *src, uint size)
  491. {
  492. char *endp;
  493. char *p;
  494. p = dest + strlen(dest);
  495. endp = p + size;
  496. while (p != endp && (*p++ = *src++) != '\0')
  497. ;
  498. return (dest);
  499. }
  500. /****************************************************************************
  501. * Function: bcmstrtok
  502. *
  503. * Purpose:
  504. * Tokenizes a string. This function is conceptually similiar to ANSI C strtok(),
  505. * but allows strToken() to be used by different strings or callers at the same
  506. * time. Each call modifies '*string' by substituting a NULL character for the
  507. * first delimiter that is encountered, and updates 'string' to point to the char
  508. * after the delimiter. Leading delimiters are skipped.
  509. *
  510. * Parameters:
  511. * string (mod) Ptr to string ptr, updated by token.
  512. * delimiters (in) Set of delimiter characters.
  513. * tokdelim (out) Character that delimits the returned token. (May
  514. * be set to NULL if token delimiter is not required).
  515. *
  516. * Returns: Pointer to the next token found. NULL when no more tokens are found.
  517. *****************************************************************************
  518. */
  519. char *
  520. bcmstrtok(char **string, const char *delimiters, char *tokdelim)
  521. {
  522. unsigned char *str;
  523. unsigned long map[8];
  524. int count;
  525. char *nextoken;
  526. if (tokdelim != NULL) {
  527. /* Prime the token delimiter */
  528. *tokdelim = '\0';
  529. }
  530. /* Clear control map */
  531. for (count = 0; count < 8; count++) {
  532. map[count] = 0;
  533. }
  534. /* Set bits in delimiter table */
  535. do {
  536. map[*delimiters >> 5] |= (1 << (*delimiters & 31));
  537. }
  538. while (*delimiters++);
  539. str = (unsigned char*)*string;
  540. /* Find beginning of token (skip over leading delimiters). Note that
  541. * there is no token iff this loop sets str to point to the terminal
  542. * null (*str == '\0')
  543. */
  544. while (((map[*str >> 5] & (1 << (*str & 31))) && *str) || (*str == ' ')) {
  545. str++;
  546. }
  547. nextoken = (char*)str;
  548. /* Find the end of the token. If it is not the end of the string,
  549. * put a null there.
  550. */
  551. for (; *str; str++) {
  552. if (map[*str >> 5] & (1 << (*str & 31))) {
  553. if (tokdelim != NULL) {
  554. *tokdelim = *str;
  555. }
  556. *str++ = '\0';
  557. break;
  558. }
  559. }
  560. *string = (char*)str;
  561. /* Determine if a token has been found. */
  562. if (nextoken == (char *) str) {
  563. return NULL;
  564. }
  565. else {
  566. return nextoken;
  567. }
  568. }
  569. #define xToLower(C) \
  570. ((C >= 'A' && C <= 'Z') ? (char)((int)C - (int)'A' + (int)'a') : C)
  571. /****************************************************************************
  572. * Function: bcmstricmp
  573. *
  574. * Purpose: Compare to strings case insensitively.
  575. *
  576. * Parameters: s1 (in) First string to compare.
  577. * s2 (in) Second string to compare.
  578. *
  579. * Returns: Return 0 if the two strings are equal, -1 if t1 < t2 and 1 if
  580. * t1 > t2, when ignoring case sensitivity.
  581. *****************************************************************************
  582. */
  583. int
  584. bcmstricmp(const char *s1, const char *s2)
  585. {
  586. char dc, sc;
  587. while (*s2 && *s1) {
  588. dc = xToLower(*s1);
  589. sc = xToLower(*s2);
  590. if (dc < sc) return -1;
  591. if (dc > sc) return 1;
  592. s1++;
  593. s2++;
  594. }
  595. if (*s1 && !*s2) return 1;
  596. if (!*s1 && *s2) return -1;
  597. return 0;
  598. }
  599. /****************************************************************************
  600. * Function: bcmstrnicmp
  601. *
  602. * Purpose: Compare to strings case insensitively, upto a max of 'cnt'
  603. * characters.
  604. *
  605. * Parameters: s1 (in) First string to compare.
  606. * s2 (in) Second string to compare.
  607. * cnt (in) Max characters to compare.
  608. *
  609. * Returns: Return 0 if the two strings are equal, -1 if t1 < t2 and 1 if
  610. * t1 > t2, when ignoring case sensitivity.
  611. *****************************************************************************
  612. */
  613. int
  614. bcmstrnicmp(const char* s1, const char* s2, int cnt)
  615. {
  616. char dc, sc;
  617. while (*s2 && *s1 && cnt) {
  618. dc = xToLower(*s1);
  619. sc = xToLower(*s2);
  620. if (dc < sc) return -1;
  621. if (dc > sc) return 1;
  622. s1++;
  623. s2++;
  624. cnt--;
  625. }
  626. if (!cnt) return 0;
  627. if (*s1 && !*s2) return 1;
  628. if (!*s1 && *s2) return -1;
  629. return 0;
  630. }
  631. /* parse a xx:xx:xx:xx:xx:xx format ethernet address */
  632. int
  633. bcm_ether_atoe(char *p, struct ether_addr *ea)
  634. {
  635. int i = 0;
  636. for (;;) {
  637. ea->octet[i++] = (char) bcm_strtoul(p, &p, 16);
  638. if (!*p++ || i == 6)
  639. break;
  640. }
  641. return (i == 6);
  642. }
  643. #if defined(CONFIG_USBRNDIS_RETAIL) || defined(NDIS_MINIPORT_DRIVER)
  644. /* registry routine buffer preparation utility functions:
  645. * parameter order is like strncpy, but returns count
  646. * of bytes copied. Minimum bytes copied is null char(1)/wchar(2)
  647. */
  648. ulong
  649. wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen)
  650. {
  651. ulong copyct = 1;
  652. ushort i;
  653. if (abuflen == 0)
  654. return 0;
  655. /* wbuflen is in bytes */
  656. wbuflen /= sizeof(ushort);
  657. for (i = 0; i < wbuflen; ++i) {
  658. if (--abuflen == 0)
  659. break;
  660. *abuf++ = (char) *wbuf++;
  661. ++copyct;
  662. }
  663. *abuf = '\0';
  664. return copyct;
  665. }
  666. #endif /* CONFIG_USBRNDIS_RETAIL || NDIS_MINIPORT_DRIVER */
  667. char *
  668. bcm_ether_ntoa(const struct ether_addr *ea, char *buf)
  669. {
  670. static const char template[] = "%02x:%02x:%02x:%02x:%02x:%02x";
  671. snprintf(buf, 18, template,
  672. ea->octet[0]&0xff, ea->octet[1]&0xff, ea->octet[2]&0xff,
  673. ea->octet[3]&0xff, ea->octet[4]&0xff, ea->octet[5]&0xff);
  674. return (buf);
  675. }
  676. char *
  677. bcm_ip_ntoa(struct ipv4_addr *ia, char *buf)
  678. {
  679. snprintf(buf, 16, "%d.%d.%d.%d",
  680. ia->addr[0], ia->addr[1], ia->addr[2], ia->addr[3]);
  681. return (buf);
  682. }
  683. #ifdef BCMDRIVER
  684. void
  685. bcm_mdelay(uint ms)
  686. {
  687. uint i;
  688. for (i = 0; i < ms; i++) {
  689. OSL_DELAY(1000);
  690. }
  691. }
  692. #if defined(DHD_DEBUG)
  693. /* pretty hex print a pkt buffer chain */
  694. void
  695. prpkt(const char *msg, osl_t *osh, void *p0)
  696. {
  697. void *p;
  698. if (msg && (msg[0] != '\0'))
  699. printf("%s:\n", msg);
  700. for (p = p0; p; p = PKTNEXT(osh, p))
  701. prhex(NULL, PKTDATA(osh, p), PKTLEN(osh, p));
  702. }
  703. #endif
  704. /* Takes an Ethernet frame and sets out-of-bound PKTPRIO.
  705. * Also updates the inplace vlan tag if requested.
  706. * For debugging, it returns an indication of what it did.
  707. */
  708. uint
  709. pktsetprio(void *pkt, bool update_vtag)
  710. {
  711. struct ether_header *eh;
  712. struct ethervlan_header *evh;
  713. uint8 *pktdata;
  714. int priority = 0;
  715. int rc = 0;
  716. pktdata = (uint8 *) PKTDATA(NULL, pkt);
  717. ASSERT(ISALIGNED((uintptr)pktdata, sizeof(uint16)));
  718. eh = (struct ether_header *) pktdata;
  719. if (ntoh16(eh->ether_type) == ETHER_TYPE_8021Q) {
  720. uint16 vlan_tag;
  721. int vlan_prio, dscp_prio = 0;
  722. evh = (struct ethervlan_header *)eh;
  723. vlan_tag = ntoh16(evh->vlan_tag);
  724. vlan_prio = (int) (vlan_tag >> VLAN_PRI_SHIFT) & VLAN_PRI_MASK;
  725. if (ntoh16(evh->ether_type) == ETHER_TYPE_IP) {
  726. uint8 *ip_body = pktdata + sizeof(struct ethervlan_header);
  727. uint8 tos_tc = IP_TOS(ip_body);
  728. dscp_prio = (int)(tos_tc >> IPV4_TOS_PREC_SHIFT);
  729. }
  730. /* DSCP priority gets precedence over 802.1P (vlan tag) */
  731. if (dscp_prio != 0) {
  732. priority = dscp_prio;
  733. rc |= PKTPRIO_VDSCP;
  734. } else {
  735. priority = vlan_prio;
  736. rc |= PKTPRIO_VLAN;
  737. }
  738. /*
  739. * If the DSCP priority is not the same as the VLAN priority,
  740. * then overwrite the priority field in the vlan tag, with the
  741. * DSCP priority value. This is required for Linux APs because
  742. * the VLAN driver on Linux, overwrites the skb->priority field
  743. * with the priority value in the vlan tag
  744. */
  745. if (update_vtag && (priority != vlan_prio)) {
  746. vlan_tag &= ~(VLAN_PRI_MASK << VLAN_PRI_SHIFT);
  747. vlan_tag |= (uint16)priority << VLAN_PRI_SHIFT;
  748. evh->vlan_tag = hton16(vlan_tag);
  749. rc |= PKTPRIO_UPD;
  750. }
  751. } else if (ntoh16(eh->ether_type) == ETHER_TYPE_IP) {
  752. uint8 *ip_body = pktdata + sizeof(struct ether_header);
  753. uint8 tos_tc = IP_TOS(ip_body);
  754. priority = (int)(tos_tc >> IPV4_TOS_PREC_SHIFT);
  755. rc |= PKTPRIO_DSCP;
  756. }
  757. ASSERT(priority >= 0 && priority <= MAXPRIO);
  758. PKTSETPRIO(pkt, priority);
  759. return (rc | priority);
  760. }
  761. static char bcm_undeferrstr[BCME_STRLEN];
  762. static const char *bcmerrorstrtable[] = BCMERRSTRINGTABLE;
  763. /* Convert the error codes into related error strings */
  764. const char *
  765. bcmerrorstr(int bcmerror)
  766. {
  767. /* check if someone added a bcmerror code but forgot to add errorstring */
  768. ASSERT(ABS(BCME_LAST) == (ARRAYSIZE(bcmerrorstrtable) - 1));
  769. if (bcmerror > 0 || bcmerror < BCME_LAST) {
  770. snprintf(bcm_undeferrstr, BCME_STRLEN, "Undefined error %d", bcmerror);
  771. return bcm_undeferrstr;
  772. }
  773. ASSERT(strlen(bcmerrorstrtable[-bcmerror]) < BCME_STRLEN);
  774. return bcmerrorstrtable[-bcmerror];
  775. }
  776. /* iovar table lookup */
  777. const bcm_iovar_t*
  778. bcm_iovar_lookup(const bcm_iovar_t *table, const char *name)
  779. {
  780. const bcm_iovar_t *vi;
  781. const char *lookup_name;
  782. /* skip any ':' delimited option prefixes */
  783. lookup_name = strrchr(name, ':');
  784. if (lookup_name != NULL)
  785. lookup_name++;
  786. else
  787. lookup_name = name;
  788. ASSERT(table != NULL);
  789. for (vi = table; vi->name; vi++) {
  790. if (!strcmp(vi->name, lookup_name))
  791. return vi;
  792. }
  793. /* ran to end of table */
  794. return NULL; /* var name not found */
  795. }
  796. int
  797. bcm_iovar_lencheck(const bcm_iovar_t *vi, void *arg, int len, bool set)
  798. {
  799. int bcmerror = 0;
  800. /* length check on io buf */
  801. switch (vi->type) {
  802. case IOVT_BOOL:
  803. case IOVT_INT8:
  804. case IOVT_INT16:
  805. case IOVT_INT32:
  806. case IOVT_UINT8:
  807. case IOVT_UINT16:
  808. case IOVT_UINT32:
  809. /* all integers are int32 sized args at the ioctl interface */
  810. if (len < (int)sizeof(int)) {
  811. bcmerror = BCME_BUFTOOSHORT;
  812. }
  813. break;
  814. case IOVT_BUFFER:
  815. /* buffer must meet minimum length requirement */
  816. if (len < vi->minlen) {
  817. bcmerror = BCME_BUFTOOSHORT;
  818. }
  819. break;
  820. case IOVT_VOID:
  821. if (!set) {
  822. /* Cannot return nil... */
  823. bcmerror = BCME_UNSUPPORTED;
  824. } else if (len) {
  825. /* Set is an action w/o parameters */
  826. bcmerror = BCME_BUFTOOLONG;
  827. }
  828. break;
  829. default:
  830. /* unknown type for length check in iovar info */
  831. ASSERT(0);
  832. bcmerror = BCME_UNSUPPORTED;
  833. }
  834. return bcmerror;
  835. }
  836. #endif /* BCMDRIVER */
  837. /*******************************************************************************
  838. * crc8
  839. *
  840. * Computes a crc8 over the input data using the polynomial:
  841. *
  842. * x^8 + x^7 +x^6 + x^4 + x^2 + 1
  843. *
  844. * The caller provides the initial value (either CRC8_INIT_VALUE
  845. * or the previous returned value) to allow for processing of
  846. * discontiguous blocks of data. When generating the CRC the
  847. * caller is responsible for complementing the final return value
  848. * and inserting it into the byte stream. When checking, a final
  849. * return value of CRC8_GOOD_VALUE indicates a valid CRC.
  850. *
  851. * Reference: Dallas Semiconductor Application Note 27
  852. * Williams, Ross N., "A Painless Guide to CRC Error Detection Algorithms",
  853. * ver 3, Aug 1993, ross@guest.adelaide.edu.au, Rocksoft Pty Ltd.,
  854. * ftp://ftp.rocksoft.com/clients/rocksoft/papers/crc_v3.txt
  855. *
  856. * ****************************************************************************
  857. */
  858. STATIC const uint8 crc8_table[256] = {
  859. 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
  860. 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
  861. 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
  862. 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
  863. 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
  864. 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
  865. 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
  866. 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
  867. 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
  868. 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
  869. 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
  870. 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
  871. 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
  872. 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
  873. 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
  874. 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
  875. 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
  876. 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
  877. 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
  878. 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
  879. 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
  880. 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
  881. 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
  882. 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
  883. 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
  884. 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
  885. 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
  886. 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
  887. 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
  888. 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
  889. 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
  890. 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F
  891. };
  892. #define CRC_INNER_LOOP(n, c, x) \
  893. (c) = ((c) >> 8) ^ crc##n##_table[((c) ^ (x)) & 0xff]
  894. uint8
  895. hndcrc8(
  896. uint8 *pdata, /* pointer to array of data to process */
  897. uint nbytes, /* number of input data bytes to process */
  898. uint8 crc /* either CRC8_INIT_VALUE or previous return value */
  899. )
  900. {
  901. /* hard code the crc loop instead of using CRC_INNER_LOOP macro
  902. * to avoid the undefined and unnecessary (uint8 >> 8) operation.
  903. */
  904. while (nbytes-- > 0)
  905. crc = crc8_table[(crc ^ *pdata++) & 0xff];
  906. return crc;
  907. }
  908. /*******************************************************************************
  909. * crc16
  910. *
  911. * Computes a crc16 over the input data using the polynomial:
  912. *
  913. * x^16 + x^12 +x^5 + 1
  914. *
  915. * The caller provides the initial value (either CRC16_INIT_VALUE
  916. * or the previous returned value) to allow for processing of
  917. * discontiguous blocks of data. When generating the CRC the
  918. * caller is responsible for complementing the final return value
  919. * and inserting it into the byte stream. When checking, a final
  920. * return value of CRC16_GOOD_VALUE indicates a valid CRC.
  921. *
  922. * Reference: Dallas Semiconductor Application Note 27
  923. * Williams, Ross N., "A Painless Guide to CRC Error Detection Algorithms",
  924. * ver 3, Aug 1993, ross@guest.adelaide.edu.au, Rocksoft Pty Ltd.,
  925. * ftp://ftp.rocksoft.com/clients/rocksoft/papers/crc_v3.txt
  926. *
  927. * ****************************************************************************
  928. */
  929. static const uint16 crc16_table[256] = {
  930. 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
  931. 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
  932. 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
  933. 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
  934. 0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
  935. 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
  936. 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
  937. 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
  938. 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
  939. 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
  940. 0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
  941. 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
  942. 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
  943. 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
  944. 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
  945. 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
  946. 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
  947. 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
  948. 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
  949. 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
  950. 0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
  951. 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
  952. 0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
  953. 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
  954. 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
  955. 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
  956. 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
  957. 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
  958. 0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
  959. 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
  960. 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
  961. 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78
  962. };
  963. uint16
  964. hndcrc16(
  965. uint8 *pdata, /* pointer to array of data to process */
  966. uint nbytes, /* number of input data bytes to process */
  967. uint16 crc /* either CRC16_INIT_VALUE or previous return value */
  968. )
  969. {
  970. while (nbytes-- > 0)
  971. CRC_INNER_LOOP(16, crc, *pdata++);
  972. return crc;
  973. }
  974. STATIC const uint32 crc32_table[256] = {
  975. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
  976. 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  977. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  978. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  979. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  980. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  981. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
  982. 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  983. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  984. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  985. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
  986. 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  987. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
  988. 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  989. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  990. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  991. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
  992. 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  993. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
  994. 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  995. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  996. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  997. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
  998. 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  999. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  1000. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  1001. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  1002. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  1003. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
  1004. 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  1005. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
  1006. 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  1007. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  1008. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  1009. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  1010. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  1011. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
  1012. 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  1013. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  1014. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  1015. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
  1016. 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  1017. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
  1018. 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  1019. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  1020. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  1021. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
  1022. 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  1023. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
  1024. 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  1025. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  1026. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  1027. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
  1028. 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  1029. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  1030. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  1031. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  1032. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  1033. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
  1034. 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  1035. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
  1036. 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  1037. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  1038. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  1039. };
  1040. uint32
  1041. hndcrc32(
  1042. uint8 *pdata, /* pointer to array of data to process */
  1043. uint nbytes, /* number of input data bytes to process */
  1044. uint32 crc /* either CRC32_INIT_VALUE or previous return value */
  1045. )
  1046. {
  1047. uint8 *pend;
  1048. #ifdef __mips__
  1049. uint8 tmp[4];
  1050. ulong *tptr = (ulong *)tmp;
  1051. /* in case the beginning of the buffer isn't aligned */
  1052. pend = (uint8 *)((uint)(pdata + 3) & 0xfffffffc);
  1053. nbytes -= (pend - pdata);
  1054. while (pdata < pend)
  1055. CRC_INNER_LOOP(32, crc, *pdata++);
  1056. /* handle bulk of data as 32-bit words */
  1057. pend = pdata + (nbytes & 0xfffffffc);
  1058. while (pdata < pend) {
  1059. *tptr = *(ulong *)pdata;
  1060. pdata += sizeof(ulong *);
  1061. CRC_INNER_LOOP(32, crc, tmp[0]);
  1062. CRC_INNER_LOOP(32, crc, tmp[1]);
  1063. CRC_INNER_LOOP(32, crc, tmp[2]);
  1064. CRC_INNER_LOOP(32, crc, tmp[3]);
  1065. }
  1066. /* 1-3 bytes at end of buffer */
  1067. pend = pdata + (nbytes & 0x03);
  1068. while (pdata < pend)
  1069. CRC_INNER_LOOP(32, crc, *pdata++);
  1070. #else
  1071. pend = pdata + nbytes;
  1072. while (pdata < pend)
  1073. CRC_INNER_LOOP(32, crc, *pdata++);
  1074. #endif /* __mips__ */
  1075. return crc;
  1076. }
  1077. #ifdef notdef
  1078. #define CLEN 1499 /* CRC Length */
  1079. #define CBUFSIZ (CLEN+4)
  1080. #define CNBUFS 5 /* # of bufs */
  1081. void testcrc32(void)
  1082. {
  1083. uint j, k, l;
  1084. uint8 *buf;
  1085. uint len[CNBUFS];
  1086. uint32 crcr;
  1087. uint32 crc32tv[CNBUFS] =
  1088. {0xd2cb1faa, 0xd385c8fa, 0xf5b4f3f3, 0x55789e20, 0x00343110};
  1089. ASSERT((buf = MALLOC(CBUFSIZ*CNBUFS)) != NULL);
  1090. /* step through all possible alignments */
  1091. for (l = 0; l <= 4; l++) {
  1092. for (j = 0; j < CNBUFS; j++) {
  1093. len[j] = CLEN;
  1094. for (k = 0; k < len[j]; k++)
  1095. *(buf + j*CBUFSIZ + (k+l)) = (j+k) & 0xff;
  1096. }
  1097. for (j = 0; j < CNBUFS; j++) {
  1098. crcr = crc32(buf + j*CBUFSIZ + l, len[j], CRC32_INIT_VALUE);
  1099. ASSERT(crcr == crc32tv[j]);
  1100. }
  1101. }
  1102. MFREE(buf, CBUFSIZ*CNBUFS);
  1103. return;
  1104. }
  1105. #endif /* notdef */
  1106. /*
  1107. * Advance from the current 1-byte tag/1-byte length/variable-length value
  1108. * triple, to the next, returning a pointer to the next.
  1109. * If the current or next TLV is invalid (does not fit in given buffer length),
  1110. * NULL is returned.
  1111. * *buflen is not modified if the TLV elt parameter is invalid, or is decremented
  1112. * by the TLV parameter's length if it is valid.
  1113. */
  1114. bcm_tlv_t *
  1115. bcm_next_tlv(bcm_tlv_t *elt, int *buflen)
  1116. {
  1117. int len;
  1118. /* validate current elt */
  1119. if (!bcm_valid_tlv(elt, *buflen))
  1120. return NULL;
  1121. /* advance to next elt */
  1122. len = elt->len;
  1123. elt = (bcm_tlv_t*)(elt->data + len);
  1124. *buflen -= (2 + len);
  1125. /* validate next elt */
  1126. if (!bcm_valid_tlv(elt, *buflen))
  1127. return NULL;
  1128. return elt;
  1129. }
  1130. /*
  1131. * Traverse a string of 1-byte tag/1-byte length/variable-length value
  1132. * triples, returning a pointer to the substring whose first element
  1133. * matches tag
  1134. */
  1135. bcm_tlv_t *
  1136. bcm_parse_tlvs(void *buf, int buflen, uint key)
  1137. {
  1138. bcm_tlv_t *elt;
  1139. int totlen;
  1140. elt = (bcm_tlv_t*)buf;
  1141. totlen = buflen;
  1142. /* find tagged parameter */
  1143. while (totlen >= 2) {
  1144. int len = elt->len;
  1145. /* validate remaining totlen */
  1146. if ((elt->id == key) && (totlen >= (len + 2)))
  1147. return (elt);
  1148. elt = (bcm_tlv_t*)((uint8*)elt + (len + 2));
  1149. totlen -= (len + 2);
  1150. }
  1151. return NULL;
  1152. }
  1153. /*
  1154. * Traverse a string of 1-byte tag/1-byte length/variable-length value
  1155. * triples, returning a pointer to the substring whose first element
  1156. * matches tag. Stop parsing when we see an element whose ID is greater
  1157. * than the target key.
  1158. */
  1159. bcm_tlv_t *
  1160. bcm_parse_ordered_tlvs(void *buf, int buflen, uint key)
  1161. {
  1162. bcm_tlv_t *elt;
  1163. int totlen;
  1164. elt = (bcm_tlv_t*)buf;
  1165. totlen = buflen;
  1166. /* find tagged parameter */
  1167. while (totlen >= 2) {
  1168. uint id = elt->id;
  1169. int len = elt->len;
  1170. /* Punt if we start seeing IDs > than target key */
  1171. if (id > key)
  1172. return (NULL);
  1173. /* validate remaining totlen */
  1174. if ((id == key) && (totlen >= (len + 2)))
  1175. return (elt);
  1176. elt = (bcm_tlv_t*)((uint8*)elt + (len + 2));
  1177. totlen -= (len + 2);
  1178. }
  1179. return NULL;
  1180. }
  1181. #if defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || defined(WLMSG_ASSOC) || \
  1182. defined(DHD_DEBUG)
  1183. int
  1184. bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, int len)
  1185. {
  1186. int i;
  1187. char* p = buf;
  1188. char hexstr[16];
  1189. int slen = 0;
  1190. uint32 bit;
  1191. const char* name;
  1192. if (len < 2 || !buf)
  1193. return 0;
  1194. buf[0] = '\0';
  1195. len -= 1;
  1196. for (i = 0; flags != 0; i++) {
  1197. bit = bd[i].bit;
  1198. name = bd[i].name;
  1199. if (bit == 0 && flags) {
  1200. /* print any unnamed bits */
  1201. sprintf(hexstr, "0x%X", flags);
  1202. name = hexstr;
  1203. flags = 0; /* exit loop */
  1204. } else if ((flags & bit) == 0)
  1205. continue;
  1206. slen += strlen(name);
  1207. if (len < slen)
  1208. break;
  1209. if (p != buf) p += sprintf(p, " "); /* btwn flag space */
  1210. strcat(p, name);
  1211. p += strlen(name);
  1212. flags &= ~bit;
  1213. len -= slen;
  1214. slen = 1; /* account for btwn flag space */
  1215. }
  1216. /* indicate the str was too short */
  1217. if (flags != 0) {
  1218. if (len == 0)
  1219. p--; /* overwrite last char */
  1220. p += sprintf(p, ">");
  1221. }
  1222. return (int)(p - buf);
  1223. }
  1224. /* print bytes formatted as hex to a string. return the resulting string length */
  1225. int
  1226. bcm_format_hex(char *str, const void *bytes, int len)
  1227. {
  1228. int i;
  1229. char *p = str;
  1230. const uint8 *src = (const uint8*)bytes;
  1231. for (i = 0; i < len; i++) {
  1232. p += sprintf(p, "%02X", *src);
  1233. src++;
  1234. }
  1235. return (int)(p - str);
  1236. }
  1237. /* pretty hex print a contiguous buffer */
  1238. void
  1239. prhex(const char *msg, uchar *buf, uint nbytes)
  1240. {
  1241. char line[128], *p;
  1242. uint i;
  1243. if (msg && (msg[0] != '\0'))
  1244. printf("%s:\n", msg);
  1245. p = line;
  1246. for (i = 0; i < nbytes; i++) {
  1247. if (i % 16 == 0) {
  1248. p += sprintf(p, " %04d: ", i); /* line prefix */
  1249. }
  1250. p += sprintf(p, "%02x ", buf[i]);
  1251. if (i % 16 == 15) {
  1252. printf("%s\n", line); /* flush line */
  1253. p = line;
  1254. }
  1255. }
  1256. /* flush last partial line */
  1257. if (p != line)
  1258. printf("%s\n", line);
  1259. }
  1260. #endif
  1261. /* Produce a human-readable string for boardrev */
  1262. char *
  1263. bcm_brev_str(uint32 brev, char *buf)
  1264. {
  1265. if (brev < 0x100)
  1266. snprintf(buf, 8, "%d.%d", (brev & 0xf0) >> 4, brev & 0xf);
  1267. else
  1268. snprintf(buf, 8, "%c%03x", ((brev & 0xf000) == 0x1000) ? 'P' : 'A', brev & 0xfff);
  1269. return (buf);
  1270. }
  1271. #define BUFSIZE_TODUMP_ATONCE 512 /* Buffer size */
  1272. /* dump large strings to console */
  1273. void
  1274. printbig(char *buf)
  1275. {
  1276. uint len, max_len;
  1277. char c;
  1278. len = strlen(buf);
  1279. max_len = BUFSIZE_TODUMP_ATONCE;
  1280. while (len > max_len) {
  1281. c = buf[max_len];
  1282. buf[max_len] = '\0';
  1283. printf("%s", buf);
  1284. buf[max_len] = c;
  1285. buf += max_len;
  1286. len -= max_len;
  1287. }
  1288. /* print the remaining string */
  1289. printf("%s\n", buf);
  1290. return;
  1291. }
  1292. /* routine to dump fields in a fileddesc structure */
  1293. uint
  1294. bcmdumpfields(bcmutl_rdreg_rtn read_rtn, void *arg0, uint arg1, struct fielddesc *fielddesc_array,
  1295. char *buf, uint32 bufsize)
  1296. {
  1297. uint filled_len;
  1298. int len;
  1299. struct fielddesc *cur_ptr;
  1300. filled_len = 0;
  1301. cur_ptr = fielddesc_array;
  1302. while (bufsize > 1) {
  1303. if (cur_ptr->nameandfmt == NULL)
  1304. break;
  1305. len = snprintf(buf, bufsize, cur_ptr->nameandfmt,
  1306. read_rtn(arg0, arg1, cur_ptr->offset));
  1307. /* check for snprintf overflow or error */
  1308. if (len < 0 || (uint32)len >= bufsize)
  1309. len = bufsize - 1;
  1310. buf += len;
  1311. bufsize -= len;
  1312. filled_len += len;
  1313. cur_ptr++;
  1314. }
  1315. return filled_len;
  1316. }
  1317. uint
  1318. bcm_mkiovar(char *name, char *data, uint datalen, char *buf, uint buflen)
  1319. {
  1320. uint len;
  1321. len = strlen(name) + 1;
  1322. if ((len + datalen) > buflen)
  1323. return 0;
  1324. strncpy(buf, name, buflen);
  1325. /* append data onto the end of the name string */
  1326. memcpy(&buf[len], data, datalen);
  1327. len += datalen;
  1328. return len;
  1329. }
  1330. /* Quarter dBm units to mW
  1331. * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
  1332. * Table is offset so the last entry is largest mW value that fits in
  1333. * a uint16.
  1334. */
  1335. #define QDBM_OFFSET 153 /* Offset for first entry */
  1336. #define QDBM_TABLE_LEN 40 /* Table size */
  1337. /* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
  1338. * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
  1339. */
  1340. #define QDBM_TABLE_LOW_BOUND 6493 /* Low bound */
  1341. /* Largest mW value that will round down to the last table entry,
  1342. * QDBM_OFFSET + QDBM_TABLE_LEN-1.
  1343. * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) + mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
  1344. */
  1345. #define QDBM_TABLE_HIGH_BOUND 64938 /* High bound */
  1346. static const uint16 nqdBm_to_mW_map[QDBM_TABLE_LEN] = {
  1347. /* qdBm: +0 +1 +2 +3 +4 +5 +6 +7 */
  1348. /* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
  1349. /* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
  1350. /* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
  1351. /* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
  1352. /* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
  1353. };
  1354. uint16
  1355. bcm_qdbm_to_mw(uint8 qdbm)
  1356. {
  1357. uint factor = 1;
  1358. int idx = qdbm - QDBM_OFFSET;
  1359. if (idx >= QDBM_TABLE_LEN) {
  1360. /* clamp to max uint16 mW value */
  1361. return 0xFFFF;
  1362. }
  1363. /* scale the qdBm index up to the range of the table 0-40
  1364. * where an offset of 40 qdBm equals a factor of 10 mW.
  1365. */
  1366. while (idx < 0) {
  1367. idx += 40;
  1368. factor *= 10;
  1369. }
  1370. /* return the mW value scaled down to the correct factor of 10,
  1371. * adding in factor/2 to get proper rounding.
  1372. */
  1373. return ((nqdBm_to_mW_map[idx] + factor/2) / factor);
  1374. }
  1375. uint8
  1376. bcm_mw_to_qdbm(uint16 mw)
  1377. {
  1378. uint8 qdbm;
  1379. int offset;
  1380. uint mw_uint = mw;
  1381. uint boundary;
  1382. /* handle boundary case */
  1383. if (mw_uint <= 1)
  1384. return 0;
  1385. offset = QDBM_OFFSET;
  1386. /* move mw into the range of the table */
  1387. while (mw_uint < QDBM_TABLE_LOW_BOUND) {
  1388. mw_uint *= 10;
  1389. offset -= 40;
  1390. }
  1391. for (qdbm = 0; qdbm < QDBM_TABLE_LEN-1; qdbm++) {
  1392. boundary = nqdBm_to_mW_map[qdbm] + (nqdBm_to_mW_map[qdbm+1] -
  1393. nqdBm_to_mW_map[qdbm])/2;
  1394. if (mw_uint < boundary) break;
  1395. }
  1396. qdbm += (uint8)offset;
  1397. return (qdbm);
  1398. }
  1399. uint
  1400. bcm_bitcount(uint8 *bitmap, uint length)
  1401. {
  1402. uint bitcount = 0, i;
  1403. uint8 tmp;
  1404. for (i = 0; i < length; i++) {
  1405. tmp = bitmap[i];
  1406. while (tmp) {
  1407. bitcount++;
  1408. tmp &= (tmp - 1);
  1409. }
  1410. }
  1411. return bitcount;
  1412. }
  1413. #ifdef BCMDRIVER
  1414. /* Initialization of bcmstrbuf structure */
  1415. void
  1416. bcm_binit(struct bcmstrbuf *b, char *buf, uint size)
  1417. {
  1418. b->origsize = b->size = size;
  1419. b->origbuf = b->buf = buf;
  1420. }
  1421. /* Buffer sprintf wrapper to guard against buffer overflow */
  1422. int
  1423. bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...)
  1424. {
  1425. va_list ap;
  1426. int r;
  1427. va_start(ap, fmt);
  1428. r = vsnprintf(b->buf, b->size, fmt, ap);
  1429. /* Non Ansi C99 compliant returns -1,
  1430. * Ansi compliant return r >= b->size,
  1431. * bcmstdlib returns 0, handle all
  1432. */
  1433. if ((r == -1) || (r >= (int)b->size) || (r == 0)) {
  1434. b->size = 0;
  1435. } else {
  1436. b->size -= r;
  1437. b->buf += r;
  1438. }
  1439. va_end(ap);
  1440. return r;
  1441. }
  1442. void
  1443. bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount)
  1444. {
  1445. int i;
  1446. for (i = 0; i < num_bytes; i++) {
  1447. num[i] += amount;
  1448. if (num[i] >= amount)
  1449. break;
  1450. amount = 1;
  1451. }
  1452. }
  1453. int
  1454. bcm_cmp_bytes(uchar *arg1, uchar *arg2, uint8 nbytes)
  1455. {
  1456. int i;
  1457. for (i = nbytes - 1; i >= 0; i--) {
  1458. if (arg1[i] != arg2[i])
  1459. return (arg1[i] - arg2[i]);
  1460. }
  1461. return 0;
  1462. }
  1463. void
  1464. bcm_print_bytes(char *name, const uchar *data, int len)
  1465. {
  1466. int i;
  1467. int per_line = 0;
  1468. printf("%s: %d \n", name ? name : "", len);
  1469. for (i = 0; i < len; i++) {
  1470. printf("%02x ", *data++);
  1471. per_line++;
  1472. if (per_line == 16) {
  1473. per_line = 0;
  1474. printf("\n");
  1475. }
  1476. }
  1477. printf("\n");
  1478. }
  1479. /*
  1480. * buffer length needed for wlc_format_ssid
  1481. * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
  1482. */
  1483. #if defined(WLTINYDUMP) || defined(WLMSG_INFORM) || defined(WLMSG_ASSOC) || \
  1484. defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
  1485. int
  1486. bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len)
  1487. {
  1488. uint i, c;
  1489. char *p = buf;
  1490. char *endp = buf + SSID_FMT_BUF_LEN;
  1491. if (ssid_len > DOT11_MAX_SSID_LEN) ssid_len = DOT11_MAX_SSID_LEN;
  1492. for (i = 0; i < ssid_len; i++) {
  1493. c = (uint)ssid[i];
  1494. if (c == '\\') {
  1495. *p++ = '\\';
  1496. *p++ = '\\';
  1497. } else if (bcm_isprint((uchar)c)) {
  1498. *p++ = (char)c;
  1499. } else {
  1500. p += snprintf(p, (endp - p), "\\x%02X", c);
  1501. }
  1502. }
  1503. *p = '\0';
  1504. ASSERT(p < endp);
  1505. return (int)(p - buf);
  1506. }
  1507. #endif
  1508. #endif /* BCMDRIVER */