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