1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Merged with mainline rtllib.h in Aug 2004.  Original ieee802_11
4  * remains copyright by the original authors
5  *
6  * Portions of the merged code are based on Host AP (software wireless
7  * LAN access point) driver for Intersil Prism2/2.5/3.
8  *
9  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
10  * <jkmaline@cc.hut.fi>
11  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
12  *
13  * Adaption to a generic IEEE 802.11 stack by James Ketrenos
14  * <jketreno@linux.intel.com>
15  * Copyright (c) 2004, Intel Corporation
16  *
17  * Modified for Realtek's wi-fi cards by Andrea Merello
18  * <andrea.merello@gmail.com>
19  */
20 #ifndef RTLLIB_H
21 #define RTLLIB_H
22 #include <linux/if_ether.h> /* ETH_ALEN */
23 #include <linux/kernel.h>   /* ARRAY_SIZE */
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/jiffies.h>
27 #include <linux/timer.h>
28 #include <linux/sched.h>
29 #include <linux/mutex.h>
30 
31 #include <linux/delay.h>
32 #include <linux/wireless.h>
33 
34 #include "rtllib_debug.h"
35 #include "rtl819x_HT.h"
36 #include "rtl819x_BA.h"
37 #include "rtl819x_TS.h"
38 
39 #include <linux/netdevice.h>
40 #include <linux/if_arp.h> /* ARPHRD_ETHER */
41 #include <net/lib80211.h>
42 
43 #define MAX_PRECMD_CNT 16
44 #define MAX_RFDEPENDCMD_CNT 16
45 #define MAX_POSTCMD_CNT 16
46 
47 #ifndef WIRELESS_SPY
48 #define WIRELESS_SPY
49 #endif
50 #include <net/iw_handler.h>
51 
52 #ifndef IW_MODE_MONITOR
53 #define IW_MODE_MONITOR 6
54 #endif
55 
56 #ifndef IWEVCUSTOM
57 #define IWEVCUSTOM 0x8c02
58 #endif
59 
60 #ifndef IW_CUSTOM_MAX
61 /* Max number of char in custom event - use multiple of them if needed */
62 #define IW_CUSTOM_MAX	256	/* In bytes */
63 #endif
64 
65 #define skb_tail_pointer_rsl(skb) skb_tail_pointer(skb)
66 
67 #define queue_delayed_work_rsl(x, y, z) queue_delayed_work(x, y, z)
68 #define INIT_DELAYED_WORK_RSL(x, y, z) INIT_DELAYED_WORK(x, y)
69 
70 #define queue_work_rsl(x, y) queue_work(x, y)
71 #define INIT_WORK_RSL(x, y, z) INIT_WORK(x, y)
72 
73 #define container_of_work_rsl(x, y, z) container_of(x, y, z)
74 #define container_of_dwork_rsl(x, y, z)				\
75 	container_of(to_delayed_work(x), y, z)
76 
77 #define iwe_stream_add_event_rsl(info, start, stop, iwe, len)	\
78 	iwe_stream_add_event(info, start, stop, iwe, len)
79 
80 #define iwe_stream_add_point_rsl(info, start, stop, iwe, p)	\
81 	iwe_stream_add_point(info, start, stop, iwe, p)
82 
83 static inline void *netdev_priv_rsl(struct net_device *dev)
84 {
85 	return netdev_priv(dev);
86 }
87 
88 #define KEY_TYPE_NA		0x0
89 #define KEY_TYPE_WEP40		0x1
90 #define KEY_TYPE_TKIP		0x2
91 #define KEY_TYPE_CCMP		0x4
92 #define KEY_TYPE_WEP104		0x5
93 /* added for rtl819x tx procedure */
94 #define MAX_QUEUE_SIZE		0x10
95 
96 #define BK_QUEUE			       0
97 #define BE_QUEUE			       1
98 #define VI_QUEUE			       2
99 #define VO_QUEUE			       3
100 #define HCCA_QUEUE			     4
101 #define TXCMD_QUEUE			    5
102 #define MGNT_QUEUE			     6
103 #define HIGH_QUEUE			     7
104 #define BEACON_QUEUE			   8
105 
106 #ifndef IW_MODE_MESH
107 #define IW_MODE_MESH			7
108 #endif
109 
110 #define IE_CISCO_FLAG_POSITION		0x08
111 #define SUPPORT_CKIP_MIC			0x08
112 #define SUPPORT_CKIP_PK			0x10
113 #define	RT_RF_OFF_LEVL_HALT_NIC		BIT3
114 #define	RT_IN_PS_LEVEL(pPSC, _PS_FLAG)		\
115 	((pPSC->CurPsLevel & _PS_FLAG) ? true : false)
116 #define	RT_CLEAR_PS_LEVEL(pPSC, _PS_FLAG)	\
117 	(pPSC->CurPsLevel &= (~(_PS_FLAG)))
118 #define	RT_SET_PS_LEVEL(pPSC, _PS_FLAG)	(pPSC->CurPsLevel |= _PS_FLAG)
119 
120 /* defined for skb cb field */
121 /* At most 28 byte */
122 struct cb_desc {
123 	/* Tx Desc Related flags (8-9) */
124 	u8 bLastIniPkt:1;
125 	u8 bCmdOrInit:1;
126 	u8 bFirstSeg:1;
127 	u8 bLastSeg:1;
128 	u8 bEncrypt:1;
129 	u8 bTxDisableRateFallBack:1;
130 	u8 bTxUseDriverAssingedRate:1;
131 	u8 bHwSec:1;
132 
133 	u8 nStuckCount;
134 
135 	/* Tx Firmware Related flags (10-11)*/
136 	u8 bCTSEnable:1;
137 	u8 bRTSEnable:1;
138 	u8 bUseShortGI:1;
139 	u8 bUseShortPreamble:1;
140 	u8 bTxEnableFwCalcDur:1;
141 	u8 bAMPDUEnable:1;
142 	u8 bRTSSTBC:1;
143 	u8 RTSSC:1;
144 
145 	u8 bRTSBW:1;
146 	u8 bPacketBW:1;
147 	u8 bRTSUseShortPreamble:1;
148 	u8 bRTSUseShortGI:1;
149 	u8 bMulticast:1;
150 	u8 bBroadcast:1;
151 	u8 drv_agg_enable:1;
152 	u8 reserved2:1;
153 
154 	/* Tx Desc related element(12-19) */
155 	u8 rata_index;
156 	u8 queue_index;
157 	u16 txbuf_size;
158 	u8 RATRIndex;
159 	u8 bAMSDU:1;
160 	u8 bFromAggrQ:1;
161 	u8 reserved6:6;
162 	u8 macId;
163 	u8 priority;
164 
165 	/* Tx firmware related element(20-27) */
166 	u8 data_rate;
167 	u8 rts_rate;
168 	u8 ampdu_factor;
169 	u8 ampdu_density;
170 	u8 DrvAggrNum;
171 	u8 bdhcp;
172 	u16 pkt_size;
173 	u8 bIsSpecialDataFrame;
174 
175 	u8 bBTTxPacket;
176 	u8 bIsBTProbRsp;
177 };
178 
179 enum sw_chnl_cmd_id {
180 	CmdID_End,
181 	CmdID_SetTxPowerLevel,
182 	CmdID_BBRegWrite10,
183 	CmdID_WritePortUlong,
184 	CmdID_WritePortUshort,
185 	CmdID_WritePortUchar,
186 	CmdID_RF_WriteReg,
187 };
188 
189 struct sw_chnl_cmd {
190 	enum sw_chnl_cmd_id CmdID;
191 	u32			Para1;
192 	u32			Para2;
193 	u32			msDelay;
194 } __packed;
195 
196 /*--------------------------Define -------------------------------------------*/
197 #define MGN_1M		  0x02
198 #define MGN_2M		  0x04
199 #define MGN_5_5M		0x0b
200 #define MGN_11M		 0x16
201 
202 #define MGN_6M		  0x0c
203 #define MGN_9M		  0x12
204 #define MGN_12M		 0x18
205 #define MGN_18M		 0x24
206 #define MGN_24M		 0x30
207 #define MGN_36M		 0x48
208 #define MGN_48M		 0x60
209 #define MGN_54M		 0x6c
210 
211 #define MGN_MCS0		0x80
212 #define MGN_MCS1		0x81
213 #define MGN_MCS2		0x82
214 #define MGN_MCS3		0x83
215 #define MGN_MCS4		0x84
216 #define MGN_MCS5		0x85
217 #define MGN_MCS6		0x86
218 #define MGN_MCS7		0x87
219 #define MGN_MCS8		0x88
220 #define MGN_MCS9		0x89
221 #define MGN_MCS10	       0x8a
222 #define MGN_MCS11	       0x8b
223 #define MGN_MCS12	       0x8c
224 #define MGN_MCS13	       0x8d
225 #define MGN_MCS14	       0x8e
226 #define MGN_MCS15	       0x8f
227 
228 enum hw_variables {
229 	HW_VAR_ETHER_ADDR,
230 	HW_VAR_MULTICAST_REG,
231 	HW_VAR_BASIC_RATE,
232 	HW_VAR_BSSID,
233 	HW_VAR_MEDIA_STATUS,
234 	HW_VAR_SECURITY_CONF,
235 	HW_VAR_BEACON_INTERVAL,
236 	HW_VAR_ATIM_WINDOW,
237 	HW_VAR_LISTEN_INTERVAL,
238 	HW_VAR_CS_COUNTER,
239 	HW_VAR_DEFAULTKEY0,
240 	HW_VAR_DEFAULTKEY1,
241 	HW_VAR_DEFAULTKEY2,
242 	HW_VAR_DEFAULTKEY3,
243 	HW_VAR_SIFS,
244 	HW_VAR_DIFS,
245 	HW_VAR_EIFS,
246 	HW_VAR_SLOT_TIME,
247 	HW_VAR_ACK_PREAMBLE,
248 	HW_VAR_CW_CONFIG,
249 	HW_VAR_CW_VALUES,
250 	HW_VAR_RATE_FALLBACK_CONTROL,
251 	HW_VAR_CONTENTION_WINDOW,
252 	HW_VAR_RETRY_COUNT,
253 	HW_VAR_TR_SWITCH,
254 	HW_VAR_COMMAND,
255 	HW_VAR_WPA_CONFIG,
256 	HW_VAR_AMPDU_MIN_SPACE,
257 	HW_VAR_SHORTGI_DENSITY,
258 	HW_VAR_AMPDU_FACTOR,
259 	HW_VAR_MCS_RATE_AVAILABLE,
260 	HW_VAR_AC_PARAM,
261 	HW_VAR_ACM_CTRL,
262 	HW_VAR_DIS_Req_Qsize,
263 	HW_VAR_CCX_CHNL_LOAD,
264 	HW_VAR_CCX_NOISE_HISTOGRAM,
265 	HW_VAR_CCX_CLM_NHM,
266 	HW_VAR_TxOPLimit,
267 	HW_VAR_TURBO_MODE,
268 	HW_VAR_RF_STATE,
269 	HW_VAR_RF_OFF_BY_HW,
270 	HW_VAR_BUS_SPEED,
271 	HW_VAR_SET_DEV_POWER,
272 
273 	HW_VAR_RCR,
274 	HW_VAR_RATR_0,
275 	HW_VAR_RRSR,
276 	HW_VAR_CPU_RST,
277 	HW_VAR_CECHK_BSSID,
278 	HW_VAR_LBK_MODE,
279 	HW_VAR_AES_11N_FIX,
280 	HW_VAR_USB_RX_AGGR,
281 	HW_VAR_USER_CONTROL_TURBO_MODE,
282 	HW_VAR_RETRY_LIMIT,
283 	HW_VAR_INIT_TX_RATE,
284 	HW_VAR_TX_RATE_REG,
285 	HW_VAR_EFUSE_USAGE,
286 	HW_VAR_EFUSE_BYTES,
287 	HW_VAR_AUTOLOAD_STATUS,
288 	HW_VAR_RF_2R_DISABLE,
289 	HW_VAR_SET_RPWM,
290 	HW_VAR_H2C_FW_PWRMODE,
291 	HW_VAR_H2C_FW_JOINBSSRPT,
292 	HW_VAR_1X1_RECV_COMBINE,
293 	HW_VAR_STOP_SEND_BEACON,
294 	HW_VAR_TSF_TIMER,
295 	HW_VAR_IO_CMD,
296 
297 	HW_VAR_RF_RECOVERY,
298 	HW_VAR_H2C_FW_UPDATE_GTK,
299 	HW_VAR_WF_MASK,
300 	HW_VAR_WF_CRC,
301 	HW_VAR_WF_IS_MAC_ADDR,
302 	HW_VAR_H2C_FW_OFFLOAD,
303 	HW_VAR_RESET_WFCRC,
304 
305 	HW_VAR_HANDLE_FW_C2H,
306 	HW_VAR_DL_FW_RSVD_PAGE,
307 	HW_VAR_AID,
308 	HW_VAR_HW_SEQ_ENABLE,
309 	HW_VAR_CORRECT_TSF,
310 	HW_VAR_BCN_VALID,
311 	HW_VAR_FWLPS_RF_ON,
312 	HW_VAR_DUAL_TSF_RST,
313 	HW_VAR_SWITCH_EPHY_WoWLAN,
314 	HW_VAR_INT_MIGRATION,
315 	HW_VAR_INT_AC,
316 	HW_VAR_RF_TIMING,
317 };
318 
319 enum rt_op_mode {
320 	RT_OP_MODE_AP,
321 	RT_OP_MODE_INFRASTRUCTURE,
322 	RT_OP_MODE_IBSS,
323 	RT_OP_MODE_NO_LINK,
324 };
325 
326 
327 #define aSifsTime						\
328 	 (((priv->rtllib->current_network.mode == IEEE_A)	\
329 	|| (priv->rtllib->current_network.mode == IEEE_N_24G)	\
330 	|| (priv->rtllib->current_network.mode == IEEE_N_5G)) ? 16 : 10)
331 
332 #define MGMT_QUEUE_NUM 5
333 
334 #define MAX_IE_LEN  0xff
335 
336 #define msleep_interruptible_rsl  msleep_interruptible
337 
338 /* Maximum size for the MA-UNITDATA primitive, 802.11 standard section
339  * 6.2.1.1.2.
340  *
341  * The figure in section 7.1.2 suggests a body size of up to 2312
342  * bytes is allowed, which is a bit confusing, I suspect this
343  * represents the 2304 bytes of real data, plus a possible 8 bytes of
344  * WEP IV and ICV. (this interpretation suggested by Ramiro Barreiro)
345  */
346 #define RTLLIB_1ADDR_LEN 10
347 #define RTLLIB_2ADDR_LEN 16
348 #define RTLLIB_3ADDR_LEN 24
349 #define RTLLIB_4ADDR_LEN 30
350 #define RTLLIB_FCS_LEN    4
351 
352 #define RTLLIB_SKBBUFFER_SIZE 2500
353 
354 #define MIN_FRAG_THRESHOLD     256U
355 #define MAX_FRAG_THRESHOLD     2346U
356 
357 /* Frame control field constants */
358 #define RTLLIB_FCTL_FTYPE		0x000c
359 #define RTLLIB_FCTL_STYPE		0x00f0
360 #define RTLLIB_FCTL_FRAMETYPE	0x00fc
361 #define RTLLIB_FCTL_TODS		0x0100
362 #define RTLLIB_FCTL_FROMDS		0x0200
363 #define RTLLIB_FCTL_DSTODS		0x0300
364 #define RTLLIB_FCTL_MOREFRAGS	0x0400
365 #define RTLLIB_FCTL_RETRY		0x0800
366 #define RTLLIB_FCTL_PM		0x1000
367 #define RTLLIB_FCTL_MOREDATA		0x2000
368 #define RTLLIB_FCTL_WEP		0x4000
369 #define RTLLIB_FCTL_ORDER		0x8000
370 
371 #define RTLLIB_FTYPE_MGMT		0x0000
372 #define RTLLIB_FTYPE_CTL		0x0004
373 #define RTLLIB_FTYPE_DATA		0x0008
374 
375 /* management */
376 #define RTLLIB_STYPE_ASSOC_REQ	0x0000
377 #define RTLLIB_STYPE_ASSOC_RESP		0x0010
378 #define RTLLIB_STYPE_REASSOC_REQ	0x0020
379 #define RTLLIB_STYPE_REASSOC_RESP	0x0030
380 #define RTLLIB_STYPE_PROBE_REQ	0x0040
381 #define RTLLIB_STYPE_PROBE_RESP	0x0050
382 #define RTLLIB_STYPE_BEACON		0x0080
383 #define RTLLIB_STYPE_ATIM		0x0090
384 #define RTLLIB_STYPE_DISASSOC	0x00A0
385 #define RTLLIB_STYPE_AUTH		0x00B0
386 #define RTLLIB_STYPE_DEAUTH		0x00C0
387 #define RTLLIB_STYPE_MANAGE_ACT	0x00D0
388 
389 /* control */
390 #define RTLLIB_STYPE_PSPOLL		0x00A0
391 #define RTLLIB_STYPE_RTS		0x00B0
392 #define RTLLIB_STYPE_CTS		0x00C0
393 #define RTLLIB_STYPE_ACK		0x00D0
394 
395 /* data */
396 #define RTLLIB_STYPE_DATA		0x0000
397 #define RTLLIB_STYPE_DATA_CFACK	0x0010
398 #define RTLLIB_STYPE_DATA_CFPOLL	0x0020
399 #define RTLLIB_STYPE_DATA_CFACKPOLL	0x0030
400 #define RTLLIB_STYPE_NULLFUNC	0x0040
401 #define RTLLIB_STYPE_QOS_DATA	0x0080
402 #define RTLLIB_STYPE_QOS_NULL	0x00C0
403 
404 #define RTLLIB_SCTL_FRAG		0x000F
405 #define RTLLIB_SCTL_SEQ		0xFFF0
406 
407 /* QOS control */
408 #define RTLLIB_QCTL_TID	      0x000F
409 
410 #define	FC_QOS_BIT					BIT7
411 #define IsDataFrame(pdu)	(((pdu[0] & 0x0C) == 0x08) ? true : false)
412 #define	IsLegacyDataFrame(pdu)	(IsDataFrame(pdu) && (!(pdu[0]&FC_QOS_BIT)))
413 #define IsQoSDataFrame(pframe)			\
414 	((*(u16 *)pframe&(RTLLIB_STYPE_QOS_DATA|RTLLIB_FTYPE_DATA)) ==	\
415 	(RTLLIB_STYPE_QOS_DATA|RTLLIB_FTYPE_DATA))
416 #define Frame_Order(pframe)     (*(u16 *)pframe&RTLLIB_FCTL_ORDER)
417 #define SN_LESS(a, b)		(((a-b)&0x800) != 0)
418 #define SN_EQUAL(a, b)	(a == b)
419 #define MAX_DEV_ADDR_SIZE 8
420 
421 enum act_category {
422 	ACT_CAT_QOS = 1,
423 	ACT_CAT_DLS = 2,
424 	ACT_CAT_BA  = 3,
425 	ACT_CAT_HT  = 7,
426 	ACT_CAT_WMM = 17,
427 };
428 
429 enum ba_action {
430 	ACT_ADDBAREQ = 0,
431 	ACT_ADDBARSP = 1,
432 	ACT_DELBA    = 2,
433 };
434 
435 enum init_gain_op_type {
436 	IG_Backup = 0,
437 	IG_Restore,
438 	IG_Max
439 };
440 
441 enum led_ctl_mode {
442 	LED_CTL_POWER_ON = 1,
443 	LED_CTL_LINK = 2,
444 	LED_CTL_NO_LINK = 3,
445 	LED_CTL_TX = 4,
446 	LED_CTL_RX = 5,
447 	LED_CTL_SITE_SURVEY = 6,
448 	LED_CTL_POWER_OFF = 7,
449 	LED_CTL_START_TO_LINK = 8,
450 };
451 
452 enum rt_rf_type_def {
453 	RF_1T2R = 0,
454 	RF_2T4R,
455 };
456 
457 enum wireless_mode {
458 	WIRELESS_MODE_UNKNOWN = 0x00,
459 	WIRELESS_MODE_A = 0x01,
460 	WIRELESS_MODE_B = 0x02,
461 	WIRELESS_MODE_G = 0x04,
462 	WIRELESS_MODE_AUTO = 0x08,
463 	WIRELESS_MODE_N_24G = 0x10,
464 	WIRELESS_MODE_N_5G = 0x20
465 };
466 
467 #ifndef ETH_P_PAE
468 #define ETH_P_PAE	0x888E		/* Port Access Entity (IEEE 802.1X) */
469 #define ETH_P_IP	0x0800		/* Internet Protocol packet	*/
470 #define ETH_P_ARP	0x0806		/* Address Resolution packet	*/
471 #endif /* ETH_P_PAE */
472 
473 #ifndef ETH_P_80211_RAW
474 #define ETH_P_80211_RAW (ETH_P_ECONET + 1)
475 #endif
476 
477 /* IEEE 802.11 defines */
478 
479 #define P80211_OUI_LEN 3
480 
481 struct rtllib_snap_hdr {
482 
483 	u8    dsap;   /* always 0xAA */
484 	u8    ssap;   /* always 0xAA */
485 	u8    ctrl;   /* always 0x03 */
486 	u8    oui[P80211_OUI_LEN];    /* organizational universal id */
487 
488 } __packed;
489 
490 enum _REG_PREAMBLE_MODE {
491 	PREAMBLE_LONG = 1,
492 	PREAMBLE_AUTO = 2,
493 	PREAMBLE_SHORT = 3,
494 };
495 
496 #define SNAP_SIZE sizeof(struct rtllib_snap_hdr)
497 
498 #define WLAN_FC_GET_TYPE(fc) ((fc) & RTLLIB_FCTL_FTYPE)
499 #define WLAN_FC_GET_STYPE(fc) ((fc) & RTLLIB_FCTL_STYPE)
500 #define WLAN_FC_MORE_DATA(fc) ((fc) & RTLLIB_FCTL_MOREDATA)
501 
502 #define WLAN_FC_GET_FRAMETYPE(fc) ((fc) & RTLLIB_FCTL_FRAMETYPE)
503 #define WLAN_GET_SEQ_FRAG(seq) ((seq) & RTLLIB_SCTL_FRAG)
504 #define WLAN_GET_SEQ_SEQ(seq)  (((seq) & RTLLIB_SCTL_SEQ) >> 4)
505 
506 /* Authentication algorithms */
507 #define WLAN_AUTH_OPEN 0
508 #define WLAN_AUTH_SHARED_KEY 1
509 #define WLAN_AUTH_LEAP 128
510 
511 #define WLAN_CAPABILITY_ESS (1<<0)
512 #define WLAN_CAPABILITY_IBSS (1<<1)
513 #define WLAN_CAPABILITY_PRIVACY (1<<4)
514 #define WLAN_CAPABILITY_SHORT_PREAMBLE (1<<5)
515 #define WLAN_CAPABILITY_SHORT_SLOT_TIME (1<<10)
516 
517 #define RTLLIB_STATMASK_SIGNAL (1<<0)
518 #define RTLLIB_STATMASK_RSSI (1<<1)
519 #define RTLLIB_STATMASK_NOISE (1<<2)
520 #define RTLLIB_STATMASK_WEMASK 0x7
521 
522 #define RTLLIB_CCK_MODULATION    (1<<0)
523 #define RTLLIB_OFDM_MODULATION   (1<<1)
524 
525 #define RTLLIB_24GHZ_BAND     (1<<0)
526 #define RTLLIB_52GHZ_BAND     (1<<1)
527 
528 #define RTLLIB_CCK_RATE_LEN		4
529 #define RTLLIB_CCK_RATE_1MB			0x02
530 #define RTLLIB_CCK_RATE_2MB			0x04
531 #define RTLLIB_CCK_RATE_5MB			0x0B
532 #define RTLLIB_CCK_RATE_11MB			0x16
533 #define RTLLIB_OFDM_RATE_LEN		8
534 #define RTLLIB_OFDM_RATE_6MB			0x0C
535 #define RTLLIB_OFDM_RATE_9MB			0x12
536 #define RTLLIB_OFDM_RATE_12MB		0x18
537 #define RTLLIB_OFDM_RATE_18MB		0x24
538 #define RTLLIB_OFDM_RATE_24MB		0x30
539 #define RTLLIB_OFDM_RATE_36MB		0x48
540 #define RTLLIB_OFDM_RATE_48MB		0x60
541 #define RTLLIB_OFDM_RATE_54MB		0x6C
542 #define RTLLIB_BASIC_RATE_MASK		0x80
543 
544 /* this is stolen and modified from the madwifi driver*/
545 #define RTLLIB_FC0_TYPE_MASK		0x0c
546 #define RTLLIB_FC0_TYPE_DATA		0x08
547 #define RTLLIB_FC0_SUBTYPE_MASK	0xB0
548 #define RTLLIB_FC0_SUBTYPE_QOS	0x80
549 
550 #define RTLLIB_QOS_HAS_SEQ(fc) \
551 	(((fc) & (RTLLIB_FC0_TYPE_MASK | RTLLIB_FC0_SUBTYPE_MASK)) == \
552 	 (RTLLIB_FC0_TYPE_DATA | RTLLIB_FC0_SUBTYPE_QOS))
553 
554 /* this is stolen from ipw2200 driver */
555 #define IEEE_IBSS_MAC_HASH_SIZE 31
556 struct ieee_ibss_seq {
557 	u8 mac[ETH_ALEN];
558 	u16 seq_num[17];
559 	u16 frag_num[17];
560 	unsigned long packet_time[17];
561 	struct list_head list;
562 };
563 
564 /* NOTE: This data is for statistical purposes; not all hardware provides this
565  *       information for frames received.  Not setting these will not cause
566  *       any adverse affects.
567  */
568 struct rtllib_rx_stats {
569 	u64 mac_time;
570 	s8  rssi;
571 	u8  signal;
572 	u8  noise;
573 	u16 rate; /* in 100 kbps */
574 	u8  received_channel;
575 	u8  control;
576 	u8  mask;
577 	u8  freq;
578 	u16 len;
579 	u64 tsf;
580 	u32 beacon_time;
581 	u8  nic_type;
582 	u16 Length;
583 	u8  SignalQuality;
584 	s32 RecvSignalPower;
585 	s8  RxPower;
586 	u8  SignalStrength;
587 	u16 bHwError:1;
588 	u16 bCRC:1;
589 	u16 bICV:1;
590 	u16 bShortPreamble:1;
591 	u16 Antenna:1;
592 	u16 Decrypted:1;
593 	u16 Wakeup:1;
594 	u16 Reserved0:1;
595 	u8  AGC;
596 	u32 TimeStampLow;
597 	u32 TimeStampHigh;
598 	bool bShift;
599 	bool bIsQosData;
600 
601 	u8    RxDrvInfoSize;
602 	u8    RxBufShift;
603 	bool  bIsAMPDU;
604 	bool  bFirstMPDU;
605 	bool  bContainHTC;
606 	bool  RxIs40MHzPacket;
607 	u32   RxPWDBAll;
608 	u8    RxMIMOSignalStrength[4];
609 	s8    RxMIMOSignalQuality[2];
610 	bool  bPacketMatchBSSID;
611 	bool  bIsCCK;
612 	bool  bPacketToSelf;
613 	u16    packetlength;
614 	u16    fraglength;
615 	u16    fragoffset;
616 	u16    ntotalfrag;
617 	bool   bPacketBeacon;
618 	bool   bToSelfBA;
619 	u16    Seq_Num;
620 };
621 
622 /* IEEE 802.11 requires that STA supports concurrent reception of at least
623  * three fragmented frames. This define can be increased to support more
624  * concurrent frames, but it should be noted that each entry can consume about
625  * 2 kB of RAM and increasing cache size will slow down frame reassembly.
626  */
627 #define RTLLIB_FRAG_CACHE_LEN 4
628 
629 struct rtllib_frag_entry {
630 	unsigned long first_frag_time;
631 	unsigned int seq;
632 	unsigned int last_frag;
633 	struct sk_buff *skb;
634 	u8 src_addr[ETH_ALEN];
635 	u8 dst_addr[ETH_ALEN];
636 };
637 
638 struct rtllib_device;
639 
640 #define SEC_ACTIVE_KEY    (1<<4)
641 #define SEC_AUTH_MODE     (1<<5)
642 #define SEC_UNICAST_GROUP (1<<6)
643 #define SEC_LEVEL	 (1<<7)
644 #define SEC_ENABLED       (1<<8)
645 
646 #define SEC_LEVEL_0      0 /* None */
647 #define SEC_LEVEL_1      1 /* WEP 40 and 104 bit */
648 #define SEC_LEVEL_2      2 /* Level 1 + TKIP */
649 #define SEC_LEVEL_2_CKIP 3 /* Level 1 + CKIP */
650 #define SEC_LEVEL_3      4 /* Level 2 + CCMP */
651 
652 #define SEC_ALG_NONE		0
653 #define SEC_ALG_WEP		1
654 #define SEC_ALG_TKIP		2
655 #define SEC_ALG_CCMP		4
656 
657 #define WEP_KEY_LEN		13
658 #define SCM_KEY_LEN		32
659 
660 struct rtllib_security {
661 	u16 active_key:2,
662 	    enabled:1,
663 	    auth_mode:2,
664 	    auth_algo:4,
665 	    unicast_uses_group:1,
666 	    encrypt:1;
667 	u8 key_sizes[NUM_WEP_KEYS];
668 	u8 keys[NUM_WEP_KEYS][SCM_KEY_LEN];
669 	u8 level;
670 	u16 flags;
671 } __packed;
672 
673 
674 /* 802.11 data frame from AP
675  *       ,-------------------------------------------------------------------.
676  * Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
677  *       |------|------|---------|---------|---------|------|---------|------|
678  * Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  frame  |  fcs |
679  *       |      | tion | (BSSID) |         |         | ence |  data   |      |
680  *       `-------------------------------------------------------------------'
681  * Total: 28-2340 bytes
682  */
683 
684 /* Management Frame Information Element Types */
685 enum rtllib_mfie {
686 	MFIE_TYPE_SSID = 0,
687 	MFIE_TYPE_RATES = 1,
688 	MFIE_TYPE_FH_SET = 2,
689 	MFIE_TYPE_DS_SET = 3,
690 	MFIE_TYPE_CF_SET = 4,
691 	MFIE_TYPE_TIM = 5,
692 	MFIE_TYPE_IBSS_SET = 6,
693 	MFIE_TYPE_COUNTRY = 7,
694 	MFIE_TYPE_HOP_PARAMS = 8,
695 	MFIE_TYPE_HOP_TABLE = 9,
696 	MFIE_TYPE_REQUEST = 10,
697 	MFIE_TYPE_CHALLENGE = 16,
698 	MFIE_TYPE_POWER_CONSTRAINT = 32,
699 	MFIE_TYPE_POWER_CAPABILITY = 33,
700 	MFIE_TYPE_TPC_REQUEST = 34,
701 	MFIE_TYPE_TPC_REPORT = 35,
702 	MFIE_TYPE_SUPP_CHANNELS = 36,
703 	MFIE_TYPE_CSA = 37,
704 	MFIE_TYPE_MEASURE_REQUEST = 38,
705 	MFIE_TYPE_MEASURE_REPORT = 39,
706 	MFIE_TYPE_QUIET = 40,
707 	MFIE_TYPE_IBSS_DFS = 41,
708 	MFIE_TYPE_ERP = 42,
709 	MFIE_TYPE_HT_CAP = 45,
710 	MFIE_TYPE_RSN = 48,
711 	MFIE_TYPE_RATES_EX = 50,
712 	MFIE_TYPE_HT_INFO = 61,
713 	MFIE_TYPE_AIRONET = 133,
714 	MFIE_TYPE_GENERIC = 221,
715 	MFIE_TYPE_QOS_PARAMETER = 222,
716 };
717 
718 /* Minimal header; can be used for passing 802.11 frames with sufficient
719  * information to determine what type of underlying data type is actually
720  * stored in the data.
721  */
722 struct rtllib_pspoll_hdr {
723 	__le16 frame_ctl;
724 	__le16 aid;
725 	u8 bssid[ETH_ALEN];
726 	u8 ta[ETH_ALEN];
727 } __packed;
728 
729 struct rtllib_hdr {
730 	__le16 frame_ctl;
731 	__le16 duration_id;
732 	u8 payload[0];
733 } __packed;
734 
735 struct rtllib_hdr_1addr {
736 	__le16 frame_ctl;
737 	__le16 duration_id;
738 	u8 addr1[ETH_ALEN];
739 	u8 payload[0];
740 } __packed;
741 
742 struct rtllib_hdr_2addr {
743 	__le16 frame_ctl;
744 	__le16 duration_id;
745 	u8 addr1[ETH_ALEN];
746 	u8 addr2[ETH_ALEN];
747 	u8 payload[0];
748 } __packed;
749 
750 struct rtllib_hdr_3addr {
751 	__le16 frame_ctl;
752 	__le16 duration_id;
753 	u8 addr1[ETH_ALEN];
754 	u8 addr2[ETH_ALEN];
755 	u8 addr3[ETH_ALEN];
756 	__le16 seq_ctl;
757 	u8 payload[0];
758 } __packed;
759 
760 struct rtllib_hdr_4addr {
761 	__le16 frame_ctl;
762 	__le16 duration_id;
763 	u8 addr1[ETH_ALEN];
764 	u8 addr2[ETH_ALEN];
765 	u8 addr3[ETH_ALEN];
766 	__le16 seq_ctl;
767 	u8 addr4[ETH_ALEN];
768 	u8 payload[0];
769 } __packed;
770 
771 struct rtllib_hdr_3addrqos {
772 	__le16 frame_ctl;
773 	__le16 duration_id;
774 	u8 addr1[ETH_ALEN];
775 	u8 addr2[ETH_ALEN];
776 	u8 addr3[ETH_ALEN];
777 	__le16 seq_ctl;
778 	__le16 qos_ctl;
779 	u8 payload[0];
780 } __packed;
781 
782 struct rtllib_hdr_4addrqos {
783 	__le16 frame_ctl;
784 	__le16 duration_id;
785 	u8 addr1[ETH_ALEN];
786 	u8 addr2[ETH_ALEN];
787 	u8 addr3[ETH_ALEN];
788 	__le16 seq_ctl;
789 	u8 addr4[ETH_ALEN];
790 	__le16 qos_ctl;
791 	u8 payload[0];
792 } __packed;
793 
794 struct rtllib_info_element {
795 	u8 id;
796 	u8 len;
797 	u8 data[0];
798 } __packed;
799 
800 struct rtllib_authentication {
801 	struct rtllib_hdr_3addr header;
802 	__le16 algorithm;
803 	__le16 transaction;
804 	__le16 status;
805 	/*challenge*/
806 	struct rtllib_info_element info_element[0];
807 } __packed;
808 
809 struct rtllib_disauth {
810 	struct rtllib_hdr_3addr header;
811 	__le16 reason;
812 } __packed;
813 
814 struct rtllib_disassoc {
815 	struct rtllib_hdr_3addr header;
816 	__le16 reason;
817 } __packed;
818 
819 struct rtllib_probe_request {
820 	struct rtllib_hdr_3addr header;
821 	/* SSID, supported rates */
822 	struct rtllib_info_element info_element[0];
823 } __packed;
824 
825 struct rtllib_probe_response {
826 	struct rtllib_hdr_3addr header;
827 	u32 time_stamp[2];
828 	__le16 beacon_interval;
829 	__le16 capability;
830 	/* SSID, supported rates, FH params, DS params,
831 	 * CF params, IBSS params, TIM (if beacon), RSN
832 	 */
833 	struct rtllib_info_element info_element[0];
834 } __packed;
835 
836 /* Alias beacon for probe_response */
837 #define rtllib_beacon rtllib_probe_response
838 
839 struct rtllib_assoc_request_frame {
840 	struct rtllib_hdr_3addr header;
841 	__le16 capability;
842 	__le16 listen_interval;
843 	/* SSID, supported rates, RSN */
844 	struct rtllib_info_element info_element[0];
845 } __packed;
846 
847 struct rtllib_assoc_response_frame {
848 	struct rtllib_hdr_3addr header;
849 	__le16 capability;
850 	__le16 status;
851 	__le16 aid;
852 	struct rtllib_info_element info_element[0]; /* supported rates */
853 } __packed;
854 
855 struct rtllib_txb {
856 	u8 nr_frags;
857 	u8 encrypted;
858 	u8 queue_index;
859 	u8 rts_included;
860 	u16 reserved;
861 	__le16 frag_size;
862 	__le16 payload_size;
863 	struct sk_buff *fragments[0];
864 };
865 
866 #define MAX_SUBFRAME_COUNT		  64
867 struct rtllib_rxb {
868 	u8 nr_subframes;
869 	struct sk_buff *subframes[MAX_SUBFRAME_COUNT];
870 	u8 dst[ETH_ALEN];
871 	u8 src[ETH_ALEN];
872 } __packed;
873 
874 union frameqos {
875 	u16 shortdata;
876 	u8  chardata[2];
877 	struct {
878 		u16 tid:4;
879 		u16 eosp:1;
880 		u16 ack_policy:2;
881 		u16 reserved:1;
882 		u16 txop:8;
883 	} field;
884 };
885 
886 /* MAX_RATES_LENGTH needs to be 12.  The spec says 8, and many APs
887  * only use 8, and then use extended rates for the remaining supported
888  * rates.  Other APs, however, stick all of their supported rates on the
889  * main rates information element...
890  */
891 #define MAX_RATES_LENGTH		  ((u8)12)
892 #define MAX_RATES_EX_LENGTH	       ((u8)16)
893 #define MAX_NETWORK_COUNT		  96
894 
895 #define MAX_CHANNEL_NUMBER		 161
896 #define RTLLIB_SOFTMAC_SCAN_TIME	   100
897 #define RTLLIB_SOFTMAC_ASSOC_RETRY_TIME (HZ * 2)
898 
899 #define MAX_WPA_IE_LEN 64
900 #define MAX_WZC_IE_LEN 256
901 
902 #define NETWORK_EMPTY_ESSID (1<<0)
903 #define NETWORK_HAS_OFDM    (1<<1)
904 #define NETWORK_HAS_CCK     (1<<2)
905 
906 /* QoS structure */
907 #define NETWORK_HAS_QOS_PARAMETERS      (1<<3)
908 #define NETWORK_HAS_QOS_INFORMATION     (1<<4)
909 #define NETWORK_HAS_QOS_MASK	    (NETWORK_HAS_QOS_PARAMETERS | \
910 					 NETWORK_HAS_QOS_INFORMATION)
911 /* 802.11h */
912 #define NETWORK_HAS_ERP_VALUE	   (1<<10)
913 
914 #define QOS_QUEUE_NUM		   4
915 #define QOS_OUI_LEN		     3
916 #define QOS_OUI_TYPE		    2
917 #define QOS_ELEMENT_ID		  221
918 #define QOS_OUI_INFO_SUB_TYPE	   0
919 #define QOS_OUI_PARAM_SUB_TYPE	  1
920 #define QOS_VERSION_1		   1
921 
922 struct rtllib_qos_information_element {
923 	u8 elementID;
924 	u8 length;
925 	u8 qui[QOS_OUI_LEN];
926 	u8 qui_type;
927 	u8 qui_subtype;
928 	u8 version;
929 	u8 ac_info;
930 } __packed;
931 
932 struct rtllib_qos_ac_parameter {
933 	u8 aci_aifsn;
934 	u8 ecw_min_max;
935 	__le16 tx_op_limit;
936 } __packed;
937 
938 struct rtllib_qos_parameter_info {
939 	struct rtllib_qos_information_element info_element;
940 	u8 reserved;
941 	struct rtllib_qos_ac_parameter ac_params_record[QOS_QUEUE_NUM];
942 } __packed;
943 
944 struct rtllib_qos_parameters {
945 	__le16 cw_min[QOS_QUEUE_NUM];
946 	__le16 cw_max[QOS_QUEUE_NUM];
947 	u8 aifs[QOS_QUEUE_NUM];
948 	u8 flag[QOS_QUEUE_NUM];
949 	__le16 tx_op_limit[QOS_QUEUE_NUM];
950 } __packed;
951 
952 struct rtllib_qos_data {
953 	struct rtllib_qos_parameters parameters;
954 	unsigned int wmm_acm;
955 	int active;
956 	int supported;
957 	u8 param_count;
958 	u8 old_param_count;
959 };
960 
961 struct rtllib_tim_parameters {
962 	u8 tim_count;
963 	u8 tim_period;
964 } __packed;
965 
966 struct rtllib_wmm_ac_param {
967 	u8 ac_aci_acm_aifsn;
968 	u8 ac_ecwmin_ecwmax;
969 	u16 ac_txop_limit;
970 };
971 
972 enum eap_type {
973 	EAP_PACKET = 0,
974 	EAPOL_START,
975 	EAPOL_LOGOFF,
976 	EAPOL_KEY,
977 	EAPOL_ENCAP_ASF_ALERT
978 };
979 
980 static const char * const eap_types[] = {
981 	[EAP_PACKET]		= "EAP-Packet",
982 	[EAPOL_START]		= "EAPOL-Start",
983 	[EAPOL_LOGOFF]		= "EAPOL-Logoff",
984 	[EAPOL_KEY]		= "EAPOL-Key",
985 	[EAPOL_ENCAP_ASF_ALERT]	= "EAPOL-Encap-ASF-Alert"
986 };
987 
988 static inline const char *eap_get_type(int type)
989 {
990 	return ((u32)type >= ARRAY_SIZE(eap_types)) ? "Unknown" :
991 		 eap_types[type];
992 }
993 static inline u8 Frame_QoSTID(u8 *buf)
994 {
995 	struct rtllib_hdr_3addr *hdr;
996 	u16 fc;
997 
998 	hdr = (struct rtllib_hdr_3addr *)buf;
999 	fc = le16_to_cpu(hdr->frame_ctl);
1000 	return (u8)((union frameqos *)(buf + (((fc & RTLLIB_FCTL_TODS) &&
1001 		    (fc & RTLLIB_FCTL_FROMDS)) ? 30 : 24)))->field.tid;
1002 }
1003 
1004 
1005 struct eapol {
1006 	u8 snap[6];
1007 	u16 ethertype;
1008 	u8 version;
1009 	u8 type;
1010 	u16 length;
1011 } __packed;
1012 
1013 struct rtllib_softmac_stats {
1014 	unsigned int rx_ass_ok;
1015 	unsigned int rx_ass_err;
1016 	unsigned int rx_probe_rq;
1017 	unsigned int tx_probe_rs;
1018 	unsigned int tx_beacons;
1019 	unsigned int rx_auth_rq;
1020 	unsigned int rx_auth_rs_ok;
1021 	unsigned int rx_auth_rs_err;
1022 	unsigned int tx_auth_rq;
1023 	unsigned int no_auth_rs;
1024 	unsigned int no_ass_rs;
1025 	unsigned int tx_ass_rq;
1026 	unsigned int rx_ass_rq;
1027 	unsigned int tx_probe_rq;
1028 	unsigned int reassoc;
1029 	unsigned int swtxstop;
1030 	unsigned int swtxawake;
1031 	unsigned char CurrentShowTxate;
1032 	unsigned char last_packet_rate;
1033 	unsigned int txretrycount;
1034 };
1035 
1036 /* These are the data types that can make up management packets
1037  *
1038  * u16 auth_algorithm;
1039  * u16 auth_sequence;
1040  * u16 beacon_interval;
1041  * u16 capability;
1042  * u8 current_ap[ETH_ALEN];
1043  * u16 listen_interval;
1044  * struct {
1045  *   u16 association_id:14, reserved:2;
1046  * } __packed;
1047  * u32 time_stamp[2];
1048  * u16 reason;
1049  * u16 status;
1050  */
1051 
1052 #define RTLLIB_DEFAULT_TX_ESSID "Penguin"
1053 #define RTLLIB_DEFAULT_BASIC_RATE 2
1054 
1055 enum {WMM_all_frame, WMM_two_frame, WMM_four_frame, WMM_six_frame};
1056 #define MAX_SP_Len  (WMM_all_frame << 4)
1057 #define RTLLIB_QOS_TID 0x0f
1058 #define QOS_CTL_NOTCONTAIN_ACK (0x01 << 5)
1059 
1060 #define RTLLIB_DTIM_MBCAST 4
1061 #define RTLLIB_DTIM_UCAST 2
1062 #define RTLLIB_DTIM_VALID 1
1063 #define RTLLIB_DTIM_INVALID 0
1064 
1065 #define RTLLIB_PS_DISABLED 0
1066 #define RTLLIB_PS_UNICAST RTLLIB_DTIM_UCAST
1067 #define RTLLIB_PS_MBCAST RTLLIB_DTIM_MBCAST
1068 
1069 #define WME_AC_BK   0x00
1070 #define WME_AC_BE   0x01
1071 #define WME_AC_VI   0x02
1072 #define WME_AC_VO   0x03
1073 #define WME_AC_PRAM_LEN 16
1074 
1075 #define MAX_RECEIVE_BUFFER_SIZE 9100
1076 
1077 #define UP2AC(up) (		   \
1078 	((up) < 1) ? WME_AC_BE : \
1079 	((up) < 3) ? WME_AC_BK : \
1080 	((up) < 4) ? WME_AC_BE : \
1081 	((up) < 6) ? WME_AC_VI : \
1082 	WME_AC_VO)
1083 
1084 #define	ETHER_ADDR_LEN		6	/* length of an Ethernet address */
1085 #define ETHERNET_HEADER_SIZE    14      /* length of two Ethernet address
1086 					 * plus ether type
1087 					 */
1088 
1089 enum erp_t {
1090 	ERP_NonERPpresent	= 0x01,
1091 	ERP_UseProtection	= 0x02,
1092 	ERP_BarkerPreambleMode = 0x04,
1093 };
1094 
1095 struct rtllib_network {
1096 	/* These entries are used to identify a unique network */
1097 	u8 bssid[ETH_ALEN];
1098 	u8 channel;
1099 	/* Ensure null-terminated for any debug msgs */
1100 	u8 ssid[IW_ESSID_MAX_SIZE + 1];
1101 	u8 ssid_len;
1102 	u8 hidden_ssid[IW_ESSID_MAX_SIZE + 1];
1103 	u8 hidden_ssid_len;
1104 	struct rtllib_qos_data qos_data;
1105 
1106 	bool	bWithAironetIE;
1107 	bool	bCkipSupported;
1108 	bool	bCcxRmEnable;
1109 	u16	CcxRmState[2];
1110 	bool	bMBssidValid;
1111 	u8	MBssidMask;
1112 	u8	MBssid[ETH_ALEN];
1113 	bool	bWithCcxVerNum;
1114 	u8	BssCcxVerNumber;
1115 	/* These are network statistics */
1116 	struct rtllib_rx_stats stats;
1117 	u16 capability;
1118 	u8  rates[MAX_RATES_LENGTH];
1119 	u8  rates_len;
1120 	u8  rates_ex[MAX_RATES_EX_LENGTH];
1121 	u8  rates_ex_len;
1122 	unsigned long last_scanned;
1123 	u8  mode;
1124 	u32 flags;
1125 	u32 time_stamp[2];
1126 	u16 beacon_interval;
1127 	u16 listen_interval;
1128 	u16 atim_window;
1129 	u8  erp_value;
1130 	u8  wpa_ie[MAX_WPA_IE_LEN];
1131 	size_t wpa_ie_len;
1132 	u8  rsn_ie[MAX_WPA_IE_LEN];
1133 	size_t rsn_ie_len;
1134 	u8  wzc_ie[MAX_WZC_IE_LEN];
1135 	size_t wzc_ie_len;
1136 
1137 	struct rtllib_tim_parameters tim;
1138 	u8  dtim_period;
1139 	u8  dtim_data;
1140 	u64 last_dtim_sta_time;
1141 
1142 	u8 wmm_info;
1143 	struct rtllib_wmm_ac_param wmm_param[4];
1144 	u8 Turbo_Enable;
1145 	u16 CountryIeLen;
1146 	u8 CountryIeBuf[MAX_IE_LEN];
1147 	struct bss_ht bssht;
1148 	bool broadcom_cap_exist;
1149 	bool realtek_cap_exit;
1150 	bool marvell_cap_exist;
1151 	bool ralink_cap_exist;
1152 	bool atheros_cap_exist;
1153 	bool cisco_cap_exist;
1154 	bool airgo_cap_exist;
1155 	bool unknown_cap_exist;
1156 	bool	berp_info_valid;
1157 	bool buseprotection;
1158 	u8 SignalStrength;
1159 	u8 RSSI;
1160 	struct list_head list;
1161 };
1162 
1163 enum rtllib_state {
1164 
1165 	/* the card is not linked at all */
1166 	RTLLIB_NOLINK = 0,
1167 
1168 	/* RTLLIB_ASSOCIATING* are for BSS client mode
1169 	 * the driver shall not perform RX filtering unless
1170 	 * the state is LINKED.
1171 	 * The driver shall just check for the state LINKED and
1172 	 * defaults to NOLINK for ALL the other states (including
1173 	 * LINKED_SCANNING)
1174 	 */
1175 
1176 	/* the association procedure will start (wq scheduling)*/
1177 	RTLLIB_ASSOCIATING,
1178 	RTLLIB_ASSOCIATING_RETRY,
1179 
1180 	/* the association procedure is sending AUTH request*/
1181 	RTLLIB_ASSOCIATING_AUTHENTICATING,
1182 
1183 	/* the association procedure has successfully authenticated
1184 	 * and is sending association request
1185 	 */
1186 	RTLLIB_ASSOCIATING_AUTHENTICATED,
1187 
1188 	/* the link is ok. the card associated to a BSS or linked
1189 	 * to a ibss cell or acting as an AP and creating the bss
1190 	 */
1191 	RTLLIB_LINKED,
1192 
1193 	/* same as LINKED, but the driver shall apply RX filter
1194 	 * rules as we are in NO_LINK mode. As the card is still
1195 	 * logically linked, but it is doing a syncro site survey
1196 	 * then it will be back to LINKED state.
1197 	 */
1198 	RTLLIB_LINKED_SCANNING,
1199 };
1200 
1201 #define DEFAULT_MAX_SCAN_AGE (15 * HZ)
1202 #define DEFAULT_FTS 2346
1203 
1204 #define CFG_RTLLIB_RESERVE_FCS (1<<0)
1205 #define CFG_RTLLIB_COMPUTE_FCS (1<<1)
1206 
1207 struct tx_pending {
1208 	int frag;
1209 	struct rtllib_txb *txb;
1210 };
1211 
1212 struct bandwidth_autoswitch {
1213 	long threshold_20Mhzto40Mhz;
1214 	long	threshold_40Mhzto20Mhz;
1215 	bool bforced_tx20Mhz;
1216 	bool bautoswitch_enable;
1217 };
1218 
1219 
1220 
1221 #define REORDER_WIN_SIZE	128
1222 #define REORDER_ENTRY_NUM	128
1223 struct rx_reorder_entry {
1224 	struct list_head	List;
1225 	u16			SeqNum;
1226 	struct rtllib_rxb *prxb;
1227 };
1228 enum fsync_state {
1229 	Default_Fsync,
1230 	HW_Fsync,
1231 	SW_Fsync
1232 };
1233 
1234 enum rt_ps_mode {
1235 	eActive,
1236 	eMaxPs,
1237 	eFastPs,
1238 	eAutoPs,
1239 };
1240 
1241 enum ips_callback_function {
1242 	IPS_CALLBACK_NONE = 0,
1243 	IPS_CALLBACK_MGNT_LINK_REQUEST = 1,
1244 	IPS_CALLBACK_JOIN_REQUEST = 2,
1245 };
1246 
1247 enum rt_rf_power_state {
1248 	eRfOn,
1249 	eRfSleep,
1250 	eRfOff
1251 };
1252 
1253 struct rt_pwr_save_ctrl {
1254 
1255 	bool				bInactivePs;
1256 	bool				bIPSModeBackup;
1257 	bool				bSwRfProcessing;
1258 	enum rt_rf_power_state eInactivePowerState;
1259 	enum ips_callback_function ReturnPoint;
1260 
1261 	bool				bLeisurePs;
1262 	u8				LpsIdleCount;
1263 	u8				RegMaxLPSAwakeIntvl;
1264 	u8				LPSAwakeIntvl;
1265 
1266 	u32				CurPsLevel;
1267 	u32				RegRfPsLevel;
1268 
1269 	bool				bFwCtrlLPS;
1270 
1271 };
1272 
1273 #define RT_RF_CHANGE_SOURCE u32
1274 
1275 #define RF_CHANGE_BY_SW BIT31
1276 #define RF_CHANGE_BY_HW BIT30
1277 #define RF_CHANGE_BY_PS BIT29
1278 #define RF_CHANGE_BY_IPS BIT28
1279 #define RF_CHANGE_BY_INIT	0
1280 
1281 enum country_code_type {
1282 	COUNTRY_CODE_FCC = 0,
1283 	COUNTRY_CODE_IC = 1,
1284 	COUNTRY_CODE_ETSI = 2,
1285 	COUNTRY_CODE_SPAIN = 3,
1286 	COUNTRY_CODE_FRANCE = 4,
1287 	COUNTRY_CODE_MKK = 5,
1288 	COUNTRY_CODE_MKK1 = 6,
1289 	COUNTRY_CODE_ISRAEL = 7,
1290 	COUNTRY_CODE_TELEC = 8,
1291 	COUNTRY_CODE_MIC = 9,
1292 	COUNTRY_CODE_GLOBAL_DOMAIN = 10,
1293 	COUNTRY_CODE_WORLD_WIDE_13 = 11,
1294 	COUNTRY_CODE_TELEC_NETGEAR = 12,
1295 	COUNTRY_CODE_MAX
1296 };
1297 
1298 enum scan_op_backup_opt {
1299 	SCAN_OPT_BACKUP = 0,
1300 	SCAN_OPT_RESTORE,
1301 	SCAN_OPT_MAX
1302 };
1303 
1304 enum fw_cmd_io_type {
1305 	FW_CMD_DIG_ENABLE = 0,
1306 	FW_CMD_DIG_DISABLE = 1,
1307 	FW_CMD_DIG_HALT = 2,
1308 	FW_CMD_DIG_RESUME = 3,
1309 	FW_CMD_HIGH_PWR_ENABLE = 4,
1310 	FW_CMD_HIGH_PWR_DISABLE = 5,
1311 	FW_CMD_RA_RESET = 6,
1312 	FW_CMD_RA_ACTIVE = 7,
1313 	FW_CMD_RA_REFRESH_N = 8,
1314 	FW_CMD_RA_REFRESH_BG = 9,
1315 	FW_CMD_RA_INIT = 10,
1316 	FW_CMD_IQK_ENABLE = 11,
1317 	FW_CMD_TXPWR_TRACK_ENABLE = 12,
1318 	FW_CMD_TXPWR_TRACK_DISABLE = 13,
1319 	FW_CMD_TXPWR_TRACK_THERMAL = 14,
1320 	FW_CMD_PAUSE_DM_BY_SCAN = 15,
1321 	FW_CMD_RESUME_DM_BY_SCAN = 16,
1322 	FW_CMD_RA_REFRESH_N_COMB = 17,
1323 	FW_CMD_RA_REFRESH_BG_COMB = 18,
1324 	FW_CMD_ANTENNA_SW_ENABLE = 19,
1325 	FW_CMD_ANTENNA_SW_DISABLE = 20,
1326 	FW_CMD_TX_FEEDBACK_CCX_ENABLE = 21,
1327 	FW_CMD_LPS_ENTER = 22,
1328 	FW_CMD_LPS_LEAVE = 23,
1329 };
1330 
1331 #define RT_MAX_LD_SLOT_NUM	10
1332 struct rt_link_detect {
1333 
1334 	u32				NumRecvBcnInPeriod;
1335 	u32				NumRecvDataInPeriod;
1336 
1337 	u32				RxBcnNum[RT_MAX_LD_SLOT_NUM];
1338 	u32				RxDataNum[RT_MAX_LD_SLOT_NUM];
1339 	u16				SlotNum;
1340 	u16				SlotIndex;
1341 
1342 	u32				NumTxOkInPeriod;
1343 	u32				NumRxOkInPeriod;
1344 	u32				NumRxUnicastOkInPeriod;
1345 	bool				bBusyTraffic;
1346 	bool				bHigherBusyTraffic;
1347 	bool				bHigherBusyRxTraffic;
1348 };
1349 
1350 struct sw_cam_table {
1351 
1352 	u8				macaddr[ETH_ALEN];
1353 	bool				bused;
1354 	u8				key_buf[16];
1355 	u16				key_type;
1356 	u8				useDK;
1357 	u8				key_index;
1358 
1359 };
1360 #define   TOTAL_CAM_ENTRY				32
1361 struct rate_adaptive {
1362 	u8				rate_adaptive_disabled;
1363 	u8				ratr_state;
1364 	u16				reserve;
1365 
1366 	u32				high_rssi_thresh_for_ra;
1367 	u32				high2low_rssi_thresh_for_ra;
1368 	u8				low2high_rssi_thresh_for_ra40M;
1369 	u32				low_rssi_thresh_for_ra40M;
1370 	u8				low2high_rssi_thresh_for_ra20M;
1371 	u32				low_rssi_thresh_for_ra20M;
1372 	u32				upper_rssi_threshold_ratr;
1373 	u32				middle_rssi_threshold_ratr;
1374 	u32				low_rssi_threshold_ratr;
1375 	u32				low_rssi_threshold_ratr_40M;
1376 	u32				low_rssi_threshold_ratr_20M;
1377 	u8				ping_rssi_enable;
1378 	u32				ping_rssi_ratr;
1379 	u32				ping_rssi_thresh_for_ra;
1380 	u32				last_ratr;
1381 	u8				PreRATRState;
1382 
1383 };
1384 
1385 #define	NUM_PMKID_CACHE		16
1386 struct rt_pmkid_list {
1387 	u8 Bssid[ETH_ALEN];
1388 	u8 PMKID[16];
1389 	u8 SsidBuf[33];
1390 	u8 bUsed;
1391 };
1392 
1393 struct rt_intel_promisc_mode {
1394 	bool bPromiscuousOn;
1395 	bool bFilterSourceStationFrame;
1396 };
1397 
1398 
1399 /*************** DRIVER STATUS   *****/
1400 #define STATUS_SCANNING			0
1401 /*************** DRIVER STATUS   *****/
1402 
1403 enum {
1404 	LPS_IS_WAKE = 0,
1405 	LPS_IS_SLEEP = 1,
1406 	LPS_WAIT_NULL_DATA_SEND = 2,
1407 };
1408 
1409 struct rtllib_device {
1410 	struct pci_dev *pdev;
1411 	struct net_device *dev;
1412 	struct rtllib_security sec;
1413 
1414 	bool disable_mgnt_queue;
1415 
1416 	unsigned long status;
1417 	u8	CntAfterLink;
1418 
1419 	enum rt_op_mode OpMode;
1420 
1421 	/* The last AssocReq/Resp IEs */
1422 	u8 *assocreq_ies, *assocresp_ies;
1423 	size_t assocreq_ies_len, assocresp_ies_len;
1424 
1425 	bool	bForcedBgMode;
1426 	u8 RF_Type;
1427 
1428 	u8 hwsec_active;
1429 	bool is_silent_reset;
1430 	bool is_roaming;
1431 	bool ieee_up;
1432 	bool cannot_notify;
1433 	bool bSupportRemoteWakeUp;
1434 	bool actscanning;
1435 	bool FirstIe_InScan;
1436 	bool be_scan_inprogress;
1437 	bool beinretry;
1438 	enum rt_rf_power_state eRFPowerState;
1439 	RT_RF_CHANGE_SOURCE	RfOffReason;
1440 	bool is_set_key;
1441 	bool wx_set_enc;
1442 	struct rt_hi_throughput *pHTInfo;
1443 
1444 	spinlock_t reorder_spinlock;
1445 	u8	Regdot11HTOperationalRateSet[16];
1446 	u8	Regdot11TxHTOperationalRateSet[16];
1447 	u8	dot11HTOperationalRateSet[16];
1448 	u8	RegHTSuppRateSet[16];
1449 	u8	HTCurrentOperaRate;
1450 	u8	HTHighestOperaRate;
1451 	u8	bTxDisableRateFallBack;
1452 	u8	bTxUseDriverAssingedRate;
1453 	u8	bTxEnableFwCalcDur;
1454 	atomic_t	atm_swbw;
1455 
1456 	struct list_head		Tx_TS_Admit_List;
1457 	struct list_head		Tx_TS_Pending_List;
1458 	struct list_head		Tx_TS_Unused_List;
1459 	struct tx_ts_record TxTsRecord[TOTAL_TS_NUM];
1460 	struct list_head		Rx_TS_Admit_List;
1461 	struct list_head		Rx_TS_Pending_List;
1462 	struct list_head		Rx_TS_Unused_List;
1463 	struct rx_ts_record RxTsRecord[TOTAL_TS_NUM];
1464 	struct rx_reorder_entry RxReorderEntry[128];
1465 	struct list_head		RxReorder_Unused_List;
1466 
1467 
1468 	/* Bookkeeping structures */
1469 	struct net_device_stats stats;
1470 	struct rtllib_softmac_stats softmac_stats;
1471 
1472 	/* Probe / Beacon management */
1473 	struct list_head network_free_list;
1474 	struct list_head network_list;
1475 	struct rtllib_network *networks;
1476 	int scans;
1477 	int scan_age;
1478 
1479 	int iw_mode; /* operating mode (IW_MODE_*) */
1480 	bool bNetPromiscuousMode;
1481 	struct rt_intel_promisc_mode IntelPromiscuousModeInfo;
1482 
1483 	spinlock_t lock;
1484 	spinlock_t wpax_suitlist_lock;
1485 
1486 	int tx_headroom; /* Set to size of any additional room needed at front
1487 			  * of allocated Tx SKBs
1488 			  */
1489 	u32 config;
1490 
1491 	/* WEP and other encryption related settings at the device level */
1492 	int open_wep; /* Set to 1 to allow unencrypted frames */
1493 	int auth_mode;
1494 	int reset_on_keychange; /* Set to 1 if the HW needs to be reset on
1495 				 * WEP key changes
1496 				 */
1497 
1498 	/* If the host performs {en,de}cryption, then set to 1 */
1499 	int host_encrypt;
1500 	int host_decrypt;
1501 
1502 	int ieee802_1x; /* is IEEE 802.1X used */
1503 
1504 	/* WPA data */
1505 	bool bHalfWirelessN24GMode;
1506 	int wpa_enabled;
1507 	int drop_unencrypted;
1508 	int tkip_countermeasures;
1509 	int privacy_invoked;
1510 	size_t wpa_ie_len;
1511 	u8 *wpa_ie;
1512 	size_t wps_ie_len;
1513 	u8 *wps_ie;
1514 	u8 ap_mac_addr[ETH_ALEN];
1515 	u16 pairwise_key_type;
1516 	u16 group_key_type;
1517 
1518 	struct lib80211_crypt_info crypt_info;
1519 
1520 	struct sw_cam_table swcamtable[TOTAL_CAM_ENTRY];
1521 
1522 	struct rt_pmkid_list PMKIDList[NUM_PMKID_CACHE];
1523 
1524 	/* Fragmentation structures */
1525 	struct rtllib_frag_entry frag_cache[17][RTLLIB_FRAG_CACHE_LEN];
1526 	unsigned int frag_next_idx[17];
1527 	u16 fts; /* Fragmentation Threshold */
1528 #define DEFAULT_RTS_THRESHOLD 2346U
1529 #define MIN_RTS_THRESHOLD 1
1530 #define MAX_RTS_THRESHOLD 2346U
1531 	u16 rts; /* RTS threshold */
1532 
1533 	/* Association info */
1534 	u8 bssid[ETH_ALEN];
1535 
1536 	/* This stores infos for the current network.
1537 	 * Either the network we are associated in INFRASTRUCTURE
1538 	 * or the network that we are creating in MASTER mode.
1539 	 * ad-hoc is a mixture ;-).
1540 	 * Note that in infrastructure mode, even when not associated,
1541 	 * fields bssid and essid may be valid (if wpa_set and essid_set
1542 	 * are true) as thy carry the value set by the user via iwconfig
1543 	 */
1544 	struct rtllib_network current_network;
1545 
1546 	enum rtllib_state state;
1547 
1548 	int short_slot;
1549 	int mode;       /* A, B, G */
1550 	int modulation; /* CCK, OFDM */
1551 
1552 	/* used for forcing the ibss workqueue to terminate
1553 	 * without wait for the syncro scan to terminate
1554 	 */
1555 	short sync_scan_hurryup;
1556 	u16 scan_watch_dog;
1557 
1558 	/* map of allowed channels. 0 is dummy */
1559 	void *dot11d_info;
1560 	bool global_domain;
1561 	u8 active_channel_map[MAX_CHANNEL_NUMBER+1];
1562 
1563 	u8   bss_start_channel;
1564 	u8   ibss_maxjoin_chal;
1565 
1566 	int rate;       /* current rate */
1567 	int basic_rate;
1568 
1569 	short active_scan;
1570 
1571 	/* this contains flags for selectively enable softmac support */
1572 	u16 softmac_features;
1573 
1574 	/* if the sequence control field is not filled by HW */
1575 	u16 seq_ctrl[5];
1576 
1577 	/* association procedure transaction sequence number */
1578 	u16 associate_seq;
1579 
1580 	/* AID for RTXed association responses */
1581 	u16 assoc_id;
1582 
1583 	/* power save mode related*/
1584 	u8 ack_tx_to_ieee;
1585 	short ps;
1586 	short sta_sleep;
1587 	int ps_timeout;
1588 	int ps_period;
1589 	struct tasklet_struct ps_task;
1590 	u64 ps_time;
1591 	bool polling;
1592 
1593 	short raw_tx;
1594 	/* used if IEEE_SOFTMAC_TX_QUEUE is set */
1595 	short queue_stop;
1596 	short scanning_continue;
1597 	short proto_started;
1598 	short proto_stoppping;
1599 
1600 	struct mutex wx_mutex;
1601 	struct mutex scan_mutex;
1602 	struct mutex ips_mutex;
1603 
1604 	spinlock_t mgmt_tx_lock;
1605 	spinlock_t beacon_lock;
1606 
1607 	short beacon_txing;
1608 
1609 	short wap_set;
1610 	short ssid_set;
1611 
1612 	/* set on initialization */
1613 	unsigned int wmm_acm;
1614 
1615 	/* for discarding duplicated packets in IBSS */
1616 	struct list_head ibss_mac_hash[IEEE_IBSS_MAC_HASH_SIZE];
1617 
1618 	/* for discarding duplicated packets in BSS */
1619 	u16 last_rxseq_num[17]; /* rx seq previous per-tid */
1620 	u16 last_rxfrag_num[17];/* tx frag previous per-tid */
1621 	unsigned long last_packet_time[17];
1622 
1623 	/* for PS mode */
1624 	unsigned long last_rx_ps_time;
1625 	bool			bAwakePktSent;
1626 	u8			LPSDelayCnt;
1627 
1628 	/* used if IEEE_SOFTMAC_SINGLE_QUEUE is set */
1629 	struct sk_buff *mgmt_queue_ring[MGMT_QUEUE_NUM];
1630 	int mgmt_queue_head;
1631 	int mgmt_queue_tail;
1632 	u8 AsocRetryCount;
1633 	struct sk_buff_head skb_waitQ[MAX_QUEUE_SIZE];
1634 	struct sk_buff_head  skb_aggQ[MAX_QUEUE_SIZE];
1635 
1636 	bool	bdynamic_txpower_enable;
1637 
1638 	bool bCTSToSelfEnable;
1639 
1640 	u32	fsync_time_interval;
1641 	u32	fsync_rate_bitmap;
1642 	u8	fsync_rssi_threshold;
1643 	bool	bfsync_enable;
1644 
1645 	u8	fsync_multiple_timeinterval;
1646 	u32	fsync_firstdiff_ratethreshold;
1647 	u32	fsync_seconddiff_ratethreshold;
1648 	enum fsync_state fsync_state;
1649 	bool		bis_any_nonbepkts;
1650 	struct bandwidth_autoswitch bandwidth_auto_switch;
1651 	bool FwRWRF;
1652 
1653 	struct rt_link_detect LinkDetectInfo;
1654 	bool bIsAggregateFrame;
1655 	struct rt_pwr_save_ctrl PowerSaveControl;
1656 
1657 	/* used if IEEE_SOFTMAC_TX_QUEUE is set */
1658 	struct tx_pending tx_pending;
1659 
1660 	/* used if IEEE_SOFTMAC_ASSOCIATE is set */
1661 	struct timer_list associate_timer;
1662 
1663 	/* used if IEEE_SOFTMAC_BEACONS is set */
1664 	struct timer_list beacon_timer;
1665 	u8 need_sw_enc;
1666 	struct work_struct associate_complete_wq;
1667 	struct work_struct ips_leave_wq;
1668 	struct delayed_work associate_procedure_wq;
1669 	struct delayed_work softmac_scan_wq;
1670 	struct delayed_work associate_retry_wq;
1671 	struct delayed_work start_ibss_wq;
1672 	struct delayed_work hw_wakeup_wq;
1673 	struct delayed_work hw_sleep_wq;
1674 	struct delayed_work link_change_wq;
1675 	struct work_struct wx_sync_scan_wq;
1676 
1677 	union {
1678 		struct rtllib_rxb *RfdArray[REORDER_WIN_SIZE];
1679 		struct rtllib_rxb *stats_IndicateArray[REORDER_WIN_SIZE];
1680 		struct rtllib_rxb *prxbIndicateArray[REORDER_WIN_SIZE];
1681 		struct {
1682 			struct sw_chnl_cmd PreCommonCmd[MAX_PRECMD_CNT];
1683 			struct sw_chnl_cmd PostCommonCmd[MAX_POSTCMD_CNT];
1684 			struct sw_chnl_cmd RfDependCmd[MAX_RFDEPENDCMD_CNT];
1685 		};
1686 	};
1687 
1688 	/* Callback functions */
1689 	void (*set_security)(struct net_device *dev,
1690 			     struct rtllib_security *sec);
1691 
1692 	/* Used to TX data frame by using txb structs.
1693 	 * this is not used if in the softmac_features
1694 	 * is set the flag IEEE_SOFTMAC_TX_QUEUE
1695 	 */
1696 	int (*hard_start_xmit)(struct rtllib_txb *txb,
1697 			       struct net_device *dev);
1698 
1699 	int (*reset_port)(struct net_device *dev);
1700 
1701 	/* Softmac-generated frames (management) are TXed via this
1702 	 * callback if the flag IEEE_SOFTMAC_SINGLE_QUEUE is
1703 	 * not set. As some cards may have different HW queues that
1704 	 * one might want to use for data and management frames
1705 	 * the option to have two callbacks might be useful.
1706 	 * This function can't sleep.
1707 	 */
1708 	int (*softmac_hard_start_xmit)(struct sk_buff *skb,
1709 			       struct net_device *dev);
1710 
1711 	/* used instead of hard_start_xmit (not softmac_hard_start_xmit)
1712 	 * if the IEEE_SOFTMAC_TX_QUEUE feature is used to TX data
1713 	 * frames. If the option IEEE_SOFTMAC_SINGLE_QUEUE is also set
1714 	 * then also management frames are sent via this callback.
1715 	 * This function can't sleep.
1716 	 */
1717 	void (*softmac_data_hard_start_xmit)(struct sk_buff *skb,
1718 			       struct net_device *dev, int rate);
1719 
1720 	/* stops the HW queue for DATA frames. Useful to avoid
1721 	 * waste time to TX data frame when we are reassociating
1722 	 * This function can sleep.
1723 	 */
1724 	void (*data_hard_stop)(struct net_device *dev);
1725 
1726 	/* OK this is complementing to data_poll_hard_stop */
1727 	void (*data_hard_resume)(struct net_device *dev);
1728 
1729 	/* ask to the driver to retune the radio.
1730 	 * This function can sleep. the driver should ensure
1731 	 * the radio has been switched before return.
1732 	 */
1733 	void (*set_chan)(struct net_device *dev, short ch);
1734 
1735 	void (*rtllib_start_hw_scan)(struct net_device *dev);
1736 	void (*rtllib_stop_hw_scan)(struct net_device *dev);
1737 
1738 	/* indicate the driver that the link state is changed
1739 	 * for example it may indicate the card is associated now.
1740 	 * Driver might be interested in this to apply RX filter
1741 	 * rules or simply light the LINK led
1742 	 */
1743 	void (*link_change)(struct net_device *dev);
1744 
1745 	/* these two function indicates to the HW when to start
1746 	 * and stop to send beacons. This is used when the
1747 	 * IEEE_SOFTMAC_BEACONS is not set. For now the
1748 	 * stop_send_bacons is NOT guaranteed to be called only
1749 	 * after start_send_beacons.
1750 	 */
1751 	void (*start_send_beacons)(struct net_device *dev);
1752 	void (*stop_send_beacons)(struct net_device *dev);
1753 
1754 	/* power save mode related */
1755 	void (*sta_wake_up)(struct net_device *dev);
1756 	void (*enter_sleep_state)(struct net_device *dev, u64 time);
1757 	short (*ps_is_queue_empty)(struct net_device *dev);
1758 	int (*handle_beacon)(struct net_device *dev,
1759 			     struct rtllib_beacon *beacon,
1760 			     struct rtllib_network *network);
1761 	int (*handle_assoc_response)(struct net_device *dev,
1762 				     struct rtllib_assoc_response_frame *resp,
1763 				     struct rtllib_network *network);
1764 
1765 
1766 	/* check whether Tx hw resource available */
1767 	short (*check_nic_enough_desc)(struct net_device *dev, int queue_index);
1768 	void (*SetBWModeHandler)(struct net_device *dev,
1769 				 enum ht_channel_width Bandwidth,
1770 				 enum ht_extchnl_offset Offset);
1771 	bool (*GetNmodeSupportBySecCfg)(struct net_device *dev);
1772 	void (*SetWirelessMode)(struct net_device *dev, u8 wireless_mode);
1773 	bool (*GetHalfNmodeSupportByAPsHandler)(struct net_device *dev);
1774 	u8   (*rtllib_ap_sec_type)(struct rtllib_device *ieee);
1775 	void (*InitialGainHandler)(struct net_device *dev, u8 Operation);
1776 	bool (*SetFwCmdHandler)(struct net_device *dev,
1777 				enum fw_cmd_io_type FwCmdIO);
1778 	void (*UpdateBeaconInterruptHandler)(struct net_device *dev,
1779 					     bool start);
1780 	void (*ScanOperationBackupHandler)(struct net_device *dev,
1781 					   u8 Operation);
1782 	void (*LedControlHandler)(struct net_device *dev,
1783 				  enum led_ctl_mode LedAction);
1784 	void (*SetHwRegHandler)(struct net_device *dev, u8 variable, u8 *val);
1785 
1786 	void (*AllowAllDestAddrHandler)(struct net_device *dev,
1787 					bool bAllowAllDA, bool WriteIntoReg);
1788 
1789 	void (*rtllib_ips_leave_wq)(struct net_device *dev);
1790 	void (*rtllib_ips_leave)(struct net_device *dev);
1791 	void (*LeisurePSLeave)(struct net_device *dev);
1792 
1793 	/* This must be the last item so that it points to the data
1794 	 * allocated beyond this structure by alloc_rtllib
1795 	 */
1796 	u8 priv[0];
1797 };
1798 
1799 #define IEEE_A	    (1<<0)
1800 #define IEEE_B	    (1<<1)
1801 #define IEEE_G	    (1<<2)
1802 #define IEEE_N_24G		  (1<<4)
1803 #define	IEEE_N_5G		  (1<<5)
1804 #define IEEE_MODE_MASK    (IEEE_A|IEEE_B|IEEE_G)
1805 
1806 /* Generate a 802.11 header */
1807 
1808 /* Uses the channel change callback directly
1809  * instead of [start/stop] scan callbacks
1810  */
1811 #define IEEE_SOFTMAC_SCAN (1<<2)
1812 
1813 /* Perform authentication and association handshake */
1814 #define IEEE_SOFTMAC_ASSOCIATE (1<<3)
1815 
1816 /* Generate probe requests */
1817 #define IEEE_SOFTMAC_PROBERQ (1<<4)
1818 
1819 /* Generate response to probe requests */
1820 #define IEEE_SOFTMAC_PROBERS (1<<5)
1821 
1822 /* The ieee802.11 stack will manage the netif queue
1823  * wake/stop for the driver, taking care of 802.11
1824  * fragmentation. See softmac.c for details.
1825  */
1826 #define IEEE_SOFTMAC_TX_QUEUE (1<<7)
1827 
1828 /* Uses only the softmac_data_hard_start_xmit
1829  * even for TX management frames.
1830  */
1831 #define IEEE_SOFTMAC_SINGLE_QUEUE (1<<8)
1832 
1833 /* Generate beacons.  The stack will enqueue beacons
1834  * to the card
1835  */
1836 #define IEEE_SOFTMAC_BEACONS (1<<6)
1837 
1838 
1839 static inline void *rtllib_priv(struct net_device *dev)
1840 {
1841 	return ((struct rtllib_device *)netdev_priv(dev))->priv;
1842 }
1843 
1844 static inline int rtllib_is_empty_essid(const char *essid, int essid_len)
1845 {
1846 	/* Single white space is for Linksys APs */
1847 	if (essid_len == 1 && essid[0] == ' ')
1848 		return 1;
1849 
1850 	/* Otherwise, if the entire essid is 0, we assume it is hidden */
1851 	while (essid_len) {
1852 		essid_len--;
1853 		if (essid[essid_len] != '\0')
1854 			return 0;
1855 	}
1856 
1857 	return 1;
1858 }
1859 
1860 static inline int rtllib_get_hdrlen(u16 fc)
1861 {
1862 	int hdrlen = RTLLIB_3ADDR_LEN;
1863 
1864 	switch (WLAN_FC_GET_TYPE(fc)) {
1865 	case RTLLIB_FTYPE_DATA:
1866 		if ((fc & RTLLIB_FCTL_FROMDS) && (fc & RTLLIB_FCTL_TODS))
1867 			hdrlen = RTLLIB_4ADDR_LEN; /* Addr4 */
1868 		if (RTLLIB_QOS_HAS_SEQ(fc))
1869 			hdrlen += 2; /* QOS ctrl*/
1870 		break;
1871 	case RTLLIB_FTYPE_CTL:
1872 		switch (WLAN_FC_GET_STYPE(fc)) {
1873 		case RTLLIB_STYPE_CTS:
1874 		case RTLLIB_STYPE_ACK:
1875 			hdrlen = RTLLIB_1ADDR_LEN;
1876 			break;
1877 		default:
1878 			hdrlen = RTLLIB_2ADDR_LEN;
1879 			break;
1880 		}
1881 		break;
1882 	}
1883 
1884 	return hdrlen;
1885 }
1886 
1887 static inline u8 *rtllib_get_payload(struct rtllib_hdr *hdr)
1888 {
1889 	switch (rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl))) {
1890 	case RTLLIB_1ADDR_LEN:
1891 		return ((struct rtllib_hdr_1addr *)hdr)->payload;
1892 	case RTLLIB_2ADDR_LEN:
1893 		return ((struct rtllib_hdr_2addr *)hdr)->payload;
1894 	case RTLLIB_3ADDR_LEN:
1895 		return ((struct rtllib_hdr_3addr *)hdr)->payload;
1896 	case RTLLIB_4ADDR_LEN:
1897 		return ((struct rtllib_hdr_4addr *)hdr)->payload;
1898 	}
1899 	return NULL;
1900 }
1901 
1902 static inline int rtllib_is_ofdm_rate(u8 rate)
1903 {
1904 	switch (rate & ~RTLLIB_BASIC_RATE_MASK) {
1905 	case RTLLIB_OFDM_RATE_6MB:
1906 	case RTLLIB_OFDM_RATE_9MB:
1907 	case RTLLIB_OFDM_RATE_12MB:
1908 	case RTLLIB_OFDM_RATE_18MB:
1909 	case RTLLIB_OFDM_RATE_24MB:
1910 	case RTLLIB_OFDM_RATE_36MB:
1911 	case RTLLIB_OFDM_RATE_48MB:
1912 	case RTLLIB_OFDM_RATE_54MB:
1913 		return 1;
1914 	}
1915 	return 0;
1916 }
1917 
1918 static inline int rtllib_is_cck_rate(u8 rate)
1919 {
1920 	switch (rate & ~RTLLIB_BASIC_RATE_MASK) {
1921 	case RTLLIB_CCK_RATE_1MB:
1922 	case RTLLIB_CCK_RATE_2MB:
1923 	case RTLLIB_CCK_RATE_5MB:
1924 	case RTLLIB_CCK_RATE_11MB:
1925 		return 1;
1926 	}
1927 	return 0;
1928 }
1929 
1930 
1931 /* rtllib.c */
1932 void free_rtllib(struct net_device *dev);
1933 struct net_device *alloc_rtllib(int sizeof_priv);
1934 
1935 /* rtllib_tx.c */
1936 
1937 int rtllib_encrypt_fragment(
1938 	struct rtllib_device *ieee,
1939 	struct sk_buff *frag,
1940 	int hdr_len);
1941 
1942 int rtllib_xmit(struct sk_buff *skb,  struct net_device *dev);
1943 void rtllib_txb_free(struct rtllib_txb *);
1944 
1945 /* rtllib_rx.c */
1946 int rtllib_rx(struct rtllib_device *ieee, struct sk_buff *skb,
1947 	      struct rtllib_rx_stats *rx_stats);
1948 void rtllib_rx_probe_rq(struct rtllib_device *ieee,
1949 			struct sk_buff *skb);
1950 int rtllib_legal_channel(struct rtllib_device *rtllib, u8 channel);
1951 
1952 /* rtllib_wx.c */
1953 int rtllib_wx_get_scan(struct rtllib_device *ieee,
1954 		       struct iw_request_info *info,
1955 		       union iwreq_data *wrqu, char *key);
1956 int rtllib_wx_set_encode(struct rtllib_device *ieee,
1957 			 struct iw_request_info *info,
1958 			 union iwreq_data *wrqu, char *key);
1959 int rtllib_wx_get_encode(struct rtllib_device *ieee,
1960 			 struct iw_request_info *info,
1961 			 union iwreq_data *wrqu, char *key);
1962 int rtllib_wx_set_encode_ext(struct rtllib_device *ieee,
1963 			     struct iw_request_info *info,
1964 			     union iwreq_data *wrqu, char *extra);
1965 int rtllib_wx_set_auth(struct rtllib_device *ieee,
1966 		       struct iw_request_info *info,
1967 		       struct iw_param *data, char *extra);
1968 int rtllib_wx_set_mlme(struct rtllib_device *ieee,
1969 		       struct iw_request_info *info,
1970 		       union iwreq_data *wrqu, char *extra);
1971 int rtllib_wx_set_gen_ie(struct rtllib_device *ieee, u8 *ie, size_t len);
1972 
1973 /* rtllib_softmac.c */
1974 int rtllib_rx_frame_softmac(struct rtllib_device *ieee, struct sk_buff *skb,
1975 			    struct rtllib_rx_stats *rx_stats, u16 type,
1976 			    u16 stype);
1977 void rtllib_softmac_new_net(struct rtllib_device *ieee,
1978 			    struct rtllib_network *net);
1979 
1980 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn);
1981 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee);
1982 
1983 void rtllib_stop_send_beacons(struct rtllib_device *ieee);
1984 void notify_wx_assoc_event(struct rtllib_device *ieee);
1985 void rtllib_start_ibss(struct rtllib_device *ieee);
1986 void rtllib_softmac_init(struct rtllib_device *ieee);
1987 void rtllib_softmac_free(struct rtllib_device *ieee);
1988 void rtllib_disassociate(struct rtllib_device *ieee);
1989 void rtllib_stop_scan(struct rtllib_device *ieee);
1990 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan);
1991 void rtllib_stop_scan_syncro(struct rtllib_device *ieee);
1992 void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh);
1993 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr);
1994 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee);
1995 void rtllib_start_protocol(struct rtllib_device *ieee);
1996 void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown);
1997 
1998 void rtllib_EnableNetMonitorMode(struct net_device *dev, bool bInitState);
1999 void rtllib_DisableNetMonitorMode(struct net_device *dev, bool bInitState);
2000 void rtllib_EnableIntelPromiscuousMode(struct net_device *dev, bool bInitState);
2001 void rtllib_DisableIntelPromiscuousMode(struct net_device *dev,
2002 					bool bInitState);
2003 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee,
2004 				  u8 mesh_flag, u8 shutdown);
2005 void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag);
2006 
2007 void rtllib_reset_queue(struct rtllib_device *ieee);
2008 void rtllib_wake_all_queues(struct rtllib_device *ieee);
2009 void rtllib_stop_all_queues(struct rtllib_device *ieee);
2010 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee);
2011 void rtllib_start_send_beacons(struct rtllib_device *ieee);
2012 void rtllib_stop_send_beacons(struct rtllib_device *ieee);
2013 
2014 void notify_wx_assoc_event(struct rtllib_device *ieee);
2015 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success);
2016 
2017 void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee);
2018 u8 rtllib_ap_sec_type(struct rtllib_device *ieee);
2019 
2020 /* rtllib_softmac_wx.c */
2021 
2022 int rtllib_wx_get_wap(struct rtllib_device *ieee, struct iw_request_info *info,
2023 		      union iwreq_data *wrqu, char *ext);
2024 
2025 int rtllib_wx_set_wap(struct rtllib_device *ieee, struct iw_request_info *info,
2026 		      union iwreq_data *awrq, char *extra);
2027 
2028 int rtllib_wx_get_essid(struct rtllib_device *ieee, struct iw_request_info *a,
2029 			union iwreq_data *wrqu, char *b);
2030 
2031 int rtllib_wx_set_rate(struct rtllib_device *ieee, struct iw_request_info *info,
2032 		       union iwreq_data *wrqu, char *extra);
2033 
2034 int rtllib_wx_get_rate(struct rtllib_device *ieee, struct iw_request_info *info,
2035 		       union iwreq_data *wrqu, char *extra);
2036 
2037 int rtllib_wx_set_mode(struct rtllib_device *ieee, struct iw_request_info *a,
2038 		       union iwreq_data *wrqu, char *b);
2039 
2040 int rtllib_wx_set_scan(struct rtllib_device *ieee, struct iw_request_info *a,
2041 		       union iwreq_data *wrqu, char *b);
2042 
2043 int rtllib_wx_set_essid(struct rtllib_device *ieee, struct iw_request_info *a,
2044 			union iwreq_data *wrqu, char *extra);
2045 
2046 int rtllib_wx_get_mode(struct rtllib_device *ieee, struct iw_request_info *a,
2047 		       union iwreq_data *wrqu, char *b);
2048 
2049 int rtllib_wx_set_freq(struct rtllib_device *ieee, struct iw_request_info *a,
2050 		       union iwreq_data *wrqu, char *b);
2051 
2052 int rtllib_wx_get_freq(struct rtllib_device *ieee, struct iw_request_info *a,
2053 		       union iwreq_data *wrqu, char *b);
2054 void rtllib_wx_sync_scan_wq(void *data);
2055 
2056 int rtllib_wx_set_rawtx(struct rtllib_device *ieee,
2057 			struct iw_request_info *info,
2058 			union iwreq_data *wrqu, char *extra);
2059 
2060 int rtllib_wx_get_name(struct rtllib_device *ieee, struct iw_request_info *info,
2061 		       union iwreq_data *wrqu, char *extra);
2062 
2063 int rtllib_wx_set_power(struct rtllib_device *ieee,
2064 			struct iw_request_info *info,
2065 			union iwreq_data *wrqu, char *extra);
2066 
2067 int rtllib_wx_get_power(struct rtllib_device *ieee,
2068 			struct iw_request_info *info,
2069 			union iwreq_data *wrqu, char *extra);
2070 
2071 int rtllib_wx_set_rts(struct rtllib_device *ieee, struct iw_request_info *info,
2072 		      union iwreq_data *wrqu, char *extra);
2073 
2074 int rtllib_wx_get_rts(struct rtllib_device *ieee, struct iw_request_info *info,
2075 		      union iwreq_data *wrqu, char *extra);
2076 #define MAX_RECEIVE_BUFFER_SIZE 9100
2077 
2078 void HTSetConnectBwMode(struct rtllib_device *ieee,
2079 			enum ht_channel_width Bandwidth,
2080 			enum ht_extchnl_offset Offset);
2081 void HTUpdateDefaultSetting(struct rtllib_device *ieee);
2082 void HTConstructCapabilityElement(struct rtllib_device *ieee,
2083 				  u8 *posHTCap, u8 *len,
2084 				  u8 isEncrypt, bool bAssoc);
2085 void HTConstructInfoElement(struct rtllib_device *ieee,
2086 			    u8 *posHTInfo, u8 *len, u8 isEncrypt);
2087 void HTConstructRT2RTAggElement(struct rtllib_device *ieee,
2088 				u8 *posRT2RTAgg, u8 *len);
2089 void HTOnAssocRsp(struct rtllib_device *ieee);
2090 void HTInitializeHTInfo(struct rtllib_device *ieee);
2091 void HTInitializeBssDesc(struct bss_ht *pBssHT);
2092 void HTResetSelfAndSavePeerSetting(struct rtllib_device *ieee,
2093 				   struct rtllib_network *pNetwork);
2094 void HT_update_self_and_peer_setting(struct rtllib_device *ieee,
2095 				     struct rtllib_network *pNetwork);
2096 u8 HTGetHighestMCSRate(struct rtllib_device *ieee, u8 *pMCSRateSet,
2097 		       u8 *pMCSFilter);
2098 extern u8 MCS_FILTER_ALL[];
2099 extern u16 MCS_DATA_RATE[2][2][77];
2100 u8 HTCCheck(struct rtllib_device *ieee, u8 *pFrame);
2101 void HTResetIOTSetting(struct rt_hi_throughput *pHTInfo);
2102 bool IsHTHalfNmodeAPs(struct rtllib_device *ieee);
2103 u16  TxCountToDataRate(struct rtllib_device *ieee, u8 nDataRate);
2104 int rtllib_rx_ADDBAReq(struct rtllib_device *ieee, struct sk_buff *skb);
2105 int rtllib_rx_ADDBARsp(struct rtllib_device *ieee, struct sk_buff *skb);
2106 int rtllib_rx_DELBA(struct rtllib_device *ieee, struct sk_buff *skb);
2107 void TsInitAddBA(struct rtllib_device *ieee, struct tx_ts_record *pTS,
2108 		 u8 Policy, u8 bOverwritePending);
2109 void TsInitDelBA(struct rtllib_device *ieee,
2110 		 struct ts_common_info *pTsCommonInfo,
2111 		 enum tr_select TxRxSelect);
2112 void BaSetupTimeOut(struct timer_list *t);
2113 void TxBaInactTimeout(struct timer_list *t);
2114 void RxBaInactTimeout(struct timer_list *t);
2115 void ResetBaEntry(struct ba_record *pBA);
2116 bool GetTs(struct rtllib_device *ieee, struct ts_common_info **ppTS, u8 *Addr,
2117 	   u8 TID, enum tr_select TxRxSelect, bool bAddNewTs);
2118 void TSInitialize(struct rtllib_device *ieee);
2119 void TsStartAddBaProcess(struct rtllib_device *ieee,
2120 			 struct tx_ts_record *pTxTS);
2121 void RemovePeerTS(struct rtllib_device *ieee, u8 *Addr);
2122 void RemoveAllTS(struct rtllib_device *ieee);
2123 
2124 extern const long rtllib_wlan_frequencies[];
2125 
2126 static inline const char *escape_essid(const char *essid, u8 essid_len)
2127 {
2128 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
2129 
2130 	if (rtllib_is_empty_essid(essid, essid_len)) {
2131 		memcpy(escaped, "<hidden>", sizeof("<hidden>"));
2132 		return escaped;
2133 	}
2134 
2135 	snprintf(escaped, sizeof(escaped), "%*pEn", essid_len, essid);
2136 	return escaped;
2137 }
2138 
2139 /* fun with the built-in rtllib stack... */
2140 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn);
2141 
2142 
2143 /* For the function is more related to hardware setting, it's better to use the
2144  * ieee handler to refer to it.
2145  */
2146 void rtllib_FlushRxTsPendingPkts(struct rtllib_device *ieee,
2147 				 struct rx_ts_record *pTS);
2148 int rtllib_parse_info_param(struct rtllib_device *ieee,
2149 			    struct rtllib_info_element *info_element,
2150 			    u16 length,
2151 			    struct rtllib_network *network,
2152 			    struct rtllib_rx_stats *stats);
2153 
2154 void rtllib_indicate_packets(struct rtllib_device *ieee,
2155 			     struct rtllib_rxb **prxbIndicateArray, u8  index);
2156 void HTUseDefaultSetting(struct rtllib_device *ieee);
2157 #define RT_ASOC_RETRY_LIMIT	5
2158 u8 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee);
2159 
2160 #endif /* RTLLIB_H */
2161