Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / magpie_1_1 / sboot / vdesc / src / vdesc.c
1 /*
2  * @File: vdesc.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 <vdesc_api.h>
16 #include <Magpie_api.h>
17
18 #include "vdesc.h"
19
20 #define VDESC_SIZE              sizeof(VDESC)
21 //#define VDESC_TX_SIZE           sizeof(VDESC_TX)
22 //#define VDESC_RX_SIZE           sizeof(VDESC_RX)
23
24 static VDESC* alloc_rx_desc(void);
25 static VDESC* alloc_tx_desc(void);
26 void _vdesc_init(int nDesc);
27 A_UINT8* _vdesc_get_hw_desc(VDESC *desc);
28 VDESC* _vdesc_alloc_desc();
29 void _vdesc_swap_vdesc(VDESC *dest, VDESC *src);
30
31 struct VDESC_CONTEXT g_vdescCtx;
32
33 void _vdesc_init(int nDesc) 
34 {
35     int i;
36     VDESC *vdesc;  
37     //int nextAddr = dataAddr;
38     //int nDesc = nTxDesc + nRxDesc;
39     
40     // Initialize VDESC_TX for nTxDesc number
41     //vdesc = (VDESC*)dataAddr;
42     vdesc = (VDESC *)A_ALLOCRAM(sizeof(VDESC));
43     vdesc->next_desc = NULL;
44     vdesc->control = 0;
45
46 #if 1
47     g_vdescCtx.free_vdesc_head = vdesc;  
48     
49     for(i=1; i<nDesc; i++)
50     {
51         //nextAddr = dataAddr + i*VDESC_SIZE;
52         //vdesc = (VDESC*)nextAddr;
53         vdesc = (VDESC *)A_ALLOCRAM(sizeof(VDESC));
54         
55         vdesc->control = 0;
56         
57         vdesc->next_desc = g_vdescCtx.free_vdesc_head;
58         g_vdescCtx.free_vdesc_head = vdesc;
59     }    
60     
61     //nextAddr = nextAddr + VDESC_SIZE;
62     //return nextAddr;
63     return;
64 #else                    
65     g_vdescCtx.free_vdesc_tx_head = vdesc;    
66     for(i=1; i<nTxDesc; i++)
67     {
68         nextAddr = dataAddr + i*VDESC_TX_SIZE;
69         vdesc = (VDESC*)nextAddr;
70         vdesc->control = 0;
71         
72         vdesc->next_desc = g_vdescCtx.free_vdesc_tx_head;
73         g_vdescCtx.free_vdesc_tx_head = vdesc;
74     }    
75     
76     nextAddr = nextAddr + VDESC_TX_SIZE;
77     
78     // Initialize VDESC_RX for nTxDesc number
79     vdesc = (VDESC*)nextAddr;
80     vdesc->next_desc = NULL;
81     vdesc->control = 0;
82     
83     g_vdescCtx.free_vdesc_rx_head = vdesc;  
84     for(i=1; i<nRxDesc; i++)
85     {
86         //nextAddr = nextAddr + i*VDESC_RX_SIZE;
87         vdesc = (VDESC*)(nextAddr + i*VDESC_RX_SIZE);
88         vdesc->control = 0;
89         
90         vdesc->next_desc = g_vdescCtx.free_vdesc_rx_head;
91         g_vdescCtx.free_vdesc_rx_head = vdesc;
92     }  
93
94     return (nextAddr + nRxDesc*VDESC_RX_SIZE);   
95 #endif                
96 }
97
98 #if 0
99 static VDESC* alloc_tx_desc(void)
100 {
101     VDESC *allocDesc = NULL;
102     
103     if ( g_vdescCtx.free_vdesc_tx_head != NULL )
104     {
105         allocDesc = g_vdescCtx.free_vdesc_tx_head;
106         
107         //g_vbufCtx.nVbufNum--;
108         
109         g_vdescCtx.free_vdesc_tx_head = allocDesc->next_desc;
110         allocDesc->next_desc = NULL;        
111     }    
112     
113     return allocDesc;
114 }
115
116 static VDESC* alloc_rx_desc(void)
117 {
118     VDESC *allocDesc = NULL;
119     
120     if ( g_vdescCtx.free_vdesc_rx_head != NULL )
121     {
122         allocDesc = g_vdescCtx.free_vdesc_rx_head;
123         //g_vbufCtx.nVbufNum--;
124         
125         g_vdescCtx.free_vdesc_rx_head = allocDesc->next_desc;
126         allocDesc->next_desc = NULL;        
127     }    
128     
129     return allocDesc;    
130 }
131 #endif
132
133 VDESC* _vdesc_alloc_desc()
134 {
135 #if 1
136     VDESC *allocDesc = NULL;
137     
138     if ( g_vdescCtx.free_vdesc_head != NULL )
139     {
140         allocDesc = g_vdescCtx.free_vdesc_head;
141         //g_vbufCtx.nVbufNum--;
142         
143         g_vdescCtx.free_vdesc_head = allocDesc->next_desc;
144         allocDesc->next_desc = NULL;        
145     }    
146     
147     return allocDesc;    
148 #else    
149     if ( type == VDESC_TYPE_RX )
150     {
151         return alloc_rx_desc();
152     }
153     else
154     {
155         return alloc_tx_desc();
156     }
157 #endif    
158 }
159
160 A_UINT8* _vdesc_get_hw_desc(VDESC *desc)
161 {
162 #if 1
163     return desc->hw_desc_buf;
164 #else    
165     if ( type == VDESC_TYPE_RX )
166     {
167         return ((VDESC_RX*)desc)->hw_desc_buf;
168     }
169     else
170     {
171         return ((VDESC_TX*)desc)->hw_desc_buf;
172     }
173 #endif    
174
175
176 void _vdesc_swap_vdesc(VDESC *dest, VDESC *src)
177 {
178     A_UINT32 tmp;
179     A_UINT8 *tmpAddr;
180         
181     tmp = dest->buf_size;
182     dest->buf_size = src->buf_size;
183     src->buf_size = tmp;
184     
185     tmp = dest->data_offset;       
186     dest->data_offset = src->data_offset;
187     src->data_offset = tmp;
188     
189     tmp = dest->data_size;       
190     dest->data_size = src->data_size;
191     src->data_size = tmp;
192         
193     tmp = dest->control;
194     dest->control = src->control;
195     src->control = tmp;
196     
197     tmpAddr = dest->buf_addr;
198     dest->buf_addr = src->buf_addr;
199     src->buf_addr = tmpAddr;     
200 }
201
202 /* the exported entry point into this module. All apis are accessed through
203  * function pointers */
204 void vdesc_module_install(struct vdesc_api *apis)
205 {    
206         /* hook in APIs */
207     apis->_init = _vdesc_init;
208     apis->_alloc_vdesc = _vdesc_alloc_desc;
209     apis->_get_hw_desc = _vdesc_get_hw_desc;
210     apis->_swap_vdesc  = _vdesc_swap_vdesc;
211     
212     //apis->_free_vbuf = _vbuf_free_vbuf;
213     
214         /* save ptr to the ptr to the context for external code to inspect/modify internal module state */
215     //apis->pReserved = &g_pMboxHWContext;
216 }
217  
218