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