]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/winbond/wb35rx.c
Staging: w35und: Use pr_debug() for debugging
[mv-sheeva.git] / drivers / staging / winbond / wb35rx.c
1 /*
2  * ============================================================================
3  *  Copyright (c) 1996-2002 Winbond Electronic Corporation
4  *
5  *  Module Name:
6  *    Wb35Rx.c
7  *
8  *  Abstract:
9  *    Processing the Rx message from down layer
10  *
11  * ============================================================================
12  */
13 #include <linux/usb.h>
14 #include <linux/slab.h>
15
16 #include "core.h"
17 #include "sysdef.h"
18 #include "wb35rx_f.h"
19
20 static void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize)
21 {
22         struct wbsoft_priv *priv = hw->priv;
23         struct sk_buff *skb;
24         struct ieee80211_rx_status rx_status = {0};
25
26         if (!priv->enabled)
27                 return;
28
29         skb = dev_alloc_skb(PacketSize);
30         if (!skb) {
31                 printk("Not enough memory for packet, FIXME\n");
32                 return;
33         }
34
35         memcpy(skb_put(skb, PacketSize), pRxBufferAddress, PacketSize);
36
37         memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
38         ieee80211_rx_irqsafe(hw, skb);
39 }
40
41 static void Wb35Rx_adjust(struct wb35_descriptor *pRxDes)
42 {
43         u32     *pRxBufferAddress;
44         u32     DecryptionMethod;
45         u32     i;
46         u16     BufferSize;
47
48         DecryptionMethod = pRxDes->R01.R01_decryption_method;
49         pRxBufferAddress = pRxDes->buffer_address[0];
50         BufferSize = pRxDes->buffer_size[0];
51
52         /* Adjust the last part of data. Only data left */
53         BufferSize -= 4; /* For CRC-32 */
54         if (DecryptionMethod)
55                 BufferSize -= 4;
56         if (DecryptionMethod == 3) /* For CCMP */
57                 BufferSize -= 4;
58
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                 pRxDes->buffer_address[0] = pRxBufferAddress + 2; /* Update the descriptor, shift 8 byte */
69                 BufferSize -= 8; /* 8 byte for IV + ICV */
70         }
71         pRxDes->buffer_size[0] = BufferSize;
72 }
73
74 static u16 Wb35Rx_indicate(struct ieee80211_hw *hw)
75 {
76         struct wbsoft_priv      *priv = hw->priv;
77         struct hw_data          *pHwData = &priv->sHwData;
78         struct wb35_descriptor  RxDes;
79         struct wb35_rx          *pWb35Rx = &pHwData->Wb35Rx;
80         u8                      *pRxBufferAddress;
81         u16                     PacketSize;
82         u16                     stmp, BufferSize, stmp2 = 0;
83         u32                     RxBufferId;
84
85         /* Only one thread be allowed to run into the following */
86         do {
87                 RxBufferId = pWb35Rx->RxProcessIndex;
88                 if (pWb35Rx->RxOwner[RxBufferId]) /* Owner by VM */
89                         break;
90
91                 pWb35Rx->RxProcessIndex++;
92                 pWb35Rx->RxProcessIndex %= MAX_USB_RX_BUFFER_NUMBER;
93
94                 pRxBufferAddress = pWb35Rx->pDRx;
95                 BufferSize = pWb35Rx->RxBufferSize[RxBufferId];
96
97                 /* Parse the bulkin buffer */
98                 while (BufferSize >= 4) {
99                         if ((cpu_to_le32(*(u32 *)pRxBufferAddress) & 0x0fffffff) == RX_END_TAG) /* Is ending? */
100                                 break;
101
102                         /* Get the R00 R01 first */
103                         RxDes.R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
104                         PacketSize = (u16)RxDes.R00.R00_receive_byte_count;
105                         RxDes.R01.value = le32_to_cpu(*((u32 *)(pRxBufferAddress + 4)));
106                         /* For new DMA 4k */
107                         if ((PacketSize & 0x03) > 0)
108                                 PacketSize -= 4;
109
110                         /* Basic check for Rx length. Is length valid? */
111                         if (PacketSize > MAX_PACKET_SIZE) {
112                                 pr_debug("Serious ERROR : Rx data size too long, size =%d\n", PacketSize);
113                                 pWb35Rx->EP3vm_state = VM_STOP;
114                                 pWb35Rx->Ep3ErrorCount2++;
115                                 break;
116                         }
117
118                         /*
119                          * Wb35Rx_indicate() is called synchronously so it isn't
120                          * necessary to set "RxDes.Desctriptor_ID = RxBufferID;"
121                          */
122                         BufferSize -= 8; /* subtract 8 byte for 35's USB header length */
123                         pRxBufferAddress += 8;
124
125                         RxDes.buffer_address[0] = pRxBufferAddress;
126                         RxDes.buffer_size[0] = PacketSize;
127                         RxDes.buffer_number = 1;
128                         RxDes.buffer_start_index = 0;
129                         RxDes.buffer_total_size = RxDes.buffer_size[0];
130                         Wb35Rx_adjust(&RxDes);
131
132                         packet_came(hw, pRxBufferAddress, PacketSize);
133
134                         /* Move RxBuffer point to the next */
135                         stmp = PacketSize + 3;
136                         stmp &= ~0x03; /* 4n alignment */
137                         pRxBufferAddress += stmp;
138                         BufferSize -= stmp;
139                         stmp2 += stmp;
140                 }
141
142                 /* Reclaim resource */
143                 pWb35Rx->RxOwner[RxBufferId] = 1;
144         } while (true);
145         return stmp2;
146 }
147
148 static void Wb35Rx(struct ieee80211_hw *hw);
149
150 static void Wb35Rx_Complete(struct urb *urb)
151 {
152         struct ieee80211_hw     *hw = urb->context;
153         struct wbsoft_priv      *priv = hw->priv;
154         struct hw_data          *pHwData = &priv->sHwData;
155         struct wb35_rx          *pWb35Rx = &pHwData->Wb35Rx;
156         u8                      *pRxBufferAddress;
157         u32                     SizeCheck;
158         u16                     BulkLength;
159         u32                     RxBufferId;
160         struct R00_descriptor           R00;
161
162         /* Variable setting */
163         pWb35Rx->EP3vm_state = VM_COMPLETED;
164         pWb35Rx->EP3VM_status = urb->status; /* Store the last result of Irp */
165
166         RxBufferId = pWb35Rx->CurrentRxBufferId;
167
168         pRxBufferAddress = pWb35Rx->pDRx;
169         BulkLength = (u16)urb->actual_length;
170
171         /* The IRP is completed */
172         pWb35Rx->EP3vm_state = VM_COMPLETED;
173
174         if (pHwData->SurpriseRemove) /* Must be here, or RxBufferId is invalid */
175                 goto error;
176
177         if (pWb35Rx->rx_halt)
178                 goto error;
179
180         /* Start to process the data only in successful condition */
181         pWb35Rx->RxOwner[RxBufferId] = 0; /* Set the owner to driver */
182         R00.value = le32_to_cpu(*(u32 *)pRxBufferAddress);
183
184         /* The URB is completed, check the result */
185         if (pWb35Rx->EP3VM_status != 0) {
186                 pr_debug("EP3 IoCompleteRoutine return error\n");
187                 pWb35Rx->EP3vm_state = VM_STOP;
188                 goto error;
189         }
190
191         /* For recovering. check if operating in single USB mode */
192         if (!HAL_USB_MODE_BURST(pHwData)) {
193                 SizeCheck = R00.R00_receive_byte_count;
194                 if ((SizeCheck & 0x03) > 0)
195                         SizeCheck -= 4;
196                 SizeCheck = (SizeCheck + 3) & ~0x03;
197                 SizeCheck += 12; /* 8 + 4 badbeef */
198                 if ((BulkLength > 1600) ||
199                         (SizeCheck > 1600) ||
200                         (BulkLength != SizeCheck) ||
201                         (BulkLength == 0)) { /* Add for fail Urb */
202                         pWb35Rx->EP3vm_state = VM_STOP;
203                         pWb35Rx->Ep3ErrorCount2++;
204                 }
205         }
206
207         /* Indicating the receiving data */
208         pWb35Rx->ByteReceived += BulkLength;
209         pWb35Rx->RxBufferSize[RxBufferId] = BulkLength;
210
211         if (!pWb35Rx->RxOwner[RxBufferId])
212                 Wb35Rx_indicate(hw);
213
214         kfree(pWb35Rx->pDRx);
215         /* Do the next receive */
216         Wb35Rx(hw);
217         return;
218
219 error:
220         pWb35Rx->RxOwner[RxBufferId] = 1; /* Set the owner to hardware */
221         atomic_dec(&pWb35Rx->RxFireCounter);
222         pWb35Rx->EP3vm_state = VM_STOP;
223 }
224
225 /* This function cannot reentrain */
226 static void Wb35Rx(struct ieee80211_hw *hw)
227 {
228         struct wbsoft_priv      *priv = hw->priv;
229         struct hw_data          *pHwData = &priv->sHwData;
230         struct wb35_rx          *pWb35Rx = &pHwData->Wb35Rx;
231         u8                      *pRxBufferAddress;
232         struct urb              *urb = pWb35Rx->RxUrb;
233         int                     retv;
234         u32                     RxBufferId;
235
236         /* Issuing URB */
237         if (pHwData->SurpriseRemove)
238                 goto error;
239
240         if (pWb35Rx->rx_halt)
241                 goto error;
242
243         /* Get RxBuffer's ID */
244         RxBufferId = pWb35Rx->RxBufferId;
245         if (!pWb35Rx->RxOwner[RxBufferId]) {
246                 /* It's impossible to run here. */
247                 pr_debug("Rx driver fifo unavailable\n");
248                 goto error;
249         }
250
251         /* Update buffer point, then start to bulkin the data from USB */
252         pWb35Rx->RxBufferId++;
253         pWb35Rx->RxBufferId %= MAX_USB_RX_BUFFER_NUMBER;
254
255         pWb35Rx->CurrentRxBufferId = RxBufferId;
256
257         pWb35Rx->pDRx = kzalloc(MAX_USB_RX_BUFFER, GFP_ATOMIC);
258         if (!pWb35Rx->pDRx) {
259                 printk("w35und: Rx memory alloc failed\n");
260                 goto error;
261         }
262         pRxBufferAddress = pWb35Rx->pDRx;
263
264         usb_fill_bulk_urb(urb, pHwData->WbUsb.udev,
265                           usb_rcvbulkpipe(pHwData->WbUsb.udev, 3),
266                           pRxBufferAddress, MAX_USB_RX_BUFFER,
267                           Wb35Rx_Complete, hw);
268
269         pWb35Rx->EP3vm_state = VM_RUNNING;
270
271         retv = usb_submit_urb(urb, GFP_ATOMIC);
272
273         if (retv != 0) {
274                 printk("Rx URB sending error\n");
275                 goto error;
276         }
277         return;
278
279 error:
280         /* VM stop */
281         pWb35Rx->EP3vm_state = VM_STOP;
282         atomic_dec(&pWb35Rx->RxFireCounter);
283 }
284
285 void Wb35Rx_start(struct ieee80211_hw *hw)
286 {
287         struct wbsoft_priv      *priv = hw->priv;
288         struct hw_data          *pHwData = &priv->sHwData;
289         struct wb35_rx          *pWb35Rx = &pHwData->Wb35Rx;
290
291         /* Allow only one thread to run into the Wb35Rx() function */
292         if (atomic_inc_return(&pWb35Rx->RxFireCounter) == 1) {
293                 pWb35Rx->EP3vm_state = VM_RUNNING;
294                 Wb35Rx(hw);
295         } else
296                 atomic_dec(&pWb35Rx->RxFireCounter);
297 }
298
299 static void Wb35Rx_reset_descriptor(struct hw_data *pHwData)
300 {
301         struct wb35_rx  *pWb35Rx = &pHwData->Wb35Rx;
302         u32             i;
303
304         pWb35Rx->ByteReceived = 0;
305         pWb35Rx->RxProcessIndex = 0;
306         pWb35Rx->RxBufferId = 0;
307         pWb35Rx->EP3vm_state = VM_STOP;
308         pWb35Rx->rx_halt = 0;
309
310         /* Initial the Queue. The last buffer is reserved for used if the Rx resource is unavailable. */
311         for (i = 0; i < MAX_USB_RX_BUFFER_NUMBER; i++)
312                 pWb35Rx->RxOwner[i] = 1;
313 }
314
315 unsigned char Wb35Rx_initial(struct hw_data *pHwData)
316 {
317         struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
318
319         /* Initial the Buffer Queue */
320         Wb35Rx_reset_descriptor(pHwData);
321
322         pWb35Rx->RxUrb = usb_alloc_urb(0, GFP_ATOMIC);
323         return !!pWb35Rx->RxUrb;
324 }
325
326 void Wb35Rx_stop(struct hw_data *pHwData)
327 {
328         struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
329
330         /* Canceling the Irp if already sends it out. */
331         if (pWb35Rx->EP3vm_state == VM_RUNNING) {
332                 usb_unlink_urb(pWb35Rx->RxUrb); /* Only use unlink, let Wb35Rx_destroy to free them */
333                 pr_debug("EP3 Rx stop\n");
334         }
335 }
336
337 /* Needs process context */
338 void Wb35Rx_destroy(struct hw_data *pHwData)
339 {
340         struct wb35_rx *pWb35Rx = &pHwData->Wb35Rx;
341
342         do {
343                 msleep(10); /* Delay for waiting function enter */
344         } while (pWb35Rx->EP3vm_state != VM_STOP);
345         msleep(10); /* Delay for waiting function exit */
346
347         if (pWb35Rx->RxUrb)
348                 usb_free_urb(pWb35Rx->RxUrb);
349         pr_debug("Wb35Rx_destroy OK\n");
350 }
351