PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/tiwlan1251/pform/linux/src/osmemapi.c

http://github.com/CyanogenMod/cm-kernel
C | 483 lines | 234 code | 40 blank | 209 comment | 21 complexity | db8f612734ecb5a7ec4ea5bfde96c38e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /****************************************************************************
  2. **+-----------------------------------------------------------------------+**
  3. **| |**
  4. **| Copyright(c) 1998 - 2008 Texas Instruments. All rights reserved. |**
  5. **| All rights reserved. |**
  6. **| |**
  7. **| Redistribution and use in source and binary forms, with or without |**
  8. **| modification, are permitted provided that the following conditions |**
  9. **| are met: |**
  10. **| |**
  11. **| * Redistributions of source code must retain the above copyright |**
  12. **| notice, this list of conditions and the following disclaimer. |**
  13. **| * Redistributions in binary form must reproduce the above copyright |**
  14. **| notice, this list of conditions and the following disclaimer in |**
  15. **| the documentation and/or other materials provided with the |**
  16. **| distribution. |**
  17. **| * Neither the name Texas Instruments nor the names of its |**
  18. **| contributors may be used to endorse or promote products derived |**
  19. **| from this software without specific prior written permission. |**
  20. **| |**
  21. **| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |**
  22. **| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |**
  23. **| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |**
  24. **| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |**
  25. **| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |**
  26. **| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |**
  27. **| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |**
  28. **| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |**
  29. **| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |**
  30. **| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |**
  31. **| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |**
  32. **| |**
  33. **+-----------------------------------------------------------------------+**
  34. ****************************************************************************/
  35. #include "arch_ti.h"
  36. #include <linux/stddef.h>
  37. #include <linux/string.h>
  38. #include <linux/time.h>
  39. #include <linux/timer.h>
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/vmalloc.h>
  45. #include <linux/string.h>
  46. #include <linux/delay.h>
  47. #include <linux/time.h>
  48. #include <linux/list.h>
  49. #include "osApi.h"
  50. #include "osTIType.h"
  51. #include "esta_drv.h"
  52. typedef void (*os_free)(void *);
  53. struct os_mem_block
  54. {
  55. struct list_head blk_list;
  56. os_free f_free;
  57. __u32 size;
  58. __u32 signature;
  59. };
  60. #define MEM_BLOCK_START (('m'<<24) | ('e'<<16) | ('m'<<8) | 's')
  61. #define MEM_BLOCK_END (('m'<<24) | ('e'<<16) | ('m'<<8) | 'e')
  62. /****************************************************************************************
  63. * *
  64. * OS Memory API *
  65. * *
  66. ****************************************************************************************/
  67. /****************************************************************************************
  68. * os_memoryAlloc()
  69. ****************************************************************************************
  70. DESCRIPTION: Allocates resident (nonpaged) system-space memory.
  71. ARGUMENTS: OsContext - our adapter context.
  72. Size - Specifies the size, in bytes, to be allocated.
  73. RETURN: Pointer to the allocated memory.
  74. NULL if there is insufficient memory available.
  75. NOTES: With the call to vmalloc it is assumed that this function will
  76. never be called in an interrupt context. vmalloc has the potential to
  77. sleep the caller while waiting for memory to become available.
  78. *****************************************************************************************/
  79. PVOID
  80. os_memoryAlloc(
  81. TI_HANDLE OsContext,
  82. UINT32 Size
  83. )
  84. {
  85. struct os_mem_block *blk;
  86. __u32 total_size = Size + sizeof(struct os_mem_block) + sizeof(__u32);
  87. #ifdef TI_MEM_ALLOC_TRACE
  88. os_printf("MTT:%s:%d ::os_memoryAlloc(0x%p, %lu) : %lu\n",__FUNCTION__, __LINE__,OsContext,Size,total_size);
  89. #endif
  90. if( total_size < Size ) { /* Dm: Security fix */
  91. return NULL;
  92. }
  93. /*
  94. memory optimization issue. Allocate 8 kB and less from the SLAB allocator (2^n)
  95. otherwise allocate from virtual pool.
  96. */
  97. /* 2 pages */
  98. if (total_size < 2 * 4096)
  99. {
  100. if (in_atomic())
  101. blk = kmalloc(total_size, GFP_ATOMIC);
  102. else
  103. blk = kmalloc(total_size, GFP_KERNEL);
  104. if (!blk)
  105. return NULL;
  106. blk->f_free = (os_free)kfree;
  107. }
  108. else
  109. {
  110. /* We expect that the big allocations should be made outside the interrupt,
  111. otherwise fail
  112. */
  113. if (in_atomic())
  114. return NULL;
  115. blk = vmalloc(total_size);
  116. if (!blk)
  117. return NULL;
  118. blk->f_free = (os_free)vfree;
  119. }
  120. os_profile (OsContext, 4, total_size);
  121. /*list_add(&blk->blk_list, &drv->mem_blocks);*/
  122. blk->size = Size;
  123. blk->signature = MEM_BLOCK_START;
  124. *(__u32 *)((unsigned char *)blk + total_size - sizeof(__u32)) = MEM_BLOCK_END;
  125. return (PVOID)((char *)blk + sizeof(struct os_mem_block));
  126. }
  127. /****************************************************************************************
  128. * os_memoryPreFree()
  129. ****************************************************************************************
  130. DESCRIPTION: Frees preallocated by the kernel memory.
  131. ARGUMENTS: ptr - pointer to memory
  132. *****************************************************************************************/
  133. void os_memoryPreFree( void *ptr )
  134. {
  135. }
  136. /****************************************************************************************
  137. * os_memoryPreAlloc()
  138. ****************************************************************************************
  139. DESCRIPTION: Gets system-space memory preallocated by kernel.
  140. ARGUMENTS: OsContext - our adapter context.
  141. section - section number
  142. Size - Specifies the size, in bytes, to be allocated.
  143. RETURN: Pointer to the allocated memory.
  144. NULL if there is insufficient memory available.
  145. *****************************************************************************************/
  146. PVOID
  147. os_memoryPreAlloc(
  148. TI_HANDLE OsContext,
  149. int section,
  150. UINT32 Size
  151. )
  152. {
  153. struct os_mem_block *blk;
  154. __u32 total_size = Size + sizeof(struct os_mem_block) + sizeof(__u32);
  155. #ifdef TI_MEM_ALLOC_TRACE
  156. os_printf("MTT:%s:%d ::os_memoryPreAlloc(0x%p, %lu) : %lu\n",__FUNCTION__, __LINE__,OsContext,Size,total_size);
  157. #endif
  158. if( total_size < Size ) { /* Dm: Security fix */
  159. return NULL;
  160. }
  161. blk = (struct os_mem_block *)wifi_kernel_prealloc( section, total_size );
  162. if( !blk ) {
  163. return os_memoryAlloc(OsContext, Size);
  164. }
  165. blk->f_free = (os_free)os_memoryPreFree;
  166. os_profile (OsContext, 4, total_size);
  167. /*list_add(&blk->blk_list, &drv->mem_blocks);*/
  168. blk->size = Size;
  169. blk->signature = MEM_BLOCK_START;
  170. *(__u32 *)((unsigned char *)blk + total_size - sizeof(__u32)) = MEM_BLOCK_END;
  171. return (PVOID)((char *)blk + sizeof(struct os_mem_block));
  172. }
  173. /****************************************************************************************
  174. * os_memoryCAlloc()
  175. ****************************************************************************************
  176. DESCRIPTION: Allocates an array in memory with elements initialized to 0.
  177. ARGUMENTS: OsContext - our adapter context.
  178. Number - Number of elements
  179. Size - Length in bytes of each element
  180. RETURN: None
  181. NOTES:
  182. *****************************************************************************************/
  183. PVOID
  184. os_memoryCAlloc(
  185. TI_HANDLE OsContext,
  186. UINT32 Number,
  187. UINT32 Size
  188. )
  189. {
  190. PVOID pAllocatedMem;
  191. ULONG MemSize;
  192. #ifdef TI_MEM_ALLOC_TRACE
  193. os_printf("MTT:%s:%d ::os_memoryCAlloc(0x%p, %lu, %lu) : %lu\n",__FUNCTION__,__LINE__,OsContext,Number,Size,Number*Size);
  194. #endif
  195. MemSize = Number * Size;
  196. if( (Number > 0) && (Size >= (0xFFFFFFFFUL / Number)) ) { /* Dm: Security fix */
  197. return NULL;
  198. }
  199. pAllocatedMem = os_memoryAlloc(OsContext, MemSize);
  200. if(!pAllocatedMem)
  201. return NULL;
  202. memset(pAllocatedMem,0,MemSize);
  203. return pAllocatedMem;
  204. }
  205. /****************************************************************************************
  206. * os_memoryFree()
  207. ****************************************************************************************
  208. DESCRIPTION: This function releases a block of memory previously allocated with the
  209. os_memoryAlloc function.
  210. ARGUMENTS: OsContext - our adapter context.
  211. pMemPtr - Pointer to the base virtual address of the allocated memory.
  212. This address was returned by the os_memoryAlloc function.
  213. Size - Specifies the size, in bytes, of the memory block to be released.
  214. This parameter must be identical to the Length that was passed to
  215. os_memoryAlloc.
  216. RETURN: None
  217. NOTES:
  218. *****************************************************************************************/
  219. VOID
  220. os_memoryFree(
  221. TI_HANDLE OsContext,
  222. PVOID pMemPtr,
  223. UINT32 Size
  224. )
  225. {
  226. struct os_mem_block *blk =
  227. (struct os_mem_block *)((char *)pMemPtr - sizeof(struct os_mem_block));
  228. #ifdef TI_MEM_ALLOC_TRACE
  229. os_printf("MTT:%s:%d ::os_memoryFree(0x%p, 0x%p, %lu) : %d\n",__FUNCTION__,__LINE__,OsContext,pMemPtr,Size,-Size);
  230. #endif
  231. if (blk->signature != MEM_BLOCK_START)
  232. {
  233. printk("\n\n%s: memory block signature is incorrect - 0x%x\n\n\n",
  234. __FUNCTION__, blk->signature);
  235. return;
  236. }
  237. *(char *)(&blk->signature) = '~';
  238. if (*(__u32 *)((unsigned char *)blk + blk->size + sizeof(struct os_mem_block))
  239. != MEM_BLOCK_END)
  240. {
  241. printk("\n\n%s: memory block corruption. Size=%u\n\n\n",
  242. __FUNCTION__, blk->size);
  243. }
  244. os_profile (OsContext, 5, blk->size + sizeof(struct os_mem_block) + sizeof(__u32));
  245. blk->f_free(blk);
  246. }
  247. /****************************************************************************************
  248. * os_memorySet()
  249. ****************************************************************************************
  250. DESCRIPTION: This function fills a block of memory with given value.
  251. ARGUMENTS: OsContext - our adapter context.
  252. pMemPtr - Specifies the base address of a block of memory
  253. Value - Specifies the value to set
  254. Length - Specifies the size, in bytes, to copy.
  255. RETURN: None
  256. NOTES:
  257. *****************************************************************************************/
  258. VOID
  259. os_memorySet(
  260. TI_HANDLE OsContext,
  261. PVOID pMemPtr,
  262. INT32 Value,
  263. UINT32 Length
  264. )
  265. {
  266. memset(pMemPtr,Value,Length);
  267. }
  268. /****************************************************************************************
  269. * _os_memoryAlloc4HwDma()
  270. ****************************************************************************************
  271. DESCRIPTION: Allocates resident (nonpaged) system-space memory for DMA operations.
  272. ARGUMENTS: OsContext - our adapter context.
  273. Size - Specifies the size, in bytes, to be allocated.
  274. RETURN: Pointer to the allocated memory.
  275. NULL if there is insufficient memory available.
  276. NOTES:
  277. *****************************************************************************************/
  278. PVOID
  279. os_memoryAlloc4HwDma(
  280. TI_HANDLE pOsContext,
  281. UINT32 Size
  282. )
  283. {
  284. struct os_mem_block *blk;
  285. __u32 total_size = Size + sizeof(struct os_mem_block) + sizeof(__u32);
  286. /*
  287. if the size is greater than 2 pages then we cant allocate the memory through kmalloc so the function fails
  288. */
  289. if (Size < 2 * OS_PAGE_SIZE)
  290. {
  291. blk = kmalloc(total_size, GFP_ATOMIC);
  292. if (!blk)
  293. return NULL;
  294. blk->f_free = (os_free)kfree;
  295. }
  296. else
  297. {
  298. printk("\n\n%s: memory cant be allocated-Size = %d\n\n\n",
  299. __FUNCTION__, Size);
  300. return NULL;
  301. }
  302. blk->size = Size;
  303. blk->signature = MEM_BLOCK_START;
  304. *(__u32 *)((unsigned char *)blk + total_size - sizeof(__u32)) = MEM_BLOCK_END;
  305. return (PVOID)((char *)blk + sizeof(struct os_mem_block));
  306. }
  307. /****************************************************************************************
  308. * _os_memory4HwDmaFree()
  309. ****************************************************************************************
  310. DESCRIPTION: This function releases a block of memory previously allocated with the
  311. _os_memoryAlloc4HwDma function.
  312. ARGUMENTS: OsContext - our adapter context.
  313. pMemPtr - Pointer to the base virtual address of the allocated memory.
  314. This address was returned by the os_memoryAlloc function.
  315. Size - Specifies the size, in bytes, of the memory block to be released.
  316. This parameter must be identical to the Length that was passed to
  317. os_memoryAlloc.
  318. RETURN: None
  319. NOTES:
  320. *****************************************************************************************/
  321. void
  322. os_memory4HwDmaFree(
  323. TI_HANDLE pOsContext,
  324. PVOID pMem_ptr,
  325. UINT32 Size
  326. )
  327. {
  328. struct os_mem_block *blk =
  329. (struct os_mem_block *)((char *)pMem_ptr - sizeof(struct os_mem_block));
  330. if (blk->signature != MEM_BLOCK_START)
  331. {
  332. printk("\n\n%s: memory block signature is incorrect - 0x%x\n\n\n",
  333. __FUNCTION__, blk->signature);
  334. return;
  335. }
  336. *(char *)(&blk->signature) = '~';
  337. if (*(__u32 *)((unsigned char *)blk + blk->size + sizeof(struct os_mem_block))
  338. != MEM_BLOCK_END)
  339. {
  340. printk("\n\n%s: memory block corruption. Size=%u\n\n\n",
  341. __FUNCTION__, blk->size);
  342. }
  343. blk->f_free(blk);
  344. }
  345. /****************************************************************************************
  346. * os_memoryZero()
  347. ****************************************************************************************
  348. DESCRIPTION: This function fills a block of memory with 0s.
  349. ARGUMENTS: OsContext - our adapter context.
  350. pMemPtr - Specifies the base address of a block of memory
  351. Length - Specifies how many bytes to fill with 0s.
  352. RETURN: None
  353. NOTES:
  354. *****************************************************************************************/
  355. VOID
  356. os_memoryZero(
  357. TI_HANDLE OsContext,
  358. PVOID pMemPtr,
  359. UINT32 Length
  360. )
  361. {
  362. memset(pMemPtr,0,Length);
  363. }
  364. /****************************************************************************************
  365. * os_memoryCopy()
  366. ****************************************************************************************
  367. DESCRIPTION: This function copies a specified number of bytes from one caller-supplied
  368. location to another.
  369. ARGUMENTS: OsContext - our adapter context.
  370. pDstPtr - Destination buffer
  371. pSrcPtr - Source buffer
  372. Size - Specifies the size, in bytes, to copy.
  373. RETURN: None
  374. NOTES:
  375. *****************************************************************************************/
  376. VOID
  377. os_memoryCopy(
  378. TI_HANDLE OsContext,
  379. PVOID pDstPtr,
  380. PVOID pSrcPtr,
  381. UINT32 Size
  382. )
  383. {
  384. memcpy(pDstPtr,pSrcPtr,Size);
  385. }
  386. /****************************************************************************************
  387. * os_memoryCompare()
  388. ****************************************************************************************
  389. DESCRIPTION: Compare characters in two buffers.
  390. ARGUMENTS: OsContext - our adapter context.
  391. Buf1 - First buffer
  392. Buf2 - Second buffer
  393. Count - Number of characters
  394. RETURN: The return value indicates the relationship between the buffers:
  395. < 0 Buf1 less than Buf2
  396. 0 Buf1 identical to Buf2
  397. > 0 Buf1 greater than Buf2
  398. NOTES:
  399. *****************************************************************************************/
  400. INT32
  401. os_memoryCompare(
  402. TI_HANDLE OsContext,
  403. PUINT8 Buf1,
  404. PUINT8 Buf2,
  405. INT32 Count
  406. )
  407. {
  408. return memcmp(Buf1, Buf2, Count);
  409. }