1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
8 // Processing the Rx message from down layer
10 //============================================================================
11 #include <linux/usb.h>
16 void Wb35Rx_start(phw_data_t pHwData)
18 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
20 // Allow only one thread to run into the Wb35Rx() function
21 if (atomic_inc_return(&pWb35Rx->RxFireCounter) == 1) {
22 pWb35Rx->EP3vm_state = VM_RUNNING;
25 atomic_dec(&pWb35Rx->RxFireCounter);
28 // This function cannot reentrain
29 void Wb35Rx( phw_data_t pHwData )
31 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
32 u8 * pRxBufferAddress;
33 struct urb *urb = pWb35Rx->RxUrb;
40 if (pHwData->SurpriseRemove || pHwData->HwStop)
47 RxBufferId = pWb35Rx->RxBufferId;
48 if (!pWb35Rx->RxOwner[RxBufferId]) {
49 // It's impossible to run here.
51 WBDEBUG(("Rx driver fifo unavailable\n"));
56 // Update buffer point, then start to bulkin the data from USB
57 pWb35Rx->RxBufferId++;
58 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
60 pWb35Rx->CurrentRxBufferId = RxBufferId;
62 pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
64 printk("w35und: Rx memory alloc failed\n");
67 pRxBufferAddress = pWb35Rx->pDRx;
69 usb_fill_bulk_urb(urb, pHwData->WbUsb.udev,
70 usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
71 pRxBufferAddress, MAX_USB_RX_BUFFER,
72 Wb35Rx_Complete, pHwData);
74 pWb35Rx->EP3vm_state = VM_RUNNING;
76 retv = usb_submit_urb(urb, GFP_ATOMIC);
79 printk("Rx URB sending error\n");
86 pWb35Rx->EP3vm_state = VM_STOP;
87 atomic_dec(&pWb35Rx->RxFireCounter);
90 void Wb35Rx_Complete(struct urb *urb)
92 phw_data_t pHwData = urb->context;
93 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
94 u8 * pRxBufferAddress;
101 pWb35Rx->EP3vm_state = VM_COMPLETED;
102 pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
104 RxBufferId = pWb35Rx->CurrentRxBufferId;
106 pRxBufferAddress = pWb35Rx->pDRx;
107 BulkLength = (u16)urb->actual_length;
109 // The IRP is completed
110 pWb35Rx->EP3vm_state = VM_COMPLETED;
112 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
115 if (pWb35Rx->rx_halt)
118 // Start to process the data only in successful condition
119 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
120 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
122 // The URB is completed, check the result
123 if (pWb35Rx->EP3VM_status != 0) {
124 #ifdef _PE_USB_STATE_DUMP_
125 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
126 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
128 pWb35Rx->EP3vm_state = VM_STOP;
132 // 20060220 For recovering. check if operating in single USB mode
133 if (!HAL_USB_MODE_BURST(pHwData)) {
134 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
135 if ((SizeCheck & 0x03) > 0)
137 SizeCheck = (SizeCheck + 3) & ~0x03;
138 SizeCheck += 12; // 8 + 4 badbeef
139 if ((BulkLength > 1600) ||
140 (SizeCheck > 1600) ||
141 (BulkLength != SizeCheck) ||
142 (BulkLength == 0)) { // Add for fail Urb
143 pWb35Rx->EP3vm_state = VM_STOP;
144 pWb35Rx->Ep3ErrorCount2++;
148 // Indicating the receiving data
149 pWb35Rx->ByteReceived += BulkLength;
150 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
152 if (!pWb35Rx->RxOwner[ RxBufferId ])
153 Wb35Rx_indicate(pHwData);
155 kfree(pWb35Rx->pDRx);
156 // Do the next receive
161 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
162 atomic_dec(&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 = usb_alloc_urb(0, GFP_ATOMIC);
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 msleep(10); // Delay for waiting function enter 940623.1.a
198 } while (pWb35Rx->EP3vm_state != VM_STOP);
199 msleep(10); // 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 u32 * 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 u8 * 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(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
289 // Get the R00 R01 first
290 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
291 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
292 RxDes.R01.value = le32_to_cpu(*((u32 *)(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;