Staging: add w35und wifi driver
[linux-2.6/mini2440.git] / drivers / staging / winbond / linux / wb35rx.c
blob26157eb3d5a29fa49860967ccee36fd081c4bf7a
1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
3 //
4 // Module Name:
5 // Wb35Rx.c
6 //
7 // Abstract:
8 // Processing the Rx message from down layer
9 //
10 //============================================================================
11 #include "sysdef.h"
14 void Wb35Rx_start(phw_data_t pHwData)
16 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
18 // Allow only one thread to run into the Wb35Rx() function
19 if (OS_ATOMIC_INC(pHwData->Adapter, &pWb35Rx->RxFireCounter) == 1) {
20 pWb35Rx->EP3vm_state = VM_RUNNING;
21 Wb35Rx(pHwData);
22 } else
23 OS_ATOMIC_DEC(pHwData->Adapter, &pWb35Rx->RxFireCounter);
26 // This function cannot reentrain
27 void Wb35Rx( phw_data_t pHwData )
29 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
30 PUCHAR pRxBufferAddress;
31 PURB pUrb = (PURB)pWb35Rx->RxUrb;
32 int retv;
33 u32 RxBufferId;
36 // Issuing URB
38 do {
39 if (pHwData->SurpriseRemove || pHwData->HwStop)
40 break;
42 if (pWb35Rx->rx_halt)
43 break;
45 // Get RxBuffer's ID
46 RxBufferId = pWb35Rx->RxBufferId;
47 if (!pWb35Rx->RxOwner[RxBufferId]) {
48 // It's impossible to run here.
49 #ifdef _PE_RX_DUMP_
50 WBDEBUG(("Rx driver fifo unavailable\n"));
51 #endif
52 break;
55 // Update buffer point, then start to bulkin the data from USB
56 pWb35Rx->RxBufferId++;
57 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
59 pWb35Rx->CurrentRxBufferId = RxBufferId;
61 if (1 != OS_MEMORY_ALLOC((void* *)&pWb35Rx->pDRx, MAX_USB_RX_BUFFER)) {
62 printk("w35und: Rx memory alloc failed\n");
63 break;
65 pRxBufferAddress = pWb35Rx->pDRx;
67 usb_fill_bulk_urb(pUrb, pHwData->WbUsb.udev,
68 usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
69 pRxBufferAddress, MAX_USB_RX_BUFFER,
70 Wb35Rx_Complete, pHwData);
72 pWb35Rx->EP3vm_state = VM_RUNNING;
74 retv = wb_usb_submit_urb(pUrb);
76 if (retv != 0) {
77 printk("Rx URB sending error\n");
78 break;
80 return;
81 } while(FALSE);
83 // VM stop
84 pWb35Rx->EP3vm_state = VM_STOP;
85 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
88 void Wb35Rx_Complete(PURB pUrb)
90 phw_data_t pHwData = pUrb->context;
91 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
92 PUCHAR pRxBufferAddress;
93 u32 SizeCheck;
94 u16 BulkLength;
95 u32 RxBufferId;
96 R00_DESCRIPTOR R00;
98 // Variable setting
99 pWb35Rx->EP3vm_state = VM_COMPLETED;
100 pWb35Rx->EP3VM_status = pUrb->status;//Store the last result of Irp
102 do {
103 RxBufferId = pWb35Rx->CurrentRxBufferId;
105 pRxBufferAddress = pWb35Rx->pDRx;
106 BulkLength = (u16)pUrb->actual_length;
108 // The IRP is completed
109 pWb35Rx->EP3vm_state = VM_COMPLETED;
111 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
112 break;
114 if (pWb35Rx->rx_halt)
115 break;
117 // Start to process the data only in successful condition
118 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
119 R00.value = le32_to_cpu(*(PULONG)pRxBufferAddress);
121 // The URB is completed, check the result
122 if (pWb35Rx->EP3VM_status != 0) {
123 #ifdef _PE_USB_STATE_DUMP_
124 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
125 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
126 #endif
127 pWb35Rx->EP3vm_state = VM_STOP;
128 break;
131 // 20060220 For recovering. check if operating in single USB mode
132 if (!HAL_USB_MODE_BURST(pHwData)) {
133 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
134 if ((SizeCheck & 0x03) > 0)
135 SizeCheck -= 4;
136 SizeCheck = (SizeCheck + 3) & ~0x03;
137 SizeCheck += 12; // 8 + 4 badbeef
138 if ((BulkLength > 1600) ||
139 (SizeCheck > 1600) ||
140 (BulkLength != SizeCheck) ||
141 (BulkLength == 0)) { // Add for fail Urb
142 pWb35Rx->EP3vm_state = VM_STOP;
143 pWb35Rx->Ep3ErrorCount2++;
147 // Indicating the receiving data
148 pWb35Rx->ByteReceived += BulkLength;
149 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
151 if (!pWb35Rx->RxOwner[ RxBufferId ])
152 Wb35Rx_indicate(pHwData);
154 kfree(pWb35Rx->pDRx);
155 // Do the next receive
156 Wb35Rx(pHwData);
157 return;
159 } while(FALSE);
161 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
162 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
163 pWb35Rx->EP3vm_state = VM_STOP;
166 //=====================================================================================
167 unsigned char Wb35Rx_initial(phw_data_t pHwData)
169 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
171 // Initial the Buffer Queue
172 Wb35Rx_reset_descriptor( pHwData );
174 pWb35Rx->RxUrb = wb_usb_alloc_urb(0);
175 return (!!pWb35Rx->RxUrb);
178 void Wb35Rx_stop(phw_data_t pHwData)
180 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
182 // Canceling the Irp if already sends it out.
183 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
184 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
185 #ifdef _PE_RX_DUMP_
186 WBDEBUG(("EP3 Rx stop\n"));
187 #endif
191 // Needs process context
192 void Wb35Rx_destroy(phw_data_t pHwData)
194 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
196 do {
197 OS_SLEEP(10000); // Delay for waiting function enter 940623.1.a
198 } while (pWb35Rx->EP3vm_state != VM_STOP);
199 OS_SLEEP(10000); // Delay for waiting function exit 940623.1.b
201 if (pWb35Rx->RxUrb)
202 usb_free_urb( pWb35Rx->RxUrb );
203 #ifdef _PE_RX_DUMP_
204 WBDEBUG(("Wb35Rx_destroy OK\n"));
205 #endif
208 void Wb35Rx_reset_descriptor( phw_data_t pHwData )
210 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
211 u32 i;
213 pWb35Rx->ByteReceived = 0;
214 pWb35Rx->RxProcessIndex = 0;
215 pWb35Rx->RxBufferId = 0;
216 pWb35Rx->EP3vm_state = VM_STOP;
217 pWb35Rx->rx_halt = 0;
219 // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
220 for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
221 pWb35Rx->RxOwner[i] = 1;
224 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
226 PULONG pRxBufferAddress;
227 u32 DecryptionMethod;
228 u32 i;
229 u16 BufferSize;
231 DecryptionMethod = pRxDes->R01.R01_decryption_method;
232 pRxBufferAddress = pRxDes->buffer_address[0];
233 BufferSize = pRxDes->buffer_size[0];
235 // Adjust the last part of data. Only data left
236 BufferSize -= 4; // For CRC-32
237 if (DecryptionMethod)
238 BufferSize -= 4;
239 if (DecryptionMethod == 3) // For CCMP
240 BufferSize -= 4;
242 // Adjust the IV field which after 802.11 header and ICV field.
243 if (DecryptionMethod == 1) // For WEP
245 for( i=6; i>0; i-- )
246 pRxBufferAddress[i] = pRxBufferAddress[i-1];
247 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
248 BufferSize -= 4; // 4 byte for IV
250 else if( DecryptionMethod ) // For TKIP and CCMP
252 for (i=7; i>1; i--)
253 pRxBufferAddress[i] = pRxBufferAddress[i-2];
254 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
255 BufferSize -= 8; // 8 byte for IV + ICV
257 pRxDes->buffer_size[0] = BufferSize;
260 extern void packet_came(char *pRxBufferAddress, int PacketSize);
263 u16 Wb35Rx_indicate(phw_data_t pHwData)
265 DESCRIPTOR RxDes;
266 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
267 PUCHAR pRxBufferAddress;
268 u16 PacketSize;
269 u16 stmp, BufferSize, stmp2 = 0;
270 u32 RxBufferId;
272 // Only one thread be allowed to run into the following
273 do {
274 RxBufferId = pWb35Rx->RxProcessIndex;
275 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
276 break;
278 pWb35Rx->RxProcessIndex++;
279 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
281 pRxBufferAddress = pWb35Rx->pDRx;
282 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
284 // Parse the bulkin buffer
285 while (BufferSize >= 4) {
286 if ((cpu_to_le32(*(PULONG)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
287 break;
289 // Get the R00 R01 first
290 RxDes.R00.value = le32_to_cpu(*(PULONG)pRxBufferAddress);
291 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
292 RxDes.R01.value = le32_to_cpu(*((PULONG)(pRxBufferAddress+4)));
293 // For new DMA 4k
294 if ((PacketSize & 0x03) > 0)
295 PacketSize -= 4;
297 // Basic check for Rx length. Is length valid?
298 if (PacketSize > MAX_PACKET_SIZE) {
299 #ifdef _PE_RX_DUMP_
300 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
301 #endif
303 pWb35Rx->EP3vm_state = VM_STOP;
304 pWb35Rx->Ep3ErrorCount2++;
305 break;
308 // Start to process Rx buffer
309 // RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
310 BufferSize -= 8; //subtract 8 byte for 35's USB header length
311 pRxBufferAddress += 8;
313 RxDes.buffer_address[0] = pRxBufferAddress;
314 RxDes.buffer_size[0] = PacketSize;
315 RxDes.buffer_number = 1;
316 RxDes.buffer_start_index = 0;
317 RxDes.buffer_total_size = RxDes.buffer_size[0];
318 Wb35Rx_adjust(&RxDes);
320 packet_came(pRxBufferAddress, PacketSize);
322 // Move RxBuffer point to the next
323 stmp = PacketSize + 3;
324 stmp &= ~0x03; // 4n alignment
325 pRxBufferAddress += stmp;
326 BufferSize -= stmp;
327 stmp2 += stmp;
330 // Reclaim resource
331 pWb35Rx->RxOwner[ RxBufferId ] = 1;
332 } while(TRUE);
334 return stmp2;