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