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