Staging: w35und: remove global struct ieee80211_hw
[firefly-linux-kernel-4.4.55.git] / drivers / staging / winbond / wb35rx.c
1 //============================================================================
2 //  Copyright (c) 1996-2002 Winbond Electronic Corporation
3 //
4 //  Module Name:
5 //    Wb35Rx.c
6 //
7 //  Abstract:
8 //    Processing the Rx message from down layer
9 //
10 //============================================================================
11 #include <linux/usb.h>
12
13 #include "core.h"
14 #include "sysdef.h"
15 #include "wb35rx_f.h"
16
17 void Wb35Rx_start(struct ieee80211_hw *hw)
18 {
19         struct wbsoft_priv *priv = hw->priv;
20         phw_data_t pHwData = &priv->sHwData;
21         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
22
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;
26                 Wb35Rx(hw);
27         } else
28                 atomic_dec(&pWb35Rx->RxFireCounter);
29 }
30
31 // This function cannot reentrain
32 void Wb35Rx(struct ieee80211_hw *hw)
33 {
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;
39         int     retv;
40         u32     RxBufferId;
41
42         //
43         // Issuing URB
44         //
45         if (pHwData->SurpriseRemove || pHwData->HwStop)
46                 goto error;
47
48         if (pWb35Rx->rx_halt)
49                 goto error;
50
51         // Get RxBuffer's ID
52         RxBufferId = pWb35Rx->RxBufferId;
53         if (!pWb35Rx->RxOwner[RxBufferId]) {
54                 // It's impossible to run here.
55                 #ifdef _PE_RX_DUMP_
56                 WBDEBUG(("Rx driver fifo unavailable\n"));
57                 #endif
58                 goto error;
59         }
60
61         // Update buffer point, then start to bulkin the data from USB
62         pWb35Rx->RxBufferId++;
63         pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
64
65         pWb35Rx->CurrentRxBufferId = RxBufferId;
66
67         pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
68         if (!pWb35Rx->pDRx) {
69                 printk("w35und: Rx memory alloc failed\n");
70                 goto error;
71         }
72         pRxBufferAddress = pWb35Rx->pDRx;
73
74         usb_fill_bulk_urb(urb, pHwData->WbUsb.udev,
75                           usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
76                           pRxBufferAddress, MAX_USB_RX_BUFFER,
77                           Wb35Rx_Complete, hw);
78
79         pWb35Rx->EP3vm_state = VM_RUNNING;
80
81         retv = usb_submit_urb(urb, GFP_ATOMIC);
82
83         if (retv != 0) {
84                 printk("Rx URB sending error\n");
85                 goto error;
86         }
87         return;
88
89 error:
90         // VM stop
91         pWb35Rx->EP3vm_state = VM_STOP;
92         atomic_dec(&pWb35Rx->RxFireCounter);
93 }
94
95 void Wb35Rx_Complete(struct urb *urb)
96 {
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;
102         u32             SizeCheck;
103         u16             BulkLength;
104         u32             RxBufferId;
105         R00_DESCRIPTOR  R00;
106
107         // Variable setting
108         pWb35Rx->EP3vm_state = VM_COMPLETED;
109         pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
110
111         RxBufferId = pWb35Rx->CurrentRxBufferId;
112
113         pRxBufferAddress = pWb35Rx->pDRx;
114         BulkLength = (u16)urb->actual_length;
115
116         // The IRP is completed
117         pWb35Rx->EP3vm_state = VM_COMPLETED;
118
119         if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
120                 goto error;
121
122         if (pWb35Rx->rx_halt)
123                 goto error;
124
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);
128
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 );
134                 #endif
135                 pWb35Rx->EP3vm_state = VM_STOP;
136                 goto error;
137         }
138
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)
143                         SizeCheck -= 4;
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++;
152                 }
153         }
154
155         // Indicating the receiving data
156         pWb35Rx->ByteReceived += BulkLength;
157         pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
158
159         if (!pWb35Rx->RxOwner[ RxBufferId ])
160                 Wb35Rx_indicate(hw);
161
162         kfree(pWb35Rx->pDRx);
163         // Do the next receive
164         Wb35Rx(hw);
165         return;
166
167 error:
168         pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
169         atomic_dec(&pWb35Rx->RxFireCounter);
170         pWb35Rx->EP3vm_state = VM_STOP;
171 }
172
173 //=====================================================================================
174 unsigned char Wb35Rx_initial(phw_data_t pHwData)
175 {
176         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
177
178         // Initial the Buffer Queue
179         Wb35Rx_reset_descriptor( pHwData );
180
181         pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
182         return (!!pWb35Rx->RxUrb);
183 }
184
185 void Wb35Rx_stop(phw_data_t pHwData)
186 {
187         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
188
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
192                 #ifdef _PE_RX_DUMP_
193                 WBDEBUG(("EP3 Rx stop\n"));
194                 #endif
195         }
196 }
197
198 // Needs process context
199 void Wb35Rx_destroy(phw_data_t pHwData)
200 {
201         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
202
203         do {
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
207
208         if (pWb35Rx->RxUrb)
209                 usb_free_urb( pWb35Rx->RxUrb );
210         #ifdef _PE_RX_DUMP_
211         WBDEBUG(("Wb35Rx_destroy OK\n"));
212         #endif
213 }
214
215 void Wb35Rx_reset_descriptor(  phw_data_t pHwData )
216 {
217         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
218         u32     i;
219
220         pWb35Rx->ByteReceived = 0;
221         pWb35Rx->RxProcessIndex = 0;
222         pWb35Rx->RxBufferId = 0;
223         pWb35Rx->EP3vm_state = VM_STOP;
224         pWb35Rx->rx_halt = 0;
225
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;
229 }
230
231 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
232 {
233         u32 *   pRxBufferAddress;
234         u32     DecryptionMethod;
235         u32     i;
236         u16     BufferSize;
237
238         DecryptionMethod = pRxDes->R01.R01_decryption_method;
239         pRxBufferAddress = pRxDes->buffer_address[0];
240         BufferSize = pRxDes->buffer_size[0];
241
242         // Adjust the last part of data. Only data left
243         BufferSize -= 4; // For CRC-32
244         if (DecryptionMethod)
245                 BufferSize -= 4;
246         if (DecryptionMethod == 3) // For CCMP
247                 BufferSize -= 4;
248
249         // Adjust the IV field which after 802.11 header and ICV field.
250         if (DecryptionMethod == 1) // For WEP
251         {
252                 for( i=6; i>0; i-- )
253                         pRxBufferAddress[i] = pRxBufferAddress[i-1];
254                 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
255                 BufferSize -= 4; // 4 byte for IV
256         }
257         else if( DecryptionMethod ) // For TKIP and CCMP
258         {
259                 for (i=7; i>1; i--)
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
263         }
264         pRxDes->buffer_size[0] = BufferSize;
265 }
266
267 extern void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize);
268
269
270 u16 Wb35Rx_indicate(struct ieee80211_hw *hw)
271 {
272         struct wbsoft_priv *priv = hw->priv;
273         phw_data_t pHwData = &priv->sHwData;
274         DESCRIPTOR      RxDes;
275         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
276         u8 *            pRxBufferAddress;
277         u16             PacketSize;
278         u16             stmp, BufferSize, stmp2 = 0;
279         u32             RxBufferId;
280
281         // Only one thread be allowed to run into the following
282         do {
283                 RxBufferId = pWb35Rx->RxProcessIndex;
284                 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
285                         break;
286
287                 pWb35Rx->RxProcessIndex++;
288                 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
289
290                 pRxBufferAddress = pWb35Rx->pDRx;
291                 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
292
293                 // Parse the bulkin buffer
294                 while (BufferSize >= 4) {
295                         if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
296                                 break;
297
298                         // Get the R00 R01 first
299                         RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
300                         PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
301                         RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
302                         // For new DMA 4k
303                         if ((PacketSize & 0x03) > 0)
304                                 PacketSize -= 4;
305
306                         // Basic check for Rx length. Is length valid?
307                         if (PacketSize > MAX_PACKET_SIZE) {
308                                 #ifdef _PE_RX_DUMP_
309                                 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
310                                 #endif
311
312                                 pWb35Rx->EP3vm_state = VM_STOP;
313                                 pWb35Rx->Ep3ErrorCount2++;
314                                 break;
315                         }
316
317                         // Start to process Rx buffer
318 //                      RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
319                         BufferSize -= 8; //subtract 8 byte for 35's USB header length
320                         pRxBufferAddress += 8;
321
322                         RxDes.buffer_address[0] = pRxBufferAddress;
323                         RxDes.buffer_size[0] = PacketSize;
324                         RxDes.buffer_number = 1;
325                         RxDes.buffer_start_index = 0;
326                         RxDes.buffer_total_size = RxDes.buffer_size[0];
327                         Wb35Rx_adjust(&RxDes);
328
329                         packet_came(hw, pRxBufferAddress, PacketSize);
330
331                         // Move RxBuffer point to the next
332                         stmp = PacketSize + 3;
333                         stmp &= ~0x03; // 4n alignment
334                         pRxBufferAddress += stmp;
335                         BufferSize -= stmp;
336                         stmp2 += stmp;
337                 }
338
339                 // Reclaim resource
340                 pWb35Rx->RxOwner[ RxBufferId ] = 1;
341         } while (true);
342
343         return stmp2;
344 }
345
346