xref: /linux/drivers/staging/rtl8723bs/include/wifi.h (revision 30b09d4b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #ifndef _WIFI_H_
8 #define _WIFI_H_
9 
10 #define WLAN_HDR_A3_LEN		24
11 #define WLAN_HDR_A3_QOS_LEN	26
12 
13 #define WLAN_WMM_LEN		24
14 
15 /*  This value is tested by WiFi 11n Test Plan 5.2.3. */
16 /*  This test verifies the WLAN NIC can update the NAV through sending the CTS with large duration. */
17 #define	WiFiNavUpperUs				30000	/*  30 ms */
18 
19 enum {
20 	WIFI_MGT_TYPE  =	(0),
21 	WIFI_CTRL_TYPE =	(BIT(2)),
22 	WIFI_DATA_TYPE =	(BIT(3)),
23 	WIFI_QOS_DATA_TYPE	= (BIT(7)|BIT(3)),	/*  QoS Data */
24 };
25 
26 enum {
27 
28     /*  below is for mgt frame */
29     WIFI_ASSOCREQ       = (0 | WIFI_MGT_TYPE),
30     WIFI_ASSOCRSP       = (BIT(4) | WIFI_MGT_TYPE),
31     WIFI_REASSOCREQ     = (BIT(5) | WIFI_MGT_TYPE),
32     WIFI_REASSOCRSP     = (BIT(5) | BIT(4) | WIFI_MGT_TYPE),
33     WIFI_PROBEREQ       = (BIT(6) | WIFI_MGT_TYPE),
34     WIFI_PROBERSP       = (BIT(6) | BIT(4) | WIFI_MGT_TYPE),
35     WIFI_BEACON         = (BIT(7) | WIFI_MGT_TYPE),
36     WIFI_ATIM           = (BIT(7) | BIT(4) | WIFI_MGT_TYPE),
37     WIFI_DISASSOC       = (BIT(7) | BIT(5) | WIFI_MGT_TYPE),
38     WIFI_AUTH           = (BIT(7) | BIT(5) | BIT(4) | WIFI_MGT_TYPE),
39     WIFI_DEAUTH         = (BIT(7) | BIT(6) | WIFI_MGT_TYPE),
40     WIFI_ACTION         = (BIT(7) | BIT(6) | BIT(4) | WIFI_MGT_TYPE),
41     WIFI_ACTION_NOACK = (BIT(7) | BIT(6) | BIT(5) | WIFI_MGT_TYPE),
42 
43     /*  below is for control frame */
44     WIFI_NDPA         = (BIT(6) | BIT(4) | WIFI_CTRL_TYPE),
45     WIFI_PSPOLL         = (BIT(7) | BIT(5) | WIFI_CTRL_TYPE),
46     WIFI_RTS            = (BIT(7) | BIT(5) | BIT(4) | WIFI_CTRL_TYPE),
47     WIFI_CTS            = (BIT(7) | BIT(6) | WIFI_CTRL_TYPE),
48     WIFI_ACK            = (BIT(7) | BIT(6) | BIT(4) | WIFI_CTRL_TYPE),
49     WIFI_CFEND          = (BIT(7) | BIT(6) | BIT(5) | WIFI_CTRL_TYPE),
50     WIFI_CFEND_CFACK    = (BIT(7) | BIT(6) | BIT(5) | BIT(4) | WIFI_CTRL_TYPE),
51 
52     /*  below is for data frame */
53     WIFI_DATA           = (0 | WIFI_DATA_TYPE),
54     WIFI_DATA_CFACK     = (BIT(4) | WIFI_DATA_TYPE),
55     WIFI_DATA_CFPOLL    = (BIT(5) | WIFI_DATA_TYPE),
56     WIFI_DATA_CFACKPOLL = (BIT(5) | BIT(4) | WIFI_DATA_TYPE),
57     WIFI_DATA_NULL      = (BIT(6) | WIFI_DATA_TYPE),
58     WIFI_CF_ACK         = (BIT(6) | BIT(4) | WIFI_DATA_TYPE),
59     WIFI_CF_POLL        = (BIT(6) | BIT(5) | WIFI_DATA_TYPE),
60     WIFI_CF_ACKPOLL     = (BIT(6) | BIT(5) | BIT(4) | WIFI_DATA_TYPE),
61     WIFI_QOS_DATA_NULL	= (BIT(6) | WIFI_QOS_DATA_TYPE),
62 };
63 
64 #define _TO_DS_		BIT(8)
65 #define _FROM_DS_	BIT(9)
66 #define _MORE_FRAG_	BIT(10)
67 #define _RETRY_		BIT(11)
68 #define _PWRMGT_	BIT(12)
69 #define _MORE_DATA_	BIT(13)
70 #define _PRIVACY_	BIT(14)
71 #define _ORDER_			BIT(15)
72 
73 #define SetToDs(pbuf)	\
74 	(*(__le16 *)(pbuf) |= cpu_to_le16(_TO_DS_))
75 
76 #define GetToDs(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_TO_DS_)) != 0)
77 
78 #define SetFrDs(pbuf)	\
79 	(*(__le16 *)(pbuf) |= cpu_to_le16(_FROM_DS_))
80 
81 #define GetFrDs(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_FROM_DS_)) != 0)
82 
83 #define get_tofr_ds(pframe)	((GetToDs(pframe) << 1) | GetFrDs(pframe))
84 
85 #define SetMFrag(pbuf)	\
86 	(*(__le16 *)(pbuf) |= cpu_to_le16(_MORE_FRAG_))
87 
88 #define GetMFrag(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_MORE_FRAG_)) != 0)
89 
90 #define ClearMFrag(pbuf)	\
91 	(*(__le16 *)(pbuf) &= (~cpu_to_le16(_MORE_FRAG_)))
92 
93 #define GetRetry(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_RETRY_)) != 0)
94 
95 #define ClearRetry(pbuf)	\
96 	(*(__le16 *)(pbuf) &= (~cpu_to_le16(_RETRY_)))
97 
98 #define SetPwrMgt(pbuf)	\
99 	(*(__le16 *)(pbuf) |= cpu_to_le16(_PWRMGT_))
100 
101 #define GetPwrMgt(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_PWRMGT_)) != 0)
102 
103 #define ClearPwrMgt(pbuf)	\
104 	(*(__le16 *)(pbuf) &= (~cpu_to_le16(_PWRMGT_)))
105 
106 #define SetMData(pbuf)	\
107 	(*(__le16 *)(pbuf) |= cpu_to_le16(_MORE_DATA_))
108 
109 #define GetMData(pbuf)	(((*(__le16 *)(pbuf)) & cpu_to_le16(_MORE_DATA_)) != 0)
110 
111 #define ClearMData(pbuf)	\
112 	(*(__le16 *)(pbuf) &= (~cpu_to_le16(_MORE_DATA_)))
113 
114 #define SetPrivacy(pbuf)	\
115 	(*(__le16 *)(pbuf) |= cpu_to_le16(_PRIVACY_))
116 
117 #define GetPrivacy(pbuf)					\
118 	(((*(__le16 *)(pbuf)) & cpu_to_le16(_PRIVACY_)) != 0)
119 
120 #define GetOrder(pbuf)					\
121 	(((*(__le16 *)(pbuf)) & cpu_to_le16(_ORDER_)) != 0)
122 
123 #define GetFrameType(pbuf)				\
124 	(le16_to_cpu(*(__le16 *)(pbuf)) & (BIT(3) | BIT(2)))
125 
126 #define SetFrameType(pbuf, type)	\
127 	do {	\
128 		*(unsigned short *)(pbuf) &= cpu_to_le16(~(BIT(3) | BIT(2))); \
129 		*(unsigned short *)(pbuf) |= cpu_to_le16(type); \
130 	} while (0)
131 
132 #define GetFrameSubType(pbuf)	(le16_to_cpu(*(__le16 *)(pbuf)) & (BIT(7) |\
133 	 BIT(6) | BIT(5) | BIT(4) | BIT(3) | BIT(2)))
134 
135 #define SetFrameSubType(pbuf, type) \
136 	do {    \
137 		*(__le16 *)(pbuf) &= cpu_to_le16(~(BIT(7) | BIT(6) |	\
138 		 BIT(5) | BIT(4) | BIT(3) | BIT(2))); \
139 		*(__le16 *)(pbuf) |= cpu_to_le16(type); \
140 	} while (0)
141 
142 #define GetSequence(pbuf)			\
143 	(le16_to_cpu(*(__le16 *)((size_t)(pbuf) + 22)) >> 4)
144 
145 #define GetFragNum(pbuf)			\
146 	(le16_to_cpu(*(__le16 *)((size_t)(pbuf) + 22)) & 0x0f)
147 
148 #define SetFragNum(pbuf, num) \
149 	do {    \
150 		*(unsigned short *)((size_t)(pbuf) + 22) = \
151 			((*(unsigned short *)((size_t)(pbuf) + 22)) &	\
152 			le16_to_cpu(~(0x000f))) | \
153 			cpu_to_le16(0x0f & (num));     \
154 	} while (0)
155 
156 #define SetSeqNum(pbuf, num) \
157 	do {    \
158 		*(__le16 *)((size_t)(pbuf) + 22) = \
159 			((*(__le16 *)((size_t)(pbuf) + 22)) & cpu_to_le16((unsigned short)0x000f)) | \
160 			cpu_to_le16((unsigned short)(0xfff0 & (num << 4))); \
161 	} while (0)
162 
163 #define SetDuration(pbuf, dur) \
164 	(*(__le16 *)((size_t)(pbuf) + 2) = cpu_to_le16(0xffff & (dur)))
165 
166 
167 #define SetPriority(pbuf, tid)	\
168 	(*(__le16 *)(pbuf) |= cpu_to_le16(tid & 0xf))
169 
170 #define GetPriority(pbuf)	((le16_to_cpu(*(__le16 *)(pbuf))) & 0xf)
171 
172 #define SetEOSP(pbuf, eosp)	\
173 		(*(__le16 *)(pbuf) |= cpu_to_le16((eosp & 1) << 4))
174 
175 #define SetAckpolicy(pbuf, ack)	\
176 	(*(__le16 *)(pbuf) |= cpu_to_le16((ack & 3) << 5))
177 
178 #define GetAckpolicy(pbuf) (((le16_to_cpu(*(__le16 *)pbuf)) >> 5) & 0x3)
179 
180 #define GetAMsdu(pbuf) (((le16_to_cpu(*(__le16 *)pbuf)) >> 7) & 0x1)
181 
182 #define GetAid(pbuf)	(le16_to_cpu(*(__le16 *)((size_t)(pbuf) + 2)) & 0x3fff)
183 
184 #define GetAddr1Ptr(pbuf)	((unsigned char *)((size_t)(pbuf) + 4))
185 
186 #define GetAddr2Ptr(pbuf)	((unsigned char *)((size_t)(pbuf) + 10))
187 
188 #define GetAddr3Ptr(pbuf)	((unsigned char *)((size_t)(pbuf) + 16))
189 
190 #define GetAddr4Ptr(pbuf)	((unsigned char *)((size_t)(pbuf) + 24))
191 
rtl8723bs_get_ra(unsigned char * pframe)192 static inline unsigned char *rtl8723bs_get_ra(unsigned char *pframe)
193 {
194 	unsigned char *ra;
195 	ra = GetAddr1Ptr(pframe);
196 	return ra;
197 }
get_ta(unsigned char * pframe)198 static inline unsigned char *get_ta(unsigned char *pframe)
199 {
200 	unsigned char *ta;
201 	ta = GetAddr2Ptr(pframe);
202 	return ta;
203 }
204 
get_da(unsigned char * pframe)205 static inline unsigned char *get_da(unsigned char *pframe)
206 {
207 	unsigned char *da;
208 	unsigned int	to_fr_ds	= (GetToDs(pframe) << 1) | GetFrDs(pframe);
209 
210 	switch (to_fr_ds) {
211 	case 0x00:	/*  ToDs = 0, FromDs = 0 */
212 		da = GetAddr1Ptr(pframe);
213 		break;
214 	case 0x01:	/*  ToDs = 0, FromDs = 1 */
215 		da = GetAddr1Ptr(pframe);
216 		break;
217 	case 0x02:	/*  ToDs = 1, FromDs = 0 */
218 		da = GetAddr3Ptr(pframe);
219 		break;
220 	default:	/*  ToDs = 1, FromDs = 1 */
221 		da = GetAddr3Ptr(pframe);
222 		break;
223 	}
224 
225 	return da;
226 }
227 
228 
get_sa(unsigned char * pframe)229 static inline unsigned char *get_sa(unsigned char *pframe)
230 {
231 	unsigned char *sa;
232 	unsigned int	to_fr_ds	= (GetToDs(pframe) << 1) | GetFrDs(pframe);
233 
234 	switch (to_fr_ds) {
235 	case 0x00:	/*  ToDs = 0, FromDs = 0 */
236 		sa = GetAddr2Ptr(pframe);
237 		break;
238 	case 0x01:	/*  ToDs = 0, FromDs = 1 */
239 		sa = GetAddr3Ptr(pframe);
240 		break;
241 	case 0x02:	/*  ToDs = 1, FromDs = 0 */
242 		sa = GetAddr2Ptr(pframe);
243 		break;
244 	default:	/*  ToDs = 1, FromDs = 1 */
245 		sa = GetAddr4Ptr(pframe);
246 		break;
247 	}
248 
249 	return sa;
250 }
251 
get_hdr_bssid(unsigned char * pframe)252 static inline unsigned char *get_hdr_bssid(unsigned char *pframe)
253 {
254 	unsigned char *sa = NULL;
255 	unsigned int	to_fr_ds	= (GetToDs(pframe) << 1) | GetFrDs(pframe);
256 
257 	switch (to_fr_ds) {
258 	case 0x00:	/*  ToDs = 0, FromDs = 0 */
259 		sa = GetAddr3Ptr(pframe);
260 		break;
261 	case 0x01:	/*  ToDs = 0, FromDs = 1 */
262 		sa = GetAddr2Ptr(pframe);
263 		break;
264 	case 0x02:	/*  ToDs = 1, FromDs = 0 */
265 		sa = GetAddr1Ptr(pframe);
266 		break;
267 	case 0x03:	/*  ToDs = 1, FromDs = 1 */
268 		sa = GetAddr1Ptr(pframe);
269 		break;
270 	}
271 
272 	return sa;
273 }
274 
275 
IsFrameTypeCtrl(unsigned char * pframe)276 static inline int IsFrameTypeCtrl(unsigned char *pframe)
277 {
278 	if (WIFI_CTRL_TYPE == GetFrameType(pframe))
279 		return true;
280 	else
281 		return false;
282 }
283 /*-----------------------------------------------------------------------------
284 			Below is for the security related definition
285 ------------------------------------------------------------------------------*/
286 
287 #define _ASOCREQ_IE_OFFSET_		4	/*  excluding wlan_hdr */
288 #define _REASOCREQ_IE_OFFSET_	10
289 #define _PROBEREQ_IE_OFFSET_	0
290 #define	_PROBERSP_IE_OFFSET_	12
291 #define _AUTH_IE_OFFSET_		6
292 #define _BEACON_IE_OFFSET_		12
293 
294 #define _FIXED_IE_LENGTH_			_BEACON_IE_OFFSET_
295 
296 /* ---------------------------------------------------------------------------
297 					Below is the fixed elements...
298 -----------------------------------------------------------------------------*/
299 #define _AUTH_ALGM_NUM_			2
300 #define _AUTH_SEQ_NUM_			2
301 #define _BEACON_ITERVAL_		2
302 #define _CAPABILITY_			2
303 #define _RSON_CODE_				2
304 #define _ASOC_ID_				2
305 #define _STATUS_CODE_			2
306 #define _TIMESTAMP_				8
307 
308 /*-----------------------------------------------------------------------------
309 				Below is the definition for 802.11i / 802.1x
310 ------------------------------------------------------------------------------*/
311 #define _IEEE8021X_MGT_			1		/*  WPA */
312 #define _IEEE8021X_PSK_			2		/*  WPA with pre-shared key */
313 
314 #define _MME_IE_LENGTH_  18
315 /*-----------------------------------------------------------------------------
316 				Below is the definition for WMM
317 ------------------------------------------------------------------------------*/
318 #define _WMM_IE_Length_				7  /*  for WMM STA */
319 
320 /*-----------------------------------------------------------------------------
321 				Below is the definition for 802.11n
322 ------------------------------------------------------------------------------*/
323 #define GetOrderBit(pbuf)	(((*(unsigned short *)(pbuf)) & cpu_to_le16(_ORDER_)) != 0)
324 
325 #define ACT_CAT_VENDOR				0x7F/* 127 */
326 
327 /**
328  * struct rtw_ieee80211_ht_cap - HT additional information
329  *
330  * This structure refers to "HT information element" as
331  * described in 802.11n draft section 7.3.2.53
332  */
333 struct ieee80211_ht_addt_info {
334 	unsigned char control_chan;
335 	unsigned char 	ht_param;
336 	__le16	operation_mode;
337 	__le16	stbc_param;
338 	unsigned char 	basic_set[16];
339 } __attribute__ ((packed));
340 
341 
342 struct HT_caps_element {
343 	union {
344 		struct {
345 			__le16	HT_caps_info;
346 			unsigned char AMPDU_para;
347 			unsigned char MCS_rate[16];
348 			__le16	HT_ext_caps;
349 			__le16	Beamforming_caps;
350 			unsigned char ASEL_caps;
351 		} HT_cap_element;
352 		unsigned char HT_cap[26];
353 	} u;
354 } __attribute__ ((packed));
355 
356 struct HT_info_element {
357 	unsigned char primary_channel;
358 	unsigned char infos[5];
359 	unsigned char MCS_rate[16];
360 }  __attribute__ ((packed));
361 
362 struct AC_param {
363 	unsigned char 	ACI_AIFSN;
364 	unsigned char 	CW;
365 	__le16	TXOP_limit;
366 }  __attribute__ ((packed));
367 
368 struct WMM_para_element {
369 	unsigned char 	QoS_info;
370 	unsigned char 	reserved;
371 	struct AC_param	ac_param[4];
372 }  __attribute__ ((packed));
373 
374 struct ADDBA_request {
375 	unsigned char 	dialog_token;
376 	__le16	BA_para_set;
377 	__le16	BA_timeout_value;
378 	__le16	BA_starting_seqctrl;
379 }  __attribute__ ((packed));
380 
381 /* 802.11n HT capabilities masks */
382 #define IEEE80211_HT_CAP_LDPC_CODING		0x0001
383 #define IEEE80211_HT_CAP_SUP_WIDTH		0x0002
384 #define IEEE80211_HT_CAP_SM_PS			0x000C
385 #define IEEE80211_HT_CAP_GRN_FLD		0x0010
386 #define IEEE80211_HT_CAP_SGI_20			0x0020
387 #define IEEE80211_HT_CAP_SGI_40			0x0040
388 #define IEEE80211_HT_CAP_TX_STBC			0x0080
389 #define IEEE80211_HT_CAP_RX_STBC_1R		0x0100
390 #define IEEE80211_HT_CAP_RX_STBC_3R		0x0300
391 #define IEEE80211_HT_CAP_MAX_AMSDU		0x0800
392 #define IEEE80211_HT_CAP_DSSSCCK40		0x1000
393 /* 802.11n HT capability AMPDU settings */
394 #define IEEE80211_HT_CAP_AMPDU_FACTOR		0x03
395 #define IEEE80211_HT_CAP_AMPDU_DENSITY		0x1C
396 
397 /* endif */
398 
399 /* 	===============WPS Section =============== */
400 /* 	WPS attribute ID */
401 #define WPS_ATTR_SELECTED_REGISTRAR	0x1041
402 
403 /* 	=====================P2P Section ===================== */
404 enum p2p_role {
405 	P2P_ROLE_DISABLE = 0,
406 	P2P_ROLE_DEVICE = 1,
407 	P2P_ROLE_CLIENT = 2,
408 	P2P_ROLE_GO = 3
409 };
410 
411 enum p2p_state {
412 	P2P_STATE_NONE = 0,							/* 	P2P disable */
413 	P2P_STATE_IDLE = 1,								/* 	P2P had enabled and do nothing */
414 	P2P_STATE_LISTEN = 2,							/* 	In pure listen state */
415 	P2P_STATE_SCAN = 3,							/* 	In scan phase */
416 	P2P_STATE_FIND_PHASE_LISTEN = 4,				/* 	In the listen state of find phase */
417 	P2P_STATE_FIND_PHASE_SEARCH = 5,				/* 	In the search state of find phase */
418 	P2P_STATE_TX_PROVISION_DIS_REQ = 6,			/* 	In P2P provisioning discovery */
419 	P2P_STATE_RX_PROVISION_DIS_RSP = 7,
420 	P2P_STATE_RX_PROVISION_DIS_REQ = 8,
421 	P2P_STATE_GONEGO_ING = 9,						/* 	Doing the group owner negotiation handshake */
422 	P2P_STATE_GONEGO_OK = 10,						/* 	finish the group negotiation handshake with success */
423 	P2P_STATE_GONEGO_FAIL = 11,					/* 	finish the group negotiation handshake with failure */
424 	P2P_STATE_RECV_INVITE_REQ_MATCH = 12,		/* 	receiving the P2P Invitation request and match with the profile. */
425 	P2P_STATE_PROVISIONING_ING = 13,				/* 	Doing the P2P WPS */
426 	P2P_STATE_PROVISIONING_DONE = 14,			/* 	Finish the P2P WPS */
427 	P2P_STATE_TX_INVITE_REQ = 15,					/* 	Transmit the P2P Invitation request */
428 	P2P_STATE_RX_INVITE_RESP_OK = 16,				/* 	Receiving the P2P Invitation response */
429 	P2P_STATE_RECV_INVITE_REQ_DISMATCH = 17,	/* 	receiving the P2P Invitation request and mismatch with the profile. */
430 	P2P_STATE_RECV_INVITE_REQ_GO = 18,			/* 	receiving the P2P Invitation request and this wifi is GO. */
431 	P2P_STATE_RECV_INVITE_REQ_JOIN = 19,			/* 	receiving the P2P Invitation request to join an existing P2P Group. */
432 	P2P_STATE_RX_INVITE_RESP_FAIL = 20,			/* 	recveing the P2P Invitation response with failure */
433 	P2P_STATE_RX_INFOR_NOREADY = 21,			/*  receiving p2p negotiation response with information is not available */
434 	P2P_STATE_TX_INFOR_NOREADY = 22,			/*  sending p2p negotiation response with information is not available */
435 };
436 
437 enum p2p_wpsinfo {
438 	P2P_NO_WPSINFO						= 0,
439 	P2P_GOT_WPSINFO_PEER_DISPLAY_PIN	= 1,
440 	P2P_GOT_WPSINFO_SELF_DISPLAY_PIN	= 2,
441 	P2P_GOT_WPSINFO_PBC					= 3,
442 };
443 
444 #define IP_MCAST_MAC(mac)		((mac[0] == 0x01) && (mac[1] == 0x00) && (mac[2] == 0x5e))
445 #define ICMPV6_MCAST_MAC(mac)	((mac[0] == 0x33) && (mac[1] == 0x33) && (mac[2] != 0xff))
446 
447 /* Regulatroy Domain */
448 struct regd_pair_mapping {
449 	u16 reg_dmnenum;
450 	u16 reg_2ghz_ctl;
451 };
452 
453 struct rtw_regulatory {
454 	char alpha2[2];
455 	u16 country_code;
456 	u16 max_power_level;
457 	u32 tp_scale;
458 	u16 current_rd;
459 	u16 current_rd_ext;
460 	int16_t power_limit;
461 	struct regd_pair_mapping *regpair;
462 };
463 
464 #endif /*  _WIFI_H_ */
465