Staging: rt2860: remove dead INF_AMAZON_SE code
[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 /*---------------------------------------------------------------------*/
44 /* Private Variables Used                                              */
45 /*---------------------------------------------------------------------*/
46 //static RALINK_TIMER_STRUCT     PeriodicTimer;
47
48 char *mac = "";            // default 00:00:00:00:00:00
49 char *hostname = "";               // default CMPC
50 module_param (mac, charp, 0);
51 MODULE_PARM_DESC (mac, "rt28xx: wireless mac addr");
52
53
54 /*---------------------------------------------------------------------*/
55 /* Prototypes of Functions Used                                        */
56 /*---------------------------------------------------------------------*/
57 #ifdef DOT11_N_SUPPORT
58 extern BOOLEAN ba_reordering_resource_init(PRTMP_ADAPTER pAd, int num);
59 extern void ba_reordering_resource_release(PRTMP_ADAPTER pAd);
60 #endif // DOT11_N_SUPPORT //
61 extern NDIS_STATUS NICLoadRateSwitchingParams(IN PRTMP_ADAPTER pAd);
62
63 extern void init_thread_task(PRTMP_ADAPTER pAd);
64
65 // public function prototype
66 INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
67                                                         IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
68
69 // private function prototype
70 static int rt28xx_init(IN struct net_device *net_dev);
71 INT rt28xx_send_packets(IN struct sk_buff *skb_p, IN struct net_device *net_dev);
72
73 static void CfgInitHook(PRTMP_ADAPTER pAd);
74
75 #ifdef CONFIG_STA_SUPPORT
76 extern  const struct iw_handler_def rt28xx_iw_handler_def;
77 #endif // CONFIG_STA_SUPPORT //
78
79 #if WIRELESS_EXT >= 12
80 // This function will be called when query /proc
81 struct iw_statistics *rt28xx_get_wireless_stats(
82     IN struct net_device *net_dev);
83 #endif
84
85 struct net_device_stats *RT28xx_get_ether_stats(
86     IN  struct net_device *net_dev);
87
88 /*
89 ========================================================================
90 Routine Description:
91     Close raxx interface.
92
93 Arguments:
94         *net_dev                        the raxx interface pointer
95
96 Return Value:
97     0                                   Open OK
98         otherwise                       Open Fail
99
100 Note:
101         1. if open fail, kernel will not call the close function.
102         2. Free memory for
103                 (1) Mlme Memory Handler:                MlmeHalt()
104                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
105                 (3) BA Reordering:                              ba_reordering_resource_release()
106 ========================================================================
107 */
108 int MainVirtualIF_close(IN struct net_device *net_dev)
109 {
110     RTMP_ADAPTER *pAd = net_dev->ml_priv;
111
112         // Sanity check for pAd
113         if (pAd == NULL)
114                 return 0; // close ok
115
116         netif_carrier_off(pAd->net_dev);
117         netif_stop_queue(pAd->net_dev);
118
119
120         VIRTUAL_IF_DOWN(pAd);
121
122         RT_MOD_DEC_USE_COUNT();
123
124         return 0; // close ok
125 }
126
127 /*
128 ========================================================================
129 Routine Description:
130     Open raxx interface.
131
132 Arguments:
133         *net_dev                        the raxx interface pointer
134
135 Return Value:
136     0                                   Open OK
137         otherwise                       Open Fail
138
139 Note:
140         1. if open fail, kernel will not call the close function.
141         2. Free memory for
142                 (1) Mlme Memory Handler:                MlmeHalt()
143                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
144                 (3) BA Reordering:                              ba_reordering_resource_release()
145 ========================================================================
146 */
147 int MainVirtualIF_open(IN struct net_device *net_dev)
148 {
149     RTMP_ADAPTER *pAd = net_dev->ml_priv;
150
151         // Sanity check for pAd
152         if (pAd == NULL)
153                 return 0; // close ok
154
155         if (VIRTUAL_IF_UP(pAd) != 0)
156                 return -1;
157
158         // increase MODULE use count
159         RT_MOD_INC_USE_COUNT();
160
161         netif_start_queue(net_dev);
162         netif_carrier_on(net_dev);
163         netif_wake_queue(net_dev);
164
165         return 0;
166 }
167
168 /*
169 ========================================================================
170 Routine Description:
171     Close raxx interface.
172
173 Arguments:
174         *net_dev                        the raxx interface pointer
175
176 Return Value:
177     0                                   Open OK
178         otherwise                       Open Fail
179
180 Note:
181         1. if open fail, kernel will not call the close function.
182         2. Free memory for
183                 (1) Mlme Memory Handler:                MlmeHalt()
184                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
185                 (3) BA Reordering:                              ba_reordering_resource_release()
186 ========================================================================
187 */
188 int rt28xx_close(IN PNET_DEV dev)
189 {
190         struct net_device * net_dev = (struct net_device *)dev;
191     RTMP_ADAPTER        *pAd = net_dev->ml_priv;
192         BOOLEAN                 Cancelled = FALSE;
193         UINT32                  i = 0;
194
195
196     DBGPRINT(RT_DEBUG_TRACE, ("===> rt28xx_close\n"));
197
198         // Sanity check for pAd
199         if (pAd == NULL)
200                 return 0; // close ok
201
202 #ifdef CONFIG_STA_SUPPORT
203         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
204         {
205                 // If dirver doesn't wake up firmware here,
206                 // NICLoadFirmware will hang forever when interface is up again.
207                 if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE) ||
208                         RTMP_SET_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND) ||
209                         RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF))
210         {
211                     AsicForceWakeup(pAd, RTMP_HALT);
212         }
213
214                 if (INFRA_ON(pAd) &&
215                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
216                 {
217                         MLME_DISASSOC_REQ_STRUCT        DisReq;
218                         MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
219
220                         COPY_MAC_ADDR(DisReq.Addr, pAd->CommonCfg.Bssid);
221                         DisReq.Reason =  REASON_DEAUTH_STA_LEAVING;
222
223                         MsgElem->Machine = ASSOC_STATE_MACHINE;
224                         MsgElem->MsgType = MT2_MLME_DISASSOC_REQ;
225                         MsgElem->MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
226                         NdisMoveMemory(MsgElem->Msg, &DisReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
227
228                         // Prevent to connect AP again in STAMlmePeriodicExec
229                         pAd->MlmeAux.AutoReconnectSsidLen= 32;
230                         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
231
232                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
233                         MlmeDisassocReqAction(pAd, MsgElem);
234                         kfree(MsgElem);
235
236                         RTMPusecDelay(1000);
237                 }
238
239
240 #ifdef CCX_SUPPORT
241                 RTMPCancelTimer(&pAd->StaCfg.LeapAuthTimer, &Cancelled);
242 #endif
243
244                 RTMPCancelTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, &Cancelled);
245                 RTMPCancelTimer(&pAd->StaCfg.WpaDisassocAndBlockAssocTimer, &Cancelled);
246
247 #ifdef WPA_SUPPLICANT_SUPPORT
248 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
249                 {
250                         union iwreq_data    wrqu;
251                         // send wireless event to wpa_supplicant for infroming interface down.
252                         memset(&wrqu, 0, sizeof(wrqu));
253                         wrqu.data.flags = RT_INTERFACE_DOWN;
254                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
255                 }
256 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
257 #endif // WPA_SUPPLICANT_SUPPORT //
258
259                 MlmeRadioOff(pAd);
260                 pAd->bPCIclkOff = FALSE;
261         }
262 #endif // CONFIG_STA_SUPPORT //
263
264         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
265
266         for (i = 0 ; i < NUM_OF_TX_RING; i++)
267         {
268                 while (pAd->DeQueueRunning[i] == TRUE)
269                 {
270                         printk("Waiting for TxQueue[%d] done..........\n", i);
271                         RTMPusecDelay(1000);
272                 }
273         }
274
275         // Stop Mlme state machine
276         MlmeHalt(pAd);
277
278         // Close kernel threads or tasklets
279         kill_thread_task(pAd);
280
281
282 #ifdef CONFIG_STA_SUPPORT
283         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
284         {
285                 MacTableReset(pAd);
286         }
287 #endif // CONFIG_STA_SUPPORT //
288
289
290         MeasureReqTabExit(pAd);
291         TpcReqTabExit(pAd);
292
293
294         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
295         {
296                 NICDisableInterrupt(pAd);
297         }
298
299         // Disable Rx, register value supposed will remain after reset
300         NICIssueReset(pAd);
301
302         // Free IRQ
303         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
304         {
305                 // Deregister interrupt function
306                 RT28XX_IRQ_RELEASE(net_dev)
307                 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
308         }
309
310
311         // Free Ring or USB buffers
312         RTMPFreeTxRxRingMemory(pAd);
313
314         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
315
316 #ifdef DOT11_N_SUPPORT
317         // Free BA reorder resource
318         ba_reordering_resource_release(pAd);
319 #endif // DOT11_N_SUPPORT //
320
321
322         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_START_UP);
323
324         return 0; // close ok
325 } /* End of rt28xx_close */
326
327 static int rt28xx_init(IN struct net_device *net_dev)
328 {
329         PRTMP_ADAPTER                   pAd = (PRTMP_ADAPTER)net_dev->ml_priv;
330         UINT                                    index;
331         UCHAR                                   TmpPhy;
332         NDIS_STATUS                             Status;
333         UINT32          MacCsr0 = 0;
334
335
336 #ifdef DOT11_N_SUPPORT
337         // Allocate BA Reordering memory
338         ba_reordering_resource_init(pAd, MAX_REORDERING_MPDU_NUM);
339 #endif // DOT11_N_SUPPORT //
340
341         // Make sure MAC gets ready.
342         index = 0;
343         do
344         {
345                 RTMP_IO_READ32(pAd, MAC_CSR0, &MacCsr0);
346                 pAd->MACVersion = MacCsr0;
347
348                 if ((pAd->MACVersion != 0x00) && (pAd->MACVersion != 0xFFFFFFFF))
349                         break;
350
351                 RTMPusecDelay(10);
352         } while (index++ < 100);
353
354         DBGPRINT(RT_DEBUG_TRACE, ("MAC_CSR0  [ Ver:Rev=0x%08x]\n", pAd->MACVersion));
355
356         // Disable DMA
357         RT28XXDMADisable(pAd);
358
359
360         // Load 8051 firmware
361         Status = NICLoadFirmware(pAd);
362         if (Status != NDIS_STATUS_SUCCESS)
363         {
364                 DBGPRINT_ERR(("NICLoadFirmware failed, Status[=0x%08x]\n", Status));
365                 goto err1;
366         }
367
368         NICLoadRateSwitchingParams(pAd);
369
370         // Disable interrupts here which is as soon as possible
371         // This statement should never be true. We might consider to remove it later
372         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
373         {
374                 NICDisableInterrupt(pAd);
375         }
376
377         Status = RTMPAllocTxRxRingMemory(pAd);
378         if (Status != NDIS_STATUS_SUCCESS)
379         {
380                 DBGPRINT_ERR(("RTMPAllocDMAMemory failed, Status[=0x%08x]\n", Status));
381                 goto err1;
382         }
383
384         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
385
386         // initialize MLME
387         //
388
389         Status = MlmeInit(pAd);
390         if (Status != NDIS_STATUS_SUCCESS)
391         {
392                 DBGPRINT_ERR(("MlmeInit failed, Status[=0x%08x]\n", Status));
393                 goto err2;
394         }
395
396         // Initialize pAd->StaCfg, pAd->ApCfg, pAd->CommonCfg to manufacture default
397         //
398         UserCfgInit(pAd);
399
400
401         RT28XX_TASK_THREAD_INIT(pAd, Status);
402         if (Status != NDIS_STATUS_SUCCESS)
403                 goto err1;
404
405         CfgInitHook(pAd);
406
407 #ifdef CONFIG_STA_SUPPORT
408         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
409                 NdisAllocateSpinLock(&pAd->MacTabLock);
410 #endif // CONFIG_STA_SUPPORT //
411
412         MeasureReqTabInit(pAd);
413         TpcReqTabInit(pAd);
414
415         //
416         // Init the hardware, we need to init asic before read registry, otherwise mac register will be reset
417         //
418         Status = NICInitializeAdapter(pAd, TRUE);
419         if (Status != NDIS_STATUS_SUCCESS)
420         {
421                 DBGPRINT_ERR(("NICInitializeAdapter failed, Status[=0x%08x]\n", Status));
422                 if (Status != NDIS_STATUS_SUCCESS)
423                 goto err3;
424         }
425
426         // Read parameters from Config File
427         Status = RTMPReadParametersHook(pAd);
428
429         printk("1. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
430         if (Status != NDIS_STATUS_SUCCESS)
431         {
432                 DBGPRINT_ERR(("NICReadRegParameters failed, Status[=0x%08x]\n",Status));
433                 goto err4;
434         }
435
436
437
438         //Init Ba Capability parameters.
439 #ifdef DOT11_N_SUPPORT
440         pAd->CommonCfg.DesiredHtPhy.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
441         pAd->CommonCfg.DesiredHtPhy.AmsduEnable = (USHORT)pAd->CommonCfg.BACapability.field.AmsduEnable;
442         pAd->CommonCfg.DesiredHtPhy.AmsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
443         pAd->CommonCfg.DesiredHtPhy.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
444         // UPdata to HT IE
445         pAd->CommonCfg.HtCapability.HtCapInfo.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
446         pAd->CommonCfg.HtCapability.HtCapInfo.AMsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
447         pAd->CommonCfg.HtCapability.HtCapParm.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
448 #endif // DOT11_N_SUPPORT //
449
450         printk("2. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
451
452         // We should read EEPROM for all cases.  rt2860b
453         NICReadEEPROMParameters(pAd, mac);
454
455         printk("3. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
456
457         NICInitAsicFromEEPROM(pAd); //rt2860b
458
459         // Set PHY to appropriate mode
460         TmpPhy = pAd->CommonCfg.PhyMode;
461         pAd->CommonCfg.PhyMode = 0xff;
462         RTMPSetPhyMode(pAd, TmpPhy);
463 #ifdef DOT11_N_SUPPORT
464         SetCommonHT(pAd);
465 #endif // DOT11_N_SUPPORT //
466
467         // No valid channels.
468         if (pAd->ChannelListNum == 0)
469         {
470                 printk("Wrong configuration. No valid channel found. Check \"ContryCode\" and \"ChannelGeography\" setting.\n");
471                 goto err4;
472         }
473
474 #ifdef DOT11_N_SUPPORT
475         printk("MCS Set = %02x %02x %02x %02x %02x\n", pAd->CommonCfg.HtCapability.MCSSet[0],
476            pAd->CommonCfg.HtCapability.MCSSet[1], pAd->CommonCfg.HtCapability.MCSSet[2],
477            pAd->CommonCfg.HtCapability.MCSSet[3], pAd->CommonCfg.HtCapability.MCSSet[4]);
478 #endif // DOT11_N_SUPPORT //
479
480 #ifdef IKANOS_VX_1X0
481         VR_IKANOS_FP_Init(pAd->ApCfg.BssidNum, pAd->PermanentAddress);
482 #endif // IKANOS_VX_1X0 //
483
484                 //
485         // Initialize RF register to default value
486         //
487         AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
488         AsicLockChannel(pAd, pAd->CommonCfg.Channel);
489
490         // 8051 firmware require the signal during booting time.
491         AsicSendCommandToMcu(pAd, 0x72, 0xFF, 0x00, 0x00);
492
493         if (pAd && (Status != NDIS_STATUS_SUCCESS))
494         {
495                 //
496                 // Undo everything if it failed
497                 //
498                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
499                 {
500                         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
501                 }
502         }
503         else if (pAd)
504         {
505                 // Microsoft HCT require driver send a disconnect event after driver initialization.
506                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
507                 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE);
508                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event B!\n"));
509
510
511         }// end of else
512
513
514         DBGPRINT_S(Status, ("<==== RTMPInitialize, Status=%x\n", Status));
515
516         return TRUE;
517
518
519 err4:
520 err3:
521         MlmeHalt(pAd);
522 err2:
523         RTMPFreeTxRxRingMemory(pAd);
524 err1:
525
526 #ifdef DOT11_N_SUPPORT
527         os_free_mem(pAd, pAd->mpdu_blk_pool.mem); // free BA pool
528 #endif // DOT11_N_SUPPORT //
529         RT28XX_IRQ_RELEASE(net_dev);
530
531         // shall not set ml_priv to NULL here because the ml_priv didn't been free yet.
532         //net_dev->ml_priv = 0;
533
534         printk("!!! %s Initialized fail !!!\n", RT28xx_CHIP_NAME);
535         return FALSE;
536 } /* End of rt28xx_init */
537
538
539 /*
540 ========================================================================
541 Routine Description:
542     Open raxx interface.
543
544 Arguments:
545         *net_dev                        the raxx interface pointer
546
547 Return Value:
548     0                                   Open OK
549         otherwise                       Open Fail
550
551 Note:
552 ========================================================================
553 */
554 int rt28xx_open(IN PNET_DEV dev)
555 {
556         struct net_device * net_dev = (struct net_device *)dev;
557         PRTMP_ADAPTER pAd = net_dev->ml_priv;
558         int retval = 0;
559         POS_COOKIE pObj;
560
561
562         // Sanity check for pAd
563         if (pAd == NULL)
564         {
565                 /* if 1st open fail, pAd will be free;
566                    So the net_dev->ml_priv will be NULL in 2rd open */
567                 return -1;
568         }
569
570         // Init
571         pObj = (POS_COOKIE)pAd->OS_Cookie;
572
573         // reset Adapter flags
574         RTMP_CLEAR_FLAGS(pAd);
575
576         // Request interrupt service routine for PCI device
577         // register the interrupt routine with the os
578         RT28XX_IRQ_REQUEST(net_dev);
579
580
581         // Init BssTab & ChannelInfo tabbles for auto channel select.
582
583
584         // Chip & other init
585         if (rt28xx_init(net_dev) == FALSE)
586                 goto err;
587
588 #ifdef CONFIG_STA_SUPPORT
589         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
590         {
591                 NdisZeroMemory(pAd->StaCfg.dev_name, 16);
592                 NdisMoveMemory(pAd->StaCfg.dev_name, net_dev->name, strlen(net_dev->name));
593         }
594 #endif // CONFIG_STA_SUPPORT //
595
596         // Set up the Mac address
597         NdisMoveMemory(net_dev->dev_addr, (void *) pAd->CurrentAddress, 6);
598
599         // Init IRQ parameters
600         RT28XX_IRQ_INIT(pAd);
601
602         // Various AP function init
603
604 #ifdef CONFIG_STA_SUPPORT
605         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
606         {
607 #ifdef WPA_SUPPLICANT_SUPPORT
608 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
609                 {
610                         union iwreq_data    wrqu;
611                         // send wireless event to wpa_supplicant for infroming interface down.
612                         memset(&wrqu, 0, sizeof(wrqu));
613                         wrqu.data.flags = RT_INTERFACE_UP;
614                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
615                 }
616 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
617 #endif // WPA_SUPPLICANT_SUPPORT //
618
619         }
620 #endif // CONFIG_STA_SUPPORT //
621
622         // Enable Interrupt
623         RT28XX_IRQ_ENABLE(pAd);
624
625         // Now Enable RxTx
626         RTMPEnableRxTx(pAd);
627         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP);
628
629         {
630         UINT32 reg = 0;
631         RTMP_IO_READ32(pAd, 0x1300, &reg);  // clear garbage interrupts
632         printk("0x1300 = %08x\n", reg);
633         }
634
635 #ifdef CONFIG_STA_SUPPORT
636         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
637         RTMPInitPCIeLinkCtrlValue(pAd);
638 #endif // CONFIG_STA_SUPPORT //
639
640         return (retval);
641
642 err:
643         return (-1);
644 } /* End of rt28xx_open */
645
646 static const struct net_device_ops rt2860_netdev_ops = {
647         .ndo_open               = MainVirtualIF_open,
648         .ndo_stop               = MainVirtualIF_close,
649         .ndo_do_ioctl           = rt28xx_ioctl,
650         .ndo_get_stats          = RT28xx_get_ether_stats,
651         .ndo_validate_addr      = NULL,
652         .ndo_set_mac_address    = eth_mac_addr,
653         .ndo_change_mtu         = eth_change_mtu,
654 #ifdef IKANOS_VX_1X0
655         .ndo_start_xmit         = IKANOS_DataFramesTx,
656 #else
657         .ndo_start_xmit         = rt28xx_send_packets,
658 #endif
659 };
660
661 /* Must not be called for mdev and apdev */
662 static NDIS_STATUS rt_ieee80211_if_setup(struct net_device *dev, PRTMP_ADAPTER pAd)
663 {
664         NDIS_STATUS Status;
665         INT     i=0;
666         CHAR    slot_name[IFNAMSIZ];
667         struct net_device   *device;
668
669
670         //ether_setup(dev);
671
672 #ifdef CONFIG_STA_SUPPORT
673 #if WIRELESS_EXT >= 12
674         if (pAd->OpMode == OPMODE_STA)
675         {
676                 dev->wireless_handlers = &rt28xx_iw_handler_def;
677         }
678 #endif //WIRELESS_EXT >= 12
679 #endif // CONFIG_STA_SUPPORT //
680
681 #if WIRELESS_EXT < 21
682                 dev->get_wireless_stats = rt28xx_get_wireless_stats;
683 #endif
684         dev->priv_flags = INT_MAIN;
685         dev->netdev_ops = &rt2860_netdev_ops;
686         // find available device name
687         for (i = 0; i < 8; i++)
688         {
689                 sprintf(slot_name, "ra%d", i);
690
691                 device = dev_get_by_name(dev_net(dev), slot_name);
692                 if (device != NULL)
693                         dev_put(device);
694
695                 if (device == NULL)
696                         break;
697         }
698
699         if(i == 8)
700         {
701                 DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
702                 Status = NDIS_STATUS_FAILURE;
703         }
704         else
705         {
706                 sprintf(dev->name, "ra%d", i);
707                 Status = NDIS_STATUS_SUCCESS;
708         }
709
710         return Status;
711
712 }
713
714 /*
715 ========================================================================
716 Routine Description:
717     Probe RT28XX chipset.
718
719 Arguments:
720     _dev_p                              Point to the PCI or USB device
721         _dev_id_p                       Point to the PCI or USB device ID
722
723 Return Value:
724     0                                   Probe OK
725         -ENODEV                         Probe Fail
726
727 Note:
728 ========================================================================
729 */
730 INT __devinit   rt28xx_probe(
731     IN  void *_dev_p,
732     IN  void *_dev_id_p,
733         IN  UINT argc,
734         OUT PRTMP_ADAPTER *ppAd)
735 {
736     struct  net_device  *net_dev;
737     PRTMP_ADAPTER       pAd = (PRTMP_ADAPTER) NULL;
738     INT                 status;
739         PVOID                           handle;
740         struct pci_dev *dev_p = (struct pci_dev *)_dev_p;
741
742
743 #ifdef CONFIG_STA_SUPPORT
744     DBGPRINT(RT_DEBUG_TRACE, ("STA Driver version-%s\n", STA_DRIVER_VERSION));
745 #endif // CONFIG_STA_SUPPORT //
746
747     net_dev = alloc_etherdev(sizeof(PRTMP_ADAPTER));
748     if (net_dev == NULL)
749     {
750         printk("alloc_netdev failed\n");
751
752         goto err_out;
753     }
754
755         netif_stop_queue(net_dev);
756 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
757 /* for supporting Network Manager */
758 /* Set the sysfs physical device reference for the network logical device
759  * if set prior to registration will cause a symlink during initialization.
760  */
761     SET_NETDEV_DEV(net_dev, &(dev_p->dev));
762 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
763
764         // Allocate RTMP_ADAPTER miniport adapter structure
765         handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
766         RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p);
767
768         status = RTMPAllocAdapterBlock(handle, &pAd);
769         if (status != NDIS_STATUS_SUCCESS)
770                 goto err_out_free_netdev;
771
772         net_dev->ml_priv = (PVOID)pAd;
773     pAd->net_dev = net_dev; // must be before RT28XXNetDevInit()
774
775         RT28XXNetDevInit(_dev_p, net_dev, pAd);
776
777 #ifdef CONFIG_STA_SUPPORT
778     pAd->StaCfg.OriDevType = net_dev->type;
779 #endif // CONFIG_STA_SUPPORT //
780
781         // Post config
782         if (RT28XXProbePostConfig(_dev_p, pAd, 0) == FALSE)
783                 goto err_out_unmap;
784
785 #ifdef CONFIG_STA_SUPPORT
786         pAd->OpMode = OPMODE_STA;
787 #endif // CONFIG_STA_SUPPORT //
788
789         // sample move
790         if (rt_ieee80211_if_setup(net_dev, pAd) != NDIS_STATUS_SUCCESS)
791                 goto err_out_unmap;
792
793     // Register this device
794     status = register_netdev(net_dev);
795     if (status)
796         goto err_out_unmap;
797
798     // Set driver data
799         RT28XX_DRVDATA_SET(_dev_p);
800
801         *ppAd = pAd;
802     return 0; // probe ok
803
804
805         /* --------------------------- ERROR HANDLE --------------------------- */
806 err_out_unmap:
807         RTMPFreeAdapter(pAd);
808         RT28XX_UNMAP();
809
810 err_out_free_netdev:
811         free_netdev(net_dev);
812
813 err_out:
814         RT28XX_PUT_DEVICE(dev_p);
815
816         return -ENODEV; /* probe fail */
817 } /* End of rt28xx_probe */
818
819
820 /*
821 ========================================================================
822 Routine Description:
823     The entry point for Linux kernel sent packet to our driver.
824
825 Arguments:
826     sk_buff *skb                the pointer refer to a sk_buffer.
827
828 Return Value:
829     0
830
831 Note:
832         This function is the entry point of Tx Path for Os delivery packet to
833         our driver. You only can put OS-depened & STA/AP common handle procedures
834         in here.
835 ========================================================================
836 */
837 int rt28xx_packet_xmit(struct sk_buff *skb)
838 {
839         struct net_device *net_dev = skb->dev;
840         PRTMP_ADAPTER pAd = net_dev->ml_priv;
841         int status = 0;
842         PNDIS_PACKET pPacket = (PNDIS_PACKET) skb;
843
844 #ifdef CONFIG_STA_SUPPORT
845         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
846         {
847                 // Drop send request since we are in monitor mode
848                 if (MONITOR_ON(pAd))
849                 {
850                         RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
851                         goto done;
852                 }
853         }
854 #endif // CONFIG_STA_SUPPORT //
855
856         // EapolStart size is 18
857         if (skb->len < 14)
858         {
859                 //printk("bad packet size: %d\n", pkt->len);
860                 hex_dump("bad packet", skb->data, skb->len);
861                 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
862                 goto done;
863         }
864
865         RTMP_SET_PACKET_5VT(pPacket, 0);
866 #ifdef CONFIG_5VT_ENHANCE
867     if (*(int*)(skb->cb) == BRIDGE_TAG) {
868                 RTMP_SET_PACKET_5VT(pPacket, 1);
869     }
870 #endif
871
872
873
874 #ifdef CONFIG_STA_SUPPORT
875         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
876         {
877
878                 STASendPackets((NDIS_HANDLE)pAd, (PPNDIS_PACKET) &pPacket, 1);
879         }
880
881 #endif // CONFIG_STA_SUPPORT //
882
883         status = 0;
884 done:
885
886         return status;
887 }
888
889
890 /*
891 ========================================================================
892 Routine Description:
893     Send a packet to WLAN.
894
895 Arguments:
896     skb_p           points to our adapter
897     dev_p           which WLAN network interface
898
899 Return Value:
900     0: transmit successfully
901     otherwise: transmit fail
902
903 Note:
904 ========================================================================
905 */
906 INT rt28xx_send_packets(
907         IN struct sk_buff               *skb_p,
908         IN struct net_device    *net_dev)
909 {
910     RTMP_ADAPTER *pAd = net_dev->ml_priv;
911         if (!(net_dev->flags & IFF_UP))
912         {
913                 RELEASE_NDIS_PACKET(pAd, (PNDIS_PACKET)skb_p, NDIS_STATUS_FAILURE);
914                 return 0;
915         }
916
917         NdisZeroMemory((PUCHAR)&skb_p->cb[CB_OFF], 15);
918         RTMP_SET_PACKET_NET_DEVICE_MBSSID(skb_p, MAIN_MBSSID);
919
920         return rt28xx_packet_xmit(skb_p);
921
922 } /* End of MBSS_VirtualIF_PacketSend */
923
924
925
926
927 void CfgInitHook(PRTMP_ADAPTER pAd)
928 {
929         pAd->bBroadComHT = TRUE;
930 } /* End of CfgInitHook */
931
932
933 #if WIRELESS_EXT >= 12
934 // This function will be called when query /proc
935 struct iw_statistics *rt28xx_get_wireless_stats(
936     IN struct net_device *net_dev)
937 {
938         PRTMP_ADAPTER pAd = net_dev->ml_priv;
939
940
941         DBGPRINT(RT_DEBUG_TRACE, ("rt28xx_get_wireless_stats --->\n"));
942
943         pAd->iw_stats.status = 0; // Status - device dependent for now
944
945         // link quality
946         pAd->iw_stats.qual.qual = ((pAd->Mlme.ChannelQuality * 12)/10 + 10);
947         if(pAd->iw_stats.qual.qual > 100)
948                 pAd->iw_stats.qual.qual = 100;
949
950 #ifdef CONFIG_STA_SUPPORT
951         if (pAd->OpMode == OPMODE_STA)
952                 pAd->iw_stats.qual.level = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
953 #endif // CONFIG_STA_SUPPORT //
954
955         pAd->iw_stats.qual.noise = pAd->BbpWriteLatch[66]; // noise level (dBm)
956
957         pAd->iw_stats.qual.noise += 256 - 143;
958         pAd->iw_stats.qual.updated = 1;     // Flags to know if updated
959 #ifdef IW_QUAL_DBM
960         pAd->iw_stats.qual.updated |= IW_QUAL_DBM;      // Level + Noise are dBm
961 #endif // IW_QUAL_DBM //
962
963         pAd->iw_stats.discard.nwid = 0;     // Rx : Wrong nwid/essid
964         pAd->iw_stats.miss.beacon = 0;      // Missed beacons/superframe
965
966         DBGPRINT(RT_DEBUG_TRACE, ("<--- rt28xx_get_wireless_stats\n"));
967         return &pAd->iw_stats;
968 } /* End of rt28xx_get_wireless_stats */
969 #endif // WIRELESS_EXT //
970
971
972
973 void tbtt_tasklet(unsigned long data)
974 {
975 #define MAX_TX_IN_TBTT          (16)
976
977 }
978
979 INT rt28xx_ioctl(
980         IN      struct net_device       *net_dev,
981         IN      OUT     struct ifreq    *rq,
982         IN      INT                                     cmd)
983 {
984         VIRTUAL_ADAPTER *pVirtualAd = NULL;
985         RTMP_ADAPTER    *pAd = NULL;
986         INT                             ret = 0;
987
988         if (net_dev->priv_flags == INT_MAIN)
989         {
990                 pAd = net_dev->ml_priv;
991         }
992         else
993         {
994                 pVirtualAd = net_dev->ml_priv;
995                 pAd = pVirtualAd->RtmpDev->ml_priv;
996         }
997
998         if (pAd == NULL)
999         {
1000                 /* if 1st open fail, pAd will be free;
1001                    So the net_dev->ml_priv will be NULL in 2rd open */
1002                 return -ENETDOWN;
1003         }
1004
1005
1006 #ifdef CONFIG_STA_SUPPORT
1007         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1008         {
1009                 ret = rt28xx_sta_ioctl(net_dev, rq, cmd);
1010         }
1011 #endif // CONFIG_STA_SUPPORT //
1012
1013         return ret;
1014 }
1015
1016 /*
1017     ========================================================================
1018
1019     Routine Description:
1020         return ethernet statistics counter
1021
1022     Arguments:
1023         net_dev                     Pointer to net_device
1024
1025     Return Value:
1026         net_device_stats*
1027
1028     Note:
1029
1030     ========================================================================
1031 */
1032 struct net_device_stats *RT28xx_get_ether_stats(
1033     IN  struct net_device *net_dev)
1034 {
1035     RTMP_ADAPTER *pAd = NULL;
1036
1037         if (net_dev)
1038                 pAd = net_dev->ml_priv;
1039
1040         if (pAd)
1041         {
1042
1043                 pAd->stats.rx_packets = pAd->WlanCounters.ReceivedFragmentCount.QuadPart;
1044                 pAd->stats.tx_packets = pAd->WlanCounters.TransmittedFragmentCount.QuadPart;
1045
1046                 pAd->stats.rx_bytes = pAd->RalinkCounters.ReceivedByteCount;
1047                 pAd->stats.tx_bytes = pAd->RalinkCounters.TransmittedByteCount;
1048
1049                 pAd->stats.rx_errors = pAd->Counters8023.RxErrors;
1050                 pAd->stats.tx_errors = pAd->Counters8023.TxErrors;
1051
1052                 pAd->stats.rx_dropped = 0;
1053                 pAd->stats.tx_dropped = 0;
1054
1055             pAd->stats.multicast = pAd->WlanCounters.MulticastReceivedFrameCount.QuadPart;   // multicast packets received
1056             pAd->stats.collisions = pAd->Counters8023.OneCollision + pAd->Counters8023.MoreCollisions;  // Collision packets
1057
1058             pAd->stats.rx_length_errors = 0;
1059             pAd->stats.rx_over_errors = pAd->Counters8023.RxNoBuffer;                   // receiver ring buff overflow
1060             pAd->stats.rx_crc_errors = 0;//pAd->WlanCounters.FCSErrorCount;     // recved pkt with crc error
1061             pAd->stats.rx_frame_errors = pAd->Counters8023.RcvAlignmentErrors;          // recv'd frame alignment error
1062             pAd->stats.rx_fifo_errors = pAd->Counters8023.RxNoBuffer;                   // recv'r fifo overrun
1063             pAd->stats.rx_missed_errors = 0;                                            // receiver missed packet
1064
1065             // detailed tx_errors
1066             pAd->stats.tx_aborted_errors = 0;
1067             pAd->stats.tx_carrier_errors = 0;
1068             pAd->stats.tx_fifo_errors = 0;
1069             pAd->stats.tx_heartbeat_errors = 0;
1070             pAd->stats.tx_window_errors = 0;
1071
1072             // for cslip etc
1073             pAd->stats.rx_compressed = 0;
1074             pAd->stats.tx_compressed = 0;
1075
1076                 return &pAd->stats;
1077         }
1078         else
1079         return NULL;
1080 }
1081