Merge remote-tracking branches 'regulator/fix/da9211', 'regulator/fix/ltc3589' and...
[sfrench/cifs-2.6.git] / drivers / staging / vt6655 / hostap.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: hostap.c
20  *
21  * Purpose: handle hostap deamon ioctl input/out functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Oct. 20, 2003
26  *
27  * Functions:
28  *
29  * Revision History:
30  *
31  */
32
33 #include "hostap.h"
34 #include "iocmd.h"
35 #include "mac.h"
36 #include "card.h"
37 #include "baseband.h"
38 #include "wpactl.h"
39 #include "key.h"
40
41 #define VIAWGET_HOSTAPD_MAX_BUF_SIZE 1024
42 #define HOSTAP_CRYPT_FLAG_SET_TX_KEY BIT0
43 #define HOSTAP_CRYPT_ERR_UNKNOWN_ADDR 3
44 #define HOSTAP_CRYPT_ERR_KEY_SET_FAILED 5
45
46 /*---------------------  Static Definitions -------------------------*/
47
48 /*---------------------  Static Classes  ----------------------------*/
49
50 /*---------------------  Static Variables  --------------------------*/
51 static int msglevel = MSG_LEVEL_INFO;
52
53 /*---------------------  Static Functions  --------------------------*/
54
55 /*---------------------  Export Variables  --------------------------*/
56
57 /*
58  * Description:
59  *      register net_device (AP) for hostap deamon
60  *
61  * Parameters:
62  *  In:
63  *      pDevice             -
64  *      rtnl_locked         -
65  *  Out:
66  *
67  * Return Value:
68  *
69  */
70
71 static int hostap_enable_hostapd(PSDevice pDevice, int rtnl_locked)
72 {
73         PSDevice apdev_priv;
74         struct net_device *dev = pDevice->dev;
75         int ret;
76         const struct net_device_ops apdev_netdev_ops = {
77                 .ndo_start_xmit         = pDevice->tx_80211,
78         };
79
80         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Enabling hostapd mode\n", dev->name);
81
82         pDevice->apdev = alloc_etherdev(sizeof(*apdev_priv));
83         if (pDevice->apdev == NULL)
84                 return -ENOMEM;
85
86         apdev_priv = netdev_priv(pDevice->apdev);
87         *apdev_priv = *pDevice;
88         eth_hw_addr_inherit(pDevice->apdev, dev);
89
90         pDevice->apdev->netdev_ops = &apdev_netdev_ops;
91
92         pDevice->apdev->type = ARPHRD_IEEE80211;
93
94         pDevice->apdev->base_addr = dev->base_addr;
95         pDevice->apdev->irq = dev->irq;
96         pDevice->apdev->mem_start = dev->mem_start;
97         pDevice->apdev->mem_end = dev->mem_end;
98         sprintf(pDevice->apdev->name, "%sap", dev->name);
99         if (rtnl_locked)
100                 ret = register_netdevice(pDevice->apdev);
101         else
102                 ret = register_netdev(pDevice->apdev);
103         if (ret) {
104                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: register_netdevice(AP) failed!\n",
105                         dev->name);
106                 free_netdev(pDevice->apdev);
107                 pDevice->apdev = NULL;
108                 return -1;
109         }
110
111         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Registered netdevice %s for AP management\n",
112                 dev->name, pDevice->apdev->name);
113
114         KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
115
116         return 0;
117 }
118
119 /*
120  * Description:
121  *      unregister net_device(AP)
122  *
123  * Parameters:
124  *  In:
125  *      pDevice             -
126  *      rtnl_locked         -
127  *  Out:
128  *
129  * Return Value:
130  *
131  */
132
133 static int hostap_disable_hostapd(PSDevice pDevice, int rtnl_locked)
134 {
135         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: disabling hostapd mode\n", pDevice->dev->name);
136
137         if (pDevice->apdev && pDevice->apdev->name && pDevice->apdev->name[0]) {
138                 if (rtnl_locked)
139                         unregister_netdevice(pDevice->apdev);
140                 else
141                         unregister_netdev(pDevice->apdev);
142                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: Netdevice %s unregistered\n",
143                         pDevice->dev->name, pDevice->apdev->name);
144         }
145         if (pDevice->apdev)
146                 free_netdev(pDevice->apdev);
147         pDevice->apdev = NULL;
148         pDevice->bEnable8021x = false;
149         pDevice->bEnableHostWEP = false;
150         pDevice->bEncryptionEnable = false;
151
152 //4.2007-0118-03,<Add> by EinsnLiu
153 //execute some clear work
154         pDevice->pMgmt->byCSSPK = KEY_CTL_NONE;
155         pDevice->pMgmt->byCSSGK = KEY_CTL_NONE;
156         KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
157
158         return 0;
159 }
160
161 /*
162  * Description:
163  *      Set enable/disable hostapd mode
164  *
165  * Parameters:
166  *  In:
167  *      pDevice             -
168  *      rtnl_locked         -
169  *  Out:
170  *
171  * Return Value:
172  *
173  */
174
175 int vt6655_hostap_set_hostapd(PSDevice pDevice, int val, int rtnl_locked)
176 {
177         if (val < 0 || val > 1)
178                 return -EINVAL;
179
180         if (pDevice->bEnableHostapd == val)
181                 return 0;
182
183         pDevice->bEnableHostapd = val;
184
185         if (val)
186                 return hostap_enable_hostapd(pDevice, rtnl_locked);
187         else
188                 return hostap_disable_hostapd(pDevice, rtnl_locked);
189 }
190
191 /*
192  * Description:
193  *      remove station function supported for hostap deamon
194  *
195  * Parameters:
196  *  In:
197  *      pDevice   -
198  *      param     -
199  *  Out:
200  *
201  * Return Value:
202  *
203  */
204 static int hostap_remove_sta(PSDevice pDevice,
205                              struct viawget_hostapd_param *param)
206 {
207         unsigned int uNodeIndex;
208
209         if (BSSDBbIsSTAInNodeDB(pDevice->pMgmt, param->sta_addr, &uNodeIndex))
210                 BSSvRemoveOneNode(pDevice, uNodeIndex);
211         else
212                 return -ENOENT;
213
214         return 0;
215 }
216
217 /*
218  * Description:
219  *      add a station from hostap deamon
220  *
221  * Parameters:
222  *  In:
223  *      pDevice   -
224  *      param     -
225  *  Out:
226  *
227  * Return Value:
228  *
229  */
230 static int hostap_add_sta(PSDevice pDevice,
231                           struct viawget_hostapd_param *param)
232 {
233         PSMgmtObject    pMgmt = pDevice->pMgmt;
234         unsigned int uNodeIndex;
235
236         if (!BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex))
237                 BSSvCreateOneNode((PSDevice)pDevice, &uNodeIndex);
238
239         memcpy(pMgmt->sNodeDBTable[uNodeIndex].abyMACAddr, param->sta_addr, WLAN_ADDR_LEN);
240         pMgmt->sNodeDBTable[uNodeIndex].eNodeState = NODE_ASSOC;
241         pMgmt->sNodeDBTable[uNodeIndex].wCapInfo = param->u.add_sta.capability;
242 // TODO listenInterval
243         pMgmt->sNodeDBTable[uNodeIndex].bPSEnable = false;
244         pMgmt->sNodeDBTable[uNodeIndex].bySuppRate = param->u.add_sta.tx_supp_rates;
245
246         // set max tx rate
247         pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate =
248                 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate;
249         // set max basic rate
250         pMgmt->sNodeDBTable[uNodeIndex].wMaxBasicRate = RATE_2M;
251         // Todo: check sta preamble, if ap can't support, set status code
252         pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble =
253                 WLAN_GET_CAP_INFO_SHORTPREAMBLE(pMgmt->sNodeDBTable[uNodeIndex].wCapInfo);
254
255         pMgmt->sNodeDBTable[uNodeIndex].wAID = (unsigned short)param->u.add_sta.aid;
256
257         pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer = jiffies;
258
259         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Add STA AID= %d\n", pMgmt->sNodeDBTable[uNodeIndex].wAID);
260         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
261                 param->sta_addr[0],
262                 param->sta_addr[1],
263                 param->sta_addr[2],
264                 param->sta_addr[3],
265                 param->sta_addr[4],
266                 param->sta_addr[5]
267                 );
268         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Max Support rate = %d\n",
269                 pMgmt->sNodeDBTable[uNodeIndex].wMaxSuppRate);
270
271         return 0;
272 }
273
274 /*
275  * Description:
276  *      get station info
277  *
278  * Parameters:
279  *  In:
280  *      pDevice   -
281  *      param     -
282  *  Out:
283  *
284  * Return Value:
285  *
286  */
287
288 static int hostap_get_info_sta(PSDevice pDevice,
289                                struct viawget_hostapd_param *param)
290 {
291         PSMgmtObject    pMgmt = pDevice->pMgmt;
292         unsigned int uNodeIndex;
293
294         if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
295                 param->u.get_info_sta.inactive_sec =
296                         (jiffies - pMgmt->sNodeDBTable[uNodeIndex].ulLastRxJiffer) / HZ;
297         } else {
298                 return -ENOENT;
299         }
300
301         return 0;
302 }
303
304 /*
305  * Description:
306  *      set station flag
307  *
308  * Parameters:
309  *  In:
310  *      pDevice   -
311  *      param     -
312  *  Out:
313  *
314  * Return Value:
315  *
316  */
317 static int hostap_set_flags_sta(PSDevice pDevice,
318                                 struct viawget_hostapd_param *param)
319 {
320         PSMgmtObject    pMgmt = pDevice->pMgmt;
321         unsigned int uNodeIndex;
322
323         if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &uNodeIndex)) {
324                 pMgmt->sNodeDBTable[uNodeIndex].dwFlags |= param->u.set_flags_sta.flags_or;
325                 pMgmt->sNodeDBTable[uNodeIndex].dwFlags &= param->u.set_flags_sta.flags_and;
326                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " dwFlags = %x\n",
327                         (unsigned int)pMgmt->sNodeDBTable[uNodeIndex].dwFlags);
328         } else {
329                 return -ENOENT;
330         }
331
332         return 0;
333 }
334
335 /*
336  * Description:
337  *      set generic element (wpa ie)
338  *
339  * Parameters:
340  *  In:
341  *      pDevice   -
342  *      param     -
343  *  Out:
344  *
345  * Return Value:
346  *
347  */
348 static int hostap_set_generic_element(PSDevice pDevice,
349                                       struct viawget_hostapd_param *param)
350 {
351         PSMgmtObject    pMgmt = pDevice->pMgmt;
352
353         if (param->u.generic_elem.len > sizeof(pMgmt->abyWPAIE))
354                 return -EINVAL;
355
356         memcpy(pMgmt->abyWPAIE,
357                param->u.generic_elem.data,
358                param->u.generic_elem.len
359                 );
360
361         pMgmt->wWPAIELen = param->u.generic_elem.len;
362
363         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pMgmt->wWPAIELen = %d\n", pMgmt->wWPAIELen);
364
365         // disable wpa
366         if (pMgmt->wWPAIELen == 0) {
367                 pMgmt->eAuthenMode = WMAC_AUTH_OPEN;
368                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " No WPAIE, Disable WPA\n");
369         } else  {
370                 // enable wpa
371                 if ((pMgmt->abyWPAIE[0] == WLAN_EID_RSN_WPA) ||
372                     (pMgmt->abyWPAIE[0] == WLAN_EID_RSN)) {
373                         pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
374                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set WPAIE enable WPA\n");
375                 } else
376                         return -EINVAL;
377         }
378
379         return 0;
380 }
381
382 /*
383  * Description:
384  *      flush station nodes table.
385  *
386  * Parameters:
387  *  In:
388  *      pDevice   -
389  *  Out:
390  *
391  * Return Value:
392  *
393  */
394
395 static void hostap_flush_sta(PSDevice pDevice)
396 {
397         // reserved node index =0 for multicast node.
398         BSSvClearNodeDBTable(pDevice, 1);
399         pDevice->uAssocCount = 0;
400 }
401
402 /*
403  * Description:
404  *      set each stations encryption key
405  *
406  * Parameters:
407  *  In:
408  *      pDevice   -
409  *      param     -
410  *  Out:
411  *
412  * Return Value:
413  *
414  */
415 static int hostap_set_encryption(PSDevice pDevice,
416                                  struct viawget_hostapd_param *param,
417                                  int param_len)
418 {
419         PSMgmtObject    pMgmt = pDevice->pMgmt;
420         unsigned long dwKeyIndex = 0;
421         unsigned char abyKey[MAX_KEY_LEN];
422         unsigned char abySeq[MAX_KEY_LEN];
423         unsigned long long KeyRSC;
424         unsigned char byKeyDecMode = KEY_CTL_WEP;
425         int     iNodeIndex = -1;
426         int     ii;
427         bool bKeyTableFull = false;
428         unsigned short wKeyCtl = 0;
429
430         param->u.crypt.err = 0;
431
432         if (param->u.crypt.alg > WPA_ALG_CCMP)
433                 return -EINVAL;
434
435         if ((param->u.crypt.idx > 3) || (param->u.crypt.key_len > MAX_KEY_LEN)) {
436                 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
437                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_KEY_SET_FAILED\n");
438                 return -EINVAL;
439         }
440
441         if (is_broadcast_ether_addr(param->sta_addr)) {
442                 if (param->u.crypt.idx >= MAX_GROUP_KEY)
443                         return -EINVAL;
444                 iNodeIndex = 0;
445
446         } else {
447                 if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &iNodeIndex) == false) {
448                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
449                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
450                         return -EINVAL;
451                 }
452         }
453         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: sta_index %d\n", iNodeIndex);
454         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " hostap_set_encryption: alg %d\n", param->u.crypt.alg);
455
456         if (param->u.crypt.alg == WPA_ALG_NONE) {
457                 if (pMgmt->sNodeDBTable[iNodeIndex].bOnFly) {
458                         if (!KeybRemoveKey(&(pDevice->sKey),
459                                           param->sta_addr,
460                                           pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex,
461                                           pDevice->PortOffset)) {
462                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "KeybRemoveKey fail\n");
463                         }
464                         pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
465                 }
466                 pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = 0;
467                 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = 0;
468                 pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = 0;
469                 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = 0;
470                 pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
471                 pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
472                 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = 0;
473                 memset(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
474                        0,
475                        MAX_KEY_LEN
476 );
477
478                 return 0;
479         }
480
481         memcpy(abyKey, param->u.crypt.key, param->u.crypt.key_len);
482         // copy to node key tbl
483         pMgmt->sNodeDBTable[iNodeIndex].byKeyIndex = param->u.crypt.idx;
484         pMgmt->sNodeDBTable[iNodeIndex].uWepKeyLength = param->u.crypt.key_len;
485         memcpy(&pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
486                param->u.crypt.key,
487                param->u.crypt.key_len
488 );
489
490         dwKeyIndex = (unsigned long)(param->u.crypt.idx);
491         if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
492                 pDevice->byKeyIndex = (unsigned char)dwKeyIndex;
493                 pDevice->bTransmitKey = true;
494                 dwKeyIndex |= (1 << 31);
495         }
496
497         if (param->u.crypt.alg == WPA_ALG_WEP) {
498                 if ((pDevice->bEnable8021x == false) || (iNodeIndex == 0)) {
499                         KeybSetDefaultKey(&(pDevice->sKey),
500                                           dwKeyIndex & ~(BIT30 | USE_KEYRSC),
501                                           param->u.crypt.key_len,
502                                           NULL,
503                                           abyKey,
504                                           KEY_CTL_WEP,
505                                           pDevice->PortOffset,
506                                           pDevice->byLocalID);
507
508                 } else {
509                         // 8021x enable, individual key
510                         dwKeyIndex |= (1 << 30); // set pairwise key
511                         if (KeybSetKey(&(pDevice->sKey),
512                                        &param->sta_addr[0],
513                                        dwKeyIndex & ~(USE_KEYRSC),
514                                        param->u.crypt.key_len,
515                                        (PQWORD) &(KeyRSC),
516                                        (unsigned char *)abyKey,
517                                        KEY_CTL_WEP,
518                                        pDevice->PortOffset,
519                                        pDevice->byLocalID)) {
520                                 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
521
522                         } else {
523                                 // Key Table Full
524                                 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
525                                 bKeyTableFull = true;
526                         }
527                 }
528                 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
529                 pDevice->bEncryptionEnable = true;
530                 pMgmt->byCSSPK = KEY_CTL_WEP;
531                 pMgmt->byCSSGK = KEY_CTL_WEP;
532                 pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = KEY_CTL_WEP;
533                 pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
534                 return 0;
535         }
536
537         if (param->u.crypt.seq) {
538                 memcpy(&abySeq, param->u.crypt.seq, 8);
539                 for (ii = 0; ii < 8; ii++)
540                         KeyRSC |= (unsigned long)abySeq[ii] << (ii * 8);
541
542                 dwKeyIndex |= 1 << 29;
543                 pMgmt->sNodeDBTable[iNodeIndex].KeyRSC = KeyRSC;
544         }
545
546         if (param->u.crypt.alg == WPA_ALG_TKIP) {
547                 if (param->u.crypt.key_len != MAX_KEY_LEN)
548                         return -EINVAL;
549                 pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
550                 byKeyDecMode = KEY_CTL_TKIP;
551                 pMgmt->byCSSPK = KEY_CTL_TKIP;
552                 pMgmt->byCSSGK = KEY_CTL_TKIP;
553         }
554
555         if (param->u.crypt.alg == WPA_ALG_CCMP) {
556                 if ((param->u.crypt.key_len != AES_KEY_LEN) ||
557                     (pDevice->byLocalID <= REV_ID_VT3253_A1))
558                         return -EINVAL;
559                 pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
560                 byKeyDecMode = KEY_CTL_CCMP;
561                 pMgmt->byCSSPK = KEY_CTL_CCMP;
562                 pMgmt->byCSSGK = KEY_CTL_CCMP;
563         }
564
565         if (iNodeIndex == 0) {
566                 KeybSetDefaultKey(&(pDevice->sKey),
567                                   dwKeyIndex,
568                                   param->u.crypt.key_len,
569                                   (PQWORD) &(KeyRSC),
570                                   abyKey,
571                                   byKeyDecMode,
572                                   pDevice->PortOffset,
573                                   pDevice->byLocalID);
574                 pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
575
576         } else {
577                 dwKeyIndex |= (1 << 30); // set pairwise key
578                 if (KeybSetKey(&(pDevice->sKey),
579                                &param->sta_addr[0],
580                                dwKeyIndex,
581                                param->u.crypt.key_len,
582                                (PQWORD) &(KeyRSC),
583                                (unsigned char *)abyKey,
584                                byKeyDecMode,
585                                pDevice->PortOffset,
586                                pDevice->byLocalID)) {
587                         pMgmt->sNodeDBTable[iNodeIndex].bOnFly = true;
588
589                 } else {
590                         // Key Table Full
591                         pMgmt->sNodeDBTable[iNodeIndex].bOnFly = false;
592                         bKeyTableFull = true;
593                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Key Table Full\n");
594                 }
595
596         }
597
598         if (bKeyTableFull) {
599                 wKeyCtl &= 0x7F00;              // clear all key control filed
600                 wKeyCtl |= (byKeyDecMode << 4);
601                 wKeyCtl |= (byKeyDecMode);
602                 wKeyCtl |= 0x0044;              // use group key for all address
603                 wKeyCtl |= 0x4000;              // disable KeyTable[MAX_KEY_TABLE-1] on-fly to genernate rx int
604                 MACvSetDefaultKeyCtl(pDevice->PortOffset, wKeyCtl, MAX_KEY_TABLE-1, pDevice->byLocalID);
605         }
606
607         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set key sta_index= %d\n", iNodeIndex);
608         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " tx_index=%d len=%d\n", param->u.crypt.idx,
609                 param->u.crypt.key_len);
610         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " key=%x-%x-%x-%x-%x-xxxxx\n",
611                 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[0],
612                 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[1],
613                 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[2],
614                 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[3],
615                 pMgmt->sNodeDBTable[iNodeIndex].abyWepKey[4]
616 );
617
618         // set wep key
619         pDevice->bEncryptionEnable = true;
620         pMgmt->sNodeDBTable[iNodeIndex].byCipherSuite = byKeyDecMode;
621         pMgmt->sNodeDBTable[iNodeIndex].dwKeyIndex = dwKeyIndex;
622         pMgmt->sNodeDBTable[iNodeIndex].dwTSC47_16 = 0;
623         pMgmt->sNodeDBTable[iNodeIndex].wTSC15_0 = 0;
624
625         return 0;
626 }
627
628 /*
629  * Description:
630  *      get each stations encryption key
631  *
632  * Parameters:
633  *  In:
634  *      pDevice   -
635  *      param     -
636  *  Out:
637  *
638  * Return Value:
639  *
640  */
641 static int hostap_get_encryption(PSDevice pDevice,
642                                  struct viawget_hostapd_param *param,
643                                  int param_len)
644 {
645         PSMgmtObject    pMgmt = pDevice->pMgmt;
646         int     ii;
647         int     iNodeIndex = 0;
648
649         param->u.crypt.err = 0;
650
651         if (is_broadcast_ether_addr(param->sta_addr)) {
652                 iNodeIndex = 0;
653         } else {
654                 if (BSSDBbIsSTAInNodeDB(pMgmt, param->sta_addr, &iNodeIndex) == false) {
655                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
656                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: HOSTAP_CRYPT_ERR_UNKNOWN_ADDR\n");
657                         return -EINVAL;
658                 }
659         }
660         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "hostap_get_encryption: %d\n", iNodeIndex);
661         memset(param->u.crypt.seq, 0, 8);
662         for (ii = 0; ii < 8; ii++)
663                 param->u.crypt.seq[ii] = (unsigned char)pMgmt->sNodeDBTable[iNodeIndex].KeyRSC >> (ii * 8);
664
665         return 0;
666 }
667
668 /*
669  * Description:
670  *      vt6655_hostap_ioctl main function supported for hostap deamon.
671  *
672  * Parameters:
673  *  In:
674  *      pDevice   -
675  *      iw_point  -
676  *  Out:
677  *
678  * Return Value:
679  *
680  */
681 int vt6655_hostap_ioctl(PSDevice pDevice, struct iw_point *p)
682 {
683         struct viawget_hostapd_param *param;
684         int ret = 0;
685         int ap_ioctl = 0;
686
687         if (p->length < sizeof(struct viawget_hostapd_param) ||
688             p->length > VIAWGET_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
689                 return -EINVAL;
690
691         param = kmalloc((int)p->length, GFP_KERNEL);
692         if (param == NULL)
693                 return -ENOMEM;
694
695         if (copy_from_user(param, p->pointer, p->length)) {
696                 ret = -EFAULT;
697                 goto out;
698         }
699
700         switch (param->cmd) {
701         case VIAWGET_HOSTAPD_SET_ENCRYPTION:
702                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ENCRYPTION\n");
703                 spin_lock_irq(&pDevice->lock);
704                 ret = hostap_set_encryption(pDevice, param, p->length);
705                 spin_unlock_irq(&pDevice->lock);
706                 break;
707         case VIAWGET_HOSTAPD_GET_ENCRYPTION:
708                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_ENCRYPTION\n");
709                 spin_lock_irq(&pDevice->lock);
710                 ret = hostap_get_encryption(pDevice, param, p->length);
711                 spin_unlock_irq(&pDevice->lock);
712                 break;
713         case VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR:
714                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_ASSOC_AP_ADDR\n");
715                 ret = -EOPNOTSUPP;
716                 goto out;
717         case VIAWGET_HOSTAPD_FLUSH:
718                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_FLUSH\n");
719                 spin_lock_irq(&pDevice->lock);
720                 hostap_flush_sta(pDevice);
721                 spin_unlock_irq(&pDevice->lock);
722                 break;
723         case VIAWGET_HOSTAPD_ADD_STA:
724                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_ADD_STA\n");
725                 spin_lock_irq(&pDevice->lock);
726                 ret = hostap_add_sta(pDevice, param);
727                 spin_unlock_irq(&pDevice->lock);
728                 break;
729         case VIAWGET_HOSTAPD_REMOVE_STA:
730                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_REMOVE_STA\n");
731                 spin_lock_irq(&pDevice->lock);
732                 ret = hostap_remove_sta(pDevice, param);
733                 spin_unlock_irq(&pDevice->lock);
734                 break;
735         case VIAWGET_HOSTAPD_GET_INFO_STA:
736                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_GET_INFO_STA\n");
737                 ret = hostap_get_info_sta(pDevice, param);
738                 ap_ioctl = 1;
739                 break;
740         case VIAWGET_HOSTAPD_SET_FLAGS_STA:
741                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_FLAGS_STA\n");
742                 ret = hostap_set_flags_sta(pDevice, param);
743                 break;
744         case VIAWGET_HOSTAPD_MLME:
745                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_MLME\n");
746                 ret = -EOPNOTSUPP;
747                 goto out;
748         case VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT:
749                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SET_GENERIC_ELEMENT\n");
750                 ret = hostap_set_generic_element(pDevice, param);
751                 break;
752         case VIAWGET_HOSTAPD_SCAN_REQ:
753                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_SCAN_REQ\n");
754                 ret = -EOPNOTSUPP;
755                 goto out;
756         case VIAWGET_HOSTAPD_STA_CLEAR_STATS:
757                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "VIAWGET_HOSTAPD_STA_CLEAR_STATS\n");
758                 ret = -EOPNOTSUPP;
759                 goto out;
760         default:
761                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vt6655_hostap_ioctl: unknown cmd=%d\n",
762                         (int)param->cmd);
763                 ret = -EOPNOTSUPP;
764                 goto out;
765         }
766
767         if ((ret == 0) && ap_ioctl) {
768                 if (copy_to_user(p->pointer, param, p->length))
769                         ret = -EFAULT;
770         }
771
772 out:
773         kfree(param);
774         return ret;
775 }