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 struct urb *urb = 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 pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
62 printk("w35und: Rx memory alloc failed\n");
65 pRxBufferAddress = pWb35Rx->pDRx;
67 usb_fill_bulk_urb(urb, 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 = usb_submit_urb(urb, GFP_ATOMIC);
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(struct urb *urb)
90 phw_data_t pHwData = urb->context;
91 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
92 u8 * pRxBufferAddress;
99 pWb35Rx->EP3vm_state = VM_COMPLETED;
100 pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
102 RxBufferId = pWb35Rx->CurrentRxBufferId;
104 pRxBufferAddress = pWb35Rx->pDRx;
105 BulkLength = (u16)urb->actual_length;
107 // The IRP is completed
108 pWb35Rx->EP3vm_state = VM_COMPLETED;
110 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
113 if (pWb35Rx->rx_halt)
116 // Start to process the data only in successful condition
117 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
118 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
120 // The URB is completed, check the result
121 if (pWb35Rx->EP3VM_status != 0) {
122 #ifdef _PE_USB_STATE_DUMP_
123 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
124 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
126 pWb35Rx->EP3vm_state = VM_STOP;
130 // 20060220 For recovering. check if operating in single USB mode
131 if (!HAL_USB_MODE_BURST(pHwData)) {
132 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
133 if ((SizeCheck & 0x03) > 0)
135 SizeCheck = (SizeCheck + 3) & ~0x03;
136 SizeCheck += 12; // 8 + 4 badbeef
137 if ((BulkLength > 1600) ||
138 (SizeCheck > 1600) ||
139 (BulkLength != SizeCheck) ||
140 (BulkLength == 0)) { // Add for fail Urb
141 pWb35Rx->EP3vm_state = VM_STOP;
142 pWb35Rx->Ep3ErrorCount2++;
146 // Indicating the receiving data
147 pWb35Rx->ByteReceived += BulkLength;
148 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
150 if (!pWb35Rx->RxOwner[ RxBufferId ])
151 Wb35Rx_indicate(pHwData);
153 kfree(pWb35Rx->pDRx);
154 // Do the next receive
159 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
160 OS_ATOMIC_DEC( pHwData->adapter, &pWb35Rx->RxFireCounter );
161 pWb35Rx->EP3vm_state = VM_STOP;
164 //=====================================================================================
165 unsigned char Wb35Rx_initial(phw_data_t pHwData)
167 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
169 // Initial the Buffer Queue
170 Wb35Rx_reset_descriptor( pHwData );
172 pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
173 return (!!pWb35Rx->RxUrb);
176 void Wb35Rx_stop(phw_data_t pHwData)
178 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
180 // Canceling the Irp if already sends it out.
181 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
182 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
184 WBDEBUG(("EP3 Rx stop\n"));
189 // Needs process context
190 void Wb35Rx_destroy(phw_data_t pHwData)
192 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
195 msleep(10); // Delay for waiting function enter 940623.1.a
196 } while (pWb35Rx->EP3vm_state != VM_STOP);
197 msleep(10); // Delay for waiting function exit 940623.1.b
200 usb_free_urb( pWb35Rx->RxUrb );
202 WBDEBUG(("Wb35Rx_destroy OK\n"));
206 void Wb35Rx_reset_descriptor( phw_data_t pHwData )
208 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
211 pWb35Rx->ByteReceived = 0;
212 pWb35Rx->RxProcessIndex = 0;
213 pWb35Rx->RxBufferId = 0;
214 pWb35Rx->EP3vm_state = VM_STOP;
215 pWb35Rx->rx_halt = 0;
217 // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
218 for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
219 pWb35Rx->RxOwner[i] = 1;
222 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
224 u32 * pRxBufferAddress;
225 u32 DecryptionMethod;
229 DecryptionMethod = pRxDes->R01.R01_decryption_method;
230 pRxBufferAddress = pRxDes->buffer_address[0];
231 BufferSize = pRxDes->buffer_size[0];
233 // Adjust the last part of data. Only data left
234 BufferSize -= 4; // For CRC-32
235 if (DecryptionMethod)
237 if (DecryptionMethod == 3) // For CCMP
240 // Adjust the IV field which after 802.11 header and ICV field.
241 if (DecryptionMethod == 1) // For WEP
244 pRxBufferAddress[i] = pRxBufferAddress[i-1];
245 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
246 BufferSize -= 4; // 4 byte for IV
248 else if( DecryptionMethod ) // For TKIP and CCMP
251 pRxBufferAddress[i] = pRxBufferAddress[i-2];
252 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
253 BufferSize -= 8; // 8 byte for IV + ICV
255 pRxDes->buffer_size[0] = BufferSize;
258 extern void packet_came(char *pRxBufferAddress, int PacketSize);
261 u16 Wb35Rx_indicate(phw_data_t pHwData)
264 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
265 u8 * pRxBufferAddress;
267 u16 stmp, BufferSize, stmp2 = 0;
270 // Only one thread be allowed to run into the following
272 RxBufferId = pWb35Rx->RxProcessIndex;
273 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
276 pWb35Rx->RxProcessIndex++;
277 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
279 pRxBufferAddress = pWb35Rx->pDRx;
280 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
282 // Parse the bulkin buffer
283 while (BufferSize >= 4) {
284 if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
287 // Get the R00 R01 first
288 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
289 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
290 RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
292 if ((PacketSize & 0x03) > 0)
295 // Basic check for Rx length. Is length valid?
296 if (PacketSize > MAX_PACKET_SIZE) {
298 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
301 pWb35Rx->EP3vm_state = VM_STOP;
302 pWb35Rx->Ep3ErrorCount2++;
306 // Start to process Rx buffer
307 // RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
308 BufferSize -= 8; //subtract 8 byte for 35's USB header length
309 pRxBufferAddress += 8;
311 RxDes.buffer_address[0] = pRxBufferAddress;
312 RxDes.buffer_size[0] = PacketSize;
313 RxDes.buffer_number = 1;
314 RxDes.buffer_start_index = 0;
315 RxDes.buffer_total_size = RxDes.buffer_size[0];
316 Wb35Rx_adjust(&RxDes);
318 packet_came(pRxBufferAddress, PacketSize);
320 // Move RxBuffer point to the next
321 stmp = PacketSize + 3;
322 stmp &= ~0x03; // 4n alignment
323 pRxBufferAddress += stmp;
329 pWb35Rx->RxOwner[ RxBufferId ] = 1;