1 //============================================================================
2 // Copyright (c) 1996-2002 Winbond Electronic Corporation
8 // Processing the Rx message from down layer
10 //============================================================================
11 #include <linux/usb.h>
17 static void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize)
19 struct wbsoft_priv *priv = hw->priv;
21 struct ieee80211_rx_status rx_status = {0};
26 skb = dev_alloc_skb(PacketSize);
28 printk("Not enough memory for packet, FIXME\n");
32 memcpy(skb_put(skb, PacketSize),
38 rx_status.channel = 1;
39 rx_status.freq = 12345;
40 rx_status.phymode = MODE_IEEE80211B;
43 ieee80211_rx_irqsafe(hw, skb, &rx_status);
46 static void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
48 u32 * pRxBufferAddress;
53 DecryptionMethod = pRxDes->R01.R01_decryption_method;
54 pRxBufferAddress = pRxDes->buffer_address[0];
55 BufferSize = pRxDes->buffer_size[0];
57 // Adjust the last part of data. Only data left
58 BufferSize -= 4; // For CRC-32
61 if (DecryptionMethod == 3) // For CCMP
64 // Adjust the IV field which after 802.11 header and ICV field.
65 if (DecryptionMethod == 1) // For WEP
68 pRxBufferAddress[i] = pRxBufferAddress[i-1];
69 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
70 BufferSize -= 4; // 4 byte for IV
72 else if( DecryptionMethod ) // For TKIP and CCMP
75 pRxBufferAddress[i] = pRxBufferAddress[i-2];
76 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
77 BufferSize -= 8; // 8 byte for IV + ICV
79 pRxDes->buffer_size[0] = BufferSize;
82 static u16 Wb35Rx_indicate(struct ieee80211_hw *hw)
84 struct wbsoft_priv *priv = hw->priv;
85 phw_data_t pHwData = &priv->sHwData;
87 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
88 u8 * pRxBufferAddress;
90 u16 stmp, BufferSize, stmp2 = 0;
93 // Only one thread be allowed to run into the following
95 RxBufferId = pWb35Rx->RxProcessIndex;
96 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
99 pWb35Rx->RxProcessIndex++;
100 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
102 pRxBufferAddress = pWb35Rx->pDRx;
103 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
105 // Parse the bulkin buffer
106 while (BufferSize >= 4) {
107 if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
110 // Get the R00 R01 first
111 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
112 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
113 RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
115 if ((PacketSize & 0x03) > 0)
118 // Basic check for Rx length. Is length valid?
119 if (PacketSize > MAX_PACKET_SIZE) {
121 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
124 pWb35Rx->EP3vm_state = VM_STOP;
125 pWb35Rx->Ep3ErrorCount2++;
129 // Start to process Rx buffer
130 // RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
131 BufferSize -= 8; //subtract 8 byte for 35's USB header length
132 pRxBufferAddress += 8;
134 RxDes.buffer_address[0] = pRxBufferAddress;
135 RxDes.buffer_size[0] = PacketSize;
136 RxDes.buffer_number = 1;
137 RxDes.buffer_start_index = 0;
138 RxDes.buffer_total_size = RxDes.buffer_size[0];
139 Wb35Rx_adjust(&RxDes);
141 packet_came(hw, pRxBufferAddress, PacketSize);
143 // Move RxBuffer point to the next
144 stmp = PacketSize + 3;
145 stmp &= ~0x03; // 4n alignment
146 pRxBufferAddress += stmp;
152 pWb35Rx->RxOwner[ RxBufferId ] = 1;
158 static void Wb35Rx(struct ieee80211_hw *hw);
160 static void Wb35Rx_Complete(struct urb *urb)
162 struct ieee80211_hw *hw = urb->context;
163 struct wbsoft_priv *priv = hw->priv;
164 phw_data_t pHwData = &priv->sHwData;
165 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
166 u8 * pRxBufferAddress;
173 pWb35Rx->EP3vm_state = VM_COMPLETED;
174 pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
176 RxBufferId = pWb35Rx->CurrentRxBufferId;
178 pRxBufferAddress = pWb35Rx->pDRx;
179 BulkLength = (u16)urb->actual_length;
181 // The IRP is completed
182 pWb35Rx->EP3vm_state = VM_COMPLETED;
184 if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
187 if (pWb35Rx->rx_halt)
190 // Start to process the data only in successful condition
191 pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
192 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
194 // The URB is completed, check the result
195 if (pWb35Rx->EP3VM_status != 0) {
196 #ifdef _PE_USB_STATE_DUMP_
197 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
198 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
200 pWb35Rx->EP3vm_state = VM_STOP;
204 // 20060220 For recovering. check if operating in single USB mode
205 if (!HAL_USB_MODE_BURST(pHwData)) {
206 SizeCheck = R00.R00_receive_byte_count; //20060926 anson's endian
207 if ((SizeCheck & 0x03) > 0)
209 SizeCheck = (SizeCheck + 3) & ~0x03;
210 SizeCheck += 12; // 8 + 4 badbeef
211 if ((BulkLength > 1600) ||
212 (SizeCheck > 1600) ||
213 (BulkLength != SizeCheck) ||
214 (BulkLength == 0)) { // Add for fail Urb
215 pWb35Rx->EP3vm_state = VM_STOP;
216 pWb35Rx->Ep3ErrorCount2++;
220 // Indicating the receiving data
221 pWb35Rx->ByteReceived += BulkLength;
222 pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
224 if (!pWb35Rx->RxOwner[ RxBufferId ])
227 kfree(pWb35Rx->pDRx);
228 // Do the next receive
233 pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
234 atomic_dec(&pWb35Rx->RxFireCounter);
235 pWb35Rx->EP3vm_state = VM_STOP;
238 // This function cannot reentrain
239 static void Wb35Rx(struct ieee80211_hw *hw)
241 struct wbsoft_priv *priv = hw->priv;
242 phw_data_t pHwData = &priv->sHwData;
243 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
244 u8 * pRxBufferAddress;
245 struct urb *urb = pWb35Rx->RxUrb;
252 if (pHwData->SurpriseRemove || pHwData->HwStop)
255 if (pWb35Rx->rx_halt)
259 RxBufferId = pWb35Rx->RxBufferId;
260 if (!pWb35Rx->RxOwner[RxBufferId]) {
261 // It's impossible to run here.
263 WBDEBUG(("Rx driver fifo unavailable\n"));
268 // Update buffer point, then start to bulkin the data from USB
269 pWb35Rx->RxBufferId++;
270 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
272 pWb35Rx->CurrentRxBufferId = RxBufferId;
274 pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
275 if (!pWb35Rx->pDRx) {
276 printk("w35und: Rx memory alloc failed\n");
279 pRxBufferAddress = pWb35Rx->pDRx;
281 usb_fill_bulk_urb(urb, pHwData->WbUsb.udev,
282 usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
283 pRxBufferAddress, MAX_USB_RX_BUFFER,
284 Wb35Rx_Complete, hw);
286 pWb35Rx->EP3vm_state = VM_RUNNING;
288 retv = usb_submit_urb(urb, GFP_ATOMIC);
291 printk("Rx URB sending error\n");
298 pWb35Rx->EP3vm_state = VM_STOP;
299 atomic_dec(&pWb35Rx->RxFireCounter);
302 void Wb35Rx_start(struct ieee80211_hw *hw)
304 struct wbsoft_priv *priv = hw->priv;
305 phw_data_t pHwData = &priv->sHwData;
306 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
308 // Allow only one thread to run into the Wb35Rx() function
309 if (atomic_inc_return(&pWb35Rx->RxFireCounter) == 1) {
310 pWb35Rx->EP3vm_state = VM_RUNNING;
313 atomic_dec(&pWb35Rx->RxFireCounter);
316 //=====================================================================================
317 static void Wb35Rx_reset_descriptor( phw_data_t pHwData )
319 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
322 pWb35Rx->ByteReceived = 0;
323 pWb35Rx->RxProcessIndex = 0;
324 pWb35Rx->RxBufferId = 0;
325 pWb35Rx->EP3vm_state = VM_STOP;
326 pWb35Rx->rx_halt = 0;
328 // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
329 for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
330 pWb35Rx->RxOwner[i] = 1;
333 unsigned char Wb35Rx_initial(phw_data_t pHwData)
335 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
337 // Initial the Buffer Queue
338 Wb35Rx_reset_descriptor( pHwData );
340 pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
341 return (!!pWb35Rx->RxUrb);
344 void Wb35Rx_stop(phw_data_t pHwData)
346 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
348 // Canceling the Irp if already sends it out.
349 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
350 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
352 WBDEBUG(("EP3 Rx stop\n"));
357 // Needs process context
358 void Wb35Rx_destroy(phw_data_t pHwData)
360 PWB35RX pWb35Rx = &pHwData->Wb35Rx;
363 msleep(10); // Delay for waiting function enter 940623.1.a
364 } while (pWb35Rx->EP3vm_state != VM_STOP);
365 msleep(10); // Delay for waiting function exit 940623.1.b
368 usb_free_urb( pWb35Rx->RxUrb );
370 WBDEBUG(("Wb35Rx_destroy OK\n"));