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