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