Staging: rt2870: remove dead BLOCK_NET_IF code
[linux-2.6] / drivers / staging / rt2870 / rt_main_dev.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27     Module Name:
28     rt_main_dev.c
29
30     Abstract:
31     Create and register network interface.
32
33     Revision History:
34     Who         When            What
35     --------    ----------      ----------------------------------------------
36         Sample          Mar/21/07               Merge RT2870 and RT2860 drivers.
37 */
38
39 #include "rt_config.h"
40
41 #define FORTY_MHZ_INTOLERANT_INTERVAL   (60*1000) // 1 min
42
43 #ifdef MULTIPLE_CARD_SUPPORT
44 // record whether the card in the card list is used in the card file
45 UINT8  MC_CardUsed[MAX_NUM_OF_MULTIPLE_CARD];
46 // record used card mac address in the card list
47 static UINT8  MC_CardMac[MAX_NUM_OF_MULTIPLE_CARD][6];
48 #endif // MULTIPLE_CARD_SUPPORT //
49
50 /*---------------------------------------------------------------------*/
51 /* Private Variables Used                                              */
52 /*---------------------------------------------------------------------*/
53 //static RALINK_TIMER_STRUCT     PeriodicTimer;
54
55 char *mac = "";            // default 00:00:00:00:00:00
56 char *hostname = "";
57 module_param (mac, charp, 0);
58 MODULE_PARM_DESC (mac, "rt28xx: wireless mac addr");
59
60
61 /*---------------------------------------------------------------------*/
62 /* Prototypes of Functions Used                                        */
63 /*---------------------------------------------------------------------*/
64 #ifdef DOT11_N_SUPPORT
65 extern BOOLEAN ba_reordering_resource_init(PRTMP_ADAPTER pAd, int num);
66 extern void ba_reordering_resource_release(PRTMP_ADAPTER pAd);
67 #endif // DOT11_N_SUPPORT //
68 extern NDIS_STATUS NICLoadRateSwitchingParams(IN PRTMP_ADAPTER pAd);
69
70
71 // public function prototype
72 INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
73                                                         IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
74
75 // private function prototype
76 static int rt28xx_init(IN struct net_device *net_dev);
77 INT rt28xx_send_packets(IN struct sk_buff *skb_p, IN struct net_device *net_dev);
78
79 static void CfgInitHook(PRTMP_ADAPTER pAd);
80 //static BOOLEAN RT28XXAvailRANameAssign(IN CHAR *name_p);
81
82 #ifdef CONFIG_STA_SUPPORT
83 extern  const struct iw_handler_def rt28xx_iw_handler_def;
84 #endif // CONFIG_STA_SUPPORT //
85
86 #if WIRELESS_EXT >= 12
87 // This function will be called when query /proc
88 struct iw_statistics *rt28xx_get_wireless_stats(
89     IN struct net_device *net_dev);
90 #endif
91
92 struct net_device_stats *RT28xx_get_ether_stats(
93     IN  struct net_device *net_dev);
94
95 /*
96 ========================================================================
97 Routine Description:
98     Close raxx interface.
99
100 Arguments:
101         *net_dev                        the raxx interface pointer
102
103 Return Value:
104     0                                   Open OK
105         otherwise                       Open Fail
106
107 Note:
108         1. if open fail, kernel will not call the close function.
109         2. Free memory for
110                 (1) Mlme Memory Handler:                MlmeHalt()
111                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
112                 (3) BA Reordering:                              ba_reordering_resource_release()
113 ========================================================================
114 */
115 int MainVirtualIF_close(IN struct net_device *net_dev)
116 {
117     RTMP_ADAPTER *pAd = net_dev->ml_priv;
118
119         // Sanity check for pAd
120         if (pAd == NULL)
121                 return 0; // close ok
122
123         netif_carrier_off(pAd->net_dev);
124         netif_stop_queue(pAd->net_dev);
125
126
127
128         VIRTUAL_IF_DOWN(pAd);
129
130         RT_MOD_DEC_USE_COUNT();
131
132         return 0; // close ok
133 }
134
135 /*
136 ========================================================================
137 Routine Description:
138     Open raxx interface.
139
140 Arguments:
141         *net_dev                        the raxx interface pointer
142
143 Return Value:
144     0                                   Open OK
145         otherwise                       Open Fail
146
147 Note:
148         1. if open fail, kernel will not call the close function.
149         2. Free memory for
150                 (1) Mlme Memory Handler:                MlmeHalt()
151                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
152                 (3) BA Reordering:                              ba_reordering_resource_release()
153 ========================================================================
154 */
155 int MainVirtualIF_open(IN struct net_device *net_dev)
156 {
157     RTMP_ADAPTER *pAd = net_dev->ml_priv;
158
159         // Sanity check for pAd
160         if (pAd == NULL)
161                 return 0; // close ok
162
163         if (VIRTUAL_IF_UP(pAd) != 0)
164                 return -1;
165
166         // increase MODULE use count
167         RT_MOD_INC_USE_COUNT();
168
169         netif_start_queue(net_dev);
170         netif_carrier_on(net_dev);
171         netif_wake_queue(net_dev);
172
173         return 0;
174 }
175
176 /*
177 ========================================================================
178 Routine Description:
179     Close raxx interface.
180
181 Arguments:
182         *net_dev                        the raxx interface pointer
183
184 Return Value:
185     0                                   Open OK
186         otherwise                       Open Fail
187
188 Note:
189         1. if open fail, kernel will not call the close function.
190         2. Free memory for
191                 (1) Mlme Memory Handler:                MlmeHalt()
192                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
193                 (3) BA Reordering:                              ba_reordering_resource_release()
194 ========================================================================
195 */
196 int rt28xx_close(IN PNET_DEV dev)
197 {
198         struct net_device * net_dev = (struct net_device *)dev;
199     RTMP_ADAPTER        *pAd = net_dev->ml_priv;
200         BOOLEAN                 Cancelled = FALSE;
201         UINT32                  i = 0;
202 #ifdef RT2870
203         DECLARE_WAIT_QUEUE_HEAD(unlink_wakeup);
204         DECLARE_WAITQUEUE(wait, current);
205
206         //RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_REMOVE_IN_PROGRESS);
207 #endif // RT2870 //
208
209
210     DBGPRINT(RT_DEBUG_TRACE, ("===> rt28xx_close\n"));
211
212         // Sanity check for pAd
213         if (pAd == NULL)
214                 return 0; // close ok
215
216
217 #ifdef CONFIG_STA_SUPPORT
218         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
219         {
220
221                 // If dirver doesn't wake up firmware here,
222                 // NICLoadFirmware will hang forever when interface is up again.
223                 if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
224         {
225                     AsicForceWakeup(pAd, TRUE);
226         }
227
228 #ifdef QOS_DLS_SUPPORT
229                 // send DLS-TEAR_DOWN message,
230                 if (pAd->CommonCfg.bDLSCapable)
231                 {
232                         UCHAR i;
233
234                         // tear down local dls table entry
235                         for (i=0; i<MAX_NUM_OF_INIT_DLS_ENTRY; i++)
236                         {
237                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
238                                 {
239                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
240                                         pAd->StaCfg.DLSEntry[i].Status  = DLS_NONE;
241                                         pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
242                                 }
243                         }
244
245                         // tear down peer dls table entry
246                         for (i=MAX_NUM_OF_INIT_DLS_ENTRY; i<MAX_NUM_OF_DLS_ENTRY; i++)
247                         {
248                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
249                                 {
250                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
251                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
252                                         pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
253                                 }
254                         }
255                         RT28XX_MLME_HANDLER(pAd);
256                 }
257 #endif // QOS_DLS_SUPPORT //
258
259                 if (INFRA_ON(pAd) &&
260                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
261                 {
262                         MLME_DISASSOC_REQ_STRUCT        DisReq;
263                         MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
264
265                         COPY_MAC_ADDR(DisReq.Addr, pAd->CommonCfg.Bssid);
266                         DisReq.Reason =  REASON_DEAUTH_STA_LEAVING;
267
268                         MsgElem->Machine = ASSOC_STATE_MACHINE;
269                         MsgElem->MsgType = MT2_MLME_DISASSOC_REQ;
270                         MsgElem->MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
271                         NdisMoveMemory(MsgElem->Msg, &DisReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
272
273                         // Prevent to connect AP again in STAMlmePeriodicExec
274                         pAd->MlmeAux.AutoReconnectSsidLen= 32;
275                         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
276
277                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
278                         MlmeDisassocReqAction(pAd, MsgElem);
279                         kfree(MsgElem);
280
281                         RTMPusecDelay(1000);
282                 }
283
284 #ifdef RT2870
285         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_REMOVE_IN_PROGRESS);
286 #endif // RT2870 //
287
288 #ifdef CCX_SUPPORT
289                 RTMPCancelTimer(&pAd->StaCfg.LeapAuthTimer, &Cancelled);
290 #endif
291
292                 RTMPCancelTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, &Cancelled);
293                 RTMPCancelTimer(&pAd->StaCfg.WpaDisassocAndBlockAssocTimer, &Cancelled);
294
295 #ifdef WPA_SUPPLICANT_SUPPORT
296 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
297                 {
298                         union iwreq_data    wrqu;
299                         // send wireless event to wpa_supplicant for infroming interface down.
300                         memset(&wrqu, 0, sizeof(wrqu));
301                         wrqu.data.flags = RT_INTERFACE_DOWN;
302                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
303                 }
304 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
305 #endif // WPA_SUPPLICANT_SUPPORT //
306
307                 MlmeRadioOff(pAd);
308         }
309 #endif // CONFIG_STA_SUPPORT //
310
311         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
312
313         for (i = 0 ; i < NUM_OF_TX_RING; i++)
314         {
315                 while (pAd->DeQueueRunning[i] == TRUE)
316                 {
317                         printk("Waiting for TxQueue[%d] done..........\n", i);
318                         RTMPusecDelay(1000);
319                 }
320         }
321
322 #ifdef RT2870
323         // ensure there are no more active urbs.
324         add_wait_queue (&unlink_wakeup, &wait);
325         pAd->wait = &unlink_wakeup;
326
327         // maybe wait for deletions to finish.
328         i = 0;
329         //while((i < 25) && atomic_read(&pAd->PendingRx) > 0)
330         while(i < 25)
331         {
332                 unsigned long IrqFlags;
333
334                 RTMP_IRQ_LOCK(&pAd->BulkInLock, IrqFlags);
335                 if (pAd->PendingRx == 0)
336                 {
337                         RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
338                         break;
339                 }
340                 RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
341
342                 msleep(UNLINK_TIMEOUT_MS);      //Time in millisecond
343                 i++;
344         }
345         pAd->wait = NULL;
346         remove_wait_queue (&unlink_wakeup, &wait);
347 #endif // RT2870 //
348
349         //RTUSBCleanUpMLMEWaitQueue(pAd);       /*not used in RT28xx*/
350
351
352 #ifdef RT2870
353         // We need clear timerQ related structure before exits of the timer thread.
354         RT2870_TimerQ_Exit(pAd);
355         // Close kernel threads or tasklets
356         RT28xxThreadTerminate(pAd);
357 #endif // RT2870 //
358
359         // Stop Mlme state machine
360         MlmeHalt(pAd);
361
362         // Close kernel threads or tasklets
363         kill_thread_task(pAd);
364
365
366 #ifdef CONFIG_STA_SUPPORT
367         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
368         {
369                 MacTableReset(pAd);
370         }
371 #endif // CONFIG_STA_SUPPORT //
372
373
374         MeasureReqTabExit(pAd);
375         TpcReqTabExit(pAd);
376
377
378
379
380         // Free Ring or USB buffers
381         RTMPFreeTxRxRingMemory(pAd);
382
383         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
384
385 #ifdef DOT11_N_SUPPORT
386         // Free BA reorder resource
387         ba_reordering_resource_release(pAd);
388 #endif // DOT11_N_SUPPORT //
389
390 #ifdef RT2870
391 #ifdef INF_AMAZON_SE
392         if (pAd->UsbVendorReqBuf)
393                 os_free_mem(pAd, pAd->UsbVendorReqBuf);
394 #endif // INF_AMAZON_SE //
395 #endif // RT2870 //
396
397         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_START_UP);
398
399         return 0; // close ok
400 } /* End of rt28xx_close */
401
402 static int rt28xx_init(IN struct net_device *net_dev)
403 {
404         PRTMP_ADAPTER                   pAd = net_dev->ml_priv;
405         UINT                                    index;
406         UCHAR                                   TmpPhy;
407         NDIS_STATUS                             Status;
408         UINT32          MacCsr0 = 0;
409
410 #ifdef RT2870
411 #ifdef INF_AMAZON_SE
412         init_MUTEX(&(pAd->UsbVendorReq_semaphore));
413         os_alloc_mem(pAd, (PUCHAR)&pAd->UsbVendorReqBuf, MAX_PARAM_BUFFER_SIZE - 1);
414         if (pAd->UsbVendorReqBuf == NULL)
415         {
416                 DBGPRINT(RT_DEBUG_ERROR, ("Allocate vendor request temp buffer failed!\n"));
417                 goto err0;
418         }
419 #endif // INF_AMAZON_SE //
420 #endif // RT2870 //
421
422 #ifdef DOT11_N_SUPPORT
423         // Allocate BA Reordering memory
424         ba_reordering_resource_init(pAd, MAX_REORDERING_MPDU_NUM);
425 #endif // DOT11_N_SUPPORT //
426
427         // Make sure MAC gets ready.
428         index = 0;
429         do
430         {
431                 RTMP_IO_READ32(pAd, MAC_CSR0, &MacCsr0);
432                 pAd->MACVersion = MacCsr0;
433
434                 if ((pAd->MACVersion != 0x00) && (pAd->MACVersion != 0xFFFFFFFF))
435                         break;
436
437                 RTMPusecDelay(10);
438         } while (index++ < 100);
439
440         DBGPRINT(RT_DEBUG_TRACE, ("MAC_CSR0  [ Ver:Rev=0x%08x]\n", pAd->MACVersion));
441
442         // Disable DMA
443         RT28XXDMADisable(pAd);
444
445
446         // Load 8051 firmware
447         Status = NICLoadFirmware(pAd);
448         if (Status != NDIS_STATUS_SUCCESS)
449         {
450                 DBGPRINT_ERR(("NICLoadFirmware failed, Status[=0x%08x]\n", Status));
451                 goto err1;
452         }
453
454         NICLoadRateSwitchingParams(pAd);
455
456         // Disable interrupts here which is as soon as possible
457         // This statement should never be true. We might consider to remove it later
458
459         Status = RTMPAllocTxRxRingMemory(pAd);
460         if (Status != NDIS_STATUS_SUCCESS)
461         {
462                 DBGPRINT_ERR(("RTMPAllocDMAMemory failed, Status[=0x%08x]\n", Status));
463                 goto err1;
464         }
465
466         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
467
468         // initialize MLME
469         //
470
471         Status = MlmeInit(pAd);
472         if (Status != NDIS_STATUS_SUCCESS)
473         {
474                 DBGPRINT_ERR(("MlmeInit failed, Status[=0x%08x]\n", Status));
475                 goto err2;
476         }
477
478         // Initialize pAd->StaCfg, pAd->ApCfg, pAd->CommonCfg to manufacture default
479         //
480         UserCfgInit(pAd);
481
482 #ifdef RT2870
483         // We need init timerQ related structure before create the timer thread.
484         RT2870_TimerQ_Init(pAd);
485 #endif // RT2870 //
486
487         RT28XX_TASK_THREAD_INIT(pAd, Status);
488         if (Status != NDIS_STATUS_SUCCESS)
489                 goto err1;
490
491 //      COPY_MAC_ADDR(pAd->ApCfg.MBSSID[apidx].Bssid, netif->hwaddr);
492 //      pAd->bForcePrintTX = TRUE;
493
494         CfgInitHook(pAd);
495
496 #ifdef CONFIG_STA_SUPPORT
497         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
498                 NdisAllocateSpinLock(&pAd->MacTabLock);
499 #endif // CONFIG_STA_SUPPORT //
500
501         MeasureReqTabInit(pAd);
502         TpcReqTabInit(pAd);
503
504         //
505         // Init the hardware, we need to init asic before read registry, otherwise mac register will be reset
506         //
507         Status = NICInitializeAdapter(pAd, TRUE);
508         if (Status != NDIS_STATUS_SUCCESS)
509         {
510                 DBGPRINT_ERR(("NICInitializeAdapter failed, Status[=0x%08x]\n", Status));
511                 if (Status != NDIS_STATUS_SUCCESS)
512                 goto err3;
513         }
514
515         // Read parameters from Config File
516         Status = RTMPReadParametersHook(pAd);
517
518         printk("1. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
519         if (Status != NDIS_STATUS_SUCCESS)
520         {
521                 DBGPRINT_ERR(("NICReadRegParameters failed, Status[=0x%08x]\n",Status));
522                 goto err4;
523         }
524
525 #ifdef RT2870
526         pAd->CommonCfg.bMultipleIRP = FALSE;
527
528         if (pAd->CommonCfg.bMultipleIRP)
529                 pAd->CommonCfg.NumOfBulkInIRP = RX_RING_SIZE;
530         else
531                 pAd->CommonCfg.NumOfBulkInIRP = 1;
532 #endif // RT2870 //
533
534
535         //Init Ba Capability parameters.
536 //      RT28XX_BA_INIT(pAd);
537 #ifdef DOT11_N_SUPPORT
538         pAd->CommonCfg.DesiredHtPhy.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
539         pAd->CommonCfg.DesiredHtPhy.AmsduEnable = (USHORT)pAd->CommonCfg.BACapability.field.AmsduEnable;
540         pAd->CommonCfg.DesiredHtPhy.AmsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
541         pAd->CommonCfg.DesiredHtPhy.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
542         // UPdata to HT IE
543         pAd->CommonCfg.HtCapability.HtCapInfo.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
544         pAd->CommonCfg.HtCapability.HtCapInfo.AMsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
545         pAd->CommonCfg.HtCapability.HtCapParm.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
546 #endif // DOT11_N_SUPPORT //
547
548         // after reading Registry, we now know if in AP mode or STA mode
549
550         // Load 8051 firmware; crash when FW image not existent
551         // Status = NICLoadFirmware(pAd);
552         // if (Status != NDIS_STATUS_SUCCESS)
553         //    break;
554
555         printk("2. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
556
557         // We should read EEPROM for all cases.  rt2860b
558         NICReadEEPROMParameters(pAd, mac);
559 #ifdef CONFIG_STA_SUPPORT
560 #endif // CONFIG_STA_SUPPORT //
561
562         printk("3. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
563
564         NICInitAsicFromEEPROM(pAd); //rt2860b
565
566         // Set PHY to appropriate mode
567         TmpPhy = pAd->CommonCfg.PhyMode;
568         pAd->CommonCfg.PhyMode = 0xff;
569         RTMPSetPhyMode(pAd, TmpPhy);
570 #ifdef DOT11_N_SUPPORT
571         SetCommonHT(pAd);
572 #endif // DOT11_N_SUPPORT //
573
574         // No valid channels.
575         if (pAd->ChannelListNum == 0)
576         {
577                 printk("Wrong configuration. No valid channel found. Check \"ContryCode\" and \"ChannelGeography\" setting.\n");
578                 goto err4;
579         }
580
581 #ifdef DOT11_N_SUPPORT
582         printk("MCS Set = %02x %02x %02x %02x %02x\n", pAd->CommonCfg.HtCapability.MCSSet[0],
583            pAd->CommonCfg.HtCapability.MCSSet[1], pAd->CommonCfg.HtCapability.MCSSet[2],
584            pAd->CommonCfg.HtCapability.MCSSet[3], pAd->CommonCfg.HtCapability.MCSSet[4]);
585 #endif // DOT11_N_SUPPORT //
586
587 #ifdef RT2870
588     //Init RT30xx RFRegisters after read RFIC type from EEPROM
589         NICInitRT30xxRFRegisters(pAd);
590 #endif // RT2870 //
591
592 #if 0
593         // Patch cardbus controller if EEPROM said so.
594         if (pAd->bTest1 == FALSE)
595                 RTMPPatchCardBus(pAd);
596 #endif
597
598
599 //              APInitialize(pAd);
600
601 #ifdef IKANOS_VX_1X0
602         VR_IKANOS_FP_Init(pAd->ApCfg.BssidNum, pAd->PermanentAddress);
603 #endif // IKANOS_VX_1X0 //
604
605                 //
606         // Initialize RF register to default value
607         //
608         AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
609         AsicLockChannel(pAd, pAd->CommonCfg.Channel);
610
611         // 8051 firmware require the signal during booting time.
612         AsicSendCommandToMcu(pAd, 0x72, 0xFF, 0x00, 0x00);
613
614         if (pAd && (Status != NDIS_STATUS_SUCCESS))
615         {
616                 //
617                 // Undo everything if it failed
618                 //
619                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
620                 {
621 //                      NdisMDeregisterInterrupt(&pAd->Interrupt);
622                         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
623                 }
624 //              RTMPFreeAdapter(pAd); // we will free it in disconnect()
625         }
626         else if (pAd)
627         {
628                 // Microsoft HCT require driver send a disconnect event after driver initialization.
629                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
630 //              pAd->IndicateMediaState = NdisMediaStateDisconnected;
631                 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE);
632
633                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event B!\n"));
634
635
636 #ifdef RT2870
637                 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS);
638                 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_REMOVE_IN_PROGRESS);
639
640                 //
641                 // Support multiple BulkIn IRP,
642                 // the value on pAd->CommonCfg.NumOfBulkInIRP may be large than 1.
643                 //
644                 for(index=0; index<pAd->CommonCfg.NumOfBulkInIRP; index++)
645                 {
646                         RTUSBBulkReceive(pAd);
647                         DBGPRINT(RT_DEBUG_TRACE, ("RTUSBBulkReceive!\n" ));
648                 }
649 #endif // RT2870 //
650         }// end of else
651
652
653         DBGPRINT_S(Status, ("<==== RTMPInitialize, Status=%x\n", Status));
654
655         return TRUE;
656
657
658 err4:
659 err3:
660         MlmeHalt(pAd);
661 err2:
662         RTMPFreeTxRxRingMemory(pAd);
663 //      RTMPFreeAdapter(pAd);
664 err1:
665
666 #ifdef DOT11_N_SUPPORT
667         os_free_mem(pAd, pAd->mpdu_blk_pool.mem); // free BA pool
668 #endif // DOT11_N_SUPPORT //
669         RT28XX_IRQ_RELEASE(net_dev);
670
671         // shall not set ml_priv to NULL here because the ml_priv didn't been free yet.
672         //net_dev->ml_priv = 0;
673 #ifdef INF_AMAZON_SE
674 err0:
675 #endif // INF_AMAZON_SE //
676         printk("!!! %s Initialized fail !!!\n", RT28xx_CHIP_NAME);
677         return FALSE;
678 } /* End of rt28xx_init */
679
680
681 /*
682 ========================================================================
683 Routine Description:
684     Open raxx interface.
685
686 Arguments:
687         *net_dev                        the raxx interface pointer
688
689 Return Value:
690     0                                   Open OK
691         otherwise                       Open Fail
692
693 Note:
694 ========================================================================
695 */
696 int rt28xx_open(IN PNET_DEV dev)
697 {
698         struct net_device * net_dev = (struct net_device *)dev;
699         PRTMP_ADAPTER pAd = net_dev->ml_priv;
700         int retval = 0;
701         POS_COOKIE pObj;
702
703
704         // Sanity check for pAd
705         if (pAd == NULL)
706         {
707                 /* if 1st open fail, pAd will be free;
708                    So the net_dev->ml_priv will be NULL in 2rd open */
709                 return -1;
710         }
711
712 #ifdef CONFIG_STA_SUPPORT
713 #endif // CONFIG_STA_SUPPORT //
714
715         // Init
716         pObj = (POS_COOKIE)pAd->OS_Cookie;
717
718         // reset Adapter flags
719         RTMP_CLEAR_FLAGS(pAd);
720
721         // Request interrupt service routine for PCI device
722         // register the interrupt routine with the os
723         RT28XX_IRQ_REQUEST(net_dev);
724
725
726         // Init BssTab & ChannelInfo tabbles for auto channel select.
727
728
729         // Chip & other init
730         if (rt28xx_init(net_dev) == FALSE)
731                 goto err;
732
733 #ifdef CONFIG_STA_SUPPORT
734         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
735         {
736                 NdisZeroMemory(pAd->StaCfg.dev_name, 16);
737                 NdisMoveMemory(pAd->StaCfg.dev_name, net_dev->name, strlen(net_dev->name));
738         }
739 #endif // CONFIG_STA_SUPPORT //
740
741         // Set up the Mac address
742         NdisMoveMemory(net_dev->dev_addr, (void *) pAd->CurrentAddress, 6);
743
744         // Init IRQ parameters
745         RT28XX_IRQ_INIT(pAd);
746
747         // Various AP function init
748
749 #ifdef CONFIG_STA_SUPPORT
750         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
751         {
752 #ifdef WPA_SUPPLICANT_SUPPORT
753 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
754                 {
755                         union iwreq_data    wrqu;
756                         // send wireless event to wpa_supplicant for infroming interface down.
757                         memset(&wrqu, 0, sizeof(wrqu));
758                         wrqu.data.flags = RT_INTERFACE_UP;
759                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
760                 }
761 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
762 #endif // WPA_SUPPLICANT_SUPPORT //
763
764         }
765 #endif // CONFIG_STA_SUPPORT //
766
767         // Enable Interrupt
768         RT28XX_IRQ_ENABLE(pAd);
769
770         // Now Enable RxTx
771         RTMPEnableRxTx(pAd);
772         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP);
773
774         {
775         UINT32 reg = 0;
776         RTMP_IO_READ32(pAd, 0x1300, &reg);  // clear garbage interrupts
777         printk("0x1300 = %08x\n", reg);
778         }
779
780         {
781 //      u32 reg;
782 //      u8  byte;
783 //      u16 tmp;
784
785 //      RTMP_IO_READ32(pAd, XIFS_TIME_CFG, &reg);
786
787 //      tmp = 0x0805;
788 //      reg  = (reg & 0xffff0000) | tmp;
789 //      RTMP_IO_WRITE32(pAd, XIFS_TIME_CFG, reg);
790
791         }
792
793 #if 0
794         /*
795          * debugging helper
796          *              show the size of main table in Adapter structure
797          *              MacTab  -- 185K
798          *              BATable -- 137K
799          *              Total   -- 385K  !!!!! (5/26/2006)
800          */
801         printk("sizeof(pAd->MacTab) = %ld\n", sizeof(pAd->MacTab));
802         printk("sizeof(pAd->AccessControlList) = %ld\n", sizeof(pAd->AccessControlList));
803         printk("sizeof(pAd->ApCfg) = %ld\n", sizeof(pAd->ApCfg));
804         printk("sizeof(pAd->BATable) = %ld\n", sizeof(pAd->BATable));
805         BUG();
806 #endif
807
808 #ifdef CONFIG_STA_SUPPORT
809 #endif // CONFIG_STA_SUPPORT //
810
811         return (retval);
812
813 err:
814         return (-1);
815 } /* End of rt28xx_open */
816
817 static const struct net_device_ops rt2870_netdev_ops = {
818         .ndo_open               = MainVirtualIF_open,
819         .ndo_stop               = MainVirtualIF_close,
820         .ndo_do_ioctl           = rt28xx_ioctl,
821         .ndo_get_stats          = RT28xx_get_ether_stats,
822         .ndo_validate_addr      = NULL,
823         .ndo_set_mac_address    = eth_mac_addr,
824         .ndo_change_mtu         = eth_change_mtu,
825 #ifdef IKANOS_VX_1X0
826         .ndo_start_xmit         = IKANOS_DataFramesTx,
827 #else
828         .ndo_start_xmit         = rt28xx_send_packets,
829 #endif
830 };
831
832 /* Must not be called for mdev and apdev */
833 static NDIS_STATUS rt_ieee80211_if_setup(struct net_device *dev, PRTMP_ADAPTER pAd)
834 {
835         NDIS_STATUS Status;
836         INT     i=0;
837         CHAR    slot_name[IFNAMSIZ];
838         struct net_device   *device;
839
840
841         //ether_setup(dev);
842 //      dev->set_multicast_list = ieee80211_set_multicast_list;
843 //      dev->change_mtu = ieee80211_change_mtu;
844 #ifdef CONFIG_STA_SUPPORT
845 #if WIRELESS_EXT >= 12
846         if (pAd->OpMode == OPMODE_STA)
847         {
848                 dev->wireless_handlers = &rt28xx_iw_handler_def;
849         }
850 #endif //WIRELESS_EXT >= 12
851 #endif // CONFIG_STA_SUPPORT //
852
853 #if WIRELESS_EXT < 21
854                 dev->get_wireless_stats = rt28xx_get_wireless_stats;
855 #endif
856 //      dev->uninit = ieee80211_if_reinit;
857 //      dev->destructor = ieee80211_if_free;
858         dev->priv_flags = INT_MAIN;
859         dev->netdev_ops = &rt2870_netdev_ops;
860         // find available device name
861         for (i = 0; i < 8; i++)
862         {
863 #ifdef MULTIPLE_CARD_SUPPORT
864                 if (pAd->MC_RowID >= 0)
865                         sprintf(slot_name, "ra%02d_%d", pAd->MC_RowID, i);
866                 else
867 #endif // MULTIPLE_CARD_SUPPORT //
868                 sprintf(slot_name, "ra%d", i);
869
870                 device = dev_get_by_name(dev_net(dev), slot_name);
871                 if (device != NULL)
872                         dev_put(device);
873
874                 if (device == NULL)
875                         break;
876         }
877
878         if(i == 8)
879         {
880                 DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
881                 Status = NDIS_STATUS_FAILURE;
882         }
883         else
884         {
885 #ifdef MULTIPLE_CARD_SUPPORT
886                 if (pAd->MC_RowID >= 0)
887                 sprintf(dev->name, "ra%02d_%d", pAd->MC_RowID, i);
888                 else
889 #endif // MULTIPLE_CARD_SUPPORT //
890                 sprintf(dev->name, "ra%d", i);
891                 Status = NDIS_STATUS_SUCCESS;
892         }
893
894         return Status;
895
896 }
897
898
899 #ifdef MULTIPLE_CARD_SUPPORT
900 /*
901 ========================================================================
902 Routine Description:
903     Get card profile path.
904
905 Arguments:
906     pAd
907
908 Return Value:
909     TRUE                - Find a card profile
910         FALSE           - use default profile
911
912 Note:
913 ========================================================================
914 */
915 extern INT RTMPGetKeyParameter(
916     IN  PCHAR   key,
917     OUT PCHAR   dest,
918     IN  INT     destsize,
919     IN  PCHAR   buffer);
920
921 BOOLEAN RTMP_CardInfoRead(
922         IN      PRTMP_ADAPTER pAd)
923 {
924 #define MC_SELECT_CARDID                0       /* use CARD ID (0 ~ 31) to identify different cards */
925 #define MC_SELECT_MAC                   1       /* use CARD MAC to identify different cards */
926 #define MC_SELECT_CARDTYPE              2       /* use CARD type (abgn or bgn) to identify different cards */
927
928 #define LETTER_CASE_TRANSLATE(txt_p, card_id)                   \
929         {       UINT32 _len; char _char;                                                \
930                 for(_len=0; _len<strlen(card_id); _len++) {             \
931                         _char = *(txt_p + _len);                                        \
932                         if (('A' <= _char) && (_char <= 'Z'))           \
933                                 *(txt_p+_len) = 'a'+(_char-'A');                \
934                 } }
935
936         struct file *srcf;
937         INT retval, orgfsuid, orgfsgid;
938         mm_segment_t orgfs;
939         CHAR *buffer, *tmpbuf, card_id_buf[30], RFIC_word[30];
940         BOOLEAN flg_match_ok = FALSE;
941         INT32 card_select_method;
942         INT32 card_free_id, card_nouse_id, card_same_mac_id, card_match_id;
943         EEPROM_ANTENNA_STRUC antenna;
944         USHORT addr01, addr23, addr45;
945         UINT8 mac[6];
946         UINT32 data, card_index;
947         UCHAR *start_ptr;
948
949
950         // init
951         buffer = kmalloc(MAX_INI_BUFFER_SIZE, MEM_ALLOC_FLAG);
952         if (buffer == NULL)
953         return FALSE;
954
955         tmpbuf = kmalloc(MAX_PARAM_BUFFER_SIZE, MEM_ALLOC_FLAG);
956         if(tmpbuf == NULL)
957         {
958                 kfree(buffer);
959         return NDIS_STATUS_FAILURE;
960         }
961
962         orgfsuid = current->fsuid;
963         orgfsgid = current->fsgid;
964         current->fsuid = current->fsgid = 0;
965     orgfs = get_fs();
966     set_fs(KERNEL_DS);
967
968         // get RF IC type
969         RTMP_IO_READ32(pAd, E2PROM_CSR, &data);
970
971         if ((data & 0x30) == 0)
972                 pAd->EEPROMAddressNum = 6;      // 93C46
973         else if ((data & 0x30) == 0x10)
974                 pAd->EEPROMAddressNum = 8;      // 93C66
975         else
976                 pAd->EEPROMAddressNum = 8;      // 93C86
977
978         //antenna.word = RTMP_EEPROM_READ16(pAd, EEPROM_NIC1_OFFSET);
979         RT28xx_EEPROM_READ16(pAd, EEPROM_NIC1_OFFSET, antenna.word);
980
981         if ((antenna.field.RfIcType == RFIC_2850) ||
982                 (antenna.field.RfIcType == RFIC_2750))
983         {
984                 /* ABGN card */
985                 strcpy(RFIC_word, "abgn");
986         }
987         else
988         {
989                 /* BGN card */
990                 strcpy(RFIC_word, "bgn");
991         }
992
993         // get MAC address
994         //addr01 = RTMP_EEPROM_READ16(pAd, 0x04);
995         //addr23 = RTMP_EEPROM_READ16(pAd, 0x06);
996         //addr45 = RTMP_EEPROM_READ16(pAd, 0x08);
997         RT28xx_EEPROM_READ16(pAd, 0x04, addr01);
998         RT28xx_EEPROM_READ16(pAd, 0x06, addr23);
999         RT28xx_EEPROM_READ16(pAd, 0x08, addr45);
1000
1001         mac[0] = (UCHAR)(addr01 & 0xff);
1002         mac[1] = (UCHAR)(addr01 >> 8);
1003         mac[2] = (UCHAR)(addr23 & 0xff);
1004         mac[3] = (UCHAR)(addr23 >> 8);
1005         mac[4] = (UCHAR)(addr45 & 0xff);
1006         mac[5] = (UCHAR)(addr45 >> 8);
1007
1008         // open card information file
1009         srcf = filp_open(CARD_INFO_PATH, O_RDONLY, 0);
1010         if (IS_ERR(srcf))
1011         {
1012                 /* card information file does not exist */
1013                         DBGPRINT(RT_DEBUG_TRACE,
1014                                 ("--> Error %ld opening %s\n", -PTR_ERR(srcf), CARD_INFO_PATH));
1015                 return FALSE;
1016         }
1017
1018         if (srcf->f_op && srcf->f_op->read)
1019         {
1020                 /* card information file exists so reading the card information */
1021                 memset(buffer, 0x00, MAX_INI_BUFFER_SIZE);
1022                 retval = srcf->f_op->read(srcf, buffer, MAX_INI_BUFFER_SIZE, &srcf->f_pos);
1023                 if (retval < 0)
1024                 {
1025                         /* read fail */
1026                                 DBGPRINT(RT_DEBUG_TRACE,
1027                                         ("--> Read %s error %d\n", CARD_INFO_PATH, -retval));
1028                 }
1029                 else
1030                 {
1031                         /* get card selection method */
1032                         memset(tmpbuf, 0x00, MAX_PARAM_BUFFER_SIZE);
1033                         card_select_method = MC_SELECT_CARDTYPE; // default
1034
1035                         if (RTMPGetKeyParameter("SELECT", tmpbuf, 256, buffer))
1036                         {
1037                                 if (strcmp(tmpbuf, "CARDID") == 0)
1038                                         card_select_method = MC_SELECT_CARDID;
1039                                 else if (strcmp(tmpbuf, "MAC") == 0)
1040                                         card_select_method = MC_SELECT_MAC;
1041                                 else if (strcmp(tmpbuf, "CARDTYPE") == 0)
1042                                         card_select_method = MC_SELECT_CARDTYPE;
1043                         }
1044
1045                         DBGPRINT(RT_DEBUG_TRACE,
1046                                         ("MC> Card Selection = %d\n", card_select_method));
1047
1048                         // init
1049                         card_free_id = -1;
1050                         card_nouse_id = -1;
1051                         card_same_mac_id = -1;
1052                         card_match_id = -1;
1053
1054                         // search current card information records
1055                         for(card_index=0;
1056                                 card_index<MAX_NUM_OF_MULTIPLE_CARD;
1057                                 card_index++)
1058                         {
1059                                 if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
1060                                         (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
1061                                 {
1062                                         // MAC is all-0 so the entry is available
1063                                         MC_CardUsed[card_index] = 0;
1064
1065                                         if (card_free_id < 0)
1066                                                 card_free_id = card_index; // 1st free entry
1067                                 }
1068                                 else
1069                                 {
1070                                         if (memcmp(MC_CardMac[card_index], mac, 6) == 0)
1071                                         {
1072                                                 // we find the entry with same MAC
1073                                                 if (card_same_mac_id < 0)
1074                                                         card_same_mac_id = card_index; // 1st same entry
1075                                         }
1076                                         else
1077                                         {
1078                                                 // MAC is not all-0 but used flag == 0
1079                                                 if ((MC_CardUsed[card_index] == 0) &&
1080                                                         (card_nouse_id < 0))
1081                                                 {
1082                                                         card_nouse_id = card_index; // 1st available entry
1083                                                 }
1084                                         }
1085                                 }
1086                         }
1087
1088                         DBGPRINT(RT_DEBUG_TRACE,
1089                                         ("MC> Free = %d, Same = %d, NOUSE = %d\n",
1090                                         card_free_id, card_same_mac_id, card_nouse_id));
1091
1092                         if ((card_same_mac_id >= 0) &&
1093                                 ((card_select_method == MC_SELECT_CARDID) ||
1094                                 (card_select_method == MC_SELECT_CARDTYPE)))
1095                         {
1096                                 // same MAC entry is found
1097                                 card_match_id = card_same_mac_id;
1098
1099                                 if (card_select_method == MC_SELECT_CARDTYPE)
1100                                 {
1101                                         // for CARDTYPE
1102                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
1103                                                         card_match_id, RFIC_word);
1104
1105                                         if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
1106                                         {
1107                                                 // we found the card ID
1108                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1109                                         }
1110                                 }
1111                         }
1112                         else
1113                         {
1114                                 // the card is 1st plug-in, try to find the match card profile
1115                                 switch(card_select_method)
1116                                 {
1117                                         case MC_SELECT_CARDID: // CARDID
1118                                         default:
1119                                                 if (card_free_id >= 0)
1120                                                         card_match_id = card_free_id;
1121                                                 else
1122                                                         card_match_id = card_nouse_id;
1123                                                 break;
1124
1125                                         case MC_SELECT_MAC: // MAC
1126                                                 sprintf(card_id_buf, "MAC%02x:%02x:%02x:%02x:%02x:%02x",
1127                                                                 mac[0], mac[1], mac[2],
1128                                                                 mac[3], mac[4], mac[5]);
1129
1130                                                 /* try to find the key word in the card file */
1131                                                 if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
1132                                                 {
1133                                                         LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1134
1135                                                         /* get the row ID (2 ASCII characters) */
1136                                                         start_ptr -= 2;
1137                                                         card_id_buf[0] = *(start_ptr);
1138                                                         card_id_buf[1] = *(start_ptr+1);
1139                                                         card_id_buf[2] = 0x00;
1140
1141                                                         card_match_id = simple_strtol(card_id_buf, 0, 10);
1142                                                 }
1143                                                 break;
1144
1145                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1146                                                 card_nouse_id = -1;
1147
1148                                                 for(card_index=0;
1149                                                         card_index<MAX_NUM_OF_MULTIPLE_CARD;
1150                                                         card_index++)
1151                                                 {
1152                                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
1153                                                                         card_index, RFIC_word);
1154
1155                                                         if ((start_ptr=rtstrstruncasecmp(buffer,
1156                                                                                                                 card_id_buf)) != NULL)
1157                                                         {
1158                                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1159
1160                                                                 if (MC_CardUsed[card_index] == 0)
1161                                                                 {
1162                                                                         /* current the card profile is not used */
1163                                                                         if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
1164                                                                                 (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
1165                                                                         {
1166                                                                                 // find it and no previous card use it
1167                                                                                 card_match_id = card_index;
1168                                                                                 break;
1169                                                                         }
1170                                                                         else
1171                                                                         {
1172                                                                                 // ever a card use it
1173                                                                                 if (card_nouse_id < 0)
1174                                                                                         card_nouse_id = card_index;
1175                                                                         }
1176                                                                 }
1177                                                         }
1178                                                 }
1179
1180                                                 // if not find a free one, use the available one
1181                                                 if (card_match_id < 0)
1182                                                         card_match_id = card_nouse_id;
1183                                                 break;
1184                                 }
1185                         }
1186
1187                         if (card_match_id >= 0)
1188                         {
1189                                 // make up search keyword
1190                                 switch(card_select_method)
1191                                 {
1192                                         case MC_SELECT_CARDID: // CARDID
1193                                                 sprintf(card_id_buf, "%02dCARDID", card_match_id);
1194                                                 break;
1195
1196                                         case MC_SELECT_MAC: // MAC
1197                                                 sprintf(card_id_buf,
1198                                                                 "%02dmac%02x:%02x:%02x:%02x:%02x:%02x",
1199                                                                 card_match_id,
1200                                                                 mac[0], mac[1], mac[2],
1201                                                                 mac[3], mac[4], mac[5]);
1202                                                 break;
1203
1204                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1205                                         default:
1206                                                 sprintf(card_id_buf, "%02dcardtype%s",
1207                                                                 card_match_id, RFIC_word);
1208                                                 break;
1209                                 }
1210
1211                                 DBGPRINT(RT_DEBUG_TRACE, ("Search Keyword = %s\n", card_id_buf));
1212
1213                                 // read card file path
1214                                 if (RTMPGetKeyParameter(card_id_buf, tmpbuf, 256, buffer))
1215                                 {
1216                                         if (strlen(tmpbuf) < sizeof(pAd->MC_FileName))
1217                                         {
1218                                                 // backup card information
1219                                                 pAd->MC_RowID = card_match_id; /* base 0 */
1220                                                 MC_CardUsed[card_match_id] = 1;
1221                                                 memcpy(MC_CardMac[card_match_id], mac, sizeof(mac));
1222
1223                                                 // backup card file path
1224                                                 NdisMoveMemory(pAd->MC_FileName, tmpbuf , strlen(tmpbuf));
1225                                                 pAd->MC_FileName[strlen(tmpbuf)] = '\0';
1226                                                 flg_match_ok = TRUE;
1227
1228                                                 DBGPRINT(RT_DEBUG_TRACE,
1229                                                                 ("Card Profile Name = %s\n", pAd->MC_FileName));
1230                                         }
1231                                         else
1232                                         {
1233                                                 DBGPRINT(RT_DEBUG_ERROR,
1234                                                                 ("Card Profile Name length too large!\n"));
1235                                         }
1236                                 }
1237                                 else
1238                                 {
1239                                         DBGPRINT(RT_DEBUG_ERROR,
1240                                                         ("Can not find search key word in card.dat!\n"));
1241                                 }
1242
1243                                 if ((flg_match_ok != TRUE) &&
1244                                         (card_match_id < MAX_NUM_OF_MULTIPLE_CARD))
1245                                 {
1246                                         MC_CardUsed[card_match_id] = 0;
1247                                         memset(MC_CardMac[card_match_id], 0, sizeof(mac));
1248                                 }
1249                         } // if (card_match_id >= 0)
1250                 }
1251         }
1252
1253         // close file
1254         retval = filp_close(srcf, NULL);
1255         set_fs(orgfs);
1256         current->fsuid = orgfsuid;
1257         current->fsgid = orgfsgid;
1258         kfree(buffer);
1259         kfree(tmpbuf);
1260         return flg_match_ok;
1261 }
1262 #endif // MULTIPLE_CARD_SUPPORT //
1263
1264
1265 /*
1266 ========================================================================
1267 Routine Description:
1268     Probe RT28XX chipset.
1269
1270 Arguments:
1271     _dev_p                              Point to the PCI or USB device
1272         _dev_id_p                       Point to the PCI or USB device ID
1273
1274 Return Value:
1275     0                                   Probe OK
1276         -ENODEV                         Probe Fail
1277
1278 Note:
1279 ========================================================================
1280 */
1281 INT __devinit   rt28xx_probe(
1282     IN  void *_dev_p,
1283     IN  void *_dev_id_p,
1284         IN  UINT argc,
1285         OUT PRTMP_ADAPTER *ppAd)
1286 {
1287     struct  net_device  *net_dev;
1288     PRTMP_ADAPTER       pAd = (PRTMP_ADAPTER) NULL;
1289     INT                 status;
1290         PVOID                           handle;
1291 #ifdef RT2870
1292         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1293         struct usb_device *dev_p = interface_to_usbdev(intf);
1294
1295         dev_p = usb_get_dev(dev_p);
1296 #endif // RT2870 //
1297
1298
1299 #ifdef CONFIG_STA_SUPPORT
1300     DBGPRINT(RT_DEBUG_TRACE, ("STA Driver version-%s\n", STA_DRIVER_VERSION));
1301 #endif // CONFIG_STA_SUPPORT //
1302
1303         // Check chipset vendor/product ID
1304 //      if (RT28XXChipsetCheck(_dev_p) == FALSE)
1305 //              goto err_out;
1306
1307     net_dev = alloc_etherdev(sizeof(PRTMP_ADAPTER));
1308     if (net_dev == NULL)
1309     {
1310         printk("alloc_netdev failed\n");
1311
1312         goto err_out;
1313     }
1314
1315 // sample
1316 //      if (rt_ieee80211_if_setup(net_dev) != NDIS_STATUS_SUCCESS)
1317 //              goto err_out;
1318
1319         netif_stop_queue(net_dev);
1320 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
1321 /* for supporting Network Manager */
1322 /* Set the sysfs physical device reference for the network logical device
1323  * if set prior to registration will cause a symlink during initialization.
1324  */
1325     SET_NETDEV_DEV(net_dev, &(dev_p->dev));
1326 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
1327
1328         // Allocate RTMP_ADAPTER miniport adapter structure
1329         handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
1330         RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p);
1331
1332         status = RTMPAllocAdapterBlock(handle, &pAd);
1333         if (status != NDIS_STATUS_SUCCESS)
1334                 goto err_out_free_netdev;
1335
1336         net_dev->ml_priv = (PVOID)pAd;
1337     pAd->net_dev = net_dev; // must be before RT28XXNetDevInit()
1338
1339         RT28XXNetDevInit(_dev_p, net_dev, pAd);
1340
1341 #ifdef CONFIG_STA_SUPPORT
1342     pAd->StaCfg.OriDevType = net_dev->type;
1343 #endif // CONFIG_STA_SUPPORT //
1344
1345         // Find and assign a free interface name, raxx
1346 //      RT28XXAvailRANameAssign(net_dev->name);
1347
1348         // Post config
1349         if (RT28XXProbePostConfig(_dev_p, pAd, 0) == FALSE)
1350                 goto err_out_unmap;
1351
1352 #ifdef CONFIG_STA_SUPPORT
1353         pAd->OpMode = OPMODE_STA;
1354 #endif // CONFIG_STA_SUPPORT //
1355
1356
1357 #ifdef MULTIPLE_CARD_SUPPORT
1358         // find its profile path
1359         pAd->MC_RowID = -1; // use default profile path
1360         RTMP_CardInfoRead(pAd);
1361
1362         if (pAd->MC_RowID == -1)
1363 #ifdef CONFIG_STA_SUPPORT
1364                 strcpy(pAd->MC_FileName, STA_PROFILE_PATH);
1365 #endif // CONFIG_STA_SUPPORT //
1366
1367         DBGPRINT(RT_DEBUG_TRACE,
1368                         ("MC> ROW = %d, PATH = %s\n", pAd->MC_RowID, pAd->MC_FileName));
1369 #endif // MULTIPLE_CARD_SUPPORT //
1370
1371         // sample move
1372         if (rt_ieee80211_if_setup(net_dev, pAd) != NDIS_STATUS_SUCCESS)
1373                 goto err_out_unmap;
1374
1375     // Register this device
1376     status = register_netdev(net_dev);
1377     if (status)
1378         goto err_out_unmap;
1379
1380     // Set driver data
1381         RT28XX_DRVDATA_SET(_dev_p);
1382
1383
1384
1385         *ppAd = pAd;
1386     return 0; // probe ok
1387
1388
1389         /* --------------------------- ERROR HANDLE --------------------------- */
1390 err_out_unmap:
1391         RTMPFreeAdapter(pAd);
1392         RT28XX_UNMAP();
1393
1394 err_out_free_netdev:
1395         free_netdev(net_dev);
1396
1397 err_out:
1398         RT28XX_PUT_DEVICE(dev_p);
1399
1400         return -ENODEV; /* probe fail */
1401 } /* End of rt28xx_probe */
1402
1403
1404 /*
1405 ========================================================================
1406 Routine Description:
1407     The entry point for Linux kernel sent packet to our driver.
1408
1409 Arguments:
1410     sk_buff *skb                the pointer refer to a sk_buffer.
1411
1412 Return Value:
1413     0
1414
1415 Note:
1416         This function is the entry point of Tx Path for Os delivery packet to
1417         our driver. You only can put OS-depened & STA/AP common handle procedures
1418         in here.
1419 ========================================================================
1420 */
1421 int rt28xx_packet_xmit(struct sk_buff *skb)
1422 {
1423         struct net_device *net_dev = skb->dev;
1424         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1425         int status = 0;
1426         PNDIS_PACKET pPacket = (PNDIS_PACKET) skb;
1427
1428 #ifdef CONFIG_STA_SUPPORT
1429         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1430         {
1431                 // Drop send request since we are in monitor mode
1432                 if (MONITOR_ON(pAd))
1433                 {
1434                         RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1435                         goto done;
1436                 }
1437         }
1438 #endif // CONFIG_STA_SUPPORT //
1439
1440         // EapolStart size is 18
1441         if (skb->len < 14)
1442         {
1443                 //printk("bad packet size: %d\n", pkt->len);
1444                 hex_dump("bad packet", skb->data, skb->len);
1445                 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1446                 goto done;
1447         }
1448
1449 #if 0
1450 //      if ((pkt->data[0] & 0x1) == 0)
1451         {
1452                 //hex_dump(__func__, pkt->data, pkt->len);
1453                 printk("pPacket = %x\n", pPacket);
1454         }
1455 #endif
1456
1457         RTMP_SET_PACKET_5VT(pPacket, 0);
1458 //      MiniportMMRequest(pAd, pkt->data, pkt->len);
1459 #ifdef CONFIG_5VT_ENHANCE
1460     if (*(int*)(skb->cb) == BRIDGE_TAG) {
1461                 RTMP_SET_PACKET_5VT(pPacket, 1);
1462     }
1463 #endif
1464
1465
1466
1467 #ifdef CONFIG_STA_SUPPORT
1468         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1469         {
1470
1471                 STASendPackets((NDIS_HANDLE)pAd, (PPNDIS_PACKET) &pPacket, 1);
1472         }
1473
1474 #endif // CONFIG_STA_SUPPORT //
1475
1476         status = 0;
1477 done:
1478
1479         return status;
1480 }
1481
1482
1483 /*
1484 ========================================================================
1485 Routine Description:
1486     Send a packet to WLAN.
1487
1488 Arguments:
1489     skb_p           points to our adapter
1490     dev_p           which WLAN network interface
1491
1492 Return Value:
1493     0: transmit successfully
1494     otherwise: transmit fail
1495
1496 Note:
1497 ========================================================================
1498 */
1499 INT rt28xx_send_packets(
1500         IN struct sk_buff               *skb_p,
1501         IN struct net_device    *net_dev)
1502 {
1503     RTMP_ADAPTER *pAd = net_dev->ml_priv;
1504
1505         if (!(net_dev->flags & IFF_UP))
1506         {
1507                 RELEASE_NDIS_PACKET(pAd, (PNDIS_PACKET)skb_p, NDIS_STATUS_FAILURE);
1508                 return 0;
1509         }
1510
1511         NdisZeroMemory((PUCHAR)&skb_p->cb[CB_OFF], 15);
1512         RTMP_SET_PACKET_NET_DEVICE_MBSSID(skb_p, MAIN_MBSSID);
1513
1514         return rt28xx_packet_xmit(skb_p);
1515 } /* End of MBSS_VirtualIF_PacketSend */
1516
1517
1518
1519
1520 void CfgInitHook(PRTMP_ADAPTER pAd)
1521 {
1522         pAd->bBroadComHT = TRUE;
1523 } /* End of CfgInitHook */
1524
1525
1526 #if 0   // Not used now, should keep it in our source tree??
1527 /*
1528 ========================================================================
1529 Routine Description:
1530     Find and assign a free interface name (raxx).
1531
1532 Arguments:
1533     *name_p                             the interface name pointer
1534
1535 Return Value:
1536         TRUE                            OK
1537         FALSE                           FAIL
1538
1539 Note:
1540 ========================================================================
1541 */
1542 static BOOLEAN RT28XXAvailRANameAssign(
1543         IN CHAR                 *name_p)
1544 {
1545     CHAR                                slot_name[IFNAMSIZ];
1546     struct net_device   *device;
1547         UINT32                          if_id;
1548
1549
1550     for(if_id=0; if_id<8; if_id++)
1551     {
1552         sprintf(slot_name, "ra%d", if_id);
1553
1554         for(device=dev_base; device!=NULL; device=device->next)
1555         {
1556             if (strncmp(device->name, slot_name, 4) == 0)
1557                 break;
1558         }
1559
1560         if (device == NULL)
1561                         break;
1562     }
1563
1564     if (if_id == 8)
1565     {
1566         DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
1567         return FALSE;
1568     }
1569
1570     sprintf(name_p, "ra%d", if_id);
1571         return TRUE;
1572 } /* End of RT28XXAvailRANameAssign */
1573 #endif
1574
1575 #if WIRELESS_EXT >= 12
1576 // This function will be called when query /proc
1577 struct iw_statistics *rt28xx_get_wireless_stats(
1578     IN struct net_device *net_dev)
1579 {
1580         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1581
1582
1583         DBGPRINT(RT_DEBUG_TRACE, ("rt28xx_get_wireless_stats --->\n"));
1584
1585         pAd->iw_stats.status = 0; // Status - device dependent for now
1586
1587         // link quality
1588         pAd->iw_stats.qual.qual = ((pAd->Mlme.ChannelQuality * 12)/10 + 10);
1589         if(pAd->iw_stats.qual.qual > 100)
1590                 pAd->iw_stats.qual.qual = 100;
1591
1592 #ifdef CONFIG_STA_SUPPORT
1593         if (pAd->OpMode == OPMODE_STA)
1594                 pAd->iw_stats.qual.level = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
1595 #endif // CONFIG_STA_SUPPORT //
1596
1597         pAd->iw_stats.qual.noise = pAd->BbpWriteLatch[66]; // noise level (dBm)
1598
1599         pAd->iw_stats.qual.noise += 256 - 143;
1600         pAd->iw_stats.qual.updated = 1;     // Flags to know if updated
1601 #ifdef IW_QUAL_DBM
1602         pAd->iw_stats.qual.updated |= IW_QUAL_DBM;      // Level + Noise are dBm
1603 #endif // IW_QUAL_DBM //
1604
1605         pAd->iw_stats.discard.nwid = 0;     // Rx : Wrong nwid/essid
1606         pAd->iw_stats.miss.beacon = 0;      // Missed beacons/superframe
1607
1608         DBGPRINT(RT_DEBUG_TRACE, ("<--- rt28xx_get_wireless_stats\n"));
1609         return &pAd->iw_stats;
1610 } /* End of rt28xx_get_wireless_stats */
1611 #endif // WIRELESS_EXT //
1612
1613
1614
1615 void tbtt_tasklet(unsigned long data)
1616 {
1617 #define MAX_TX_IN_TBTT          (16)
1618
1619 }
1620
1621 INT rt28xx_ioctl(
1622         IN      struct net_device       *net_dev,
1623         IN      OUT     struct ifreq    *rq,
1624         IN      INT                                     cmd)
1625 {
1626         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1627         RTMP_ADAPTER    *pAd = NULL;
1628         INT                             ret = 0;
1629
1630         if (net_dev->priv_flags == INT_MAIN)
1631         {
1632                 pAd = net_dev->ml_priv;
1633         }
1634         else
1635         {
1636                 pVirtualAd = net_dev->ml_priv;
1637                 pAd = pVirtualAd->RtmpDev->ml_priv;
1638         }
1639
1640         if (pAd == NULL)
1641         {
1642                 /* if 1st open fail, pAd will be free;
1643                    So the net_dev->ml_priv will be NULL in 2rd open */
1644                 return -ENETDOWN;
1645         }
1646
1647
1648 #ifdef CONFIG_STA_SUPPORT
1649         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1650         {
1651                 ret = rt28xx_sta_ioctl(net_dev, rq, cmd);
1652         }
1653 #endif // CONFIG_STA_SUPPORT //
1654
1655         return ret;
1656 }
1657
1658 /*
1659     ========================================================================
1660
1661     Routine Description:
1662         return ethernet statistics counter
1663
1664     Arguments:
1665         net_dev                     Pointer to net_device
1666
1667     Return Value:
1668         net_device_stats*
1669
1670     Note:
1671
1672     ========================================================================
1673 */
1674 struct net_device_stats *RT28xx_get_ether_stats(
1675     IN  struct net_device *net_dev)
1676 {
1677     RTMP_ADAPTER *pAd = NULL;
1678
1679         if (net_dev)
1680                 pAd = net_dev->ml_priv;
1681
1682         if (pAd)
1683         {
1684
1685                 pAd->stats.rx_packets = pAd->WlanCounters.ReceivedFragmentCount.QuadPart;
1686                 pAd->stats.tx_packets = pAd->WlanCounters.TransmittedFragmentCount.QuadPart;
1687
1688                 pAd->stats.rx_bytes = pAd->RalinkCounters.ReceivedByteCount;
1689                 pAd->stats.tx_bytes = pAd->RalinkCounters.TransmittedByteCount;
1690
1691                 pAd->stats.rx_errors = pAd->Counters8023.RxErrors;
1692                 pAd->stats.tx_errors = pAd->Counters8023.TxErrors;
1693
1694                 pAd->stats.rx_dropped = 0;
1695                 pAd->stats.tx_dropped = 0;
1696
1697             pAd->stats.multicast = pAd->WlanCounters.MulticastReceivedFrameCount.QuadPart;   // multicast packets received
1698             pAd->stats.collisions = pAd->Counters8023.OneCollision + pAd->Counters8023.MoreCollisions;  // Collision packets
1699
1700             pAd->stats.rx_length_errors = 0;
1701             pAd->stats.rx_over_errors = pAd->Counters8023.RxNoBuffer;                   // receiver ring buff overflow
1702             pAd->stats.rx_crc_errors = 0;//pAd->WlanCounters.FCSErrorCount;     // recved pkt with crc error
1703             pAd->stats.rx_frame_errors = pAd->Counters8023.RcvAlignmentErrors;          // recv'd frame alignment error
1704             pAd->stats.rx_fifo_errors = pAd->Counters8023.RxNoBuffer;                   // recv'r fifo overrun
1705             pAd->stats.rx_missed_errors = 0;                                            // receiver missed packet
1706
1707             // detailed tx_errors
1708             pAd->stats.tx_aborted_errors = 0;
1709             pAd->stats.tx_carrier_errors = 0;
1710             pAd->stats.tx_fifo_errors = 0;
1711             pAd->stats.tx_heartbeat_errors = 0;
1712             pAd->stats.tx_window_errors = 0;
1713
1714             // for cslip etc
1715             pAd->stats.rx_compressed = 0;
1716             pAd->stats.tx_compressed = 0;
1717
1718                 return &pAd->stats;
1719         }
1720         else
1721         return NULL;
1722 }
1723