xref: /linux/drivers/net/wireless/realtek/rtl8xxxu/core.c (revision 2f77c66694108785a1f6ae6c5de06453e7f7a91f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RTL8XXXU mac80211 USB driver
4  *
5  * Copyright (c) 2014 - 2017 Jes Sorensen <Jes.Sorensen@gmail.com>
6  *
7  * Portions, notably calibration code:
8  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
9  *
10  * This driver was written as a replacement for the vendor provided
11  * rtl8723au driver. As the Realtek 8xxx chips are very similar in
12  * their programming interface, I have started adding support for
13  * additional 8xxx chips like the 8192cu, 8188cus, etc.
14  */
15 
16 #include <linux/firmware.h>
17 #include <linux/iopoll.h>
18 #include "regs.h"
19 #include "rtl8xxxu.h"
20 
21 #define DRIVER_NAME "rtl8xxxu"
22 
23 int rtl8xxxu_debug;
24 static bool rtl8xxxu_dma_aggregation;
25 static int rtl8xxxu_dma_agg_timeout = -1;
26 static int rtl8xxxu_dma_agg_pages = -1;
27 
28 MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
29 MODULE_DESCRIPTION("RTL8XXXu USB mac80211 Wireless LAN Driver");
30 MODULE_LICENSE("GPL");
31 MODULE_FIRMWARE("rtlwifi/rtl8723aufw_A.bin");
32 MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B.bin");
33 MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B_NoBT.bin");
34 MODULE_FIRMWARE("rtlwifi/rtl8188eufw.bin");
35 MODULE_FIRMWARE("rtlwifi/rtl8192cufw_A.bin");
36 MODULE_FIRMWARE("rtlwifi/rtl8192cufw_B.bin");
37 MODULE_FIRMWARE("rtlwifi/rtl8192cufw_TMSC.bin");
38 MODULE_FIRMWARE("rtlwifi/rtl8192eu_nic.bin");
39 MODULE_FIRMWARE("rtlwifi/rtl8723bu_nic.bin");
40 MODULE_FIRMWARE("rtlwifi/rtl8188fufw.bin");
41 MODULE_FIRMWARE("rtlwifi/rtl8710bufw_SMIC.bin");
42 MODULE_FIRMWARE("rtlwifi/rtl8710bufw_UMC.bin");
43 MODULE_FIRMWARE("rtlwifi/rtl8192fufw.bin");
44 
45 module_param_named(debug, rtl8xxxu_debug, int, 0600);
46 MODULE_PARM_DESC(debug, "Set debug mask");
47 module_param_named(dma_aggregation, rtl8xxxu_dma_aggregation, bool, 0600);
48 MODULE_PARM_DESC(dma_aggregation, "Enable DMA packet aggregation");
49 module_param_named(dma_agg_timeout, rtl8xxxu_dma_agg_timeout, int, 0600);
50 MODULE_PARM_DESC(dma_agg_timeout, "Set DMA aggregation timeout (range 1-127)");
51 module_param_named(dma_agg_pages, rtl8xxxu_dma_agg_pages, int, 0600);
52 MODULE_PARM_DESC(dma_agg_pages, "Set DMA aggregation pages (range 1-127, 0 to disable)");
53 
54 #define USB_VENDOR_ID_REALTEK		0x0bda
55 #define RTL8XXXU_RX_URBS		32
56 #define RTL8XXXU_RX_URB_PENDING_WATER	8
57 #define RTL8XXXU_RX_URB_RETRY_DELAY_MS	100
58 #define RTL8XXXU_TX_URBS		64
59 #define RTL8XXXU_TX_URB_LOW_WATER	25
60 #define RTL8XXXU_TX_URB_HIGH_WATER	32
61 
62 static void rtl8xxxu_stop(struct ieee80211_hw *hw, bool suspend);
63 static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
64 				  struct rtl8xxxu_rx_urb *rx_urb);
65 
66 static struct ieee80211_rate rtl8xxxu_rates[] = {
67 	{ .bitrate = 10, .hw_value = DESC_RATE_1M, .flags = 0 },
68 	{ .bitrate = 20, .hw_value = DESC_RATE_2M, .flags = 0 },
69 	{ .bitrate = 55, .hw_value = DESC_RATE_5_5M, .flags = 0 },
70 	{ .bitrate = 110, .hw_value = DESC_RATE_11M, .flags = 0 },
71 	{ .bitrate = 60, .hw_value = DESC_RATE_6M, .flags = 0 },
72 	{ .bitrate = 90, .hw_value = DESC_RATE_9M, .flags = 0 },
73 	{ .bitrate = 120, .hw_value = DESC_RATE_12M, .flags = 0 },
74 	{ .bitrate = 180, .hw_value = DESC_RATE_18M, .flags = 0 },
75 	{ .bitrate = 240, .hw_value = DESC_RATE_24M, .flags = 0 },
76 	{ .bitrate = 360, .hw_value = DESC_RATE_36M, .flags = 0 },
77 	{ .bitrate = 480, .hw_value = DESC_RATE_48M, .flags = 0 },
78 	{ .bitrate = 540, .hw_value = DESC_RATE_54M, .flags = 0 },
79 };
80 
81 static struct ieee80211_channel rtl8xxxu_channels_2g[] = {
82 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2412,
83 	  .hw_value = 1, .max_power = 30 },
84 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2417,
85 	  .hw_value = 2, .max_power = 30 },
86 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2422,
87 	  .hw_value = 3, .max_power = 30 },
88 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2427,
89 	  .hw_value = 4, .max_power = 30 },
90 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2432,
91 	  .hw_value = 5, .max_power = 30 },
92 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2437,
93 	  .hw_value = 6, .max_power = 30 },
94 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2442,
95 	  .hw_value = 7, .max_power = 30 },
96 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2447,
97 	  .hw_value = 8, .max_power = 30 },
98 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2452,
99 	  .hw_value = 9, .max_power = 30 },
100 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2457,
101 	  .hw_value = 10, .max_power = 30 },
102 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2462,
103 	  .hw_value = 11, .max_power = 30 },
104 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2467,
105 	  .hw_value = 12, .max_power = 30 },
106 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2472,
107 	  .hw_value = 13, .max_power = 30 },
108 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2484,
109 	  .hw_value = 14, .max_power = 30 }
110 };
111 
112 static struct ieee80211_supported_band rtl8xxxu_supported_band = {
113 	.channels = rtl8xxxu_channels_2g,
114 	.n_channels = ARRAY_SIZE(rtl8xxxu_channels_2g),
115 	.bitrates = rtl8xxxu_rates,
116 	.n_bitrates = ARRAY_SIZE(rtl8xxxu_rates),
117 };
118 
119 static const struct rtl8xxxu_reg32val rtl8723a_phy_1t_init_table[] = {
120 	{0x800, 0x80040000}, {0x804, 0x00000003},
121 	{0x808, 0x0000fc00}, {0x80c, 0x0000000a},
122 	{0x810, 0x10001331}, {0x814, 0x020c3d10},
123 	{0x818, 0x02200385}, {0x81c, 0x00000000},
124 	{0x820, 0x01000100}, {0x824, 0x00390004},
125 	{0x828, 0x00000000}, {0x82c, 0x00000000},
126 	{0x830, 0x00000000}, {0x834, 0x00000000},
127 	{0x838, 0x00000000}, {0x83c, 0x00000000},
128 	{0x840, 0x00010000}, {0x844, 0x00000000},
129 	{0x848, 0x00000000}, {0x84c, 0x00000000},
130 	{0x850, 0x00000000}, {0x854, 0x00000000},
131 	{0x858, 0x569a569a}, {0x85c, 0x001b25a4},
132 	{0x860, 0x66f60110}, {0x864, 0x061f0130},
133 	{0x868, 0x00000000}, {0x86c, 0x32323200},
134 	{0x870, 0x07000760}, {0x874, 0x22004000},
135 	{0x878, 0x00000808}, {0x87c, 0x00000000},
136 	{0x880, 0xc0083070}, {0x884, 0x000004d5},
137 	{0x888, 0x00000000}, {0x88c, 0xccc000c0},
138 	{0x890, 0x00000800}, {0x894, 0xfffffffe},
139 	{0x898, 0x40302010}, {0x89c, 0x00706050},
140 	{0x900, 0x00000000}, {0x904, 0x00000023},
141 	{0x908, 0x00000000}, {0x90c, 0x81121111},
142 	{0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
143 	{0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
144 	{0xa10, 0x9500bb78}, {0xa14, 0x11144028},
145 	{0xa18, 0x00881117}, {0xa1c, 0x89140f00},
146 	{0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
147 	{0xa28, 0x00000204}, {0xa2c, 0x00d30000},
148 	{0xa70, 0x101fbf00}, {0xa74, 0x00000007},
149 	{0xa78, 0x00000900},
150 	{0xc00, 0x48071d40}, {0xc04, 0x03a05611},
151 	{0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
152 	{0xc10, 0x08800000}, {0xc14, 0x40000100},
153 	{0xc18, 0x08800000}, {0xc1c, 0x40000100},
154 	{0xc20, 0x00000000}, {0xc24, 0x00000000},
155 	{0xc28, 0x00000000}, {0xc2c, 0x00000000},
156 	{0xc30, 0x69e9ac44}, {0xc34, 0x469652af},
157 	{0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
158 	{0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
159 	{0xc48, 0xec020107}, {0xc4c, 0x007f037f},
160 	{0xc50, 0x69543420}, {0xc54, 0x43bc0094},
161 	{0xc58, 0x69543420}, {0xc5c, 0x433c0094},
162 	{0xc60, 0x00000000}, {0xc64, 0x7112848b},
163 	{0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
164 	{0xc70, 0x2c7f000d}, {0xc74, 0x018610db},
165 	{0xc78, 0x0000001f}, {0xc7c, 0x00b91612},
166 	{0xc80, 0x40000100}, {0xc84, 0x20f60000},
167 	{0xc88, 0x40000100}, {0xc8c, 0x20200000},
168 	{0xc90, 0x00121820}, {0xc94, 0x00000000},
169 	{0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
170 	{0xca0, 0x00000000}, {0xca4, 0x00000080},
171 	{0xca8, 0x00000000}, {0xcac, 0x00000000},
172 	{0xcb0, 0x00000000}, {0xcb4, 0x00000000},
173 	{0xcb8, 0x00000000}, {0xcbc, 0x28000000},
174 	{0xcc0, 0x00000000}, {0xcc4, 0x00000000},
175 	{0xcc8, 0x00000000}, {0xccc, 0x00000000},
176 	{0xcd0, 0x00000000}, {0xcd4, 0x00000000},
177 	{0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
178 	{0xce0, 0x00222222}, {0xce4, 0x00000000},
179 	{0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
180 	{0xd00, 0x00080740}, {0xd04, 0x00020401},
181 	{0xd08, 0x0000907f}, {0xd0c, 0x20010201},
182 	{0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
183 	{0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
184 	{0xd30, 0x00000000}, {0xd34, 0x80608000},
185 	{0xd38, 0x00000000}, {0xd3c, 0x00027293},
186 	{0xd40, 0x00000000}, {0xd44, 0x00000000},
187 	{0xd48, 0x00000000}, {0xd4c, 0x00000000},
188 	{0xd50, 0x6437140a}, {0xd54, 0x00000000},
189 	{0xd58, 0x00000000}, {0xd5c, 0x30032064},
190 	{0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
191 	{0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
192 	{0xd70, 0x1812362e}, {0xd74, 0x322c2220},
193 	{0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
194 	{0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
195 	{0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
196 	{0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
197 	{0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
198 	{0xe34, 0x10008c1f}, {0xe38, 0x02140102},
199 	{0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
200 	{0xe44, 0x01004800}, {0xe48, 0xfb000000},
201 	{0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
202 	{0xe54, 0x10008c1f}, {0xe58, 0x02140102},
203 	{0xe5c, 0x28160d05}, {0xe60, 0x00000008},
204 	{0xe68, 0x001b25a4}, {0xe6c, 0x631b25a0},
205 	{0xe70, 0x631b25a0}, {0xe74, 0x081b25a0},
206 	{0xe78, 0x081b25a0}, {0xe7c, 0x081b25a0},
207 	{0xe80, 0x081b25a0}, {0xe84, 0x631b25a0},
208 	{0xe88, 0x081b25a0}, {0xe8c, 0x631b25a0},
209 	{0xed0, 0x631b25a0}, {0xed4, 0x631b25a0},
210 	{0xed8, 0x631b25a0}, {0xedc, 0x001b25a0},
211 	{0xee0, 0x001b25a0}, {0xeec, 0x6b1b25a0},
212 	{0xf14, 0x00000003}, {0xf4c, 0x00000000},
213 	{0xf00, 0x00000300},
214 	{0xffff, 0xffffffff},
215 };
216 
217 static const struct rtl8xxxu_reg32val rtl8192cu_phy_2t_init_table[] = {
218 	{0x024, 0x0011800f}, {0x028, 0x00ffdb83},
219 	{0x800, 0x80040002}, {0x804, 0x00000003},
220 	{0x808, 0x0000fc00}, {0x80c, 0x0000000a},
221 	{0x810, 0x10000330}, {0x814, 0x020c3d10},
222 	{0x818, 0x02200385}, {0x81c, 0x00000000},
223 	{0x820, 0x01000100}, {0x824, 0x00390004},
224 	{0x828, 0x01000100}, {0x82c, 0x00390004},
225 	{0x830, 0x27272727}, {0x834, 0x27272727},
226 	{0x838, 0x27272727}, {0x83c, 0x27272727},
227 	{0x840, 0x00010000}, {0x844, 0x00010000},
228 	{0x848, 0x27272727}, {0x84c, 0x27272727},
229 	{0x850, 0x00000000}, {0x854, 0x00000000},
230 	{0x858, 0x569a569a}, {0x85c, 0x0c1b25a4},
231 	{0x860, 0x66e60230}, {0x864, 0x061f0130},
232 	{0x868, 0x27272727}, {0x86c, 0x2b2b2b27},
233 	{0x870, 0x07000700}, {0x874, 0x22184000},
234 	{0x878, 0x08080808}, {0x87c, 0x00000000},
235 	{0x880, 0xc0083070}, {0x884, 0x000004d5},
236 	{0x888, 0x00000000}, {0x88c, 0xcc0000c0},
237 	{0x890, 0x00000800}, {0x894, 0xfffffffe},
238 	{0x898, 0x40302010}, {0x89c, 0x00706050},
239 	{0x900, 0x00000000}, {0x904, 0x00000023},
240 	{0x908, 0x00000000}, {0x90c, 0x81121313},
241 	{0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
242 	{0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
243 	{0xa10, 0x9500bb78}, {0xa14, 0x11144028},
244 	{0xa18, 0x00881117}, {0xa1c, 0x89140f00},
245 	{0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
246 	{0xa28, 0x00000204}, {0xa2c, 0x00d30000},
247 	{0xa70, 0x101fbf00}, {0xa74, 0x00000007},
248 	{0xc00, 0x48071d40}, {0xc04, 0x03a05633},
249 	{0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
250 	{0xc10, 0x08800000}, {0xc14, 0x40000100},
251 	{0xc18, 0x08800000}, {0xc1c, 0x40000100},
252 	{0xc20, 0x00000000}, {0xc24, 0x00000000},
253 	{0xc28, 0x00000000}, {0xc2c, 0x00000000},
254 	{0xc30, 0x69e9ac44}, {0xc34, 0x469652cf},
255 	{0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
256 	{0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
257 	{0xc48, 0xec020107}, {0xc4c, 0x007f037f},
258 	{0xc50, 0x69543420}, {0xc54, 0x43bc0094},
259 	{0xc58, 0x69543420}, {0xc5c, 0x433c0094},
260 	{0xc60, 0x00000000}, {0xc64, 0x5116848b},
261 	{0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
262 	{0xc70, 0x2c7f000d}, {0xc74, 0x2186115b},
263 	{0xc78, 0x0000001f}, {0xc7c, 0x00b99612},
264 	{0xc80, 0x40000100}, {0xc84, 0x20f60000},
265 	{0xc88, 0x40000100}, {0xc8c, 0xa0e40000},
266 	{0xc90, 0x00121820}, {0xc94, 0x00000000},
267 	{0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
268 	{0xca0, 0x00000000}, {0xca4, 0x00000080},
269 	{0xca8, 0x00000000}, {0xcac, 0x00000000},
270 	{0xcb0, 0x00000000}, {0xcb4, 0x00000000},
271 	{0xcb8, 0x00000000}, {0xcbc, 0x28000000},
272 	{0xcc0, 0x00000000}, {0xcc4, 0x00000000},
273 	{0xcc8, 0x00000000}, {0xccc, 0x00000000},
274 	{0xcd0, 0x00000000}, {0xcd4, 0x00000000},
275 	{0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
276 	{0xce0, 0x00222222}, {0xce4, 0x00000000},
277 	{0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
278 	{0xd00, 0x00080740}, {0xd04, 0x00020403},
279 	{0xd08, 0x0000907f}, {0xd0c, 0x20010201},
280 	{0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
281 	{0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
282 	{0xd30, 0x00000000}, {0xd34, 0x80608000},
283 	{0xd38, 0x00000000}, {0xd3c, 0x00027293},
284 	{0xd40, 0x00000000}, {0xd44, 0x00000000},
285 	{0xd48, 0x00000000}, {0xd4c, 0x00000000},
286 	{0xd50, 0x6437140a}, {0xd54, 0x00000000},
287 	{0xd58, 0x00000000}, {0xd5c, 0x30032064},
288 	{0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
289 	{0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
290 	{0xd70, 0x1812362e}, {0xd74, 0x322c2220},
291 	{0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
292 	{0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
293 	{0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
294 	{0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
295 	{0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
296 	{0xe34, 0x10008c1f}, {0xe38, 0x02140102},
297 	{0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
298 	{0xe44, 0x01004800}, {0xe48, 0xfb000000},
299 	{0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
300 	{0xe54, 0x10008c1f}, {0xe58, 0x02140102},
301 	{0xe5c, 0x28160d05}, {0xe60, 0x00000010},
302 	{0xe68, 0x001b25a4}, {0xe6c, 0x63db25a4},
303 	{0xe70, 0x63db25a4}, {0xe74, 0x0c1b25a4},
304 	{0xe78, 0x0c1b25a4}, {0xe7c, 0x0c1b25a4},
305 	{0xe80, 0x0c1b25a4}, {0xe84, 0x63db25a4},
306 	{0xe88, 0x0c1b25a4}, {0xe8c, 0x63db25a4},
307 	{0xed0, 0x63db25a4}, {0xed4, 0x63db25a4},
308 	{0xed8, 0x63db25a4}, {0xedc, 0x001b25a4},
309 	{0xee0, 0x001b25a4}, {0xeec, 0x6fdb25a4},
310 	{0xf14, 0x00000003}, {0xf4c, 0x00000000},
311 	{0xf00, 0x00000300},
312 	{0xffff, 0xffffffff},
313 };
314 
315 static const struct rtl8xxxu_reg32val rtl8188ru_phy_1t_highpa_table[] = {
316 	{0x024, 0x0011800f}, {0x028, 0x00ffdb83},
317 	{0x040, 0x000c0004}, {0x800, 0x80040000},
318 	{0x804, 0x00000001}, {0x808, 0x0000fc00},
319 	{0x80c, 0x0000000a}, {0x810, 0x10005388},
320 	{0x814, 0x020c3d10}, {0x818, 0x02200385},
321 	{0x81c, 0x00000000}, {0x820, 0x01000100},
322 	{0x824, 0x00390204}, {0x828, 0x00000000},
323 	{0x82c, 0x00000000}, {0x830, 0x00000000},
324 	{0x834, 0x00000000}, {0x838, 0x00000000},
325 	{0x83c, 0x00000000}, {0x840, 0x00010000},
326 	{0x844, 0x00000000}, {0x848, 0x00000000},
327 	{0x84c, 0x00000000}, {0x850, 0x00000000},
328 	{0x854, 0x00000000}, {0x858, 0x569a569a},
329 	{0x85c, 0x001b25a4}, {0x860, 0x66e60230},
330 	{0x864, 0x061f0130}, {0x868, 0x00000000},
331 	{0x86c, 0x20202000}, {0x870, 0x03000300},
332 	{0x874, 0x22004000}, {0x878, 0x00000808},
333 	{0x87c, 0x00ffc3f1}, {0x880, 0xc0083070},
334 	{0x884, 0x000004d5}, {0x888, 0x00000000},
335 	{0x88c, 0xccc000c0}, {0x890, 0x00000800},
336 	{0x894, 0xfffffffe}, {0x898, 0x40302010},
337 	{0x89c, 0x00706050}, {0x900, 0x00000000},
338 	{0x904, 0x00000023}, {0x908, 0x00000000},
339 	{0x90c, 0x81121111}, {0xa00, 0x00d047c8},
340 	{0xa04, 0x80ff000c}, {0xa08, 0x8c838300},
341 	{0xa0c, 0x2e68120f}, {0xa10, 0x9500bb78},
342 	{0xa14, 0x11144028}, {0xa18, 0x00881117},
343 	{0xa1c, 0x89140f00}, {0xa20, 0x15160000},
344 	{0xa24, 0x070b0f12}, {0xa28, 0x00000104},
345 	{0xa2c, 0x00d30000}, {0xa70, 0x101fbf00},
346 	{0xa74, 0x00000007}, {0xc00, 0x48071d40},
347 	{0xc04, 0x03a05611}, {0xc08, 0x000000e4},
348 	{0xc0c, 0x6c6c6c6c}, {0xc10, 0x08800000},
349 	{0xc14, 0x40000100}, {0xc18, 0x08800000},
350 	{0xc1c, 0x40000100}, {0xc20, 0x00000000},
351 	{0xc24, 0x00000000}, {0xc28, 0x00000000},
352 	{0xc2c, 0x00000000}, {0xc30, 0x69e9ac44},
353 	{0xc34, 0x469652cf}, {0xc38, 0x49795994},
354 	{0xc3c, 0x0a97971c}, {0xc40, 0x1f7c403f},
355 	{0xc44, 0x000100b7}, {0xc48, 0xec020107},
356 	{0xc4c, 0x007f037f}, {0xc50, 0x6954342e},
357 	{0xc54, 0x43bc0094}, {0xc58, 0x6954342f},
358 	{0xc5c, 0x433c0094}, {0xc60, 0x00000000},
359 	{0xc64, 0x5116848b}, {0xc68, 0x47c00bff},
360 	{0xc6c, 0x00000036}, {0xc70, 0x2c46000d},
361 	{0xc74, 0x018610db}, {0xc78, 0x0000001f},
362 	{0xc7c, 0x00b91612}, {0xc80, 0x24000090},
363 	{0xc84, 0x20f60000}, {0xc88, 0x24000090},
364 	{0xc8c, 0x20200000}, {0xc90, 0x00121820},
365 	{0xc94, 0x00000000}, {0xc98, 0x00121820},
366 	{0xc9c, 0x00007f7f}, {0xca0, 0x00000000},
367 	{0xca4, 0x00000080}, {0xca8, 0x00000000},
368 	{0xcac, 0x00000000}, {0xcb0, 0x00000000},
369 	{0xcb4, 0x00000000}, {0xcb8, 0x00000000},
370 	{0xcbc, 0x28000000}, {0xcc0, 0x00000000},
371 	{0xcc4, 0x00000000}, {0xcc8, 0x00000000},
372 	{0xccc, 0x00000000}, {0xcd0, 0x00000000},
373 	{0xcd4, 0x00000000}, {0xcd8, 0x64b22427},
374 	{0xcdc, 0x00766932}, {0xce0, 0x00222222},
375 	{0xce4, 0x00000000}, {0xce8, 0x37644302},
376 	{0xcec, 0x2f97d40c}, {0xd00, 0x00080740},
377 	{0xd04, 0x00020401}, {0xd08, 0x0000907f},
378 	{0xd0c, 0x20010201}, {0xd10, 0xa0633333},
379 	{0xd14, 0x3333bc43}, {0xd18, 0x7a8f5b6b},
380 	{0xd2c, 0xcc979975}, {0xd30, 0x00000000},
381 	{0xd34, 0x80608000}, {0xd38, 0x00000000},
382 	{0xd3c, 0x00027293}, {0xd40, 0x00000000},
383 	{0xd44, 0x00000000}, {0xd48, 0x00000000},
384 	{0xd4c, 0x00000000}, {0xd50, 0x6437140a},
385 	{0xd54, 0x00000000}, {0xd58, 0x00000000},
386 	{0xd5c, 0x30032064}, {0xd60, 0x4653de68},
387 	{0xd64, 0x04518a3c}, {0xd68, 0x00002101},
388 	{0xd6c, 0x2a201c16}, {0xd70, 0x1812362e},
389 	{0xd74, 0x322c2220}, {0xd78, 0x000e3c24},
390 	{0xe00, 0x24242424}, {0xe04, 0x24242424},
391 	{0xe08, 0x03902024}, {0xe10, 0x24242424},
392 	{0xe14, 0x24242424}, {0xe18, 0x24242424},
393 	{0xe1c, 0x24242424}, {0xe28, 0x00000000},
394 	{0xe30, 0x1000dc1f}, {0xe34, 0x10008c1f},
395 	{0xe38, 0x02140102}, {0xe3c, 0x681604c2},
396 	{0xe40, 0x01007c00}, {0xe44, 0x01004800},
397 	{0xe48, 0xfb000000}, {0xe4c, 0x000028d1},
398 	{0xe50, 0x1000dc1f}, {0xe54, 0x10008c1f},
399 	{0xe58, 0x02140102}, {0xe5c, 0x28160d05},
400 	{0xe60, 0x00000008}, {0xe68, 0x001b25a4},
401 	{0xe6c, 0x631b25a0}, {0xe70, 0x631b25a0},
402 	{0xe74, 0x081b25a0}, {0xe78, 0x081b25a0},
403 	{0xe7c, 0x081b25a0}, {0xe80, 0x081b25a0},
404 	{0xe84, 0x631b25a0}, {0xe88, 0x081b25a0},
405 	{0xe8c, 0x631b25a0}, {0xed0, 0x631b25a0},
406 	{0xed4, 0x631b25a0}, {0xed8, 0x631b25a0},
407 	{0xedc, 0x001b25a0}, {0xee0, 0x001b25a0},
408 	{0xeec, 0x6b1b25a0}, {0xee8, 0x31555448},
409 	{0xf14, 0x00000003}, {0xf4c, 0x00000000},
410 	{0xf00, 0x00000300},
411 	{0xffff, 0xffffffff},
412 };
413 
414 static const struct rtl8xxxu_reg32val rtl8xxx_agc_standard_table[] = {
415 	{0xc78, 0x7b000001}, {0xc78, 0x7b010001},
416 	{0xc78, 0x7b020001}, {0xc78, 0x7b030001},
417 	{0xc78, 0x7b040001}, {0xc78, 0x7b050001},
418 	{0xc78, 0x7a060001}, {0xc78, 0x79070001},
419 	{0xc78, 0x78080001}, {0xc78, 0x77090001},
420 	{0xc78, 0x760a0001}, {0xc78, 0x750b0001},
421 	{0xc78, 0x740c0001}, {0xc78, 0x730d0001},
422 	{0xc78, 0x720e0001}, {0xc78, 0x710f0001},
423 	{0xc78, 0x70100001}, {0xc78, 0x6f110001},
424 	{0xc78, 0x6e120001}, {0xc78, 0x6d130001},
425 	{0xc78, 0x6c140001}, {0xc78, 0x6b150001},
426 	{0xc78, 0x6a160001}, {0xc78, 0x69170001},
427 	{0xc78, 0x68180001}, {0xc78, 0x67190001},
428 	{0xc78, 0x661a0001}, {0xc78, 0x651b0001},
429 	{0xc78, 0x641c0001}, {0xc78, 0x631d0001},
430 	{0xc78, 0x621e0001}, {0xc78, 0x611f0001},
431 	{0xc78, 0x60200001}, {0xc78, 0x49210001},
432 	{0xc78, 0x48220001}, {0xc78, 0x47230001},
433 	{0xc78, 0x46240001}, {0xc78, 0x45250001},
434 	{0xc78, 0x44260001}, {0xc78, 0x43270001},
435 	{0xc78, 0x42280001}, {0xc78, 0x41290001},
436 	{0xc78, 0x402a0001}, {0xc78, 0x262b0001},
437 	{0xc78, 0x252c0001}, {0xc78, 0x242d0001},
438 	{0xc78, 0x232e0001}, {0xc78, 0x222f0001},
439 	{0xc78, 0x21300001}, {0xc78, 0x20310001},
440 	{0xc78, 0x06320001}, {0xc78, 0x05330001},
441 	{0xc78, 0x04340001}, {0xc78, 0x03350001},
442 	{0xc78, 0x02360001}, {0xc78, 0x01370001},
443 	{0xc78, 0x00380001}, {0xc78, 0x00390001},
444 	{0xc78, 0x003a0001}, {0xc78, 0x003b0001},
445 	{0xc78, 0x003c0001}, {0xc78, 0x003d0001},
446 	{0xc78, 0x003e0001}, {0xc78, 0x003f0001},
447 	{0xc78, 0x7b400001}, {0xc78, 0x7b410001},
448 	{0xc78, 0x7b420001}, {0xc78, 0x7b430001},
449 	{0xc78, 0x7b440001}, {0xc78, 0x7b450001},
450 	{0xc78, 0x7a460001}, {0xc78, 0x79470001},
451 	{0xc78, 0x78480001}, {0xc78, 0x77490001},
452 	{0xc78, 0x764a0001}, {0xc78, 0x754b0001},
453 	{0xc78, 0x744c0001}, {0xc78, 0x734d0001},
454 	{0xc78, 0x724e0001}, {0xc78, 0x714f0001},
455 	{0xc78, 0x70500001}, {0xc78, 0x6f510001},
456 	{0xc78, 0x6e520001}, {0xc78, 0x6d530001},
457 	{0xc78, 0x6c540001}, {0xc78, 0x6b550001},
458 	{0xc78, 0x6a560001}, {0xc78, 0x69570001},
459 	{0xc78, 0x68580001}, {0xc78, 0x67590001},
460 	{0xc78, 0x665a0001}, {0xc78, 0x655b0001},
461 	{0xc78, 0x645c0001}, {0xc78, 0x635d0001},
462 	{0xc78, 0x625e0001}, {0xc78, 0x615f0001},
463 	{0xc78, 0x60600001}, {0xc78, 0x49610001},
464 	{0xc78, 0x48620001}, {0xc78, 0x47630001},
465 	{0xc78, 0x46640001}, {0xc78, 0x45650001},
466 	{0xc78, 0x44660001}, {0xc78, 0x43670001},
467 	{0xc78, 0x42680001}, {0xc78, 0x41690001},
468 	{0xc78, 0x406a0001}, {0xc78, 0x266b0001},
469 	{0xc78, 0x256c0001}, {0xc78, 0x246d0001},
470 	{0xc78, 0x236e0001}, {0xc78, 0x226f0001},
471 	{0xc78, 0x21700001}, {0xc78, 0x20710001},
472 	{0xc78, 0x06720001}, {0xc78, 0x05730001},
473 	{0xc78, 0x04740001}, {0xc78, 0x03750001},
474 	{0xc78, 0x02760001}, {0xc78, 0x01770001},
475 	{0xc78, 0x00780001}, {0xc78, 0x00790001},
476 	{0xc78, 0x007a0001}, {0xc78, 0x007b0001},
477 	{0xc78, 0x007c0001}, {0xc78, 0x007d0001},
478 	{0xc78, 0x007e0001}, {0xc78, 0x007f0001},
479 	{0xc78, 0x3800001e}, {0xc78, 0x3801001e},
480 	{0xc78, 0x3802001e}, {0xc78, 0x3803001e},
481 	{0xc78, 0x3804001e}, {0xc78, 0x3805001e},
482 	{0xc78, 0x3806001e}, {0xc78, 0x3807001e},
483 	{0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
484 	{0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
485 	{0xc78, 0x440c001e}, {0xc78, 0x480d001e},
486 	{0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
487 	{0xc78, 0x5210001e}, {0xc78, 0x5611001e},
488 	{0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
489 	{0xc78, 0x6014001e}, {0xc78, 0x6015001e},
490 	{0xc78, 0x6016001e}, {0xc78, 0x6217001e},
491 	{0xc78, 0x6218001e}, {0xc78, 0x6219001e},
492 	{0xc78, 0x621a001e}, {0xc78, 0x621b001e},
493 	{0xc78, 0x621c001e}, {0xc78, 0x621d001e},
494 	{0xc78, 0x621e001e}, {0xc78, 0x621f001e},
495 	{0xffff, 0xffffffff}
496 };
497 
498 static const struct rtl8xxxu_reg32val rtl8xxx_agc_highpa_table[] = {
499 	{0xc78, 0x7b000001}, {0xc78, 0x7b010001},
500 	{0xc78, 0x7b020001}, {0xc78, 0x7b030001},
501 	{0xc78, 0x7b040001}, {0xc78, 0x7b050001},
502 	{0xc78, 0x7b060001}, {0xc78, 0x7b070001},
503 	{0xc78, 0x7b080001}, {0xc78, 0x7a090001},
504 	{0xc78, 0x790a0001}, {0xc78, 0x780b0001},
505 	{0xc78, 0x770c0001}, {0xc78, 0x760d0001},
506 	{0xc78, 0x750e0001}, {0xc78, 0x740f0001},
507 	{0xc78, 0x73100001}, {0xc78, 0x72110001},
508 	{0xc78, 0x71120001}, {0xc78, 0x70130001},
509 	{0xc78, 0x6f140001}, {0xc78, 0x6e150001},
510 	{0xc78, 0x6d160001}, {0xc78, 0x6c170001},
511 	{0xc78, 0x6b180001}, {0xc78, 0x6a190001},
512 	{0xc78, 0x691a0001}, {0xc78, 0x681b0001},
513 	{0xc78, 0x671c0001}, {0xc78, 0x661d0001},
514 	{0xc78, 0x651e0001}, {0xc78, 0x641f0001},
515 	{0xc78, 0x63200001}, {0xc78, 0x62210001},
516 	{0xc78, 0x61220001}, {0xc78, 0x60230001},
517 	{0xc78, 0x46240001}, {0xc78, 0x45250001},
518 	{0xc78, 0x44260001}, {0xc78, 0x43270001},
519 	{0xc78, 0x42280001}, {0xc78, 0x41290001},
520 	{0xc78, 0x402a0001}, {0xc78, 0x262b0001},
521 	{0xc78, 0x252c0001}, {0xc78, 0x242d0001},
522 	{0xc78, 0x232e0001}, {0xc78, 0x222f0001},
523 	{0xc78, 0x21300001}, {0xc78, 0x20310001},
524 	{0xc78, 0x06320001}, {0xc78, 0x05330001},
525 	{0xc78, 0x04340001}, {0xc78, 0x03350001},
526 	{0xc78, 0x02360001}, {0xc78, 0x01370001},
527 	{0xc78, 0x00380001}, {0xc78, 0x00390001},
528 	{0xc78, 0x003a0001}, {0xc78, 0x003b0001},
529 	{0xc78, 0x003c0001}, {0xc78, 0x003d0001},
530 	{0xc78, 0x003e0001}, {0xc78, 0x003f0001},
531 	{0xc78, 0x7b400001}, {0xc78, 0x7b410001},
532 	{0xc78, 0x7b420001}, {0xc78, 0x7b430001},
533 	{0xc78, 0x7b440001}, {0xc78, 0x7b450001},
534 	{0xc78, 0x7b460001}, {0xc78, 0x7b470001},
535 	{0xc78, 0x7b480001}, {0xc78, 0x7a490001},
536 	{0xc78, 0x794a0001}, {0xc78, 0x784b0001},
537 	{0xc78, 0x774c0001}, {0xc78, 0x764d0001},
538 	{0xc78, 0x754e0001}, {0xc78, 0x744f0001},
539 	{0xc78, 0x73500001}, {0xc78, 0x72510001},
540 	{0xc78, 0x71520001}, {0xc78, 0x70530001},
541 	{0xc78, 0x6f540001}, {0xc78, 0x6e550001},
542 	{0xc78, 0x6d560001}, {0xc78, 0x6c570001},
543 	{0xc78, 0x6b580001}, {0xc78, 0x6a590001},
544 	{0xc78, 0x695a0001}, {0xc78, 0x685b0001},
545 	{0xc78, 0x675c0001}, {0xc78, 0x665d0001},
546 	{0xc78, 0x655e0001}, {0xc78, 0x645f0001},
547 	{0xc78, 0x63600001}, {0xc78, 0x62610001},
548 	{0xc78, 0x61620001}, {0xc78, 0x60630001},
549 	{0xc78, 0x46640001}, {0xc78, 0x45650001},
550 	{0xc78, 0x44660001}, {0xc78, 0x43670001},
551 	{0xc78, 0x42680001}, {0xc78, 0x41690001},
552 	{0xc78, 0x406a0001}, {0xc78, 0x266b0001},
553 	{0xc78, 0x256c0001}, {0xc78, 0x246d0001},
554 	{0xc78, 0x236e0001}, {0xc78, 0x226f0001},
555 	{0xc78, 0x21700001}, {0xc78, 0x20710001},
556 	{0xc78, 0x06720001}, {0xc78, 0x05730001},
557 	{0xc78, 0x04740001}, {0xc78, 0x03750001},
558 	{0xc78, 0x02760001}, {0xc78, 0x01770001},
559 	{0xc78, 0x00780001}, {0xc78, 0x00790001},
560 	{0xc78, 0x007a0001}, {0xc78, 0x007b0001},
561 	{0xc78, 0x007c0001}, {0xc78, 0x007d0001},
562 	{0xc78, 0x007e0001}, {0xc78, 0x007f0001},
563 	{0xc78, 0x3800001e}, {0xc78, 0x3801001e},
564 	{0xc78, 0x3802001e}, {0xc78, 0x3803001e},
565 	{0xc78, 0x3804001e}, {0xc78, 0x3805001e},
566 	{0xc78, 0x3806001e}, {0xc78, 0x3807001e},
567 	{0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
568 	{0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
569 	{0xc78, 0x440c001e}, {0xc78, 0x480d001e},
570 	{0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
571 	{0xc78, 0x5210001e}, {0xc78, 0x5611001e},
572 	{0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
573 	{0xc78, 0x6014001e}, {0xc78, 0x6015001e},
574 	{0xc78, 0x6016001e}, {0xc78, 0x6217001e},
575 	{0xc78, 0x6218001e}, {0xc78, 0x6219001e},
576 	{0xc78, 0x621a001e}, {0xc78, 0x621b001e},
577 	{0xc78, 0x621c001e}, {0xc78, 0x621d001e},
578 	{0xc78, 0x621e001e}, {0xc78, 0x621f001e},
579 	{0xffff, 0xffffffff}
580 };
581 
582 static const struct rtl8xxxu_rfregs rtl8xxxu_rfregs[] = {
583 	{	/* RF_A */
584 		.hssiparm1 = REG_FPGA0_XA_HSSI_PARM1,
585 		.hssiparm2 = REG_FPGA0_XA_HSSI_PARM2,
586 		.lssiparm = REG_FPGA0_XA_LSSI_PARM,
587 		.hspiread = REG_HSPI_XA_READBACK,
588 		.lssiread = REG_FPGA0_XA_LSSI_READBACK,
589 		.rf_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL,
590 	},
591 	{	/* RF_B */
592 		.hssiparm1 = REG_FPGA0_XB_HSSI_PARM1,
593 		.hssiparm2 = REG_FPGA0_XB_HSSI_PARM2,
594 		.lssiparm = REG_FPGA0_XB_LSSI_PARM,
595 		.hspiread = REG_HSPI_XB_READBACK,
596 		.lssiread = REG_FPGA0_XB_LSSI_READBACK,
597 		.rf_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL,
598 	},
599 };
600 
601 const u32 rtl8xxxu_iqk_phy_iq_bb_reg[RTL8XXXU_BB_REGS] = {
602 	REG_OFDM0_XA_RX_IQ_IMBALANCE,
603 	REG_OFDM0_XB_RX_IQ_IMBALANCE,
604 	REG_OFDM0_ENERGY_CCA_THRES,
605 	REG_OFDM0_AGC_RSSI_TABLE,
606 	REG_OFDM0_XA_TX_IQ_IMBALANCE,
607 	REG_OFDM0_XB_TX_IQ_IMBALANCE,
608 	REG_OFDM0_XC_TX_AFE,
609 	REG_OFDM0_XD_TX_AFE,
610 	REG_OFDM0_RX_IQ_EXT_ANTA
611 };
612 
613 u8 rtl8xxxu_read8(struct rtl8xxxu_priv *priv, u16 addr)
614 {
615 	struct usb_device *udev = priv->udev;
616 	int len;
617 	u8 data;
618 
619 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
620 		addr |= 0x8000;
621 
622 	mutex_lock(&priv->usb_buf_mutex);
623 	len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
624 			      REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
625 			      addr, 0, &priv->usb_buf.val8, sizeof(u8),
626 			      RTW_USB_CONTROL_MSG_TIMEOUT);
627 	data = priv->usb_buf.val8;
628 	mutex_unlock(&priv->usb_buf_mutex);
629 
630 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
631 		dev_info(&udev->dev, "%s(%04x)   = 0x%02x, len %i\n",
632 			 __func__, addr, data, len);
633 	return data;
634 }
635 
636 u16 rtl8xxxu_read16(struct rtl8xxxu_priv *priv, u16 addr)
637 {
638 	struct usb_device *udev = priv->udev;
639 	int len;
640 	u16 data;
641 
642 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
643 		addr |= 0x8000;
644 
645 	mutex_lock(&priv->usb_buf_mutex);
646 	len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
647 			      REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
648 			      addr, 0, &priv->usb_buf.val16, sizeof(u16),
649 			      RTW_USB_CONTROL_MSG_TIMEOUT);
650 	data = le16_to_cpu(priv->usb_buf.val16);
651 	mutex_unlock(&priv->usb_buf_mutex);
652 
653 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
654 		dev_info(&udev->dev, "%s(%04x)  = 0x%04x, len %i\n",
655 			 __func__, addr, data, len);
656 	return data;
657 }
658 
659 u32 rtl8xxxu_read32(struct rtl8xxxu_priv *priv, u16 addr)
660 {
661 	struct usb_device *udev = priv->udev;
662 	int len;
663 	u32 data;
664 
665 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
666 		addr |= 0x8000;
667 
668 	mutex_lock(&priv->usb_buf_mutex);
669 	len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
670 			      REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
671 			      addr, 0, &priv->usb_buf.val32, sizeof(u32),
672 			      RTW_USB_CONTROL_MSG_TIMEOUT);
673 	data = le32_to_cpu(priv->usb_buf.val32);
674 	mutex_unlock(&priv->usb_buf_mutex);
675 
676 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
677 		dev_info(&udev->dev, "%s(%04x)  = 0x%08x, len %i\n",
678 			 __func__, addr, data, len);
679 	return data;
680 }
681 
682 int rtl8xxxu_write8(struct rtl8xxxu_priv *priv, u16 addr, u8 val)
683 {
684 	struct usb_device *udev = priv->udev;
685 	int ret;
686 
687 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
688 		addr |= 0x8000;
689 
690 	mutex_lock(&priv->usb_buf_mutex);
691 	priv->usb_buf.val8 = val;
692 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
693 			      REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
694 			      addr, 0, &priv->usb_buf.val8, sizeof(u8),
695 			      RTW_USB_CONTROL_MSG_TIMEOUT);
696 
697 	mutex_unlock(&priv->usb_buf_mutex);
698 
699 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
700 		dev_info(&udev->dev, "%s(%04x) = 0x%02x\n",
701 			 __func__, addr, val);
702 	return ret;
703 }
704 
705 int rtl8xxxu_write16(struct rtl8xxxu_priv *priv, u16 addr, u16 val)
706 {
707 	struct usb_device *udev = priv->udev;
708 	int ret;
709 
710 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
711 		addr |= 0x8000;
712 
713 	mutex_lock(&priv->usb_buf_mutex);
714 	priv->usb_buf.val16 = cpu_to_le16(val);
715 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
716 			      REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
717 			      addr, 0, &priv->usb_buf.val16, sizeof(u16),
718 			      RTW_USB_CONTROL_MSG_TIMEOUT);
719 	mutex_unlock(&priv->usb_buf_mutex);
720 
721 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
722 		dev_info(&udev->dev, "%s(%04x) = 0x%04x\n",
723 			 __func__, addr, val);
724 	return ret;
725 }
726 
727 int rtl8xxxu_write32(struct rtl8xxxu_priv *priv, u16 addr, u32 val)
728 {
729 	struct usb_device *udev = priv->udev;
730 	int ret;
731 
732 	if (priv->rtl_chip == RTL8710B && addr <= 0xff)
733 		addr |= 0x8000;
734 
735 	mutex_lock(&priv->usb_buf_mutex);
736 	priv->usb_buf.val32 = cpu_to_le32(val);
737 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
738 			      REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
739 			      addr, 0, &priv->usb_buf.val32, sizeof(u32),
740 			      RTW_USB_CONTROL_MSG_TIMEOUT);
741 	mutex_unlock(&priv->usb_buf_mutex);
742 
743 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
744 		dev_info(&udev->dev, "%s(%04x) = 0x%08x\n",
745 			 __func__, addr, val);
746 	return ret;
747 }
748 
749 int rtl8xxxu_write8_set(struct rtl8xxxu_priv *priv, u16 addr, u8 bits)
750 {
751 	u8 val8;
752 
753 	val8 = rtl8xxxu_read8(priv, addr);
754 	val8 |= bits;
755 	return rtl8xxxu_write8(priv, addr, val8);
756 }
757 
758 int rtl8xxxu_write8_clear(struct rtl8xxxu_priv *priv, u16 addr, u8 bits)
759 {
760 	u8 val8;
761 
762 	val8 = rtl8xxxu_read8(priv, addr);
763 	val8 &= ~bits;
764 	return rtl8xxxu_write8(priv, addr, val8);
765 }
766 
767 int rtl8xxxu_write16_set(struct rtl8xxxu_priv *priv, u16 addr, u16 bits)
768 {
769 	u16 val16;
770 
771 	val16 = rtl8xxxu_read16(priv, addr);
772 	val16 |= bits;
773 	return rtl8xxxu_write16(priv, addr, val16);
774 }
775 
776 int rtl8xxxu_write16_clear(struct rtl8xxxu_priv *priv, u16 addr, u16 bits)
777 {
778 	u16 val16;
779 
780 	val16 = rtl8xxxu_read16(priv, addr);
781 	val16 &= ~bits;
782 	return rtl8xxxu_write16(priv, addr, val16);
783 }
784 
785 int rtl8xxxu_write32_set(struct rtl8xxxu_priv *priv, u16 addr, u32 bits)
786 {
787 	u32 val32;
788 
789 	val32 = rtl8xxxu_read32(priv, addr);
790 	val32 |= bits;
791 	return rtl8xxxu_write32(priv, addr, val32);
792 }
793 
794 int rtl8xxxu_write32_clear(struct rtl8xxxu_priv *priv, u16 addr, u32 bits)
795 {
796 	u32 val32;
797 
798 	val32 = rtl8xxxu_read32(priv, addr);
799 	val32 &= ~bits;
800 	return rtl8xxxu_write32(priv, addr, val32);
801 }
802 
803 int rtl8xxxu_write32_mask(struct rtl8xxxu_priv *priv, u16 addr,
804 			  u32 mask, u32 val)
805 {
806 	u32 orig, new, shift;
807 
808 	shift = __ffs(mask);
809 
810 	orig = rtl8xxxu_read32(priv, addr);
811 	new = (orig & ~mask) | ((val << shift) & mask);
812 	return rtl8xxxu_write32(priv, addr, new);
813 }
814 
815 int rtl8xxxu_write_rfreg_mask(struct rtl8xxxu_priv *priv,
816 			      enum rtl8xxxu_rfpath path, u8 reg,
817 			      u32 mask, u32 val)
818 {
819 	u32 orig, new, shift;
820 
821 	shift = __ffs(mask);
822 
823 	orig = rtl8xxxu_read_rfreg(priv, path, reg);
824 	new = (orig & ~mask) | ((val << shift) & mask);
825 	return rtl8xxxu_write_rfreg(priv, path, reg, new);
826 }
827 
828 static int
829 rtl8xxxu_writeN(struct rtl8xxxu_priv *priv, u16 addr, u8 *buf, u16 len)
830 {
831 	struct usb_device *udev = priv->udev;
832 	int blocksize = priv->fops->writeN_block_size;
833 	int ret, i, count, remainder;
834 
835 	count = len / blocksize;
836 	remainder = len % blocksize;
837 
838 	for (i = 0; i < count; i++) {
839 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
840 				      REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
841 				      addr, 0, buf, blocksize,
842 				      RTW_USB_CONTROL_MSG_TIMEOUT);
843 		if (ret != blocksize)
844 			goto write_error;
845 
846 		addr += blocksize;
847 		buf += blocksize;
848 	}
849 
850 	if (remainder) {
851 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
852 				      REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
853 				      addr, 0, buf, remainder,
854 				      RTW_USB_CONTROL_MSG_TIMEOUT);
855 		if (ret != remainder)
856 			goto write_error;
857 	}
858 
859 	return len;
860 
861 write_error:
862 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
863 		dev_info(&udev->dev,
864 			 "%s: Failed to write block at addr: %04x size: %04x\n",
865 			 __func__, addr, blocksize);
866 	return -EAGAIN;
867 }
868 
869 u32 rtl8xxxu_read_rfreg(struct rtl8xxxu_priv *priv,
870 			enum rtl8xxxu_rfpath path, u8 reg)
871 {
872 	u32 hssia, val32, retval;
873 
874 	hssia = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM2);
875 	if (path != RF_A)
876 		val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm2);
877 	else
878 		val32 = hssia;
879 
880 	val32 &= ~FPGA0_HSSI_PARM2_ADDR_MASK;
881 	val32 |= (reg << FPGA0_HSSI_PARM2_ADDR_SHIFT);
882 	val32 |= FPGA0_HSSI_PARM2_EDGE_READ;
883 	hssia &= ~FPGA0_HSSI_PARM2_EDGE_READ;
884 	rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
885 
886 	udelay(10);
887 
888 	rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].hssiparm2, val32);
889 	udelay(100);
890 
891 	hssia |= FPGA0_HSSI_PARM2_EDGE_READ;
892 	rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
893 	udelay(10);
894 
895 	val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm1);
896 	if (val32 & FPGA0_HSSI_PARM1_PI)
897 		retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hspiread);
898 	else
899 		retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].lssiread);
900 
901 	retval &= 0xfffff;
902 
903 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_READ)
904 		dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
905 			 __func__, reg, retval);
906 	return retval;
907 }
908 
909 /*
910  * The RTL8723BU driver indicates that registers 0xb2 and 0xb6 can
911  * have write issues in high temperature conditions. We may have to
912  * retry writing them.
913  */
914 int rtl8xxxu_write_rfreg(struct rtl8xxxu_priv *priv,
915 			 enum rtl8xxxu_rfpath path, u8 reg, u32 data)
916 {
917 	int ret, retval;
918 	u32 dataaddr, val32;
919 
920 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_WRITE)
921 		dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
922 			 __func__, reg, data);
923 
924 	data &= FPGA0_LSSI_PARM_DATA_MASK;
925 	dataaddr = (reg << FPGA0_LSSI_PARM_ADDR_SHIFT) | data;
926 
927 	if (priv->rtl_chip == RTL8192E) {
928 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
929 		val32 &= ~0x20000;
930 		rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
931 	}
932 
933 	/* Use XB for path B */
934 	ret = rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].lssiparm, dataaddr);
935 	if (ret != sizeof(dataaddr))
936 		retval = -EIO;
937 	else
938 		retval = 0;
939 
940 	udelay(1);
941 
942 	if (priv->rtl_chip == RTL8192E) {
943 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
944 		val32 |= 0x20000;
945 		rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
946 	}
947 
948 	return retval;
949 }
950 
951 static int
952 rtl8xxxu_gen1_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
953 {
954 	struct device *dev = &priv->udev->dev;
955 	int mbox_nr, retry, retval = 0;
956 	int mbox_reg, mbox_ext_reg;
957 	u8 val8;
958 
959 	mutex_lock(&priv->h2c_mutex);
960 
961 	mbox_nr = priv->next_mbox;
962 	mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
963 	mbox_ext_reg = REG_HMBOX_EXT_0 + (mbox_nr * 2);
964 
965 	/*
966 	 * MBOX ready?
967 	 */
968 	retry = 100;
969 	do {
970 		val8 = rtl8xxxu_read8(priv, REG_HMTFR);
971 		if (!(val8 & BIT(mbox_nr)))
972 			break;
973 	} while (retry--);
974 
975 	if (!retry) {
976 		dev_info(dev, "%s: Mailbox busy\n", __func__);
977 		retval = -EBUSY;
978 		goto error;
979 	}
980 
981 	/*
982 	 * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
983 	 */
984 	if (len > sizeof(u32)) {
985 		rtl8xxxu_write16(priv, mbox_ext_reg, le16_to_cpu(h2c->raw.ext));
986 		if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
987 			dev_info(dev, "H2C_EXT %04x\n",
988 				 le16_to_cpu(h2c->raw.ext));
989 	}
990 	rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
991 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
992 		dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
993 
994 	priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
995 
996 error:
997 	mutex_unlock(&priv->h2c_mutex);
998 	return retval;
999 }
1000 
1001 int
1002 rtl8xxxu_gen2_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
1003 {
1004 	struct device *dev = &priv->udev->dev;
1005 	int mbox_nr, retry, retval = 0;
1006 	int mbox_reg, mbox_ext_reg;
1007 	u8 val8;
1008 
1009 	mutex_lock(&priv->h2c_mutex);
1010 
1011 	mbox_nr = priv->next_mbox;
1012 	mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
1013 	mbox_ext_reg = REG_HMBOX_EXT0_8723B + (mbox_nr * 4);
1014 
1015 	/*
1016 	 * MBOX ready?
1017 	 */
1018 	retry = 100;
1019 	do {
1020 		val8 = rtl8xxxu_read8(priv, REG_HMTFR);
1021 		if (!(val8 & BIT(mbox_nr)))
1022 			break;
1023 	} while (retry--);
1024 
1025 	if (!retry) {
1026 		dev_info(dev, "%s: Mailbox busy\n", __func__);
1027 		retval = -EBUSY;
1028 		goto error;
1029 	}
1030 
1031 	/*
1032 	 * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
1033 	 */
1034 	if (len > sizeof(u32)) {
1035 		rtl8xxxu_write32(priv, mbox_ext_reg,
1036 				 le32_to_cpu(h2c->raw_wide.ext));
1037 		if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
1038 			dev_info(dev, "H2C_EXT %08x\n",
1039 				 le32_to_cpu(h2c->raw_wide.ext));
1040 	}
1041 	rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
1042 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
1043 		dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
1044 
1045 	priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
1046 
1047 error:
1048 	mutex_unlock(&priv->h2c_mutex);
1049 	return retval;
1050 }
1051 
1052 void rtl8xxxu_gen1_enable_rf(struct rtl8xxxu_priv *priv)
1053 {
1054 	u8 val8;
1055 	u32 val32;
1056 
1057 	val8 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
1058 	val8 |= BIT(0) | BIT(3);
1059 	rtl8xxxu_write8(priv, REG_SPS0_CTRL, val8);
1060 
1061 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
1062 	val32 &= ~(BIT(4) | BIT(5));
1063 	val32 |= BIT(3);
1064 	if (priv->rf_paths == 2) {
1065 		val32 &= ~(BIT(20) | BIT(21));
1066 		val32 |= BIT(19);
1067 	}
1068 	rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
1069 
1070 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1071 	val32 &= ~OFDM_RF_PATH_TX_MASK;
1072 	if (priv->tx_paths == 2)
1073 		val32 |= OFDM_RF_PATH_TX_A | OFDM_RF_PATH_TX_B;
1074 	else if (priv->rtl_chip == RTL8192C || priv->rtl_chip == RTL8191C)
1075 		val32 |= OFDM_RF_PATH_TX_B;
1076 	else
1077 		val32 |= OFDM_RF_PATH_TX_A;
1078 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1079 
1080 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1081 	val32 &= ~FPGA_RF_MODE_JAPAN;
1082 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1083 
1084 	if (priv->rf_paths == 2)
1085 		rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x63db25a0);
1086 	else
1087 		rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x631b25a0);
1088 
1089 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0x32d95);
1090 	if (priv->rf_paths == 2)
1091 		rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0x32d95);
1092 
1093 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
1094 }
1095 
1096 void rtl8xxxu_gen1_disable_rf(struct rtl8xxxu_priv *priv)
1097 {
1098 	u8 sps0;
1099 	u32 val32;
1100 
1101 	sps0 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
1102 
1103 	/* RF RX code for preamble power saving */
1104 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
1105 	val32 &= ~(BIT(3) | BIT(4) | BIT(5));
1106 	if (priv->rf_paths == 2)
1107 		val32 &= ~(BIT(19) | BIT(20) | BIT(21));
1108 	rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
1109 
1110 	/* Disable TX for four paths */
1111 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1112 	val32 &= ~OFDM_RF_PATH_TX_MASK;
1113 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1114 
1115 	/* Enable power saving */
1116 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1117 	val32 |= FPGA_RF_MODE_JAPAN;
1118 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1119 
1120 	/* AFE control register to power down bits [30:22] */
1121 	if (priv->rf_paths == 2)
1122 		rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x00db25a0);
1123 	else
1124 		rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x001b25a0);
1125 
1126 	/* Power down RF module */
1127 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0);
1128 	if (priv->rf_paths == 2)
1129 		rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0);
1130 
1131 	sps0 &= ~(BIT(0) | BIT(3));
1132 	rtl8xxxu_write8(priv, REG_SPS0_CTRL, sps0);
1133 }
1134 
1135 static void rtl8xxxu_stop_tx_beacon(struct rtl8xxxu_priv *priv)
1136 {
1137 	u8 val8;
1138 
1139 	val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL + 2);
1140 	val8 &= ~BIT(6);
1141 	rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL + 2, val8);
1142 
1143 	rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 1, 0x64);
1144 	val8 = rtl8xxxu_read8(priv, REG_TBTT_PROHIBIT + 2);
1145 	val8 &= ~BIT(0);
1146 	rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 2, val8);
1147 }
1148 
1149 static void rtl8xxxu_start_tx_beacon(struct rtl8xxxu_priv *priv)
1150 {
1151 	u8 val8;
1152 
1153 	val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL + 2);
1154 	val8 |= EN_BCNQ_DL >> 16;
1155 	rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL + 2, val8);
1156 
1157 	rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 1, 0x80);
1158 	val8 = rtl8xxxu_read8(priv, REG_TBTT_PROHIBIT + 2);
1159 	val8 &= 0xF0;
1160 	rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 2, val8);
1161 }
1162 
1163 
1164 /*
1165  * The rtl8723a has 3 channel groups for its efuse settings. It only
1166  * supports the 2.4GHz band, so channels 1 - 14:
1167  *  group 0: channels 1 - 3
1168  *  group 1: channels 4 - 9
1169  *  group 2: channels 10 - 14
1170  *
1171  * Note: We index from 0 in the code
1172  */
1173 static int rtl8xxxu_gen1_channel_to_group(int channel)
1174 {
1175 	int group;
1176 
1177 	if (channel < 4)
1178 		group = 0;
1179 	else if (channel < 10)
1180 		group = 1;
1181 	else
1182 		group = 2;
1183 
1184 	return group;
1185 }
1186 
1187 /*
1188  * Valid for rtl8723bu and rtl8192eu
1189  */
1190 int rtl8xxxu_gen2_channel_to_group(int channel)
1191 {
1192 	int group;
1193 
1194 	if (channel < 3)
1195 		group = 0;
1196 	else if (channel < 6)
1197 		group = 1;
1198 	else if (channel < 9)
1199 		group = 2;
1200 	else if (channel < 12)
1201 		group = 3;
1202 	else
1203 		group = 4;
1204 
1205 	return group;
1206 }
1207 
1208 void rtl8xxxu_gen1_config_channel(struct ieee80211_hw *hw)
1209 {
1210 	struct rtl8xxxu_priv *priv = hw->priv;
1211 	u32 val32, rsr;
1212 	u8 val8, opmode;
1213 	bool ht = true;
1214 	int sec_ch_above, channel;
1215 	int i;
1216 
1217 	opmode = rtl8xxxu_read8(priv, REG_BW_OPMODE);
1218 	rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
1219 	channel = hw->conf.chandef.chan->hw_value;
1220 
1221 	switch (hw->conf.chandef.width) {
1222 	case NL80211_CHAN_WIDTH_20_NOHT:
1223 		ht = false;
1224 		fallthrough;
1225 	case NL80211_CHAN_WIDTH_20:
1226 		opmode |= BW_OPMODE_20MHZ;
1227 		rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1228 
1229 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1230 		val32 &= ~FPGA_RF_MODE;
1231 		rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1232 
1233 		val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1234 		val32 &= ~FPGA_RF_MODE;
1235 		rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1236 
1237 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1238 		val32 |= FPGA0_ANALOG2_20MHZ;
1239 		rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1240 		break;
1241 	case NL80211_CHAN_WIDTH_40:
1242 		if (hw->conf.chandef.center_freq1 >
1243 		    hw->conf.chandef.chan->center_freq) {
1244 			sec_ch_above = 1;
1245 			channel += 2;
1246 		} else {
1247 			sec_ch_above = 0;
1248 			channel -= 2;
1249 		}
1250 
1251 		opmode &= ~BW_OPMODE_20MHZ;
1252 		rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1253 		rsr &= ~RSR_RSC_BANDWIDTH_40M;
1254 		if (!sec_ch_above)
1255 			rsr |= RSR_RSC_UPPER_SUB_CHANNEL;
1256 		else
1257 			rsr |= RSR_RSC_LOWER_SUB_CHANNEL;
1258 		rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, rsr);
1259 
1260 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1261 		val32 |= FPGA_RF_MODE;
1262 		rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1263 
1264 		val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1265 		val32 |= FPGA_RF_MODE;
1266 		rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1267 
1268 		/*
1269 		 * Set Control channel to upper or lower. These settings
1270 		 * are required only for 40MHz
1271 		 */
1272 		val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1273 		val32 &= ~CCK0_SIDEBAND;
1274 		if (!sec_ch_above)
1275 			val32 |= CCK0_SIDEBAND;
1276 		rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1277 
1278 		val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1279 		val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1280 		if (sec_ch_above)
1281 			val32 |= OFDM_LSTF_PRIME_CH_LOW;
1282 		else
1283 			val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1284 		rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1285 
1286 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1287 		val32 &= ~FPGA0_ANALOG2_20MHZ;
1288 		rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1289 
1290 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1291 		val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1292 		if (sec_ch_above)
1293 			val32 |= FPGA0_PS_UPPER_CHANNEL;
1294 		else
1295 			val32 |= FPGA0_PS_LOWER_CHANNEL;
1296 		rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1297 		break;
1298 
1299 	default:
1300 		break;
1301 	}
1302 
1303 	for (i = RF_A; i < priv->rf_paths; i++) {
1304 		val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1305 		val32 &= ~MODE_AG_CHANNEL_MASK;
1306 		val32 |= channel;
1307 		rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1308 	}
1309 
1310 	if (ht)
1311 		val8 = 0x0e;
1312 	else
1313 		val8 = 0x0a;
1314 
1315 	rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1316 	rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1317 
1318 	rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1319 	rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1320 
1321 	for (i = RF_A; i < priv->rf_paths; i++) {
1322 		val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1323 		val32 &= ~MODE_AG_BW_MASK;
1324 		if (hw->conf.chandef.width != NL80211_CHAN_WIDTH_40)
1325 			val32 |= MODE_AG_CHANNEL_20MHZ;
1326 		rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1327 	}
1328 }
1329 
1330 void rtl8xxxu_gen2_config_channel(struct ieee80211_hw *hw)
1331 {
1332 	struct rtl8xxxu_priv *priv = hw->priv;
1333 	u32 val32;
1334 	u8 val8, subchannel;
1335 	u16 rf_mode_bw;
1336 	bool ht = true;
1337 	int sec_ch_above, channel;
1338 	int i;
1339 
1340 	rf_mode_bw = rtl8xxxu_read16(priv, REG_WMAC_TRXPTCL_CTL);
1341 	rf_mode_bw &= ~WMAC_TRXPTCL_CTL_BW_MASK;
1342 	channel = hw->conf.chandef.chan->hw_value;
1343 
1344 /* Hack */
1345 	subchannel = 0;
1346 
1347 	switch (hw->conf.chandef.width) {
1348 	case NL80211_CHAN_WIDTH_20_NOHT:
1349 		ht = false;
1350 		fallthrough;
1351 	case NL80211_CHAN_WIDTH_20:
1352 		rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_20;
1353 		subchannel = 0;
1354 
1355 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1356 		val32 &= ~FPGA_RF_MODE;
1357 		rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1358 
1359 		val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1360 		val32 &= ~FPGA_RF_MODE;
1361 		rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1362 
1363 		val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
1364 		val32 &= ~(BIT(30) | BIT(31));
1365 		rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
1366 
1367 		break;
1368 	case NL80211_CHAN_WIDTH_40:
1369 		rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_40;
1370 
1371 		if (hw->conf.chandef.center_freq1 >
1372 		    hw->conf.chandef.chan->center_freq) {
1373 			sec_ch_above = 1;
1374 			channel += 2;
1375 			subchannel = 2;
1376 		} else {
1377 			sec_ch_above = 0;
1378 			channel -= 2;
1379 			subchannel = 1;
1380 		}
1381 
1382 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1383 		val32 |= FPGA_RF_MODE;
1384 		rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1385 
1386 		val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1387 		val32 |= FPGA_RF_MODE;
1388 		rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1389 
1390 		/*
1391 		 * Set Control channel to upper or lower. These settings
1392 		 * are required only for 40MHz
1393 		 */
1394 		val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1395 		val32 &= ~CCK0_SIDEBAND;
1396 		if (!sec_ch_above)
1397 			val32 |= CCK0_SIDEBAND;
1398 		rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1399 
1400 		val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1401 		val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1402 		if (sec_ch_above)
1403 			val32 |= OFDM_LSTF_PRIME_CH_LOW;
1404 		else
1405 			val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1406 		rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1407 
1408 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1409 		val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1410 		if (sec_ch_above)
1411 			val32 |= FPGA0_PS_UPPER_CHANNEL;
1412 		else
1413 			val32 |= FPGA0_PS_LOWER_CHANNEL;
1414 		rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1415 		break;
1416 	case NL80211_CHAN_WIDTH_80:
1417 		rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_80;
1418 		break;
1419 	default:
1420 		break;
1421 	}
1422 
1423 	for (i = RF_A; i < priv->rf_paths; i++) {
1424 		val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1425 		val32 &= ~MODE_AG_CHANNEL_MASK;
1426 		val32 |= channel;
1427 		rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1428 	}
1429 
1430 	rtl8xxxu_write16(priv, REG_WMAC_TRXPTCL_CTL, rf_mode_bw);
1431 	rtl8xxxu_write8(priv, REG_DATA_SUBCHANNEL, subchannel);
1432 
1433 	if (ht)
1434 		val8 = 0x0e;
1435 	else
1436 		val8 = 0x0a;
1437 
1438 	rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1439 	rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1440 
1441 	rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1442 	rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1443 
1444 	for (i = RF_A; i < priv->rf_paths; i++) {
1445 		val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1446 		val32 &= ~MODE_AG_BW_MASK;
1447 		switch(hw->conf.chandef.width) {
1448 		case NL80211_CHAN_WIDTH_80:
1449 			val32 |= MODE_AG_BW_80MHZ_8723B;
1450 			break;
1451 		case NL80211_CHAN_WIDTH_40:
1452 			val32 |= MODE_AG_BW_40MHZ_8723B;
1453 			break;
1454 		default:
1455 			val32 |= MODE_AG_BW_20MHZ_8723B;
1456 			break;
1457 		}
1458 		rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1459 	}
1460 }
1461 
1462 void
1463 rtl8xxxu_gen1_set_tx_power(struct rtl8xxxu_priv *priv, int channel, bool ht40)
1464 {
1465 	struct rtl8xxxu_power_base *power_base = priv->power_base;
1466 	u8 cck[RTL8723A_MAX_RF_PATHS], ofdm[RTL8723A_MAX_RF_PATHS];
1467 	u8 ofdmbase[RTL8723A_MAX_RF_PATHS], mcsbase[RTL8723A_MAX_RF_PATHS];
1468 	u32 val32, ofdm_a, ofdm_b, mcs_a, mcs_b;
1469 	u8 val8, base;
1470 	int group, i;
1471 
1472 	group = rtl8xxxu_gen1_channel_to_group(channel);
1473 
1474 	cck[0] = priv->cck_tx_power_index_A[group];
1475 	cck[1] = priv->cck_tx_power_index_B[group];
1476 
1477 	if (priv->hi_pa) {
1478 		if (cck[0] > 0x20)
1479 			cck[0] = 0x20;
1480 		if (cck[1] > 0x20)
1481 			cck[1] = 0x20;
1482 	}
1483 
1484 	ofdm[0] = priv->ht40_1s_tx_power_index_A[group];
1485 	ofdm[1] = priv->ht40_1s_tx_power_index_B[group];
1486 
1487 	ofdmbase[0] = ofdm[0] +	priv->ofdm_tx_power_index_diff[group].a;
1488 	ofdmbase[1] = ofdm[1] +	priv->ofdm_tx_power_index_diff[group].b;
1489 
1490 	mcsbase[0] = ofdm[0];
1491 	mcsbase[1] = ofdm[1];
1492 	if (!ht40) {
1493 		mcsbase[0] += priv->ht20_tx_power_index_diff[group].a;
1494 		mcsbase[1] += priv->ht20_tx_power_index_diff[group].b;
1495 	}
1496 
1497 	if (priv->tx_paths > 1) {
1498 		if (ofdm[0] > priv->ht40_2s_tx_power_index_diff[group].a)
1499 			ofdm[0] -=  priv->ht40_2s_tx_power_index_diff[group].a;
1500 		if (ofdm[1] > priv->ht40_2s_tx_power_index_diff[group].b)
1501 			ofdm[1] -=  priv->ht40_2s_tx_power_index_diff[group].b;
1502 	}
1503 
1504 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
1505 		dev_info(&priv->udev->dev,
1506 			 "%s: Setting TX power CCK A: %02x, "
1507 			 "CCK B: %02x, OFDM A: %02x, OFDM B: %02x\n",
1508 			 __func__, cck[0], cck[1], ofdm[0], ofdm[1]);
1509 
1510 	for (i = 0; i < RTL8723A_MAX_RF_PATHS; i++) {
1511 		if (cck[i] > RF6052_MAX_TX_PWR)
1512 			cck[i] = RF6052_MAX_TX_PWR;
1513 		if (ofdm[i] > RF6052_MAX_TX_PWR)
1514 			ofdm[i] = RF6052_MAX_TX_PWR;
1515 	}
1516 
1517 	val32 = rtl8xxxu_read32(priv, REG_TX_AGC_A_CCK1_MCS32);
1518 	val32 &= 0xffff00ff;
1519 	val32 |= (cck[0] << 8);
1520 	rtl8xxxu_write32(priv, REG_TX_AGC_A_CCK1_MCS32, val32);
1521 
1522 	val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1523 	val32 &= 0xff;
1524 	val32 |= ((cck[0] << 8) | (cck[0] << 16) | (cck[0] << 24));
1525 	rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1526 
1527 	val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1528 	val32 &= 0xffffff00;
1529 	val32 |= cck[1];
1530 	rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1531 
1532 	val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK1_55_MCS32);
1533 	val32 &= 0xff;
1534 	val32 |= ((cck[1] << 8) | (cck[1] << 16) | (cck[1] << 24));
1535 	rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK1_55_MCS32, val32);
1536 
1537 	ofdm_a = ofdmbase[0] | ofdmbase[0] << 8 |
1538 		ofdmbase[0] << 16 | ofdmbase[0] << 24;
1539 	ofdm_b = ofdmbase[1] | ofdmbase[1] << 8 |
1540 		ofdmbase[1] << 16 | ofdmbase[1] << 24;
1541 
1542 	rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE18_06,
1543 			 ofdm_a + power_base->reg_0e00);
1544 	rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE18_06,
1545 			 ofdm_b + power_base->reg_0830);
1546 
1547 	rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE54_24,
1548 			 ofdm_a + power_base->reg_0e04);
1549 	rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE54_24,
1550 			 ofdm_b + power_base->reg_0834);
1551 
1552 	mcs_a = mcsbase[0] | mcsbase[0] << 8 |
1553 		mcsbase[0] << 16 | mcsbase[0] << 24;
1554 	mcs_b = mcsbase[1] | mcsbase[1] << 8 |
1555 		mcsbase[1] << 16 | mcsbase[1] << 24;
1556 
1557 	rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS03_MCS00,
1558 			 mcs_a + power_base->reg_0e10);
1559 	rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS03_MCS00,
1560 			 mcs_b + power_base->reg_083c);
1561 
1562 	rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS07_MCS04,
1563 			 mcs_a + power_base->reg_0e14);
1564 	rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS07_MCS04,
1565 			 mcs_b + power_base->reg_0848);
1566 
1567 	rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS11_MCS08,
1568 			 mcs_a + power_base->reg_0e18);
1569 	rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS11_MCS08,
1570 			 mcs_b + power_base->reg_084c);
1571 
1572 	rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS15_MCS12,
1573 			 mcs_a + power_base->reg_0e1c);
1574 	val8 = u32_get_bits(mcs_a + power_base->reg_0e1c, 0xff000000);
1575 	for (i = 0; i < 3; i++) {
1576 		base = i != 2 ? 8 : 6;
1577 		val8 = max_t(int, val8 - base, 0);
1578 		rtl8xxxu_write8(priv, REG_OFDM0_XC_TX_IQ_IMBALANCE + i, val8);
1579 	}
1580 
1581 	rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS15_MCS12,
1582 			 mcs_b + power_base->reg_0868);
1583 	val8 = u32_get_bits(mcs_b + power_base->reg_0868, 0xff000000);
1584 	for (i = 0; i < 3; i++) {
1585 		base = i != 2 ? 8 : 6;
1586 		val8 = max_t(int, val8 - base, 0);
1587 		rtl8xxxu_write8(priv, REG_OFDM0_XD_TX_IQ_IMBALANCE + i, val8);
1588 	}
1589 }
1590 
1591 static void rtl8xxxu_set_linktype(struct rtl8xxxu_priv *priv,
1592 				  enum nl80211_iftype linktype, int port_num)
1593 {
1594 	u8 val8, type;
1595 
1596 	switch (linktype) {
1597 	case NL80211_IFTYPE_UNSPECIFIED:
1598 		type = MSR_LINKTYPE_NONE;
1599 		break;
1600 	case NL80211_IFTYPE_ADHOC:
1601 		type = MSR_LINKTYPE_ADHOC;
1602 		break;
1603 	case NL80211_IFTYPE_STATION:
1604 		type = MSR_LINKTYPE_STATION;
1605 		break;
1606 	case NL80211_IFTYPE_AP:
1607 		type = MSR_LINKTYPE_AP;
1608 		break;
1609 	default:
1610 		return;
1611 	}
1612 
1613 	switch (port_num) {
1614 	case 0:
1615 		val8 = rtl8xxxu_read8(priv, REG_MSR) & 0x0c;
1616 		val8 |= type;
1617 		break;
1618 	case 1:
1619 		val8 = rtl8xxxu_read8(priv, REG_MSR) & 0x03;
1620 		val8 |= type << 2;
1621 		break;
1622 	default:
1623 		return;
1624 	}
1625 
1626 	rtl8xxxu_write8(priv, REG_MSR, val8);
1627 }
1628 
1629 static void
1630 rtl8xxxu_set_retry(struct rtl8xxxu_priv *priv, u16 short_retry, u16 long_retry)
1631 {
1632 	u16 val16;
1633 
1634 	val16 = ((short_retry << RETRY_LIMIT_SHORT_SHIFT) &
1635 		 RETRY_LIMIT_SHORT_MASK) |
1636 		((long_retry << RETRY_LIMIT_LONG_SHIFT) &
1637 		 RETRY_LIMIT_LONG_MASK);
1638 
1639 	rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
1640 }
1641 
1642 static void
1643 rtl8xxxu_set_spec_sifs(struct rtl8xxxu_priv *priv, u16 cck, u16 ofdm)
1644 {
1645 	u16 val16;
1646 
1647 	val16 = ((cck << SPEC_SIFS_CCK_SHIFT) & SPEC_SIFS_CCK_MASK) |
1648 		((ofdm << SPEC_SIFS_OFDM_SHIFT) & SPEC_SIFS_OFDM_MASK);
1649 
1650 	rtl8xxxu_write16(priv, REG_SPEC_SIFS, val16);
1651 }
1652 
1653 static void rtl8xxxu_print_chipinfo(struct rtl8xxxu_priv *priv)
1654 {
1655 	struct device *dev = &priv->udev->dev;
1656 	char cut = 'A' + priv->chip_cut;
1657 
1658 	dev_info(dev,
1659 		 "RTL%s rev %c (%s) romver %d, %iT%iR, TX queues %i, WiFi=%i, BT=%i, GPS=%i, HI PA=%i\n",
1660 		 priv->chip_name, cut, priv->chip_vendor, priv->rom_rev,
1661 		 priv->tx_paths, priv->rx_paths, priv->ep_tx_count,
1662 		 priv->has_wifi, priv->has_bluetooth, priv->has_gps,
1663 		 priv->hi_pa);
1664 
1665 	dev_info(dev, "RTL%s MAC: %pM\n", priv->chip_name, priv->mac_addr);
1666 }
1667 
1668 void rtl8xxxu_identify_vendor_1bit(struct rtl8xxxu_priv *priv, u32 vendor)
1669 {
1670 	if (vendor) {
1671 		strscpy(priv->chip_vendor, "UMC", sizeof(priv->chip_vendor));
1672 		priv->vendor_umc = 1;
1673 	} else {
1674 		strscpy(priv->chip_vendor, "TSMC", sizeof(priv->chip_vendor));
1675 	}
1676 }
1677 
1678 void rtl8xxxu_identify_vendor_2bits(struct rtl8xxxu_priv *priv, u32 vendor)
1679 {
1680 	switch (vendor) {
1681 	case SYS_CFG_VENDOR_ID_TSMC:
1682 		strscpy(priv->chip_vendor, "TSMC", sizeof(priv->chip_vendor));
1683 		break;
1684 	case SYS_CFG_VENDOR_ID_SMIC:
1685 		strscpy(priv->chip_vendor, "SMIC", sizeof(priv->chip_vendor));
1686 		priv->vendor_smic = 1;
1687 		break;
1688 	case SYS_CFG_VENDOR_ID_UMC:
1689 		strscpy(priv->chip_vendor, "UMC", sizeof(priv->chip_vendor));
1690 		priv->vendor_umc = 1;
1691 		break;
1692 	default:
1693 		strscpy(priv->chip_vendor, "unknown", sizeof(priv->chip_vendor));
1694 	}
1695 }
1696 
1697 void rtl8xxxu_config_endpoints_sie(struct rtl8xxxu_priv *priv)
1698 {
1699 	u16 val16;
1700 
1701 	val16 = rtl8xxxu_read16(priv, REG_NORMAL_SIE_EP_TX);
1702 
1703 	if (val16 & NORMAL_SIE_EP_TX_HIGH_MASK) {
1704 		priv->ep_tx_high_queue = 1;
1705 		priv->ep_tx_count++;
1706 	}
1707 
1708 	if (val16 & NORMAL_SIE_EP_TX_NORMAL_MASK) {
1709 		priv->ep_tx_normal_queue = 1;
1710 		priv->ep_tx_count++;
1711 	}
1712 
1713 	if (val16 & NORMAL_SIE_EP_TX_LOW_MASK) {
1714 		priv->ep_tx_low_queue = 1;
1715 		priv->ep_tx_count++;
1716 	}
1717 }
1718 
1719 int rtl8xxxu_config_endpoints_no_sie(struct rtl8xxxu_priv *priv)
1720 {
1721 	struct device *dev = &priv->udev->dev;
1722 
1723 	switch (priv->nr_out_eps) {
1724 	case 6:
1725 	case 5:
1726 	case 4:
1727 	case 3:
1728 		priv->ep_tx_low_queue = 1;
1729 		priv->ep_tx_count++;
1730 		fallthrough;
1731 	case 2:
1732 		priv->ep_tx_normal_queue = 1;
1733 		priv->ep_tx_count++;
1734 		fallthrough;
1735 	case 1:
1736 		priv->ep_tx_high_queue = 1;
1737 		priv->ep_tx_count++;
1738 		break;
1739 	default:
1740 		dev_info(dev, "Unsupported USB TX end-points\n");
1741 		return -ENOTSUPP;
1742 	}
1743 
1744 	return 0;
1745 }
1746 
1747 int
1748 rtl8xxxu_read_efuse8(struct rtl8xxxu_priv *priv, u16 offset, u8 *data)
1749 {
1750 	int i;
1751 	u8 val8;
1752 	u32 val32;
1753 
1754 	/* Write Address */
1755 	rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 1, offset & 0xff);
1756 	val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 2);
1757 	val8 &= 0xfc;
1758 	val8 |= (offset >> 8) & 0x03;
1759 	rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 2, val8);
1760 
1761 	val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 3);
1762 	rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 3, val8 & 0x7f);
1763 
1764 	/* Poll for data read */
1765 	val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1766 	for (i = 0; i < RTL8XXXU_MAX_REG_POLL; i++) {
1767 		val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1768 		if (val32 & BIT(31))
1769 			break;
1770 	}
1771 
1772 	if (i == RTL8XXXU_MAX_REG_POLL)
1773 		return -EIO;
1774 
1775 	udelay(50);
1776 	val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1777 
1778 	*data = val32 & 0xff;
1779 	return 0;
1780 }
1781 
1782 int rtl8xxxu_read_efuse(struct rtl8xxxu_priv *priv)
1783 {
1784 	struct device *dev = &priv->udev->dev;
1785 	int i, ret = 0;
1786 	u8 val8, word_mask, header, extheader;
1787 	u16 val16, efuse_addr, offset;
1788 	u32 val32;
1789 
1790 	val16 = rtl8xxxu_read16(priv, REG_9346CR);
1791 	if (val16 & EEPROM_ENABLE)
1792 		priv->has_eeprom = 1;
1793 	if (val16 & EEPROM_BOOT)
1794 		priv->boot_eeprom = 1;
1795 
1796 	if (priv->is_multi_func) {
1797 		val32 = rtl8xxxu_read32(priv, REG_EFUSE_TEST);
1798 		val32 = (val32 & ~EFUSE_SELECT_MASK) | EFUSE_WIFI_SELECT;
1799 		rtl8xxxu_write32(priv, REG_EFUSE_TEST, val32);
1800 	}
1801 
1802 	dev_dbg(dev, "Booting from %s\n",
1803 		priv->boot_eeprom ? "EEPROM" : "EFUSE");
1804 
1805 	rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_ENABLE);
1806 
1807 	/*  1.2V Power: From VDDON with Power Cut(0x0000[15]), default valid */
1808 	val16 = rtl8xxxu_read16(priv, REG_SYS_ISO_CTRL);
1809 	if (!(val16 & SYS_ISO_PWC_EV12V)) {
1810 		val16 |= SYS_ISO_PWC_EV12V;
1811 		rtl8xxxu_write16(priv, REG_SYS_ISO_CTRL, val16);
1812 	}
1813 	/*  Reset: 0x0000[28], default valid */
1814 	val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1815 	if (!(val16 & SYS_FUNC_ELDR)) {
1816 		val16 |= SYS_FUNC_ELDR;
1817 		rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
1818 	}
1819 
1820 	/*
1821 	 * Clock: Gated(0x0008[5]) 8M(0x0008[1]) clock from ANA, default valid
1822 	 */
1823 	val16 = rtl8xxxu_read16(priv, REG_SYS_CLKR);
1824 	if (!(val16 & SYS_CLK_LOADER_ENABLE) || !(val16 & SYS_CLK_ANA8M)) {
1825 		val16 |= (SYS_CLK_LOADER_ENABLE | SYS_CLK_ANA8M);
1826 		rtl8xxxu_write16(priv, REG_SYS_CLKR, val16);
1827 	}
1828 
1829 	/* Default value is 0xff */
1830 	memset(priv->efuse_wifi.raw, 0xff, EFUSE_MAP_LEN);
1831 
1832 	efuse_addr = 0;
1833 	while (efuse_addr < EFUSE_REAL_CONTENT_LEN_8723A) {
1834 		u16 map_addr;
1835 
1836 		ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &header);
1837 		if (ret || header == 0xff)
1838 			goto exit;
1839 
1840 		if ((header & 0x1f) == 0x0f) {	/* extended header */
1841 			offset = (header & 0xe0) >> 5;
1842 
1843 			ret = rtl8xxxu_read_efuse8(priv, efuse_addr++,
1844 						   &extheader);
1845 			if (ret)
1846 				goto exit;
1847 			/* All words disabled */
1848 			if ((extheader & 0x0f) == 0x0f)
1849 				continue;
1850 
1851 			offset |= ((extheader & 0xf0) >> 1);
1852 			word_mask = extheader & 0x0f;
1853 		} else {
1854 			offset = (header >> 4) & 0x0f;
1855 			word_mask = header & 0x0f;
1856 		}
1857 
1858 		/* Get word enable value from PG header */
1859 
1860 		/* We have 8 bits to indicate validity */
1861 		map_addr = offset * 8;
1862 		for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
1863 			/* Check word enable condition in the section */
1864 			if (word_mask & BIT(i)) {
1865 				map_addr += 2;
1866 				continue;
1867 			}
1868 
1869 			ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1870 			if (ret)
1871 				goto exit;
1872 			if (map_addr >= EFUSE_MAP_LEN - 1) {
1873 				dev_warn(dev, "%s: Illegal map_addr (%04x), "
1874 					 "efuse corrupt!\n",
1875 					 __func__, map_addr);
1876 				ret = -EINVAL;
1877 				goto exit;
1878 			}
1879 			priv->efuse_wifi.raw[map_addr++] = val8;
1880 
1881 			ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1882 			if (ret)
1883 				goto exit;
1884 			priv->efuse_wifi.raw[map_addr++] = val8;
1885 		}
1886 	}
1887 
1888 exit:
1889 	rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_DISABLE);
1890 
1891 	return ret;
1892 }
1893 
1894 static void rtl8xxxu_dump_efuse(struct rtl8xxxu_priv *priv)
1895 {
1896 	dev_info(&priv->udev->dev,
1897 		 "Dumping efuse for RTL%s (0x%02x bytes):\n",
1898 		 priv->chip_name, EFUSE_MAP_LEN);
1899 
1900 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1901 		       priv->efuse_wifi.raw, EFUSE_MAP_LEN, true);
1902 }
1903 
1904 static ssize_t read_file_efuse(struct file *file, char __user *user_buf,
1905 			       size_t count, loff_t *ppos)
1906 {
1907 	struct rtl8xxxu_priv *priv = file_inode(file)->i_private;
1908 
1909 	return simple_read_from_buffer(user_buf, count, ppos,
1910 				       priv->efuse_wifi.raw, EFUSE_MAP_LEN);
1911 }
1912 
1913 static const struct debugfs_short_fops fops_efuse = {
1914 	.read = read_file_efuse,
1915 };
1916 
1917 static void rtl8xxxu_debugfs_init(struct rtl8xxxu_priv *priv)
1918 {
1919 	struct dentry *phydir;
1920 
1921 	phydir = debugfs_create_dir("rtl8xxxu", priv->hw->wiphy->debugfsdir);
1922 	debugfs_create_file("efuse", 0400, phydir, priv, &fops_efuse);
1923 }
1924 
1925 void rtl8xxxu_reset_8051(struct rtl8xxxu_priv *priv)
1926 {
1927 	u8 val8;
1928 	u16 sys_func;
1929 
1930 	val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
1931 	val8 &= ~BIT(0);
1932 	rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
1933 
1934 	sys_func = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1935 	sys_func &= ~SYS_FUNC_CPU_ENABLE;
1936 	rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
1937 
1938 	val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
1939 	val8 |= BIT(0);
1940 	rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
1941 
1942 	sys_func |= SYS_FUNC_CPU_ENABLE;
1943 	rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
1944 }
1945 
1946 static int rtl8xxxu_start_firmware(struct rtl8xxxu_priv *priv)
1947 {
1948 	struct device *dev = &priv->udev->dev;
1949 	u16 reg_mcu_fw_dl;
1950 	int ret = 0, i;
1951 	u32 val32;
1952 
1953 	if (priv->rtl_chip == RTL8710B)
1954 		reg_mcu_fw_dl = REG_8051FW_CTRL_V1_8710B;
1955 	else
1956 		reg_mcu_fw_dl = REG_MCU_FW_DL;
1957 
1958 	/* Poll checksum report */
1959 	for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1960 		val32 = rtl8xxxu_read32(priv, reg_mcu_fw_dl);
1961 		if (val32 & MCU_FW_DL_CSUM_REPORT)
1962 			break;
1963 	}
1964 
1965 	if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1966 		dev_warn(dev, "Firmware checksum poll timed out\n");
1967 		ret = -EAGAIN;
1968 		goto exit;
1969 	}
1970 
1971 	val32 = rtl8xxxu_read32(priv, reg_mcu_fw_dl);
1972 	val32 |= MCU_FW_DL_READY;
1973 	val32 &= ~MCU_WINT_INIT_READY;
1974 	rtl8xxxu_write32(priv, reg_mcu_fw_dl, val32);
1975 
1976 	/*
1977 	 * Reset the 8051 in order for the firmware to start running,
1978 	 * otherwise it won't come up on the 8192eu
1979 	 */
1980 	priv->fops->reset_8051(priv);
1981 
1982 	/* Wait for firmware to become ready */
1983 	for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1984 		val32 = rtl8xxxu_read32(priv, reg_mcu_fw_dl);
1985 		if (val32 & MCU_WINT_INIT_READY)
1986 			break;
1987 
1988 		udelay(100);
1989 	}
1990 
1991 	if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1992 		dev_warn(dev, "Firmware failed to start\n");
1993 		ret = -EAGAIN;
1994 		goto exit;
1995 	}
1996 
1997 	/*
1998 	 * Init H2C command
1999 	 */
2000 	if (priv->fops->init_reg_hmtfr)
2001 		rtl8xxxu_write8(priv, REG_HMTFR, 0x0f);
2002 exit:
2003 	return ret;
2004 }
2005 
2006 static int rtl8xxxu_download_firmware(struct rtl8xxxu_priv *priv)
2007 {
2008 	int pages, remainder, i, ret;
2009 	u16 reg_fw_start_address;
2010 	u16 reg_mcu_fw_dl;
2011 	u8 val8;
2012 	u16 val16;
2013 	u32 val32;
2014 	u8 *fwptr;
2015 
2016 	if (priv->rtl_chip == RTL8192F)
2017 		reg_fw_start_address = REG_FW_START_ADDRESS_8192F;
2018 	else
2019 		reg_fw_start_address = REG_FW_START_ADDRESS;
2020 
2021 	if (priv->rtl_chip == RTL8710B) {
2022 		reg_mcu_fw_dl = REG_8051FW_CTRL_V1_8710B;
2023 	} else {
2024 		reg_mcu_fw_dl = REG_MCU_FW_DL;
2025 
2026 		val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC + 1);
2027 		val8 |= 4;
2028 		rtl8xxxu_write8(priv, REG_SYS_FUNC + 1, val8);
2029 
2030 		/* 8051 enable */
2031 		val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2032 		val16 |= SYS_FUNC_CPU_ENABLE;
2033 		rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
2034 	}
2035 
2036 	val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl);
2037 	if (val8 & MCU_FW_RAM_SEL) {
2038 		dev_info(&priv->udev->dev,
2039 			 "Firmware is already running, resetting the MCU.\n");
2040 		rtl8xxxu_write8(priv, reg_mcu_fw_dl, 0x00);
2041 		priv->fops->reset_8051(priv);
2042 	}
2043 
2044 	/* MCU firmware download enable */
2045 	val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl);
2046 	val8 |= MCU_FW_DL_ENABLE;
2047 	rtl8xxxu_write8(priv, reg_mcu_fw_dl, val8);
2048 
2049 	/* 8051 reset */
2050 	val32 = rtl8xxxu_read32(priv, reg_mcu_fw_dl);
2051 	val32 &= ~BIT(19);
2052 	rtl8xxxu_write32(priv, reg_mcu_fw_dl, val32);
2053 
2054 	if (priv->rtl_chip == RTL8710B) {
2055 		/* We must set 0x8090[8]=1 before download FW. */
2056 		val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl + 1);
2057 		val8 |= BIT(0);
2058 		rtl8xxxu_write8(priv, reg_mcu_fw_dl + 1, val8);
2059 	}
2060 
2061 	/* Reset firmware download checksum */
2062 	val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl);
2063 	val8 |= MCU_FW_DL_CSUM_REPORT;
2064 	rtl8xxxu_write8(priv, reg_mcu_fw_dl, val8);
2065 
2066 	pages = priv->fw_size / RTL_FW_PAGE_SIZE;
2067 	remainder = priv->fw_size % RTL_FW_PAGE_SIZE;
2068 
2069 	fwptr = priv->fw_data->data;
2070 
2071 	for (i = 0; i < pages; i++) {
2072 		val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl + 2) & 0xF8;
2073 		val8 |= i;
2074 		rtl8xxxu_write8(priv, reg_mcu_fw_dl + 2, val8);
2075 
2076 		ret = rtl8xxxu_writeN(priv, reg_fw_start_address,
2077 				      fwptr, RTL_FW_PAGE_SIZE);
2078 		if (ret != RTL_FW_PAGE_SIZE) {
2079 			ret = -EAGAIN;
2080 			goto fw_abort;
2081 		}
2082 
2083 		fwptr += RTL_FW_PAGE_SIZE;
2084 	}
2085 
2086 	if (remainder) {
2087 		val8 = rtl8xxxu_read8(priv, reg_mcu_fw_dl + 2) & 0xF8;
2088 		val8 |= i;
2089 		rtl8xxxu_write8(priv, reg_mcu_fw_dl + 2, val8);
2090 		ret = rtl8xxxu_writeN(priv, reg_fw_start_address,
2091 				      fwptr, remainder);
2092 		if (ret != remainder) {
2093 			ret = -EAGAIN;
2094 			goto fw_abort;
2095 		}
2096 	}
2097 
2098 	ret = 0;
2099 fw_abort:
2100 	/* MCU firmware download disable */
2101 	val16 = rtl8xxxu_read16(priv, reg_mcu_fw_dl);
2102 	val16 &= ~MCU_FW_DL_ENABLE;
2103 	rtl8xxxu_write16(priv, reg_mcu_fw_dl, val16);
2104 
2105 	return ret;
2106 }
2107 
2108 int rtl8xxxu_load_firmware(struct rtl8xxxu_priv *priv, const char *fw_name)
2109 {
2110 	struct device *dev = &priv->udev->dev;
2111 	const struct firmware *fw;
2112 	int ret = 0;
2113 	u16 signature;
2114 
2115 	dev_info(dev, "%s: Loading firmware %s\n", DRIVER_NAME, fw_name);
2116 	if (request_firmware(&fw, fw_name, &priv->udev->dev)) {
2117 		dev_warn(dev, "request_firmware(%s) failed\n", fw_name);
2118 		ret = -EAGAIN;
2119 		goto exit;
2120 	}
2121 	if (!fw) {
2122 		dev_warn(dev, "Firmware data not available\n");
2123 		ret = -EINVAL;
2124 		goto exit;
2125 	}
2126 
2127 	priv->fw_data = kmemdup(fw->data, fw->size, GFP_KERNEL);
2128 	if (!priv->fw_data) {
2129 		ret = -ENOMEM;
2130 		goto exit;
2131 	}
2132 	priv->fw_size = fw->size - sizeof(struct rtl8xxxu_firmware_header);
2133 
2134 	signature = le16_to_cpu(priv->fw_data->signature);
2135 	switch (signature & 0xfff0) {
2136 	case 0x92e0:
2137 	case 0x92c0:
2138 	case 0x88e0:
2139 	case 0x88c0:
2140 	case 0x5300:
2141 	case 0x2300:
2142 	case 0x88f0:
2143 	case 0x10b0:
2144 	case 0x92f0:
2145 		break;
2146 	default:
2147 		ret = -EINVAL;
2148 		dev_warn(dev, "%s: Invalid firmware signature: 0x%04x\n",
2149 			 __func__, signature);
2150 	}
2151 
2152 	dev_info(dev, "Firmware revision %i.%i (signature 0x%04x)\n",
2153 		 le16_to_cpu(priv->fw_data->major_version),
2154 		 priv->fw_data->minor_version, signature);
2155 
2156 exit:
2157 	release_firmware(fw);
2158 	return ret;
2159 }
2160 
2161 void rtl8xxxu_firmware_self_reset(struct rtl8xxxu_priv *priv)
2162 {
2163 	u16 val16;
2164 	int i = 100;
2165 
2166 	/* Inform 8051 to perform reset */
2167 	rtl8xxxu_write8(priv, REG_HMTFR + 3, 0x20);
2168 
2169 	for (i = 100; i > 0; i--) {
2170 		val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2171 
2172 		if (!(val16 & SYS_FUNC_CPU_ENABLE)) {
2173 			dev_dbg(&priv->udev->dev,
2174 				"%s: Firmware self reset success!\n", __func__);
2175 			break;
2176 		}
2177 		udelay(50);
2178 	}
2179 
2180 	if (!i) {
2181 		/* Force firmware reset */
2182 		val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2183 		val16 &= ~SYS_FUNC_CPU_ENABLE;
2184 		rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
2185 	}
2186 }
2187 
2188 static int
2189 rtl8xxxu_init_mac(struct rtl8xxxu_priv *priv)
2190 {
2191 	const struct rtl8xxxu_reg8val *array = priv->fops->mactable;
2192 	int i, ret;
2193 	u16 reg;
2194 	u8 val;
2195 
2196 	for (i = 0; ; i++) {
2197 		reg = array[i].reg;
2198 		val = array[i].val;
2199 
2200 		if (reg == 0xffff && val == 0xff)
2201 			break;
2202 
2203 		ret = rtl8xxxu_write8(priv, reg, val);
2204 		if (ret != 1) {
2205 			dev_warn(&priv->udev->dev,
2206 				 "Failed to initialize MAC "
2207 				 "(reg: %04x, val %02x)\n", reg, val);
2208 			return -EAGAIN;
2209 		}
2210 	}
2211 
2212 	switch (priv->rtl_chip) {
2213 	case RTL8188C:
2214 	case RTL8188R:
2215 	case RTL8191C:
2216 	case RTL8192C:
2217 	case RTL8723A:
2218 		rtl8xxxu_write8(priv, REG_MAX_AGGR_NUM, 0x0a);
2219 		break;
2220 	case RTL8188E:
2221 		rtl8xxxu_write16(priv, REG_MAX_AGGR_NUM, 0x0707);
2222 		break;
2223 	default:
2224 		break;
2225 	}
2226 
2227 	return 0;
2228 }
2229 
2230 int rtl8xxxu_init_phy_regs(struct rtl8xxxu_priv *priv,
2231 			   const struct rtl8xxxu_reg32val *array)
2232 {
2233 	int i, ret;
2234 	u16 reg;
2235 	u32 val;
2236 
2237 	for (i = 0; ; i++) {
2238 		reg = array[i].reg;
2239 		val = array[i].val;
2240 
2241 		if (reg == 0xffff && val == 0xffffffff)
2242 			break;
2243 
2244 		ret = rtl8xxxu_write32(priv, reg, val);
2245 		if (ret != sizeof(val)) {
2246 			dev_warn(&priv->udev->dev,
2247 				 "Failed to initialize PHY\n");
2248 			return -EAGAIN;
2249 		}
2250 		udelay(1);
2251 	}
2252 
2253 	return 0;
2254 }
2255 
2256 void rtl8xxxu_gen1_init_phy_bb(struct rtl8xxxu_priv *priv)
2257 {
2258 	u8 val8, ldoa15, ldov12d, lpldo, ldohci12;
2259 	u16 val16;
2260 	u32 val32;
2261 
2262 	val8 = rtl8xxxu_read8(priv, REG_AFE_PLL_CTRL);
2263 	udelay(2);
2264 	val8 |= AFE_PLL_320_ENABLE;
2265 	rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL, val8);
2266 	udelay(2);
2267 
2268 	rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL + 1, 0xff);
2269 	udelay(2);
2270 
2271 	val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2272 	val16 |= SYS_FUNC_BB_GLB_RSTN | SYS_FUNC_BBRSTB;
2273 	rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
2274 
2275 	val32 = rtl8xxxu_read32(priv, REG_AFE_XTAL_CTRL);
2276 	val32 &= ~AFE_XTAL_RF_GATE;
2277 	if (priv->has_bluetooth)
2278 		val32 &= ~AFE_XTAL_BT_GATE;
2279 	rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, val32);
2280 
2281 	/* 6. 0x1f[7:0] = 0x07 */
2282 	val8 = RF_ENABLE | RF_RSTB | RF_SDMRSTB;
2283 	rtl8xxxu_write8(priv, REG_RF_CTRL, val8);
2284 
2285 	if (priv->hi_pa)
2286 		rtl8xxxu_init_phy_regs(priv, rtl8188ru_phy_1t_highpa_table);
2287 	else if (priv->tx_paths == 2)
2288 		rtl8xxxu_init_phy_regs(priv, rtl8192cu_phy_2t_init_table);
2289 	else
2290 		rtl8xxxu_init_phy_regs(priv, rtl8723a_phy_1t_init_table);
2291 
2292 	if (priv->rtl_chip == RTL8188R && priv->hi_pa &&
2293 	    priv->vendor_umc && priv->chip_cut == 1)
2294 		rtl8xxxu_write8(priv, REG_OFDM0_AGC_PARM1 + 2, 0x50);
2295 
2296 	if (priv->hi_pa)
2297 		rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_highpa_table);
2298 	else
2299 		rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_standard_table);
2300 
2301 	ldoa15 = LDOA15_ENABLE | LDOA15_OBUF;
2302 	ldov12d = LDOV12D_ENABLE | BIT(2) | (2 << LDOV12D_VADJ_SHIFT);
2303 	ldohci12 = 0x57;
2304 	lpldo = 1;
2305 	val32 = (lpldo << 24) | (ldohci12 << 16) | (ldov12d << 8) | ldoa15;
2306 	rtl8xxxu_write32(priv, REG_LDOA15_CTRL, val32);
2307 }
2308 
2309 /*
2310  * Most of this is black magic retrieved from the old rtl8723au driver
2311  */
2312 static int rtl8xxxu_init_phy_bb(struct rtl8xxxu_priv *priv)
2313 {
2314 	u32 val32;
2315 
2316 	priv->fops->init_phy_bb(priv);
2317 
2318 	if (priv->tx_paths == 1 && priv->rx_paths == 2) {
2319 		/*
2320 		 * For 1T2R boards, patch the registers.
2321 		 *
2322 		 * It looks like 8191/2 1T2R boards use path B for TX
2323 		 */
2324 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_TX_INFO);
2325 		val32 &= ~(BIT(0) | BIT(1));
2326 		val32 |= BIT(1);
2327 		rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, val32);
2328 
2329 		val32 = rtl8xxxu_read32(priv, REG_FPGA1_TX_INFO);
2330 		val32 &= ~0x300033;
2331 		val32 |= 0x200022;
2332 		rtl8xxxu_write32(priv, REG_FPGA1_TX_INFO, val32);
2333 
2334 		val32 = rtl8xxxu_read32(priv, REG_CCK0_AFE_SETTING);
2335 		val32 &= ~CCK0_AFE_RX_MASK;
2336 		val32 &= 0x00ffffff;
2337 		val32 |= 0x40000000;
2338 		val32 |= CCK0_AFE_RX_ANT_B;
2339 		rtl8xxxu_write32(priv, REG_CCK0_AFE_SETTING, val32);
2340 
2341 		val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
2342 		val32 &= ~(OFDM_RF_PATH_RX_MASK | OFDM_RF_PATH_TX_MASK);
2343 		val32 |= (OFDM_RF_PATH_RX_A | OFDM_RF_PATH_RX_B |
2344 			  OFDM_RF_PATH_TX_B);
2345 		rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
2346 
2347 		val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGC_PARM1);
2348 		val32 &= ~(BIT(4) | BIT(5));
2349 		val32 |= BIT(4);
2350 		rtl8xxxu_write32(priv, REG_OFDM0_AGC_PARM1, val32);
2351 
2352 		val32 = rtl8xxxu_read32(priv, REG_TX_CCK_RFON);
2353 		val32 &= ~(BIT(27) | BIT(26));
2354 		val32 |= BIT(27);
2355 		rtl8xxxu_write32(priv, REG_TX_CCK_RFON, val32);
2356 
2357 		val32 = rtl8xxxu_read32(priv, REG_TX_CCK_BBON);
2358 		val32 &= ~(BIT(27) | BIT(26));
2359 		val32 |= BIT(27);
2360 		rtl8xxxu_write32(priv, REG_TX_CCK_BBON, val32);
2361 
2362 		val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_RFON);
2363 		val32 &= ~(BIT(27) | BIT(26));
2364 		val32 |= BIT(27);
2365 		rtl8xxxu_write32(priv, REG_TX_OFDM_RFON, val32);
2366 
2367 		val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_BBON);
2368 		val32 &= ~(BIT(27) | BIT(26));
2369 		val32 |= BIT(27);
2370 		rtl8xxxu_write32(priv, REG_TX_OFDM_BBON, val32);
2371 
2372 		val32 = rtl8xxxu_read32(priv, REG_TX_TO_TX);
2373 		val32 &= ~(BIT(27) | BIT(26));
2374 		val32 |= BIT(27);
2375 		rtl8xxxu_write32(priv, REG_TX_TO_TX, val32);
2376 	}
2377 
2378 	if (priv->fops->set_crystal_cap)
2379 		priv->fops->set_crystal_cap(priv, priv->default_crystal_cap);
2380 
2381 	if (priv->rtl_chip == RTL8192E)
2382 		rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, 0x000f81fb);
2383 
2384 	return 0;
2385 }
2386 
2387 static int rtl8xxxu_init_rf_regs(struct rtl8xxxu_priv *priv,
2388 				 const struct rtl8xxxu_rfregval *array,
2389 				 enum rtl8xxxu_rfpath path)
2390 {
2391 	int i, ret;
2392 	u8 reg;
2393 	u32 val;
2394 
2395 	for (i = 0; ; i++) {
2396 		reg = array[i].reg;
2397 		val = array[i].val;
2398 
2399 		if (reg == 0xff && val == 0xffffffff)
2400 			break;
2401 
2402 		switch (reg) {
2403 		case 0xfe:
2404 			msleep(50);
2405 			continue;
2406 		case 0xfd:
2407 			mdelay(5);
2408 			continue;
2409 		case 0xfc:
2410 			mdelay(1);
2411 			continue;
2412 		case 0xfb:
2413 			udelay(50);
2414 			continue;
2415 		case 0xfa:
2416 			udelay(5);
2417 			continue;
2418 		case 0xf9:
2419 			udelay(1);
2420 			continue;
2421 		}
2422 
2423 		ret = rtl8xxxu_write_rfreg(priv, path, reg, val);
2424 		if (ret) {
2425 			dev_warn(&priv->udev->dev,
2426 				 "Failed to initialize RF\n");
2427 			return -EAGAIN;
2428 		}
2429 		udelay(1);
2430 	}
2431 
2432 	return 0;
2433 }
2434 
2435 int rtl8xxxu_init_phy_rf(struct rtl8xxxu_priv *priv,
2436 			 const struct rtl8xxxu_rfregval *table,
2437 			 enum rtl8xxxu_rfpath path)
2438 {
2439 	u32 val32;
2440 	u16 val16, rfsi_rfenv;
2441 	u16 reg_sw_ctrl, reg_int_oe, reg_hssi_parm2;
2442 
2443 	switch (path) {
2444 	case RF_A:
2445 		reg_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL;
2446 		reg_int_oe = REG_FPGA0_XA_RF_INT_OE;
2447 		reg_hssi_parm2 = REG_FPGA0_XA_HSSI_PARM2;
2448 		break;
2449 	case RF_B:
2450 		reg_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL;
2451 		reg_int_oe = REG_FPGA0_XB_RF_INT_OE;
2452 		reg_hssi_parm2 = REG_FPGA0_XB_HSSI_PARM2;
2453 		break;
2454 	default:
2455 		dev_err(&priv->udev->dev, "%s:Unsupported RF path %c\n",
2456 			__func__, path + 'A');
2457 		return -EINVAL;
2458 	}
2459 	/* For path B, use XB */
2460 	rfsi_rfenv = rtl8xxxu_read16(priv, reg_sw_ctrl);
2461 	rfsi_rfenv &= FPGA0_RF_RFENV;
2462 
2463 	/*
2464 	 * These two we might be able to optimize into one
2465 	 */
2466 	val32 = rtl8xxxu_read32(priv, reg_int_oe);
2467 	val32 |= BIT(20);	/* 0x10 << 16 */
2468 	rtl8xxxu_write32(priv, reg_int_oe, val32);
2469 	udelay(1);
2470 
2471 	val32 = rtl8xxxu_read32(priv, reg_int_oe);
2472 	val32 |= BIT(4);
2473 	rtl8xxxu_write32(priv, reg_int_oe, val32);
2474 	udelay(1);
2475 
2476 	/*
2477 	 * These two we might be able to optimize into one
2478 	 */
2479 	val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2480 	val32 &= ~FPGA0_HSSI_3WIRE_ADDR_LEN;
2481 	rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2482 	udelay(1);
2483 
2484 	val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2485 	val32 &= ~FPGA0_HSSI_3WIRE_DATA_LEN;
2486 	rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2487 	udelay(1);
2488 
2489 	rtl8xxxu_init_rf_regs(priv, table, path);
2490 
2491 	/* For path B, use XB */
2492 	val16 = rtl8xxxu_read16(priv, reg_sw_ctrl);
2493 	val16 &= ~FPGA0_RF_RFENV;
2494 	val16 |= rfsi_rfenv;
2495 	rtl8xxxu_write16(priv, reg_sw_ctrl, val16);
2496 
2497 	return 0;
2498 }
2499 
2500 static int rtl8xxxu_llt_write(struct rtl8xxxu_priv *priv, u8 address, u8 data)
2501 {
2502 	int ret = -EBUSY;
2503 	int count = 0;
2504 	u32 value;
2505 
2506 	value = LLT_OP_WRITE | address << 8 | data;
2507 
2508 	rtl8xxxu_write32(priv, REG_LLT_INIT, value);
2509 
2510 	do {
2511 		value = rtl8xxxu_read32(priv, REG_LLT_INIT);
2512 		if ((value & LLT_OP_MASK) == LLT_OP_INACTIVE) {
2513 			ret = 0;
2514 			break;
2515 		}
2516 	} while (count++ < 20);
2517 
2518 	return ret;
2519 }
2520 
2521 int rtl8xxxu_init_llt_table(struct rtl8xxxu_priv *priv)
2522 {
2523 	int ret;
2524 	int i, last_entry;
2525 	u8 last_tx_page;
2526 
2527 	last_tx_page = priv->fops->total_page_num;
2528 
2529 	if (priv->fops->last_llt_entry)
2530 		last_entry = priv->fops->last_llt_entry;
2531 	else
2532 		last_entry = 255;
2533 
2534 	for (i = 0; i < last_tx_page; i++) {
2535 		ret = rtl8xxxu_llt_write(priv, i, i + 1);
2536 		if (ret)
2537 			goto exit;
2538 	}
2539 
2540 	ret = rtl8xxxu_llt_write(priv, last_tx_page, 0xff);
2541 	if (ret)
2542 		goto exit;
2543 
2544 	/* Mark remaining pages as a ring buffer */
2545 	for (i = last_tx_page + 1; i < last_entry; i++) {
2546 		ret = rtl8xxxu_llt_write(priv, i, (i + 1));
2547 		if (ret)
2548 			goto exit;
2549 	}
2550 
2551 	/*  Let last entry point to the start entry of ring buffer */
2552 	ret = rtl8xxxu_llt_write(priv, last_entry, last_tx_page + 1);
2553 	if (ret)
2554 		goto exit;
2555 
2556 exit:
2557 	return ret;
2558 }
2559 
2560 int rtl8xxxu_auto_llt_table(struct rtl8xxxu_priv *priv)
2561 {
2562 	u32 val32;
2563 	int ret = 0;
2564 	int i;
2565 
2566 	val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
2567 	val32 |= AUTO_LLT_INIT_LLT;
2568 	rtl8xxxu_write32(priv, REG_AUTO_LLT, val32);
2569 
2570 	for (i = 500; i; i--) {
2571 		val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
2572 		if (!(val32 & AUTO_LLT_INIT_LLT))
2573 			break;
2574 		usleep_range(2, 4);
2575 	}
2576 
2577 	if (!i) {
2578 		ret = -EBUSY;
2579 		dev_warn(&priv->udev->dev, "LLT table init failed\n");
2580 	}
2581 
2582 	return ret;
2583 }
2584 
2585 static int rtl8xxxu_init_queue_priority(struct rtl8xxxu_priv *priv)
2586 {
2587 	u16 val16, hi, lo;
2588 	u16 hiq, mgq, bkq, beq, viq, voq;
2589 	int hip, mgp, bkp, bep, vip, vop;
2590 	int ret = 0;
2591 	u32 val32;
2592 
2593 	switch (priv->ep_tx_count) {
2594 	case 1:
2595 		if (priv->ep_tx_high_queue) {
2596 			hi = TRXDMA_QUEUE_HIGH;
2597 		} else if (priv->ep_tx_low_queue) {
2598 			hi = TRXDMA_QUEUE_LOW;
2599 		} else if (priv->ep_tx_normal_queue) {
2600 			hi = TRXDMA_QUEUE_NORMAL;
2601 		} else {
2602 			hi = 0;
2603 			ret = -EINVAL;
2604 		}
2605 
2606 		hiq = hi;
2607 		mgq = hi;
2608 		bkq = hi;
2609 		beq = hi;
2610 		viq = hi;
2611 		voq = hi;
2612 
2613 		hip = 0;
2614 		mgp = 0;
2615 		bkp = 0;
2616 		bep = 0;
2617 		vip = 0;
2618 		vop = 0;
2619 		break;
2620 	case 2:
2621 		if (priv->ep_tx_high_queue && priv->ep_tx_low_queue) {
2622 			hi = TRXDMA_QUEUE_HIGH;
2623 			lo = TRXDMA_QUEUE_LOW;
2624 		} else if (priv->ep_tx_normal_queue && priv->ep_tx_low_queue) {
2625 			hi = TRXDMA_QUEUE_NORMAL;
2626 			lo = TRXDMA_QUEUE_LOW;
2627 		} else if (priv->ep_tx_high_queue && priv->ep_tx_normal_queue) {
2628 			hi = TRXDMA_QUEUE_HIGH;
2629 			lo = TRXDMA_QUEUE_NORMAL;
2630 		} else {
2631 			ret = -EINVAL;
2632 			hi = 0;
2633 			lo = 0;
2634 		}
2635 
2636 		hiq = hi;
2637 		mgq = hi;
2638 		bkq = lo;
2639 		beq = lo;
2640 		viq = hi;
2641 		voq = hi;
2642 
2643 		hip = 0;
2644 		mgp = 0;
2645 		bkp = 1;
2646 		bep = 1;
2647 		vip = 0;
2648 		vop = 0;
2649 		break;
2650 	case 3:
2651 		beq = TRXDMA_QUEUE_LOW;
2652 		bkq = TRXDMA_QUEUE_LOW;
2653 		viq = TRXDMA_QUEUE_NORMAL;
2654 		voq = TRXDMA_QUEUE_HIGH;
2655 		mgq = TRXDMA_QUEUE_HIGH;
2656 		hiq = TRXDMA_QUEUE_HIGH;
2657 
2658 		hip = hiq ^ 3;
2659 		mgp = mgq ^ 3;
2660 		bkp = bkq ^ 3;
2661 		bep = beq ^ 3;
2662 		vip = viq ^ 3;
2663 		vop = viq ^ 3;
2664 		break;
2665 	default:
2666 		ret = -EINVAL;
2667 	}
2668 
2669 	/*
2670 	 * None of the vendor drivers are configuring the beacon
2671 	 * queue here .... why?
2672 	 */
2673 	if (!ret) {
2674 		/* Only RTL8192F seems to do it like this. */
2675 		if (priv->rtl_chip == RTL8192F) {
2676 			val32 = rtl8xxxu_read32(priv, REG_TRXDMA_CTRL);
2677 			val32 &= 0x7;
2678 			val32 |= (voq << TRXDMA_CTRL_VOQ_SHIFT_8192F) |
2679 				 (viq << TRXDMA_CTRL_VIQ_SHIFT_8192F) |
2680 				 (beq << TRXDMA_CTRL_BEQ_SHIFT_8192F) |
2681 				 (bkq << TRXDMA_CTRL_BKQ_SHIFT_8192F) |
2682 				 (mgq << TRXDMA_CTRL_MGQ_SHIFT_8192F) |
2683 				 (hiq << TRXDMA_CTRL_HIQ_SHIFT_8192F);
2684 			rtl8xxxu_write32(priv, REG_TRXDMA_CTRL, val32);
2685 		} else {
2686 			val16 = rtl8xxxu_read16(priv, REG_TRXDMA_CTRL);
2687 			val16 &= 0x7;
2688 			val16 |= (voq << TRXDMA_CTRL_VOQ_SHIFT) |
2689 				 (viq << TRXDMA_CTRL_VIQ_SHIFT) |
2690 				 (beq << TRXDMA_CTRL_BEQ_SHIFT) |
2691 				 (bkq << TRXDMA_CTRL_BKQ_SHIFT) |
2692 				 (mgq << TRXDMA_CTRL_MGQ_SHIFT) |
2693 				 (hiq << TRXDMA_CTRL_HIQ_SHIFT);
2694 			rtl8xxxu_write16(priv, REG_TRXDMA_CTRL, val16);
2695 		}
2696 
2697 		priv->pipe_out[TXDESC_QUEUE_VO] =
2698 			usb_sndbulkpipe(priv->udev, priv->out_ep[vop]);
2699 		priv->pipe_out[TXDESC_QUEUE_VI] =
2700 			usb_sndbulkpipe(priv->udev, priv->out_ep[vip]);
2701 		priv->pipe_out[TXDESC_QUEUE_BE] =
2702 			usb_sndbulkpipe(priv->udev, priv->out_ep[bep]);
2703 		priv->pipe_out[TXDESC_QUEUE_BK] =
2704 			usb_sndbulkpipe(priv->udev, priv->out_ep[bkp]);
2705 		priv->pipe_out[TXDESC_QUEUE_BEACON] =
2706 			usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2707 		priv->pipe_out[TXDESC_QUEUE_MGNT] =
2708 			usb_sndbulkpipe(priv->udev, priv->out_ep[mgp]);
2709 		priv->pipe_out[TXDESC_QUEUE_HIGH] =
2710 			usb_sndbulkpipe(priv->udev, priv->out_ep[hip]);
2711 		priv->pipe_out[TXDESC_QUEUE_CMD] =
2712 			usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2713 	}
2714 
2715 	return ret;
2716 }
2717 
2718 void rtl8xxxu_fill_iqk_matrix_a(struct rtl8xxxu_priv *priv, bool iqk_ok,
2719 				int result[][8], int candidate, bool tx_only)
2720 {
2721 	u32 oldval, x, tx0_a, reg;
2722 	int y, tx0_c;
2723 	u32 val32;
2724 
2725 	if (!iqk_ok)
2726 		return;
2727 
2728 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2729 	oldval = val32 >> 22;
2730 
2731 	x = result[candidate][0];
2732 	if ((x & 0x00000200) != 0)
2733 		x = x | 0xfffffc00;
2734 	tx0_a = (x * oldval) >> 8;
2735 
2736 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2737 	val32 &= ~0x3ff;
2738 	val32 |= tx0_a;
2739 	rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2740 
2741 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2742 	val32 &= ~BIT(31);
2743 	if ((x * oldval >> 7) & 0x1)
2744 		val32 |= BIT(31);
2745 	rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2746 
2747 	y = result[candidate][1];
2748 	if ((y & 0x00000200) != 0)
2749 		y = y | 0xfffffc00;
2750 	tx0_c = (y * oldval) >> 8;
2751 
2752 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XC_TX_AFE);
2753 	val32 &= ~0xf0000000;
2754 	val32 |= (((tx0_c & 0x3c0) >> 6) << 28);
2755 	rtl8xxxu_write32(priv, REG_OFDM0_XC_TX_AFE, val32);
2756 
2757 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2758 	val32 &= ~0x003f0000;
2759 	val32 |= ((tx0_c & 0x3f) << 16);
2760 	rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2761 
2762 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2763 	val32 &= ~BIT(29);
2764 	if ((y * oldval >> 7) & 0x1)
2765 		val32 |= BIT(29);
2766 	rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2767 
2768 	if (tx_only) {
2769 		dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2770 		return;
2771 	}
2772 
2773 	reg = result[candidate][2];
2774 
2775 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2776 	val32 &= ~0x3ff;
2777 	val32 |= (reg & 0x3ff);
2778 	rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2779 
2780 	reg = result[candidate][3] & 0x3F;
2781 
2782 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2783 	val32 &= ~0xfc00;
2784 	val32 |= ((reg << 10) & 0xfc00);
2785 	rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2786 
2787 	reg = (result[candidate][3] >> 6) & 0xF;
2788 
2789 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_RX_IQ_EXT_ANTA);
2790 	val32 &= ~0xf0000000;
2791 	val32 |= (reg << 28);
2792 	rtl8xxxu_write32(priv, REG_OFDM0_RX_IQ_EXT_ANTA, val32);
2793 }
2794 
2795 void rtl8xxxu_fill_iqk_matrix_b(struct rtl8xxxu_priv *priv, bool iqk_ok,
2796 				int result[][8], int candidate, bool tx_only)
2797 {
2798 	u32 oldval, x, tx1_a, reg;
2799 	int y, tx1_c;
2800 	u32 val32;
2801 
2802 	if (!iqk_ok)
2803 		return;
2804 
2805 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2806 	oldval = val32 >> 22;
2807 
2808 	x = result[candidate][4];
2809 	if ((x & 0x00000200) != 0)
2810 		x = x | 0xfffffc00;
2811 	tx1_a = (x * oldval) >> 8;
2812 
2813 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2814 	val32 &= ~0x3ff;
2815 	val32 |= tx1_a;
2816 	rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2817 
2818 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2819 	val32 &= ~BIT(27);
2820 	if ((x * oldval >> 7) & 0x1)
2821 		val32 |= BIT(27);
2822 	rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2823 
2824 	y = result[candidate][5];
2825 	if ((y & 0x00000200) != 0)
2826 		y = y | 0xfffffc00;
2827 	tx1_c = (y * oldval) >> 8;
2828 
2829 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XD_TX_AFE);
2830 	val32 &= ~0xf0000000;
2831 	val32 |= (((tx1_c & 0x3c0) >> 6) << 28);
2832 	rtl8xxxu_write32(priv, REG_OFDM0_XD_TX_AFE, val32);
2833 
2834 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2835 	val32 &= ~0x003f0000;
2836 	val32 |= ((tx1_c & 0x3f) << 16);
2837 	rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2838 
2839 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2840 	val32 &= ~BIT(25);
2841 	if ((y * oldval >> 7) & 0x1)
2842 		val32 |= BIT(25);
2843 	rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2844 
2845 	if (tx_only) {
2846 		dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2847 		return;
2848 	}
2849 
2850 	reg = result[candidate][6];
2851 
2852 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2853 	val32 &= ~0x3ff;
2854 	val32 |= (reg & 0x3ff);
2855 	rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2856 
2857 	reg = result[candidate][7] & 0x3f;
2858 
2859 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2860 	val32 &= ~0xfc00;
2861 	val32 |= ((reg << 10) & 0xfc00);
2862 	rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2863 
2864 	reg = (result[candidate][7] >> 6) & 0xf;
2865 
2866 	if (priv->rtl_chip == RTL8192F) {
2867 		rtl8xxxu_write32_mask(priv, REG_RXIQB_EXT, 0x000000f0, reg);
2868 	} else {
2869 		val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGC_RSSI_TABLE);
2870 		val32 &= ~0x0000f000;
2871 		val32 |= (reg << 12);
2872 		rtl8xxxu_write32(priv, REG_OFDM0_AGC_RSSI_TABLE, val32);
2873 	}
2874 }
2875 
2876 #define MAX_TOLERANCE		5
2877 
2878 bool rtl8xxxu_simularity_compare(struct rtl8xxxu_priv *priv,
2879 				 int result[][8], int c1, int c2)
2880 {
2881 	u32 i, j, diff, simubitmap, bound = 0;
2882 	int candidate[2] = {-1, -1};	/* for path A and path B */
2883 	bool retval = true;
2884 
2885 	if (priv->tx_paths > 1)
2886 		bound = 8;
2887 	else
2888 		bound = 4;
2889 
2890 	simubitmap = 0;
2891 
2892 	for (i = 0; i < bound; i++) {
2893 		diff = (result[c1][i] > result[c2][i]) ?
2894 			(result[c1][i] - result[c2][i]) :
2895 			(result[c2][i] - result[c1][i]);
2896 		if (diff > MAX_TOLERANCE) {
2897 			if ((i == 2 || i == 6) && !simubitmap) {
2898 				if (result[c1][i] + result[c1][i + 1] == 0)
2899 					candidate[(i / 4)] = c2;
2900 				else if (result[c2][i] + result[c2][i + 1] == 0)
2901 					candidate[(i / 4)] = c1;
2902 				else
2903 					simubitmap = simubitmap | (1 << i);
2904 			} else {
2905 				simubitmap = simubitmap | (1 << i);
2906 			}
2907 		}
2908 	}
2909 
2910 	if (simubitmap == 0) {
2911 		for (i = 0; i < (bound / 4); i++) {
2912 			if (candidate[i] >= 0) {
2913 				for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2914 					result[3][j] = result[candidate[i]][j];
2915 				retval = false;
2916 			}
2917 		}
2918 		return retval;
2919 	} else if (!(simubitmap & 0x0f)) {
2920 		/* path A OK */
2921 		for (i = 0; i < 4; i++)
2922 			result[3][i] = result[c1][i];
2923 	} else if (!(simubitmap & 0xf0) && priv->tx_paths > 1) {
2924 		/* path B OK */
2925 		for (i = 4; i < 8; i++)
2926 			result[3][i] = result[c1][i];
2927 	}
2928 
2929 	return false;
2930 }
2931 
2932 bool rtl8xxxu_gen2_simularity_compare(struct rtl8xxxu_priv *priv,
2933 				      int result[][8], int c1, int c2)
2934 {
2935 	u32 i, j, diff, simubitmap, bound = 0;
2936 	int candidate[2] = {-1, -1};	/* for path A and path B */
2937 	int tmp1, tmp2;
2938 	bool retval = true;
2939 
2940 	if (priv->tx_paths > 1)
2941 		bound = 8;
2942 	else
2943 		bound = 4;
2944 
2945 	simubitmap = 0;
2946 
2947 	for (i = 0; i < bound; i++) {
2948 		if (i & 1) {
2949 			if ((result[c1][i] & 0x00000200))
2950 				tmp1 = result[c1][i] | 0xfffffc00;
2951 			else
2952 				tmp1 = result[c1][i];
2953 
2954 			if ((result[c2][i]& 0x00000200))
2955 				tmp2 = result[c2][i] | 0xfffffc00;
2956 			else
2957 				tmp2 = result[c2][i];
2958 		} else {
2959 			tmp1 = result[c1][i];
2960 			tmp2 = result[c2][i];
2961 		}
2962 
2963 		diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
2964 
2965 		if (diff > MAX_TOLERANCE) {
2966 			if ((i == 2 || i == 6) && !simubitmap) {
2967 				if (result[c1][i] + result[c1][i + 1] == 0)
2968 					candidate[(i / 4)] = c2;
2969 				else if (result[c2][i] + result[c2][i + 1] == 0)
2970 					candidate[(i / 4)] = c1;
2971 				else
2972 					simubitmap = simubitmap | (1 << i);
2973 			} else {
2974 				simubitmap = simubitmap | (1 << i);
2975 			}
2976 		}
2977 	}
2978 
2979 	if (simubitmap == 0) {
2980 		for (i = 0; i < (bound / 4); i++) {
2981 			if (candidate[i] >= 0) {
2982 				for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2983 					result[3][j] = result[candidate[i]][j];
2984 				retval = false;
2985 			}
2986 		}
2987 		return retval;
2988 	} else {
2989 		if (!(simubitmap & 0x03)) {
2990 			/* path A TX OK */
2991 			for (i = 0; i < 2; i++)
2992 				result[3][i] = result[c1][i];
2993 		}
2994 
2995 		if (!(simubitmap & 0x0c)) {
2996 			/* path A RX OK */
2997 			for (i = 2; i < 4; i++)
2998 				result[3][i] = result[c1][i];
2999 		}
3000 
3001 		if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
3002 			/* path B TX OK */
3003 			for (i = 4; i < 6; i++)
3004 				result[3][i] = result[c1][i];
3005 		}
3006 
3007 		if (!(simubitmap & 0xc0) && priv->tx_paths > 1) {
3008 			/* path B RX OK */
3009 			for (i = 6; i < 8; i++)
3010 				result[3][i] = result[c1][i];
3011 		}
3012 	}
3013 
3014 	return false;
3015 }
3016 
3017 void
3018 rtl8xxxu_save_mac_regs(struct rtl8xxxu_priv *priv, const u32 *reg, u32 *backup)
3019 {
3020 	int i;
3021 
3022 	for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
3023 		backup[i] = rtl8xxxu_read8(priv, reg[i]);
3024 
3025 	backup[i] = rtl8xxxu_read32(priv, reg[i]);
3026 }
3027 
3028 void rtl8xxxu_restore_mac_regs(struct rtl8xxxu_priv *priv,
3029 			       const u32 *reg, u32 *backup)
3030 {
3031 	int i;
3032 
3033 	for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
3034 		rtl8xxxu_write8(priv, reg[i], backup[i]);
3035 
3036 	rtl8xxxu_write32(priv, reg[i], backup[i]);
3037 }
3038 
3039 void rtl8xxxu_save_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
3040 			u32 *backup, int count)
3041 {
3042 	int i;
3043 
3044 	for (i = 0; i < count; i++)
3045 		backup[i] = rtl8xxxu_read32(priv, regs[i]);
3046 }
3047 
3048 void rtl8xxxu_restore_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
3049 			   u32 *backup, int count)
3050 {
3051 	int i;
3052 
3053 	for (i = 0; i < count; i++)
3054 		rtl8xxxu_write32(priv, regs[i], backup[i]);
3055 }
3056 
3057 
3058 void rtl8xxxu_path_adda_on(struct rtl8xxxu_priv *priv, const u32 *regs,
3059 			   bool path_a_on)
3060 {
3061 	u32 path_on;
3062 	int i;
3063 
3064 	if (priv->tx_paths == 1) {
3065 		path_on = priv->fops->adda_1t_path_on;
3066 		rtl8xxxu_write32(priv, regs[0], priv->fops->adda_1t_init);
3067 	} else {
3068 		path_on = path_a_on ? priv->fops->adda_2t_path_on_a :
3069 			priv->fops->adda_2t_path_on_b;
3070 
3071 		rtl8xxxu_write32(priv, regs[0], path_on);
3072 	}
3073 
3074 	for (i = 1 ; i < RTL8XXXU_ADDA_REGS ; i++)
3075 		rtl8xxxu_write32(priv, regs[i], path_on);
3076 }
3077 
3078 void rtl8xxxu_mac_calibration(struct rtl8xxxu_priv *priv,
3079 			      const u32 *regs, u32 *backup)
3080 {
3081 	int i = 0;
3082 
3083 	rtl8xxxu_write8(priv, regs[i], 0x3f);
3084 
3085 	for (i = 1 ; i < (RTL8XXXU_MAC_REGS - 1); i++)
3086 		rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(3)));
3087 
3088 	rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(5)));
3089 }
3090 
3091 static int rtl8xxxu_iqk_path_a(struct rtl8xxxu_priv *priv)
3092 {
3093 	u32 reg_eac, reg_e94, reg_e9c, reg_ea4, val32;
3094 	int result = 0;
3095 
3096 	/* path-A IQK setting */
3097 	rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x10008c1f);
3098 	rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x10008c1f);
3099 	rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x82140102);
3100 
3101 	val32 = (priv->rf_paths > 1) ? 0x28160202 :
3102 		/*IS_81xxC_VENDOR_UMC_B_CUT(pHalData->VersionID)?0x28160202: */
3103 		0x28160502;
3104 	rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, val32);
3105 
3106 	/* path-B IQK setting */
3107 	if (priv->rf_paths > 1) {
3108 		rtl8xxxu_write32(priv, REG_TX_IQK_TONE_B, 0x10008c22);
3109 		rtl8xxxu_write32(priv, REG_RX_IQK_TONE_B, 0x10008c22);
3110 		rtl8xxxu_write32(priv, REG_TX_IQK_PI_B, 0x82140102);
3111 		rtl8xxxu_write32(priv, REG_RX_IQK_PI_B, 0x28160202);
3112 	}
3113 
3114 	/* LO calibration setting */
3115 	rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x001028d1);
3116 
3117 	/* One shot, path A LOK & IQK */
3118 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
3119 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
3120 
3121 	mdelay(1);
3122 
3123 	/* Check failed */
3124 	reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3125 	reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
3126 	reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
3127 	reg_ea4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
3128 
3129 	if (!(reg_eac & BIT(28)) &&
3130 	    ((reg_e94 & 0x03ff0000) != 0x01420000) &&
3131 	    ((reg_e9c & 0x03ff0000) != 0x00420000))
3132 		result |= 0x01;
3133 	else	/* If TX not OK, ignore RX */
3134 		goto out;
3135 
3136 	/* If TX is OK, check whether RX is OK */
3137 	if (!(reg_eac & BIT(27)) &&
3138 	    ((reg_ea4 & 0x03ff0000) != 0x01320000) &&
3139 	    ((reg_eac & 0x03ff0000) != 0x00360000))
3140 		result |= 0x02;
3141 	else
3142 		dev_warn(&priv->udev->dev, "%s: Path A RX IQK failed!\n",
3143 			 __func__);
3144 out:
3145 	return result;
3146 }
3147 
3148 static int rtl8xxxu_iqk_path_b(struct rtl8xxxu_priv *priv)
3149 {
3150 	u32 reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3151 	int result = 0;
3152 
3153 	/* One shot, path B LOK & IQK */
3154 	rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000002);
3155 	rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000000);
3156 
3157 	mdelay(1);
3158 
3159 	/* Check failed */
3160 	reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3161 	reg_eb4 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3162 	reg_ebc = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3163 	reg_ec4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3164 	reg_ecc = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3165 
3166 	if (!(reg_eac & BIT(31)) &&
3167 	    ((reg_eb4 & 0x03ff0000) != 0x01420000) &&
3168 	    ((reg_ebc & 0x03ff0000) != 0x00420000))
3169 		result |= 0x01;
3170 	else
3171 		goto out;
3172 
3173 	if (!(reg_eac & BIT(30)) &&
3174 	    (((reg_ec4 & 0x03ff0000) >> 16) != 0x132) &&
3175 	    (((reg_ecc & 0x03ff0000) >> 16) != 0x36))
3176 		result |= 0x02;
3177 	else
3178 		dev_warn(&priv->udev->dev, "%s: Path B RX IQK failed!\n",
3179 			 __func__);
3180 out:
3181 	return result;
3182 }
3183 
3184 static void rtl8xxxu_phy_iqcalibrate(struct rtl8xxxu_priv *priv,
3185 				     int result[][8], int t)
3186 {
3187 	struct device *dev = &priv->udev->dev;
3188 	u32 i, val32;
3189 	int path_a_ok, path_b_ok;
3190 	int retry = 2;
3191 	static const u32 adda_regs[RTL8XXXU_ADDA_REGS] = {
3192 		REG_FPGA0_XCD_SWITCH_CTRL, REG_BLUETOOTH,
3193 		REG_RX_WAIT_CCA, REG_TX_CCK_RFON,
3194 		REG_TX_CCK_BBON, REG_TX_OFDM_RFON,
3195 		REG_TX_OFDM_BBON, REG_TX_TO_RX,
3196 		REG_TX_TO_TX, REG_RX_CCK,
3197 		REG_RX_OFDM, REG_RX_WAIT_RIFS,
3198 		REG_RX_TO_RX, REG_STANDBY,
3199 		REG_SLEEP, REG_PMPD_ANAEN
3200 	};
3201 	static const u32 iqk_mac_regs[RTL8XXXU_MAC_REGS] = {
3202 		REG_TXPAUSE, REG_BEACON_CTRL,
3203 		REG_BEACON_CTRL_1, REG_GPIO_MUXCFG
3204 	};
3205 	static const u32 iqk_bb_regs[RTL8XXXU_BB_REGS] = {
3206 		REG_OFDM0_TRX_PATH_ENABLE, REG_OFDM0_TR_MUX_PAR,
3207 		REG_FPGA0_XCD_RF_SW_CTRL, REG_CONFIG_ANT_A, REG_CONFIG_ANT_B,
3208 		REG_FPGA0_XAB_RF_SW_CTRL, REG_FPGA0_XA_RF_INT_OE,
3209 		REG_FPGA0_XB_RF_INT_OE, REG_FPGA0_RF_MODE
3210 	};
3211 
3212 	/*
3213 	 * Note: IQ calibration must be performed after loading
3214 	 *       PHY_REG.txt , and radio_a, radio_b.txt
3215 	 */
3216 
3217 	if (t == 0) {
3218 		/* Save ADDA parameters, turn Path A ADDA on */
3219 		rtl8xxxu_save_regs(priv, adda_regs, priv->adda_backup,
3220 				   RTL8XXXU_ADDA_REGS);
3221 		rtl8xxxu_save_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3222 		rtl8xxxu_save_regs(priv, iqk_bb_regs,
3223 				   priv->bb_backup, RTL8XXXU_BB_REGS);
3224 	}
3225 
3226 	rtl8xxxu_path_adda_on(priv, adda_regs, true);
3227 
3228 	if (t == 0) {
3229 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM1);
3230 		if (val32 & FPGA0_HSSI_PARM1_PI)
3231 			priv->pi_enabled = 1;
3232 	}
3233 
3234 	if (!priv->pi_enabled) {
3235 		/* Switch BB to PI mode to do IQ Calibration. */
3236 		rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, 0x01000100);
3237 		rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, 0x01000100);
3238 	}
3239 
3240 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3241 	val32 &= ~FPGA_RF_MODE_CCK;
3242 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
3243 
3244 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, 0x03a05600);
3245 	rtl8xxxu_write32(priv, REG_OFDM0_TR_MUX_PAR, 0x000800e4);
3246 	rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_SW_CTRL, 0x22204000);
3247 
3248 	if (!priv->no_pape) {
3249 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_SW_CTRL);
3250 		val32 |= (FPGA0_RF_PAPE |
3251 			  (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
3252 		rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
3253 	}
3254 
3255 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_RF_INT_OE);
3256 	val32 &= ~BIT(10);
3257 	rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, val32);
3258 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_RF_INT_OE);
3259 	val32 &= ~BIT(10);
3260 	rtl8xxxu_write32(priv, REG_FPGA0_XB_RF_INT_OE, val32);
3261 
3262 	if (priv->tx_paths > 1) {
3263 		rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3264 		rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM, 0x00010000);
3265 	}
3266 
3267 	/* MAC settings */
3268 	rtl8xxxu_mac_calibration(priv, iqk_mac_regs, priv->mac_backup);
3269 
3270 	/* Page B init */
3271 	rtl8xxxu_write32(priv, REG_CONFIG_ANT_A, 0x00080000);
3272 
3273 	if (priv->tx_paths > 1)
3274 		rtl8xxxu_write32(priv, REG_CONFIG_ANT_B, 0x00080000);
3275 
3276 	/* IQ calibration setting */
3277 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3278 	rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
3279 	rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
3280 
3281 	for (i = 0; i < retry; i++) {
3282 		path_a_ok = rtl8xxxu_iqk_path_a(priv);
3283 		if (path_a_ok == 0x03) {
3284 			val32 = rtl8xxxu_read32(priv,
3285 						REG_TX_POWER_BEFORE_IQK_A);
3286 			result[t][0] = (val32 >> 16) & 0x3ff;
3287 			val32 = rtl8xxxu_read32(priv,
3288 						REG_TX_POWER_AFTER_IQK_A);
3289 			result[t][1] = (val32 >> 16) & 0x3ff;
3290 			val32 = rtl8xxxu_read32(priv,
3291 						REG_RX_POWER_BEFORE_IQK_A_2);
3292 			result[t][2] = (val32 >> 16) & 0x3ff;
3293 			val32 = rtl8xxxu_read32(priv,
3294 						REG_RX_POWER_AFTER_IQK_A_2);
3295 			result[t][3] = (val32 >> 16) & 0x3ff;
3296 			break;
3297 		} else if (i == (retry - 1) && path_a_ok == 0x01) {
3298 			/* TX IQK OK */
3299 			dev_dbg(dev, "%s: Path A IQK Only Tx Success!!\n",
3300 				__func__);
3301 
3302 			val32 = rtl8xxxu_read32(priv,
3303 						REG_TX_POWER_BEFORE_IQK_A);
3304 			result[t][0] = (val32 >> 16) & 0x3ff;
3305 			val32 = rtl8xxxu_read32(priv,
3306 						REG_TX_POWER_AFTER_IQK_A);
3307 			result[t][1] = (val32 >> 16) & 0x3ff;
3308 		}
3309 	}
3310 
3311 	if (!path_a_ok)
3312 		dev_dbg(dev, "%s: Path A IQK failed!\n", __func__);
3313 
3314 	if (priv->tx_paths > 1) {
3315 		/*
3316 		 * Path A into standby
3317 		 */
3318 		rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x0);
3319 		rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3320 		rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3321 
3322 		/* Turn Path B ADDA on */
3323 		rtl8xxxu_path_adda_on(priv, adda_regs, false);
3324 
3325 		for (i = 0; i < retry; i++) {
3326 			path_b_ok = rtl8xxxu_iqk_path_b(priv);
3327 			if (path_b_ok == 0x03) {
3328 				val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3329 				result[t][4] = (val32 >> 16) & 0x3ff;
3330 				val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3331 				result[t][5] = (val32 >> 16) & 0x3ff;
3332 				val32 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3333 				result[t][6] = (val32 >> 16) & 0x3ff;
3334 				val32 = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3335 				result[t][7] = (val32 >> 16) & 0x3ff;
3336 				break;
3337 			} else if (i == (retry - 1) && path_b_ok == 0x01) {
3338 				/* TX IQK OK */
3339 				val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3340 				result[t][4] = (val32 >> 16) & 0x3ff;
3341 				val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3342 				result[t][5] = (val32 >> 16) & 0x3ff;
3343 			}
3344 		}
3345 
3346 		if (!path_b_ok)
3347 			dev_dbg(dev, "%s: Path B IQK failed!\n", __func__);
3348 	}
3349 
3350 	/* Back to BB mode, load original value */
3351 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0);
3352 
3353 	if (t) {
3354 		if (!priv->pi_enabled) {
3355 			/*
3356 			 * Switch back BB to SI mode after finishing
3357 			 * IQ Calibration
3358 			 */
3359 			val32 = 0x01000000;
3360 			rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, val32);
3361 			rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, val32);
3362 		}
3363 
3364 		/* Reload ADDA power saving parameters */
3365 		rtl8xxxu_restore_regs(priv, adda_regs, priv->adda_backup,
3366 				      RTL8XXXU_ADDA_REGS);
3367 
3368 		/* Reload MAC parameters */
3369 		rtl8xxxu_restore_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3370 
3371 		/* Reload BB parameters */
3372 		rtl8xxxu_restore_regs(priv, iqk_bb_regs,
3373 				      priv->bb_backup, RTL8XXXU_BB_REGS);
3374 
3375 		/* Restore RX initial gain */
3376 		rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00032ed3);
3377 
3378 		if (priv->tx_paths > 1) {
3379 			rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM,
3380 					 0x00032ed3);
3381 		}
3382 
3383 		/* Load 0xe30 IQC default value */
3384 		rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x01008c00);
3385 		rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x01008c00);
3386 	}
3387 }
3388 
3389 void rtl8xxxu_gen2_prepare_calibrate(struct rtl8xxxu_priv *priv, u8 start)
3390 {
3391 	struct h2c_cmd h2c;
3392 
3393 	memset(&h2c, 0, sizeof(struct h2c_cmd));
3394 	h2c.bt_wlan_calibration.cmd = H2C_8723B_BT_WLAN_CALIBRATION;
3395 	h2c.bt_wlan_calibration.data = start;
3396 
3397 	rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.bt_wlan_calibration));
3398 }
3399 
3400 void rtl8xxxu_gen1_phy_iq_calibrate(struct rtl8xxxu_priv *priv)
3401 {
3402 	struct device *dev = &priv->udev->dev;
3403 	int result[4][8];	/* last is final result */
3404 	int i, candidate;
3405 	bool path_a_ok, path_b_ok;
3406 	u32 reg_e94, reg_e9c, reg_ea4, reg_eac;
3407 	u32 reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3408 	s32 reg_tmp = 0;
3409 	bool simu;
3410 
3411 	memset(result, 0, sizeof(result));
3412 	candidate = -1;
3413 
3414 	path_a_ok = false;
3415 	path_b_ok = false;
3416 
3417 	rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3418 
3419 	for (i = 0; i < 3; i++) {
3420 		rtl8xxxu_phy_iqcalibrate(priv, result, i);
3421 
3422 		if (i == 1) {
3423 			simu = rtl8xxxu_simularity_compare(priv, result, 0, 1);
3424 			if (simu) {
3425 				candidate = 0;
3426 				break;
3427 			}
3428 		}
3429 
3430 		if (i == 2) {
3431 			simu = rtl8xxxu_simularity_compare(priv, result, 0, 2);
3432 			if (simu) {
3433 				candidate = 0;
3434 				break;
3435 			}
3436 
3437 			simu = rtl8xxxu_simularity_compare(priv, result, 1, 2);
3438 			if (simu) {
3439 				candidate = 1;
3440 			} else {
3441 				for (i = 0; i < 8; i++)
3442 					reg_tmp += result[3][i];
3443 
3444 				if (reg_tmp)
3445 					candidate = 3;
3446 				else
3447 					candidate = -1;
3448 			}
3449 		}
3450 	}
3451 
3452 	for (i = 0; i < 4; i++) {
3453 		reg_e94 = result[i][0];
3454 		reg_e9c = result[i][1];
3455 		reg_ea4 = result[i][2];
3456 		reg_eac = result[i][3];
3457 		reg_eb4 = result[i][4];
3458 		reg_ebc = result[i][5];
3459 		reg_ec4 = result[i][6];
3460 		reg_ecc = result[i][7];
3461 	}
3462 
3463 	if (candidate >= 0) {
3464 		reg_e94 = result[candidate][0];
3465 		priv->rege94 =  reg_e94;
3466 		reg_e9c = result[candidate][1];
3467 		priv->rege9c = reg_e9c;
3468 		reg_ea4 = result[candidate][2];
3469 		reg_eac = result[candidate][3];
3470 		reg_eb4 = result[candidate][4];
3471 		priv->regeb4 = reg_eb4;
3472 		reg_ebc = result[candidate][5];
3473 		priv->regebc = reg_ebc;
3474 		reg_ec4 = result[candidate][6];
3475 		reg_ecc = result[candidate][7];
3476 		dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
3477 		dev_dbg(dev,
3478 			"%s: e94 =%x e9c=%x ea4=%x eac=%x eb4=%x ebc=%x ec4=%x ecc=%x\n",
3479 			__func__, reg_e94, reg_e9c,
3480 			reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc);
3481 		path_a_ok = true;
3482 		path_b_ok = true;
3483 	} else {
3484 		reg_e94 = reg_eb4 = priv->rege94 = priv->regeb4 = 0x100;
3485 		reg_e9c = reg_ebc = priv->rege9c = priv->regebc = 0x0;
3486 	}
3487 
3488 	if (reg_e94 && candidate >= 0)
3489 		rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
3490 					   candidate, (reg_ea4 == 0));
3491 
3492 	if (priv->tx_paths > 1 && reg_eb4)
3493 		rtl8xxxu_fill_iqk_matrix_b(priv, path_b_ok, result,
3494 					   candidate, (reg_ec4 == 0));
3495 
3496 	rtl8xxxu_save_regs(priv, rtl8xxxu_iqk_phy_iq_bb_reg,
3497 			   priv->bb_recovery_backup, RTL8XXXU_BB_REGS);
3498 }
3499 
3500 void rtl8723a_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
3501 {
3502 	u32 val32;
3503 	u32 rf_amode, rf_bmode = 0, lstf;
3504 
3505 	/* Check continuous TX and Packet TX */
3506 	lstf = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
3507 
3508 	if (lstf & OFDM_LSTF_MASK) {
3509 		/* Disable all continuous TX */
3510 		val32 = lstf & ~OFDM_LSTF_MASK;
3511 		rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
3512 
3513 		/* Read original RF mode Path A */
3514 		rf_amode = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_AC);
3515 
3516 		/* Set RF mode to standby Path A */
3517 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC,
3518 				     (rf_amode & 0x8ffff) | 0x10000);
3519 
3520 		/* Path-B */
3521 		if (priv->tx_paths > 1) {
3522 			rf_bmode = rtl8xxxu_read_rfreg(priv, RF_B,
3523 						       RF6052_REG_AC);
3524 
3525 			rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3526 					     (rf_bmode & 0x8ffff) | 0x10000);
3527 		}
3528 	} else {
3529 		/*  Deal with Packet TX case */
3530 		/*  block all queues */
3531 		rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3532 	}
3533 
3534 	/* Start LC calibration */
3535 	if (priv->fops->has_s0s1)
3536 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdfbe0);
3537 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
3538 	val32 |= 0x08000;
3539 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
3540 
3541 	msleep(100);
3542 
3543 	if (priv->fops->has_s0s1)
3544 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdffe0);
3545 
3546 	/* Restore original parameters */
3547 	if (lstf & OFDM_LSTF_MASK) {
3548 		/* Path-A */
3549 		rtl8xxxu_write32(priv, REG_OFDM1_LSTF, lstf);
3550 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, rf_amode);
3551 
3552 		/* Path-B */
3553 		if (priv->tx_paths > 1)
3554 			rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3555 					     rf_bmode);
3556 	} else /*  Deal with Packet TX case */
3557 		rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
3558 }
3559 
3560 static int rtl8xxxu_set_mac(struct rtl8xxxu_priv *priv, int port_num)
3561 {
3562 	int i;
3563 	u16 reg;
3564 
3565 	switch (port_num) {
3566 	case 0:
3567 		reg = REG_MACID;
3568 		break;
3569 	case 1:
3570 		reg = REG_MACID1;
3571 		break;
3572 	default:
3573 		WARN_ONCE(1, "%s: invalid port_num\n", __func__);
3574 		return -EINVAL;
3575 	}
3576 
3577 	for (i = 0; i < ETH_ALEN; i++)
3578 		rtl8xxxu_write8(priv, reg + i, priv->vifs[port_num]->addr[i]);
3579 
3580 	return 0;
3581 }
3582 
3583 static int rtl8xxxu_set_bssid(struct rtl8xxxu_priv *priv, const u8 *bssid, int port_num)
3584 {
3585 	int i;
3586 	u16 reg;
3587 
3588 	dev_dbg(&priv->udev->dev, "%s: (%pM)\n", __func__, bssid);
3589 
3590 	switch (port_num) {
3591 	case 0:
3592 		reg = REG_BSSID;
3593 		break;
3594 	case 1:
3595 		reg = REG_BSSID1;
3596 		break;
3597 	default:
3598 		WARN_ONCE(1, "%s: invalid port_num\n", __func__);
3599 		return -EINVAL;
3600 	}
3601 
3602 	for (i = 0; i < ETH_ALEN; i++)
3603 		rtl8xxxu_write8(priv, reg + i, bssid[i]);
3604 
3605 	return 0;
3606 }
3607 
3608 static void
3609 rtl8xxxu_set_ampdu_factor(struct rtl8xxxu_priv *priv, u8 ampdu_factor)
3610 {
3611 	u8 vals[4] = { 0x41, 0xa8, 0x72, 0xb9 };
3612 	u8 max_agg = 0xf;
3613 	int i;
3614 
3615 	ampdu_factor = 1 << (ampdu_factor + 2);
3616 	if (ampdu_factor > max_agg)
3617 		ampdu_factor = max_agg;
3618 
3619 	for (i = 0; i < 4; i++) {
3620 		if ((vals[i] & 0xf0) > (ampdu_factor << 4))
3621 			vals[i] = (vals[i] & 0x0f) | (ampdu_factor << 4);
3622 
3623 		if ((vals[i] & 0x0f) > ampdu_factor)
3624 			vals[i] = (vals[i] & 0xf0) | ampdu_factor;
3625 
3626 		rtl8xxxu_write8(priv, REG_AGGLEN_LMT + i, vals[i]);
3627 	}
3628 }
3629 
3630 static void rtl8xxxu_set_ampdu_min_space(struct rtl8xxxu_priv *priv, u8 density)
3631 {
3632 	u8 val8;
3633 
3634 	val8 = rtl8xxxu_read8(priv, REG_AMPDU_MIN_SPACE);
3635 	val8 &= 0xf8;
3636 	val8 |= density;
3637 	rtl8xxxu_write8(priv, REG_AMPDU_MIN_SPACE, val8);
3638 }
3639 
3640 int rtl8xxxu_active_to_lps(struct rtl8xxxu_priv *priv)
3641 {
3642 	u8 val8;
3643 	u8 val32;
3644 	int count, ret = 0;
3645 
3646 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3647 
3648 	/*
3649 	 * Poll - wait for RX packet to complete
3650 	 */
3651 	for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
3652 		val32 = rtl8xxxu_read32(priv, 0x5f8);
3653 		if (!val32)
3654 			break;
3655 		udelay(10);
3656 	}
3657 
3658 	if (!count) {
3659 		dev_warn(&priv->udev->dev,
3660 			 "%s: RX poll timed out (0x05f8)\n", __func__);
3661 		ret = -EBUSY;
3662 		goto exit;
3663 	}
3664 
3665 	/* Disable CCK and OFDM, clock gated */
3666 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3667 	val8 &= ~SYS_FUNC_BBRSTB;
3668 	rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3669 
3670 	udelay(2);
3671 
3672 	/* Reset baseband */
3673 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3674 	val8 &= ~SYS_FUNC_BB_GLB_RSTN;
3675 	rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3676 
3677 	/* Reset MAC TRX */
3678 	val8 = rtl8xxxu_read8(priv, REG_CR);
3679 	val8 = CR_HCI_TXDMA_ENABLE | CR_HCI_RXDMA_ENABLE;
3680 	rtl8xxxu_write8(priv, REG_CR, val8);
3681 
3682 	/* Reset MAC TRX */
3683 	val8 = rtl8xxxu_read8(priv, REG_CR + 1);
3684 	val8 &= ~BIT(1); /* CR_SECURITY_ENABLE */
3685 	rtl8xxxu_write8(priv, REG_CR + 1, val8);
3686 
3687 	/* Respond TX OK to scheduler */
3688 	val8 = rtl8xxxu_read8(priv, REG_DUAL_TSF_RST);
3689 	val8 |= DUAL_TSF_TX_OK;
3690 	rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, val8);
3691 
3692 exit:
3693 	return ret;
3694 }
3695 
3696 void rtl8xxxu_disabled_to_emu(struct rtl8xxxu_priv *priv)
3697 {
3698 	u8 val8;
3699 
3700 	/* Clear suspend enable and power down enable*/
3701 	val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3702 	val8 &= ~(BIT(3) | BIT(7));
3703 	rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3704 
3705 	/* 0x48[16] = 0 to disable GPIO9 as EXT WAKEUP*/
3706 	val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
3707 	val8 &= ~BIT(0);
3708 	rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
3709 
3710 	/* 0x04[12:11] = 11 enable WL suspend*/
3711 	val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3712 	val8 &= ~(BIT(3) | BIT(4));
3713 	rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3714 }
3715 
3716 int rtl8xxxu_flush_fifo(struct rtl8xxxu_priv *priv)
3717 {
3718 	struct device *dev = &priv->udev->dev;
3719 	u32 val32;
3720 	int retry, retval;
3721 
3722 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3723 
3724 	val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3725 	val32 |= RXPKT_NUM_RW_RELEASE_EN;
3726 	rtl8xxxu_write32(priv, REG_RXPKT_NUM, val32);
3727 
3728 	retry = 100;
3729 	retval = -EBUSY;
3730 
3731 	do {
3732 		val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3733 		if (val32 & RXPKT_NUM_RXDMA_IDLE) {
3734 			retval = 0;
3735 			break;
3736 		}
3737 	} while (retry--);
3738 
3739 	rtl8xxxu_write16(priv, REG_RQPN_NPQ, 0);
3740 	rtl8xxxu_write32(priv, REG_RQPN, 0x80000000);
3741 	mdelay(2);
3742 
3743 	if (!retry)
3744 		dev_warn(dev, "Failed to flush FIFO\n");
3745 
3746 	return retval;
3747 }
3748 
3749 void rtl8xxxu_gen1_usb_quirks(struct rtl8xxxu_priv *priv)
3750 {
3751 	/* Fix USB interface interference issue */
3752 	rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3753 	rtl8xxxu_write8(priv, 0xfe41, 0x8d);
3754 	rtl8xxxu_write8(priv, 0xfe42, 0x80);
3755 	/*
3756 	 * This sets TXDMA_OFFSET_DROP_DATA_EN (bit 9) as well as bits
3757 	 * 8 and 5, for which I have found no documentation.
3758 	 */
3759 	rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, 0xfd0320);
3760 
3761 	/*
3762 	 * Solve too many protocol error on USB bus.
3763 	 * Can't do this for 8188/8192 UMC A cut parts
3764 	 */
3765 	if (!(!priv->chip_cut && priv->vendor_umc)) {
3766 		rtl8xxxu_write8(priv, 0xfe40, 0xe6);
3767 		rtl8xxxu_write8(priv, 0xfe41, 0x94);
3768 		rtl8xxxu_write8(priv, 0xfe42, 0x80);
3769 
3770 		rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3771 		rtl8xxxu_write8(priv, 0xfe41, 0x19);
3772 		rtl8xxxu_write8(priv, 0xfe42, 0x80);
3773 
3774 		rtl8xxxu_write8(priv, 0xfe40, 0xe5);
3775 		rtl8xxxu_write8(priv, 0xfe41, 0x91);
3776 		rtl8xxxu_write8(priv, 0xfe42, 0x80);
3777 
3778 		rtl8xxxu_write8(priv, 0xfe40, 0xe2);
3779 		rtl8xxxu_write8(priv, 0xfe41, 0x81);
3780 		rtl8xxxu_write8(priv, 0xfe42, 0x80);
3781 	}
3782 }
3783 
3784 void rtl8xxxu_gen2_usb_quirks(struct rtl8xxxu_priv *priv)
3785 {
3786 	u32 val32;
3787 
3788 	val32 = rtl8xxxu_read32(priv, REG_TXDMA_OFFSET_CHK);
3789 	val32 |= TXDMA_OFFSET_DROP_DATA_EN;
3790 	rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, val32);
3791 }
3792 
3793 void rtl8723bu_set_ps_tdma(struct rtl8xxxu_priv *priv,
3794 			   u8 arg1, u8 arg2, u8 arg3, u8 arg4, u8 arg5)
3795 {
3796 	struct h2c_cmd h2c;
3797 
3798 	memset(&h2c, 0, sizeof(struct h2c_cmd));
3799 	h2c.b_type_dma.cmd = H2C_8723B_B_TYPE_TDMA;
3800 	h2c.b_type_dma.data1 = arg1;
3801 	h2c.b_type_dma.data2 = arg2;
3802 	h2c.b_type_dma.data3 = arg3;
3803 	h2c.b_type_dma.data4 = arg4;
3804 	h2c.b_type_dma.data5 = arg5;
3805 	rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_type_dma));
3806 }
3807 
3808 void rtl8xxxu_gen2_disable_rf(struct rtl8xxxu_priv *priv)
3809 {
3810 	u32 val32;
3811 
3812 	val32 = rtl8xxxu_read32(priv, REG_RX_WAIT_CCA);
3813 	val32 &= ~(BIT(22) | BIT(23));
3814 	rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, val32);
3815 }
3816 
3817 static void rtl8xxxu_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
3818 {
3819 	struct rtl8xxxu_fileops *fops = priv->fops;
3820 	u32 hq, lq, nq, eq, pubq;
3821 	u32 val32;
3822 
3823 	hq = 0;
3824 	lq = 0;
3825 	nq = 0;
3826 	eq = 0;
3827 	pubq = 0;
3828 
3829 	if (priv->ep_tx_high_queue)
3830 		hq = fops->page_num_hi;
3831 	if (priv->ep_tx_low_queue)
3832 		lq = fops->page_num_lo;
3833 	if (priv->ep_tx_normal_queue)
3834 		nq = fops->page_num_norm;
3835 
3836 	val32 = (nq << RQPN_NPQ_SHIFT) | (eq << RQPN_EPQ_SHIFT);
3837 	rtl8xxxu_write32(priv, REG_RQPN_NPQ, val32);
3838 
3839 	pubq = fops->total_page_num - hq - lq - nq - 1;
3840 
3841 	val32 = RQPN_LOAD;
3842 	val32 |= (hq << RQPN_HI_PQ_SHIFT);
3843 	val32 |= (lq << RQPN_LO_PQ_SHIFT);
3844 	val32 |= (pubq << RQPN_PUB_PQ_SHIFT);
3845 
3846 	rtl8xxxu_write32(priv, REG_RQPN, val32);
3847 }
3848 
3849 void rtl8xxxu_init_burst(struct rtl8xxxu_priv *priv)
3850 {
3851 	u8 val8;
3852 
3853 	/*
3854 	 * For USB high speed set 512B packets
3855 	 */
3856 	val8 = rtl8xxxu_read8(priv, REG_RXDMA_PRO_8723B);
3857 	u8p_replace_bits(&val8, 1, RXDMA_PRO_DMA_BURST_SIZE);
3858 	u8p_replace_bits(&val8, 3, RXDMA_PRO_DMA_BURST_CNT);
3859 	val8 |= RXDMA_PRO_DMA_MODE;
3860 	rtl8xxxu_write8(priv, REG_RXDMA_PRO_8723B, val8);
3861 
3862 	/*
3863 	 * Enable single packet AMPDU
3864 	 */
3865 	val8 = rtl8xxxu_read8(priv, REG_HT_SINGLE_AMPDU_8723B);
3866 	val8 |= HT_SINGLE_AMPDU_ENABLE;
3867 	rtl8xxxu_write8(priv, REG_HT_SINGLE_AMPDU_8723B, val8);
3868 
3869 	rtl8xxxu_write16(priv, REG_MAX_AGGR_NUM, priv->fops->max_aggr_num);
3870 	rtl8xxxu_write8(priv, REG_AMPDU_MAX_TIME_8723B,
3871 			priv->fops->ampdu_max_time);
3872 	rtl8xxxu_write32(priv, REG_AGGLEN_LMT, 0xffffffff);
3873 	rtl8xxxu_write8(priv, REG_RX_PKT_LIMIT, 0x18);
3874 	rtl8xxxu_write8(priv, REG_PIFS, 0x00);
3875 	if (priv->rtl_chip == RTL8188F || priv->rtl_chip == RTL8710B ||
3876 	    priv->rtl_chip == RTL8192F) {
3877 		rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL, FWHW_TXQ_CTRL_AMPDU_RETRY);
3878 		rtl8xxxu_write32(priv, REG_FAST_EDCA_CTRL, 0x03086666);
3879 	}
3880 	rtl8xxxu_write8(priv, REG_USTIME_TSF_8723B, priv->fops->ustime_tsf_edca);
3881 	rtl8xxxu_write8(priv, REG_USTIME_EDCA, priv->fops->ustime_tsf_edca);
3882 
3883 	/* to prevent mac is reseted by bus. */
3884 	val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL);
3885 	val8 |= RSV_CTRL_WLOCK_1C | RSV_CTRL_DIS_PRST;
3886 	rtl8xxxu_write8(priv, REG_RSV_CTRL, val8);
3887 }
3888 
3889 static u8 rtl8xxxu_acquire_macid(struct rtl8xxxu_priv *priv)
3890 {
3891 	u8 macid;
3892 
3893 	macid = find_first_zero_bit(priv->mac_id_map, RTL8XXXU_MAX_MAC_ID_NUM);
3894 	if (macid < RTL8XXXU_MAX_MAC_ID_NUM)
3895 		set_bit(macid, priv->mac_id_map);
3896 
3897 	return macid;
3898 }
3899 
3900 static void rtl8xxxu_release_macid(struct rtl8xxxu_priv *priv, u8 macid)
3901 {
3902 	clear_bit(macid, priv->mac_id_map);
3903 }
3904 
3905 static inline u8 rtl8xxxu_get_macid(struct rtl8xxxu_priv *priv,
3906 				    struct ieee80211_sta *sta)
3907 {
3908 	struct rtl8xxxu_sta_info *sta_info;
3909 
3910 	if (!sta)
3911 		return 0;
3912 
3913 	sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
3914 	if (!sta_info)
3915 		return 0;
3916 
3917 	return sta_info->macid;
3918 }
3919 
3920 static void rtl8xxxu_request_hw_feature(struct rtl8xxxu_priv *priv)
3921 {
3922 	if (!priv->fops->hw_feature_report)
3923 		return;
3924 
3925 	rtl8xxxu_write8(priv, REG_C2HEVT_MSG_NORMAL, C2H_HW_FEATURE_DUMP);
3926 }
3927 
3928 static int rtl8xxxu_dump_hw_feature(struct rtl8xxxu_priv *priv)
3929 {
3930 	static const u8 bw_map[8] = { 0, 0, 160, 5, 10, 20, 40, 80 };
3931 	struct rtl8xxxu_hw_feature *hw_feature = &priv->hw_feature;
3932 	u8 feature[13];
3933 	int i, ret;
3934 	u8 id, bw;
3935 
3936 	if (!priv->fops->hw_feature_report) {
3937 		hw_feature->max_bw = 40;
3938 		return 0;
3939 	}
3940 
3941 	ret = read_poll_timeout(rtl8xxxu_read8, id,
3942 				id == C2H_HW_FEATURE_REPORT,
3943 				10000, 800000, false,
3944 				priv, REG_C2HEVT_MSG_NORMAL);
3945 	if (ret)
3946 		return ret;
3947 
3948 	for (i = 0; i < ARRAY_SIZE(feature); i++)
3949 		feature[i] = rtl8xxxu_read8(priv, REG_C2HEVT_MSG_NORMAL + 2 + i);
3950 
3951 	rtl8xxxu_write8(priv, REG_C2HEVT_MSG_NORMAL, 0);
3952 
3953 	bw = u8_get_bits(feature[6], GENMASK(2, 0));
3954 
3955 	hw_feature->max_bw = bw_map[bw];
3956 
3957 	return 0;
3958 }
3959 
3960 static int rtl8xxxu_init_device(struct ieee80211_hw *hw)
3961 {
3962 	struct rtl8xxxu_priv *priv = hw->priv;
3963 	struct device *dev = &priv->udev->dev;
3964 	struct rtl8xxxu_fileops *fops = priv->fops;
3965 	bool macpower;
3966 	int ret;
3967 	u8 val8;
3968 	u16 val16;
3969 	u32 val32;
3970 
3971 	/* Check if MAC is already powered on */
3972 	val8 = rtl8xxxu_read8(priv, REG_CR);
3973 	val16 = rtl8xxxu_read16(priv, REG_SYS_CLKR);
3974 
3975 	/*
3976 	 * Fix 92DU-VC S3 hang with the reason is that secondary mac is not
3977 	 * initialized. First MAC returns 0xea, second MAC returns 0x00
3978 	 */
3979 	if (val8 == 0xea || !(val16 & SYS_CLK_MAC_CLK_ENABLE))
3980 		macpower = false;
3981 	else
3982 		macpower = true;
3983 
3984 	if (fops->needs_full_init)
3985 		macpower = false;
3986 
3987 	ret = fops->power_on(priv);
3988 	if (ret < 0) {
3989 		dev_warn(dev, "%s: Failed power on\n", __func__);
3990 		goto exit;
3991 	}
3992 
3993 	if (!macpower)
3994 		rtl8xxxu_init_queue_reserved_page(priv);
3995 
3996 	ret = rtl8xxxu_init_queue_priority(priv);
3997 	dev_dbg(dev, "%s: init_queue_priority %i\n", __func__, ret);
3998 	if (ret)
3999 		goto exit;
4000 
4001 	/*
4002 	 * Set RX page boundary
4003 	 */
4004 	rtl8xxxu_write16(priv, REG_TRXFF_BNDY + 2, fops->trxff_boundary);
4005 
4006 	rtl8xxxu_request_hw_feature(priv);
4007 
4008 	for (int retry = 5; retry >= 0 ; retry--) {
4009 		ret = rtl8xxxu_download_firmware(priv);
4010 		dev_dbg(dev, "%s: download_firmware %i\n", __func__, ret);
4011 		if (ret != -EAGAIN)
4012 			break;
4013 		if (retry)
4014 			dev_dbg(dev, "%s: retry firmware download\n", __func__);
4015 	}
4016 	if (ret)
4017 		goto exit;
4018 	ret = rtl8xxxu_start_firmware(priv);
4019 	dev_dbg(dev, "%s: start_firmware %i\n", __func__, ret);
4020 	if (ret)
4021 		goto exit;
4022 
4023 	ret = rtl8xxxu_dump_hw_feature(priv);
4024 	if (ret) {
4025 		dev_err(dev, "failed to dump hw feature\n");
4026 		goto exit;
4027 	}
4028 
4029 	if (fops->phy_init_antenna_selection)
4030 		fops->phy_init_antenna_selection(priv);
4031 
4032 	ret = rtl8xxxu_init_mac(priv);
4033 
4034 	dev_dbg(dev, "%s: init_mac %i\n", __func__, ret);
4035 	if (ret)
4036 		goto exit;
4037 
4038 	ret = rtl8xxxu_init_phy_bb(priv);
4039 	dev_dbg(dev, "%s: init_phy_bb %i\n", __func__, ret);
4040 	if (ret)
4041 		goto exit;
4042 
4043 	ret = fops->init_phy_rf(priv);
4044 	if (ret)
4045 		goto exit;
4046 
4047 	/* Mac APLL Setting */
4048 	if (priv->rtl_chip == RTL8192F)
4049 		rtl8xxxu_write16_set(priv, REG_AFE_CTRL4, BIT(4) | BIT(15));
4050 
4051 	/* RFSW Control - clear bit 14 ?? */
4052 	if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E &&
4053 	    priv->rtl_chip != RTL8188E && priv->rtl_chip != RTL8710B &&
4054 	    priv->rtl_chip != RTL8192F)
4055 		rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, 0x00000003);
4056 
4057 	val32 = FPGA0_RF_TRSW | FPGA0_RF_TRSWB | FPGA0_RF_ANTSW |
4058 		FPGA0_RF_ANTSWB |
4059 		((FPGA0_RF_ANTSW | FPGA0_RF_ANTSWB) << FPGA0_RF_BD_CTRL_SHIFT);
4060 	if (!priv->no_pape) {
4061 		val32 |= (FPGA0_RF_PAPE |
4062 			  (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
4063 	}
4064 	rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
4065 
4066 	/* 0x860[6:5]= 00 - why? - this sets antenna B */
4067 	if (priv->rtl_chip != RTL8192E && priv->rtl_chip != RTL8188E &&
4068 	    priv->rtl_chip != RTL8710B && priv->rtl_chip != RTL8192F)
4069 		rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, 0x66f60210);
4070 
4071 	if (!macpower) {
4072 		/*
4073 		 * Set TX buffer boundary
4074 		 */
4075 		val8 = fops->total_page_num + 1;
4076 
4077 		rtl8xxxu_write8(priv, REG_TXPKTBUF_BCNQ_BDNY, val8);
4078 		rtl8xxxu_write8(priv, REG_TXPKTBUF_MGQ_BDNY, val8);
4079 		rtl8xxxu_write8(priv, REG_TXPKTBUF_WMAC_LBK_BF_HD, val8);
4080 		rtl8xxxu_write8(priv, REG_TRXFF_BNDY, val8);
4081 		rtl8xxxu_write8(priv, REG_TDECTRL + 1, val8);
4082 	}
4083 
4084 	/*
4085 	 * The vendor drivers set PBP for all devices, except 8192e.
4086 	 * There is no explanation for this in any of the sources.
4087 	 */
4088 	val8 = (fops->pbp_rx << PBP_PAGE_SIZE_RX_SHIFT) |
4089 		(fops->pbp_tx << PBP_PAGE_SIZE_TX_SHIFT);
4090 	if (priv->rtl_chip != RTL8192E)
4091 		rtl8xxxu_write8(priv, REG_PBP, val8);
4092 
4093 	dev_dbg(dev, "%s: macpower %i\n", __func__, macpower);
4094 	if (!macpower) {
4095 		ret = fops->llt_init(priv);
4096 		if (ret) {
4097 			dev_warn(dev, "%s: LLT table init failed\n", __func__);
4098 			goto exit;
4099 		}
4100 
4101 		/*
4102 		 * Chip specific quirks
4103 		 */
4104 		fops->usb_quirks(priv);
4105 
4106 		/*
4107 		 * Enable TX report and TX report timer for 8723bu/8188eu/...
4108 		 */
4109 		if (fops->has_tx_report) {
4110 			/*
4111 			 * The RTL8188EU has two types of TX reports:
4112 			 * rpt_sel=1:
4113 			 *   One report for one frame. We can use this for frames
4114 			 *   with IEEE80211_TX_CTL_REQ_TX_STATUS.
4115 			 * rpt_sel=2:
4116 			 *   One report for many frames transmitted over a period
4117 			 *   of time. (This is what REG_TX_REPORT_TIME is for.) The
4118 			 *   report includes the number of frames transmitted
4119 			 *   successfully, and the number of unsuccessful
4120 			 *   transmissions. We use this for software rate control.
4121 			 *
4122 			 * Bit 0 of REG_TX_REPORT_CTRL is required for both types.
4123 			 * Bit 1 (TX_REPORT_CTRL_TIMER_ENABLE) is required for
4124 			 * type 2.
4125 			 */
4126 			val8 = rtl8xxxu_read8(priv, REG_TX_REPORT_CTRL);
4127 			if (priv->rtl_chip == RTL8188E)
4128 				val8 |= BIT(0);
4129 			val8 |= TX_REPORT_CTRL_TIMER_ENABLE;
4130 			rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL, val8);
4131 			/* Set MAX RPT MACID */
4132 			rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL + 1, 0x02);
4133 			/* TX report Timer. Unit: 32us */
4134 			rtl8xxxu_write16(priv, REG_TX_REPORT_TIME, 0xcdf0);
4135 
4136 			/* tmp ps ? */
4137 			val8 = rtl8xxxu_read8(priv, 0xa3);
4138 			val8 &= 0xf8;
4139 			rtl8xxxu_write8(priv, 0xa3, val8);
4140 		}
4141 
4142 		if (priv->rtl_chip == RTL8710B || priv->rtl_chip == RTL8192F)
4143 			rtl8xxxu_write8(priv, REG_EARLY_MODE_CONTROL_8710B, 0);
4144 	}
4145 
4146 	/*
4147 	 * Unit in 8 bytes.
4148 	 * Get Rx PHY status in order to report RSSI and others.
4149 	 */
4150 	rtl8xxxu_write8(priv, REG_RX_DRVINFO_SZ, 4);
4151 
4152 	if (priv->rtl_chip == RTL8192E) {
4153 		rtl8xxxu_write32(priv, REG_HIMR0, 0x00);
4154 		rtl8xxxu_write32(priv, REG_HIMR1, 0x00);
4155 	} else if (priv->rtl_chip == RTL8188F) {
4156 		rtl8xxxu_write32(priv, REG_HISR0, 0xffffffff);
4157 		rtl8xxxu_write32(priv, REG_HISR1, 0xffffffff);
4158 	} else if (priv->rtl_chip == RTL8188E) {
4159 		rtl8xxxu_write32(priv, REG_HISR0, 0xffffffff);
4160 		val32 = IMR0_PSTIMEOUT | IMR0_TBDER | IMR0_CPWM | IMR0_CPWM2;
4161 		rtl8xxxu_write32(priv, REG_HIMR0, val32);
4162 		val32 = IMR1_TXERR | IMR1_RXERR | IMR1_TXFOVW | IMR1_RXFOVW;
4163 		rtl8xxxu_write32(priv, REG_HIMR1, val32);
4164 		val8 = rtl8xxxu_read8(priv, REG_USB_SPECIAL_OPTION);
4165 		val8 |= USB_SPEC_INT_BULK_SELECT;
4166 		rtl8xxxu_write8(priv, REG_USB_SPECIAL_OPTION, val8);
4167 	} else if (priv->rtl_chip == RTL8710B) {
4168 		rtl8xxxu_write32(priv, REG_HIMR0_8710B, 0);
4169 	} else if (priv->rtl_chip != RTL8192F) {
4170 		/*
4171 		 * Enable all interrupts - not obvious USB needs to do this
4172 		 */
4173 		rtl8xxxu_write32(priv, REG_HISR, 0xffffffff);
4174 		rtl8xxxu_write32(priv, REG_HIMR, 0xffffffff);
4175 	}
4176 
4177 	/*
4178 	 * Configure initial WMAC settings
4179 	 */
4180 	val32 = RCR_ACCEPT_PHYS_MATCH | RCR_ACCEPT_MCAST | RCR_ACCEPT_BCAST |
4181 		RCR_ACCEPT_MGMT_FRAME | RCR_HTC_LOC_CTRL |
4182 		RCR_APPEND_PHYSTAT | RCR_APPEND_ICV | RCR_APPEND_MIC;
4183 	rtl8xxxu_write32(priv, REG_RCR, val32);
4184 	priv->regrcr = val32;
4185 
4186 	if (fops->init_reg_rxfltmap) {
4187 		/* Accept all data frames */
4188 		rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0xffff);
4189 
4190 		/*
4191 		 * Since ADF is removed from RCR, ps-poll will not be indicate to driver,
4192 		 * RxFilterMap should mask ps-poll to gurantee AP mode can rx ps-poll.
4193 		 */
4194 		rtl8xxxu_write16(priv, REG_RXFLTMAP1, 0x400);
4195 
4196 		/* Accept all management frames */
4197 		rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0xffff);
4198 	} else {
4199 		/*
4200 		 * Accept all multicast
4201 		 */
4202 		rtl8xxxu_write32(priv, REG_MAR, 0xffffffff);
4203 		rtl8xxxu_write32(priv, REG_MAR + 4, 0xffffffff);
4204 	}
4205 
4206 	/*
4207 	 * Init adaptive controls
4208 	 */
4209 	val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4210 	val32 &= ~RESPONSE_RATE_BITMAP_ALL;
4211 	val32 |= RESPONSE_RATE_RRSR_CCK_ONLY_1M;
4212 	rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4213 
4214 	/* CCK = 0x0a, OFDM = 0x10 */
4215 	rtl8xxxu_set_spec_sifs(priv, 0x10, 0x10);
4216 	rtl8xxxu_set_retry(priv, 0x30, 0x30);
4217 	rtl8xxxu_set_spec_sifs(priv, 0x0a, 0x10);
4218 
4219 	/*
4220 	 * Init EDCA
4221 	 */
4222 	rtl8xxxu_write16(priv, REG_MAC_SPEC_SIFS, 0x100a);
4223 
4224 	/* Set CCK SIFS */
4225 	rtl8xxxu_write16(priv, REG_SIFS_CCK, 0x100a);
4226 
4227 	/* Set OFDM SIFS */
4228 	rtl8xxxu_write16(priv, REG_SIFS_OFDM, 0x100a);
4229 
4230 	/* TXOP */
4231 	rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, 0x005ea42b);
4232 	rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, 0x0000a44f);
4233 	rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, 0x005ea324);
4234 	rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, 0x002fa226);
4235 
4236 	/* Set data auto rate fallback retry count */
4237 	rtl8xxxu_write32(priv, REG_DARFRC, 0x00000000);
4238 	rtl8xxxu_write32(priv, REG_DARFRC + 4, 0x10080404);
4239 	rtl8xxxu_write32(priv, REG_RARFRC, 0x04030201);
4240 	rtl8xxxu_write32(priv, REG_RARFRC + 4, 0x08070605);
4241 
4242 	val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL);
4243 	val8 |= FWHW_TXQ_CTRL_AMPDU_RETRY;
4244 	rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL, val8);
4245 
4246 	/*  Set ACK timeout */
4247 	rtl8xxxu_write8(priv, REG_ACKTO, 0x40);
4248 
4249 	/*
4250 	 * Initialize beacon parameters
4251 	 */
4252 	val16 = BEACON_DISABLE_TSF_UPDATE | (BEACON_DISABLE_TSF_UPDATE << 8);
4253 	rtl8xxxu_write16(priv, REG_BEACON_CTRL, val16);
4254 	rtl8xxxu_write16(priv, REG_TBTT_PROHIBIT, 0x6404);
4255 	if (priv->rtl_chip != RTL8188F && priv->rtl_chip != RTL8710B &&
4256 	    priv->rtl_chip != RTL8192F)
4257 		/* Firmware will control REG_DRVERLYINT when power saving is enable, */
4258 		/* so don't set this register on STA mode. */
4259 		rtl8xxxu_write8(priv, REG_DRIVER_EARLY_INT, DRIVER_EARLY_INT_TIME);
4260 	rtl8xxxu_write8(priv, REG_BEACON_DMA_TIME, BEACON_DMA_ATIME_INT_TIME);
4261 	rtl8xxxu_write16(priv, REG_BEACON_TCFG, 0x660F);
4262 
4263 	/*
4264 	 * Initialize burst parameters
4265 	 */
4266 	if (priv->fops->init_burst)
4267 		priv->fops->init_burst(priv);
4268 
4269 	if (fops->init_aggregation)
4270 		fops->init_aggregation(priv);
4271 
4272 	if (fops->init_reg_pkt_life_time) {
4273 		rtl8xxxu_write16(priv, REG_PKT_VO_VI_LIFE_TIME, 0x0400); /* unit: 256us. 256ms */
4274 		rtl8xxxu_write16(priv, REG_PKT_BE_BK_LIFE_TIME, 0x0400); /* unit: 256us. 256ms */
4275 	}
4276 
4277 	/*
4278 	 * Enable CCK and OFDM block
4279 	 */
4280 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4281 	val32 |= (FPGA_RF_MODE_CCK | FPGA_RF_MODE_OFDM);
4282 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4283 
4284 	/*
4285 	 * Invalidate all CAM entries - bit 30 is undocumented
4286 	 */
4287 	rtl8xxxu_write32(priv, REG_CAM_CMD, CAM_CMD_POLLING | BIT(30));
4288 
4289 	/*
4290 	 * Start out with default power levels for channel 6, 20MHz
4291 	 */
4292 	fops->set_tx_power(priv, 1, false);
4293 
4294 	/* Let the 8051 take control of antenna setting */
4295 	if (priv->rtl_chip != RTL8192E && priv->rtl_chip != RTL8188F &&
4296 	    priv->rtl_chip != RTL8710B && priv->rtl_chip != RTL8192C) {
4297 		val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
4298 		val8 |= LEDCFG2_DPDT_SELECT;
4299 		rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
4300 	}
4301 
4302 	rtl8xxxu_write8(priv, REG_HWSEQ_CTRL, 0xff);
4303 
4304 	/* Disable BAR - not sure if this has any effect on USB */
4305 	rtl8xxxu_write32(priv, REG_BAR_MODE_CTRL, 0x0201ffff);
4306 
4307 	if (priv->rtl_chip != RTL8188F && priv->rtl_chip != RTL8188E &&
4308 	    priv->rtl_chip != RTL8710B && priv->rtl_chip != RTL8192F)
4309 		rtl8xxxu_write16(priv, REG_FAST_EDCA_CTRL, 0);
4310 
4311 	if (fops->init_statistics)
4312 		fops->init_statistics(priv);
4313 
4314 	if (priv->rtl_chip == RTL8192E) {
4315 		/*
4316 		 * 0x4c6[3] 1: RTS BW = Data BW
4317 		 * 0: RTS BW depends on CCA / secondary CCA result.
4318 		 */
4319 		val8 = rtl8xxxu_read8(priv, REG_QUEUE_CTRL);
4320 		val8 &= ~BIT(3);
4321 		rtl8xxxu_write8(priv, REG_QUEUE_CTRL, val8);
4322 		/*
4323 		 * Reset USB mode switch setting
4324 		 */
4325 		rtl8xxxu_write8(priv, REG_ACLK_MON, 0x00);
4326 	} else if (priv->rtl_chip == RTL8188F || priv->rtl_chip == RTL8188E ||
4327 		   priv->rtl_chip == RTL8192F) {
4328 		/*
4329 		 * Init GPIO settings for 8188f, 8188e, 8192f
4330 		 */
4331 		val8 = rtl8xxxu_read8(priv, REG_GPIO_MUXCFG);
4332 		val8 &= ~GPIO_MUXCFG_IO_SEL_ENBT;
4333 		rtl8xxxu_write8(priv, REG_GPIO_MUXCFG, val8);
4334 	}
4335 
4336 	if (priv->rtl_chip == RTL8188F)
4337 		/* CCK PD */
4338 		rtl8xxxu_write8(priv, REG_CCK_PD_THRESH, CCK_PD_TYPE1_LV1_TH);
4339 
4340 	fops->phy_lc_calibrate(priv);
4341 
4342 	fops->phy_iq_calibrate(priv);
4343 
4344 	/*
4345 	 * This should enable thermal meter
4346 	 */
4347 	if (fops->gen2_thermal_meter) {
4348 		if (priv->rtl_chip == RTL8188F || priv->rtl_chip == RTL8710B) {
4349 			val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_T_METER_8723B);
4350 			val32 |= 0x30000;
4351 			rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_T_METER_8723B, val32);
4352 		} else {
4353 			rtl8xxxu_write_rfreg(priv,
4354 					     RF_A, RF6052_REG_T_METER_8723B, 0x37cf8);
4355 		}
4356 	} else {
4357 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_T_METER, 0x60);
4358 	}
4359 
4360 	/* Set NAV_UPPER to 30000us */
4361 	val8 = ((30000 + NAV_UPPER_UNIT - 1) / NAV_UPPER_UNIT);
4362 	rtl8xxxu_write8(priv, REG_NAV_UPPER, val8);
4363 
4364 	if (priv->rtl_chip == RTL8723A) {
4365 		/*
4366 		 * 2011/03/09 MH debug only, UMC-B cut pass 2500 S5 test,
4367 		 * but we need to find root cause.
4368 		 * This is 8723au only.
4369 		 */
4370 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4371 		if ((val32 & 0xff000000) != 0x83000000) {
4372 			val32 |= FPGA_RF_MODE_CCK;
4373 			rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4374 		}
4375 	} else if (priv->rtl_chip == RTL8192E || priv->rtl_chip == RTL8188E) {
4376 		rtl8xxxu_write8(priv, REG_USB_HRPWM, 0x00);
4377 	}
4378 
4379 	val32 = rtl8xxxu_read32(priv, REG_FWHW_TXQ_CTRL);
4380 	val32 |= FWHW_TXQ_CTRL_XMIT_MGMT_ACK;
4381 	/* ack for xmit mgmt frames. */
4382 	rtl8xxxu_write32(priv, REG_FWHW_TXQ_CTRL, val32);
4383 
4384 	if (priv->rtl_chip == RTL8192E) {
4385 		/*
4386 		 * Fix LDPC rx hang issue.
4387 		 */
4388 		val32 = rtl8xxxu_read32(priv, REG_AFE_MISC);
4389 		rtl8xxxu_write8(priv, REG_8192E_LDOV12_CTRL, 0x75);
4390 		val32 &= 0xfff00fff;
4391 		val32 |= 0x0007e000;
4392 		rtl8xxxu_write32(priv, REG_AFE_MISC, val32);
4393 
4394 		/*
4395 		 * 0x824[9] = 0x82C[9] = 0xA80[7] those registers setting
4396 		 * should be equal or CCK RSSI report may be incorrect
4397 		 */
4398 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM2);
4399 		priv->cck_agc_report_type =
4400 			u32_get_bits(val32, FPGA0_HSSI_PARM2_CCK_HIGH_PWR);
4401 
4402 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_HSSI_PARM2);
4403 		if (priv->cck_agc_report_type !=
4404 		    u32_get_bits(val32, FPGA0_HSSI_PARM2_CCK_HIGH_PWR)) {
4405 			if (priv->cck_agc_report_type)
4406 				val32 |= FPGA0_HSSI_PARM2_CCK_HIGH_PWR;
4407 			else
4408 				val32 &= ~FPGA0_HSSI_PARM2_CCK_HIGH_PWR;
4409 			rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM2, val32);
4410 		}
4411 
4412 		val32 = rtl8xxxu_read32(priv, REG_AGC_RPT);
4413 		if (priv->cck_agc_report_type)
4414 			val32 |= AGC_RPT_CCK;
4415 		else
4416 			val32 &= ~AGC_RPT_CCK;
4417 		rtl8xxxu_write32(priv, REG_AGC_RPT, val32);
4418 	}
4419 
4420 	if (priv->rtl_chip == RTL8710B) {
4421 		/*
4422 		 * 0x76D[5:4] is Port0,Port1 Enable Bit.
4423 		 * This is only for 8710B, 2b'00 for MP and 2b'11 for Normal Driver
4424 		 */
4425 		val8 = rtl8xxxu_read8(priv, REG_PORT_CONTROL_8710B);
4426 		val8 |= BIT(5) | BIT(4);
4427 		rtl8xxxu_write8(priv, REG_PORT_CONTROL_8710B, val8);
4428 
4429 		/* Set 0x5c[8] and [2:0] = 1, LDO mode */
4430 		val32 = rtl8xxxu_read32(priv, REG_WL_RF_PSS_8710B);
4431 		val32 |= 0x107;
4432 		rtl8xxxu_write32(priv, REG_WL_RF_PSS_8710B, val32);
4433 	}
4434 
4435 	val32 = rtl8xxxu_read32(priv, 0xa9c);
4436 	priv->cck_new_agc = u32_get_bits(val32, BIT(17));
4437 
4438 	/* Initialise the center frequency offset tracking */
4439 	if (priv->fops->set_crystal_cap) {
4440 		val32 = rtl8xxxu_read32(priv, REG_OFDM1_CFO_TRACKING);
4441 		priv->cfo_tracking.atc_status = val32 & CFO_TRACKING_ATC_STATUS;
4442 		priv->cfo_tracking.adjust = true;
4443 		priv->cfo_tracking.crystal_cap = priv->default_crystal_cap;
4444 	}
4445 
4446 	if (priv->rtl_chip == RTL8188E)
4447 		rtl8188e_ra_info_init_all(&priv->ra_info);
4448 
4449 	set_bit(RTL8XXXU_BC_MC_MACID, priv->mac_id_map);
4450 	set_bit(RTL8XXXU_BC_MC_MACID1, priv->mac_id_map);
4451 
4452 exit:
4453 	return ret;
4454 }
4455 
4456 static void rtl8xxxu_cam_write(struct rtl8xxxu_priv *priv,
4457 			       struct ieee80211_key_conf *key, const u8 *mac)
4458 {
4459 	u32 cmd, val32, addr, ctrl;
4460 	int j, i, tmp_debug;
4461 
4462 	tmp_debug = rtl8xxxu_debug;
4463 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_KEY)
4464 		rtl8xxxu_debug |= RTL8XXXU_DEBUG_REG_WRITE;
4465 
4466 	/*
4467 	 * This is a bit of a hack - the lower bits of the cipher
4468 	 * suite selector happens to match the cipher index in the CAM
4469 	 */
4470 	addr = key->hw_key_idx << CAM_CMD_KEY_SHIFT;
4471 	ctrl = (key->cipher & 0x0f) << 2 | key->keyidx | CAM_WRITE_VALID;
4472 	if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
4473 		ctrl |= BIT(6);
4474 
4475 	for (j = 5; j >= 0; j--) {
4476 		switch (j) {
4477 		case 0:
4478 			val32 = ctrl | (mac[0] << 16) | (mac[1] << 24);
4479 			break;
4480 		case 1:
4481 			val32 = mac[2] | (mac[3] << 8) |
4482 				(mac[4] << 16) | (mac[5] << 24);
4483 			break;
4484 		default:
4485 			i = (j - 2) << 2;
4486 			val32 = key->key[i] | (key->key[i + 1] << 8) |
4487 				key->key[i + 2] << 16 | key->key[i + 3] << 24;
4488 			break;
4489 		}
4490 
4491 		rtl8xxxu_write32(priv, REG_CAM_WRITE, val32);
4492 		cmd = CAM_CMD_POLLING | CAM_CMD_WRITE | (addr + j);
4493 		rtl8xxxu_write32(priv, REG_CAM_CMD, cmd);
4494 		udelay(100);
4495 	}
4496 
4497 	rtl8xxxu_debug = tmp_debug;
4498 }
4499 
4500 static
4501 int rtl8xxxu_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant,
4502 			 u32 *rx_ant)
4503 {
4504 	struct rtl8xxxu_priv *priv = hw->priv;
4505 
4506 	*tx_ant = BIT(priv->tx_paths) - 1;
4507 	*rx_ant = BIT(priv->rx_paths) - 1;
4508 
4509 	return 0;
4510 }
4511 
4512 static int rtl8xxxu_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
4513 			    bool set)
4514 {
4515 	struct rtl8xxxu_priv *priv = hw->priv;
4516 
4517 	schedule_delayed_work(&priv->update_beacon_work, 0);
4518 
4519 	return 0;
4520 }
4521 
4522 static void rtl8xxxu_sw_scan_start(struct ieee80211_hw *hw,
4523 				   struct ieee80211_vif *vif, const u8 *mac)
4524 {
4525 	struct rtl8xxxu_priv *priv = hw->priv;
4526 	u8 val8;
4527 
4528 	val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4529 	val8 |= BEACON_DISABLE_TSF_UPDATE;
4530 	rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4531 }
4532 
4533 static void rtl8xxxu_sw_scan_complete(struct ieee80211_hw *hw,
4534 				      struct ieee80211_vif *vif)
4535 {
4536 	struct rtl8xxxu_priv *priv = hw->priv;
4537 	u8 val8;
4538 
4539 	val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4540 	val8 &= ~BEACON_DISABLE_TSF_UPDATE;
4541 	rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4542 }
4543 
4544 void rtl8xxxu_update_rate_mask(struct rtl8xxxu_priv *priv,
4545 			       u32 ramask, u8 rateid, int sgi, int txbw_40mhz,
4546 			       u8 macid)
4547 {
4548 	struct h2c_cmd h2c;
4549 
4550 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4551 
4552 	h2c.ramask.cmd = H2C_SET_RATE_MASK;
4553 	h2c.ramask.mask_lo = cpu_to_le16(ramask & 0xffff);
4554 	h2c.ramask.mask_hi = cpu_to_le16(ramask >> 16);
4555 
4556 	h2c.ramask.arg = 0x80;
4557 	if (sgi)
4558 		h2c.ramask.arg |= 0x20;
4559 
4560 	dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
4561 		__func__, ramask, h2c.ramask.arg, sizeof(h2c.ramask));
4562 	rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.ramask));
4563 }
4564 
4565 void rtl8xxxu_gen2_update_rate_mask(struct rtl8xxxu_priv *priv,
4566 				    u32 ramask, u8 rateid, int sgi, int txbw_40mhz,
4567 				    u8 macid)
4568 {
4569 	struct h2c_cmd h2c;
4570 	u8 bw;
4571 
4572 	if (txbw_40mhz)
4573 		bw = RTL8XXXU_CHANNEL_WIDTH_40;
4574 	else
4575 		bw = RTL8XXXU_CHANNEL_WIDTH_20;
4576 
4577 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4578 
4579 	h2c.b_macid_cfg.cmd = H2C_8723B_MACID_CFG_RAID;
4580 	h2c.b_macid_cfg.ramask0 = ramask & 0xff;
4581 	h2c.b_macid_cfg.ramask1 = (ramask >> 8) & 0xff;
4582 	h2c.b_macid_cfg.ramask2 = (ramask >> 16) & 0xff;
4583 	h2c.b_macid_cfg.ramask3 = (ramask >> 24) & 0xff;
4584 	h2c.b_macid_cfg.macid = macid;
4585 
4586 	h2c.b_macid_cfg.data1 = rateid;
4587 	if (sgi)
4588 		h2c.b_macid_cfg.data1 |= BIT(7);
4589 
4590 	h2c.b_macid_cfg.data2 = bw;
4591 
4592 	dev_dbg(&priv->udev->dev, "%s: rate mask %08x, rateid %02x, sgi %d, size %zi\n",
4593 		__func__, ramask, rateid, sgi, sizeof(h2c.b_macid_cfg));
4594 	rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_macid_cfg));
4595 }
4596 
4597 void rtl8xxxu_gen1_report_connect(struct rtl8xxxu_priv *priv,
4598 				  u8 macid, u8 role, bool connect)
4599 {
4600 	struct h2c_cmd h2c;
4601 
4602 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4603 
4604 	h2c.joinbss.cmd = H2C_JOIN_BSS_REPORT;
4605 
4606 	if (connect)
4607 		h2c.joinbss.data = H2C_JOIN_BSS_CONNECT;
4608 	else
4609 		h2c.joinbss.data = H2C_JOIN_BSS_DISCONNECT;
4610 
4611 	rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.joinbss));
4612 }
4613 
4614 void rtl8xxxu_gen2_report_connect(struct rtl8xxxu_priv *priv,
4615 				  u8 macid, u8 role, bool connect)
4616 {
4617 	/*
4618 	 * The firmware turns on the rate control when it knows it's
4619 	 * connected to a network.
4620 	 */
4621 	struct h2c_cmd h2c;
4622 
4623 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4624 
4625 	h2c.media_status_rpt.cmd = H2C_8723B_MEDIA_STATUS_RPT;
4626 	if (connect)
4627 		h2c.media_status_rpt.parm |= BIT(0);
4628 	else
4629 		h2c.media_status_rpt.parm &= ~BIT(0);
4630 
4631 	h2c.media_status_rpt.parm |= ((role << 4) & 0xf0);
4632 	h2c.media_status_rpt.macid = macid;
4633 
4634 	rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.media_status_rpt));
4635 }
4636 
4637 void rtl8xxxu_gen1_report_rssi(struct rtl8xxxu_priv *priv, u8 macid, u8 rssi)
4638 {
4639 	struct h2c_cmd h2c;
4640 	const int h2c_size = 4;
4641 
4642 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4643 
4644 	h2c.rssi_report.cmd = H2C_SET_RSSI;
4645 	h2c.rssi_report.macid = macid;
4646 	h2c.rssi_report.rssi = rssi;
4647 
4648 	rtl8xxxu_gen1_h2c_cmd(priv, &h2c, h2c_size);
4649 }
4650 
4651 void rtl8xxxu_gen2_report_rssi(struct rtl8xxxu_priv *priv, u8 macid, u8 rssi)
4652 {
4653 	struct h2c_cmd h2c;
4654 	int h2c_size = sizeof(h2c.rssi_report);
4655 
4656 	if (priv->rtl_chip == RTL8723B)
4657 		h2c_size = 4;
4658 
4659 	memset(&h2c, 0, sizeof(struct h2c_cmd));
4660 
4661 	h2c.rssi_report.cmd = H2C_8723B_RSSI_SETTING;
4662 	h2c.rssi_report.macid = macid;
4663 	h2c.rssi_report.rssi = rssi;
4664 
4665 	rtl8xxxu_gen2_h2c_cmd(priv, &h2c, h2c_size);
4666 }
4667 
4668 void rtl8xxxu_gen1_init_aggregation(struct rtl8xxxu_priv *priv)
4669 {
4670 	u8 agg_ctrl, usb_spec, page_thresh, timeout;
4671 
4672 	usb_spec = rtl8xxxu_read8(priv, REG_USB_SPECIAL_OPTION);
4673 	usb_spec &= ~USB_SPEC_USB_AGG_ENABLE;
4674 	rtl8xxxu_write8(priv, REG_USB_SPECIAL_OPTION, usb_spec);
4675 
4676 	agg_ctrl = rtl8xxxu_read8(priv, REG_TRXDMA_CTRL);
4677 	agg_ctrl &= ~TRXDMA_CTRL_RXDMA_AGG_EN;
4678 
4679 	if (!rtl8xxxu_dma_aggregation) {
4680 		rtl8xxxu_write8(priv, REG_TRXDMA_CTRL, agg_ctrl);
4681 		return;
4682 	}
4683 
4684 	agg_ctrl |= TRXDMA_CTRL_RXDMA_AGG_EN;
4685 	rtl8xxxu_write8(priv, REG_TRXDMA_CTRL, agg_ctrl);
4686 
4687 	/*
4688 	 * The number of packets we can take looks to be buffer size / 512
4689 	 * which matches the 512 byte rounding we have to do when de-muxing
4690 	 * the packets.
4691 	 *
4692 	 * Sample numbers from the vendor driver:
4693 	 * USB High-Speed mode values:
4694 	 *   RxAggBlockCount = 8 : 512 byte unit
4695 	 *   RxAggBlockTimeout = 6
4696 	 *   RxAggPageCount = 48 : 128 byte unit
4697 	 *   RxAggPageTimeout = 4 or 6 (absolute time 34ms/(2^6))
4698 	 */
4699 
4700 	page_thresh = (priv->fops->rx_agg_buf_size / 512);
4701 	if (rtl8xxxu_dma_agg_pages >= 0) {
4702 		if (rtl8xxxu_dma_agg_pages <= page_thresh)
4703 			timeout = page_thresh;
4704 		else if (rtl8xxxu_dma_agg_pages <= 6)
4705 			dev_err(&priv->udev->dev,
4706 				"%s: dma_agg_pages=%i too small, minimum is 6\n",
4707 				__func__, rtl8xxxu_dma_agg_pages);
4708 		else
4709 			dev_err(&priv->udev->dev,
4710 				"%s: dma_agg_pages=%i larger than limit %i\n",
4711 				__func__, rtl8xxxu_dma_agg_pages, page_thresh);
4712 	}
4713 	rtl8xxxu_write8(priv, REG_RXDMA_AGG_PG_TH, page_thresh);
4714 	/*
4715 	 * REG_RXDMA_AGG_PG_TH + 1 seems to be the timeout register on
4716 	 * gen2 chips and rtl8188eu. The rtl8723au seems unhappy if we
4717 	 * don't set it, so better set both.
4718 	 */
4719 	timeout = 4;
4720 
4721 	if (rtl8xxxu_dma_agg_timeout >= 0) {
4722 		if (rtl8xxxu_dma_agg_timeout <= 127)
4723 			timeout = rtl8xxxu_dma_agg_timeout;
4724 		else
4725 			dev_err(&priv->udev->dev,
4726 				"%s: Invalid dma_agg_timeout: %i\n",
4727 				__func__, rtl8xxxu_dma_agg_timeout);
4728 	}
4729 
4730 	rtl8xxxu_write8(priv, REG_RXDMA_AGG_PG_TH + 1, timeout);
4731 	rtl8xxxu_write8(priv, REG_USB_DMA_AGG_TO, timeout);
4732 	priv->rx_buf_aggregation = 1;
4733 }
4734 
4735 static const struct ieee80211_rate rtl8xxxu_legacy_ratetable[] = {
4736 	{.bitrate = 10, .hw_value = 0x00,},
4737 	{.bitrate = 20, .hw_value = 0x01,},
4738 	{.bitrate = 55, .hw_value = 0x02,},
4739 	{.bitrate = 110, .hw_value = 0x03,},
4740 	{.bitrate = 60, .hw_value = 0x04,},
4741 	{.bitrate = 90, .hw_value = 0x05,},
4742 	{.bitrate = 120, .hw_value = 0x06,},
4743 	{.bitrate = 180, .hw_value = 0x07,},
4744 	{.bitrate = 240, .hw_value = 0x08,},
4745 	{.bitrate = 360, .hw_value = 0x09,},
4746 	{.bitrate = 480, .hw_value = 0x0a,},
4747 	{.bitrate = 540, .hw_value = 0x0b,},
4748 };
4749 
4750 static void rtl8xxxu_set_basic_rates(struct rtl8xxxu_priv *priv, u32 rate_cfg)
4751 {
4752 	struct ieee80211_hw *hw = priv->hw;
4753 	u32 val32;
4754 	u8 rate_idx = 0;
4755 
4756 	rate_cfg &= RESPONSE_RATE_BITMAP_ALL;
4757 
4758 	val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4759 	if (hw->conf.chandef.chan->band == NL80211_BAND_5GHZ)
4760 		val32 &= RESPONSE_RATE_RRSR_INIT_5G;
4761 	else
4762 		val32 &= RESPONSE_RATE_RRSR_INIT_2G;
4763 	val32 |= rate_cfg;
4764 	rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4765 
4766 	dev_dbg(&priv->udev->dev, "%s: rates %08x\n", __func__,	rate_cfg);
4767 
4768 	if (rate_cfg)
4769 		rate_idx = __fls(rate_cfg);
4770 
4771 	rtl8xxxu_write8(priv, REG_INIRTS_RATE_SEL, rate_idx);
4772 }
4773 
4774 static u16
4775 rtl8xxxu_wireless_mode(struct ieee80211_hw *hw, struct ieee80211_sta *sta)
4776 {
4777 	u16 network_type = WIRELESS_MODE_UNKNOWN;
4778 
4779 	if (hw->conf.chandef.chan->band == NL80211_BAND_5GHZ) {
4780 		if (sta->deflink.vht_cap.vht_supported)
4781 			network_type = WIRELESS_MODE_AC;
4782 		else if (sta->deflink.ht_cap.ht_supported)
4783 			network_type = WIRELESS_MODE_N_5G;
4784 
4785 		network_type |= WIRELESS_MODE_A;
4786 	} else {
4787 		if (sta->deflink.vht_cap.vht_supported)
4788 			network_type = WIRELESS_MODE_AC;
4789 		else if (sta->deflink.ht_cap.ht_supported)
4790 			network_type = WIRELESS_MODE_N_24G;
4791 
4792 		if (sta->deflink.supp_rates[0] <= 0xf)
4793 			network_type |= WIRELESS_MODE_B;
4794 		else if (sta->deflink.supp_rates[0] & 0xf)
4795 			network_type |= (WIRELESS_MODE_B | WIRELESS_MODE_G);
4796 		else
4797 			network_type |= WIRELESS_MODE_G;
4798 	}
4799 
4800 	return network_type;
4801 }
4802 
4803 static void rtl8xxxu_set_aifs(struct rtl8xxxu_priv *priv, u8 slot_time)
4804 {
4805 	u32 reg_edca_param[IEEE80211_NUM_ACS] = {
4806 		[IEEE80211_AC_VO] = REG_EDCA_VO_PARAM,
4807 		[IEEE80211_AC_VI] = REG_EDCA_VI_PARAM,
4808 		[IEEE80211_AC_BE] = REG_EDCA_BE_PARAM,
4809 		[IEEE80211_AC_BK] = REG_EDCA_BK_PARAM,
4810 	};
4811 	u32 val32;
4812 	u16 wireless_mode = 0;
4813 	u8 aifs, aifsn, sifs;
4814 	int i;
4815 
4816 	for (i = 0; i < ARRAY_SIZE(priv->vifs); i++) {
4817 		struct ieee80211_sta *sta;
4818 
4819 		if (!priv->vifs[i])
4820 			continue;
4821 
4822 		rcu_read_lock();
4823 		sta = ieee80211_find_sta(priv->vifs[i], priv->vifs[i]->bss_conf.bssid);
4824 		if (sta)
4825 			wireless_mode = rtl8xxxu_wireless_mode(priv->hw, sta);
4826 		rcu_read_unlock();
4827 
4828 		if (wireless_mode)
4829 			break;
4830 	}
4831 
4832 	if (priv->hw->conf.chandef.chan->band == NL80211_BAND_5GHZ ||
4833 	    (wireless_mode & WIRELESS_MODE_N_24G))
4834 		sifs = 16;
4835 	else
4836 		sifs = 10;
4837 
4838 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
4839 		val32 = rtl8xxxu_read32(priv, reg_edca_param[i]);
4840 
4841 		/* It was set in conf_tx. */
4842 		aifsn = val32 & 0xff;
4843 
4844 		/* aifsn not set yet or already fixed */
4845 		if (aifsn < 2 || aifsn > 15)
4846 			continue;
4847 
4848 		aifs = aifsn * slot_time + sifs;
4849 
4850 		val32 &= ~0xff;
4851 		val32 |= aifs;
4852 		rtl8xxxu_write32(priv, reg_edca_param[i], val32);
4853 	}
4854 }
4855 
4856 void rtl8xxxu_update_ra_report(struct rtl8xxxu_ra_report *rarpt,
4857 			       u8 rate, u8 sgi, u8 bw)
4858 {
4859 	rarpt->txrate.flags = 0;
4860 
4861 	if (rate <= DESC_RATE_54M) {
4862 		rarpt->txrate.legacy = rtl8xxxu_legacy_ratetable[rate].bitrate;
4863 	} else if (rate >= DESC_RATE_MCS0 && rate <= DESC_RATE_MCS15) {
4864 		rarpt->txrate.flags |= RATE_INFO_FLAGS_MCS;
4865 		if (rate < DESC_RATE_MCS8)
4866 			rarpt->txrate.nss = 1;
4867 		else
4868 			rarpt->txrate.nss = 2;
4869 
4870 		rarpt->txrate.mcs = rate - DESC_RATE_MCS0;
4871 
4872 		if (sgi)
4873 			rarpt->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
4874 
4875 		rarpt->txrate.bw = bw;
4876 	} else {
4877 		return;
4878 	}
4879 
4880 	rarpt->bit_rate = cfg80211_calculate_bitrate(&rarpt->txrate);
4881 	rarpt->desc_rate = rate;
4882 }
4883 
4884 static void
4885 rtl8xxxu_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4886 			  struct ieee80211_bss_conf *bss_conf, u64 changed)
4887 {
4888 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
4889 	struct rtl8xxxu_priv *priv = hw->priv;
4890 	struct device *dev = &priv->udev->dev;
4891 	struct rtl8xxxu_sta_info *sta_info;
4892 	struct ieee80211_sta *sta;
4893 	struct rtl8xxxu_ra_report *rarpt;
4894 	u8 val8, macid;
4895 	u32 val32;
4896 
4897 	rarpt = &priv->ra_report;
4898 
4899 	if (changed & BSS_CHANGED_ASSOC) {
4900 		dev_dbg(dev, "Changed ASSOC: %i!\n", vif->cfg.assoc);
4901 
4902 		rtl8xxxu_set_linktype(priv, vif->type, rtlvif->port_num);
4903 
4904 		if (vif->cfg.assoc) {
4905 			u32 ramask;
4906 			int sgi = 0;
4907 			u8 highest_rate;
4908 			u8 bw;
4909 
4910 			rcu_read_lock();
4911 			sta = ieee80211_find_sta(vif, bss_conf->bssid);
4912 			if (!sta) {
4913 				dev_info(dev, "%s: ASSOC no sta found\n",
4914 					 __func__);
4915 				rcu_read_unlock();
4916 				goto error;
4917 			}
4918 			macid = rtl8xxxu_get_macid(priv, sta);
4919 
4920 			if (sta->deflink.ht_cap.ht_supported)
4921 				dev_info(dev, "%s: HT supported\n", __func__);
4922 			if (sta->deflink.vht_cap.vht_supported)
4923 				dev_info(dev, "%s: VHT supported\n", __func__);
4924 
4925 			/* TODO: Set bits 28-31 for rate adaptive id */
4926 			ramask = (sta->deflink.supp_rates[0] & 0xfff) |
4927 				sta->deflink.ht_cap.mcs.rx_mask[0] << 12 |
4928 				sta->deflink.ht_cap.mcs.rx_mask[1] << 20;
4929 			if (sta->deflink.ht_cap.cap &
4930 			    (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
4931 				sgi = 1;
4932 
4933 			highest_rate = fls(ramask) - 1;
4934 			if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
4935 				bw = RATE_INFO_BW_40;
4936 			else
4937 				bw = RATE_INFO_BW_20;
4938 
4939 			sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
4940 			sta_info->rssi_level = RTL8XXXU_RATR_STA_INIT;
4941 			rcu_read_unlock();
4942 
4943 			rtl8xxxu_update_ra_report(rarpt, highest_rate, sgi, bw);
4944 
4945 			priv->fops->update_rate_mask(priv, ramask, 0, sgi,
4946 						     bw == RATE_INFO_BW_40, macid);
4947 
4948 			rtl8xxxu_write8(priv, REG_BCN_MAX_ERR, 0xff);
4949 
4950 			if (rtlvif->port_num == 0)
4951 				rtl8xxxu_stop_tx_beacon(priv);
4952 
4953 			/* joinbss sequence */
4954 			rtl8xxxu_write16(priv, REG_BCN_PSR_RPT,
4955 					 0xc000 | vif->cfg.aid);
4956 
4957 			priv->fops->report_connect(priv, 0, H2C_MACID_ROLE_AP, true);
4958 		} else {
4959 			val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4960 			val8 |= BEACON_DISABLE_TSF_UPDATE;
4961 			rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4962 
4963 			priv->fops->report_connect(priv, 0, H2C_MACID_ROLE_AP, false);
4964 		}
4965 	}
4966 
4967 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4968 		dev_dbg(dev, "Changed ERP_PREAMBLE: Use short preamble %i\n",
4969 			bss_conf->use_short_preamble);
4970 		val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4971 		if (bss_conf->use_short_preamble)
4972 			val32 |= RSR_ACK_SHORT_PREAMBLE;
4973 		else
4974 			val32 &= ~RSR_ACK_SHORT_PREAMBLE;
4975 		rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4976 	}
4977 
4978 	if (changed & BSS_CHANGED_ERP_SLOT) {
4979 		dev_dbg(dev, "Changed ERP_SLOT: short_slot_time %i\n",
4980 			bss_conf->use_short_slot);
4981 
4982 		if (bss_conf->use_short_slot)
4983 			val8 = 9;
4984 		else
4985 			val8 = 20;
4986 		rtl8xxxu_write8(priv, REG_SLOT, val8);
4987 
4988 		rtl8xxxu_set_aifs(priv, val8);
4989 	}
4990 
4991 	if (changed & BSS_CHANGED_BSSID) {
4992 		dev_dbg(dev, "Changed BSSID!\n");
4993 		rtl8xxxu_set_bssid(priv, bss_conf->bssid, rtlvif->port_num);
4994 	}
4995 
4996 	if (changed & BSS_CHANGED_BASIC_RATES) {
4997 		dev_dbg(dev, "Changed BASIC_RATES!\n");
4998 		rtl8xxxu_set_basic_rates(priv, bss_conf->basic_rates);
4999 	}
5000 
5001 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
5002 		if (bss_conf->enable_beacon) {
5003 			rtl8xxxu_start_tx_beacon(priv);
5004 			schedule_delayed_work(&priv->update_beacon_work, 0);
5005 		} else {
5006 			rtl8xxxu_stop_tx_beacon(priv);
5007 		}
5008 	}
5009 
5010 	if (changed & BSS_CHANGED_BEACON)
5011 		schedule_delayed_work(&priv->update_beacon_work, 0);
5012 
5013 error:
5014 	return;
5015 }
5016 
5017 static int rtl8xxxu_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5018 			     struct ieee80211_bss_conf *link_conf)
5019 {
5020 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
5021 	struct rtl8xxxu_priv *priv = hw->priv;
5022 	struct device *dev = &priv->udev->dev;
5023 
5024 	dev_dbg(dev, "Start AP mode\n");
5025 	rtl8xxxu_set_bssid(priv, vif->bss_conf.bssid, rtlvif->port_num);
5026 	rtl8xxxu_write16(priv, REG_BCN_INTERVAL, vif->bss_conf.beacon_int);
5027 	priv->fops->report_connect(priv, RTL8XXXU_BC_MC_MACID, 0, true);
5028 
5029 	return 0;
5030 }
5031 
5032 static u32 rtl8xxxu_80211_to_rtl_queue(u32 queue)
5033 {
5034 	u32 rtlqueue;
5035 
5036 	switch (queue) {
5037 	case IEEE80211_AC_VO:
5038 		rtlqueue = TXDESC_QUEUE_VO;
5039 		break;
5040 	case IEEE80211_AC_VI:
5041 		rtlqueue = TXDESC_QUEUE_VI;
5042 		break;
5043 	case IEEE80211_AC_BE:
5044 		rtlqueue = TXDESC_QUEUE_BE;
5045 		break;
5046 	case IEEE80211_AC_BK:
5047 		rtlqueue = TXDESC_QUEUE_BK;
5048 		break;
5049 	default:
5050 		rtlqueue = TXDESC_QUEUE_BE;
5051 	}
5052 
5053 	return rtlqueue;
5054 }
5055 
5056 static u32 rtl8xxxu_queue_select(struct ieee80211_hdr *hdr, struct sk_buff *skb)
5057 {
5058 	u32 queue;
5059 
5060 	if (unlikely(ieee80211_is_beacon(hdr->frame_control)))
5061 		queue = TXDESC_QUEUE_BEACON;
5062 	else if (ieee80211_is_mgmt(hdr->frame_control))
5063 		queue = TXDESC_QUEUE_MGNT;
5064 	else
5065 		queue = rtl8xxxu_80211_to_rtl_queue(skb_get_queue_mapping(skb));
5066 
5067 	return queue;
5068 }
5069 
5070 /*
5071  * Despite newer chips 8723b/8812/8821 having a larger TX descriptor
5072  * format. The descriptor checksum is still only calculated over the
5073  * initial 32 bytes of the descriptor!
5074  */
5075 static void rtl8xxxu_calc_tx_desc_csum(struct rtl8xxxu_txdesc32 *tx_desc)
5076 {
5077 	__le16 *ptr = (__le16 *)tx_desc;
5078 	u16 csum = 0;
5079 	int i;
5080 
5081 	/*
5082 	 * Clear csum field before calculation, as the csum field is
5083 	 * in the middle of the struct.
5084 	 */
5085 	tx_desc->csum = cpu_to_le16(0);
5086 
5087 	for (i = 0; i < (sizeof(struct rtl8xxxu_txdesc32) / sizeof(u16)); i++)
5088 		csum = csum ^ le16_to_cpu(ptr[i]);
5089 
5090 	tx_desc->csum |= cpu_to_le16(csum);
5091 }
5092 
5093 static void rtl8xxxu_free_tx_resources(struct rtl8xxxu_priv *priv)
5094 {
5095 	struct rtl8xxxu_tx_urb *tx_urb, *tmp;
5096 	unsigned long flags;
5097 
5098 	spin_lock_irqsave(&priv->tx_urb_lock, flags);
5099 	list_for_each_entry_safe(tx_urb, tmp, &priv->tx_urb_free_list, list) {
5100 		list_del(&tx_urb->list);
5101 		priv->tx_urb_free_count--;
5102 		usb_free_urb(&tx_urb->urb);
5103 	}
5104 	spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
5105 }
5106 
5107 static struct rtl8xxxu_tx_urb *
5108 rtl8xxxu_alloc_tx_urb(struct rtl8xxxu_priv *priv)
5109 {
5110 	struct rtl8xxxu_tx_urb *tx_urb;
5111 	unsigned long flags;
5112 
5113 	spin_lock_irqsave(&priv->tx_urb_lock, flags);
5114 	tx_urb = list_first_entry_or_null(&priv->tx_urb_free_list,
5115 					  struct rtl8xxxu_tx_urb, list);
5116 	if (tx_urb) {
5117 		list_del(&tx_urb->list);
5118 		priv->tx_urb_free_count--;
5119 		if (priv->tx_urb_free_count < RTL8XXXU_TX_URB_LOW_WATER &&
5120 		    !priv->tx_stopped) {
5121 			priv->tx_stopped = true;
5122 			ieee80211_stop_queues(priv->hw);
5123 		}
5124 	}
5125 
5126 	spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
5127 
5128 	return tx_urb;
5129 }
5130 
5131 static void rtl8xxxu_free_tx_urb(struct rtl8xxxu_priv *priv,
5132 				 struct rtl8xxxu_tx_urb *tx_urb)
5133 {
5134 	unsigned long flags;
5135 
5136 	INIT_LIST_HEAD(&tx_urb->list);
5137 
5138 	spin_lock_irqsave(&priv->tx_urb_lock, flags);
5139 
5140 	list_add(&tx_urb->list, &priv->tx_urb_free_list);
5141 	priv->tx_urb_free_count++;
5142 	if (priv->tx_urb_free_count > RTL8XXXU_TX_URB_HIGH_WATER &&
5143 	    priv->tx_stopped) {
5144 		priv->tx_stopped = false;
5145 		ieee80211_wake_queues(priv->hw);
5146 	}
5147 
5148 	spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
5149 }
5150 
5151 static void rtl8xxxu_tx_complete(struct urb *urb)
5152 {
5153 	struct sk_buff *skb = (struct sk_buff *)urb->context;
5154 	struct ieee80211_tx_info *tx_info;
5155 	struct ieee80211_hw *hw;
5156 	struct rtl8xxxu_priv *priv;
5157 	struct rtl8xxxu_tx_urb *tx_urb =
5158 		container_of(urb, struct rtl8xxxu_tx_urb, urb);
5159 
5160 	tx_info = IEEE80211_SKB_CB(skb);
5161 	hw = tx_info->rate_driver_data[0];
5162 	priv = hw->priv;
5163 
5164 	skb_pull(skb, priv->fops->tx_desc_size);
5165 
5166 	ieee80211_tx_info_clear_status(tx_info);
5167 	tx_info->status.rates[0].idx = -1;
5168 	tx_info->status.rates[0].count = 0;
5169 
5170 	if (!urb->status)
5171 		tx_info->flags |= IEEE80211_TX_STAT_ACK;
5172 
5173 	ieee80211_tx_status_irqsafe(hw, skb);
5174 
5175 	rtl8xxxu_free_tx_urb(priv, tx_urb);
5176 }
5177 
5178 static void rtl8xxxu_dump_action(struct device *dev,
5179 				 struct ieee80211_hdr *hdr, unsigned int skb_len)
5180 {
5181 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
5182 	u16 cap, timeout;
5183 
5184 	if (!(rtl8xxxu_debug & RTL8XXXU_DEBUG_ACTION))
5185 		return;
5186 
5187 	if (skb_len < IEEE80211_MIN_ACTION_SIZE(action_code))
5188 		return;
5189 
5190 	switch (mgmt->u.action.action_code) {
5191 	case WLAN_ACTION_ADDBA_RESP:
5192 		if (skb_len < IEEE80211_MIN_ACTION_SIZE(addba_resp))
5193 			break;
5194 
5195 		cap = le16_to_cpu(mgmt->u.action.addba_resp.capab);
5196 		timeout = le16_to_cpu(mgmt->u.action.addba_resp.timeout);
5197 		dev_info(dev, "WLAN_ACTION_ADDBA_RESP: "
5198 			 "timeout %i, tid %02x, buf_size %02x, policy %02x, "
5199 			 "status %02x\n",
5200 			 timeout,
5201 			 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
5202 			 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
5203 			 (cap >> 1) & 0x1,
5204 			 le16_to_cpu(mgmt->u.action.addba_resp.status));
5205 		break;
5206 	case WLAN_ACTION_ADDBA_REQ:
5207 		if (skb_len < IEEE80211_MIN_ACTION_SIZE(addba_req))
5208 			break;
5209 
5210 		cap = le16_to_cpu(mgmt->u.action.addba_req.capab);
5211 		timeout = le16_to_cpu(mgmt->u.action.addba_req.timeout);
5212 		dev_info(dev, "WLAN_ACTION_ADDBA_REQ: "
5213 			 "timeout %i, tid %02x, buf_size %02x, policy %02x\n",
5214 			 timeout,
5215 			 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
5216 			 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
5217 			 (cap >> 1) & 0x1);
5218 		break;
5219 	default:
5220 		dev_info(dev, "action frame %02x\n",
5221 			 mgmt->u.action.action_code);
5222 		break;
5223 	}
5224 }
5225 
5226 /*
5227  * Fill in v1 (gen1) specific TX descriptor bits.
5228  * This format is used on 8188cu/8192cu/8723au
5229  */
5230 void
5231 rtl8xxxu_fill_txdesc_v1(struct ieee80211_hw *hw, struct ieee80211_hdr *hdr,
5232 			struct ieee80211_tx_info *tx_info,
5233 			struct rtl8xxxu_txdesc32 *tx_desc, bool sgi,
5234 			bool short_preamble, bool ampdu_enable, u32 rts_rate,
5235 			u8 macid)
5236 {
5237 	struct rtl8xxxu_priv *priv = hw->priv;
5238 	struct device *dev = &priv->udev->dev;
5239 	u8 *qc = ieee80211_get_qos_ctl(hdr);
5240 	u8 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
5241 	u32 rate = 0;
5242 	u16 seq_number;
5243 
5244 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
5245 		dev_info(dev, "%s: TX rate: %d, pkt size %u\n",
5246 			 __func__, rate, le16_to_cpu(tx_desc->pkt_size));
5247 
5248 	seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
5249 
5250 	tx_desc->txdw5 = cpu_to_le32(rate);
5251 
5252 	if (ieee80211_is_data(hdr->frame_control))
5253 		tx_desc->txdw5 |= cpu_to_le32(0x0001ff00);
5254 
5255 	tx_desc->txdw3 = cpu_to_le32((u32)seq_number << TXDESC32_SEQ_SHIFT);
5256 
5257 	if (ampdu_enable && test_bit(tid, priv->tid_tx_operational))
5258 		tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_ENABLE);
5259 	else
5260 		tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_BREAK);
5261 
5262 	if (ieee80211_is_mgmt(hdr->frame_control)) {
5263 		tx_desc->txdw5 = cpu_to_le32(rate);
5264 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
5265 		tx_desc->txdw5 |= cpu_to_le32(6 << TXDESC32_RETRY_LIMIT_SHIFT);
5266 		tx_desc->txdw5 |= cpu_to_le32(TXDESC32_RETRY_LIMIT_ENABLE);
5267 	}
5268 
5269 	if (ieee80211_is_data_qos(hdr->frame_control)) {
5270 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_QOS);
5271 
5272 		if (conf_is_ht40(&hw->conf)) {
5273 			tx_desc->txdw4 |= cpu_to_le32(TXDESC_DATA_BW);
5274 
5275 			if (conf_is_ht40_minus(&hw->conf))
5276 				tx_desc->txdw4 |= cpu_to_le32(TXDESC_PRIME_CH_OFF_UPPER);
5277 			else
5278 				tx_desc->txdw4 |= cpu_to_le32(TXDESC_PRIME_CH_OFF_LOWER);
5279 		}
5280 	}
5281 
5282 	if (short_preamble)
5283 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_SHORT_PREAMBLE);
5284 
5285 	if (sgi)
5286 		tx_desc->txdw5 |= cpu_to_le32(TXDESC32_SHORT_GI);
5287 
5288 	/*
5289 	 * rts_rate is zero if RTS/CTS or CTS to SELF are not enabled
5290 	 */
5291 	tx_desc->txdw4 |= cpu_to_le32(rts_rate << TXDESC32_RTS_RATE_SHIFT);
5292 	if (ampdu_enable || tx_info->control.use_rts) {
5293 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_RTS_CTS_ENABLE);
5294 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
5295 	} else if (tx_info->control.use_cts_prot) {
5296 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_CTS_SELF_ENABLE);
5297 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
5298 	}
5299 }
5300 
5301 /*
5302  * Fill in v2 (gen2) specific TX descriptor bits.
5303  * This format is used on 8192eu/8723bu
5304  */
5305 void
5306 rtl8xxxu_fill_txdesc_v2(struct ieee80211_hw *hw, struct ieee80211_hdr *hdr,
5307 			struct ieee80211_tx_info *tx_info,
5308 			struct rtl8xxxu_txdesc32 *tx_desc32, bool sgi,
5309 			bool short_preamble, bool ampdu_enable, u32 rts_rate,
5310 			u8 macid)
5311 {
5312 	struct rtl8xxxu_priv *priv = hw->priv;
5313 	struct device *dev = &priv->udev->dev;
5314 	struct rtl8xxxu_txdesc40 *tx_desc40;
5315 	u8 *qc = ieee80211_get_qos_ctl(hdr);
5316 	u8 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
5317 	u32 rate = 0;
5318 	u16 seq_number;
5319 
5320 	tx_desc40 = (struct rtl8xxxu_txdesc40 *)tx_desc32;
5321 
5322 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
5323 		dev_info(dev, "%s: TX rate: %d, pkt size %u\n",
5324 			 __func__, rate, le16_to_cpu(tx_desc40->pkt_size));
5325 
5326 	tx_desc40->txdw1 |= cpu_to_le32(macid << TXDESC40_MACID_SHIFT);
5327 
5328 	seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
5329 
5330 	tx_desc40->txdw4 = cpu_to_le32(rate);
5331 	if (ieee80211_is_data(hdr->frame_control)) {
5332 		tx_desc40->txdw4 |= cpu_to_le32(0x1f <<
5333 						TXDESC40_DATA_RATE_FB_SHIFT);
5334 	}
5335 
5336 	tx_desc40->txdw9 = cpu_to_le32((u32)seq_number << TXDESC40_SEQ_SHIFT);
5337 
5338 	if (ampdu_enable && test_bit(tid, priv->tid_tx_operational))
5339 		tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_ENABLE);
5340 	else
5341 		tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_BREAK);
5342 
5343 	if (ieee80211_is_mgmt(hdr->frame_control)) {
5344 		tx_desc40->txdw4 = cpu_to_le32(rate);
5345 		tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_USE_DRIVER_RATE);
5346 		tx_desc40->txdw4 |=
5347 			cpu_to_le32(6 << TXDESC40_RETRY_LIMIT_SHIFT);
5348 		tx_desc40->txdw4 |= cpu_to_le32(TXDESC40_RETRY_LIMIT_ENABLE);
5349 	}
5350 
5351 	if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
5352 		tx_desc40->txdw8 |= cpu_to_le32(TXDESC40_HW_SEQ_ENABLE);
5353 
5354 	if (short_preamble)
5355 		tx_desc40->txdw5 |= cpu_to_le32(TXDESC40_SHORT_PREAMBLE);
5356 
5357 	tx_desc40->txdw4 |= cpu_to_le32(rts_rate << TXDESC40_RTS_RATE_SHIFT);
5358 
5359 	/*
5360 	 * rts_rate is zero if RTS/CTS or CTS to SELF are not enabled
5361 	 */
5362 	if (ampdu_enable || tx_info->control.use_rts) {
5363 		tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_RTS_CTS_ENABLE);
5364 		tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_HW_RTS_ENABLE);
5365 	} else if (tx_info->control.use_cts_prot) {
5366 		/*
5367 		 * For some reason the vendor driver doesn't set
5368 		 * TXDESC40_HW_RTS_ENABLE for CTS to SELF
5369 		 */
5370 		tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_CTS_SELF_ENABLE);
5371 	}
5372 }
5373 
5374 /*
5375  * Fill in v3 (gen1) specific TX descriptor bits.
5376  * This format is a hybrid between the v1 and v2 formats, only seen
5377  * on 8188eu devices so far.
5378  */
5379 void
5380 rtl8xxxu_fill_txdesc_v3(struct ieee80211_hw *hw, struct ieee80211_hdr *hdr,
5381 			struct ieee80211_tx_info *tx_info,
5382 			struct rtl8xxxu_txdesc32 *tx_desc, bool sgi,
5383 			bool short_preamble, bool ampdu_enable, u32 rts_rate,
5384 			u8 macid)
5385 {
5386 	struct rtl8xxxu_priv *priv = hw->priv;
5387 	struct device *dev = &priv->udev->dev;
5388 	struct rtl8xxxu_ra_info *ra = &priv->ra_info;
5389 	u8 *qc = ieee80211_get_qos_ctl(hdr);
5390 	u8 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
5391 	u32 rate = 0;
5392 	u16 seq_number;
5393 
5394 	seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
5395 
5396 	if (ieee80211_is_data(hdr->frame_control)) {
5397 		rate = ra->decision_rate;
5398 		tx_desc->txdw5 = cpu_to_le32(rate);
5399 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
5400 		tx_desc->txdw4 |= le32_encode_bits(ra->pt_stage, TXDESC32_PT_STAGE_MASK);
5401 		/* Data/RTS rate FB limit */
5402 		tx_desc->txdw5 |= cpu_to_le32(0x0001ff00);
5403 	}
5404 
5405 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
5406 		dev_info(dev, "%s: TX rate: %d, pkt size %d\n",
5407 			 __func__, rate, le16_to_cpu(tx_desc->pkt_size));
5408 
5409 	tx_desc->txdw3 = cpu_to_le32((u32)seq_number << TXDESC32_SEQ_SHIFT);
5410 
5411 	if (ampdu_enable && test_bit(tid, priv->tid_tx_operational))
5412 		tx_desc->txdw2 |= cpu_to_le32(TXDESC40_AGG_ENABLE);
5413 	else
5414 		tx_desc->txdw2 |= cpu_to_le32(TXDESC40_AGG_BREAK);
5415 
5416 	if (ieee80211_is_mgmt(hdr->frame_control)) {
5417 		tx_desc->txdw5 = cpu_to_le32(rate);
5418 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
5419 		tx_desc->txdw5 |= cpu_to_le32(6 << TXDESC32_RETRY_LIMIT_SHIFT);
5420 		tx_desc->txdw5 |= cpu_to_le32(TXDESC32_RETRY_LIMIT_ENABLE);
5421 	}
5422 
5423 	if (ieee80211_is_data_qos(hdr->frame_control)) {
5424 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_QOS);
5425 
5426 		if (conf_is_ht40(&hw->conf)) {
5427 			tx_desc->txdw4 |= cpu_to_le32(TXDESC_DATA_BW);
5428 
5429 			if (conf_is_ht40_minus(&hw->conf))
5430 				tx_desc->txdw4 |= cpu_to_le32(TXDESC_PRIME_CH_OFF_UPPER);
5431 			else
5432 				tx_desc->txdw4 |= cpu_to_le32(TXDESC_PRIME_CH_OFF_LOWER);
5433 		}
5434 	}
5435 
5436 	if (short_preamble)
5437 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_SHORT_PREAMBLE);
5438 
5439 	if (sgi && ra->rate_sgi)
5440 		tx_desc->txdw5 |= cpu_to_le32(TXDESC32_SHORT_GI);
5441 
5442 	/*
5443 	 * rts_rate is zero if RTS/CTS or CTS to SELF are not enabled
5444 	 */
5445 	tx_desc->txdw4 |= cpu_to_le32(rts_rate << TXDESC32_RTS_RATE_SHIFT);
5446 	if (ampdu_enable || tx_info->control.use_rts) {
5447 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_RTS_CTS_ENABLE);
5448 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
5449 	} else if (tx_info->control.use_cts_prot) {
5450 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_CTS_SELF_ENABLE);
5451 		tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
5452 	}
5453 
5454 	tx_desc->txdw2 |= cpu_to_le32(TXDESC_ANTENNA_SELECT_A |
5455 				      TXDESC_ANTENNA_SELECT_B);
5456 	tx_desc->txdw7 |= cpu_to_le16(TXDESC_ANTENNA_SELECT_C >> 16);
5457 }
5458 
5459 static void rtl8xxxu_tx(struct ieee80211_hw *hw,
5460 			struct ieee80211_tx_control *control,
5461 			struct sk_buff *skb)
5462 {
5463 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5464 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
5465 	struct rtl8xxxu_priv *priv = hw->priv;
5466 	struct rtl8xxxu_txdesc32 *tx_desc;
5467 	struct rtl8xxxu_tx_urb *tx_urb;
5468 	struct ieee80211_sta *sta = NULL;
5469 	struct ieee80211_vif *vif = tx_info->control.vif;
5470 	struct rtl8xxxu_vif *rtlvif = vif ? (struct rtl8xxxu_vif *)vif->drv_priv : NULL;
5471 	struct device *dev = &priv->udev->dev;
5472 	u32 queue, rts_rate;
5473 	u16 pktlen = skb->len;
5474 	int tx_desc_size = priv->fops->tx_desc_size;
5475 	u8 macid;
5476 	int ret;
5477 	bool ampdu_enable, sgi = false, short_preamble = false, bmc = false;
5478 
5479 	if (skb_headroom(skb) < tx_desc_size) {
5480 		dev_warn(dev,
5481 			 "%s: Not enough headroom (%i) for tx descriptor\n",
5482 			 __func__, skb_headroom(skb));
5483 		goto error;
5484 	}
5485 
5486 	if (unlikely(skb->len > (65535 - tx_desc_size))) {
5487 		dev_warn(dev, "%s: Trying to send over-sized skb (%i)\n",
5488 			 __func__, skb->len);
5489 		goto error;
5490 	}
5491 
5492 	tx_urb = rtl8xxxu_alloc_tx_urb(priv);
5493 	if (!tx_urb) {
5494 		dev_warn(dev, "%s: Unable to allocate tx urb\n", __func__);
5495 		goto error;
5496 	}
5497 
5498 	if (ieee80211_is_action(hdr->frame_control))
5499 		rtl8xxxu_dump_action(dev, hdr, skb->len);
5500 
5501 	tx_info->rate_driver_data[0] = hw;
5502 
5503 	if (control && control->sta)
5504 		sta = control->sta;
5505 
5506 	queue = rtl8xxxu_queue_select(hdr, skb);
5507 
5508 	tx_desc = skb_push(skb, tx_desc_size);
5509 
5510 	memset(tx_desc, 0, tx_desc_size);
5511 	tx_desc->pkt_size = cpu_to_le16(pktlen);
5512 	tx_desc->pkt_offset = tx_desc_size;
5513 
5514 	/* These bits mean different things to the RTL8192F. */
5515 	if (priv->rtl_chip != RTL8192F)
5516 		tx_desc->txdw0 =
5517 			TXDESC_OWN | TXDESC_FIRST_SEGMENT | TXDESC_LAST_SEGMENT;
5518 	if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
5519 	    is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
5520 		tx_desc->txdw0 |= TXDESC_BROADMULTICAST;
5521 		bmc = true;
5522 	}
5523 
5524 
5525 	tx_desc->txdw1 = cpu_to_le32(queue << TXDESC_QUEUE_SHIFT);
5526 	macid = rtl8xxxu_get_macid(priv, sta);
5527 
5528 	if (tx_info->control.hw_key) {
5529 		switch (tx_info->control.hw_key->cipher) {
5530 		case WLAN_CIPHER_SUITE_WEP40:
5531 		case WLAN_CIPHER_SUITE_WEP104:
5532 		case WLAN_CIPHER_SUITE_TKIP:
5533 			tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_RC4);
5534 			break;
5535 		case WLAN_CIPHER_SUITE_CCMP:
5536 			tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_AES);
5537 			break;
5538 		default:
5539 			break;
5540 		}
5541 		if (bmc && rtlvif && rtlvif->hw_key_idx != 0xff) {
5542 			tx_desc->txdw1 |= cpu_to_le32(TXDESC_EN_DESC_ID);
5543 			macid = rtlvif->hw_key_idx;
5544 		}
5545 	}
5546 
5547 	/* (tx_info->flags & IEEE80211_TX_CTL_AMPDU) && */
5548 	ampdu_enable = false;
5549 	if (ieee80211_is_data_qos(hdr->frame_control) && sta) {
5550 		if (sta->deflink.ht_cap.ht_supported) {
5551 			u32 ampdu, val32;
5552 			u8 *qc = ieee80211_get_qos_ctl(hdr);
5553 			u8 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
5554 
5555 			ampdu = (u32)sta->deflink.ht_cap.ampdu_density;
5556 			val32 = ampdu << TXDESC_AMPDU_DENSITY_SHIFT;
5557 			tx_desc->txdw2 |= cpu_to_le32(val32);
5558 
5559 			ampdu_enable = true;
5560 
5561 			if (!test_bit(tid, priv->tx_aggr_started) &&
5562 			    !(skb->protocol == cpu_to_be16(ETH_P_PAE)))
5563 				if (!ieee80211_start_tx_ba_session(sta, tid, 0))
5564 					set_bit(tid, priv->tx_aggr_started);
5565 		}
5566 	}
5567 
5568 	if (ieee80211_is_data_qos(hdr->frame_control) &&
5569 	    sta && sta->deflink.ht_cap.cap &
5570 	    (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
5571 		sgi = true;
5572 
5573 	if (sta && vif && vif->bss_conf.use_short_preamble)
5574 		short_preamble = true;
5575 
5576 	if (skb->len > hw->wiphy->rts_threshold)
5577 		tx_info->control.use_rts = true;
5578 
5579 	if (sta && vif && vif->bss_conf.use_cts_prot)
5580 		tx_info->control.use_cts_prot = true;
5581 
5582 	if (ampdu_enable || tx_info->control.use_rts ||
5583 	    tx_info->control.use_cts_prot)
5584 		rts_rate = DESC_RATE_24M;
5585 	else
5586 		rts_rate = 0;
5587 
5588 	priv->fops->fill_txdesc(hw, hdr, tx_info, tx_desc, sgi, short_preamble,
5589 				ampdu_enable, rts_rate, macid);
5590 
5591 	rtl8xxxu_calc_tx_desc_csum(tx_desc);
5592 
5593 	/* avoid zero checksum make tx hang */
5594 	if (priv->rtl_chip == RTL8710B || priv->rtl_chip == RTL8192F)
5595 		tx_desc->csum = ~tx_desc->csum;
5596 
5597 	usb_fill_bulk_urb(&tx_urb->urb, priv->udev, priv->pipe_out[queue],
5598 			  skb->data, skb->len, rtl8xxxu_tx_complete, skb);
5599 
5600 	usb_anchor_urb(&tx_urb->urb, &priv->tx_anchor);
5601 	ret = usb_submit_urb(&tx_urb->urb, GFP_ATOMIC);
5602 	if (ret) {
5603 		usb_unanchor_urb(&tx_urb->urb);
5604 		rtl8xxxu_free_tx_urb(priv, tx_urb);
5605 		goto error;
5606 	}
5607 	return;
5608 error:
5609 	dev_kfree_skb(skb);
5610 }
5611 
5612 static void rtl8xxxu_send_beacon_frame(struct ieee80211_hw *hw,
5613 				       struct ieee80211_vif *vif)
5614 {
5615 	struct rtl8xxxu_priv *priv = hw->priv;
5616 	struct sk_buff *skb = ieee80211_beacon_get(hw, vif, 0);
5617 	struct device *dev = &priv->udev->dev;
5618 	int retry;
5619 	u8 val8;
5620 
5621 	/* BCN_VALID, write 1 to clear, cleared by SW */
5622 	val8 = rtl8xxxu_read8(priv, REG_TDECTRL + 2);
5623 	val8 |= BIT_BCN_VALID >> 16;
5624 	rtl8xxxu_write8(priv, REG_TDECTRL + 2, val8);
5625 
5626 	/* SW_BCN_SEL - Port0 */
5627 	val8 = rtl8xxxu_read8(priv, REG_DWBCN1_CTRL_8723B + 2);
5628 	val8 &= ~(BIT_SW_BCN_SEL >> 16);
5629 	rtl8xxxu_write8(priv, REG_DWBCN1_CTRL_8723B + 2, val8);
5630 
5631 	if (skb)
5632 		rtl8xxxu_tx(hw, NULL, skb);
5633 
5634 	retry = 100;
5635 	do {
5636 		val8 = rtl8xxxu_read8(priv, REG_TDECTRL + 2);
5637 		if (val8 & (BIT_BCN_VALID >> 16))
5638 			break;
5639 		usleep_range(10, 20);
5640 	} while (--retry);
5641 
5642 	if (!retry)
5643 		dev_err(dev, "%s: Failed to read beacon valid bit\n", __func__);
5644 }
5645 
5646 static void rtl8xxxu_update_beacon_work_callback(struct work_struct *work)
5647 {
5648 	struct rtl8xxxu_priv *priv =
5649 		container_of(work, struct rtl8xxxu_priv, update_beacon_work.work);
5650 	struct ieee80211_hw *hw = priv->hw;
5651 	struct ieee80211_vif *vif = priv->vifs[0];
5652 
5653 	if (!vif) {
5654 		WARN_ONCE(true, "no vif to update beacon\n");
5655 		return;
5656 	}
5657 
5658 	if (vif->bss_conf.csa_active) {
5659 		if (ieee80211_beacon_cntdwn_is_complete(vif, 0)) {
5660 			ieee80211_csa_finish(vif, 0);
5661 			return;
5662 		}
5663 		schedule_delayed_work(&priv->update_beacon_work,
5664 				      msecs_to_jiffies(vif->bss_conf.beacon_int));
5665 	}
5666 	rtl8xxxu_send_beacon_frame(hw, vif);
5667 }
5668 
5669 static inline bool rtl8xxxu_is_packet_match_bssid(struct rtl8xxxu_priv *priv,
5670 						  struct ieee80211_hdr *hdr,
5671 						  int port_num)
5672 {
5673 	return priv->vifs[port_num] &&
5674 	       priv->vifs[port_num]->type == NL80211_IFTYPE_STATION &&
5675 	       priv->vifs[port_num]->cfg.assoc &&
5676 	       ether_addr_equal(priv->vifs[port_num]->bss_conf.bssid, hdr->addr2);
5677 }
5678 
5679 static inline bool rtl8xxxu_is_sta_sta(struct rtl8xxxu_priv *priv)
5680 {
5681 	return (priv->vifs[0] && priv->vifs[0]->cfg.assoc &&
5682 		priv->vifs[0]->type == NL80211_IFTYPE_STATION) &&
5683 	       (priv->vifs[1] && priv->vifs[1]->cfg.assoc &&
5684 		priv->vifs[1]->type == NL80211_IFTYPE_STATION);
5685 }
5686 
5687 void rtl8723au_rx_parse_phystats(struct rtl8xxxu_priv *priv,
5688 				 struct ieee80211_rx_status *rx_status,
5689 				 struct rtl8723au_phy_stats *phy_stats,
5690 				 u32 rxmcs, struct ieee80211_hdr *hdr,
5691 				 bool crc_icv_err)
5692 {
5693 	if (phy_stats->sgi_en)
5694 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
5695 
5696 	if (rxmcs < DESC_RATE_6M) {
5697 		/*
5698 		 * Handle PHY stats for CCK rates
5699 		 */
5700 		rx_status->signal = priv->fops->cck_rssi(priv, phy_stats);
5701 	} else {
5702 		bool parse_cfo = priv->fops->set_crystal_cap &&
5703 				 !crc_icv_err &&
5704 				 !ieee80211_is_ctl(hdr->frame_control) &&
5705 				 !rtl8xxxu_is_sta_sta(priv) &&
5706 				 (rtl8xxxu_is_packet_match_bssid(priv, hdr, 0) ||
5707 				  rtl8xxxu_is_packet_match_bssid(priv, hdr, 1));
5708 
5709 		if (parse_cfo) {
5710 			priv->cfo_tracking.cfo_tail[0] = phy_stats->path_cfotail[0];
5711 			priv->cfo_tracking.cfo_tail[1] = phy_stats->path_cfotail[1];
5712 
5713 			priv->cfo_tracking.packet_count++;
5714 		}
5715 
5716 		rx_status->signal =
5717 			(phy_stats->cck_sig_qual_ofdm_pwdb_all >> 1) - 110;
5718 	}
5719 }
5720 
5721 static void jaguar2_rx_parse_phystats_type0(struct rtl8xxxu_priv *priv,
5722 					    struct ieee80211_rx_status *rx_status,
5723 					    struct jaguar2_phy_stats_type0 *phy_stats0,
5724 					    u32 rxmcs, struct ieee80211_hdr *hdr,
5725 					    bool crc_icv_err)
5726 {
5727 	s8 rx_power = phy_stats0->pwdb - 110;
5728 
5729 	if (!priv->cck_new_agc)
5730 		rx_power = priv->fops->cck_rssi(priv, (struct rtl8723au_phy_stats *)phy_stats0);
5731 
5732 	rx_status->signal = rx_power;
5733 }
5734 
5735 static void jaguar2_rx_parse_phystats_type1(struct rtl8xxxu_priv *priv,
5736 					    struct ieee80211_rx_status *rx_status,
5737 					    struct jaguar2_phy_stats_type1 *phy_stats1,
5738 					    u32 rxmcs, struct ieee80211_hdr *hdr,
5739 					    bool crc_icv_err)
5740 {
5741 	bool parse_cfo = priv->fops->set_crystal_cap &&
5742 			 !crc_icv_err &&
5743 			 !ieee80211_is_ctl(hdr->frame_control) &&
5744 			 !rtl8xxxu_is_sta_sta(priv) &&
5745 			 (rtl8xxxu_is_packet_match_bssid(priv, hdr, 0) ||
5746 			  rtl8xxxu_is_packet_match_bssid(priv, hdr, 1));
5747 	u8 pwdb_max = 0, rxsc;
5748 	int rx_path;
5749 
5750 	if (parse_cfo) {
5751 		/* Only path-A and path-B have CFO tail and short CFO */
5752 		priv->cfo_tracking.cfo_tail[RF_A] = phy_stats1->cfo_tail[RF_A];
5753 		priv->cfo_tracking.cfo_tail[RF_B] = phy_stats1->cfo_tail[RF_B];
5754 
5755 		priv->cfo_tracking.packet_count++;
5756 	}
5757 
5758 	for (rx_path = 0; rx_path < priv->rx_paths; rx_path++)
5759 		pwdb_max = max(pwdb_max, phy_stats1->pwdb[rx_path]);
5760 
5761 	rx_status->signal = pwdb_max - 110;
5762 
5763 	if (rxmcs >= DESC_RATE_6M && rxmcs <= DESC_RATE_54M)
5764 		rxsc = phy_stats1->l_rxsc;
5765 	else
5766 		rxsc = phy_stats1->ht_rxsc;
5767 
5768 	if (phy_stats1->rf_mode == 0 || rxsc == 1 || rxsc == 2)
5769 		rx_status->bw = RATE_INFO_BW_20;
5770 	else
5771 		rx_status->bw = RATE_INFO_BW_40;
5772 }
5773 
5774 static void jaguar2_rx_parse_phystats_type2(struct rtl8xxxu_priv *priv,
5775 					    struct ieee80211_rx_status *rx_status,
5776 					    struct jaguar2_phy_stats_type2 *phy_stats2,
5777 					    u32 rxmcs, struct ieee80211_hdr *hdr,
5778 					    bool crc_icv_err)
5779 {
5780 	u8 pwdb_max = 0;
5781 	int rx_path;
5782 
5783 	for (rx_path = 0; rx_path < priv->rx_paths; rx_path++)
5784 		pwdb_max = max(pwdb_max, phy_stats2->pwdb[rx_path]);
5785 
5786 	rx_status->signal = pwdb_max - 110;
5787 }
5788 
5789 void jaguar2_rx_parse_phystats(struct rtl8xxxu_priv *priv,
5790 			       struct ieee80211_rx_status *rx_status,
5791 			       struct rtl8723au_phy_stats *phy_stats,
5792 			       u32 rxmcs, struct ieee80211_hdr *hdr,
5793 			       bool crc_icv_err)
5794 {
5795 	struct jaguar2_phy_stats_type0 *phy_stats0 = (struct jaguar2_phy_stats_type0 *)phy_stats;
5796 	struct jaguar2_phy_stats_type1 *phy_stats1 = (struct jaguar2_phy_stats_type1 *)phy_stats;
5797 	struct jaguar2_phy_stats_type2 *phy_stats2 = (struct jaguar2_phy_stats_type2 *)phy_stats;
5798 
5799 	switch (phy_stats0->page_num) {
5800 	case 0:
5801 		/* CCK */
5802 		jaguar2_rx_parse_phystats_type0(priv, rx_status, phy_stats0,
5803 						rxmcs, hdr, crc_icv_err);
5804 		break;
5805 	case 1:
5806 		/* OFDM */
5807 		jaguar2_rx_parse_phystats_type1(priv, rx_status, phy_stats1,
5808 						rxmcs, hdr, crc_icv_err);
5809 		break;
5810 	case 2:
5811 		/* Also OFDM but different (how?) */
5812 		jaguar2_rx_parse_phystats_type2(priv, rx_status, phy_stats2,
5813 						rxmcs, hdr, crc_icv_err);
5814 		break;
5815 	default:
5816 		return;
5817 	}
5818 }
5819 
5820 static void rtl8xxxu_free_rx_resources(struct rtl8xxxu_priv *priv)
5821 {
5822 	struct rtl8xxxu_rx_urb *rx_urb, *tmp;
5823 	unsigned long flags;
5824 
5825 	spin_lock_irqsave(&priv->rx_urb_lock, flags);
5826 
5827 	list_for_each_entry_safe(rx_urb, tmp,
5828 				 &priv->rx_urb_pending_list, list) {
5829 		list_del(&rx_urb->list);
5830 		priv->rx_urb_pending_count--;
5831 		usb_free_urb(&rx_urb->urb);
5832 	}
5833 
5834 	spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5835 }
5836 
5837 static int rtl8xxxu_alloc_rx_urbs(struct rtl8xxxu_priv *priv)
5838 {
5839 	struct rtl8xxxu_rx_urb *rx_urb;
5840 	int i;
5841 
5842 	/* No RX work is active until the complete pool has been allocated. */
5843 	for (i = 0; i < RTL8XXXU_RX_URBS; i++) {
5844 		rx_urb = kmalloc_obj(struct rtl8xxxu_rx_urb);
5845 		if (!rx_urb)
5846 			return -ENOMEM;
5847 
5848 		usb_init_urb(&rx_urb->urb);
5849 		INIT_LIST_HEAD(&rx_urb->list);
5850 		rx_urb->hw = priv->hw;
5851 
5852 		list_add_tail(&rx_urb->list, &priv->rx_urb_pending_list);
5853 		priv->rx_urb_pending_count++;
5854 	}
5855 
5856 	return 0;
5857 }
5858 
5859 static void rtl8xxxu_queue_rx_urb(struct rtl8xxxu_priv *priv,
5860 				  struct rtl8xxxu_rx_urb *rx_urb, bool defer_schedule)
5861 {
5862 	unsigned long flags;
5863 
5864 	spin_lock_irqsave(&priv->rx_urb_lock, flags);
5865 
5866 	if (!priv->shutdown) {
5867 		list_add_tail(&rx_urb->list, &priv->rx_urb_pending_list);
5868 		priv->rx_urb_pending_count++;
5869 		/* Serialize scheduling with the shutdown check and cancellation. */
5870 		if (defer_schedule)
5871 			schedule_delayed_work(&priv->rx_urb_wq,
5872 					      msecs_to_jiffies(RTL8XXXU_RX_URB_RETRY_DELAY_MS));
5873 		else if (priv->rx_urb_pending_count > RTL8XXXU_RX_URB_PENDING_WATER)
5874 			mod_delayed_work(system_percpu_wq, &priv->rx_urb_wq, 0);
5875 	} else {
5876 		usb_free_urb(&rx_urb->urb);
5877 	}
5878 
5879 	spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5880 }
5881 
5882 static int rtl8xxxu_submit_rx_urbs(struct rtl8xxxu_priv *priv, bool startup)
5883 {
5884 	struct rtl8xxxu_rx_urb *rx_urb, *tmp;
5885 	unsigned long flags;
5886 	LIST_HEAD(local);
5887 	int ret;
5888 
5889 	spin_lock_irqsave(&priv->rx_urb_lock, flags);
5890 	list_splice_init(&priv->rx_urb_pending_list, &local);
5891 	priv->rx_urb_pending_count = 0;
5892 	spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5893 
5894 	list_for_each_entry_safe(rx_urb, tmp, &local, list) {
5895 		list_del_init(&rx_urb->list);
5896 		ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
5897 		switch (ret) {
5898 		case 0:
5899 			break;
5900 		case -ENOMEM:
5901 		case -EAGAIN:
5902 			rtl8xxxu_queue_rx_urb(priv, rx_urb, true);
5903 			break;
5904 		default:
5905 			usb_free_urb(&rx_urb->urb);
5906 			if (startup)
5907 				goto free_remaining;
5908 			dev_warn(&priv->udev->dev,
5909 				 "failed to requeue urb with error %i\n", ret);
5910 		}
5911 	}
5912 
5913 	return 0;
5914 
5915 free_remaining:
5916 	list_for_each_entry_safe(rx_urb, tmp, &local, list) {
5917 		list_del(&rx_urb->list);
5918 		usb_free_urb(&rx_urb->urb);
5919 	}
5920 	return ret;
5921 }
5922 
5923 static void rtl8xxxu_rx_urb_work(struct work_struct *work)
5924 {
5925 	struct rtl8xxxu_priv *priv = container_of(to_delayed_work(work),
5926 					       struct rtl8xxxu_priv, rx_urb_wq);
5927 
5928 	rtl8xxxu_submit_rx_urbs(priv, false);
5929 }
5930 
5931 /*
5932  * The RTL8723BU/RTL8192EU vendor driver use coexistence table type
5933  * 0-7 to represent writing different combinations of register values
5934  * to REG_BT_COEX_TABLEs. It's for different kinds of coexistence use
5935  * cases which Realtek doesn't provide detail for these settings. Keep
5936  * this aligned with vendor driver for easier maintenance.
5937  */
5938 static
5939 void rtl8723bu_set_coex_with_type(struct rtl8xxxu_priv *priv, u8 type)
5940 {
5941 	switch (type) {
5942 	case 0:
5943 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x55555555);
5944 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0x55555555);
5945 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5946 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5947 		break;
5948 	case 1:
5949 	case 3:
5950 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x55555555);
5951 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0x5a5a5a5a);
5952 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5953 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5954 		break;
5955 	case 2:
5956 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x5a5a5a5a);
5957 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0x5a5a5a5a);
5958 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5959 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5960 		break;
5961 	case 4:
5962 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x5a5a5a5a);
5963 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0xaaaa5a5a);
5964 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5965 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5966 		break;
5967 	case 5:
5968 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x5a5a5a5a);
5969 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0xaa5a5a5a);
5970 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5971 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5972 		break;
5973 	case 6:
5974 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0x55555555);
5975 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0xaaaaaaaa);
5976 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5977 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5978 		break;
5979 	case 7:
5980 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE1, 0xaaaaaaaa);
5981 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE2, 0xaaaaaaaa);
5982 		rtl8xxxu_write32(priv, REG_BT_COEX_TABLE3, 0x00ffffff);
5983 		rtl8xxxu_write8(priv, REG_BT_COEX_TABLE4, 0x03);
5984 		break;
5985 	default:
5986 		break;
5987 	}
5988 }
5989 
5990 static
5991 void rtl8723bu_update_bt_link_info(struct rtl8xxxu_priv *priv, u8 bt_info)
5992 {
5993 	struct rtl8xxxu_btcoex *btcoex = &priv->bt_coex;
5994 
5995 	if (bt_info & BT_INFO_8723B_1ANT_B_INQ_PAGE)
5996 		btcoex->c2h_bt_inquiry = true;
5997 	else
5998 		btcoex->c2h_bt_inquiry = false;
5999 
6000 	if (!(bt_info & BT_INFO_8723B_1ANT_B_CONNECTION)) {
6001 		btcoex->bt_status = BT_8723B_1ANT_STATUS_NON_CONNECTED_IDLE;
6002 		btcoex->has_sco = false;
6003 		btcoex->has_hid = false;
6004 		btcoex->has_pan = false;
6005 		btcoex->has_a2dp = false;
6006 	} else {
6007 		if ((bt_info & 0x1f) == BT_INFO_8723B_1ANT_B_CONNECTION)
6008 			btcoex->bt_status = BT_8723B_1ANT_STATUS_CONNECTED_IDLE;
6009 		else if ((bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO) ||
6010 			 (bt_info & BT_INFO_8723B_1ANT_B_SCO_BUSY))
6011 			btcoex->bt_status = BT_8723B_1ANT_STATUS_SCO_BUSY;
6012 		else if (bt_info & BT_INFO_8723B_1ANT_B_ACL_BUSY)
6013 			btcoex->bt_status = BT_8723B_1ANT_STATUS_ACL_BUSY;
6014 		else
6015 			btcoex->bt_status = BT_8723B_1ANT_STATUS_MAX;
6016 
6017 		if (bt_info & BT_INFO_8723B_1ANT_B_FTP)
6018 			btcoex->has_pan = true;
6019 		else
6020 			btcoex->has_pan = false;
6021 
6022 		if (bt_info & BT_INFO_8723B_1ANT_B_A2DP)
6023 			btcoex->has_a2dp = true;
6024 		else
6025 			btcoex->has_a2dp = false;
6026 
6027 		if (bt_info & BT_INFO_8723B_1ANT_B_HID)
6028 			btcoex->has_hid = true;
6029 		else
6030 			btcoex->has_hid = false;
6031 
6032 		if (bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO)
6033 			btcoex->has_sco = true;
6034 		else
6035 			btcoex->has_sco = false;
6036 	}
6037 
6038 	if (!btcoex->has_a2dp && !btcoex->has_sco &&
6039 	    !btcoex->has_pan && btcoex->has_hid)
6040 		btcoex->hid_only = true;
6041 	else
6042 		btcoex->hid_only = false;
6043 
6044 	if (!btcoex->has_sco && !btcoex->has_pan &&
6045 	    !btcoex->has_hid && btcoex->has_a2dp)
6046 		btcoex->has_a2dp = true;
6047 	else
6048 		btcoex->has_a2dp = false;
6049 
6050 	if (btcoex->bt_status == BT_8723B_1ANT_STATUS_SCO_BUSY ||
6051 	    btcoex->bt_status == BT_8723B_1ANT_STATUS_ACL_BUSY)
6052 		btcoex->bt_busy = true;
6053 	else
6054 		btcoex->bt_busy = false;
6055 }
6056 
6057 static inline bool rtl8xxxu_is_assoc(struct rtl8xxxu_priv *priv)
6058 {
6059 	return (priv->vifs[0] && priv->vifs[0]->cfg.assoc) ||
6060 	       (priv->vifs[1] && priv->vifs[1]->cfg.assoc);
6061 }
6062 
6063 static
6064 void rtl8723bu_handle_bt_inquiry(struct rtl8xxxu_priv *priv)
6065 {
6066 	struct rtl8xxxu_btcoex *btcoex;
6067 
6068 	btcoex = &priv->bt_coex;
6069 
6070 	if (!rtl8xxxu_is_assoc(priv)) {
6071 		rtl8723bu_set_ps_tdma(priv, 0x8, 0x0, 0x0, 0x0, 0x0);
6072 		rtl8723bu_set_coex_with_type(priv, 0);
6073 	} else if (btcoex->has_sco || btcoex->has_hid || btcoex->has_a2dp) {
6074 		rtl8723bu_set_ps_tdma(priv, 0x61, 0x35, 0x3, 0x11, 0x11);
6075 		rtl8723bu_set_coex_with_type(priv, 4);
6076 	} else if (btcoex->has_pan) {
6077 		rtl8723bu_set_ps_tdma(priv, 0x61, 0x3f, 0x3, 0x11, 0x11);
6078 		rtl8723bu_set_coex_with_type(priv, 4);
6079 	} else {
6080 		rtl8723bu_set_ps_tdma(priv, 0x8, 0x0, 0x0, 0x0, 0x0);
6081 		rtl8723bu_set_coex_with_type(priv, 7);
6082 	}
6083 }
6084 
6085 static
6086 void rtl8723bu_handle_bt_info(struct rtl8xxxu_priv *priv)
6087 {
6088 	struct rtl8xxxu_btcoex *btcoex;
6089 
6090 	btcoex = &priv->bt_coex;
6091 
6092 	if (rtl8xxxu_is_assoc(priv)) {
6093 		u32 val32 = 0;
6094 		u32 high_prio_tx = 0, high_prio_rx = 0;
6095 
6096 		val32 = rtl8xxxu_read32(priv, 0x770);
6097 		high_prio_tx = val32 & 0x0000ffff;
6098 		high_prio_rx = (val32  & 0xffff0000) >> 16;
6099 
6100 		if (btcoex->bt_busy) {
6101 			if (btcoex->hid_only) {
6102 				rtl8723bu_set_ps_tdma(priv, 0x61, 0x20,
6103 						      0x3, 0x11, 0x11);
6104 				rtl8723bu_set_coex_with_type(priv, 5);
6105 			} else if (btcoex->a2dp_only) {
6106 				rtl8723bu_set_ps_tdma(priv, 0x61, 0x35,
6107 						      0x3, 0x11, 0x11);
6108 				rtl8723bu_set_coex_with_type(priv, 4);
6109 			} else if ((btcoex->has_a2dp && btcoex->has_pan) ||
6110 				   (btcoex->has_hid && btcoex->has_a2dp &&
6111 				    btcoex->has_pan)) {
6112 				rtl8723bu_set_ps_tdma(priv, 0x51, 0x21,
6113 						      0x3, 0x10, 0x10);
6114 				rtl8723bu_set_coex_with_type(priv, 4);
6115 			} else if (btcoex->has_hid && btcoex->has_a2dp) {
6116 				rtl8723bu_set_ps_tdma(priv, 0x51, 0x21,
6117 						      0x3, 0x10, 0x10);
6118 				rtl8723bu_set_coex_with_type(priv, 3);
6119 			} else {
6120 				rtl8723bu_set_ps_tdma(priv, 0x61, 0x35,
6121 						      0x3, 0x11, 0x11);
6122 				rtl8723bu_set_coex_with_type(priv, 4);
6123 			}
6124 		} else {
6125 			rtl8723bu_set_ps_tdma(priv, 0x8, 0x0, 0x0, 0x0, 0x0);
6126 			if (high_prio_tx + high_prio_rx <= 60)
6127 				rtl8723bu_set_coex_with_type(priv, 2);
6128 			else
6129 				rtl8723bu_set_coex_with_type(priv, 7);
6130 		}
6131 	} else {
6132 		rtl8723bu_set_ps_tdma(priv, 0x8, 0x0, 0x0, 0x0, 0x0);
6133 		rtl8723bu_set_coex_with_type(priv, 0);
6134 	}
6135 }
6136 
6137 static void rtl8xxxu_c2hcmd_callback(struct work_struct *work)
6138 {
6139 	struct rtl8xxxu_priv *priv;
6140 	struct rtl8723bu_c2h *c2h;
6141 	struct sk_buff *skb = NULL;
6142 	u8 bt_info = 0;
6143 	struct rtl8xxxu_btcoex *btcoex;
6144 	struct rtl8xxxu_ra_report *rarpt;
6145 	u8 bw;
6146 
6147 	priv = container_of(work, struct rtl8xxxu_priv, c2hcmd_work);
6148 	btcoex = &priv->bt_coex;
6149 	rarpt = &priv->ra_report;
6150 
6151 	while (!skb_queue_empty(&priv->c2hcmd_queue)) {
6152 		skb = skb_dequeue(&priv->c2hcmd_queue);
6153 
6154 		c2h = (struct rtl8723bu_c2h *)skb->data;
6155 
6156 		switch (c2h->id) {
6157 		case C2H_8723B_BT_INFO:
6158 			bt_info = c2h->bt_info.bt_info;
6159 
6160 			rtl8723bu_update_bt_link_info(priv, bt_info);
6161 			if (btcoex->c2h_bt_inquiry) {
6162 				rtl8723bu_handle_bt_inquiry(priv);
6163 				break;
6164 			}
6165 			rtl8723bu_handle_bt_info(priv);
6166 			break;
6167 		case C2H_8723B_RA_REPORT:
6168 			bw = rarpt->txrate.bw;
6169 
6170 			if (skb->len >= offsetofend(typeof(*c2h), ra_report.bw)) {
6171 				if (c2h->ra_report.bw == RTL8XXXU_CHANNEL_WIDTH_40)
6172 					bw = RATE_INFO_BW_40;
6173 				else
6174 					bw = RATE_INFO_BW_20;
6175 			}
6176 
6177 			rtl8xxxu_update_ra_report(rarpt, c2h->ra_report.rate,
6178 						  c2h->ra_report.sgi, bw);
6179 			break;
6180 		default:
6181 			break;
6182 		}
6183 
6184 		dev_kfree_skb(skb);
6185 	}
6186 }
6187 
6188 static void rtl8723bu_handle_c2h(struct rtl8xxxu_priv *priv,
6189 				 struct sk_buff *skb)
6190 {
6191 	struct rtl8723bu_c2h *c2h = (struct rtl8723bu_c2h *)skb->data;
6192 	struct device *dev = &priv->udev->dev;
6193 	int len;
6194 
6195 	len = skb->len - 2;
6196 
6197 	dev_dbg(dev, "C2H ID %02x seq %02x, len %02x source %02x\n",
6198 		c2h->id, c2h->seq, len, c2h->bt_info.response_source);
6199 
6200 	switch(c2h->id) {
6201 	case C2H_8723B_BT_INFO:
6202 		if (c2h->bt_info.response_source >
6203 		    BT_INFO_SRC_8723B_BT_ACTIVE_SEND)
6204 			dev_dbg(dev, "C2H_BT_INFO WiFi only firmware\n");
6205 		else
6206 			dev_dbg(dev, "C2H_BT_INFO BT/WiFi coexist firmware\n");
6207 
6208 		if (c2h->bt_info.bt_has_reset)
6209 			dev_dbg(dev, "BT has been reset\n");
6210 		if (c2h->bt_info.tx_rx_mask)
6211 			dev_dbg(dev, "BT TRx mask\n");
6212 
6213 		break;
6214 	case C2H_8723B_BT_MP_INFO:
6215 		dev_dbg(dev, "C2H_MP_INFO ext ID %02x, status %02x\n",
6216 			c2h->bt_mp_info.ext_id, c2h->bt_mp_info.status);
6217 		break;
6218 	case C2H_8723B_RA_REPORT:
6219 		dev_dbg(dev,
6220 			"C2H RA RPT: rate %02x, unk %i, macid %02x, noise %i\n",
6221 			c2h->ra_report.rate, c2h->ra_report.sgi,
6222 			c2h->ra_report.macid, c2h->ra_report.noisy_state);
6223 		break;
6224 	default:
6225 		dev_info(dev, "Unhandled C2H event %02x seq %02x\n",
6226 			 c2h->id, c2h->seq);
6227 		print_hex_dump(KERN_INFO, "C2H content: ", DUMP_PREFIX_NONE,
6228 			       16, 1, c2h->raw.payload, len, false);
6229 		break;
6230 	}
6231 
6232 	skb_queue_tail(&priv->c2hcmd_queue, skb);
6233 
6234 	schedule_work(&priv->c2hcmd_work);
6235 }
6236 
6237 static void rtl8188e_c2hcmd_callback(struct work_struct *work)
6238 {
6239 	struct rtl8xxxu_priv *priv = container_of(work, struct rtl8xxxu_priv, c2hcmd_work);
6240 	struct device *dev = &priv->udev->dev;
6241 	struct sk_buff *skb = NULL;
6242 	struct rtl8xxxu_rxdesc16 *rx_desc;
6243 
6244 	while (!skb_queue_empty(&priv->c2hcmd_queue)) {
6245 		skb = skb_dequeue(&priv->c2hcmd_queue);
6246 
6247 		rx_desc = (struct rtl8xxxu_rxdesc16 *)(skb->data - sizeof(struct rtl8xxxu_rxdesc16));
6248 
6249 		switch (rx_desc->rpt_sel) {
6250 		case 1:
6251 			dev_dbg(dev, "C2H TX report type 1\n");
6252 
6253 			break;
6254 		case 2:
6255 			dev_dbg(dev, "C2H TX report type 2\n");
6256 
6257 			rtl8188e_handle_ra_tx_report2(priv, skb);
6258 
6259 			break;
6260 		case 3:
6261 			dev_dbg(dev, "C2H USB interrupt report\n");
6262 
6263 			break;
6264 		default:
6265 			dev_warn(dev, "%s: rpt_sel should not be %d\n",
6266 				 __func__, rx_desc->rpt_sel);
6267 
6268 			break;
6269 		}
6270 
6271 		dev_kfree_skb(skb);
6272 	}
6273 }
6274 
6275 #define rtl8xxxu_iterate_vifs_atomic(priv, iterator, data)			\
6276 	ieee80211_iterate_active_interfaces_atomic((priv)->hw,			\
6277 			IEEE80211_IFACE_ITER_NORMAL, iterator, data)
6278 
6279 struct rtl8xxxu_rx_update_rssi_data {
6280 	struct rtl8xxxu_priv *priv;
6281 	struct ieee80211_hdr *hdr;
6282 	struct ieee80211_rx_status *rx_status;
6283 	u8 *bssid;
6284 };
6285 
6286 static void rtl8xxxu_rx_update_rssi_iter(void *data, u8 *mac,
6287 					 struct ieee80211_vif *vif)
6288 {
6289 	struct rtl8xxxu_rx_update_rssi_data *iter_data = data;
6290 	struct ieee80211_sta *sta;
6291 	struct ieee80211_hdr *hdr = iter_data->hdr;
6292 	struct rtl8xxxu_priv *priv = iter_data->priv;
6293 	struct rtl8xxxu_sta_info *sta_info;
6294 	struct ieee80211_rx_status *rx_status = iter_data->rx_status;
6295 	u8 *bssid = iter_data->bssid;
6296 
6297 	if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
6298 		return;
6299 
6300 	if (!(ether_addr_equal(vif->addr, hdr->addr1) ||
6301 	      ieee80211_is_beacon(hdr->frame_control)))
6302 		return;
6303 
6304 	sta = ieee80211_find_sta_by_ifaddr(priv->hw, hdr->addr2,
6305 					   vif->addr);
6306 	if (!sta)
6307 		return;
6308 
6309 	sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
6310 	ewma_rssi_add(&sta_info->avg_rssi, -rx_status->signal);
6311 }
6312 
6313 static inline u8 *get_hdr_bssid(struct ieee80211_hdr *hdr)
6314 {
6315 	__le16 fc = hdr->frame_control;
6316 	u8 *bssid;
6317 
6318 	if (ieee80211_has_tods(fc))
6319 		bssid = hdr->addr1;
6320 	else if (ieee80211_has_fromds(fc))
6321 		bssid = hdr->addr2;
6322 	else
6323 		bssid = hdr->addr3;
6324 
6325 	return bssid;
6326 }
6327 
6328 static void rtl8xxxu_rx_update_rssi(struct rtl8xxxu_priv *priv,
6329 				    struct ieee80211_rx_status *rx_status,
6330 				    struct ieee80211_hdr *hdr)
6331 {
6332 	struct rtl8xxxu_rx_update_rssi_data data = {};
6333 
6334 	if (ieee80211_is_ctl(hdr->frame_control))
6335 		return;
6336 
6337 	data.priv = priv;
6338 	data.hdr = hdr;
6339 	data.rx_status = rx_status;
6340 	data.bssid = get_hdr_bssid(hdr);
6341 
6342 	rtl8xxxu_iterate_vifs_atomic(priv, rtl8xxxu_rx_update_rssi_iter, &data);
6343 }
6344 
6345 int rtl8xxxu_parse_rxdesc16(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
6346 {
6347 	struct ieee80211_hw *hw = priv->hw;
6348 	struct ieee80211_rx_status *rx_status;
6349 	struct rtl8xxxu_rxdesc16 *rx_desc;
6350 	struct rtl8723au_phy_stats *phy_stats;
6351 	struct sk_buff *next_skb = NULL;
6352 	__le32 *_rx_desc_le;
6353 	u32 *_rx_desc;
6354 	int drvinfo_sz, desc_shift;
6355 	int i, pkt_cnt, pkt_len, urb_len, pkt_offset;
6356 
6357 	urb_len = skb->len;
6358 	pkt_cnt = 0;
6359 
6360 	if (urb_len < sizeof(struct rtl8xxxu_rxdesc16)) {
6361 		kfree_skb(skb);
6362 		return RX_TYPE_ERROR;
6363 	}
6364 
6365 	do {
6366 		rx_desc = (struct rtl8xxxu_rxdesc16 *)skb->data;
6367 		_rx_desc_le = (__le32 *)skb->data;
6368 		_rx_desc = (u32 *)skb->data;
6369 
6370 		for (i = 0;
6371 		     i < (sizeof(struct rtl8xxxu_rxdesc16) / sizeof(u32)); i++)
6372 			_rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
6373 
6374 		/*
6375 		 * Only read pkt_cnt from the header if we're parsing the
6376 		 * first packet
6377 		 */
6378 		if (!pkt_cnt)
6379 			pkt_cnt = rx_desc->pkt_cnt;
6380 		pkt_len = rx_desc->pktlen;
6381 
6382 		drvinfo_sz = rx_desc->drvinfo_sz * 8;
6383 		desc_shift = rx_desc->shift;
6384 		pkt_offset = roundup(pkt_len + drvinfo_sz + desc_shift +
6385 				     sizeof(struct rtl8xxxu_rxdesc16), 128);
6386 
6387 		/*
6388 		 * Only clone the skb if there's enough data at the end to
6389 		 * at least cover the rx descriptor
6390 		 */
6391 		if (pkt_cnt > 1 &&
6392 		    urb_len >= (pkt_offset + sizeof(struct rtl8xxxu_rxdesc16)))
6393 			next_skb = skb_clone(skb, GFP_ATOMIC);
6394 
6395 		rx_status = IEEE80211_SKB_RXCB(skb);
6396 		memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
6397 
6398 		skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc16));
6399 
6400 		if (rx_desc->rpt_sel) {
6401 			skb_queue_tail(&priv->c2hcmd_queue, skb);
6402 			schedule_work(&priv->c2hcmd_work);
6403 		} else {
6404 			struct ieee80211_hdr *hdr;
6405 
6406 			phy_stats = (struct rtl8723au_phy_stats *)skb->data;
6407 
6408 			skb_pull(skb, drvinfo_sz + desc_shift);
6409 
6410 			skb_trim(skb, pkt_len);
6411 
6412 			hdr = (struct ieee80211_hdr *)skb->data;
6413 			if (rx_desc->phy_stats) {
6414 				priv->fops->parse_phystats(
6415 					priv, rx_status, phy_stats,
6416 					rx_desc->rxmcs,
6417 					hdr,
6418 					rx_desc->crc32 || rx_desc->icverr);
6419 				if (!rx_desc->crc32 && !rx_desc->icverr)
6420 					rtl8xxxu_rx_update_rssi(priv,
6421 								rx_status,
6422 								hdr);
6423 			} else {
6424 				rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
6425 			}
6426 
6427 			rx_status->mactime = rx_desc->tsfl;
6428 			rx_status->flag |= RX_FLAG_MACTIME_START;
6429 
6430 			if (!rx_desc->swdec &&
6431 			    rx_desc->security != RX_DESC_ENC_NONE)
6432 				rx_status->flag |= RX_FLAG_DECRYPTED;
6433 			if (rx_desc->crc32)
6434 				rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
6435 			if (rx_desc->bw)
6436 				rx_status->bw = RATE_INFO_BW_40;
6437 
6438 			if (rx_desc->rxht) {
6439 				rx_status->encoding = RX_ENC_HT;
6440 				rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
6441 			} else {
6442 				rx_status->rate_idx = rx_desc->rxmcs;
6443 			}
6444 
6445 			rx_status->freq = hw->conf.chandef.chan->center_freq;
6446 			rx_status->band = hw->conf.chandef.chan->band;
6447 
6448 			ieee80211_rx_irqsafe(hw, skb);
6449 		}
6450 
6451 		skb = next_skb;
6452 		if (skb)
6453 			skb_pull(next_skb, pkt_offset);
6454 
6455 		pkt_cnt--;
6456 		urb_len -= pkt_offset;
6457 		next_skb = NULL;
6458 	} while (skb && pkt_cnt > 0 &&
6459 		 urb_len >= sizeof(struct rtl8xxxu_rxdesc16));
6460 
6461 	return RX_TYPE_DATA_PKT;
6462 }
6463 
6464 int rtl8xxxu_parse_rxdesc24(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
6465 {
6466 	struct ieee80211_hw *hw = priv->hw;
6467 	struct ieee80211_rx_status *rx_status;
6468 	struct rtl8xxxu_rxdesc24 *rx_desc;
6469 	struct rtl8723au_phy_stats *phy_stats;
6470 	struct sk_buff *next_skb = NULL;
6471 	__le32 *_rx_desc_le;
6472 	u32 *_rx_desc;
6473 	int drvinfo_sz, desc_shift;
6474 	int i, pkt_len, urb_len, pkt_offset;
6475 
6476 	urb_len = skb->len;
6477 
6478 	if (urb_len < sizeof(struct rtl8xxxu_rxdesc24)) {
6479 		kfree_skb(skb);
6480 		return RX_TYPE_ERROR;
6481 	}
6482 
6483 	do {
6484 		rx_desc = (struct rtl8xxxu_rxdesc24 *)skb->data;
6485 		_rx_desc_le = (__le32 *)skb->data;
6486 		_rx_desc = (u32 *)skb->data;
6487 
6488 		for (i = 0; i < (sizeof(struct rtl8xxxu_rxdesc24) / sizeof(u32)); i++)
6489 			_rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
6490 
6491 		pkt_len = rx_desc->pktlen;
6492 
6493 		drvinfo_sz = rx_desc->drvinfo_sz * 8;
6494 		desc_shift = rx_desc->shift;
6495 		pkt_offset = roundup(pkt_len + drvinfo_sz + desc_shift +
6496 				     sizeof(struct rtl8xxxu_rxdesc24), 8);
6497 
6498 		/*
6499 		 * Only clone the skb if there's enough data at the end to
6500 		 * at least cover the rx descriptor
6501 		 */
6502 		if (urb_len >= (pkt_offset + sizeof(struct rtl8xxxu_rxdesc24)))
6503 			next_skb = skb_clone(skb, GFP_ATOMIC);
6504 
6505 		rx_status = IEEE80211_SKB_RXCB(skb);
6506 		memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
6507 
6508 		skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc24));
6509 
6510 		phy_stats = (struct rtl8723au_phy_stats *)skb->data;
6511 
6512 		skb_pull(skb, drvinfo_sz + desc_shift);
6513 
6514 		skb_trim(skb, pkt_len);
6515 
6516 		if (rx_desc->rpt_sel) {
6517 			struct device *dev = &priv->udev->dev;
6518 			dev_dbg(dev, "%s: C2H packet\n", __func__);
6519 			rtl8723bu_handle_c2h(priv, skb);
6520 		} else {
6521 			struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6522 
6523 			if (rx_desc->phy_stats) {
6524 				priv->fops->parse_phystats(priv, rx_status, phy_stats,
6525 							   rx_desc->rxmcs, hdr,
6526 							   rx_desc->crc32 || rx_desc->icverr);
6527 				if (!rx_desc->crc32 && !rx_desc->icverr)
6528 					rtl8xxxu_rx_update_rssi(priv,
6529 								rx_status,
6530 								hdr);
6531 			} else {
6532 				rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
6533 			}
6534 
6535 			rx_status->mactime = rx_desc->tsfl;
6536 			rx_status->flag |= RX_FLAG_MACTIME_START;
6537 
6538 			if (!rx_desc->swdec &&
6539 			    rx_desc->security != RX_DESC_ENC_NONE)
6540 				rx_status->flag |= RX_FLAG_DECRYPTED;
6541 			if (rx_desc->crc32)
6542 				rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
6543 			if (rx_desc->bw)
6544 				rx_status->bw = RATE_INFO_BW_40;
6545 
6546 			if (rx_desc->rxmcs >= DESC_RATE_MCS0) {
6547 				rx_status->encoding = RX_ENC_HT;
6548 				rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
6549 			} else {
6550 				rx_status->rate_idx = rx_desc->rxmcs;
6551 			}
6552 
6553 			rx_status->freq = hw->conf.chandef.chan->center_freq;
6554 			rx_status->band = hw->conf.chandef.chan->band;
6555 
6556 			ieee80211_rx_irqsafe(hw, skb);
6557 		}
6558 
6559 		skb = next_skb;
6560 		if (skb)
6561 			skb_pull(next_skb, pkt_offset);
6562 
6563 		urb_len -= pkt_offset;
6564 		next_skb = NULL;
6565 	} while (skb && urb_len >= sizeof(struct rtl8xxxu_rxdesc24));
6566 
6567 	return RX_TYPE_DATA_PKT;
6568 }
6569 
6570 static void rtl8xxxu_rx_complete(struct urb *urb)
6571 {
6572 	struct rtl8xxxu_rx_urb *rx_urb =
6573 		container_of(urb, struct rtl8xxxu_rx_urb, urb);
6574 	struct ieee80211_hw *hw = rx_urb->hw;
6575 	struct rtl8xxxu_priv *priv = hw->priv;
6576 	struct sk_buff *skb = (struct sk_buff *)urb->context;
6577 	struct device *dev = &priv->udev->dev;
6578 
6579 	skb_put(skb, urb->actual_length);
6580 
6581 	if (urb->status == 0) {
6582 		priv->fops->parse_rx_desc(priv, skb);
6583 
6584 		skb = NULL;
6585 		rx_urb->urb.context = NULL;
6586 		rtl8xxxu_queue_rx_urb(priv, rx_urb, false);
6587 	} else {
6588 		dev_dbg(dev, "%s: status %i\n",	__func__, urb->status);
6589 
6590 		switch (urb->status) {
6591 		case -EPROTO:
6592 		case -EILSEQ:
6593 		case -ETIME:
6594 		case -EOVERFLOW:
6595 		case -ECOMM:
6596 		case -ENOSR:
6597 			dev_kfree_skb(skb);
6598 			urb->context = NULL;
6599 			rtl8xxxu_queue_rx_urb(priv, rx_urb, true);
6600 			return;
6601 		default:
6602 			goto cleanup;
6603 		}
6604 	}
6605 	return;
6606 
6607 cleanup:
6608 	usb_free_urb(urb);
6609 	dev_kfree_skb(skb);
6610 }
6611 
6612 static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
6613 				  struct rtl8xxxu_rx_urb *rx_urb)
6614 {
6615 	struct rtl8xxxu_fileops *fops = priv->fops;
6616 	struct sk_buff *skb;
6617 	int skb_size;
6618 	int ret, rx_desc_sz;
6619 
6620 	rx_desc_sz = fops->rx_desc_size;
6621 
6622 	if (priv->rx_buf_aggregation && fops->rx_agg_buf_size) {
6623 		skb_size = fops->rx_agg_buf_size;
6624 		skb_size += (rx_desc_sz + sizeof(struct rtl8723au_phy_stats));
6625 	} else {
6626 		skb_size = IEEE80211_MAX_FRAME_LEN + rx_desc_sz;
6627 	}
6628 
6629 	skb = __netdev_alloc_skb(NULL, skb_size, GFP_KERNEL);
6630 	if (!skb)
6631 		return -ENOMEM;
6632 
6633 	memset(skb->data, 0, rx_desc_sz);
6634 	usb_fill_bulk_urb(&rx_urb->urb, priv->udev, priv->pipe_in, skb->data,
6635 			  skb_size, rtl8xxxu_rx_complete, skb);
6636 	usb_anchor_urb(&rx_urb->urb, &priv->rx_anchor);
6637 	ret = usb_submit_urb(&rx_urb->urb, GFP_ATOMIC);
6638 	if (ret) {
6639 		usb_unanchor_urb(&rx_urb->urb);
6640 		dev_kfree_skb(skb);
6641 		rx_urb->urb.context = NULL;
6642 	}
6643 	return ret;
6644 }
6645 
6646 static void rtl8xxxu_int_complete(struct urb *urb)
6647 {
6648 	struct rtl8xxxu_priv *priv = (struct rtl8xxxu_priv *)urb->context;
6649 	struct device *dev = &priv->udev->dev;
6650 	int ret;
6651 
6652 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_INTERRUPT)
6653 		dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
6654 	if (urb->status == 0) {
6655 		usb_anchor_urb(urb, &priv->int_anchor);
6656 		ret = usb_submit_urb(urb, GFP_ATOMIC);
6657 		if (ret)
6658 			usb_unanchor_urb(urb);
6659 	} else {
6660 		dev_dbg(dev, "%s: Error %i\n", __func__, urb->status);
6661 	}
6662 }
6663 
6664 
6665 static int rtl8xxxu_submit_int_urb(struct ieee80211_hw *hw)
6666 {
6667 	struct rtl8xxxu_priv *priv = hw->priv;
6668 	struct urb *urb;
6669 	u32 val32;
6670 	int ret;
6671 
6672 	urb = usb_alloc_urb(0, GFP_KERNEL);
6673 	if (!urb)
6674 		return -ENOMEM;
6675 
6676 	usb_fill_int_urb(urb, priv->udev, priv->pipe_interrupt,
6677 			 priv->int_buf, USB_INTR_CONTENT_LENGTH,
6678 			 rtl8xxxu_int_complete, priv, 1);
6679 	usb_anchor_urb(urb, &priv->int_anchor);
6680 	ret = usb_submit_urb(urb, GFP_KERNEL);
6681 	if (ret) {
6682 		usb_unanchor_urb(urb);
6683 		goto error;
6684 	}
6685 
6686 	val32 = rtl8xxxu_read32(priv, REG_USB_HIMR);
6687 	val32 |= USB_HIMR_CPWM;
6688 	rtl8xxxu_write32(priv, REG_USB_HIMR, val32);
6689 
6690 error:
6691 	usb_free_urb(urb);
6692 	return ret;
6693 }
6694 
6695 static void rtl8xxxu_switch_ports(struct rtl8xxxu_priv *priv)
6696 {
6697 	u8 macid[ETH_ALEN], bssid[ETH_ALEN], macid_1[ETH_ALEN], bssid_1[ETH_ALEN];
6698 	u8 msr, bcn_ctrl, bcn_ctrl_1, atimwnd[2], atimwnd_1[2];
6699 	struct rtl8xxxu_vif *rtlvif;
6700 	u8 tsftr[8], tsftr_1[8];
6701 	int i;
6702 
6703 	msr = rtl8xxxu_read8(priv, REG_MSR);
6704 	bcn_ctrl = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
6705 	bcn_ctrl_1 = rtl8xxxu_read8(priv, REG_BEACON_CTRL_1);
6706 
6707 	for (i = 0; i < ARRAY_SIZE(atimwnd); i++)
6708 		atimwnd[i] = rtl8xxxu_read8(priv, REG_ATIMWND + i);
6709 	for (i = 0; i < ARRAY_SIZE(atimwnd_1); i++)
6710 		atimwnd_1[i] = rtl8xxxu_read8(priv, REG_ATIMWND_1 + i);
6711 
6712 	for (i = 0; i < ARRAY_SIZE(tsftr); i++)
6713 		tsftr[i] = rtl8xxxu_read8(priv, REG_TSFTR + i);
6714 	for (i = 0; i < ARRAY_SIZE(tsftr); i++)
6715 		tsftr_1[i] = rtl8xxxu_read8(priv, REG_TSFTR1 + i);
6716 
6717 	for (i = 0; i < ARRAY_SIZE(macid); i++)
6718 		macid[i] = rtl8xxxu_read8(priv, REG_MACID + i);
6719 
6720 	for (i = 0; i < ARRAY_SIZE(bssid); i++)
6721 		bssid[i] = rtl8xxxu_read8(priv, REG_BSSID + i);
6722 
6723 	for (i = 0; i < ARRAY_SIZE(macid_1); i++)
6724 		macid_1[i] = rtl8xxxu_read8(priv, REG_MACID1 + i);
6725 
6726 	for (i = 0; i < ARRAY_SIZE(bssid_1); i++)
6727 		bssid_1[i] = rtl8xxxu_read8(priv, REG_BSSID1 + i);
6728 
6729 	/* disable bcn function, disable update TSF */
6730 	rtl8xxxu_write8(priv, REG_BEACON_CTRL, (bcn_ctrl &
6731 			(~BEACON_FUNCTION_ENABLE)) | BEACON_DISABLE_TSF_UPDATE);
6732 	rtl8xxxu_write8(priv, REG_BEACON_CTRL_1, (bcn_ctrl_1 &
6733 			(~BEACON_FUNCTION_ENABLE)) | BEACON_DISABLE_TSF_UPDATE);
6734 
6735 	/* switch msr */
6736 	msr = (msr & 0xf0) | ((msr & 0x03) << 2) | ((msr & 0x0c) >> 2);
6737 	rtl8xxxu_write8(priv, REG_MSR, msr);
6738 
6739 	/* write port0 */
6740 	rtl8xxxu_write8(priv, REG_BEACON_CTRL, bcn_ctrl_1 & ~BEACON_FUNCTION_ENABLE);
6741 	for (i = 0; i < ARRAY_SIZE(atimwnd_1); i++)
6742 		rtl8xxxu_write8(priv, REG_ATIMWND + i, atimwnd_1[i]);
6743 	for (i = 0; i < ARRAY_SIZE(tsftr_1); i++)
6744 		rtl8xxxu_write8(priv, REG_TSFTR + i, tsftr_1[i]);
6745 	for (i = 0; i < ARRAY_SIZE(macid_1); i++)
6746 		rtl8xxxu_write8(priv, REG_MACID + i, macid_1[i]);
6747 	for (i = 0; i < ARRAY_SIZE(bssid_1); i++)
6748 		rtl8xxxu_write8(priv, REG_BSSID + i, bssid_1[i]);
6749 
6750 	/* write port1 */
6751 	rtl8xxxu_write8(priv, REG_BEACON_CTRL_1, bcn_ctrl & ~BEACON_FUNCTION_ENABLE);
6752 	for (i = 0; i < ARRAY_SIZE(atimwnd); i++)
6753 		rtl8xxxu_write8(priv, REG_ATIMWND_1 + i, atimwnd[i]);
6754 	for (i = 0; i < ARRAY_SIZE(tsftr); i++)
6755 		rtl8xxxu_write8(priv, REG_TSFTR1 + i, tsftr[i]);
6756 	for (i = 0; i < ARRAY_SIZE(macid); i++)
6757 		rtl8xxxu_write8(priv, REG_MACID1 + i, macid[i]);
6758 	for (i = 0; i < ARRAY_SIZE(bssid); i++)
6759 		rtl8xxxu_write8(priv, REG_BSSID1 + i, bssid[i]);
6760 
6761 	/* write bcn ctl */
6762 	rtl8xxxu_write8(priv, REG_BEACON_CTRL, bcn_ctrl_1);
6763 	rtl8xxxu_write8(priv, REG_BEACON_CTRL_1, bcn_ctrl);
6764 	swap(priv->vifs[0], priv->vifs[1]);
6765 
6766 	/* priv->vifs[0] is NULL here, based on how this function is currently
6767 	 * called from rtl8xxxu_add_interface().
6768 	 * When this function will be used in the future for a different
6769 	 * scenario, please check whether vifs[0] or vifs[1] can be NULL and if
6770 	 * necessary add code to set port_num = 1.
6771 	 */
6772 	rtlvif = (struct rtl8xxxu_vif *)priv->vifs[1]->drv_priv;
6773 	rtlvif->port_num = 1;
6774 }
6775 
6776 static int rtl8xxxu_add_interface(struct ieee80211_hw *hw,
6777 				  struct ieee80211_vif *vif)
6778 {
6779 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
6780 	struct rtl8xxxu_priv *priv = hw->priv;
6781 	int port_num;
6782 	u8 val8;
6783 
6784 	if (!priv->vifs[0])
6785 		port_num = 0;
6786 	else if (!priv->vifs[1])
6787 		port_num = 1;
6788 	else
6789 		return -EOPNOTSUPP;
6790 
6791 	switch (vif->type) {
6792 	case NL80211_IFTYPE_STATION:
6793 		if (port_num == 0) {
6794 			rtl8xxxu_stop_tx_beacon(priv);
6795 
6796 			val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
6797 			val8 |= BEACON_ATIM | BEACON_FUNCTION_ENABLE |
6798 				BEACON_DISABLE_TSF_UPDATE;
6799 			rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
6800 		}
6801 		break;
6802 	case NL80211_IFTYPE_AP:
6803 		if (port_num == 1) {
6804 			rtl8xxxu_switch_ports(priv);
6805 			port_num = 0;
6806 		}
6807 
6808 		rtl8xxxu_write8(priv, REG_BEACON_CTRL,
6809 				BEACON_DISABLE_TSF_UPDATE | BEACON_CTRL_MBSSID);
6810 		rtl8xxxu_write8(priv, REG_ATIMWND, 0x0c); /* 12ms */
6811 		rtl8xxxu_write16(priv, REG_TSFTR_SYN_OFFSET, 0x7fff); /* ~32ms */
6812 		rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, DUAL_TSF_RESET_TSF0);
6813 
6814 		/* enable BCN0 function */
6815 		rtl8xxxu_write8(priv, REG_BEACON_CTRL,
6816 				BEACON_DISABLE_TSF_UPDATE |
6817 				BEACON_FUNCTION_ENABLE | BEACON_CTRL_MBSSID |
6818 				BEACON_CTRL_TX_BEACON_RPT);
6819 
6820 		/* select BCN on port 0 */
6821 		val8 = rtl8xxxu_read8(priv, REG_CCK_CHECK);
6822 		val8 &= ~BIT_BCN_PORT_SEL;
6823 		rtl8xxxu_write8(priv, REG_CCK_CHECK, val8);
6824 		break;
6825 	default:
6826 		return -EOPNOTSUPP;
6827 	}
6828 
6829 	priv->vifs[port_num] = vif;
6830 	rtlvif->port_num = port_num;
6831 	rtlvif->hw_key_idx = 0xff;
6832 
6833 	rtl8xxxu_set_linktype(priv, vif->type, port_num);
6834 	ether_addr_copy(priv->mac_addr, vif->addr);
6835 	rtl8xxxu_set_mac(priv, port_num);
6836 
6837 	return 0;
6838 }
6839 
6840 static void rtl8xxxu_remove_interface(struct ieee80211_hw *hw,
6841 				      struct ieee80211_vif *vif)
6842 {
6843 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
6844 	struct rtl8xxxu_priv *priv = hw->priv;
6845 
6846 	dev_dbg(&priv->udev->dev, "%s\n", __func__);
6847 
6848 	priv->vifs[rtlvif->port_num] = NULL;
6849 }
6850 
6851 static int rtl8xxxu_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
6852 {
6853 	struct rtl8xxxu_priv *priv = hw->priv;
6854 	struct device *dev = &priv->udev->dev;
6855 	int ret = 0, channel;
6856 	bool ht40;
6857 
6858 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
6859 		dev_info(dev,
6860 			 "%s: channel: %i (changed %08x chandef.width %02x)\n",
6861 			 __func__, hw->conf.chandef.chan->hw_value,
6862 			 changed, hw->conf.chandef.width);
6863 
6864 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
6865 		switch (hw->conf.chandef.width) {
6866 		case NL80211_CHAN_WIDTH_20_NOHT:
6867 		case NL80211_CHAN_WIDTH_20:
6868 			ht40 = false;
6869 			break;
6870 		case NL80211_CHAN_WIDTH_40:
6871 			ht40 = true;
6872 			break;
6873 		default:
6874 			ret = -ENOTSUPP;
6875 			goto exit;
6876 		}
6877 
6878 		channel = hw->conf.chandef.chan->hw_value;
6879 
6880 		priv->fops->set_tx_power(priv, channel, ht40);
6881 
6882 		priv->fops->config_channel(hw);
6883 	}
6884 
6885 exit:
6886 	return ret;
6887 }
6888 
6889 static int rtl8xxxu_conf_tx(struct ieee80211_hw *hw,
6890 			    struct ieee80211_vif *vif,
6891 			    unsigned int link_id, u16 queue,
6892 			    const struct ieee80211_tx_queue_params *param)
6893 {
6894 	struct rtl8xxxu_priv *priv = hw->priv;
6895 	struct device *dev = &priv->udev->dev;
6896 	u32 val32;
6897 	u8 aifs, acm_ctrl, acm_bit;
6898 
6899 	aifs = param->aifs;
6900 
6901 	val32 = aifs |
6902 		fls(param->cw_min) << EDCA_PARAM_ECW_MIN_SHIFT |
6903 		fls(param->cw_max) << EDCA_PARAM_ECW_MAX_SHIFT |
6904 		(u32)param->txop << EDCA_PARAM_TXOP_SHIFT;
6905 
6906 	acm_ctrl = rtl8xxxu_read8(priv, REG_ACM_HW_CTRL);
6907 	dev_dbg(dev,
6908 		"%s: IEEE80211 queue %02x val %08x, acm %i, acm_ctrl %02x\n",
6909 		__func__, queue, val32, param->acm, acm_ctrl);
6910 
6911 	switch (queue) {
6912 	case IEEE80211_AC_VO:
6913 		acm_bit = ACM_HW_CTRL_VO;
6914 		rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, val32);
6915 		break;
6916 	case IEEE80211_AC_VI:
6917 		acm_bit = ACM_HW_CTRL_VI;
6918 		rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, val32);
6919 		break;
6920 	case IEEE80211_AC_BE:
6921 		acm_bit = ACM_HW_CTRL_BE;
6922 		rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, val32);
6923 		break;
6924 	case IEEE80211_AC_BK:
6925 		acm_bit = ACM_HW_CTRL_BK;
6926 		rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, val32);
6927 		break;
6928 	default:
6929 		acm_bit = 0;
6930 		break;
6931 	}
6932 
6933 	if (param->acm)
6934 		acm_ctrl |= acm_bit;
6935 	else
6936 		acm_ctrl &= ~acm_bit;
6937 	rtl8xxxu_write8(priv, REG_ACM_HW_CTRL, acm_ctrl);
6938 
6939 	return 0;
6940 }
6941 
6942 static void rtl8xxxu_configure_filter(struct ieee80211_hw *hw,
6943 				      unsigned int changed_flags,
6944 				      unsigned int *total_flags, u64 multicast)
6945 {
6946 	struct rtl8xxxu_priv *priv = hw->priv;
6947 	u32 rcr = priv->regrcr;
6948 
6949 	dev_dbg(&priv->udev->dev, "%s: changed_flags %08x, total_flags %08x\n",
6950 		__func__, changed_flags, *total_flags);
6951 
6952 	/*
6953 	 * FIF_ALLMULTI ignored as all multicast frames are accepted (REG_MAR)
6954 	 */
6955 
6956 	if (*total_flags & FIF_FCSFAIL)
6957 		rcr |= RCR_ACCEPT_CRC32;
6958 	else
6959 		rcr &= ~RCR_ACCEPT_CRC32;
6960 
6961 	/*
6962 	 * FIF_PLCPFAIL not supported?
6963 	 */
6964 
6965 	if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6966 		rcr &= ~(RCR_CHECK_BSSID_BEACON | RCR_CHECK_BSSID_MATCH);
6967 	else
6968 		rcr |= RCR_CHECK_BSSID_BEACON | RCR_CHECK_BSSID_MATCH;
6969 
6970 	if (priv->vifs[0] && priv->vifs[0]->type == NL80211_IFTYPE_AP)
6971 		rcr &= ~(RCR_CHECK_BSSID_MATCH | RCR_CHECK_BSSID_BEACON);
6972 
6973 	if (*total_flags & FIF_CONTROL)
6974 		rcr |= RCR_ACCEPT_CTRL_FRAME;
6975 	else
6976 		rcr &= ~RCR_ACCEPT_CTRL_FRAME;
6977 
6978 	if (*total_flags & FIF_OTHER_BSS)
6979 		rcr |= RCR_ACCEPT_AP;
6980 	else
6981 		rcr &= ~RCR_ACCEPT_AP;
6982 
6983 	if (*total_flags & FIF_PSPOLL)
6984 		rcr |= RCR_ACCEPT_PM;
6985 	else
6986 		rcr &= ~RCR_ACCEPT_PM;
6987 
6988 	/*
6989 	 * FIF_PROBE_REQ ignored as probe requests always seem to be accepted
6990 	 */
6991 
6992 	rtl8xxxu_write32(priv, REG_RCR, rcr);
6993 	priv->regrcr = rcr;
6994 
6995 	*total_flags &= (FIF_ALLMULTI | FIF_FCSFAIL | FIF_BCN_PRBRESP_PROMISC |
6996 			 FIF_CONTROL | FIF_OTHER_BSS | FIF_PSPOLL |
6997 			 FIF_PROBE_REQ);
6998 }
6999 
7000 static int rtl8xxxu_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
7001 				      u32 rts)
7002 {
7003 	if (rts > 2347 && rts != (u32)-1)
7004 		return -EINVAL;
7005 
7006 	return 0;
7007 }
7008 
7009 static int rtl8xxxu_get_free_sec_cam(struct ieee80211_hw *hw)
7010 {
7011 	struct rtl8xxxu_priv *priv = hw->priv;
7012 
7013 	return find_first_zero_bit(priv->cam_map, priv->fops->max_sec_cam_num);
7014 }
7015 
7016 static int rtl8xxxu_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7017 			    struct ieee80211_vif *vif,
7018 			    struct ieee80211_sta *sta,
7019 			    struct ieee80211_key_conf *key)
7020 {
7021 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
7022 	struct rtl8xxxu_priv *priv = hw->priv;
7023 	struct device *dev = &priv->udev->dev;
7024 	u8 mac_addr[ETH_ALEN];
7025 	u8 val8;
7026 	u16 val16;
7027 	u32 val32;
7028 	int retval = -EOPNOTSUPP;
7029 
7030 	dev_dbg(dev, "%s: cmd %02x, cipher %08x, index %i\n",
7031 		__func__, cmd, key->cipher, key->keyidx);
7032 
7033 	if (key->keyidx > 3)
7034 		return -EOPNOTSUPP;
7035 
7036 	switch (key->cipher) {
7037 	case WLAN_CIPHER_SUITE_WEP40:
7038 	case WLAN_CIPHER_SUITE_WEP104:
7039 
7040 		break;
7041 	case WLAN_CIPHER_SUITE_CCMP:
7042 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
7043 		break;
7044 	case WLAN_CIPHER_SUITE_TKIP:
7045 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
7046 		break;
7047 	default:
7048 		return -EOPNOTSUPP;
7049 	}
7050 
7051 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
7052 		dev_dbg(dev, "%s: pairwise key\n", __func__);
7053 		ether_addr_copy(mac_addr, sta->addr);
7054 	} else {
7055 		dev_dbg(dev, "%s: group key\n", __func__);
7056 		ether_addr_copy(mac_addr, vif->bss_conf.bssid);
7057 	}
7058 
7059 	val16 = rtl8xxxu_read16(priv, REG_CR);
7060 	val16 |= CR_SECURITY_ENABLE;
7061 	rtl8xxxu_write16(priv, REG_CR, val16);
7062 
7063 	val8 = SEC_CFG_TX_SEC_ENABLE | SEC_CFG_TXBC_USE_DEFKEY |
7064 		SEC_CFG_RX_SEC_ENABLE | SEC_CFG_RXBC_USE_DEFKEY;
7065 	val8 |= SEC_CFG_TX_USE_DEFKEY | SEC_CFG_RX_USE_DEFKEY;
7066 	rtl8xxxu_write8(priv, REG_SECURITY_CFG, val8);
7067 
7068 	switch (cmd) {
7069 	case SET_KEY:
7070 
7071 		retval = rtl8xxxu_get_free_sec_cam(hw);
7072 		if (retval < 0)
7073 			return -EOPNOTSUPP;
7074 
7075 		key->hw_key_idx = retval;
7076 
7077 		if (vif->type == NL80211_IFTYPE_AP && !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
7078 			rtlvif->hw_key_idx = key->hw_key_idx;
7079 
7080 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7081 		rtl8xxxu_cam_write(priv, key, mac_addr);
7082 		set_bit(key->hw_key_idx, priv->cam_map);
7083 		retval = 0;
7084 		break;
7085 	case DISABLE_KEY:
7086 		rtl8xxxu_write32(priv, REG_CAM_WRITE, 0x00000000);
7087 		val32 = CAM_CMD_POLLING | CAM_CMD_WRITE |
7088 			key->hw_key_idx << CAM_CMD_KEY_SHIFT;
7089 		rtl8xxxu_write32(priv, REG_CAM_CMD, val32);
7090 		rtlvif->hw_key_idx = 0xff;
7091 		clear_bit(key->hw_key_idx, priv->cam_map);
7092 		retval = 0;
7093 		break;
7094 	default:
7095 		dev_warn(dev, "%s: Unsupported command %02x\n", __func__, cmd);
7096 	}
7097 
7098 	return retval;
7099 }
7100 
7101 static int
7102 rtl8xxxu_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7103 		      struct ieee80211_ampdu_params *params)
7104 {
7105 	struct rtl8xxxu_priv *priv = hw->priv;
7106 	struct device *dev = &priv->udev->dev;
7107 	u8 ampdu_factor, ampdu_density;
7108 	struct ieee80211_sta *sta = params->sta;
7109 	u16 tid = params->tid;
7110 	enum ieee80211_ampdu_mlme_action action = params->action;
7111 
7112 	switch (action) {
7113 	case IEEE80211_AMPDU_TX_START:
7114 		dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_START\n", __func__);
7115 		ampdu_factor = sta->deflink.ht_cap.ampdu_factor;
7116 		ampdu_density = sta->deflink.ht_cap.ampdu_density;
7117 		rtl8xxxu_set_ampdu_factor(priv, ampdu_factor);
7118 		rtl8xxxu_set_ampdu_min_space(priv, ampdu_density);
7119 		dev_dbg(dev,
7120 			"Changed HT: ampdu_factor %02x, ampdu_density %02x\n",
7121 			ampdu_factor, ampdu_density);
7122 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
7123 	case IEEE80211_AMPDU_TX_STOP_CONT:
7124 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
7125 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7126 		dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_STOP\n", __func__);
7127 		rtl8xxxu_set_ampdu_factor(priv, 0);
7128 		rtl8xxxu_set_ampdu_min_space(priv, 0);
7129 		clear_bit(tid, priv->tx_aggr_started);
7130 		clear_bit(tid, priv->tid_tx_operational);
7131 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
7132 		break;
7133 	case IEEE80211_AMPDU_TX_OPERATIONAL:
7134 		dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_OPERATIONAL\n", __func__);
7135 		set_bit(tid, priv->tid_tx_operational);
7136 		break;
7137 	case IEEE80211_AMPDU_RX_START:
7138 		dev_dbg(dev, "%s: IEEE80211_AMPDU_RX_START\n", __func__);
7139 		break;
7140 	case IEEE80211_AMPDU_RX_STOP:
7141 		dev_dbg(dev, "%s: IEEE80211_AMPDU_RX_STOP\n", __func__);
7142 		break;
7143 	default:
7144 		break;
7145 	}
7146 	return 0;
7147 }
7148 
7149 static void
7150 rtl8xxxu_sta_statistics(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7151 			struct ieee80211_sta *sta, struct station_info *sinfo)
7152 {
7153 	struct rtl8xxxu_priv *priv = hw->priv;
7154 
7155 	sinfo->txrate = priv->ra_report.txrate;
7156 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
7157 }
7158 
7159 static u8 rtl8xxxu_signal_to_snr(int signal)
7160 {
7161 	if (signal < RTL8XXXU_NOISE_FLOOR_MIN)
7162 		signal = RTL8XXXU_NOISE_FLOOR_MIN;
7163 	else if (signal > 0)
7164 		signal = 0;
7165 	return (u8)(signal - RTL8XXXU_NOISE_FLOOR_MIN);
7166 }
7167 
7168 static void rtl8xxxu_refresh_rate_mask(struct rtl8xxxu_priv *priv,
7169 				       int signal, struct ieee80211_sta *sta,
7170 				       bool force)
7171 {
7172 	struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
7173 	struct ieee80211_hw *hw = priv->hw;
7174 	u16 wireless_mode;
7175 	u8 rssi_level, ratr_idx;
7176 	u8 txbw_40mhz;
7177 	u8 snr, snr_thresh_high, snr_thresh_low;
7178 	u8 go_up_gap = 5;
7179 	u8 macid = rtl8xxxu_get_macid(priv, sta);
7180 
7181 	rssi_level = sta_info->rssi_level;
7182 	snr = rtl8xxxu_signal_to_snr(signal);
7183 	snr_thresh_high = RTL8XXXU_SNR_THRESH_HIGH;
7184 	snr_thresh_low = RTL8XXXU_SNR_THRESH_LOW;
7185 	txbw_40mhz = (hw->conf.chandef.width == NL80211_CHAN_WIDTH_40) ? 1 : 0;
7186 
7187 	switch (rssi_level) {
7188 	case RTL8XXXU_RATR_STA_MID:
7189 		snr_thresh_high += go_up_gap;
7190 		break;
7191 	case RTL8XXXU_RATR_STA_LOW:
7192 		snr_thresh_high += go_up_gap;
7193 		snr_thresh_low += go_up_gap;
7194 		break;
7195 	default:
7196 		break;
7197 	}
7198 
7199 	if (snr > snr_thresh_high)
7200 		rssi_level = RTL8XXXU_RATR_STA_HIGH;
7201 	else if (snr > snr_thresh_low)
7202 		rssi_level = RTL8XXXU_RATR_STA_MID;
7203 	else
7204 		rssi_level = RTL8XXXU_RATR_STA_LOW;
7205 
7206 	if (rssi_level != sta_info->rssi_level || force) {
7207 		int sgi = 0;
7208 		u32 rate_bitmap = 0;
7209 
7210 		rate_bitmap = (sta->deflink.supp_rates[0] & 0xfff) |
7211 				(sta->deflink.ht_cap.mcs.rx_mask[0] << 12) |
7212 				(sta->deflink.ht_cap.mcs.rx_mask[1] << 20);
7213 		if (sta->deflink.ht_cap.cap &
7214 		    (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
7215 			sgi = 1;
7216 
7217 		wireless_mode = rtl8xxxu_wireless_mode(hw, sta);
7218 		switch (wireless_mode) {
7219 		case WIRELESS_MODE_B:
7220 			ratr_idx = RATEID_IDX_B;
7221 			if (rate_bitmap & 0x0000000c)
7222 				rate_bitmap &= 0x0000000d;
7223 			else
7224 				rate_bitmap &= 0x0000000f;
7225 			break;
7226 		case WIRELESS_MODE_A:
7227 		case WIRELESS_MODE_G:
7228 			ratr_idx = RATEID_IDX_G;
7229 			if (rssi_level == RTL8XXXU_RATR_STA_HIGH)
7230 				rate_bitmap &= 0x00000f00;
7231 			else
7232 				rate_bitmap &= 0x00000ff0;
7233 			break;
7234 		case (WIRELESS_MODE_B | WIRELESS_MODE_G):
7235 			ratr_idx = RATEID_IDX_BG;
7236 			if (rssi_level == RTL8XXXU_RATR_STA_HIGH)
7237 				rate_bitmap &= 0x00000f00;
7238 			else if (rssi_level == RTL8XXXU_RATR_STA_MID)
7239 				rate_bitmap &= 0x00000ff0;
7240 			else
7241 				rate_bitmap &= 0x00000ff5;
7242 			break;
7243 		case WIRELESS_MODE_N_24G:
7244 		case WIRELESS_MODE_N_5G:
7245 		case (WIRELESS_MODE_G | WIRELESS_MODE_N_24G):
7246 		case (WIRELESS_MODE_A | WIRELESS_MODE_N_5G):
7247 			if (priv->tx_paths == 2 && priv->rx_paths == 2)
7248 				ratr_idx = RATEID_IDX_GN_N2SS;
7249 			else
7250 				ratr_idx = RATEID_IDX_GN_N1SS;
7251 			break;
7252 		case (WIRELESS_MODE_B | WIRELESS_MODE_G | WIRELESS_MODE_N_24G):
7253 		case (WIRELESS_MODE_B | WIRELESS_MODE_N_24G):
7254 			if (txbw_40mhz) {
7255 				if (priv->tx_paths == 2 && priv->rx_paths == 2)
7256 					ratr_idx = RATEID_IDX_BGN_40M_2SS;
7257 				else
7258 					ratr_idx = RATEID_IDX_BGN_40M_1SS;
7259 			} else {
7260 				if (priv->tx_paths == 2 && priv->rx_paths == 2)
7261 					ratr_idx = RATEID_IDX_BGN_20M_2SS_BN;
7262 				else
7263 					ratr_idx = RATEID_IDX_BGN_20M_1SS_BN;
7264 			}
7265 
7266 			if (priv->tx_paths == 2 && priv->rx_paths == 2) {
7267 				if (rssi_level == RTL8XXXU_RATR_STA_HIGH) {
7268 					rate_bitmap &= 0x0f8f0000;
7269 				} else if (rssi_level == RTL8XXXU_RATR_STA_MID) {
7270 					rate_bitmap &= 0x0f8ff000;
7271 				} else {
7272 					if (txbw_40mhz)
7273 						rate_bitmap &= 0x0f8ff015;
7274 					else
7275 						rate_bitmap &= 0x0f8ff005;
7276 				}
7277 			} else {
7278 				if (rssi_level == RTL8XXXU_RATR_STA_HIGH) {
7279 					rate_bitmap &= 0x000f0000;
7280 				} else if (rssi_level == RTL8XXXU_RATR_STA_MID) {
7281 					rate_bitmap &= 0x000ff000;
7282 				} else {
7283 					if (txbw_40mhz)
7284 						rate_bitmap &= 0x000ff015;
7285 					else
7286 						rate_bitmap &= 0x000ff005;
7287 				}
7288 			}
7289 			break;
7290 		default:
7291 			ratr_idx = RATEID_IDX_BGN_40M_2SS;
7292 			rate_bitmap &= 0x0fffffff;
7293 			break;
7294 		}
7295 
7296 		sta_info->rssi_level = rssi_level;
7297 		priv->fops->update_rate_mask(priv, rate_bitmap, ratr_idx, sgi, txbw_40mhz, macid);
7298 	}
7299 }
7300 
7301 static void rtl8xxxu_set_atc_status(struct rtl8xxxu_priv *priv, bool atc_status)
7302 {
7303 	struct rtl8xxxu_cfo_tracking *cfo = &priv->cfo_tracking;
7304 	u32 val32;
7305 
7306 	if (atc_status == cfo->atc_status)
7307 		return;
7308 
7309 	cfo->atc_status = atc_status;
7310 
7311 	val32 = rtl8xxxu_read32(priv, REG_OFDM1_CFO_TRACKING);
7312 	if (atc_status)
7313 		val32 |= CFO_TRACKING_ATC_STATUS;
7314 	else
7315 		val32 &= ~CFO_TRACKING_ATC_STATUS;
7316 	rtl8xxxu_write32(priv, REG_OFDM1_CFO_TRACKING, val32);
7317 }
7318 
7319 /* Central frequency offset correction */
7320 static void rtl8xxxu_track_cfo(struct rtl8xxxu_priv *priv)
7321 {
7322 	struct rtl8xxxu_cfo_tracking *cfo = &priv->cfo_tracking;
7323 	int cfo_khz_a, cfo_khz_b, cfo_average;
7324 	int crystal_cap;
7325 
7326 	if (!rtl8xxxu_is_assoc(priv)) {
7327 		/* Reset */
7328 		cfo->adjust = true;
7329 
7330 		if (cfo->crystal_cap > priv->default_crystal_cap)
7331 			priv->fops->set_crystal_cap(priv, cfo->crystal_cap - 1);
7332 		else if (cfo->crystal_cap < priv->default_crystal_cap)
7333 			priv->fops->set_crystal_cap(priv, cfo->crystal_cap + 1);
7334 
7335 		rtl8xxxu_set_atc_status(priv, true);
7336 
7337 		return;
7338 	}
7339 
7340 	if (cfo->packet_count == cfo->packet_count_pre)
7341 		/* No new information. */
7342 		return;
7343 
7344 	cfo->packet_count_pre = cfo->packet_count;
7345 
7346 	/* CFO_tail[1:0] is S(8,7), (num_subcarrier>>7) x 312.5K = CFO value(K Hz) */
7347 	cfo_khz_a = (int)((cfo->cfo_tail[0] * 3125) / 10) >> 7;
7348 	cfo_khz_b = (int)((cfo->cfo_tail[1] * 3125) / 10) >> 7;
7349 
7350 	if (priv->tx_paths == 1)
7351 		cfo_average = cfo_khz_a;
7352 	else
7353 		cfo_average = (cfo_khz_a + cfo_khz_b) / 2;
7354 
7355 	dev_dbg(&priv->udev->dev, "cfo_average: %d\n", cfo_average);
7356 
7357 	if (cfo->adjust) {
7358 		if (abs(cfo_average) < CFO_TH_XTAL_LOW)
7359 			cfo->adjust = false;
7360 	} else {
7361 		if (abs(cfo_average) > CFO_TH_XTAL_HIGH)
7362 			cfo->adjust = true;
7363 	}
7364 
7365 	/*
7366 	 * TODO: We should return here only if bluetooth is enabled.
7367 	 * See the vendor drivers for how to determine that.
7368 	 */
7369 	if (priv->has_bluetooth)
7370 		return;
7371 
7372 	if (!cfo->adjust)
7373 		return;
7374 
7375 	crystal_cap = cfo->crystal_cap;
7376 
7377 	if (cfo_average > CFO_TH_XTAL_LOW)
7378 		crystal_cap++;
7379 	else if (cfo_average < -CFO_TH_XTAL_LOW)
7380 		crystal_cap--;
7381 
7382 	crystal_cap = clamp(crystal_cap, 0, 0x3f);
7383 
7384 	priv->fops->set_crystal_cap(priv, crystal_cap);
7385 
7386 	rtl8xxxu_set_atc_status(priv, abs(cfo_average) >= CFO_TH_ATC);
7387 }
7388 
7389 static void rtl8xxxu_ra_iter(void *data, struct ieee80211_sta *sta)
7390 {
7391 	struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
7392 	struct rtl8xxxu_priv *priv = data;
7393 	int signal = -ewma_rssi_read(&sta_info->avg_rssi);
7394 
7395 	priv->fops->report_rssi(priv, rtl8xxxu_get_macid(priv, sta),
7396 				rtl8xxxu_signal_to_snr(signal));
7397 	rtl8xxxu_refresh_rate_mask(priv, signal, sta, false);
7398 }
7399 
7400 struct rtl8xxxu_stas_entry {
7401 	struct list_head list;
7402 	struct ieee80211_sta *sta;
7403 };
7404 
7405 struct rtl8xxxu_iter_stas_data {
7406 	struct rtl8xxxu_priv *priv;
7407 	struct list_head list;
7408 };
7409 
7410 static void rtl8xxxu_collect_sta_iter(void *data, struct ieee80211_sta *sta)
7411 {
7412 	struct rtl8xxxu_iter_stas_data *iter_stas = data;
7413 	struct rtl8xxxu_stas_entry *stas_entry;
7414 
7415 	stas_entry = kmalloc_obj(*stas_entry, GFP_ATOMIC);
7416 	if (!stas_entry)
7417 		return;
7418 
7419 	stas_entry->sta = sta;
7420 	list_add_tail(&stas_entry->list, &iter_stas->list);
7421 }
7422 
7423 static void rtl8xxxu_watchdog_callback(struct work_struct *work)
7424 {
7425 
7426 	struct rtl8xxxu_iter_stas_data iter_data;
7427 	struct rtl8xxxu_stas_entry *sta_entry, *tmp;
7428 	struct rtl8xxxu_priv *priv;
7429 
7430 	priv = container_of(work, struct rtl8xxxu_priv, ra_watchdog.work);
7431 	iter_data.priv = priv;
7432 	INIT_LIST_HEAD(&iter_data.list);
7433 
7434 	mutex_lock(&priv->sta_mutex);
7435 	ieee80211_iterate_stations_atomic(priv->hw, rtl8xxxu_collect_sta_iter,
7436 					  &iter_data);
7437 	list_for_each_entry_safe(sta_entry, tmp, &iter_data.list, list) {
7438 		list_del_init(&sta_entry->list);
7439 		rtl8xxxu_ra_iter(priv, sta_entry->sta);
7440 		kfree(sta_entry);
7441 	}
7442 	mutex_unlock(&priv->sta_mutex);
7443 
7444 	if (priv->fops->set_crystal_cap)
7445 		rtl8xxxu_track_cfo(priv);
7446 
7447 	schedule_delayed_work(&priv->ra_watchdog, 2 * HZ);
7448 }
7449 
7450 static int rtl8xxxu_start(struct ieee80211_hw *hw)
7451 {
7452 	struct rtl8xxxu_priv *priv = hw->priv;
7453 	struct rtl8xxxu_tx_urb *tx_urb;
7454 	unsigned long flags;
7455 	int ret, i;
7456 
7457 	ret = 0;
7458 
7459 	init_usb_anchor(&priv->rx_anchor);
7460 	init_usb_anchor(&priv->tx_anchor);
7461 	init_usb_anchor(&priv->int_anchor);
7462 
7463 	priv->fops->enable_rf(priv);
7464 	if (priv->usb_interrupts) {
7465 		ret = rtl8xxxu_submit_int_urb(hw);
7466 		if (ret)
7467 			goto error_out;
7468 	}
7469 
7470 	for (i = 0; i < RTL8XXXU_TX_URBS; i++) {
7471 		tx_urb = kmalloc_obj(struct rtl8xxxu_tx_urb);
7472 		if (!tx_urb) {
7473 			ret = -ENOMEM;
7474 
7475 			goto error_out;
7476 		}
7477 		usb_init_urb(&tx_urb->urb);
7478 		INIT_LIST_HEAD(&tx_urb->list);
7479 		tx_urb->hw = hw;
7480 		list_add(&tx_urb->list, &priv->tx_urb_free_list);
7481 		priv->tx_urb_free_count++;
7482 	}
7483 
7484 	ret = rtl8xxxu_alloc_rx_urbs(priv);
7485 	if (ret)
7486 		goto error_out;
7487 
7488 	priv->tx_stopped = false;
7489 
7490 	spin_lock_irqsave(&priv->rx_urb_lock, flags);
7491 	priv->shutdown = false;
7492 	spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
7493 
7494 	ret = rtl8xxxu_submit_rx_urbs(priv, true);
7495 	if (ret)
7496 		goto error_out;
7497 
7498 	schedule_delayed_work(&priv->ra_watchdog, 2 * HZ);
7499 	/*
7500 	 * Accept all data and mgmt frames
7501 	 */
7502 	rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0xffff);
7503 	rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0xffff);
7504 
7505 	rtl8xxxu_write32_mask(priv, REG_OFDM0_XA_AGC_CORE1,
7506 			      OFDM0_X_AGC_CORE1_IGI_MASK, 0x1e);
7507 
7508 	return ret;
7509 
7510 error_out:
7511 	rtl8xxxu_stop(hw, false);
7512 	return ret;
7513 }
7514 
7515 static void rtl8xxxu_stop(struct ieee80211_hw *hw, bool suspend)
7516 {
7517 	struct rtl8xxxu_priv *priv = hw->priv;
7518 	unsigned long flags;
7519 
7520 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
7521 
7522 	rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
7523 	rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
7524 
7525 	spin_lock_irqsave(&priv->rx_urb_lock, flags);
7526 	priv->shutdown = true;
7527 	spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
7528 
7529 	/*
7530 	 * Cancel before killing rx_anchor: the worker re-anchors every URB
7531 	 * it drained via rtl8xxxu_submit_rx_urb(), so a worker still running
7532 	 * after the kill could submit a URB that escapes it.
7533 	 */
7534 	cancel_delayed_work_sync(&priv->rx_urb_wq);
7535 
7536 	usb_kill_anchored_urbs(&priv->rx_anchor);
7537 	usb_kill_anchored_urbs(&priv->tx_anchor);
7538 	if (priv->usb_interrupts)
7539 		usb_kill_anchored_urbs(&priv->int_anchor);
7540 
7541 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
7542 
7543 	priv->fops->disable_rf(priv);
7544 
7545 	/*
7546 	 * Disable interrupts
7547 	 */
7548 	if (priv->usb_interrupts)
7549 		rtl8xxxu_write32(priv, REG_USB_HIMR, 0);
7550 
7551 	cancel_work_sync(&priv->c2hcmd_work);
7552 	cancel_delayed_work_sync(&priv->ra_watchdog);
7553 	cancel_delayed_work_sync(&priv->update_beacon_work);
7554 
7555 	rtl8xxxu_free_rx_resources(priv);
7556 	rtl8xxxu_free_tx_resources(priv);
7557 }
7558 
7559 static int rtl8xxxu_sta_add(struct ieee80211_hw *hw,
7560 			    struct ieee80211_vif *vif,
7561 			    struct ieee80211_sta *sta)
7562 {
7563 	struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
7564 	struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
7565 	struct rtl8xxxu_priv *priv = hw->priv;
7566 
7567 	mutex_lock(&priv->sta_mutex);
7568 	ewma_rssi_init(&sta_info->avg_rssi);
7569 	if (vif->type == NL80211_IFTYPE_AP) {
7570 		sta_info->rssi_level = RTL8XXXU_RATR_STA_INIT;
7571 		sta_info->macid = rtl8xxxu_acquire_macid(priv);
7572 		if (sta_info->macid >= RTL8XXXU_MAX_MAC_ID_NUM) {
7573 			mutex_unlock(&priv->sta_mutex);
7574 			return -ENOSPC;
7575 		}
7576 
7577 		rtl8xxxu_refresh_rate_mask(priv, 0, sta, true);
7578 		priv->fops->report_connect(priv, sta_info->macid, H2C_MACID_ROLE_STA, true);
7579 	} else {
7580 		switch (rtlvif->port_num) {
7581 		case 0:
7582 			sta_info->macid = RTL8XXXU_BC_MC_MACID;
7583 			break;
7584 		case 1:
7585 			sta_info->macid = RTL8XXXU_BC_MC_MACID1;
7586 			break;
7587 		default:
7588 			break;
7589 		}
7590 	}
7591 	mutex_unlock(&priv->sta_mutex);
7592 
7593 	return 0;
7594 }
7595 
7596 static int rtl8xxxu_sta_remove(struct ieee80211_hw *hw,
7597 			       struct ieee80211_vif *vif,
7598 			       struct ieee80211_sta *sta)
7599 {
7600 	struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
7601 	struct rtl8xxxu_priv *priv = hw->priv;
7602 
7603 	mutex_lock(&priv->sta_mutex);
7604 	if (vif->type == NL80211_IFTYPE_AP)
7605 		rtl8xxxu_release_macid(priv, sta_info->macid);
7606 	mutex_unlock(&priv->sta_mutex);
7607 
7608 	return 0;
7609 }
7610 
7611 static const struct ieee80211_ops rtl8xxxu_ops = {
7612 	.add_chanctx = ieee80211_emulate_add_chanctx,
7613 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
7614 	.change_chanctx = ieee80211_emulate_change_chanctx,
7615 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
7616 	.tx = rtl8xxxu_tx,
7617 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
7618 	.add_interface = rtl8xxxu_add_interface,
7619 	.remove_interface = rtl8xxxu_remove_interface,
7620 	.config = rtl8xxxu_config,
7621 	.conf_tx = rtl8xxxu_conf_tx,
7622 	.bss_info_changed = rtl8xxxu_bss_info_changed,
7623 	.start_ap = rtl8xxxu_start_ap,
7624 	.configure_filter = rtl8xxxu_configure_filter,
7625 	.set_rts_threshold = rtl8xxxu_set_rts_threshold,
7626 	.start = rtl8xxxu_start,
7627 	.stop = rtl8xxxu_stop,
7628 	.sw_scan_start = rtl8xxxu_sw_scan_start,
7629 	.sw_scan_complete = rtl8xxxu_sw_scan_complete,
7630 	.set_key = rtl8xxxu_set_key,
7631 	.ampdu_action = rtl8xxxu_ampdu_action,
7632 	.sta_statistics = rtl8xxxu_sta_statistics,
7633 	.get_antenna = rtl8xxxu_get_antenna,
7634 	.set_tim = rtl8xxxu_set_tim,
7635 	.sta_add = rtl8xxxu_sta_add,
7636 	.sta_remove = rtl8xxxu_sta_remove,
7637 };
7638 
7639 static int rtl8xxxu_parse_usb(struct rtl8xxxu_priv *priv,
7640 			      struct usb_interface *interface)
7641 {
7642 	struct usb_interface_descriptor *interface_desc;
7643 	struct usb_host_interface *host_interface;
7644 	struct usb_endpoint_descriptor *endpoint;
7645 	struct device *dev = &priv->udev->dev;
7646 	int i, j = 0, endpoints;
7647 	u8 dir, xtype, num;
7648 	int ret = 0;
7649 
7650 	host_interface = interface->cur_altsetting;
7651 	interface_desc = &host_interface->desc;
7652 	endpoints = interface_desc->bNumEndpoints;
7653 
7654 	for (i = 0; i < endpoints; i++) {
7655 		endpoint = &host_interface->endpoint[i].desc;
7656 
7657 		dir = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
7658 		num = usb_endpoint_num(endpoint);
7659 		xtype = usb_endpoint_type(endpoint);
7660 		if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
7661 			dev_dbg(dev,
7662 				"%s: endpoint: dir %02x, # %02x, type %02x\n",
7663 				__func__, dir, num, xtype);
7664 		if (usb_endpoint_dir_in(endpoint) &&
7665 		    usb_endpoint_xfer_bulk(endpoint)) {
7666 			if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
7667 				dev_dbg(dev, "%s: in endpoint num %i\n",
7668 					__func__, num);
7669 
7670 			if (priv->pipe_in) {
7671 				dev_warn(dev,
7672 					 "%s: Too many IN pipes\n", __func__);
7673 				ret = -EINVAL;
7674 				goto exit;
7675 			}
7676 
7677 			priv->pipe_in =	usb_rcvbulkpipe(priv->udev, num);
7678 		}
7679 
7680 		if (usb_endpoint_dir_in(endpoint) &&
7681 		    usb_endpoint_xfer_int(endpoint)) {
7682 			if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
7683 				dev_dbg(dev, "%s: interrupt endpoint num %i\n",
7684 					__func__, num);
7685 
7686 			if (priv->pipe_interrupt) {
7687 				dev_warn(dev, "%s: Too many INTERRUPT pipes\n",
7688 					 __func__);
7689 				ret = -EINVAL;
7690 				goto exit;
7691 			}
7692 
7693 			priv->pipe_interrupt = usb_rcvintpipe(priv->udev, num);
7694 		}
7695 
7696 		if (usb_endpoint_dir_out(endpoint) &&
7697 		    usb_endpoint_xfer_bulk(endpoint)) {
7698 			if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
7699 				dev_dbg(dev, "%s: out endpoint num %i\n",
7700 					__func__, num);
7701 			if (j >= RTL8XXXU_OUT_ENDPOINTS) {
7702 				dev_warn(dev,
7703 					 "%s: Too many OUT pipes\n", __func__);
7704 				ret = -EINVAL;
7705 				goto exit;
7706 			}
7707 			priv->out_ep[j++] = num;
7708 		}
7709 	}
7710 exit:
7711 	priv->nr_out_eps = j;
7712 	return ret;
7713 }
7714 
7715 static void rtl8xxxu_init_led(struct rtl8xxxu_priv *priv)
7716 {
7717 	struct led_classdev *led = &priv->led_cdev;
7718 
7719 	if (!priv->fops->led_classdev_brightness_set)
7720 		return;
7721 
7722 	led->brightness_set_blocking = priv->fops->led_classdev_brightness_set;
7723 
7724 	snprintf(priv->led_name, sizeof(priv->led_name),
7725 		 "rtl8xxxu-usb%s", dev_name(&priv->udev->dev));
7726 	led->name = priv->led_name;
7727 	led->max_brightness = RTL8XXXU_HW_LED_CONTROL;
7728 
7729 	if (led_classdev_register(&priv->udev->dev, led))
7730 		return;
7731 
7732 	priv->led_registered = true;
7733 
7734 	led->brightness = led->max_brightness;
7735 	priv->fops->led_classdev_brightness_set(led, led->brightness);
7736 }
7737 
7738 static void rtl8xxxu_deinit_led(struct rtl8xxxu_priv *priv)
7739 {
7740 	struct led_classdev *led = &priv->led_cdev;
7741 
7742 	if (!priv->led_registered)
7743 		return;
7744 
7745 	priv->fops->led_classdev_brightness_set(led, LED_OFF);
7746 	led_classdev_unregister(led);
7747 }
7748 
7749 static const struct ieee80211_iface_limit rtl8xxxu_limits[] = {
7750 	{ .max = 2, .types = BIT(NL80211_IFTYPE_STATION), },
7751 	{ .max = 1, .types = BIT(NL80211_IFTYPE_AP), },
7752 };
7753 
7754 static const struct ieee80211_iface_combination rtl8xxxu_combinations[] = {
7755 	{
7756 		.limits = rtl8xxxu_limits,
7757 		.n_limits = ARRAY_SIZE(rtl8xxxu_limits),
7758 		.max_interfaces = 2,
7759 		.num_different_channels = 1,
7760 	},
7761 };
7762 
7763 static int rtl8xxxu_probe(struct usb_interface *interface,
7764 			  const struct usb_device_id *id)
7765 {
7766 	struct rtl8xxxu_priv *priv;
7767 	struct ieee80211_hw *hw;
7768 	struct usb_device *udev;
7769 	struct ieee80211_supported_band *sband;
7770 	int ret;
7771 	int untested = 1;
7772 
7773 	udev = interface_to_usbdev(interface);
7774 
7775 	switch (id->idVendor) {
7776 	case USB_VENDOR_ID_REALTEK:
7777 		switch(id->idProduct) {
7778 		case 0x0179:
7779 		case 0x1724:
7780 		case 0x8176:
7781 		case 0x8178:
7782 		case 0x817f:
7783 		case 0x818b:
7784 		case 0xf179:
7785 		case 0x8179:
7786 		case 0xb711:
7787 		case 0xf192:
7788 		case 0x2005:
7789 			untested = 0;
7790 			break;
7791 		}
7792 		break;
7793 	case 0x7392:
7794 		if (id->idProduct == 0x7811 || id->idProduct == 0xa611 || id->idProduct == 0xb811)
7795 			untested = 0;
7796 		break;
7797 	case 0x050d:
7798 		if (id->idProduct == 0x1004)
7799 			untested = 0;
7800 		break;
7801 	case 0x20f4:
7802 		if (id->idProduct == 0x648b)
7803 			untested = 0;
7804 		break;
7805 	case 0x2001:
7806 		if (id->idProduct == 0x3308)
7807 			untested = 0;
7808 		break;
7809 	case 0x2357:
7810 		if (id->idProduct == 0x0109 || id->idProduct == 0x010c ||
7811 		    id->idProduct == 0x0135)
7812 			untested = 0;
7813 		break;
7814 	case 0x0b05:
7815 		if (id->idProduct == 0x18f1)
7816 			untested = 0;
7817 		break;
7818 	default:
7819 		break;
7820 	}
7821 
7822 	if (untested) {
7823 		rtl8xxxu_debug |= RTL8XXXU_DEBUG_EFUSE;
7824 		dev_info(&udev->dev,
7825 			 "This Realtek USB WiFi dongle (0x%04x:0x%04x) is untested!\n",
7826 			 id->idVendor, id->idProduct);
7827 		dev_info(&udev->dev,
7828 			 "Please report results to Jes.Sorensen@gmail.com\n");
7829 	}
7830 
7831 	hw = ieee80211_alloc_hw(sizeof(struct rtl8xxxu_priv), &rtl8xxxu_ops);
7832 	if (!hw)
7833 		return -ENOMEM;
7834 
7835 	priv = hw->priv;
7836 	priv->hw = hw;
7837 	priv->udev = udev;
7838 	priv->fops = (struct rtl8xxxu_fileops *)id->driver_info;
7839 	mutex_init(&priv->usb_buf_mutex);
7840 	mutex_init(&priv->syson_indirect_access_mutex);
7841 	mutex_init(&priv->h2c_mutex);
7842 	mutex_init(&priv->sta_mutex);
7843 	INIT_LIST_HEAD(&priv->tx_urb_free_list);
7844 	spin_lock_init(&priv->tx_urb_lock);
7845 	INIT_LIST_HEAD(&priv->rx_urb_pending_list);
7846 	spin_lock_init(&priv->rx_urb_lock);
7847 	INIT_DELAYED_WORK(&priv->rx_urb_wq, rtl8xxxu_rx_urb_work);
7848 	INIT_DELAYED_WORK(&priv->ra_watchdog, rtl8xxxu_watchdog_callback);
7849 	INIT_DELAYED_WORK(&priv->update_beacon_work, rtl8xxxu_update_beacon_work_callback);
7850 	skb_queue_head_init(&priv->c2hcmd_queue);
7851 
7852 	usb_set_intfdata(interface, hw);
7853 
7854 	ret = rtl8xxxu_parse_usb(priv, interface);
7855 	if (ret)
7856 		goto err_set_intfdata;
7857 
7858 	ret = priv->fops->identify_chip(priv);
7859 	if (ret) {
7860 		dev_err(&udev->dev, "Fatal - failed to identify chip\n");
7861 		goto err_set_intfdata;
7862 	}
7863 
7864 	hw->wiphy->available_antennas_tx = BIT(priv->tx_paths) - 1;
7865 	hw->wiphy->available_antennas_rx = BIT(priv->rx_paths) - 1;
7866 
7867 	if (priv->rtl_chip == RTL8188E)
7868 		INIT_WORK(&priv->c2hcmd_work, rtl8188e_c2hcmd_callback);
7869 	else
7870 		INIT_WORK(&priv->c2hcmd_work, rtl8xxxu_c2hcmd_callback);
7871 
7872 	ret = priv->fops->read_efuse(priv);
7873 	if (ret) {
7874 		dev_err(&udev->dev, "Fatal - failed to read EFuse\n");
7875 		goto err_set_intfdata;
7876 	}
7877 
7878 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_EFUSE)
7879 		rtl8xxxu_dump_efuse(priv);
7880 
7881 	ret = priv->fops->parse_efuse(priv);
7882 	if (ret) {
7883 		dev_err(&udev->dev, "Fatal - failed to parse EFuse\n");
7884 		goto err_set_intfdata;
7885 	}
7886 
7887 	rtl8xxxu_print_chipinfo(priv);
7888 
7889 	ret = priv->fops->load_firmware(priv);
7890 	if (ret) {
7891 		dev_err(&udev->dev, "Fatal - failed to load firmware\n");
7892 		goto err_set_intfdata;
7893 	}
7894 
7895 	ret = rtl8xxxu_init_device(hw);
7896 	if (ret)
7897 		goto err_set_intfdata;
7898 
7899 	hw->vif_data_size = sizeof(struct rtl8xxxu_vif);
7900 	hw->sta_data_size = sizeof(struct rtl8xxxu_sta_info);
7901 
7902 	hw->wiphy->max_scan_ssids = 1;
7903 	hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
7904 	if (priv->fops->max_macid_num)
7905 		hw->wiphy->max_ap_assoc_sta = priv->fops->max_macid_num - 1;
7906 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
7907 	if (priv->fops->supports_ap)
7908 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
7909 	hw->queues = 4;
7910 
7911 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
7912 
7913 	if (priv->fops->supports_concurrent) {
7914 		hw->wiphy->iface_combinations = rtl8xxxu_combinations;
7915 		hw->wiphy->n_iface_combinations = ARRAY_SIZE(rtl8xxxu_combinations);
7916 	}
7917 
7918 	sband = &rtl8xxxu_supported_band;
7919 	sband->ht_cap.ht_supported = true;
7920 	sband->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
7921 	sband->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
7922 	sband->ht_cap.cap = IEEE80211_HT_CAP_SGI_20;
7923 
7924 	if (priv->hw_feature.max_bw >= 40) {
7925 		sband->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
7926 		sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7927 	} else {
7928 		dev_info(&udev->dev, "hardware doesn't support HT40\n");
7929 	}
7930 
7931 	memset(&sband->ht_cap.mcs, 0, sizeof(sband->ht_cap.mcs));
7932 	sband->ht_cap.mcs.rx_mask[0] = 0xff;
7933 	sband->ht_cap.mcs.rx_mask[4] = 0x01;
7934 	if (priv->rf_paths > 1)
7935 		sband->ht_cap.mcs.rx_mask[1] = 0xff;
7936 	sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
7937 
7938 	hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
7939 
7940 	hw->wiphy->rts_threshold = 2347;
7941 
7942 	SET_IEEE80211_DEV(priv->hw, &interface->dev);
7943 	SET_IEEE80211_PERM_ADDR(hw, priv->mac_addr);
7944 
7945 	hw->extra_tx_headroom = priv->fops->tx_desc_size;
7946 	ieee80211_hw_set(hw, SIGNAL_DBM);
7947 
7948 	/*
7949 	 * The firmware handles rate control, except for RTL8188EU,
7950 	 * where we handle the rate control in the driver.
7951 	 */
7952 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
7953 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
7954 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
7955 	ieee80211_hw_set(hw, MFP_CAPABLE);
7956 
7957 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
7958 
7959 	ret = ieee80211_register_hw(priv->hw);
7960 	if (ret) {
7961 		dev_err(&udev->dev, "%s: Failed to register: %i\n",
7962 			__func__, ret);
7963 		goto err_set_intfdata;
7964 	}
7965 
7966 	rtl8xxxu_init_led(priv);
7967 	rtl8xxxu_debugfs_init(priv);
7968 
7969 	return 0;
7970 
7971 err_set_intfdata:
7972 	usb_set_intfdata(interface, NULL);
7973 
7974 	kfree(priv->fw_data);
7975 	mutex_destroy(&priv->usb_buf_mutex);
7976 	mutex_destroy(&priv->syson_indirect_access_mutex);
7977 	mutex_destroy(&priv->h2c_mutex);
7978 
7979 	ieee80211_free_hw(hw);
7980 
7981 	return ret;
7982 }
7983 
7984 static void rtl8xxxu_disconnect(struct usb_interface *interface)
7985 {
7986 	struct rtl8xxxu_priv *priv;
7987 	struct ieee80211_hw *hw;
7988 
7989 	hw = usb_get_intfdata(interface);
7990 	priv = hw->priv;
7991 
7992 	rtl8xxxu_deinit_led(priv);
7993 
7994 	ieee80211_unregister_hw(hw);
7995 
7996 	priv->fops->power_off(priv);
7997 
7998 	usb_set_intfdata(interface, NULL);
7999 
8000 	dev_info(&priv->udev->dev, "disconnecting\n");
8001 
8002 	kfree(priv->fw_data);
8003 	mutex_destroy(&priv->usb_buf_mutex);
8004 	mutex_destroy(&priv->syson_indirect_access_mutex);
8005 	mutex_destroy(&priv->h2c_mutex);
8006 
8007 	if (priv->udev->state != USB_STATE_NOTATTACHED) {
8008 		dev_info(&priv->udev->dev,
8009 			 "Device still attached, trying to reset\n");
8010 		usb_reset_device(priv->udev);
8011 	}
8012 	ieee80211_free_hw(hw);
8013 }
8014 
8015 static const struct usb_device_id dev_table[] = {
8016 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8724, 0xff, 0xff, 0xff),
8017 	.driver_info = (unsigned long)&rtl8723au_fops},
8018 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1724, 0xff, 0xff, 0xff),
8019 	.driver_info = (unsigned long)&rtl8723au_fops},
8020 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x0724, 0xff, 0xff, 0xff),
8021 	.driver_info = (unsigned long)&rtl8723au_fops},
8022 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818b, 0xff, 0xff, 0xff),
8023 	.driver_info = (unsigned long)&rtl8192eu_fops},
8024 /* TP-Link TL-WN822N v4 */
8025 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0108, 0xff, 0xff, 0xff),
8026 	.driver_info = (unsigned long)&rtl8192eu_fops},
8027 /* D-Link DWA-131 rev E1, tested by David Patiño */
8028 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3319, 0xff, 0xff, 0xff),
8029 	.driver_info = (unsigned long)&rtl8192eu_fops},
8030 /* Tested by Myckel Habets */
8031 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0109, 0xff, 0xff, 0xff),
8032 	.driver_info = (unsigned long)&rtl8192eu_fops},
8033 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xb720, 0xff, 0xff, 0xff),
8034 	.driver_info = (unsigned long)&rtl8723bu_fops},
8035 {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0xa611, 0xff, 0xff, 0xff),
8036 	.driver_info = (unsigned long)&rtl8723bu_fops},
8037 /* RTL8188FU */
8038 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xf179, 0xff, 0xff, 0xff),
8039 	.driver_info = (unsigned long)&rtl8188fu_fops},
8040 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8179, 0xff, 0xff, 0xff),
8041 	.driver_info = (unsigned long)&rtl8188eu_fops},
8042 /* Tested by Hans de Goede - rtl8188etv */
8043 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x0179, 0xff, 0xff, 0xff),
8044 	.driver_info = (unsigned long)&rtl8188eu_fops},
8045 /* Sitecom rtl8188eus */
8046 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0076, 0xff, 0xff, 0xff),
8047 	.driver_info = (unsigned long)&rtl8188eu_fops},
8048 /* D-Link USB-GO-N150 */
8049 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3311, 0xff, 0xff, 0xff),
8050 	.driver_info = (unsigned long)&rtl8188eu_fops},
8051 /* D-Link DWA-125 REV D1 */
8052 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330f, 0xff, 0xff, 0xff),
8053 	.driver_info = (unsigned long)&rtl8188eu_fops},
8054 /* D-Link DWA-123 REV D1 */
8055 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3310, 0xff, 0xff, 0xff),
8056 	.driver_info = (unsigned long)&rtl8188eu_fops},
8057 /* D-Link DWA-121 rev B1 */
8058 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x331b, 0xff, 0xff, 0xff),
8059 	.driver_info = (unsigned long)&rtl8188eu_fops},
8060 /* Abocom - Abocom */
8061 {USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8179, 0xff, 0xff, 0xff),
8062 	.driver_info = (unsigned long)&rtl8188eu_fops},
8063 /* Elecom WDC-150SU2M */
8064 {USB_DEVICE_AND_INTERFACE_INFO(0x056e, 0x4008, 0xff, 0xff, 0xff),
8065 	.driver_info = (unsigned long)&rtl8188eu_fops},
8066 /* TP-Link TL-WN722N v2 */
8067 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x010c, 0xff, 0xff, 0xff),
8068 	.driver_info = (unsigned long)&rtl8188eu_fops},
8069 /* TP-Link TL-WN727N v5.21 */
8070 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0111, 0xff, 0xff, 0xff),
8071 	.driver_info = (unsigned long)&rtl8188eu_fops},
8072 /* MERCUSYS MW150US v2 */
8073 {USB_DEVICE_AND_INTERFACE_INFO(0x2c4e, 0x0102, 0xff, 0xff, 0xff),
8074 	.driver_info = (unsigned long)&rtl8188eu_fops},
8075 /* ASUS USB-N10 Nano B1 */
8076 {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x18f0, 0xff, 0xff, 0xff),
8077 	.driver_info = (unsigned long)&rtl8188eu_fops},
8078  /* Edimax EW-7811Un V2 */
8079 {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0xb811, 0xff, 0xff, 0xff),
8080 	.driver_info = (unsigned long)&rtl8188eu_fops},
8081 /* Rosewill USB-N150 Nano */
8082 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xffef, 0xff, 0xff, 0xff),
8083 	.driver_info = (unsigned long)&rtl8188eu_fops},
8084 /* RTL8710BU aka RTL8188GU (not to be confused with RTL8188GTVU) */
8085 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xb711, 0xff, 0xff, 0xff),
8086 	.driver_info = (unsigned long)&rtl8710bu_fops},
8087 /* TOTOLINK N150UA V5 / N150UA-B */
8088 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x2005, 0xff, 0xff, 0xff),
8089 	.driver_info = (unsigned long)&rtl8710bu_fops},
8090 /* Comfast CF-826F */
8091 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xf192, 0xff, 0xff, 0xff),
8092 	.driver_info = (unsigned long)&rtl8192fu_fops},
8093 /* Asus USB-N13 rev C1 */
8094 {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x18f1, 0xff, 0xff, 0xff),
8095 	.driver_info = (unsigned long)&rtl8192fu_fops},
8096 /* EDIMAX EW-7722UTn V3 */
8097 {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0xb722, 0xff, 0xff, 0xff),
8098 	.driver_info = (unsigned long)&rtl8192fu_fops},
8099 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x318b, 0xff, 0xff, 0xff),
8100 	.driver_info = (unsigned long)&rtl8192fu_fops},
8101 /* TP-Link TL-WN823N V2 */
8102 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0135, 0xff, 0xff, 0xff),
8103 	.driver_info = (unsigned long)&rtl8192fu_fops},
8104 /* D-Link AN3U rev. A1 */
8105 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3328, 0xff, 0xff, 0xff),
8106 	.driver_info = (unsigned long)&rtl8192fu_fops},
8107 #ifdef CONFIG_RTL8XXXU_UNTESTED
8108 /* Still supported by rtlwifi */
8109 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8176, 0xff, 0xff, 0xff),
8110 	.driver_info = (unsigned long)&rtl8192cu_fops},
8111 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8178, 0xff, 0xff, 0xff),
8112 	.driver_info = (unsigned long)&rtl8192cu_fops},
8113 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817f, 0xff, 0xff, 0xff),
8114 	.driver_info = (unsigned long)&rtl8192cu_fops},
8115 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x819a, 0xff, 0xff, 0xff),
8116 	.driver_info = (unsigned long)&rtl8192cu_fops},
8117 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8754, 0xff, 0xff, 0xff),
8118 	.driver_info = (unsigned long)&rtl8192cu_fops},
8119 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817c, 0xff, 0xff, 0xff),
8120 	.driver_info = (unsigned long)&rtl8192cu_fops},
8121 /* Tested by Larry Finger */
8122 {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7811, 0xff, 0xff, 0xff),
8123 	.driver_info = (unsigned long)&rtl8192cu_fops},
8124 /* Tested by Andrea Merello */
8125 {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1004, 0xff, 0xff, 0xff),
8126 	.driver_info = (unsigned long)&rtl8192cu_fops},
8127 /* Tested by Jocelyn Mayer */
8128 {USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x648b, 0xff, 0xff, 0xff),
8129 	.driver_info = (unsigned long)&rtl8192cu_fops},
8130 /* Tested by Stefano Bravi */
8131 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3308, 0xff, 0xff, 0xff),
8132 	.driver_info = (unsigned long)&rtl8192cu_fops},
8133 /* Currently untested 8188 series devices */
8134 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x018a, 0xff, 0xff, 0xff),
8135 	.driver_info = (unsigned long)&rtl8192cu_fops},
8136 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8191, 0xff, 0xff, 0xff),
8137 	.driver_info = (unsigned long)&rtl8192cu_fops},
8138 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8170, 0xff, 0xff, 0xff),
8139 	.driver_info = (unsigned long)&rtl8192cu_fops},
8140 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8177, 0xff, 0xff, 0xff),
8141 	.driver_info = (unsigned long)&rtl8192cu_fops},
8142 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817a, 0xff, 0xff, 0xff),
8143 	.driver_info = (unsigned long)&rtl8192cu_fops},
8144 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817b, 0xff, 0xff, 0xff),
8145 	.driver_info = (unsigned long)&rtl8192cu_fops},
8146 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817d, 0xff, 0xff, 0xff),
8147 	.driver_info = (unsigned long)&rtl8192cu_fops},
8148 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817e, 0xff, 0xff, 0xff),
8149 	.driver_info = (unsigned long)&rtl8192cu_fops},
8150 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8186, 0xff, 0xff, 0xff),
8151 	.driver_info = (unsigned long)&rtl8192cu_fops},
8152 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818a, 0xff, 0xff, 0xff),
8153 	.driver_info = (unsigned long)&rtl8192cu_fops},
8154 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x317f, 0xff, 0xff, 0xff),
8155 	.driver_info = (unsigned long)&rtl8192cu_fops},
8156 {USB_DEVICE_AND_INTERFACE_INFO(0x1058, 0x0631, 0xff, 0xff, 0xff),
8157 	.driver_info = (unsigned long)&rtl8192cu_fops},
8158 {USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x094c, 0xff, 0xff, 0xff),
8159 	.driver_info = (unsigned long)&rtl8192cu_fops},
8160 {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1102, 0xff, 0xff, 0xff),
8161 	.driver_info = (unsigned long)&rtl8192cu_fops},
8162 {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x11f2, 0xff, 0xff, 0xff),
8163 	.driver_info = (unsigned long)&rtl8192cu_fops},
8164 {USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe033, 0xff, 0xff, 0xff),
8165 	.driver_info = (unsigned long)&rtl8192cu_fops},
8166 {USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8189, 0xff, 0xff, 0xff),
8167 	.driver_info = (unsigned long)&rtl8192cu_fops},
8168 {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9041, 0xff, 0xff, 0xff),
8169 	.driver_info = (unsigned long)&rtl8192cu_fops},
8170 {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9043, 0xff, 0xff, 0xff),
8171 	.driver_info = (unsigned long)&rtl8192cu_fops},
8172 {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ba, 0xff, 0xff, 0xff),
8173 	.driver_info = (unsigned long)&rtl8192cu_fops},
8174 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1e1e, 0xff, 0xff, 0xff),
8175 	.driver_info = (unsigned long)&rtl8192cu_fops},
8176 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x5088, 0xff, 0xff, 0xff),
8177 	.driver_info = (unsigned long)&rtl8192cu_fops},
8178 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0052, 0xff, 0xff, 0xff),
8179 	.driver_info = (unsigned long)&rtl8192cu_fops},
8180 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x005c, 0xff, 0xff, 0xff),
8181 	.driver_info = (unsigned long)&rtl8192cu_fops},
8182 {USB_DEVICE_AND_INTERFACE_INFO(0x0eb0, 0x9071, 0xff, 0xff, 0xff),
8183 	.driver_info = (unsigned long)&rtl8192cu_fops},
8184 {USB_DEVICE_AND_INTERFACE_INFO(0x103c, 0x1629, 0xff, 0xff, 0xff),
8185 	.driver_info = (unsigned long)&rtl8192cu_fops},
8186 {USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3357, 0xff, 0xff, 0xff),
8187 	.driver_info = (unsigned long)&rtl8192cu_fops},
8188 {USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3358, 0xff, 0xff, 0xff),
8189 	.driver_info = (unsigned long)&rtl8192cu_fops},
8190 {USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3359, 0xff, 0xff, 0xff),
8191 	.driver_info = (unsigned long)&rtl8192cu_fops},
8192 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330b, 0xff, 0xff, 0xff),
8193 	.driver_info = (unsigned long)&rtl8192cu_fops},
8194 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x4902, 0xff, 0xff, 0xff),
8195 	.driver_info = (unsigned long)&rtl8192cu_fops},
8196 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2a, 0xff, 0xff, 0xff),
8197 	.driver_info = (unsigned long)&rtl8192cu_fops},
8198 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2e, 0xff, 0xff, 0xff),
8199 	.driver_info = (unsigned long)&rtl8192cu_fops},
8200 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xed17, 0xff, 0xff, 0xff),
8201 	.driver_info = (unsigned long)&rtl8192cu_fops},
8202 {USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0090, 0xff, 0xff, 0xff),
8203 	.driver_info = (unsigned long)&rtl8192cu_fops},
8204 {USB_DEVICE_AND_INTERFACE_INFO(0x4856, 0x0091, 0xff, 0xff, 0xff),
8205 	.driver_info = (unsigned long)&rtl8192cu_fops},
8206 {USB_DEVICE_AND_INTERFACE_INFO(0x9846, 0x9041, 0xff, 0xff, 0xff),
8207 	.driver_info = (unsigned long)&rtl8192cu_fops},
8208 {USB_DEVICE_AND_INTERFACE_INFO(0xcdab, 0x8010, 0xff, 0xff, 0xff),
8209 	.driver_info = (unsigned long)&rtl8192cu_fops},
8210 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff7, 0xff, 0xff, 0xff),
8211 	.driver_info = (unsigned long)&rtl8192cu_fops},
8212 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff9, 0xff, 0xff, 0xff),
8213 	.driver_info = (unsigned long)&rtl8192cu_fops},
8214 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffa, 0xff, 0xff, 0xff),
8215 	.driver_info = (unsigned long)&rtl8192cu_fops},
8216 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff8, 0xff, 0xff, 0xff),
8217 	.driver_info = (unsigned long)&rtl8192cu_fops},
8218 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffb, 0xff, 0xff, 0xff),
8219 	.driver_info = (unsigned long)&rtl8192cu_fops},
8220 {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffc, 0xff, 0xff, 0xff),
8221 	.driver_info = (unsigned long)&rtl8192cu_fops},
8222 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x1201, 0xff, 0xff, 0xff),
8223 	.driver_info = (unsigned long)&rtl8192cu_fops},
8224 /* Currently untested 8192 series devices */
8225 {USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x0950, 0xff, 0xff, 0xff),
8226 	.driver_info = (unsigned long)&rtl8192cu_fops},
8227 {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2102, 0xff, 0xff, 0xff),
8228 	.driver_info = (unsigned long)&rtl8192cu_fops},
8229 {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2103, 0xff, 0xff, 0xff),
8230 	.driver_info = (unsigned long)&rtl8192cu_fops},
8231 {USB_DEVICE_AND_INTERFACE_INFO(0x0586, 0x341f, 0xff, 0xff, 0xff),
8232 	.driver_info = (unsigned long)&rtl8192cu_fops},
8233 {USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe033, 0xff, 0xff, 0xff),
8234 	.driver_info = (unsigned long)&rtl8192cu_fops},
8235 {USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe035, 0xff, 0xff, 0xff),
8236 	.driver_info = (unsigned long)&rtl8192cu_fops},
8237 {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ab, 0xff, 0xff, 0xff),
8238 	.driver_info = (unsigned long)&rtl8192cu_fops},
8239 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0061, 0xff, 0xff, 0xff),
8240 	.driver_info = (unsigned long)&rtl8192cu_fops},
8241 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0070, 0xff, 0xff, 0xff),
8242 	.driver_info = (unsigned long)&rtl8192cu_fops},
8243 {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0077, 0xff, 0xff, 0xff),
8244 	.driver_info = (unsigned long)&rtl8192cu_fops},
8245 {USB_DEVICE_AND_INTERFACE_INFO(0x0789, 0x016d, 0xff, 0xff, 0xff),
8246 	.driver_info = (unsigned long)&rtl8192cu_fops},
8247 {USB_DEVICE_AND_INTERFACE_INFO(0x07aa, 0x0056, 0xff, 0xff, 0xff),
8248 	.driver_info = (unsigned long)&rtl8192cu_fops},
8249 {USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8178, 0xff, 0xff, 0xff),
8250 	.driver_info = (unsigned long)&rtl8192cu_fops},
8251 {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9021, 0xff, 0xff, 0xff),
8252 	.driver_info = (unsigned long)&rtl8192cu_fops},
8253 {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0xf001, 0xff, 0xff, 0xff),
8254 	.driver_info = (unsigned long)&rtl8192cu_fops},
8255 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x2e2e, 0xff, 0xff, 0xff),
8256 	.driver_info = (unsigned long)&rtl8192cu_fops},
8257 {USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0019, 0xff, 0xff, 0xff),
8258 	.driver_info = (unsigned long)&rtl8192cu_fops},
8259 {USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0020, 0xff, 0xff, 0xff),
8260 	.driver_info = (unsigned long)&rtl8192cu_fops},
8261 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3307, 0xff, 0xff, 0xff),
8262 	.driver_info = (unsigned long)&rtl8192cu_fops},
8263 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3309, 0xff, 0xff, 0xff),
8264 	.driver_info = (unsigned long)&rtl8192cu_fops},
8265 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330a, 0xff, 0xff, 0xff),
8266 	.driver_info = (unsigned long)&rtl8192cu_fops},
8267 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330d, 0xff, 0xff, 0xff),
8268 	.driver_info = (unsigned long)&rtl8192cu_fops},
8269 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2b, 0xff, 0xff, 0xff),
8270 	.driver_info = (unsigned long)&rtl8192cu_fops},
8271 {USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x624d, 0xff, 0xff, 0xff),
8272 	.driver_info = (unsigned long)&rtl8192cu_fops},
8273 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0100, 0xff, 0xff, 0xff),
8274 	.driver_info = (unsigned long)&rtl8192cu_fops},
8275 {USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0091, 0xff, 0xff, 0xff),
8276 	.driver_info = (unsigned long)&rtl8192cu_fops},
8277 {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7822, 0xff, 0xff, 0xff),
8278 	.driver_info = (unsigned long)&rtl8192cu_fops},
8279 /* found in rtl8192eu vendor driver */
8280 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0107, 0xff, 0xff, 0xff),
8281 	.driver_info = (unsigned long)&rtl8192eu_fops},
8282 {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab33, 0xff, 0xff, 0xff),
8283 	.driver_info = (unsigned long)&rtl8192eu_fops},
8284 {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818c, 0xff, 0xff, 0xff),
8285 	.driver_info = (unsigned long)&rtl8192eu_fops},
8286 /* D-Link DWA-131 rev C1 */
8287 {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3312, 0xff, 0xff, 0xff),
8288 	.driver_info = (unsigned long)&rtl8192eu_fops},
8289 /* TP-Link TL-WN8200ND V2 */
8290 {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0126, 0xff, 0xff, 0xff),
8291 	.driver_info = (unsigned long)&rtl8192eu_fops},
8292 /* Mercusys MW300UM */
8293 {USB_DEVICE_AND_INTERFACE_INFO(0x2c4e, 0x0100, 0xff, 0xff, 0xff),
8294 	.driver_info = (unsigned long)&rtl8192eu_fops},
8295 /* Mercusys MW300UH */
8296 {USB_DEVICE_AND_INTERFACE_INFO(0x2c4e, 0x0104, 0xff, 0xff, 0xff),
8297 	.driver_info = (unsigned long)&rtl8192eu_fops},
8298 #endif
8299 { }
8300 };
8301 
8302 static struct usb_driver rtl8xxxu_driver = {
8303 	.name = DRIVER_NAME,
8304 	.probe = rtl8xxxu_probe,
8305 	.disconnect = rtl8xxxu_disconnect,
8306 	.id_table = dev_table,
8307 	.no_dynamic_id = 1,
8308 	.disable_hub_initiated_lpm = 1,
8309 };
8310 
8311 MODULE_DEVICE_TABLE(usb, dev_table);
8312 
8313 module_usb_driver(rtl8xxxu_driver);
8314