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