bcmutils.c 45 KB

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