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