Staging: add w35und wifi driver
[linux-2.6/mini2440.git] / drivers / staging / winbond / linux / wb35tx.c
blobcf19c3bc524a7b72c7d6e1c2cec4c837b8ed463c
1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
3 //
4 // Module Name:
5 // Wb35Tx.c
6 //
7 // Abstract:
8 // Processing the Tx message and put into down layer
9 //
10 //============================================================================
11 #include "sysdef.h"
14 unsigned char
15 Wb35Tx_get_tx_buffer(phw_data_t pHwData, PUCHAR *pBuffer )
17 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
19 *pBuffer = pWb35Tx->TxBuffer[0];
20 return TRUE;
23 void Wb35Tx_start(phw_data_t pHwData)
25 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
27 // Allow only one thread to run into function
28 if (OS_ATOMIC_INC(pHwData->Adapter, &pWb35Tx->TxFireCounter) == 1) {
29 pWb35Tx->EP4vm_state = VM_RUNNING;
30 Wb35Tx(pHwData);
31 } else
32 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxFireCounter );
36 void Wb35Tx(phw_data_t pHwData)
38 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
39 PADAPTER Adapter = pHwData->Adapter;
40 PUCHAR pTxBufferAddress;
41 PMDS pMds = &Adapter->Mds;
42 struct urb * pUrb = (struct urb *)pWb35Tx->Tx4Urb;
43 int retv;
44 u32 SendIndex;
47 if (pHwData->SurpriseRemove || pHwData->HwStop)
48 goto cleanup;
50 if (pWb35Tx->tx_halt)
51 goto cleanup;
53 // Ownership checking
54 SendIndex = pWb35Tx->TxSendIndex;
55 if (!pMds->TxOwner[SendIndex]) //No more data need to be sent, return immediately
56 goto cleanup;
58 pTxBufferAddress = pWb35Tx->TxBuffer[SendIndex];
60 // Issuing URB
62 usb_fill_bulk_urb(pUrb, pHwData->WbUsb.udev,
63 usb_sndbulkpipe(pHwData->WbUsb.udev, 4),
64 pTxBufferAddress, pMds->TxBufferSize[ SendIndex ],
65 Wb35Tx_complete, pHwData);
67 pWb35Tx->EP4vm_state = VM_RUNNING;
68 retv = wb_usb_submit_urb( pUrb );
69 if (retv<0) {
70 printk("EP4 Tx Irp sending error\n");
71 goto cleanup;
74 // Check if driver needs issue Irp for EP2
75 pWb35Tx->TxFillCount += pMds->TxCountInBuffer[SendIndex];
76 if (pWb35Tx->TxFillCount > 12)
77 Wb35Tx_EP2VM_start( pHwData );
79 pWb35Tx->ByteTransfer += pMds->TxBufferSize[SendIndex];
80 return;
82 cleanup:
83 pWb35Tx->EP4vm_state = VM_STOP;
84 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxFireCounter );
88 void Wb35Tx_complete(struct urb * pUrb)
90 phw_data_t pHwData = pUrb->context;
91 PADAPTER Adapter = (PADAPTER)pHwData->Adapter;
92 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
93 PMDS pMds = &Adapter->Mds;
95 printk("wb35: tx complete\n");
96 // Variable setting
97 pWb35Tx->EP4vm_state = VM_COMPLETED;
98 pWb35Tx->EP4VM_status = pUrb->status; //Store the last result of Irp
99 pMds->TxOwner[ pWb35Tx->TxSendIndex ] = 0;// Set the owner. Free the owner bit always.
100 pWb35Tx->TxSendIndex++;
101 pWb35Tx->TxSendIndex %= MAX_USB_TX_BUFFER_NUMBER;
103 do {
104 if (pHwData->SurpriseRemove || pHwData->HwStop) // Let WbWlanHalt to handle surprise remove
105 break;
107 if (pWb35Tx->tx_halt)
108 break;
110 // The URB is completed, check the result
111 if (pWb35Tx->EP4VM_status != 0) {
112 printk("URB submission failed\n");
113 pWb35Tx->EP4vm_state = VM_STOP;
114 break; // Exit while(FALSE);
117 Mds_Tx(Adapter);
118 Wb35Tx(pHwData);
119 return;
120 } while(FALSE);
122 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxFireCounter );
123 pWb35Tx->EP4vm_state = VM_STOP;
126 void Wb35Tx_reset_descriptor( phw_data_t pHwData )
128 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
130 pWb35Tx->TxSendIndex = 0;
131 pWb35Tx->tx_halt = 0;
134 unsigned char Wb35Tx_initial(phw_data_t pHwData)
136 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
138 pWb35Tx->Tx4Urb = wb_usb_alloc_urb(0);
139 if (!pWb35Tx->Tx4Urb)
140 return FALSE;
142 pWb35Tx->Tx2Urb = wb_usb_alloc_urb(0);
143 if (!pWb35Tx->Tx2Urb)
145 usb_free_urb( pWb35Tx->Tx4Urb );
146 return FALSE;
149 return TRUE;
152 //======================================================
153 void Wb35Tx_stop(phw_data_t pHwData)
155 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
157 // Trying to canceling the Trp of EP2
158 if (pWb35Tx->EP2vm_state == VM_RUNNING)
159 usb_unlink_urb( pWb35Tx->Tx2Urb ); // Only use unlink, let Wb35Tx_destrot to free them
160 #ifdef _PE_TX_DUMP_
161 WBDEBUG(("EP2 Tx stop\n"));
162 #endif
164 // Trying to canceling the Irp of EP4
165 if (pWb35Tx->EP4vm_state == VM_RUNNING)
166 usb_unlink_urb( pWb35Tx->Tx4Urb ); // Only use unlink, let Wb35Tx_destrot to free them
167 #ifdef _PE_TX_DUMP_
168 WBDEBUG(("EP4 Tx stop\n"));
169 #endif
172 //======================================================
173 void Wb35Tx_destroy(phw_data_t pHwData)
175 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
177 // Wait for VM stop
178 do {
179 OS_SLEEP(10000); // Delay for waiting function enter 940623.1.a
180 } while( (pWb35Tx->EP2vm_state != VM_STOP) && (pWb35Tx->EP4vm_state != VM_STOP) );
181 OS_SLEEP(10000); // Delay for waiting function enter 940623.1.b
183 if (pWb35Tx->Tx4Urb)
184 usb_free_urb( pWb35Tx->Tx4Urb );
186 if (pWb35Tx->Tx2Urb)
187 usb_free_urb( pWb35Tx->Tx2Urb );
189 #ifdef _PE_TX_DUMP_
190 WBDEBUG(("Wb35Tx_destroy OK\n"));
191 #endif
194 void Wb35Tx_CurrentTime(phw_data_t pHwData, u32 TimeCount)
196 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
197 unsigned char Trigger = FALSE;
199 if (pWb35Tx->TxTimer > TimeCount)
200 Trigger = TRUE;
201 else if (TimeCount > (pWb35Tx->TxTimer+500))
202 Trigger = TRUE;
204 if (Trigger) {
205 pWb35Tx->TxTimer = TimeCount;
206 Wb35Tx_EP2VM_start( pHwData );
210 void Wb35Tx_EP2VM_start(phw_data_t pHwData)
212 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
214 // Allow only one thread to run into function
215 if (OS_ATOMIC_INC( pHwData->Adapter, &pWb35Tx->TxResultCount ) == 1) {
216 pWb35Tx->EP2vm_state = VM_RUNNING;
217 Wb35Tx_EP2VM( pHwData );
219 else
220 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxResultCount );
224 void Wb35Tx_EP2VM(phw_data_t pHwData)
226 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
227 struct urb * pUrb = (struct urb *)pWb35Tx->Tx2Urb;
228 PULONG pltmp = (PULONG)pWb35Tx->EP2_buf;
229 int retv;
231 do {
232 if (pHwData->SurpriseRemove || pHwData->HwStop)
233 break;
235 if (pWb35Tx->tx_halt)
236 break;
239 // Issuing URB
241 usb_fill_int_urb( pUrb, pHwData->WbUsb.udev, usb_rcvintpipe(pHwData->WbUsb.udev,2),
242 pltmp, MAX_INTERRUPT_LENGTH, Wb35Tx_EP2VM_complete, pHwData, 32);
244 pWb35Tx->EP2vm_state = VM_RUNNING;
245 retv = wb_usb_submit_urb( pUrb );
247 if(retv < 0) {
248 #ifdef _PE_TX_DUMP_
249 WBDEBUG(("EP2 Tx Irp sending error\n"));
250 #endif
251 break;
254 return;
256 } while(FALSE);
258 pWb35Tx->EP2vm_state = VM_STOP;
259 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxResultCount );
263 void Wb35Tx_EP2VM_complete(struct urb * pUrb)
265 phw_data_t pHwData = pUrb->context;
266 T02_DESCRIPTOR T02, TSTATUS;
267 PADAPTER Adapter = (PADAPTER)pHwData->Adapter;
268 PWB35TX pWb35Tx = &pHwData->Wb35Tx;
269 PULONG pltmp = (PULONG)pWb35Tx->EP2_buf;
270 u32 i;
271 u16 InterruptInLength;
274 // Variable setting
275 pWb35Tx->EP2vm_state = VM_COMPLETED;
276 pWb35Tx->EP2VM_status = pUrb->status;
278 do {
279 // For Linux 2.4. Interrupt will always trigger
280 if( pHwData->SurpriseRemove || pHwData->HwStop ) // Let WbWlanHalt to handle surprise remove
281 break;
283 if( pWb35Tx->tx_halt )
284 break;
286 //The Urb is completed, check the result
287 if (pWb35Tx->EP2VM_status != 0) {
288 WBDEBUG(("EP2 IoCompleteRoutine return error\n"));
289 pWb35Tx->EP2vm_state= VM_STOP;
290 break; // Exit while(FALSE);
293 // Update the Tx result
294 InterruptInLength = pUrb->actual_length;
295 // Modify for minimum memory access and DWORD alignment.
296 T02.value = cpu_to_le32(pltmp[0]) >> 8; // [31:8] -> [24:0]
297 InterruptInLength -= 1;// 20051221.1.c Modify the follow for more stable
298 InterruptInLength >>= 2; // InterruptInLength/4
299 for (i=1; i<=InterruptInLength; i++) {
300 T02.value |= ((cpu_to_le32(pltmp[i]) & 0xff) << 24);
302 TSTATUS.value = T02.value; //20061009 anson's endian
303 Mds_SendComplete( Adapter, &TSTATUS );
304 T02.value = cpu_to_le32(pltmp[i]) >> 8;
307 return;
308 } while(FALSE);
310 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Tx->TxResultCount );
311 pWb35Tx->EP2vm_state = VM_STOP;