1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../usb.h"
6 #include "../ps.h"
7 #include "../base.h"
8 #include "reg.h"
9 #include "def.h"
10 #include "phy.h"
11 #include "rf.h"
12 #include "dm.h"
13 #include "mac.h"
14 #include "trx.h"
15 #include "../rtl8192c/fw_common.h"
16 
17 static int _ConfigVerTOutEP(struct ieee80211_hw *hw)
18 {
19 	u8 ep_cfg, txqsele;
20 	u8 ep_nums = 0;
21 
22 	struct rtl_priv *rtlpriv = rtl_priv(hw);
23 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
24 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
25 
26 	rtlusb->out_queue_sel = 0;
27 	ep_cfg = rtl_read_byte(rtlpriv, REG_TEST_SIE_OPTIONAL);
28 	ep_cfg = (ep_cfg & USB_TEST_EP_MASK) >> USB_TEST_EP_SHIFT;
29 	switch (ep_cfg)	{
30 	case 0:		/* 2 bulk OUT, 1 bulk IN */
31 	case 3:
32 		rtlusb->out_queue_sel  = TX_SELE_HQ | TX_SELE_LQ;
33 		ep_nums = 2;
34 		break;
35 	case 1:	/* 1 bulk IN/OUT => map all endpoint to Low queue */
36 	case 2:	/* 1 bulk IN, 1 bulk OUT => map all endpoint to High queue */
37 		txqsele = rtl_read_byte(rtlpriv, REG_TEST_USB_TXQS);
38 		if (txqsele & 0x0F) /* /map all endpoint to High queue */
39 			rtlusb->out_queue_sel =  TX_SELE_HQ;
40 		else if (txqsele&0xF0) /* map all endpoint to Low queue */
41 			rtlusb->out_queue_sel =  TX_SELE_LQ;
42 		ep_nums = 1;
43 		break;
44 	default:
45 		break;
46 	}
47 	return (rtlusb->out_ep_nums == ep_nums) ? 0 : -EINVAL;
48 }
49 
50 static int _ConfigVerNOutEP(struct ieee80211_hw *hw)
51 {
52 	u8 ep_cfg;
53 	u8 ep_nums = 0;
54 
55 	struct rtl_priv *rtlpriv = rtl_priv(hw);
56 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
57 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
58 
59 	rtlusb->out_queue_sel = 0;
60 	/* Normal and High queue */
61 	ep_cfg =  rtl_read_byte(rtlpriv, (REG_NORMAL_SIE_EP + 1));
62 	if (ep_cfg & USB_NORMAL_SIE_EP_MASK) {
63 		rtlusb->out_queue_sel |= TX_SELE_HQ;
64 		ep_nums++;
65 	}
66 	if ((ep_cfg >> USB_NORMAL_SIE_EP_SHIFT) & USB_NORMAL_SIE_EP_MASK) {
67 		rtlusb->out_queue_sel |= TX_SELE_NQ;
68 		ep_nums++;
69 	}
70 	/* Low queue */
71 	ep_cfg =  rtl_read_byte(rtlpriv, (REG_NORMAL_SIE_EP + 2));
72 	if (ep_cfg & USB_NORMAL_SIE_EP_MASK) {
73 		rtlusb->out_queue_sel |= TX_SELE_LQ;
74 		ep_nums++;
75 	}
76 	return (rtlusb->out_ep_nums == ep_nums) ? 0 : -EINVAL;
77 }
78 
79 static void _TwoOutEpMapping(struct ieee80211_hw *hw, bool bIsChipB,
80 			     bool  bwificfg, struct rtl_ep_map *ep_map)
81 {
82 	struct rtl_priv *rtlpriv = rtl_priv(hw);
83 
84 	if (bwificfg) { /* for WMM */
85 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
86 			 "USB Chip-B & WMM Setting.....\n");
87 		ep_map->ep_mapping[RTL_TXQ_BE]	= 2;
88 		ep_map->ep_mapping[RTL_TXQ_BK]	= 3;
89 		ep_map->ep_mapping[RTL_TXQ_VI]	= 3;
90 		ep_map->ep_mapping[RTL_TXQ_VO] = 2;
91 		ep_map->ep_mapping[RTL_TXQ_MGT] = 2;
92 		ep_map->ep_mapping[RTL_TXQ_BCN] = 2;
93 		ep_map->ep_mapping[RTL_TXQ_HI]	= 2;
94 	} else { /* typical setting */
95 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
96 			 "USB typical Setting.....\n");
97 		ep_map->ep_mapping[RTL_TXQ_BE]	= 3;
98 		ep_map->ep_mapping[RTL_TXQ_BK]	= 3;
99 		ep_map->ep_mapping[RTL_TXQ_VI]	= 2;
100 		ep_map->ep_mapping[RTL_TXQ_VO]	= 2;
101 		ep_map->ep_mapping[RTL_TXQ_MGT] = 2;
102 		ep_map->ep_mapping[RTL_TXQ_BCN] = 2;
103 		ep_map->ep_mapping[RTL_TXQ_HI]	= 2;
104 	}
105 }
106 
107 static void _ThreeOutEpMapping(struct ieee80211_hw *hw, bool  bwificfg,
108 			       struct rtl_ep_map *ep_map)
109 {
110 	struct rtl_priv *rtlpriv = rtl_priv(hw);
111 	if (bwificfg) { /* for WMM */
112 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
113 			 "USB 3EP Setting for WMM.....\n");
114 		ep_map->ep_mapping[RTL_TXQ_BE]	= 5;
115 		ep_map->ep_mapping[RTL_TXQ_BK]	= 3;
116 		ep_map->ep_mapping[RTL_TXQ_VI]	= 3;
117 		ep_map->ep_mapping[RTL_TXQ_VO]	= 2;
118 		ep_map->ep_mapping[RTL_TXQ_MGT] = 2;
119 		ep_map->ep_mapping[RTL_TXQ_BCN] = 2;
120 		ep_map->ep_mapping[RTL_TXQ_HI]	= 2;
121 	} else { /* typical setting */
122 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
123 			 "USB 3EP Setting for typical.....\n");
124 		ep_map->ep_mapping[RTL_TXQ_BE]	= 5;
125 		ep_map->ep_mapping[RTL_TXQ_BK]	= 5;
126 		ep_map->ep_mapping[RTL_TXQ_VI]	= 3;
127 		ep_map->ep_mapping[RTL_TXQ_VO]	= 2;
128 		ep_map->ep_mapping[RTL_TXQ_MGT] = 2;
129 		ep_map->ep_mapping[RTL_TXQ_BCN] = 2;
130 		ep_map->ep_mapping[RTL_TXQ_HI]	= 2;
131 	}
132 }
133 
134 static void _OneOutEpMapping(struct ieee80211_hw *hw, struct rtl_ep_map *ep_map)
135 {
136 	ep_map->ep_mapping[RTL_TXQ_BE]	= 2;
137 	ep_map->ep_mapping[RTL_TXQ_BK]	= 2;
138 	ep_map->ep_mapping[RTL_TXQ_VI]	= 2;
139 	ep_map->ep_mapping[RTL_TXQ_VO] = 2;
140 	ep_map->ep_mapping[RTL_TXQ_MGT] = 2;
141 	ep_map->ep_mapping[RTL_TXQ_BCN] = 2;
142 	ep_map->ep_mapping[RTL_TXQ_HI]	= 2;
143 }
144 static int _out_ep_mapping(struct ieee80211_hw *hw)
145 {
146 	int err = 0;
147 	bool bIsChipN, bwificfg = false;
148 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
149 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
150 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
151 	struct rtl_ep_map *ep_map = &(rtlusb->ep_map);
152 
153 	bIsChipN = IS_NORMAL_CHIP(rtlhal->version);
154 	switch (rtlusb->out_ep_nums) {
155 	case 2:
156 		_TwoOutEpMapping(hw, bIsChipN, bwificfg, ep_map);
157 		break;
158 	case 3:
159 		/* Test chip doesn't support three out EPs. */
160 		if (!bIsChipN) {
161 			err  =  -EINVAL;
162 			goto err_out;
163 		}
164 		_ThreeOutEpMapping(hw, bIsChipN, ep_map);
165 		break;
166 	case 1:
167 		_OneOutEpMapping(hw, ep_map);
168 		break;
169 	default:
170 		err  =  -EINVAL;
171 		break;
172 	}
173 err_out:
174 	return err;
175 
176 }
177 /* endpoint mapping */
178 int  rtl8192cu_endpoint_mapping(struct ieee80211_hw *hw)
179 {
180 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
181 	int error = 0;
182 	if (likely(IS_NORMAL_CHIP(rtlhal->version)))
183 		error = _ConfigVerNOutEP(hw);
184 	else
185 		error = _ConfigVerTOutEP(hw);
186 	if (error)
187 		goto err_out;
188 	error = _out_ep_mapping(hw);
189 	if (error)
190 		goto err_out;
191 err_out:
192 	return error;
193 }
194 
195 u16 rtl8192cu_mq_to_hwq(__le16 fc, u16 mac80211_queue_index)
196 {
197 	u16 hw_queue_index;
198 
199 	if (unlikely(ieee80211_is_beacon(fc))) {
200 		hw_queue_index = RTL_TXQ_BCN;
201 		goto out;
202 	}
203 	if (ieee80211_is_mgmt(fc)) {
204 		hw_queue_index = RTL_TXQ_MGT;
205 		goto out;
206 	}
207 	switch (mac80211_queue_index) {
208 	case 0:
209 		hw_queue_index = RTL_TXQ_VO;
210 		break;
211 	case 1:
212 		hw_queue_index = RTL_TXQ_VI;
213 		break;
214 	case 2:
215 		hw_queue_index = RTL_TXQ_BE;
216 		break;
217 	case 3:
218 		hw_queue_index = RTL_TXQ_BK;
219 		break;
220 	default:
221 		hw_queue_index = RTL_TXQ_BE;
222 		WARN_ONCE(true, "rtl8192cu: QSLT_BE queue, skb_queue:%d\n",
223 			  mac80211_queue_index);
224 		break;
225 	}
226 out:
227 	return hw_queue_index;
228 }
229 
230 static enum rtl_desc_qsel _rtl8192cu_mq_to_descq(struct ieee80211_hw *hw,
231 					 __le16 fc, u16 mac80211_queue_index)
232 {
233 	enum rtl_desc_qsel qsel;
234 	struct rtl_priv *rtlpriv = rtl_priv(hw);
235 
236 	if (unlikely(ieee80211_is_beacon(fc))) {
237 		qsel = QSLT_BEACON;
238 		goto out;
239 	}
240 	if (ieee80211_is_mgmt(fc)) {
241 		qsel = QSLT_MGNT;
242 		goto out;
243 	}
244 	switch (mac80211_queue_index) {
245 	case 0:	/* VO */
246 		qsel = QSLT_VO;
247 		RT_TRACE(rtlpriv, COMP_USB, DBG_DMESG,
248 			 "VO queue, set qsel = 0x%x\n", QSLT_VO);
249 		break;
250 	case 1:	/* VI */
251 		qsel = QSLT_VI;
252 		RT_TRACE(rtlpriv, COMP_USB, DBG_DMESG,
253 			 "VI queue, set qsel = 0x%x\n", QSLT_VI);
254 		break;
255 	case 3:	/* BK */
256 		qsel = QSLT_BK;
257 		RT_TRACE(rtlpriv, COMP_USB, DBG_DMESG,
258 			 "BK queue, set qsel = 0x%x\n", QSLT_BK);
259 		break;
260 	case 2:	/* BE */
261 	default:
262 		qsel = QSLT_BE;
263 		RT_TRACE(rtlpriv, COMP_USB, DBG_DMESG,
264 			 "BE queue, set qsel = 0x%x\n", QSLT_BE);
265 		break;
266 	}
267 out:
268 	return qsel;
269 }
270 
271 /* =============================================================== */
272 
273 /*----------------------------------------------------------------------
274  *
275  *	Rx handler
276  *
277  *---------------------------------------------------------------------- */
278 bool rtl92cu_rx_query_desc(struct ieee80211_hw *hw,
279 			   struct rtl_stats *stats,
280 			   struct ieee80211_rx_status *rx_status,
281 			   u8 *pdesc, struct sk_buff *skb)
282 {
283 	struct rx_fwinfo_92c *p_drvinfo;
284 	struct rx_desc_92c *p_desc = (struct rx_desc_92c *)pdesc;
285 	u32 phystatus = GET_RX_DESC_PHY_STATUS(pdesc);
286 
287 	stats->length = (u16) GET_RX_DESC_PKT_LEN(pdesc);
288 	stats->rx_drvinfo_size = (u8)GET_RX_DESC_DRVINFO_SIZE(pdesc) *
289 				 RX_DRV_INFO_SIZE_UNIT;
290 	stats->rx_bufshift = (u8) (GET_RX_DESC_SHIFT(pdesc) & 0x03);
291 	stats->icv = (u16) GET_RX_DESC_ICV(pdesc);
292 	stats->crc = (u16) GET_RX_DESC_CRC32(pdesc);
293 	stats->hwerror = (stats->crc | stats->icv);
294 	stats->decrypted = !GET_RX_DESC_SWDEC(pdesc);
295 	stats->rate = (u8) GET_RX_DESC_RX_MCS(pdesc);
296 	stats->shortpreamble = (u16) GET_RX_DESC_SPLCP(pdesc);
297 	stats->isampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
298 	stats->isfirst_ampdu = (bool)((GET_RX_DESC_PAGGR(pdesc) == 1)
299 				   && (GET_RX_DESC_FAGGR(pdesc) == 1));
300 	stats->timestamp_low = GET_RX_DESC_TSFL(pdesc);
301 	stats->rx_is40Mhzpacket = (bool) GET_RX_DESC_BW(pdesc);
302 	stats->is_ht = (bool)GET_RX_DESC_RX_HT(pdesc);
303 	rx_status->freq = hw->conf.chandef.chan->center_freq;
304 	rx_status->band = hw->conf.chandef.chan->band;
305 	if (GET_RX_DESC_CRC32(pdesc))
306 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
307 	if (!GET_RX_DESC_SWDEC(pdesc))
308 		rx_status->flag |= RX_FLAG_DECRYPTED;
309 	if (GET_RX_DESC_BW(pdesc))
310 		rx_status->bw = RATE_INFO_BW_40;
311 	if (GET_RX_DESC_RX_HT(pdesc))
312 		rx_status->encoding = RX_ENC_HT;
313 	rx_status->flag |= RX_FLAG_MACTIME_START;
314 	if (stats->decrypted)
315 		rx_status->flag |= RX_FLAG_DECRYPTED;
316 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, stats->is_ht,
317 						   false, stats->rate);
318 	rx_status->mactime = GET_RX_DESC_TSFL(pdesc);
319 	if (phystatus) {
320 		p_drvinfo = (struct rx_fwinfo_92c *)(skb->data +
321 						     stats->rx_bufshift);
322 		rtl92c_translate_rx_signal_stuff(hw, skb, stats, p_desc,
323 						 p_drvinfo);
324 	}
325 	/*rx_status->qual = stats->signal; */
326 	rx_status->signal = stats->recvsignalpower + 10;
327 	return true;
328 }
329 
330 #define RTL_RX_DRV_INFO_UNIT		8
331 
332 static void _rtl_rx_process(struct ieee80211_hw *hw, struct sk_buff *skb)
333 {
334 	struct ieee80211_rx_status *rx_status =
335 		 (struct ieee80211_rx_status *)IEEE80211_SKB_RXCB(skb);
336 	u32 skb_len, pkt_len, drvinfo_len;
337 	struct rtl_priv *rtlpriv = rtl_priv(hw);
338 	u8 *rxdesc;
339 	struct rtl_stats stats = {
340 		.signal = 0,
341 		.rate = 0,
342 	};
343 	struct rx_fwinfo_92c *p_drvinfo;
344 	bool bv;
345 	__le16 fc;
346 	struct ieee80211_hdr *hdr;
347 
348 	memset(rx_status, 0, sizeof(*rx_status));
349 	rxdesc	= skb->data;
350 	skb_len	= skb->len;
351 	drvinfo_len = (GET_RX_DESC_DRVINFO_SIZE(rxdesc) * RTL_RX_DRV_INFO_UNIT);
352 	pkt_len		= GET_RX_DESC_PKT_LEN(rxdesc);
353 	/* TODO: Error recovery. drop this skb or something. */
354 	WARN_ON(skb_len < (pkt_len + RTL_RX_DESC_SIZE + drvinfo_len));
355 	stats.length = (u16) GET_RX_DESC_PKT_LEN(rxdesc);
356 	stats.rx_drvinfo_size = (u8)GET_RX_DESC_DRVINFO_SIZE(rxdesc) *
357 				RX_DRV_INFO_SIZE_UNIT;
358 	stats.rx_bufshift = (u8) (GET_RX_DESC_SHIFT(rxdesc) & 0x03);
359 	stats.icv = (u16) GET_RX_DESC_ICV(rxdesc);
360 	stats.crc = (u16) GET_RX_DESC_CRC32(rxdesc);
361 	stats.hwerror = (stats.crc | stats.icv);
362 	stats.decrypted = !GET_RX_DESC_SWDEC(rxdesc);
363 	stats.rate = (u8) GET_RX_DESC_RX_MCS(rxdesc);
364 	stats.shortpreamble = (u16) GET_RX_DESC_SPLCP(rxdesc);
365 	stats.isampdu = (bool) ((GET_RX_DESC_PAGGR(rxdesc) == 1)
366 				   && (GET_RX_DESC_FAGGR(rxdesc) == 1));
367 	stats.timestamp_low = GET_RX_DESC_TSFL(rxdesc);
368 	stats.rx_is40Mhzpacket = (bool) GET_RX_DESC_BW(rxdesc);
369 	stats.is_ht = (bool)GET_RX_DESC_RX_HT(rxdesc);
370 	/* TODO: is center_freq changed when doing scan? */
371 	/* TODO: Shall we add protection or just skip those two step? */
372 	rx_status->freq = hw->conf.chandef.chan->center_freq;
373 	rx_status->band = hw->conf.chandef.chan->band;
374 	if (GET_RX_DESC_CRC32(rxdesc))
375 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
376 	if (!GET_RX_DESC_SWDEC(rxdesc))
377 		rx_status->flag |= RX_FLAG_DECRYPTED;
378 	if (GET_RX_DESC_BW(rxdesc))
379 		rx_status->bw = RATE_INFO_BW_40;
380 	if (GET_RX_DESC_RX_HT(rxdesc))
381 		rx_status->encoding = RX_ENC_HT;
382 	/* Data rate */
383 	rx_status->rate_idx = rtlwifi_rate_mapping(hw, stats.is_ht,
384 						   false, stats.rate);
385 	/*  There is a phy status after this rx descriptor. */
386 	if (GET_RX_DESC_PHY_STATUS(rxdesc)) {
387 		p_drvinfo = (struct rx_fwinfo_92c *)(rxdesc + RTL_RX_DESC_SIZE);
388 		rtl92c_translate_rx_signal_stuff(hw, skb, &stats,
389 				 (struct rx_desc_92c *)rxdesc, p_drvinfo);
390 	}
391 	skb_pull(skb, (drvinfo_len + RTL_RX_DESC_SIZE));
392 	hdr = (struct ieee80211_hdr *)(skb->data);
393 	fc = hdr->frame_control;
394 	bv = ieee80211_is_probe_resp(fc);
395 	if (bv)
396 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
397 			 "Got probe response frame\n");
398 	if (ieee80211_is_beacon(fc))
399 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Got beacon frame\n");
400 	if (ieee80211_is_data(fc))
401 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Got data frame\n");
402 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
403 		 "Fram: fc = 0x%X addr1 = 0x%02X:0x%02X:0x%02X:0x%02X:0x%02X:0x%02X\n",
404 		 fc,
405 		 (u32)hdr->addr1[0], (u32)hdr->addr1[1],
406 		 (u32)hdr->addr1[2], (u32)hdr->addr1[3],
407 		 (u32)hdr->addr1[4], (u32)hdr->addr1[5]);
408 	ieee80211_rx(hw, skb);
409 }
410 
411 void  rtl8192cu_rx_hdl(struct ieee80211_hw *hw, struct sk_buff * skb)
412 {
413 	_rtl_rx_process(hw, skb);
414 }
415 
416 /*----------------------------------------------------------------------
417  *
418  *	Tx handler
419  *
420  *---------------------------------------------------------------------- */
421 void rtl8192c_tx_cleanup(struct ieee80211_hw *hw, struct sk_buff  *skb)
422 {
423 }
424 
425 int rtl8192c_tx_post_hdl(struct ieee80211_hw *hw, struct urb *urb,
426 			 struct sk_buff *skb)
427 {
428 	return 0;
429 }
430 
431 struct sk_buff *rtl8192c_tx_aggregate_hdl(struct ieee80211_hw *hw,
432 					   struct sk_buff_head *list)
433 {
434 	return skb_dequeue(list);
435 }
436 
437 /*======================================== trx ===============================*/
438 
439 static void _rtl_fill_usb_tx_desc(u8 *txdesc)
440 {
441 	SET_TX_DESC_OWN(txdesc, 1);
442 	SET_TX_DESC_LAST_SEG(txdesc, 1);
443 	SET_TX_DESC_FIRST_SEG(txdesc, 1);
444 }
445 /**
446  *	For HW recovery information
447  */
448 static void _rtl_tx_desc_checksum(u8 *txdesc)
449 {
450 	__le16 *ptr = (__le16 *)txdesc;
451 	u16	checksum = 0;
452 	u32 index;
453 
454 	/* Clear first */
455 	SET_TX_DESC_TX_DESC_CHECKSUM(txdesc, 0);
456 	for (index = 0; index < 16; index++)
457 		checksum = checksum ^ le16_to_cpu(*(ptr + index));
458 	SET_TX_DESC_TX_DESC_CHECKSUM(txdesc, checksum);
459 }
460 
461 void rtl92cu_tx_fill_desc(struct ieee80211_hw *hw,
462 			  struct ieee80211_hdr *hdr, u8 *pdesc_tx,
463 			  u8 *pbd_desc_tx, struct ieee80211_tx_info *info,
464 			  struct ieee80211_sta *sta,
465 			  struct sk_buff *skb,
466 			  u8 queue_index,
467 			  struct rtl_tcb_desc *tcb_desc)
468 {
469 	struct rtl_priv *rtlpriv = rtl_priv(hw);
470 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
471 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
472 	bool defaultadapter = true;
473 	u8 *qc = ieee80211_get_qos_ctl(hdr);
474 	u8 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
475 	u16 seq_number;
476 	__le16 fc = hdr->frame_control;
477 	u8 rate_flag = info->control.rates[0].flags;
478 	u16 pktlen = skb->len;
479 	enum rtl_desc_qsel fw_qsel = _rtl8192cu_mq_to_descq(hw, fc,
480 						skb_get_queue_mapping(skb));
481 	u8 *txdesc;
482 
483 	seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
484 	rtl_get_tcb_desc(hw, info, sta, skb, tcb_desc);
485 	txdesc = skb_push(skb, RTL_TX_HEADER_SIZE);
486 	memset(txdesc, 0, RTL_TX_HEADER_SIZE);
487 	SET_TX_DESC_PKT_SIZE(txdesc, pktlen);
488 	SET_TX_DESC_LINIP(txdesc, 0);
489 	SET_TX_DESC_PKT_OFFSET(txdesc, RTL_DUMMY_OFFSET);
490 	SET_TX_DESC_OFFSET(txdesc, RTL_TX_HEADER_SIZE);
491 	SET_TX_DESC_TX_RATE(txdesc, tcb_desc->hw_rate);
492 	if (tcb_desc->use_shortgi || tcb_desc->use_shortpreamble)
493 		SET_TX_DESC_DATA_SHORTGI(txdesc, 1);
494 	if (mac->tids[tid].agg.agg_state == RTL_AGG_ON &&
495 		    info->flags & IEEE80211_TX_CTL_AMPDU) {
496 		SET_TX_DESC_AGG_ENABLE(txdesc, 1);
497 		SET_TX_DESC_MAX_AGG_NUM(txdesc, 0x14);
498 	} else {
499 		SET_TX_DESC_AGG_BREAK(txdesc, 1);
500 	}
501 	SET_TX_DESC_SEQ(txdesc, seq_number);
502 	SET_TX_DESC_RTS_ENABLE(txdesc, ((tcb_desc->rts_enable &&
503 			       !tcb_desc->cts_enable) ? 1 : 0));
504 	SET_TX_DESC_HW_RTS_ENABLE(txdesc, ((tcb_desc->rts_enable ||
505 				  tcb_desc->cts_enable) ? 1 : 0));
506 	SET_TX_DESC_CTS2SELF(txdesc, ((tcb_desc->cts_enable) ? 1 : 0));
507 	SET_TX_DESC_RTS_STBC(txdesc, ((tcb_desc->rts_stbc) ? 1 : 0));
508 	SET_TX_DESC_RTS_RATE(txdesc, tcb_desc->rts_rate);
509 	SET_TX_DESC_RTS_BW(txdesc, 0);
510 	SET_TX_DESC_RTS_SC(txdesc, tcb_desc->rts_sc);
511 	SET_TX_DESC_RTS_SHORT(txdesc,
512 			      ((tcb_desc->rts_rate <= DESC_RATE54M) ?
513 			       (tcb_desc->rts_use_shortpreamble ? 1 : 0)
514 			       : (tcb_desc->rts_use_shortgi ? 1 : 0)));
515 	if (mac->bw_40) {
516 		if (rate_flag & IEEE80211_TX_RC_DUP_DATA) {
517 			SET_TX_DESC_DATA_BW(txdesc, 1);
518 			SET_TX_DESC_DATA_SC(txdesc, 3);
519 		} else if(rate_flag & IEEE80211_TX_RC_40_MHZ_WIDTH){
520 			SET_TX_DESC_DATA_BW(txdesc, 1);
521 			SET_TX_DESC_DATA_SC(txdesc, mac->cur_40_prime_sc);
522 		} else {
523 			SET_TX_DESC_DATA_BW(txdesc, 0);
524 			SET_TX_DESC_DATA_SC(txdesc, 0);
525 		}
526 	} else {
527 		SET_TX_DESC_DATA_BW(txdesc, 0);
528 		SET_TX_DESC_DATA_SC(txdesc, 0);
529 	}
530 	rcu_read_lock();
531 	sta = ieee80211_find_sta(mac->vif, mac->bssid);
532 	if (sta) {
533 		u8 ampdu_density = sta->ht_cap.ampdu_density;
534 		SET_TX_DESC_AMPDU_DENSITY(txdesc, ampdu_density);
535 	}
536 	rcu_read_unlock();
537 	if (info->control.hw_key) {
538 		struct ieee80211_key_conf *keyconf = info->control.hw_key;
539 		switch (keyconf->cipher) {
540 		case WLAN_CIPHER_SUITE_WEP40:
541 		case WLAN_CIPHER_SUITE_WEP104:
542 		case WLAN_CIPHER_SUITE_TKIP:
543 			SET_TX_DESC_SEC_TYPE(txdesc, 0x1);
544 			break;
545 		case WLAN_CIPHER_SUITE_CCMP:
546 			SET_TX_DESC_SEC_TYPE(txdesc, 0x3);
547 			break;
548 		default:
549 			SET_TX_DESC_SEC_TYPE(txdesc, 0x0);
550 			break;
551 		}
552 	}
553 	SET_TX_DESC_PKT_ID(txdesc, 0);
554 	SET_TX_DESC_QUEUE_SEL(txdesc, fw_qsel);
555 	SET_TX_DESC_DATA_RATE_FB_LIMIT(txdesc, 0x1F);
556 	SET_TX_DESC_RTS_RATE_FB_LIMIT(txdesc, 0xF);
557 	SET_TX_DESC_DISABLE_FB(txdesc, 0);
558 	SET_TX_DESC_USE_RATE(txdesc, tcb_desc->use_driver_rate ? 1 : 0);
559 	if (ieee80211_is_data_qos(fc)) {
560 		if (mac->rdg_en) {
561 			RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
562 				 "Enable RDG function\n");
563 			SET_TX_DESC_RDG_ENABLE(txdesc, 1);
564 			SET_TX_DESC_HTC(txdesc, 1);
565 		}
566 	}
567 	if (rtlpriv->dm.useramask) {
568 		SET_TX_DESC_RATE_ID(txdesc, tcb_desc->ratr_index);
569 		SET_TX_DESC_MACID(txdesc, tcb_desc->mac_id);
570 	} else {
571 		SET_TX_DESC_RATE_ID(txdesc, 0xC + tcb_desc->ratr_index);
572 		SET_TX_DESC_MACID(txdesc, tcb_desc->ratr_index);
573 	}
574 	if ((!ieee80211_is_data_qos(fc)) && ppsc->leisure_ps &&
575 	      ppsc->fwctrl_lps) {
576 		SET_TX_DESC_HWSEQ_EN(txdesc, 1);
577 		SET_TX_DESC_PKT_ID(txdesc, 8);
578 		if (!defaultadapter)
579 			SET_TX_DESC_QOS(txdesc, 1);
580 	}
581 	if (ieee80211_has_morefrags(fc))
582 		SET_TX_DESC_MORE_FRAG(txdesc, 1);
583 	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
584 	    is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
585 		SET_TX_DESC_BMC(txdesc, 1);
586 	_rtl_fill_usb_tx_desc(txdesc);
587 	_rtl_tx_desc_checksum(txdesc);
588 	RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE, "==>\n");
589 }
590 
591 void rtl92cu_fill_fake_txdesc(struct ieee80211_hw *hw, u8 * pDesc,
592 			      u32 buffer_len, bool bIsPsPoll)
593 {
594 	/* Clear all status */
595 	memset(pDesc, 0, RTL_TX_HEADER_SIZE);
596 	SET_TX_DESC_FIRST_SEG(pDesc, 1); /* bFirstSeg; */
597 	SET_TX_DESC_LAST_SEG(pDesc, 1); /* bLastSeg; */
598 	SET_TX_DESC_OFFSET(pDesc, RTL_TX_HEADER_SIZE); /* Offset = 32 */
599 	SET_TX_DESC_PKT_SIZE(pDesc, buffer_len); /* Buffer size + command hdr */
600 	SET_TX_DESC_QUEUE_SEL(pDesc, QSLT_MGNT); /* Fixed queue of Mgnt queue */
601 	/* Set NAVUSEHDR to prevent Ps-poll AId filed to be changed to error
602 	 * vlaue by Hw. */
603 	if (bIsPsPoll) {
604 		SET_TX_DESC_NAV_USE_HDR(pDesc, 1);
605 	} else {
606 		SET_TX_DESC_HWSEQ_EN(pDesc, 1); /* Hw set sequence number */
607 		SET_TX_DESC_PKT_ID(pDesc, 0x100); /* set bit3 to 1. */
608 	}
609 	SET_TX_DESC_USE_RATE(pDesc, 1); /* use data rate which is set by Sw */
610 	SET_TX_DESC_OWN(pDesc, 1);
611 	SET_TX_DESC_TX_RATE(pDesc, DESC_RATE1M);
612 	_rtl_tx_desc_checksum(pDesc);
613 }
614 
615 void rtl92cu_tx_fill_cmddesc(struct ieee80211_hw *hw,
616 			     u8 *pdesc, bool firstseg,
617 			     bool lastseg, struct sk_buff *skb)
618 {
619 	struct rtl_priv *rtlpriv = rtl_priv(hw);
620 	u8 fw_queue = QSLT_BEACON;
621 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
622 	__le16 fc = hdr->frame_control;
623 
624 	memset((void *)pdesc, 0, RTL_TX_HEADER_SIZE);
625 	if (firstseg)
626 		SET_TX_DESC_OFFSET(pdesc, RTL_TX_HEADER_SIZE);
627 	SET_TX_DESC_TX_RATE(pdesc, DESC_RATE1M);
628 	SET_TX_DESC_SEQ(pdesc, 0);
629 	SET_TX_DESC_LINIP(pdesc, 0);
630 	SET_TX_DESC_QUEUE_SEL(pdesc, fw_queue);
631 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
632 	SET_TX_DESC_LAST_SEG(pdesc, 1);
633 	SET_TX_DESC_RATE_ID(pdesc, 7);
634 	SET_TX_DESC_MACID(pdesc, 0);
635 	SET_TX_DESC_OWN(pdesc, 1);
636 	SET_TX_DESC_PKT_SIZE(pdesc, (u16)skb->len);
637 	SET_TX_DESC_FIRST_SEG(pdesc, 1);
638 	SET_TX_DESC_LAST_SEG(pdesc, 1);
639 	SET_TX_DESC_OFFSET(pdesc, 0x20);
640 	SET_TX_DESC_USE_RATE(pdesc, 1);
641 	if (!ieee80211_is_data_qos(fc)) {
642 		SET_TX_DESC_HWSEQ_EN(pdesc, 1);
643 		SET_TX_DESC_PKT_ID(pdesc, 8);
644 	}
645 	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD, "H2C Tx Cmd Content",
646 		      pdesc, RTL_TX_DESC_SIZE);
647 }
648