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