Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / magpie_1_1 / sboot / vbuf / src / vbuf.c
1 /*
2  * @File: vbuf.c
3  * 
4  * @Abstract: 
5  * 
6  * @Notes:
7  * 
8  * Copyright (c) 2008 Atheros Communications Inc.
9  * All rights reserved.
10  *
11  */
12
13
14 #include <osapi.h>
15 #include <vbuf_api.h>
16 #include <Magpie_api.h>
17
18 #include "vbuf.h"
19
20 #define VBUF_SIZE           sizeof(VBUF)
21
22 struct VBUF_CONTEXT g_vbufCtx;
23
24 void _vbuf_init(int nBuf);
25 VBUF* _vbuf_alloc_vbuf(void);
26 void _vbuf_free_vbuf(VBUF *buf);
27
28 void _vbuf_init(int nBuf) 
29 {
30     int i;
31     VBUF *vbuf;
32     
33     //vbuf = (VBUF*)dataAddr;
34     vbuf = (VBUF*)A_ALLOCRAM(VBUF_SIZE);
35     vbuf->next_buf = NULL;
36     vbuf->desc_list = NULL;
37                     
38     g_vbufCtx.free_buf_head = vbuf;
39     
40     for(i=1; i<nBuf; i++)
41     {
42         //vbuf = (VBUF*)(dataAddr + i*VBUF_SIZE);
43         vbuf = (VBUF*)A_ALLOCRAM(VBUF_SIZE);
44         
45         vbuf->desc_list = NULL;
46         vbuf->next_buf = g_vbufCtx.free_buf_head;
47         g_vbufCtx.free_buf_head = vbuf;
48     }    
49     
50     g_vbufCtx.nVbufNum = nBuf;
51     //return (dataAddr + nBuf*VBUF_SIZE);
52     return;
53 }
54
55 VBUF* _vbuf_alloc_vbuf(void)
56 {
57     VBUF *allocBuf = NULL;
58     
59     if ( g_vbufCtx.free_buf_head != NULL )
60     {
61         allocBuf = g_vbufCtx.free_buf_head;
62         g_vbufCtx.nVbufNum--;
63         
64         g_vbufCtx.free_buf_head = allocBuf->next_buf;
65         allocBuf->next_buf = NULL;        
66     }
67     
68     return allocBuf;
69 }
70
71 void _vbuf_free_vbuf(VBUF *buf)
72 {
73     // assert buf != NULL
74     
75     buf->next_buf = g_vbufCtx.free_buf_head;
76     g_vbufCtx.free_buf_head = buf;
77     
78     g_vbufCtx.nVbufNum++;
79 }
80
81 /* the exported entry point into this module. All apis are accessed through
82  * function pointers */
83 void vbuf_module_install(struct vbuf_api *apis)
84 {    
85         /* hook in APIs */
86     apis->_init = _vbuf_init;
87     apis->_alloc_vbuf = _vbuf_alloc_vbuf;
88     apis->_free_vbuf = _vbuf_free_vbuf;
89     
90         /* save ptr to the ptr to the context for external code to inspect/modify internal module state */
91     //apis->pReserved = &g_pMboxHWContext;
92 }
93  
94