]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/winbond/linux/wb35rx.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6-omap-h63xx.git] / drivers / staging / winbond / linux / 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 "sysdef.h"
12
13
14 void Wb35Rx_start(phw_data_t pHwData)
15 {
16         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
17
18         // Allow only one thread to run into the Wb35Rx() function
19         if (OS_ATOMIC_INC(pHwData->Adapter, &pWb35Rx->RxFireCounter) == 1) {
20                 pWb35Rx->EP3vm_state = VM_RUNNING;
21                 Wb35Rx(pHwData);
22         } else
23                 OS_ATOMIC_DEC(pHwData->Adapter, &pWb35Rx->RxFireCounter);
24 }
25
26 // This function cannot reentrain
27 void Wb35Rx(  phw_data_t pHwData )
28 {
29         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
30         u8 *    pRxBufferAddress;
31         PURB    pUrb = (PURB)pWb35Rx->RxUrb;
32         int     retv;
33         u32     RxBufferId;
34
35         //
36         // Issuing URB
37         //
38         if (pHwData->SurpriseRemove || pHwData->HwStop)
39                 goto error;
40
41         if (pWb35Rx->rx_halt)
42                 goto error;
43
44         // Get RxBuffer's ID
45         RxBufferId = pWb35Rx->RxBufferId;
46         if (!pWb35Rx->RxOwner[RxBufferId]) {
47                 // It's impossible to run here.
48                 #ifdef _PE_RX_DUMP_
49                 WBDEBUG(("Rx driver fifo unavailable\n"));
50                 #endif
51                 goto error;
52         }
53
54         // Update buffer point, then start to bulkin the data from USB
55         pWb35Rx->RxBufferId++;
56         pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
57
58         pWb35Rx->CurrentRxBufferId = RxBufferId;
59
60         if (1 != OS_MEMORY_ALLOC((void* *)&pWb35Rx->pDRx, MAX_USB_RX_BUFFER)) {
61                 printk("w35und: Rx memory alloc failed\n");
62                 goto error;
63         }
64         pRxBufferAddress = pWb35Rx->pDRx;
65
66         usb_fill_bulk_urb(pUrb, pHwData->WbUsb.udev,
67                           usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
68                           pRxBufferAddress, MAX_USB_RX_BUFFER,
69                           Wb35Rx_Complete, pHwData);
70
71         pWb35Rx->EP3vm_state = VM_RUNNING;
72
73         retv = wb_usb_submit_urb(pUrb);
74
75         if (retv != 0) {
76                 printk("Rx URB sending error\n");
77                 goto error;
78         }
79         return;
80
81 error:
82         // VM stop
83         pWb35Rx->EP3vm_state = VM_STOP;
84         OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
85 }
86
87 void Wb35Rx_Complete(PURB pUrb)
88 {
89         phw_data_t      pHwData = pUrb->context;
90         PWB35RX         pWb35Rx = &pHwData->Wb35Rx;
91         u8 *            pRxBufferAddress;
92         u32             SizeCheck;
93         u16             BulkLength;
94         u32             RxBufferId;
95         R00_DESCRIPTOR  R00;
96
97         // Variable setting
98         pWb35Rx->EP3vm_state = VM_COMPLETED;
99         pWb35Rx->EP3VM_status = pUrb->status;//Store the last result of Irp
100
101         RxBufferId = pWb35Rx->CurrentRxBufferId;
102
103         pRxBufferAddress = pWb35Rx->pDRx;
104         BulkLength = (u16)pUrb->actual_length;
105
106         // The IRP is completed
107         pWb35Rx->EP3vm_state = VM_COMPLETED;
108
109         if (pHwData->SurpriseRemove || pHwData->HwStop) // Must be here, or RxBufferId is invalid
110                 goto error;
111
112         if (pWb35Rx->rx_halt)
113                 goto error;
114
115         // Start to process the data only in successful condition
116         pWb35Rx->RxOwner[ RxBufferId ] = 0; // Set the owner to driver
117         R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
118
119         // The URB is completed, check the result
120         if (pWb35Rx->EP3VM_status != 0) {
121                 #ifdef _PE_USB_STATE_DUMP_
122                 WBDEBUG(("EP3 IoCompleteRoutine return error\n"));
123                 DebugUsbdStatusInformation( pWb35Rx->EP3VM_status );
124                 #endif
125                 pWb35Rx->EP3vm_state = VM_STOP;
126                 goto error;
127         }
128
129         // 20060220 For recovering. check if operating in single USB mode
130         if (!HAL_USB_MODE_BURST(pHwData)) {
131                 SizeCheck = R00.R00_receive_byte_count;  //20060926 anson's endian
132                 if ((SizeCheck & 0x03) > 0)
133                         SizeCheck -= 4;
134                 SizeCheck = (SizeCheck + 3) & ~0x03;
135                 SizeCheck += 12; // 8 + 4 badbeef
136                 if ((BulkLength > 1600) ||
137                         (SizeCheck > 1600) ||
138                         (BulkLength != SizeCheck) ||
139                         (BulkLength == 0)) { // Add for fail Urb
140                         pWb35Rx->EP3vm_state = VM_STOP;
141                         pWb35Rx->Ep3ErrorCount2++;
142                 }
143         }
144
145         // Indicating the receiving data
146         pWb35Rx->ByteReceived += BulkLength;
147         pWb35Rx->RxBufferSize[ RxBufferId ] = BulkLength;
148
149         if (!pWb35Rx->RxOwner[ RxBufferId ])
150                 Wb35Rx_indicate(pHwData);
151
152         kfree(pWb35Rx->pDRx);
153         // Do the next receive
154         Wb35Rx(pHwData);
155         return;
156
157 error:
158         pWb35Rx->RxOwner[ RxBufferId ] = 1; // Set the owner to hardware
159         OS_ATOMIC_DEC( pHwData->Adapter, &pWb35Rx->RxFireCounter );
160         pWb35Rx->EP3vm_state = VM_STOP;
161 }
162
163 //=====================================================================================
164 unsigned char Wb35Rx_initial(phw_data_t pHwData)
165 {
166         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
167
168         // Initial the Buffer Queue
169         Wb35Rx_reset_descriptor( pHwData );
170
171         pWb35Rx->RxUrb = wb_usb_alloc_urb(0);
172         return (!!pWb35Rx->RxUrb);
173 }
174
175 void Wb35Rx_stop(phw_data_t pHwData)
176 {
177         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
178
179         // Canceling the Irp if already sends it out.
180         if (pWb35Rx->EP3vm_state == VM_RUNNING) {
181                 usb_unlink_urb( pWb35Rx->RxUrb ); // Only use unlink, let Wb35Rx_destroy to free them
182                 #ifdef _PE_RX_DUMP_
183                 WBDEBUG(("EP3 Rx stop\n"));
184                 #endif
185         }
186 }
187
188 // Needs process context
189 void Wb35Rx_destroy(phw_data_t pHwData)
190 {
191         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
192
193         do {
194                 OS_SLEEP(10000); // Delay for waiting function enter 940623.1.a
195         } while (pWb35Rx->EP3vm_state != VM_STOP);
196         OS_SLEEP(10000); // Delay for waiting function exit 940623.1.b
197
198         if (pWb35Rx->RxUrb)
199                 usb_free_urb( pWb35Rx->RxUrb );
200         #ifdef _PE_RX_DUMP_
201         WBDEBUG(("Wb35Rx_destroy OK\n"));
202         #endif
203 }
204
205 void Wb35Rx_reset_descriptor(  phw_data_t pHwData )
206 {
207         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
208         u32     i;
209
210         pWb35Rx->ByteReceived = 0;
211         pWb35Rx->RxProcessIndex = 0;
212         pWb35Rx->RxBufferId = 0;
213         pWb35Rx->EP3vm_state = VM_STOP;
214         pWb35Rx->rx_halt = 0;
215
216         // Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable.
217         for( i=0; i<MAX_USB_RX_BUFFER_NUMBER; i++ )
218                 pWb35Rx->RxOwner[i] = 1;
219 }
220
221 void Wb35Rx_adjust(PDESCRIPTOR pRxDes)
222 {
223         u32 *   pRxBufferAddress;
224         u32     DecryptionMethod;
225         u32     i;
226         u16     BufferSize;
227
228         DecryptionMethod = pRxDes->R01.R01_decryption_method;
229         pRxBufferAddress = pRxDes->buffer_address[0];
230         BufferSize = pRxDes->buffer_size[0];
231
232         // Adjust the last part of data. Only data left
233         BufferSize -= 4; // For CRC-32
234         if (DecryptionMethod)
235                 BufferSize -= 4;
236         if (DecryptionMethod == 3) // For CCMP
237                 BufferSize -= 4;
238
239         // Adjust the IV field which after 802.11 header and ICV field.
240         if (DecryptionMethod == 1) // For WEP
241         {
242                 for( i=6; i>0; i-- )
243                         pRxBufferAddress[i] = pRxBufferAddress[i-1];
244                 pRxDes->buffer_address[0] = pRxBufferAddress + 1;
245                 BufferSize -= 4; // 4 byte for IV
246         }
247         else if( DecryptionMethod ) // For TKIP and CCMP
248         {
249                 for (i=7; i>1; i--)
250                         pRxBufferAddress[i] = pRxBufferAddress[i-2];
251                 pRxDes->buffer_address[0] = pRxBufferAddress + 2;//Update the descriptor, shift 8 byte
252                 BufferSize -= 8; // 8 byte for IV + ICV
253         }
254         pRxDes->buffer_size[0] = BufferSize;
255 }
256
257 extern void packet_came(char *pRxBufferAddress, int PacketSize);
258
259
260 u16 Wb35Rx_indicate(phw_data_t pHwData)
261 {
262         DESCRIPTOR      RxDes;
263         PWB35RX pWb35Rx = &pHwData->Wb35Rx;
264         u8 *            pRxBufferAddress;
265         u16             PacketSize;
266         u16             stmp, BufferSize, stmp2 = 0;
267         u32             RxBufferId;
268
269         // Only one thread be allowed to run into the following
270         do {
271                 RxBufferId = pWb35Rx->RxProcessIndex;
272                 if (pWb35Rx->RxOwner[ RxBufferId ]) //Owner by VM
273                         break;
274
275                 pWb35Rx->RxProcessIndex++;
276                 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
277
278                 pRxBufferAddress = pWb35Rx->pDRx;
279                 BufferSize = pWb35Rx->RxBufferSize[ RxBufferId ];
280
281                 // Parse the bulkin buffer
282                 while (BufferSize >= 4) {
283                         if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) //Is ending? 921002.9.a
284                                 break;
285
286                         // Get the R00 R01 first
287                         RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
288                         PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
289                         RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress+4)));
290                         // For new DMA 4k
291                         if ((PacketSize & 0x03) > 0)
292                                 PacketSize -= 4;
293
294                         // Basic check for Rx length. Is length valid?
295                         if (PacketSize > MAX_PACKET_SIZE) {
296                                 #ifdef _PE_RX_DUMP_
297                                 WBDEBUG(("Serious ERROR : Rx data size too long, size =%d\n", PacketSize));
298                                 #endif
299
300                                 pWb35Rx->EP3vm_state = VM_STOP;
301                                 pWb35Rx->Ep3ErrorCount2++;
302                                 break;
303                         }
304
305                         // Start to process Rx buffer
306 //                      RxDes.Descriptor_ID = RxBufferId; // Due to synchronous indicate, the field doesn't necessary to use.
307                         BufferSize -= 8; //subtract 8 byte for 35's USB header length
308                         pRxBufferAddress += 8;
309
310                         RxDes.buffer_address[0] = pRxBufferAddress;
311                         RxDes.buffer_size[0] = PacketSize;
312                         RxDes.buffer_number = 1;
313                         RxDes.buffer_start_index = 0;
314                         RxDes.buffer_total_size = RxDes.buffer_size[0];
315                         Wb35Rx_adjust(&RxDes);
316
317                         packet_came(pRxBufferAddress, PacketSize);
318
319                         // Move RxBuffer point to the next
320                         stmp = PacketSize + 3;
321                         stmp &= ~0x03; // 4n alignment
322                         pRxBufferAddress += stmp;
323                         BufferSize -= stmp;
324                         stmp2 += stmp;
325                 }
326
327                 // Reclaim resource
328                 pWb35Rx->RxOwner[ RxBufferId ] = 1;
329         } while(TRUE);
330
331         return stmp2;
332 }
333
334