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