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