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