Debian Codegraph @

drivers/net/wireless/realtek/rtl8xxxu/core.c

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