Staging: rt2860: remove dead RALINK_ATE code
[linux-2.6] / drivers / staging / rt2860 / rt_main_dev.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27     Module Name:
28     rt_main_dev.c
29
30     Abstract:
31     Create and register network interface.
32
33     Revision History:
34     Who         When            What
35     --------    ----------      ----------------------------------------------
36         Sample          Mar/21/07               Merge RT2870 and RT2860 drivers.
37 */
38
39 #include "rt_config.h"
40
41 #define FORTY_MHZ_INTOLERANT_INTERVAL   (60*1000) // 1 min
42
43 #ifdef MULTIPLE_CARD_SUPPORT
44 // record whether the card in the card list is used in the card file
45 UINT8  MC_CardUsed[MAX_NUM_OF_MULTIPLE_CARD];
46 // record used card mac address in the card list
47 static UINT8  MC_CardMac[MAX_NUM_OF_MULTIPLE_CARD][6];
48 #endif // MULTIPLE_CARD_SUPPORT //
49
50 /*---------------------------------------------------------------------*/
51 /* Private Variables Used                                              */
52 /*---------------------------------------------------------------------*/
53 //static RALINK_TIMER_STRUCT     PeriodicTimer;
54
55 char *mac = "";            // default 00:00:00:00:00:00
56 char *hostname = "";               // default CMPC
57 module_param (mac, charp, 0);
58 MODULE_PARM_DESC (mac, "rt28xx: wireless mac addr");
59
60
61 /*---------------------------------------------------------------------*/
62 /* Prototypes of Functions Used                                        */
63 /*---------------------------------------------------------------------*/
64 #ifdef DOT11_N_SUPPORT
65 extern BOOLEAN ba_reordering_resource_init(PRTMP_ADAPTER pAd, int num);
66 extern void ba_reordering_resource_release(PRTMP_ADAPTER pAd);
67 #endif // DOT11_N_SUPPORT //
68 extern NDIS_STATUS NICLoadRateSwitchingParams(IN PRTMP_ADAPTER pAd);
69
70 extern void init_thread_task(PRTMP_ADAPTER pAd);
71
72 // public function prototype
73 INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
74                                                         IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
75
76 // private function prototype
77 static int rt28xx_init(IN struct net_device *net_dev);
78 INT rt28xx_send_packets(IN struct sk_buff *skb_p, IN struct net_device *net_dev);
79
80 static void CfgInitHook(PRTMP_ADAPTER pAd);
81
82 #ifdef CONFIG_STA_SUPPORT
83 extern  const struct iw_handler_def rt28xx_iw_handler_def;
84 #endif // CONFIG_STA_SUPPORT //
85
86 #if WIRELESS_EXT >= 12
87 // This function will be called when query /proc
88 struct iw_statistics *rt28xx_get_wireless_stats(
89     IN struct net_device *net_dev);
90 #endif
91
92 struct net_device_stats *RT28xx_get_ether_stats(
93     IN  struct net_device *net_dev);
94
95 /*
96 ========================================================================
97 Routine Description:
98     Close raxx interface.
99
100 Arguments:
101         *net_dev                        the raxx interface pointer
102
103 Return Value:
104     0                                   Open OK
105         otherwise                       Open Fail
106
107 Note:
108         1. if open fail, kernel will not call the close function.
109         2. Free memory for
110                 (1) Mlme Memory Handler:                MlmeHalt()
111                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
112                 (3) BA Reordering:                              ba_reordering_resource_release()
113 ========================================================================
114 */
115 int MainVirtualIF_close(IN struct net_device *net_dev)
116 {
117     RTMP_ADAPTER *pAd = net_dev->ml_priv;
118
119         // Sanity check for pAd
120         if (pAd == NULL)
121                 return 0; // close ok
122
123         netif_carrier_off(pAd->net_dev);
124         netif_stop_queue(pAd->net_dev);
125
126
127         VIRTUAL_IF_DOWN(pAd);
128
129         RT_MOD_DEC_USE_COUNT();
130
131         return 0; // close ok
132 }
133
134 /*
135 ========================================================================
136 Routine Description:
137     Open raxx interface.
138
139 Arguments:
140         *net_dev                        the raxx interface pointer
141
142 Return Value:
143     0                                   Open OK
144         otherwise                       Open Fail
145
146 Note:
147         1. if open fail, kernel will not call the close function.
148         2. Free memory for
149                 (1) Mlme Memory Handler:                MlmeHalt()
150                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
151                 (3) BA Reordering:                              ba_reordering_resource_release()
152 ========================================================================
153 */
154 int MainVirtualIF_open(IN struct net_device *net_dev)
155 {
156     RTMP_ADAPTER *pAd = net_dev->ml_priv;
157
158         // Sanity check for pAd
159         if (pAd == NULL)
160                 return 0; // close ok
161
162         if (VIRTUAL_IF_UP(pAd) != 0)
163                 return -1;
164
165         // increase MODULE use count
166         RT_MOD_INC_USE_COUNT();
167
168         netif_start_queue(net_dev);
169         netif_carrier_on(net_dev);
170         netif_wake_queue(net_dev);
171
172         return 0;
173 }
174
175 /*
176 ========================================================================
177 Routine Description:
178     Close raxx interface.
179
180 Arguments:
181         *net_dev                        the raxx interface pointer
182
183 Return Value:
184     0                                   Open OK
185         otherwise                       Open Fail
186
187 Note:
188         1. if open fail, kernel will not call the close function.
189         2. Free memory for
190                 (1) Mlme Memory Handler:                MlmeHalt()
191                 (2) TX & RX:                                    RTMPFreeTxRxRingMemory()
192                 (3) BA Reordering:                              ba_reordering_resource_release()
193 ========================================================================
194 */
195 int rt28xx_close(IN PNET_DEV dev)
196 {
197         struct net_device * net_dev = (struct net_device *)dev;
198     RTMP_ADAPTER        *pAd = net_dev->ml_priv;
199         BOOLEAN                 Cancelled = FALSE;
200         UINT32                  i = 0;
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                 // If dirver doesn't wake up firmware here,
213                 // NICLoadFirmware will hang forever when interface is up again.
214                 if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE) ||
215                         RTMP_SET_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND) ||
216                         RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF))
217         {
218                     AsicForceWakeup(pAd, RTMP_HALT);
219         }
220
221 #ifdef QOS_DLS_SUPPORT
222                 // send DLS-TEAR_DOWN message,
223                 if (pAd->CommonCfg.bDLSCapable)
224                 {
225                         UCHAR i;
226
227                         // tear down local dls table entry
228                         for (i=0; i<MAX_NUM_OF_INIT_DLS_ENTRY; i++)
229                         {
230                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
231                                 {
232                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
233                                         pAd->StaCfg.DLSEntry[i].Status  = DLS_NONE;
234                                         pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
235                                 }
236                         }
237
238                         // tear down peer dls table entry
239                         for (i=MAX_NUM_OF_INIT_DLS_ENTRY; i<MAX_NUM_OF_DLS_ENTRY; i++)
240                         {
241                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
242                                 {
243                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
244                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
245                                         pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
246                                 }
247                         }
248                         RT28XX_MLME_HANDLER(pAd);
249                 }
250 #endif // QOS_DLS_SUPPORT //
251
252                 if (INFRA_ON(pAd) &&
253                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
254                 {
255                         MLME_DISASSOC_REQ_STRUCT        DisReq;
256                         MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
257
258                         COPY_MAC_ADDR(DisReq.Addr, pAd->CommonCfg.Bssid);
259                         DisReq.Reason =  REASON_DEAUTH_STA_LEAVING;
260
261                         MsgElem->Machine = ASSOC_STATE_MACHINE;
262                         MsgElem->MsgType = MT2_MLME_DISASSOC_REQ;
263                         MsgElem->MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
264                         NdisMoveMemory(MsgElem->Msg, &DisReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
265
266                         // Prevent to connect AP again in STAMlmePeriodicExec
267                         pAd->MlmeAux.AutoReconnectSsidLen= 32;
268                         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
269
270                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
271                         MlmeDisassocReqAction(pAd, MsgElem);
272                         kfree(MsgElem);
273
274                         RTMPusecDelay(1000);
275                 }
276
277
278 #ifdef CCX_SUPPORT
279                 RTMPCancelTimer(&pAd->StaCfg.LeapAuthTimer, &Cancelled);
280 #endif
281
282                 RTMPCancelTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, &Cancelled);
283                 RTMPCancelTimer(&pAd->StaCfg.WpaDisassocAndBlockAssocTimer, &Cancelled);
284
285 #ifdef WPA_SUPPLICANT_SUPPORT
286 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
287                 {
288                         union iwreq_data    wrqu;
289                         // send wireless event to wpa_supplicant for infroming interface down.
290                         memset(&wrqu, 0, sizeof(wrqu));
291                         wrqu.data.flags = RT_INTERFACE_DOWN;
292                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
293                 }
294 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
295 #endif // WPA_SUPPLICANT_SUPPORT //
296
297                 MlmeRadioOff(pAd);
298                 pAd->bPCIclkOff = FALSE;
299         }
300 #endif // CONFIG_STA_SUPPORT //
301
302         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
303
304         for (i = 0 ; i < NUM_OF_TX_RING; i++)
305         {
306                 while (pAd->DeQueueRunning[i] == TRUE)
307                 {
308                         printk("Waiting for TxQueue[%d] done..........\n", i);
309                         RTMPusecDelay(1000);
310                 }
311         }
312
313         // Stop Mlme state machine
314         MlmeHalt(pAd);
315
316         // Close kernel threads or tasklets
317         kill_thread_task(pAd);
318
319
320 #ifdef CONFIG_STA_SUPPORT
321         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
322         {
323                 MacTableReset(pAd);
324         }
325 #endif // CONFIG_STA_SUPPORT //
326
327
328         MeasureReqTabExit(pAd);
329         TpcReqTabExit(pAd);
330
331
332         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
333         {
334                 NICDisableInterrupt(pAd);
335         }
336
337         // Disable Rx, register value supposed will remain after reset
338         NICIssueReset(pAd);
339
340         // Free IRQ
341         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
342         {
343                 // Deregister interrupt function
344                 RT28XX_IRQ_RELEASE(net_dev)
345                 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
346         }
347
348
349         // Free Ring or USB buffers
350         RTMPFreeTxRxRingMemory(pAd);
351
352         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
353
354 #ifdef DOT11_N_SUPPORT
355         // Free BA reorder resource
356         ba_reordering_resource_release(pAd);
357 #endif // DOT11_N_SUPPORT //
358
359
360         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_START_UP);
361
362         return 0; // close ok
363 } /* End of rt28xx_close */
364
365 static int rt28xx_init(IN struct net_device *net_dev)
366 {
367         PRTMP_ADAPTER                   pAd = (PRTMP_ADAPTER)net_dev->ml_priv;
368         UINT                                    index;
369         UCHAR                                   TmpPhy;
370         NDIS_STATUS                             Status;
371         UINT32          MacCsr0 = 0;
372
373
374 #ifdef DOT11_N_SUPPORT
375         // Allocate BA Reordering memory
376         ba_reordering_resource_init(pAd, MAX_REORDERING_MPDU_NUM);
377 #endif // DOT11_N_SUPPORT //
378
379         // Make sure MAC gets ready.
380         index = 0;
381         do
382         {
383                 RTMP_IO_READ32(pAd, MAC_CSR0, &MacCsr0);
384                 pAd->MACVersion = MacCsr0;
385
386                 if ((pAd->MACVersion != 0x00) && (pAd->MACVersion != 0xFFFFFFFF))
387                         break;
388
389                 RTMPusecDelay(10);
390         } while (index++ < 100);
391
392         DBGPRINT(RT_DEBUG_TRACE, ("MAC_CSR0  [ Ver:Rev=0x%08x]\n", pAd->MACVersion));
393
394         // Disable DMA
395         RT28XXDMADisable(pAd);
396
397
398         // Load 8051 firmware
399         Status = NICLoadFirmware(pAd);
400         if (Status != NDIS_STATUS_SUCCESS)
401         {
402                 DBGPRINT_ERR(("NICLoadFirmware failed, Status[=0x%08x]\n", Status));
403                 goto err1;
404         }
405
406         NICLoadRateSwitchingParams(pAd);
407
408         // Disable interrupts here which is as soon as possible
409         // This statement should never be true. We might consider to remove it later
410         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
411         {
412                 NICDisableInterrupt(pAd);
413         }
414
415         Status = RTMPAllocTxRxRingMemory(pAd);
416         if (Status != NDIS_STATUS_SUCCESS)
417         {
418                 DBGPRINT_ERR(("RTMPAllocDMAMemory failed, Status[=0x%08x]\n", Status));
419                 goto err1;
420         }
421
422         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
423
424         // initialize MLME
425         //
426
427         Status = MlmeInit(pAd);
428         if (Status != NDIS_STATUS_SUCCESS)
429         {
430                 DBGPRINT_ERR(("MlmeInit failed, Status[=0x%08x]\n", Status));
431                 goto err2;
432         }
433
434         // Initialize pAd->StaCfg, pAd->ApCfg, pAd->CommonCfg to manufacture default
435         //
436         UserCfgInit(pAd);
437
438
439         RT28XX_TASK_THREAD_INIT(pAd, Status);
440         if (Status != NDIS_STATUS_SUCCESS)
441                 goto err1;
442
443         CfgInitHook(pAd);
444
445
446 #ifdef BLOCK_NET_IF
447         initblockQueueTab(pAd);
448 #endif // BLOCK_NET_IF //
449
450 #ifdef CONFIG_STA_SUPPORT
451         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
452                 NdisAllocateSpinLock(&pAd->MacTabLock);
453 #endif // CONFIG_STA_SUPPORT //
454
455         MeasureReqTabInit(pAd);
456         TpcReqTabInit(pAd);
457
458         //
459         // Init the hardware, we need to init asic before read registry, otherwise mac register will be reset
460         //
461         Status = NICInitializeAdapter(pAd, TRUE);
462         if (Status != NDIS_STATUS_SUCCESS)
463         {
464                 DBGPRINT_ERR(("NICInitializeAdapter failed, Status[=0x%08x]\n", Status));
465                 if (Status != NDIS_STATUS_SUCCESS)
466                 goto err3;
467         }
468
469         // Read parameters from Config File
470         Status = RTMPReadParametersHook(pAd);
471
472         printk("1. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
473         if (Status != NDIS_STATUS_SUCCESS)
474         {
475                 DBGPRINT_ERR(("NICReadRegParameters failed, Status[=0x%08x]\n",Status));
476                 goto err4;
477         }
478
479
480
481         //Init Ba Capability parameters.
482 #ifdef DOT11_N_SUPPORT
483         pAd->CommonCfg.DesiredHtPhy.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
484         pAd->CommonCfg.DesiredHtPhy.AmsduEnable = (USHORT)pAd->CommonCfg.BACapability.field.AmsduEnable;
485         pAd->CommonCfg.DesiredHtPhy.AmsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
486         pAd->CommonCfg.DesiredHtPhy.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
487         // UPdata to HT IE
488         pAd->CommonCfg.HtCapability.HtCapInfo.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
489         pAd->CommonCfg.HtCapability.HtCapInfo.AMsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
490         pAd->CommonCfg.HtCapability.HtCapParm.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
491 #endif // DOT11_N_SUPPORT //
492
493         printk("2. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
494
495         // We should read EEPROM for all cases.  rt2860b
496         NICReadEEPROMParameters(pAd, mac);
497
498         printk("3. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
499
500         NICInitAsicFromEEPROM(pAd); //rt2860b
501
502         // Set PHY to appropriate mode
503         TmpPhy = pAd->CommonCfg.PhyMode;
504         pAd->CommonCfg.PhyMode = 0xff;
505         RTMPSetPhyMode(pAd, TmpPhy);
506 #ifdef DOT11_N_SUPPORT
507         SetCommonHT(pAd);
508 #endif // DOT11_N_SUPPORT //
509
510         // No valid channels.
511         if (pAd->ChannelListNum == 0)
512         {
513                 printk("Wrong configuration. No valid channel found. Check \"ContryCode\" and \"ChannelGeography\" setting.\n");
514                 goto err4;
515         }
516
517 #ifdef DOT11_N_SUPPORT
518         printk("MCS Set = %02x %02x %02x %02x %02x\n", pAd->CommonCfg.HtCapability.MCSSet[0],
519            pAd->CommonCfg.HtCapability.MCSSet[1], pAd->CommonCfg.HtCapability.MCSSet[2],
520            pAd->CommonCfg.HtCapability.MCSSet[3], pAd->CommonCfg.HtCapability.MCSSet[4]);
521 #endif // DOT11_N_SUPPORT //
522
523 #ifdef IKANOS_VX_1X0
524         VR_IKANOS_FP_Init(pAd->ApCfg.BssidNum, pAd->PermanentAddress);
525 #endif // IKANOS_VX_1X0 //
526
527                 //
528         // Initialize RF register to default value
529         //
530         AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
531         AsicLockChannel(pAd, pAd->CommonCfg.Channel);
532
533         // 8051 firmware require the signal during booting time.
534         AsicSendCommandToMcu(pAd, 0x72, 0xFF, 0x00, 0x00);
535
536         if (pAd && (Status != NDIS_STATUS_SUCCESS))
537         {
538                 //
539                 // Undo everything if it failed
540                 //
541                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
542                 {
543                         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
544                 }
545         }
546         else if (pAd)
547         {
548                 // Microsoft HCT require driver send a disconnect event after driver initialization.
549                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
550                 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE);
551                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event B!\n"));
552
553
554         }// end of else
555
556
557         DBGPRINT_S(Status, ("<==== RTMPInitialize, Status=%x\n", Status));
558
559         return TRUE;
560
561
562 err4:
563 err3:
564         MlmeHalt(pAd);
565 err2:
566         RTMPFreeTxRxRingMemory(pAd);
567 err1:
568
569 #ifdef DOT11_N_SUPPORT
570         os_free_mem(pAd, pAd->mpdu_blk_pool.mem); // free BA pool
571 #endif // DOT11_N_SUPPORT //
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 #ifdef INF_AMAZON_SE
577 err0:
578 #endif // INF_AMAZON_SE //
579         printk("!!! %s Initialized fail !!!\n", RT28xx_CHIP_NAME);
580         return FALSE;
581 } /* End of rt28xx_init */
582
583
584 /*
585 ========================================================================
586 Routine Description:
587     Open raxx interface.
588
589 Arguments:
590         *net_dev                        the raxx interface pointer
591
592 Return Value:
593     0                                   Open OK
594         otherwise                       Open Fail
595
596 Note:
597 ========================================================================
598 */
599 int rt28xx_open(IN PNET_DEV dev)
600 {
601         struct net_device * net_dev = (struct net_device *)dev;
602         PRTMP_ADAPTER pAd = net_dev->ml_priv;
603         int retval = 0;
604         POS_COOKIE pObj;
605
606
607         // Sanity check for pAd
608         if (pAd == NULL)
609         {
610                 /* if 1st open fail, pAd will be free;
611                    So the net_dev->ml_priv will be NULL in 2rd open */
612                 return -1;
613         }
614
615         // Init
616         pObj = (POS_COOKIE)pAd->OS_Cookie;
617
618         // reset Adapter flags
619         RTMP_CLEAR_FLAGS(pAd);
620
621         // Request interrupt service routine for PCI device
622         // register the interrupt routine with the os
623         RT28XX_IRQ_REQUEST(net_dev);
624
625
626         // Init BssTab & ChannelInfo tabbles for auto channel select.
627
628
629         // Chip & other init
630         if (rt28xx_init(net_dev) == FALSE)
631                 goto err;
632
633 #ifdef CONFIG_STA_SUPPORT
634         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
635         {
636                 NdisZeroMemory(pAd->StaCfg.dev_name, 16);
637                 NdisMoveMemory(pAd->StaCfg.dev_name, net_dev->name, strlen(net_dev->name));
638         }
639 #endif // CONFIG_STA_SUPPORT //
640
641         // Set up the Mac address
642         NdisMoveMemory(net_dev->dev_addr, (void *) pAd->CurrentAddress, 6);
643
644         // Init IRQ parameters
645         RT28XX_IRQ_INIT(pAd);
646
647         // Various AP function init
648
649 #ifdef CONFIG_STA_SUPPORT
650         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
651         {
652 #ifdef WPA_SUPPLICANT_SUPPORT
653 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
654                 {
655                         union iwreq_data    wrqu;
656                         // send wireless event to wpa_supplicant for infroming interface down.
657                         memset(&wrqu, 0, sizeof(wrqu));
658                         wrqu.data.flags = RT_INTERFACE_UP;
659                         wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
660                 }
661 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
662 #endif // WPA_SUPPLICANT_SUPPORT //
663
664         }
665 #endif // CONFIG_STA_SUPPORT //
666
667         // Enable Interrupt
668         RT28XX_IRQ_ENABLE(pAd);
669
670         // Now Enable RxTx
671         RTMPEnableRxTx(pAd);
672         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP);
673
674         {
675         UINT32 reg = 0;
676         RTMP_IO_READ32(pAd, 0x1300, &reg);  // clear garbage interrupts
677         printk("0x1300 = %08x\n", reg);
678         }
679
680 #ifdef CONFIG_STA_SUPPORT
681         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
682         RTMPInitPCIeLinkCtrlValue(pAd);
683 #endif // CONFIG_STA_SUPPORT //
684
685         return (retval);
686
687 err:
688         return (-1);
689 } /* End of rt28xx_open */
690
691 static const struct net_device_ops rt2860_netdev_ops = {
692         .ndo_open               = MainVirtualIF_open,
693         .ndo_stop               = MainVirtualIF_close,
694         .ndo_do_ioctl           = rt28xx_ioctl,
695         .ndo_get_stats          = RT28xx_get_ether_stats,
696         .ndo_validate_addr      = NULL,
697         .ndo_set_mac_address    = eth_mac_addr,
698         .ndo_change_mtu         = eth_change_mtu,
699 #ifdef IKANOS_VX_1X0
700         .ndo_start_xmit         = IKANOS_DataFramesTx,
701 #else
702         .ndo_start_xmit         = rt28xx_send_packets,
703 #endif
704 };
705
706 /* Must not be called for mdev and apdev */
707 static NDIS_STATUS rt_ieee80211_if_setup(struct net_device *dev, PRTMP_ADAPTER pAd)
708 {
709         NDIS_STATUS Status;
710         INT     i=0;
711         CHAR    slot_name[IFNAMSIZ];
712         struct net_device   *device;
713
714
715         //ether_setup(dev);
716
717 #ifdef CONFIG_STA_SUPPORT
718 #if WIRELESS_EXT >= 12
719         if (pAd->OpMode == OPMODE_STA)
720         {
721                 dev->wireless_handlers = &rt28xx_iw_handler_def;
722         }
723 #endif //WIRELESS_EXT >= 12
724 #endif // CONFIG_STA_SUPPORT //
725
726 #if WIRELESS_EXT < 21
727                 dev->get_wireless_stats = rt28xx_get_wireless_stats;
728 #endif
729         dev->priv_flags = INT_MAIN;
730         dev->netdev_ops = &rt2860_netdev_ops;
731         // find available device name
732         for (i = 0; i < 8; i++)
733         {
734 #ifdef MULTIPLE_CARD_SUPPORT
735                 if (pAd->MC_RowID >= 0)
736                         sprintf(slot_name, "ra%02d_%d", pAd->MC_RowID, i);
737                 else
738 #endif // MULTIPLE_CARD_SUPPORT //
739                 sprintf(slot_name, "ra%d", i);
740
741                 device = dev_get_by_name(dev_net(dev), slot_name);
742                 if (device != NULL)
743                         dev_put(device);
744
745                 if (device == NULL)
746                         break;
747         }
748
749         if(i == 8)
750         {
751                 DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
752                 Status = NDIS_STATUS_FAILURE;
753         }
754         else
755         {
756 #ifdef MULTIPLE_CARD_SUPPORT
757                 if (pAd->MC_RowID >= 0)
758                 sprintf(dev->name, "ra%02d_%d", pAd->MC_RowID, i);
759                 else
760 #endif // MULTIPLE_CARD_SUPPORT //
761                 sprintf(dev->name, "ra%d", i);
762                 Status = NDIS_STATUS_SUCCESS;
763         }
764
765         return Status;
766
767 }
768
769
770 #ifdef MULTIPLE_CARD_SUPPORT
771 /*
772 ========================================================================
773 Routine Description:
774     Get card profile path.
775
776 Arguments:
777     pAd
778
779 Return Value:
780     TRUE                - Find a card profile
781         FALSE           - use default profile
782
783 Note:
784 ========================================================================
785 */
786 extern INT RTMPGetKeyParameter(
787     IN  PCHAR   key,
788     OUT PCHAR   dest,
789     IN  INT     destsize,
790     IN  PCHAR   buffer);
791
792 BOOLEAN RTMP_CardInfoRead(
793         IN      PRTMP_ADAPTER pAd)
794 {
795 #define MC_SELECT_CARDID                0       /* use CARD ID (0 ~ 31) to identify different cards */
796 #define MC_SELECT_MAC                   1       /* use CARD MAC to identify different cards */
797 #define MC_SELECT_CARDTYPE              2       /* use CARD type (abgn or bgn) to identify different cards */
798
799 #define LETTER_CASE_TRANSLATE(txt_p, card_id)                   \
800         {       UINT32 _len; char _char;                                                \
801                 for(_len=0; _len<strlen(card_id); _len++) {             \
802                         _char = *(txt_p + _len);                                        \
803                         if (('A' <= _char) && (_char <= 'Z'))           \
804                                 *(txt_p+_len) = 'a'+(_char-'A');                \
805                 } }
806
807         struct file *srcf;
808         INT retval, orgfsuid, orgfsgid;
809         mm_segment_t orgfs;
810         CHAR *buffer, *tmpbuf, card_id_buf[30], RFIC_word[30];
811         BOOLEAN flg_match_ok = FALSE;
812         INT32 card_select_method;
813         INT32 card_free_id, card_nouse_id, card_same_mac_id, card_match_id;
814         EEPROM_ANTENNA_STRUC antenna;
815         USHORT addr01, addr23, addr45;
816         UINT8 mac[6];
817         UINT32 data, card_index;
818         UCHAR *start_ptr;
819
820
821         // init
822         buffer = kmalloc(MAX_INI_BUFFER_SIZE, MEM_ALLOC_FLAG);
823         if (buffer == NULL)
824         return FALSE;
825
826         tmpbuf = kmalloc(MAX_PARAM_BUFFER_SIZE, MEM_ALLOC_FLAG);
827         if(tmpbuf == NULL)
828         {
829                 kfree(buffer);
830         return NDIS_STATUS_FAILURE;
831         }
832
833         orgfsuid = current->fsuid;
834         orgfsgid = current->fsgid;
835         current->fsuid = current->fsgid = 0;
836     orgfs = get_fs();
837     set_fs(KERNEL_DS);
838
839         // get RF IC type
840         RTMP_IO_READ32(pAd, E2PROM_CSR, &data);
841
842         if ((data & 0x30) == 0)
843                 pAd->EEPROMAddressNum = 6;      // 93C46
844         else if ((data & 0x30) == 0x10)
845                 pAd->EEPROMAddressNum = 8;      // 93C66
846         else
847                 pAd->EEPROMAddressNum = 8;      // 93C86
848
849         RT28xx_EEPROM_READ16(pAd, EEPROM_NIC1_OFFSET, antenna.word);
850
851         if ((antenna.field.RfIcType == RFIC_2850) ||
852                 (antenna.field.RfIcType == RFIC_2750))
853         {
854                 /* ABGN card */
855                 strcpy(RFIC_word, "abgn");
856         }
857         else
858         {
859                 /* BGN card */
860                 strcpy(RFIC_word, "bgn");
861         }
862
863         // get MAC address
864         RT28xx_EEPROM_READ16(pAd, 0x04, addr01);
865         RT28xx_EEPROM_READ16(pAd, 0x06, addr23);
866         RT28xx_EEPROM_READ16(pAd, 0x08, addr45);
867
868         mac[0] = (UCHAR)(addr01 & 0xff);
869         mac[1] = (UCHAR)(addr01 >> 8);
870         mac[2] = (UCHAR)(addr23 & 0xff);
871         mac[3] = (UCHAR)(addr23 >> 8);
872         mac[4] = (UCHAR)(addr45 & 0xff);
873         mac[5] = (UCHAR)(addr45 >> 8);
874
875         // open card information file
876         srcf = filp_open(CARD_INFO_PATH, O_RDONLY, 0);
877         if (IS_ERR(srcf))
878         {
879                 /* card information file does not exist */
880                         DBGPRINT(RT_DEBUG_TRACE,
881                                 ("--> Error %ld opening %s\n", -PTR_ERR(srcf), CARD_INFO_PATH));
882                 return FALSE;
883         }
884
885         if (srcf->f_op && srcf->f_op->read)
886         {
887                 /* card information file exists so reading the card information */
888                 memset(buffer, 0x00, MAX_INI_BUFFER_SIZE);
889                 retval = srcf->f_op->read(srcf, buffer, MAX_INI_BUFFER_SIZE, &srcf->f_pos);
890                 if (retval < 0)
891                 {
892                         /* read fail */
893                                 DBGPRINT(RT_DEBUG_TRACE,
894                                         ("--> Read %s error %d\n", CARD_INFO_PATH, -retval));
895                 }
896                 else
897                 {
898                         /* get card selection method */
899                         memset(tmpbuf, 0x00, MAX_PARAM_BUFFER_SIZE);
900                         card_select_method = MC_SELECT_CARDTYPE; // default
901
902                         if (RTMPGetKeyParameter("SELECT", tmpbuf, 256, buffer))
903                         {
904                                 if (strcmp(tmpbuf, "CARDID") == 0)
905                                         card_select_method = MC_SELECT_CARDID;
906                                 else if (strcmp(tmpbuf, "MAC") == 0)
907                                         card_select_method = MC_SELECT_MAC;
908                                 else if (strcmp(tmpbuf, "CARDTYPE") == 0)
909                                         card_select_method = MC_SELECT_CARDTYPE;
910                         }
911
912                         DBGPRINT(RT_DEBUG_TRACE,
913                                         ("MC> Card Selection = %d\n", card_select_method));
914
915                         // init
916                         card_free_id = -1;
917                         card_nouse_id = -1;
918                         card_same_mac_id = -1;
919                         card_match_id = -1;
920
921                         // search current card information records
922                         for(card_index=0;
923                                 card_index<MAX_NUM_OF_MULTIPLE_CARD;
924                                 card_index++)
925                         {
926                                 if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
927                                         (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
928                                 {
929                                         // MAC is all-0 so the entry is available
930                                         MC_CardUsed[card_index] = 0;
931
932                                         if (card_free_id < 0)
933                                                 card_free_id = card_index; // 1st free entry
934                                 }
935                                 else
936                                 {
937                                         if (memcmp(MC_CardMac[card_index], mac, 6) == 0)
938                                         {
939                                                 // we find the entry with same MAC
940                                                 if (card_same_mac_id < 0)
941                                                         card_same_mac_id = card_index; // 1st same entry
942                                         }
943                                         else
944                                         {
945                                                 // MAC is not all-0 but used flag == 0
946                                                 if ((MC_CardUsed[card_index] == 0) &&
947                                                         (card_nouse_id < 0))
948                                                 {
949                                                         card_nouse_id = card_index; // 1st available entry
950                                                 }
951                                         }
952                                 }
953                         }
954
955                         DBGPRINT(RT_DEBUG_TRACE,
956                                         ("MC> Free = %d, Same = %d, NOUSE = %d\n",
957                                         card_free_id, card_same_mac_id, card_nouse_id));
958
959                         if ((card_same_mac_id >= 0) &&
960                                 ((card_select_method == MC_SELECT_CARDID) ||
961                                 (card_select_method == MC_SELECT_CARDTYPE)))
962                         {
963                                 // same MAC entry is found
964                                 card_match_id = card_same_mac_id;
965
966                                 if (card_select_method == MC_SELECT_CARDTYPE)
967                                 {
968                                         // for CARDTYPE
969                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
970                                                         card_match_id, RFIC_word);
971
972                                         if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
973                                         {
974                                                 // we found the card ID
975                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
976                                         }
977                                 }
978                         }
979                         else
980                         {
981                                 // the card is 1st plug-in, try to find the match card profile
982                                 switch(card_select_method)
983                                 {
984                                         case MC_SELECT_CARDID: // CARDID
985                                         default:
986                                                 if (card_free_id >= 0)
987                                                         card_match_id = card_free_id;
988                                                 else
989                                                         card_match_id = card_nouse_id;
990                                                 break;
991
992                                         case MC_SELECT_MAC: // MAC
993                                                 sprintf(card_id_buf, "MAC%02x:%02x:%02x:%02x:%02x:%02x",
994                                                                 mac[0], mac[1], mac[2],
995                                                                 mac[3], mac[4], mac[5]);
996
997                                                 /* try to find the key word in the card file */
998                                                 if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
999                                                 {
1000                                                         LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1001
1002                                                         /* get the row ID (2 ASCII characters) */
1003                                                         start_ptr -= 2;
1004                                                         card_id_buf[0] = *(start_ptr);
1005                                                         card_id_buf[1] = *(start_ptr+1);
1006                                                         card_id_buf[2] = 0x00;
1007
1008                                                         card_match_id = simple_strtol(card_id_buf, 0, 10);
1009                                                 }
1010                                                 break;
1011
1012                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1013                                                 card_nouse_id = -1;
1014
1015                                                 for(card_index=0;
1016                                                         card_index<MAX_NUM_OF_MULTIPLE_CARD;
1017                                                         card_index++)
1018                                                 {
1019                                                         sprintf(card_id_buf, "%02dCARDTYPE%s",
1020                                                                         card_index, RFIC_word);
1021
1022                                                         if ((start_ptr=rtstrstruncasecmp(buffer,
1023                                                                                                                 card_id_buf)) != NULL)
1024                                                         {
1025                                                                 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1026
1027                                                                 if (MC_CardUsed[card_index] == 0)
1028                                                                 {
1029                                                                         /* current the card profile is not used */
1030                                                                         if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
1031                                                                                 (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
1032                                                                         {
1033                                                                                 // find it and no previous card use it
1034                                                                                 card_match_id = card_index;
1035                                                                                 break;
1036                                                                         }
1037                                                                         else
1038                                                                         {
1039                                                                                 // ever a card use it
1040                                                                                 if (card_nouse_id < 0)
1041                                                                                         card_nouse_id = card_index;
1042                                                                         }
1043                                                                 }
1044                                                         }
1045                                                 }
1046
1047                                                 // if not find a free one, use the available one
1048                                                 if (card_match_id < 0)
1049                                                         card_match_id = card_nouse_id;
1050                                                 break;
1051                                 }
1052                         }
1053
1054                         if (card_match_id >= 0)
1055                         {
1056                                 // make up search keyword
1057                                 switch(card_select_method)
1058                                 {
1059                                         case MC_SELECT_CARDID: // CARDID
1060                                                 sprintf(card_id_buf, "%02dCARDID", card_match_id);
1061                                                 break;
1062
1063                                         case MC_SELECT_MAC: // MAC
1064                                                 sprintf(card_id_buf,
1065                                                                 "%02dmac%02x:%02x:%02x:%02x:%02x:%02x",
1066                                                                 card_match_id,
1067                                                                 mac[0], mac[1], mac[2],
1068                                                                 mac[3], mac[4], mac[5]);
1069                                                 break;
1070
1071                                         case MC_SELECT_CARDTYPE: // CARDTYPE
1072                                         default:
1073                                                 sprintf(card_id_buf, "%02dcardtype%s",
1074                                                                 card_match_id, RFIC_word);
1075                                                 break;
1076                                 }
1077
1078                                 DBGPRINT(RT_DEBUG_TRACE, ("Search Keyword = %s\n", card_id_buf));
1079
1080                                 // read card file path
1081                                 if (RTMPGetKeyParameter(card_id_buf, tmpbuf, 256, buffer))
1082                                 {
1083                                         if (strlen(tmpbuf) < sizeof(pAd->MC_FileName))
1084                                         {
1085                                                 // backup card information
1086                                                 pAd->MC_RowID = card_match_id; /* base 0 */
1087                                                 MC_CardUsed[card_match_id] = 1;
1088                                                 memcpy(MC_CardMac[card_match_id], mac, sizeof(mac));
1089
1090                                                 // backup card file path
1091                                                 NdisMoveMemory(pAd->MC_FileName, tmpbuf , strlen(tmpbuf));
1092                                                 pAd->MC_FileName[strlen(tmpbuf)] = '\0';
1093                                                 flg_match_ok = TRUE;
1094
1095                                                 DBGPRINT(RT_DEBUG_TRACE,
1096                                                                 ("Card Profile Name = %s\n", pAd->MC_FileName));
1097                                         }
1098                                         else
1099                                         {
1100                                                 DBGPRINT(RT_DEBUG_ERROR,
1101                                                                 ("Card Profile Name length too large!\n"));
1102                                         }
1103                                 }
1104                                 else
1105                                 {
1106                                         DBGPRINT(RT_DEBUG_ERROR,
1107                                                         ("Can not find search key word in card.dat!\n"));
1108                                 }
1109
1110                                 if ((flg_match_ok != TRUE) &&
1111                                         (card_match_id < MAX_NUM_OF_MULTIPLE_CARD))
1112                                 {
1113                                         MC_CardUsed[card_match_id] = 0;
1114                                         memset(MC_CardMac[card_match_id], 0, sizeof(mac));
1115                                 }
1116                         } // if (card_match_id >= 0)
1117                 }
1118         }
1119
1120         // close file
1121         retval = filp_close(srcf, NULL);
1122         set_fs(orgfs);
1123         current->fsuid = orgfsuid;
1124         current->fsgid = orgfsgid;
1125         kfree(buffer);
1126         kfree(tmpbuf);
1127         return flg_match_ok;
1128 }
1129 #endif // MULTIPLE_CARD_SUPPORT //
1130
1131
1132 /*
1133 ========================================================================
1134 Routine Description:
1135     Probe RT28XX chipset.
1136
1137 Arguments:
1138     _dev_p                              Point to the PCI or USB device
1139         _dev_id_p                       Point to the PCI or USB device ID
1140
1141 Return Value:
1142     0                                   Probe OK
1143         -ENODEV                         Probe Fail
1144
1145 Note:
1146 ========================================================================
1147 */
1148 INT __devinit   rt28xx_probe(
1149     IN  void *_dev_p,
1150     IN  void *_dev_id_p,
1151         IN  UINT argc,
1152         OUT PRTMP_ADAPTER *ppAd)
1153 {
1154     struct  net_device  *net_dev;
1155     PRTMP_ADAPTER       pAd = (PRTMP_ADAPTER) NULL;
1156     INT                 status;
1157         PVOID                           handle;
1158         struct pci_dev *dev_p = (struct pci_dev *)_dev_p;
1159
1160
1161 #ifdef CONFIG_STA_SUPPORT
1162     DBGPRINT(RT_DEBUG_TRACE, ("STA Driver version-%s\n", STA_DRIVER_VERSION));
1163 #endif // CONFIG_STA_SUPPORT //
1164
1165     net_dev = alloc_etherdev(sizeof(PRTMP_ADAPTER));
1166     if (net_dev == NULL)
1167     {
1168         printk("alloc_netdev failed\n");
1169
1170         goto err_out;
1171     }
1172
1173         netif_stop_queue(net_dev);
1174 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
1175 /* for supporting Network Manager */
1176 /* Set the sysfs physical device reference for the network logical device
1177  * if set prior to registration will cause a symlink during initialization.
1178  */
1179     SET_NETDEV_DEV(net_dev, &(dev_p->dev));
1180 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
1181
1182         // Allocate RTMP_ADAPTER miniport adapter structure
1183         handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
1184         RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p);
1185
1186         status = RTMPAllocAdapterBlock(handle, &pAd);
1187         if (status != NDIS_STATUS_SUCCESS)
1188                 goto err_out_free_netdev;
1189
1190         net_dev->ml_priv = (PVOID)pAd;
1191     pAd->net_dev = net_dev; // must be before RT28XXNetDevInit()
1192
1193         RT28XXNetDevInit(_dev_p, net_dev, pAd);
1194
1195 #ifdef CONFIG_STA_SUPPORT
1196     pAd->StaCfg.OriDevType = net_dev->type;
1197 #endif // CONFIG_STA_SUPPORT //
1198
1199         // Post config
1200         if (RT28XXProbePostConfig(_dev_p, pAd, 0) == FALSE)
1201                 goto err_out_unmap;
1202
1203 #ifdef CONFIG_STA_SUPPORT
1204         pAd->OpMode = OPMODE_STA;
1205 #endif // CONFIG_STA_SUPPORT //
1206
1207
1208 #ifdef MULTIPLE_CARD_SUPPORT
1209         // find its profile path
1210         pAd->MC_RowID = -1; // use default profile path
1211         RTMP_CardInfoRead(pAd);
1212
1213         if (pAd->MC_RowID == -1)
1214 #ifdef CONFIG_STA_SUPPORT
1215                 strcpy(pAd->MC_FileName, STA_PROFILE_PATH);
1216 #endif // CONFIG_STA_SUPPORT //
1217
1218         DBGPRINT(RT_DEBUG_TRACE,
1219                         ("MC> ROW = %d, PATH = %s\n", pAd->MC_RowID, pAd->MC_FileName));
1220 #endif // MULTIPLE_CARD_SUPPORT //
1221
1222         // sample move
1223         if (rt_ieee80211_if_setup(net_dev, pAd) != NDIS_STATUS_SUCCESS)
1224                 goto err_out_unmap;
1225
1226     // Register this device
1227     status = register_netdev(net_dev);
1228     if (status)
1229         goto err_out_unmap;
1230
1231     // Set driver data
1232         RT28XX_DRVDATA_SET(_dev_p);
1233
1234         *ppAd = pAd;
1235     return 0; // probe ok
1236
1237
1238         /* --------------------------- ERROR HANDLE --------------------------- */
1239 err_out_unmap:
1240         RTMPFreeAdapter(pAd);
1241         RT28XX_UNMAP();
1242
1243 err_out_free_netdev:
1244         free_netdev(net_dev);
1245
1246 err_out:
1247         RT28XX_PUT_DEVICE(dev_p);
1248
1249         return -ENODEV; /* probe fail */
1250 } /* End of rt28xx_probe */
1251
1252
1253 /*
1254 ========================================================================
1255 Routine Description:
1256     The entry point for Linux kernel sent packet to our driver.
1257
1258 Arguments:
1259     sk_buff *skb                the pointer refer to a sk_buffer.
1260
1261 Return Value:
1262     0
1263
1264 Note:
1265         This function is the entry point of Tx Path for Os delivery packet to
1266         our driver. You only can put OS-depened & STA/AP common handle procedures
1267         in here.
1268 ========================================================================
1269 */
1270 int rt28xx_packet_xmit(struct sk_buff *skb)
1271 {
1272         struct net_device *net_dev = skb->dev;
1273         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1274         int status = 0;
1275         PNDIS_PACKET pPacket = (PNDIS_PACKET) skb;
1276
1277 #ifdef CONFIG_STA_SUPPORT
1278         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1279         {
1280                 // Drop send request since we are in monitor mode
1281                 if (MONITOR_ON(pAd))
1282                 {
1283                         RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1284                         goto done;
1285                 }
1286         }
1287 #endif // CONFIG_STA_SUPPORT //
1288
1289         // EapolStart size is 18
1290         if (skb->len < 14)
1291         {
1292                 //printk("bad packet size: %d\n", pkt->len);
1293                 hex_dump("bad packet", skb->data, skb->len);
1294                 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1295                 goto done;
1296         }
1297
1298         RTMP_SET_PACKET_5VT(pPacket, 0);
1299 #ifdef CONFIG_5VT_ENHANCE
1300     if (*(int*)(skb->cb) == BRIDGE_TAG) {
1301                 RTMP_SET_PACKET_5VT(pPacket, 1);
1302     }
1303 #endif
1304
1305
1306
1307 #ifdef CONFIG_STA_SUPPORT
1308         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1309         {
1310
1311                 STASendPackets((NDIS_HANDLE)pAd, (PPNDIS_PACKET) &pPacket, 1);
1312         }
1313
1314 #endif // CONFIG_STA_SUPPORT //
1315
1316         status = 0;
1317 done:
1318
1319         return status;
1320 }
1321
1322
1323 /*
1324 ========================================================================
1325 Routine Description:
1326     Send a packet to WLAN.
1327
1328 Arguments:
1329     skb_p           points to our adapter
1330     dev_p           which WLAN network interface
1331
1332 Return Value:
1333     0: transmit successfully
1334     otherwise: transmit fail
1335
1336 Note:
1337 ========================================================================
1338 */
1339 INT rt28xx_send_packets(
1340         IN struct sk_buff               *skb_p,
1341         IN struct net_device    *net_dev)
1342 {
1343     RTMP_ADAPTER *pAd = net_dev->ml_priv;
1344         if (!(net_dev->flags & IFF_UP))
1345         {
1346                 RELEASE_NDIS_PACKET(pAd, (PNDIS_PACKET)skb_p, NDIS_STATUS_FAILURE);
1347                 return 0;
1348         }
1349
1350         NdisZeroMemory((PUCHAR)&skb_p->cb[CB_OFF], 15);
1351         RTMP_SET_PACKET_NET_DEVICE_MBSSID(skb_p, MAIN_MBSSID);
1352
1353         return rt28xx_packet_xmit(skb_p);
1354
1355 } /* End of MBSS_VirtualIF_PacketSend */
1356
1357
1358
1359
1360 void CfgInitHook(PRTMP_ADAPTER pAd)
1361 {
1362         pAd->bBroadComHT = TRUE;
1363 } /* End of CfgInitHook */
1364
1365
1366 #if WIRELESS_EXT >= 12
1367 // This function will be called when query /proc
1368 struct iw_statistics *rt28xx_get_wireless_stats(
1369     IN struct net_device *net_dev)
1370 {
1371         PRTMP_ADAPTER pAd = net_dev->ml_priv;
1372
1373
1374         DBGPRINT(RT_DEBUG_TRACE, ("rt28xx_get_wireless_stats --->\n"));
1375
1376         pAd->iw_stats.status = 0; // Status - device dependent for now
1377
1378         // link quality
1379         pAd->iw_stats.qual.qual = ((pAd->Mlme.ChannelQuality * 12)/10 + 10);
1380         if(pAd->iw_stats.qual.qual > 100)
1381                 pAd->iw_stats.qual.qual = 100;
1382
1383 #ifdef CONFIG_STA_SUPPORT
1384         if (pAd->OpMode == OPMODE_STA)
1385                 pAd->iw_stats.qual.level = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
1386 #endif // CONFIG_STA_SUPPORT //
1387
1388         pAd->iw_stats.qual.noise = pAd->BbpWriteLatch[66]; // noise level (dBm)
1389
1390         pAd->iw_stats.qual.noise += 256 - 143;
1391         pAd->iw_stats.qual.updated = 1;     // Flags to know if updated
1392 #ifdef IW_QUAL_DBM
1393         pAd->iw_stats.qual.updated |= IW_QUAL_DBM;      // Level + Noise are dBm
1394 #endif // IW_QUAL_DBM //
1395
1396         pAd->iw_stats.discard.nwid = 0;     // Rx : Wrong nwid/essid
1397         pAd->iw_stats.miss.beacon = 0;      // Missed beacons/superframe
1398
1399         DBGPRINT(RT_DEBUG_TRACE, ("<--- rt28xx_get_wireless_stats\n"));
1400         return &pAd->iw_stats;
1401 } /* End of rt28xx_get_wireless_stats */
1402 #endif // WIRELESS_EXT //
1403
1404
1405
1406 void tbtt_tasklet(unsigned long data)
1407 {
1408 #define MAX_TX_IN_TBTT          (16)
1409
1410 }
1411
1412 INT rt28xx_ioctl(
1413         IN      struct net_device       *net_dev,
1414         IN      OUT     struct ifreq    *rq,
1415         IN      INT                                     cmd)
1416 {
1417         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1418         RTMP_ADAPTER    *pAd = NULL;
1419         INT                             ret = 0;
1420
1421         if (net_dev->priv_flags == INT_MAIN)
1422         {
1423                 pAd = net_dev->ml_priv;
1424         }
1425         else
1426         {
1427                 pVirtualAd = net_dev->ml_priv;
1428                 pAd = pVirtualAd->RtmpDev->ml_priv;
1429         }
1430
1431         if (pAd == NULL)
1432         {
1433                 /* if 1st open fail, pAd will be free;
1434                    So the net_dev->ml_priv will be NULL in 2rd open */
1435                 return -ENETDOWN;
1436         }
1437
1438
1439 #ifdef CONFIG_STA_SUPPORT
1440         IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1441         {
1442                 ret = rt28xx_sta_ioctl(net_dev, rq, cmd);
1443         }
1444 #endif // CONFIG_STA_SUPPORT //
1445
1446         return ret;
1447 }
1448
1449 /*
1450     ========================================================================
1451
1452     Routine Description:
1453         return ethernet statistics counter
1454
1455     Arguments:
1456         net_dev                     Pointer to net_device
1457
1458     Return Value:
1459         net_device_stats*
1460
1461     Note:
1462
1463     ========================================================================
1464 */
1465 struct net_device_stats *RT28xx_get_ether_stats(
1466     IN  struct net_device *net_dev)
1467 {
1468     RTMP_ADAPTER *pAd = NULL;
1469
1470         if (net_dev)
1471                 pAd = net_dev->ml_priv;
1472
1473         if (pAd)
1474         {
1475
1476                 pAd->stats.rx_packets = pAd->WlanCounters.ReceivedFragmentCount.QuadPart;
1477                 pAd->stats.tx_packets = pAd->WlanCounters.TransmittedFragmentCount.QuadPart;
1478
1479                 pAd->stats.rx_bytes = pAd->RalinkCounters.ReceivedByteCount;
1480                 pAd->stats.tx_bytes = pAd->RalinkCounters.TransmittedByteCount;
1481
1482                 pAd->stats.rx_errors = pAd->Counters8023.RxErrors;
1483                 pAd->stats.tx_errors = pAd->Counters8023.TxErrors;
1484
1485                 pAd->stats.rx_dropped = 0;
1486                 pAd->stats.tx_dropped = 0;
1487
1488             pAd->stats.multicast = pAd->WlanCounters.MulticastReceivedFrameCount.QuadPart;   // multicast packets received
1489             pAd->stats.collisions = pAd->Counters8023.OneCollision + pAd->Counters8023.MoreCollisions;  // Collision packets
1490
1491             pAd->stats.rx_length_errors = 0;
1492             pAd->stats.rx_over_errors = pAd->Counters8023.RxNoBuffer;                   // receiver ring buff overflow
1493             pAd->stats.rx_crc_errors = 0;//pAd->WlanCounters.FCSErrorCount;     // recved pkt with crc error
1494             pAd->stats.rx_frame_errors = pAd->Counters8023.RcvAlignmentErrors;          // recv'd frame alignment error
1495             pAd->stats.rx_fifo_errors = pAd->Counters8023.RxNoBuffer;                   // recv'r fifo overrun
1496             pAd->stats.rx_missed_errors = 0;                                            // receiver missed packet
1497
1498             // detailed tx_errors
1499             pAd->stats.tx_aborted_errors = 0;
1500             pAd->stats.tx_carrier_errors = 0;
1501             pAd->stats.tx_fifo_errors = 0;
1502             pAd->stats.tx_heartbeat_errors = 0;
1503             pAd->stats.tx_window_errors = 0;
1504
1505             // for cslip etc
1506             pAd->stats.rx_compressed = 0;
1507             pAd->stats.tx_compressed = 0;
1508
1509                 return &pAd->stats;
1510         }
1511         else
1512         return NULL;
1513 }
1514