1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
8 // Processing the Rx message from down layer
10 //============================================================================
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;
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 u8 * pRxBufferAddress;
31 PURB pUrb = (PURB)pWb35Rx->RxUrb;
38 if (pHwData->SurpriseRemove || pHwData->HwStop)
45 RxBufferId = pWb35Rx->RxBufferId;
46 if (!pWb35Rx->RxOwner[RxBufferId]) {
47 // It's impossible to run here.
49 WBDEBUG(("Rx driver fifo unavailable\n"));
54 // Update buffer point, then start to bulkin the data from USB
55 pWb35Rx->RxBufferId++;
56 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
58 pWb35Rx->CurrentRxBufferId = RxBufferId;
60 if (1 != OS_MEMORY_ALLOC((void* *)&pWb35Rx->pDRx, MAX_USB_RX_BUFFER)) {
61 printk("w35und: Rx memory alloc failed\n");
64 pRxBufferAddress = pWb35Rx->pDRx;
66 usb_fill_bulk_urb(pUrb, pHwData->WbUsb.udev,
67 usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
68 pRxBufferAddress, MAX_USB_RX_BUFFER,
69 Wb35Rx_Complete, pHwData);
71 pWb35Rx->EP3vm_state = VM_RUNNING;
73 retv = wb_usb_submit_urb(pUrb);
76 printk("Rx URB sending error\n");
83 pWb35Rx->EP3vm_state = VM_STOP;
84 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
87 void Wb35Rx_Complete(PURB pUrb)
89 phw_data_t pHwData = pUrb->context;
90 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
91 u8 * pRxBufferAddress;
98 pWb35Rx->EP3vm_state = VM_COMPLETED;
99 pWb35Rx->EP3VM_status = pUrb->status;//Store the last result of Irp
101 RxBufferId = pWb35Rx->CurrentRxBufferId;
103 pRxBufferAddress = pWb35Rx->pDRx;
104 BulkLength = (u16)pUrb->actual_length;
106 // The IRP is completed
107 pWb35Rx->EP3vm_state = VM_COMPLETED;
109 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
112 if (pWb35Rx->rx_halt)
115 // Start to process the data only in successful condition
116 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
117 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
119 // The URB is completed, check the result
120 if (pWb35Rx->EP3VM_status != 0) {
121 #ifdef _PE_USB_STATE_DUMP_
122 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
123 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
125 pWb35Rx->EP3vm_state = VM_STOP;
129 // 20060220 For recovering. check if operating in single USB mode
130 if (!HAL_USB_MODE_BURST(pHwData)) {
131 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
132 if ((SizeCheck & 0x03) > 0)
134 SizeCheck = (SizeCheck + 3) & ~0x03;
135 SizeCheck += 12; // 8 + 4 badbeef
136 if ((BulkLength > 1600) ||
137 (SizeCheck > 1600) ||
138 (BulkLength != SizeCheck) ||
139 (BulkLength == 0)) { // Add for fail Urb
140 pWb35Rx->EP3vm_state = VM_STOP;
141 pWb35Rx->Ep3ErrorCount2++;
145 // Indicating the receiving data
146 pWb35Rx->ByteReceived += BulkLength;
147 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
149 if (!pWb35Rx->RxOwner[ RxBufferId ])
150 Wb35Rx_indicate(pHwData);
152 kfree(pWb35Rx->pDRx);
153 // Do the next receive
158 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
159 OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
160 pWb35Rx->EP3vm_state = VM_STOP;
163 //=====================================================================================
164 unsigned char Wb35Rx_initial(phw_data_t pHwData)
166 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
168 // Initial the Buffer Queue
169 Wb35Rx_reset_descriptor( pHwData );
171 pWb35Rx->RxUrb = wb_usb_alloc_urb(0);
172 return (!!pWb35Rx->RxUrb);
175 void Wb35Rx_stop(phw_data_t pHwData)
177 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
179 // Canceling the Irp if already sends it out.
180 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
181 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
183 WBDEBUG(("EP3 Rx stop\n"));
188 // Needs process context
189 void Wb35Rx_destroy(phw_data_t pHwData)
191 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
194 OS_SLEEP(10000); // Delay for waiting function enter 940623.1.a
195 } while (pWb35Rx->EP3vm_state != VM_STOP);
196 OS_SLEEP(10000); // Delay for waiting function exit 940623.1.b
199 usb_free_urb( pWb35Rx->RxUrb );
201 WBDEBUG(("Wb35Rx_destroy OK\n"));
205 void Wb35Rx_reset_descriptor( phw_data_t pHwData )
207 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
210 pWb35Rx->ByteReceived = 0;
211 pWb35Rx->RxProcessIndex = 0;
212 pWb35Rx->RxBufferId = 0;
213 pWb35Rx->EP3vm_state = VM_STOP;
214 pWb35Rx->rx_halt = 0;
216 // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
217 for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
218 pWb35Rx->RxOwner[i] = 1;
221 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
223 u32 * pRxBufferAddress;
224 u32 DecryptionMethod;
228 DecryptionMethod = pRxDes->R01.R01_decryption_method;
229 pRxBufferAddress = pRxDes->buffer_address[0];
230 BufferSize = pRxDes->buffer_size[0];
232 // Adjust the last part of data. Only data left
233 BufferSize -= 4; // For CRC-32
234 if (DecryptionMethod)
236 if (DecryptionMethod == 3) // For CCMP
239 // Adjust the IV field which after 802.11 header and ICV field.
240 if (DecryptionMethod == 1) // For WEP
243 pRxBufferAddress[i] = pRxBufferAddress[i-1];
244 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
245 BufferSize -= 4; // 4 byte for IV
247 else if( DecryptionMethod ) // For TKIP and CCMP
250 pRxBufferAddress[i] = pRxBufferAddress[i-2];
251 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
252 BufferSize -= 8; // 8 byte for IV + ICV
254 pRxDes->buffer_size[0] = BufferSize;
257 extern void packet_came(char *pRxBufferAddress, int PacketSize);
260 u16 Wb35Rx_indicate(phw_data_t pHwData)
263 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
264 u8 * pRxBufferAddress;
266 u16 stmp, BufferSize, stmp2 = 0;
269 // Only one thread be allowed to run into the following
271 RxBufferId = pWb35Rx->RxProcessIndex;
272 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
275 pWb35Rx->RxProcessIndex++;
276 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
278 pRxBufferAddress = pWb35Rx->pDRx;
279 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
281 // Parse the bulkin buffer
282 while (BufferSize >= 4) {
283 if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
286 // Get the R00 R01 first
287 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
288 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
289 RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
291 if ((PacketSize & 0x03) > 0)
294 // Basic check for Rx length. Is length valid?
295 if (PacketSize > MAX_PACKET_SIZE) {
297 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
300 pWb35Rx->EP3vm_state = VM_STOP;
301 pWb35Rx->Ep3ErrorCount2++;
305 // Start to process Rx buffer
306 // RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
307 BufferSize -= 8; //subtract 8 byte for 35's USB header length
308 pRxBufferAddress += 8;
310 RxDes.buffer_address[0] = pRxBufferAddress;
311 RxDes.buffer_size[0] = PacketSize;
312 RxDes.buffer_number = 1;
313 RxDes.buffer_start_index = 0;
314 RxDes.buffer_total_size = RxDes.buffer_size[0];
315 Wb35Rx_adjust(&RxDes);
317 packet_came(pRxBufferAddress, PacketSize);
319 // Move RxBuffer point to the next
320 stmp = PacketSize + 3;
321 stmp &= ~0x03; // 4n alignment
322 pRxBufferAddress += stmp;
328 pWb35Rx->RxOwner[ RxBufferId ] = 1;