Staging: w35und: remove some dead code
[pandora-kernel.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 "sysdef.h"
14 #include "wb35rx_f.h"
15
16 void Wb35Rx_start(phw_data_t pHwData)
17 {
18         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
19
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;
23                 Wb35Rx(pHwData);
24         } else
25                 atomic_dec(&pWb35Rx->RxFireCounter);
26 }
27
28 // This function cannot reentrain
29 void Wb35Rx(  phw_data_t pHwData )
30 {
31         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
32         u8 *    pRxBufferAddress;
33         struct urb *urb = pWb35Rx->RxUrb;
34         int     retv;
35         u32     RxBufferId;
36
37         //
38         // Issuing URB
39         //
40         if (pHwData->SurpriseRemove || pHwData->HwStop)
41                 goto error;
42
43         if (pWb35Rx->rx_halt)
44                 goto error;
45
46         // Get RxBuffer's ID
47         RxBufferId = pWb35Rx->RxBufferId;
48         if (!pWb35Rx->RxOwner[RxBufferId]) {
49                 // It's impossible to run here.
50                 #ifdef _PE_RX_DUMP_
51                 WBDEBUG(("Rx driver fifo unavailable\n"));
52                 #endif
53                 goto error;
54         }
55
56         // Update buffer point, then start to bulkin the data from USB
57         pWb35Rx->RxBufferId++;
58         pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
59
60         pWb35Rx->CurrentRxBufferId = RxBufferId;
61
62         pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
63         if (!pWb35Rx->pDRx) {
64                 printk("w35und: Rx memory alloc failed\n");
65                 goto error;
66         }
67         pRxBufferAddress = pWb35Rx->pDRx;
68
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);
73
74         pWb35Rx->EP3vm_state = VM_RUNNING;
75
76         retv = usb_submit_urb(urb, GFP_ATOMIC);
77
78         if (retv != 0) {
79                 printk("Rx URB sending error\n");
80                 goto error;
81         }
82         return;
83
84 error:
85         // VM stop
86         pWb35Rx->EP3vm_state = VM_STOP;
87         atomic_dec(&pWb35Rx->RxFireCounter);
88 }
89
90 void Wb35Rx_Complete(struct urb *urb)
91 {
92         phw_data_t      pHwData = urb->context;
93         PWB35RX         pWb35Rx = &pHwData->Wb35Rx;
94         u8 *            pRxBufferAddress;
95         u32             SizeCheck;
96         u16             BulkLength;
97         u32             RxBufferId;
98         R00_DESCRIPTOR  R00;
99
100         // Variable setting
101         pWb35Rx->EP3vm_state = VM_COMPLETED;
102         pWb35Rx->EP3VM_status = urb->status;//Store the last result of Irp
103
104         RxBufferId = pWb35Rx->CurrentRxBufferId;
105
106         pRxBufferAddress = pWb35Rx->pDRx;
107         BulkLength = (u16)urb->actual_length;
108
109         // The IRP is completed
110         pWb35Rx->EP3vm_state = VM_COMPLETED;
111
112         if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
113                 goto error;
114
115         if (pWb35Rx->rx_halt)
116                 goto error;
117
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);
121
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 );
127                 #endif
128                 pWb35Rx->EP3vm_state = VM_STOP;
129                 goto error;
130         }
131
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)
136                         SizeCheck -= 4;
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++;
145                 }
146         }
147
148         // Indicating the receiving data
149         pWb35Rx->ByteReceived += BulkLength;
150         pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
151
152         if (!pWb35Rx->RxOwner[ RxBufferId ])
153                 Wb35Rx_indicate(pHwData);
154
155         kfree(pWb35Rx->pDRx);
156         // Do the next receive
157         Wb35Rx(pHwData);
158         return;
159
160 error:
161         pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
162         atomic_dec(&pWb35Rx->RxFireCounter);
163         pWb35Rx->EP3vm_state = VM_STOP;
164 }
165
166 //=====================================================================================
167 unsigned char Wb35Rx_initial(phw_data_t pHwData)
168 {
169         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
170
171         // Initial the Buffer Queue
172         Wb35Rx_reset_descriptor( pHwData );
173
174         pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
175         return (!!pWb35Rx->RxUrb);
176 }
177
178 void Wb35Rx_stop(phw_data_t pHwData)
179 {
180         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
181
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
185                 #ifdef _PE_RX_DUMP_
186                 WBDEBUG(("EP3 Rx stop\n"));
187                 #endif
188         }
189 }
190
191 // Needs process context
192 void Wb35Rx_destroy(phw_data_t pHwData)
193 {
194         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
195
196         do {
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
200
201         if (pWb35Rx->RxUrb)
202                 usb_free_urb( pWb35Rx->RxUrb );
203         #ifdef _PE_RX_DUMP_
204         WBDEBUG(("Wb35Rx_destroy OK\n"));
205         #endif
206 }
207
208 void Wb35Rx_reset_descriptor(  phw_data_t pHwData )
209 {
210         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
211         u32     i;
212
213         pWb35Rx->ByteReceived = 0;
214         pWb35Rx->RxProcessIndex = 0;
215         pWb35Rx->RxBufferId = 0;
216         pWb35Rx->EP3vm_state = VM_STOP;
217         pWb35Rx->rx_halt = 0;
218
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;
222 }
223
224 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
225 {
226         u32 *   pRxBufferAddress;
227         u32     DecryptionMethod;
228         u32     i;
229         u16     BufferSize;
230
231         DecryptionMethod = pRxDes->R01.R01_decryption_method;
232         pRxBufferAddress = pRxDes->buffer_address[0];
233         BufferSize = pRxDes->buffer_size[0];
234
235         // Adjust the last part of data. Only data left
236         BufferSize -= 4; // For CRC-32
237         if (DecryptionMethod)
238                 BufferSize -= 4;
239         if (DecryptionMethod == 3) // For CCMP
240                 BufferSize -= 4;
241
242         // Adjust the IV field which after 802.11 header and ICV field.
243         if (DecryptionMethod == 1) // For WEP
244         {
245                 for( i=6; i>0; i-- )
246                         pRxBufferAddress[i] = pRxBufferAddress[i-1];
247                 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
248                 BufferSize -= 4; // 4 byte for IV
249         }
250         else if( DecryptionMethod ) // For TKIP and CCMP
251         {
252                 for (i=7; i>1; i--)
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
256         }
257         pRxDes->buffer_size[0] = BufferSize;
258 }
259
260 extern void packet_came(char *pRxBufferAddress, int PacketSize);
261
262
263 u16 Wb35Rx_indicate(phw_data_t pHwData)
264 {
265         DESCRIPTOR      RxDes;
266         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
267         u8 *            pRxBufferAddress;
268         u16             PacketSize;
269         u16             stmp, BufferSize, stmp2 = 0;
270         u32             RxBufferId;
271
272         // Only one thread be allowed to run into the following
273         do {
274                 RxBufferId = pWb35Rx->RxProcessIndex;
275                 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
276                         break;
277
278                 pWb35Rx->RxProcessIndex++;
279                 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
280
281                 pRxBufferAddress = pWb35Rx->pDRx;
282                 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
283
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
287                                 break;
288
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)));
293                         // For new DMA 4k
294                         if ((PacketSize & 0x03) > 0)
295                                 PacketSize -= 4;
296
297                         // Basic check for Rx length. Is length valid?
298                         if (PacketSize > MAX_PACKET_SIZE) {
299                                 #ifdef _PE_RX_DUMP_
300                                 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
301                                 #endif
302
303                                 pWb35Rx->EP3vm_state = VM_STOP;
304                                 pWb35Rx->Ep3ErrorCount2++;
305                                 break;
306                         }
307
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;
312
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);
319
320                         packet_came(pRxBufferAddress, PacketSize);
321
322                         // Move RxBuffer point to the next
323                         stmp = PacketSize + 3;
324                         stmp &= ~0x03; // 4n alignment
325                         pRxBufferAddress += stmp;
326                         BufferSize -= stmp;
327                         stmp2 += stmp;
328                 }
329
330                 // Reclaim resource
331                 pWb35Rx->RxOwner[ RxBufferId ] = 1;
332         } while (true);
333
334         return stmp2;
335 }
336
337