2 * ============================================================================
3 * Copyright (c) 1996-2002 Winbond Electronic Corporation
9 * Processing the Rx message from down layer
11 * ============================================================================
13 #include <linux/usb.h>
14 #include <linux/slab.h>
19 static void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress,
22 struct wbsoft_priv *priv = hw->priv;
24 struct ieee80211_rx_status rx_status = {0};
29 skb = dev_alloc_skb(PacketSize);
31 printk("Not enough memory for packet, FIXME\n");
35 memcpy(skb_put(skb, PacketSize), pRxBufferAddress, PacketSize);
37 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
38 ieee80211_rx_irqsafe(hw, skb);
41 static void Wb35Rx_adjust(struct wb35_descriptor *pRxDes)
43 u32 *pRxBufferAddress;
48 DecryptionMethod = pRxDes->R01.R01_decryption_method;
49 pRxBufferAddress = pRxDes->buffer_address[0];
50 BufferSize = pRxDes->buffer_size[0];
52 /* Adjust the last part of data. Only data left */
53 BufferSize -= 4; /* For CRC-32 */
56 if (DecryptionMethod == 3) /* For CCMP */
59 /* Adjust the IV field which after 802.11 header and ICV field. */
60 if (DecryptionMethod == 1) { /* For WEP */
61 for (i = 6; i > 0; i--)
62 pRxBufferAddress[i] = pRxBufferAddress[i - 1];
63 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
64 BufferSize -= 4; /* 4 byte for IV */
65 } else if (DecryptionMethod) { /* For TKIP and CCMP */
66 for (i = 7; i > 1; i--)
67 pRxBufferAddress[i] = pRxBufferAddress[i - 2];
68 /* Update the descriptor, shift 8 byte */
69 pRxDes->buffer_address[0] = pRxBufferAddress + 2;
70 BufferSize -= 8; /* 8 byte for IV + ICV */
72 pRxDes->buffer_size[0] = BufferSize;
75 static u16 Wb35Rx_indicate(struct ieee80211_hw *hw)
77 struct wbsoft_priv *priv = hw->priv;
78 struct hw_data *pHwData = &priv->sHwData;
79 struct wb35_descriptor RxDes;
80 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
83 u16 stmp, BufferSize, stmp2 = 0;
86 /* Only one thread be allowed to run into the following */
88 RxBufferId = pWb35Rx->RxProcessIndex;
89 if (pWb35Rx->RxOwner[RxBufferId]) /* Owner by VM */
92 pWb35Rx->RxProcessIndex++;
93 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
95 pRxBufferAddress = pWb35Rx->pDRx;
96 BufferSize = pWb35Rx->RxBufferSize[RxBufferId];
98 /* Parse the bulkin buffer */
99 while (BufferSize >= 4) {
101 if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) ==
105 /* Get the R00 R01 first */
106 RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
107 PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
108 RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress + 4)));
110 if ((PacketSize & 0x03) > 0)
113 /* Basic check for Rx length. Is length valid? */
114 if (PacketSize > MAX_PACKET_SIZE) {
115 pr_debug("Serious ERROR : Rx data size too long, size =%d\n",
117 pWb35Rx->EP3vm_state = VM_STOP;
118 pWb35Rx->Ep3ErrorCount2++;
123 * Wb35Rx_indicate() is called synchronously so it isn't
124 * necessary to set "RxDes.Desctriptor_ID = RxBufferID;"
126 /* subtract 8 byte for 35's USB header length */
128 pRxBufferAddress += 8;
130 RxDes.buffer_address[0] = pRxBufferAddress;
131 RxDes.buffer_size[0] = PacketSize;
132 RxDes.buffer_number = 1;
133 RxDes.buffer_start_index = 0;
134 RxDes.buffer_total_size = RxDes.buffer_size[0];
135 Wb35Rx_adjust(&RxDes);
137 packet_came(hw, pRxBufferAddress, PacketSize);
139 /* Move RxBuffer point to the next */
140 stmp = PacketSize + 3;
141 stmp &= ~0x03; /* 4n alignment */
142 pRxBufferAddress += stmp;
147 /* Reclaim resource */
148 pWb35Rx->RxOwner[RxBufferId] = 1;
153 static void Wb35Rx(struct ieee80211_hw *hw);
155 static void Wb35Rx_Complete(struct urb *urb)
157 struct ieee80211_hw *hw = urb->context;
158 struct wbsoft_priv *priv = hw->priv;
159 struct hw_data *pHwData = &priv->sHwData;
160 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
161 u8 *pRxBufferAddress;
165 struct R00_descriptor R00;
167 /* Variable setting */
168 pWb35Rx->EP3vm_state = VM_COMPLETED;
169 pWb35Rx->EP3VM_status = urb->status; /* Store the last result of Irp */
171 RxBufferId = pWb35Rx->CurrentRxBufferId;
173 pRxBufferAddress = pWb35Rx->pDRx;
174 BulkLength = (u16)urb->actual_length;
176 /* The IRP is completed */
177 pWb35Rx->EP3vm_state = VM_COMPLETED;
179 if (pHwData->SurpriseRemove) /* Must be here, or RxBufferId is invalid */
182 if (pWb35Rx->rx_halt)
185 /* Start to process the data only in successful condition */
186 pWb35Rx->RxOwner[RxBufferId] = 0; /* Set the owner to driver */
187 R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
189 /* The URB is completed, check the result */
190 if (pWb35Rx->EP3VM_status != 0) {
191 pr_debug("EP3 IoCompleteRoutine return error\n");
192 pWb35Rx->EP3vm_state = VM_STOP;
196 /* For recovering. check if operating in single USB mode */
197 if (!HAL_USB_MODE_BURST(pHwData)) {
198 SizeCheck = R00.R00_receive_byte_count;
199 if ((SizeCheck & 0x03) > 0)
201 SizeCheck = (SizeCheck + 3) & ~0x03;
202 SizeCheck += 12; /* 8 + 4 badbeef */
203 if ((BulkLength > 1600) ||
204 (SizeCheck > 1600) ||
205 (BulkLength != SizeCheck) ||
206 (BulkLength == 0)) { /* Add for fail Urb */
207 pWb35Rx->EP3vm_state = VM_STOP;
208 pWb35Rx->Ep3ErrorCount2++;
212 /* Indicating the receiving data */
213 pWb35Rx->ByteReceived += BulkLength;
214 pWb35Rx->RxBufferSize[RxBufferId] = BulkLength;
216 if (!pWb35Rx->RxOwner[RxBufferId])
219 kfree(pWb35Rx->pDRx);
220 /* Do the next receive */
225 pWb35Rx->RxOwner[RxBufferId] = 1; /* Set the owner to hardware */
226 atomic_dec(&pWb35Rx->RxFireCounter);
227 pWb35Rx->EP3vm_state = VM_STOP;
230 /* This function cannot reentrain */
231 static void Wb35Rx(struct ieee80211_hw *hw)
233 struct wbsoft_priv *priv = hw->priv;
234 struct hw_data *pHwData = &priv->sHwData;
235 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
236 u8 *pRxBufferAddress;
237 struct urb *urb = pWb35Rx->RxUrb;
242 if (pHwData->SurpriseRemove)
245 if (pWb35Rx->rx_halt)
248 /* Get RxBuffer's ID */
249 RxBufferId = pWb35Rx->RxBufferId;
250 if (!pWb35Rx->RxOwner[RxBufferId]) {
251 /* It's impossible to run here. */
252 pr_debug("Rx driver fifo unavailable\n");
256 /* Update buffer point, then start to bulkin the data from USB */
257 pWb35Rx->RxBufferId++;
258 pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
260 pWb35Rx->CurrentRxBufferId = RxBufferId;
262 pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
263 if (!pWb35Rx->pDRx) {
264 dev_info(&hw->wiphy->dev, "w35und: Rx memory alloc failed\n");
267 pRxBufferAddress = pWb35Rx->pDRx;
269 usb_fill_bulk_urb(urb, pHwData->udev,
270 usb_rcvbulkpipe(pHwData->udev, 3),
271 pRxBufferAddress, MAX_USB_RX_BUFFER,
272 Wb35Rx_Complete, hw);
274 pWb35Rx->EP3vm_state = VM_RUNNING;
276 retv = usb_submit_urb(urb, GFP_ATOMIC);
279 dev_info(&hw->wiphy->dev, "Rx URB sending error\n");
286 pWb35Rx->EP3vm_state = VM_STOP;
287 atomic_dec(&pWb35Rx->RxFireCounter);
290 void Wb35Rx_start(struct ieee80211_hw *hw)
292 struct wbsoft_priv *priv = hw->priv;
293 struct hw_data *pHwData = &priv->sHwData;
294 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
296 /* Allow only one thread to run into the Wb35Rx() function */
297 if (atomic_inc_return(&pWb35Rx->RxFireCounter) == 1) {
298 pWb35Rx->EP3vm_state = VM_RUNNING;
301 atomic_dec(&pWb35Rx->RxFireCounter);
304 static void Wb35Rx_reset_descriptor(struct hw_data *pHwData)
306 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
309 pWb35Rx->ByteReceived = 0;
310 pWb35Rx->RxProcessIndex = 0;
311 pWb35Rx->RxBufferId = 0;
312 pWb35Rx->EP3vm_state = VM_STOP;
313 pWb35Rx->rx_halt = 0;
315 /* Initial the Queue. The last buffer is reserved for used
316 * if the Rx resource is unavailable.
318 for (i = 0; i < MAX_USB_RX_BUFFER_NUMBER; i++)
319 pWb35Rx->RxOwner[i] = 1;
322 unsigned char Wb35Rx_initial(struct hw_data *pHwData)
324 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
326 /* Initial the Buffer Queue */
327 Wb35Rx_reset_descriptor(pHwData);
329 pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
330 return !!pWb35Rx->RxUrb;
333 void Wb35Rx_stop(struct hw_data *pHwData)
335 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
337 /* Canceling the Irp if already sends it out. */
338 if (pWb35Rx->EP3vm_state == VM_RUNNING) {
339 /* Only use unlink, let Wb35Rx_destroy to free them */
340 usb_unlink_urb(pWb35Rx->RxUrb);
341 pr_debug("EP3 Rx stop\n");
345 /* Needs process context */
346 void Wb35Rx_destroy(struct hw_data *pHwData)
348 struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
351 msleep(10); /* Delay for waiting function enter */
352 } while (pWb35Rx->EP3vm_state != VM_STOP);
353 msleep(10); /* Delay for waiting function exit */
355 usb_free_urb(pWb35Rx->RxUrb);
356 pr_debug("Wb35Rx_destroy OK\n");