Merge branch 'master' into next
[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 static const struct net_device_ops rt2860_netdev_ops = {
726         .ndo_open               = MainVirtualIF_open,
727         .ndo_stop               = MainVirtualIF_close,
728         .ndo_do_ioctl           = rt28xx_ioctl,
729         .ndo_get_stats          = RT28xx_get_ether_stats,
730         .ndo_validate_addr      = NULL,
731         .ndo_set_mac_address    = eth_mac_addr,
732         .ndo_change_mtu         = eth_change_mtu,
733 #ifdef IKANOS_VX_1X0
734         .ndo_start_xmit         = IKANOS_DataFramesTx,
735 #else
736         .ndo_start_xmit         = rt28xx_send_packets,
737 #endif
738 };
739
740 /* Must not be called for mdev and apdev */
741 static NDIS_STATUS rt_ieee80211_if_setup(struct net_device *dev, PRTMP_ADAPTER pAd)
742 {
743         NDIS_STATUS Status;
744         INT     i=0;
745         CHAR    slot_name[IFNAMSIZ];
746         struct net_device   *device;
747
748
749         //ether_setup(dev);
750
751 #ifdef CONFIG_STA_SUPPORT
752 #if WIRELESS_EXT >= 12
753         if (pAd->OpMode == OPMODE_STA)
754         {
755                 dev->wireless_handlers = &rt28xx_iw_handler_def;
756         }
757 #endif //WIRELESS_EXT >= 12
758 #endif // CONFIG_STA_SUPPORT //
759
760 #ifdef CONFIG_APSTA_MIXED_SUPPORT
761 #if WIRELESS_EXT >= 12
762         if (pAd->OpMode == OPMODE_AP)
763         {
764                 dev->wireless_handlers = &rt28xx_ap_iw_handler_def;
765         }
766 #endif //WIRELESS_EXT >= 12
767 #endif // CONFIG_APSTA_MIXED_SUPPORT //
768
769 #if WIRELESS_EXT < 21
770                 dev->get_wireless_stats = rt28xx_get_wireless_stats;
771 #endif
772         dev->priv_flags = INT_MAIN;
773         dev->netdev_ops = &rt2860_netdev_ops;
774         // find available device name
775         for (i = 0; i < 8; i++)
776         {
777 #ifdef MULTIPLE_CARD_SUPPORT
778                 if (pAd->MC_RowID >= 0)
779                         sprintf(slot_name, "ra%02d_%d", pAd->MC_RowID, i);
780                 else
781 #endif // MULTIPLE_CARD_SUPPORT //
782                 sprintf(slot_name, "ra%d", i);
783
784                 device = dev_get_by_name(dev_net(dev), slot_name);
785                 if (device != NULL)
786                         dev_put(device);
787
788                 if (device == NULL)
789                         break;
790         }
791
792         if(i == 8)
793         {
794                 DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
795                 Status = NDIS_STATUS_FAILURE;
796         }
797         else
798         {
799 #ifdef MULTIPLE_CARD_SUPPORT
800                 if (pAd->MC_RowID >= 0)
801                 sprintf(dev->name, "ra%02d_%d", pAd->MC_RowID, i);
802                 else
803 #endif // MULTIPLE_CARD_SUPPORT //
804                 sprintf(dev->name, "ra%d", i);
805                 Status = NDIS_STATUS_SUCCESS;
806         }
807
808         return Status;
809
810 }
811
812
813 #ifdef MULTIPLE_CARD_SUPPORT
814 /*
815 ========================================================================
816 Routine Description:
817     Get card profile path.
818
819 Arguments:
820     pAd
821
822 Return Value:
823     TRUE                - Find a card profile
824         FALSE           - use default profile
825
826 Note:
827 ========================================================================
828 */
829 extern INT RTMPGetKeyParameter(
830     IN  PCHAR   key,
831     OUT PCHAR   dest,
832     IN  INT     destsize,
833     IN  PCHAR   buffer);
834
835 BOOLEAN RTMP_CardInfoRead(
836         IN      PRTMP_ADAPTER pAd)
837 {
838 #define MC_SELECT_CARDID                0       /* use CARD ID (0 ~ 31) to identify different cards */
839 #define MC_SELECT_MAC                   1       /* use CARD MAC to identify different cards */
840 #define MC_SELECT_CARDTYPE              2       /* use CARD type (abgn or bgn) to identify different cards */
841
842 #define LETTER_CASE_TRANSLATE(txt_p, card_id)                   \
843         {       UINT32 _len; char _char;                                                \
844                 for(_len=0; _len<strlen(card_id); _len++) {             \
845                         _char = *(txt_p + _len);                                        \
846                         if (('A' <= _char) && (_char <= 'Z'))           \
847                                 *(txt_p+_len) = 'a'+(_char-'A');                \
848                 } }
849
850         struct file *srcf;
851         INT retval, orgfsuid, orgfsgid;
852         mm_segment_t orgfs;
853         CHAR *buffer, *tmpbuf, card_id_buf[30], RFIC_word[30];
854         BOOLEAN flg_match_ok = FALSE;
855         INT32 card_select_method;
856         INT32 card_free_id, card_nouse_id, card_same_mac_id, card_match_id;
857         EEPROM_ANTENNA_STRUC antenna;
858         USHORT addr01, addr23, addr45;
859         UINT8 mac[6];
860         UINT32 data, card_index;
861         UCHAR *start_ptr;
862
863
864         // init
865         buffer = kmalloc(MAX_INI_BUFFER_SIZE, MEM_ALLOC_FLAG);
866         if (buffer == NULL)
867         return FALSE;
868
869         tmpbuf = kmalloc(MAX_PARAM_BUFFER_SIZE, MEM_ALLOC_FLAG);
870         if(tmpbuf == NULL)
871         {
872                 kfree(buffer);
873         return NDIS_STATUS_FAILURE;
874         }
875
876         orgfsuid = current->fsuid;
877         orgfsgid = current->fsgid;
878         current->fsuid = current->fsgid = 0;
879     orgfs = get_fs();
880     set_fs(KERNEL_DS);
881
882         // get RF IC type
883         RTMP_IO_READ32(pAd, E2PROM_CSR, &data);
884
885         if ((data & 0x30) == 0)
886                 pAd->EEPROMAddressNum = 6;      // 93C46
887         else if ((data & 0x30) == 0x10)
888                 pAd->EEPROMAddressNum = 8;      // 93C66
889         else
890                 pAd->EEPROMAddressNum = 8;      // 93C86
891
892         RT28xx_EEPROM_READ16(pAd, EEPROM_NIC1_OFFSET, antenna.word);
893
894         if ((antenna.field.RfIcType == RFIC_2850) ||
895                 (antenna.field.RfIcType == RFIC_2750))
896         {
897                 /* ABGN card */
898                 strcpy(RFIC_word, "abgn");
899         }
900         else
901         {
902                 /* BGN card */
903                 strcpy(RFIC_word, "bgn");
904         }
905
906         // get MAC address
907         RT28xx_EEPROM_READ16(pAd, 0x04, addr01);
908         RT28xx_EEPROM_READ16(pAd, 0x06, addr23);
909         RT28xx_EEPROM_READ16(pAd, 0x08, addr45);
910
911         mac[0] = (UCHAR)(addr01 & 0xff);
912         mac[1] = (UCHAR)(addr01 >> 8);
913         mac[2] = (UCHAR)(addr23 & 0xff);
914         mac[3] = (UCHAR)(addr23 >> 8);
915         mac[4] = (UCHAR)(addr45 & 0xff);
916         mac[5] = (UCHAR)(addr45 >> 8);
917
918         // open card information file
919         srcf = filp_open(CARD_INFO_PATH, O_RDONLY, 0);
920         if (IS_ERR(srcf))
921         {
922                 /* card information file does not exist */
923                         DBGPRINT(RT_DEBUG_TRACE,
924                                 ("--> Error %ld opening %s\n", -PTR_ERR(srcf), CARD_INFO_PATH));
925                 return FALSE;
926         }
927
928         if (srcf->f_op && srcf->f_op->read)
929         {
930                 /* card information file exists so reading the card information */
931                 memset(buffer, 0x00, MAX_INI_BUFFER_SIZE);
932                 retval = srcf->f_op->read(srcf, buffer, MAX_INI_BUFFER_SIZE, &srcf->f_pos);
933                 if (retval < 0)
934                 {
935                         /* read fail */
936                                 DBGPRINT(RT_DEBUG_TRACE,
937                                         ("--> Read %s error %d\n", CARD_INFO_PATH, -retval));
938                 }
939                 else
940                 {
941                         /* get card selection method */
942                         memset(tmpbuf, 0x00, MAX_PARAM_BUFFER_SIZE);
943                         card_select_method = MC_SELECT_CARDTYPE; // default
944
945                         if (RTMPGetKeyParameter("SELECT", tmpbuf, 256, buffer))
946                         {
947                                 if (strcmp(tmpbuf, "CARDID") == 0)
948                                         card_select_method = MC_SELECT_CARDID;
949                                 else if (strcmp(tmpbuf, "MAC") == 0)
950                                         card_select_method = MC_SELECT_MAC;
951                                 else if (strcmp(tmpbuf, "CARDTYPE") == 0)
952                                         card_select_method = MC_SELECT_CARDTYPE;
953                         }
954
955                         DBGPRINT(RT_DEBUG_TRACE,
956                                         ("MC> Card Selection = %d\n", card_select_method));
957
958                         // init
959                         card_free_id = -1;
960                         card_nouse_id = -1;
961                         card_same_mac_id = -1;
962                         card_match_id = -1;
963
964                         // search current card information records
965                         for(card_index=0;
966                                 card_index<MAX_NUM_OF_MULTIPLE_CARD;
967                                 card_index++)
968                         {
969                                 if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
970                                         (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
971                                 {
972                                         // MAC is all-0 so the entry is available
973                                         MC_CardUsed[card_index] = 0;
974
975                                         if (card_free_id < 0)
976                                                 card_free_id = card_index; // 1st free entry
977                                 }
978                                 else
979                                 {
980                                         if (memcmp(MC_CardMac[card_index], mac, 6) == 0)
981                                         {
982                                                 // we find the entry with same MAC
983                                                 if (card_same_mac_id < 0)
984                                                         card_same_mac_id = card_index; // 1st same entry
985                                         }
986                                         else
987                                         {
988                                                 // MAC is not all-0 but used flag == 0
989                                                 if ((MC_CardUsed[card_index] == 0) &&
990                                                         (card_nouse_id < 0))
991                                                 {
992                                                         card_nouse_id = card_index; // 1st available entry
993                                                 }
994                                         }
995                                 }
996                         }
997
998                         DBGPRINT(RT_DEBUG_TRACE,
999                                         ("MC> Free = %d, Same = %d, NOUSE = %d\n",
1000                                         card_free_id, card_same_mac_id, card_nouse_id));
1001
1002                         if ((card_same_mac_id >= 0) &&
1003                                 ((card_select_method == MC_SELECT_CARDID) ||
1004                                 (card_select_method == MC_SELECT_CARDTYPE)))
1005                         {
1006                                 // same MAC entry is found
1007                                 card_match_id = card_same_mac_id;
1008
1009                                 if (card_select_method == MC_SELECT_CARDTYPE)
1010                                 {
1011                                         // for CARDTYPE
1012                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
1013                                                         card_match_id, RFIC_word);
1014
1015                                         if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
1016                                         {
1017                                                 // we found the card ID
1018                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1019                                         }
1020                                 }
1021                         }
1022                         else
1023                         {
1024                                 // the card is 1st plug-in, try to find the match card profile
1025                                 switch(card_select_method)
1026                                 {
1027                                         case MC_SELECT_CARDID: // CARDID
1028                                         default:
1029                                                 if (card_free_id >= 0)
1030                                                         card_match_id = card_free_id;
1031                                                 else
1032                                                         card_match_id = card_nouse_id;
1033                                                 break;
1034
1035                                         case MC_SELECT_MAC: // MAC
1036                                                 sprintf(card_id_buf, "MAC%02x:%02x:%02x:%02x:%02x:%02x",
1037                                                                 mac[0], mac[1], mac[2],
1038                                                                 mac[3], mac[4], mac[5]);
1039
1040                                                 /* try to find the key word in the card file */
1041                                                 if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
1042                                                 {
1043                                                         LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1044
1045                                                         /* get the row ID (2 ASCII characters) */
1046                                                         start_ptr -= 2;
1047                                                         card_id_buf[0] = *(start_ptr);
1048                                                         card_id_buf[1] = *(start_ptr+1);
1049                                                         card_id_buf[2] = 0x00;
1050
1051                                                         card_match_id = simple_strtol(card_id_buf, 0, 10);
1052                                                 }
1053                                                 break;
1054
1055                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1056                                                 card_nouse_id = -1;
1057
1058                                                 for(card_index=0;
1059                                                         card_index<MAX_NUM_OF_MULTIPLE_CARD;
1060                                                         card_index++)
1061                                                 {
1062                                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
1063                                                                         card_index, RFIC_word);
1064
1065                                                         if ((start_ptr=rtstrstruncasecmp(buffer,
1066                                                                                                                 card_id_buf)) != NULL)
1067                                                         {
1068                                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1069
1070                                                                 if (MC_CardUsed[card_index] == 0)
1071                                                                 {
1072                                                                         /* current the card profile is not used */
1073                                                                         if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
1074                                                                                 (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
1075                                                                         {
1076                                                                                 // find it and no previous card use it
1077                                                                                 card_match_id = card_index;
1078                                                                                 break;
1079                                                                         }
1080                                                                         else
1081                                                                         {
1082                                                                                 // ever a card use it
1083                                                                                 if (card_nouse_id < 0)
1084                                                                                         card_nouse_id = card_index;
1085                                                                         }
1086                                                                 }
1087                                                         }
1088                                                 }
1089
1090                                                 // if not find a free one, use the available one
1091                                                 if (card_match_id < 0)
1092                                                         card_match_id = card_nouse_id;
1093                                                 break;
1094                                 }
1095                         }
1096
1097                         if (card_match_id >= 0)
1098                         {
1099                                 // make up search keyword
1100                                 switch(card_select_method)
1101                                 {
1102                                         case MC_SELECT_CARDID: // CARDID
1103                                                 sprintf(card_id_buf, "%02dCARDID", card_match_id);
1104                                                 break;
1105
1106                                         case MC_SELECT_MAC: // MAC
1107                                                 sprintf(card_id_buf,
1108                                                                 "%02dmac%02x:%02x:%02x:%02x:%02x:%02x",
1109                                                                 card_match_id,
1110                                                                 mac[0], mac[1], mac[2],
1111                                                                 mac[3], mac[4], mac[5]);
1112                                                 break;
1113
1114                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1115                                         default:
1116                                                 sprintf(card_id_buf, "%02dcardtype%s",
1117                                                                 card_match_id, RFIC_word);
1118                                                 break;
1119                                 }
1120
1121                                 DBGPRINT(RT_DEBUG_TRACE, ("Search Keyword = %s\n", card_id_buf));
1122
1123                                 // read card file path
1124                                 if (RTMPGetKeyParameter(card_id_buf, tmpbuf, 256, buffer))
1125                                 {
1126                                         if (strlen(tmpbuf) < sizeof(pAd->MC_FileName))
1127                                         {
1128                                                 // backup card information
1129                                                 pAd->MC_RowID = card_match_id; /* base 0 */
1130                                                 MC_CardUsed[card_match_id] = 1;
1131                                                 memcpy(MC_CardMac[card_match_id], mac, sizeof(mac));
1132
1133                                                 // backup card file path
1134                                                 NdisMoveMemory(pAd->MC_FileName, tmpbuf , strlen(tmpbuf));
1135                                                 pAd->MC_FileName[strlen(tmpbuf)] = '\0';
1136                                                 flg_match_ok = TRUE;
1137
1138                                                 DBGPRINT(RT_DEBUG_TRACE,
1139                                                                 ("Card Profile Name = %s\n", pAd->MC_FileName));
1140                                         }
1141                                         else
1142                                         {
1143                                                 DBGPRINT(RT_DEBUG_ERROR,
1144                                                                 ("Card Profile Name length too large!\n"));
1145                                         }
1146                                 }
1147                                 else
1148                                 {
1149                                         DBGPRINT(RT_DEBUG_ERROR,
1150                                                         ("Can not find search key word in card.dat!\n"));
1151                                 }
1152
1153                                 if ((flg_match_ok != TRUE) &&
1154                                         (card_match_id < MAX_NUM_OF_MULTIPLE_CARD))
1155                                 {
1156                                         MC_CardUsed[card_match_id] = 0;
1157                                         memset(MC_CardMac[card_match_id], 0, sizeof(mac));
1158                                 }
1159                         } // if (card_match_id >= 0)
1160                 }
1161         }
1162
1163         // close file
1164         retval = filp_close(srcf, NULL);
1165         set_fs(orgfs);
1166         current->fsuid = orgfsuid;
1167         current->fsgid = orgfsgid;
1168         kfree(buffer);
1169         kfree(tmpbuf);
1170         return flg_match_ok;
1171 }
1172 #endif // MULTIPLE_CARD_SUPPORT //
1173
1174
1175 /*
1176 ========================================================================
1177 Routine Description:
1178     Probe RT28XX chipset.
1179
1180 Arguments:
1181     _dev_p                              Point to the PCI or USB device
1182         _dev_id_p                       Point to the PCI or USB device ID
1183
1184 Return Value:
1185     0                                   Probe OK
1186         -ENODEV                         Probe Fail
1187
1188 Note:
1189 ========================================================================
1190 */
1191 INT __devinit   rt28xx_probe(
1192     IN  void *_dev_p,
1193     IN  void *_dev_id_p,
1194         IN  UINT argc,
1195         OUT PRTMP_ADAPTER *ppAd)
1196 {
1197     struct  net_device  *net_dev;
1198     PRTMP_ADAPTER       pAd = (PRTMP_ADAPTER) NULL;
1199     INT                 status;
1200         PVOID                           handle;
1201         struct pci_dev *dev_p = (struct pci_dev *)_dev_p;
1202
1203
1204 #ifdef CONFIG_STA_SUPPORT
1205     DBGPRINT(RT_DEBUG_TRACE, ("STA Driver version-%s\n", STA_DRIVER_VERSION));
1206 #endif // CONFIG_STA_SUPPORT //
1207
1208     net_dev = alloc_etherdev(sizeof(PRTMP_ADAPTER));
1209     if (net_dev == NULL)
1210     {
1211         printk("alloc_netdev failed\n");
1212
1213         goto err_out;
1214     }
1215
1216         netif_stop_queue(net_dev);
1217 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
1218 /* for supporting Network Manager */
1219 /* Set the sysfs physical device reference for the network logical device
1220  * if set prior to registration will cause a symlink during initialization.
1221  */
1222     SET_NETDEV_DEV(net_dev, &(dev_p->dev));
1223 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
1224
1225         // Allocate RTMP_ADAPTER miniport adapter structure
1226         handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
1227         RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p);
1228
1229         status = RTMPAllocAdapterBlock(handle, &pAd);
1230         if (status != NDIS_STATUS_SUCCESS)
1231                 goto err_out_free_netdev;
1232
1233         net_dev->ml_priv = (PVOID)pAd;
1234     pAd->net_dev = net_dev; // must be before RT28XXNetDevInit()
1235
1236         RT28XXNetDevInit(_dev_p, net_dev, pAd);
1237
1238 #ifdef CONFIG_STA_SUPPORT
1239     pAd->StaCfg.OriDevType = net_dev->type;
1240 #endif // CONFIG_STA_SUPPORT //
1241
1242         // Post config
1243         if (RT28XXProbePostConfig(_dev_p, pAd, 0) == FALSE)
1244                 goto err_out_unmap;
1245
1246 #ifdef CONFIG_STA_SUPPORT
1247         pAd->OpMode = OPMODE_STA;
1248 #endif // CONFIG_STA_SUPPORT //
1249
1250
1251 #ifdef MULTIPLE_CARD_SUPPORT
1252         // find its profile path
1253         pAd->MC_RowID = -1; // use default profile path
1254         RTMP_CardInfoRead(pAd);
1255
1256         if (pAd->MC_RowID == -1)
1257 #ifdef CONFIG_STA_SUPPORT
1258                 strcpy(pAd->MC_FileName, STA_PROFILE_PATH);
1259 #endif // CONFIG_STA_SUPPORT //
1260
1261         DBGPRINT(RT_DEBUG_TRACE,
1262                         ("MC> ROW = %d, PATH = %s\n", pAd->MC_RowID, pAd->MC_FileName));
1263 #endif // MULTIPLE_CARD_SUPPORT //
1264
1265         // sample move
1266         if (rt_ieee80211_if_setup(net_dev, pAd) != NDIS_STATUS_SUCCESS)
1267                 goto err_out_unmap;
1268
1269     // Register this device
1270     status = register_netdev(net_dev);
1271     if (status)
1272         goto err_out_unmap;
1273
1274     // Set driver data
1275         RT28XX_DRVDATA_SET(_dev_p);
1276
1277         *ppAd = pAd;
1278     return 0; // probe ok
1279
1280
1281         /* --------------------------- ERROR HANDLE --------------------------- */
1282 err_out_unmap:
1283         RTMPFreeAdapter(pAd);
1284         RT28XX_UNMAP();
1285
1286 err_out_free_netdev:
1287         free_netdev(net_dev);
1288
1289 err_out:
1290         RT28XX_PUT_DEVICE(dev_p);
1291
1292         return -ENODEV; /* probe fail */
1293 } /* End of rt28xx_probe */
1294
1295
1296 /*
1297 ========================================================================
1298 Routine Description:
1299     The entry point for Linux kernel sent packet to our driver.
1300
1301 Arguments:
1302     sk_buff *skb                the pointer refer to a sk_buffer.
1303
1304 Return Value:
1305     0
1306
1307 Note:
1308         This function is the entry point of Tx Path for Os delivery packet to
1309         our driver. You only can put OS-depened & STA/AP common handle procedures
1310         in here.
1311 ========================================================================
1312 */
1313 int rt28xx_packet_xmit(struct sk_buff *skb)
1314 {
1315         struct net_device *net_dev = skb->dev;
1316         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1317         int status = 0;
1318         PNDIS_PACKET pPacket = (PNDIS_PACKET) skb;
1319
1320         /* RT2870STA does this in RTMPSendPackets() */
1321 #ifdef RALINK_ATE
1322         if (ATE_ON(pAd))
1323         {
1324                 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_RESOURCES);
1325                 return 0;
1326         }
1327 #endif // RALINK_ATE //
1328
1329 #ifdef CONFIG_STA_SUPPORT
1330         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1331         {
1332                 // Drop send request since we are in monitor mode
1333                 if (MONITOR_ON(pAd))
1334                 {
1335                         RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1336                         goto done;
1337                 }
1338         }
1339 #endif // CONFIG_STA_SUPPORT //
1340
1341         // EapolStart size is 18
1342         if (skb->len < 14)
1343         {
1344                 //printk("bad packet size: %d\n", pkt->len);
1345                 hex_dump("bad packet", skb->data, skb->len);
1346                 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1347                 goto done;
1348         }
1349
1350         RTMP_SET_PACKET_5VT(pPacket, 0);
1351 #ifdef CONFIG_5VT_ENHANCE
1352     if (*(int*)(skb->cb) == BRIDGE_TAG) {
1353                 RTMP_SET_PACKET_5VT(pPacket, 1);
1354     }
1355 #endif
1356
1357
1358
1359 #ifdef CONFIG_STA_SUPPORT
1360         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1361         {
1362
1363                 STASendPackets((NDIS_HANDLE)pAd, (PPNDIS_PACKET) &pPacket, 1);
1364         }
1365
1366 #endif // CONFIG_STA_SUPPORT //
1367
1368         status = 0;
1369 done:
1370
1371         return status;
1372 }
1373
1374
1375 /*
1376 ========================================================================
1377 Routine Description:
1378     Send a packet to WLAN.
1379
1380 Arguments:
1381     skb_p           points to our adapter
1382     dev_p           which WLAN network interface
1383
1384 Return Value:
1385     0: transmit successfully
1386     otherwise: transmit fail
1387
1388 Note:
1389 ========================================================================
1390 */
1391 INT rt28xx_send_packets(
1392         IN struct sk_buff               *skb_p,
1393         IN struct net_device    *net_dev)
1394 {
1395     RTMP_ADAPTER *pAd = net_dev->ml_priv;
1396         if (!(net_dev->flags & IFF_UP))
1397         {
1398                 RELEASE_NDIS_PACKET(pAd, (PNDIS_PACKET)skb_p, NDIS_STATUS_FAILURE);
1399                 return 0;
1400         }
1401
1402         NdisZeroMemory((PUCHAR)&skb_p->cb[CB_OFF], 15);
1403         RTMP_SET_PACKET_NET_DEVICE_MBSSID(skb_p, MAIN_MBSSID);
1404
1405         return rt28xx_packet_xmit(skb_p);
1406
1407 } /* End of MBSS_VirtualIF_PacketSend */
1408
1409
1410
1411
1412 void CfgInitHook(PRTMP_ADAPTER pAd)
1413 {
1414         pAd->bBroadComHT = TRUE;
1415 } /* End of CfgInitHook */
1416
1417
1418 #if WIRELESS_EXT >= 12
1419 // This function will be called when query /proc
1420 struct iw_statistics *rt28xx_get_wireless_stats(
1421     IN struct net_device *net_dev)
1422 {
1423         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1424
1425
1426         DBGPRINT(RT_DEBUG_TRACE, ("rt28xx_get_wireless_stats --->\n"));
1427
1428         pAd->iw_stats.status = 0; // Status - device dependent for now
1429
1430         // link quality
1431         pAd->iw_stats.qual.qual = ((pAd->Mlme.ChannelQuality * 12)/10 + 10);
1432         if(pAd->iw_stats.qual.qual > 100)
1433                 pAd->iw_stats.qual.qual = 100;
1434
1435 #ifdef CONFIG_STA_SUPPORT
1436         if (pAd->OpMode == OPMODE_STA)
1437                 pAd->iw_stats.qual.level = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
1438 #endif // CONFIG_STA_SUPPORT //
1439
1440         pAd->iw_stats.qual.noise = pAd->BbpWriteLatch[66]; // noise level (dBm)
1441
1442         pAd->iw_stats.qual.noise += 256 - 143;
1443         pAd->iw_stats.qual.updated = 1;     // Flags to know if updated
1444 #ifdef IW_QUAL_DBM
1445         pAd->iw_stats.qual.updated |= IW_QUAL_DBM;      // Level + Noise are dBm
1446 #endif // IW_QUAL_DBM //
1447
1448         pAd->iw_stats.discard.nwid = 0;     // Rx : Wrong nwid/essid
1449         pAd->iw_stats.miss.beacon = 0;      // Missed beacons/superframe
1450
1451         DBGPRINT(RT_DEBUG_TRACE, ("<--- rt28xx_get_wireless_stats\n"));
1452         return &pAd->iw_stats;
1453 } /* End of rt28xx_get_wireless_stats */
1454 #endif // WIRELESS_EXT //
1455
1456
1457
1458 void tbtt_tasklet(unsigned long data)
1459 {
1460 #define MAX_TX_IN_TBTT          (16)
1461
1462 }
1463
1464 INT rt28xx_ioctl(
1465         IN      struct net_device       *net_dev,
1466         IN      OUT     struct ifreq    *rq,
1467         IN      INT                                     cmd)
1468 {
1469         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1470         RTMP_ADAPTER    *pAd = NULL;
1471         INT                             ret = 0;
1472
1473         if (net_dev->priv_flags == INT_MAIN)
1474         {
1475                 pAd = net_dev->ml_priv;
1476         }
1477         else
1478         {
1479                 pVirtualAd = net_dev->ml_priv;
1480                 pAd = pVirtualAd->RtmpDev->ml_priv;
1481         }
1482
1483         if (pAd == NULL)
1484         {
1485                 /* if 1st open fail, pAd will be free;
1486                    So the net_dev->ml_priv will be NULL in 2rd open */
1487                 return -ENETDOWN;
1488         }
1489
1490
1491 #ifdef CONFIG_STA_SUPPORT
1492         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1493         {
1494                 ret = rt28xx_sta_ioctl(net_dev, rq, cmd);
1495         }
1496 #endif // CONFIG_STA_SUPPORT //
1497
1498         return ret;
1499 }
1500
1501 /*
1502     ========================================================================
1503
1504     Routine Description:
1505         return ethernet statistics counter
1506
1507     Arguments:
1508         net_dev                     Pointer to net_device
1509
1510     Return Value:
1511         net_device_stats*
1512
1513     Note:
1514
1515     ========================================================================
1516 */
1517 struct net_device_stats *RT28xx_get_ether_stats(
1518     IN  struct net_device *net_dev)
1519 {
1520     RTMP_ADAPTER *pAd = NULL;
1521
1522         if (net_dev)
1523                 pAd = net_dev->ml_priv;
1524
1525         if (pAd)
1526         {
1527
1528                 pAd->stats.rx_packets = pAd->WlanCounters.ReceivedFragmentCount.QuadPart;
1529                 pAd->stats.tx_packets = pAd->WlanCounters.TransmittedFragmentCount.QuadPart;
1530
1531                 pAd->stats.rx_bytes = pAd->RalinkCounters.ReceivedByteCount;
1532                 pAd->stats.tx_bytes = pAd->RalinkCounters.TransmittedByteCount;
1533
1534                 pAd->stats.rx_errors = pAd->Counters8023.RxErrors;
1535                 pAd->stats.tx_errors = pAd->Counters8023.TxErrors;
1536
1537                 pAd->stats.rx_dropped = 0;
1538                 pAd->stats.tx_dropped = 0;
1539
1540             pAd->stats.multicast = pAd->WlanCounters.MulticastReceivedFrameCount.QuadPart;   // multicast packets received
1541             pAd->stats.collisions = pAd->Counters8023.OneCollision + pAd->Counters8023.MoreCollisions;  // Collision packets
1542
1543             pAd->stats.rx_length_errors = 0;
1544             pAd->stats.rx_over_errors = pAd->Counters8023.RxNoBuffer;                   // receiver ring buff overflow
1545             pAd->stats.rx_crc_errors = 0;//pAd->WlanCounters.FCSErrorCount;     // recved pkt with crc error
1546             pAd->stats.rx_frame_errors = pAd->Counters8023.RcvAlignmentErrors;          // recv'd frame alignment error
1547             pAd->stats.rx_fifo_errors = pAd->Counters8023.RxNoBuffer;                   // recv'r fifo overrun
1548             pAd->stats.rx_missed_errors = 0;                                            // receiver missed packet
1549
1550             // detailed tx_errors
1551             pAd->stats.tx_aborted_errors = 0;
1552             pAd->stats.tx_carrier_errors = 0;
1553             pAd->stats.tx_fifo_errors = 0;
1554             pAd->stats.tx_heartbeat_errors = 0;
1555             pAd->stats.tx_window_errors = 0;
1556
1557             // for cslip etc
1558             pAd->stats.rx_compressed = 0;
1559             pAd->stats.tx_compressed = 0;
1560
1561                 return &pAd->stats;
1562         }
1563         else
1564         return NULL;
1565 }
1566