osdep_service.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
  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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. *
  19. ******************************************************************************/
  20. #define _OSDEP_SERVICE_C_
  21. #include <drv_types.h>
  22. #define RT_TAG '1178'
  23. #ifdef DBG_MEMORY_LEAK
  24. #ifdef PLATFORM_LINUX
  25. atomic_t _malloc_cnt = ATOMIC_INIT(0);
  26. atomic_t _malloc_size = ATOMIC_INIT(0);
  27. #endif
  28. #endif /* DBG_MEMORY_LEAK */
  29. #if defined(PLATFORM_LINUX)
  30. /*
  31. * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
  32. * @return: one of RTW_STATUS_CODE
  33. */
  34. inline int RTW_STATUS_CODE(int error_code){
  35. if(error_code >=0)
  36. return _SUCCESS;
  37. switch(error_code) {
  38. //case -ETIMEDOUT:
  39. // return RTW_STATUS_TIMEDOUT;
  40. default:
  41. return _FAIL;
  42. }
  43. }
  44. #else
  45. inline int RTW_STATUS_CODE(int error_code){
  46. return error_code;
  47. }
  48. #endif
  49. u32 rtw_atoi(u8* s)
  50. {
  51. int num=0,flag=0;
  52. int i;
  53. for(i=0;i<=strlen(s);i++)
  54. {
  55. if(s[i] >= '0' && s[i] <= '9')
  56. num = num * 10 + s[i] -'0';
  57. else if(s[0] == '-' && i==0)
  58. flag =1;
  59. else
  60. break;
  61. }
  62. if(flag == 1)
  63. num = num * -1;
  64. return(num);
  65. }
  66. inline u8* _rtw_vmalloc(u32 sz)
  67. {
  68. u8 *pbuf;
  69. #ifdef PLATFORM_LINUX
  70. pbuf = vmalloc(sz);
  71. #endif
  72. #ifdef PLATFORM_FREEBSD
  73. pbuf = malloc(sz,M_DEVBUF,M_NOWAIT);
  74. #endif
  75. #ifdef PLATFORM_WINDOWS
  76. NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
  77. #endif
  78. #ifdef DBG_MEMORY_LEAK
  79. #ifdef PLATFORM_LINUX
  80. if ( pbuf != NULL) {
  81. atomic_inc(&_malloc_cnt);
  82. atomic_add(sz, &_malloc_size);
  83. }
  84. #endif
  85. #endif /* DBG_MEMORY_LEAK */
  86. return pbuf;
  87. }
  88. inline u8* _rtw_zvmalloc(u32 sz)
  89. {
  90. u8 *pbuf;
  91. #ifdef PLATFORM_LINUX
  92. pbuf = _rtw_vmalloc(sz);
  93. if (pbuf != NULL)
  94. memset(pbuf, 0, sz);
  95. #endif
  96. #ifdef PLATFORM_FREEBSD
  97. pbuf = malloc(sz,M_DEVBUF,M_ZERO|M_NOWAIT);
  98. #endif
  99. #ifdef PLATFORM_WINDOWS
  100. NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
  101. if (pbuf != NULL)
  102. NdisFillMemory(pbuf, sz, 0);
  103. #endif
  104. return pbuf;
  105. }
  106. inline void _rtw_vmfree(u8 *pbuf, u32 sz)
  107. {
  108. #ifdef PLATFORM_LINUX
  109. vfree(pbuf);
  110. #endif
  111. #ifdef PLATFORM_FREEBSD
  112. free(pbuf,M_DEVBUF);
  113. #endif
  114. #ifdef PLATFORM_WINDOWS
  115. NdisFreeMemory(pbuf,sz, 0);
  116. #endif
  117. #ifdef DBG_MEMORY_LEAK
  118. #ifdef PLATFORM_LINUX
  119. atomic_dec(&_malloc_cnt);
  120. atomic_sub(sz, &_malloc_size);
  121. #endif
  122. #endif /* DBG_MEMORY_LEAK */
  123. }
  124. u8* _rtw_malloc(u32 sz)
  125. {
  126. u8 *pbuf=NULL;
  127. #ifdef PLATFORM_LINUX
  128. #ifdef RTK_DMP_PLATFORM
  129. if(sz > 0x4000)
  130. pbuf = (u8 *)dvr_malloc(sz);
  131. else
  132. #endif
  133. pbuf = kmalloc(sz,in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  134. #endif
  135. #ifdef PLATFORM_FREEBSD
  136. pbuf = malloc(sz,M_DEVBUF,M_NOWAIT);
  137. #endif
  138. #ifdef PLATFORM_WINDOWS
  139. NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
  140. #endif
  141. #ifdef DBG_MEMORY_LEAK
  142. #ifdef PLATFORM_LINUX
  143. if ( pbuf != NULL) {
  144. atomic_inc(&_malloc_cnt);
  145. atomic_add(sz, &_malloc_size);
  146. }
  147. #endif
  148. #endif /* DBG_MEMORY_LEAK */
  149. return pbuf;
  150. }
  151. u8* _rtw_zmalloc(u32 sz)
  152. {
  153. #ifdef PLATFORM_FREEBSD
  154. return malloc(sz,M_DEVBUF,M_ZERO|M_NOWAIT);
  155. #else // PLATFORM_FREEBSD
  156. u8 *pbuf = _rtw_malloc(sz);
  157. if (pbuf != NULL) {
  158. #ifdef PLATFORM_LINUX
  159. memset(pbuf, 0, sz);
  160. #endif
  161. #ifdef PLATFORM_WINDOWS
  162. NdisFillMemory(pbuf, sz, 0);
  163. #endif
  164. }
  165. return pbuf;
  166. #endif // PLATFORM_FREEBSD
  167. }
  168. void _rtw_mfree(u8 *pbuf, u32 sz)
  169. {
  170. #ifdef PLATFORM_LINUX
  171. #ifdef RTK_DMP_PLATFORM
  172. if(sz > 0x4000)
  173. dvr_free(pbuf);
  174. else
  175. #endif
  176. kfree(pbuf);
  177. #endif
  178. #ifdef PLATFORM_FREEBSD
  179. free(pbuf,M_DEVBUF);
  180. #endif
  181. #ifdef PLATFORM_WINDOWS
  182. NdisFreeMemory(pbuf,sz, 0);
  183. #endif
  184. #ifdef DBG_MEMORY_LEAK
  185. #ifdef PLATFORM_LINUX
  186. atomic_dec(&_malloc_cnt);
  187. atomic_sub(sz, &_malloc_size);
  188. #endif
  189. #endif /* DBG_MEMORY_LEAK */
  190. }
  191. #ifdef DBG_MEM_ALLOC
  192. struct rtw_dbg_mem_stat {
  193. ATOMIC_T vir_alloc; // the memory bytes we allocate now
  194. ATOMIC_T vir_peak; // the peak memory bytes we allocate
  195. ATOMIC_T vir_alloc_err; // the error times we fail to allocate memory
  196. ATOMIC_T phy_alloc;
  197. ATOMIC_T phy_peak;
  198. ATOMIC_T phy_alloc_err;
  199. ATOMIC_T tx_alloc;
  200. ATOMIC_T tx_peak;
  201. ATOMIC_T tx_alloc_err;
  202. ATOMIC_T rx_alloc;
  203. ATOMIC_T rx_peak;
  204. ATOMIC_T rx_alloc_err;
  205. } rtw_dbg_mem_stat;
  206. void rtw_dump_mem_stat (void)
  207. {
  208. int vir_alloc, vir_peak, vir_alloc_err, phy_alloc, phy_peak, phy_alloc_err;
  209. int tx_alloc, tx_peak, tx_alloc_err, rx_alloc, rx_peak, rx_alloc_err;
  210. vir_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.vir_alloc);
  211. vir_peak=ATOMIC_READ(&rtw_dbg_mem_stat.vir_peak);
  212. vir_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.vir_alloc_err);
  213. phy_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.phy_alloc);
  214. phy_peak=ATOMIC_READ(&rtw_dbg_mem_stat.phy_peak);
  215. phy_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.phy_alloc_err);
  216. tx_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.tx_alloc);
  217. tx_peak=ATOMIC_READ(&rtw_dbg_mem_stat.tx_peak);
  218. tx_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.tx_alloc_err);
  219. rx_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.rx_alloc);
  220. rx_peak=ATOMIC_READ(&rtw_dbg_mem_stat.rx_peak);
  221. rx_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.rx_alloc_err);
  222. DBG_871X( "vir_alloc:%d, vir_peak:%d, vir_alloc_err:%d\n"
  223. "phy_alloc:%d, phy_peak:%d, phy_alloc_err:%d\n"
  224. "tx_alloc:%d, tx_peak:%d, tx_alloc_err:%d\n"
  225. "rx_alloc:%d, rx_peak:%d, rx_alloc_err:%d\n"
  226. , vir_alloc, vir_peak, vir_alloc_err
  227. , phy_alloc, phy_peak, phy_alloc_err
  228. , tx_alloc, tx_peak, tx_alloc_err
  229. , rx_alloc, rx_peak, rx_alloc_err
  230. );
  231. }
  232. void rtw_update_mem_stat(u8 flag, u32 sz)
  233. {
  234. static u32 update_time = 0;
  235. int peak, alloc;
  236. if(!update_time) {
  237. ATOMIC_SET(&rtw_dbg_mem_stat.vir_alloc,0);
  238. ATOMIC_SET(&rtw_dbg_mem_stat.vir_peak,0);
  239. ATOMIC_SET(&rtw_dbg_mem_stat.vir_alloc_err,0);
  240. ATOMIC_SET(&rtw_dbg_mem_stat.phy_alloc,0);
  241. ATOMIC_SET(&rtw_dbg_mem_stat.phy_peak,0);
  242. ATOMIC_SET(&rtw_dbg_mem_stat.phy_alloc_err,0);
  243. }
  244. switch(flag) {
  245. case MEM_STAT_VIR_ALLOC_SUCCESS:
  246. alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.vir_alloc, sz);
  247. peak=ATOMIC_READ(&rtw_dbg_mem_stat.vir_peak);
  248. if (peak<alloc)
  249. ATOMIC_SET(&rtw_dbg_mem_stat.vir_peak, alloc);
  250. break;
  251. case MEM_STAT_VIR_ALLOC_FAIL:
  252. ATOMIC_INC(&rtw_dbg_mem_stat.vir_alloc_err);
  253. break;
  254. case MEM_STAT_VIR_FREE:
  255. alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.vir_alloc, sz);
  256. break;
  257. case MEM_STAT_PHY_ALLOC_SUCCESS:
  258. alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.phy_alloc, sz);
  259. peak=ATOMIC_READ(&rtw_dbg_mem_stat.phy_peak);
  260. if (peak<alloc)
  261. ATOMIC_SET(&rtw_dbg_mem_stat.phy_peak, alloc);
  262. break;
  263. case MEM_STAT_PHY_ALLOC_FAIL:
  264. ATOMIC_INC(&rtw_dbg_mem_stat.phy_alloc_err);
  265. break;
  266. case MEM_STAT_PHY_FREE:
  267. alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.phy_alloc, sz);
  268. break;
  269. case MEM_STAT_TX_ALLOC_SUCCESS:
  270. alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.tx_alloc, sz);
  271. peak=ATOMIC_READ(&rtw_dbg_mem_stat.tx_peak);
  272. if (peak<alloc)
  273. ATOMIC_SET(&rtw_dbg_mem_stat.tx_peak, alloc);
  274. break;
  275. case MEM_STAT_TX_ALLOC_FAIL:
  276. ATOMIC_INC(&rtw_dbg_mem_stat.tx_alloc_err);
  277. break;
  278. case MEM_STAT_TX_FREE:
  279. alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.tx_alloc, sz);
  280. break;
  281. case MEM_STAT_RX_ALLOC_SUCCESS:
  282. alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.rx_alloc, sz);
  283. peak=ATOMIC_READ(&rtw_dbg_mem_stat.rx_peak);
  284. if (peak<alloc)
  285. ATOMIC_SET(&rtw_dbg_mem_stat.rx_peak, alloc);
  286. break;
  287. case MEM_STAT_RX_ALLOC_FAIL:
  288. ATOMIC_INC(&rtw_dbg_mem_stat.rx_alloc_err);
  289. break;
  290. case MEM_STAT_RX_FREE:
  291. alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.rx_alloc, sz);
  292. break;
  293. };
  294. if (rtw_get_passing_time_ms(update_time) > 5000) {
  295. rtw_dump_mem_stat();
  296. update_time=rtw_get_current_time();
  297. }
  298. }
  299. inline u8* dbg_rtw_vmalloc(u32 sz, const char *func, int line)
  300. {
  301. u8 *p;
  302. //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  303. p=_rtw_vmalloc((sz));
  304. rtw_update_mem_stat(
  305. p ? MEM_STAT_VIR_ALLOC_SUCCESS : MEM_STAT_VIR_ALLOC_FAIL
  306. , sz
  307. );
  308. return p;
  309. }
  310. inline u8* dbg_rtw_zvmalloc(u32 sz, const char *func, int line)
  311. {
  312. u8 *p;
  313. //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  314. p=_rtw_zvmalloc((sz));
  315. rtw_update_mem_stat(
  316. p ? MEM_STAT_VIR_ALLOC_SUCCESS : MEM_STAT_VIR_ALLOC_FAIL
  317. , sz
  318. );
  319. return p;
  320. }
  321. inline void dbg_rtw_vmfree(u8 *pbuf, u32 sz, const char *func, int line)
  322. {
  323. //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
  324. _rtw_vmfree((pbuf), (sz));
  325. rtw_update_mem_stat(
  326. MEM_STAT_VIR_FREE
  327. , sz
  328. );
  329. }
  330. inline u8* dbg_rtw_malloc(u32 sz, const char *func, int line)
  331. {
  332. u8 *p;
  333. if((sz)>4096)
  334. DBG_871X("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  335. p=_rtw_malloc((sz));
  336. rtw_update_mem_stat(
  337. p ? MEM_STAT_PHY_ALLOC_SUCCESS : MEM_STAT_PHY_ALLOC_FAIL
  338. , sz
  339. );
  340. return p;
  341. }
  342. inline u8* dbg_rtw_zmalloc(u32 sz, const char *func, int line)
  343. {
  344. u8 *p;
  345. if((sz)>4096)
  346. DBG_871X("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
  347. p = _rtw_zmalloc((sz));
  348. rtw_update_mem_stat(
  349. p ? MEM_STAT_PHY_ALLOC_SUCCESS : MEM_STAT_PHY_ALLOC_FAIL
  350. , sz
  351. );
  352. return p;
  353. }
  354. inline void dbg_rtw_mfree(u8 *pbuf, u32 sz, const char *func, int line)
  355. {
  356. if((sz)>4096)
  357. DBG_871X("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
  358. _rtw_mfree((pbuf), (sz));
  359. rtw_update_mem_stat(
  360. MEM_STAT_PHY_FREE
  361. , sz
  362. );
  363. }
  364. #endif
  365. void* rtw_malloc2d(int h, int w, int size)
  366. {
  367. int j;
  368. void **a = (void **) rtw_zmalloc( h*sizeof(void *) + h*w*size );
  369. if(a == NULL)
  370. {
  371. DBG_871X("%s: alloc memory fail!\n", __FUNCTION__);
  372. return NULL;
  373. }
  374. for( j=0; j<h; j++ )
  375. a[j] = ((char *)(a+h)) + j*w*size;
  376. return a;
  377. }
  378. void rtw_mfree2d(void *pbuf, int h, int w, int size)
  379. {
  380. rtw_mfree((u8 *)pbuf, h*sizeof(void*) + w*h*size);
  381. }
  382. void _rtw_memcpy(void* dst, void* src, u32 sz)
  383. {
  384. #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
  385. memcpy(dst, src, sz);
  386. #endif
  387. #ifdef PLATFORM_WINDOWS
  388. NdisMoveMemory(dst, src, sz);
  389. #endif
  390. }
  391. int _rtw_memcmp(void *dst, void *src, u32 sz)
  392. {
  393. #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
  394. //under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0
  395. if (!(memcmp(dst, src, sz)))
  396. return _TRUE;
  397. else
  398. return _FALSE;
  399. #endif
  400. #ifdef PLATFORM_WINDOWS
  401. //under Windows, the return value of NdisEqualMemory for two same mem. chunk is 1
  402. if (NdisEqualMemory (dst, src, sz))
  403. return _TRUE;
  404. else
  405. return _FALSE;
  406. #endif
  407. }
  408. void _rtw_memset(void *pbuf, int c, u32 sz)
  409. {
  410. #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
  411. memset(pbuf, c, sz);
  412. #endif
  413. #ifdef PLATFORM_WINDOWS
  414. #if 0
  415. NdisZeroMemory(pbuf, sz);
  416. if (c != 0) memset(pbuf, c, sz);
  417. #else
  418. NdisFillMemory(pbuf, sz, c);
  419. #endif
  420. #endif
  421. }
  422. #ifdef PLATFORM_FREEBSD
  423. static inline void __list_add(_list *pnew, _list *pprev, _list *pnext)
  424. {
  425. pnext->prev = pnew;
  426. pnew->next = pnext;
  427. pnew->prev = pprev;
  428. pprev->next = pnew;
  429. }
  430. //review again
  431. struct sk_buff * dev_alloc_skb(unsigned int size)
  432. {
  433. struct sk_buff *skb=NULL;
  434. u8 *data=NULL;
  435. //skb = (struct sk_buff *)_rtw_zmalloc(sizeof(struct sk_buff)); // for skb->len, etc.
  436. skb = (struct sk_buff *)_rtw_malloc(sizeof(struct sk_buff));
  437. if(!skb)
  438. goto out;
  439. data = _rtw_malloc(size);
  440. if(!data)
  441. goto nodata;
  442. skb->head = (unsigned char*)data;
  443. skb->data = (unsigned char*)data;
  444. skb->tail = (unsigned char*)data;
  445. skb->end = (unsigned char*)data + size;
  446. skb->len = 0;
  447. //printf("%s()-%d: skb=%p, skb->head = %p\n", __FUNCTION__, __LINE__, skb, skb->head);
  448. out:
  449. return skb;
  450. nodata:
  451. _rtw_mfree((u8 *)skb, sizeof(struct sk_buff));
  452. skb = NULL;
  453. goto out;
  454. }
  455. void dev_kfree_skb_any(struct sk_buff *skb)
  456. {
  457. //printf("%s()-%d: skb->head = %p\n", __FUNCTION__, __LINE__, skb->head);
  458. if(skb->head)
  459. _rtw_mfree(skb->head, 0);
  460. //printf("%s()-%d: skb = %p\n", __FUNCTION__, __LINE__, skb);
  461. if(skb)
  462. _rtw_mfree((u8 *)skb, 0);
  463. }
  464. struct sk_buff *skb_clone(const struct sk_buff *skb)
  465. {
  466. return NULL;
  467. }
  468. #endif
  469. void _rtw_init_listhead(_list *list)
  470. {
  471. #ifdef PLATFORM_LINUX
  472. INIT_LIST_HEAD(list);
  473. #endif
  474. #ifdef PLATFORM_FREEBSD
  475. list->next = list;
  476. list->prev = list;
  477. #endif
  478. #ifdef PLATFORM_WINDOWS
  479. NdisInitializeListHead(list);
  480. #endif
  481. }
  482. /*
  483. For the following list_xxx operations,
  484. caller must guarantee the atomic context.
  485. Otherwise, there will be racing condition.
  486. */
  487. u32 rtw_is_list_empty(_list *phead)
  488. {
  489. #ifdef PLATFORM_LINUX
  490. if (list_empty(phead))
  491. return _TRUE;
  492. else
  493. return _FALSE;
  494. #endif
  495. #ifdef PLATFORM_FREEBSD
  496. if (phead->next == phead)
  497. return _TRUE;
  498. else
  499. return _FALSE;
  500. #endif
  501. #ifdef PLATFORM_WINDOWS
  502. if (IsListEmpty(phead))
  503. return _TRUE;
  504. else
  505. return _FALSE;
  506. #endif
  507. }
  508. void rtw_list_insert_head(_list *plist, _list *phead)
  509. {
  510. #ifdef PLATFORM_LINUX
  511. list_add(plist, phead);
  512. #endif
  513. #ifdef PLATFORM_FREEBSD
  514. __list_add(plist, phead, phead->next);
  515. #endif
  516. #ifdef PLATFORM_WINDOWS
  517. InsertHeadList(phead, plist);
  518. #endif
  519. }
  520. void rtw_list_insert_tail(_list *plist, _list *phead)
  521. {
  522. #ifdef PLATFORM_LINUX
  523. list_add_tail(plist, phead);
  524. #endif
  525. #ifdef PLATFORM_FREEBSD
  526. __list_add(plist, phead->prev, phead);
  527. #endif
  528. #ifdef PLATFORM_WINDOWS
  529. InsertTailList(phead, plist);
  530. #endif
  531. }
  532. void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc)
  533. {
  534. _adapter *adapter = (_adapter *)padapter;
  535. #ifdef PLATFORM_LINUX
  536. _init_timer(ptimer, adapter->pnetdev, pfunc, adapter);
  537. #endif
  538. #ifdef PLATFORM_FREEBSD
  539. _init_timer(ptimer, adapter->pifp, pfunc, adapter->mlmepriv.nic_hdl);
  540. #endif
  541. #ifdef PLATFORM_WINDOWS
  542. _init_timer(ptimer, adapter->hndis_adapter, pfunc, adapter->mlmepriv.nic_hdl);
  543. #endif
  544. }
  545. /*
  546. Caller must check if the list is empty before calling rtw_list_delete
  547. */
  548. void _rtw_init_sema(_sema *sema, int init_val)
  549. {
  550. #ifdef PLATFORM_LINUX
  551. sema_init(sema, init_val);
  552. #endif
  553. #ifdef PLATFORM_FREEBSD
  554. sema_init(sema, init_val, "rtw_drv");
  555. #endif
  556. #ifdef PLATFORM_OS_XP
  557. KeInitializeSemaphore(sema, init_val, SEMA_UPBND); // count=0;
  558. #endif
  559. #ifdef PLATFORM_OS_CE
  560. if(*sema == NULL)
  561. *sema = CreateSemaphore(NULL, init_val, SEMA_UPBND, NULL);
  562. #endif
  563. }
  564. void _rtw_free_sema(_sema *sema)
  565. {
  566. #ifdef PLATFORM_FREEBSD
  567. sema_destroy(sema);
  568. #endif
  569. #ifdef PLATFORM_OS_CE
  570. CloseHandle(*sema);
  571. #endif
  572. }
  573. void _rtw_up_sema(_sema *sema)
  574. {
  575. #ifdef PLATFORM_LINUX
  576. up(sema);
  577. #endif
  578. #ifdef PLATFORM_FREEBSD
  579. sema_post(sema);
  580. #endif
  581. #ifdef PLATFORM_OS_XP
  582. KeReleaseSemaphore(sema, IO_NETWORK_INCREMENT, 1, FALSE );
  583. #endif
  584. #ifdef PLATFORM_OS_CE
  585. ReleaseSemaphore(*sema, 1, NULL );
  586. #endif
  587. }
  588. u32 _rtw_down_sema(_sema *sema)
  589. {
  590. #ifdef PLATFORM_LINUX
  591. if (down_interruptible(sema))
  592. return _FAIL;
  593. else
  594. return _SUCCESS;
  595. #endif
  596. #ifdef PLATFORM_FREEBSD
  597. sema_wait(sema);
  598. return _SUCCESS;
  599. #endif
  600. #ifdef PLATFORM_OS_XP
  601. if(STATUS_SUCCESS == KeWaitForSingleObject(sema, Executive, KernelMode, TRUE, NULL))
  602. return _SUCCESS;
  603. else
  604. return _FAIL;
  605. #endif
  606. #ifdef PLATFORM_OS_CE
  607. if(WAIT_OBJECT_0 == WaitForSingleObject(*sema, INFINITE ))
  608. return _SUCCESS;
  609. else
  610. return _FAIL;
  611. #endif
  612. }
  613. void _rtw_mutex_init(_mutex *pmutex)
  614. {
  615. #ifdef PLATFORM_LINUX
  616. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
  617. mutex_init(pmutex);
  618. #else
  619. init_MUTEX(pmutex);
  620. #endif
  621. #endif
  622. #ifdef PLATFORM_FREEBSD
  623. mtx_init(pmutex, "", NULL, MTX_DEF|MTX_RECURSE);
  624. #endif
  625. #ifdef PLATFORM_OS_XP
  626. KeInitializeMutex(pmutex, 0);
  627. #endif
  628. #ifdef PLATFORM_OS_CE
  629. *pmutex = CreateMutex( NULL, _FALSE, NULL);
  630. #endif
  631. }
  632. void _rtw_mutex_free(_mutex *pmutex);
  633. void _rtw_mutex_free(_mutex *pmutex)
  634. {
  635. #ifdef PLATFORM_LINUX
  636. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
  637. mutex_destroy(pmutex);
  638. #else
  639. #endif
  640. #ifdef PLATFORM_FREEBSD
  641. sema_destroy(pmutex);
  642. #endif
  643. #endif
  644. #ifdef PLATFORM_OS_XP
  645. #endif
  646. #ifdef PLATFORM_OS_CE
  647. #endif
  648. }
  649. void _rtw_spinlock_init(_lock *plock)
  650. {
  651. #ifdef PLATFORM_LINUX
  652. spin_lock_init(plock);
  653. #endif
  654. #ifdef PLATFORM_FREEBSD
  655. mtx_init(plock, "", NULL, MTX_DEF|MTX_RECURSE);
  656. #endif
  657. #ifdef PLATFORM_WINDOWS
  658. NdisAllocateSpinLock(plock);
  659. #endif
  660. }
  661. void _rtw_spinlock_free(_lock *plock)
  662. {
  663. #ifdef PLATFORM_FREEBSD
  664. mtx_destroy(plock);
  665. #endif
  666. #ifdef PLATFORM_WINDOWS
  667. NdisFreeSpinLock(plock);
  668. #endif
  669. }
  670. #ifdef PLATFORM_FREEBSD
  671. extern PADAPTER prtw_lock;
  672. void rtw_mtx_lock(_lock *plock){
  673. if(prtw_lock){
  674. mtx_lock(&prtw_lock->glock);
  675. }
  676. else{
  677. printf("%s prtw_lock==NULL",__FUNCTION__);
  678. }
  679. }
  680. void rtw_mtx_unlock(_lock *plock){
  681. if(prtw_lock){
  682. mtx_unlock(&prtw_lock->glock);
  683. }
  684. else{
  685. printf("%s prtw_lock==NULL",__FUNCTION__);
  686. }
  687. }
  688. #endif //PLATFORM_FREEBSD
  689. void _rtw_spinlock(_lock *plock)
  690. {
  691. #ifdef PLATFORM_LINUX
  692. spin_lock(plock);
  693. #endif
  694. #ifdef PLATFORM_FREEBSD
  695. mtx_lock(plock);
  696. #endif
  697. #ifdef PLATFORM_WINDOWS
  698. NdisAcquireSpinLock(plock);
  699. #endif
  700. }
  701. void _rtw_spinunlock(_lock *plock)
  702. {
  703. #ifdef PLATFORM_LINUX
  704. spin_unlock(plock);
  705. #endif
  706. #ifdef PLATFORM_FREEBSD
  707. mtx_unlock(plock);
  708. #endif
  709. #ifdef PLATFORM_WINDOWS
  710. NdisReleaseSpinLock(plock);
  711. #endif
  712. }
  713. void _rtw_spinlock_ex(_lock *plock)
  714. {
  715. #ifdef PLATFORM_LINUX
  716. spin_lock(plock);
  717. #endif
  718. #ifdef PLATFORM_FREEBSD
  719. mtx_lock(plock);
  720. #endif
  721. #ifdef PLATFORM_WINDOWS
  722. NdisDprAcquireSpinLock(plock);
  723. #endif
  724. }
  725. void _rtw_spinunlock_ex(_lock *plock)
  726. {
  727. #ifdef PLATFORM_LINUX
  728. spin_unlock(plock);
  729. #endif
  730. #ifdef PLATFORM_FREEBSD
  731. mtx_unlock(plock);
  732. #endif
  733. #ifdef PLATFORM_WINDOWS
  734. NdisDprReleaseSpinLock(plock);
  735. #endif
  736. }
  737. void _rtw_init_queue(_queue *pqueue)
  738. {
  739. _rtw_init_listhead(&(pqueue->queue));
  740. _rtw_spinlock_init(&(pqueue->lock));
  741. }
  742. u32 _rtw_queue_empty(_queue *pqueue)
  743. {
  744. return (rtw_is_list_empty(&(pqueue->queue)));
  745. }
  746. u32 rtw_end_of_queue_search(_list *head, _list *plist)
  747. {
  748. if (head == plist)
  749. return _TRUE;
  750. else
  751. return _FALSE;
  752. }
  753. u32 rtw_get_current_time(void)
  754. {
  755. #ifdef PLATFORM_LINUX
  756. return jiffies;
  757. #endif
  758. #ifdef PLATFORM_FREEBSD
  759. struct timeval tvp;
  760. getmicrotime(&tvp);
  761. return tvp.tv_sec;
  762. #endif
  763. #ifdef PLATFORM_WINDOWS
  764. LARGE_INTEGER SystemTime;
  765. NdisGetCurrentSystemTime(&SystemTime);
  766. return (u32)(SystemTime.LowPart);// count of 100-nanosecond intervals
  767. #endif
  768. }
  769. inline u32 rtw_systime_to_ms(u32 systime)
  770. {
  771. #ifdef PLATFORM_LINUX
  772. return systime * 1000 / HZ;
  773. #endif
  774. #ifdef PLATFORM_FREEBSD
  775. return systime * 1000;
  776. #endif
  777. #ifdef PLATFORM_WINDOWS
  778. return systime / 10000 ;
  779. #endif
  780. }
  781. inline u32 rtw_ms_to_systime(u32 ms)
  782. {
  783. #ifdef PLATFORM_LINUX
  784. return ms * HZ / 1000;
  785. #endif
  786. #ifdef PLATFORM_FREEBSD
  787. return ms /1000;
  788. #endif
  789. #ifdef PLATFORM_WINDOWS
  790. return ms * 10000 ;
  791. #endif
  792. }
  793. // the input parameter start use the same unit as returned by rtw_get_current_time
  794. inline s32 rtw_get_passing_time_ms(u32 start)
  795. {
  796. #ifdef PLATFORM_LINUX
  797. return rtw_systime_to_ms(jiffies-start);
  798. #endif
  799. #ifdef PLATFORM_FREEBSD
  800. return rtw_systime_to_ms(rtw_get_current_time());
  801. #endif
  802. #ifdef PLATFORM_WINDOWS
  803. LARGE_INTEGER SystemTime;
  804. NdisGetCurrentSystemTime(&SystemTime);
  805. return rtw_systime_to_ms((u32)(SystemTime.LowPart) - start) ;
  806. #endif
  807. }
  808. inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
  809. {
  810. #ifdef PLATFORM_LINUX
  811. return rtw_systime_to_ms(end-start);
  812. #endif
  813. #ifdef PLATFORM_FREEBSD
  814. return rtw_systime_to_ms(rtw_get_current_time());
  815. #endif
  816. #ifdef PLATFORM_WINDOWS
  817. return rtw_systime_to_ms(end-start);
  818. #endif
  819. }
  820. void rtw_sleep_schedulable(int ms)
  821. {
  822. #ifdef PLATFORM_LINUX
  823. u32 delta;
  824. delta = (ms * HZ)/1000;//(ms)
  825. if (delta == 0) {
  826. delta = 1;// 1 ms
  827. }
  828. set_current_state(TASK_INTERRUPTIBLE);
  829. if (schedule_timeout(delta) != 0) {
  830. return ;
  831. }
  832. return;
  833. #endif
  834. #ifdef PLATFORM_FREEBSD
  835. DELAY(ms*1000);
  836. return ;
  837. #endif
  838. #ifdef PLATFORM_WINDOWS
  839. NdisMSleep(ms*1000); //(us)*1000=(ms)
  840. #endif
  841. }
  842. void rtw_msleep_os(int ms)
  843. {
  844. #ifdef PLATFORM_LINUX
  845. msleep((unsigned int)ms);
  846. #endif
  847. #ifdef PLATFORM_FREEBSD
  848. //Delay for delay microseconds
  849. DELAY(ms*1000);
  850. return ;
  851. #endif
  852. #ifdef PLATFORM_WINDOWS
  853. NdisMSleep(ms*1000); //(us)*1000=(ms)
  854. #endif
  855. }
  856. void rtw_usleep_os(int us)
  857. {
  858. #ifdef PLATFORM_LINUX
  859. // msleep((unsigned int)us);
  860. if ( 1 < (us/1000) )
  861. msleep(1);
  862. else
  863. msleep( (us/1000) + 1);
  864. #endif
  865. #ifdef PLATFORM_FREEBSD
  866. //Delay for delay microseconds
  867. DELAY(us);
  868. return ;
  869. #endif
  870. #ifdef PLATFORM_WINDOWS
  871. NdisMSleep(us); //(us)
  872. #endif
  873. }
  874. #ifdef DBG_DELAY_OS
  875. void _rtw_mdelay_os(int ms, const char *func, const int line)
  876. {
  877. #if 0
  878. if(ms>10)
  879. DBG_871X("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
  880. rtw_msleep_os(ms);
  881. return;
  882. #endif
  883. DBG_871X("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
  884. #if defined(PLATFORM_LINUX)
  885. mdelay((unsigned long)ms);
  886. #elif defined(PLATFORM_WINDOWS)
  887. NdisStallExecution(ms*1000); //(us)*1000=(ms)
  888. #endif
  889. }
  890. void _rtw_udelay_os(int us, const char *func, const int line)
  891. {
  892. #if 0
  893. if(us > 1000) {
  894. DBG_871X("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
  895. rtw_usleep_os(us);
  896. return;
  897. }
  898. #endif
  899. DBG_871X("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
  900. #if defined(PLATFORM_LINUX)
  901. udelay((unsigned long)us);
  902. #elif defined(PLATFORM_WINDOWS)
  903. NdisStallExecution(us); //(us)
  904. #endif
  905. }
  906. #else
  907. void rtw_mdelay_os(int ms)
  908. {
  909. #ifdef PLATFORM_LINUX
  910. mdelay((unsigned long)ms);
  911. #endif
  912. #ifdef PLATFORM_FREEBSD
  913. DELAY(ms*1000);
  914. return ;
  915. #endif
  916. #ifdef PLATFORM_WINDOWS
  917. NdisStallExecution(ms*1000); //(us)*1000=(ms)
  918. #endif
  919. }
  920. void rtw_udelay_os(int us)
  921. {
  922. #ifdef PLATFORM_LINUX
  923. udelay((unsigned long)us);
  924. #endif
  925. #ifdef PLATFORM_FREEBSD
  926. //Delay for delay microseconds
  927. DELAY(us);
  928. return ;
  929. #endif
  930. #ifdef PLATFORM_WINDOWS
  931. NdisStallExecution(us); //(us)
  932. #endif
  933. }
  934. #endif
  935. void rtw_yield_os()
  936. {
  937. #ifdef PLATFORM_LINUX
  938. yield();
  939. #endif
  940. #ifdef PLATFORM_FREEBSD
  941. yield();
  942. #endif
  943. #ifdef PLATFORM_WINDOWS
  944. SwitchToThread();
  945. #endif
  946. }
  947. #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
  948. #ifdef CONFIG_WAKELOCK
  949. static struct wake_lock rtw_suspend_lock;
  950. #elif defined(CONFIG_ANDROID_POWER)
  951. static android_suspend_lock_t rtw_suspend_lock ={
  952. .name = RTW_SUSPEND_LOCK_NAME
  953. };
  954. #endif
  955. inline void rtw_suspend_lock_init()
  956. {
  957. #ifdef CONFIG_WAKELOCK
  958. wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME);
  959. #elif defined(CONFIG_ANDROID_POWER)
  960. android_init_suspend_lock(&rtw_suspend_lock);
  961. #endif
  962. }
  963. inline void rtw_suspend_lock_uninit()
  964. {
  965. #ifdef CONFIG_WAKELOCK
  966. wake_lock_destroy(&rtw_suspend_lock);
  967. #elif defined(CONFIG_ANDROID_POWER)
  968. android_uninit_suspend_lock(&rtw_suspend_lock);
  969. #endif
  970. }
  971. inline void rtw_lock_suspend()
  972. {
  973. #ifdef CONFIG_WAKELOCK
  974. wake_lock(&rtw_suspend_lock);
  975. #elif defined(CONFIG_ANDROID_POWER)
  976. android_lock_suspend(&rtw_suspend_lock);
  977. #endif
  978. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  979. //DBG_871X("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
  980. #endif
  981. }
  982. inline void rtw_unlock_suspend()
  983. {
  984. #ifdef CONFIG_WAKELOCK
  985. wake_unlock(&rtw_suspend_lock);
  986. #elif defined(CONFIG_ANDROID_POWER)
  987. android_unlock_suspend(&rtw_suspend_lock);
  988. #endif
  989. #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
  990. //DBG_871X("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
  991. #endif
  992. }
  993. #ifdef CONFIG_WOWLAN
  994. inline void rtw_lock_suspend_timeout(long timeout)
  995. {
  996. #ifdef CONFIG_WAKELOCK
  997. wake_lock_timeout(&rtw_suspend_lock, timeout);
  998. #elif defined(CONFIG_ANDROID_POWER)
  999. android_lock_suspend_auto_expire(&rtw_suspend_lock, timeout);
  1000. #endif
  1001. }
  1002. #endif //CONFIG_WOWLAN
  1003. inline void ATOMIC_SET(ATOMIC_T *v, int i)
  1004. {
  1005. #ifdef PLATFORM_LINUX
  1006. atomic_set(v,i);
  1007. #elif defined(PLATFORM_WINDOWS)
  1008. *v=i;// other choice????
  1009. #elif defined(PLATFORM_FREEBSD)
  1010. atomic_set_int(v,i);
  1011. #endif
  1012. }
  1013. inline int ATOMIC_READ(ATOMIC_T *v)
  1014. {
  1015. #ifdef PLATFORM_LINUX
  1016. return atomic_read(v);
  1017. #elif defined(PLATFORM_WINDOWS)
  1018. return *v; // other choice????
  1019. #elif defined(PLATFORM_FREEBSD)
  1020. return atomic_load_acq_32(v);
  1021. #endif
  1022. }
  1023. inline void ATOMIC_ADD(ATOMIC_T *v, int i)
  1024. {
  1025. #ifdef PLATFORM_LINUX
  1026. atomic_add(i,v);
  1027. #elif defined(PLATFORM_WINDOWS)
  1028. InterlockedAdd(v,i);
  1029. #elif defined(PLATFORM_FREEBSD)
  1030. atomic_add_int(v,i);
  1031. #endif
  1032. }
  1033. inline void ATOMIC_SUB(ATOMIC_T *v, int i)
  1034. {
  1035. #ifdef PLATFORM_LINUX
  1036. atomic_sub(i,v);
  1037. #elif defined(PLATFORM_WINDOWS)
  1038. InterlockedAdd(v,-i);
  1039. #elif defined(PLATFORM_FREEBSD)
  1040. atomic_subtract_int(v,i);
  1041. #endif
  1042. }
  1043. inline void ATOMIC_INC(ATOMIC_T *v)
  1044. {
  1045. #ifdef PLATFORM_LINUX
  1046. atomic_inc(v);
  1047. #elif defined(PLATFORM_WINDOWS)
  1048. InterlockedIncrement(v);
  1049. #elif defined(PLATFORM_FREEBSD)
  1050. atomic_add_int(v,1);
  1051. #endif
  1052. }
  1053. inline void ATOMIC_DEC(ATOMIC_T *v)
  1054. {
  1055. #ifdef PLATFORM_LINUX
  1056. atomic_dec(v);
  1057. #elif defined(PLATFORM_WINDOWS)
  1058. InterlockedDecrement(v);
  1059. #elif defined(PLATFORM_FREEBSD)
  1060. atomic_subtract_int(v,1);
  1061. #endif
  1062. }
  1063. inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
  1064. {
  1065. #ifdef PLATFORM_LINUX
  1066. return atomic_add_return(i,v);
  1067. #elif defined(PLATFORM_WINDOWS)
  1068. return InterlockedAdd(v,i);
  1069. #elif defined(PLATFORM_FREEBSD)
  1070. atomic_add_int(v,i);
  1071. return atomic_load_acq_32(v);
  1072. #endif
  1073. }
  1074. inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
  1075. {
  1076. #ifdef PLATFORM_LINUX
  1077. return atomic_sub_return(i,v);
  1078. #elif defined(PLATFORM_WINDOWS)
  1079. return InterlockedAdd(v,-i);
  1080. #elif defined(PLATFORM_FREEBSD)
  1081. atomic_subtract_int(v,i);
  1082. return atomic_load_acq_32(v);
  1083. #endif
  1084. }
  1085. inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
  1086. {
  1087. #ifdef PLATFORM_LINUX
  1088. return atomic_inc_return(v);
  1089. #elif defined(PLATFORM_WINDOWS)
  1090. return InterlockedIncrement(v);
  1091. #elif defined(PLATFORM_FREEBSD)
  1092. atomic_add_int(v,1);
  1093. return atomic_load_acq_32(v);
  1094. #endif
  1095. }
  1096. inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
  1097. {
  1098. #ifdef PLATFORM_LINUX
  1099. return atomic_dec_return(v);
  1100. #elif defined(PLATFORM_WINDOWS)
  1101. return InterlockedDecrement(v);
  1102. #elif defined(PLATFORM_FREEBSD)
  1103. atomic_subtract_int(v,1);
  1104. return atomic_load_acq_32(v);
  1105. #endif
  1106. }
  1107. #ifdef PLATFORM_LINUX
  1108. /*
  1109. * Open a file with the specific @param path, @param flag, @param mode
  1110. * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
  1111. * @param path the path of the file to open
  1112. * @param flag file operation flags, please refer to linux document
  1113. * @param mode please refer to linux document
  1114. * @return Linux specific error code
  1115. */
  1116. static int openFile(struct file **fpp, char *path, int flag, int mode)
  1117. {
  1118. struct file *fp;
  1119. fp=filp_open(path, flag, mode);
  1120. if(IS_ERR(fp)) {
  1121. *fpp=NULL;
  1122. return PTR_ERR(fp);
  1123. }
  1124. else {
  1125. *fpp=fp;
  1126. return 0;
  1127. }
  1128. }
  1129. /*
  1130. * Close the file with the specific @param fp
  1131. * @param fp the pointer of struct file to close
  1132. * @return always 0
  1133. */
  1134. static int closeFile(struct file *fp)
  1135. {
  1136. filp_close(fp,NULL);
  1137. return 0;
  1138. }
  1139. static int readFile(struct file *fp,char *buf,int len)
  1140. {
  1141. int rlen=0, sum=0;
  1142. if (!fp->f_op || !fp->f_op->read)
  1143. return -EPERM;
  1144. while(sum<len) {
  1145. rlen=fp->f_op->read(fp,buf+sum,len-sum, &fp->f_pos);
  1146. if(rlen>0)
  1147. sum+=rlen;
  1148. else if(0 != rlen)
  1149. return rlen;
  1150. else
  1151. break;
  1152. }
  1153. return sum;
  1154. }
  1155. static int writeFile(struct file *fp,char *buf,int len)
  1156. {
  1157. int wlen=0, sum=0;
  1158. if (!fp->f_op || !fp->f_op->write)
  1159. return -EPERM;
  1160. while(sum<len) {
  1161. wlen=fp->f_op->write(fp,buf+sum,len-sum, &fp->f_pos);
  1162. if(wlen>0)
  1163. sum+=wlen;
  1164. else if(0 != wlen)
  1165. return wlen;
  1166. else
  1167. break;
  1168. }
  1169. return sum;
  1170. }
  1171. /*
  1172. * Test if the specifi @param path is a file and readable
  1173. * @param path the path of the file to test
  1174. * @return Linux specific error code
  1175. */
  1176. static int isFileReadable(char *path)
  1177. {
  1178. struct file *fp;
  1179. int ret = 0;
  1180. char buf;
  1181. fp=filp_open(path, O_RDONLY, 0);
  1182. if(IS_ERR(fp)) {
  1183. ret = PTR_ERR(fp);
  1184. }
  1185. else {
  1186. ret = kernel_read(fp, &buf, 1, NULL);
  1187. closeFile(fp);
  1188. }
  1189. return ret;
  1190. }
  1191. /*
  1192. * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
  1193. * @param path the path of the file to open and read
  1194. * @param buf the starting address of the buffer to store file content
  1195. * @param sz how many bytes to read at most
  1196. * @return the byte we've read, or Linux specific error code
  1197. */
  1198. static int retriveFromFile(char *path, u8* buf, u32 sz)
  1199. {
  1200. int ret =-1;
  1201. struct file *fp;
  1202. if(path && buf) {
  1203. if( 0 == (ret=openFile(&fp,path, O_RDONLY, 0)) ){
  1204. DBG_871X("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
  1205. ret = kernel_read(fp, buf, sz, NULL);
  1206. closeFile(fp);
  1207. DBG_871X("%s readFile, ret:%d\n",__FUNCTION__, ret);
  1208. } else {
  1209. DBG_871X("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
  1210. }
  1211. } else {
  1212. DBG_871X("%s NULL pointer\n",__FUNCTION__);
  1213. ret = -EINVAL;
  1214. }
  1215. return ret;
  1216. }
  1217. /*
  1218. * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
  1219. * @param path the path of the file to open and write
  1220. * @param buf the starting address of the data to write into file
  1221. * @param sz how many bytes to write at most
  1222. * @return the byte we've written, or Linux specific error code
  1223. */
  1224. static int storeToFile(char *path, u8* buf, u32 sz)
  1225. {
  1226. int ret =0;
  1227. struct file *fp;
  1228. if(path && buf) {
  1229. if( 0 == (ret=openFile(&fp, path, O_CREAT|O_WRONLY, 0666)) ) {
  1230. DBG_871X("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
  1231. ret = kernel_write(fp, buf, sz, NULL);
  1232. closeFile(fp);
  1233. DBG_871X("%s writeFile, ret:%d\n",__FUNCTION__, ret);
  1234. } else {
  1235. DBG_871X("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
  1236. }
  1237. } else {
  1238. DBG_871X("%s NULL pointer\n",__FUNCTION__);
  1239. ret = -EINVAL;
  1240. }
  1241. return ret;
  1242. }
  1243. #endif //PLATFORM_LINUX
  1244. /*
  1245. * Test if the specifi @param path is a file and readable
  1246. * @param path the path of the file to test
  1247. * @return _TRUE or _FALSE
  1248. */
  1249. int rtw_is_file_readable(char *path)
  1250. {
  1251. #ifdef PLATFORM_LINUX
  1252. if(isFileReadable(path) == 0)
  1253. return _TRUE;
  1254. else
  1255. return _FALSE;
  1256. #else
  1257. //Todo...
  1258. return _FALSE;
  1259. #endif
  1260. }
  1261. /*
  1262. * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
  1263. * @param path the path of the file to open and read
  1264. * @param buf the starting address of the buffer to store file content
  1265. * @param sz how many bytes to read at most
  1266. * @return the byte we've read
  1267. */
  1268. int rtw_retrive_from_file(char *path, u8* buf, u32 sz)
  1269. {
  1270. #ifdef PLATFORM_LINUX
  1271. int ret =retriveFromFile(path, buf, sz);
  1272. return ret>=0?ret:0;
  1273. #else
  1274. //Todo...
  1275. return 0;
  1276. #endif
  1277. }
  1278. /*
  1279. * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
  1280. * @param path the path of the file to open and write
  1281. * @param buf the starting address of the data to write into file
  1282. * @param sz how many bytes to write at most
  1283. * @return the byte we've written
  1284. */
  1285. int rtw_store_to_file(char *path, u8* buf, u32 sz)
  1286. {
  1287. #ifdef PLATFORM_LINUX
  1288. int ret =storeToFile(path, buf, sz);
  1289. return ret>=0?ret:0;
  1290. #else
  1291. //Todo...
  1292. return 0;
  1293. #endif
  1294. }
  1295. #ifdef PLATFORM_LINUX
  1296. struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
  1297. {
  1298. struct net_device *pnetdev;
  1299. struct rtw_netdev_priv_indicator *pnpi;
  1300. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
  1301. pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
  1302. #else
  1303. pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
  1304. #endif
  1305. if (!pnetdev)
  1306. goto RETURN;
  1307. pnpi = netdev_priv(pnetdev);
  1308. pnpi->priv=old_priv;
  1309. pnpi->sizeof_priv=sizeof_priv;
  1310. RETURN:
  1311. return pnetdev;
  1312. }
  1313. struct net_device *rtw_alloc_etherdev(int sizeof_priv)
  1314. {
  1315. struct net_device *pnetdev;
  1316. struct rtw_netdev_priv_indicator *pnpi;
  1317. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
  1318. pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
  1319. #else
  1320. pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
  1321. #endif
  1322. if (!pnetdev)
  1323. goto RETURN;
  1324. pnpi = netdev_priv(pnetdev);
  1325. pnpi->priv = rtw_zvmalloc(sizeof_priv);
  1326. if (!pnpi->priv) {
  1327. free_netdev(pnetdev);
  1328. pnetdev = NULL;
  1329. goto RETURN;
  1330. }
  1331. pnpi->sizeof_priv=sizeof_priv;
  1332. RETURN:
  1333. return pnetdev;
  1334. }
  1335. void rtw_free_netdev(struct net_device * netdev)
  1336. {
  1337. struct rtw_netdev_priv_indicator *pnpi;
  1338. if(!netdev)
  1339. goto RETURN;
  1340. pnpi = netdev_priv(netdev);
  1341. if(!pnpi->priv)
  1342. goto RETURN;
  1343. rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
  1344. free_netdev(netdev);
  1345. RETURN:
  1346. return;
  1347. }
  1348. /*
  1349. * Jeff: this function should be called under ioctl (rtnl_lock is accquired) while
  1350. * LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
  1351. */
  1352. int rtw_change_ifname(_adapter *padapter, const char *ifname)
  1353. {
  1354. struct net_device *pnetdev;
  1355. struct net_device *cur_pnetdev = padapter->pnetdev;
  1356. struct rereg_nd_name_data *rereg_priv;
  1357. int ret;
  1358. if(!padapter)
  1359. goto error;
  1360. rereg_priv = &padapter->rereg_nd_name_priv;
  1361. //free the old_pnetdev
  1362. if(rereg_priv->old_pnetdev) {
  1363. free_netdev(rereg_priv->old_pnetdev);
  1364. rereg_priv->old_pnetdev = NULL;
  1365. }
  1366. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
  1367. if(!rtnl_is_locked())
  1368. unregister_netdev(cur_pnetdev);
  1369. else
  1370. #endif
  1371. unregister_netdevice(cur_pnetdev);
  1372. rtw_proc_remove_one(cur_pnetdev);
  1373. rereg_priv->old_pnetdev=cur_pnetdev;
  1374. pnetdev = rtw_init_netdev(padapter);
  1375. if (!pnetdev) {
  1376. ret = -1;
  1377. goto error;
  1378. }
  1379. SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
  1380. rtw_init_netdev_name(pnetdev, ifname);
  1381. _rtw_memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
  1382. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
  1383. if(!rtnl_is_locked())
  1384. ret = register_netdev(pnetdev);
  1385. else
  1386. #endif
  1387. ret = register_netdevice(pnetdev);
  1388. if ( ret != 0) {
  1389. RT_TRACE(_module_hci_intfs_c_,_drv_err_,("register_netdev() failed\n"));
  1390. goto error;
  1391. }
  1392. rtw_proc_init_one(pnetdev);
  1393. return 0;
  1394. error:
  1395. return -1;
  1396. }
  1397. #endif
  1398. #ifdef PLATFORM_FREEBSD
  1399. /*
  1400. * Copy a buffer from userspace and write into kernel address
  1401. * space.
  1402. *
  1403. * This emulation just calls the FreeBSD copyin function (to
  1404. * copy data from user space buffer into a kernel space buffer)
  1405. * and is designed to be used with the above io_write_wrapper.
  1406. *
  1407. * This function should return the number of bytes not copied.
  1408. * I.e. success results in a zero value.
  1409. * Negative error values are not returned.
  1410. */
  1411. unsigned long
  1412. copy_from_user(void *to, const void *from, unsigned long n)
  1413. {
  1414. if ( copyin(from, to, n) != 0 ) {
  1415. /* Any errors will be treated as a failure
  1416. to copy any of the requested bytes */
  1417. return n;
  1418. }
  1419. return 0;
  1420. }
  1421. unsigned long
  1422. copy_to_user(void *to, const void *from, unsigned long n)
  1423. {
  1424. if ( copyout(from, to, n) != 0 ) {
  1425. /* Any errors will be treated as a failure
  1426. to copy any of the requested bytes */
  1427. return n;
  1428. }
  1429. return 0;
  1430. }
  1431. /*
  1432. * The usb_register and usb_deregister functions are used to register
  1433. * usb drivers with the usb subsystem. In this compatibility layer
  1434. * emulation a list of drivers (struct usb_driver) is maintained
  1435. * and is used for probing/attaching etc.
  1436. *
  1437. * usb_register and usb_deregister simply call these functions.
  1438. */
  1439. int
  1440. usb_register(struct usb_driver *driver)
  1441. {
  1442. rtw_usb_linux_register(driver);
  1443. return 0;
  1444. }
  1445. int
  1446. usb_deregister(struct usb_driver *driver)
  1447. {
  1448. rtw_usb_linux_deregister(driver);
  1449. return 0;
  1450. }
  1451. void module_init_exit_wrapper(void *arg)
  1452. {
  1453. int (*func)(void) = arg;
  1454. func();
  1455. return;
  1456. }
  1457. #endif //PLATFORM_FREEBSD
  1458. #ifdef CONFIG_PLATFORM_SPRD
  1459. #ifdef do_div
  1460. #undef do_div
  1461. #endif
  1462. #include <asm-generic/div64.h>
  1463. #endif
  1464. u64 rtw_modular64(u64 x, u64 y)
  1465. {
  1466. #ifdef PLATFORM_LINUX
  1467. return do_div(x, y);
  1468. #elif defined(PLATFORM_WINDOWS)
  1469. return (x % y);
  1470. #elif defined(PLATFORM_FREEBSD)
  1471. return (x %y);
  1472. #endif
  1473. }
  1474. u64 rtw_division64(u64 x, u64 y)
  1475. {
  1476. #ifdef PLATFORM_LINUX
  1477. do_div(x, y);
  1478. return x;
  1479. #elif defined(PLATFORM_WINDOWS)
  1480. return (x / y);
  1481. #elif defined(PLATFORM_FREEBSD)
  1482. return (x / y);
  1483. #endif
  1484. }
  1485. void rtw_buf_free(u8 **buf, u32 *buf_len)
  1486. {
  1487. u32 ori_len;
  1488. if (!buf || !buf_len)
  1489. return;
  1490. ori_len = *buf_len;
  1491. if (*buf) {
  1492. *buf_len = 0;
  1493. _rtw_mfree(*buf, *buf_len);
  1494. *buf = NULL;
  1495. }
  1496. }
  1497. void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
  1498. {
  1499. u32 ori_len = 0, dup_len = 0;
  1500. u8 *ori = NULL;
  1501. u8 *dup = NULL;
  1502. if (!buf || !buf_len)
  1503. return;
  1504. if (!src || !src_len)
  1505. goto keep_ori;
  1506. /* duplicate src */
  1507. dup = rtw_malloc(src_len);
  1508. if (dup) {
  1509. dup_len = src_len;
  1510. _rtw_memcpy(dup, src, dup_len);
  1511. }
  1512. keep_ori:
  1513. ori = *buf;
  1514. ori_len = *buf_len;
  1515. /* replace buf with dup */
  1516. *buf_len = 0;
  1517. *buf = dup;
  1518. *buf_len = dup_len;
  1519. /* free ori */
  1520. if (ori && ori_len > 0)
  1521. _rtw_mfree(ori, ori_len);
  1522. }
  1523. /**
  1524. * rtw_cbuf_full - test if cbuf is full
  1525. * @cbuf: pointer of struct rtw_cbuf
  1526. *
  1527. * Returns: _TRUE if cbuf is full
  1528. */
  1529. inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
  1530. {
  1531. return (cbuf->write == cbuf->read-1)? _TRUE : _FALSE;
  1532. }
  1533. /**
  1534. * rtw_cbuf_empty - test if cbuf is empty
  1535. * @cbuf: pointer of struct rtw_cbuf
  1536. *
  1537. * Returns: _TRUE if cbuf is empty
  1538. */
  1539. inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
  1540. {
  1541. return (cbuf->write == cbuf->read)? _TRUE : _FALSE;
  1542. }
  1543. /**
  1544. * rtw_cbuf_push - push a pointer into cbuf
  1545. * @cbuf: pointer of struct rtw_cbuf
  1546. * @buf: pointer to push in
  1547. *
  1548. * Lock free operation, be careful of the use scheme
  1549. * Returns: _TRUE push success
  1550. */
  1551. bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
  1552. {
  1553. if (rtw_cbuf_full(cbuf))
  1554. return _FAIL;
  1555. if (0)
  1556. DBG_871X("%s on %u\n", __func__, cbuf->write);
  1557. cbuf->bufs[cbuf->write] = buf;
  1558. cbuf->write = (cbuf->write+1)%cbuf->size;
  1559. return _SUCCESS;
  1560. }
  1561. /**
  1562. * rtw_cbuf_pop - pop a pointer from cbuf
  1563. * @cbuf: pointer of struct rtw_cbuf
  1564. *
  1565. * Lock free operation, be careful of the use scheme
  1566. * Returns: pointer popped out
  1567. */
  1568. void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
  1569. {
  1570. void *buf;
  1571. if (rtw_cbuf_empty(cbuf))
  1572. return NULL;
  1573. if (0)
  1574. DBG_871X("%s on %u\n", __func__, cbuf->read);
  1575. buf = cbuf->bufs[cbuf->read];
  1576. cbuf->read = (cbuf->read+1)%cbuf->size;
  1577. return buf;
  1578. }
  1579. /**
  1580. * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
  1581. * @size: size of pointer
  1582. *
  1583. * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
  1584. */
  1585. struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
  1586. {
  1587. struct rtw_cbuf *cbuf;
  1588. cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void*)*size);
  1589. if (cbuf) {
  1590. cbuf->write = cbuf->read = 0;
  1591. cbuf->size = size;
  1592. }
  1593. return cbuf;
  1594. }
  1595. /**
  1596. * rtw_cbuf_free - free the given rtw_cbuf
  1597. * @cbuf: pointer of struct rtw_cbuf to free
  1598. */
  1599. void rtw_cbuf_free(struct rtw_cbuf *cbuf)
  1600. {
  1601. rtw_mfree((u8*)cbuf, sizeof(*cbuf) + sizeof(void*)*cbuf->size);
  1602. }