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