osdep_service.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _OSDEP_SERVICE_C_
  16. #include <drv_types.h>
  17. #define RT_TAG '1178'
  18. #ifdef DBG_MEMORY_LEAK
  19. #ifdef PLATFORM_LINUX
  20. atomic_t _malloc_cnt = ATOMIC_INIT(0);
  21. atomic_t _malloc_size = ATOMIC_INIT(0);
  22. #endif
  23. #endif /* DBG_MEMORY_LEAK */
  24. #if defined(PLATFORM_LINUX)
  25. /*
  26. * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
  27. * @return: one of RTW_STATUS_CODE
  28. */
  29. inline int RTW_STATUS_CODE(int error_code)
  30. {
  31. if (error_code >= 0)
  32. return _SUCCESS;
  33. switch (error_code) {
  34. /* case -ETIMEDOUT: */
  35. /* return RTW_STATUS_TIMEDOUT; */
  36. default:
  37. return _FAIL;
  38. }
  39. }
  40. #else
  41. inline int RTW_STATUS_CODE(int error_code)
  42. {
  43. return error_code;
  44. }
  45. #endif
  46. u32 rtw_atoi(u8 *s)
  47. {
  48. int num = 0, flag = 0;
  49. int i;
  50. for (i = 0; i <= strlen(s); i++) {
  51. if (s[i] >= '0' && s[i] <= '9')
  52. num = num * 10 + s[i] - '0';
  53. else if (s[0] == '-' && i == 0)
  54. flag = 1;
  55. else
  56. break;
  57. }
  58. if (flag == 1)
  59. num = num * -1;
  60. return num;
  61. }
  62. inline void *_rtw_vmalloc(u32 sz)
  63. {
  64. void *pbuf;
  65. #ifdef PLATFORM_LINUX
  66. pbuf = vmalloc(sz);
  67. #endif
  68. #ifdef PLATFORM_FREEBSD
  69. pbuf = malloc(sz, M_DEVBUF, M_NOWAIT);
  70. #endif
  71. #ifdef PLATFORM_WINDOWS
  72. NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG);
  73. #endif
  74. #ifdef DBG_MEMORY_LEAK
  75. #ifdef PLATFORM_LINUX
  76. if (pbuf != NULL) {
  77. atomic_inc(&_malloc_cnt);
  78. atomic_add(sz, &_malloc_size);
  79. }
  80. #endif
  81. #endif /* DBG_MEMORY_LEAK */
  82. return pbuf;
  83. }
  84. inline void *_rtw_zvmalloc(u32 sz)
  85. {
  86. void *pbuf;
  87. #ifdef PLATFORM_LINUX
  88. pbuf = _rtw_vmalloc(sz);
  89. if (pbuf != NULL)
  90. memset(pbuf, 0, sz);
  91. #endif
  92. #ifdef PLATFORM_FREEBSD
  93. pbuf = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
  94. #endif
  95. #ifdef PLATFORM_WINDOWS
  96. NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG);
  97. if (pbuf != NULL)
  98. NdisFillMemory(pbuf, sz, 0);
  99. #endif
  100. return pbuf;
  101. }
  102. inline void _rtw_vmfree(void *pbuf, u32 sz)
  103. {
  104. #ifdef PLATFORM_LINUX
  105. vfree(pbuf);
  106. #endif
  107. #ifdef PLATFORM_FREEBSD
  108. free(pbuf, M_DEVBUF);
  109. #endif
  110. #ifdef PLATFORM_WINDOWS
  111. NdisFreeMemory(pbuf, sz, 0);
  112. #endif
  113. #ifdef DBG_MEMORY_LEAK
  114. #ifdef PLATFORM_LINUX
  115. atomic_dec(&_malloc_cnt);
  116. atomic_sub(sz, &_malloc_size);
  117. #endif
  118. #endif /* DBG_MEMORY_LEAK */
  119. }
  120. void *_rtw_malloc(u32 sz)
  121. {
  122. void *pbuf = NULL;
  123. #ifdef PLATFORM_LINUX
  124. #ifdef RTK_DMP_PLATFORM
  125. if (sz > 0x4000)
  126. pbuf = dvr_malloc(sz);
  127. else
  128. #endif
  129. pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  130. #endif
  131. #ifdef PLATFORM_FREEBSD
  132. pbuf = malloc(sz, M_DEVBUF, M_NOWAIT);
  133. #endif
  134. #ifdef PLATFORM_WINDOWS
  135. NdisAllocateMemoryWithTag(&pbuf, sz, RT_TAG);
  136. #endif
  137. #ifdef DBG_MEMORY_LEAK
  138. #ifdef PLATFORM_LINUX
  139. if (pbuf != NULL) {
  140. atomic_inc(&_malloc_cnt);
  141. atomic_add(sz, &_malloc_size);
  142. }
  143. #endif
  144. #endif /* DBG_MEMORY_LEAK */
  145. return pbuf;
  146. }
  147. void *_rtw_zmalloc(u32 sz)
  148. {
  149. #ifdef PLATFORM_FREEBSD
  150. return malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
  151. #else /* PLATFORM_FREEBSD */
  152. void *pbuf = _rtw_malloc(sz);
  153. if (pbuf != NULL) {
  154. #ifdef PLATFORM_LINUX
  155. memset(pbuf, 0, sz);
  156. #endif
  157. #ifdef PLATFORM_WINDOWS
  158. NdisFillMemory(pbuf, sz, 0);
  159. #endif
  160. }
  161. return pbuf;
  162. #endif /* PLATFORM_FREEBSD */
  163. }
  164. void _rtw_mfree(void *pbuf, u32 sz)
  165. {
  166. #ifdef PLATFORM_LINUX
  167. #ifdef RTK_DMP_PLATFORM
  168. if (sz > 0x4000)
  169. dvr_free(pbuf);
  170. else
  171. #endif
  172. kfree(pbuf);
  173. #endif
  174. #ifdef PLATFORM_FREEBSD
  175. free(pbuf, M_DEVBUF);
  176. #endif
  177. #ifdef PLATFORM_WINDOWS
  178. NdisFreeMemory(pbuf, sz, 0);
  179. #endif
  180. #ifdef DBG_MEMORY_LEAK
  181. #ifdef PLATFORM_LINUX
  182. atomic_dec(&_malloc_cnt);
  183. atomic_sub(sz, &_malloc_size);
  184. #endif
  185. #endif /* DBG_MEMORY_LEAK */
  186. }
  187. #ifdef PLATFORM_FREEBSD
  188. /* review again */
  189. struct sk_buff *dev_alloc_skb(unsigned int size)
  190. {
  191. struct sk_buff *skb = NULL;
  192. u8 *data = NULL;
  193. /* skb = _rtw_zmalloc(sizeof(struct sk_buff)); */ /* for skb->len, etc. */
  194. skb = _rtw_malloc(sizeof(struct sk_buff));
  195. if (!skb)
  196. goto out;
  197. data = _rtw_malloc(size);
  198. if (!data)
  199. goto nodata;
  200. skb->head = (unsigned char *)data;
  201. skb->data = (unsigned char *)data;
  202. skb->tail = (unsigned char *)data;
  203. skb->end = (unsigned char *)data + size;
  204. skb->len = 0;
  205. /* printf("%s()-%d: skb=%p, skb->head = %p\n", __FUNCTION__, __LINE__, skb, skb->head); */
  206. out:
  207. return skb;
  208. nodata:
  209. _rtw_mfree(skb, sizeof(struct sk_buff));
  210. skb = NULL;
  211. goto out;
  212. }
  213. void dev_kfree_skb_any(struct sk_buff *skb)
  214. {
  215. /* printf("%s()-%d: skb->head = %p\n", __FUNCTION__, __LINE__, skb->head); */
  216. if (skb->head)
  217. _rtw_mfree(skb->head, 0);
  218. /* printf("%s()-%d: skb = %p\n", __FUNCTION__, __LINE__, skb); */
  219. if (skb)
  220. _rtw_mfree(skb, 0);
  221. }
  222. struct sk_buff *skb_clone(const struct sk_buff *skb)
  223. {
  224. return NULL;
  225. }
  226. #endif /* PLATFORM_FREEBSD */
  227. inline struct sk_buff *_rtw_skb_alloc(u32 sz)
  228. {
  229. #ifdef PLATFORM_LINUX
  230. return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  231. #endif /* PLATFORM_LINUX */
  232. #ifdef PLATFORM_FREEBSD
  233. return dev_alloc_skb(sz);
  234. #endif /* PLATFORM_FREEBSD */
  235. }
  236. inline void _rtw_skb_free(struct sk_buff *skb)
  237. {
  238. dev_kfree_skb_any(skb);
  239. }
  240. inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb)
  241. {
  242. #ifdef PLATFORM_LINUX
  243. return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  244. #endif /* PLATFORM_LINUX */
  245. #ifdef PLATFORM_FREEBSD
  246. return NULL;
  247. #endif /* PLATFORM_FREEBSD */
  248. }
  249. inline struct sk_buff *_rtw_skb_clone(struct sk_buff *skb)
  250. {
  251. #ifdef PLATFORM_LINUX
  252. return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  253. #endif /* PLATFORM_LINUX */
  254. #ifdef PLATFORM_FREEBSD
  255. return skb_clone(skb);
  256. #endif /* PLATFORM_FREEBSD */
  257. }
  258. inline struct sk_buff *_rtw_pskb_copy(struct sk_buff *skb)
  259. {
  260. #ifdef PLATFORM_LINUX
  261. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36))
  262. return pskb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  263. #else
  264. return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  265. #endif
  266. #endif /* PLATFORM_LINUX */
  267. #ifdef PLATFORM_FREEBSD
  268. return NULL;
  269. #endif /* PLATFORM_FREEBSD */
  270. }
  271. inline int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb)
  272. {
  273. #if defined(PLATFORM_LINUX)
  274. skb->dev = ndev;
  275. return netif_rx(skb);
  276. #elif defined(PLATFORM_FREEBSD)
  277. return (*ndev->if_input)(ndev, skb);
  278. #else
  279. rtw_warn_on(1);
  280. return -1;
  281. #endif
  282. }
  283. #ifdef CONFIG_RTW_NAPI
  284. inline int _rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb)
  285. {
  286. #if defined(PLATFORM_LINUX)
  287. skb->dev = ndev;
  288. return netif_receive_skb(skb);
  289. #else
  290. rtw_warn_on(1);
  291. return -1;
  292. #endif
  293. }
  294. #ifdef CONFIG_RTW_GRO
  295. inline gro_result_t _rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  296. {
  297. #if defined(PLATFORM_LINUX)
  298. return napi_gro_receive(napi, skb);
  299. #else
  300. rtw_warn_on(1);
  301. return -1;
  302. #endif
  303. }
  304. #endif /* CONFIG_RTW_GRO */
  305. #endif /* CONFIG_RTW_NAPI */
  306. void _rtw_skb_queue_purge(struct sk_buff_head *list)
  307. {
  308. struct sk_buff *skb;
  309. while ((skb = skb_dequeue(list)) != NULL)
  310. _rtw_skb_free(skb);
  311. }
  312. #ifdef CONFIG_USB_HCI
  313. inline void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma)
  314. {
  315. #ifdef PLATFORM_LINUX
  316. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
  317. return usb_alloc_coherent(dev, size, (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL), dma);
  318. #else
  319. return usb_buffer_alloc(dev, size, (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL), dma);
  320. #endif
  321. #endif /* PLATFORM_LINUX */
  322. #ifdef PLATFORM_FREEBSD
  323. return malloc(size, M_USBDEV, M_NOWAIT | M_ZERO);
  324. #endif /* PLATFORM_FREEBSD */
  325. }
  326. inline void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma)
  327. {
  328. #ifdef PLATFORM_LINUX
  329. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
  330. usb_free_coherent(dev, size, addr, dma);
  331. #else
  332. usb_buffer_free(dev, size, addr, dma);
  333. #endif
  334. #endif /* PLATFORM_LINUX */
  335. #ifdef PLATFORM_FREEBSD
  336. free(addr, M_USBDEV);
  337. #endif /* PLATFORM_FREEBSD */
  338. }
  339. #endif /* CONFIG_USB_HCI */
  340. #if defined(DBG_MEM_ALLOC)
  341. struct rtw_mem_stat {
  342. ATOMIC_T alloc; /* the memory bytes we allocate currently */
  343. ATOMIC_T peak; /* the peak memory bytes we allocate */
  344. ATOMIC_T alloc_cnt; /* the alloc count for alloc currently */
  345. ATOMIC_T alloc_err_cnt; /* the error times we fail to allocate memory */
  346. };
  347. struct rtw_mem_stat rtw_mem_type_stat[mstat_tf_idx(MSTAT_TYPE_MAX)];
  348. #ifdef RTW_MEM_FUNC_STAT
  349. struct rtw_mem_stat rtw_mem_func_stat[mstat_ff_idx(MSTAT_FUNC_MAX)];
  350. #endif
  351. char *MSTAT_TYPE_str[] = {
  352. "VIR",
  353. "PHY",
  354. "SKB",
  355. "USB",
  356. };
  357. #ifdef RTW_MEM_FUNC_STAT
  358. char *MSTAT_FUNC_str[] = {
  359. "UNSP",
  360. "IO",
  361. "TXIO",
  362. "RXIO",
  363. "TX",
  364. "RX",
  365. };
  366. #endif
  367. void rtw_mstat_dump(void *sel)
  368. {
  369. int i;
  370. int value_t[4][mstat_tf_idx(MSTAT_TYPE_MAX)];
  371. #ifdef RTW_MEM_FUNC_STAT
  372. int value_f[4][mstat_ff_idx(MSTAT_FUNC_MAX)];
  373. #endif
  374. int vir_alloc, vir_peak, vir_alloc_err, phy_alloc, phy_peak, phy_alloc_err;
  375. int tx_alloc, tx_peak, tx_alloc_err, rx_alloc, rx_peak, rx_alloc_err;
  376. for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++) {
  377. value_t[0][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc));
  378. value_t[1][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].peak));
  379. value_t[2][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc_cnt));
  380. value_t[3][i] = ATOMIC_READ(&(rtw_mem_type_stat[i].alloc_err_cnt));
  381. }
  382. #ifdef RTW_MEM_FUNC_STAT
  383. for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++) {
  384. value_f[0][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc));
  385. value_f[1][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].peak));
  386. value_f[2][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc_cnt));
  387. value_f[3][i] = ATOMIC_READ(&(rtw_mem_func_stat[i].alloc_err_cnt));
  388. }
  389. #endif
  390. RTW_PRINT_SEL(sel, "===================== MSTAT =====================\n");
  391. RTW_PRINT_SEL(sel, "%4s %10s %10s %10s %10s\n", "TAG", "alloc", "peak", "aloc_cnt", "err_cnt");
  392. RTW_PRINT_SEL(sel, "-------------------------------------------------\n");
  393. for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++)
  394. RTW_PRINT_SEL(sel, "%4s %10d %10d %10d %10d\n", MSTAT_TYPE_str[i], value_t[0][i], value_t[1][i], value_t[2][i], value_t[3][i]);
  395. #ifdef RTW_MEM_FUNC_STAT
  396. RTW_PRINT_SEL(sel, "-------------------------------------------------\n");
  397. for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++)
  398. RTW_PRINT_SEL(sel, "%4s %10d %10d %10d %10d\n", MSTAT_FUNC_str[i], value_f[0][i], value_f[1][i], value_f[2][i], value_f[3][i]);
  399. #endif
  400. }
  401. void rtw_mstat_update(const enum mstat_f flags, const MSTAT_STATUS status, u32 sz)
  402. {
  403. static systime update_time = 0;
  404. int peak, alloc;
  405. int i;
  406. /* initialization */
  407. if (!update_time) {
  408. for (i = 0; i < mstat_tf_idx(MSTAT_TYPE_MAX); i++) {
  409. ATOMIC_SET(&(rtw_mem_type_stat[i].alloc), 0);
  410. ATOMIC_SET(&(rtw_mem_type_stat[i].peak), 0);
  411. ATOMIC_SET(&(rtw_mem_type_stat[i].alloc_cnt), 0);
  412. ATOMIC_SET(&(rtw_mem_type_stat[i].alloc_err_cnt), 0);
  413. }
  414. #ifdef RTW_MEM_FUNC_STAT
  415. for (i = 0; i < mstat_ff_idx(MSTAT_FUNC_MAX); i++) {
  416. ATOMIC_SET(&(rtw_mem_func_stat[i].alloc), 0);
  417. ATOMIC_SET(&(rtw_mem_func_stat[i].peak), 0);
  418. ATOMIC_SET(&(rtw_mem_func_stat[i].alloc_cnt), 0);
  419. ATOMIC_SET(&(rtw_mem_func_stat[i].alloc_err_cnt), 0);
  420. }
  421. #endif
  422. }
  423. switch (status) {
  424. case MSTAT_ALLOC_SUCCESS:
  425. ATOMIC_INC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_cnt));
  426. alloc = ATOMIC_ADD_RETURN(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc), sz);
  427. peak = ATOMIC_READ(&(rtw_mem_type_stat[mstat_tf_idx(flags)].peak));
  428. if (peak < alloc)
  429. ATOMIC_SET(&(rtw_mem_type_stat[mstat_tf_idx(flags)].peak), alloc);
  430. #ifdef RTW_MEM_FUNC_STAT
  431. ATOMIC_INC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_cnt));
  432. alloc = ATOMIC_ADD_RETURN(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc), sz);
  433. peak = ATOMIC_READ(&(rtw_mem_func_stat[mstat_ff_idx(flags)].peak));
  434. if (peak < alloc)
  435. ATOMIC_SET(&(rtw_mem_func_stat[mstat_ff_idx(flags)].peak), alloc);
  436. #endif
  437. break;
  438. case MSTAT_ALLOC_FAIL:
  439. ATOMIC_INC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_err_cnt));
  440. #ifdef RTW_MEM_FUNC_STAT
  441. ATOMIC_INC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_err_cnt));
  442. #endif
  443. break;
  444. case MSTAT_FREE:
  445. ATOMIC_DEC(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc_cnt));
  446. ATOMIC_SUB(&(rtw_mem_type_stat[mstat_tf_idx(flags)].alloc), sz);
  447. #ifdef RTW_MEM_FUNC_STAT
  448. ATOMIC_DEC(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc_cnt));
  449. ATOMIC_SUB(&(rtw_mem_func_stat[mstat_ff_idx(flags)].alloc), sz);
  450. #endif
  451. break;
  452. };
  453. /* if (rtw_get_passing_time_ms(update_time) > 5000) { */
  454. /* rtw_mstat_dump(RTW_DBGDUMP); */
  455. update_time = rtw_get_current_time();
  456. /* } */
  457. }
  458. #ifndef SIZE_MAX
  459. #define SIZE_MAX (~(size_t)0)
  460. #endif
  461. struct mstat_sniff_rule {
  462. enum mstat_f flags;
  463. size_t lb;
  464. size_t hb;
  465. };
  466. struct mstat_sniff_rule mstat_sniff_rules[] = {
  467. {MSTAT_TYPE_PHY, 4097, SIZE_MAX},
  468. };
  469. int mstat_sniff_rule_num = sizeof(mstat_sniff_rules) / sizeof(struct mstat_sniff_rule);
  470. bool match_mstat_sniff_rules(const enum mstat_f flags, const size_t size)
  471. {
  472. int i;
  473. for (i = 0; i < mstat_sniff_rule_num; i++) {
  474. if (mstat_sniff_rules[i].flags == flags
  475. && mstat_sniff_rules[i].lb <= size
  476. && mstat_sniff_rules[i].hb >= size)
  477. return _TRUE;
  478. }
  479. return _FALSE;
  480. }
  481. inline void *dbg_rtw_vmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line)
  482. {
  483. void *p;
  484. if (match_mstat_sniff_rules(flags, sz))
  485. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  486. p = _rtw_vmalloc((sz));
  487. rtw_mstat_update(
  488. flags
  489. , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  490. , sz
  491. );
  492. return p;
  493. }
  494. inline void *dbg_rtw_zvmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line)
  495. {
  496. void *p;
  497. if (match_mstat_sniff_rules(flags, sz))
  498. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  499. p = _rtw_zvmalloc((sz));
  500. rtw_mstat_update(
  501. flags
  502. , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  503. , sz
  504. );
  505. return p;
  506. }
  507. inline void dbg_rtw_vmfree(void *pbuf, u32 sz, const enum mstat_f flags, const char *func, const int line)
  508. {
  509. if (match_mstat_sniff_rules(flags, sz))
  510. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  511. _rtw_vmfree((pbuf), (sz));
  512. rtw_mstat_update(
  513. flags
  514. , MSTAT_FREE
  515. , sz
  516. );
  517. }
  518. inline void *dbg_rtw_malloc(u32 sz, const enum mstat_f flags, const char *func, const int line)
  519. {
  520. void *p;
  521. if (match_mstat_sniff_rules(flags, sz))
  522. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  523. p = _rtw_malloc((sz));
  524. rtw_mstat_update(
  525. flags
  526. , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  527. , sz
  528. );
  529. return p;
  530. }
  531. inline void *dbg_rtw_zmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line)
  532. {
  533. void *p;
  534. if (match_mstat_sniff_rules(flags, sz))
  535. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  536. p = _rtw_zmalloc((sz));
  537. rtw_mstat_update(
  538. flags
  539. , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  540. , sz
  541. );
  542. return p;
  543. }
  544. inline void dbg_rtw_mfree(void *pbuf, u32 sz, const enum mstat_f flags, const char *func, const int line)
  545. {
  546. if (match_mstat_sniff_rules(flags, sz))
  547. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  548. _rtw_mfree((pbuf), (sz));
  549. rtw_mstat_update(
  550. flags
  551. , MSTAT_FREE
  552. , sz
  553. );
  554. }
  555. inline struct sk_buff *dbg_rtw_skb_alloc(unsigned int size, const enum mstat_f flags, const char *func, int line)
  556. {
  557. struct sk_buff *skb;
  558. unsigned int truesize = 0;
  559. skb = _rtw_skb_alloc(size);
  560. if (skb)
  561. truesize = skb->truesize;
  562. if (!skb || truesize < size || match_mstat_sniff_rules(flags, truesize))
  563. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%d), skb:%p, truesize=%u\n", func, line, __FUNCTION__, size, skb, truesize);
  564. rtw_mstat_update(
  565. flags
  566. , skb ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  567. , truesize
  568. );
  569. return skb;
  570. }
  571. inline void dbg_rtw_skb_free(struct sk_buff *skb, const enum mstat_f flags, const char *func, int line)
  572. {
  573. unsigned int truesize = skb->truesize;
  574. if (match_mstat_sniff_rules(flags, truesize))
  575. RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
  576. _rtw_skb_free(skb);
  577. rtw_mstat_update(
  578. flags
  579. , MSTAT_FREE
  580. , truesize
  581. );
  582. }
  583. inline struct sk_buff *dbg_rtw_skb_copy(const struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line)
  584. {
  585. struct sk_buff *skb_cp;
  586. unsigned int truesize = skb->truesize;
  587. unsigned int cp_truesize = 0;
  588. skb_cp = _rtw_skb_copy(skb);
  589. if (skb_cp)
  590. cp_truesize = skb_cp->truesize;
  591. if (!skb_cp || cp_truesize < truesize || match_mstat_sniff_rules(flags, cp_truesize))
  592. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%u), skb_cp:%p, cp_truesize=%u\n", func, line, __FUNCTION__, truesize, skb_cp, cp_truesize);
  593. rtw_mstat_update(
  594. flags
  595. , skb_cp ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  596. , cp_truesize
  597. );
  598. return skb_cp;
  599. }
  600. inline struct sk_buff *dbg_rtw_skb_clone(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line)
  601. {
  602. struct sk_buff *skb_cl;
  603. unsigned int truesize = skb->truesize;
  604. unsigned int cl_truesize = 0;
  605. skb_cl = _rtw_skb_clone(skb);
  606. if (skb_cl)
  607. cl_truesize = skb_cl->truesize;
  608. if (!skb_cl || cl_truesize < truesize || match_mstat_sniff_rules(flags, cl_truesize))
  609. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%u), skb_cl:%p, cl_truesize=%u\n", func, line, __FUNCTION__, truesize, skb_cl, cl_truesize);
  610. rtw_mstat_update(
  611. flags
  612. , skb_cl ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  613. , cl_truesize
  614. );
  615. return skb_cl;
  616. }
  617. inline int dbg_rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line)
  618. {
  619. int ret;
  620. unsigned int truesize = skb->truesize;
  621. if (match_mstat_sniff_rules(flags, truesize))
  622. RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
  623. ret = _rtw_netif_rx(ndev, skb);
  624. rtw_mstat_update(
  625. flags
  626. , MSTAT_FREE
  627. , truesize
  628. );
  629. return ret;
  630. }
  631. #ifdef CONFIG_RTW_NAPI
  632. inline int dbg_rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line)
  633. {
  634. int ret;
  635. unsigned int truesize = skb->truesize;
  636. if (match_mstat_sniff_rules(flags, truesize))
  637. RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
  638. ret = _rtw_netif_receive_skb(ndev, skb);
  639. rtw_mstat_update(
  640. flags
  641. , MSTAT_FREE
  642. , truesize
  643. );
  644. return ret;
  645. }
  646. #ifdef CONFIG_RTW_GRO
  647. inline gro_result_t dbg_rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line)
  648. {
  649. int ret;
  650. unsigned int truesize = skb->truesize;
  651. if (match_mstat_sniff_rules(flags, truesize))
  652. RTW_INFO("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
  653. ret = _rtw_napi_gro_receive(napi, skb);
  654. rtw_mstat_update(
  655. flags
  656. , MSTAT_FREE
  657. , truesize
  658. );
  659. return ret;
  660. }
  661. #endif /* CONFIG_RTW_GRO */
  662. #endif /* CONFIG_RTW_NAPI */
  663. inline void dbg_rtw_skb_queue_purge(struct sk_buff_head *list, enum mstat_f flags, const char *func, int line)
  664. {
  665. struct sk_buff *skb;
  666. while ((skb = skb_dequeue(list)) != NULL)
  667. dbg_rtw_skb_free(skb, flags, func, line);
  668. }
  669. #ifdef CONFIG_USB_HCI
  670. inline void *dbg_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma, const enum mstat_f flags, const char *func, int line)
  671. {
  672. void *p;
  673. if (match_mstat_sniff_rules(flags, size))
  674. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%zu)\n", func, line, __FUNCTION__, size);
  675. p = _rtw_usb_buffer_alloc(dev, size, dma);
  676. rtw_mstat_update(
  677. flags
  678. , p ? MSTAT_ALLOC_SUCCESS : MSTAT_ALLOC_FAIL
  679. , size
  680. );
  681. return p;
  682. }
  683. inline void dbg_rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma, const enum mstat_f flags, const char *func, int line)
  684. {
  685. if (match_mstat_sniff_rules(flags, size))
  686. RTW_INFO("DBG_MEM_ALLOC %s:%d %s(%zu)\n", func, line, __FUNCTION__, size);
  687. _rtw_usb_buffer_free(dev, size, addr, dma);
  688. rtw_mstat_update(
  689. flags
  690. , MSTAT_FREE
  691. , size
  692. );
  693. }
  694. #endif /* CONFIG_USB_HCI */
  695. #endif /* defined(DBG_MEM_ALLOC) */
  696. void *rtw_malloc2d(int h, int w, size_t size)
  697. {
  698. int j;
  699. void **a = (void **) rtw_zmalloc(h * sizeof(void *) + h * w * size);
  700. if (a == NULL) {
  701. RTW_INFO("%s: alloc memory fail!\n", __FUNCTION__);
  702. return NULL;
  703. }
  704. for (j = 0; j < h; j++)
  705. a[j] = ((char *)(a + h)) + j * w * size;
  706. return a;
  707. }
  708. void rtw_mfree2d(void *pbuf, int h, int w, int size)
  709. {
  710. rtw_mfree((u8 *)pbuf, h * sizeof(void *) + w * h * size);
  711. }
  712. inline void rtw_os_pkt_free(_pkt *pkt)
  713. {
  714. #if defined(PLATFORM_LINUX)
  715. rtw_skb_free(pkt);
  716. #elif defined(PLATFORM_FREEBSD)
  717. m_freem(pkt);
  718. #else
  719. #error "TBD\n"
  720. #endif
  721. }
  722. inline _pkt *rtw_os_pkt_copy(_pkt *pkt)
  723. {
  724. #if defined(PLATFORM_LINUX)
  725. return rtw_skb_copy(pkt);
  726. #elif defined(PLATFORM_FREEBSD)
  727. return m_dup(pkt, M_NOWAIT);
  728. #else
  729. #error "TBD\n"
  730. #endif
  731. }
  732. inline void *rtw_os_pkt_data(_pkt *pkt)
  733. {
  734. #if defined(PLATFORM_LINUX)
  735. return pkt->data;
  736. #elif defined(PLATFORM_FREEBSD)
  737. return pkt->m_data;
  738. #else
  739. #error "TBD\n"
  740. #endif
  741. }
  742. inline u32 rtw_os_pkt_len(_pkt *pkt)
  743. {
  744. #if defined(PLATFORM_LINUX)
  745. return pkt->len;
  746. #elif defined(PLATFORM_FREEBSD)
  747. return pkt->m_pkthdr.len;
  748. #else
  749. #error "TBD\n"
  750. #endif
  751. }
  752. void _rtw_memcpy(void *dst, const void *src, u32 sz)
  753. {
  754. #if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD)
  755. memcpy(dst, src, sz);
  756. #endif
  757. #ifdef PLATFORM_WINDOWS
  758. NdisMoveMemory(dst, src, sz);
  759. #endif
  760. }
  761. inline void _rtw_memmove(void *dst, const void *src, u32 sz)
  762. {
  763. #if defined(PLATFORM_LINUX)
  764. memmove(dst, src, sz);
  765. #else
  766. #error "TBD\n"
  767. #endif
  768. }
  769. int _rtw_memcmp(const void *dst, const void *src, u32 sz)
  770. {
  771. #if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD)
  772. /* under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0 */
  773. if (!(memcmp(dst, src, sz)))
  774. return _TRUE;
  775. else
  776. return _FALSE;
  777. #endif
  778. #ifdef PLATFORM_WINDOWS
  779. /* under Windows, the return value of NdisEqualMemory for two same mem. chunk is 1 */
  780. if (NdisEqualMemory(dst, src, sz))
  781. return _TRUE;
  782. else
  783. return _FALSE;
  784. #endif
  785. }
  786. void _rtw_memset(void *pbuf, int c, u32 sz)
  787. {
  788. #if defined(PLATFORM_LINUX) || defined (PLATFORM_FREEBSD)
  789. memset(pbuf, c, sz);
  790. #endif
  791. #ifdef PLATFORM_WINDOWS
  792. #if 0
  793. NdisZeroMemory(pbuf, sz);
  794. if (c != 0)
  795. memset(pbuf, c, sz);
  796. #else
  797. NdisFillMemory(pbuf, sz, c);
  798. #endif
  799. #endif
  800. }
  801. #ifdef PLATFORM_FREEBSD
  802. static inline void __list_add(_list *pnew, _list *pprev, _list *pnext)
  803. {
  804. pnext->prev = pnew;
  805. pnew->next = pnext;
  806. pnew->prev = pprev;
  807. pprev->next = pnew;
  808. }
  809. #endif /* PLATFORM_FREEBSD */
  810. void _rtw_init_listhead(_list *list)
  811. {
  812. #ifdef PLATFORM_LINUX
  813. INIT_LIST_HEAD(list);
  814. #endif
  815. #ifdef PLATFORM_FREEBSD
  816. list->next = list;
  817. list->prev = list;
  818. #endif
  819. #ifdef PLATFORM_WINDOWS
  820. NdisInitializeListHead(list);
  821. #endif
  822. }
  823. /*
  824. For the following list_xxx operations,
  825. caller must guarantee the atomic context.
  826. Otherwise, there will be racing condition.
  827. */
  828. u32 rtw_is_list_empty(_list *phead)
  829. {
  830. #ifdef PLATFORM_LINUX
  831. if (list_empty(phead))
  832. return _TRUE;
  833. else
  834. return _FALSE;
  835. #endif
  836. #ifdef PLATFORM_FREEBSD
  837. if (phead->next == phead)
  838. return _TRUE;
  839. else
  840. return _FALSE;
  841. #endif
  842. #ifdef PLATFORM_WINDOWS
  843. if (IsListEmpty(phead))
  844. return _TRUE;
  845. else
  846. return _FALSE;
  847. #endif
  848. }
  849. void rtw_list_insert_head(_list *plist, _list *phead)
  850. {
  851. #ifdef PLATFORM_LINUX
  852. list_add(plist, phead);
  853. #endif
  854. #ifdef PLATFORM_FREEBSD
  855. __list_add(plist, phead, phead->next);
  856. #endif
  857. #ifdef PLATFORM_WINDOWS
  858. InsertHeadList(phead, plist);
  859. #endif
  860. }
  861. void rtw_list_insert_tail(_list *plist, _list *phead)
  862. {
  863. #ifdef PLATFORM_LINUX
  864. list_add_tail(plist, phead);
  865. #endif
  866. #ifdef PLATFORM_FREEBSD
  867. __list_add(plist, phead->prev, phead);
  868. #endif
  869. #ifdef PLATFORM_WINDOWS
  870. InsertTailList(phead, plist);
  871. #endif
  872. }
  873. inline void rtw_list_splice(_list *list, _list *head)
  874. {
  875. #ifdef PLATFORM_LINUX
  876. list_splice(list, head);
  877. #else
  878. #error "TBD\n"
  879. #endif
  880. }
  881. inline void rtw_list_splice_init(_list *list, _list *head)
  882. {
  883. #ifdef PLATFORM_LINUX
  884. list_splice_init(list, head);
  885. #else
  886. #error "TBD\n"
  887. #endif
  888. }
  889. inline void rtw_list_splice_tail(_list *list, _list *head)
  890. {
  891. #ifdef PLATFORM_LINUX
  892. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27))
  893. if (!list_empty(list))
  894. __list_splice(list, head);
  895. #else
  896. list_splice_tail(list, head);
  897. #endif
  898. #else
  899. #error "TBD\n"
  900. #endif
  901. }
  902. inline void rtw_hlist_head_init(rtw_hlist_head *h)
  903. {
  904. #ifdef PLATFORM_LINUX
  905. INIT_HLIST_HEAD(h);
  906. #else
  907. #error "TBD\n"
  908. #endif
  909. }
  910. inline void rtw_hlist_add_head(rtw_hlist_node *n, rtw_hlist_head *h)
  911. {
  912. #ifdef PLATFORM_LINUX
  913. hlist_add_head(n, h);
  914. #else
  915. #error "TBD\n"
  916. #endif
  917. }
  918. inline void rtw_hlist_del(rtw_hlist_node *n)
  919. {
  920. #ifdef PLATFORM_LINUX
  921. hlist_del(n);
  922. #else
  923. #error "TBD\n"
  924. #endif
  925. }
  926. inline void rtw_hlist_add_head_rcu(rtw_hlist_node *n, rtw_hlist_head *h)
  927. {
  928. #ifdef PLATFORM_LINUX
  929. hlist_add_head_rcu(n, h);
  930. #else
  931. #error "TBD\n"
  932. #endif
  933. }
  934. inline void rtw_hlist_del_rcu(rtw_hlist_node *n)
  935. {
  936. #ifdef PLATFORM_LINUX
  937. hlist_del_rcu(n);
  938. #else
  939. #error "TBD\n"
  940. #endif
  941. }
  942. void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc, void *ctx)
  943. {
  944. _adapter *adapter = (_adapter *)padapter;
  945. #ifdef PLATFORM_LINUX
  946. _init_timer(ptimer, adapter->pnetdev, pfunc, ctx);
  947. #endif
  948. #ifdef PLATFORM_FREEBSD
  949. _init_timer(ptimer, adapter->pifp, pfunc, ctx);
  950. #endif
  951. #ifdef PLATFORM_WINDOWS
  952. _init_timer(ptimer, adapter->hndis_adapter, pfunc, ctx);
  953. #endif
  954. }
  955. /*
  956. Caller must check if the list is empty before calling rtw_list_delete
  957. */
  958. void _rtw_init_sema(_sema *sema, int init_val)
  959. {
  960. #ifdef PLATFORM_LINUX
  961. sema_init(sema, init_val);
  962. #endif
  963. #ifdef PLATFORM_FREEBSD
  964. sema_init(sema, init_val, "rtw_drv");
  965. #endif
  966. #ifdef PLATFORM_OS_XP
  967. KeInitializeSemaphore(sema, init_val, SEMA_UPBND); /* count=0; */
  968. #endif
  969. #ifdef PLATFORM_OS_CE
  970. if (*sema == NULL)
  971. *sema = CreateSemaphore(NULL, init_val, SEMA_UPBND, NULL);
  972. #endif
  973. }
  974. void _rtw_free_sema(_sema *sema)
  975. {
  976. #ifdef PLATFORM_FREEBSD
  977. sema_destroy(sema);
  978. #endif
  979. #ifdef PLATFORM_OS_CE
  980. CloseHandle(*sema);
  981. #endif
  982. }
  983. void _rtw_up_sema(_sema *sema)
  984. {
  985. #ifdef PLATFORM_LINUX
  986. up(sema);
  987. #endif
  988. #ifdef PLATFORM_FREEBSD
  989. sema_post(sema);
  990. #endif
  991. #ifdef PLATFORM_OS_XP
  992. KeReleaseSemaphore(sema, IO_NETWORK_INCREMENT, 1, FALSE);
  993. #endif
  994. #ifdef PLATFORM_OS_CE
  995. ReleaseSemaphore(*sema, 1, NULL);
  996. #endif
  997. }
  998. u32 _rtw_down_sema(_sema *sema)
  999. {
  1000. #ifdef PLATFORM_LINUX
  1001. if (down_interruptible(sema))
  1002. return _FAIL;
  1003. else
  1004. return _SUCCESS;
  1005. #endif
  1006. #ifdef PLATFORM_FREEBSD
  1007. sema_wait(sema);
  1008. return _SUCCESS;
  1009. #endif
  1010. #ifdef PLATFORM_OS_XP
  1011. if (STATUS_SUCCESS == KeWaitForSingleObject(sema, Executive, KernelMode, TRUE, NULL))
  1012. return _SUCCESS;
  1013. else
  1014. return _FAIL;
  1015. #endif
  1016. #ifdef PLATFORM_OS_CE
  1017. if (WAIT_OBJECT_0 == WaitForSingleObject(*sema, INFINITE))
  1018. return _SUCCESS;
  1019. else
  1020. return _FAIL;
  1021. #endif
  1022. }
  1023. inline void thread_exit(_completion *comp)
  1024. {
  1025. #ifdef PLATFORM_LINUX
  1026. complete_and_exit(comp, 0);
  1027. #endif
  1028. #ifdef PLATFORM_FREEBSD
  1029. printf("%s", "RTKTHREAD_exit");
  1030. #endif
  1031. #ifdef PLATFORM_OS_CE
  1032. ExitThread(STATUS_SUCCESS);
  1033. #endif
  1034. #ifdef PLATFORM_OS_XP
  1035. PsTerminateSystemThread(STATUS_SUCCESS);
  1036. #endif
  1037. }
  1038. inline void _rtw_init_completion(_completion *comp)
  1039. {
  1040. #ifdef PLATFORM_LINUX
  1041. init_completion(comp);
  1042. #endif
  1043. }
  1044. inline void _rtw_wait_for_comp_timeout(_completion *comp)
  1045. {
  1046. #ifdef PLATFORM_LINUX
  1047. wait_for_completion_timeout(comp, msecs_to_jiffies(3000));
  1048. #endif
  1049. }
  1050. inline void _rtw_wait_for_comp(_completion *comp)
  1051. {
  1052. #ifdef PLATFORM_LINUX
  1053. wait_for_completion(comp);
  1054. #endif
  1055. }
  1056. void _rtw_mutex_init(_mutex *pmutex)
  1057. {
  1058. #ifdef PLATFORM_LINUX
  1059. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
  1060. mutex_init(pmutex);
  1061. #else
  1062. init_MUTEX(pmutex);
  1063. #endif
  1064. #endif
  1065. #ifdef PLATFORM_FREEBSD
  1066. mtx_init(pmutex, "", NULL, MTX_DEF | MTX_RECURSE);
  1067. #endif
  1068. #ifdef PLATFORM_OS_XP
  1069. KeInitializeMutex(pmutex, 0);
  1070. #endif
  1071. #ifdef PLATFORM_OS_CE
  1072. *pmutex = CreateMutex(NULL, _FALSE, NULL);
  1073. #endif
  1074. }
  1075. void _rtw_mutex_free(_mutex *pmutex);
  1076. void _rtw_mutex_free(_mutex *pmutex)
  1077. {
  1078. #ifdef PLATFORM_LINUX
  1079. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
  1080. mutex_destroy(pmutex);
  1081. #else
  1082. #endif
  1083. #ifdef PLATFORM_FREEBSD
  1084. sema_destroy(pmutex);
  1085. #endif
  1086. #endif
  1087. #ifdef PLATFORM_OS_XP
  1088. #endif
  1089. #ifdef PLATFORM_OS_CE
  1090. #endif
  1091. }
  1092. void _rtw_spinlock_init(_lock *plock)
  1093. {
  1094. #ifdef PLATFORM_LINUX
  1095. spin_lock_init(plock);
  1096. #endif
  1097. #ifdef PLATFORM_FREEBSD
  1098. mtx_init(plock, "", NULL, MTX_DEF | MTX_RECURSE);
  1099. #endif
  1100. #ifdef PLATFORM_WINDOWS
  1101. NdisAllocateSpinLock(plock);
  1102. #endif
  1103. }
  1104. void _rtw_spinlock_free(_lock *plock)
  1105. {
  1106. #ifdef PLATFORM_FREEBSD
  1107. mtx_destroy(plock);
  1108. #endif
  1109. #ifdef PLATFORM_WINDOWS
  1110. NdisFreeSpinLock(plock);
  1111. #endif
  1112. }
  1113. #ifdef PLATFORM_FREEBSD
  1114. extern PADAPTER prtw_lock;
  1115. void rtw_mtx_lock(_lock *plock)
  1116. {
  1117. if (prtw_lock)
  1118. mtx_lock(&prtw_lock->glock);
  1119. else
  1120. printf("%s prtw_lock==NULL", __FUNCTION__);
  1121. }
  1122. void rtw_mtx_unlock(_lock *plock)
  1123. {
  1124. if (prtw_lock)
  1125. mtx_unlock(&prtw_lock->glock);
  1126. else
  1127. printf("%s prtw_lock==NULL", __FUNCTION__);
  1128. }
  1129. #endif /* PLATFORM_FREEBSD */
  1130. void _rtw_spinlock(_lock *plock)
  1131. {
  1132. #ifdef PLATFORM_LINUX
  1133. spin_lock(plock);
  1134. #endif
  1135. #ifdef PLATFORM_FREEBSD
  1136. mtx_lock(plock);
  1137. #endif
  1138. #ifdef PLATFORM_WINDOWS
  1139. NdisAcquireSpinLock(plock);
  1140. #endif
  1141. }
  1142. void _rtw_spinunlock(_lock *plock)
  1143. {
  1144. #ifdef PLATFORM_LINUX
  1145. spin_unlock(plock);
  1146. #endif
  1147. #ifdef PLATFORM_FREEBSD
  1148. mtx_unlock(plock);
  1149. #endif
  1150. #ifdef PLATFORM_WINDOWS
  1151. NdisReleaseSpinLock(plock);
  1152. #endif
  1153. }
  1154. void _rtw_spinlock_ex(_lock *plock)
  1155. {
  1156. #ifdef PLATFORM_LINUX
  1157. spin_lock(plock);
  1158. #endif
  1159. #ifdef PLATFORM_FREEBSD
  1160. mtx_lock(plock);
  1161. #endif
  1162. #ifdef PLATFORM_WINDOWS
  1163. NdisDprAcquireSpinLock(plock);
  1164. #endif
  1165. }
  1166. void _rtw_spinunlock_ex(_lock *plock)
  1167. {
  1168. #ifdef PLATFORM_LINUX
  1169. spin_unlock(plock);
  1170. #endif
  1171. #ifdef PLATFORM_FREEBSD
  1172. mtx_unlock(plock);
  1173. #endif
  1174. #ifdef PLATFORM_WINDOWS
  1175. NdisDprReleaseSpinLock(plock);
  1176. #endif
  1177. }
  1178. void _rtw_init_queue(_queue *pqueue)
  1179. {
  1180. _rtw_init_listhead(&(pqueue->queue));
  1181. _rtw_spinlock_init(&(pqueue->lock));
  1182. }
  1183. void _rtw_deinit_queue(_queue *pqueue)
  1184. {
  1185. _rtw_spinlock_free(&(pqueue->lock));
  1186. }
  1187. u32 _rtw_queue_empty(_queue *pqueue)
  1188. {
  1189. return rtw_is_list_empty(&(pqueue->queue));
  1190. }
  1191. u32 rtw_end_of_queue_search(_list *head, _list *plist)
  1192. {
  1193. if (head == plist)
  1194. return _TRUE;
  1195. else
  1196. return _FALSE;
  1197. }
  1198. systime _rtw_get_current_time(void)
  1199. {
  1200. #ifdef PLATFORM_LINUX
  1201. return jiffies;
  1202. #endif
  1203. #ifdef PLATFORM_FREEBSD
  1204. struct timeval tvp;
  1205. getmicrotime(&tvp);
  1206. return tvp.tv_sec;
  1207. #endif
  1208. #ifdef PLATFORM_WINDOWS
  1209. LARGE_INTEGER SystemTime;
  1210. NdisGetCurrentSystemTime(&SystemTime);
  1211. return SystemTime.LowPart;/* count of 100-nanosecond intervals */
  1212. #endif
  1213. }
  1214. inline u32 _rtw_systime_to_ms(systime stime)
  1215. {
  1216. #ifdef PLATFORM_LINUX
  1217. return jiffies_to_msecs(stime);
  1218. #endif
  1219. #ifdef PLATFORM_FREEBSD
  1220. return stime * 1000;
  1221. #endif
  1222. #ifdef PLATFORM_WINDOWS
  1223. return stime / 10000 ;
  1224. #endif
  1225. }
  1226. inline systime _rtw_ms_to_systime(u32 ms)
  1227. {
  1228. #ifdef PLATFORM_LINUX
  1229. return msecs_to_jiffies(ms);
  1230. #endif
  1231. #ifdef PLATFORM_FREEBSD
  1232. return ms / 1000;
  1233. #endif
  1234. #ifdef PLATFORM_WINDOWS
  1235. return ms * 10000 ;
  1236. #endif
  1237. }
  1238. inline systime _rtw_us_to_systime(u32 us)
  1239. {
  1240. #ifdef PLATFORM_LINUX
  1241. return usecs_to_jiffies(us);
  1242. #else
  1243. #error "TBD\n"
  1244. #endif
  1245. }
  1246. /* the input parameter start use the same unit as returned by rtw_get_current_time */
  1247. inline s32 _rtw_get_passing_time_ms(systime start)
  1248. {
  1249. return _rtw_systime_to_ms(_rtw_get_current_time() - start);
  1250. }
  1251. inline s32 _rtw_get_remaining_time_ms(systime end)
  1252. {
  1253. return _rtw_systime_to_ms(end - _rtw_get_current_time());
  1254. }
  1255. inline s32 _rtw_get_time_interval_ms(systime start, systime end)
  1256. {
  1257. return _rtw_systime_to_ms(end - start);
  1258. }
  1259. inline bool _rtw_time_after(systime a, systime b)
  1260. {
  1261. #ifdef PLATFORM_LINUX
  1262. return time_after(a, b);
  1263. #else
  1264. #error "TBD\n"
  1265. #endif
  1266. }
  1267. void rtw_sleep_schedulable(int ms)
  1268. {
  1269. #ifdef PLATFORM_LINUX
  1270. u32 delta;
  1271. delta = (ms * HZ) / 1000; /* (ms) */
  1272. if (delta == 0) {
  1273. delta = 1;/* 1 ms */
  1274. }
  1275. set_current_state(TASK_INTERRUPTIBLE);
  1276. if (schedule_timeout(delta) != 0)
  1277. return ;
  1278. return;
  1279. #endif
  1280. #ifdef PLATFORM_FREEBSD
  1281. DELAY(ms * 1000);
  1282. return ;
  1283. #endif
  1284. #ifdef PLATFORM_WINDOWS
  1285. NdisMSleep(ms * 1000); /* (us)*1000=(ms) */
  1286. #endif
  1287. }
  1288. void rtw_msleep_os(int ms)
  1289. {
  1290. #ifdef PLATFORM_LINUX
  1291. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36))
  1292. if (ms < 20) {
  1293. unsigned long us = ms * 1000UL;
  1294. usleep_range(us, us + 1000UL);
  1295. } else
  1296. #endif
  1297. msleep((unsigned int)ms);
  1298. #endif
  1299. #ifdef PLATFORM_FREEBSD
  1300. /* Delay for delay microseconds */
  1301. DELAY(ms * 1000);
  1302. return ;
  1303. #endif
  1304. #ifdef PLATFORM_WINDOWS
  1305. NdisMSleep(ms * 1000); /* (us)*1000=(ms) */
  1306. #endif
  1307. }
  1308. void rtw_usleep_os(int us)
  1309. {
  1310. #ifdef PLATFORM_LINUX
  1311. /* msleep((unsigned int)us); */
  1312. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36))
  1313. usleep_range(us, us + 1);
  1314. #else
  1315. if (1 < (us / 1000))
  1316. msleep(1);
  1317. else
  1318. msleep((us / 1000) + 1);
  1319. #endif
  1320. #endif
  1321. #ifdef PLATFORM_FREEBSD
  1322. /* Delay for delay microseconds */
  1323. DELAY(us);
  1324. return ;
  1325. #endif
  1326. #ifdef PLATFORM_WINDOWS
  1327. NdisMSleep(us); /* (us) */
  1328. #endif
  1329. }
  1330. #ifdef DBG_DELAY_OS
  1331. void _rtw_mdelay_os(int ms, const char *func, const int line)
  1332. {
  1333. #if 0
  1334. if (ms > 10)
  1335. RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
  1336. rtw_msleep_os(ms);
  1337. return;
  1338. #endif
  1339. RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
  1340. #if defined(PLATFORM_LINUX)
  1341. mdelay((unsigned long)ms);
  1342. #elif defined(PLATFORM_WINDOWS)
  1343. NdisStallExecution(ms * 1000); /* (us)*1000=(ms) */
  1344. #endif
  1345. }
  1346. void _rtw_udelay_os(int us, const char *func, const int line)
  1347. {
  1348. #if 0
  1349. if (us > 1000) {
  1350. RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
  1351. rtw_usleep_os(us);
  1352. return;
  1353. }
  1354. #endif
  1355. RTW_INFO("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
  1356. #if defined(PLATFORM_LINUX)
  1357. udelay((unsigned long)us);
  1358. #elif defined(PLATFORM_WINDOWS)
  1359. NdisStallExecution(us); /* (us) */
  1360. #endif
  1361. }
  1362. #else
  1363. void rtw_mdelay_os(int ms)
  1364. {
  1365. #ifdef PLATFORM_LINUX
  1366. mdelay((unsigned long)ms);
  1367. #endif
  1368. #ifdef PLATFORM_FREEBSD
  1369. DELAY(ms * 1000);
  1370. return ;
  1371. #endif
  1372. #ifdef PLATFORM_WINDOWS
  1373. NdisStallExecution(ms * 1000); /* (us)*1000=(ms) */
  1374. #endif
  1375. }
  1376. void rtw_udelay_os(int us)
  1377. {
  1378. #ifdef PLATFORM_LINUX
  1379. udelay((unsigned long)us);
  1380. #endif
  1381. #ifdef PLATFORM_FREEBSD
  1382. /* Delay for delay microseconds */
  1383. DELAY(us);
  1384. return ;
  1385. #endif
  1386. #ifdef PLATFORM_WINDOWS
  1387. NdisStallExecution(us); /* (us) */
  1388. #endif
  1389. }
  1390. #endif
  1391. void rtw_yield_os(void)
  1392. {
  1393. #ifdef PLATFORM_LINUX
  1394. yield();
  1395. #endif
  1396. #ifdef PLATFORM_FREEBSD
  1397. yield();
  1398. #endif
  1399. #ifdef PLATFORM_WINDOWS
  1400. SwitchToThread();
  1401. #endif
  1402. }
  1403. bool rtw_macaddr_is_larger(const u8 *a, const u8 *b)
  1404. {
  1405. u32 va, vb;
  1406. va = be32_to_cpu(*((u32 *)a));
  1407. vb = be32_to_cpu(*((u32 *)b));
  1408. if (va > vb)
  1409. return 1;
  1410. else if (va < vb)
  1411. return 0;
  1412. return be16_to_cpu(*((u16 *)(a + 4))) > be16_to_cpu(*((u16 *)(b + 4)));
  1413. }
  1414. #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
  1415. #define RTW_SUSPEND_TRAFFIC_LOCK_NAME "rtw_wifi_traffic"
  1416. #define RTW_SUSPEND_RESUME_LOCK_NAME "rtw_wifi_resume"
  1417. #ifdef CONFIG_WAKELOCK
  1418. static struct wake_lock rtw_suspend_lock;
  1419. static struct wake_lock rtw_suspend_traffic_lock;
  1420. static struct wake_lock rtw_suspend_resume_lock;
  1421. #elif defined(CONFIG_ANDROID_POWER)
  1422. static android_suspend_lock_t rtw_suspend_lock = {
  1423. .name = RTW_SUSPEND_LOCK_NAME
  1424. };
  1425. static android_suspend_lock_t rtw_suspend_traffic_lock = {
  1426. .name = RTW_SUSPEND_TRAFFIC_LOCK_NAME
  1427. };
  1428. static android_suspend_lock_t rtw_suspend_resume_lock = {
  1429. .name = RTW_SUSPEND_RESUME_LOCK_NAME
  1430. };
  1431. #endif
  1432. inline void rtw_suspend_lock_init(void)
  1433. {
  1434. #ifdef CONFIG_WAKELOCK
  1435. wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME);
  1436. wake_lock_init(&rtw_suspend_traffic_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_TRAFFIC_LOCK_NAME);
  1437. wake_lock_init(&rtw_suspend_resume_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_RESUME_LOCK_NAME);
  1438. #elif defined(CONFIG_ANDROID_POWER)
  1439. android_init_suspend_lock(&rtw_suspend_lock);
  1440. android_init_suspend_lock(&rtw_suspend_traffic_lock);
  1441. android_init_suspend_lock(&rtw_suspend_resume_lock);
  1442. #endif
  1443. }
  1444. inline void rtw_suspend_lock_uninit(void)
  1445. {
  1446. #ifdef CONFIG_WAKELOCK
  1447. wake_lock_destroy(&rtw_suspend_lock);
  1448. wake_lock_destroy(&rtw_suspend_traffic_lock);
  1449. wake_lock_destroy(&rtw_suspend_resume_lock);
  1450. #elif defined(CONFIG_ANDROID_POWER)
  1451. android_uninit_suspend_lock(&rtw_suspend_lock);
  1452. android_uninit_suspend_lock(&rtw_suspend_traffic_lock);
  1453. android_uninit_suspend_lock(&rtw_suspend_resume_lock);
  1454. #endif
  1455. }
  1456. inline void rtw_lock_suspend(void)
  1457. {
  1458. #ifdef CONFIG_WAKELOCK
  1459. wake_lock(&rtw_suspend_lock);
  1460. #elif defined(CONFIG_ANDROID_POWER)
  1461. android_lock_suspend(&rtw_suspend_lock);
  1462. #endif
  1463. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  1464. /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */
  1465. #endif
  1466. }
  1467. inline void rtw_unlock_suspend(void)
  1468. {
  1469. #ifdef CONFIG_WAKELOCK
  1470. wake_unlock(&rtw_suspend_lock);
  1471. #elif defined(CONFIG_ANDROID_POWER)
  1472. android_unlock_suspend(&rtw_suspend_lock);
  1473. #endif
  1474. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  1475. /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */
  1476. #endif
  1477. }
  1478. inline void rtw_resume_lock_suspend(void)
  1479. {
  1480. #ifdef CONFIG_WAKELOCK
  1481. wake_lock(&rtw_suspend_resume_lock);
  1482. #elif defined(CONFIG_ANDROID_POWER)
  1483. android_lock_suspend(&rtw_suspend_resume_lock);
  1484. #endif
  1485. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  1486. /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */
  1487. #endif
  1488. }
  1489. inline void rtw_resume_unlock_suspend(void)
  1490. {
  1491. #ifdef CONFIG_WAKELOCK
  1492. wake_unlock(&rtw_suspend_resume_lock);
  1493. #elif defined(CONFIG_ANDROID_POWER)
  1494. android_unlock_suspend(&rtw_suspend_resume_lock);
  1495. #endif
  1496. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  1497. /* RTW_INFO("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count); */
  1498. #endif
  1499. }
  1500. inline void rtw_lock_suspend_timeout(u32 timeout_ms)
  1501. {
  1502. #ifdef CONFIG_WAKELOCK
  1503. wake_lock_timeout(&rtw_suspend_lock, rtw_ms_to_systime(timeout_ms));
  1504. #elif defined(CONFIG_ANDROID_POWER)
  1505. android_lock_suspend_auto_expire(&rtw_suspend_lock, rtw_ms_to_systime(timeout_ms));
  1506. #endif
  1507. }
  1508. inline void rtw_lock_traffic_suspend_timeout(u32 timeout_ms)
  1509. {
  1510. #ifdef CONFIG_WAKELOCK
  1511. wake_lock_timeout(&rtw_suspend_traffic_lock, rtw_ms_to_systime(timeout_ms));
  1512. #elif defined(CONFIG_ANDROID_POWER)
  1513. android_lock_suspend_auto_expire(&rtw_suspend_traffic_lock, rtw_ms_to_systime(timeout_ms));
  1514. #endif
  1515. /* RTW_INFO("traffic lock timeout:%d\n", timeout_ms); */
  1516. }
  1517. inline void rtw_set_bit(int nr, unsigned long *addr)
  1518. {
  1519. #ifdef PLATFORM_LINUX
  1520. set_bit(nr, addr);
  1521. #else
  1522. #error "TBD\n";
  1523. #endif
  1524. }
  1525. inline void rtw_clear_bit(int nr, unsigned long *addr)
  1526. {
  1527. #ifdef PLATFORM_LINUX
  1528. clear_bit(nr, addr);
  1529. #else
  1530. #error "TBD\n";
  1531. #endif
  1532. }
  1533. inline int rtw_test_and_clear_bit(int nr, unsigned long *addr)
  1534. {
  1535. #ifdef PLATFORM_LINUX
  1536. return test_and_clear_bit(nr, addr);
  1537. #else
  1538. #error "TBD\n";
  1539. #endif
  1540. }
  1541. inline void ATOMIC_SET(ATOMIC_T *v, int i)
  1542. {
  1543. #ifdef PLATFORM_LINUX
  1544. atomic_set(v, i);
  1545. #elif defined(PLATFORM_WINDOWS)
  1546. *v = i; /* other choice???? */
  1547. #elif defined(PLATFORM_FREEBSD)
  1548. atomic_set_int(v, i);
  1549. #endif
  1550. }
  1551. inline int ATOMIC_READ(ATOMIC_T *v)
  1552. {
  1553. #ifdef PLATFORM_LINUX
  1554. return atomic_read(v);
  1555. #elif defined(PLATFORM_WINDOWS)
  1556. return *v; /* other choice???? */
  1557. #elif defined(PLATFORM_FREEBSD)
  1558. return atomic_load_acq_32(v);
  1559. #endif
  1560. }
  1561. inline void ATOMIC_ADD(ATOMIC_T *v, int i)
  1562. {
  1563. #ifdef PLATFORM_LINUX
  1564. atomic_add(i, v);
  1565. #elif defined(PLATFORM_WINDOWS)
  1566. InterlockedAdd(v, i);
  1567. #elif defined(PLATFORM_FREEBSD)
  1568. atomic_add_int(v, i);
  1569. #endif
  1570. }
  1571. inline void ATOMIC_SUB(ATOMIC_T *v, int i)
  1572. {
  1573. #ifdef PLATFORM_LINUX
  1574. atomic_sub(i, v);
  1575. #elif defined(PLATFORM_WINDOWS)
  1576. InterlockedAdd(v, -i);
  1577. #elif defined(PLATFORM_FREEBSD)
  1578. atomic_subtract_int(v, i);
  1579. #endif
  1580. }
  1581. inline void ATOMIC_INC(ATOMIC_T *v)
  1582. {
  1583. #ifdef PLATFORM_LINUX
  1584. atomic_inc(v);
  1585. #elif defined(PLATFORM_WINDOWS)
  1586. InterlockedIncrement(v);
  1587. #elif defined(PLATFORM_FREEBSD)
  1588. atomic_add_int(v, 1);
  1589. #endif
  1590. }
  1591. inline void ATOMIC_DEC(ATOMIC_T *v)
  1592. {
  1593. #ifdef PLATFORM_LINUX
  1594. atomic_dec(v);
  1595. #elif defined(PLATFORM_WINDOWS)
  1596. InterlockedDecrement(v);
  1597. #elif defined(PLATFORM_FREEBSD)
  1598. atomic_subtract_int(v, 1);
  1599. #endif
  1600. }
  1601. inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
  1602. {
  1603. #ifdef PLATFORM_LINUX
  1604. return atomic_add_return(i, v);
  1605. #elif defined(PLATFORM_WINDOWS)
  1606. return InterlockedAdd(v, i);
  1607. #elif defined(PLATFORM_FREEBSD)
  1608. atomic_add_int(v, i);
  1609. return atomic_load_acq_32(v);
  1610. #endif
  1611. }
  1612. inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
  1613. {
  1614. #ifdef PLATFORM_LINUX
  1615. return atomic_sub_return(i, v);
  1616. #elif defined(PLATFORM_WINDOWS)
  1617. return InterlockedAdd(v, -i);
  1618. #elif defined(PLATFORM_FREEBSD)
  1619. atomic_subtract_int(v, i);
  1620. return atomic_load_acq_32(v);
  1621. #endif
  1622. }
  1623. inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
  1624. {
  1625. #ifdef PLATFORM_LINUX
  1626. return atomic_inc_return(v);
  1627. #elif defined(PLATFORM_WINDOWS)
  1628. return InterlockedIncrement(v);
  1629. #elif defined(PLATFORM_FREEBSD)
  1630. atomic_add_int(v, 1);
  1631. return atomic_load_acq_32(v);
  1632. #endif
  1633. }
  1634. inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
  1635. {
  1636. #ifdef PLATFORM_LINUX
  1637. return atomic_dec_return(v);
  1638. #elif defined(PLATFORM_WINDOWS)
  1639. return InterlockedDecrement(v);
  1640. #elif defined(PLATFORM_FREEBSD)
  1641. atomic_subtract_int(v, 1);
  1642. return atomic_load_acq_32(v);
  1643. #endif
  1644. }
  1645. inline bool ATOMIC_INC_UNLESS(ATOMIC_T *v, int u)
  1646. {
  1647. #ifdef PLATFORM_LINUX
  1648. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 15))
  1649. return atomic_add_unless(v, 1, u);
  1650. #else
  1651. /* only make sure not exceed after this function */
  1652. if (ATOMIC_INC_RETURN(v) > u) {
  1653. ATOMIC_DEC(v);
  1654. return 0;
  1655. }
  1656. return 1;
  1657. #endif
  1658. #else
  1659. #error "TBD\n"
  1660. #endif
  1661. }
  1662. #ifdef PLATFORM_LINUX
  1663. /*
  1664. * Open a file with the specific @param path, @param flag, @param mode
  1665. * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
  1666. * @param path the path of the file to open
  1667. * @param flag file operation flags, please refer to linux document
  1668. * @param mode please refer to linux document
  1669. * @return Linux specific error code
  1670. */
  1671. static int openFile(struct file **fpp, const char *path, int flag, int mode)
  1672. {
  1673. struct file *fp;
  1674. fp = filp_open(path, flag, mode);
  1675. if (IS_ERR(fp)) {
  1676. *fpp = NULL;
  1677. return PTR_ERR(fp);
  1678. } else {
  1679. *fpp = fp;
  1680. return 0;
  1681. }
  1682. }
  1683. /*
  1684. * Close the file with the specific @param fp
  1685. * @param fp the pointer of struct file to close
  1686. * @return always 0
  1687. */
  1688. static int closeFile(struct file *fp)
  1689. {
  1690. filp_close(fp, NULL);
  1691. return 0;
  1692. }
  1693. static int readFile(struct file *fp, char *buf, int len)
  1694. {
  1695. int rlen = 0, sum = 0;
  1696. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0))
  1697. if (!(fp->f_mode & FMODE_CAN_READ))
  1698. #else
  1699. if (!fp->f_op || !fp->f_op->read)
  1700. #endif
  1701. return -EPERM;
  1702. while (sum < len) {
  1703. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0))
  1704. rlen = kernel_read(fp, buf + sum, len - sum, &fp->f_pos);
  1705. #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0))
  1706. rlen = __vfs_read(fp, buf + sum, len - sum, &fp->f_pos);
  1707. #else
  1708. rlen = fp->f_op->read(fp, buf + sum, len - sum, &fp->f_pos);
  1709. #endif
  1710. if (rlen > 0)
  1711. sum += rlen;
  1712. else if (0 != rlen)
  1713. return rlen;
  1714. else
  1715. break;
  1716. }
  1717. return sum;
  1718. }
  1719. static int writeFile(struct file *fp, char *buf, int len)
  1720. {
  1721. int wlen = 0, sum = 0;
  1722. if (!fp->f_op || !fp->f_op->write)
  1723. return -EPERM;
  1724. while (sum < len) {
  1725. wlen = fp->f_op->write(fp, buf + sum, len - sum, &fp->f_pos);
  1726. if (wlen > 0)
  1727. sum += wlen;
  1728. else if (0 != wlen)
  1729. return wlen;
  1730. else
  1731. break;
  1732. }
  1733. return sum;
  1734. }
  1735. /*
  1736. * Test if the specifi @param path is a file and readable
  1737. * If readable, @param sz is got
  1738. * @param path the path of the file to test
  1739. * @return Linux specific error code
  1740. */
  1741. static int isFileReadable(const char *path, u32 *sz)
  1742. {
  1743. struct file *fp;
  1744. int ret = 0;
  1745. mm_segment_t oldfs;
  1746. char buf;
  1747. fp = filp_open(path, O_RDONLY, 0);
  1748. if (IS_ERR(fp))
  1749. ret = PTR_ERR(fp);
  1750. else {
  1751. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1752. oldfs = get_fs();
  1753. set_fs(KERNEL_DS);
  1754. #else
  1755. oldfs = (current_thread_info()->addr_limit);
  1756. current_thread_info()->addr_limit = ((mm_segment_t) { (-1UL) });
  1757. #endif
  1758. if (1 != readFile(fp, &buf, 1))
  1759. ret = PTR_ERR(fp);
  1760. if (ret == 0 && sz) {
  1761. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0))
  1762. *sz = i_size_read(fp->f_path.dentry->d_inode);
  1763. #else
  1764. *sz = i_size_read(fp->f_dentry->d_inode);
  1765. #endif
  1766. }
  1767. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1768. set_fs(oldfs);
  1769. #else
  1770. current_thread_info()->addr_limit = (oldfs);
  1771. #endif
  1772. filp_close(fp, NULL);
  1773. }
  1774. return ret;
  1775. }
  1776. /*
  1777. * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
  1778. * @param path the path of the file to open and read
  1779. * @param buf the starting address of the buffer to store file content
  1780. * @param sz how many bytes to read at most
  1781. * @return the byte we've read, or Linux specific error code
  1782. */
  1783. static int retriveFromFile(const char *path, u8 *buf, u32 sz)
  1784. {
  1785. int ret = -1;
  1786. mm_segment_t oldfs;
  1787. struct file *fp;
  1788. if (path && buf) {
  1789. ret = openFile(&fp, path, O_RDONLY, 0);
  1790. if (0 == ret) {
  1791. RTW_INFO("%s openFile path:%s fp=%p\n", __FUNCTION__, path , fp);
  1792. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1793. oldfs = get_fs();
  1794. set_fs(KERNEL_DS);
  1795. #else
  1796. oldfs = (current_thread_info()->addr_limit);
  1797. current_thread_info()->addr_limit = ((mm_segment_t) { (-1UL) });
  1798. #endif
  1799. ret = readFile(fp, buf, sz);
  1800. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1801. set_fs(oldfs);
  1802. #else
  1803. current_thread_info()->addr_limit = (oldfs);
  1804. #endif
  1805. closeFile(fp);
  1806. RTW_INFO("%s readFile, ret:%d\n", __FUNCTION__, ret);
  1807. } else
  1808. RTW_INFO("%s openFile path:%s Fail, ret:%d\n", __FUNCTION__, path, ret);
  1809. } else {
  1810. RTW_INFO("%s NULL pointer\n", __FUNCTION__);
  1811. ret = -EINVAL;
  1812. }
  1813. return ret;
  1814. }
  1815. /*
  1816. * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
  1817. * @param path the path of the file to open and write
  1818. * @param buf the starting address of the data to write into file
  1819. * @param sz how many bytes to write at most
  1820. * @return the byte we've written, or Linux specific error code
  1821. */
  1822. static int storeToFile(const char *path, u8 *buf, u32 sz)
  1823. {
  1824. int ret = 0;
  1825. mm_segment_t oldfs;
  1826. struct file *fp;
  1827. if (path && buf) {
  1828. ret = openFile(&fp, path, O_CREAT | O_WRONLY, 0666);
  1829. if (0 == ret) {
  1830. RTW_INFO("%s openFile path:%s fp=%p\n", __FUNCTION__, path , fp);
  1831. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1832. oldfs = get_fs();
  1833. set_fs(KERNEL_DS);
  1834. #else
  1835. oldfs = (current_thread_info()->addr_limit);
  1836. current_thread_info()->addr_limit = ((mm_segment_t) { (-1UL) });
  1837. #endif
  1838. ret = writeFile(fp, buf, sz);
  1839. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0))
  1840. set_fs(oldfs);
  1841. #else
  1842. current_thread_info()->addr_limit = (oldfs);
  1843. #endif
  1844. closeFile(fp);
  1845. RTW_INFO("%s writeFile, ret:%d\n", __FUNCTION__, ret);
  1846. } else
  1847. RTW_INFO("%s openFile path:%s Fail, ret:%d\n", __FUNCTION__, path, ret);
  1848. } else {
  1849. RTW_INFO("%s NULL pointer\n", __FUNCTION__);
  1850. ret = -EINVAL;
  1851. }
  1852. return ret;
  1853. }
  1854. #endif /* PLATFORM_LINUX */
  1855. /*
  1856. * Test if the specifi @param path is a file and readable
  1857. * @param path the path of the file to test
  1858. * @return _TRUE or _FALSE
  1859. */
  1860. int rtw_is_file_readable(const char *path)
  1861. {
  1862. #ifdef PLATFORM_LINUX
  1863. if (isFileReadable(path, NULL) == 0)
  1864. return _TRUE;
  1865. else
  1866. return _FALSE;
  1867. #else
  1868. /* Todo... */
  1869. return _FALSE;
  1870. #endif
  1871. }
  1872. /*
  1873. * Test if the specifi @param path is a file and readable.
  1874. * If readable, @param sz is got
  1875. * @param path the path of the file to test
  1876. * @return _TRUE or _FALSE
  1877. */
  1878. int rtw_is_file_readable_with_size(const char *path, u32 *sz)
  1879. {
  1880. #ifdef PLATFORM_LINUX
  1881. if (isFileReadable(path, sz) == 0)
  1882. return _TRUE;
  1883. else
  1884. return _FALSE;
  1885. #else
  1886. /* Todo... */
  1887. return _FALSE;
  1888. #endif
  1889. }
  1890. /*
  1891. * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
  1892. * @param path the path of the file to open and read
  1893. * @param buf the starting address of the buffer to store file content
  1894. * @param sz how many bytes to read at most
  1895. * @return the byte we've read
  1896. */
  1897. int rtw_retrieve_from_file(const char *path, u8 *buf, u32 sz)
  1898. {
  1899. #ifdef PLATFORM_LINUX
  1900. int ret = retriveFromFile(path, buf, sz);
  1901. return ret >= 0 ? ret : 0;
  1902. #else
  1903. /* Todo... */
  1904. return 0;
  1905. #endif
  1906. }
  1907. /*
  1908. * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
  1909. * @param path the path of the file to open and write
  1910. * @param buf the starting address of the data to write into file
  1911. * @param sz how many bytes to write at most
  1912. * @return the byte we've written
  1913. */
  1914. int rtw_store_to_file(const char *path, u8 *buf, u32 sz)
  1915. {
  1916. #ifdef PLATFORM_LINUX
  1917. int ret = storeToFile(path, buf, sz);
  1918. return ret >= 0 ? ret : 0;
  1919. #else
  1920. /* Todo... */
  1921. return 0;
  1922. #endif
  1923. }
  1924. #ifdef PLATFORM_LINUX
  1925. struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
  1926. {
  1927. struct net_device *pnetdev;
  1928. struct rtw_netdev_priv_indicator *pnpi;
  1929. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
  1930. pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
  1931. #else
  1932. pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
  1933. #endif
  1934. if (!pnetdev)
  1935. goto RETURN;
  1936. pnpi = netdev_priv(pnetdev);
  1937. pnpi->priv = old_priv;
  1938. pnpi->sizeof_priv = sizeof_priv;
  1939. RETURN:
  1940. return pnetdev;
  1941. }
  1942. struct net_device *rtw_alloc_etherdev(int sizeof_priv)
  1943. {
  1944. struct net_device *pnetdev;
  1945. struct rtw_netdev_priv_indicator *pnpi;
  1946. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
  1947. pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
  1948. #else
  1949. pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
  1950. #endif
  1951. if (!pnetdev)
  1952. goto RETURN;
  1953. pnpi = netdev_priv(pnetdev);
  1954. pnpi->priv = rtw_zvmalloc(sizeof_priv);
  1955. if (!pnpi->priv) {
  1956. free_netdev(pnetdev);
  1957. pnetdev = NULL;
  1958. goto RETURN;
  1959. }
  1960. pnpi->sizeof_priv = sizeof_priv;
  1961. RETURN:
  1962. return pnetdev;
  1963. }
  1964. void rtw_free_netdev(struct net_device *netdev)
  1965. {
  1966. struct rtw_netdev_priv_indicator *pnpi;
  1967. if (!netdev)
  1968. goto RETURN;
  1969. pnpi = netdev_priv(netdev);
  1970. if (!pnpi->priv)
  1971. goto RETURN;
  1972. free_netdev(netdev);
  1973. RETURN:
  1974. return;
  1975. }
  1976. int rtw_change_ifname(_adapter *padapter, const char *ifname)
  1977. {
  1978. struct dvobj_priv *dvobj;
  1979. struct net_device *pnetdev;
  1980. struct net_device *cur_pnetdev;
  1981. struct rereg_nd_name_data *rereg_priv;
  1982. int ret;
  1983. u8 rtnl_lock_needed;
  1984. if (!padapter)
  1985. goto error;
  1986. dvobj = adapter_to_dvobj(padapter);
  1987. cur_pnetdev = padapter->pnetdev;
  1988. rereg_priv = &padapter->rereg_nd_name_priv;
  1989. /* free the old_pnetdev */
  1990. if (rereg_priv->old_pnetdev) {
  1991. free_netdev(rereg_priv->old_pnetdev);
  1992. rereg_priv->old_pnetdev = NULL;
  1993. }
  1994. rtnl_lock_needed = rtw_rtnl_lock_needed(dvobj);
  1995. if (rtnl_lock_needed)
  1996. unregister_netdev(cur_pnetdev);
  1997. else
  1998. unregister_netdevice(cur_pnetdev);
  1999. rereg_priv->old_pnetdev = cur_pnetdev;
  2000. pnetdev = rtw_init_netdev(padapter);
  2001. if (!pnetdev) {
  2002. ret = -1;
  2003. goto error;
  2004. }
  2005. SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
  2006. rtw_init_netdev_name(pnetdev, ifname);
  2007. _rtw_memcpy(pnetdev->dev_addr, adapter_mac_addr(padapter), ETH_ALEN);
  2008. if (rtnl_lock_needed)
  2009. ret = register_netdev(pnetdev);
  2010. else
  2011. ret = register_netdevice(pnetdev);
  2012. if (ret != 0) {
  2013. goto error;
  2014. }
  2015. return 0;
  2016. error:
  2017. return -1;
  2018. }
  2019. #endif
  2020. #ifdef PLATFORM_FREEBSD
  2021. /*
  2022. * Copy a buffer from userspace and write into kernel address
  2023. * space.
  2024. *
  2025. * This emulation just calls the FreeBSD copyin function (to
  2026. * copy data from user space buffer into a kernel space buffer)
  2027. * and is designed to be used with the above io_write_wrapper.
  2028. *
  2029. * This function should return the number of bytes not copied.
  2030. * I.e. success results in a zero value.
  2031. * Negative error values are not returned.
  2032. */
  2033. unsigned long
  2034. copy_from_user(void *to, const void *from, unsigned long n)
  2035. {
  2036. if (copyin(from, to, n) != 0) {
  2037. /* Any errors will be treated as a failure
  2038. to copy any of the requested bytes */
  2039. return n;
  2040. }
  2041. return 0;
  2042. }
  2043. unsigned long
  2044. copy_to_user(void *to, const void *from, unsigned long n)
  2045. {
  2046. if (copyout(from, to, n) != 0) {
  2047. /* Any errors will be treated as a failure
  2048. to copy any of the requested bytes */
  2049. return n;
  2050. }
  2051. return 0;
  2052. }
  2053. /*
  2054. * The usb_register and usb_deregister functions are used to register
  2055. * usb drivers with the usb subsystem. In this compatibility layer
  2056. * emulation a list of drivers (struct usb_driver) is maintained
  2057. * and is used for probing/attaching etc.
  2058. *
  2059. * usb_register and usb_deregister simply call these functions.
  2060. */
  2061. int
  2062. usb_register(struct usb_driver *driver)
  2063. {
  2064. rtw_usb_linux_register(driver);
  2065. return 0;
  2066. }
  2067. int
  2068. usb_deregister(struct usb_driver *driver)
  2069. {
  2070. rtw_usb_linux_deregister(driver);
  2071. return 0;
  2072. }
  2073. void module_init_exit_wrapper(void *arg)
  2074. {
  2075. int (*func)(void) = arg;
  2076. func();
  2077. return;
  2078. }
  2079. #endif /* PLATFORM_FREEBSD */
  2080. #ifdef CONFIG_PLATFORM_SPRD
  2081. #ifdef do_div
  2082. #undef do_div
  2083. #endif
  2084. #include <asm-generic/div64.h>
  2085. #endif
  2086. u64 rtw_modular64(u64 x, u64 y)
  2087. {
  2088. #ifdef PLATFORM_LINUX
  2089. return do_div(x, y);
  2090. #elif defined(PLATFORM_WINDOWS)
  2091. return x % y;
  2092. #elif defined(PLATFORM_FREEBSD)
  2093. return x % y;
  2094. #endif
  2095. }
  2096. u64 rtw_division64(u64 x, u64 y)
  2097. {
  2098. #ifdef PLATFORM_LINUX
  2099. do_div(x, y);
  2100. return x;
  2101. #elif defined(PLATFORM_WINDOWS)
  2102. return x / y;
  2103. #elif defined(PLATFORM_FREEBSD)
  2104. return x / y;
  2105. #endif
  2106. }
  2107. inline u32 rtw_random32(void)
  2108. {
  2109. #ifdef PLATFORM_LINUX
  2110. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
  2111. return prandom_u32();
  2112. #elif (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18))
  2113. u32 random_int;
  2114. get_random_bytes(&random_int , 4);
  2115. return random_int;
  2116. #else
  2117. return random32();
  2118. #endif
  2119. #elif defined(PLATFORM_WINDOWS)
  2120. #error "to be implemented\n"
  2121. #elif defined(PLATFORM_FREEBSD)
  2122. #error "to be implemented\n"
  2123. #endif
  2124. }
  2125. void rtw_buf_free(u8 **buf, u32 *buf_len)
  2126. {
  2127. u32 ori_len;
  2128. if (!buf || !buf_len)
  2129. return;
  2130. ori_len = *buf_len;
  2131. if (*buf) {
  2132. u32 tmp_buf_len = *buf_len;
  2133. *buf_len = 0;
  2134. rtw_mfree(*buf, tmp_buf_len);
  2135. *buf = NULL;
  2136. }
  2137. }
  2138. void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
  2139. {
  2140. u32 ori_len = 0, dup_len = 0;
  2141. u8 *ori = NULL;
  2142. u8 *dup = NULL;
  2143. if (!buf || !buf_len)
  2144. return;
  2145. if (!src || !src_len)
  2146. goto keep_ori;
  2147. /* duplicate src */
  2148. dup = rtw_malloc(src_len);
  2149. if (dup) {
  2150. dup_len = src_len;
  2151. _rtw_memcpy(dup, src, dup_len);
  2152. }
  2153. keep_ori:
  2154. ori = *buf;
  2155. ori_len = *buf_len;
  2156. /* replace buf with dup */
  2157. *buf_len = 0;
  2158. *buf = dup;
  2159. *buf_len = dup_len;
  2160. /* free ori */
  2161. if (ori && ori_len > 0)
  2162. rtw_mfree(ori, ori_len);
  2163. }
  2164. /**
  2165. * rtw_cbuf_full - test if cbuf is full
  2166. * @cbuf: pointer of struct rtw_cbuf
  2167. *
  2168. * Returns: _TRUE if cbuf is full
  2169. */
  2170. inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
  2171. {
  2172. return (cbuf->write == cbuf->read - 1) ? _TRUE : _FALSE;
  2173. }
  2174. /**
  2175. * rtw_cbuf_empty - test if cbuf is empty
  2176. * @cbuf: pointer of struct rtw_cbuf
  2177. *
  2178. * Returns: _TRUE if cbuf is empty
  2179. */
  2180. inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
  2181. {
  2182. return (cbuf->write == cbuf->read) ? _TRUE : _FALSE;
  2183. }
  2184. /**
  2185. * rtw_cbuf_push - push a pointer into cbuf
  2186. * @cbuf: pointer of struct rtw_cbuf
  2187. * @buf: pointer to push in
  2188. *
  2189. * Lock free operation, be careful of the use scheme
  2190. * Returns: _TRUE push success
  2191. */
  2192. bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
  2193. {
  2194. if (rtw_cbuf_full(cbuf))
  2195. return _FAIL;
  2196. if (0)
  2197. RTW_INFO("%s on %u\n", __func__, cbuf->write);
  2198. cbuf->bufs[cbuf->write] = buf;
  2199. cbuf->write = (cbuf->write + 1) % cbuf->size;
  2200. return _SUCCESS;
  2201. }
  2202. /**
  2203. * rtw_cbuf_pop - pop a pointer from cbuf
  2204. * @cbuf: pointer of struct rtw_cbuf
  2205. *
  2206. * Lock free operation, be careful of the use scheme
  2207. * Returns: pointer popped out
  2208. */
  2209. void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
  2210. {
  2211. void *buf;
  2212. if (rtw_cbuf_empty(cbuf))
  2213. return NULL;
  2214. if (0)
  2215. RTW_INFO("%s on %u\n", __func__, cbuf->read);
  2216. buf = cbuf->bufs[cbuf->read];
  2217. cbuf->read = (cbuf->read + 1) % cbuf->size;
  2218. return buf;
  2219. }
  2220. /**
  2221. * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
  2222. * @size: size of pointer
  2223. *
  2224. * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
  2225. */
  2226. struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
  2227. {
  2228. struct rtw_cbuf *cbuf;
  2229. cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void *) * size);
  2230. if (cbuf) {
  2231. cbuf->write = cbuf->read = 0;
  2232. cbuf->size = size;
  2233. }
  2234. return cbuf;
  2235. }
  2236. /**
  2237. * rtw_cbuf_free - free the given rtw_cbuf
  2238. * @cbuf: pointer of struct rtw_cbuf to free
  2239. */
  2240. void rtw_cbuf_free(struct rtw_cbuf *cbuf)
  2241. {
  2242. rtw_mfree((u8 *)cbuf, sizeof(*cbuf) + sizeof(void *) * cbuf->size);
  2243. }
  2244. /**
  2245. * map_readN - read a range of map data
  2246. * @map: map to read
  2247. * @offset: start address to read
  2248. * @len: length to read
  2249. * @buf: pointer of buffer to store data read
  2250. *
  2251. * Returns: _SUCCESS or _FAIL
  2252. */
  2253. int map_readN(const struct map_t *map, u16 offset, u16 len, u8 *buf)
  2254. {
  2255. const struct map_seg_t *seg;
  2256. int ret = _FAIL;
  2257. int i;
  2258. if (len == 0) {
  2259. rtw_warn_on(1);
  2260. goto exit;
  2261. }
  2262. if (offset + len > map->len) {
  2263. rtw_warn_on(1);
  2264. goto exit;
  2265. }
  2266. _rtw_memset(buf, map->init_value, len);
  2267. for (i = 0; i < map->seg_num; i++) {
  2268. u8 *c_dst, *c_src;
  2269. u16 c_len;
  2270. seg = map->segs + i;
  2271. if (seg->sa + seg->len <= offset || seg->sa >= offset + len)
  2272. continue;
  2273. if (seg->sa >= offset) {
  2274. c_dst = buf + (seg->sa - offset);
  2275. c_src = seg->c;
  2276. if (seg->sa + seg->len <= offset + len)
  2277. c_len = seg->len;
  2278. else
  2279. c_len = offset + len - seg->sa;
  2280. } else {
  2281. c_dst = buf;
  2282. c_src = seg->c + (offset - seg->sa);
  2283. if (seg->sa + seg->len >= offset + len)
  2284. c_len = len;
  2285. else
  2286. c_len = seg->sa + seg->len - offset;
  2287. }
  2288. _rtw_memcpy(c_dst, c_src, c_len);
  2289. }
  2290. exit:
  2291. return ret;
  2292. }
  2293. /**
  2294. * map_read8 - read 1 byte of map data
  2295. * @map: map to read
  2296. * @offset: address to read
  2297. *
  2298. * Returns: value of data of specified offset. map.init_value if offset is out of range
  2299. */
  2300. u8 map_read8(const struct map_t *map, u16 offset)
  2301. {
  2302. const struct map_seg_t *seg;
  2303. u8 val = map->init_value;
  2304. int i;
  2305. if (offset + 1 > map->len) {
  2306. rtw_warn_on(1);
  2307. goto exit;
  2308. }
  2309. for (i = 0; i < map->seg_num; i++) {
  2310. seg = map->segs + i;
  2311. if (seg->sa + seg->len <= offset || seg->sa >= offset + 1)
  2312. continue;
  2313. val = *(seg->c + offset - seg->sa);
  2314. break;
  2315. }
  2316. exit:
  2317. return val;
  2318. }
  2319. int rtw_blacklist_add(_queue *blist, const u8 *addr, u32 timeout_ms)
  2320. {
  2321. struct blacklist_ent *ent;
  2322. _list *list, *head;
  2323. u8 exist = _FALSE, timeout = _FALSE;
  2324. enter_critical_bh(&blist->lock);
  2325. head = &blist->queue;
  2326. list = get_next(head);
  2327. while (rtw_end_of_queue_search(head, list) == _FALSE) {
  2328. ent = LIST_CONTAINOR(list, struct blacklist_ent, list);
  2329. list = get_next(list);
  2330. if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) {
  2331. exist = _TRUE;
  2332. if (rtw_time_after(rtw_get_current_time(), ent->exp_time))
  2333. timeout = _TRUE;
  2334. ent->exp_time = rtw_get_current_time()
  2335. + rtw_ms_to_systime(timeout_ms);
  2336. break;
  2337. }
  2338. if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) {
  2339. rtw_list_delete(&ent->list);
  2340. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2341. }
  2342. }
  2343. if (exist == _FALSE) {
  2344. ent = rtw_malloc(sizeof(struct blacklist_ent));
  2345. if (ent) {
  2346. _rtw_memcpy(ent->addr, addr, ETH_ALEN);
  2347. ent->exp_time = rtw_get_current_time()
  2348. + rtw_ms_to_systime(timeout_ms);
  2349. rtw_list_insert_tail(&ent->list, head);
  2350. }
  2351. }
  2352. exit_critical_bh(&blist->lock);
  2353. exit:
  2354. return (exist == _TRUE && timeout == _FALSE) ? RTW_ALREADY : (ent ? _SUCCESS : _FAIL);
  2355. }
  2356. int rtw_blacklist_del(_queue *blist, const u8 *addr)
  2357. {
  2358. struct blacklist_ent *ent = NULL;
  2359. _list *list, *head;
  2360. u8 exist = _FALSE;
  2361. enter_critical_bh(&blist->lock);
  2362. head = &blist->queue;
  2363. list = get_next(head);
  2364. while (rtw_end_of_queue_search(head, list) == _FALSE) {
  2365. ent = LIST_CONTAINOR(list, struct blacklist_ent, list);
  2366. list = get_next(list);
  2367. if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) {
  2368. rtw_list_delete(&ent->list);
  2369. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2370. exist = _TRUE;
  2371. break;
  2372. }
  2373. if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) {
  2374. rtw_list_delete(&ent->list);
  2375. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2376. }
  2377. }
  2378. exit_critical_bh(&blist->lock);
  2379. exit:
  2380. return exist == _TRUE ? _SUCCESS : RTW_ALREADY;
  2381. }
  2382. int rtw_blacklist_search(_queue *blist, const u8 *addr)
  2383. {
  2384. struct blacklist_ent *ent = NULL;
  2385. _list *list, *head;
  2386. u8 exist = _FALSE;
  2387. enter_critical_bh(&blist->lock);
  2388. head = &blist->queue;
  2389. list = get_next(head);
  2390. while (rtw_end_of_queue_search(head, list) == _FALSE) {
  2391. ent = LIST_CONTAINOR(list, struct blacklist_ent, list);
  2392. list = get_next(list);
  2393. if (_rtw_memcmp(ent->addr, addr, ETH_ALEN) == _TRUE) {
  2394. if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) {
  2395. rtw_list_delete(&ent->list);
  2396. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2397. } else
  2398. exist = _TRUE;
  2399. break;
  2400. }
  2401. if (rtw_time_after(rtw_get_current_time(), ent->exp_time)) {
  2402. rtw_list_delete(&ent->list);
  2403. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2404. }
  2405. }
  2406. exit_critical_bh(&blist->lock);
  2407. exit:
  2408. return exist;
  2409. }
  2410. void rtw_blacklist_flush(_queue *blist)
  2411. {
  2412. struct blacklist_ent *ent;
  2413. _list *list, *head;
  2414. _list tmp;
  2415. _rtw_init_listhead(&tmp);
  2416. enter_critical_bh(&blist->lock);
  2417. rtw_list_splice_init(&blist->queue, &tmp);
  2418. exit_critical_bh(&blist->lock);
  2419. head = &tmp;
  2420. list = get_next(head);
  2421. while (rtw_end_of_queue_search(head, list) == _FALSE) {
  2422. ent = LIST_CONTAINOR(list, struct blacklist_ent, list);
  2423. list = get_next(list);
  2424. rtw_list_delete(&ent->list);
  2425. rtw_mfree(ent, sizeof(struct blacklist_ent));
  2426. }
  2427. }
  2428. void dump_blacklist(void *sel, _queue *blist, const char *title)
  2429. {
  2430. struct blacklist_ent *ent = NULL;
  2431. _list *list, *head;
  2432. enter_critical_bh(&blist->lock);
  2433. head = &blist->queue;
  2434. list = get_next(head);
  2435. if (rtw_end_of_queue_search(head, list) == _FALSE) {
  2436. if (title)
  2437. RTW_PRINT_SEL(sel, "%s:\n", title);
  2438. while (rtw_end_of_queue_search(head, list) == _FALSE) {
  2439. ent = LIST_CONTAINOR(list, struct blacklist_ent, list);
  2440. list = get_next(list);
  2441. if (rtw_time_after(rtw_get_current_time(), ent->exp_time))
  2442. RTW_PRINT_SEL(sel, MAC_FMT" expired\n", MAC_ARG(ent->addr));
  2443. else
  2444. RTW_PRINT_SEL(sel, MAC_FMT" %u\n", MAC_ARG(ent->addr)
  2445. , rtw_get_remaining_time_ms(ent->exp_time));
  2446. }
  2447. }
  2448. exit_critical_bh(&blist->lock);
  2449. }
  2450. /**
  2451. * is_null -
  2452. *
  2453. * Return TRUE if c is null character
  2454. * FALSE otherwise.
  2455. */
  2456. inline BOOLEAN is_null(char c)
  2457. {
  2458. if (c == '\0')
  2459. return _TRUE;
  2460. else
  2461. return _FALSE;
  2462. }
  2463. inline BOOLEAN is_all_null(char *c, int len)
  2464. {
  2465. for (; len > 0; len--)
  2466. if (c[len - 1] != '\0')
  2467. return _FALSE;
  2468. return _TRUE;
  2469. }
  2470. /**
  2471. * is_eol -
  2472. *
  2473. * Return TRUE if c is represent for EOL (end of line)
  2474. * FALSE otherwise.
  2475. */
  2476. inline BOOLEAN is_eol(char c)
  2477. {
  2478. if (c == '\r' || c == '\n')
  2479. return _TRUE;
  2480. else
  2481. return _FALSE;
  2482. }
  2483. /**
  2484. * is_space -
  2485. *
  2486. * Return TRUE if c is represent for space
  2487. * FALSE otherwise.
  2488. */
  2489. inline BOOLEAN is_space(char c)
  2490. {
  2491. if (c == ' ' || c == '\t')
  2492. return _TRUE;
  2493. else
  2494. return _FALSE;
  2495. }
  2496. /**
  2497. * IsHexDigit -
  2498. *
  2499. * Return TRUE if chTmp is represent for hex digit
  2500. * FALSE otherwise.
  2501. */
  2502. inline BOOLEAN IsHexDigit(char chTmp)
  2503. {
  2504. if ((chTmp >= '0' && chTmp <= '9') ||
  2505. (chTmp >= 'a' && chTmp <= 'f') ||
  2506. (chTmp >= 'A' && chTmp <= 'F'))
  2507. return _TRUE;
  2508. else
  2509. return _FALSE;
  2510. }
  2511. /**
  2512. * is_alpha -
  2513. *
  2514. * Return TRUE if chTmp is represent for alphabet
  2515. * FALSE otherwise.
  2516. */
  2517. inline BOOLEAN is_alpha(char chTmp)
  2518. {
  2519. if ((chTmp >= 'a' && chTmp <= 'z') ||
  2520. (chTmp >= 'A' && chTmp <= 'Z'))
  2521. return _TRUE;
  2522. else
  2523. return _FALSE;
  2524. }
  2525. inline char alpha_to_upper(char c)
  2526. {
  2527. if ((c >= 'a' && c <= 'z'))
  2528. c = 'A' + (c - 'a');
  2529. return c;
  2530. }
  2531. int hex2num_i(char c)
  2532. {
  2533. if (c >= '0' && c <= '9')
  2534. return c - '0';
  2535. if (c >= 'a' && c <= 'f')
  2536. return c - 'a' + 10;
  2537. if (c >= 'A' && c <= 'F')
  2538. return c - 'A' + 10;
  2539. return -1;
  2540. }
  2541. int hex2byte_i(const char *hex)
  2542. {
  2543. int a, b;
  2544. a = hex2num_i(*hex++);
  2545. if (a < 0)
  2546. return -1;
  2547. b = hex2num_i(*hex++);
  2548. if (b < 0)
  2549. return -1;
  2550. return (a << 4) | b;
  2551. }
  2552. int hexstr2bin(const char *hex, u8 *buf, size_t len)
  2553. {
  2554. size_t i;
  2555. int a;
  2556. const char *ipos = hex;
  2557. u8 *opos = buf;
  2558. for (i = 0; i < len; i++) {
  2559. a = hex2byte_i(ipos);
  2560. if (a < 0)
  2561. return -1;
  2562. *opos++ = a;
  2563. ipos += 2;
  2564. }
  2565. return 0;
  2566. }