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 PUCHAR pRxBufferAddress;
31 PURB pUrb = (PURB)pWb35Rx->RxUrb;
39 if (pHwData->SurpriseRemove || pHwData->HwStop)
46 RxBufferId = pWb35Rx->RxBufferId;
47 if (!pWb35Rx->RxOwner[RxBufferId]) {
48 // It's impossible to run here.
50 WBDEBUG(("Rx driver fifo unavailable\n"));
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");
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);
77 printk("Rx URB sending error\n");
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;
99 pWb35Rx->EP3vm_state = VM_COMPLETED;
100 pWb35Rx->EP3VM_status = pUrb->status;//Store the last result of Irp
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
114 if (pWb35Rx->rx_halt)
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 );
127 pWb35Rx->EP3vm_state = VM_STOP;
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)
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
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
186 WBDEBUG(("EP3 Rx stop\n"));
191 // Needs process context
192 void Wb35Rx_destroy(phw_data_t pHwData)
194 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
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
202 usb_free_urb( pWb35Rx->RxUrb );
204 WBDEBUG(("Wb35Rx_destroy OK\n"));
208 void Wb35Rx_reset_descriptor( phw_data_t pHwData )
210 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
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;
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)
239 if (DecryptionMethod == 3) // For CCMP
242 // Adjust the IV field which after 802.11 header and ICV field.
243 if (DecryptionMethod == 1) // For WEP
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
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)
266 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
267 PUCHAR pRxBufferAddress;
269 u16 stmp, BufferSize, stmp2 = 0;
272 // Only one thread be allowed to run into the following
274 RxBufferId = pWb35Rx->RxProcessIndex;
275 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
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
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)));
294 if ((PacketSize & 0x03) > 0)
297 // Basic check for Rx length. Is length valid?
298 if (PacketSize > MAX_PACKET_SIZE) {
300 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
303 pWb35Rx->EP3vm_state = VM_STOP;
304 pWb35Rx->Ep3ErrorCount2++;
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;
331 pWb35Rx->RxOwner[ RxBufferId ] = 1;