1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
8 // Processing the Rx message from down layer
10 //============================================================================
11 #include <linux/usb.h>
17 void Wb35Rx_start(struct ieee80211_hw *hw)
19 struct wbsoft_priv *priv = hw->priv;
20 phw_data_t pHwData = &priv->sHwData;
21 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
23 // Allow only one thread to run into the Wb35Rx() function
24 if (atomic_inc_return(&pWb35Rx->RxFireCounter) == 1) {
25 pWb35Rx->EP3vm_state = VM_RUNNING;
28 atomic_dec(&pWb35Rx->RxFireCounter);
31 // This function cannot reentrain
32 void Wb35Rx(struct ieee80211_hw *hw)
34 struct wbsoft_priv *priv = hw->priv;
35 phw_data_t pHwData = &priv->sHwData;
36 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
37 u8 * pRxBufferAddress;
38 struct urb *urb = pWb35Rx->RxUrb;
45 if (pHwData->SurpriseRemove || pHwData->HwStop)
52 RxBufferId = pWb35Rx->RxBufferId;
53 if (!pWb35Rx->RxOwner[RxBufferId]) {
54 // It's impossible to run here.
56 WBDEBUG(("Rx driver fifo unavailable\n"));
61 // Update buffer point, then start to bulkin the data from USB
62 pWb35Rx->RxBufferId++;
63 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
65 pWb35Rx->CurrentRxBufferId = RxBufferId;
67 pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
69 printk("w35und: Rx memory alloc failed\n");
72 pRxBufferAddress = pWb35Rx->pDRx;
74 usb_fill_bulk_urb(urb, pHwData->WbUsb.udev,
75 usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
76 pRxBufferAddress, MAX_USB_RX_BUFFER,
79 pWb35Rx->EP3vm_state = VM_RUNNING;
81 retv = usb_submit_urb(urb, GFP_ATOMIC);
84 printk("Rx URB sending error\n");
91 pWb35Rx->EP3vm_state = VM_STOP;
92 atomic_dec(&pWb35Rx->RxFireCounter);
95 void Wb35Rx_Complete(struct urb *urb)
97 struct ieee80211_hw *hw = urb->context;
98 struct wbsoft_priv *priv = hw->priv;
99 phw_data_t pHwData = &priv->sHwData;
100 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
101 u8 * pRxBufferAddress;
108 pWb35Rx->EP3vm_state = VM_COMPLETED;
109 pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
111 RxBufferId = pWb35Rx->CurrentRxBufferId;
113 pRxBufferAddress = pWb35Rx->pDRx;
114 BulkLength = (u16)urb->actual_length;
116 // The IRP is completed
117 pWb35Rx->EP3vm_state = VM_COMPLETED;
119 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
122 if (pWb35Rx->rx_halt)
125 // Start to process the data only in successful condition
126 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
127 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
129 // The URB is completed, check the result
130 if (pWb35Rx->EP3VM_status != 0) {
131 #ifdef _PE_USB_STATE_DUMP_
132 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
133 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
135 pWb35Rx->EP3vm_state = VM_STOP;
139 // 20060220 For recovering. check if operating in single USB mode
140 if (!HAL_USB_MODE_BURST(pHwData)) {
141 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
142 if ((SizeCheck & 0x03) > 0)
144 SizeCheck = (SizeCheck + 3) & ~0x03;
145 SizeCheck += 12; // 8 + 4 badbeef
146 if ((BulkLength > 1600) ||
147 (SizeCheck > 1600) ||
148 (BulkLength != SizeCheck) ||
149 (BulkLength == 0)) { // Add for fail Urb
150 pWb35Rx->EP3vm_state = VM_STOP;
151 pWb35Rx->Ep3ErrorCount2++;
155 // Indicating the receiving data
156 pWb35Rx->ByteReceived += BulkLength;
157 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
159 if (!pWb35Rx->RxOwner[ RxBufferId ])
162 kfree(pWb35Rx->pDRx);
163 // Do the next receive
168 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
169 atomic_dec(&pWb35Rx->RxFireCounter);
170 pWb35Rx->EP3vm_state = VM_STOP;
173 //=====================================================================================
174 unsigned char Wb35Rx_initial(phw_data_t pHwData)
176 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
178 // Initial the Buffer Queue
179 Wb35Rx_reset_descriptor( pHwData );
181 pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
182 return (!!pWb35Rx->RxUrb);
185 void Wb35Rx_stop(phw_data_t pHwData)
187 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
189 // Canceling the Irp if already sends it out.
190 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
191 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
193 WBDEBUG(("EP3 Rx stop\n"));
198 // Needs process context
199 void Wb35Rx_destroy(phw_data_t pHwData)
201 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
204 msleep(10); // Delay for waiting function enter 940623.1.a
205 } while (pWb35Rx->EP3vm_state != VM_STOP);
206 msleep(10); // Delay for waiting function exit 940623.1.b
209 usb_free_urb( pWb35Rx->RxUrb );
211 WBDEBUG(("Wb35Rx_destroy OK\n"));
215 void Wb35Rx_reset_descriptor( phw_data_t pHwData )
217 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
220 pWb35Rx->ByteReceived = 0;
221 pWb35Rx->RxProcessIndex = 0;
222 pWb35Rx->RxBufferId = 0;
223 pWb35Rx->EP3vm_state = VM_STOP;
224 pWb35Rx->rx_halt = 0;
226 // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
227 for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
228 pWb35Rx->RxOwner[i] = 1;
231 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
233 u32 * pRxBufferAddress;
234 u32 DecryptionMethod;
238 DecryptionMethod = pRxDes->R01.R01_decryption_method;
239 pRxBufferAddress = pRxDes->buffer_address[0];
240 BufferSize = pRxDes->buffer_size[0];
242 // Adjust the last part of data. Only data left
243 BufferSize -= 4; // For CRC-32
244 if (DecryptionMethod)
246 if (DecryptionMethod == 3) // For CCMP
249 // Adjust the IV field which after 802.11 header and ICV field.
250 if (DecryptionMethod == 1) // For WEP
253 pRxBufferAddress[i] = pRxBufferAddress[i-1];
254 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
255 BufferSize -= 4; // 4 byte for IV
257 else if( DecryptionMethod ) // For TKIP and CCMP
260 pRxBufferAddress[i] = pRxBufferAddress[i-2];
261 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
262 BufferSize -= 8; // 8 byte for IV + ICV
264 pRxDes->buffer_size[0] = BufferSize;
267 static void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize)
269 struct wbsoft_priv *priv = hw->priv;
271 struct ieee80211_rx_status rx_status = {0};
276 skb = dev_alloc_skb(PacketSize);
278 printk("Not enough memory for packet, FIXME\n");
282 memcpy(skb_put(skb, PacketSize),
288 rx_status.channel = 1;
289 rx_status.freq = 12345;
290 rx_status.phymode = MODE_IEEE80211B;
293 ieee80211_rx_irqsafe(hw, skb, &rx_status);
296 u16 Wb35Rx_indicate(struct ieee80211_hw *hw)
298 struct wbsoft_priv *priv = hw->priv;
299 phw_data_t pHwData = &priv->sHwData;
301 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
302 u8 * pRxBufferAddress;
304 u16 stmp, BufferSize, stmp2 = 0;
307 // Only one thread be allowed to run into the following
309 RxBufferId = pWb35Rx->RxProcessIndex;
310 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
313 pWb35Rx->RxProcessIndex++;
314 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
316 pRxBufferAddress = pWb35Rx->pDRx;
317 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
319 // Parse the bulkin buffer
320 while (BufferSize >= 4) {
321 if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
324 // Get the R00 R01 first
325 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
326 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
327 RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
329 if ((PacketSize & 0x03) > 0)
332 // Basic check for Rx length. Is length valid?
333 if (PacketSize > MAX_PACKET_SIZE) {
335 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
338 pWb35Rx->EP3vm_state = VM_STOP;
339 pWb35Rx->Ep3ErrorCount2++;
343 // Start to process Rx buffer
344 // RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
345 BufferSize -= 8; //subtract 8 byte for 35's USB header length
346 pRxBufferAddress += 8;
348 RxDes.buffer_address[0] = pRxBufferAddress;
349 RxDes.buffer_size[0] = PacketSize;
350 RxDes.buffer_number = 1;
351 RxDes.buffer_start_index = 0;
352 RxDes.buffer_total_size = RxDes.buffer_size[0];
353 Wb35Rx_adjust(&RxDes);
355 packet_came(hw, pRxBufferAddress, PacketSize);
357 // Move RxBuffer point to the next
358 stmp = PacketSize + 3;
359 stmp &= ~0x03; // 4n alignment
360 pRxBufferAddress += stmp;
366 pWb35Rx->RxOwner[ RxBufferId ] = 1;