ARM: OMAP3: fix dpll4_m3_ck and dpll4_m4_ck dividers
[cascardo/linux.git] / drivers / staging / vt6656 / wctl.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: wctl.c
20  *
21  * Purpose: handle WMAC duplicate filter & defragment
22  *
23  * Author: Jerry Chen
24  *
25  * Date: Jun. 27, 2002
26  *
27  * Functions:
28  *      WCTLbIsDuplicate - Test if duplicate packet
29  *      WCTLuSearchDFCB - Search DeFragment Control Database
30  *      WCTLuInsertDFCB - Insert DeFragment Control Database
31  *      WCTLbHandleFragment - Handle received fragment packet
32  *
33  * Revision History:
34  *
35  */
36
37 #include "wctl.h"
38 #include "device.h"
39 #include "card.h"
40 #include "tmacro.h"
41
42 // static int          msglevel                =MSG_LEVEL_INFO;
43
44 /*
45  * Description:
46  *      Scan Rx cache.  Return true if packet is duplicate, else
47  *      inserts in receive cache and returns false.
48  *
49  * Parameters:
50  *  In:
51  *      pCache      - Receive packets history
52  *      pMACHeader  - 802.11 MAC Header of received packet
53  *  Out:
54  *      none
55  *
56  * Return Value: true if packet duplicate; otherwise false
57  *
58  */
59
60 bool WCTLbIsDuplicate (PSCache pCache, struct ieee80211_hdr *pMACHeader)
61 {
62     unsigned int            uIndex;
63     unsigned int            ii;
64     PSCacheEntry    pCacheEntry;
65
66     if (IS_FC_RETRY(pMACHeader)) {
67
68         uIndex = pCache->uInPtr;
69         for (ii = 0; ii < DUPLICATE_RX_CACHE_LENGTH; ii++) {
70             pCacheEntry = &(pCache->asCacheEntry[uIndex]);
71             if ((pCacheEntry->wFmSequence == pMACHeader->seq_ctrl) &&
72                 (!compare_ether_addr(&(pCacheEntry->abyAddr2[0]),
73                                      &(pMACHeader->addr2[0]))) &&
74                 (LOBYTE(pCacheEntry->wFrameCtl) == LOBYTE(pMACHeader->frame_control))
75                 ) {
76                 /* Duplicate match */
77                 return true;
78             }
79             ADD_ONE_WITH_WRAP_AROUND(uIndex, DUPLICATE_RX_CACHE_LENGTH);
80         }
81     }
82     /* Not found in cache - insert */
83     pCacheEntry = &pCache->asCacheEntry[pCache->uInPtr];
84     pCacheEntry->wFmSequence = pMACHeader->seq_ctrl;
85     memcpy(&(pCacheEntry->abyAddr2[0]), &(pMACHeader->addr2[0]), ETH_ALEN);
86     pCacheEntry->wFrameCtl = pMACHeader->frame_control;
87     ADD_ONE_WITH_WRAP_AROUND(pCache->uInPtr, DUPLICATE_RX_CACHE_LENGTH);
88     return false;
89 }
90
91 /*
92  * Description:
93  *      Found if sequence number of received fragment packet in Defragment Database
94  *
95  * Parameters:
96  *  In:
97  *      pDevice     - Pointer to adapter
98  *      pMACHeader  - 802.11 MAC Header of received packet
99  *  Out:
100  *      none
101  *
102  * Return Value: index number in Defragment Database
103  *
104  */
105
106 unsigned int WCTLuSearchDFCB(struct vnt_private *pDevice,
107                              struct ieee80211_hdr *pMACHeader)
108 {
109         unsigned int ii;
110
111         for (ii = 0; ii < pDevice->cbDFCB; ii++) {
112                 if ((pDevice->sRxDFCB[ii].bInUse == true) &&
113                     (!compare_ether_addr(&(pDevice->sRxDFCB[ii].abyAddr2[0]),
114                                           &(pMACHeader->addr2[0])))) {
115                         return ii;
116                 }
117         }
118         return pDevice->cbDFCB;
119 }
120
121 /*
122  * Description:
123  *      Insert received fragment packet in Defragment Database
124  *
125  * Parameters:
126  *  In:
127  *      pDevice     - Pointer to adapter
128  *      pMACHeader  - 802.11 MAC Header of received packet
129  *  Out:
130  *      none
131  *
132  * Return Value: index number in Defragment Database
133  *
134  */
135 unsigned int WCTLuInsertDFCB(struct vnt_private *pDevice,
136                              struct ieee80211_hdr *pMACHeader)
137 {
138         unsigned int ii;
139
140     if (pDevice->cbFreeDFCB == 0)
141         return(pDevice->cbDFCB);
142     for (ii = 0; ii < pDevice->cbDFCB; ii++) {
143         if (pDevice->sRxDFCB[ii].bInUse == false) {
144             pDevice->cbFreeDFCB--;
145             pDevice->sRxDFCB[ii].uLifetime = pDevice->dwMaxReceiveLifetime;
146             pDevice->sRxDFCB[ii].bInUse = true;
147             pDevice->sRxDFCB[ii].wSequence = (pMACHeader->seq_ctrl >> 4);
148             pDevice->sRxDFCB[ii].wFragNum = (pMACHeader->seq_ctrl & 0x000F);
149             memcpy(&(pDevice->sRxDFCB[ii].abyAddr2[0]),
150                    &(pMACHeader->addr2[0]),
151                    ETH_ALEN);
152             return(ii);
153         }
154     }
155     return(pDevice->cbDFCB);
156 }
157
158 /*
159  * Description:
160  *      Handle received fragment packet
161  *
162  * Parameters:
163  *  In:
164  *      pDevice         - Pointer to adapter
165  *      pMACHeader      - 802.11 MAC Header of received packet
166  *      cbFrameLength   - Frame length
167  *      bWEP            - is WEP packet
168  *  Out:
169  *      none
170  *
171  * Return Value: true if it is valid fragment packet and we have resource to defragment; otherwise false
172  *
173  */
174 bool WCTLbHandleFragment(struct vnt_private *pDevice, struct ieee80211_hdr *pMACHeader, unsigned int cbFrameLength, bool bWEP, bool bExtIV)
175 {
176         unsigned int uHeaderSize;
177
178     if (bWEP == true) {
179         uHeaderSize = 28;
180         if (bExtIV)
181         // ExtIV
182             uHeaderSize +=4;
183     }
184     else {
185         uHeaderSize = 24;
186     }
187
188     if (IS_FIRST_FRAGMENT_PKT(pMACHeader)) {
189         pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
190         if (pDevice->uCurrentDFCBIdx < pDevice->cbDFCB) {
191             // duplicate, we must flush previous DCB
192             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].uLifetime = pDevice->dwMaxReceiveLifetime;
193             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence = (pMACHeader->seq_ctrl >> 4);
194             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum = (pMACHeader->seq_ctrl & 0x000F);
195         }
196         else {
197             pDevice->uCurrentDFCBIdx = WCTLuInsertDFCB(pDevice, pMACHeader);
198             if (pDevice->uCurrentDFCBIdx == pDevice->cbDFCB) {
199                 return(false);
200             }
201         }
202         // reserve 8 byte to match MAC RX Buffer
203         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (u8 *) (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 8);
204 //        pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer = (u8 *) (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb->data + 4);
205         memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, pMACHeader, cbFrameLength);
206         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength = cbFrameLength;
207         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += cbFrameLength;
208         pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
209         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "First pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
210         return(false);
211     }
212     else {
213         pDevice->uCurrentDFCBIdx = WCTLuSearchDFCB(pDevice, pMACHeader);
214         if (pDevice->uCurrentDFCBIdx != pDevice->cbDFCB) {
215             if ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wSequence == (pMACHeader->seq_ctrl >> 4)) &&
216                 (pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum == (pMACHeader->seq_ctrl & 0x000F)) &&
217                 ((pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength + cbFrameLength - uHeaderSize) < 2346)) {
218
219                 memcpy(pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer, ((u8 *) (pMACHeader) + uHeaderSize), (cbFrameLength - uHeaderSize));
220                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength += (cbFrameLength - uHeaderSize);
221                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].pbyRxBuffer += (cbFrameLength - uHeaderSize);
222                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].wFragNum++;
223                 //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Second pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
224             }
225             else {
226                 // seq error or frag # error flush DFCB
227                 pDevice->cbFreeDFCB++;
228                 pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
229                 return(false);
230             }
231         }
232         else {
233             return(false);
234         }
235         if (IS_LAST_FRAGMENT_PKT(pMACHeader)) {
236             //enq defragcontrolblock
237             pDevice->cbFreeDFCB++;
238             pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].bInUse = false;
239             //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Last pDevice->uCurrentDFCBIdx= %d\n", pDevice->uCurrentDFCBIdx);
240             return(true);
241         }
242         return(false);
243     }
244 }
245