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